Home | History | Annotate | Line # | Download | only in ic
i82586.c revision 1.12
      1 /*	$NetBSD: i82586.c,v 1.12 1998/02/28 01:14:57 pk Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1997 Paul Kranenburg.
     41  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
     42  * Copyright (c) 1992, 1993, University of Vermont and State
     43  *  Agricultural College.
     44  * Copyright (c) 1992, 1993, Garrett A. Wollman.
     45  *
     46  * Portions:
     47  * Copyright (c) 1994, 1995, Rafal K. Boni
     48  * Copyright (c) 1990, 1991, William F. Jolitz
     49  * Copyright (c) 1990, The Regents of the University of California
     50  *
     51  * All rights reserved.
     52  *
     53  * Redistribution and use in source and binary forms, with or without
     54  * modification, are permitted provided that the following conditions
     55  * are met:
     56  * 1. Redistributions of source code must retain the above copyright
     57  *    notice, this list of conditions and the following disclaimer.
     58  * 2. Redistributions in binary form must reproduce the above copyright
     59  *    notice, this list of conditions and the following disclaimer in the
     60  *    documentation and/or other materials provided with the distribution.
     61  * 3. All advertising materials mentioning features or use of this software
     62  *    must display the following acknowledgement:
     63  *	This product includes software developed by Charles Hannum, by the
     64  *	University of Vermont and State Agricultural College and Garrett A.
     65  *	Wollman, by William F. Jolitz, and by the University of California,
     66  *	Berkeley, Lawrence Berkeley Laboratory, and its contributors.
     67  * 4. Neither the names of the Universities nor the names of the authors
     68  *    may be used to endorse or promote products derived from this software
     69  *    without specific prior written permission.
     70  *
     71  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     72  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     73  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     74  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
     75  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     76  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     77  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     78  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     79  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     80  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     81  * SUCH DAMAGE.
     82  */
     83 
     84 /*
     85  * Intel 82586 Ethernet chip
     86  * Register, bit, and structure definitions.
     87  *
     88  * Original StarLAN driver written by Garrett Wollman with reference to the
     89  * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
     90  *
     91  * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
     92  *
     93  * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
     94  *
     95  * Majorly cleaned up and 3C507 code merged by Charles Hannum.
     96  *
     97  * Converted to SUN ie driver by Charles D. Cranor,
     98  *		October 1994, January 1995.
     99  * This sun version based on i386 version 1.30.
    100  */
    101 
    102 /*
    103  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
    104  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
    105  * SUN, making life difficult.  Programming this chip is mostly the same,
    106  * but certain details differ from system to system.  This driver is
    107  * written so that different "ie" interfaces can be controled by the same
    108  * driver.
    109  */
    110 
    111 /*
    112 Mode of operation:
    113 
    114    We run the 82586 in a standard Ethernet mode.  We keep NFRAMES
    115    received frame descriptors around for the receiver to use, and
    116    NRXBUF associated receive buffer descriptors, both in a circular
    117    list.  Whenever a frame is received, we rotate both lists as
    118    necessary.  (The 586 treats both lists as a simple queue.)  We also
    119    keep a transmit command around so that packets can be sent off
    120    quickly.
    121 
    122    We configure the adapter in AL-LOC = 1 mode, which means that the
    123    Ethernet/802.3 MAC header is placed at the beginning of the receive
    124    buffer rather than being split off into various fields in the RFD.
    125    This also means that we must include this header in the transmit
    126    buffer as well.
    127 
    128    By convention, all transmit commands, and only transmit commands,
    129    shall have the I (IE_CMD_INTR) bit set in the command.  This way,
    130    when an interrupt arrives at i82586_intr(), it is immediately possible
    131    to tell what precisely caused it.  ANY OTHER command-sending
    132    routines should run at splnet(), and should post an acknowledgement
    133    to every interrupt they generate.
    134 
    135    To save the expense of shipping a command to 82586 every time we
    136    want to send a frame, we use a linked list of commands consisting
    137    of alternate XMIT and NOP commands. The links of these elements
    138    are manipulated (in iexmit()) such that the NOP command loops back
    139    to itself whenever the following XMIT command is not yet ready to
    140    go. Whenever an XMIT is ready, the preceding NOP link is pointed
    141    at it, while its own link field points to the following NOP command.
    142    Thus, a single transmit command sets off an interlocked traversal
    143    of the xmit command chain, with the host processor in control of
    144    the synchronization.
    145 */
    146 
    147 #include "bpfilter.h"
    148 
    149 #include <sys/param.h>
    150 #include <sys/systm.h>
    151 #include <sys/mbuf.h>
    152 #include <sys/buf.h>
    153 #include <sys/protosw.h>
    154 #include <sys/socket.h>
    155 #include <sys/ioctl.h>
    156 #include <sys/errno.h>
    157 #include <sys/syslog.h>
    158 #include <sys/device.h>
    159 
    160 #include <net/if.h>
    161 #include <net/if_dl.h>
    162 #include <net/if_types.h>
    163 #include <net/if_media.h>
    164 #include <net/if_ether.h>
    165 
    166 #if NBPFILTER > 0
    167 #include <net/bpf.h>
    168 #include <net/bpfdesc.h>
    169 #endif
    170 
    171 #ifdef INET
    172 #include <netinet/in.h>
    173 #include <netinet/in_systm.h>
    174 #include <netinet/in_var.h>
    175 #include <netinet/ip.h>
    176 #include <netinet/if_inarp.h>
    177 #endif
    178 
    179 #ifdef NS
    180 #include <netns/ns.h>
    181 #include <netns/ns_if.h>
    182 #endif
    183 
    184 #include <machine/bus.h>
    185 
    186 #include <dev/ic/i82586reg.h>
    187 #include <dev/ic/i82586var.h>
    188 
    189 void	 	i82586_reset 	__P((struct ie_softc *, int));
    190 void 		i82586_watchdog	__P((struct ifnet *));
    191 int 		i82586_init 	__P((struct ie_softc *));
    192 int 		i82586_ioctl 	__P((struct ifnet *, u_long, caddr_t));
    193 void 		i82586_start 	__P((struct ifnet *));
    194 
    195 int 		i82586_rint 	__P((struct ie_softc *, int));
    196 int 		i82586_tint 	__P((struct ie_softc *, int));
    197 
    198 int     	i82586_mediachange 	__P((struct ifnet *));
    199 void    	i82586_mediastatus 	__P((struct ifnet *,
    200 						struct ifmediareq *));
    201 
    202 static int 	ie_readframe		__P((struct ie_softc *, int));
    203 static struct mbuf *ieget 		__P((struct ie_softc *,
    204 					     struct ether_header *, int *,
    205 					     int, int));
    206 static int	i82586_get_rbd_list	__P((struct ie_softc *,
    207 					     u_int16_t *, u_int16_t *, int *));
    208 static void	i82586_release_rbd_list	__P((struct ie_softc *,
    209 					     u_int16_t, u_int16_t));
    210 static int	i82586_drop_frames	__P((struct ie_softc *));
    211 static int	i82586_chk_rx_ring	__P((struct ie_softc *));
    212 
    213 static __inline__ void 	ie_ack 		__P((struct ie_softc *, u_int));
    214 static __inline__ void 	iexmit 		__P((struct ie_softc *));
    215 static void 		i82586_start_transceiver
    216 					__P((struct ie_softc *));
    217 static void 		iestop 		__P((struct ie_softc *));
    218 
    219 static __inline__ int 	ether_equal 	__P((u_char *, u_char *));
    220 static __inline__ int 	check_eh 	__P((struct ie_softc *,
    221 					     struct ether_header *, int *));
    222 
    223 static void	i82586_count_errors	__P((struct ie_softc *));
    224 static void	i82586_rx_errors	__P((struct ie_softc *, int, int));
    225 static void 	i82586_setup_bufs	__P((struct ie_softc *));
    226 static void	setup_simple_command	__P((struct ie_softc *, int, int));
    227 static int 	ie_cfg_setup		__P((struct ie_softc *, int, int, int));
    228 static int	ie_ia_setup		__P((struct ie_softc *, int));
    229 static void 	ie_run_tdr		__P((struct ie_softc *, int));
    230 static int 	ie_mc_setup 		__P((struct ie_softc *, int));
    231 static void 	ie_mc_reset 		__P((struct ie_softc *));
    232 static int 	i82586_start_cmd 	__P((struct ie_softc *,
    233 					    int, int, int, int));
    234 static int	i82586_cmd_wait		__P((struct ie_softc *));
    235 
    236 #ifdef I82586_DEBUG
    237 void 		print_rbd 	__P((struct ie_softc *, int));
    238 
    239 int spurious_intrs = 0;
    240 #endif
    241 
    242 
    243 /*
    244  * Front-ends call this function to attach to the MI driver.
    245  *
    246  * The front-end has responsibility for managing the ICP and ISCP
    247  * structures. Both of these are opaque to us.  Also, the front-end
    248  * chooses a location for the SCB which is expected to be addressable
    249  * (through `sc->scb') as an offset against the shared-memory bus handle.
    250  *
    251  * The following MD interface function must be setup by the front-end
    252  * before calling here:
    253  *
    254  *	hwreset			- board dependent reset
    255  *	hwinit			- board dependent initialization
    256  *	chan_attn		- channel attention
    257  *	intrhook		- board dependent interrupt processing
    258  *	memcopyin		- shared memory copy: board to KVA
    259  *	memcopyout		- shared memory copy: KVA to board
    260  *	ie_bus_read16		- read a sixteen-bit i82586 pointer
    261  *	ie_bus_write16		- write a sixteen-bit i82586 pointer
    262  *	ie_bus_write24		- write a twenty-four-bit i82586 pointer
    263  *
    264  */
    265 void
    266 i82586_attach(sc, name, etheraddr, media, nmedia, defmedia)
    267 	struct ie_softc *sc;
    268 	char *name;
    269 	u_int8_t *etheraddr;
    270         int *media, nmedia, defmedia;
    271 {
    272 	int i;
    273 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    274 
    275 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    276 	ifp->if_softc = sc;
    277 	ifp->if_start = i82586_start;
    278 	ifp->if_ioctl = i82586_ioctl;
    279 	ifp->if_watchdog = i82586_watchdog;
    280 	ifp->if_flags =
    281 		IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    282 
    283         /* Initialize media goo. */
    284         ifmedia_init(&sc->sc_media, 0, i82586_mediachange, i82586_mediastatus);
    285         if (media != NULL) {
    286                 for (i = 0; i < nmedia; i++)
    287                         ifmedia_add(&sc->sc_media, media[i], 0, NULL);
    288                 ifmedia_set(&sc->sc_media, defmedia);
    289         } else {
    290                 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    291                 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    292         }
    293 
    294 	/* Attach the interface. */
    295 	if_attach(ifp);
    296 	ether_ifattach(ifp, etheraddr);
    297 
    298 	printf(" address %s, type %s\n", ether_sprintf(etheraddr), name);
    299 
    300 #if NBPFILTER > 0
    301 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    302 #endif
    303 }
    304 
    305 
    306 /*
    307  * Device timeout/watchdog routine.
    308  * Entered if the device neglects to generate an interrupt after a
    309  * transmit has been started on it.
    310  */
    311 void
    312 i82586_watchdog(ifp)
    313 	struct ifnet *ifp;
    314 {
    315 	struct ie_softc *sc = ifp->if_softc;
    316 
    317 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    318 	++ifp->if_oerrors;
    319 
    320 	i82586_reset(sc, 1);
    321 }
    322 
    323 
    324 /*
    325  * Compare two Ether/802 addresses for equality, inlined and unrolled for
    326  * speed.
    327  */
    328 static __inline__ int
    329 ether_equal(one, two)
    330 	u_char *one, *two;
    331 {
    332 
    333 	if (one[5] != two[5] || one[4] != two[4] || one[3] != two[3] ||
    334 	    one[2] != two[2] || one[1] != two[1] || one[0] != two[0])
    335 		return (0);
    336 	return (1);
    337 }
    338 
    339 /*
    340  * Check for a valid address.  to_bpf is filled in with one of the following:
    341  *   0 -> BPF doesn't get this packet
    342  *   1 -> BPF does get this packet
    343  *   2 -> BPF does get this packet, but we don't
    344  * Return value is true if the packet is for us, and false otherwise.
    345  *
    346  * This routine is a mess, but it's also critical that it be as fast
    347  * as possible.  It could be made cleaner if we can assume that the
    348  * only client which will fiddle with IFF_PROMISC is BPF.  This is
    349  * probably a good assumption, but we do not make it here.  (Yet.)
    350  */
    351 static __inline__ int
    352 check_eh(sc, eh, to_bpf)
    353 	struct ie_softc *sc;
    354 	struct ether_header *eh;
    355 	int *to_bpf;
    356 {
    357 	struct ifnet *ifp;
    358 	int i;
    359 
    360 	ifp = &sc->sc_ethercom.ec_if;
    361 
    362 	switch(sc->promisc) {
    363 	case IFF_ALLMULTI:
    364 		/*
    365 		 * Receiving all multicasts, but no unicasts except those
    366 		 * destined for us.
    367 		 */
    368 #if NBPFILTER > 0
    369 		/* BPF gets this packet if anybody cares */
    370 		*to_bpf = (ifp->if_bpf != 0);
    371 #endif
    372 		if (eh->ether_dhost[0] & 1)
    373 			return (1);
    374 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    375 			return (1);
    376 		return (0);
    377 
    378 	case IFF_PROMISC:
    379 		/*
    380 		 * Receiving all packets.  These need to be passed on to BPF.
    381 		 */
    382 #if NBPFILTER > 0
    383 		*to_bpf = (ifp->if_bpf != 0);
    384 #endif
    385 		/* If for us, accept and hand up to BPF */
    386 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    387 			return (1);
    388 
    389 #if NBPFILTER > 0
    390 		if (*to_bpf)
    391 			*to_bpf = 2; /* we don't need to see it */
    392 #endif
    393 
    394 		/*
    395 		 * Not a multicast, so BPF wants to see it but we don't.
    396 		 */
    397 		if ((eh->ether_dhost[0] & 1) == 0)
    398 			return (1);
    399 
    400 		/*
    401 		 * If it's one of our multicast groups, accept it
    402 		 * and pass it up.
    403 		 */
    404 		for (i = 0; i < sc->mcast_count; i++) {
    405 			if (ether_equal(eh->ether_dhost,
    406 					(u_char *)&sc->mcast_addrs[i])) {
    407 #if NBPFILTER > 0
    408 				if (*to_bpf)
    409 					*to_bpf = 1;
    410 #endif
    411 				return (1);
    412 			}
    413 		}
    414 		return (1);
    415 
    416 	case IFF_ALLMULTI | IFF_PROMISC:
    417 		/*
    418 		 * Acting as a multicast router, and BPF running at the same
    419 		 * time.  Whew!  (Hope this is a fast machine...)
    420 		 */
    421 #if NBPFILTER > 0
    422 		*to_bpf = (ifp->if_bpf != 0);
    423 #endif
    424 		/* We want to see multicasts. */
    425 		if (eh->ether_dhost[0] & 1)
    426 			return (1);
    427 
    428 		/* We want to see our own packets */
    429 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    430 			return (1);
    431 
    432 		/* Anything else goes to BPF but nothing else. */
    433 #if NBPFILTER > 0
    434 		if (*to_bpf)
    435 			*to_bpf = 2;
    436 #endif
    437 		return (1);
    438 
    439 	default:
    440 		/*
    441 		 * Only accept unicast packets destined for us, or multicasts
    442 		 * for groups that we belong to.  For now, we assume that the
    443 		 * '586 will only return packets that we asked it for.  This
    444 		 * isn't strictly true (it uses hashing for the multicast
    445 		 * filter), but it will do in this case, and we want to get
    446 		 * out of here as quickly as possible.
    447 		 */
    448 #if NBPFILTER > 0
    449 		*to_bpf = (ifp->if_bpf != 0);
    450 #endif
    451 		return (1);
    452 	}
    453 	return (0);
    454 }
    455 
    456 static int
    457 i82586_cmd_wait(sc)
    458 	struct ie_softc *sc;
    459 {
    460 	/* spin on i82586 command acknowledge; wait at most 0.9 (!) seconds */
    461 	int i, off;
    462 
    463 	for (i = 0; i < 900000; i++) {
    464 		/* Read the command word */
    465 		off = IE_SCB_CMD(sc->scb);
    466 		bus_space_barrier(sc->bt, sc->bh, off, 2,
    467 				  BUS_SPACE_BARRIER_READ);
    468 		if ((sc->ie_bus_read16)(sc, off) == 0)
    469 			return (0);
    470 		delay(1);
    471 	}
    472 
    473 	printf("i82586_cmd_wait: timo(%ssync): scb status: 0x%x\n",
    474 		sc->async_cmd_inprogress?"a":"", sc->ie_bus_read16(sc, off));
    475 	return (1);	/* Timeout */
    476 }
    477 
    478 /*
    479  * Send a command to the controller and wait for it to either complete
    480  * or be accepted, depending on the command.  If the command pointer
    481  * is null, then pretend that the command is not an action command.
    482  * If the command pointer is not null, and the command is an action
    483  * command, wait for one of the MASK bits to turn on in the command's
    484  * status field.
    485  * If ASYNC is set, we just call the chip's attention and return.
    486  * We may have to wait for the command's acceptance later though.
    487  */
    488 static int
    489 i82586_start_cmd(sc, cmd, iecmdbuf, mask, async)
    490 	struct ie_softc *sc;
    491 	int cmd;
    492 	int iecmdbuf;
    493 	int mask;
    494 	int async;
    495 {
    496 	int i;
    497 	int off;
    498 
    499 	if (sc->async_cmd_inprogress != 0) {
    500 		/*
    501 		 * If previous command was issued asynchronously, wait
    502 		 * for it now.
    503 		 */
    504 		if (i82586_cmd_wait(sc) != 0)
    505 			return (1);
    506 		sc->async_cmd_inprogress = 0;
    507 	}
    508 
    509 	off = IE_SCB_CMD(sc->scb);
    510 	(sc->ie_bus_write16)(sc, off, cmd);
    511 	bus_space_barrier(sc->bt, sc->bh, off, 2, BUS_SPACE_BARRIER_WRITE);
    512 	(sc->chan_attn)(sc);
    513 
    514 	if (async != 0) {
    515 		sc->async_cmd_inprogress = 1;
    516 		return (0);
    517 	}
    518 
    519 	if (IE_ACTION_COMMAND(cmd) && iecmdbuf) {
    520 		int status;
    521 		/*
    522 		 * Now spin-lock waiting for status.  This is not a very nice
    523 		 * thing to do, and can kill performance pretty well...
    524 		 * According to the packet driver, the minimum timeout
    525 		 * should be .369 seconds.
    526 		 */
    527 		for (i = 0; i < 369000; i++) {
    528 			/* Read the command status */
    529 			off = IE_CMD_COMMON_STATUS(iecmdbuf);
    530 			bus_space_barrier(sc->bt, sc->bh, off, 2,
    531 					  BUS_SPACE_BARRIER_READ);
    532 			status = (sc->ie_bus_read16)(sc, off);
    533 			if (status & mask)
    534 				return (0);
    535 			delay(1);
    536 		}
    537 
    538 	} else {
    539 		/*
    540 		 * Otherwise, just wait for the command to be accepted.
    541 		 */
    542 		return (i82586_cmd_wait(sc));
    543 	}
    544 
    545 	/* Timeout */
    546 	return (1);
    547 }
    548 
    549 /*
    550  * Interrupt Acknowledge.
    551  */
    552 static __inline__ void
    553 ie_ack(sc, mask)
    554 	struct ie_softc *sc;
    555 	u_int mask;	/* in native byte-order */
    556 {
    557 	u_int status;
    558 
    559 	bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
    560 	status = (sc->ie_bus_read16)(sc, IE_SCB_STATUS(sc->scb));
    561 	i82586_start_cmd(sc, status & mask, 0, 0, 0);
    562 }
    563 
    564 /*
    565  * Transfer accumulated chip error counters to IF.
    566  */
    567 static __inline void
    568 i82586_count_errors(sc)
    569 	struct ie_softc *sc;
    570 {
    571 	int scb = sc->scb;
    572 
    573 	sc->sc_ethercom.ec_if.if_ierrors +=
    574 	    sc->ie_bus_read16(sc, IE_SCB_ERRCRC(scb)) +
    575 	    sc->ie_bus_read16(sc, IE_SCB_ERRALN(scb)) +
    576 	    sc->ie_bus_read16(sc, IE_SCB_ERRRES(scb)) +
    577 	    sc->ie_bus_read16(sc, IE_SCB_ERROVR(scb));
    578 
    579 	/* Clear error counters */
    580 	sc->ie_bus_write16(sc, IE_SCB_ERRCRC(scb), 0);
    581 	sc->ie_bus_write16(sc, IE_SCB_ERRALN(scb), 0);
    582 	sc->ie_bus_write16(sc, IE_SCB_ERRRES(scb), 0);
    583 	sc->ie_bus_write16(sc, IE_SCB_ERROVR(scb), 0);
    584 }
    585 
    586 static void
    587 i82586_rx_errors(sc, fn, status)
    588 	struct ie_softc *sc;
    589 	int fn;
    590 	int status;
    591 {
    592 	char bits[128];
    593 
    594 	log(LOG_ERR, "%s: rx error (frame# %d): %s\n", sc->sc_dev.dv_xname, fn,
    595 	    bitmask_snprintf(status, IE_FD_STATUSBITS, bits, sizeof(bits)));
    596 }
    597 
    598 /*
    599  * i82586 interrupt entry point.
    600  */
    601 int
    602 i82586_intr(v)
    603 	void *v;
    604 {
    605 	struct ie_softc *sc = v;
    606 	u_int status;
    607 	int off;
    608 
    609         /*
    610          * Implementation dependent interrupt handling.
    611          */
    612 	if (sc->intrhook)
    613 		(sc->intrhook)(sc, INTR_ENTER);
    614 
    615 	off = IE_SCB_STATUS(sc->scb);
    616 	bus_space_barrier(sc->bt, sc->bh, off, 2, BUS_SPACE_BARRIER_READ);
    617 	status = sc->ie_bus_read16(sc, off) & IE_ST_WHENCE;
    618 
    619 	if ((status & IE_ST_WHENCE) == 0) {
    620 #ifdef I82586_DEBUG
    621 		if ((spurious_intrs++ % 25) == 0)
    622 		    printf("%s: i82586_intr: %d spurious interrupts\n",
    623 			   sc->sc_dev.dv_xname, spurious_intrs);
    624 #endif
    625 		if (sc->intrhook)
    626 			(sc->intrhook)(sc, INTR_EXIT);
    627 
    628 		return (0);
    629 	}
    630 
    631 loop:
    632 	/* Ack interrupts FIRST in case we receive more during the ISR. */
    633 #if 0
    634 	ie_ack(sc, status & IE_ST_WHENCE);
    635 #endif
    636 	i82586_start_cmd(sc, status & IE_ST_WHENCE, 0, 0, 1);
    637 
    638 	if (status & (IE_ST_FR | IE_ST_RNR))
    639 		if (i82586_rint(sc, status) != 0)
    640 			goto reset;
    641 
    642 	if (status & IE_ST_CX)
    643 		if (i82586_tint(sc, status) != 0)
    644 			goto reset;
    645 
    646 #ifdef I82586_DEBUG
    647 	if ((status & IE_ST_CNA) && (sc->sc_debug & IED_CNA))
    648 		printf("%s: cna; status=0x%x\n", sc->sc_dev.dv_xname, status);
    649 #endif
    650 	if (sc->intrhook)
    651 		(sc->intrhook)(sc, INTR_LOOP);
    652 
    653 	/*
    654 	 * Interrupt ACK was posted asynchronously; wait for
    655 	 * completion here before reading SCB status again.
    656 	 */
    657 	i82586_cmd_wait(sc);
    658 
    659 	bus_space_barrier(sc->bt, sc->bh, off, 2, BUS_SPACE_BARRIER_READ);
    660 	status = sc->ie_bus_read16(sc, off);
    661 	if ((status & IE_ST_WHENCE) != 0)
    662 		goto loop;
    663 
    664 out:
    665 	if (sc->intrhook)
    666 		(sc->intrhook)(sc, INTR_EXIT);
    667 	return (1);
    668 
    669 reset:
    670 	i82586_cmd_wait(sc);
    671 	i82586_reset(sc, 1);
    672 	goto out;
    673 
    674 }
    675 
    676 /*
    677  * Process a received-frame interrupt.
    678  */
    679 int
    680 i82586_rint(sc, scbstatus)
    681 	struct	ie_softc *sc;
    682 	int	scbstatus;
    683 {
    684 static	int timesthru = 1024;
    685 	int i, status, off;
    686 
    687 #ifdef I82586_DEBUG
    688 	if (sc->sc_debug & IED_RINT)
    689 		printf("%s: rint: status 0x%x\n",
    690 			sc->sc_dev.dv_xname, scbstatus);
    691 #endif
    692 
    693 	for (;;) {
    694 		int drop = 0;
    695 
    696 		i = sc->rfhead;
    697 		off = IE_RFRAME_STATUS(sc->rframes, i);
    698 		bus_space_barrier(sc->bt, sc->bh, off, 2,
    699 				  BUS_SPACE_BARRIER_READ);
    700 		status = sc->ie_bus_read16(sc, off);
    701 
    702 #ifdef I82586_DEBUG
    703 		if (sc->sc_debug & IED_RINT)
    704 			printf("%s: rint: frame(%d) status 0x%x\n",
    705 				sc->sc_dev.dv_xname, i, status);
    706 #endif
    707 		if ((status & IE_FD_COMPLETE) == 0) {
    708 			if ((status & IE_FD_OK) != 0) {
    709 				printf("%s: rint: weird: ",
    710 					sc->sc_dev.dv_xname);
    711 				i82586_rx_errors(sc, i, status);
    712 				break;
    713 			}
    714 			if (--timesthru == 0) {
    715 				/* Account the accumulated errors */
    716 				i82586_count_errors(sc);
    717 				timesthru = 1024;
    718 			}
    719 			break;
    720 		} else if ((status & IE_FD_OK) == 0) {
    721 			/*
    722 			 * If the chip is configured to automatically
    723 			 * discard bad frames, the only reason we can
    724 			 * get here is an "out-of-resource" condition.
    725 			 */
    726 			i82586_rx_errors(sc, i, status);
    727 			drop = 1;
    728 		}
    729 
    730 #ifdef I82586_DEBUG
    731 		if ((status & IE_FD_BUSY) != 0)
    732 			printf("%s: rint: frame(%d) busy; status=0x%x\n",
    733 				sc->sc_dev.dv_xname, i, status);
    734 #endif
    735 
    736 
    737 		/*
    738 		 * Advance the RFD list, since we're done with
    739 		 * this descriptor.
    740 		 */
    741 
    742 		/* Clear frame status */
    743 		sc->ie_bus_write16(sc, off, 0);
    744 
    745 		/* Put fence at this frame (the head) */
    746 		off = IE_RFRAME_LAST(sc->rframes, i);
    747 		sc->ie_bus_write16(sc, off, IE_FD_EOL|IE_FD_SUSP);
    748 
    749 		/* and clear RBD field */
    750 		off = IE_RFRAME_BUFDESC(sc->rframes, i);
    751 		sc->ie_bus_write16(sc, off, 0xffff);
    752 
    753 		/* Remove fence from current tail */
    754 		off = IE_RFRAME_LAST(sc->rframes, sc->rftail);
    755 		sc->ie_bus_write16(sc, off, 0);
    756 
    757 		if (++sc->rftail == sc->nframes)
    758 			sc->rftail = 0;
    759 		if (++sc->rfhead == sc->nframes)
    760 			sc->rfhead = 0;
    761 
    762 		/* Pull the frame off the board */
    763 		if (drop) {
    764 			i82586_drop_frames(sc);
    765 			if ((status & IE_FD_RNR) != 0)
    766 				sc->rnr_expect = 1;
    767 			sc->sc_ethercom.ec_if.if_ierrors++;
    768 		} else if (ie_readframe(sc, i) != 0)
    769 			return (1);
    770 	}
    771 
    772 	if ((scbstatus & IE_ST_RNR) != 0) {
    773 
    774 		/*
    775 		 * Receiver went "Not Ready". We try to figure out
    776 		 * whether this was an expected event based on past
    777 		 * frame status values.
    778 		 */
    779 
    780 		if ((scbstatus & IE_RUS_SUSPEND) != 0) {
    781 			/*
    782 			 * We use the "suspend on last frame" flag.
    783 			 * Send a RU RESUME command in response, since
    784 			 * we should have dealt with all completed frames
    785 			 * by now.
    786 			 */
    787 			printf("RINT: SUSPENDED; scbstatus=0x%x\n",
    788 				scbstatus);
    789 			if (i82586_start_cmd(sc, IE_RUC_RESUME, 0, 0, 0) == 0)
    790 				return (0);
    791 			printf("%s: RU RESUME command timed out\n",
    792 				sc->sc_dev.dv_xname);
    793 			return (1);	/* Ask for a reset */
    794 		}
    795 
    796 		if (sc->rnr_expect != 0) {
    797 			/*
    798 			 * The RNR condition was announced in the previously
    799 			 * completed frame.  Assume the receive ring is Ok,
    800 			 * so restart the receiver without further delay.
    801 			 */
    802 			i82586_start_transceiver(sc);
    803 			sc->rnr_expect = 0;
    804 			return (0);
    805 
    806 		} else if ((scbstatus & IE_RUS_NOSPACE) != 0) {
    807 			/*
    808 			 * We saw no previous IF_FD_RNR flag.
    809 			 * We check our ring invariants and, if ok,
    810 			 * just restart the receiver at the current
    811 			 * point in the ring.
    812 			 */
    813 			if (i82586_chk_rx_ring(sc) != 0)
    814 				return (1);
    815 
    816 			i82586_start_transceiver(sc);
    817 			sc->sc_ethercom.ec_if.if_ierrors++;
    818 			return (0);
    819 		} else
    820 			printf("%s: receiver not ready; scbstatus=0x%x\n",
    821 				sc->sc_dev.dv_xname, scbstatus);
    822 
    823 		sc->sc_ethercom.ec_if.if_ierrors++;
    824 		return (1);	/* Ask for a reset */
    825 	}
    826 
    827 	return (0);
    828 }
    829 
    830 /*
    831  * Process a command-complete interrupt.  These are only generated by the
    832  * transmission of frames.  This routine is deceptively simple, since most
    833  * of the real work is done by i82586_start().
    834  */
    835 int
    836 i82586_tint(sc, scbstatus)
    837 	struct ie_softc *sc;
    838 	int	scbstatus;
    839 {
    840 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    841 	int status;
    842 
    843 	ifp->if_timer = 0;
    844 	ifp->if_flags &= ~IFF_OACTIVE;
    845 
    846 #ifdef I82586_DEBUG
    847 	if (sc->xmit_busy <= 0) {
    848 	    printf("i82586_tint: WEIRD: xmit_busy=%d, xctail=%d, xchead=%d\n",
    849 		   sc->xmit_busy, sc->xctail, sc->xchead);
    850 		return (0);
    851 	}
    852 #endif
    853 
    854 	status = sc->ie_bus_read16(sc, IE_CMD_XMIT_STATUS(sc->xmit_cmds,
    855 							  sc->xctail));
    856 
    857 #ifdef I82586_DEBUG
    858 	if (sc->sc_debug & IED_TINT)
    859 		printf("%s: tint: SCB status 0x%x; xmit status 0x%x\n",
    860 			sc->sc_dev.dv_xname, scbstatus, status);
    861 #endif
    862 
    863 	if ((status & IE_STAT_COMPL) == 0 || (status & IE_STAT_BUSY)) {
    864 	    printf("i82586_tint: command still busy; status=0x%x; tail=%d\n",
    865 		   status, sc->xctail);
    866 	    printf("iestatus = 0x%x\n", scbstatus);
    867 	}
    868 
    869 	if (status & IE_STAT_OK) {
    870 		ifp->if_opackets++;
    871 		ifp->if_collisions += (status & IE_XS_MAXCOLL);
    872 	} else {
    873 		ifp->if_oerrors++;
    874 		/*
    875 		 * Check SQE and DEFERRED?
    876 		 * What if more than one bit is set?
    877 		 */
    878 		if (status & IE_STAT_ABORT)
    879 			printf("%s: send aborted\n", sc->sc_dev.dv_xname);
    880 		else if (status & IE_XS_NOCARRIER)
    881 			printf("%s: no carrier\n", sc->sc_dev.dv_xname);
    882 		else if (status & IE_XS_LOSTCTS)
    883 			printf("%s: lost CTS\n", sc->sc_dev.dv_xname);
    884 		else if (status & IE_XS_UNDERRUN)
    885 			printf("%s: DMA underrun\n", sc->sc_dev.dv_xname);
    886 		else if (status & IE_XS_EXCMAX) {
    887 			printf("%s: too many collisions\n",
    888 				sc->sc_dev.dv_xname);
    889 			sc->sc_ethercom.ec_if.if_collisions += 16;
    890 		}
    891 	}
    892 
    893 	/*
    894 	 * If multicast addresses were added or deleted while transmitting,
    895 	 * ie_mc_reset() set the want_mcsetup flag indicating that we
    896 	 * should do it.
    897 	 */
    898 	if (sc->want_mcsetup) {
    899 		ie_mc_setup(sc, IE_XBUF_ADDR(sc, sc->xctail));
    900 		sc->want_mcsetup = 0;
    901 	}
    902 
    903 	/* Done with the buffer. */
    904 	sc->xmit_busy--;
    905 	sc->xctail = (sc->xctail + 1) % NTXBUF;
    906 
    907 	/* Start the next packet, if any, transmitting. */
    908 	if (sc->xmit_busy > 0)
    909 		iexmit(sc);
    910 
    911 	i82586_start(ifp);
    912 	return (0);
    913 }
    914 
    915 /*
    916  * Get a range of receive buffer descriptors that represent one packet.
    917  */
    918 static int
    919 i82586_get_rbd_list(sc, start, end, pktlen)
    920 	struct ie_softc *sc;
    921 	u_int16_t	*start;
    922 	u_int16_t	*end;
    923 	int		*pktlen;
    924 {
    925 	int	off, rbbase = sc->rbds;
    926 	int	rbindex, count = 0;
    927 	int	plen = 0;
    928 	int	rbdstatus;
    929 
    930 	*start = rbindex = sc->rbhead;
    931 
    932 	do {
    933 		off = IE_RBD_STATUS(rbbase, rbindex);
    934 		bus_space_barrier(sc->bt, sc->bh, off, 2,
    935 				  BUS_SPACE_BARRIER_READ);
    936 		rbdstatus = sc->ie_bus_read16(sc, off);
    937 		if ((rbdstatus & IE_RBD_USED) == 0) {
    938 			/*
    939 			 * This means we are somehow out of sync.  So, we
    940 			 * reset the adapter.
    941 			 */
    942 #ifdef I82586_DEBUG
    943 			print_rbd(sc, rbindex);
    944 #endif
    945 			log(LOG_ERR,
    946 			    "%s: receive descriptors out of sync at %d\n",
    947 			    sc->sc_dev.dv_xname, rbindex);
    948 			return (0);
    949 		}
    950 		plen += (rbdstatus & IE_RBD_CNTMASK);
    951 
    952 		if (++rbindex == sc->nrxbuf)
    953 			rbindex = 0;
    954 
    955 		++count;
    956 	} while ((rbdstatus & IE_RBD_LAST) == 0);
    957 	*end = rbindex;
    958 	*pktlen = plen;
    959 	return (count);
    960 }
    961 
    962 
    963 /*
    964  * Release a range of receive buffer descriptors after we've copied the packet.
    965  */
    966 static void
    967 i82586_release_rbd_list(sc, start, end)
    968 	struct ie_softc *sc;
    969 	u_int16_t	start;
    970 	u_int16_t	end;
    971 {
    972 	int	off, rbbase = sc->rbds;
    973 	int	rbindex = start;
    974 
    975 	do {
    976 		/* Clear buffer status */
    977 		off = IE_RBD_STATUS(rbbase, rbindex);
    978 		sc->ie_bus_write16(sc, off, 0);
    979 		if (++rbindex == sc->nrxbuf)
    980 			rbindex = 0;
    981 	} while (rbindex != end);
    982 
    983 	/* Mark EOL at new tail */
    984 	rbindex = ((rbindex == 0) ? sc->nrxbuf : rbindex) - 1;
    985 	off = IE_RBD_BUFLEN(rbbase, rbindex);
    986 	sc->ie_bus_write16(sc, off, IE_RBUF_SIZE|IE_RBD_EOL);
    987 
    988 	/* Remove EOL from current tail */
    989 	off = IE_RBD_BUFLEN(rbbase, sc->rbtail);
    990 	sc->ie_bus_write16(sc, off, IE_RBUF_SIZE);
    991 
    992 	/* New head & tail pointer */
    993 /* hmm, why have both? head is always (tail + 1) % NRXBUF */
    994 	sc->rbhead = end;
    995 	sc->rbtail = rbindex;
    996 }
    997 
    998 /*
    999  * Drop the packet at the head of the RX buffer ring.
   1000  * Called if the frame descriptor reports an error on this packet.
   1001  * Returns 1 if the buffer descriptor ring appears to be corrupt;
   1002  * and 0 otherwise.
   1003  */
   1004 static int
   1005 i82586_drop_frames(sc)
   1006 	struct ie_softc *sc;
   1007 {
   1008 	u_int16_t bstart, bend;
   1009 	int pktlen;
   1010 
   1011 	if (i82586_get_rbd_list(sc, &bstart, &bend, &pktlen) == 0)
   1012 		return (1);
   1013 	i82586_release_rbd_list(sc, bstart, bend);
   1014 	return (0);
   1015 }
   1016 
   1017 /*
   1018  * Check the RX frame & buffer descriptor lists for our invariants,
   1019  * i.e.: EOL bit set iff. it is pointed at by the r*tail pointer.
   1020  *
   1021  * Called when the receive unit has stopped unexpectedly.
   1022  * Returns 1 if an inconsistency is detected; 0 otherwise.
   1023  *
   1024  * The Receive Unit is expected to be NOT RUNNING.
   1025  */
   1026 static int
   1027 i82586_chk_rx_ring(sc)
   1028 	struct ie_softc *sc;
   1029 {
   1030 	int n, off, val;
   1031 
   1032 	for (n = 0; n < sc->nrxbuf; n++) {
   1033 		off = IE_RBD_BUFLEN(sc->rbds, n);
   1034 		val = sc->ie_bus_read16(sc, off);
   1035 		if ((n == sc->rbtail) ^ ((val & IE_RBD_EOL) != 0)) {
   1036 			/* `rbtail' and EOL flag out of sync */
   1037 			log(LOG_ERR,
   1038 			    "%s: rx buffer descriptors out of sync at %d\n",
   1039 			    sc->sc_dev.dv_xname, n);
   1040 			return (1);
   1041 		}
   1042 
   1043 		/* Take the opportunity to clear the status fields here ? */
   1044 	}
   1045 
   1046 	for (n = 0; n < sc->nframes; n++) {
   1047 		off = IE_RFRAME_LAST(sc->rframes, n);
   1048 		val = sc->ie_bus_read16(sc, off);
   1049 		if ((n == sc->rftail) ^ ((val & (IE_FD_EOL|IE_FD_SUSP)) != 0)) {
   1050 			/* `rftail' and EOL flag out of sync */
   1051 			log(LOG_ERR,
   1052 			    "%s: rx frame list out of sync at %d\n",
   1053 			    sc->sc_dev.dv_xname, n);
   1054 			return (1);
   1055 		}
   1056 	}
   1057 
   1058 	return (0);
   1059 }
   1060 
   1061 /*
   1062  * Read data off the interface, and turn it into an mbuf chain.
   1063  *
   1064  * This code is DRAMATICALLY different from the previous version; this
   1065  * version tries to allocate the entire mbuf chain up front, given the
   1066  * length of the data available.  This enables us to allocate mbuf
   1067  * clusters in many situations where before we would have had a long
   1068  * chain of partially-full mbufs.  This should help to speed up the
   1069  * operation considerably.  (Provided that it works, of course.)
   1070  */
   1071 static __inline struct mbuf *
   1072 ieget(sc, ehp, to_bpf, head, totlen)
   1073 	struct ie_softc *sc;
   1074 	struct ether_header *ehp;
   1075 	int *to_bpf;
   1076 	int head;
   1077 	int totlen;
   1078 {
   1079 	struct mbuf *top, **mp, *m;
   1080 	int len, resid;
   1081 	int thisrboff, thismboff;
   1082 
   1083 	/*
   1084 	 * Snarf the Ethernet header.
   1085 	 */
   1086 	(sc->memcopyin)(sc, ehp, IE_RBUF_ADDR(sc,head), sizeof *ehp);
   1087 
   1088 	/*
   1089 	 * As quickly as possible, check if this packet is for us.
   1090 	 * If not, don't waste a single cycle copying the rest of the
   1091 	 * packet in.
   1092 	 * This is only a consideration when FILTER is defined; i.e., when
   1093 	 * we are either running BPF or doing multicasting.
   1094 	 */
   1095 	if (!check_eh(sc, ehp, to_bpf)) {
   1096 		/* just this case, it's not an error */
   1097 		sc->sc_ethercom.ec_if.if_ierrors--;
   1098 		return (0);
   1099 	}
   1100 
   1101 	resid = totlen -= (thisrboff = sizeof *ehp);
   1102 
   1103 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1104 	if (m == 0)
   1105 		return (0);
   1106 	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
   1107 	m->m_pkthdr.len = totlen;
   1108 	len = MHLEN;
   1109 	top = 0;
   1110 	mp = &top;
   1111 
   1112 	/*
   1113 	 * This loop goes through and allocates mbufs for all the data we will
   1114 	 * be copying in.  It does not actually do the copying yet.
   1115 	 */
   1116 	while (totlen > 0) {
   1117 		if (top) {
   1118 			MGET(m, M_DONTWAIT, MT_DATA);
   1119 			if (m == 0) {
   1120 				m_freem(top);
   1121 				return (0);
   1122 			}
   1123 			len = MLEN;
   1124 		}
   1125 		if (totlen >= MINCLSIZE) {
   1126 			MCLGET(m, M_DONTWAIT);
   1127 			if ((m->m_flags & M_EXT) == 0) {
   1128 				m_freem(top);
   1129 				return (0);
   1130 			}
   1131 			len = MCLBYTES;
   1132 		}
   1133 		m->m_len = len = min(totlen, len);
   1134 		totlen -= len;
   1135 		*mp = m;
   1136 		mp = &m->m_next;
   1137 	}
   1138 
   1139 	m = top;
   1140 	thismboff = 0;
   1141 
   1142 	/*
   1143 	 * Now we take the mbuf chain (hopefully only one mbuf most of the
   1144 	 * time) and stuff the data into it.  There are no possible failures
   1145 	 * at or after this point.
   1146 	 */
   1147 	while (resid > 0) {
   1148 		int thisrblen = IE_RBUF_SIZE - thisrboff,
   1149 		    thismblen = m->m_len - thismboff;
   1150 		len = min(thisrblen, thismblen);
   1151 
   1152 		(sc->memcopyin)(sc, mtod(m, caddr_t) + thismboff,
   1153 				IE_RBUF_ADDR(sc,head) + thisrboff,
   1154 				(u_int)len);
   1155 		resid -= len;
   1156 
   1157 		if (len == thismblen) {
   1158 			m = m->m_next;
   1159 			thismboff = 0;
   1160 		} else
   1161 			thismboff += len;
   1162 
   1163 		if (len == thisrblen) {
   1164 			if (++head == sc->nrxbuf)
   1165 				head = 0;
   1166 			thisrboff = 0;
   1167 		} else
   1168 			thisrboff += len;
   1169 	}
   1170 
   1171 	/*
   1172 	 * Unless something changed strangely while we were doing the copy,
   1173 	 * we have now copied everything in from the shared memory.
   1174 	 * This means that we are done.
   1175 	 */
   1176 	return (top);
   1177 }
   1178 
   1179 /*
   1180  * Read frame NUM from unit UNIT (pre-cached as IE).
   1181  *
   1182  * This routine reads the RFD at NUM, and copies in the buffers from the list
   1183  * of RBD, then rotates the RBD list so that the receiver doesn't start
   1184  * complaining.  Trailers are DROPPED---there's no point in wasting time
   1185  * on confusing code to deal with them.  Hopefully, this machine will
   1186  * never ARP for trailers anyway.
   1187  */
   1188 static int
   1189 ie_readframe(sc, num)
   1190 	struct ie_softc *sc;
   1191 	int num;		/* frame number to read */
   1192 {
   1193 	struct mbuf *m;
   1194 	struct ether_header eh;
   1195 	u_int16_t bstart, bend;
   1196 	int pktlen;
   1197 #if NBPFILTER > 0
   1198 	int bpf_gets_it = 0;
   1199 #endif
   1200 
   1201 	if (i82586_get_rbd_list(sc, &bstart, &bend, &pktlen) == 0) {
   1202 		sc->sc_ethercom.ec_if.if_ierrors++;
   1203 		return (1);
   1204 	}
   1205 
   1206 #if NBPFILTER > 0
   1207 	m = ieget(sc, &eh, &bpf_gets_it, bstart, pktlen);
   1208 #else
   1209 	m = ieget(sc, &eh, 0, bstart, pktlen);
   1210 #endif
   1211 	i82586_release_rbd_list(sc, bstart, bend);
   1212 
   1213 	if (m == 0) {
   1214 		sc->sc_ethercom.ec_if.if_ierrors++;
   1215 		return (0);
   1216 	}
   1217 
   1218 #ifdef I82586_DEBUG
   1219 	if (sc->sc_debug & IED_READFRAME)
   1220 		printf("%s: frame from ether %s type 0x%x len %d\n",
   1221 			sc->sc_dev.dv_xname,
   1222 			ether_sprintf(eh.ether_shost),
   1223 			(u_int)eh.ether_type,
   1224 			pktlen);
   1225 #endif
   1226 
   1227 #if NBPFILTER > 0
   1228 	/*
   1229 	 * Check for a BPF filter; if so, hand it up.
   1230 	 * Note that we have to stick an extra mbuf up front, because bpf_mtap
   1231 	 * expects to have the ether header at the front.
   1232 	 * It doesn't matter that this results in an ill-formatted mbuf chain,
   1233 	 * since BPF just looks at the data.  (It doesn't try to free the mbuf,
   1234 	 * tho' it will make a copy for tcpdump.)
   1235 	 */
   1236 	if (bpf_gets_it) {
   1237 		struct mbuf m0;
   1238 		m0.m_len = sizeof eh;
   1239 		m0.m_data = (caddr_t)&eh;
   1240 		m0.m_next = m;
   1241 
   1242 		/* Pass it up. */
   1243 		bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, &m0);
   1244 
   1245 		/*
   1246 		 * A signal passed up from the filtering code indicating that
   1247 		 * the packet is intended for BPF but not for the protocol
   1248 		 * machinery.  We can save a few cycles by not handing it
   1249 		 * off to them.
   1250 		 */
   1251 		if (bpf_gets_it == 2) {
   1252 			m_freem(m);
   1253 			return (0);
   1254 		}
   1255 	}
   1256 #endif /* NBPFILTER > 0 */
   1257 
   1258 	/*
   1259 	 * Finally pass this packet up to higher layers.
   1260 	 */
   1261 	ether_input(&sc->sc_ethercom.ec_if, &eh, m);
   1262 	sc->sc_ethercom.ec_if.if_ipackets++;
   1263 	return (0);
   1264 }
   1265 
   1266 
   1267 /*
   1268  * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
   1269  * command to the chip to be executed.
   1270  */
   1271 static __inline__ void
   1272 iexmit(sc)
   1273 	struct ie_softc *sc;
   1274 {
   1275 	int off;
   1276 	int cur, prev;
   1277 
   1278 	cur = sc->xctail;
   1279 
   1280 #ifdef I82586_DEBUG
   1281 	if (sc->sc_debug & IED_XMIT)
   1282 		printf("%s: xmit buffer %d\n", sc->sc_dev.dv_xname, cur);
   1283 #endif
   1284 
   1285 	/*
   1286 	 * Setup the transmit command.
   1287 	 */
   1288 	sc->ie_bus_write16(sc, IE_CMD_XMIT_DESC(sc->xmit_cmds, cur),
   1289 			       IE_XBD_ADDR(sc->xbds, cur));
   1290 
   1291 	sc->ie_bus_write16(sc, IE_CMD_XMIT_STATUS(sc->xmit_cmds, cur), 0);
   1292 
   1293 	if (sc->do_xmitnopchain) {
   1294 		/*
   1295 		 * Gate this XMIT command to the following NOP
   1296 		 */
   1297 		sc->ie_bus_write16(sc, IE_CMD_XMIT_LINK(sc->xmit_cmds, cur),
   1298 				       IE_CMD_NOP_ADDR(sc->nop_cmds, cur));
   1299 		sc->ie_bus_write16(sc, IE_CMD_XMIT_CMD(sc->xmit_cmds, cur),
   1300 				       IE_CMD_XMIT | IE_CMD_INTR);
   1301 
   1302 		/*
   1303 		 * Loopback at following NOP
   1304 		 */
   1305 		sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, cur), 0);
   1306 		sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, cur),
   1307 				       IE_CMD_NOP_ADDR(sc->nop_cmds, cur));
   1308 
   1309 		/*
   1310 		 * Gate preceding NOP to this XMIT command
   1311 		 */
   1312 		prev = (cur + NTXBUF - 1) % NTXBUF;
   1313 		sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, prev), 0);
   1314 		sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, prev),
   1315 				       IE_CMD_XMIT_ADDR(sc->xmit_cmds, cur));
   1316 
   1317 		off = IE_SCB_STATUS(sc->scb);
   1318 		bus_space_barrier(sc->bt, sc->bh, off, 2,
   1319 				  BUS_SPACE_BARRIER_READ);
   1320 		if ((sc->ie_bus_read16(sc, off) & IE_CUS_ACTIVE) == 0) {
   1321 			printf("iexmit: CU not active\n");
   1322 			i82586_start_transceiver(sc);
   1323 		}
   1324 	} else {
   1325 		sc->ie_bus_write16(sc, IE_CMD_XMIT_LINK(sc->xmit_cmds,cur),
   1326 				       0xffff);
   1327 
   1328 		sc->ie_bus_write16(sc, IE_CMD_XMIT_CMD(sc->xmit_cmds, cur),
   1329 				       IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST);
   1330 
   1331 		off = IE_SCB_CMDLST(sc->scb);
   1332 		sc->ie_bus_write16(sc, off, IE_CMD_XMIT_ADDR(sc->xmit_cmds, cur));
   1333 		bus_space_barrier(sc->bt, sc->bh, off, 2,
   1334 				  BUS_SPACE_BARRIER_WRITE);
   1335 
   1336 		if (i82586_start_cmd(sc, IE_CUC_START, 0, 0, 1))
   1337 			printf("%s: iexmit: start xmit command timed out\n",
   1338 				sc->sc_dev.dv_xname);
   1339 	}
   1340 
   1341 	sc->sc_ethercom.ec_if.if_timer = 5;
   1342 }
   1343 
   1344 
   1345 /*
   1346  * Start transmission on an interface.
   1347  */
   1348 void
   1349 i82586_start(ifp)
   1350 	struct ifnet *ifp;
   1351 {
   1352 	struct ie_softc *sc = ifp->if_softc;
   1353 	struct mbuf *m0, *m;
   1354 	int	buffer, head, xbase;
   1355 	u_short	len;
   1356 	int	s;
   1357 
   1358 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1359 		return;
   1360 
   1361 	for (;;) {
   1362 		if (sc->xmit_busy == NTXBUF) {
   1363 			ifp->if_flags |= IFF_OACTIVE;
   1364 			break;
   1365 		}
   1366 
   1367 		head = sc->xchead;
   1368 		xbase = sc->xbds;
   1369 
   1370 		IF_DEQUEUE(&ifp->if_snd, m0);
   1371 		if (m0 == 0)
   1372 			break;
   1373 
   1374 		/* We need to use m->m_pkthdr.len, so require the header */
   1375 		if ((m0->m_flags & M_PKTHDR) == 0)
   1376 			panic("i82586_start: no header mbuf");
   1377 
   1378 #if NBPFILTER > 0
   1379 		/* Tap off here if there is a BPF listener. */
   1380 		if (ifp->if_bpf)
   1381 			bpf_mtap(ifp->if_bpf, m0);
   1382 #endif
   1383 
   1384 #ifdef I82586_DEBUG
   1385 		if (sc->sc_debug & IED_ENQ)
   1386 			printf("%s: fill buffer %d\n", sc->sc_dev.dv_xname,
   1387 				sc->xchead);
   1388 #endif
   1389 
   1390 		if (m0->m_pkthdr.len > IE_TBUF_SIZE)
   1391 			printf("%s: tbuf overflow\n", sc->sc_dev.dv_xname);
   1392 
   1393 		buffer = IE_XBUF_ADDR(sc, head);
   1394 		for (m = m0; m != 0; m = m->m_next) {
   1395 			(sc->memcopyout)(sc, mtod(m,caddr_t), buffer, m->m_len);
   1396 			buffer += m->m_len;
   1397 		}
   1398 
   1399 		len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
   1400 		m_freem(m0);
   1401 
   1402 		/*
   1403 		 * Setup the transmit buffer descriptor here, while we
   1404 		 * know the packet's length.
   1405 		 */
   1406 		sc->ie_bus_write16(sc, IE_XBD_FLAGS(xbase, head),
   1407 				       len | IE_TBD_EOL);
   1408 		sc->ie_bus_write16(sc, IE_XBD_NEXT(xbase, head), 0xffff);
   1409 		sc->ie_bus_write24(sc, IE_XBD_BUF(xbase, head),
   1410 				       IE_XBUF_ADDR(sc, head));
   1411 
   1412 		if (++head == NTXBUF)
   1413 			head = 0;
   1414 		sc->xchead = head;
   1415 
   1416 		s = splnet();
   1417 		/* Start the first packet transmitting. */
   1418 		if (sc->xmit_busy == 0)
   1419 			iexmit(sc);
   1420 
   1421 		sc->xmit_busy++;
   1422 		splx(s);
   1423 	}
   1424 }
   1425 
   1426 /*
   1427  * Probe IE's ram setup   [ Move all this into MD front-end!? ]
   1428  * Use only if SCP and ISCP represent offsets into shared ram space.
   1429  */
   1430 int
   1431 i82586_proberam(sc)
   1432 	struct ie_softc *sc;
   1433 {
   1434 	int result, off;
   1435 
   1436 	/* Put in 16-bit mode */
   1437 	off = IE_SCP_BUS_USE(sc->scp);
   1438 	bus_space_write_1(sc->bt, sc->bh, off, 0);
   1439 	bus_space_barrier(sc->bt, sc->bh, off, 1, BUS_SPACE_BARRIER_WRITE);
   1440 
   1441 	/* Set the ISCP `busy' bit */
   1442 	off = IE_ISCP_BUSY(sc->iscp);
   1443 	bus_space_write_1(sc->bt, sc->bh, off, 1);
   1444 	bus_space_barrier(sc->bt, sc->bh, off, 1, BUS_SPACE_BARRIER_WRITE);
   1445 
   1446 	if (sc->hwreset)
   1447 		(sc->hwreset)(sc, CHIP_PROBE);
   1448 
   1449 	(sc->chan_attn) (sc);
   1450 
   1451 	delay(100);		/* wait a while... */
   1452 
   1453 	/* Read back the ISCP `busy' bit; it should be clear by now */
   1454 	off = IE_ISCP_BUSY(sc->iscp);
   1455 	bus_space_barrier(sc->bt, sc->bh, off, 1, BUS_SPACE_BARRIER_READ);
   1456 	result = bus_space_read_1(sc->bt, sc->bh, off) == 0;
   1457 
   1458 	/* Acknowledge any interrupts we may have caused. */
   1459 	ie_ack(sc, IE_ST_WHENCE);
   1460 
   1461 	return (result);
   1462 }
   1463 
   1464 void
   1465 i82586_reset(sc, hard)
   1466 	struct ie_softc *sc;
   1467 	int hard;
   1468 {
   1469 	int s = splnet();
   1470 
   1471 	if (hard)
   1472 		printf("%s: reset\n", sc->sc_dev.dv_xname);
   1473 
   1474 	/* Clear OACTIVE in case we're called from watchdog (frozen xmit). */
   1475 	sc->sc_ethercom.ec_if.if_timer = 0;
   1476 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1477 
   1478 	/*
   1479 	 * Stop i82586 dead in its tracks.
   1480 	 */
   1481 	if (i82586_start_cmd(sc, IE_RUC_ABORT | IE_CUC_ABORT, 0, 0, 0))
   1482 		printf("%s: abort commands timed out\n", sc->sc_dev.dv_xname);
   1483 
   1484 	/*
   1485 	 * This can really slow down the i82586_reset() on some cards, but it's
   1486 	 * necessary to unwedge other ones (eg, the Sun VME ones) from certain
   1487 	 * lockups.
   1488 	 */
   1489 	if (hard && sc->hwreset)
   1490 		(sc->hwreset)(sc, CARD_RESET);
   1491 
   1492 	delay(100);
   1493 	ie_ack(sc, IE_ST_WHENCE);
   1494 
   1495 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0) {
   1496 		int retries=0;	/* XXX - find out why init sometimes fails */
   1497 		while (retries++ < 2)
   1498 			if (i82586_init(sc) == 1)
   1499 				break;
   1500 	}
   1501 
   1502 	splx(s);
   1503 }
   1504 
   1505 
   1506 static void
   1507 setup_simple_command(sc, cmd, cmdbuf)
   1508 	struct ie_softc *sc;
   1509 	int cmd;
   1510 	int cmdbuf;
   1511 {
   1512 	/* Setup a simple command */
   1513 	sc->ie_bus_write16(sc, IE_CMD_COMMON_STATUS(cmdbuf), 0);
   1514 	sc->ie_bus_write16(sc, IE_CMD_COMMON_CMD(cmdbuf), cmd | IE_CMD_LAST);
   1515 	sc->ie_bus_write16(sc, IE_CMD_COMMON_LINK(cmdbuf), 0xffff);
   1516 
   1517 	/* Assign the command buffer to the SCB command list */
   1518 	sc->ie_bus_write16(sc, IE_SCB_CMDLST(sc->scb), cmdbuf);
   1519 }
   1520 
   1521 /*
   1522  * Run the time-domain reflectometer.
   1523  */
   1524 static void
   1525 ie_run_tdr(sc, cmd)
   1526 	struct ie_softc *sc;
   1527 	int cmd;
   1528 {
   1529 	int result;
   1530 
   1531 	setup_simple_command(sc, IE_CMD_TDR, cmd);
   1532 	(sc->ie_bus_write16)(sc, IE_CMD_TDR_TIME(cmd), 0);
   1533 
   1534 	if (i82586_start_cmd(sc, IE_CUC_START, cmd, IE_STAT_COMPL, 0) ||
   1535 	    (sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmd)) & IE_STAT_OK) == 0)
   1536 		result = 0x10000; /* XXX */
   1537 	else
   1538 		result = sc->ie_bus_read16(sc, IE_CMD_TDR_TIME(cmd));
   1539 
   1540 	/* Squash any pending interrupts */
   1541 	ie_ack(sc, IE_ST_WHENCE);
   1542 
   1543 	if (result & IE_TDR_SUCCESS)
   1544 		return;
   1545 
   1546 	if (result & 0x10000)
   1547 		printf("%s: TDR command failed\n", sc->sc_dev.dv_xname);
   1548 	else if (result & IE_TDR_XCVR)
   1549 		printf("%s: transceiver problem\n", sc->sc_dev.dv_xname);
   1550 	else if (result & IE_TDR_OPEN)
   1551 		printf("%s: TDR detected an open %d clocks away\n",
   1552 			sc->sc_dev.dv_xname, result & IE_TDR_TIME);
   1553 	else if (result & IE_TDR_SHORT)
   1554 		printf("%s: TDR detected a short %d clocks away\n",
   1555 			sc->sc_dev.dv_xname, result & IE_TDR_TIME);
   1556 	else
   1557 		printf("%s: TDR returned unknown status 0x%x\n",
   1558 			sc->sc_dev.dv_xname, result);
   1559 }
   1560 
   1561 
   1562 /*
   1563  * i82586_setup_bufs: set up the buffers
   1564  *
   1565  * We have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
   1566  * this is to be used for the buffers.  The chip indexs its control data
   1567  * structures with 16 bit offsets, and it indexes actual buffers with
   1568  * 24 bit addresses.   So we should allocate control buffers first so that
   1569  * we don't overflow the 16 bit offset field.   The number of transmit
   1570  * buffers is fixed at compile time.
   1571  *
   1572  */
   1573 static void
   1574 i82586_setup_bufs(sc)
   1575 	struct ie_softc *sc;
   1576 {
   1577 	int	ptr = sc->buf_area;	/* memory pool */
   1578 	int     n, r;
   1579 
   1580 	/*
   1581 	 * step 0: zero memory and figure out how many recv buffers and
   1582 	 * frames we can have.
   1583 	 */
   1584 	ptr = (ptr + 3) & ~3;	/* set alignment and stick with it */
   1585 
   1586 
   1587 	/*
   1588 	 *  step 1: lay out data structures in the shared-memory area
   1589 	 */
   1590 
   1591 	/* The no-op commands; used if "nop-chaining" is in effect */
   1592 	sc->nop_cmds = ptr;
   1593 	ptr += NTXBUF * IE_CMD_NOP_SZ;
   1594 
   1595 	/* The transmit commands */
   1596 	sc->xmit_cmds = ptr;
   1597 	ptr += NTXBUF * IE_CMD_XMIT_SZ;
   1598 
   1599 	/* The transmit buffers descriptors */
   1600 	sc->xbds = ptr;
   1601 	ptr += NTXBUF * IE_XBD_SZ;
   1602 
   1603 	/* The transmit buffers */
   1604 	sc->xbufs = ptr;
   1605 	ptr += NTXBUF * IE_TBUF_SIZE;
   1606 
   1607 	ptr = (ptr + 3) & ~3;		/* re-align.. just in case */
   1608 
   1609 	/* Compute free space for RECV stuff */
   1610 	n = sc->buf_area_sz - (ptr - sc->buf_area);
   1611 
   1612 	/* Compute size of one RECV frame */
   1613 	r = IE_RFRAME_SZ + ((IE_RBD_SZ + IE_RBUF_SIZE) * B_PER_F);
   1614 
   1615 	sc->nframes = n / r;
   1616 
   1617 	if (sc->nframes <= 0)
   1618 		panic("ie: bogus buffer calc\n");
   1619 
   1620 	sc->nrxbuf = sc->nframes * B_PER_F;
   1621 
   1622 	/* The receice frame descriptors */
   1623 	sc->rframes = ptr;
   1624 	ptr += sc->nframes * IE_RFRAME_SZ;
   1625 
   1626 	/* The receive buffer descriptors */
   1627 	sc->rbds = ptr;
   1628 	ptr += sc->nrxbuf * IE_RBD_SZ;
   1629 
   1630 	/* The receive buffers */
   1631 	sc->rbufs = ptr;
   1632 	ptr += sc->nrxbuf * IE_RBUF_SIZE;
   1633 
   1634 #ifdef I82586_DEBUG
   1635 	printf("%s: %d frames %d bufs\n", sc->sc_dev.dv_xname, sc->nframes,
   1636 		sc->nrxbuf);
   1637 #endif
   1638 
   1639 	/*
   1640 	 * step 2: link together the recv frames and set EOL on last one
   1641 	 */
   1642 	for (n = 0; n < sc->nframes; n++) {
   1643 		int m = (n == sc->nframes - 1) ? 0 : n + 1;
   1644 
   1645 		/* Clear status */
   1646 		sc->ie_bus_write16(sc, IE_RFRAME_STATUS(sc->rframes,n), 0);
   1647 
   1648 		/* RBD link = NULL */
   1649 		sc->ie_bus_write16(sc, IE_RFRAME_BUFDESC(sc->rframes,n),
   1650 				       0xffff);
   1651 
   1652 		/* Make a circular list */
   1653 		sc->ie_bus_write16(sc, IE_RFRAME_NEXT(sc->rframes,n),
   1654 				       IE_RFRAME_ADDR(sc->rframes,m));
   1655 
   1656 		/* Mark last as EOL */
   1657 		sc->ie_bus_write16(sc, IE_RFRAME_LAST(sc->rframes,n),
   1658 				       ((m==0)? (IE_FD_EOL|IE_FD_SUSP) : 0));
   1659 	}
   1660 
   1661 	/*
   1662 	 * step 3: link the RBDs and set EOL on last one
   1663 	 */
   1664 	for (n = 0; n < sc->nrxbuf; n++) {
   1665 		int m = (n == sc->nrxbuf - 1) ? 0 : n + 1;
   1666 
   1667 		/* Clear status */
   1668 		sc->ie_bus_write16(sc, IE_RBD_STATUS(sc->rbds,n), 0);
   1669 
   1670 		/* Make a circular list */
   1671 		sc->ie_bus_write16(sc, IE_RBD_NEXT(sc->rbds,n),
   1672 				       IE_RBD_ADDR(sc->rbds,m));
   1673 
   1674 		/* Link to data buffers */
   1675 		sc->ie_bus_write24(sc, IE_RBD_BUFADDR(sc->rbds, n),
   1676 				       IE_RBUF_ADDR(sc, n));
   1677 		sc->ie_bus_write16(sc, IE_RBD_BUFLEN(sc->rbds,n),
   1678 				       IE_RBUF_SIZE | ((m==0)?IE_RBD_EOL:0));
   1679 	}
   1680 
   1681 	/*
   1682 	 * step 4: all xmit no-op commands loopback onto themselves
   1683 	 */
   1684 	for (n = 0; n < NTXBUF; n++) {
   1685 		(sc->ie_bus_write16)(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, n), 0);
   1686 
   1687 		(sc->ie_bus_write16)(sc, IE_CMD_NOP_CMD(sc->nop_cmds, n),
   1688 					 IE_CMD_NOP);
   1689 
   1690 		(sc->ie_bus_write16)(sc, IE_CMD_NOP_LINK(sc->nop_cmds, n),
   1691 					 IE_CMD_NOP_ADDR(sc->nop_cmds, n));
   1692 	}
   1693 
   1694 
   1695 	/*
   1696 	 * step 6: set the head and tail pointers on receive to keep track of
   1697 	 * the order in which RFDs and RBDs are used.
   1698 	 */
   1699 
   1700 	/* Pointers to last packet sent and next available transmit buffer. */
   1701 	sc->xchead = sc->xctail = 0;
   1702 
   1703 	/* Clear transmit-busy flag and set number of free transmit buffers. */
   1704 	sc->xmit_busy = 0;
   1705 
   1706 	/*
   1707 	 * Pointers to first and last receive frame.
   1708 	 * The RFD pointed to by rftail is the only one that has EOL set.
   1709 	 */
   1710 	sc->rfhead = 0;
   1711 	sc->rftail = sc->nframes - 1;
   1712 
   1713 	/*
   1714 	 * Pointers to first and last receive descriptor buffer.
   1715 	 * The RBD pointed to by rbtail is the only one that has EOL set.
   1716 	 */
   1717 	sc->rbhead = 0;
   1718 	sc->rbtail = sc->nrxbuf - 1;
   1719 
   1720 /* link in recv frames * and buffer into the scb. */
   1721 #ifdef I82586_DEBUG
   1722 	printf("%s: reserved %d bytes\n",
   1723 		sc->sc_dev.dv_xname, ptr - sc->buf_area);
   1724 #endif
   1725 }
   1726 
   1727 static int
   1728 ie_cfg_setup(sc, cmd, promiscuous, manchester)
   1729 	struct ie_softc *sc;
   1730 	int cmd;
   1731 	int promiscuous, manchester;
   1732 {
   1733 	int cmdresult, status;
   1734 
   1735 	setup_simple_command(sc, IE_CMD_CONFIG, cmd);
   1736 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_CNT(cmd), 0x0c);
   1737 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_FIFO(cmd), 8);
   1738 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_SAVEBAD(cmd), 0x40);
   1739 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_ADDRLEN(cmd), 0x2e);
   1740 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_PRIORITY(cmd), 0);
   1741 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_IFS(cmd), 0x60);
   1742 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_SLOT_LOW(cmd), 0);
   1743 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_SLOT_HIGH(cmd), 0xf2);
   1744 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_PROMISC(cmd),
   1745 					  !!promiscuous | manchester << 2);
   1746 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_CRSCDT(cmd), 0);
   1747 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_MINLEN(cmd), 64);
   1748 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_JUNK(cmd), 0xff);
   1749 	bus_space_barrier(sc->bt, sc->bh, cmd, IE_CMD_CFG_SZ,
   1750 			  BUS_SPACE_BARRIER_WRITE);
   1751 
   1752 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmd, IE_STAT_COMPL, 0);
   1753 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmd));
   1754 	if (cmdresult != 0) {
   1755 		printf("%s: configure command timed out; status %x\n",
   1756 			sc->sc_dev.dv_xname, status);
   1757 		return (0);
   1758 	}
   1759 	if ((status & IE_STAT_OK) == 0) {
   1760 		printf("%s: configure command failed; status %x\n",
   1761 			sc->sc_dev.dv_xname, status);
   1762 		return (0);
   1763 	}
   1764 
   1765 	/* Squash any pending interrupts */
   1766 	ie_ack(sc, IE_ST_WHENCE);
   1767 	return (1);
   1768 }
   1769 
   1770 static int
   1771 ie_ia_setup(sc, cmdbuf)
   1772 	struct ie_softc *sc;
   1773 	int cmdbuf;
   1774 {
   1775 	int cmdresult, status;
   1776 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1777 
   1778 	setup_simple_command(sc, IE_CMD_IASETUP, cmdbuf);
   1779 
   1780 	(sc->memcopyout)(sc, LLADDR(ifp->if_sadl),
   1781 			 IE_CMD_IAS_EADDR(cmdbuf), ETHER_ADDR_LEN);
   1782 
   1783 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmdbuf, IE_STAT_COMPL, 0);
   1784 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmdbuf));
   1785 	if (cmdresult != 0) {
   1786 		printf("%s: individual address command timed out; status %x\n",
   1787 			sc->sc_dev.dv_xname, status);
   1788 		return (0);
   1789 	}
   1790 	if ((status & IE_STAT_OK) == 0) {
   1791 		printf("%s: individual address command failed; status %x\n",
   1792 			sc->sc_dev.dv_xname, status);
   1793 		return (0);
   1794 	}
   1795 
   1796 	/* Squash any pending interrupts */
   1797 	ie_ack(sc, IE_ST_WHENCE);
   1798 	return (1);
   1799 }
   1800 
   1801 /*
   1802  * Run the multicast setup command.
   1803  * Called at splnet().
   1804  */
   1805 static int
   1806 ie_mc_setup(sc, cmdbuf)
   1807 	struct ie_softc *sc;
   1808 	int cmdbuf;
   1809 {
   1810 	int cmdresult, status;
   1811 
   1812 	if (sc->mcast_count == 0)
   1813 		return (1);
   1814 
   1815 	setup_simple_command(sc, IE_CMD_MCAST, cmdbuf);
   1816 
   1817 	(sc->memcopyout)(sc, (caddr_t)sc->mcast_addrs,
   1818 			 IE_CMD_MCAST_MADDR(cmdbuf),
   1819 			 sc->mcast_count * ETHER_ADDR_LEN);
   1820 
   1821 	sc->ie_bus_write16(sc, IE_CMD_MCAST_BYTES(cmdbuf),
   1822 			       sc->mcast_count * ETHER_ADDR_LEN);
   1823 
   1824 	/* Start the command */
   1825 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmdbuf, IE_STAT_COMPL, 0);
   1826 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmdbuf));
   1827 	if (cmdresult != 0) {
   1828 		printf("%s: multicast setup command timed out; status %x\n",
   1829 			sc->sc_dev.dv_xname, status);
   1830 		return (0);
   1831 	}
   1832 	if ((status & IE_STAT_OK) == 0) {
   1833 		printf("%s: multicast setup command failed; status %x\n",
   1834 			sc->sc_dev.dv_xname, status);
   1835 		return (0);
   1836 	}
   1837 
   1838 	/* Squash any pending interrupts */
   1839 	ie_ack(sc, IE_ST_WHENCE);
   1840 	return (1);
   1841 }
   1842 
   1843 /*
   1844  * This routine takes the environment generated by check_ie_present() and adds
   1845  * to it all the other structures we need to operate the adapter.  This
   1846  * includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands, starting
   1847  * the receiver unit, and clearing interrupts.
   1848  *
   1849  * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
   1850  */
   1851 int
   1852 i82586_init(sc)
   1853 	struct ie_softc *sc;
   1854 {
   1855 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1856 	int cmd;
   1857 
   1858 	sc->async_cmd_inprogress = 0;
   1859 
   1860 	cmd = sc->buf_area;
   1861 
   1862 	/*
   1863 	 * Send the configure command first.
   1864 	 */
   1865 	if (ie_cfg_setup(sc, cmd, sc->promisc, 0) == 0)
   1866 		return (0);
   1867 
   1868 	/*
   1869 	 * Send the Individual Address Setup command.
   1870 	 */
   1871 	if (ie_ia_setup(sc, cmd) == 0)
   1872 		return (0);
   1873 
   1874 	/*
   1875 	 * Run the time-domain reflectometer.
   1876 	 */
   1877 	ie_run_tdr(sc, cmd);
   1878 
   1879 	/*
   1880 	 * Set the multi-cast filter, if any
   1881 	 */
   1882 	if (ie_mc_setup(sc, cmd) == 0)
   1883 		return (0);
   1884 
   1885 	/*
   1886 	 * Acknowledge any interrupts we have generated thus far.
   1887 	 */
   1888 	ie_ack(sc, IE_ST_WHENCE);
   1889 
   1890 	/*
   1891 	 * Set up the transmit and recv buffers.
   1892 	 */
   1893 	i82586_setup_bufs(sc);
   1894 
   1895 	if (sc->hwinit)
   1896 		(sc->hwinit)(sc);
   1897 
   1898 	ifp->if_flags |= IFF_RUNNING;
   1899 	ifp->if_flags &= ~IFF_OACTIVE;
   1900 
   1901 	if (NTXBUF < 2)
   1902 		sc->do_xmitnopchain = 0;
   1903 
   1904 	i82586_start_transceiver(sc);
   1905 	return (1);
   1906 }
   1907 
   1908 /*
   1909  * Start the RU and possibly the CU unit
   1910  */
   1911 static void
   1912 i82586_start_transceiver(sc)
   1913 	struct ie_softc *sc;
   1914 {
   1915 
   1916 	/*
   1917 	 * Start RU at current position in frame & RBD lists.
   1918 	 */
   1919 	sc->ie_bus_write16(sc, IE_RFRAME_BUFDESC(sc->rframes,sc->rfhead),
   1920 			       IE_RBD_ADDR(sc->rbds, sc->rbhead));
   1921 
   1922 	sc->ie_bus_write16(sc, IE_SCB_RCVLST(sc->scb),
   1923 			       IE_RFRAME_ADDR(sc->rframes,sc->rfhead));
   1924 
   1925 	if (sc->do_xmitnopchain) {
   1926 		/* Stop transmit command chain */
   1927 		if (i82586_start_cmd(sc, IE_CUC_SUSPEND|IE_RUC_SUSPEND, 0, 0, 0))
   1928 			printf("%s: CU/RU stop command timed out\n",
   1929 				sc->sc_dev.dv_xname);
   1930 
   1931 		/* Start the receiver & transmitter chain */
   1932 		/* sc->scb->ie_command_list =
   1933 			IEADDR(sc->nop_cmds[(sc->xctail+NTXBUF-1) % NTXBUF]);*/
   1934 		sc->ie_bus_write16(sc, IE_SCB_CMDLST(sc->scb),
   1935 				   IE_CMD_NOP_ADDR(
   1936 					sc->nop_cmds,
   1937 					(sc->xctail + NTXBUF - 1) % NTXBUF));
   1938 
   1939 		if (i82586_start_cmd(sc, IE_CUC_START|IE_RUC_START, 0, 0, 0))
   1940 			printf("%s: CU/RU command timed out\n",
   1941 				sc->sc_dev.dv_xname);
   1942 	} else {
   1943 		if (i82586_start_cmd(sc, IE_RUC_START, 0, 0, 0))
   1944 			printf("%s: RU command timed out\n",
   1945 				sc->sc_dev.dv_xname);
   1946 	}
   1947 }
   1948 
   1949 static void
   1950 iestop(sc)
   1951 	struct ie_softc *sc;
   1952 {
   1953 
   1954 	if (i82586_start_cmd(sc, IE_RUC_SUSPEND | IE_CUC_SUSPEND, 0, 0, 0))
   1955 		printf("%s: iestop: disable commands timed out\n",
   1956 			sc->sc_dev.dv_xname);
   1957 }
   1958 
   1959 int
   1960 i82586_ioctl(ifp, cmd, data)
   1961 	register struct ifnet *ifp;
   1962 	u_long cmd;
   1963 	caddr_t data;
   1964 {
   1965 	struct ie_softc *sc = ifp->if_softc;
   1966 	struct ifaddr *ifa = (struct ifaddr *)data;
   1967 	struct ifreq *ifr = (struct ifreq *)data;
   1968 	int s, error = 0;
   1969 
   1970 	s = splnet();
   1971 
   1972 	switch(cmd) {
   1973 
   1974 	case SIOCSIFADDR:
   1975 		ifp->if_flags |= IFF_UP;
   1976 
   1977 		switch(ifa->ifa_addr->sa_family) {
   1978 #ifdef INET
   1979 		case AF_INET:
   1980 			i82586_init(sc);
   1981 			arp_ifinit(ifp, ifa);
   1982 			break;
   1983 #endif
   1984 #ifdef NS
   1985 		/* XXX - This code is probably wrong. */
   1986 		case AF_NS:
   1987 		    {
   1988 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1989 
   1990 			if (ns_nullhost(*ina))
   1991 				ina->x_host =
   1992 				    *(union ns_host *)LLADDR(ifp->if_sadl);
   1993 			else
   1994 				bcopy(ina->x_host.c_host,
   1995 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1996 			/* Set new address. */
   1997 			i82586_init(sc);
   1998 			break;
   1999 		    }
   2000 #endif /* NS */
   2001 		default:
   2002 			i82586_init(sc);
   2003 			break;
   2004 		}
   2005 		break;
   2006 
   2007 	case SIOCSIFFLAGS:
   2008 		sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
   2009 		if ((ifp->if_flags & IFF_UP) == 0 &&
   2010 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   2011 			/*
   2012 			 * If interface is marked down and it is running, then
   2013 			 * stop it.
   2014 			 */
   2015 			iestop(sc);
   2016 			ifp->if_flags &= ~IFF_RUNNING;
   2017 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   2018 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   2019 			/*
   2020 			 * If interface is marked up and it is stopped, then
   2021 			 * start it.
   2022 			 */
   2023 			i82586_init(sc);
   2024 		} else {
   2025 			/*
   2026 			 * Reset the interface to pick up changes in any other
   2027 			 * flags that affect hardware registers.
   2028 			 */
   2029 			iestop(sc);
   2030 			i82586_init(sc);
   2031 		}
   2032 #ifdef I82586_DEBUG
   2033 		if (ifp->if_flags & IFF_DEBUG)
   2034 			sc->sc_debug = IED_ALL;
   2035 		else
   2036 			sc->sc_debug = 0;
   2037 #endif
   2038 		break;
   2039 
   2040 	case SIOCADDMULTI:
   2041 	case SIOCDELMULTI:
   2042 		error = (cmd == SIOCADDMULTI) ?
   2043 		    ether_addmulti(ifr, &sc->sc_ethercom):
   2044 		    ether_delmulti(ifr, &sc->sc_ethercom);
   2045 
   2046 		if (error == ENETRESET) {
   2047 			/*
   2048 			 * Multicast list has changed; set the hardware filter
   2049 			 * accordingly.
   2050 			 */
   2051 			ie_mc_reset(sc);
   2052 			error = 0;
   2053 		}
   2054 		break;
   2055 
   2056         case SIOCGIFMEDIA:
   2057         case SIOCSIFMEDIA:
   2058                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   2059                 break;
   2060 
   2061 	default:
   2062 		error = EINVAL;
   2063 	}
   2064 	splx(s);
   2065 	return (error);
   2066 }
   2067 
   2068 static void
   2069 ie_mc_reset(sc)
   2070 	struct ie_softc *sc;
   2071 {
   2072 	struct ether_multi *enm;
   2073 	struct ether_multistep step;
   2074 	int size;
   2075 
   2076 	/*
   2077 	 * Step through the list of addresses.
   2078 	 */
   2079 again:
   2080 	size = 0;
   2081 	sc->mcast_count = 0;
   2082 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   2083 	while (enm) {
   2084 		size += 6;
   2085 		if (sc->mcast_count >= IE_MAXMCAST ||
   2086 		    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
   2087 			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
   2088 			i82586_ioctl(&sc->sc_ethercom.ec_if,
   2089 				     SIOCSIFFLAGS, (void *)0);
   2090 			return;
   2091 		}
   2092 		ETHER_NEXT_MULTI(step, enm);
   2093 	}
   2094 
   2095 	if (size > sc->mcast_addrs_size) {
   2096 		/* Need to allocate more space */
   2097 		if (sc->mcast_addrs_size)
   2098 			free(sc->mcast_addrs, M_IPMADDR);
   2099 		sc->mcast_addrs = (char *)
   2100 			malloc(size, M_IPMADDR, M_WAITOK);
   2101 		sc->mcast_addrs_size = size;
   2102 	}
   2103 
   2104 	/*
   2105 	 * We've got the space; now copy the addresses
   2106 	 */
   2107 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   2108 	while (enm) {
   2109 		if (sc->mcast_count >= IE_MAXMCAST)
   2110 			goto again; /* Just in case */
   2111 
   2112 		bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
   2113 		sc->mcast_count++;
   2114 		ETHER_NEXT_MULTI(step, enm);
   2115 	}
   2116 	sc->want_mcsetup = 1;
   2117 }
   2118 
   2119 /*
   2120  * Media change callback.
   2121  */
   2122 int
   2123 i82586_mediachange(ifp)
   2124         struct ifnet *ifp;
   2125 {
   2126         struct ie_softc *sc = ifp->if_softc;
   2127 
   2128         if (sc->sc_mediachange)
   2129                 return ((*sc->sc_mediachange)(sc));
   2130         return (EINVAL);
   2131 }
   2132 
   2133 /*
   2134  * Media status callback.
   2135  */
   2136 void
   2137 i82586_mediastatus(ifp, ifmr)
   2138         struct ifnet *ifp;
   2139         struct ifmediareq *ifmr;
   2140 {
   2141         struct ie_softc *sc = ifp->if_softc;
   2142 
   2143         if (sc->sc_mediastatus)
   2144                 (*sc->sc_mediastatus)(sc, ifmr);
   2145 }
   2146 
   2147 #ifdef I82586_DEBUG
   2148 void
   2149 print_rbd(sc, n)
   2150 	struct ie_softc *sc;
   2151 	int n;
   2152 {
   2153 
   2154 	printf("RBD at %08x:\n  status %04x, next %04x, buffer %lx\n"
   2155 		"length/EOL %04x\n", IE_RBD_ADDR(sc->rbds,n),
   2156 		sc->ie_bus_read16(sc, IE_RBD_STATUS(sc->rbds,n)),
   2157 		sc->ie_bus_read16(sc, IE_RBD_NEXT(sc->rbds,n)),
   2158 		(u_long)0,/*bus_space_read_4(sc->bt, sc->bh, IE_RBD_BUFADDR(sc->rbds,n)),-* XXX */
   2159 		sc->ie_bus_read16(sc, IE_RBD_BUFLEN(sc->rbds,n)));
   2160 }
   2161 #endif
   2162