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