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