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