Home | History | Annotate | Line # | Download | only in dev
if_ie.c revision 1.50.20.3
      1 /*	$NetBSD: if_ie.c,v 1.50.20.3 2010/08/11 22:52:50 yamt Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
      5  * Copyright (c) 1992, 1993, University of Vermont and State
      6  *  Agricultural College.
      7  * Copyright (c) 1992, 1993, Garrett A. Wollman.
      8  *
      9  * Portions:
     10  * Copyright (c) 1994, 1995, Rafal K. Boni
     11  * Copyright (c) 1990, 1991, William F. Jolitz
     12  * Copyright (c) 1990, The Regents of the University of California
     13  *
     14  * All rights reserved.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by Charles M. Hannum, by the
     27  *	University of Vermont and State Agricultural College and Garrett A.
     28  *	Wollman, by William F. Jolitz, and by the University of California,
     29  *	Berkeley, Lawrence Berkeley Laboratory, and its contributors.
     30  * 4. Neither the names of the Universities nor the names of the authors
     31  *    may be used to endorse or promote products derived from this software
     32  *    without specific prior written permission.
     33  *
     34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
     38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     44  * SUCH DAMAGE.
     45  */
     46 
     47 /*
     48  * Intel 82586 Ethernet chip
     49  * Register, bit, and structure definitions.
     50  *
     51  * Original StarLAN driver written by Garrett Wollman with reference to the
     52  * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
     53  *
     54  * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
     55  *
     56  * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
     57  *
     58  * Majorly cleaned up and 3C507 code merged by Charles Hannum.
     59  *
     60  * Converted to SUN ie driver by Charles D. Cranor,
     61  *		October 1994, January 1995.
     62  * This sun version based on i386 version 1.30.
     63  * [ see sys/dev/isa/if_ie.c ]
     64  */
     65 
     66 /*
     67  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
     68  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
     69  * SUN, making life difficult.  Programming this chip is mostly the same,
     70  * but certain details differ from system to system.  This driver is
     71  * written so that different "ie" interfaces can be controled by the same
     72  * driver.
     73  */
     74 
     75 /*
     76    Mode of operation:
     77 
     78    We run the 82586 in a standard Ethernet mode.  We keep NFRAMES
     79    received frame descriptors around for the receiver to use, and
     80    NRXBUF associated receive buffer descriptors, both in a circular
     81    list.  Whenever a frame is received, we rotate both lists as
     82    necessary.  (The 586 treats both lists as a simple queue.)  We also
     83    keep a transmit command around so that packets can be sent off
     84    quickly.
     85 
     86    We configure the adapter in AL-LOC = 1 mode, which means that the
     87    Ethernet/802.3 MAC header is placed at the beginning of the receive
     88    buffer rather than being split off into various fields in the RFD.
     89    This also means that we must include this header in the transmit
     90    buffer as well.
     91 
     92    By convention, all transmit commands, and only transmit commands,
     93    shall have the I (IE_CMD_INTR) bit set in the command.  This way,
     94    when an interrupt arrives at ieintr(), it is immediately possible
     95    to tell what precisely caused it.  ANY OTHER command-sending
     96    routines should run at splnet(), and should post an acknowledgement
     97    to every interrupt they generate.
     98 */
     99 
    100 #include <sys/cdefs.h>
    101 __KERNEL_RCSID(0, "$NetBSD: if_ie.c,v 1.50.20.3 2010/08/11 22:52:50 yamt Exp $");
    102 
    103 #include "opt_inet.h"
    104 #include "opt_ns.h"
    105 
    106 #include <sys/param.h>
    107 #include <sys/systm.h>
    108 #include <sys/mbuf.h>
    109 #include <sys/buf.h>
    110 #include <sys/protosw.h>
    111 #include <sys/socket.h>
    112 #include <sys/ioctl.h>
    113 #include <sys/errno.h>
    114 #include <sys/syslog.h>
    115 #include <sys/device.h>
    116 
    117 #include <net/if.h>
    118 #include <net/if_types.h>
    119 #include <net/if_dl.h>
    120 #include <net/if_ether.h>
    121 
    122 #include <net/bpf.h>
    123 #include <net/bpfdesc.h>
    124 
    125 #ifdef INET
    126 #include <netinet/in.h>
    127 #include <netinet/in_systm.h>
    128 #include <netinet/in_var.h>
    129 #include <netinet/ip.h>
    130 #include <netinet/if_inarp.h>
    131 #endif
    132 
    133 #ifdef NS
    134 #include <netns/ns.h>
    135 #include <netns/ns_if.h>
    136 #endif
    137 
    138 #include <uvm/uvm_extern.h>
    139 
    140 #include <machine/autoconf.h>
    141 #include <machine/cpu.h>
    142 #include <machine/pmap.h>
    143 
    144 /*
    145  * ugly byte-order hack for SUNs
    146  */
    147 
    148 #define XSWAP(y)	( (((y) & 0xff00) >> 8) | (((y) & 0xff) << 8) )
    149 #define SWAP(x)		((u_short)(XSWAP((u_short)(x))))
    150 
    151 #include "i82586.h"
    152 #include "if_iereg.h"
    153 #include "if_ievar.h"
    154 
    155 /* #define	IEDEBUG	XXX */
    156 
    157 /*
    158  * IED: ie debug flags
    159  */
    160 
    161 #define	IED_RINT	0x01
    162 #define	IED_TINT	0x02
    163 #define	IED_RNR		0x04
    164 #define	IED_CNA		0x08
    165 #define	IED_READFRAME	0x10
    166 #define	IED_ENQ		0x20
    167 #define	IED_XMIT	0x40
    168 #define	IED_ALL		0x7f
    169 
    170 #ifdef	IEDEBUG
    171 #define	inline	/* not */
    172 void print_rbd(volatile struct ie_recv_buf_desc *);
    173 int     in_ierint = 0;
    174 int     in_ietint = 0;
    175 int     ie_debug_flags = 0;
    176 #endif
    177 
    178 /* XXX - Skip TDR for now - it always complains... */
    179 int 	ie_run_tdr = 0;
    180 
    181 static void iewatchdog(struct ifnet *);
    182 static int ieinit(struct ie_softc *);
    183 static int ieioctl(struct ifnet *, u_long, void *);
    184 static void iestart(struct ifnet *);
    185 static void iereset(struct ie_softc *);
    186 static int ie_setupram(struct ie_softc *);
    187 
    188 static int cmd_and_wait(struct ie_softc *, int, void *, int);
    189 
    190 static void ie_drop_packet_buffer(struct ie_softc *);
    191 static void ie_readframe(struct ie_softc *, int);
    192 static inline void ie_setup_config(struct ie_config_cmd *, int, int);
    193 
    194 static void ierint(struct ie_softc *);
    195 static void iestop(struct ie_softc *);
    196 static void ietint(struct ie_softc *);
    197 static void iexmit(struct ie_softc *);
    198 
    199 static int mc_setup(struct ie_softc *, void *);
    200 static void mc_reset(struct ie_softc *);
    201 static void run_tdr(struct ie_softc *, struct ie_tdr_cmd *);
    202 static void iememinit(struct ie_softc *);
    203 
    204 static inline uint8_t *Align(char *);
    205 static inline u_int Swap32(u_int);
    206 static inline u_int vtop24(struct ie_softc *, void *);
    207 static inline uint16_t vtop16sw(struct ie_softc *, void *);
    208 
    209 static inline void ie_ack(struct ie_softc *, u_int);
    210 static inline u_short ether_cmp(u_char *, uint8_t *);
    211 static inline int check_eh(struct ie_softc *, struct ether_header *, int *);
    212 static inline int ie_buflen(struct ie_softc *, int);
    213 static inline int ie_packet_len(struct ie_softc *);
    214 static inline struct mbuf * ieget(struct ie_softc *, int *);
    215 
    216 
    217 /*
    218  * Here are a few useful functions.  We could have done these as macros,
    219  * but since we have the inline facility, it makes sense to use that
    220  * instead.
    221  */
    222 
    223 /* KVA to 24 bit device address */
    224 static inline u_int
    225 vtop24(struct ie_softc *sc, void *ptr)
    226 {
    227 	u_int pa;
    228 
    229 	pa = (vaddr_t)ptr - (vaddr_t)sc->sc_iobase;
    230 #ifdef	IEDEBUG
    231 	if (pa & ~0xffFFff)
    232 		panic("ie:vtop24");
    233 #endif
    234 	return pa;
    235 }
    236 
    237 /* KVA to 16 bit offset, swapped */
    238 static inline u_short
    239 vtop16sw(struct ie_softc *sc, void *ptr)
    240 {
    241 	u_int pa;
    242 
    243 	pa = (vaddr_t)ptr - (vaddr_t)sc->sc_maddr;
    244 #ifdef	IEDEBUG
    245 	if (pa & ~0xFFff)
    246 		panic("ie:vtop16");
    247 #endif
    248 
    249 	return SWAP(pa);
    250 }
    251 
    252 static inline u_int
    253 Swap32(u_int x)
    254 {
    255 	u_int y;
    256 
    257 	y = x & 0xFF;
    258 	y <<= 8; x >>= 8;
    259 	y |= x & 0xFF;
    260 	y <<= 8; x >>= 8;
    261 	y |= x & 0xFF;
    262 	y <<= 8; x >>= 8;
    263 	y |= x & 0xFF;
    264 
    265 	return y;
    266 }
    267 
    268 static inline uint8_t *
    269 Align(char *ptr)
    270 {
    271 	u_long  l = (u_long)ptr;
    272 
    273 	l = (l + 3) & ~3L;
    274 	return (uint8_t *)l;
    275 }
    276 
    277 
    278 static inline void
    279 ie_ack(struct ie_softc *sc, u_int mask)
    280 {
    281 	volatile struct ie_sys_ctl_block *scb = sc->scb;
    282 
    283 	cmd_and_wait(sc, scb->ie_status & mask, 0, 0);
    284 }
    285 
    286 
    287 /*
    288  * Taken almost exactly from Bill's if_is.c,
    289  * then modified beyond recognition...
    290  */
    291 void
    292 ie_attach(struct ie_softc *sc)
    293 {
    294 	struct ifnet *ifp = &sc->sc_if;
    295 
    296 	/* MD code has done its part before calling this. */
    297 	printf(": macaddr %s\n", ether_sprintf(sc->sc_addr));
    298 
    299 	/*
    300 	 * Compute number of transmit and receive buffers.
    301 	 * Tx buffers take 1536 bytes, and fixed in number.
    302 	 * Rx buffers are 512 bytes each, variable number.
    303 	 * Need at least 1 frame for each 3 rx buffers.
    304 	 * The ratio 3bufs:2frames is a compromise.
    305 	 */
    306 	sc->ntxbuf = NTXBUF;	/* XXX - Fix me... */
    307 	switch (sc->sc_msize) {
    308 	case 16384:
    309 		sc->nframes = 8 * 4;
    310 		sc->nrxbuf  = 8 * 6;
    311 		break;
    312 	case 32768:
    313 		sc->nframes = 16 * 4;
    314 		sc->nrxbuf  = 16 * 6;
    315 		break;
    316 	case 65536:
    317 		sc->nframes = 32 * 4;
    318 		sc->nrxbuf  = 32 * 6;
    319 		break;
    320 	default:
    321 		sc->nframes = 0;
    322 	}
    323 	if (sc->nframes > MXFRAMES)
    324 		sc->nframes = MXFRAMES;
    325 	if (sc->nrxbuf > MXRXBUF)
    326 		sc->nrxbuf = MXRXBUF;
    327 
    328 #ifdef	IEDEBUG
    329 	aprint_debug_dev(sc->sc_dev,
    330 	    "%dK memory, %d tx frames, %d rx frames, %d rx bufs\n",
    331 	    (sc->sc_msize >> 10), sc->ntxbuf, sc->nframes, sc->nrxbuf);
    332 #endif
    333 
    334 	if ((sc->nframes <= 0) || (sc->nrxbuf <= 0))
    335 		panic("%s: weird memory size", __func__);
    336 
    337 	/*
    338 	 * Setup RAM for transmit/receive
    339 	 */
    340 	if (ie_setupram(sc) == 0) {
    341 		aprint_error(": RAM CONFIG FAILED!\n");
    342 		/* XXX should reclaim resources? */
    343 		return;
    344 	}
    345 
    346 	/*
    347 	 * Initialize and attach S/W interface
    348 	 */
    349 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    350 	ifp->if_softc = sc;
    351 	ifp->if_start = iestart;
    352 	ifp->if_ioctl = ieioctl;
    353 	ifp->if_watchdog = iewatchdog;
    354 	ifp->if_flags =
    355 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    356 
    357 	/* Attach the interface. */
    358 	if_attach(ifp);
    359 	ether_ifattach(ifp, sc->sc_addr);
    360 }
    361 
    362 /*
    363  * Setup IE's ram space.
    364  */
    365 static int
    366 ie_setupram(struct ie_softc *sc)
    367 {
    368 	volatile struct ie_sys_conf_ptr *scp;
    369 	volatile struct ie_int_sys_conf_ptr *iscp;
    370 	volatile struct ie_sys_ctl_block *scb;
    371 	int off;
    372 
    373 	/*
    374 	 * Allocate from end of buffer space for
    375 	 * ISCP, SCB, and other small stuff.
    376 	 */
    377 	off = sc->buf_area_sz;
    378 	off &= ~3;
    379 
    380 	/* SCP (address already chosen). */
    381 	scp = sc->scp;
    382 	(sc->sc_memset)(__UNVOLATILE(scp), 0, sizeof(*scp));
    383 
    384 	/* ISCP */
    385 	off -= sizeof(*iscp);
    386 	iscp = (volatile void *)(sc->buf_area + off);
    387 	(sc->sc_memset)(__UNVOLATILE(iscp), 0, sizeof(*iscp));
    388 	sc->iscp = iscp;
    389 
    390 	/* SCB */
    391 	off -= sizeof(*scb);
    392 	scb  = (volatile void *)(sc->buf_area + off);
    393 	(sc->sc_memset)(__UNVOLATILE(scb), 0, sizeof(*scb));
    394 	sc->scb = scb;
    395 
    396 	/* Remainder is for buffers, etc. */
    397 	sc->buf_area_sz = off;
    398 
    399 	/*
    400 	 * Now fill in the structures we just allocated.
    401 	 */
    402 
    403 	/* SCP: main thing is 24-bit ptr to ISCP */
    404 	scp->ie_bus_use = 0;	/* 16-bit */
    405 	scp->ie_iscp_ptr = Swap32(vtop24(sc, __UNVOLATILE(iscp)));
    406 
    407 	/* ISCP */
    408 	iscp->ie_busy = 1;	/* ie_busy == char */
    409 	iscp->ie_scb_offset = vtop16sw(sc, __UNVOLATILE(scb));
    410 	iscp->ie_base = Swap32(vtop24(sc, sc->sc_maddr));
    411 
    412 	/* SCB */
    413 	scb->ie_command_list = SWAP(0xffff);
    414 	scb->ie_recv_list    = SWAP(0xffff);
    415 
    416 	/* Other stuff is done in ieinit() */
    417 	(sc->reset_586)(sc);
    418 	(sc->chan_attn)(sc);
    419 
    420 	delay(100);		/* wait a while... */
    421 
    422 	if (iscp->ie_busy) {
    423 		return 0;
    424 	}
    425 	/*
    426 	 * Acknowledge any interrupts we may have caused...
    427 	 */
    428 	ie_ack(sc, IE_ST_WHENCE);
    429 
    430 	return 1;
    431 }
    432 
    433 /*
    434  * Device timeout/watchdog routine.  Entered if the device neglects to
    435  * generate an interrupt after a transmit has been started on it.
    436  */
    437 static void
    438 iewatchdog(struct ifnet *ifp)
    439 {
    440 	struct ie_softc *sc = ifp->if_softc;
    441 
    442 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    443 	++ifp->if_oerrors;
    444 	iereset(sc);
    445 }
    446 
    447 /*
    448  * What to do upon receipt of an interrupt.
    449  */
    450 int
    451 ie_intr(void *arg)
    452 {
    453 	struct ie_softc *sc = arg;
    454 	uint16_t status;
    455 	int loopcnt;
    456 
    457 	/*
    458 	 * check for parity error
    459 	 */
    460 	if (sc->hard_type == IE_VME) {
    461 		volatile struct ievme *iev =
    462 		    (volatile struct ievme *)sc->sc_reg;
    463 
    464 		if (iev->status & IEVME_PERR) {
    465 			printf("%s: parity error (ctrl 0x%x @ 0x%02x%04x)\n",
    466 			    device_xname(sc->sc_dev), iev->pectrl,
    467 			    iev->pectrl & IEVME_HADDR, iev->peaddr);
    468 			iev->pectrl = iev->pectrl | IEVME_PARACK;
    469 		}
    470 	}
    471 
    472 	status = sc->scb->ie_status;
    473 	if ((status & IE_ST_WHENCE) == 0)
    474 		return 0;
    475 
    476 	loopcnt = sc->nframes;
    477  loop:
    478 	/* Ack interrupts FIRST in case we receive more during the ISR. */
    479 	ie_ack(sc, IE_ST_WHENCE & status);
    480 
    481 	if (status & (IE_ST_RECV | IE_ST_RNR)) {
    482 #ifdef IEDEBUG
    483 		in_ierint++;
    484 		if (sc->sc_debug & IED_RINT)
    485 			printf("%s: rint\n", device_xname(sc->sc_dev));
    486 #endif
    487 		ierint(sc);
    488 #ifdef IEDEBUG
    489 		in_ierint--;
    490 #endif
    491 	}
    492 
    493 	if (status & IE_ST_DONE) {
    494 #ifdef IEDEBUG
    495 		in_ietint++;
    496 		if (sc->sc_debug & IED_TINT)
    497 			printf("%s: tint\n", device_xname(sc->sc_dev));
    498 #endif
    499 		ietint(sc);
    500 #ifdef IEDEBUG
    501 		in_ietint--;
    502 #endif
    503 	}
    504 
    505 	/*
    506 	 * Receiver not ready (RNR) just means it has
    507 	 * run out of resources (buffers or frames).
    508 	 * One can easily cause this with (i.e.) spray.
    509 	 * This is not a serious error, so be silent.
    510 	 */
    511 	if (status & IE_ST_RNR) {
    512 #ifdef IEDEBUG
    513 		printf("%s: receiver not ready\n", device_xname(sc->sc_dev));
    514 #endif
    515 		sc->sc_if.if_ierrors++;
    516 		iereset(sc);
    517 	}
    518 
    519 #ifdef IEDEBUG
    520 	if ((status & IE_ST_ALLDONE) && (sc->sc_debug & IED_CNA))
    521 		printf("%s: cna\n", device_xname(sc->sc_dev));
    522 #endif
    523 
    524 	status = sc->scb->ie_status;
    525 	if (status & IE_ST_WHENCE) {
    526 		/* It still wants service... */
    527 		if (--loopcnt > 0)
    528 			goto loop;
    529 		/* ... but we've been here long enough. */
    530 		log(LOG_ERR, "%s: interrupt stuck?\n",
    531 		    device_xname(sc->sc_dev));
    532 		iereset(sc);
    533 	}
    534 	return 1;
    535 }
    536 
    537 /*
    538  * Process a received-frame interrupt.
    539  */
    540 void
    541 ierint(struct ie_softc *sc)
    542 {
    543 	volatile struct ie_sys_ctl_block *scb = sc->scb;
    544 	int i, status;
    545 	static int timesthru = 1024;
    546 
    547 	i = sc->rfhead;
    548 	for (;;) {
    549 		status = sc->rframes[i]->ie_fd_status;
    550 
    551 		if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
    552 			if (!--timesthru) {
    553 				sc->sc_if.if_ierrors +=
    554 				    SWAP(scb->ie_err_crc) +
    555 				    SWAP(scb->ie_err_align) +
    556 				    SWAP(scb->ie_err_resource) +
    557 				    SWAP(scb->ie_err_overrun);
    558 				scb->ie_err_crc = 0;
    559 				scb->ie_err_align = 0;
    560 				scb->ie_err_resource = 0;
    561 				scb->ie_err_overrun = 0;
    562 				timesthru = 1024;
    563 			}
    564 			ie_readframe(sc, i);
    565 		} else {
    566 			if ((status & IE_FD_RNR) != 0 &&
    567 			    (scb->ie_status & IE_RU_READY) == 0) {
    568 				sc->rframes[0]->ie_fd_buf_desc = vtop16sw(sc,
    569 				    __UNVOLATILE(sc->rbuffs[0]));
    570 				scb->ie_recv_list = vtop16sw(sc,
    571 				    __UNVOLATILE(sc->rframes[0]));
    572 				cmd_and_wait(sc, IE_RU_START, 0, 0);
    573 			}
    574 			break;
    575 		}
    576 		i = (i + 1) % sc->nframes;
    577 	}
    578 }
    579 
    580 /*
    581  * Process a command-complete interrupt.  These are only generated by the
    582  * transmission of frames.  This routine is deceptively simple, since most
    583  * of the real work is done by iestart().
    584  */
    585 void
    586 ietint(struct ie_softc *sc)
    587 {
    588 	struct ifnet *ifp;
    589 	int status;
    590 
    591 	ifp = &sc->sc_if;
    592 
    593 	ifp->if_timer = 0;
    594 	ifp->if_flags &= ~IFF_OACTIVE;
    595 
    596 	status = sc->xmit_cmds[sc->xctail]->ie_xmit_status;
    597 
    598 	if (!(status & IE_STAT_COMPL) || (status & IE_STAT_BUSY))
    599 		printf("%s: command still busy!\n", __func__);
    600 
    601 	if (status & IE_STAT_OK) {
    602 		ifp->if_opackets++;
    603 		ifp->if_collisions +=
    604 		  SWAP(status & IE_XS_MAXCOLL);
    605 	} else {
    606 		ifp->if_oerrors++;
    607 		/*
    608 		 * XXX
    609 		 * Check SQE and DEFERRED?
    610 		 * What if more than one bit is set?
    611 		 */
    612 		if (status & IE_STAT_ABORT)
    613 			printf("%s: send aborted\n", device_xname(sc->sc_dev));
    614 		if (status & IE_XS_LATECOLL)
    615 			printf("%s: late collision\n",
    616 			    device_xname(sc->sc_dev));
    617 		if (status & IE_XS_NOCARRIER)
    618 			printf("%s: no carrier\n", device_xname(sc->sc_dev));
    619 		if (status & IE_XS_LOSTCTS)
    620 			printf("%s: lost CTS\n", device_xname(sc->sc_dev));
    621 		if (status & IE_XS_UNDERRUN)
    622 			printf("%s: DMA underrun\n", device_xname(sc->sc_dev));
    623 		if (status & IE_XS_EXCMAX) {
    624 			/* Do not print this one (too noisy). */
    625 			ifp->if_collisions += 16;
    626 		}
    627 	}
    628 
    629 	/*
    630 	 * If multicast addresses were added or deleted while we
    631 	 * were transmitting, mc_reset() set the want_mcsetup flag
    632 	 * indicating that we should do it.
    633 	 */
    634 	if (sc->want_mcsetup) {
    635 		mc_setup(sc, (void *)sc->xmit_cbuffs[sc->xctail]);
    636 		sc->want_mcsetup = 0;
    637 	}
    638 
    639 	/* Done with the buffer. */
    640 	sc->xmit_busy--;
    641 	sc->xctail = (sc->xctail + 1) % NTXBUF;
    642 
    643 	/* Start the next packet, if any, transmitting. */
    644 	if (sc->xmit_busy > 0)
    645 		iexmit(sc);
    646 
    647 	iestart(ifp);
    648 }
    649 
    650 /*
    651  * Compare two Ether/802 addresses for equality, inlined and
    652  * unrolled for speed.  I'd love to have an inline assembler
    653  * version of this...   XXX: Who wanted that? mycroft?
    654  * I wrote one, but the following is just as efficient.
    655  * This expands to 10 short m68k instructions! -gwr
    656  * Note: use this like memcmp()
    657  */
    658 static inline uint16_t
    659 ether_cmp(uint8_t *one, uint8_t *two)
    660 {
    661 	uint16_t *a = (uint16_t *)one;
    662 	uint16_t *b = (uint16_t *)two;
    663 	uint16_t diff;
    664 
    665 	diff  = *a++ - *b++;
    666 	diff |= *a++ - *b++;
    667 	diff |= *a++ - *b++;
    668 
    669 	return diff;
    670 }
    671 #define	ether_equal !ether_cmp
    672 
    673 /*
    674  * Check for a valid address.  to_bpf is filled in with one of the following:
    675  *   0 -> BPF doesn't get this packet
    676  *   1 -> BPF does get this packet
    677  *   2 -> BPF does get this packet, but we don't
    678  * Return value is true if the packet is for us, and false otherwise.
    679  *
    680  * This routine is a mess, but it's also critical that it be as fast
    681  * as possible.  It could be made cleaner if we can assume that the
    682  * only client which will fiddle with IFF_PROMISC is BPF.  This is
    683  * probably a good assumption, but we do not make it here.  (Yet.)
    684  */
    685 static inline int
    686 check_eh(struct ie_softc *sc, struct ether_header *eh, int *to_bpf)
    687 {
    688 	struct ifnet *ifp;
    689 
    690 	ifp = &sc->sc_if;
    691 	*to_bpf = (ifp->if_bpf != 0);
    692 
    693 	/*
    694 	 * This is all handled at a higher level now.
    695 	 */
    696 	return 1;
    697 }
    698 
    699 /*
    700  * We want to isolate the bits that have meaning...  This assumes that
    701  * IE_RBUF_SIZE is an even power of two.  If somehow the act_len exceeds
    702  * the size of the buffer, then we are screwed anyway.
    703  */
    704 static inline int
    705 ie_buflen(struct ie_softc *sc, int head)
    706 {
    707 	int len;
    708 
    709 	len = SWAP(sc->rbuffs[head]->ie_rbd_actual);
    710 	len &= (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1));
    711 	return len;
    712 }
    713 
    714 static inline int
    715 ie_packet_len(struct ie_softc *sc)
    716 {
    717 	int i;
    718 	int head = sc->rbhead;
    719 	int acc = 0;
    720 
    721 	do {
    722 		if ((sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)
    723 		    == 0) {
    724 #ifdef IEDEBUG
    725 			print_rbd(sc->rbuffs[sc->rbhead]);
    726 #endif
    727 			log(LOG_ERR,
    728 			    "%s: receive descriptors out of sync at %d\n",
    729 			    device_xname(sc->sc_dev), sc->rbhead);
    730 			iereset(sc);
    731 			return -1;
    732 		}
    733 
    734 		i = sc->rbuffs[head]->ie_rbd_actual & IE_RBD_LAST;
    735 
    736 		acc += ie_buflen(sc, head);
    737 		head = (head + 1) % sc->nrxbuf;
    738 	} while (i == 0);
    739 
    740 	return acc;
    741 }
    742 
    743 /*
    744  * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
    745  * command to the chip to be executed.  On the way, if we have a BPF listener
    746  * also give him a copy.
    747  */
    748 static void
    749 iexmit(struct ie_softc *sc)
    750 {
    751 	struct ifnet *ifp;
    752 
    753 	ifp = &sc->sc_if;
    754 
    755 #ifdef IEDEBUG
    756 	if (sc->sc_debug & IED_XMIT)
    757 		printf("%s: xmit buffer %d\n", device_xname(sc->sc_dev),
    758 		    sc->xctail);
    759 #endif
    760 
    761 	/*
    762 	 * If BPF is listening on this interface, let it see the packet before
    763 	 * we push it on the wire.
    764 	 */
    765 	bpf_tap(ifp, sc->xmit_cbuffs[sc->xctail],
    766 	    SWAP(sc->xmit_buffs[sc->xctail]->ie_xmit_flags));
    767 
    768 	sc->xmit_buffs[sc->xctail]->ie_xmit_flags |= IE_XMIT_LAST;
    769 	sc->xmit_buffs[sc->xctail]->ie_xmit_next = SWAP(0xffff);
    770 	sc->xmit_buffs[sc->xctail]->ie_xmit_buf =
    771 	    Swap32(vtop24(sc, sc->xmit_cbuffs[sc->xctail]));
    772 
    773 	sc->xmit_cmds[sc->xctail]->com.ie_cmd_link = SWAP(0xffff);
    774 	sc->xmit_cmds[sc->xctail]->com.ie_cmd_cmd =
    775 	    IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST;
    776 
    777 	sc->xmit_cmds[sc->xctail]->ie_xmit_status = SWAP(0);
    778 	sc->xmit_cmds[sc->xctail]->ie_xmit_desc =
    779 	    vtop16sw(sc, __UNVOLATILE(sc->xmit_buffs[sc->xctail]));
    780 
    781 	sc->scb->ie_command_list =
    782 	    vtop16sw(sc, __UNVOLATILE(sc->xmit_cmds[sc->xctail]));
    783 	cmd_and_wait(sc, IE_CU_START, 0, 0);
    784 
    785 	ifp->if_timer = 5;
    786 }
    787 
    788 /*
    789  * Read data off the interface, and turn it into an mbuf chain.
    790  *
    791  * This code is DRAMATICALLY different from the previous version; this
    792  * version tries to allocate the entire mbuf chain up front, given the
    793  * length of the data available.  This enables us to allocate mbuf
    794  * clusters in many situations where before we would have had a long
    795  * chain of partially-full mbufs.  This should help to speed up the
    796  * operation considerably.  (Provided that it works, of course.)
    797  */
    798 static inline struct mbuf *
    799 ieget(struct ie_softc *sc, int *to_bpf)
    800 {
    801 	struct mbuf *top, **mp, *m;
    802 	int len, totlen, resid;
    803 	int thisrboff, thismboff;
    804 	int head;
    805 	struct ether_header eh;
    806 
    807 	totlen = ie_packet_len(sc);
    808 	if (totlen <= 0)
    809 		return 0;
    810 
    811 	head = sc->rbhead;
    812 
    813 	/*
    814 	 * Snarf the Ethernet header.
    815 	 */
    816 	(sc->sc_memcpy)((void *)&eh, (void *)sc->cbuffs[head],
    817 	    sizeof(struct ether_header));
    818 
    819 	/*
    820 	 * As quickly as possible, check if this packet is for us.
    821 	 * If not, don't waste a single cycle copying the rest of the
    822 	 * packet in.
    823 	 * This is only a consideration when FILTER is defined; i.e., when
    824 	 * we are either running BPF or doing multicasting.
    825 	 */
    826 	if (check_eh(sc, &eh, to_bpf) == 0) {
    827 		/* just this case, it's not an error */
    828 		sc->sc_if.if_ierrors--;
    829 		return 0;
    830 	}
    831 
    832 	resid = totlen;
    833 
    834 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    835 	if (m == 0)
    836 		return 0;
    837 
    838 	m->m_pkthdr.rcvif = &sc->sc_if;
    839 	m->m_pkthdr.len = totlen;
    840 	len = MHLEN;
    841 	top = 0;
    842 	mp = &top;
    843 
    844 	/*
    845 	 * This loop goes through and allocates mbufs for all the data we will
    846 	 * be copying in.  It does not actually do the copying yet.
    847 	 */
    848 	while (totlen > 0) {
    849 		if (top) {
    850 			MGET(m, M_DONTWAIT, MT_DATA);
    851 			if (m == 0) {
    852 				m_freem(top);
    853 				return 0;
    854 			}
    855 			len = MLEN;
    856 		}
    857 		if (totlen >= MINCLSIZE) {
    858 			MCLGET(m, M_DONTWAIT);
    859 			if (m->m_flags & M_EXT)
    860 				len = MCLBYTES;
    861 		}
    862 
    863 		if (mp == &top) {
    864 			char *newdata = (char *)
    865 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    866 			    sizeof(struct ether_header);
    867 			len -= newdata - m->m_data;
    868 			m->m_data = newdata;
    869 		}
    870 
    871 		m->m_len = len = min(totlen, len);
    872 
    873 		totlen -= len;
    874 		*mp = m;
    875 		mp = &m->m_next;
    876 	}
    877 
    878 	m = top;
    879 	thismboff = 0;
    880 
    881 	/*
    882 	 * Copy the Ethernet header into the mbuf chain.
    883 	 */
    884 	memcpy(mtod(m, void *), &eh, sizeof(struct ether_header));
    885 	thismboff = sizeof(struct ether_header);
    886 	thisrboff = sizeof(struct ether_header);
    887 	resid -= sizeof(struct ether_header);
    888 
    889 	/*
    890 	 * Now we take the mbuf chain (hopefully only one mbuf most of the
    891 	 * time) and stuff the data into it.  There are no possible failures
    892 	 * at or after this point.
    893 	 */
    894 	while (resid > 0) {
    895 		int thisrblen = ie_buflen(sc, head) - thisrboff;
    896 		int thismblen = m->m_len - thismboff;
    897 
    898 		len = min(thisrblen, thismblen);
    899 		(sc->sc_memcpy)(mtod(m, char *) + thismboff,
    900 		    (void *)(sc->cbuffs[head] + thisrboff),
    901 		    (u_int)len);
    902 		resid -= len;
    903 
    904 		if (len == thismblen) {
    905 			m = m->m_next;
    906 			thismboff = 0;
    907 		} else
    908 			thismboff += len;
    909 
    910 		if (len == thisrblen) {
    911 			head = (head + 1) % sc->nrxbuf;
    912 			thisrboff = 0;
    913 		} else
    914 			thisrboff += len;
    915 	}
    916 
    917 	/*
    918 	 * Unless something changed strangely while we were doing the copy,
    919 	 * we have now copied everything in from the shared memory.
    920 	 * This means that we are done.
    921 	 */
    922 	return top;
    923 }
    924 
    925 /*
    926  * Read frame NUM from unit UNIT (pre-cached as IE).
    927  *
    928  * This routine reads the RFD at NUM, and copies in the buffers from
    929  * the list of RBD, then rotates the RBD and RFD lists so that the receiver
    930  * doesn't start complaining.  Trailers are DROPPED---there's no point
    931  * in wasting time on confusing code to deal with them.  Hopefully,
    932  * this machine will never ARP for trailers anyway.
    933  */
    934 static void
    935 ie_readframe(struct ie_softc *sc, int num)
    936 {
    937 	int status;
    938 	struct mbuf *m = 0;
    939 	int bpf_gets_it = 0;
    940 
    941 	status = sc->rframes[num]->ie_fd_status;
    942 
    943 	/* Advance the RFD list, since we're done with this descriptor. */
    944 	sc->rframes[num]->ie_fd_status = SWAP(0);
    945 	sc->rframes[num]->ie_fd_last |= IE_FD_LAST;
    946 	sc->rframes[sc->rftail]->ie_fd_last &= ~IE_FD_LAST;
    947 	sc->rftail = (sc->rftail + 1) % sc->nframes;
    948 	sc->rfhead = (sc->rfhead + 1) % sc->nframes;
    949 
    950 	if (status & IE_FD_OK) {
    951 		m = ieget(sc, &bpf_gets_it);
    952 		ie_drop_packet_buffer(sc);
    953 	}
    954 	if (m == 0) {
    955 		sc->sc_if.if_ierrors++;
    956 		return;
    957 	}
    958 
    959 #ifdef IEDEBUG
    960 	if (sc->sc_debug & IED_READFRAME) {
    961 		struct ether_header *eh = mtod(m, struct ether_header *);
    962 
    963 		printf("%s: frame from ether %s type 0x%x\n",
    964 		    device_xname(sc->sc_dev),
    965 		    ether_sprintf(eh->ether_shost), (u_int)eh->ether_type);
    966 	}
    967 #endif
    968 
    969 	/*
    970 	 * Check for a BPF filter; if so, hand it up.
    971 	 * Note that we have to stick an extra mbuf up front, because
    972 	 * bpf_mtap expects to have the ether header at the front.
    973 	 * It doesn't matter that this results in an ill-formatted mbuf chain,
    974 	 * since BPF just looks at the data.  (It doesn't try to free the mbuf,
    975 	 * tho' it will make a copy for tcpdump.)
    976 	 */
    977 	if (bpf_gets_it) {
    978 		/* Pass it up. */
    979 		bpf_mtap(&sc->sc_if, m);
    980 
    981 		/*
    982 		 * A signal passed up from the filtering code indicating that
    983 		 * the packet is intended for BPF but not for the protocol
    984 		 * machinery.  We can save a few cycles by not handing it off
    985 		 * to them.
    986 		 */
    987 		if (bpf_gets_it == 2) {
    988 			m_freem(m);
    989 			return;
    990 		}
    991 	}
    992 
    993 	/*
    994 	 * In here there used to be code to check destination addresses upon
    995 	 * receipt of a packet.  We have deleted that code, and replaced it
    996 	 * with code to check the address much earlier in the cycle, before
    997 	 * copying the data in; this saves us valuable cycles when operating
    998 	 * as a multicast router or when using BPF.
    999 	 */
   1000 
   1001 	/*
   1002 	 * Finally pass this packet up to higher layers.
   1003 	 */
   1004 	(*sc->sc_if.if_input)(&sc->sc_if, m);
   1005 	sc->sc_if.if_ipackets++;
   1006 }
   1007 
   1008 static void
   1009 ie_drop_packet_buffer(struct ie_softc *sc)
   1010 {
   1011 	int i;
   1012 
   1013 	do {
   1014 		/*
   1015 		 * This means we are somehow out of sync.  So, we reset the
   1016 		 * adapter.
   1017 		 */
   1018 		if ((sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)
   1019 		    == 0) {
   1020 #ifdef IEDEBUG
   1021 			print_rbd(sc->rbuffs[sc->rbhead]);
   1022 #endif
   1023 			log(LOG_ERR,
   1024 			    "%s: receive descriptors out of sync at %d\n",
   1025 			    device_xname(sc->sc_dev), sc->rbhead);
   1026 			iereset(sc);
   1027 			return;
   1028 		}
   1029 
   1030 		i = sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_LAST;
   1031 
   1032 		sc->rbuffs[sc->rbhead]->ie_rbd_length |= IE_RBD_LAST;
   1033 		sc->rbuffs[sc->rbhead]->ie_rbd_actual = SWAP(0);
   1034 		sc->rbhead = (sc->rbhead + 1) % sc->nrxbuf;
   1035 		sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
   1036 		sc->rbtail = (sc->rbtail + 1) % sc->nrxbuf;
   1037 	} while (i == 0);
   1038 }
   1039 
   1040 /*
   1041  * Start transmission on an interface.
   1042  */
   1043 static void
   1044 iestart(struct ifnet *ifp)
   1045 {
   1046 	struct ie_softc *sc = ifp->if_softc;
   1047 	struct mbuf *m0, *m;
   1048 	uint8_t *buffer;
   1049 	uint16_t len;
   1050 
   1051 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1052 		return;
   1053 
   1054 	for (;;) {
   1055 		if (sc->xmit_busy == sc->ntxbuf) {
   1056 			ifp->if_flags |= IFF_OACTIVE;
   1057 			break;
   1058 		}
   1059 
   1060 		IF_DEQUEUE(&ifp->if_snd, m0);
   1061 		if (m0 == 0)
   1062 			break;
   1063 
   1064 		/* We need to use m->m_pkthdr.len, so require the header */
   1065 		if ((m0->m_flags & M_PKTHDR) == 0)
   1066 			panic("%s: no header mbuf", __func__);
   1067 
   1068 		/* Tap off here if there is a BPF listener. */
   1069 		bpf_mtap(ifp, m0);
   1070 
   1071 #ifdef IEDEBUG
   1072 		if (sc->sc_debug & IED_ENQ)
   1073 			printf("%s: fill buffer %d\n", device_xname(sc->sc_dev),
   1074 			    sc->xchead);
   1075 #endif
   1076 
   1077 		buffer = sc->xmit_cbuffs[sc->xchead];
   1078 		for (m = m0; m != 0; m = m->m_next) {
   1079 			(sc->sc_memcpy)(buffer, mtod(m, void *), m->m_len);
   1080 			buffer += m->m_len;
   1081 		}
   1082 		if (m0->m_pkthdr.len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
   1083 			sc->sc_memset(buffer, 0,
   1084 			    ETHER_MIN_LEN - ETHER_CRC_LEN - m0->m_pkthdr.len);
   1085 			len = ETHER_MIN_LEN - ETHER_CRC_LEN;
   1086 		} else
   1087 			len = m0->m_pkthdr.len;
   1088 
   1089 		m_freem(m0);
   1090 		sc->xmit_buffs[sc->xchead]->ie_xmit_flags = SWAP(len);
   1091 
   1092 		/* Start the first packet transmitting. */
   1093 		if (sc->xmit_busy == 0)
   1094 			iexmit(sc);
   1095 
   1096 		sc->xchead = (sc->xchead + 1) % sc->ntxbuf;
   1097 		sc->xmit_busy++;
   1098 	}
   1099 }
   1100 
   1101 static void
   1102 iereset(struct ie_softc *sc)
   1103 {
   1104 	int s;
   1105 
   1106 	s = splnet();
   1107 
   1108 	/* No message here.  The caller does that. */
   1109 	iestop(sc);
   1110 
   1111 	/*
   1112 	 * Stop i82586 dead in its tracks.
   1113 	 */
   1114 	if (cmd_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
   1115 		printf("%s: abort commands timed out\n",
   1116 		    device_xname(sc->sc_dev));
   1117 
   1118 	if (cmd_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
   1119 		printf("%s: disable commands timed out\n",
   1120 		    device_xname(sc->sc_dev));
   1121 
   1122 	ieinit(sc);
   1123 
   1124 	splx(s);
   1125 }
   1126 
   1127 /*
   1128  * Send a command to the controller and wait for it to either
   1129  * complete or be accepted, depending on the command.  If the
   1130  * command pointer is null, then pretend that the command is
   1131  * not an action command.  If the command pointer is not null,
   1132  * and the command is an action command, wait for
   1133  * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
   1134  * to become true.
   1135  */
   1136 static int
   1137 cmd_and_wait(struct ie_softc *sc, int cmd, void *pcmd, int mask)
   1138 {
   1139 	volatile struct ie_cmd_common *cc = pcmd;
   1140 	volatile struct ie_sys_ctl_block *scb = sc->scb;
   1141 	int tmo;
   1142 
   1143 	scb->ie_command = (uint16_t)cmd;
   1144 	(sc->chan_attn)(sc);
   1145 
   1146 	/* Wait for the command to be accepted by the CU. */
   1147 	tmo = 10;
   1148 	while (scb->ie_command && --tmo)
   1149 		delay(10);
   1150 	if (scb->ie_command) {
   1151 #ifdef	IEDEBUG
   1152 		printf("%s: cmd_and_wait, CU stuck (1)\n",
   1153 		    device_xname(sc->sc_dev));
   1154 #endif
   1155 		return -1;	/* timed out */
   1156 	}
   1157 
   1158 	/*
   1159 	 * If asked, also wait for it to finish.
   1160 	 */
   1161 	if (IE_ACTION_COMMAND(cmd) && pcmd) {
   1162 
   1163 		/*
   1164 		 * According to the packet driver, the minimum timeout should
   1165 		 * be .369 seconds, which we round up to .4.
   1166 		 */
   1167 		tmo = 36900;
   1168 
   1169 		/*
   1170 		 * Now spin-lock waiting for status.  This is not a very nice
   1171 		 * thing to do, but I haven't figured out how, or indeed if, we
   1172 		 * can put the process waiting for action to sleep.  (We may
   1173 		 * be getting called through some other timeout running in the
   1174 		 * kernel.)
   1175 		 */
   1176 		while (((cc->ie_cmd_status & mask) == 0) && --tmo)
   1177 			delay(10);
   1178 
   1179 		if ((cc->ie_cmd_status & mask) == 0) {
   1180 #ifdef	IEDEBUG
   1181 			printf("%s: cmd_and_wait, CU stuck (2)\n",
   1182 			    device_xname(sc->sc_dev));
   1183 #endif
   1184 			return -1;	/* timed out */
   1185 		}
   1186 	}
   1187 	return 0;
   1188 }
   1189 
   1190 /*
   1191  * Run the time-domain reflectometer.
   1192  */
   1193 static void
   1194 run_tdr(struct ie_softc *sc, struct ie_tdr_cmd *cmd)
   1195 {
   1196 	int result;
   1197 
   1198 	cmd->com.ie_cmd_status = SWAP(0);
   1199 	cmd->com.ie_cmd_cmd = IE_CMD_TDR | IE_CMD_LAST;
   1200 	cmd->com.ie_cmd_link = SWAP(0xffff);
   1201 
   1202 	sc->scb->ie_command_list = vtop16sw(sc, cmd);
   1203 	cmd->ie_tdr_time = SWAP(0);
   1204 
   1205 	if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1206 	    (cmd->com.ie_cmd_status & IE_STAT_OK) == 0)
   1207 		result = 0x10000;	/* impossible value */
   1208 	else
   1209 		result = cmd->ie_tdr_time;
   1210 
   1211 	ie_ack(sc, IE_ST_WHENCE);
   1212 
   1213 	if (result & IE_TDR_SUCCESS)
   1214 		return;
   1215 
   1216 	if (result & 0x10000) {
   1217 		printf("%s: TDR command failed\n", device_xname(sc->sc_dev));
   1218 	} else if (result & IE_TDR_XCVR) {
   1219 		printf("%s: transceiver problem\n", device_xname(sc->sc_dev));
   1220 	} else if (result & IE_TDR_OPEN) {
   1221 		printf("%s: TDR detected an open %d clocks away\n",
   1222 		    device_xname(sc->sc_dev), SWAP(result & IE_TDR_TIME));
   1223 	} else if (result & IE_TDR_SHORT) {
   1224 		printf("%s: TDR detected a short %d clocks away\n",
   1225 		    device_xname(sc->sc_dev), SWAP(result & IE_TDR_TIME));
   1226 	} else {
   1227 		printf("%s: TDR returned unknown status 0x%x\n",
   1228 		    device_xname(sc->sc_dev), result);
   1229 	}
   1230 }
   1231 
   1232 /*
   1233  * iememinit: set up the buffers
   1234  *
   1235  * we have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
   1236  * this is to be used for the buffers.  the chip indexs its control data
   1237  * structures with 16 bit offsets, and it indexes actual buffers with
   1238  * 24 bit addresses.   so we should allocate control buffers first so that
   1239  * we don't overflow the 16 bit offset field.   The number of transmit
   1240  * buffers is fixed at compile time.
   1241  *
   1242  * note: this function was written to be easy to understand, rather than
   1243  *       highly efficient (it isn't in the critical path).
   1244  *
   1245  * The memory layout is: tbufs, rbufs, (gap), control blocks
   1246  * [tbuf0, tbuf1] [rbuf0,...rbufN] gap [rframes] [tframes]
   1247  * XXX - This needs review...
   1248  */
   1249 static void
   1250 iememinit(struct ie_softc *sc)
   1251 {
   1252 	uint8_t *ptr;
   1253 	int i;
   1254 	uint16_t nxt;
   1255 
   1256 	/* First, zero all the memory. */
   1257 	ptr = sc->buf_area;
   1258 	(sc->sc_memset)(ptr, 0, sc->buf_area_sz);
   1259 
   1260 	/* Allocate tx/rx buffers. */
   1261 	for (i = 0; i < NTXBUF; i++) {
   1262 		sc->xmit_cbuffs[i] = ptr;
   1263 		ptr += IE_TBUF_SIZE;
   1264 	}
   1265 	for (i = 0; i < sc->nrxbuf; i++) {
   1266 		sc->cbuffs[i] = ptr;
   1267 		ptr += IE_RBUF_SIZE;
   1268 	}
   1269 
   1270 	/* Small pad (Don't trust the chip...) */
   1271 	ptr += 16;
   1272 
   1273 	/* Allocate and fill in xmit buffer descriptors. */
   1274 	for (i = 0; i < NTXBUF; i++) {
   1275 		sc->xmit_buffs[i] = (volatile void *)ptr;
   1276 		ptr = Align(ptr + sizeof(*sc->xmit_buffs[i]));
   1277 		sc->xmit_buffs[i]->ie_xmit_buf =
   1278 		    Swap32(vtop24(sc, sc->xmit_cbuffs[i]));
   1279 		sc->xmit_buffs[i]->ie_xmit_next = SWAP(0xffff);
   1280 	}
   1281 
   1282 	/* Allocate and fill in recv buffer descriptors. */
   1283 	for (i = 0; i < sc->nrxbuf; i++) {
   1284 		sc->rbuffs[i] = (volatile void *)ptr;
   1285 		ptr = Align(ptr + sizeof(*sc->rbuffs[i]));
   1286 		sc->rbuffs[i]->ie_rbd_buffer =
   1287 		    Swap32(vtop24(sc, sc->cbuffs[i]));
   1288 		sc->rbuffs[i]->ie_rbd_length = SWAP(IE_RBUF_SIZE);
   1289 	}
   1290 
   1291 	/* link together recv bufs and set EOL on last */
   1292 	i = sc->nrxbuf - 1;
   1293 	sc->rbuffs[i]->ie_rbd_length |= IE_RBD_LAST;
   1294 	nxt = vtop16sw(sc, __UNVOLATILE(sc->rbuffs[0]));
   1295 	do {
   1296 		sc->rbuffs[i]->ie_rbd_next = nxt;
   1297 		nxt = vtop16sw(sc, __UNVOLATILE(sc->rbuffs[i]));
   1298 	} while (--i >= 0);
   1299 
   1300 	/* Allocate transmit commands. */
   1301 	for (i = 0; i < NTXBUF; i++) {
   1302 		sc->xmit_cmds[i] = (volatile void *)ptr;
   1303 		ptr = Align(ptr + sizeof(*sc->xmit_cmds[i]));
   1304 		sc->xmit_cmds[i]->com.ie_cmd_link = SWAP(0xffff);
   1305 	}
   1306 
   1307 	/* Allocate receive frames. */
   1308 	for (i = 0; i < sc->nframes; i++) {
   1309 		sc->rframes[i] = (volatile void *)ptr;
   1310 		ptr = Align(ptr + sizeof(*sc->rframes[i]));
   1311 	}
   1312 
   1313 	/* Link together recv frames and set EOL on last */
   1314 	i = sc->nframes - 1;
   1315 	sc->rframes[i]->ie_fd_last |= IE_FD_LAST;
   1316 	nxt = vtop16sw(sc, __UNVOLATILE(sc->rframes[0]));
   1317 	do {
   1318 		sc->rframes[i]->ie_fd_next = nxt;
   1319 		nxt = vtop16sw(sc, __UNVOLATILE(sc->rframes[i]));
   1320 	} while (--i >= 0);
   1321 
   1322 
   1323 	/* Pointers to last packet sent and next available transmit buffer. */
   1324 	sc->xchead = sc->xctail = 0;
   1325 
   1326 	/* Clear transmit-busy flag. */
   1327 	sc->xmit_busy = 0;
   1328 
   1329 	/*
   1330 	 * Set the head and tail pointers on receive to keep track of
   1331 	 * the order in which RFDs and RBDs are used.   link the
   1332 	 * recv frames and buffer into the scb.
   1333 	 */
   1334 	sc->rfhead = 0;
   1335 	sc->rftail = sc->nframes - 1;
   1336 	sc->rbhead = 0;
   1337 	sc->rbtail = sc->nrxbuf - 1;
   1338 
   1339 	sc->scb->ie_recv_list =
   1340 	    vtop16sw(sc, __UNVOLATILE(sc->rframes[0]));
   1341 	sc->rframes[0]->ie_fd_buf_desc =
   1342 	    vtop16sw(sc, __UNVOLATILE(sc->rbuffs[0]));
   1343 
   1344 	i = (ptr - sc->buf_area);
   1345 #ifdef IEDEBUG
   1346 	printf("IE_DEBUG: used %d of %d bytes\n", i, sc->buf_area_sz);
   1347 #endif
   1348 	if (i > sc->buf_area_sz)
   1349 		panic("ie: iememinit, out of space");
   1350 }
   1351 
   1352 /*
   1353  * Run the multicast setup command.
   1354  * Called at splnet().
   1355  */
   1356 static int
   1357 mc_setup(struct ie_softc *sc, void *ptr)
   1358 {
   1359 	struct ie_mcast_cmd *cmd = ptr;	/* XXX - Was volatile */
   1360 
   1361 	cmd->com.ie_cmd_status = SWAP(0);
   1362 	cmd->com.ie_cmd_cmd = IE_CMD_MCAST | IE_CMD_LAST;
   1363 	cmd->com.ie_cmd_link = SWAP(0xffff);
   1364 
   1365 	(sc->sc_memcpy)((void *)cmd->ie_mcast_addrs,
   1366 	    (void *)sc->mcast_addrs,
   1367 	    sc->mcast_count * sizeof *sc->mcast_addrs);
   1368 
   1369 	cmd->ie_mcast_bytes =
   1370 	    SWAP(sc->mcast_count * ETHER_ADDR_LEN);	/* grrr... */
   1371 
   1372 	sc->scb->ie_command_list = vtop16sw(sc, cmd);
   1373 	if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1374 	    (cmd->com.ie_cmd_status & IE_STAT_OK) == 0) {
   1375 		printf("%s: multicast address setup command failed\n",
   1376 		    device_xname(sc->sc_dev));
   1377 		return 0;
   1378 	}
   1379 	return 1;
   1380 }
   1381 
   1382 static inline void
   1383 ie_setup_config(struct ie_config_cmd *cmd, int promiscuous, int manchester)
   1384 {
   1385 
   1386 	/*
   1387 	 * these are all char's so no need to byte-swap
   1388 	 */
   1389 	cmd->ie_config_count = 0x0c;
   1390 	cmd->ie_fifo = 8;
   1391 	cmd->ie_save_bad = 0x40;
   1392 	cmd->ie_addr_len = 0x2e;
   1393 	cmd->ie_priority = 0;
   1394 	cmd->ie_ifs = 0x60;
   1395 	cmd->ie_slot_low = 0;
   1396 	cmd->ie_slot_high = 0xf2;
   1397 	cmd->ie_promisc = promiscuous | manchester << 2;
   1398 	cmd->ie_crs_cdt = 0;
   1399 	cmd->ie_min_len = 64;
   1400 	cmd->ie_junk = 0xff;
   1401 }
   1402 
   1403 /*
   1404  * This routine inits the ie.
   1405  * This includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands,
   1406  * starting the receiver unit, and clearing interrupts.
   1407  *
   1408  * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
   1409  */
   1410 static int
   1411 ieinit(struct ie_softc *sc)
   1412 {
   1413 	volatile struct ie_sys_ctl_block *scb = sc->scb;
   1414 	void *ptr;
   1415 	struct ifnet *ifp;
   1416 
   1417 	ifp = &sc->sc_if;
   1418 	ptr = sc->buf_area;	/* XXX - Use scb instead? */
   1419 
   1420 	/*
   1421 	 * Send the configure command first.
   1422 	 */
   1423 	{
   1424 		struct ie_config_cmd *cmd = ptr;	/* XXX - Was volatile */
   1425 
   1426 		scb->ie_command_list = vtop16sw(sc, cmd);
   1427 		cmd->com.ie_cmd_status = SWAP(0);
   1428 		cmd->com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
   1429 		cmd->com.ie_cmd_link = SWAP(0xffff);
   1430 
   1431 		ie_setup_config(cmd, (sc->promisc != 0), 0);
   1432 
   1433 		if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1434 		    (cmd->com.ie_cmd_status & IE_STAT_OK) == 0) {
   1435 			printf("%s: configure command failed\n",
   1436 			    device_xname(sc->sc_dev));
   1437 			return 0;
   1438 		}
   1439 	}
   1440 
   1441 	/*
   1442 	 * Now send the Individual Address Setup command.
   1443 	 */
   1444 	{
   1445 		struct ie_iasetup_cmd *cmd = ptr;	/* XXX - Was volatile */
   1446 
   1447 		scb->ie_command_list = vtop16sw(sc, cmd);
   1448 		cmd->com.ie_cmd_status = SWAP(0);
   1449 		cmd->com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
   1450 		cmd->com.ie_cmd_link = SWAP(0xffff);
   1451 
   1452 		(sc->sc_memcpy)((void *)&cmd->ie_address,
   1453 		    CLLADDR(ifp->if_sadl), sizeof(cmd->ie_address));
   1454 
   1455 		if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1456 		    (cmd->com.ie_cmd_status & IE_STAT_OK) == 0) {
   1457 			printf("%s: individual address setup command failed\n",
   1458 			    device_xname(sc->sc_dev));
   1459 			return 0;
   1460 		}
   1461 	}
   1462 
   1463 	/*
   1464 	 * Now run the time-domain reflectometer.
   1465 	 */
   1466 	if (ie_run_tdr)
   1467 		run_tdr(sc, ptr);
   1468 
   1469 	/*
   1470 	 * Acknowledge any interrupts we have generated thus far.
   1471 	 */
   1472 	ie_ack(sc, IE_ST_WHENCE);
   1473 
   1474 	/*
   1475 	 * Set up the transmit and recv buffers.
   1476 	 */
   1477 	iememinit(sc);
   1478 
   1479 	/* tell higher levels that we are here */
   1480 	ifp->if_flags |= IFF_RUNNING;
   1481 	ifp->if_flags &= ~IFF_OACTIVE;
   1482 
   1483 	sc->scb->ie_recv_list =
   1484 	    vtop16sw(sc, __UNVOLATILE(sc->rframes[0]));
   1485 	cmd_and_wait(sc, IE_RU_START, 0, 0);
   1486 
   1487 	ie_ack(sc, IE_ST_WHENCE);
   1488 
   1489 	if (sc->run_586)
   1490 		(sc->run_586)(sc);
   1491 
   1492 	return 0;
   1493 }
   1494 
   1495 static void
   1496 iestop(struct ie_softc *sc)
   1497 {
   1498 
   1499 	cmd_and_wait(sc, IE_RU_DISABLE, 0, 0);
   1500 }
   1501 
   1502 static int
   1503 ieioctl(struct ifnet *ifp, u_long cmd, void *data)
   1504 {
   1505 	struct ie_softc *sc = ifp->if_softc;
   1506 	struct ifaddr *ifa = (struct ifaddr *)data;
   1507 	int s, error = 0;
   1508 
   1509 	s = splnet();
   1510 
   1511 	switch (cmd) {
   1512 
   1513 	case SIOCINITIFADDR:
   1514 		ifp->if_flags |= IFF_UP;
   1515 
   1516 		switch (ifa->ifa_addr->sa_family) {
   1517 #ifdef INET
   1518 		case AF_INET:
   1519 			ieinit(sc);
   1520 			arp_ifinit(ifp, ifa);
   1521 			break;
   1522 #endif
   1523 #ifdef NS
   1524 		/* XXX - This code is probably wrong. */
   1525 		case AF_NS:
   1526 		    {
   1527 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1528 
   1529 			if (ns_nullhost(*ina))
   1530 				ina->x_host =
   1531 				    *(union ns_host *)LLADDR(ifp->if_sadl);
   1532 			else
   1533 				memcpy(LLADDR(ifp->if_sadl),
   1534 				    ina->x_host.c_host, ETHER_ADDR_LEN);
   1535 			/* Set new address. */
   1536 			ieinit(sc);
   1537 			break;
   1538 		    }
   1539 #endif /* NS */
   1540 		default:
   1541 			ieinit(sc);
   1542 			break;
   1543 		}
   1544 		break;
   1545 
   1546 	case SIOCSIFFLAGS:
   1547 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1548 			break;
   1549 		sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
   1550 
   1551 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
   1552 		case IFF_RUNNING:
   1553 			/*
   1554 			 * If interface is marked down and it is running, then
   1555 			 * stop it.
   1556 			 */
   1557 			iestop(sc);
   1558 			ifp->if_flags &= ~IFF_RUNNING;
   1559 			break;
   1560 		case IFF_UP:
   1561 			/*
   1562 			 * If interface is marked up and it is stopped, then
   1563 			 * start it.
   1564 			 */
   1565 			ieinit(sc);
   1566 			break;
   1567 		default:
   1568 			/*
   1569 			 * Reset the interface to pick up changes in any other
   1570 			 * flags that affect hardware registers.
   1571 			 */
   1572 			iestop(sc);
   1573 			ieinit(sc);
   1574 			break;
   1575 		}
   1576 #ifdef IEDEBUG
   1577 		if (ifp->if_flags & IFF_DEBUG)
   1578 			sc->sc_debug = IED_ALL;
   1579 		else
   1580 			sc->sc_debug = ie_debug_flags;
   1581 #endif
   1582 		break;
   1583 
   1584 	case SIOCADDMULTI:
   1585 	case SIOCDELMULTI:
   1586 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1587 			/*
   1588 			 * Multicast list has changed; set the hardware filter
   1589 			 * accordingly.
   1590 			 */
   1591 			if (ifp->if_flags & IFF_RUNNING)
   1592 				mc_reset(sc);
   1593 			error = 0;
   1594 		}
   1595 		break;
   1596 
   1597 	default:
   1598 		error = ether_ioctl(ifp, cmd, data);
   1599 		break;
   1600 	}
   1601 	splx(s);
   1602 	return error;
   1603 }
   1604 
   1605 static void
   1606 mc_reset(struct ie_softc *sc)
   1607 {
   1608 	struct ether_multi *enm;
   1609 	struct ether_multistep step;
   1610 	struct ifnet *ifp;
   1611 
   1612 	ifp = &sc->sc_if;
   1613 
   1614 	/*
   1615 	 * Step through the list of addresses.
   1616 	 */
   1617 	sc->mcast_count = 0;
   1618 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1619 	while (enm) {
   1620 		if (sc->mcast_count >= MAXMCAST ||
   1621 		    ether_cmp(enm->enm_addrlo, enm->enm_addrhi) != 0) {
   1622 			ifp->if_flags |= IFF_ALLMULTI;
   1623 			ieioctl(ifp, SIOCSIFFLAGS, NULL);
   1624 			goto setflag;
   1625 		}
   1626 		memcpy(&sc->mcast_addrs[sc->mcast_count], enm->enm_addrlo,
   1627 		    ETHER_ADDR_LEN);
   1628 		sc->mcast_count++;
   1629 		ETHER_NEXT_MULTI(step, enm);
   1630 	}
   1631 setflag:
   1632 	sc->want_mcsetup = 1;
   1633 }
   1634 
   1635 #ifdef IEDEBUG
   1636 void
   1637 print_rbd(volatile struct ie_recv_buf_desc *rbd)
   1638 {
   1639 
   1640 	printf("RBD at %08lx:\nactual %04x, next %04x, buffer %08x\n"
   1641 	    "length %04x, mbz %04x\n", (u_long)rbd, rbd->ie_rbd_actual,
   1642 	    rbd->ie_rbd_next, rbd->ie_rbd_buffer, rbd->ie_rbd_length,
   1643 	    rbd->mbz);
   1644 }
   1645 #endif
   1646