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