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