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