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