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