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