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