Home | History | Annotate | Line # | Download | only in dev
am79c950.c revision 1.24.4.1
      1  1.24.4.1       jym /*	$NetBSD: am79c950.c,v 1.24.4.1 2009/05/13 17:18:01 jym Exp $	*/
      2       1.1    tsubai 
      3       1.1    tsubai /*-
      4       1.1    tsubai  * Copyright (c) 1997 David Huang <khym (at) bga.com>
      5       1.1    tsubai  * All rights reserved.
      6       1.1    tsubai  *
      7       1.1    tsubai  * Portions of this code are based on code by Denton Gentry <denny1 (at) home.com>,
      8       1.1    tsubai  * Charles M. Hannum, Yanagisawa Takeshi <yanagisw (at) aa.ap.titech.ac.jp>, and
      9       1.1    tsubai  * Jason R. Thorpe.
     10       1.1    tsubai  *
     11       1.1    tsubai  * Redistribution and use in source and binary forms, with or without
     12       1.1    tsubai  * modification, are permitted provided that the following conditions
     13       1.1    tsubai  * are met:
     14       1.1    tsubai  * 1. Redistributions of source code must retain the above copyright
     15       1.1    tsubai  *    notice, this list of conditions and the following disclaimer.
     16       1.1    tsubai  * 2. The name of the author may not be used to endorse or promote products
     17       1.1    tsubai  *    derived from this software without specific prior written permission
     18       1.1    tsubai  *
     19       1.1    tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20       1.1    tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21       1.1    tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22       1.1    tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23       1.1    tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24       1.1    tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25       1.1    tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26       1.1    tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27       1.1    tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28       1.1    tsubai  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29       1.1    tsubai  *
     30       1.1    tsubai  */
     31       1.1    tsubai 
     32       1.1    tsubai /*
     33       1.1    tsubai  * Driver for the AMD Am79C940 (MACE) ethernet chip, used for onboard
     34       1.1    tsubai  * ethernet on the Centris/Quadra 660av and Quadra 840av.
     35       1.1    tsubai  */
     36      1.15     lukem 
     37      1.15     lukem #include <sys/cdefs.h>
     38  1.24.4.1       jym __KERNEL_RCSID(0, "$NetBSD: am79c950.c,v 1.24.4.1 2009/05/13 17:18:01 jym Exp $");
     39      1.15     lukem 
     40       1.2  jonathan #include "opt_inet.h"
     41       1.1    tsubai 
     42       1.1    tsubai #include <sys/param.h>
     43       1.1    tsubai #include <sys/systm.h>
     44       1.1    tsubai #include <sys/mbuf.h>
     45       1.1    tsubai #include <sys/buf.h>
     46       1.1    tsubai #include <sys/protosw.h>
     47       1.1    tsubai #include <sys/socket.h>
     48       1.1    tsubai #include <sys/syslog.h>
     49       1.1    tsubai #include <sys/ioctl.h>
     50       1.1    tsubai #include <sys/errno.h>
     51       1.1    tsubai #include <sys/device.h>
     52       1.1    tsubai 
     53      1.13   thorpej #include <uvm/uvm_extern.h>
     54      1.13   thorpej 
     55       1.1    tsubai #include <net/if.h>
     56       1.1    tsubai #include <net/if_dl.h>
     57       1.1    tsubai #include <net/if_ether.h>
     58       1.1    tsubai #include <net/if_media.h>
     59       1.1    tsubai 
     60       1.1    tsubai #ifdef INET
     61       1.1    tsubai #include <netinet/in.h>
     62       1.1    tsubai #include <netinet/if_inarp.h>
     63       1.1    tsubai #include <netinet/in_systm.h>
     64       1.1    tsubai #include <netinet/in_var.h>
     65       1.1    tsubai #include <netinet/ip.h>
     66       1.1    tsubai #endif
     67       1.1    tsubai 
     68       1.1    tsubai #include "bpfilter.h"
     69       1.1    tsubai #if NBPFILTER > 0
     70       1.1    tsubai #include <net/bpf.h>
     71       1.1    tsubai #include <net/bpfdesc.h>
     72       1.1    tsubai #endif
     73       1.1    tsubai 
     74       1.1    tsubai #include <machine/bus.h>
     75       1.1    tsubai 
     76       1.1    tsubai #include <macppc/dev/am79c950reg.h>
     77       1.1    tsubai #include <macppc/dev/if_mcvar.h>
     78       1.1    tsubai 
     79  1.24.4.1       jym hide void	mcwatchdog(struct ifnet *);
     80  1.24.4.1       jym hide int	mcinit(struct mc_softc *sc);
     81  1.24.4.1       jym hide int	mcstop(struct mc_softc *sc);
     82  1.24.4.1       jym hide int	mcioctl(struct ifnet *ifp, u_long cmd, void *data);
     83  1.24.4.1       jym hide void	mcstart(struct ifnet *ifp);
     84  1.24.4.1       jym hide void	mcreset(struct mc_softc *sc);
     85  1.24.4.1       jym 
     86  1.24.4.1       jym integrate u_int	maceput(struct mc_softc *sc, struct mbuf *m0);
     87  1.24.4.1       jym integrate void	mc_tint(struct mc_softc *sc);
     88  1.24.4.1       jym integrate void	mace_read(struct mc_softc *, uint8_t *, int);
     89  1.24.4.1       jym integrate struct mbuf *mace_get(struct mc_softc *, uint8_t *, int);
     90  1.24.4.1       jym static void mace_calcladrf(struct ethercom *ac, u_int8_t *af);
     91  1.24.4.1       jym static inline u_int16_t ether_cmp(void *, void *);
     92  1.24.4.1       jym static int mc_mediachange(struct ifnet *);
     93  1.24.4.1       jym static void mc_mediastatus(struct ifnet *, struct ifmediareq *);
     94       1.1    tsubai 
     95       1.1    tsubai /*
     96       1.1    tsubai  * Compare two Ether/802 addresses for equality, inlined and
     97      1.11       wiz  * unrolled for speed.  Use this like memcmp().
     98       1.1    tsubai  *
     99       1.1    tsubai  * XXX: Add <machine/inlines.h> for stuff like this?
    100       1.1    tsubai  * XXX: or maybe add it to libkern.h instead?
    101       1.1    tsubai  *
    102       1.1    tsubai  * "I'd love to have an inline assembler version of this."
    103       1.1    tsubai  * XXX: Who wanted that? mycroft?  I wrote one, but this
    104       1.1    tsubai  * version in C is as good as hand-coded assembly. -gwr
    105       1.1    tsubai  *
    106       1.1    tsubai  * Please do NOT tweak this without looking at the actual
    107       1.1    tsubai  * assembly code generated before and after your tweaks!
    108       1.1    tsubai  */
    109       1.1    tsubai static inline u_int16_t
    110  1.24.4.1       jym ether_cmp(void *one, void *two)
    111       1.1    tsubai {
    112       1.1    tsubai 	register u_int16_t *a = (u_short *) one;
    113       1.1    tsubai 	register u_int16_t *b = (u_short *) two;
    114       1.1    tsubai 	register u_int16_t diff;
    115       1.1    tsubai 
    116       1.1    tsubai #ifdef	m68k
    117       1.1    tsubai 	/*
    118       1.1    tsubai 	 * The post-increment-pointer form produces the best
    119       1.1    tsubai 	 * machine code for m68k.  This was carefully tuned
    120       1.1    tsubai 	 * so it compiles to just 8 short (2-byte) op-codes!
    121       1.1    tsubai 	 */
    122       1.1    tsubai 	diff  = *a++ - *b++;
    123       1.1    tsubai 	diff |= *a++ - *b++;
    124       1.1    tsubai 	diff |= *a++ - *b++;
    125       1.1    tsubai #else
    126       1.1    tsubai 	/*
    127       1.1    tsubai 	 * Most modern CPUs do better with a single expresion.
    128       1.1    tsubai 	 * Note that short-cut evaluation is NOT helpful here,
    129       1.1    tsubai 	 * because it just makes the code longer, not faster!
    130       1.1    tsubai 	 */
    131       1.1    tsubai 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
    132       1.1    tsubai #endif
    133       1.1    tsubai 
    134       1.1    tsubai 	return (diff);
    135       1.1    tsubai }
    136       1.1    tsubai 
    137       1.1    tsubai #define ETHER_CMP	ether_cmp
    138       1.1    tsubai 
    139       1.1    tsubai /*
    140       1.1    tsubai  * Interface exists: make available by filling in network interface
    141       1.1    tsubai  * record.  System will initialize the interface when it is ready
    142       1.1    tsubai  * to accept packets.
    143       1.1    tsubai  */
    144       1.1    tsubai int
    145  1.24.4.1       jym mcsetup(struct mc_softc *sc, u_int8_t *lladdr)
    146       1.1    tsubai {
    147       1.1    tsubai 	struct ifnet *ifp = &sc->sc_if;
    148       1.1    tsubai 
    149       1.1    tsubai 	/* reset the chip and disable all interrupts */
    150       1.1    tsubai 	NIC_PUT(sc, MACE_BIUCC, SWRST);
    151       1.1    tsubai 	DELAY(100);
    152       1.1    tsubai 	NIC_PUT(sc, MACE_IMR, ~0);
    153       1.1    tsubai 
    154      1.11       wiz 	memcpy(sc->sc_enaddr, lladdr, ETHER_ADDR_LEN);
    155       1.1    tsubai 	printf(": address %s\n", ether_sprintf(lladdr));
    156       1.1    tsubai 
    157      1.11       wiz 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    158       1.1    tsubai 	ifp->if_softc = sc;
    159       1.1    tsubai 	ifp->if_ioctl = mcioctl;
    160       1.1    tsubai 	ifp->if_start = mcstart;
    161       1.1    tsubai 	ifp->if_flags =
    162       1.1    tsubai 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    163       1.1    tsubai 	ifp->if_watchdog = mcwatchdog;
    164       1.1    tsubai 
    165       1.1    tsubai 	/* initialize ifmedia structures */
    166       1.1    tsubai 	ifmedia_init(&sc->sc_media, 0, mc_mediachange, mc_mediastatus);
    167       1.1    tsubai 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    168       1.1    tsubai 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    169       1.1    tsubai 
    170       1.1    tsubai 	if_attach(ifp);
    171       1.1    tsubai 	ether_ifattach(ifp, lladdr);
    172       1.1    tsubai 
    173       1.1    tsubai 	return (0);
    174       1.1    tsubai }
    175       1.1    tsubai 
    176       1.1    tsubai hide int
    177  1.24.4.1       jym mcioctl(struct ifnet *ifp, u_long cmd, void *data)
    178       1.1    tsubai {
    179       1.1    tsubai 	struct mc_softc *sc = ifp->if_softc;
    180       1.1    tsubai 	struct ifaddr *ifa;
    181       1.1    tsubai 	struct ifreq *ifr;
    182       1.1    tsubai 
    183       1.1    tsubai 	int	s = splnet(), err = 0;
    184       1.1    tsubai 
    185       1.1    tsubai 	switch (cmd) {
    186       1.1    tsubai 
    187      1.24    dyoung 	case SIOCINITIFADDR:
    188       1.1    tsubai 		ifa = (struct ifaddr *)data;
    189       1.1    tsubai 		ifp->if_flags |= IFF_UP;
    190      1.24    dyoung 		mcinit(sc);
    191       1.1    tsubai 		switch (ifa->ifa_addr->sa_family) {
    192       1.1    tsubai #ifdef INET
    193       1.1    tsubai 		case AF_INET:
    194       1.1    tsubai 			arp_ifinit(ifp, ifa);
    195       1.1    tsubai 			break;
    196       1.1    tsubai #endif
    197       1.1    tsubai 		default:
    198       1.1    tsubai 			break;
    199       1.1    tsubai 		}
    200       1.1    tsubai 		break;
    201       1.1    tsubai 
    202       1.1    tsubai 	case SIOCSIFFLAGS:
    203      1.24    dyoung 		if ((err = ifioctl_common(ifp, cmd, data)) != 0)
    204      1.24    dyoung 			break;
    205      1.24    dyoung 		/* XXX see the comment in ed_ioctl() about code re-use */
    206       1.1    tsubai 		if ((ifp->if_flags & IFF_UP) == 0 &&
    207       1.1    tsubai 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    208       1.1    tsubai 			/*
    209       1.1    tsubai 			 * If interface is marked down and it is running,
    210       1.1    tsubai 			 * then stop it.
    211       1.1    tsubai 			 */
    212       1.1    tsubai 			mcstop(sc);
    213       1.1    tsubai 			ifp->if_flags &= ~IFF_RUNNING;
    214       1.1    tsubai 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    215       1.1    tsubai 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    216       1.1    tsubai 			/*
    217       1.1    tsubai 			 * If interface is marked up and it is stopped,
    218       1.1    tsubai 			 * then start it.
    219       1.1    tsubai 			 */
    220       1.1    tsubai 			(void)mcinit(sc);
    221       1.1    tsubai 		} else {
    222       1.1    tsubai 			/*
    223       1.1    tsubai 			 * reset the interface to pick up any other changes
    224       1.1    tsubai 			 * in flags
    225       1.1    tsubai 			 */
    226       1.1    tsubai 			mcreset(sc);
    227       1.1    tsubai 			mcstart(ifp);
    228       1.1    tsubai 		}
    229       1.1    tsubai 		break;
    230       1.1    tsubai 
    231       1.1    tsubai 	case SIOCADDMULTI:
    232       1.1    tsubai 	case SIOCDELMULTI:
    233      1.22    dyoung 		if ((err = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
    234       1.1    tsubai 			/*
    235       1.1    tsubai 			 * Multicast list has changed; set the hardware
    236       1.1    tsubai 			 * filter accordingly. But remember UP flag!
    237       1.1    tsubai 			 */
    238      1.16   thorpej 			if (ifp->if_flags & IFF_RUNNING)
    239      1.16   thorpej 				mcreset(sc);
    240       1.1    tsubai 			err = 0;
    241       1.1    tsubai 		}
    242       1.1    tsubai 		break;
    243       1.1    tsubai 
    244       1.1    tsubai 	case SIOCGIFMEDIA:
    245       1.1    tsubai 	case SIOCSIFMEDIA:
    246       1.1    tsubai 		ifr = (struct ifreq *) data;
    247       1.1    tsubai 		err = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    248       1.1    tsubai 		break;
    249       1.1    tsubai 
    250       1.1    tsubai 	default:
    251      1.24    dyoung 		err = ether_ioctl(ifp, cmd, data);
    252      1.24    dyoung 		break;
    253       1.1    tsubai 	}
    254       1.1    tsubai 	splx(s);
    255       1.1    tsubai 	return (err);
    256       1.1    tsubai }
    257       1.1    tsubai 
    258       1.1    tsubai /*
    259       1.1    tsubai  * Encapsulate a packet of type family for the local net.
    260       1.1    tsubai  */
    261       1.1    tsubai hide void
    262  1.24.4.1       jym mcstart(struct ifnet *ifp)
    263       1.1    tsubai {
    264       1.1    tsubai 	struct mc_softc	*sc = ifp->if_softc;
    265       1.1    tsubai 	struct mbuf	*m;
    266       1.1    tsubai 
    267       1.1    tsubai 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    268       1.1    tsubai 		return;
    269       1.1    tsubai 
    270       1.1    tsubai 	while (1) {
    271       1.1    tsubai 		if (ifp->if_flags & IFF_OACTIVE)
    272       1.1    tsubai 			return;
    273       1.1    tsubai 
    274       1.1    tsubai 		IF_DEQUEUE(&ifp->if_snd, m);
    275       1.1    tsubai 		if (m == 0)
    276       1.1    tsubai 			return;
    277       1.1    tsubai 
    278       1.1    tsubai #if NBPFILTER > 0
    279       1.1    tsubai 		/*
    280       1.1    tsubai 		 * If bpf is listening on this interface, let it
    281       1.1    tsubai 		 * see the packet before we commit it to the wire.
    282       1.1    tsubai 		 */
    283       1.1    tsubai 		if (ifp->if_bpf)
    284       1.1    tsubai 			bpf_mtap(ifp->if_bpf, m);
    285       1.1    tsubai #endif
    286       1.1    tsubai 
    287       1.1    tsubai 		/*
    288       1.1    tsubai 		 * Copy the mbuf chain into the transmit buffer.
    289       1.1    tsubai 		 */
    290       1.1    tsubai 		ifp->if_flags |= IFF_OACTIVE;
    291       1.1    tsubai 		maceput(sc, m);
    292       1.1    tsubai 
    293       1.1    tsubai 		ifp->if_opackets++;		/* # of pkts */
    294       1.1    tsubai 	}
    295       1.1    tsubai }
    296       1.1    tsubai 
    297       1.1    tsubai /*
    298       1.1    tsubai  * reset and restart the MACE.  Called in case of fatal
    299       1.1    tsubai  * hardware/software errors.
    300       1.1    tsubai  */
    301       1.1    tsubai hide void
    302  1.24.4.1       jym mcreset(struct mc_softc *sc)
    303       1.1    tsubai {
    304       1.1    tsubai 	mcstop(sc);
    305       1.1    tsubai 	mcinit(sc);
    306       1.1    tsubai }
    307       1.1    tsubai 
    308       1.1    tsubai hide int
    309  1.24.4.1       jym mcinit(struct mc_softc *sc)
    310       1.1    tsubai {
    311       1.1    tsubai 	int s;
    312       1.1    tsubai 	u_int8_t maccc, ladrf[8];
    313       1.1    tsubai 
    314       1.1    tsubai 	if (sc->sc_if.if_flags & IFF_RUNNING)
    315       1.1    tsubai 		/* already running */
    316       1.1    tsubai 		return (0);
    317       1.1    tsubai 
    318       1.1    tsubai 	s = splnet();
    319       1.1    tsubai 
    320       1.1    tsubai 	NIC_PUT(sc, MACE_BIUCC, sc->sc_biucc);
    321       1.1    tsubai 	NIC_PUT(sc, MACE_FIFOCC, sc->sc_fifocc);
    322       1.1    tsubai 	NIC_PUT(sc, MACE_IMR, ~0); /* disable all interrupts */
    323       1.1    tsubai 	NIC_PUT(sc, MACE_PLSCC, sc->sc_plscc);
    324       1.1    tsubai 
    325       1.1    tsubai 	NIC_PUT(sc, MACE_UTR, RTRD); /* disable reserved test registers */
    326       1.1    tsubai 
    327       1.1    tsubai 	/* set MAC address */
    328       1.1    tsubai 	NIC_PUT(sc, MACE_IAC, ADDRCHG);
    329       1.1    tsubai 	while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
    330       1.1    tsubai 		;
    331       1.1    tsubai 	NIC_PUT(sc, MACE_IAC, PHYADDR);
    332       1.1    tsubai 	bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_PADR),
    333       1.1    tsubai 	    sc->sc_enaddr, ETHER_ADDR_LEN);
    334       1.1    tsubai 
    335       1.1    tsubai 	/* set logical address filter */
    336       1.1    tsubai 	mace_calcladrf(&sc->sc_ethercom, ladrf);
    337       1.1    tsubai 
    338       1.1    tsubai 	NIC_PUT(sc, MACE_IAC, ADDRCHG);
    339       1.1    tsubai 	while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
    340       1.1    tsubai 		;
    341       1.1    tsubai 	NIC_PUT(sc, MACE_IAC, LOGADDR);
    342       1.1    tsubai 	bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_LADRF),
    343       1.1    tsubai 	    ladrf, 8);
    344       1.1    tsubai 
    345       1.1    tsubai 	NIC_PUT(sc, MACE_XMTFC, APADXMT);
    346       1.1    tsubai 	/*
    347       1.1    tsubai 	 * No need to autostrip padding on receive... Ethernet frames
    348       1.1    tsubai 	 * don't have a length field, unlike 802.3 frames, so the MACE
    349       1.1    tsubai 	 * can't figure out the length of the packet anyways.
    350       1.1    tsubai 	 */
    351       1.1    tsubai 	NIC_PUT(sc, MACE_RCVFC, 0);
    352       1.1    tsubai 
    353       1.1    tsubai 	maccc = ENXMT | ENRCV;
    354       1.1    tsubai 	if (sc->sc_if.if_flags & IFF_PROMISC)
    355       1.1    tsubai 		maccc |= PROM;
    356       1.1    tsubai 
    357       1.1    tsubai 	NIC_PUT(sc, MACE_MACCC, maccc);
    358       1.1    tsubai 
    359       1.1    tsubai 	if (sc->sc_bus_init)
    360       1.1    tsubai 		(*sc->sc_bus_init)(sc);
    361       1.1    tsubai 
    362       1.1    tsubai 	/*
    363       1.1    tsubai 	 * Enable all interrupts except receive, since we use the DMA
    364       1.1    tsubai 	 * completion interrupt for that.
    365       1.1    tsubai 	 */
    366       1.1    tsubai 	NIC_PUT(sc, MACE_IMR, RCVINTM);
    367       1.1    tsubai 
    368       1.1    tsubai 	/* flag interface as "running" */
    369       1.1    tsubai 	sc->sc_if.if_flags |= IFF_RUNNING;
    370       1.1    tsubai 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    371       1.1    tsubai 
    372       1.1    tsubai 	splx(s);
    373       1.1    tsubai 	return (0);
    374       1.1    tsubai }
    375       1.1    tsubai 
    376       1.1    tsubai /*
    377       1.1    tsubai  * close down an interface and free its buffers
    378       1.1    tsubai  * Called on final close of device, or if mcinit() fails
    379       1.1    tsubai  * part way through.
    380       1.1    tsubai  */
    381       1.1    tsubai hide int
    382  1.24.4.1       jym mcstop(struct mc_softc *sc)
    383       1.1    tsubai {
    384       1.1    tsubai 	int	s = splnet();
    385       1.1    tsubai 
    386       1.1    tsubai 	NIC_PUT(sc, MACE_BIUCC, SWRST);
    387       1.1    tsubai 	DELAY(100);
    388       1.1    tsubai 
    389       1.1    tsubai 	sc->sc_if.if_timer = 0;
    390      1.14     bjh21 	sc->sc_if.if_flags &= ~IFF_RUNNING;
    391       1.1    tsubai 
    392       1.1    tsubai 	splx(s);
    393       1.1    tsubai 	return (0);
    394       1.1    tsubai }
    395       1.1    tsubai 
    396       1.1    tsubai /*
    397       1.1    tsubai  * Called if any Tx packets remain unsent after 5 seconds,
    398       1.1    tsubai  * In all cases we just reset the chip, and any retransmission
    399       1.1    tsubai  * will be handled by higher level protocol timeouts.
    400       1.1    tsubai  */
    401       1.1    tsubai hide void
    402  1.24.4.1       jym mcwatchdog(struct ifnet *ifp)
    403       1.1    tsubai {
    404       1.1    tsubai 	struct mc_softc *sc = ifp->if_softc;
    405       1.1    tsubai 
    406       1.1    tsubai 	printf("mcwatchdog: resetting chip\n");
    407       1.1    tsubai 	mcreset(sc);
    408       1.1    tsubai }
    409       1.1    tsubai 
    410       1.1    tsubai /*
    411       1.1    tsubai  * stuff packet into MACE (at splnet)
    412       1.1    tsubai  */
    413       1.1    tsubai integrate u_int
    414  1.24.4.1       jym maceput(struct mc_softc *sc, struct mbuf *m)
    415       1.1    tsubai {
    416       1.1    tsubai 	struct mbuf *n;
    417       1.1    tsubai 	u_int len, totlen = 0;
    418       1.1    tsubai 	u_char *buff;
    419       1.1    tsubai 
    420       1.1    tsubai 	buff = sc->sc_txbuf;
    421       1.1    tsubai 
    422       1.1    tsubai 	for (; m; m = n) {
    423       1.1    tsubai 		u_char *data = mtod(m, u_char *);
    424       1.1    tsubai 		len = m->m_len;
    425       1.1    tsubai 		totlen += len;
    426      1.11       wiz 		memcpy(buff, data, len);
    427       1.1    tsubai 		buff += len;
    428       1.1    tsubai 		MFREE(m, n);
    429       1.1    tsubai 	}
    430       1.1    tsubai 
    431      1.13   thorpej 	if (totlen > PAGE_SIZE)
    432       1.1    tsubai 		panic("%s: maceput: packet overflow", sc->sc_dev.dv_xname);
    433       1.1    tsubai 
    434       1.1    tsubai #if 0
    435       1.1    tsubai 	if (totlen < ETHERMIN + sizeof(struct ether_header)) {
    436       1.1    tsubai 		int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
    437      1.11       wiz 		memset(sc->sc_txbuf + totlen, 0, pad);
    438       1.1    tsubai 		totlen = ETHERMIN + sizeof(struct ether_header);
    439       1.1    tsubai 	}
    440       1.1    tsubai #endif
    441       1.1    tsubai 
    442       1.1    tsubai 	(*sc->sc_putpacket)(sc, totlen);
    443       1.1    tsubai 
    444       1.1    tsubai 	sc->sc_if.if_timer = 5;	/* 5 seconds to watch for failing to transmit */
    445       1.1    tsubai 	return (totlen);
    446       1.1    tsubai }
    447       1.1    tsubai 
    448       1.6    tsubai int
    449  1.24.4.1       jym mcintr(void *arg)
    450       1.1    tsubai {
    451       1.1    tsubai 	struct mc_softc *sc = arg;
    452       1.1    tsubai 	u_int8_t ir;
    453       1.1    tsubai 
    454       1.1    tsubai 	ir = NIC_GET(sc, MACE_IR) & ~NIC_GET(sc, MACE_IMR);
    455       1.6    tsubai 	if (ir == 0)
    456       1.6    tsubai 		return 0;
    457       1.6    tsubai 
    458       1.1    tsubai 	if (ir & JAB) {
    459       1.1    tsubai #ifdef MCDEBUG
    460       1.1    tsubai 		printf("%s: jabber error\n", sc->sc_dev.dv_xname);
    461       1.1    tsubai #endif
    462       1.1    tsubai 		sc->sc_if.if_oerrors++;
    463       1.1    tsubai 	}
    464       1.1    tsubai 
    465       1.1    tsubai 	if (ir & BABL) {
    466       1.1    tsubai #ifdef MCDEBUG
    467       1.1    tsubai 		printf("%s: babble\n", sc->sc_dev.dv_xname);
    468       1.1    tsubai #endif
    469       1.1    tsubai 		sc->sc_if.if_oerrors++;
    470       1.1    tsubai 	}
    471       1.1    tsubai 
    472       1.1    tsubai 	if (ir & CERR) {
    473       1.1    tsubai 		printf("%s: collision error\n", sc->sc_dev.dv_xname);
    474       1.1    tsubai 		sc->sc_if.if_collisions++;
    475       1.1    tsubai 	}
    476       1.1    tsubai 
    477       1.1    tsubai 	/*
    478       1.1    tsubai 	 * Pretend we have carrier; if we don't this will be cleared
    479       1.1    tsubai 	 * shortly.
    480       1.1    tsubai 	 */
    481       1.1    tsubai 	sc->sc_havecarrier = 1;
    482       1.1    tsubai 
    483       1.1    tsubai 	if (ir & XMTINT)
    484       1.1    tsubai 		mc_tint(sc);
    485       1.1    tsubai 
    486       1.1    tsubai 	if (ir & RCVINT)
    487       1.1    tsubai 		mc_rint(sc);
    488       1.6    tsubai 
    489       1.6    tsubai 	return 1;
    490       1.1    tsubai }
    491       1.1    tsubai 
    492       1.1    tsubai integrate void
    493  1.24.4.1       jym mc_tint(struct mc_softc *sc)
    494       1.1    tsubai {
    495       1.1    tsubai 	u_int8_t xmtrc, xmtfs;
    496       1.1    tsubai 
    497       1.1    tsubai 	xmtrc = NIC_GET(sc, MACE_XMTRC);
    498       1.1    tsubai 	xmtfs = NIC_GET(sc, MACE_XMTFS);
    499       1.1    tsubai 
    500       1.1    tsubai 	if ((xmtfs & XMTSV) == 0)
    501       1.1    tsubai 		return;
    502       1.1    tsubai 
    503       1.1    tsubai 	if (xmtfs & UFLO) {
    504       1.1    tsubai 		printf("%s: underflow\n", sc->sc_dev.dv_xname);
    505       1.1    tsubai 		mcreset(sc);
    506       1.1    tsubai 		return;
    507       1.1    tsubai 	}
    508       1.1    tsubai 
    509       1.1    tsubai 	if (xmtfs & LCOL) {
    510       1.1    tsubai 		printf("%s: late collision\n", sc->sc_dev.dv_xname);
    511       1.1    tsubai 		sc->sc_if.if_oerrors++;
    512       1.1    tsubai 		sc->sc_if.if_collisions++;
    513       1.1    tsubai 	}
    514       1.1    tsubai 
    515       1.1    tsubai 	if (xmtfs & MORE)
    516       1.1    tsubai 		/* Real number is unknown. */
    517       1.1    tsubai 		sc->sc_if.if_collisions += 2;
    518       1.1    tsubai 	else if (xmtfs & ONE)
    519       1.1    tsubai 		sc->sc_if.if_collisions++;
    520       1.1    tsubai 	else if (xmtfs & RTRY) {
    521       1.1    tsubai 		sc->sc_if.if_collisions += 16;
    522       1.1    tsubai 		sc->sc_if.if_oerrors++;
    523       1.1    tsubai 	}
    524       1.1    tsubai 
    525       1.1    tsubai 	if (xmtfs & LCAR) {
    526       1.1    tsubai 		sc->sc_havecarrier = 0;
    527       1.1    tsubai 		printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
    528       1.1    tsubai 		sc->sc_if.if_oerrors++;
    529       1.1    tsubai 	}
    530       1.1    tsubai 
    531       1.1    tsubai 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    532       1.1    tsubai 	sc->sc_if.if_timer = 0;
    533       1.1    tsubai 	mcstart(&sc->sc_if);
    534       1.1    tsubai }
    535       1.1    tsubai 
    536       1.1    tsubai void
    537  1.24.4.1       jym mc_rint(struct mc_softc *sc)
    538       1.1    tsubai {
    539       1.1    tsubai #define	rxf	sc->sc_rxframe
    540       1.1    tsubai 	u_int len;
    541       1.1    tsubai 
    542       1.1    tsubai 	len = (rxf.rx_rcvcnt | ((rxf.rx_rcvsts & 0xf) << 8)) - 4;
    543       1.1    tsubai 
    544       1.1    tsubai #ifdef MCDEBUG
    545       1.1    tsubai 	if (rxf.rx_rcvsts & 0xf0)
    546       1.1    tsubai 		printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
    547       1.1    tsubai 		    sc->sc_dev.dv_xname, rxf.rx_rcvcnt, rxf.rx_rcvsts,
    548       1.1    tsubai 		    rxf.rx_rntpc, rxf.rx_rcvcc);
    549       1.1    tsubai #endif
    550       1.1    tsubai 
    551       1.1    tsubai 	if (rxf.rx_rcvsts & OFLO) {
    552       1.1    tsubai 		printf("%s: receive FIFO overflow\n", sc->sc_dev.dv_xname);
    553       1.1    tsubai 		sc->sc_if.if_ierrors++;
    554       1.1    tsubai 		return;
    555       1.1    tsubai 	}
    556       1.1    tsubai 
    557       1.1    tsubai 	if (rxf.rx_rcvsts & CLSN)
    558       1.1    tsubai 		sc->sc_if.if_collisions++;
    559       1.1    tsubai 
    560       1.1    tsubai 	if (rxf.rx_rcvsts & FRAM) {
    561       1.1    tsubai #ifdef MCDEBUG
    562       1.1    tsubai 		printf("%s: framing error\n", sc->sc_dev.dv_xname);
    563       1.1    tsubai #endif
    564       1.1    tsubai 		sc->sc_if.if_ierrors++;
    565       1.1    tsubai 		return;
    566       1.1    tsubai 	}
    567       1.1    tsubai 
    568       1.1    tsubai 	if (rxf.rx_rcvsts & FCS) {
    569       1.1    tsubai #ifdef MCDEBUG
    570       1.1    tsubai 		printf("%s: frame control checksum error\n", sc->sc_dev.dv_xname);
    571       1.1    tsubai #endif
    572       1.1    tsubai 		sc->sc_if.if_ierrors++;
    573       1.1    tsubai 		return;
    574       1.1    tsubai 	}
    575       1.1    tsubai 
    576       1.1    tsubai 	mace_read(sc, rxf.rx_frame, len);
    577       1.1    tsubai #undef	rxf
    578       1.1    tsubai }
    579       1.1    tsubai 
    580       1.1    tsubai integrate void
    581  1.24.4.1       jym mace_read(struct mc_softc *sc, uint8_t *pkt, int len)
    582       1.1    tsubai {
    583       1.1    tsubai 	struct ifnet *ifp = &sc->sc_if;
    584       1.1    tsubai 	struct mbuf *m;
    585       1.1    tsubai 
    586       1.1    tsubai 	if (len <= sizeof(struct ether_header) ||
    587       1.1    tsubai 	    len > ETHERMTU + sizeof(struct ether_header)) {
    588       1.1    tsubai #ifdef MCDEBUG
    589       1.1    tsubai 		printf("%s: invalid packet size %d; dropping\n",
    590       1.1    tsubai 		    sc->sc_dev.dv_xname, len);
    591       1.1    tsubai #endif
    592       1.1    tsubai 		ifp->if_ierrors++;
    593       1.1    tsubai 		return;
    594       1.1    tsubai 	}
    595       1.1    tsubai 
    596       1.1    tsubai 	m = mace_get(sc, pkt, len);
    597       1.1    tsubai 	if (m == NULL) {
    598       1.1    tsubai 		ifp->if_ierrors++;
    599       1.1    tsubai 		return;
    600       1.1    tsubai 	}
    601       1.1    tsubai 
    602       1.1    tsubai 	ifp->if_ipackets++;
    603      1.12   thorpej 
    604      1.12   thorpej #if NBPFILTER > 0
    605      1.12   thorpej 	/* Pass this up to any BPF listeners. */
    606      1.12   thorpej 	if (ifp->if_bpf)
    607      1.12   thorpej 		bpf_mtap(ifp->if_bpf, m);
    608      1.12   thorpej #endif
    609       1.1    tsubai 
    610       1.7   thorpej 	/* Pass the packet up. */
    611       1.7   thorpej 	(*ifp->if_input)(ifp, m);
    612       1.1    tsubai }
    613       1.1    tsubai 
    614       1.1    tsubai /*
    615       1.1    tsubai  * Pull data off an interface.
    616       1.1    tsubai  * Len is length of data, with local net header stripped.
    617       1.1    tsubai  * We copy the data into mbufs.  When full cluster sized units are present
    618       1.1    tsubai  * we copy into clusters.
    619       1.1    tsubai  */
    620       1.1    tsubai integrate struct mbuf *
    621  1.24.4.1       jym mace_get(struct mc_softc *sc, uint8_t *pkt, int totlen)
    622       1.1    tsubai {
    623       1.1    tsubai 	register struct mbuf *m;
    624       1.1    tsubai 	struct mbuf *top, **mp;
    625       1.1    tsubai 	int len;
    626       1.1    tsubai 
    627       1.1    tsubai 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    628       1.1    tsubai 	if (m == 0)
    629       1.1    tsubai 		return (0);
    630       1.1    tsubai 	m->m_pkthdr.rcvif = &sc->sc_if;
    631       1.1    tsubai 	m->m_pkthdr.len = totlen;
    632       1.1    tsubai 	len = MHLEN;
    633       1.1    tsubai 	top = 0;
    634       1.1    tsubai 	mp = &top;
    635       1.1    tsubai 
    636       1.1    tsubai 	while (totlen > 0) {
    637       1.1    tsubai 		if (top) {
    638       1.1    tsubai 			MGET(m, M_DONTWAIT, MT_DATA);
    639       1.1    tsubai 			if (m == 0) {
    640       1.1    tsubai 				m_freem(top);
    641       1.1    tsubai 				return 0;
    642       1.1    tsubai 			}
    643       1.1    tsubai 			len = MLEN;
    644       1.1    tsubai 		}
    645       1.1    tsubai 		if (totlen >= MINCLSIZE) {
    646       1.1    tsubai 			MCLGET(m, M_DONTWAIT);
    647       1.1    tsubai 			if ((m->m_flags & M_EXT) == 0) {
    648       1.1    tsubai 				m_free(m);
    649       1.1    tsubai 				m_freem(top);
    650       1.1    tsubai 				return 0;
    651       1.1    tsubai 			}
    652       1.1    tsubai 			len = MCLBYTES;
    653       1.1    tsubai 		}
    654       1.1    tsubai 		m->m_len = len = min(totlen, len);
    655      1.20  christos 		memcpy(mtod(m, void *), pkt, len);
    656       1.1    tsubai 		pkt += len;
    657       1.1    tsubai 		totlen -= len;
    658       1.1    tsubai 		*mp = m;
    659       1.1    tsubai 		mp = &m->m_next;
    660       1.1    tsubai 	}
    661       1.1    tsubai 
    662       1.1    tsubai 	return (top);
    663       1.1    tsubai }
    664       1.1    tsubai 
    665       1.1    tsubai /*
    666       1.1    tsubai  * Go through the list of multicast addresses and calculate the logical
    667       1.1    tsubai  * address filter.
    668       1.1    tsubai  */
    669       1.1    tsubai void
    670  1.24.4.1       jym mace_calcladrf(struct ethercom *ac, u_int8_t *af)
    671       1.1    tsubai {
    672       1.1    tsubai 	struct ifnet *ifp = &ac->ec_if;
    673       1.1    tsubai 	struct ether_multi *enm;
    674       1.1    tsubai 	register u_char *cp, c;
    675       1.1    tsubai 	register u_int32_t crc;
    676       1.1    tsubai 	register int i, len;
    677       1.1    tsubai 	struct ether_multistep step;
    678       1.1    tsubai 
    679       1.1    tsubai 	/*
    680       1.1    tsubai 	 * Set up multicast address filter by passing all multicast addresses
    681       1.1    tsubai 	 * through a crc generator, and then using the high order 6 bits as an
    682       1.1    tsubai 	 * index into the 64 bit logical address filter.  The high order bit
    683       1.1    tsubai 	 * selects the word, while the rest of the bits select the bit within
    684       1.1    tsubai 	 * the word.
    685       1.1    tsubai 	 */
    686       1.1    tsubai 
    687       1.1    tsubai 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0;
    688       1.1    tsubai 
    689       1.1    tsubai 	ETHER_FIRST_MULTI(step, ac, enm);
    690       1.1    tsubai 	while (enm != NULL) {
    691       1.1    tsubai 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    692       1.1    tsubai 			/*
    693       1.1    tsubai 			 * We must listen to a range of multicast addresses.
    694       1.1    tsubai 			 * For now, just accept all multicasts, rather than
    695       1.1    tsubai 			 * trying to set only those filter bits needed to match
    696       1.1    tsubai 			 * the range.  (At this time, the only use of address
    697       1.1    tsubai 			 * ranges is for IP multicast routing, for which the
    698       1.1    tsubai 			 * range is big enough to require all bits set.)
    699       1.1    tsubai 			 */
    700       1.1    tsubai 			goto allmulti;
    701       1.1    tsubai 		}
    702       1.1    tsubai 
    703       1.1    tsubai 		cp = enm->enm_addrlo;
    704       1.1    tsubai 		crc = 0xffffffff;
    705       1.1    tsubai 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
    706       1.1    tsubai 			c = *cp++;
    707       1.1    tsubai 			for (i = 8; --i >= 0;) {
    708       1.1    tsubai 				if ((crc & 0x01) ^ (c & 0x01)) {
    709       1.1    tsubai 					crc >>= 1;
    710       1.1    tsubai 					crc ^= 0xedb88320;
    711       1.1    tsubai 				} else
    712       1.1    tsubai 					crc >>= 1;
    713       1.1    tsubai 				c >>= 1;
    714       1.1    tsubai 			}
    715       1.1    tsubai 		}
    716       1.1    tsubai 		/* Just want the 6 most significant bits. */
    717       1.1    tsubai 		crc >>= 26;
    718       1.1    tsubai 
    719       1.1    tsubai 		/* Set the corresponding bit in the filter. */
    720       1.1    tsubai 		af[crc >> 3] |= 1 << (crc & 7);
    721       1.1    tsubai 
    722       1.1    tsubai 		ETHER_NEXT_MULTI(step, enm);
    723       1.1    tsubai 	}
    724       1.1    tsubai 	ifp->if_flags &= ~IFF_ALLMULTI;
    725       1.1    tsubai 	return;
    726       1.1    tsubai 
    727       1.1    tsubai allmulti:
    728       1.1    tsubai 	ifp->if_flags |= IFF_ALLMULTI;
    729       1.1    tsubai 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0xffffffff;
    730       1.1    tsubai }
    731       1.1    tsubai 
    732       1.1    tsubai int
    733  1.24.4.1       jym mc_mediachange(struct ifnet *ifp)
    734       1.1    tsubai {
    735       1.1    tsubai 	return EINVAL;
    736       1.1    tsubai }
    737       1.1    tsubai 
    738       1.1    tsubai void
    739  1.24.4.1       jym mc_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    740       1.1    tsubai {
    741       1.1    tsubai 	struct mc_softc *sc = ifp->if_softc;
    742       1.1    tsubai 
    743       1.1    tsubai 	if ((ifp->if_flags & IFF_UP) == 0)
    744       1.1    tsubai 		return;
    745       1.1    tsubai 
    746       1.1    tsubai 	if (sc->sc_havecarrier)
    747       1.1    tsubai 		ifmr->ifm_status |= IFM_ACTIVE;
    748       1.1    tsubai }
    749