Home | History | Annotate | Line # | Download | only in isa
if_el.c revision 1.80.4.3
      1 /*	$NetBSD: if_el.c,v 1.80.4.3 2010/03/11 15:03:37 yamt 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 <sys/cdefs.h>
     22 __KERNEL_RCSID(0, "$NetBSD: if_el.c,v 1.80.4.3 2010/03/11 15:03:37 yamt Exp $");
     23 
     24 #include "opt_inet.h"
     25 #include "rnd.h"
     26 
     27 #include <sys/param.h>
     28 #include <sys/systm.h>
     29 #include <sys/errno.h>
     30 #include <sys/ioctl.h>
     31 #include <sys/mbuf.h>
     32 #include <sys/socket.h>
     33 #include <sys/syslog.h>
     34 #include <sys/device.h>
     35 #if NRND > 0
     36 #include <sys/rnd.h>
     37 #endif
     38 
     39 #include <net/if.h>
     40 #include <net/if_dl.h>
     41 #include <net/if_types.h>
     42 
     43 #include <net/if_ether.h>
     44 
     45 #ifdef INET
     46 #include <netinet/in.h>
     47 #include <netinet/in_systm.h>
     48 #include <netinet/in_var.h>
     49 #include <netinet/ip.h>
     50 #include <netinet/if_inarp.h>
     51 #endif
     52 
     53 
     54 #include <net/bpf.h>
     55 #include <net/bpfdesc.h>
     56 
     57 #include <sys/cpu.h>
     58 #include <sys/intr.h>
     59 #include <sys/bus.h>
     60 
     61 #include <dev/isa/isavar.h>
     62 #include <dev/isa/if_elreg.h>
     63 
     64 /* for debugging convenience */
     65 #ifdef EL_DEBUG
     66 #define DPRINTF(x) printf x
     67 #else
     68 #define DPRINTF(x)
     69 #endif
     70 
     71 /*
     72  * per-line info and status
     73  */
     74 struct el_softc {
     75 	struct device sc_dev;
     76 	void *sc_ih;
     77 
     78 	struct ethercom sc_ethercom;	/* ethernet common */
     79 	bus_space_tag_t sc_iot;		/* bus space identifier */
     80 	bus_space_handle_t sc_ioh;	/* i/o handle */
     81 
     82 #if NRND > 0
     83 	rndsource_element_t rnd_source;
     84 #endif
     85 };
     86 
     87 /*
     88  * prototypes
     89  */
     90 int elintr(void *);
     91 void elinit(struct el_softc *);
     92 int elioctl(struct ifnet *, u_long, void *);
     93 void elstart(struct ifnet *);
     94 void elwatchdog(struct ifnet *);
     95 void elreset(struct el_softc *);
     96 void elstop(struct el_softc *);
     97 static int el_xmit(struct el_softc *);
     98 void elread(struct el_softc *, int);
     99 struct mbuf *elget(struct el_softc *sc, int);
    100 static inline void el_hardreset(struct el_softc *);
    101 
    102 int elprobe(device_t, cfdata_t, void *);
    103 void elattach(device_t, device_t, void *);
    104 
    105 CFATTACH_DECL(el, sizeof(struct el_softc),
    106     elprobe, elattach, NULL, NULL);
    107 
    108 /*
    109  * Probe routine.
    110  *
    111  * See if the card is there and at the right place.
    112  * (XXX - cgd -- needs help)
    113  */
    114 int
    115 elprobe(device_t parent, cfdata_t match, void *aux)
    116 {
    117 	struct isa_attach_args *ia = aux;
    118 	bus_space_tag_t iot = ia->ia_iot;
    119 	bus_space_handle_t ioh;
    120 	int iobase;
    121 	u_int8_t station_addr[ETHER_ADDR_LEN];
    122 	u_int8_t i;
    123 	int rval;
    124 
    125 	rval = 0;
    126 
    127 	if (ia->ia_nio < 1)
    128 		return (0);
    129 	if (ia->ia_nirq < 1)
    130 		return (0);
    131 
    132 	if (ISA_DIRECT_CONFIG(ia))
    133 		return (0);
    134 
    135 	iobase = ia->ia_io[0].ir_addr;
    136 
    137 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    138 		return (0);
    139 	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
    140 		return (0);
    141 
    142 	/* First check the base. */
    143 	if (iobase < 0x200 || iobase > 0x3f0)
    144 		return 0;
    145 
    146 	/* Map i/o space. */
    147 	if (bus_space_map(iot, iobase, 16, 0, &ioh))
    148 		return 0;
    149 
    150 	/*
    151 	 * Now attempt to grab the station address from the PROM and see if it
    152 	 * contains the 3com vendor code.
    153 	 */
    154 	DPRINTF(("Probing 3c501 at 0x%x...\n", iobase));
    155 
    156 	/* Reset the board. */
    157 	DPRINTF(("Resetting board...\n"));
    158 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
    159 	delay(5);
    160 	bus_space_write_1(iot, ioh, EL_AC, 0);
    161 
    162 	/* Now read the address. */
    163 	DPRINTF(("Reading station address...\n"));
    164 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    165 		bus_space_write_1(iot, ioh, EL_GPBL, i);
    166 		station_addr[i] = bus_space_read_1(iot, ioh, EL_EAW);
    167 	}
    168 	DPRINTF(("Address is %s\n", ether_sprintf(station_addr)));
    169 
    170 	/*
    171 	 * If the vendor code is ok, return a 1.  We'll assume that whoever
    172 	 * configured this system is right about the IRQ.
    173 	 */
    174 	if (station_addr[0] != 0x02 || station_addr[1] != 0x60 ||
    175 	    station_addr[2] != 0x8c) {
    176 		DPRINTF(("Bad vendor code.\n"));
    177 		goto out;
    178 	}
    179 	DPRINTF(("Vendor code ok.\n"));
    180 
    181 	ia->ia_nio = 1;
    182 	ia->ia_io[0].ir_size = 16;
    183 
    184 	ia->ia_nirq = 1;
    185 
    186 	ia->ia_niomem = 0;
    187 	ia->ia_ndrq = 0;
    188 
    189 	rval = 1;
    190 
    191  out:
    192 	bus_space_unmap(iot, ioh, 16);
    193 	return rval;
    194 }
    195 
    196 /*
    197  * Attach the interface to the kernel data structures.  By the time this is
    198  * called, we know that the card exists at the given I/O address.  We still
    199  * assume that the IRQ given is correct.
    200  */
    201 void
    202 elattach(device_t parent, device_t self, void *aux)
    203 {
    204 	struct el_softc *sc = (void *)self;
    205 	struct isa_attach_args *ia = aux;
    206 	bus_space_tag_t iot = ia->ia_iot;
    207 	bus_space_handle_t ioh;
    208 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    209 	u_int8_t myaddr[ETHER_ADDR_LEN];
    210 	u_int8_t i;
    211 
    212 	printf("\n");
    213 
    214 	DPRINTF(("Attaching %s...\n", device_xname(&sc->sc_dev)));
    215 
    216 	/* Map i/o space. */
    217 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, 16, 0, &ioh)) {
    218 		aprint_error_dev(self, "can't map i/o space\n");
    219 		return;
    220 	}
    221 
    222 	sc->sc_iot = iot;
    223 	sc->sc_ioh = ioh;
    224 
    225 	/* Reset the board. */
    226 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
    227 	delay(5);
    228 	bus_space_write_1(iot, ioh, EL_AC, 0);
    229 
    230 	/* Now read the address. */
    231 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    232 		bus_space_write_1(iot, ioh, EL_GPBL, i);
    233 		myaddr[i] = bus_space_read_1(iot, ioh, EL_EAW);
    234 	}
    235 
    236 	/* Stop the board. */
    237 	elstop(sc);
    238 
    239 	/* Initialize ifnet structure. */
    240 	strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
    241 	ifp->if_softc = sc;
    242 	ifp->if_start = elstart;
    243 	ifp->if_ioctl = elioctl;
    244 	ifp->if_watchdog = elwatchdog;
    245 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    246 	IFQ_SET_READY(&ifp->if_snd);
    247 
    248 	/* Now we can attach the interface. */
    249 	DPRINTF(("Attaching interface...\n"));
    250 	if_attach(ifp);
    251 	ether_ifattach(ifp, myaddr);
    252 
    253 	/* Print out some information for the user. */
    254 	printf("%s: address %s\n", device_xname(self), ether_sprintf(myaddr));
    255 
    256 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    257 	    IST_EDGE, IPL_NET, elintr, sc);
    258 
    259 #if NRND > 0
    260 	DPRINTF(("Attaching to random...\n"));
    261 	rnd_attach_source(&sc->rnd_source, device_xname(&sc->sc_dev),
    262 			  RND_TYPE_NET, 0);
    263 #endif
    264 
    265 	DPRINTF(("elattach() finished.\n"));
    266 }
    267 
    268 /*
    269  * Reset interface.
    270  */
    271 void
    272 elreset(struct el_softc *sc)
    273 {
    274 	int s;
    275 
    276 	DPRINTF(("elreset()\n"));
    277 	s = splnet();
    278 	elstop(sc);
    279 	elinit(sc);
    280 	splx(s);
    281 }
    282 
    283 /*
    284  * Stop interface.
    285  */
    286 void
    287 elstop(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(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 		    CLLADDR(sc->sc_ethercom.ec_if.if_sadl)[i]);
    311 }
    312 
    313 /*
    314  * Initialize interface.
    315  */
    316 void
    317 elinit(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(struct ifnet *ifp)
    361 {
    362 	struct el_softc *sc = ifp->if_softc;
    363 	bus_space_tag_t iot = sc->sc_iot;
    364 	bus_space_handle_t ioh = sc->sc_ioh;
    365 	struct mbuf *m, *m0;
    366 	int s, i, off, retries;
    367 
    368 	DPRINTF(("elstart()...\n"));
    369 	s = splnet();
    370 
    371 	/* Don't do anything if output is active. */
    372 	if ((ifp->if_flags & IFF_OACTIVE) != 0) {
    373 		splx(s);
    374 		return;
    375 	}
    376 
    377 	ifp->if_flags |= IFF_OACTIVE;
    378 
    379 	/*
    380 	 * The main loop.  They warned me against endless loops, but would I
    381 	 * listen?  NOOO....
    382 	 */
    383 	for (;;) {
    384 		/* Dequeue the next datagram. */
    385 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    386 
    387 		/* If there's nothing to send, return. */
    388 		if (m0 == 0)
    389 			break;
    390 
    391 		/* Give the packet to the bpf, if any. */
    392 		if (ifp->if_bpf)
    393 			bpf_ops->bpf_mtap(ifp->if_bpf, m0);
    394 
    395 		/* Disable the receiver. */
    396 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
    397 		bus_space_write_1(iot, ioh, EL_RBC, 0);
    398 
    399 		/* Transfer datagram to board. */
    400 		DPRINTF(("el: xfr pkt length=%d...\n", m0->m_pkthdr.len));
    401 		off = EL_BUFSIZ - max(m0->m_pkthdr.len,
    402 		    ETHER_MIN_LEN - ETHER_CRC_LEN);
    403 #ifdef DIAGNOSTIC
    404 		if ((off & 0xffff) != off)
    405 			printf("%s: bogus off 0x%x\n",
    406 			    device_xname(&sc->sc_dev), off);
    407 #endif
    408 		bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
    409 		bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
    410 
    411 		/* Copy the datagram to the buffer. */
    412 		for (m = m0; m != 0; m = m->m_next)
    413 			bus_space_write_multi_1(iot, ioh, EL_BUF,
    414 			    mtod(m, u_int8_t *), m->m_len);
    415 		for (i = 0;
    416 		    i < ETHER_MIN_LEN - ETHER_CRC_LEN - m0->m_pkthdr.len; i++)
    417 			bus_space_write_1(iot, ioh, EL_BUF, 0);
    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(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(void *arg)
    506 {
    507 	struct el_softc *sc = arg;
    508 	bus_space_tag_t iot = sc->sc_iot;
    509 	bus_space_handle_t ioh = sc->sc_ioh;
    510 	u_int8_t rxstat;
    511 	int len;
    512 
    513 	DPRINTF(("elintr: "));
    514 
    515 	/* Check board status. */
    516 	if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0) {
    517 		(void)bus_space_read_1(iot, ioh, EL_RXC);
    518 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    519 		return 0;
    520 	}
    521 
    522 	for (;;) {
    523 		rxstat = bus_space_read_1(iot, ioh, EL_RXS);
    524 		if (rxstat & EL_RXS_STALE)
    525 			break;
    526 
    527 		/* If there's an overflow, reinit the board. */
    528 		if ((rxstat & EL_RXS_NOFLOW) == 0) {
    529 			DPRINTF(("overflow.\n"));
    530 			el_hardreset(sc);
    531 			/* Put board back into receive mode. */
    532 			if (sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC)
    533 				bus_space_write_1(iot, ioh, EL_RXC,
    534 				    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    535 				    EL_RXC_DOFLOW | EL_RXC_PROMISC);
    536 			else
    537 				bus_space_write_1(iot, ioh, EL_RXC,
    538 				    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    539 				    EL_RXC_DOFLOW | EL_RXC_ABROAD);
    540 			(void)bus_space_read_1(iot, ioh, EL_AS);
    541 			bus_space_write_1(iot, ioh, EL_RBC, 0);
    542 			break;
    543 		}
    544 
    545 		/* Incoming packet. */
    546 		len = bus_space_read_1(iot, ioh, EL_RBL);
    547 		len |= bus_space_read_1(iot, ioh, EL_RBH) << 8;
    548 		DPRINTF(("receive len=%d rxstat=%x ", len, rxstat));
    549 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
    550 
    551 		/* Pass data up to upper levels. */
    552 		elread(sc, len);
    553 
    554 		/* Is there another packet? */
    555 		if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0)
    556 			break;
    557 
    558 #if NRND > 0
    559 		rnd_add_uint32(&sc->rnd_source, rxstat);
    560 #endif
    561 
    562 		DPRINTF(("<rescan> "));
    563 	}
    564 
    565 	(void)bus_space_read_1(iot, ioh, EL_RXC);
    566 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    567 	return 1;
    568 }
    569 
    570 /*
    571  * Pass a packet to the higher levels.
    572  */
    573 void
    574 elread(struct el_softc *sc, int len)
    575 {
    576 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    577 	struct mbuf *m;
    578 
    579 	if (len <= sizeof(struct ether_header) ||
    580 	    len > ETHER_MAX_LEN) {
    581 		printf("%s: invalid packet size %d; dropping\n",
    582 		    device_xname(&sc->sc_dev), len);
    583 		ifp->if_ierrors++;
    584 		return;
    585 	}
    586 
    587 	/* Pull packet off interface. */
    588 	m = elget(sc, len);
    589 	if (m == 0) {
    590 		ifp->if_ierrors++;
    591 		return;
    592 	}
    593 
    594 	ifp->if_ipackets++;
    595 
    596 	/*
    597 	 * Check if there's a BPF listener on this interface.
    598 	 * If so, hand off the raw packet to BPF.
    599 	 */
    600 	if (ifp->if_bpf)
    601 		bpf_ops->bpf_mtap(ifp->if_bpf, m);
    602 
    603 	(*ifp->if_input)(ifp, m);
    604 }
    605 
    606 /*
    607  * Pull read data off a interface.  Len is length of data, with local net
    608  * header stripped.  We copy the data into mbufs.  When full cluster sized
    609  * units are present we copy into clusters.
    610  */
    611 struct mbuf *
    612 elget(struct el_softc *sc, int totlen)
    613 {
    614 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    615 	bus_space_tag_t iot = sc->sc_iot;
    616 	bus_space_handle_t ioh = sc->sc_ioh;
    617 	struct mbuf *m, *m0, *newm;
    618 	int len;
    619 
    620 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    621 	if (m0 == 0)
    622 		return (0);
    623 	m0->m_pkthdr.rcvif = ifp;
    624 	m0->m_pkthdr.len = totlen;
    625 	len = MHLEN;
    626 	m = m0;
    627 
    628 	bus_space_write_1(iot, ioh, EL_GPBL, 0);
    629 	bus_space_write_1(iot, ioh, EL_GPBH, 0);
    630 
    631 	while (totlen > 0) {
    632 		if (totlen >= MINCLSIZE) {
    633 			MCLGET(m, M_DONTWAIT);
    634 			if ((m->m_flags & M_EXT) == 0)
    635 				goto bad;
    636 			len = MCLBYTES;
    637 		}
    638 
    639 		m->m_len = len = min(totlen, len);
    640 		bus_space_read_multi_1(iot, ioh, EL_BUF, mtod(m, u_int8_t *), len);
    641 
    642 		totlen -= len;
    643 		if (totlen > 0) {
    644 			MGET(newm, M_DONTWAIT, MT_DATA);
    645 			if (newm == 0)
    646 				goto bad;
    647 			len = MLEN;
    648 			m = m->m_next = newm;
    649 		}
    650 	}
    651 
    652 	bus_space_write_1(iot, ioh, EL_RBC, 0);
    653 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RX);
    654 
    655 	return (m0);
    656 
    657 bad:
    658 	m_freem(m0);
    659 	return (0);
    660 }
    661 
    662 /*
    663  * Process an ioctl request. This code needs some work - it looks pretty ugly.
    664  */
    665 int
    666 elioctl(struct ifnet *ifp, u_long cmd, void *data)
    667 {
    668 	struct el_softc *sc = ifp->if_softc;
    669 	struct ifaddr *ifa = (struct ifaddr *)data;
    670 	int s, error = 0;
    671 
    672 	s = splnet();
    673 
    674 	switch (cmd) {
    675 
    676 	case SIOCINITIFADDR:
    677 		ifp->if_flags |= IFF_UP;
    678 
    679 		elinit(sc);
    680 		switch (ifa->ifa_addr->sa_family) {
    681 #ifdef INET
    682 		case AF_INET:
    683 			arp_ifinit(ifp, ifa);
    684 			break;
    685 #endif
    686 		default:
    687 			break;
    688 		}
    689 		break;
    690 
    691 	case SIOCSIFFLAGS:
    692 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    693 			break;
    694 		/* XXX re-use ether_ioctl() */
    695 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    696 		case IFF_RUNNING:
    697 			/*
    698 			 * If interface is marked down and it is running, then
    699 			 * stop it.
    700 			 */
    701 			elstop(sc);
    702 			ifp->if_flags &= ~IFF_RUNNING;
    703 			break;
    704 		case IFF_UP:
    705 			/*
    706 			 * If interface is marked up and it is stopped, then
    707 			 * start it.
    708 			 */
    709 			elinit(sc);
    710 			break;
    711 		default:
    712 			/*
    713 			 * Some other important flag might have changed, so
    714 			 * reset.
    715 			 */
    716 			elreset(sc);
    717 			break;
    718 		}
    719 		break;
    720 
    721 	default:
    722 		error = ether_ioctl(ifp, cmd, data);
    723 		break;
    724 	}
    725 
    726 	splx(s);
    727 	return error;
    728 }
    729 
    730 /*
    731  * Device timeout routine.
    732  */
    733 void
    734 elwatchdog(struct ifnet *ifp)
    735 {
    736 	struct el_softc *sc = ifp->if_softc;
    737 
    738 	log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->sc_dev));
    739 	sc->sc_ethercom.ec_if.if_oerrors++;
    740 
    741 	elreset(sc);
    742 }
    743