Home | History | Annotate | Line # | Download | only in isa
if_el.c revision 1.62
      1 /*	$NetBSD: if_el.c,v 1.62 2000/11/15 01:02:18 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 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    246 	    IPL_NET, elintr, sc);
    247 
    248 #if NRND > 0
    249 	DPRINTF(("Attaching to random...\n"));
    250 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    251 			  RND_TYPE_NET, 0);
    252 #endif
    253 
    254 	DPRINTF(("elattach() finished.\n"));
    255 }
    256 
    257 /*
    258  * Reset interface.
    259  */
    260 void
    261 elreset(sc)
    262 	struct el_softc *sc;
    263 {
    264 	int s;
    265 
    266 	DPRINTF(("elreset()\n"));
    267 	s = splnet();
    268 	elstop(sc);
    269 	elinit(sc);
    270 	splx(s);
    271 }
    272 
    273 /*
    274  * Stop interface.
    275  */
    276 void
    277 elstop(sc)
    278 	struct el_softc *sc;
    279 {
    280 
    281 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, EL_AC, 0);
    282 }
    283 
    284 /*
    285  * Do a hardware reset of the board, and upload the ethernet address again in
    286  * case the board forgets.
    287  */
    288 static inline void
    289 el_hardreset(sc)
    290 	struct el_softc *sc;
    291 {
    292 	bus_space_tag_t iot = sc->sc_iot;
    293 	bus_space_handle_t ioh = sc->sc_ioh;
    294 	int i;
    295 
    296 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
    297 	delay(5);
    298 	bus_space_write_1(iot, ioh, EL_AC, 0);
    299 
    300 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    301 		bus_space_write_1(iot, ioh, i,
    302 		    LLADDR(sc->sc_ethercom.ec_if.if_sadl)[i]);
    303 }
    304 
    305 /*
    306  * Initialize interface.
    307  */
    308 void
    309 elinit(sc)
    310 	struct el_softc *sc;
    311 {
    312 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    313 	bus_space_tag_t iot = sc->sc_iot;
    314 	bus_space_handle_t ioh = sc->sc_ioh;
    315 
    316 	/* First, reset the board. */
    317 	el_hardreset(sc);
    318 
    319 	/* Configure rx. */
    320 	DPRINTF(("Configuring rx...\n"));
    321 	if (ifp->if_flags & IFF_PROMISC)
    322 		bus_space_write_1(iot, ioh, EL_RXC,
    323 		    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    324 		    EL_RXC_DOFLOW | EL_RXC_PROMISC);
    325 	else
    326 		bus_space_write_1(iot, ioh, EL_RXC,
    327 		    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    328 		    EL_RXC_DOFLOW | EL_RXC_ABROAD);
    329 	bus_space_write_1(iot, ioh, EL_RBC, 0);
    330 
    331 	/* Configure TX. */
    332 	DPRINTF(("Configuring tx...\n"));
    333 	bus_space_write_1(iot, ioh, EL_TXC, 0);
    334 
    335 	/* Start reception. */
    336 	DPRINTF(("Starting reception...\n"));
    337 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    338 
    339 	/* Set flags appropriately. */
    340 	ifp->if_flags |= IFF_RUNNING;
    341 	ifp->if_flags &= ~IFF_OACTIVE;
    342 
    343 	/* And start output. */
    344 	elstart(ifp);
    345 }
    346 
    347 /*
    348  * Start output on interface.  Get datagrams from the queue and output them,
    349  * giving the receiver a chance between datagrams.  Call only from splnet or
    350  * interrupt level!
    351  */
    352 void
    353 elstart(ifp)
    354 	struct ifnet *ifp;
    355 {
    356 	struct el_softc *sc = ifp->if_softc;
    357 	bus_space_tag_t iot = sc->sc_iot;
    358 	bus_space_handle_t ioh = sc->sc_ioh;
    359 	struct mbuf *m, *m0;
    360 	int s, i, off, retries;
    361 
    362 	DPRINTF(("elstart()...\n"));
    363 	s = splnet();
    364 
    365 	/* Don't do anything if output is active. */
    366 	if ((ifp->if_flags & IFF_OACTIVE) != 0) {
    367 		splx(s);
    368 		return;
    369 	}
    370 
    371 	ifp->if_flags |= IFF_OACTIVE;
    372 
    373 	/*
    374 	 * The main loop.  They warned me against endless loops, but would I
    375 	 * listen?  NOOO....
    376 	 */
    377 	for (;;) {
    378 		/* Dequeue the next datagram. */
    379 		IF_DEQUEUE(&ifp->if_snd, m0);
    380 
    381 		/* If there's nothing to send, return. */
    382 		if (m0 == 0)
    383 			break;
    384 
    385 #if NBPFILTER > 0
    386 		/* Give the packet to the bpf, if any. */
    387 		if (ifp->if_bpf)
    388 			bpf_mtap(ifp->if_bpf, m0);
    389 #endif
    390 
    391 		/* Disable the receiver. */
    392 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
    393 		bus_space_write_1(iot, ioh, EL_RBC, 0);
    394 
    395 		/* Transfer datagram to board. */
    396 		DPRINTF(("el: xfr pkt length=%d...\n", m0->m_pkthdr.len));
    397 		off = EL_BUFSIZ - max(m0->m_pkthdr.len,
    398 		    ETHER_MIN_LEN - ETHER_CRC_LEN);
    399 #ifdef DIAGNOSTIC
    400 		if ((off & 0xffff) != off)
    401 			printf("%s: bogus off 0x%x\n",
    402 			    sc->sc_dev.dv_xname, off);
    403 #endif
    404 		bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
    405 		bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
    406 
    407 		/* Copy the datagram to the buffer. */
    408 		for (m = m0; m != 0; m = m->m_next)
    409 			bus_space_write_multi_1(iot, ioh, EL_BUF,
    410 			    mtod(m, u_int8_t *), m->m_len);
    411 
    412 		m_freem(m0);
    413 
    414 		/* Now transmit the datagram. */
    415 		retries = 0;
    416 		for (;;) {
    417 			bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
    418 			bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
    419 			if (el_xmit(sc)) {
    420 				ifp->if_oerrors++;
    421 				break;
    422 			}
    423 			/* Check out status. */
    424 			i = bus_space_read_1(iot, ioh, EL_TXS);
    425 			DPRINTF(("tx status=0x%x\n", i));
    426 			if ((i & EL_TXS_READY) == 0) {
    427 				DPRINTF(("el: err txs=%x\n", i));
    428 				if (i & (EL_TXS_COLL | EL_TXS_COLL16)) {
    429 					ifp->if_collisions++;
    430 					if ((i & EL_TXC_DCOLL16) == 0 &&
    431 					    retries < 15) {
    432 						retries++;
    433 						bus_space_write_1(iot, ioh,
    434 						    EL_AC, EL_AC_HOST);
    435 					}
    436 				} else {
    437 					ifp->if_oerrors++;
    438 					break;
    439 				}
    440 			} else {
    441 				ifp->if_opackets++;
    442 				break;
    443 			}
    444 		}
    445 
    446 		/*
    447 		 * Now give the card a chance to receive.
    448 		 * Gotta love 3c501s...
    449 		 */
    450 		(void)bus_space_read_1(iot, ioh, EL_AS);
    451 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    452 		splx(s);
    453 		/* Interrupt here. */
    454 		s = splnet();
    455 	}
    456 
    457 	(void)bus_space_read_1(iot, ioh, EL_AS);
    458 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    459 	ifp->if_flags &= ~IFF_OACTIVE;
    460 	splx(s);
    461 }
    462 
    463 /*
    464  * This function actually attempts to transmit a datagram downloaded to the
    465  * board.  Call at splnet or interrupt, after downloading data!  Returns 0 on
    466  * success, non-0 on failure.
    467  */
    468 static int
    469 el_xmit(sc)
    470 	struct el_softc *sc;
    471 {
    472 	bus_space_tag_t iot = sc->sc_iot;
    473 	bus_space_handle_t ioh = sc->sc_ioh;
    474 	int i;
    475 
    476 	/*
    477 	 * XXX
    478 	 * This busy-waits for the tx completion.  Can we get an interrupt
    479 	 * instead?
    480 	 */
    481 
    482 	DPRINTF(("el: xmit..."));
    483 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_TXFRX);
    484 	i = 20000;
    485 	while ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_TXBUSY) && (i > 0))
    486 		i--;
    487 	if (i == 0) {
    488 		DPRINTF(("tx not ready\n"));
    489 		return -1;
    490 	}
    491 	DPRINTF(("%d cycles.\n", 20000 - i));
    492 	return 0;
    493 }
    494 
    495 /*
    496  * Controller interrupt.
    497  */
    498 int
    499 elintr(arg)
    500 	void *arg;
    501 {
    502 	struct el_softc *sc = arg;
    503 	bus_space_tag_t iot = sc->sc_iot;
    504 	bus_space_handle_t ioh = sc->sc_ioh;
    505 	u_int8_t rxstat;
    506 	int len;
    507 
    508 	DPRINTF(("elintr: "));
    509 
    510 	/* Check board status. */
    511 	if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0) {
    512 		(void)bus_space_read_1(iot, ioh, EL_RXC);
    513 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    514 		return 0;
    515 	}
    516 
    517 	for (;;) {
    518 		rxstat = bus_space_read_1(iot, ioh, EL_RXS);
    519 		if (rxstat & EL_RXS_STALE)
    520 			break;
    521 
    522 		/* If there's an overflow, reinit the board. */
    523 		if ((rxstat & EL_RXS_NOFLOW) == 0) {
    524 			DPRINTF(("overflow.\n"));
    525 			el_hardreset(sc);
    526 			/* Put board back into receive mode. */
    527 			if (sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC)
    528 				bus_space_write_1(iot, ioh, EL_RXC,
    529 				    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    530 				    EL_RXC_DOFLOW | EL_RXC_PROMISC);
    531 			else
    532 				bus_space_write_1(iot, ioh, EL_RXC,
    533 				    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    534 				    EL_RXC_DOFLOW | EL_RXC_ABROAD);
    535 			(void)bus_space_read_1(iot, ioh, EL_AS);
    536 			bus_space_write_1(iot, ioh, EL_RBC, 0);
    537 			break;
    538 		}
    539 
    540 		/* Incoming packet. */
    541 		len = bus_space_read_1(iot, ioh, EL_RBL);
    542 		len |= bus_space_read_1(iot, ioh, EL_RBH) << 8;
    543 		DPRINTF(("receive len=%d rxstat=%x ", len, rxstat));
    544 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
    545 
    546 		/* Pass data up to upper levels. */
    547 		elread(sc, len);
    548 
    549 		/* Is there another packet? */
    550 		if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0)
    551 			break;
    552 
    553 #if NRND > 0
    554 		rnd_add_uint32(&sc->rnd_source, rxstat);
    555 #endif
    556 
    557 		DPRINTF(("<rescan> "));
    558 	}
    559 
    560 	(void)bus_space_read_1(iot, ioh, EL_RXC);
    561 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    562 	return 1;
    563 }
    564 
    565 /*
    566  * Pass a packet to the higher levels.
    567  */
    568 void
    569 elread(sc, len)
    570 	struct el_softc *sc;
    571 	int len;
    572 {
    573 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    574 	struct mbuf *m;
    575 
    576 	if (len <= sizeof(struct ether_header) ||
    577 	    len > ETHER_MAX_LEN) {
    578 		printf("%s: invalid packet size %d; dropping\n",
    579 		    sc->sc_dev.dv_xname, len);
    580 		ifp->if_ierrors++;
    581 		return;
    582 	}
    583 
    584 	/* Pull packet off interface. */
    585 	m = elget(sc, len);
    586 	if (m == 0) {
    587 		ifp->if_ierrors++;
    588 		return;
    589 	}
    590 
    591 	ifp->if_ipackets++;
    592 
    593 #if NBPFILTER > 0
    594 	/*
    595 	 * Check if there's a BPF listener on this interface.
    596 	 * If so, hand off the raw packet to BPF.
    597 	 */
    598 	if (ifp->if_bpf)
    599 		bpf_mtap(ifp->if_bpf, m);
    600 #endif
    601 
    602 	(*ifp->if_input)(ifp, m);
    603 }
    604 
    605 /*
    606  * Pull read data off a interface.  Len is length of data, with local net
    607  * header stripped.  We copy the data into mbufs.  When full cluster sized
    608  * units are present we copy into clusters.
    609  */
    610 struct mbuf *
    611 elget(sc, totlen)
    612 	struct el_softc *sc;
    613 	int totlen;
    614 {
    615 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    616 	bus_space_tag_t iot = sc->sc_iot;
    617 	bus_space_handle_t ioh = sc->sc_ioh;
    618 	struct mbuf *m, *m0, *newm;
    619 	int len;
    620 
    621 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    622 	if (m0 == 0)
    623 		return (0);
    624 	m0->m_pkthdr.rcvif = ifp;
    625 	m0->m_pkthdr.len = totlen;
    626 	len = MHLEN;
    627 	m = m0;
    628 
    629 	bus_space_write_1(iot, ioh, EL_GPBL, 0);
    630 	bus_space_write_1(iot, ioh, EL_GPBH, 0);
    631 
    632 	while (totlen > 0) {
    633 		if (totlen >= MINCLSIZE) {
    634 			MCLGET(m, M_DONTWAIT);
    635 			if ((m->m_flags & M_EXT) == 0)
    636 				goto bad;
    637 			len = MCLBYTES;
    638 		}
    639 
    640 		m->m_len = len = min(totlen, len);
    641 		bus_space_read_multi_1(iot, ioh, EL_BUF, mtod(m, u_int8_t *), len);
    642 
    643 		totlen -= len;
    644 		if (totlen > 0) {
    645 			MGET(newm, M_DONTWAIT, MT_DATA);
    646 			if (newm == 0)
    647 				goto bad;
    648 			len = MLEN;
    649 			m = m->m_next = newm;
    650 		}
    651 	}
    652 
    653 	bus_space_write_1(iot, ioh, EL_RBC, 0);
    654 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RX);
    655 
    656 	return (m0);
    657 
    658 bad:
    659 	m_freem(m0);
    660 	return (0);
    661 }
    662 
    663 /*
    664  * Process an ioctl request. This code needs some work - it looks pretty ugly.
    665  */
    666 int
    667 elioctl(ifp, cmd, data)
    668 	struct ifnet *ifp;
    669 	u_long cmd;
    670 	caddr_t data;
    671 {
    672 	struct el_softc *sc = ifp->if_softc;
    673 	struct ifaddr *ifa = (struct ifaddr *)data;
    674 	int s, error = 0;
    675 
    676 	s = splnet();
    677 
    678 	switch (cmd) {
    679 
    680 	case SIOCSIFADDR:
    681 		ifp->if_flags |= IFF_UP;
    682 
    683 		switch (ifa->ifa_addr->sa_family) {
    684 #ifdef INET
    685 		case AF_INET:
    686 			elinit(sc);
    687 			arp_ifinit(ifp, ifa);
    688 			break;
    689 #endif
    690 #ifdef NS
    691 		/* XXX - This code is probably wrong. */
    692 		case AF_NS:
    693 		    {
    694 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    695 
    696 			if (ns_nullhost(*ina))
    697 				ina->x_host =
    698 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    699 			else
    700 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    701 				    ETHER_ADDR_LEN);
    702 			/* Set new address. */
    703 			elinit(sc);
    704 			break;
    705 		    }
    706 #endif
    707 		default:
    708 			elinit(sc);
    709 			break;
    710 		}
    711 		break;
    712 
    713 	case SIOCSIFFLAGS:
    714 		if ((ifp->if_flags & IFF_UP) == 0 &&
    715 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    716 			/*
    717 			 * If interface is marked down and it is running, then
    718 			 * stop it.
    719 			 */
    720 			elstop(sc);
    721 			ifp->if_flags &= ~IFF_RUNNING;
    722 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    723 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    724 			/*
    725 			 * If interface is marked up and it is stopped, then
    726 			 * start it.
    727 			 */
    728 			elinit(sc);
    729 		} else {
    730 			/*
    731 			 * Some other important flag might have changed, so
    732 			 * reset.
    733 			 */
    734 			elreset(sc);
    735 		}
    736 		break;
    737 
    738 	default:
    739 		error = EINVAL;
    740 		break;
    741 	}
    742 
    743 	splx(s);
    744 	return error;
    745 }
    746 
    747 /*
    748  * Device timeout routine.
    749  */
    750 void
    751 elwatchdog(ifp)
    752 	struct ifnet *ifp;
    753 {
    754 	struct el_softc *sc = ifp->if_softc;
    755 
    756 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    757 	sc->sc_ethercom.ec_if.if_oerrors++;
    758 
    759 	elreset(sc);
    760 }
    761