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