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