Home | History | Annotate | Line # | Download | only in ic
i82586.c revision 1.13
      1 /*	$NetBSD: i82586.c,v 1.13 1998/06/04 20:35:44 pk 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.
      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) 1993, 1994, 1995 Charles Hannum.
     42  * Copyright (c) 1992, 1993, University of Vermont and State
     43  *  Agricultural College.
     44  * Copyright (c) 1992, 1993, Garrett A. Wollman.
     45  *
     46  * Portions:
     47  * Copyright (c) 1994, 1995, Rafal K. Boni
     48  * Copyright (c) 1990, 1991, William F. Jolitz
     49  * Copyright (c) 1990, The Regents of the University of California
     50  *
     51  * All rights reserved.
     52  *
     53  * Redistribution and use in source and binary forms, with or without
     54  * modification, are permitted provided that the following conditions
     55  * are met:
     56  * 1. Redistributions of source code must retain the above copyright
     57  *    notice, this list of conditions and the following disclaimer.
     58  * 2. Redistributions in binary form must reproduce the above copyright
     59  *    notice, this list of conditions and the following disclaimer in the
     60  *    documentation and/or other materials provided with the distribution.
     61  * 3. All advertising materials mentioning features or use of this software
     62  *    must display the following acknowledgement:
     63  *	This product includes software developed by Charles Hannum, by the
     64  *	University of Vermont and State Agricultural College and Garrett A.
     65  *	Wollman, by William F. Jolitz, and by the University of California,
     66  *	Berkeley, Lawrence Berkeley Laboratory, and its contributors.
     67  * 4. Neither the names of the Universities nor the names of the authors
     68  *    may be used to endorse or promote products derived from this software
     69  *    without specific prior written permission.
     70  *
     71  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     72  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     73  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     74  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
     75  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     76  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     77  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     78  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     79  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     80  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     81  * SUCH DAMAGE.
     82  */
     83 
     84 /*
     85  * Intel 82586 Ethernet chip
     86  * Register, bit, and structure definitions.
     87  *
     88  * Original StarLAN driver written by Garrett Wollman with reference to the
     89  * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
     90  *
     91  * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
     92  *
     93  * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
     94  *
     95  * Majorly cleaned up and 3C507 code merged by Charles Hannum.
     96  *
     97  * Converted to SUN ie driver by Charles D. Cranor,
     98  *		October 1994, January 1995.
     99  * This sun version based on i386 version 1.30.
    100  */
    101 
    102 /*
    103  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
    104  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
    105  * SUN, making life difficult.  Programming this chip is mostly the same,
    106  * but certain details differ from system to system.  This driver is
    107  * written so that different "ie" interfaces can be controled by the same
    108  * driver.
    109  */
    110 
    111 /*
    112 Mode of operation:
    113 
    114    We run the 82586 in a standard Ethernet mode.  We keep NFRAMES
    115    received frame descriptors around for the receiver to use, and
    116    NRXBUF associated receive buffer descriptors, both in a circular
    117    list.  Whenever a frame is received, we rotate both lists as
    118    necessary.  (The 586 treats both lists as a simple queue.)  We also
    119    keep a transmit command around so that packets can be sent off
    120    quickly.
    121 
    122    We configure the adapter in AL-LOC = 1 mode, which means that the
    123    Ethernet/802.3 MAC header is placed at the beginning of the receive
    124    buffer rather than being split off into various fields in the RFD.
    125    This also means that we must include this header in the transmit
    126    buffer as well.
    127 
    128    By convention, all transmit commands, and only transmit commands,
    129    shall have the I (IE_CMD_INTR) bit set in the command.  This way,
    130    when an interrupt arrives at i82586_intr(), it is immediately possible
    131    to tell what precisely caused it.  ANY OTHER command-sending
    132    routines should run at splnet(), and should post an acknowledgement
    133    to every interrupt they generate.
    134 
    135    To save the expense of shipping a command to 82586 every time we
    136    want to send a frame, we use a linked list of commands consisting
    137    of alternate XMIT and NOP commands. The links of these elements
    138    are manipulated (in iexmit()) such that the NOP command loops back
    139    to itself whenever the following XMIT command is not yet ready to
    140    go. Whenever an XMIT is ready, the preceding NOP link is pointed
    141    at it, while its own link field points to the following NOP command.
    142    Thus, a single transmit command sets off an interlocked traversal
    143    of the xmit command chain, with the host processor in control of
    144    the synchronization.
    145 */
    146 
    147 #include "bpfilter.h"
    148 
    149 #include <sys/param.h>
    150 #include <sys/systm.h>
    151 #include <sys/mbuf.h>
    152 #include <sys/buf.h>
    153 #include <sys/protosw.h>
    154 #include <sys/socket.h>
    155 #include <sys/ioctl.h>
    156 #include <sys/errno.h>
    157 #include <sys/syslog.h>
    158 #include <sys/device.h>
    159 
    160 #include <net/if.h>
    161 #include <net/if_dl.h>
    162 #include <net/if_types.h>
    163 #include <net/if_media.h>
    164 #include <net/if_ether.h>
    165 
    166 #if NBPFILTER > 0
    167 #include <net/bpf.h>
    168 #include <net/bpfdesc.h>
    169 #endif
    170 
    171 #ifdef INET
    172 #include <netinet/in.h>
    173 #include <netinet/in_systm.h>
    174 #include <netinet/in_var.h>
    175 #include <netinet/ip.h>
    176 #include <netinet/if_inarp.h>
    177 #endif
    178 
    179 #ifdef NS
    180 #include <netns/ns.h>
    181 #include <netns/ns_if.h>
    182 #endif
    183 
    184 #include <machine/bus.h>
    185 
    186 #include <dev/ic/i82586reg.h>
    187 #include <dev/ic/i82586var.h>
    188 
    189 void	 	i82586_reset 	__P((struct ie_softc *, int));
    190 void 		i82586_watchdog	__P((struct ifnet *));
    191 int 		i82586_init 	__P((struct ie_softc *));
    192 int 		i82586_ioctl 	__P((struct ifnet *, u_long, caddr_t));
    193 void 		i82586_start 	__P((struct ifnet *));
    194 
    195 int 		i82586_rint 	__P((struct ie_softc *, int));
    196 int 		i82586_tint 	__P((struct ie_softc *, int));
    197 
    198 int     	i82586_mediachange 	__P((struct ifnet *));
    199 void    	i82586_mediastatus 	__P((struct ifnet *,
    200 						struct ifmediareq *));
    201 
    202 static int 	ie_readframe		__P((struct ie_softc *, int));
    203 static struct mbuf *ieget 		__P((struct ie_softc *,
    204 					     struct ether_header *, 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, ehp, to_bpf, head, totlen)
   1077 	struct ie_softc *sc;
   1078 	struct ether_header *ehp;
   1079 	int *to_bpf;
   1080 	int head;
   1081 	int totlen;
   1082 {
   1083 	struct mbuf *top, **mp, *m;
   1084 	int len, resid;
   1085 	int thisrboff, thismboff;
   1086 
   1087 	/*
   1088 	 * Snarf the Ethernet header.
   1089 	 */
   1090 	(sc->memcopyin)(sc, ehp, IE_RBUF_ADDR(sc,head), sizeof *ehp);
   1091 
   1092 	/*
   1093 	 * As quickly as possible, check if this packet is for us.
   1094 	 * If not, don't waste a single cycle copying the rest of the
   1095 	 * packet in.
   1096 	 * This is only a consideration when FILTER is defined; i.e., when
   1097 	 * we are either running BPF or doing multicasting.
   1098 	 */
   1099 	if (!check_eh(sc, ehp, to_bpf)) {
   1100 		/* just this case, it's not an error */
   1101 		sc->sc_ethercom.ec_if.if_ierrors--;
   1102 		return (0);
   1103 	}
   1104 
   1105 	resid = totlen -= (thisrboff = sizeof *ehp);
   1106 
   1107 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1108 	if (m == 0)
   1109 		return (0);
   1110 	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
   1111 	m->m_pkthdr.len = totlen;
   1112 	len = MHLEN;
   1113 	top = 0;
   1114 	mp = &top;
   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 (top) {
   1122 			MGET(m, M_DONTWAIT, MT_DATA);
   1123 			if (m == 0) {
   1124 				m_freem(top);
   1125 				return (0);
   1126 			}
   1127 			len = MLEN;
   1128 		}
   1129 		if (totlen >= MINCLSIZE) {
   1130 			MCLGET(m, M_DONTWAIT);
   1131 			if ((m->m_flags & M_EXT) == 0) {
   1132 				m_freem(top);
   1133 				return (0);
   1134 			}
   1135 			len = MCLBYTES;
   1136 		}
   1137 		m->m_len = len = min(totlen, len);
   1138 		totlen -= len;
   1139 		*mp = m;
   1140 		mp = &m->m_next;
   1141 	}
   1142 
   1143 	m = top;
   1144 	thismboff = 0;
   1145 
   1146 	/*
   1147 	 * Now we take the mbuf chain (hopefully only one mbuf most of the
   1148 	 * time) and stuff the data into it.  There are no possible failures
   1149 	 * at or after this point.
   1150 	 */
   1151 	while (resid > 0) {
   1152 		int thisrblen = IE_RBUF_SIZE - thisrboff,
   1153 		    thismblen = m->m_len - thismboff;
   1154 		len = min(thisrblen, thismblen);
   1155 
   1156 		(sc->memcopyin)(sc, mtod(m, caddr_t) + thismboff,
   1157 				IE_RBUF_ADDR(sc,head) + thisrboff,
   1158 				(u_int)len);
   1159 		resid -= len;
   1160 
   1161 		if (len == thismblen) {
   1162 			m = m->m_next;
   1163 			thismboff = 0;
   1164 		} else
   1165 			thismboff += len;
   1166 
   1167 		if (len == thisrblen) {
   1168 			if (++head == sc->nrxbuf)
   1169 				head = 0;
   1170 			thisrboff = 0;
   1171 		} else
   1172 			thisrboff += len;
   1173 	}
   1174 
   1175 	/*
   1176 	 * Unless something changed strangely while we were doing the copy,
   1177 	 * we have now copied everything in from the shared memory.
   1178 	 * This means that we are done.
   1179 	 */
   1180 	return (top);
   1181 }
   1182 
   1183 /*
   1184  * Read frame NUM from unit UNIT (pre-cached as IE).
   1185  *
   1186  * This routine reads the RFD at NUM, and copies in the buffers from the list
   1187  * of RBD, then rotates the RBD list so that the receiver doesn't start
   1188  * complaining.  Trailers are DROPPED---there's no point in wasting time
   1189  * on confusing code to deal with them.  Hopefully, this machine will
   1190  * never ARP for trailers anyway.
   1191  */
   1192 static int
   1193 ie_readframe(sc, num)
   1194 	struct ie_softc *sc;
   1195 	int num;		/* frame number to read */
   1196 {
   1197 	struct mbuf *m;
   1198 	struct ether_header eh;
   1199 	u_int16_t bstart, bend;
   1200 	int pktlen;
   1201 #if NBPFILTER > 0
   1202 	int bpf_gets_it = 0;
   1203 #endif
   1204 
   1205 	if (i82586_get_rbd_list(sc, &bstart, &bend, &pktlen) == 0) {
   1206 		sc->sc_ethercom.ec_if.if_ierrors++;
   1207 		return (1);
   1208 	}
   1209 
   1210 #if NBPFILTER > 0
   1211 	m = ieget(sc, &eh, &bpf_gets_it, bstart, pktlen);
   1212 #else
   1213 	m = ieget(sc, &eh, 0, bstart, pktlen);
   1214 #endif
   1215 	i82586_release_rbd_list(sc, bstart, bend);
   1216 
   1217 	if (m == 0) {
   1218 		sc->sc_ethercom.ec_if.if_ierrors++;
   1219 		return (0);
   1220 	}
   1221 
   1222 #ifdef I82586_DEBUG
   1223 	if (sc->sc_debug & IED_READFRAME)
   1224 		printf("%s: frame from ether %s type 0x%x len %d\n",
   1225 			sc->sc_dev.dv_xname,
   1226 			ether_sprintf(eh.ether_shost),
   1227 			(u_int)eh.ether_type,
   1228 			pktlen);
   1229 #endif
   1230 
   1231 #if NBPFILTER > 0
   1232 	/*
   1233 	 * Check for a BPF filter; if so, hand it up.
   1234 	 * Note that we have to stick an extra mbuf up front, because bpf_mtap
   1235 	 * expects to have the ether header at the front.
   1236 	 * It doesn't matter that this results in an ill-formatted mbuf chain,
   1237 	 * since BPF just looks at the data.  (It doesn't try to free the mbuf,
   1238 	 * tho' it will make a copy for tcpdump.)
   1239 	 */
   1240 	if (bpf_gets_it) {
   1241 		struct mbuf m0;
   1242 		m0.m_len = sizeof eh;
   1243 		m0.m_data = (caddr_t)&eh;
   1244 		m0.m_next = m;
   1245 
   1246 		/* Pass it up. */
   1247 		bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, &m0);
   1248 
   1249 		/*
   1250 		 * A signal passed up from the filtering code indicating that
   1251 		 * the packet is intended for BPF but not for the protocol
   1252 		 * machinery.  We can save a few cycles by not handing it
   1253 		 * off to them.
   1254 		 */
   1255 		if (bpf_gets_it == 2) {
   1256 			m_freem(m);
   1257 			return (0);
   1258 		}
   1259 	}
   1260 #endif /* NBPFILTER > 0 */
   1261 
   1262 	/*
   1263 	 * Finally pass this packet up to higher layers.
   1264 	 */
   1265 	ether_input(&sc->sc_ethercom.ec_if, &eh, m);
   1266 	sc->sc_ethercom.ec_if.if_ipackets++;
   1267 	return (0);
   1268 }
   1269 
   1270 
   1271 /*
   1272  * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
   1273  * command to the chip to be executed.
   1274  */
   1275 static __inline__ void
   1276 iexmit(sc)
   1277 	struct ie_softc *sc;
   1278 {
   1279 	int off;
   1280 	int cur, prev;
   1281 
   1282 	cur = sc->xctail;
   1283 
   1284 #ifdef I82586_DEBUG
   1285 	if (sc->sc_debug & IED_XMIT)
   1286 		printf("%s: xmit buffer %d\n", sc->sc_dev.dv_xname, cur);
   1287 #endif
   1288 
   1289 	/*
   1290 	 * Setup the transmit command.
   1291 	 */
   1292 	sc->ie_bus_write16(sc, IE_CMD_XMIT_DESC(sc->xmit_cmds, cur),
   1293 			       IE_XBD_ADDR(sc->xbds, cur));
   1294 
   1295 	sc->ie_bus_write16(sc, IE_CMD_XMIT_STATUS(sc->xmit_cmds, cur), 0);
   1296 
   1297 	if (sc->do_xmitnopchain) {
   1298 		/*
   1299 		 * Gate this XMIT command to the following NOP
   1300 		 */
   1301 		sc->ie_bus_write16(sc, IE_CMD_XMIT_LINK(sc->xmit_cmds, cur),
   1302 				       IE_CMD_NOP_ADDR(sc->nop_cmds, cur));
   1303 		sc->ie_bus_write16(sc, IE_CMD_XMIT_CMD(sc->xmit_cmds, cur),
   1304 				       IE_CMD_XMIT | IE_CMD_INTR);
   1305 
   1306 		/*
   1307 		 * Loopback at following NOP
   1308 		 */
   1309 		sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, cur), 0);
   1310 		sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, cur),
   1311 				       IE_CMD_NOP_ADDR(sc->nop_cmds, cur));
   1312 
   1313 		/*
   1314 		 * Gate preceding NOP to this XMIT command
   1315 		 */
   1316 		prev = (cur + NTXBUF - 1) % NTXBUF;
   1317 		sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, prev), 0);
   1318 		sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, prev),
   1319 				       IE_CMD_XMIT_ADDR(sc->xmit_cmds, cur));
   1320 
   1321 		off = IE_SCB_STATUS(sc->scb);
   1322 		bus_space_barrier(sc->bt, sc->bh, off, 2,
   1323 				  BUS_SPACE_BARRIER_READ);
   1324 		if ((sc->ie_bus_read16(sc, off) & IE_CUS_ACTIVE) == 0) {
   1325 			printf("iexmit: CU not active\n");
   1326 			i82586_start_transceiver(sc);
   1327 		}
   1328 	} else {
   1329 		sc->ie_bus_write16(sc, IE_CMD_XMIT_LINK(sc->xmit_cmds,cur),
   1330 				       0xffff);
   1331 
   1332 		sc->ie_bus_write16(sc, IE_CMD_XMIT_CMD(sc->xmit_cmds, cur),
   1333 				       IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST);
   1334 
   1335 		off = IE_SCB_CMDLST(sc->scb);
   1336 		sc->ie_bus_write16(sc, off, IE_CMD_XMIT_ADDR(sc->xmit_cmds, cur));
   1337 		bus_space_barrier(sc->bt, sc->bh, off, 2,
   1338 				  BUS_SPACE_BARRIER_WRITE);
   1339 
   1340 		if (i82586_start_cmd(sc, IE_CUC_START, 0, 0, 1))
   1341 			printf("%s: iexmit: start xmit command timed out\n",
   1342 				sc->sc_dev.dv_xname);
   1343 	}
   1344 
   1345 	sc->sc_ethercom.ec_if.if_timer = 5;
   1346 }
   1347 
   1348 
   1349 /*
   1350  * Start transmission on an interface.
   1351  */
   1352 void
   1353 i82586_start(ifp)
   1354 	struct ifnet *ifp;
   1355 {
   1356 	struct ie_softc *sc = ifp->if_softc;
   1357 	struct mbuf *m0, *m;
   1358 	int	buffer, head, xbase;
   1359 	u_short	len;
   1360 	int	s;
   1361 
   1362 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1363 		return;
   1364 
   1365 	for (;;) {
   1366 		if (sc->xmit_busy == NTXBUF) {
   1367 			ifp->if_flags |= IFF_OACTIVE;
   1368 			break;
   1369 		}
   1370 
   1371 		head = sc->xchead;
   1372 		xbase = sc->xbds;
   1373 
   1374 		IF_DEQUEUE(&ifp->if_snd, m0);
   1375 		if (m0 == 0)
   1376 			break;
   1377 
   1378 		/* We need to use m->m_pkthdr.len, so require the header */
   1379 		if ((m0->m_flags & M_PKTHDR) == 0)
   1380 			panic("i82586_start: no header mbuf");
   1381 
   1382 #if NBPFILTER > 0
   1383 		/* Tap off here if there is a BPF listener. */
   1384 		if (ifp->if_bpf)
   1385 			bpf_mtap(ifp->if_bpf, m0);
   1386 #endif
   1387 
   1388 #ifdef I82586_DEBUG
   1389 		if (sc->sc_debug & IED_ENQ)
   1390 			printf("%s: fill buffer %d\n", sc->sc_dev.dv_xname,
   1391 				sc->xchead);
   1392 #endif
   1393 
   1394 		if (m0->m_pkthdr.len > IE_TBUF_SIZE)
   1395 			printf("%s: tbuf overflow\n", sc->sc_dev.dv_xname);
   1396 
   1397 		buffer = IE_XBUF_ADDR(sc, head);
   1398 		for (m = m0; m != 0; m = m->m_next) {
   1399 			(sc->memcopyout)(sc, mtod(m,caddr_t), buffer, m->m_len);
   1400 			buffer += m->m_len;
   1401 		}
   1402 
   1403 		len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
   1404 		m_freem(m0);
   1405 
   1406 		/*
   1407 		 * Setup the transmit buffer descriptor here, while we
   1408 		 * know the packet's length.
   1409 		 */
   1410 		sc->ie_bus_write16(sc, IE_XBD_FLAGS(xbase, head),
   1411 				       len | IE_TBD_EOL);
   1412 		sc->ie_bus_write16(sc, IE_XBD_NEXT(xbase, head), 0xffff);
   1413 		sc->ie_bus_write24(sc, IE_XBD_BUF(xbase, head),
   1414 				       IE_XBUF_ADDR(sc, head));
   1415 
   1416 		if (++head == NTXBUF)
   1417 			head = 0;
   1418 		sc->xchead = head;
   1419 
   1420 		s = splnet();
   1421 		/* Start the first packet transmitting. */
   1422 		if (sc->xmit_busy == 0)
   1423 			iexmit(sc);
   1424 
   1425 		sc->xmit_busy++;
   1426 		splx(s);
   1427 	}
   1428 }
   1429 
   1430 /*
   1431  * Probe IE's ram setup   [ Move all this into MD front-end!? ]
   1432  * Use only if SCP and ISCP represent offsets into shared ram space.
   1433  */
   1434 int
   1435 i82586_proberam(sc)
   1436 	struct ie_softc *sc;
   1437 {
   1438 	int result, off;
   1439 
   1440 	/* Put in 16-bit mode */
   1441 	off = IE_SCP_BUS_USE(sc->scp);
   1442 	bus_space_write_1(sc->bt, sc->bh, off, 0);
   1443 	bus_space_barrier(sc->bt, sc->bh, off, 1, BUS_SPACE_BARRIER_WRITE);
   1444 
   1445 	/* Set the ISCP `busy' bit */
   1446 	off = IE_ISCP_BUSY(sc->iscp);
   1447 	bus_space_write_1(sc->bt, sc->bh, off, 1);
   1448 	bus_space_barrier(sc->bt, sc->bh, off, 1, BUS_SPACE_BARRIER_WRITE);
   1449 
   1450 	if (sc->hwreset)
   1451 		(sc->hwreset)(sc, CHIP_PROBE);
   1452 
   1453 	(sc->chan_attn) (sc);
   1454 
   1455 	delay(100);		/* wait a while... */
   1456 
   1457 	/* Read back the ISCP `busy' bit; it should be clear by now */
   1458 	off = IE_ISCP_BUSY(sc->iscp);
   1459 	bus_space_barrier(sc->bt, sc->bh, off, 1, BUS_SPACE_BARRIER_READ);
   1460 	result = bus_space_read_1(sc->bt, sc->bh, off) == 0;
   1461 
   1462 	/* Acknowledge any interrupts we may have caused. */
   1463 	ie_ack(sc, IE_ST_WHENCE);
   1464 
   1465 	return (result);
   1466 }
   1467 
   1468 void
   1469 i82586_reset(sc, hard)
   1470 	struct ie_softc *sc;
   1471 	int hard;
   1472 {
   1473 	int s = splnet();
   1474 
   1475 	if (hard)
   1476 		printf("%s: reset\n", sc->sc_dev.dv_xname);
   1477 
   1478 	/* Clear OACTIVE in case we're called from watchdog (frozen xmit). */
   1479 	sc->sc_ethercom.ec_if.if_timer = 0;
   1480 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1481 
   1482 	/*
   1483 	 * Stop i82586 dead in its tracks.
   1484 	 */
   1485 	if (i82586_start_cmd(sc, IE_RUC_ABORT | IE_CUC_ABORT, 0, 0, 0))
   1486 		printf("%s: abort commands timed out\n", sc->sc_dev.dv_xname);
   1487 
   1488 	/*
   1489 	 * This can really slow down the i82586_reset() on some cards, but it's
   1490 	 * necessary to unwedge other ones (eg, the Sun VME ones) from certain
   1491 	 * lockups.
   1492 	 */
   1493 	if (hard && sc->hwreset)
   1494 		(sc->hwreset)(sc, CARD_RESET);
   1495 
   1496 	delay(100);
   1497 	ie_ack(sc, IE_ST_WHENCE);
   1498 
   1499 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0) {
   1500 		int retries=0;	/* XXX - find out why init sometimes fails */
   1501 		while (retries++ < 2)
   1502 			if (i82586_init(sc) == 1)
   1503 				break;
   1504 	}
   1505 
   1506 	splx(s);
   1507 }
   1508 
   1509 
   1510 static void
   1511 setup_simple_command(sc, cmd, cmdbuf)
   1512 	struct ie_softc *sc;
   1513 	int cmd;
   1514 	int cmdbuf;
   1515 {
   1516 	/* Setup a simple command */
   1517 	sc->ie_bus_write16(sc, IE_CMD_COMMON_STATUS(cmdbuf), 0);
   1518 	sc->ie_bus_write16(sc, IE_CMD_COMMON_CMD(cmdbuf), cmd | IE_CMD_LAST);
   1519 	sc->ie_bus_write16(sc, IE_CMD_COMMON_LINK(cmdbuf), 0xffff);
   1520 
   1521 	/* Assign the command buffer to the SCB command list */
   1522 	sc->ie_bus_write16(sc, IE_SCB_CMDLST(sc->scb), cmdbuf);
   1523 }
   1524 
   1525 /*
   1526  * Run the time-domain reflectometer.
   1527  */
   1528 static void
   1529 ie_run_tdr(sc, cmd)
   1530 	struct ie_softc *sc;
   1531 	int cmd;
   1532 {
   1533 	int result;
   1534 
   1535 	setup_simple_command(sc, IE_CMD_TDR, cmd);
   1536 	(sc->ie_bus_write16)(sc, IE_CMD_TDR_TIME(cmd), 0);
   1537 
   1538 	if (i82586_start_cmd(sc, IE_CUC_START, cmd, IE_STAT_COMPL, 0) ||
   1539 	    (sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmd)) & IE_STAT_OK) == 0)
   1540 		result = 0x10000; /* XXX */
   1541 	else
   1542 		result = sc->ie_bus_read16(sc, IE_CMD_TDR_TIME(cmd));
   1543 
   1544 	/* Squash any pending interrupts */
   1545 	ie_ack(sc, IE_ST_WHENCE);
   1546 
   1547 	if (result & IE_TDR_SUCCESS)
   1548 		return;
   1549 
   1550 	if (result & 0x10000)
   1551 		printf("%s: TDR command failed\n", sc->sc_dev.dv_xname);
   1552 	else if (result & IE_TDR_XCVR)
   1553 		printf("%s: transceiver problem\n", sc->sc_dev.dv_xname);
   1554 	else if (result & IE_TDR_OPEN)
   1555 		printf("%s: TDR detected an open %d clocks away\n",
   1556 			sc->sc_dev.dv_xname, result & IE_TDR_TIME);
   1557 	else if (result & IE_TDR_SHORT)
   1558 		printf("%s: TDR detected a short %d clocks away\n",
   1559 			sc->sc_dev.dv_xname, result & IE_TDR_TIME);
   1560 	else
   1561 		printf("%s: TDR returned unknown status 0x%x\n",
   1562 			sc->sc_dev.dv_xname, result);
   1563 }
   1564 
   1565 
   1566 /*
   1567  * i82586_setup_bufs: set up the buffers
   1568  *
   1569  * We have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
   1570  * this is to be used for the buffers.  The chip indexs its control data
   1571  * structures with 16 bit offsets, and it indexes actual buffers with
   1572  * 24 bit addresses.   So we should allocate control buffers first so that
   1573  * we don't overflow the 16 bit offset field.   The number of transmit
   1574  * buffers is fixed at compile time.
   1575  *
   1576  */
   1577 static void
   1578 i82586_setup_bufs(sc)
   1579 	struct ie_softc *sc;
   1580 {
   1581 	int	ptr = sc->buf_area;	/* memory pool */
   1582 	int     n, r;
   1583 
   1584 	/*
   1585 	 * step 0: zero memory and figure out how many recv buffers and
   1586 	 * frames we can have.
   1587 	 */
   1588 	ptr = (ptr + 3) & ~3;	/* set alignment and stick with it */
   1589 
   1590 
   1591 	/*
   1592 	 *  step 1: lay out data structures in the shared-memory area
   1593 	 */
   1594 
   1595 	/* The no-op commands; used if "nop-chaining" is in effect */
   1596 	sc->nop_cmds = ptr;
   1597 	ptr += NTXBUF * IE_CMD_NOP_SZ;
   1598 
   1599 	/* The transmit commands */
   1600 	sc->xmit_cmds = ptr;
   1601 	ptr += NTXBUF * IE_CMD_XMIT_SZ;
   1602 
   1603 	/* The transmit buffers descriptors */
   1604 	sc->xbds = ptr;
   1605 	ptr += NTXBUF * IE_XBD_SZ;
   1606 
   1607 	/* The transmit buffers */
   1608 	sc->xbufs = ptr;
   1609 	ptr += NTXBUF * IE_TBUF_SIZE;
   1610 
   1611 	ptr = (ptr + 3) & ~3;		/* re-align.. just in case */
   1612 
   1613 	/* Compute free space for RECV stuff */
   1614 	n = sc->buf_area_sz - (ptr - sc->buf_area);
   1615 
   1616 	/* Compute size of one RECV frame */
   1617 	r = IE_RFRAME_SZ + ((IE_RBD_SZ + IE_RBUF_SIZE) * B_PER_F);
   1618 
   1619 	sc->nframes = n / r;
   1620 
   1621 	if (sc->nframes <= 0)
   1622 		panic("ie: bogus buffer calc\n");
   1623 
   1624 	sc->nrxbuf = sc->nframes * B_PER_F;
   1625 
   1626 	/* The receice frame descriptors */
   1627 	sc->rframes = ptr;
   1628 	ptr += sc->nframes * IE_RFRAME_SZ;
   1629 
   1630 	/* The receive buffer descriptors */
   1631 	sc->rbds = ptr;
   1632 	ptr += sc->nrxbuf * IE_RBD_SZ;
   1633 
   1634 	/* The receive buffers */
   1635 	sc->rbufs = ptr;
   1636 	ptr += sc->nrxbuf * IE_RBUF_SIZE;
   1637 
   1638 #ifdef I82586_DEBUG
   1639 	printf("%s: %d frames %d bufs\n", sc->sc_dev.dv_xname, sc->nframes,
   1640 		sc->nrxbuf);
   1641 #endif
   1642 
   1643 	/*
   1644 	 * step 2: link together the recv frames and set EOL on last one
   1645 	 */
   1646 	for (n = 0; n < sc->nframes; n++) {
   1647 		int m = (n == sc->nframes - 1) ? 0 : n + 1;
   1648 
   1649 		/* Clear status */
   1650 		sc->ie_bus_write16(sc, IE_RFRAME_STATUS(sc->rframes,n), 0);
   1651 
   1652 		/* RBD link = NULL */
   1653 		sc->ie_bus_write16(sc, IE_RFRAME_BUFDESC(sc->rframes,n),
   1654 				       0xffff);
   1655 
   1656 		/* Make a circular list */
   1657 		sc->ie_bus_write16(sc, IE_RFRAME_NEXT(sc->rframes,n),
   1658 				       IE_RFRAME_ADDR(sc->rframes,m));
   1659 
   1660 		/* Mark last as EOL */
   1661 		sc->ie_bus_write16(sc, IE_RFRAME_LAST(sc->rframes,n),
   1662 				       ((m==0)? (IE_FD_EOL|IE_FD_SUSP) : 0));
   1663 	}
   1664 
   1665 	/*
   1666 	 * step 3: link the RBDs and set EOL on last one
   1667 	 */
   1668 	for (n = 0; n < sc->nrxbuf; n++) {
   1669 		int m = (n == sc->nrxbuf - 1) ? 0 : n + 1;
   1670 
   1671 		/* Clear status */
   1672 		sc->ie_bus_write16(sc, IE_RBD_STATUS(sc->rbds,n), 0);
   1673 
   1674 		/* Make a circular list */
   1675 		sc->ie_bus_write16(sc, IE_RBD_NEXT(sc->rbds,n),
   1676 				       IE_RBD_ADDR(sc->rbds,m));
   1677 
   1678 		/* Link to data buffers */
   1679 		sc->ie_bus_write24(sc, IE_RBD_BUFADDR(sc->rbds, n),
   1680 				       IE_RBUF_ADDR(sc, n));
   1681 		sc->ie_bus_write16(sc, IE_RBD_BUFLEN(sc->rbds,n),
   1682 				       IE_RBUF_SIZE | ((m==0)?IE_RBD_EOL:0));
   1683 	}
   1684 
   1685 	/*
   1686 	 * step 4: all xmit no-op commands loopback onto themselves
   1687 	 */
   1688 	for (n = 0; n < NTXBUF; n++) {
   1689 		(sc->ie_bus_write16)(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, n), 0);
   1690 
   1691 		(sc->ie_bus_write16)(sc, IE_CMD_NOP_CMD(sc->nop_cmds, n),
   1692 					 IE_CMD_NOP);
   1693 
   1694 		(sc->ie_bus_write16)(sc, IE_CMD_NOP_LINK(sc->nop_cmds, n),
   1695 					 IE_CMD_NOP_ADDR(sc->nop_cmds, n));
   1696 	}
   1697 
   1698 
   1699 	/*
   1700 	 * step 6: set the head and tail pointers on receive to keep track of
   1701 	 * the order in which RFDs and RBDs are used.
   1702 	 */
   1703 
   1704 	/* Pointers to last packet sent and next available transmit buffer. */
   1705 	sc->xchead = sc->xctail = 0;
   1706 
   1707 	/* Clear transmit-busy flag and set number of free transmit buffers. */
   1708 	sc->xmit_busy = 0;
   1709 
   1710 	/*
   1711 	 * Pointers to first and last receive frame.
   1712 	 * The RFD pointed to by rftail is the only one that has EOL set.
   1713 	 */
   1714 	sc->rfhead = 0;
   1715 	sc->rftail = sc->nframes - 1;
   1716 
   1717 	/*
   1718 	 * Pointers to first and last receive descriptor buffer.
   1719 	 * The RBD pointed to by rbtail is the only one that has EOL set.
   1720 	 */
   1721 	sc->rbhead = 0;
   1722 	sc->rbtail = sc->nrxbuf - 1;
   1723 
   1724 /* link in recv frames * and buffer into the scb. */
   1725 #ifdef I82586_DEBUG
   1726 	printf("%s: reserved %d bytes\n",
   1727 		sc->sc_dev.dv_xname, ptr - sc->buf_area);
   1728 #endif
   1729 }
   1730 
   1731 static int
   1732 ie_cfg_setup(sc, cmd, promiscuous, manchester)
   1733 	struct ie_softc *sc;
   1734 	int cmd;
   1735 	int promiscuous, manchester;
   1736 {
   1737 	int cmdresult, status;
   1738 
   1739 	setup_simple_command(sc, IE_CMD_CONFIG, cmd);
   1740 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_CNT(cmd), 0x0c);
   1741 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_FIFO(cmd), 8);
   1742 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_SAVEBAD(cmd), 0x40);
   1743 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_ADDRLEN(cmd), 0x2e);
   1744 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_PRIORITY(cmd), 0);
   1745 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_IFS(cmd), 0x60);
   1746 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_SLOT_LOW(cmd), 0);
   1747 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_SLOT_HIGH(cmd), 0xf2);
   1748 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_PROMISC(cmd),
   1749 					  !!promiscuous | manchester << 2);
   1750 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_CRSCDT(cmd), 0);
   1751 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_MINLEN(cmd), 64);
   1752 	bus_space_write_1(sc->bt, sc->bh, IE_CMD_CFG_JUNK(cmd), 0xff);
   1753 	bus_space_barrier(sc->bt, sc->bh, cmd, IE_CMD_CFG_SZ,
   1754 			  BUS_SPACE_BARRIER_WRITE);
   1755 
   1756 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmd, IE_STAT_COMPL, 0);
   1757 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmd));
   1758 	if (cmdresult != 0) {
   1759 		printf("%s: configure command timed out; status %x\n",
   1760 			sc->sc_dev.dv_xname, status);
   1761 		return (0);
   1762 	}
   1763 	if ((status & IE_STAT_OK) == 0) {
   1764 		printf("%s: configure command failed; status %x\n",
   1765 			sc->sc_dev.dv_xname, status);
   1766 		return (0);
   1767 	}
   1768 
   1769 	/* Squash any pending interrupts */
   1770 	ie_ack(sc, IE_ST_WHENCE);
   1771 	return (1);
   1772 }
   1773 
   1774 static int
   1775 ie_ia_setup(sc, cmdbuf)
   1776 	struct ie_softc *sc;
   1777 	int cmdbuf;
   1778 {
   1779 	int cmdresult, status;
   1780 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1781 
   1782 	setup_simple_command(sc, IE_CMD_IASETUP, cmdbuf);
   1783 
   1784 	(sc->memcopyout)(sc, LLADDR(ifp->if_sadl),
   1785 			 IE_CMD_IAS_EADDR(cmdbuf), ETHER_ADDR_LEN);
   1786 
   1787 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmdbuf, IE_STAT_COMPL, 0);
   1788 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmdbuf));
   1789 	if (cmdresult != 0) {
   1790 		printf("%s: individual address command timed out; status %x\n",
   1791 			sc->sc_dev.dv_xname, status);
   1792 		return (0);
   1793 	}
   1794 	if ((status & IE_STAT_OK) == 0) {
   1795 		printf("%s: individual address command failed; status %x\n",
   1796 			sc->sc_dev.dv_xname, status);
   1797 		return (0);
   1798 	}
   1799 
   1800 	/* Squash any pending interrupts */
   1801 	ie_ack(sc, IE_ST_WHENCE);
   1802 	return (1);
   1803 }
   1804 
   1805 /*
   1806  * Run the multicast setup command.
   1807  * Called at splnet().
   1808  */
   1809 static int
   1810 ie_mc_setup(sc, cmdbuf)
   1811 	struct ie_softc *sc;
   1812 	int cmdbuf;
   1813 {
   1814 	int cmdresult, status;
   1815 
   1816 	if (sc->mcast_count == 0)
   1817 		return (1);
   1818 
   1819 	setup_simple_command(sc, IE_CMD_MCAST, cmdbuf);
   1820 
   1821 	(sc->memcopyout)(sc, (caddr_t)sc->mcast_addrs,
   1822 			 IE_CMD_MCAST_MADDR(cmdbuf),
   1823 			 sc->mcast_count * ETHER_ADDR_LEN);
   1824 
   1825 	sc->ie_bus_write16(sc, IE_CMD_MCAST_BYTES(cmdbuf),
   1826 			       sc->mcast_count * ETHER_ADDR_LEN);
   1827 
   1828 	/* Start the command */
   1829 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmdbuf, IE_STAT_COMPL, 0);
   1830 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmdbuf));
   1831 	if (cmdresult != 0) {
   1832 		printf("%s: multicast setup command timed out; status %x\n",
   1833 			sc->sc_dev.dv_xname, status);
   1834 		return (0);
   1835 	}
   1836 	if ((status & IE_STAT_OK) == 0) {
   1837 		printf("%s: multicast setup command failed; status %x\n",
   1838 			sc->sc_dev.dv_xname, status);
   1839 		return (0);
   1840 	}
   1841 
   1842 	/* Squash any pending interrupts */
   1843 	ie_ack(sc, IE_ST_WHENCE);
   1844 	return (1);
   1845 }
   1846 
   1847 /*
   1848  * This routine takes the environment generated by check_ie_present() and adds
   1849  * to it all the other structures we need to operate the adapter.  This
   1850  * includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands, starting
   1851  * the receiver unit, and clearing interrupts.
   1852  *
   1853  * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
   1854  */
   1855 int
   1856 i82586_init(sc)
   1857 	struct ie_softc *sc;
   1858 {
   1859 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1860 	int cmd;
   1861 
   1862 	sc->async_cmd_inprogress = 0;
   1863 
   1864 	cmd = sc->buf_area;
   1865 
   1866 	/*
   1867 	 * Send the configure command first.
   1868 	 */
   1869 	if (ie_cfg_setup(sc, cmd, sc->promisc, 0) == 0)
   1870 		return (0);
   1871 
   1872 	/*
   1873 	 * Send the Individual Address Setup command.
   1874 	 */
   1875 	if (ie_ia_setup(sc, cmd) == 0)
   1876 		return (0);
   1877 
   1878 	/*
   1879 	 * Run the time-domain reflectometer.
   1880 	 */
   1881 	ie_run_tdr(sc, cmd);
   1882 
   1883 	/*
   1884 	 * Set the multi-cast filter, if any
   1885 	 */
   1886 	if (ie_mc_setup(sc, cmd) == 0)
   1887 		return (0);
   1888 
   1889 	/*
   1890 	 * Acknowledge any interrupts we have generated thus far.
   1891 	 */
   1892 	ie_ack(sc, IE_ST_WHENCE);
   1893 
   1894 	/*
   1895 	 * Set up the transmit and recv buffers.
   1896 	 */
   1897 	i82586_setup_bufs(sc);
   1898 
   1899 	if (sc->hwinit)
   1900 		(sc->hwinit)(sc);
   1901 
   1902 	ifp->if_flags |= IFF_RUNNING;
   1903 	ifp->if_flags &= ~IFF_OACTIVE;
   1904 
   1905 	if (NTXBUF < 2)
   1906 		sc->do_xmitnopchain = 0;
   1907 
   1908 	i82586_start_transceiver(sc);
   1909 	return (1);
   1910 }
   1911 
   1912 /*
   1913  * Start the RU and possibly the CU unit
   1914  */
   1915 static void
   1916 i82586_start_transceiver(sc)
   1917 	struct ie_softc *sc;
   1918 {
   1919 
   1920 	/*
   1921 	 * Start RU at current position in frame & RBD lists.
   1922 	 */
   1923 	sc->ie_bus_write16(sc, IE_RFRAME_BUFDESC(sc->rframes,sc->rfhead),
   1924 			       IE_RBD_ADDR(sc->rbds, sc->rbhead));
   1925 
   1926 	sc->ie_bus_write16(sc, IE_SCB_RCVLST(sc->scb),
   1927 			       IE_RFRAME_ADDR(sc->rframes,sc->rfhead));
   1928 
   1929 	if (sc->do_xmitnopchain) {
   1930 		/* Stop transmit command chain */
   1931 		if (i82586_start_cmd(sc, IE_CUC_SUSPEND|IE_RUC_SUSPEND, 0, 0, 0))
   1932 			printf("%s: CU/RU stop command timed out\n",
   1933 				sc->sc_dev.dv_xname);
   1934 
   1935 		/* Start the receiver & transmitter chain */
   1936 		/* sc->scb->ie_command_list =
   1937 			IEADDR(sc->nop_cmds[(sc->xctail+NTXBUF-1) % NTXBUF]);*/
   1938 		sc->ie_bus_write16(sc, IE_SCB_CMDLST(sc->scb),
   1939 				   IE_CMD_NOP_ADDR(
   1940 					sc->nop_cmds,
   1941 					(sc->xctail + NTXBUF - 1) % NTXBUF));
   1942 
   1943 		if (i82586_start_cmd(sc, IE_CUC_START|IE_RUC_START, 0, 0, 0))
   1944 			printf("%s: CU/RU command timed out\n",
   1945 				sc->sc_dev.dv_xname);
   1946 	} else {
   1947 		if (i82586_start_cmd(sc, IE_RUC_START, 0, 0, 0))
   1948 			printf("%s: RU command timed out\n",
   1949 				sc->sc_dev.dv_xname);
   1950 	}
   1951 }
   1952 
   1953 static void
   1954 iestop(sc)
   1955 	struct ie_softc *sc;
   1956 {
   1957 
   1958 	if (i82586_start_cmd(sc, IE_RUC_SUSPEND | IE_CUC_SUSPEND, 0, 0, 0))
   1959 		printf("%s: iestop: disable commands timed out\n",
   1960 			sc->sc_dev.dv_xname);
   1961 }
   1962 
   1963 int
   1964 i82586_ioctl(ifp, cmd, data)
   1965 	register struct ifnet *ifp;
   1966 	u_long cmd;
   1967 	caddr_t data;
   1968 {
   1969 	struct ie_softc *sc = ifp->if_softc;
   1970 	struct ifaddr *ifa = (struct ifaddr *)data;
   1971 	struct ifreq *ifr = (struct ifreq *)data;
   1972 	int s, error = 0;
   1973 
   1974 	s = splnet();
   1975 
   1976 	switch(cmd) {
   1977 
   1978 	case SIOCSIFADDR:
   1979 		ifp->if_flags |= IFF_UP;
   1980 
   1981 		switch(ifa->ifa_addr->sa_family) {
   1982 #ifdef INET
   1983 		case AF_INET:
   1984 			i82586_init(sc);
   1985 			arp_ifinit(ifp, ifa);
   1986 			break;
   1987 #endif
   1988 #ifdef NS
   1989 		/* XXX - This code is probably wrong. */
   1990 		case AF_NS:
   1991 		    {
   1992 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1993 
   1994 			if (ns_nullhost(*ina))
   1995 				ina->x_host =
   1996 				    *(union ns_host *)LLADDR(ifp->if_sadl);
   1997 			else
   1998 				bcopy(ina->x_host.c_host,
   1999 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   2000 			/* Set new address. */
   2001 			i82586_init(sc);
   2002 			break;
   2003 		    }
   2004 #endif /* NS */
   2005 		default:
   2006 			i82586_init(sc);
   2007 			break;
   2008 		}
   2009 		break;
   2010 
   2011 	case SIOCSIFFLAGS:
   2012 		sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
   2013 		if ((ifp->if_flags & IFF_UP) == 0 &&
   2014 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   2015 			/*
   2016 			 * If interface is marked down and it is running, then
   2017 			 * stop it.
   2018 			 */
   2019 			iestop(sc);
   2020 			ifp->if_flags &= ~IFF_RUNNING;
   2021 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   2022 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   2023 			/*
   2024 			 * If interface is marked up and it is stopped, then
   2025 			 * start it.
   2026 			 */
   2027 			i82586_init(sc);
   2028 		} else {
   2029 			/*
   2030 			 * Reset the interface to pick up changes in any other
   2031 			 * flags that affect hardware registers.
   2032 			 */
   2033 			iestop(sc);
   2034 			i82586_init(sc);
   2035 		}
   2036 #ifdef I82586_DEBUG
   2037 		if (ifp->if_flags & IFF_DEBUG)
   2038 			sc->sc_debug = IED_ALL;
   2039 		else
   2040 			sc->sc_debug = 0;
   2041 #endif
   2042 		break;
   2043 
   2044 	case SIOCADDMULTI:
   2045 	case SIOCDELMULTI:
   2046 		error = (cmd == SIOCADDMULTI) ?
   2047 		    ether_addmulti(ifr, &sc->sc_ethercom):
   2048 		    ether_delmulti(ifr, &sc->sc_ethercom);
   2049 
   2050 		if (error == ENETRESET) {
   2051 			/*
   2052 			 * Multicast list has changed; set the hardware filter
   2053 			 * accordingly.
   2054 			 */
   2055 			ie_mc_reset(sc);
   2056 			error = 0;
   2057 		}
   2058 		break;
   2059 
   2060         case SIOCGIFMEDIA:
   2061         case SIOCSIFMEDIA:
   2062                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   2063                 break;
   2064 
   2065 	default:
   2066 		error = EINVAL;
   2067 	}
   2068 	splx(s);
   2069 	return (error);
   2070 }
   2071 
   2072 static void
   2073 ie_mc_reset(sc)
   2074 	struct ie_softc *sc;
   2075 {
   2076 	struct ether_multi *enm;
   2077 	struct ether_multistep step;
   2078 	int size;
   2079 
   2080 	/*
   2081 	 * Step through the list of addresses.
   2082 	 */
   2083 again:
   2084 	size = 0;
   2085 	sc->mcast_count = 0;
   2086 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   2087 	while (enm) {
   2088 		size += 6;
   2089 		if (sc->mcast_count >= IE_MAXMCAST ||
   2090 		    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
   2091 			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
   2092 			i82586_ioctl(&sc->sc_ethercom.ec_if,
   2093 				     SIOCSIFFLAGS, (void *)0);
   2094 			return;
   2095 		}
   2096 		ETHER_NEXT_MULTI(step, enm);
   2097 	}
   2098 
   2099 	if (size > sc->mcast_addrs_size) {
   2100 		/* Need to allocate more space */
   2101 		if (sc->mcast_addrs_size)
   2102 			free(sc->mcast_addrs, M_IPMADDR);
   2103 		sc->mcast_addrs = (char *)
   2104 			malloc(size, M_IPMADDR, M_WAITOK);
   2105 		sc->mcast_addrs_size = size;
   2106 	}
   2107 
   2108 	/*
   2109 	 * We've got the space; now copy the addresses
   2110 	 */
   2111 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   2112 	while (enm) {
   2113 		if (sc->mcast_count >= IE_MAXMCAST)
   2114 			goto again; /* Just in case */
   2115 
   2116 		bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
   2117 		sc->mcast_count++;
   2118 		ETHER_NEXT_MULTI(step, enm);
   2119 	}
   2120 	sc->want_mcsetup = 1;
   2121 }
   2122 
   2123 /*
   2124  * Media change callback.
   2125  */
   2126 int
   2127 i82586_mediachange(ifp)
   2128         struct ifnet *ifp;
   2129 {
   2130         struct ie_softc *sc = ifp->if_softc;
   2131 
   2132         if (sc->sc_mediachange)
   2133                 return ((*sc->sc_mediachange)(sc));
   2134         return (EINVAL);
   2135 }
   2136 
   2137 /*
   2138  * Media status callback.
   2139  */
   2140 void
   2141 i82586_mediastatus(ifp, ifmr)
   2142         struct ifnet *ifp;
   2143         struct ifmediareq *ifmr;
   2144 {
   2145         struct ie_softc *sc = ifp->if_softc;
   2146 
   2147         if (sc->sc_mediastatus)
   2148                 (*sc->sc_mediastatus)(sc, ifmr);
   2149 }
   2150 
   2151 #ifdef I82586_DEBUG
   2152 void
   2153 print_rbd(sc, n)
   2154 	struct ie_softc *sc;
   2155 	int n;
   2156 {
   2157 
   2158 	printf("RBD at %08x:\n  status %04x, next %04x, buffer %lx\n"
   2159 		"length/EOL %04x\n", IE_RBD_ADDR(sc->rbds,n),
   2160 		sc->ie_bus_read16(sc, IE_RBD_STATUS(sc->rbds,n)),
   2161 		sc->ie_bus_read16(sc, IE_RBD_NEXT(sc->rbds,n)),
   2162 		(u_long)0,/*bus_space_read_4(sc->bt, sc->bh, IE_RBD_BUFADDR(sc->rbds,n)),-* XXX */
   2163 		sc->ie_bus_read16(sc, IE_RBD_BUFLEN(sc->rbds,n)));
   2164 }
   2165 #endif
   2166