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