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