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