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