Home | History | Annotate | Line # | Download | only in isa
if_el.c revision 1.56
      1 /*	$NetBSD: if_el.c,v 1.56 1999/02/28 17:09:26 explorer 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 = &sc->sc_ethercom.ec_if;
    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 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    235 	ifp->if_softc = sc;
    236 	ifp->if_start = elstart;
    237 	ifp->if_ioctl = elioctl;
    238 	ifp->if_watchdog = elwatchdog;
    239 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    240 
    241 	/* Now we can attach the interface. */
    242 	DPRINTF(("Attaching interface...\n"));
    243 	if_attach(ifp);
    244 	ether_ifattach(ifp, myaddr);
    245 
    246 	/* Print out some information for the user. */
    247 	printf("%s: address %s\n", self->dv_xname, ether_sprintf(myaddr));
    248 
    249 	/* Finally, attach to bpf filter if it is present. */
    250 #if NBPFILTER > 0
    251 	DPRINTF(("Attaching to BPF...\n"));
    252 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    253 #endif
    254 
    255 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    256 	    IPL_NET, elintr, sc);
    257 
    258 #if NRND > 0
    259 	DPRINTF(("Attaching to random...\n"));
    260 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    261 			  RND_TYPE_NET, 0);
    262 #endif
    263 
    264 	DPRINTF(("elattach() finished.\n"));
    265 }
    266 
    267 /*
    268  * Reset interface.
    269  */
    270 void
    271 elreset(sc)
    272 	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(sc)
    288 	struct el_softc *sc;
    289 {
    290 
    291 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, EL_AC, 0);
    292 }
    293 
    294 /*
    295  * Do a hardware reset of the board, and upload the ethernet address again in
    296  * case the board forgets.
    297  */
    298 static inline void
    299 el_hardreset(sc)
    300 	struct el_softc *sc;
    301 {
    302 	bus_space_tag_t iot = sc->sc_iot;
    303 	bus_space_handle_t ioh = sc->sc_ioh;
    304 	int i;
    305 
    306 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
    307 	delay(5);
    308 	bus_space_write_1(iot, ioh, EL_AC, 0);
    309 
    310 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    311 		bus_space_write_1(iot, ioh, i,
    312 		    LLADDR(sc->sc_ethercom.ec_if.if_sadl)[i]);
    313 }
    314 
    315 /*
    316  * Initialize interface.
    317  */
    318 void
    319 elinit(sc)
    320 	struct el_softc *sc;
    321 {
    322 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    323 	bus_space_tag_t iot = sc->sc_iot;
    324 	bus_space_handle_t ioh = sc->sc_ioh;
    325 
    326 	/* First, reset the board. */
    327 	el_hardreset(sc);
    328 
    329 	/* Configure rx. */
    330 	DPRINTF(("Configuring rx...\n"));
    331 	if (ifp->if_flags & IFF_PROMISC)
    332 		bus_space_write_1(iot, ioh, EL_RXC,
    333 		    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    334 		    EL_RXC_DOFLOW | EL_RXC_PROMISC);
    335 	else
    336 		bus_space_write_1(iot, ioh, EL_RXC,
    337 		    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    338 		    EL_RXC_DOFLOW | EL_RXC_ABROAD);
    339 	bus_space_write_1(iot, ioh, EL_RBC, 0);
    340 
    341 	/* Configure TX. */
    342 	DPRINTF(("Configuring tx...\n"));
    343 	bus_space_write_1(iot, ioh, EL_TXC, 0);
    344 
    345 	/* Start reception. */
    346 	DPRINTF(("Starting reception...\n"));
    347 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    348 
    349 	/* Set flags appropriately. */
    350 	ifp->if_flags |= IFF_RUNNING;
    351 	ifp->if_flags &= ~IFF_OACTIVE;
    352 
    353 	/* And start output. */
    354 	elstart(ifp);
    355 }
    356 
    357 /*
    358  * Start output on interface.  Get datagrams from the queue and output them,
    359  * giving the receiver a chance between datagrams.  Call only from splnet or
    360  * interrupt level!
    361  */
    362 void
    363 elstart(ifp)
    364 	struct ifnet *ifp;
    365 {
    366 	struct el_softc *sc = ifp->if_softc;
    367 	bus_space_tag_t iot = sc->sc_iot;
    368 	bus_space_handle_t ioh = sc->sc_ioh;
    369 	struct mbuf *m, *m0;
    370 	int s, i, off, retries;
    371 
    372 	DPRINTF(("elstart()...\n"));
    373 	s = splnet();
    374 
    375 	/* Don't do anything if output is active. */
    376 	if ((ifp->if_flags & IFF_OACTIVE) != 0) {
    377 		splx(s);
    378 		return;
    379 	}
    380 
    381 	ifp->if_flags |= IFF_OACTIVE;
    382 
    383 	/*
    384 	 * The main loop.  They warned me against endless loops, but would I
    385 	 * listen?  NOOO....
    386 	 */
    387 	for (;;) {
    388 		/* Dequeue the next datagram. */
    389 		IF_DEQUEUE(&ifp->if_snd, m0);
    390 
    391 		/* If there's nothing to send, return. */
    392 		if (m0 == 0)
    393 			break;
    394 
    395 #if NBPFILTER > 0
    396 		/* Give the packet to the bpf, if any. */
    397 		if (ifp->if_bpf)
    398 			bpf_mtap(ifp->if_bpf, m0);
    399 #endif
    400 
    401 		/* Disable the receiver. */
    402 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
    403 		bus_space_write_1(iot, ioh, EL_RBC, 0);
    404 
    405 		/* Transfer datagram to board. */
    406 		DPRINTF(("el: xfr pkt length=%d...\n", m0->m_pkthdr.len));
    407 		off = EL_BUFSIZ - max(m0->m_pkthdr.len, ETHER_MIN_LEN);
    408 #ifdef DIAGNOSTIC
    409 		if ((off & 0xffff) != off)
    410 			printf("%s: bogus off 0x%x\n",
    411 			    sc->sc_dev.dv_xname, off);
    412 #endif
    413 		bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
    414 		bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
    415 
    416 		/* Copy the datagram to the buffer. */
    417 		for (m = m0; m != 0; m = m->m_next)
    418 			bus_space_write_multi_1(iot, ioh, EL_BUF,
    419 			    mtod(m, u_int8_t *), m->m_len);
    420 
    421 		m_freem(m0);
    422 
    423 		/* Now transmit the datagram. */
    424 		retries = 0;
    425 		for (;;) {
    426 			bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
    427 			bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
    428 			if (el_xmit(sc)) {
    429 				ifp->if_oerrors++;
    430 				break;
    431 			}
    432 			/* Check out status. */
    433 			i = bus_space_read_1(iot, ioh, EL_TXS);
    434 			DPRINTF(("tx status=0x%x\n", i));
    435 			if ((i & EL_TXS_READY) == 0) {
    436 				DPRINTF(("el: err txs=%x\n", i));
    437 				if (i & (EL_TXS_COLL | EL_TXS_COLL16)) {
    438 					ifp->if_collisions++;
    439 					if ((i & EL_TXC_DCOLL16) == 0 &&
    440 					    retries < 15) {
    441 						retries++;
    442 						bus_space_write_1(iot, ioh,
    443 						    EL_AC, EL_AC_HOST);
    444 					}
    445 				} else {
    446 					ifp->if_oerrors++;
    447 					break;
    448 				}
    449 			} else {
    450 				ifp->if_opackets++;
    451 				break;
    452 			}
    453 		}
    454 
    455 		/*
    456 		 * Now give the card a chance to receive.
    457 		 * Gotta love 3c501s...
    458 		 */
    459 		(void)bus_space_read_1(iot, ioh, EL_AS);
    460 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    461 		splx(s);
    462 		/* Interrupt here. */
    463 		s = splnet();
    464 	}
    465 
    466 	(void)bus_space_read_1(iot, ioh, EL_AS);
    467 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    468 	ifp->if_flags &= ~IFF_OACTIVE;
    469 	splx(s);
    470 }
    471 
    472 /*
    473  * This function actually attempts to transmit a datagram downloaded to the
    474  * board.  Call at splnet or interrupt, after downloading data!  Returns 0 on
    475  * success, non-0 on failure.
    476  */
    477 static int
    478 el_xmit(sc)
    479 	struct el_softc *sc;
    480 {
    481 	bus_space_tag_t iot = sc->sc_iot;
    482 	bus_space_handle_t ioh = sc->sc_ioh;
    483 	int i;
    484 
    485 	/*
    486 	 * XXX
    487 	 * This busy-waits for the tx completion.  Can we get an interrupt
    488 	 * instead?
    489 	 */
    490 
    491 	DPRINTF(("el: xmit..."));
    492 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_TXFRX);
    493 	i = 20000;
    494 	while ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_TXBUSY) && (i > 0))
    495 		i--;
    496 	if (i == 0) {
    497 		DPRINTF(("tx not ready\n"));
    498 		return -1;
    499 	}
    500 	DPRINTF(("%d cycles.\n", 20000 - i));
    501 	return 0;
    502 }
    503 
    504 /*
    505  * Controller interrupt.
    506  */
    507 int
    508 elintr(arg)
    509 	void *arg;
    510 {
    511 	register struct el_softc *sc = arg;
    512 	bus_space_tag_t iot = sc->sc_iot;
    513 	bus_space_handle_t ioh = sc->sc_ioh;
    514 	u_int8_t rxstat;
    515 	int len;
    516 
    517 	DPRINTF(("elintr: "));
    518 
    519 	/* Check board status. */
    520 	if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0) {
    521 		(void)bus_space_read_1(iot, ioh, EL_RXC);
    522 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    523 		return 0;
    524 	}
    525 
    526 	for (;;) {
    527 		rxstat = bus_space_read_1(iot, ioh, EL_RXS);
    528 		if (rxstat & EL_RXS_STALE)
    529 			break;
    530 
    531 		/* If there's an overflow, reinit the board. */
    532 		if ((rxstat & EL_RXS_NOFLOW) == 0) {
    533 			DPRINTF(("overflow.\n"));
    534 			el_hardreset(sc);
    535 			/* Put board back into receive mode. */
    536 			if (sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC)
    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_PROMISC);
    540 			else
    541 				bus_space_write_1(iot, ioh, EL_RXC,
    542 				    EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
    543 				    EL_RXC_DOFLOW | EL_RXC_ABROAD);
    544 			(void)bus_space_read_1(iot, ioh, EL_AS);
    545 			bus_space_write_1(iot, ioh, EL_RBC, 0);
    546 			break;
    547 		}
    548 
    549 		/* Incoming packet. */
    550 		len = bus_space_read_1(iot, ioh, EL_RBL);
    551 		len |= bus_space_read_1(iot, ioh, EL_RBH) << 8;
    552 		DPRINTF(("receive len=%d rxstat=%x ", len, rxstat));
    553 		bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
    554 
    555 		/* Pass data up to upper levels. */
    556 		elread(sc, len);
    557 
    558 		/* Is there another packet? */
    559 		if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0)
    560 			break;
    561 
    562 #if NRND > 0
    563 		rnd_add_uint32(&sc->rnd_source, rxstat);
    564 #endif
    565 
    566 		DPRINTF(("<rescan> "));
    567 	}
    568 
    569 	(void)bus_space_read_1(iot, ioh, EL_RXC);
    570 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
    571 	return 1;
    572 }
    573 
    574 /*
    575  * Pass a packet to the higher levels.
    576  */
    577 void
    578 elread(sc, len)
    579 	register struct el_softc *sc;
    580 	int len;
    581 {
    582 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    583 	struct mbuf *m;
    584 	struct ether_header *eh;
    585 
    586 	if (len <= sizeof(struct ether_header) ||
    587 	    len > ETHER_MAX_LEN) {
    588 		printf("%s: invalid packet size %d; dropping\n",
    589 		    sc->sc_dev.dv_xname, len);
    590 		ifp->if_ierrors++;
    591 		return;
    592 	}
    593 
    594 	/* Pull packet off interface. */
    595 	m = elget(sc, len);
    596 	if (m == 0) {
    597 		ifp->if_ierrors++;
    598 		return;
    599 	}
    600 
    601 	ifp->if_ipackets++;
    602 
    603 	/* We assume that the header fit entirely in one mbuf. */
    604 	eh = mtod(m, struct ether_header *);
    605 
    606 #if NBPFILTER > 0
    607 	/*
    608 	 * Check if there's a BPF listener on this interface.
    609 	 * If so, hand off the raw packet to BPF.
    610 	 */
    611 	if (ifp->if_bpf) {
    612 		bpf_mtap(ifp->if_bpf, m);
    613 
    614 		/*
    615 		 * Note that the interface cannot be in promiscuous mode if
    616 		 * there are no BPF listeners.  And if we are in promiscuous
    617 		 * mode, we have to check if this packet is really ours.
    618 		 */
    619 		if ((ifp->if_flags & IFF_PROMISC) &&
    620 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    621 		    bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
    622 			    sizeof(eh->ether_dhost)) != 0) {
    623 			m_freem(m);
    624 			return;
    625 		}
    626 	}
    627 #endif
    628 
    629 	/* We assume that the header fit entirely in one mbuf. */
    630 	m_adj(m, sizeof(struct ether_header));
    631 	ether_input(ifp, eh, m);
    632 }
    633 
    634 /*
    635  * Pull read data off a interface.  Len is length of data, with local net
    636  * header stripped.  We copy the data into mbufs.  When full cluster sized
    637  * units are present we copy into clusters.
    638  */
    639 struct mbuf *
    640 elget(sc, totlen)
    641 	struct el_softc *sc;
    642 	int totlen;
    643 {
    644 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    645 	bus_space_tag_t iot = sc->sc_iot;
    646 	bus_space_handle_t ioh = sc->sc_ioh;
    647 	struct mbuf *m, *m0, *newm;
    648 	int len;
    649 
    650 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    651 	if (m0 == 0)
    652 		return (0);
    653 	m0->m_pkthdr.rcvif = ifp;
    654 	m0->m_pkthdr.len = totlen;
    655 	len = MHLEN;
    656 	m = m0;
    657 
    658 	bus_space_write_1(iot, ioh, EL_GPBL, 0);
    659 	bus_space_write_1(iot, ioh, EL_GPBH, 0);
    660 
    661 	while (totlen > 0) {
    662 		if (totlen >= MINCLSIZE) {
    663 			MCLGET(m, M_DONTWAIT);
    664 			if ((m->m_flags & M_EXT) == 0)
    665 				goto bad;
    666 			len = MCLBYTES;
    667 		}
    668 
    669 		m->m_len = len = min(totlen, len);
    670 		bus_space_read_multi_1(iot, ioh, EL_BUF, mtod(m, u_int8_t *), len);
    671 
    672 		totlen -= len;
    673 		if (totlen > 0) {
    674 			MGET(newm, M_DONTWAIT, MT_DATA);
    675 			if (newm == 0)
    676 				goto bad;
    677 			len = MLEN;
    678 			m = m->m_next = newm;
    679 		}
    680 	}
    681 
    682 	bus_space_write_1(iot, ioh, EL_RBC, 0);
    683 	bus_space_write_1(iot, ioh, EL_AC, EL_AC_RX);
    684 
    685 	return (m0);
    686 
    687 bad:
    688 	m_freem(m0);
    689 	return (0);
    690 }
    691 
    692 /*
    693  * Process an ioctl request. This code needs some work - it looks pretty ugly.
    694  */
    695 int
    696 elioctl(ifp, cmd, data)
    697 	register struct ifnet *ifp;
    698 	u_long cmd;
    699 	caddr_t data;
    700 {
    701 	struct el_softc *sc = ifp->if_softc;
    702 	struct ifaddr *ifa = (struct ifaddr *)data;
    703 	int s, error = 0;
    704 
    705 	s = splnet();
    706 
    707 	switch (cmd) {
    708 
    709 	case SIOCSIFADDR:
    710 		ifp->if_flags |= IFF_UP;
    711 
    712 		switch (ifa->ifa_addr->sa_family) {
    713 #ifdef INET
    714 		case AF_INET:
    715 			elinit(sc);
    716 			arp_ifinit(ifp, ifa);
    717 			break;
    718 #endif
    719 #ifdef NS
    720 		/* XXX - This code is probably wrong. */
    721 		case AF_NS:
    722 		    {
    723 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    724 
    725 			if (ns_nullhost(*ina))
    726 				ina->x_host =
    727 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    728 			else
    729 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    730 				    ETHER_ADDR_LEN);
    731 			/* Set new address. */
    732 			elinit(sc);
    733 			break;
    734 		    }
    735 #endif
    736 		default:
    737 			elinit(sc);
    738 			break;
    739 		}
    740 		break;
    741 
    742 	case SIOCSIFFLAGS:
    743 		if ((ifp->if_flags & IFF_UP) == 0 &&
    744 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    745 			/*
    746 			 * If interface is marked down and it is running, then
    747 			 * stop it.
    748 			 */
    749 			elstop(sc);
    750 			ifp->if_flags &= ~IFF_RUNNING;
    751 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    752 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    753 			/*
    754 			 * If interface is marked up and it is stopped, then
    755 			 * start it.
    756 			 */
    757 			elinit(sc);
    758 		} else {
    759 			/*
    760 			 * Some other important flag might have changed, so
    761 			 * reset.
    762 			 */
    763 			elreset(sc);
    764 		}
    765 		break;
    766 
    767 	default:
    768 		error = EINVAL;
    769 		break;
    770 	}
    771 
    772 	splx(s);
    773 	return error;
    774 }
    775 
    776 /*
    777  * Device timeout routine.
    778  */
    779 void
    780 elwatchdog(ifp)
    781 	struct ifnet *ifp;
    782 {
    783 	struct el_softc *sc = ifp->if_softc;
    784 
    785 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    786 	sc->sc_ethercom.ec_if.if_oerrors++;
    787 
    788 	elreset(sc);
    789 }
    790