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