Home | History | Annotate | Line # | Download | only in isa
if_el.c revision 1.87
      1 /*	$NetBSD: if_el.c,v 1.87 2011/11/19 22:51:23 tls 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.87 2011/11/19 22:51:23 tls 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 	krndsource_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 		bpf_mtap(ifp, m0);
    393 
    394 		/* Disable the receiver. */
    395 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
    396 		bus_space_write_1(iot, ioh, EL_RBC, 0);
    397 
    398 		/* Transfer datagram to board. */
    399 		DPRINTF(("el: xfr pkt length=%d...\n", m0->m_pkthdr.len));
    400 		off = EL_BUFSIZ - max(m0->m_pkthdr.len,
    401 		    ETHER_MIN_LEN - ETHER_CRC_LEN);
    402 #ifdef DIAGNOSTIC
    403 		if ((off & 0xffff) != off)
    404 			printf("%s: bogus off 0x%x\n",
    405 			    device_xname(&sc->sc_dev), off);
    406 #endif
    407 		bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
    408 		bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
    409 
    410 		/* Copy the datagram to the buffer. */
    411 		for (m = m0; m != 0; m = m->m_next)
    412 			bus_space_write_multi_1(iot, ioh, EL_BUF,
    413 			    mtod(m, u_int8_t *), m->m_len);
    414 		for (i = 0;
    415 		    i < ETHER_MIN_LEN - ETHER_CRC_LEN - m0->m_pkthdr.len; i++)
    416 			bus_space_write_1(iot, ioh, EL_BUF, 0);
    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(struct el_softc *sc)
    476 {
    477 	bus_space_tag_t iot = sc->sc_iot;
    478 	bus_space_handle_t ioh = sc->sc_ioh;
    479 	int i;
    480 
    481 	/*
    482 	 * XXX
    483 	 * This busy-waits for the tx completion.  Can we get an interrupt
    484 	 * instead?
    485 	 */
    486 
    487 	DPRINTF(("el: xmit..."));
    488 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_TXFRX);
    489 	i = 20000;
    490 	while ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_TXBUSY) && (i > 0))
    491 		i--;
    492 	if (i == 0) {
    493 		DPRINTF(("tx not ready\n"));
    494 		return -1;
    495 	}
    496 	DPRINTF(("%d cycles.\n", 20000 - i));
    497 	return 0;
    498 }
    499 
    500 /*
    501  * Controller interrupt.
    502  */
    503 int
    504 elintr(void *arg)
    505 {
    506 	struct el_softc *sc = arg;
    507 	bus_space_tag_t iot = sc->sc_iot;
    508 	bus_space_handle_t ioh = sc->sc_ioh;
    509 	u_int8_t rxstat;
    510 	int len;
    511 
    512 	DPRINTF(("elintr: "));
    513 
    514 	/* Check board status. */
    515 	if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0) {
    516 		(void)bus_space_read_1(iot, ioh, EL_RXC);
    517 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    518 		return 0;
    519 	}
    520 
    521 	for (;;) {
    522 		rxstat = bus_space_read_1(iot, ioh, EL_RXS);
    523 		if (rxstat & EL_RXS_STALE)
    524 			break;
    525 
    526 		/* If there's an overflow, reinit the board. */
    527 		if ((rxstat & EL_RXS_NOFLOW) == 0) {
    528 			DPRINTF(("overflow.\n"));
    529 			el_hardreset(sc);
    530 			/* Put board back into receive mode. */
    531 			if (sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC)
    532 				bus_space_write_1(iot, ioh, EL_RXC,
    533 				    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    534 				    EL_RXC_DOFLOW | EL_RXC_PROMISC);
    535 			else
    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_ABROAD);
    539 			(void)bus_space_read_1(iot, ioh, EL_AS);
    540 			bus_space_write_1(iot, ioh, EL_RBC, 0);
    541 			break;
    542 		}
    543 
    544 		/* Incoming packet. */
    545 		len = bus_space_read_1(iot, ioh, EL_RBL);
    546 		len |= bus_space_read_1(iot, ioh, EL_RBH) << 8;
    547 		DPRINTF(("receive len=%d rxstat=%x ", len, rxstat));
    548 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
    549 
    550 		/* Pass data up to upper levels. */
    551 		elread(sc, len);
    552 
    553 		/* Is there another packet? */
    554 		if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0)
    555 			break;
    556 
    557 #if NRND > 0
    558 		rnd_add_uint32(&sc->rnd_source, rxstat);
    559 #endif
    560 
    561 		DPRINTF(("<rescan> "));
    562 	}
    563 
    564 	(void)bus_space_read_1(iot, ioh, EL_RXC);
    565 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    566 	return 1;
    567 }
    568 
    569 /*
    570  * Pass a packet to the higher levels.
    571  */
    572 void
    573 elread(struct el_softc *sc, int len)
    574 {
    575 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    576 	struct mbuf *m;
    577 
    578 	if (len <= sizeof(struct ether_header) ||
    579 	    len > ETHER_MAX_LEN) {
    580 		printf("%s: invalid packet size %d; dropping\n",
    581 		    device_xname(&sc->sc_dev), len);
    582 		ifp->if_ierrors++;
    583 		return;
    584 	}
    585 
    586 	/* Pull packet off interface. */
    587 	m = elget(sc, len);
    588 	if (m == 0) {
    589 		ifp->if_ierrors++;
    590 		return;
    591 	}
    592 
    593 	ifp->if_ipackets++;
    594 
    595 	/*
    596 	 * Check if there's a BPF listener on this interface.
    597 	 * If so, hand off the raw packet to BPF.
    598 	 */
    599 	bpf_mtap(ifp, m);
    600 
    601 	(*ifp->if_input)(ifp, m);
    602 }
    603 
    604 /*
    605  * Pull read data off a interface.  Len is length of data, with local net
    606  * header stripped.  We copy the data into mbufs.  When full cluster sized
    607  * units are present we copy into clusters.
    608  */
    609 struct mbuf *
    610 elget(struct el_softc *sc, int totlen)
    611 {
    612 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    613 	bus_space_tag_t iot = sc->sc_iot;
    614 	bus_space_handle_t ioh = sc->sc_ioh;
    615 	struct mbuf *m, *m0, *newm;
    616 	int len;
    617 
    618 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    619 	if (m0 == 0)
    620 		return (0);
    621 	m0->m_pkthdr.rcvif = ifp;
    622 	m0->m_pkthdr.len = totlen;
    623 	len = MHLEN;
    624 	m = m0;
    625 
    626 	bus_space_write_1(iot, ioh, EL_GPBL, 0);
    627 	bus_space_write_1(iot, ioh, EL_GPBH, 0);
    628 
    629 	while (totlen > 0) {
    630 		if (totlen >= MINCLSIZE) {
    631 			MCLGET(m, M_DONTWAIT);
    632 			if ((m->m_flags & M_EXT) == 0)
    633 				goto bad;
    634 			len = MCLBYTES;
    635 		}
    636 
    637 		m->m_len = len = min(totlen, len);
    638 		bus_space_read_multi_1(iot, ioh, EL_BUF, mtod(m, u_int8_t *), len);
    639 
    640 		totlen -= len;
    641 		if (totlen > 0) {
    642 			MGET(newm, M_DONTWAIT, MT_DATA);
    643 			if (newm == 0)
    644 				goto bad;
    645 			len = MLEN;
    646 			m = m->m_next = newm;
    647 		}
    648 	}
    649 
    650 	bus_space_write_1(iot, ioh, EL_RBC, 0);
    651 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RX);
    652 
    653 	return (m0);
    654 
    655 bad:
    656 	m_freem(m0);
    657 	return (0);
    658 }
    659 
    660 /*
    661  * Process an ioctl request. This code needs some work - it looks pretty ugly.
    662  */
    663 int
    664 elioctl(struct ifnet *ifp, u_long cmd, void *data)
    665 {
    666 	struct el_softc *sc = ifp->if_softc;
    667 	struct ifaddr *ifa = (struct ifaddr *)data;
    668 	int s, error = 0;
    669 
    670 	s = splnet();
    671 
    672 	switch (cmd) {
    673 
    674 	case SIOCINITIFADDR:
    675 		ifp->if_flags |= IFF_UP;
    676 
    677 		elinit(sc);
    678 		switch (ifa->ifa_addr->sa_family) {
    679 #ifdef INET
    680 		case AF_INET:
    681 			arp_ifinit(ifp, ifa);
    682 			break;
    683 #endif
    684 		default:
    685 			break;
    686 		}
    687 		break;
    688 
    689 	case SIOCSIFFLAGS:
    690 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    691 			break;
    692 		/* XXX re-use ether_ioctl() */
    693 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    694 		case IFF_RUNNING:
    695 			/*
    696 			 * If interface is marked down and it is running, then
    697 			 * stop it.
    698 			 */
    699 			elstop(sc);
    700 			ifp->if_flags &= ~IFF_RUNNING;
    701 			break;
    702 		case IFF_UP:
    703 			/*
    704 			 * If interface is marked up and it is stopped, then
    705 			 * start it.
    706 			 */
    707 			elinit(sc);
    708 			break;
    709 		default:
    710 			/*
    711 			 * Some other important flag might have changed, so
    712 			 * reset.
    713 			 */
    714 			elreset(sc);
    715 			break;
    716 		}
    717 		break;
    718 
    719 	default:
    720 		error = ether_ioctl(ifp, cmd, data);
    721 		break;
    722 	}
    723 
    724 	splx(s);
    725 	return error;
    726 }
    727 
    728 /*
    729  * Device timeout routine.
    730  */
    731 void
    732 elwatchdog(struct ifnet *ifp)
    733 {
    734 	struct el_softc *sc = ifp->if_softc;
    735 
    736 	log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->sc_dev));
    737 	sc->sc_ethercom.ec_if.if_oerrors++;
    738 
    739 	elreset(sc);
    740 }
    741