Home | History | Annotate | Line # | Download | only in ic
i82586.c revision 1.21
      1 /*	$NetBSD: i82586.c,v 1.21 1999/05/18 23:52:55 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg and Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1997 Paul Kranenburg.
     41  * Copyright (c) 1992, 1993, University of Vermont and State
     42  *  Agricultural College.
     43  * Copyright (c) 1992, 1993, Garrett A. Wollman.
     44  *
     45  * Portions:
     46  * Copyright (c) 1994, 1995, Rafal K. Boni
     47  * Copyright (c) 1990, 1991, William F. Jolitz
     48  * Copyright (c) 1990, The Regents of the University of California
     49  *
     50  * All rights reserved.
     51  *
     52  * Redistribution and use in source and binary forms, with or without
     53  * modification, are permitted provided that the following conditions
     54  * are met:
     55  * 1. Redistributions of source code must retain the above copyright
     56  *    notice, this list of conditions and the following disclaimer.
     57  * 2. Redistributions in binary form must reproduce the above copyright
     58  *    notice, this list of conditions and the following disclaimer in the
     59  *    documentation and/or other materials provided with the distribution.
     60  * 3. All advertising materials mentioning features or use of this software
     61  *    must display the following acknowledgement:
     62  *	This product includes software developed by the University of Vermont
     63  *	and State Agricultural College and Garrett A. Wollman, by William F.
     64  *	Jolitz, and by the University of California, Berkeley, Lawrence
     65  *	Berkeley Laboratory, and its contributors.
     66  * 4. Neither the names of the Universities nor the names of the authors
     67  *    may be used to endorse or promote products derived from this software
     68  *    without specific prior written permission.
     69  *
     70  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     71  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     72  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     73  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
     74  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     75  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     76  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     77  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     78  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     79  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     80  * SUCH DAMAGE.
     81  */
     82 
     83 /*
     84  * Intel 82586 Ethernet chip
     85  * Register, bit, and structure definitions.
     86  *
     87  * Original StarLAN driver written by Garrett Wollman with reference to the
     88  * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
     89  *
     90  * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
     91  *
     92  * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
     93  *
     94  * Majorly cleaned up and 3C507 code merged by Charles Hannum.
     95  *
     96  * Converted to SUN ie driver by Charles D. Cranor,
     97  *		October 1994, January 1995.
     98  * This sun version based on i386 version 1.30.
     99  */
    100 
    101 /*
    102  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
    103  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
    104  * SUN, making life difficult.  Programming this chip is mostly the same,
    105  * but certain details differ from system to system.  This driver is
    106  * written so that different "ie" interfaces can be controled by the same
    107  * driver.
    108  */
    109 
    110 /*
    111 Mode of operation:
    112 
    113    We run the 82586 in a standard Ethernet mode.  We keep NFRAMES
    114    received frame descriptors around for the receiver to use, and
    115    NRXBUF associated receive buffer descriptors, both in a circular
    116    list.  Whenever a frame is received, we rotate both lists as
    117    necessary.  (The 586 treats both lists as a simple queue.)  We also
    118    keep a transmit command around so that packets can be sent off
    119    quickly.
    120 
    121    We configure the adapter in AL-LOC = 1 mode, which means that the
    122    Ethernet/802.3 MAC header is placed at the beginning of the receive
    123    buffer rather than being split off into various fields in the RFD.
    124    This also means that we must include this header in the transmit
    125    buffer as well.
    126 
    127    By convention, all transmit commands, and only transmit commands,
    128    shall have the I (IE_CMD_INTR) bit set in the command.  This way,
    129    when an interrupt arrives at i82586_intr(), it is immediately possible
    130    to tell what precisely caused it.  ANY OTHER command-sending
    131    routines should run at splnet(), and should post an acknowledgement
    132    to every interrupt they generate.
    133 
    134    To save the expense of shipping a command to 82586 every time we
    135    want to send a frame, we use a linked list of commands consisting
    136    of alternate XMIT and NOP commands. The links of these elements
    137    are manipulated (in iexmit()) such that the NOP command loops back
    138    to itself whenever the following XMIT command is not yet ready to
    139    go. Whenever an XMIT is ready, the preceding NOP link is pointed
    140    at it, while its own link field points to the following NOP command.
    141    Thus, a single transmit command sets off an interlocked traversal
    142    of the xmit command chain, with the host processor in control of
    143    the synchronization.
    144 */
    145 
    146 #include "opt_inet.h"
    147 #include "opt_ns.h"
    148 #include "bpfilter.h"
    149 
    150 #include <sys/param.h>
    151 #include <sys/systm.h>
    152 #include <sys/mbuf.h>
    153 #include <sys/buf.h>
    154 #include <sys/protosw.h>
    155 #include <sys/socket.h>
    156 #include <sys/ioctl.h>
    157 #include <sys/errno.h>
    158 #include <sys/syslog.h>
    159 #include <sys/device.h>
    160 
    161 #include <net/if.h>
    162 #include <net/if_dl.h>
    163 #include <net/if_types.h>
    164 #include <net/if_media.h>
    165 #include <net/if_ether.h>
    166 
    167 #if NBPFILTER > 0
    168 #include <net/bpf.h>
    169 #include <net/bpfdesc.h>
    170 #endif
    171 
    172 #ifdef INET
    173 #include <netinet/in.h>
    174 #include <netinet/in_systm.h>
    175 #include <netinet/in_var.h>
    176 #include <netinet/ip.h>
    177 #include <netinet/if_inarp.h>
    178 #endif
    179 
    180 #ifdef NS
    181 #include <netns/ns.h>
    182 #include <netns/ns_if.h>
    183 #endif
    184 
    185 #include <machine/bus.h>
    186 
    187 #include <dev/ic/i82586reg.h>
    188 #include <dev/ic/i82586var.h>
    189 
    190 void	 	i82586_reset 	__P((struct ie_softc *, int));
    191 void 		i82586_watchdog	__P((struct ifnet *));
    192 int 		i82586_init 	__P((struct ie_softc *));
    193 int 		i82586_ioctl 	__P((struct ifnet *, u_long, caddr_t));
    194 void 		i82586_start 	__P((struct ifnet *));
    195 
    196 int 		i82586_rint 	__P((struct ie_softc *, int));
    197 int 		i82586_tint 	__P((struct ie_softc *, int));
    198 
    199 int     	i82586_mediachange 	__P((struct ifnet *));
    200 void    	i82586_mediastatus 	__P((struct ifnet *,
    201 						struct ifmediareq *));
    202 
    203 static int 	ie_readframe		__P((struct ie_softc *, int));
    204 static struct mbuf *ieget 		__P((struct ie_softc *, int *,
    205 					     int, int));
    206 static int	i82586_get_rbd_list	__P((struct ie_softc *,
    207 					     u_int16_t *, u_int16_t *, int *));
    208 static void	i82586_release_rbd_list	__P((struct ie_softc *,
    209 					     u_int16_t, u_int16_t));
    210 static int	i82586_drop_frames	__P((struct ie_softc *));
    211 static int	i82586_chk_rx_ring	__P((struct ie_softc *));
    212 
    213 static __inline__ void 	ie_ack 		__P((struct ie_softc *, u_int));
    214 static __inline__ void 	iexmit 		__P((struct ie_softc *));
    215 static void 		i82586_start_transceiver
    216 					__P((struct ie_softc *));
    217 static void 		iestop 		__P((struct ie_softc *));
    218 
    219 static __inline__ int 	ether_equal 	__P((u_char *, u_char *));
    220 static __inline__ int 	check_eh 	__P((struct ie_softc *,
    221 					     struct ether_header *, int *));
    222 
    223 static void	i82586_count_errors	__P((struct ie_softc *));
    224 static void	i82586_rx_errors	__P((struct ie_softc *, int, int));
    225 static void 	i82586_setup_bufs	__P((struct ie_softc *));
    226 static void	setup_simple_command	__P((struct ie_softc *, int, int));
    227 static int 	ie_cfg_setup		__P((struct ie_softc *, int, int, int));
    228 static int	ie_ia_setup		__P((struct ie_softc *, int));
    229 static void 	ie_run_tdr		__P((struct ie_softc *, int));
    230 static int 	ie_mc_setup 		__P((struct ie_softc *, int));
    231 static void 	ie_mc_reset 		__P((struct ie_softc *));
    232 static int 	i82586_start_cmd 	__P((struct ie_softc *,
    233 					    int, int, int, int));
    234 static int	i82586_cmd_wait		__P((struct ie_softc *));
    235 
    236 #ifdef I82586_DEBUG
    237 void 		print_rbd 	__P((struct ie_softc *, int));
    238 
    239 int spurious_intrs = 0;
    240 #endif
    241 
    242 
    243 /*
    244  * Front-ends call this function to attach to the MI driver.
    245  *
    246  * The front-end has responsibility for managing the ICP and ISCP
    247  * structures. Both of these are opaque to us.  Also, the front-end
    248  * chooses a location for the SCB which is expected to be addressable
    249  * (through `sc->scb') as an offset against the shared-memory bus handle.
    250  *
    251  * The following MD interface function must be setup by the front-end
    252  * before calling here:
    253  *
    254  *	hwreset			- board dependent reset
    255  *	hwinit			- board dependent initialization
    256  *	chan_attn		- channel attention
    257  *	intrhook		- board dependent interrupt processing
    258  *	memcopyin		- shared memory copy: board to KVA
    259  *	memcopyout		- shared memory copy: KVA to board
    260  *	ie_bus_read16		- read a sixteen-bit i82586 pointer
    261  *	ie_bus_write16		- write a sixteen-bit i82586 pointer
    262  *	ie_bus_write24		- write a twenty-four-bit i82586 pointer
    263  *
    264  */
    265 void
    266 i82586_attach(sc, name, etheraddr, media, nmedia, defmedia)
    267 	struct ie_softc *sc;
    268 	char *name;
    269 	u_int8_t *etheraddr;
    270         int *media, nmedia, defmedia;
    271 {
    272 	int i;
    273 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    274 
    275 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    276 	ifp->if_softc = sc;
    277 	ifp->if_start = i82586_start;
    278 	ifp->if_ioctl = i82586_ioctl;
    279 	ifp->if_watchdog = i82586_watchdog;
    280 	ifp->if_flags =
    281 		IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    282 
    283         /* Initialize media goo. */
    284         ifmedia_init(&sc->sc_media, 0, i82586_mediachange, i82586_mediastatus);
    285         if (media != NULL) {
    286                 for (i = 0; i < nmedia; i++)
    287                         ifmedia_add(&sc->sc_media, media[i], 0, NULL);
    288                 ifmedia_set(&sc->sc_media, defmedia);
    289         } else {
    290                 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    291                 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    292         }
    293 
    294 	/* Attach the interface. */
    295 	if_attach(ifp);
    296 	ether_ifattach(ifp, etheraddr);
    297 
    298 	printf(" address %s, type %s\n", ether_sprintf(etheraddr), name);
    299 
    300 #if NBPFILTER > 0
    301 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    302 #endif
    303 }
    304 
    305 
    306 /*
    307  * Device timeout/watchdog routine.
    308  * Entered if the device neglects to generate an interrupt after a
    309  * transmit has been started on it.
    310  */
    311 void
    312 i82586_watchdog(ifp)
    313 	struct ifnet *ifp;
    314 {
    315 	struct ie_softc *sc = ifp->if_softc;
    316 
    317 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    318 	++ifp->if_oerrors;
    319 
    320 	i82586_reset(sc, 1);
    321 }
    322 
    323 
    324 /*
    325  * Compare two Ether/802 addresses for equality, inlined and unrolled for
    326  * speed.
    327  */
    328 static __inline__ int
    329 ether_equal(one, two)
    330 	u_char *one, *two;
    331 {
    332 
    333 	if (one[5] != two[5] || one[4] != two[4] || one[3] != two[3] ||
    334 	    one[2] != two[2] || one[1] != two[1] || one[0] != two[0])
    335 		return (0);
    336 	return (1);
    337 }
    338 
    339 /*
    340  * Check for a valid address.  to_bpf is filled in with one of the following:
    341  *   0 -> BPF doesn't get this packet
    342  *   1 -> BPF does get this packet
    343  *   2 -> BPF does get this packet, but we don't
    344  * Return value is true if the packet is for us, and false otherwise.
    345  *
    346  * This routine is a mess, but it's also critical that it be as fast
    347  * as possible.  It could be made cleaner if we can assume that the
    348  * only client which will fiddle with IFF_PROMISC is BPF.  This is
    349  * probably a good assumption, but we do not make it here.  (Yet.)
    350  */
    351 static __inline__ int
    352 check_eh(sc, eh, to_bpf)
    353 	struct ie_softc *sc;
    354 	struct ether_header *eh;
    355 	int *to_bpf;
    356 {
    357 	struct ifnet *ifp;
    358 	int i;
    359 
    360 	ifp = &sc->sc_ethercom.ec_if;
    361 
    362 	switch(sc->promisc) {
    363 	case IFF_ALLMULTI:
    364 		/*
    365 		 * Receiving all multicasts, but no unicasts except those
    366 		 * destined for us.
    367 		 */
    368 #if NBPFILTER > 0
    369 		/* BPF gets this packet if anybody cares */
    370 		*to_bpf = (ifp->if_bpf != 0);
    371 #endif
    372 		if (eh->ether_dhost[0] & 1)
    373 			return (1);
    374 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    375 			return (1);
    376 		return (0);
    377 
    378 	case IFF_PROMISC:
    379 		/*
    380 		 * Receiving all packets.  These need to be passed on to BPF.
    381 		 */
    382 #if NBPFILTER > 0
    383 		*to_bpf = (ifp->if_bpf != 0);
    384 #endif
    385 		/*
    386 		 * If for us, accept and hand up to BPF.
    387 		 */
    388 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    389 			return (1);
    390 
    391 		/*
    392 		 * If it's the broadcast address, accept and hand up to BPF.
    393 		 */
    394 		if (ether_equal(eh->ether_dhost, etherbroadcastaddr))
    395 			return (1);
    396 
    397 		/*
    398 		 * If it's one of our multicast groups, accept it
    399 		 * and pass it up.
    400 		 */
    401 		for (i = 0; i < sc->mcast_count; i++) {
    402 			if (ether_equal(eh->ether_dhost,
    403 					(u_char *)&sc->mcast_addrs[i])) {
    404 #if NBPFILTER > 0
    405 				if (*to_bpf)
    406 					*to_bpf = 1;
    407 #endif
    408 				return (1);
    409 			}
    410 		}
    411 
    412 #if NBPFILTER > 0
    413 		/* Not for us; BPF wants to see it but we don't. */
    414 		if (*to_bpf)
    415 			*to_bpf = 2;
    416 #endif
    417 
    418 		return (1);
    419 
    420 	case IFF_ALLMULTI | IFF_PROMISC:
    421 		/*
    422 		 * Acting as a multicast router, and BPF running at the same
    423 		 * time.  Whew!  (Hope this is a fast machine...)
    424 		 */
    425 #if NBPFILTER > 0
    426 		*to_bpf = (ifp->if_bpf != 0);
    427 #endif
    428 		/* We want to see multicasts. */
    429 		if (eh->ether_dhost[0] & 1)
    430 			return (1);
    431 
    432 		/* We want to see our own packets */
    433 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    434 			return (1);
    435 
    436 		/* Anything else goes to BPF but nothing else. */
    437 #if NBPFILTER > 0
    438 		if (*to_bpf)
    439 			*to_bpf = 2;
    440 #endif
    441 		return (1);
    442 
    443 	default:
    444 		/*
    445 		 * Only accept unicast packets destined for us, or multicasts
    446 		 * for groups that we belong to.  For now, we assume that the
    447 		 * '586 will only return packets that we asked it for.  This
    448 		 * isn't strictly true (it uses hashing for the multicast
    449 		 * filter), but it will do in this case, and we want to get
    450 		 * out of here as quickly as possible.
    451 		 */
    452 #if NBPFILTER > 0
    453 		*to_bpf = (ifp->if_bpf != 0);
    454 #endif
    455 		return (1);
    456 	}
    457 	return (0);
    458 }
    459 
    460 static int
    461 i82586_cmd_wait(sc)
    462 	struct ie_softc *sc;
    463 {
    464 	/* spin on i82586 command acknowledge; wait at most 0.9 (!) seconds */
    465 	int i, off;
    466 
    467 	for (i = 0; i < 900000; i++) {
    468 		/* Read the command word */
    469 		off = IE_SCB_CMD(sc->scb);
    470 		bus_space_barrier(sc->bt, sc->bh, off, 2,
    471 				  BUS_SPACE_BARRIER_READ);
    472 		if ((sc->ie_bus_read16)(sc, off) == 0)
    473 			return (0);
    474 		delay(1);
    475 	}
    476 
    477 	printf("i82586_cmd_wait: timo(%ssync): scb status: 0x%x\n",
    478 		sc->async_cmd_inprogress?"a":"", sc->ie_bus_read16(sc, off));
    479 	return (1);	/* Timeout */
    480 }
    481 
    482 /*
    483  * Send a command to the controller and wait for it to either complete
    484  * or be accepted, depending on the command.  If the command pointer
    485  * is null, then pretend that the command is not an action command.
    486  * If the command pointer is not null, and the command is an action
    487  * command, wait for one of the MASK bits to turn on in the command's
    488  * status field.
    489  * If ASYNC is set, we just call the chip's attention and return.
    490  * We may have to wait for the command's acceptance later though.
    491  */
    492 static int
    493 i82586_start_cmd(sc, cmd, iecmdbuf, mask, async)
    494 	struct ie_softc *sc;
    495 	int cmd;
    496 	int iecmdbuf;
    497 	int mask;
    498 	int async;
    499 {
    500 	int i;
    501 	int off;
    502 
    503 	if (sc->async_cmd_inprogress != 0) {
    504 		/*
    505 		 * If previous command was issued asynchronously, wait
    506 		 * for it now.
    507 		 */
    508 		if (i82586_cmd_wait(sc) != 0)
    509 			return (1);
    510 		sc->async_cmd_inprogress = 0;
    511 	}
    512 
    513 	off = IE_SCB_CMD(sc->scb);
    514 	(sc->ie_bus_write16)(sc, off, cmd);
    515 	bus_space_barrier(sc->bt, sc->bh, off, 2, BUS_SPACE_BARRIER_WRITE);
    516 	(sc->chan_attn)(sc);
    517 
    518 	if (async != 0) {
    519 		sc->async_cmd_inprogress = 1;
    520 		return (0);
    521 	}
    522 
    523 	if (IE_ACTION_COMMAND(cmd) && iecmdbuf) {
    524 		int status;
    525 		/*
    526 		 * Now spin-lock waiting for status.  This is not a very nice
    527 		 * thing to do, and can kill performance pretty well...
    528 		 * According to the packet driver, the minimum timeout
    529 		 * should be .369 seconds.
    530 		 */
    531 		for (i = 0; i < 369000; i++) {
    532 			/* Read the command status */
    533 			off = IE_CMD_COMMON_STATUS(iecmdbuf);
    534 			bus_space_barrier(sc->bt, sc->bh, off, 2,
    535 					  BUS_SPACE_BARRIER_READ);
    536 			status = (sc->ie_bus_read16)(sc, off);
    537 			if (status & mask)
    538 				return (0);
    539 			delay(1);
    540 		}
    541 
    542 	} else {
    543 		/*
    544 		 * Otherwise, just wait for the command to be accepted.
    545 		 */
    546 		return (i82586_cmd_wait(sc));
    547 	}
    548 
    549 	/* Timeout */
    550 	return (1);
    551 }
    552 
    553 /*
    554  * Interrupt Acknowledge.
    555  */
    556 static __inline__ void
    557 ie_ack(sc, mask)
    558 	struct ie_softc *sc;
    559 	u_int mask;	/* in native byte-order */
    560 {
    561 	u_int status;
    562 
    563 	bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
    564 	status = (sc->ie_bus_read16)(sc, IE_SCB_STATUS(sc->scb));
    565 	i82586_start_cmd(sc, status & mask, 0, 0, 0);
    566 }
    567 
    568 /*
    569  * Transfer accumulated chip error counters to IF.
    570  */
    571 static __inline void
    572 i82586_count_errors(sc)
    573 	struct ie_softc *sc;
    574 {
    575 	int scb = sc->scb;
    576 
    577 	sc->sc_ethercom.ec_if.if_ierrors +=
    578 	    sc->ie_bus_read16(sc, IE_SCB_ERRCRC(scb)) +
    579 	    sc->ie_bus_read16(sc, IE_SCB_ERRALN(scb)) +
    580 	    sc->ie_bus_read16(sc, IE_SCB_ERRRES(scb)) +
    581 	    sc->ie_bus_read16(sc, IE_SCB_ERROVR(scb));
    582 
    583 	/* Clear error counters */
    584 	sc->ie_bus_write16(sc, IE_SCB_ERRCRC(scb), 0);
    585 	sc->ie_bus_write16(sc, IE_SCB_ERRALN(scb), 0);
    586 	sc->ie_bus_write16(sc, IE_SCB_ERRRES(scb), 0);
    587 	sc->ie_bus_write16(sc, IE_SCB_ERROVR(scb), 0);
    588 }
    589 
    590 static void
    591 i82586_rx_errors(sc, fn, status)
    592 	struct ie_softc *sc;
    593 	int fn;
    594 	int status;
    595 {
    596 	char bits[128];
    597 
    598 	log(LOG_ERR, "%s: rx error (frame# %d): %s\n", sc->sc_dev.dv_xname, fn,
    599 	    bitmask_snprintf(status, IE_FD_STATUSBITS, bits, sizeof(bits)));
    600 }
    601 
    602 /*
    603  * i82586 interrupt entry point.
    604  */
    605 int
    606 i82586_intr(v)
    607 	void *v;
    608 {
    609 	struct ie_softc *sc = v;
    610 	u_int status;
    611 	int off;
    612 
    613         /*
    614          * Implementation dependent interrupt handling.
    615          */
    616 	if (sc->intrhook)
    617 		(sc->intrhook)(sc, INTR_ENTER);
    618 
    619 	off = IE_SCB_STATUS(sc->scb);
    620 	bus_space_barrier(sc->bt, sc->bh, off, 2, BUS_SPACE_BARRIER_READ);
    621 	status = sc->ie_bus_read16(sc, off) & IE_ST_WHENCE;
    622 
    623 	if ((status & IE_ST_WHENCE) == 0) {
    624 #ifdef I82586_DEBUG
    625 		if ((spurious_intrs++ % 25) == 0)
    626 		    printf("%s: i82586_intr: %d spurious interrupts\n",
    627 			   sc->sc_dev.dv_xname, spurious_intrs);
    628 #endif
    629 		if (sc->intrhook)
    630 			(sc->intrhook)(sc, INTR_EXIT);
    631 
    632 		return (0);
    633 	}
    634 
    635 loop:
    636 	/* Ack interrupts FIRST in case we receive more during the ISR. */
    637 #if 0
    638 	ie_ack(sc, status & IE_ST_WHENCE);
    639 #endif
    640 	i82586_start_cmd(sc, status & IE_ST_WHENCE, 0, 0, 1);
    641 
    642 	if (status & (IE_ST_FR | IE_ST_RNR))
    643 		if (i82586_rint(sc, status) != 0)
    644 			goto reset;
    645 
    646 	if (status & IE_ST_CX)
    647 		if (i82586_tint(sc, status) != 0)
    648 			goto reset;
    649 
    650 #ifdef I82586_DEBUG
    651 	if ((status & IE_ST_CNA) && (sc->sc_debug & IED_CNA))
    652 		printf("%s: cna; status=0x%x\n", sc->sc_dev.dv_xname, status);
    653 #endif
    654 	if (sc->intrhook)
    655 		(sc->intrhook)(sc, INTR_LOOP);
    656 
    657 	/*
    658 	 * Interrupt ACK was posted asynchronously; wait for
    659 	 * completion here before reading SCB status again.
    660 	 */
    661 	i82586_cmd_wait(sc);
    662 
    663 	bus_space_barrier(sc->bt, sc->bh, off, 2, BUS_SPACE_BARRIER_READ);
    664 	status = sc->ie_bus_read16(sc, off);
    665 	if ((status & IE_ST_WHENCE) != 0)
    666 		goto loop;
    667 
    668 out:
    669 	if (sc->intrhook)
    670 		(sc->intrhook)(sc, INTR_EXIT);
    671 	return (1);
    672 
    673 reset:
    674 	i82586_cmd_wait(sc);
    675 	i82586_reset(sc, 1);
    676 	goto out;
    677 
    678 }
    679 
    680 /*
    681  * Process a received-frame interrupt.
    682  */
    683 int
    684 i82586_rint(sc, scbstatus)
    685 	struct	ie_softc *sc;
    686 	int	scbstatus;
    687 {
    688 static	int timesthru = 1024;
    689 	int i, status, off;
    690 
    691 #ifdef I82586_DEBUG
    692 	if (sc->sc_debug & IED_RINT)
    693 		printf("%s: rint: status 0x%x\n",
    694 			sc->sc_dev.dv_xname, scbstatus);
    695 #endif
    696 
    697 	for (;;) {
    698 		int drop = 0;
    699 
    700 		i = sc->rfhead;
    701 		off = IE_RFRAME_STATUS(sc->rframes, i);
    702 		bus_space_barrier(sc->bt, sc->bh, off, 2,
    703 				  BUS_SPACE_BARRIER_READ);
    704 		status = sc->ie_bus_read16(sc, off);
    705 
    706 #ifdef I82586_DEBUG
    707 		if (sc->sc_debug & IED_RINT)
    708 			printf("%s: rint: frame(%d) status 0x%x\n",
    709 				sc->sc_dev.dv_xname, i, status);
    710 #endif
    711 		if ((status & IE_FD_COMPLETE) == 0) {
    712 			if ((status & IE_FD_OK) != 0) {
    713 				printf("%s: rint: weird: ",
    714 					sc->sc_dev.dv_xname);
    715 				i82586_rx_errors(sc, i, status);
    716 				break;
    717 			}
    718 			if (--timesthru == 0) {
    719 				/* Account the accumulated errors */
    720 				i82586_count_errors(sc);
    721 				timesthru = 1024;
    722 			}
    723 			break;
    724 		} else if ((status & IE_FD_OK) == 0) {
    725 			/*
    726 			 * If the chip is configured to automatically
    727 			 * discard bad frames, the only reason we can
    728 			 * get here is an "out-of-resource" condition.
    729 			 */
    730 			i82586_rx_errors(sc, i, status);
    731 			drop = 1;
    732 		}
    733 
    734 #ifdef I82586_DEBUG
    735 		if ((status & IE_FD_BUSY) != 0)
    736 			printf("%s: rint: frame(%d) busy; status=0x%x\n",
    737 				sc->sc_dev.dv_xname, i, status);
    738 #endif
    739 
    740 
    741 		/*
    742 		 * Advance the RFD list, since we're done with
    743 		 * this descriptor.
    744 		 */
    745 
    746 		/* Clear frame status */
    747 		sc->ie_bus_write16(sc, off, 0);
    748 
    749 		/* Put fence at this frame (the head) */
    750 		off = IE_RFRAME_LAST(sc->rframes, i);
    751 		sc->ie_bus_write16(sc, off, IE_FD_EOL|IE_FD_SUSP);
    752 
    753 		/* and clear RBD field */
    754 		off = IE_RFRAME_BUFDESC(sc->rframes, i);
    755 		sc->ie_bus_write16(sc, off, 0xffff);
    756 
    757 		/* Remove fence from current tail */
    758 		off = IE_RFRAME_LAST(sc->rframes, sc->rftail);
    759 		sc->ie_bus_write16(sc, off, 0);
    760 
    761 		if (++sc->rftail == sc->nframes)
    762 			sc->rftail = 0;
    763 		if (++sc->rfhead == sc->nframes)
    764 			sc->rfhead = 0;
    765 
    766 		/* Pull the frame off the board */
    767 		if (drop) {
    768 			i82586_drop_frames(sc);
    769 			if ((status & IE_FD_RNR) != 0)
    770 				sc->rnr_expect = 1;
    771 			sc->sc_ethercom.ec_if.if_ierrors++;
    772 		} else if (ie_readframe(sc, i) != 0)
    773 			return (1);
    774 	}
    775 
    776 	if ((scbstatus & IE_ST_RNR) != 0) {
    777 
    778 		/*
    779 		 * Receiver went "Not Ready". We try to figure out
    780 		 * whether this was an expected event based on past
    781 		 * frame status values.
    782 		 */
    783 
    784 		if ((scbstatus & IE_RUS_SUSPEND) != 0) {
    785 			/*
    786 			 * We use the "suspend on last frame" flag.
    787 			 * Send a RU RESUME command in response, since
    788 			 * we should have dealt with all completed frames
    789 			 * by now.
    790 			 */
    791 			printf("RINT: SUSPENDED; scbstatus=0x%x\n",
    792 				scbstatus);
    793 			if (i82586_start_cmd(sc, IE_RUC_RESUME, 0, 0, 0) == 0)
    794 				return (0);
    795 			printf("%s: RU RESUME command timed out\n",
    796 				sc->sc_dev.dv_xname);
    797 			return (1);	/* Ask for a reset */
    798 		}
    799 
    800 		if (sc->rnr_expect != 0) {
    801 			/*
    802 			 * The RNR condition was announced in the previously
    803 			 * completed frame.  Assume the receive ring is Ok,
    804 			 * so restart the receiver without further delay.
    805 			 */
    806 			i82586_start_transceiver(sc);
    807 			sc->rnr_expect = 0;
    808 			return (0);
    809 
    810 		} else if ((scbstatus & IE_RUS_NOSPACE) != 0) {
    811 			/*
    812 			 * We saw no previous IF_FD_RNR flag.
    813 			 * We check our ring invariants and, if ok,
    814 			 * just restart the receiver at the current
    815 			 * point in the ring.
    816 			 */
    817 			if (i82586_chk_rx_ring(sc) != 0)
    818 				return (1);
    819 
    820 			i82586_start_transceiver(sc);
    821 			sc->sc_ethercom.ec_if.if_ierrors++;
    822 			return (0);
    823 		} else
    824 			printf("%s: receiver not ready; scbstatus=0x%x\n",
    825 				sc->sc_dev.dv_xname, scbstatus);
    826 
    827 		sc->sc_ethercom.ec_if.if_ierrors++;
    828 		return (1);	/* Ask for a reset */
    829 	}
    830 
    831 	return (0);
    832 }
    833 
    834 /*
    835  * Process a command-complete interrupt.  These are only generated by the
    836  * transmission of frames.  This routine is deceptively simple, since most
    837  * of the real work is done by i82586_start().
    838  */
    839 int
    840 i82586_tint(sc, scbstatus)
    841 	struct ie_softc *sc;
    842 	int	scbstatus;
    843 {
    844 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    845 	int status;
    846 
    847 	ifp->if_timer = 0;
    848 	ifp->if_flags &= ~IFF_OACTIVE;
    849 
    850 #ifdef I82586_DEBUG
    851 	if (sc->xmit_busy <= 0) {
    852 	    printf("i82586_tint: WEIRD: xmit_busy=%d, xctail=%d, xchead=%d\n",
    853 		   sc->xmit_busy, sc->xctail, sc->xchead);
    854 		return (0);
    855 	}
    856 #endif
    857 
    858 	status = sc->ie_bus_read16(sc, IE_CMD_XMIT_STATUS(sc->xmit_cmds,
    859 							  sc->xctail));
    860 
    861 #ifdef I82586_DEBUG
    862 	if (sc->sc_debug & IED_TINT)
    863 		printf("%s: tint: SCB status 0x%x; xmit status 0x%x\n",
    864 			sc->sc_dev.dv_xname, scbstatus, status);
    865 #endif
    866 
    867 	if ((status & IE_STAT_COMPL) == 0 || (status & IE_STAT_BUSY)) {
    868 	    printf("i82586_tint: command still busy; status=0x%x; tail=%d\n",
    869 		   status, sc->xctail);
    870 	    printf("iestatus = 0x%x\n", scbstatus);
    871 	}
    872 
    873 	if (status & IE_STAT_OK) {
    874 		ifp->if_opackets++;
    875 		ifp->if_collisions += (status & IE_XS_MAXCOLL);
    876 	} else {
    877 		ifp->if_oerrors++;
    878 		/*
    879 		 * Check SQE and DEFERRED?
    880 		 * What if more than one bit is set?
    881 		 */
    882 		if (status & IE_STAT_ABORT)
    883 			printf("%s: send aborted\n", sc->sc_dev.dv_xname);
    884 		else if (status & IE_XS_NOCARRIER)
    885 			printf("%s: no carrier\n", sc->sc_dev.dv_xname);
    886 		else if (status & IE_XS_LOSTCTS)
    887 			printf("%s: lost CTS\n", sc->sc_dev.dv_xname);
    888 		else if (status & IE_XS_UNDERRUN)
    889 			printf("%s: DMA underrun\n", sc->sc_dev.dv_xname);
    890 		else if (status & IE_XS_EXCMAX) {
    891 			printf("%s: too many collisions\n",
    892 				sc->sc_dev.dv_xname);
    893 			sc->sc_ethercom.ec_if.if_collisions += 16;
    894 		}
    895 	}
    896 
    897 	/*
    898 	 * If multicast addresses were added or deleted while transmitting,
    899 	 * ie_mc_reset() set the want_mcsetup flag indicating that we
    900 	 * should do it.
    901 	 */
    902 	if (sc->want_mcsetup) {
    903 		ie_mc_setup(sc, IE_XBUF_ADDR(sc, sc->xctail));
    904 		sc->want_mcsetup = 0;
    905 	}
    906 
    907 	/* Done with the buffer. */
    908 	sc->xmit_busy--;
    909 	sc->xctail = (sc->xctail + 1) % NTXBUF;
    910 
    911 	/* Start the next packet, if any, transmitting. */
    912 	if (sc->xmit_busy > 0)
    913 		iexmit(sc);
    914 
    915 	i82586_start(ifp);
    916 	return (0);
    917 }
    918 
    919 /*
    920  * Get a range of receive buffer descriptors that represent one packet.
    921  */
    922 static int
    923 i82586_get_rbd_list(sc, start, end, pktlen)
    924 	struct ie_softc *sc;
    925 	u_int16_t	*start;
    926 	u_int16_t	*end;
    927 	int		*pktlen;
    928 {
    929 	int	off, rbbase = sc->rbds;
    930 	int	rbindex, count = 0;
    931 	int	plen = 0;
    932 	int	rbdstatus;
    933 
    934 	*start = rbindex = sc->rbhead;
    935 
    936 	do {
    937 		off = IE_RBD_STATUS(rbbase, rbindex);
    938 		bus_space_barrier(sc->bt, sc->bh, off, 2,
    939 				  BUS_SPACE_BARRIER_READ);
    940 		rbdstatus = sc->ie_bus_read16(sc, off);
    941 		if ((rbdstatus & IE_RBD_USED) == 0) {
    942 			/*
    943 			 * This means we are somehow out of sync.  So, we
    944 			 * reset the adapter.
    945 			 */
    946 #ifdef I82586_DEBUG
    947 			print_rbd(sc, rbindex);
    948 #endif
    949 			log(LOG_ERR,
    950 			    "%s: receive descriptors out of sync at %d\n",
    951 			    sc->sc_dev.dv_xname, rbindex);
    952 			return (0);
    953 		}
    954 		plen += (rbdstatus & IE_RBD_CNTMASK);
    955 
    956 		if (++rbindex == sc->nrxbuf)
    957 			rbindex = 0;
    958 
    959 		++count;
    960 	} while ((rbdstatus & IE_RBD_LAST) == 0);
    961 	*end = rbindex;
    962 	*pktlen = plen;
    963 	return (count);
    964 }
    965 
    966 
    967 /*
    968  * Release a range of receive buffer descriptors after we've copied the packet.
    969  */
    970 static void
    971 i82586_release_rbd_list(sc, start, end)
    972 	struct ie_softc *sc;
    973 	u_int16_t	start;
    974 	u_int16_t	end;
    975 {
    976 	int	off, rbbase = sc->rbds;
    977 	int	rbindex = start;
    978 
    979 	do {
    980 		/* Clear buffer status */
    981 		off = IE_RBD_STATUS(rbbase, rbindex);
    982 		sc->ie_bus_write16(sc, off, 0);
    983 		if (++rbindex == sc->nrxbuf)
    984 			rbindex = 0;
    985 	} while (rbindex != end);
    986 
    987 	/* Mark EOL at new tail */
    988 	rbindex = ((rbindex == 0) ? sc->nrxbuf : rbindex) - 1;
    989 	off = IE_RBD_BUFLEN(rbbase, rbindex);
    990 	sc->ie_bus_write16(sc, off, IE_RBUF_SIZE|IE_RBD_EOL);
    991 
    992 	/* Remove EOL from current tail */
    993 	off = IE_RBD_BUFLEN(rbbase, sc->rbtail);
    994 	sc->ie_bus_write16(sc, off, IE_RBUF_SIZE);
    995 
    996 	/* New head & tail pointer */
    997 /* hmm, why have both? head is always (tail + 1) % NRXBUF */
    998 	sc->rbhead = end;
    999 	sc->rbtail = rbindex;
   1000 }
   1001 
   1002 /*
   1003  * Drop the packet at the head of the RX buffer ring.
   1004  * Called if the frame descriptor reports an error on this packet.
   1005  * Returns 1 if the buffer descriptor ring appears to be corrupt;
   1006  * and 0 otherwise.
   1007  */
   1008 static int
   1009 i82586_drop_frames(sc)
   1010 	struct ie_softc *sc;
   1011 {
   1012 	u_int16_t bstart, bend;
   1013 	int pktlen;
   1014 
   1015 	if (i82586_get_rbd_list(sc, &bstart, &bend, &pktlen) == 0)
   1016 		return (1);
   1017 	i82586_release_rbd_list(sc, bstart, bend);
   1018 	return (0);
   1019 }
   1020 
   1021 /*
   1022  * Check the RX frame & buffer descriptor lists for our invariants,
   1023  * i.e.: EOL bit set iff. it is pointed at by the r*tail pointer.
   1024  *
   1025  * Called when the receive unit has stopped unexpectedly.
   1026  * Returns 1 if an inconsistency is detected; 0 otherwise.
   1027  *
   1028  * The Receive Unit is expected to be NOT RUNNING.
   1029  */
   1030 static int
   1031 i82586_chk_rx_ring(sc)
   1032 	struct ie_softc *sc;
   1033 {
   1034 	int n, off, val;
   1035 
   1036 	for (n = 0; n < sc->nrxbuf; n++) {
   1037 		off = IE_RBD_BUFLEN(sc->rbds, n);
   1038 		val = sc->ie_bus_read16(sc, off);
   1039 		if ((n == sc->rbtail) ^ ((val & IE_RBD_EOL) != 0)) {
   1040 			/* `rbtail' and EOL flag out of sync */
   1041 			log(LOG_ERR,
   1042 			    "%s: rx buffer descriptors out of sync at %d\n",
   1043 			    sc->sc_dev.dv_xname, n);
   1044 			return (1);
   1045 		}
   1046 
   1047 		/* Take the opportunity to clear the status fields here ? */
   1048 	}
   1049 
   1050 	for (n = 0; n < sc->nframes; n++) {
   1051 		off = IE_RFRAME_LAST(sc->rframes, n);
   1052 		val = sc->ie_bus_read16(sc, off);
   1053 		if ((n == sc->rftail) ^ ((val & (IE_FD_EOL|IE_FD_SUSP)) != 0)) {
   1054 			/* `rftail' and EOL flag out of sync */
   1055 			log(LOG_ERR,
   1056 			    "%s: rx frame list out of sync at %d\n",
   1057 			    sc->sc_dev.dv_xname, n);
   1058 			return (1);
   1059 		}
   1060 	}
   1061 
   1062 	return (0);
   1063 }
   1064 
   1065 /*
   1066  * Read data off the interface, and turn it into an mbuf chain.
   1067  *
   1068  * This code is DRAMATICALLY different from the previous version; this
   1069  * version tries to allocate the entire mbuf chain up front, given the
   1070  * length of the data available.  This enables us to allocate mbuf
   1071  * clusters in many situations where before we would have had a long
   1072  * chain of partially-full mbufs.  This should help to speed up the
   1073  * operation considerably.  (Provided that it works, of course.)
   1074  */
   1075 static __inline struct mbuf *
   1076 ieget(sc, to_bpf, head, totlen)
   1077 	struct ie_softc *sc;
   1078 	int *to_bpf;
   1079 	int head;
   1080 	int totlen;
   1081 {
   1082 	struct mbuf *m, *m0, *newm;
   1083 	int len, resid;
   1084 	int thisrboff, thismboff;
   1085 	struct ether_header eh;
   1086 
   1087 	/*
   1088 	 * Snarf the Ethernet header.
   1089 	 */
   1090 	(sc->memcopyin)(sc, &eh, IE_RBUF_ADDR(sc, head),
   1091 	    sizeof(struct ether_header));
   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, &eh, 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;
   1107 
   1108 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
   1109 	if (m0 == 0)
   1110 		return (0);
   1111 	m0->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
   1112 	m0->m_pkthdr.len = totlen;
   1113 	len = MHLEN;
   1114 	m = m0;
   1115 
   1116 	/*
   1117 	 * This loop goes through and allocates mbufs for all the data we will
   1118 	 * be copying in.  It does not actually do the copying yet.
   1119 	 */
   1120 	while (totlen > 0) {
   1121 		if (totlen >= MINCLSIZE) {
   1122 			MCLGET(m, M_DONTWAIT);
   1123 			if ((m->m_flags & M_EXT) == 0)
   1124 				goto bad;
   1125 			len = MCLBYTES;
   1126 		}
   1127 
   1128 		m->m_len = len = min(totlen, len);
   1129 
   1130 		totlen -= len;
   1131 		if (totlen > 0) {
   1132 			MGET(newm, M_DONTWAIT, MT_DATA);
   1133 			if (newm == 0)
   1134 				goto bad;
   1135 			len = MLEN;
   1136 			m = m->m_next = newm;
   1137 		}
   1138 	}
   1139 
   1140 	m = m0;
   1141 	thismboff = 0;
   1142 
   1143 	/*
   1144 	 * Copy the Ethernet header into the mbuf chain.
   1145 	 */
   1146 	memcpy(mtod(m, caddr_t), &eh, sizeof(struct ether_header));
   1147 	thismboff = sizeof(struct ether_header);
   1148 	thisrboff = sizeof(struct ether_header);
   1149 	resid -= sizeof(struct ether_header);
   1150 
   1151 	/*
   1152 	 * Now we take the mbuf chain (hopefully only one mbuf most of the
   1153 	 * time) and stuff the data into it.  There are no possible failures
   1154 	 * at or after this point.
   1155 	 */
   1156 	while (resid > 0) {
   1157 		int thisrblen = IE_RBUF_SIZE - thisrboff,
   1158 		    thismblen = m->m_len - thismboff;
   1159 		len = min(thisrblen, thismblen);
   1160 
   1161 		(sc->memcopyin)(sc, mtod(m, caddr_t) + thismboff,
   1162 				IE_RBUF_ADDR(sc,head) + thisrboff,
   1163 				(u_int)len);
   1164 		resid -= len;
   1165 
   1166 		if (len == thismblen) {
   1167 			m = m->m_next;
   1168 			thismboff = 0;
   1169 		} else
   1170 			thismboff += len;
   1171 
   1172 		if (len == thisrblen) {
   1173 			if (++head == sc->nrxbuf)
   1174 				head = 0;
   1175 			thisrboff = 0;
   1176 		} else
   1177 			thisrboff += len;
   1178 	}
   1179 
   1180 	/*
   1181 	 * Unless something changed strangely while we were doing the copy,
   1182 	 * we have now copied everything in from the shared memory.
   1183 	 * This means that we are done.
   1184 	 */
   1185 	return (m0);
   1186 
   1187 bad:
   1188 	m_freem(m0);
   1189 	return (0);
   1190 }
   1191 
   1192 /*
   1193  * Read frame NUM from unit UNIT (pre-cached as IE).
   1194  *
   1195  * This routine reads the RFD at NUM, and copies in the buffers from the list
   1196  * of RBD, then rotates the RBD list so that the receiver doesn't start
   1197  * complaining.  Trailers are DROPPED---there's no point in wasting time
   1198  * on confusing code to deal with them.  Hopefully, this machine will
   1199  * never ARP for trailers anyway.
   1200  */
   1201 static int
   1202 ie_readframe(sc, num)
   1203 	struct ie_softc *sc;
   1204 	int num;		/* frame number to read */
   1205 {
   1206 	struct mbuf *m;
   1207 	u_int16_t bstart, bend;
   1208 	int pktlen;
   1209 #if NBPFILTER > 0
   1210 	int bpf_gets_it = 0;
   1211 #endif
   1212 
   1213 	if (i82586_get_rbd_list(sc, &bstart, &bend, &pktlen) == 0) {
   1214 		sc->sc_ethercom.ec_if.if_ierrors++;
   1215 		return (1);
   1216 	}
   1217 
   1218 #if NBPFILTER > 0
   1219 	m = ieget(sc, &bpf_gets_it, bstart, pktlen);
   1220 #else
   1221 	m = ieget(sc, 0, bstart, pktlen);
   1222 #endif
   1223 	i82586_release_rbd_list(sc, bstart, bend);
   1224 
   1225 	if (m == 0) {
   1226 		sc->sc_ethercom.ec_if.if_ierrors++;
   1227 		return (0);
   1228 	}
   1229 
   1230 #ifdef I82586_DEBUG
   1231 	if (sc->sc_debug & IED_READFRAME) {
   1232 		struct ether_header *eh = mtod(m, struct ether_header *);
   1233 
   1234 		printf("%s: frame from ether %s type 0x%x len %d\n",
   1235 			sc->sc_dev.dv_xname,
   1236 			ether_sprintf(eh->ether_shost),
   1237 			(u_int)eh->ether_type,
   1238 			pktlen);
   1239 	}
   1240 #endif
   1241 
   1242 #if NBPFILTER > 0
   1243 	/*
   1244 	 * Check for a BPF filter; if so, hand it up.
   1245 	 * Note that we have to stick an extra mbuf up front, because bpf_mtap
   1246 	 * expects to have the ether header at the front.
   1247 	 * It doesn't matter that this results in an ill-formatted mbuf chain,
   1248 	 * since BPF just looks at the data.  (It doesn't try to free the mbuf,
   1249 	 * tho' it will make a copy for tcpdump.)
   1250 	 */
   1251 	if (bpf_gets_it) {
   1252 		/* Pass it up. */
   1253 		bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, m);
   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 	(*sc->sc_ethercom.ec_if.if_input)(&sc->sc_ethercom.ec_if, 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 if ((ifp->if_flags & IFF_UP) != 0) {
   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