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