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