Home | History | Annotate | Line # | Download | only in isa
if_el.c revision 1.53
      1  1.53  jonathan /*	$NetBSD: if_el.c,v 1.53 1998/07/05 00:51:21 jonathan Exp $	*/
      2  1.14       cgd 
      3   1.2       cgd /*
      4   1.2       cgd  * Copyright (c) 1994, Matthew E. Kimmel.  Permission is hereby granted
      5   1.1   hpeyerl  * to use, copy, modify and distribute this software provided that both
      6   1.1   hpeyerl  * the copyright notice and this permission notice appear in all copies
      7   1.1   hpeyerl  * of the software, derivative works or modified versions, and any
      8   1.1   hpeyerl  * portions thereof.
      9   1.1   hpeyerl  */
     10   1.2       cgd 
     11   1.2       cgd /*
     12   1.2       cgd  * 3COM Etherlink 3C501 device driver
     13   1.1   hpeyerl  */
     14   1.2       cgd 
     15   1.2       cgd /*
     16   1.2       cgd  * Bugs/possible improvements:
     17   1.1   hpeyerl  *	- Does not currently support DMA
     18   1.1   hpeyerl  *	- Does not currently support multicasts
     19   1.1   hpeyerl  */
     20   1.2       cgd 
     21  1.53  jonathan #include "opt_inet.h"
     22   1.1   hpeyerl #include "bpfilter.h"
     23  1.49  explorer #include "rnd.h"
     24   1.1   hpeyerl 
     25   1.1   hpeyerl #include <sys/param.h>
     26  1.37  christos #include <sys/systm.h>
     27   1.1   hpeyerl #include <sys/errno.h>
     28   1.1   hpeyerl #include <sys/ioctl.h>
     29   1.1   hpeyerl #include <sys/mbuf.h>
     30   1.1   hpeyerl #include <sys/socket.h>
     31   1.1   hpeyerl #include <sys/syslog.h>
     32   1.3   mycroft #include <sys/device.h>
     33  1.49  explorer #if NRND > 0
     34  1.49  explorer #include <sys/rnd.h>
     35  1.49  explorer #endif
     36   1.1   hpeyerl 
     37   1.1   hpeyerl #include <net/if.h>
     38   1.1   hpeyerl #include <net/if_dl.h>
     39   1.1   hpeyerl #include <net/if_types.h>
     40   1.1   hpeyerl 
     41  1.44        is #include <net/if_ether.h>
     42  1.44        is 
     43   1.1   hpeyerl #ifdef INET
     44   1.1   hpeyerl #include <netinet/in.h>
     45   1.1   hpeyerl #include <netinet/in_systm.h>
     46   1.1   hpeyerl #include <netinet/in_var.h>
     47   1.1   hpeyerl #include <netinet/ip.h>
     48  1.44        is #include <netinet/if_inarp.h>
     49   1.1   hpeyerl #endif
     50   1.1   hpeyerl 
     51   1.1   hpeyerl #ifdef NS
     52   1.1   hpeyerl #include <netns/ns.h>
     53   1.1   hpeyerl #include <netns/ns_if.h>
     54   1.1   hpeyerl #endif
     55   1.1   hpeyerl 
     56   1.1   hpeyerl #if NBPFILTER > 0
     57   1.1   hpeyerl #include <net/bpf.h>
     58   1.1   hpeyerl #include <net/bpfdesc.h>
     59   1.1   hpeyerl #endif
     60   1.1   hpeyerl 
     61   1.7   mycroft #include <machine/cpu.h>
     62  1.39   mycroft #include <machine/intr.h>
     63  1.40   thorpej #include <machine/bus.h>
     64   1.1   hpeyerl 
     65  1.23       cgd #include <dev/isa/isavar.h>
     66  1.21       cgd #include <dev/isa/if_elreg.h>
     67   1.1   hpeyerl 
     68   1.1   hpeyerl #define ETHER_MIN_LEN	64
     69   1.1   hpeyerl #define ETHER_MAX_LEN	1518
     70   1.3   mycroft #define	ETHER_ADDR_LEN	6
     71   1.1   hpeyerl 
     72   1.3   mycroft /* for debugging convenience */
     73   1.1   hpeyerl #ifdef EL_DEBUG
     74  1.42  christos #define DPRINTF(x) printf x
     75   1.1   hpeyerl #else
     76  1.41  christos #define DPRINTF(x)
     77   1.1   hpeyerl #endif
     78   1.1   hpeyerl 
     79   1.2       cgd /*
     80   1.3   mycroft  * per-line info and status
     81   1.2       cgd  */
     82   1.1   hpeyerl struct el_softc {
     83   1.3   mycroft 	struct device sc_dev;
     84  1.23       cgd 	void *sc_ih;
     85   1.3   mycroft 
     86  1.44        is 	struct ethercom sc_ethercom;	/* ethernet common */
     87  1.43   thorpej 	bus_space_tag_t sc_iot;		/* bus space identifier */
     88  1.43   thorpej 	bus_space_handle_t sc_ioh;	/* i/o handle */
     89  1.49  explorer 
     90  1.49  explorer #if NRND > 0
     91  1.49  explorer 	rndsource_element_t rnd_source;
     92  1.49  explorer #endif
     93   1.8   mycroft };
     94   1.1   hpeyerl 
     95   1.2       cgd /*
     96   1.3   mycroft  * prototypes
     97   1.2       cgd  */
     98  1.23       cgd int elintr __P((void *));
     99  1.37  christos void elinit __P((struct el_softc *));
    100  1.30   mycroft int elioctl __P((struct ifnet *, u_long, caddr_t));
    101  1.30   mycroft void elstart __P((struct ifnet *));
    102  1.38   thorpej void elwatchdog __P((struct ifnet *));
    103  1.30   mycroft void elreset __P((struct el_softc *));
    104  1.30   mycroft void elstop __P((struct el_softc *));
    105  1.28   mycroft static int el_xmit __P((struct el_softc *));
    106  1.30   mycroft void elread __P((struct el_softc *, int));
    107  1.31   mycroft struct mbuf *elget __P((struct el_softc *sc, int));
    108   1.5   mycroft static inline void el_hardreset __P((struct el_softc *));
    109   1.1   hpeyerl 
    110  1.50  drochner int elprobe __P((struct device *, struct cfdata *, void *));
    111  1.16   mycroft void elattach __P((struct device *, struct device *, void *));
    112   1.8   mycroft 
    113  1.35   thorpej struct cfattach el_ca = {
    114  1.35   thorpej 	sizeof(struct el_softc), elprobe, elattach
    115   1.1   hpeyerl };
    116   1.1   hpeyerl 
    117   1.2       cgd /*
    118   1.2       cgd  * Probe routine.
    119   1.2       cgd  *
    120   1.2       cgd  * See if the card is there and at the right place.
    121   1.2       cgd  * (XXX - cgd -- needs help)
    122   1.2       cgd  */
    123   1.8   mycroft int
    124  1.16   mycroft elprobe(parent, match, aux)
    125  1.16   mycroft 	struct device *parent;
    126  1.50  drochner 	struct cfdata *match;
    127  1.50  drochner 	void *aux;
    128   1.8   mycroft {
    129   1.8   mycroft 	struct isa_attach_args *ia = aux;
    130  1.43   thorpej 	bus_space_tag_t iot = ia->ia_iot;
    131  1.43   thorpej 	bus_space_handle_t ioh;
    132  1.18   mycroft 	int iobase = ia->ia_iobase;
    133  1.40   thorpej 	u_int8_t station_addr[ETHER_ADDR_LEN];
    134  1.40   thorpej 	u_int8_t i;
    135  1.40   thorpej 	int rval;
    136  1.40   thorpej 
    137  1.40   thorpej 	rval = 0;
    138   1.1   hpeyerl 
    139   1.3   mycroft 	/* First check the base. */
    140  1.48   mycroft 	if (iobase < 0x200 || iobase > 0x3f0)
    141   1.3   mycroft 		return 0;
    142   1.3   mycroft 
    143  1.40   thorpej 	/* Map i/o space. */
    144  1.48   mycroft 	if (bus_space_map(iot, iobase, 16, 0, &ioh))
    145  1.40   thorpej 		return 0;
    146   1.3   mycroft 
    147   1.2       cgd 	/*
    148   1.3   mycroft 	 * Now attempt to grab the station address from the PROM and see if it
    149   1.3   mycroft 	 * contains the 3com vendor code.
    150   1.1   hpeyerl 	 */
    151  1.41  christos 	DPRINTF(("Probing 3c501 at 0x%x...\n", iobase));
    152   1.3   mycroft 
    153   1.3   mycroft 	/* Reset the board. */
    154  1.41  christos 	DPRINTF(("Resetting board...\n"));
    155  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
    156   1.6   mycroft 	delay(5);
    157  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, 0);
    158   1.3   mycroft 
    159   1.3   mycroft 	/* Now read the address. */
    160  1.41  christos 	DPRINTF(("Reading station address...\n"));
    161   1.3   mycroft 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    162  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_GPBL, i);
    163  1.43   thorpej 		station_addr[i] = bus_space_read_1(iot, ioh, EL_EAW);
    164   1.1   hpeyerl 	}
    165  1.41  christos 	DPRINTF(("Address is %s\n", ether_sprintf(station_addr)));
    166   1.1   hpeyerl 
    167   1.2       cgd 	/*
    168   1.3   mycroft 	 * If the vendor code is ok, return a 1.  We'll assume that whoever
    169   1.3   mycroft 	 * configured this system is right about the IRQ.
    170   1.1   hpeyerl 	 */
    171   1.3   mycroft 	if (station_addr[0] != 0x02 || station_addr[1] != 0x60 ||
    172   1.3   mycroft 	    station_addr[2] != 0x8c) {
    173  1.41  christos 		DPRINTF(("Bad vendor code.\n"));
    174  1.40   thorpej 		goto out;
    175   1.1   hpeyerl 	}
    176  1.41  christos 	DPRINTF(("Vendor code ok.\n"));
    177   1.8   mycroft 
    178  1.48   mycroft 	ia->ia_iosize = 16;
    179   1.8   mycroft 	ia->ia_msize = 0;
    180  1.40   thorpej 	rval = 1;
    181  1.40   thorpej 
    182  1.40   thorpej  out:
    183  1.48   mycroft 	bus_space_unmap(iot, ioh, 16);
    184  1.40   thorpej 	return rval;
    185   1.1   hpeyerl }
    186   1.1   hpeyerl 
    187   1.2       cgd /*
    188   1.3   mycroft  * Attach the interface to the kernel data structures.  By the time this is
    189   1.3   mycroft  * called, we know that the card exists at the given I/O address.  We still
    190   1.3   mycroft  * assume that the IRQ given is correct.
    191   1.1   hpeyerl  */
    192   1.8   mycroft void
    193   1.8   mycroft elattach(parent, self, aux)
    194   1.8   mycroft 	struct device *parent, *self;
    195   1.8   mycroft 	void *aux;
    196   1.1   hpeyerl {
    197   1.8   mycroft 	struct el_softc *sc = (void *)self;
    198   1.9   mycroft 	struct isa_attach_args *ia = aux;
    199  1.43   thorpej 	bus_space_tag_t iot = ia->ia_iot;
    200  1.43   thorpej 	bus_space_handle_t ioh;
    201  1.44        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    202  1.44        is 	u_int8_t myaddr[ETHER_ADDR_LEN];
    203  1.40   thorpej 	u_int8_t i;
    204  1.40   thorpej 
    205  1.42  christos 	printf("\n");
    206   1.1   hpeyerl 
    207  1.41  christos 	DPRINTF(("Attaching %s...\n", sc->sc_dev.dv_xname));
    208   1.1   hpeyerl 
    209  1.40   thorpej 	/* Map i/o space. */
    210  1.43   thorpej 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
    211  1.42  christos 		printf("%s: can't map i/o space\n", self->dv_xname);
    212  1.40   thorpej 		return;
    213  1.40   thorpej 	}
    214  1.40   thorpej 
    215  1.43   thorpej 	sc->sc_iot = iot;
    216  1.40   thorpej 	sc->sc_ioh = ioh;
    217  1.40   thorpej 
    218  1.40   thorpej 	/* Reset the board. */
    219  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
    220  1.40   thorpej 	delay(5);
    221  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, 0);
    222  1.40   thorpej 
    223  1.40   thorpej 	/* Now read the address. */
    224  1.40   thorpej 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    225  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_GPBL, i);
    226  1.44        is 		myaddr[i] = bus_space_read_1(iot, ioh, EL_EAW);
    227  1.40   thorpej 	}
    228  1.40   thorpej 
    229   1.3   mycroft 	/* Stop the board. */
    230  1.30   mycroft 	elstop(sc);
    231   1.1   hpeyerl 
    232   1.3   mycroft 	/* Initialize ifnet structure. */
    233  1.38   thorpej 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    234  1.38   thorpej 	ifp->if_softc = sc;
    235  1.30   mycroft 	ifp->if_start = elstart;
    236  1.30   mycroft 	ifp->if_ioctl = elioctl;
    237  1.30   mycroft 	ifp->if_watchdog = elwatchdog;
    238   1.3   mycroft 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    239   1.1   hpeyerl 
    240   1.3   mycroft 	/* Now we can attach the interface. */
    241  1.41  christos 	DPRINTF(("Attaching interface...\n"));
    242   1.1   hpeyerl 	if_attach(ifp);
    243  1.44        is 	ether_ifattach(ifp, myaddr);
    244   1.1   hpeyerl 
    245   1.3   mycroft 	/* Print out some information for the user. */
    246  1.44        is 	printf("%s: address %s\n", self->dv_xname, ether_sprintf(myaddr));
    247   1.1   hpeyerl 
    248   1.1   hpeyerl 	/* Finally, attach to bpf filter if it is present. */
    249   1.1   hpeyerl #if NBPFILTER > 0
    250  1.41  christos 	DPRINTF(("Attaching to BPF...\n"));
    251  1.13   mycroft 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    252   1.1   hpeyerl #endif
    253   1.1   hpeyerl 
    254  1.36       cgd 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    255  1.36       cgd 	    IPL_NET, elintr, sc);
    256   1.9   mycroft 
    257  1.49  explorer #if NRND > 0
    258  1.49  explorer 	DPRINTF(("Attaching to random...\n"));
    259  1.49  explorer 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, RND_TYPE_NET);
    260  1.49  explorer #endif
    261  1.49  explorer 
    262  1.41  christos 	DPRINTF(("elattach() finished.\n"));
    263   1.1   hpeyerl }
    264   1.1   hpeyerl 
    265   1.2       cgd /*
    266   1.2       cgd  * Reset interface.
    267   1.2       cgd  */
    268  1.30   mycroft void
    269  1.30   mycroft elreset(sc)
    270   1.3   mycroft 	struct el_softc *sc;
    271   1.1   hpeyerl {
    272   1.1   hpeyerl 	int s;
    273   1.1   hpeyerl 
    274  1.41  christos 	DPRINTF(("elreset()\n"));
    275  1.34   mycroft 	s = splnet();
    276  1.30   mycroft 	elstop(sc);
    277  1.30   mycroft 	elinit(sc);
    278   1.1   hpeyerl 	splx(s);
    279   1.1   hpeyerl }
    280   1.1   hpeyerl 
    281   1.2       cgd /*
    282   1.2       cgd  * Stop interface.
    283   1.2       cgd  */
    284  1.30   mycroft void
    285  1.30   mycroft elstop(sc)
    286   1.3   mycroft 	struct el_softc *sc;
    287   1.1   hpeyerl {
    288   1.1   hpeyerl 
    289  1.43   thorpej 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, EL_AC, 0);
    290   1.1   hpeyerl }
    291   1.1   hpeyerl 
    292   1.2       cgd /*
    293   1.5   mycroft  * Do a hardware reset of the board, and upload the ethernet address again in
    294   1.5   mycroft  * case the board forgets.
    295   1.5   mycroft  */
    296   1.5   mycroft static inline void
    297   1.5   mycroft el_hardreset(sc)
    298   1.5   mycroft 	struct el_softc *sc;
    299   1.5   mycroft {
    300  1.43   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    301  1.43   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    302   1.5   mycroft 	int i;
    303   1.5   mycroft 
    304  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
    305   1.6   mycroft 	delay(5);
    306  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, 0);
    307   1.5   mycroft 
    308   1.5   mycroft 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    309  1.44        is 		bus_space_write_1(iot, ioh, i,
    310  1.44        is 		    LLADDR(sc->sc_ethercom.ec_if.if_sadl)[i]);
    311   1.5   mycroft }
    312   1.5   mycroft 
    313   1.5   mycroft /*
    314   1.2       cgd  * Initialize interface.
    315   1.2       cgd  */
    316  1.37  christos void
    317  1.30   mycroft elinit(sc)
    318   1.3   mycroft 	struct el_softc *sc;
    319   1.1   hpeyerl {
    320  1.44        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    321  1.43   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    322  1.43   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    323   1.1   hpeyerl 
    324   1.1   hpeyerl 	/* First, reset the board. */
    325   1.5   mycroft 	el_hardreset(sc);
    326   1.1   hpeyerl 
    327   1.3   mycroft 	/* Configure rx. */
    328  1.41  christos 	DPRINTF(("Configuring rx...\n"));
    329   1.2       cgd 	if (ifp->if_flags & IFF_PROMISC)
    330  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_RXC,
    331  1.40   thorpej 		    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    332  1.40   thorpej 		    EL_RXC_DOFLOW | EL_RXC_PROMISC);
    333   1.1   hpeyerl 	else
    334  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_RXC,
    335  1.40   thorpej 		    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    336  1.40   thorpej 		    EL_RXC_DOFLOW | EL_RXC_ABROAD);
    337  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_RBC, 0);
    338   1.1   hpeyerl 
    339   1.3   mycroft 	/* Configure TX. */
    340  1.41  christos 	DPRINTF(("Configuring tx...\n"));
    341  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_TXC, 0);
    342   1.1   hpeyerl 
    343   1.3   mycroft 	/* Start reception. */
    344  1.41  christos 	DPRINTF(("Starting reception...\n"));
    345  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    346   1.1   hpeyerl 
    347   1.3   mycroft 	/* Set flags appropriately. */
    348   1.1   hpeyerl 	ifp->if_flags |= IFF_RUNNING;
    349   1.1   hpeyerl 	ifp->if_flags &= ~IFF_OACTIVE;
    350   1.1   hpeyerl 
    351   1.1   hpeyerl 	/* And start output. */
    352  1.30   mycroft 	elstart(ifp);
    353   1.1   hpeyerl }
    354   1.1   hpeyerl 
    355   1.2       cgd /*
    356   1.3   mycroft  * Start output on interface.  Get datagrams from the queue and output them,
    357  1.34   mycroft  * giving the receiver a chance between datagrams.  Call only from splnet or
    358   1.3   mycroft  * interrupt level!
    359   1.1   hpeyerl  */
    360  1.30   mycroft void
    361  1.30   mycroft elstart(ifp)
    362   1.2       cgd 	struct ifnet *ifp;
    363   1.1   hpeyerl {
    364  1.38   thorpej 	struct el_softc *sc = ifp->if_softc;
    365  1.43   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    366  1.43   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    367   1.1   hpeyerl 	struct mbuf *m, *m0;
    368  1.27   mycroft 	int s, i, off, retries;
    369   1.1   hpeyerl 
    370  1.41  christos 	DPRINTF(("elstart()...\n"));
    371  1.34   mycroft 	s = splnet();
    372   1.1   hpeyerl 
    373   1.3   mycroft 	/* Don't do anything if output is active. */
    374  1.27   mycroft 	if ((ifp->if_flags & IFF_OACTIVE) != 0) {
    375  1.27   mycroft 		splx(s);
    376   1.1   hpeyerl 		return;
    377  1.27   mycroft 	}
    378  1.27   mycroft 
    379  1.27   mycroft 	ifp->if_flags |= IFF_OACTIVE;
    380   1.1   hpeyerl 
    381   1.3   mycroft 	/*
    382   1.3   mycroft 	 * The main loop.  They warned me against endless loops, but would I
    383   1.3   mycroft 	 * listen?  NOOO....
    384   1.1   hpeyerl 	 */
    385   1.3   mycroft 	for (;;) {
    386   1.3   mycroft 		/* Dequeue the next datagram. */
    387  1.27   mycroft 		IF_DEQUEUE(&ifp->if_snd, m0);
    388   1.1   hpeyerl 
    389   1.1   hpeyerl 		/* If there's nothing to send, return. */
    390  1.27   mycroft 		if (m0 == 0)
    391  1.27   mycroft 			break;
    392  1.27   mycroft 
    393  1.27   mycroft #if NBPFILTER > 0
    394  1.27   mycroft 		/* Give the packet to the bpf, if any. */
    395  1.27   mycroft 		if (ifp->if_bpf)
    396  1.27   mycroft 			bpf_mtap(ifp->if_bpf, m0);
    397  1.27   mycroft #endif
    398   1.1   hpeyerl 
    399   1.3   mycroft 		/* Disable the receiver. */
    400  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
    401  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_RBC, 0);
    402   1.1   hpeyerl 
    403  1.27   mycroft 		/* Transfer datagram to board. */
    404  1.41  christos 		DPRINTF(("el: xfr pkt length=%d...\n", m0->m_pkthdr.len));
    405  1.27   mycroft 		off = EL_BUFSIZ - max(m0->m_pkthdr.len, ETHER_MIN_LEN);
    406  1.40   thorpej #ifdef DIAGNOSTIC
    407  1.40   thorpej 		if ((off & 0xffff) != off)
    408  1.42  christos 			printf("%s: bogus off 0x%x\n",
    409  1.40   thorpej 			    sc->sc_dev.dv_xname, off);
    410  1.40   thorpej #endif
    411  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
    412  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
    413  1.27   mycroft 
    414   1.1   hpeyerl 		/* Copy the datagram to the buffer. */
    415  1.27   mycroft 		for (m = m0; m != 0; m = m->m_next)
    416  1.43   thorpej 			bus_space_write_multi_1(iot, ioh, EL_BUF,
    417  1.40   thorpej 			    mtod(m, u_int8_t *), m->m_len);
    418  1.27   mycroft 
    419   1.1   hpeyerl 		m_freem(m0);
    420   1.1   hpeyerl 
    421   1.3   mycroft 		/* Now transmit the datagram. */
    422   1.3   mycroft 		retries = 0;
    423  1.27   mycroft 		for (;;) {
    424  1.43   thorpej 			bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
    425  1.43   thorpej 			bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
    426  1.33   mycroft 			if (el_xmit(sc)) {
    427  1.33   mycroft 				ifp->if_oerrors++;
    428   1.1   hpeyerl 				break;
    429  1.33   mycroft 			}
    430   1.3   mycroft 			/* Check out status. */
    431  1.43   thorpej 			i = bus_space_read_1(iot, ioh, EL_TXS);
    432  1.41  christos 			DPRINTF(("tx status=0x%x\n", i));
    433   1.3   mycroft 			if ((i & EL_TXS_READY) == 0) {
    434  1.41  christos 				DPRINTF(("el: err txs=%x\n", i));
    435   1.3   mycroft 				if (i & (EL_TXS_COLL | EL_TXS_COLL16)) {
    436  1.33   mycroft 					ifp->if_collisions++;
    437   1.3   mycroft 					if ((i & EL_TXC_DCOLL16) == 0 &&
    438   1.2       cgd 					    retries < 15) {
    439   1.1   hpeyerl 						retries++;
    440  1.43   thorpej 						bus_space_write_1(iot, ioh,
    441  1.40   thorpej 						    EL_AC, EL_AC_HOST);
    442   1.1   hpeyerl 					}
    443  1.33   mycroft 				} else {
    444  1.33   mycroft 					ifp->if_oerrors++;
    445  1.27   mycroft 					break;
    446  1.33   mycroft 				}
    447   1.4   mycroft 			} else {
    448  1.27   mycroft 				ifp->if_opackets++;
    449  1.27   mycroft 				break;
    450   1.4   mycroft 			}
    451   1.1   hpeyerl 		}
    452   1.1   hpeyerl 
    453   1.2       cgd 		/*
    454   1.2       cgd 		 * Now give the card a chance to receive.
    455   1.1   hpeyerl 		 * Gotta love 3c501s...
    456   1.1   hpeyerl 		 */
    457  1.43   thorpej 		(void)bus_space_read_1(iot, ioh, EL_AS);
    458  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    459   1.1   hpeyerl 		splx(s);
    460   1.3   mycroft 		/* Interrupt here. */
    461  1.34   mycroft 		s = splnet();
    462   1.1   hpeyerl 	}
    463  1.27   mycroft 
    464  1.43   thorpej 	(void)bus_space_read_1(iot, ioh, EL_AS);
    465  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    466  1.27   mycroft 	ifp->if_flags &= ~IFF_OACTIVE;
    467  1.27   mycroft 	splx(s);
    468   1.1   hpeyerl }
    469   1.1   hpeyerl 
    470   1.2       cgd /*
    471   1.3   mycroft  * This function actually attempts to transmit a datagram downloaded to the
    472  1.34   mycroft  * board.  Call at splnet or interrupt, after downloading data!  Returns 0 on
    473   1.3   mycroft  * success, non-0 on failure.
    474   1.1   hpeyerl  */
    475   1.1   hpeyerl static int
    476  1.27   mycroft el_xmit(sc)
    477   1.2       cgd 	struct el_softc *sc;
    478   1.1   hpeyerl {
    479  1.43   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    480  1.43   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    481   1.1   hpeyerl 	int i;
    482   1.1   hpeyerl 
    483  1.33   mycroft 	/*
    484  1.33   mycroft 	 * XXX
    485  1.33   mycroft 	 * This busy-waits for the tx completion.  Can we get an interrupt
    486  1.33   mycroft 	 * instead?
    487  1.33   mycroft 	 */
    488  1.33   mycroft 
    489  1.41  christos 	DPRINTF(("el: xmit..."));
    490  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_TXFRX);
    491   1.1   hpeyerl 	i = 20000;
    492  1.43   thorpej 	while ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_TXBUSY) && (i > 0))
    493   1.1   hpeyerl 		i--;
    494   1.2       cgd 	if (i == 0) {
    495  1.41  christos 		DPRINTF(("tx not ready\n"));
    496   1.3   mycroft 		return -1;
    497   1.1   hpeyerl 	}
    498  1.41  christos 	DPRINTF(("%d cycles.\n", 20000 - i));
    499   1.3   mycroft 	return 0;
    500   1.1   hpeyerl }
    501   1.1   hpeyerl 
    502   1.3   mycroft /*
    503   1.3   mycroft  * Controller interrupt.
    504   1.3   mycroft  */
    505   1.1   hpeyerl int
    506  1.23       cgd elintr(arg)
    507  1.23       cgd 	void *arg;
    508   1.1   hpeyerl {
    509  1.23       cgd 	register struct el_softc *sc = arg;
    510  1.43   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    511  1.43   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    512  1.40   thorpej 	u_int8_t rxstat;
    513  1.40   thorpej 	int len;
    514   1.1   hpeyerl 
    515  1.41  christos 	DPRINTF(("elintr: "));
    516   1.1   hpeyerl 
    517   1.3   mycroft 	/* Check board status. */
    518  1.43   thorpej 	if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0) {
    519  1.43   thorpej 		(void)bus_space_read_1(iot, ioh, EL_RXC);
    520  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    521  1.10   mycroft 		return 0;
    522   1.1   hpeyerl 	}
    523   1.1   hpeyerl 
    524  1.27   mycroft 	for (;;) {
    525  1.43   thorpej 		rxstat = bus_space_read_1(iot, ioh, EL_RXS);
    526  1.27   mycroft 		if (rxstat & EL_RXS_STALE)
    527  1.27   mycroft 			break;
    528   1.1   hpeyerl 
    529   1.1   hpeyerl 		/* If there's an overflow, reinit the board. */
    530   1.3   mycroft 		if ((rxstat & EL_RXS_NOFLOW) == 0) {
    531  1.41  christos 			DPRINTF(("overflow.\n"));
    532   1.5   mycroft 			el_hardreset(sc);
    533   1.3   mycroft 			/* Put board back into receive mode. */
    534  1.44        is 			if (sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC)
    535  1.43   thorpej 				bus_space_write_1(iot, ioh, EL_RXC,
    536  1.40   thorpej 				    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    537  1.40   thorpej 				    EL_RXC_DOFLOW | EL_RXC_PROMISC);
    538   1.1   hpeyerl 			else
    539  1.43   thorpej 				bus_space_write_1(iot, ioh, EL_RXC,
    540  1.40   thorpej 				    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    541  1.40   thorpej 				    EL_RXC_DOFLOW | EL_RXC_ABROAD);
    542  1.43   thorpej 			(void)bus_space_read_1(iot, ioh, EL_AS);
    543  1.43   thorpej 			bus_space_write_1(iot, ioh, EL_RBC, 0);
    544  1.27   mycroft 			break;
    545   1.1   hpeyerl 		}
    546   1.1   hpeyerl 
    547   1.3   mycroft 		/* Incoming packet. */
    548  1.43   thorpej 		len = bus_space_read_1(iot, ioh, EL_RBL);
    549  1.43   thorpej 		len |= bus_space_read_1(iot, ioh, EL_RBH) << 8;
    550  1.41  christos 		DPRINTF(("receive len=%d rxstat=%x ", len, rxstat));
    551  1.43   thorpej 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
    552   1.1   hpeyerl 
    553   1.3   mycroft 		/* Pass data up to upper levels. */
    554  1.27   mycroft 		elread(sc, len);
    555   1.1   hpeyerl 
    556   1.1   hpeyerl 		/* Is there another packet? */
    557  1.43   thorpej 		if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0)
    558  1.27   mycroft 			break;
    559  1.49  explorer 
    560  1.49  explorer #if NRND > 0
    561  1.49  explorer 		rnd_add_uint32(&sc->rnd_source, rxstat);
    562  1.49  explorer #endif
    563  1.27   mycroft 
    564  1.41  christos 		DPRINTF(("<rescan> "));
    565   1.1   hpeyerl 	}
    566   1.1   hpeyerl 
    567  1.43   thorpej 	(void)bus_space_read_1(iot, ioh, EL_RXC);
    568  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    569  1.10   mycroft 	return 1;
    570   1.1   hpeyerl }
    571   1.1   hpeyerl 
    572   1.2       cgd /*
    573  1.30   mycroft  * Pass a packet to the higher levels.
    574   1.2       cgd  */
    575  1.30   mycroft void
    576  1.27   mycroft elread(sc, len)
    577  1.31   mycroft 	register struct el_softc *sc;
    578   1.2       cgd 	int len;
    579   1.1   hpeyerl {
    580  1.44        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    581   1.1   hpeyerl 	struct mbuf *m;
    582  1.26   mycroft 	struct ether_header *eh;
    583   1.1   hpeyerl 
    584  1.30   mycroft 	if (len <= sizeof(struct ether_header) ||
    585  1.30   mycroft 	    len > ETHER_MAX_LEN) {
    586  1.42  christos 		printf("%s: invalid packet size %d; dropping\n",
    587  1.32   mycroft 		    sc->sc_dev.dv_xname, len);
    588  1.30   mycroft 		ifp->if_ierrors++;
    589  1.30   mycroft 		return;
    590  1.30   mycroft 	}
    591  1.30   mycroft 
    592  1.13   mycroft 	/* Pull packet off interface. */
    593  1.30   mycroft 	m = elget(sc, len);
    594  1.30   mycroft 	if (m == 0) {
    595  1.30   mycroft 		ifp->if_ierrors++;
    596   1.1   hpeyerl 		return;
    597  1.30   mycroft 	}
    598  1.30   mycroft 
    599  1.30   mycroft 	ifp->if_ipackets++;
    600   1.1   hpeyerl 
    601  1.26   mycroft 	/* We assume that the header fit entirely in one mbuf. */
    602  1.26   mycroft 	eh = mtod(m, struct ether_header *);
    603  1.26   mycroft 
    604   1.1   hpeyerl #if NBPFILTER > 0
    605   1.1   hpeyerl 	/*
    606  1.13   mycroft 	 * Check if there's a BPF listener on this interface.
    607  1.30   mycroft 	 * If so, hand off the raw packet to BPF.
    608   1.1   hpeyerl 	 */
    609  1.26   mycroft 	if (ifp->if_bpf) {
    610  1.26   mycroft 		bpf_mtap(ifp->if_bpf, m);
    611   1.1   hpeyerl 
    612   1.1   hpeyerl 		/*
    613   1.1   hpeyerl 		 * Note that the interface cannot be in promiscuous mode if
    614  1.13   mycroft 		 * there are no BPF listeners.  And if we are in promiscuous
    615  1.13   mycroft 		 * mode, we have to check if this packet is really ours.
    616   1.1   hpeyerl 		 */
    617  1.26   mycroft 		if ((ifp->if_flags & IFF_PROMISC) &&
    618  1.13   mycroft 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    619  1.44        is 		    bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
    620  1.13   mycroft 			    sizeof(eh->ether_dhost)) != 0) {
    621  1.13   mycroft 			m_freem(m);
    622   1.1   hpeyerl 			return;
    623  1.13   mycroft 		}
    624   1.1   hpeyerl 	}
    625   1.1   hpeyerl #endif
    626   1.1   hpeyerl 
    627  1.26   mycroft 	/* We assume that the header fit entirely in one mbuf. */
    628  1.30   mycroft 	m_adj(m, sizeof(struct ether_header));
    629  1.26   mycroft 	ether_input(ifp, eh, m);
    630   1.1   hpeyerl }
    631   1.1   hpeyerl 
    632   1.1   hpeyerl /*
    633   1.3   mycroft  * Pull read data off a interface.  Len is length of data, with local net
    634  1.13   mycroft  * header stripped.  We copy the data into mbufs.  When full cluster sized
    635  1.13   mycroft  * units are present we copy into clusters.
    636   1.1   hpeyerl  */
    637  1.31   mycroft struct mbuf *
    638  1.30   mycroft elget(sc, totlen)
    639  1.27   mycroft 	struct el_softc *sc;
    640  1.26   mycroft 	int totlen;
    641  1.26   mycroft {
    642  1.44        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    643  1.43   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    644  1.43   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    645  1.26   mycroft 	struct mbuf *top, **mp, *m;
    646  1.26   mycroft 	int len;
    647  1.26   mycroft 
    648  1.26   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    649  1.26   mycroft 	if (m == 0)
    650  1.26   mycroft 		return 0;
    651  1.26   mycroft 	m->m_pkthdr.rcvif = ifp;
    652  1.26   mycroft 	m->m_pkthdr.len = totlen;
    653  1.26   mycroft 	len = MHLEN;
    654  1.26   mycroft 	top = 0;
    655  1.26   mycroft 	mp = &top;
    656  1.26   mycroft 
    657  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_GPBL, 0);
    658  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_GPBH, 0);
    659  1.27   mycroft 
    660  1.26   mycroft 	while (totlen > 0) {
    661  1.26   mycroft 		if (top) {
    662  1.26   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
    663  1.26   mycroft 			if (m == 0) {
    664  1.26   mycroft 				m_freem(top);
    665  1.26   mycroft 				return 0;
    666  1.26   mycroft 			}
    667  1.26   mycroft 			len = MLEN;
    668  1.26   mycroft 		}
    669  1.26   mycroft 		if (totlen >= MINCLSIZE) {
    670  1.26   mycroft 			MCLGET(m, M_DONTWAIT);
    671  1.46   mycroft 			if ((m->m_flags & M_EXT) == 0) {
    672  1.47   mycroft 				m_free(m);
    673  1.45   mycroft 				m_freem(top);
    674  1.45   mycroft 				return 0;
    675  1.45   mycroft 			}
    676  1.45   mycroft 			len = MCLBYTES;
    677  1.26   mycroft 		}
    678  1.26   mycroft 		m->m_len = len = min(totlen, len);
    679  1.43   thorpej 		bus_space_read_multi_1(iot, ioh, EL_BUF, mtod(m, u_int8_t *), len);
    680  1.26   mycroft 		totlen -= len;
    681  1.26   mycroft 		*mp = m;
    682  1.26   mycroft 		mp = &m->m_next;
    683  1.26   mycroft 	}
    684  1.27   mycroft 
    685  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_RBC, 0);
    686  1.43   thorpej 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RX);
    687  1.13   mycroft 
    688  1.26   mycroft 	return top;
    689   1.1   hpeyerl }
    690   1.1   hpeyerl 
    691   1.1   hpeyerl /*
    692   1.3   mycroft  * Process an ioctl request. This code needs some work - it looks pretty ugly.
    693   1.1   hpeyerl  */
    694  1.30   mycroft int
    695  1.30   mycroft elioctl(ifp, cmd, data)
    696   1.1   hpeyerl 	register struct ifnet *ifp;
    697  1.15       cgd 	u_long cmd;
    698   1.1   hpeyerl 	caddr_t data;
    699   1.1   hpeyerl {
    700  1.38   thorpej 	struct el_softc *sc = ifp->if_softc;
    701  1.13   mycroft 	struct ifaddr *ifa = (struct ifaddr *)data;
    702   1.1   hpeyerl 	int s, error = 0;
    703   1.1   hpeyerl 
    704  1.34   mycroft 	s = splnet();
    705   1.1   hpeyerl 
    706  1.13   mycroft 	switch (cmd) {
    707   1.1   hpeyerl 
    708   1.1   hpeyerl 	case SIOCSIFADDR:
    709   1.1   hpeyerl 		ifp->if_flags |= IFF_UP;
    710   1.1   hpeyerl 
    711   1.1   hpeyerl 		switch (ifa->ifa_addr->sa_family) {
    712   1.1   hpeyerl #ifdef INET
    713   1.1   hpeyerl 		case AF_INET:
    714  1.30   mycroft 			elinit(sc);
    715  1.44        is 			arp_ifinit(ifp, ifa);
    716   1.1   hpeyerl 			break;
    717   1.1   hpeyerl #endif
    718   1.1   hpeyerl #ifdef NS
    719  1.24   mycroft 		/* XXX - This code is probably wrong. */
    720   1.1   hpeyerl 		case AF_NS:
    721   1.3   mycroft 		    {
    722   1.3   mycroft 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    723   1.3   mycroft 
    724   1.3   mycroft 			if (ns_nullhost(*ina))
    725   1.3   mycroft 				ina->x_host =
    726  1.44        is 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    727  1.24   mycroft 			else
    728  1.44        is 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    729  1.44        is 				    ETHER_ADDR_LEN);
    730   1.3   mycroft 			/* Set new address. */
    731  1.30   mycroft 			elinit(sc);
    732   1.3   mycroft 			break;
    733   1.3   mycroft 		    }
    734   1.1   hpeyerl #endif
    735   1.1   hpeyerl 		default:
    736  1.30   mycroft 			elinit(sc);
    737   1.1   hpeyerl 			break;
    738   1.1   hpeyerl 		}
    739   1.1   hpeyerl 		break;
    740   1.1   hpeyerl 
    741   1.1   hpeyerl 	case SIOCSIFFLAGS:
    742   1.3   mycroft 		if ((ifp->if_flags & IFF_UP) == 0 &&
    743  1.13   mycroft 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    744   1.3   mycroft 			/*
    745   1.3   mycroft 			 * If interface is marked down and it is running, then
    746   1.3   mycroft 			 * stop it.
    747   1.3   mycroft 			 */
    748  1.30   mycroft 			elstop(sc);
    749   1.1   hpeyerl 			ifp->if_flags &= ~IFF_RUNNING;
    750  1.13   mycroft 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    751   1.3   mycroft 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    752   1.3   mycroft 			/*
    753   1.3   mycroft 			 * If interface is marked up and it is stopped, then
    754   1.3   mycroft 			 * start it.
    755   1.3   mycroft 			 */
    756  1.30   mycroft 			elinit(sc);
    757   1.1   hpeyerl 		} else {
    758   1.3   mycroft 			/*
    759   1.3   mycroft 			 * Some other important flag might have changed, so
    760   1.3   mycroft 			 * reset.
    761   1.3   mycroft 			 */
    762  1.30   mycroft 			elreset(sc);
    763   1.1   hpeyerl 		}
    764  1.24   mycroft 		break;
    765   1.1   hpeyerl 
    766   1.1   hpeyerl 	default:
    767   1.1   hpeyerl 		error = EINVAL;
    768  1.24   mycroft 		break;
    769   1.1   hpeyerl 	}
    770  1.24   mycroft 
    771  1.22   mycroft 	splx(s);
    772   1.3   mycroft 	return error;
    773   1.1   hpeyerl }
    774   1.1   hpeyerl 
    775   1.3   mycroft /*
    776   1.3   mycroft  * Device timeout routine.
    777   1.3   mycroft  */
    778  1.30   mycroft void
    779  1.38   thorpej elwatchdog(ifp)
    780  1.38   thorpej 	struct ifnet *ifp;
    781   1.1   hpeyerl {
    782  1.38   thorpej 	struct el_softc *sc = ifp->if_softc;
    783   1.1   hpeyerl 
    784   1.3   mycroft 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    785  1.44        is 	sc->sc_ethercom.ec_if.if_oerrors++;
    786   1.1   hpeyerl 
    787  1.30   mycroft 	elreset(sc);
    788   1.1   hpeyerl }
    789