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