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