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