Home | History | Annotate | Line # | Download | only in ic
i82586.c revision 1.8
      1  1.8  perry /*	$NetBSD: i82586.c,v 1.8 1998/01/06 04:55:52 perry Exp $	*/
      2  1.1     pk 
      3  1.1     pk /*-
      4  1.1     pk  * Copyright (c) 1997 Paul Kranenburg.
      5  1.1     pk  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
      6  1.1     pk  * Copyright (c) 1992, 1993, University of Vermont and State
      7  1.1     pk  *  Agricultural College.
      8  1.1     pk  * Copyright (c) 1992, 1993, Garrett A. Wollman.
      9  1.1     pk  *
     10  1.1     pk  * Portions:
     11  1.1     pk  * Copyright (c) 1994, 1995, Rafal K. Boni
     12  1.1     pk  * Copyright (c) 1990, 1991, William F. Jolitz
     13  1.1     pk  * Copyright (c) 1990, The Regents of the University of California
     14  1.1     pk  *
     15  1.1     pk  * All rights reserved.
     16  1.1     pk  *
     17  1.1     pk  * Redistribution and use in source and binary forms, with or without
     18  1.1     pk  * modification, are permitted provided that the following conditions
     19  1.1     pk  * are met:
     20  1.1     pk  * 1. Redistributions of source code must retain the above copyright
     21  1.1     pk  *    notice, this list of conditions and the following disclaimer.
     22  1.1     pk  * 2. Redistributions in binary form must reproduce the above copyright
     23  1.1     pk  *    notice, this list of conditions and the following disclaimer in the
     24  1.1     pk  *    documentation and/or other materials provided with the distribution.
     25  1.1     pk  * 3. All advertising materials mentioning features or use of this software
     26  1.1     pk  *    must display the following acknowledgement:
     27  1.1     pk  *	This product includes software developed by Charles Hannum, by the
     28  1.1     pk  *	University of Vermont and State Agricultural College and Garrett A.
     29  1.1     pk  *	Wollman, by William F. Jolitz, and by the University of California,
     30  1.1     pk  *	Berkeley, Lawrence Berkeley Laboratory, and its contributors.
     31  1.1     pk  * 4. Neither the names of the Universities nor the names of the authors
     32  1.1     pk  *    may be used to endorse or promote products derived from this software
     33  1.1     pk  *    without specific prior written permission.
     34  1.1     pk  *
     35  1.1     pk  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     36  1.1     pk  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     37  1.1     pk  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     38  1.1     pk  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
     39  1.1     pk  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     40  1.1     pk  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     41  1.1     pk  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     42  1.1     pk  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     43  1.1     pk  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     44  1.1     pk  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     45  1.1     pk  * SUCH DAMAGE.
     46  1.1     pk  */
     47  1.1     pk 
     48  1.1     pk /*
     49  1.1     pk  * Intel 82586 Ethernet chip
     50  1.1     pk  * Register, bit, and structure definitions.
     51  1.1     pk  *
     52  1.1     pk  * Original StarLAN driver written by Garrett Wollman with reference to the
     53  1.1     pk  * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
     54  1.1     pk  *
     55  1.1     pk  * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
     56  1.1     pk  *
     57  1.1     pk  * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
     58  1.1     pk  *
     59  1.1     pk  * Majorly cleaned up and 3C507 code merged by Charles Hannum.
     60  1.1     pk  *
     61  1.1     pk  * Converted to SUN ie driver by Charles D. Cranor,
     62  1.1     pk  *		October 1994, January 1995.
     63  1.1     pk  * This sun version based on i386 version 1.30.
     64  1.1     pk  */
     65  1.1     pk 
     66  1.1     pk /*
     67  1.1     pk  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
     68  1.1     pk  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
     69  1.1     pk  * SUN, making life difficult.  Programming this chip is mostly the same,
     70  1.1     pk  * but certain details differ from system to system.  This driver is
     71  1.1     pk  * written so that different "ie" interfaces can be controled by the same
     72  1.1     pk  * driver.
     73  1.1     pk  */
     74  1.1     pk 
     75  1.1     pk /*
     76  1.1     pk Mode of operation:
     77  1.1     pk 
     78  1.1     pk    We run the 82586 in a standard Ethernet mode.  We keep NFRAMES
     79  1.1     pk    received frame descriptors around for the receiver to use, and
     80  1.1     pk    NRXBUF associated receive buffer descriptors, both in a circular
     81  1.1     pk    list.  Whenever a frame is received, we rotate both lists as
     82  1.1     pk    necessary.  (The 586 treats both lists as a simple queue.)  We also
     83  1.1     pk    keep a transmit command around so that packets can be sent off
     84  1.1     pk    quickly.
     85  1.1     pk 
     86  1.1     pk    We configure the adapter in AL-LOC = 1 mode, which means that the
     87  1.1     pk    Ethernet/802.3 MAC header is placed at the beginning of the receive
     88  1.1     pk    buffer rather than being split off into various fields in the RFD.
     89  1.1     pk    This also means that we must include this header in the transmit
     90  1.1     pk    buffer as well.
     91  1.1     pk 
     92  1.1     pk    By convention, all transmit commands, and only transmit commands,
     93  1.1     pk    shall have the I (IE_CMD_INTR) bit set in the command.  This way,
     94  1.1     pk    when an interrupt arrives at ieintr(), it is immediately possible
     95  1.1     pk    to tell what precisely caused it.  ANY OTHER command-sending
     96  1.1     pk    routines should run at splnet(), and should post an acknowledgement
     97  1.1     pk    to every interrupt they generate.
     98  1.1     pk 
     99  1.6     pk    To save the expense of shipping a command to 82586 every time we
    100  1.6     pk    want to send a frame, we use a linked list of commands consisting
    101  1.6     pk    of alternate XMIT and NOP commands. The links of these elements
    102  1.6     pk    are manipulated (in iexmit()) such that the NOP command loops back
    103  1.6     pk    to itself whenever the following XMIT command is not yet ready to
    104  1.6     pk    go. Whenever an XMIT is ready, the preceding NOP link is pointed
    105  1.6     pk    at it, while its own link field points to the following NOP command.
    106  1.6     pk    Thus, a single transmit command sets off an interlocked traversal
    107  1.6     pk    of the xmit command chain, with the host processor in control of
    108  1.6     pk    the synchronization.
    109  1.1     pk */
    110  1.1     pk 
    111  1.1     pk #include "bpfilter.h"
    112  1.1     pk 
    113  1.1     pk #include <sys/param.h>
    114  1.1     pk #include <sys/systm.h>
    115  1.1     pk #include <sys/mbuf.h>
    116  1.1     pk #include <sys/buf.h>
    117  1.1     pk #include <sys/protosw.h>
    118  1.1     pk #include <sys/socket.h>
    119  1.1     pk #include <sys/ioctl.h>
    120  1.1     pk #include <sys/errno.h>
    121  1.1     pk #include <sys/syslog.h>
    122  1.1     pk #include <sys/device.h>
    123  1.1     pk 
    124  1.1     pk #include <net/if.h>
    125  1.7     pk #include <net/if_dl.h>
    126  1.1     pk #include <net/if_types.h>
    127  1.7     pk #include <net/if_media.h>
    128  1.1     pk #include <net/if_ether.h>
    129  1.1     pk 
    130  1.1     pk #if NBPFILTER > 0
    131  1.1     pk #include <net/bpf.h>
    132  1.1     pk #include <net/bpfdesc.h>
    133  1.1     pk #endif
    134  1.1     pk 
    135  1.1     pk #ifdef INET
    136  1.1     pk #include <netinet/in.h>
    137  1.1     pk #include <netinet/in_systm.h>
    138  1.1     pk #include <netinet/in_var.h>
    139  1.1     pk #include <netinet/ip.h>
    140  1.1     pk #include <netinet/if_inarp.h>
    141  1.1     pk #endif
    142  1.1     pk 
    143  1.1     pk #ifdef NS
    144  1.1     pk #include <netns/ns.h>
    145  1.1     pk #include <netns/ns_if.h>
    146  1.1     pk #endif
    147  1.1     pk 
    148  1.5     pk #include <machine/bus.h>
    149  1.5     pk 
    150  1.1     pk #include <dev/ic/i82586reg.h>
    151  1.1     pk #include <dev/ic/i82586var.h>
    152  1.1     pk 
    153  1.7     pk void 		i82586_watchdog	__P((struct ifnet *));
    154  1.7     pk int 		i82586_init 	__P((struct ie_softc *));
    155  1.7     pk int 		i82586_ioctl 	__P((struct ifnet *, u_long, caddr_t));
    156  1.7     pk void 		i82586_start 	__P((struct ifnet *));
    157  1.7     pk int 		i82586_setupram __P((struct ie_softc *));
    158  1.7     pk 
    159  1.7     pk void 		i82586_rint 	__P((struct ie_softc *));
    160  1.7     pk void 		i82586_tint 	__P((struct ie_softc *));
    161  1.7     pk 
    162  1.7     pk int     	i82586_mediachange 	__P((struct ifnet *));
    163  1.7     pk void    	i82586_mediastatus 	__P((struct ifnet *,
    164  1.7     pk 						struct ifmediareq *));
    165  1.7     pk 
    166  1.7     pk static void 	ie_readframe		__P((struct ie_softc *, int));
    167  1.7     pk static void 	ie_drop_packet_buffer 	__P((struct ie_softc *));
    168  1.7     pk static int 	command_and_wait 	__P((struct ie_softc *, int,
    169  1.7     pk     						void volatile *, int));
    170  1.7     pk static struct mbuf *ieget 		__P((struct ie_softc *,
    171  1.7     pk 		      				struct ether_header *, int *));
    172  1.7     pk static void 	setup_bufs 		__P((struct ie_softc *));
    173  1.7     pk static int 	mc_setup 		__P((struct ie_softc *, void *));
    174  1.7     pk static void 	mc_reset 		__P((struct ie_softc *));
    175  1.7     pk 
    176  1.7     pk static __inline__ int 	ether_equal 	__P((u_char *, u_char *));
    177  1.7     pk static __inline__ void 	ie_ack 		__P((struct ie_softc *, u_int));
    178  1.7     pk static __inline__ void 	ie_setup_config __P((volatile struct ie_config_cmd *,
    179  1.7     pk 						  int, int));
    180  1.7     pk static __inline__ int 	check_eh 	__P((struct ie_softc *,
    181  1.7     pk 						struct ether_header *, int *));
    182  1.7     pk static __inline__ int 	ie_buflen 	__P((struct ie_softc *, int));
    183  1.7     pk static __inline__ int 	ie_packet_len 	__P((struct ie_softc *));
    184  1.7     pk static __inline__ void 	iexmit 		__P((struct ie_softc *));
    185  1.7     pk static void 		i82586_start_transceiver
    186  1.7     pk 					__P((struct ie_softc *));
    187  1.7     pk 
    188  1.7     pk static void 	run_tdr		__P((struct ie_softc *, struct ie_tdr_cmd *));
    189  1.7     pk static void 	iestop 		__P((struct ie_softc *));
    190  1.7     pk 
    191  1.7     pk #ifdef I82586_DEBUG
    192  1.7     pk void 		print_rbd 	__P((volatile struct ie_recv_buf_desc *));
    193  1.7     pk 
    194  1.7     pk int in_i82586_rint = 0;
    195  1.7     pk int in_i82586_tint = 0;
    196  1.7     pk int spur_intr = 0;
    197  1.7     pk 
    198  1.7     pk #endif
    199  1.1     pk 
    200  1.1     pk /*
    201  1.1     pk  * Address generation macros:
    202  1.1     pk  *   MK_24 = KVA -> 24 bit address in native byte order
    203  1.1     pk  *   MK_16 = KVA -> 16 bit address in INTEL byte order
    204  1.1     pk  *   ST_24 = store a 24 bit address in native byte order to INTEL byte order
    205  1.1     pk  */
    206  1.1     pk #define MK_24(base, ptr) ((caddr_t)((u_long)ptr - (u_long)base))
    207  1.1     pk 
    208  1.1     pk #if BYTE_ORDER == BIG_ENDIAN
    209  1.1     pk #define XSWAP(y)	( ((y) >> 8) | ((y) << 8) )
    210  1.1     pk #define SWAP(x)		({u_short _z=(x); (u_short)XSWAP(_z);})
    211  1.1     pk 
    212  1.1     pk #define MK_16(base, ptr) SWAP((u_short)( ((u_long)(ptr)) - ((u_long)(base)) ))
    213  1.1     pk #define ST_24(to, from) { \
    214  1.1     pk 	u_long fval = (u_long)(from); \
    215  1.1     pk 	u_char *t = (u_char *)&(to), *f = (u_char *)&fval; \
    216  1.1     pk 	t[0] = f[3]; t[1] = f[2]; t[2] = f[1]; /*t[3] = f[0] ;*/ \
    217  1.1     pk }
    218  1.1     pk #else
    219  1.1     pk #define SWAP(x) x
    220  1.1     pk #define MK_16(base, ptr) ((u_short)(u_long)MK_24(base, ptr))
    221  1.1     pk #define ST_24(to, from) {to = (from);}
    222  1.1     pk #endif
    223  1.1     pk 
    224  1.1     pk /*
    225  1.1     pk  * Here are a few useful functions.  We could have done these as macros, but
    226  1.1     pk  * since we have the inline facility, it makes sense to use that instead.
    227  1.1     pk  */
    228  1.7     pk static __inline__ void
    229  1.1     pk ie_setup_config(cmd, promiscuous, manchester)
    230  1.1     pk 	volatile struct ie_config_cmd *cmd;
    231  1.1     pk 	int promiscuous, manchester;
    232  1.1     pk {
    233  1.1     pk 
    234  1.1     pk 	cmd->ie_config_count = 0x0c;
    235  1.1     pk 	cmd->ie_fifo = 8;
    236  1.1     pk 	cmd->ie_save_bad = 0x40;
    237  1.1     pk 	cmd->ie_addr_len = 0x2e;
    238  1.1     pk 	cmd->ie_priority = 0;
    239  1.1     pk 	cmd->ie_ifs = 0x60;
    240  1.1     pk 	cmd->ie_slot_low = 0;
    241  1.1     pk 	cmd->ie_slot_high = 0xf2;
    242  1.1     pk 	cmd->ie_promisc = !!promiscuous | manchester << 2;
    243  1.1     pk 	cmd->ie_crs_cdt = 0;
    244  1.1     pk 	cmd->ie_min_len = 64;
    245  1.1     pk 	cmd->ie_junk = 0xff;
    246  1.1     pk }
    247  1.1     pk 
    248  1.7     pk static __inline__ void
    249  1.1     pk ie_ack(sc, mask)
    250  1.1     pk 	struct ie_softc *sc;
    251  1.1     pk 	u_int mask;	/* in native byte-order */
    252  1.1     pk {
    253  1.1     pk 	volatile struct ie_sys_ctl_block *scb = sc->scb;
    254  1.1     pk 
    255  1.6     pk 	bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
    256  1.1     pk 	command_and_wait(sc, SWAP(scb->ie_status) & mask, 0, 0);
    257  1.1     pk }
    258  1.1     pk 
    259  1.1     pk void
    260  1.7     pk ie_attach(sc, name, etheraddr, media, nmedia, defmedia)
    261  1.1     pk 	struct ie_softc *sc;
    262  1.1     pk 	char *name;
    263  1.1     pk 	u_int8_t *etheraddr;
    264  1.7     pk         int *media, nmedia, defmedia;
    265  1.1     pk {
    266  1.7     pk 	int i;
    267  1.1     pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    268  1.1     pk 
    269  1.7     pk 	if (i82586_setupram(sc) == 0) { /* XXX - ISA version? */
    270  1.1     pk 		printf(": RAM CONFIG FAILED!\n");
    271  1.1     pk 		/* XXX should reclaim resources? */
    272  1.1     pk 		return;
    273  1.1     pk 	}
    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.7     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.1     pk  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    308  1.1     pk  * an interrupt after a transmit has been started on it.
    309  1.1     pk  */
    310  1.1     pk void
    311  1.7     pk i82586_watchdog(ifp)
    312  1.1     pk 	struct ifnet *ifp;
    313  1.1     pk {
    314  1.1     pk 	struct ie_softc *sc = ifp->if_softc;
    315  1.1     pk 
    316  1.1     pk 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    317  1.1     pk 	++ifp->if_oerrors;
    318  1.1     pk 
    319  1.7     pk 	i82586_reset(sc, 1);
    320  1.1     pk }
    321  1.1     pk 
    322  1.1     pk /*
    323  1.1     pk  * What to do upon receipt of an interrupt.
    324  1.1     pk  */
    325  1.1     pk int
    326  1.1     pk ieintr(v)
    327  1.6     pk 	void *v;
    328  1.1     pk {
    329  1.1     pk 	struct ie_softc *sc = v;
    330  1.6     pk 	u_int status;
    331  1.1     pk 
    332  1.1     pk         /*
    333  1.1     pk          * Implementation dependent interrupt handling.
    334  1.1     pk          */
    335  1.1     pk 	if (sc->intrhook)
    336  1.7     pk 		(sc->intrhook)(sc, INTR_ENTER);
    337  1.1     pk 
    338  1.6     pk 	bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
    339  1.6     pk 	status = SWAP(sc->scb->ie_status) & IE_ST_WHENCE;
    340  1.6     pk 
    341  1.6     pk 	if (status == 0) {
    342  1.7     pk #ifdef I82586_DEBUG
    343  1.7     pk 		if ((++spur_intr % 25) == 0)
    344  1.7     pk 		    printf("%s: ieintr; %d spurious interrupts\n",
    345  1.7     pk 					sc->sc_dev.dv_xname, spur_intr);
    346  1.7     pk #endif
    347  1.7     pk 		if (sc->intrhook)
    348  1.7     pk 			(sc->intrhook)(sc, INTR_EXIT);
    349  1.7     pk 
    350  1.6     pk 		return (0);
    351  1.6     pk 	}
    352  1.7     pk 
    353  1.1     pk loop:
    354  1.1     pk 	/* Ack interrupts FIRST in case we receive more during the ISR. */
    355  1.6     pk 	ie_ack(sc, status);
    356  1.1     pk 
    357  1.1     pk 	if (status & (IE_ST_FR | IE_ST_RNR)) {
    358  1.7     pk #ifdef I82586_DEBUG
    359  1.7     pk 		in_i82586_rint++;
    360  1.1     pk 		if (sc->sc_debug & IED_RINT)
    361  1.1     pk 			printf("%s: rint\n", sc->sc_dev.dv_xname);
    362  1.1     pk #endif
    363  1.7     pk 		i82586_rint(sc);
    364  1.7     pk #ifdef I82586_DEBUG
    365  1.7     pk 		in_i82586_rint--;
    366  1.1     pk #endif
    367  1.1     pk 	}
    368  1.1     pk 
    369  1.1     pk 	if (status & IE_ST_CX) {
    370  1.7     pk #ifdef I82586_DEBUG
    371  1.7     pk 		in_i82586_tint++;
    372  1.1     pk 		if (sc->sc_debug & IED_TINT)
    373  1.1     pk 			printf("%s: tint\n", sc->sc_dev.dv_xname);
    374  1.1     pk #endif
    375  1.7     pk 		i82586_tint(sc);
    376  1.7     pk #ifdef I82586_DEBUG
    377  1.7     pk 		in_i82586_tint--;
    378  1.1     pk #endif
    379  1.1     pk 	}
    380  1.1     pk 
    381  1.1     pk 	if (status & IE_ST_RNR) {
    382  1.6     pk 		printf("%s: receiver not ready; status=0x%x\n",
    383  1.6     pk 			sc->sc_dev.dv_xname, status);
    384  1.1     pk 		sc->sc_ethercom.ec_if.if_ierrors++;
    385  1.7     pk 
    386  1.7     pk 		i82586_reset(sc, 1);
    387  1.7     pk 
    388  1.7     pk 		if (sc->intrhook)
    389  1.7     pk 			(sc->intrhook)(sc, INTR_EXIT);
    390  1.7     pk 
    391  1.1     pk 		return (1);
    392  1.1     pk 	}
    393  1.1     pk 
    394  1.7     pk #ifdef I82586_DEBUG
    395  1.1     pk 	if ((status & IE_ST_CNA) && (sc->sc_debug & IED_CNA))
    396  1.6     pk 		printf("%s: cna; status=0x%x\n", sc->sc_dev.dv_xname, status);
    397  1.1     pk #endif
    398  1.7     pk 	if (sc->intrhook)
    399  1.7     pk 		(sc->intrhook)(sc, INTR_LOOP);
    400  1.1     pk 
    401  1.5     pk 	bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
    402  1.6     pk 	status = SWAP(sc->scb->ie_status) & IE_ST_WHENCE;
    403  1.6     pk 	if (status != 0)
    404  1.1     pk 		goto loop;
    405  1.1     pk 
    406  1.7     pk 	if (sc->intrhook)
    407  1.7     pk 		(sc->intrhook)(sc, INTR_EXIT);
    408  1.7     pk 
    409  1.1     pk 	return (1);
    410  1.1     pk }
    411  1.1     pk 
    412  1.1     pk /*
    413  1.1     pk  * Process a received-frame interrupt.
    414  1.1     pk  */
    415  1.1     pk void
    416  1.7     pk i82586_rint(sc)
    417  1.1     pk 	struct ie_softc *sc;
    418  1.1     pk {
    419  1.1     pk 	volatile struct ie_sys_ctl_block *scb = sc->scb;
    420  1.1     pk 	int i, status;
    421  1.1     pk 	static int timesthru = 1024;
    422  1.1     pk 
    423  1.1     pk 	i = sc->rfhead;
    424  1.1     pk 	for (;;) {
    425  1.6     pk 		bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
    426  1.1     pk 		status = SWAP(sc->rframes[i]->ie_fd_status);
    427  1.1     pk 
    428  1.1     pk 		if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
    429  1.3     pk 			if (--timesthru == 0) {
    430  1.1     pk 				sc->sc_ethercom.ec_if.if_ierrors +=
    431  1.1     pk 				    SWAP(scb->ie_err_crc) +
    432  1.1     pk 				    SWAP(scb->ie_err_align) +
    433  1.1     pk 				    SWAP(scb->ie_err_resource) +
    434  1.1     pk 				    SWAP(scb->ie_err_overrun);
    435  1.1     pk 				scb->ie_err_crc = scb->ie_err_align =
    436  1.1     pk 				scb->ie_err_resource = scb->ie_err_overrun =
    437  1.1     pk 				    SWAP(0);
    438  1.1     pk 				timesthru = 1024;
    439  1.1     pk 			}
    440  1.1     pk 			ie_readframe(sc, i);
    441  1.1     pk 		} else {
    442  1.1     pk 			if ((status & IE_FD_RNR) != 0 &&
    443  1.1     pk 			    (SWAP(scb->ie_status) & IE_RU_READY) == 0) {
    444  1.6     pk 				i82586_start_transceiver(sc);
    445  1.1     pk 			}
    446  1.1     pk 			break;
    447  1.1     pk 		}
    448  1.1     pk 		i = (i + 1) % sc->nframes;
    449  1.1     pk 	}
    450  1.1     pk }
    451  1.1     pk 
    452  1.1     pk /*
    453  1.1     pk  * Process a command-complete interrupt.  These are only generated by the
    454  1.1     pk  * transmission of frames.  This routine is deceptively simple, since most of
    455  1.7     pk  * the real work is done by i82586_start().
    456  1.1     pk  */
    457  1.1     pk void
    458  1.7     pk i82586_tint(sc)
    459  1.1     pk 	struct ie_softc *sc;
    460  1.1     pk {
    461  1.5     pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    462  1.1     pk 	int status;
    463  1.1     pk 
    464  1.5     pk 	ifp->if_timer = 0;
    465  1.5     pk 	ifp->if_flags &= ~IFF_OACTIVE;
    466  1.1     pk 
    467  1.7     pk #ifdef I82586_DEBUG
    468  1.6     pk 	if (sc->xmit_busy <= 0) {
    469  1.7     pk 	    printf("i82586_tint: WEIRD: xmit_busy = %d, xctail = %d, xchead=%d\n",
    470  1.6     pk 		sc->xmit_busy, sc->xctail, sc->xchead);
    471  1.6     pk 		return;
    472  1.6     pk 	}
    473  1.6     pk #endif
    474  1.6     pk 
    475  1.1     pk 	status = SWAP(sc->xmit_cmds[sc->xctail]->ie_xmit_status);
    476  1.1     pk 
    477  1.6     pk 	if ((status & IE_STAT_COMPL) == 0 || (status & IE_STAT_BUSY)) {
    478  1.7     pk 	    printf("i82586_tint: command still busy; status=0x%x; tail=%d\n",
    479  1.6     pk 			status, sc->xctail);
    480  1.7     pk 	    printf("iestatus = 0x%x\n", SWAP(sc->scb->ie_status));
    481  1.6     pk 	}
    482  1.1     pk 
    483  1.1     pk 	if (status & IE_STAT_OK) {
    484  1.5     pk 		ifp->if_opackets++;
    485  1.5     pk 		ifp->if_collisions += (status & IE_XS_MAXCOLL);
    486  1.5     pk 	} else {
    487  1.5     pk 		ifp->if_oerrors++;
    488  1.5     pk 		/*
    489  1.5     pk 		 * Check SQE and DEFERRED?
    490  1.5     pk 		 * What if more than one bit is set?
    491  1.5     pk 		 */
    492  1.5     pk 		if (status & IE_STAT_ABORT)
    493  1.5     pk 			printf("%s: send aborted\n", sc->sc_dev.dv_xname);
    494  1.5     pk 		else if (status & IE_XS_NOCARRIER)
    495  1.5     pk 			printf("%s: no carrier\n", sc->sc_dev.dv_xname);
    496  1.5     pk 		else if (status & IE_XS_LOSTCTS)
    497  1.5     pk 			printf("%s: lost CTS\n", sc->sc_dev.dv_xname);
    498  1.5     pk 		else if (status & IE_XS_UNDERRUN)
    499  1.5     pk 			printf("%s: DMA underrun\n", sc->sc_dev.dv_xname);
    500  1.5     pk 		else if (status & IE_XS_EXCMAX) {
    501  1.5     pk 			printf("%s: too many collisions\n",
    502  1.5     pk 				sc->sc_dev.dv_xname);
    503  1.5     pk 			sc->sc_ethercom.ec_if.if_collisions += 16;
    504  1.5     pk 		}
    505  1.1     pk 	}
    506  1.1     pk 
    507  1.1     pk 	/*
    508  1.1     pk 	 * If multicast addresses were added or deleted while transmitting,
    509  1.1     pk 	 * mc_reset() set the want_mcsetup flag indicating that we should do
    510  1.1     pk 	 * it.
    511  1.1     pk 	 */
    512  1.1     pk 	if (sc->want_mcsetup) {
    513  1.1     pk 		mc_setup(sc, (caddr_t)sc->xmit_cbuffs[sc->xctail]);
    514  1.1     pk 		sc->want_mcsetup = 0;
    515  1.1     pk 	}
    516  1.1     pk 
    517  1.1     pk 	/* Done with the buffer. */
    518  1.3     pk 	sc->xmit_busy--;
    519  1.1     pk 	sc->xctail = (sc->xctail + 1) % NTXBUF;
    520  1.1     pk 
    521  1.3     pk 	/* Start the next packet, if any, transmitting. */
    522  1.3     pk 	if (sc->xmit_busy > 0)
    523  1.3     pk 		iexmit(sc);
    524  1.3     pk 
    525  1.7     pk 	i82586_start(ifp);
    526  1.1     pk }
    527  1.1     pk 
    528  1.1     pk /*
    529  1.1     pk  * Compare two Ether/802 addresses for equality, inlined and unrolled for
    530  1.3     pk  * speed.
    531  1.1     pk  */
    532  1.7     pk static __inline__ int
    533  1.1     pk ether_equal(one, two)
    534  1.1     pk 	u_char *one, *two;
    535  1.1     pk {
    536  1.1     pk 
    537  1.1     pk 	if (one[5] != two[5] || one[4] != two[4] || one[3] != two[3] ||
    538  1.1     pk 	    one[2] != two[2] || one[1] != two[1] || one[0] != two[0])
    539  1.1     pk 		return 0;
    540  1.1     pk 	return 1;
    541  1.1     pk }
    542  1.1     pk 
    543  1.1     pk /*
    544  1.1     pk  * Check for a valid address.  to_bpf is filled in with one of the following:
    545  1.1     pk  *   0 -> BPF doesn't get this packet
    546  1.1     pk  *   1 -> BPF does get this packet
    547  1.1     pk  *   2 -> BPF does get this packet, but we don't
    548  1.1     pk  * Return value is true if the packet is for us, and false otherwise.
    549  1.1     pk  *
    550  1.1     pk  * This routine is a mess, but it's also critical that it be as fast
    551  1.1     pk  * as possible.  It could be made cleaner if we can assume that the
    552  1.1     pk  * only client which will fiddle with IFF_PROMISC is BPF.  This is
    553  1.1     pk  * probably a good assumption, but we do not make it here.  (Yet.)
    554  1.1     pk  */
    555  1.7     pk static __inline__ int
    556  1.1     pk check_eh(sc, eh, to_bpf)
    557  1.1     pk 	struct ie_softc *sc;
    558  1.1     pk 	struct ether_header *eh;
    559  1.1     pk 	int *to_bpf;
    560  1.1     pk {
    561  1.1     pk 	struct ifnet *ifp;
    562  1.1     pk 	int i;
    563  1.1     pk 
    564  1.1     pk 	ifp = &sc->sc_ethercom.ec_if;
    565  1.1     pk 
    566  1.1     pk 	switch(sc->promisc) {
    567  1.1     pk 	case IFF_ALLMULTI:
    568  1.1     pk 		/*
    569  1.1     pk 		 * Receiving all multicasts, but no unicasts except those
    570  1.1     pk 		 * destined for us.
    571  1.1     pk 		 */
    572  1.1     pk #if NBPFILTER > 0
    573  1.1     pk 		/* BPF gets this packet if anybody cares */
    574  1.3     pk 		*to_bpf = (ifp->if_bpf != 0);
    575  1.1     pk #endif
    576  1.1     pk 		if (eh->ether_dhost[0] & 1)
    577  1.1     pk 			return 1;
    578  1.1     pk 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    579  1.1     pk 			return 1;
    580  1.1     pk 		return 0;
    581  1.1     pk 
    582  1.1     pk 	case IFF_PROMISC:
    583  1.1     pk 		/*
    584  1.1     pk 		 * Receiving all packets.  These need to be passed on to BPF.
    585  1.1     pk 		 */
    586  1.1     pk #if NBPFILTER > 0
    587  1.3     pk 		*to_bpf = (ifp->if_bpf != 0);
    588  1.1     pk #endif
    589  1.1     pk 		/* If for us, accept and hand up to BPF */
    590  1.1     pk 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    591  1.1     pk 			return 1;
    592  1.1     pk 
    593  1.1     pk #if NBPFILTER > 0
    594  1.1     pk 		if (*to_bpf)
    595  1.1     pk 			*to_bpf = 2; /* we don't need to see it */
    596  1.1     pk #endif
    597  1.1     pk 
    598  1.1     pk 		/*
    599  1.1     pk 		 * Not a multicast, so BPF wants to see it but we don't.
    600  1.1     pk 		 */
    601  1.3     pk 		if ((eh->ether_dhost[0] & 1) == 0)
    602  1.1     pk 			return 1;
    603  1.1     pk 
    604  1.1     pk 		/*
    605  1.1     pk 		 * If it's one of our multicast groups, accept it and pass it
    606  1.1     pk 		 * up.
    607  1.1     pk 		 */
    608  1.1     pk 		for (i = 0; i < sc->mcast_count; i++) {
    609  1.1     pk 			if (ether_equal(eh->ether_dhost,
    610  1.1     pk 					(u_char *)&sc->mcast_addrs[i])) {
    611  1.1     pk #if NBPFILTER > 0
    612  1.1     pk 				if (*to_bpf)
    613  1.1     pk 					*to_bpf = 1;
    614  1.1     pk #endif
    615  1.1     pk 				return 1;
    616  1.1     pk 			}
    617  1.1     pk 		}
    618  1.1     pk 		return 1;
    619  1.1     pk 
    620  1.1     pk 	case IFF_ALLMULTI | IFF_PROMISC:
    621  1.1     pk 		/*
    622  1.1     pk 		 * Acting as a multicast router, and BPF running at the same
    623  1.1     pk 		 * time.  Whew!  (Hope this is a fast machine...)
    624  1.1     pk 		 */
    625  1.1     pk #if NBPFILTER > 0
    626  1.3     pk 		*to_bpf = (ifp->if_bpf != 0);
    627  1.1     pk #endif
    628  1.1     pk 		/* We want to see multicasts. */
    629  1.1     pk 		if (eh->ether_dhost[0] & 1)
    630  1.1     pk 			return 1;
    631  1.1     pk 
    632  1.1     pk 		/* We want to see our own packets */
    633  1.1     pk 		if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
    634  1.1     pk 			return 1;
    635  1.1     pk 
    636  1.1     pk 		/* Anything else goes to BPF but nothing else. */
    637  1.1     pk #if NBPFILTER > 0
    638  1.1     pk 		if (*to_bpf)
    639  1.1     pk 			*to_bpf = 2;
    640  1.1     pk #endif
    641  1.1     pk 		return 1;
    642  1.1     pk 
    643  1.1     pk 	default:
    644  1.1     pk 		/*
    645  1.1     pk 		 * Only accept unicast packets destined for us, or multicasts
    646  1.1     pk 		 * for groups that we belong to.  For now, we assume that the
    647  1.1     pk 		 * '586 will only return packets that we asked it for.  This
    648  1.1     pk 		 * isn't strictly true (it uses hashing for the multicast
    649  1.1     pk 		 * filter), but it will do in this case, and we want to get
    650  1.1     pk 		 * out of here as quickly as possible.
    651  1.1     pk 		 */
    652  1.1     pk #if NBPFILTER > 0
    653  1.3     pk 		*to_bpf = (ifp->if_bpf != 0);
    654  1.1     pk #endif
    655  1.1     pk 		return 1;
    656  1.1     pk 	}
    657  1.1     pk 	return 0;
    658  1.1     pk }
    659  1.1     pk 
    660  1.1     pk /*
    661  1.1     pk  * We want to isolate the bits that have meaning...  This assumes that
    662  1.1     pk  * IE_RBUF_SIZE is an even power of two.  If somehow the act_len exceeds
    663  1.1     pk  * the size of the buffer, then we are screwed anyway.
    664  1.1     pk  */
    665  1.7     pk static __inline__ int
    666  1.1     pk ie_buflen(sc, head)
    667  1.1     pk 	struct ie_softc *sc;
    668  1.1     pk 	int head;
    669  1.1     pk {
    670  1.1     pk 
    671  1.1     pk 	return (SWAP(sc->rbuffs[head]->ie_rbd_actual)
    672  1.1     pk 		& (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1)));
    673  1.1     pk }
    674  1.1     pk 
    675  1.3     pk 
    676  1.7     pk static __inline__ int
    677  1.1     pk ie_packet_len(sc)
    678  1.1     pk 	struct ie_softc *sc;
    679  1.1     pk {
    680  1.1     pk 	int i;
    681  1.1     pk 	int head = sc->rbhead;
    682  1.1     pk 	int acc = 0;
    683  1.1     pk 	int oldhead = head;
    684  1.1     pk 
    685  1.1     pk 	do {
    686  1.5     pk 		bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
    687  1.3     pk 		i = SWAP(sc->rbuffs[head]->ie_rbd_actual);
    688  1.3     pk 		if ((i & IE_RBD_USED) == 0) {
    689  1.7     pk #ifdef I82586_DEBUG
    690  1.3     pk 			print_rbd(sc->rbuffs[head]);
    691  1.1     pk #endif
    692  1.1     pk 			log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
    693  1.1     pk 			    sc->sc_dev.dv_xname, sc->rbhead);
    694  1.7     pk 			i82586_reset(sc, 1);
    695  1.1     pk 			return -1;
    696  1.1     pk 		}
    697  1.1     pk 
    698  1.3     pk 		i = (i & IE_RBD_LAST) != 0;
    699  1.1     pk 
    700  1.1     pk 		acc += ie_buflen(sc, head);
    701  1.1     pk 		head = (head + 1) % sc->nrxbuf;
    702  1.3     pk 		if (oldhead == head) {
    703  1.1     pk 			printf("ie: packet len: looping: acc = %d (head=%d)\n",
    704  1.1     pk 				acc, head);
    705  1.7     pk 			i82586_reset(sc, 1);
    706  1.1     pk 			return -1;
    707  1.1     pk 		}
    708  1.1     pk 	} while (!i);
    709  1.1     pk 
    710  1.1     pk 	return acc;
    711  1.1     pk }
    712  1.1     pk 
    713  1.1     pk /*
    714  1.1     pk  * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
    715  1.6     pk  * command to the chip to be executed.
    716  1.1     pk  */
    717  1.7     pk static __inline__ void
    718  1.1     pk iexmit(sc)
    719  1.1     pk 	struct ie_softc *sc;
    720  1.1     pk {
    721  1.6     pk 	int cur, prev;
    722  1.6     pk 
    723  1.6     pk 	cur = sc->xctail;
    724  1.1     pk 
    725  1.7     pk #ifdef I82586_DEBUG
    726  1.3     pk 	if (sc->sc_debug & IED_XMIT)
    727  1.6     pk 		printf("%s: xmit buffer %d\n", sc->sc_dev.dv_xname, cur);
    728  1.1     pk #endif
    729  1.1     pk 
    730  1.6     pk 	sc->xmit_buffs[cur]->ie_xmit_flags |= SWAP(IE_XMIT_LAST);
    731  1.6     pk 	sc->xmit_buffs[cur]->ie_xmit_next = SWAP(0xffff);
    732  1.6     pk 	ST_24(sc->xmit_buffs[cur]->ie_xmit_buf,
    733  1.6     pk 	      MK_24(sc->sc_iobase, sc->xmit_cbuffs[cur]));
    734  1.6     pk 
    735  1.6     pk 	sc->xmit_cmds[cur]->ie_xmit_desc =
    736  1.6     pk 		MK_16(sc->sc_maddr, sc->xmit_buffs[cur]);
    737  1.6     pk 
    738  1.6     pk 	sc->xmit_cmds[cur]->ie_xmit_status = SWAP(0);
    739  1.6     pk 
    740  1.7     pk 	if (sc->do_xmitnopchain) {
    741  1.6     pk 		/* Gate this XMIT to following NOP */
    742  1.6     pk 		sc->xmit_cmds[cur]->com.ie_cmd_link =
    743  1.6     pk 			MK_16(sc->sc_maddr, sc->nop_cmds[cur]);
    744  1.6     pk 		sc->xmit_cmds[cur]->com.ie_cmd_cmd =
    745  1.6     pk 			SWAP(IE_CMD_XMIT | IE_CMD_INTR);
    746  1.6     pk 
    747  1.6     pk 		/* Loopback at following NOP */
    748  1.6     pk 		sc->nop_cmds[cur]->ie_cmd_status = SWAP(0);
    749  1.6     pk 		sc->nop_cmds[cur]->ie_cmd_link =
    750  1.6     pk 			MK_16(sc->sc_maddr, sc->nop_cmds[cur]);
    751  1.6     pk 
    752  1.6     pk 		/* Gate preceding NOP to this XMIT command */
    753  1.6     pk 		prev = (cur + NTXBUF - 1) % NTXBUF;
    754  1.6     pk 		sc->nop_cmds[prev]->ie_cmd_status = SWAP(0);
    755  1.6     pk 		sc->nop_cmds[prev]->ie_cmd_link =
    756  1.6     pk 			MK_16(sc->sc_maddr, sc->xmit_cmds[cur]);
    757  1.6     pk 		bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_WRITE);
    758  1.6     pk 		if ((SWAP(sc->scb->ie_status) & IE_CU_ACTIVE) == 0) {
    759  1.6     pk 			printf("iexmit: CU not active\n");
    760  1.6     pk 			i82586_start_transceiver(sc);
    761  1.6     pk 		}
    762  1.6     pk 	} else {
    763  1.6     pk 		sc->xmit_cmds[cur]->com.ie_cmd_link = SWAP(0xffff);
    764  1.6     pk 		sc->xmit_cmds[cur]->com.ie_cmd_cmd =
    765  1.6     pk 			SWAP(IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST);
    766  1.6     pk 
    767  1.6     pk 		sc->scb->ie_command_list =
    768  1.6     pk 			MK_16(sc->sc_maddr, sc->xmit_cmds[cur]);
    769  1.6     pk 
    770  1.6     pk 		bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_WRITE);
    771  1.6     pk 		if (command_and_wait(sc, IE_CU_START, 0, 0))
    772  1.6     pk 			printf("%s: iexmit: start xmit command timed out\n",
    773  1.6     pk 				sc->sc_dev.dv_xname);
    774  1.6     pk 	}
    775  1.1     pk 
    776  1.1     pk 	sc->sc_ethercom.ec_if.if_timer = 5;
    777  1.1     pk }
    778  1.1     pk 
    779  1.1     pk /*
    780  1.1     pk  * Read data off the interface, and turn it into an mbuf chain.
    781  1.1     pk  *
    782  1.1     pk  * This code is DRAMATICALLY different from the previous version; this
    783  1.1     pk  * version tries to allocate the entire mbuf chain up front, given the
    784  1.1     pk  * length of the data available.  This enables us to allocate mbuf
    785  1.1     pk  * clusters in many situations where before we would have had a long
    786  1.1     pk  * chain of partially-full mbufs.  This should help to speed up the
    787  1.1     pk  * operation considerably.  (Provided that it works, of course.)
    788  1.1     pk  */
    789  1.3     pk struct mbuf *
    790  1.3     pk ieget(sc, ehp, to_bpf)
    791  1.1     pk 	struct ie_softc *sc;
    792  1.1     pk 	struct ether_header *ehp;
    793  1.1     pk 	int *to_bpf;
    794  1.1     pk {
    795  1.3     pk 	struct mbuf *top, **mp, *m;
    796  1.3     pk 	int len, totlen, resid;
    797  1.3     pk 	int thisrboff, thismboff;
    798  1.1     pk 	int head;
    799  1.1     pk 
    800  1.1     pk 	totlen = ie_packet_len(sc);
    801  1.1     pk 	if (totlen <= 0)
    802  1.3     pk 		return 0;
    803  1.1     pk 
    804  1.3     pk 	head = sc->rbhead;
    805  1.1     pk 
    806  1.1     pk 	/*
    807  1.1     pk 	 * Snarf the Ethernet header.
    808  1.1     pk 	 */
    809  1.5     pk 	(sc->memcopy)((caddr_t)sc->cbuffs[head], (caddr_t)ehp, sizeof *ehp);
    810  1.1     pk 
    811  1.1     pk 	/*
    812  1.1     pk 	 * As quickly as possible, check if this packet is for us.
    813  1.1     pk 	 * If not, don't waste a single cycle copying the rest of the
    814  1.1     pk 	 * packet in.
    815  1.1     pk 	 * This is only a consideration when FILTER is defined; i.e., when
    816  1.1     pk 	 * we are either running BPF or doing multicasting.
    817  1.1     pk 	 */
    818  1.1     pk 	if (!check_eh(sc, ehp, to_bpf)) {
    819  1.1     pk 		/* just this case, it's not an error */
    820  1.1     pk 		sc->sc_ethercom.ec_if.if_ierrors--;
    821  1.3     pk 		return 0;
    822  1.1     pk 	}
    823  1.1     pk 
    824  1.3     pk 	resid = totlen -= (thisrboff = sizeof *ehp);
    825  1.1     pk 
    826  1.3     pk 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    827  1.3     pk 	if (m == 0)
    828  1.3     pk 		return 0;
    829  1.1     pk 	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
    830  1.3     pk 	m->m_pkthdr.len = totlen;
    831  1.3     pk 	len = MHLEN;
    832  1.1     pk 	top = 0;
    833  1.3     pk 	mp = &top;
    834  1.1     pk 
    835  1.1     pk 	/*
    836  1.1     pk 	 * This loop goes through and allocates mbufs for all the data we will
    837  1.1     pk 	 * be copying in.  It does not actually do the copying yet.
    838  1.1     pk 	 */
    839  1.3     pk 	while (totlen > 0) {
    840  1.1     pk 		if (top) {
    841  1.1     pk 			MGET(m, M_DONTWAIT, MT_DATA);
    842  1.3     pk 			if (m == 0) {
    843  1.1     pk 				m_freem(top);
    844  1.3     pk 				return 0;
    845  1.1     pk 			}
    846  1.3     pk 			len = MLEN;
    847  1.1     pk 		}
    848  1.3     pk 		if (totlen >= MINCLSIZE) {
    849  1.1     pk 			MCLGET(m, M_DONTWAIT);
    850  1.3     pk 			if ((m->m_flags & M_EXT) == 0) {
    851  1.3     pk 				m_freem(top);
    852  1.3     pk 				return 0;
    853  1.1     pk 			}
    854  1.3     pk 			len = MCLBYTES;
    855  1.1     pk 		}
    856  1.3     pk 		m->m_len = len = min(totlen, len);
    857  1.3     pk 		totlen -= len;
    858  1.3     pk 		*mp = m;
    859  1.3     pk 		mp = &m->m_next;
    860  1.3     pk 	}
    861  1.1     pk 
    862  1.1     pk 	m = top;
    863  1.1     pk 	thismboff = 0;
    864  1.1     pk 
    865  1.1     pk 	/*
    866  1.1     pk 	 * Now we take the mbuf chain (hopefully only one mbuf most of the
    867  1.3     pk 	 * time) and stuff the data into it.  There are no possible failures at
    868  1.3     pk 	 * or after this point.
    869  1.1     pk 	 */
    870  1.3     pk 	while (resid > 0) {
    871  1.3     pk 		int thisrblen = ie_buflen(sc, head) - thisrboff,
    872  1.3     pk 		    thismblen = m->m_len - thismboff;
    873  1.3     pk 		len = min(thisrblen, thismblen);
    874  1.3     pk 
    875  1.5     pk 		(sc->memcopy)((caddr_t)(sc->cbuffs[head] + thisrboff),
    876  1.5     pk 			       mtod(m, caddr_t) + thismboff, (u_int)len);
    877  1.3     pk 		resid -= len;
    878  1.1     pk 
    879  1.3     pk 		if (len == thismblen) {
    880  1.1     pk 			m = m->m_next;
    881  1.3     pk 			thismboff = 0;
    882  1.3     pk 		} else
    883  1.3     pk 			thismboff += len;
    884  1.3     pk 
    885  1.3     pk 		if (len == thisrblen) {
    886  1.3     pk 			head = (head + 1) % sc->nrxbuf;
    887  1.3     pk 			thisrboff = 0;
    888  1.3     pk 		} else
    889  1.3     pk 			thisrboff += len;
    890  1.1     pk 	}
    891  1.1     pk 
    892  1.1     pk 	/*
    893  1.1     pk 	 * Unless something changed strangely while we were doing the copy, we
    894  1.1     pk 	 * have now copied everything in from the shared memory.
    895  1.1     pk 	 * This means that we are done.
    896  1.1     pk 	 */
    897  1.3     pk 	return top;
    898  1.1     pk }
    899  1.1     pk 
    900  1.1     pk /*
    901  1.1     pk  * Read frame NUM from unit UNIT (pre-cached as IE).
    902  1.1     pk  *
    903  1.1     pk  * This routine reads the RFD at NUM, and copies in the buffers from the list
    904  1.1     pk  * of RBD, then rotates the RBD and RFD lists so that the receiver doesn't
    905  1.1     pk  * start complaining.  Trailers are DROPPED---there's no point in wasting time
    906  1.1     pk  * on confusing code to deal with them.  Hopefully, this machine will never ARP
    907  1.1     pk  * for trailers anyway.
    908  1.1     pk  */
    909  1.1     pk static void
    910  1.1     pk ie_readframe(sc, num)
    911  1.1     pk 	struct ie_softc *sc;
    912  1.1     pk 	int num;			/* frame number to read */
    913  1.1     pk {
    914  1.1     pk 	int status;
    915  1.1     pk 	struct mbuf *m = 0;
    916  1.1     pk 	struct ether_header eh;
    917  1.1     pk #if NBPFILTER > 0
    918  1.1     pk 	int bpf_gets_it = 0;
    919  1.1     pk #endif
    920  1.1     pk 
    921  1.1     pk 	status = SWAP(sc->rframes[num]->ie_fd_status);
    922  1.1     pk 
    923  1.1     pk 	/* Immediately advance the RFD list, since we have copied ours now. */
    924  1.1     pk 	sc->rframes[num]->ie_fd_status = SWAP(0);
    925  1.1     pk 	sc->rframes[num]->ie_fd_last |= SWAP(IE_FD_LAST);
    926  1.1     pk 	sc->rframes[sc->rftail]->ie_fd_last &= ~SWAP(IE_FD_LAST);
    927  1.1     pk 	sc->rftail = (sc->rftail + 1) % sc->nframes;
    928  1.1     pk 	sc->rfhead = (sc->rfhead + 1) % sc->nframes;
    929  1.1     pk 
    930  1.1     pk 	if (status & IE_FD_OK) {
    931  1.1     pk #if NBPFILTER > 0
    932  1.3     pk 		m = ieget(sc, &eh, &bpf_gets_it);
    933  1.1     pk #else
    934  1.3     pk 		m = ieget(sc, &eh, 0);
    935  1.1     pk #endif
    936  1.3     pk 		ie_drop_packet_buffer(sc);
    937  1.3     pk 	}
    938  1.3     pk 	if (m == 0) {
    939  1.3     pk 		sc->sc_ethercom.ec_if.if_ierrors++;
    940  1.3     pk 		return;
    941  1.1     pk 	}
    942  1.1     pk 
    943  1.7     pk #ifdef I82586_DEBUG
    944  1.1     pk 	if (sc->sc_debug & IED_READFRAME)
    945  1.5     pk 		printf("%s: frame from ether %s type 0x%x\n",
    946  1.5     pk 			sc->sc_dev.dv_xname,
    947  1.5     pk 			ether_sprintf(eh.ether_shost), (u_int)eh.ether_type);
    948  1.1     pk #endif
    949  1.1     pk 
    950  1.1     pk #if NBPFILTER > 0
    951  1.1     pk 	/*
    952  1.1     pk 	 * Check for a BPF filter; if so, hand it up.
    953  1.1     pk 	 * Note that we have to stick an extra mbuf up front, because bpf_mtap
    954  1.1     pk 	 * expects to have the ether header at the front.
    955  1.1     pk 	 * It doesn't matter that this results in an ill-formatted mbuf chain,
    956  1.1     pk 	 * since BPF just looks at the data.  (It doesn't try to free the mbuf,
    957  1.1     pk 	 * tho' it will make a copy for tcpdump.)
    958  1.1     pk 	 */
    959  1.1     pk 	if (bpf_gets_it) {
    960  1.1     pk 		struct mbuf m0;
    961  1.1     pk 		m0.m_len = sizeof eh;
    962  1.1     pk 		m0.m_data = (caddr_t)&eh;
    963  1.1     pk 		m0.m_next = m;
    964  1.1     pk 
    965  1.1     pk 		/* Pass it up. */
    966  1.1     pk 		bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, &m0);
    967  1.3     pk 
    968  1.3     pk 		/*
    969  1.3     pk 		 * A signal passed up from the filtering code indicating that
    970  1.3     pk 		 * the packet is intended for BPF but not for the protocol
    971  1.3     pk 		 * machinery.  We can save a few cycles by not handing it off
    972  1.3     pk 		 * to them.
    973  1.3     pk 		 */
    974  1.3     pk 		if (bpf_gets_it == 2) {
    975  1.3     pk 			m_freem(m);
    976  1.3     pk 			return;
    977  1.3     pk 		}
    978  1.1     pk 	}
    979  1.1     pk #endif /* NBPFILTER > 0 */
    980  1.1     pk 
    981  1.1     pk 	/*
    982  1.1     pk 	 * In here there used to be code to check destination addresses upon
    983  1.1     pk 	 * receipt of a packet.  We have deleted that code, and replaced it
    984  1.1     pk 	 * with code to check the address much earlier in the cycle, before
    985  1.1     pk 	 * copying the data in; this saves us valuable cycles when operating
    986  1.1     pk 	 * as a multicast router or when using BPF.
    987  1.1     pk 	 */
    988  1.1     pk 
    989  1.1     pk 	/*
    990  1.1     pk 	 * Finally pass this packet up to higher layers.
    991  1.1     pk 	 */
    992  1.1     pk 	ether_input(&sc->sc_ethercom.ec_if, &eh, m);
    993  1.3     pk 	sc->sc_ethercom.ec_if.if_ipackets++;
    994  1.1     pk }
    995  1.1     pk 
    996  1.1     pk static void
    997  1.1     pk ie_drop_packet_buffer(sc)
    998  1.1     pk 	struct ie_softc *sc;
    999  1.1     pk {
   1000  1.1     pk 	int i;
   1001  1.1     pk 
   1002  1.1     pk 	do {
   1003  1.5     pk 		bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
   1004  1.1     pk 		i = SWAP(sc->rbuffs[sc->rbhead]->ie_rbd_actual);
   1005  1.1     pk 		if ((i & IE_RBD_USED) == 0) {
   1006  1.3     pk 			/*
   1007  1.3     pk 			 * This means we are somehow out of sync.  So, we
   1008  1.3     pk 			 * reset the adapter.
   1009  1.3     pk 			 */
   1010  1.7     pk #ifdef I82586_DEBUG
   1011  1.1     pk 			print_rbd(sc->rbuffs[sc->rbhead]);
   1012  1.1     pk #endif
   1013  1.1     pk 			log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
   1014  1.1     pk 			    sc->sc_dev.dv_xname, sc->rbhead);
   1015  1.7     pk 			i82586_reset(sc, 1);
   1016  1.1     pk 			return;
   1017  1.1     pk 		}
   1018  1.1     pk 
   1019  1.1     pk 		i = (i & IE_RBD_LAST) != 0;
   1020  1.1     pk 
   1021  1.1     pk 		sc->rbuffs[sc->rbhead]->ie_rbd_length |= SWAP(IE_RBD_LAST);
   1022  1.1     pk 		sc->rbuffs[sc->rbhead]->ie_rbd_actual = SWAP(0);
   1023  1.1     pk 		sc->rbhead = (sc->rbhead + 1) % sc->nrxbuf;
   1024  1.1     pk 		sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~SWAP(IE_RBD_LAST);
   1025  1.1     pk 		sc->rbtail = (sc->rbtail + 1) % sc->nrxbuf;
   1026  1.1     pk 	} while (!i);
   1027  1.1     pk }
   1028  1.1     pk 
   1029  1.1     pk 
   1030  1.1     pk /*
   1031  1.1     pk  * Start transmission on an interface.
   1032  1.1     pk  */
   1033  1.1     pk void
   1034  1.7     pk i82586_start(ifp)
   1035  1.1     pk 	struct ifnet *ifp;
   1036  1.1     pk {
   1037  1.1     pk 	struct ie_softc *sc = ifp->if_softc;
   1038  1.1     pk 	struct mbuf *m0, *m;
   1039  1.1     pk 	u_char *buffer;
   1040  1.1     pk 	u_short len;
   1041  1.6     pk 	int s;
   1042  1.1     pk 
   1043  1.3     pk 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1044  1.1     pk 		return;
   1045  1.1     pk 
   1046  1.3     pk 	for (;;) {
   1047  1.3     pk 		if (sc->xmit_busy == NTXBUF) {
   1048  1.3     pk 			ifp->if_flags |= IFF_OACTIVE;
   1049  1.3     pk 			break;
   1050  1.3     pk 		}
   1051  1.1     pk 
   1052  1.3     pk 		IF_DEQUEUE(&ifp->if_snd, m0);
   1053  1.3     pk 		if (m0 == 0)
   1054  1.1     pk 			break;
   1055  1.1     pk 
   1056  1.3     pk 		/* We need to use m->m_pkthdr.len, so require the header */
   1057  1.3     pk 		if ((m0->m_flags & M_PKTHDR) == 0)
   1058  1.7     pk 			panic("i82586_start: no header mbuf");
   1059  1.3     pk 
   1060  1.3     pk #if NBPFILTER > 0
   1061  1.3     pk 		/* Tap off here if there is a BPF listener. */
   1062  1.3     pk 		if (ifp->if_bpf)
   1063  1.3     pk 			bpf_mtap(ifp->if_bpf, m0);
   1064  1.3     pk #endif
   1065  1.3     pk 
   1066  1.7     pk #ifdef I82586_DEBUG
   1067  1.3     pk 		if (sc->sc_debug & IED_ENQ)
   1068  1.3     pk 			printf("%s: fill buffer %d\n", sc->sc_dev.dv_xname,
   1069  1.3     pk 				sc->xchead);
   1070  1.3     pk #endif
   1071  1.3     pk 
   1072  1.3     pk 		if (m0->m_pkthdr.len > IE_TBUF_SIZE)
   1073  1.3     pk 			printf("%s: tbuf overflow\n", sc->sc_dev.dv_xname);
   1074  1.3     pk 
   1075  1.1     pk 		buffer = sc->xmit_cbuffs[sc->xchead];
   1076  1.3     pk 		for (m = m0; m != 0; m = m->m_next) {
   1077  1.5     pk 			(sc->memcopy)(mtod(m, caddr_t), buffer, m->m_len);
   1078  1.1     pk 			buffer += m->m_len;
   1079  1.1     pk 		}
   1080  1.1     pk 
   1081  1.3     pk 		len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
   1082  1.1     pk 		m_freem(m0);
   1083  1.3     pk 
   1084  1.1     pk 		sc->xmit_buffs[sc->xchead]->ie_xmit_flags = SWAP(len);
   1085  1.1     pk 
   1086  1.6     pk 		sc->xchead = (sc->xchead + 1) % NTXBUF;
   1087  1.6     pk 
   1088  1.6     pk 		s = splnet();
   1089  1.3     pk 		/* Start the first packet transmitting. */
   1090  1.3     pk 		if (sc->xmit_busy == 0)
   1091  1.3     pk 			iexmit(sc);
   1092  1.3     pk 
   1093  1.3     pk 		sc->xmit_busy++;
   1094  1.6     pk 		splx(s);
   1095  1.3     pk 	}
   1096  1.1     pk }
   1097  1.1     pk 
   1098  1.1     pk /*
   1099  1.1     pk  * set up IE's ram space
   1100  1.1     pk  */
   1101  1.1     pk int
   1102  1.7     pk i82586_setupram(sc)
   1103  1.1     pk 	struct ie_softc *sc;
   1104  1.1     pk {
   1105  1.1     pk 	volatile struct ie_sys_conf_ptr *scp;
   1106  1.1     pk 	volatile struct ie_int_sys_conf_ptr *iscp;
   1107  1.1     pk 	volatile struct ie_sys_ctl_block *scb;
   1108  1.1     pk 	int     s;
   1109  1.1     pk 
   1110  1.1     pk 	s = splnet();
   1111  1.1     pk 
   1112  1.1     pk 	scp = sc->scp;
   1113  1.1     pk 	(sc->memzero)((char *) scp, sizeof *scp);
   1114  1.1     pk 
   1115  1.1     pk 	iscp = sc->iscp;
   1116  1.1     pk 	(sc->memzero)((char *) iscp, sizeof *iscp);
   1117  1.1     pk 
   1118  1.1     pk 	scb = sc->scb;
   1119  1.1     pk 	(sc->memzero)((char *) scb, sizeof *scb);
   1120  1.1     pk 
   1121  1.1     pk 	scp->ie_bus_use = 0;	/* 16-bit */
   1122  1.1     pk 	ST_24(scp->ie_iscp_ptr, MK_24(sc->sc_iobase, iscp));
   1123  1.1     pk 
   1124  1.1     pk 	iscp->ie_busy = 1;	/* ie_busy == char */
   1125  1.1     pk 	iscp->ie_scb_offset = MK_16(sc->sc_maddr, scb);
   1126  1.1     pk 	ST_24(iscp->ie_base, MK_24(sc->sc_iobase, sc->sc_maddr));
   1127  1.1     pk 
   1128  1.1     pk 	if (sc->hwreset)
   1129  1.7     pk 		(sc->hwreset)(sc, CHIP_PROBE);
   1130  1.1     pk 
   1131  1.1     pk 	(sc->chan_attn) (sc);
   1132  1.1     pk 
   1133  1.1     pk 	delay(100);		/* wait a while... */
   1134  1.1     pk 
   1135  1.1     pk 	if (iscp->ie_busy) {
   1136  1.7     pk 		printf("%s: ISCP still busy in setupram!\n", sc->sc_dev.dv_xname);
   1137  1.1     pk 		splx(s);
   1138  1.1     pk 		return 0;
   1139  1.1     pk 	}
   1140  1.1     pk 	/*
   1141  1.1     pk 	 * Acknowledge any interrupts we may have caused...
   1142  1.1     pk 	 */
   1143  1.1     pk 	ie_ack(sc, IE_ST_WHENCE);
   1144  1.1     pk 	splx(s);
   1145  1.1     pk 
   1146  1.1     pk 	return 1;
   1147  1.1     pk }
   1148  1.1     pk 
   1149  1.1     pk void
   1150  1.7     pk i82586_reset(sc, verbose)
   1151  1.1     pk 	struct ie_softc *sc;
   1152  1.7     pk 	int verbose;
   1153  1.1     pk {
   1154  1.1     pk 	int s = splnet();
   1155  1.1     pk 
   1156  1.7     pk 	if (verbose)
   1157  1.7     pk 	    printf("%s: reset\n", sc->sc_dev.dv_xname);
   1158  1.1     pk 
   1159  1.1     pk 	/* Clear OACTIVE in case we're called from watchdog (frozen xmit). */
   1160  1.7     pk 	sc->sc_ethercom.ec_if.if_timer = 0;
   1161  1.1     pk 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1162  1.1     pk 
   1163  1.7     pk 	iestop(sc);
   1164  1.7     pk 
   1165  1.1     pk 	/*
   1166  1.1     pk 	 * Stop i82586 dead in its tracks.
   1167  1.1     pk 	 */
   1168  1.1     pk 	if (command_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
   1169  1.1     pk 		printf("%s: abort commands timed out\n", sc->sc_dev.dv_xname);
   1170  1.1     pk 
   1171  1.1     pk 	if (command_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
   1172  1.1     pk 		printf("%s: disable commands timed out\n", sc->sc_dev.dv_xname);
   1173  1.1     pk 
   1174  1.7     pk 	/*
   1175  1.7     pk 	 * This can really slow down the i82586_reset() on some cards, but it's
   1176  1.7     pk 	 * necessary to unwedge other ones (eg, the Sun VME ones) from certain
   1177  1.7     pk 	 * lockups.
   1178  1.7     pk 	 */
   1179  1.1     pk 	if (sc->hwreset)
   1180  1.7     pk 		(sc->hwreset)(sc, CARD_RESET);
   1181  1.6     pk 
   1182  1.5     pk 	ie_ack(sc, IE_ST_WHENCE);
   1183  1.6     pk 
   1184  1.1     pk #ifdef notdef
   1185  1.1     pk 	if (!check_ie_present(sc, sc->sc_maddr, sc->sc_msize))
   1186  1.1     pk 		panic("ie disappeared!\n");
   1187  1.1     pk #endif
   1188  1.1     pk 
   1189  1.6     pk 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0)
   1190  1.7     pk 		i82586_init(sc);
   1191  1.1     pk 
   1192  1.1     pk 	splx(s);
   1193  1.1     pk }
   1194  1.1     pk 
   1195  1.1     pk /*
   1196  1.1     pk  * Send a command to the controller and wait for it to either complete
   1197  1.1     pk  * or be accepted, depending on the command.  If the command pointer
   1198  1.1     pk  * is null, then pretend that the command is not an action command.
   1199  1.1     pk  * If the command pointer is not null, and the command is an action
   1200  1.1     pk  * command, wait for
   1201  1.1     pk  * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
   1202  1.1     pk  * to become true.
   1203  1.1     pk  */
   1204  1.1     pk static int
   1205  1.1     pk command_and_wait(sc, cmd, pcmd, mask)
   1206  1.1     pk 	struct ie_softc *sc;
   1207  1.1     pk 	int cmd;	/* native byte-order */
   1208  1.1     pk 	volatile void *pcmd;
   1209  1.1     pk 	int mask;	/* native byte-order */
   1210  1.1     pk {
   1211  1.1     pk 	volatile struct ie_cmd_common *cc = pcmd;
   1212  1.1     pk 	volatile struct ie_sys_ctl_block *scb = sc->scb;
   1213  1.1     pk 	int i;
   1214  1.1     pk 
   1215  1.1     pk 	scb->ie_command = (u_short)SWAP(cmd);
   1216  1.5     pk 	bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_WRITE);
   1217  1.2     pk 	(sc->chan_attn)(sc);
   1218  1.1     pk 
   1219  1.1     pk 	if (IE_ACTION_COMMAND(cmd) && pcmd) {
   1220  1.1     pk 		/*
   1221  1.1     pk 		 * According to the packet driver, the minimum timeout should
   1222  1.1     pk 		 * be .369 seconds, which we round up to .4.
   1223  1.1     pk 		 */
   1224  1.1     pk 
   1225  1.1     pk 		/*
   1226  1.1     pk 		 * Now spin-lock waiting for status.  This is not a very nice
   1227  1.1     pk 		 * thing to do, but I haven't figured out how, or indeed if, we
   1228  1.1     pk 		 * can put the process waiting for action to sleep.  (We may
   1229  1.1     pk 		 * be getting called through some other timeout running in the
   1230  1.1     pk 		 * kernel.)
   1231  1.1     pk 		 */
   1232  1.2     pk 		for (i = 0; i < 369000; i++) {
   1233  1.6     pk 			bus_space_barrier(sc->bt, sc->bh, 0, 0,
   1234  1.6     pk 					  BUS_SPACE_BARRIER_READ);
   1235  1.1     pk 			if ((SWAP(cc->ie_cmd_status) & mask))
   1236  1.1     pk 				return (0);
   1237  1.6     pk 			delay(1);
   1238  1.1     pk 		}
   1239  1.1     pk 
   1240  1.1     pk 	} else {
   1241  1.1     pk 		/*
   1242  1.1     pk 		 * Otherwise, just wait for the command to be accepted.
   1243  1.1     pk 		 */
   1244  1.1     pk 
   1245  1.6     pk 		/* XXX spin lock; wait at most 0.9 seconds */
   1246  1.6     pk 		for (i = 0; i < 900000; i++) {
   1247  1.6     pk 			bus_space_barrier(sc->bt, sc->bh, 0, 0,
   1248  1.6     pk 					  BUS_SPACE_BARRIER_READ);
   1249  1.6     pk 			if (scb->ie_command == 0)
   1250  1.1     pk 				return (0);
   1251  1.2     pk 			delay(1);
   1252  1.1     pk 		}
   1253  1.2     pk 	}
   1254  1.1     pk 
   1255  1.2     pk 	/* Timeout */
   1256  1.2     pk 	return (1);
   1257  1.1     pk }
   1258  1.1     pk 
   1259  1.1     pk /*
   1260  1.1     pk  * Run the time-domain reflectometer.
   1261  1.1     pk  */
   1262  1.1     pk static void
   1263  1.1     pk run_tdr(sc, cmd)
   1264  1.1     pk 	struct ie_softc *sc;
   1265  1.1     pk 	struct ie_tdr_cmd *cmd;
   1266  1.1     pk {
   1267  1.1     pk 	int result;
   1268  1.1     pk 
   1269  1.1     pk 	cmd->com.ie_cmd_status = SWAP(0);
   1270  1.1     pk 	cmd->com.ie_cmd_cmd = SWAP(IE_CMD_TDR | IE_CMD_LAST);
   1271  1.1     pk 	cmd->com.ie_cmd_link = SWAP(0xffff);
   1272  1.1     pk 
   1273  1.1     pk 	sc->scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
   1274  1.1     pk 	cmd->ie_tdr_time = SWAP(0);
   1275  1.1     pk 
   1276  1.1     pk 	if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1277  1.3     pk 	    (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0)
   1278  1.1     pk 		result = 0x10000; /* XXX */
   1279  1.1     pk 	else
   1280  1.1     pk 		result = SWAP(cmd->ie_tdr_time);
   1281  1.1     pk 
   1282  1.1     pk 	ie_ack(sc, IE_ST_WHENCE);
   1283  1.1     pk 
   1284  1.1     pk 	if (result & IE_TDR_SUCCESS)
   1285  1.1     pk 		return;
   1286  1.1     pk 
   1287  1.1     pk 	if (result & 0x10000)
   1288  1.1     pk 		printf("%s: TDR command failed\n", sc->sc_dev.dv_xname);
   1289  1.1     pk 	else if (result & IE_TDR_XCVR)
   1290  1.1     pk 		printf("%s: transceiver problem\n", sc->sc_dev.dv_xname);
   1291  1.1     pk 	else if (result & IE_TDR_OPEN)
   1292  1.1     pk 		printf("%s: TDR detected an open %d clocks away\n",
   1293  1.1     pk 		    sc->sc_dev.dv_xname, result & IE_TDR_TIME);
   1294  1.1     pk 	else if (result & IE_TDR_SHORT)
   1295  1.1     pk 		printf("%s: TDR detected a short %d clocks away\n",
   1296  1.1     pk 		    sc->sc_dev.dv_xname, result & IE_TDR_TIME);
   1297  1.1     pk 	else
   1298  1.5     pk 		printf("%s: TDR returned unknown status 0x%x\n",
   1299  1.1     pk 		    sc->sc_dev.dv_xname, result);
   1300  1.1     pk }
   1301  1.1     pk 
   1302  1.1     pk #ifdef notdef
   1303  1.1     pk /* ALIGN works on 8 byte boundaries.... but 4 byte boundaries are ok for sun */
   1304  1.1     pk #define	_ALLOC(p, n)	(bzero(p, n), p += n, p - n)
   1305  1.1     pk #define	ALLOC(p, n)	_ALLOC(p, ALIGN(n)) /* XXX convert to this? */
   1306  1.1     pk #endif
   1307  1.1     pk 
   1308  1.1     pk /*
   1309  1.1     pk  * setup_bufs: set up the buffers
   1310  1.1     pk  *
   1311  1.1     pk  * we have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
   1312  1.1     pk  * this is to be used for the buffers.  the chip indexs its control data
   1313  1.1     pk  * structures with 16 bit offsets, and it indexes actual buffers with
   1314  1.1     pk  * 24 bit addresses.   so we should allocate control buffers first so that
   1315  1.1     pk  * we don't overflow the 16 bit offset field.   The number of transmit
   1316  1.1     pk  * buffers is fixed at compile time.
   1317  1.1     pk  *
   1318  1.1     pk  * note: this function was written to be easy to understand, rather than
   1319  1.1     pk  *       highly efficient (it isn't in the critical path).
   1320  1.1     pk  */
   1321  1.1     pk static void
   1322  1.1     pk setup_bufs(sc)
   1323  1.1     pk 	struct ie_softc *sc;
   1324  1.1     pk {
   1325  1.1     pk 	caddr_t ptr = sc->buf_area;	/* memory pool */
   1326  1.1     pk 	int     n, r;
   1327  1.1     pk 
   1328  1.1     pk 	/*
   1329  1.1     pk 	 * step 0: zero memory and figure out how many recv buffers and
   1330  1.3     pk 	 * frames we can have.
   1331  1.1     pk 	 */
   1332  1.1     pk 	(sc->memzero)(ptr, sc->buf_area_sz);
   1333  1.1     pk 	ptr = (sc->align)(ptr);	/* set alignment and stick with it */
   1334  1.1     pk 
   1335  1.6     pk 	n = (int)(sc->align)((caddr_t) sizeof(struct ie_cmd_common)) +
   1336  1.6     pk 	    (int)(sc->align)((caddr_t) sizeof(struct ie_xmit_cmd)) +
   1337  1.1     pk 	    (int)(sc->align)((caddr_t) sizeof(struct ie_xmit_buf)) + IE_TBUF_SIZE;
   1338  1.1     pk 	n *= NTXBUF;		/* n = total size of xmit area */
   1339  1.1     pk 
   1340  1.1     pk 	n = sc->buf_area_sz - n;/* n = free space for recv stuff */
   1341  1.1     pk 
   1342  1.1     pk 	r = (int)(sc->align)((caddr_t) sizeof(struct ie_recv_frame_desc)) +
   1343  1.1     pk 	    (((int)(sc->align)((caddr_t) sizeof(struct ie_recv_buf_desc)) +
   1344  1.1     pk 		IE_RBUF_SIZE) * B_PER_F);
   1345  1.1     pk 
   1346  1.1     pk 	/* r = size of one R frame */
   1347  1.1     pk 
   1348  1.1     pk 	sc->nframes = n / r;
   1349  1.1     pk 	if (sc->nframes <= 0)
   1350  1.1     pk 		panic("ie: bogus buffer calc\n");
   1351  1.3     pk 	if (sc->nframes > MAXFRAMES)
   1352  1.3     pk 		sc->nframes = MAXFRAMES;
   1353  1.1     pk 
   1354  1.1     pk 	sc->nrxbuf = sc->nframes * B_PER_F;
   1355  1.1     pk 
   1356  1.7     pk #ifdef I82586_DEBUG
   1357  1.7     pk 	printf("%s: %d frames %d bufs\n", sc->sc_dev.dv_xname, sc->nframes,
   1358  1.7     pk 			sc->nrxbuf);
   1359  1.1     pk #endif
   1360  1.1     pk 
   1361  1.1     pk 	/*
   1362  1.1     pk 	 *  step 1a: lay out and zero frame data structures for transmit and recv
   1363  1.1     pk 	 */
   1364  1.1     pk 	for (n = 0; n < NTXBUF; n++) {
   1365  1.6     pk 		sc->nop_cmds[n] = (volatile struct ie_cmd_common *) ptr;
   1366  1.6     pk 		ptr = (sc->align)(ptr + sizeof(struct ie_cmd_common));
   1367  1.6     pk 	}
   1368  1.6     pk 	for (n = 0; n < NTXBUF; n++) {
   1369  1.1     pk 		sc->xmit_cmds[n] = (volatile struct ie_xmit_cmd *) ptr;
   1370  1.1     pk 		ptr = (sc->align)(ptr + sizeof(struct ie_xmit_cmd));
   1371  1.1     pk 	}
   1372  1.1     pk 
   1373  1.1     pk 	for (n = 0; n < sc->nframes; n++) {
   1374  1.1     pk 		sc->rframes[n] = (volatile struct ie_recv_frame_desc *) ptr;
   1375  1.1     pk 		ptr = (sc->align)(ptr + sizeof(struct ie_recv_frame_desc));
   1376  1.1     pk 	}
   1377  1.1     pk 
   1378  1.1     pk 	/*
   1379  1.1     pk 	 * step 1b: link together the recv frames and set EOL on last one
   1380  1.1     pk 	 */
   1381  1.1     pk 	for (n = 0; n < sc->nframes; n++) {
   1382  1.1     pk 		sc->rframes[n]->ie_fd_next =
   1383  1.1     pk 		    MK_16(sc->sc_maddr, sc->rframes[(n + 1) % sc->nframes]);
   1384  1.1     pk 	}
   1385  1.1     pk 	sc->rframes[sc->nframes - 1]->ie_fd_last |= SWAP(IE_FD_LAST);
   1386  1.1     pk 
   1387  1.1     pk 	/*
   1388  1.6     pk 	 * step 1c: link the xmit no-op frames to themselves
   1389  1.6     pk 	 */
   1390  1.6     pk 	for (n = 0; n < NTXBUF; n++) {
   1391  1.6     pk 		sc->nop_cmds[n]->ie_cmd_status = SWAP(0);
   1392  1.6     pk 		sc->nop_cmds[n]->ie_cmd_cmd = SWAP(IE_CMD_NOP);
   1393  1.6     pk 		sc->nop_cmds[n]->ie_cmd_link =
   1394  1.6     pk 			MK_16(sc->sc_maddr, sc->nop_cmds[n]);
   1395  1.6     pk 	}
   1396  1.6     pk 
   1397  1.6     pk 	/*
   1398  1.1     pk 	 * step 2a: lay out and zero frame buffer structures for xmit and recv
   1399  1.1     pk 	 */
   1400  1.1     pk 	for (n = 0; n < NTXBUF; n++) {
   1401  1.1     pk 		sc->xmit_buffs[n] = (volatile struct ie_xmit_buf *) ptr;
   1402  1.1     pk 		ptr = (sc->align)(ptr + sizeof(struct ie_xmit_buf));
   1403  1.1     pk 	}
   1404  1.1     pk 
   1405  1.1     pk 	for (n = 0; n < sc->nrxbuf; n++) {
   1406  1.1     pk 		sc->rbuffs[n] = (volatile struct ie_recv_buf_desc *) ptr;
   1407  1.1     pk 		ptr = (sc->align)(ptr + sizeof(struct ie_recv_buf_desc));
   1408  1.1     pk 	}
   1409  1.1     pk 
   1410  1.1     pk 	/*
   1411  1.1     pk 	 * step 2b: link together recv bufs and set EOL on last one
   1412  1.1     pk 	 */
   1413  1.1     pk 	for (n = 0; n < sc->nrxbuf; n++) {
   1414  1.1     pk 		sc->rbuffs[n]->ie_rbd_next =
   1415  1.1     pk 		    MK_16(sc->sc_maddr, sc->rbuffs[(n + 1) % sc->nrxbuf]);
   1416  1.1     pk 	}
   1417  1.1     pk 	sc->rbuffs[sc->nrxbuf - 1]->ie_rbd_length |= SWAP(IE_RBD_LAST);
   1418  1.1     pk 
   1419  1.1     pk 	/*
   1420  1.1     pk 	 * step 3: allocate the actual data buffers for xmit and recv
   1421  1.1     pk 	 * recv buffer gets linked into recv_buf_desc list here
   1422  1.1     pk 	 */
   1423  1.1     pk 	for (n = 0; n < NTXBUF; n++) {
   1424  1.1     pk 		sc->xmit_cbuffs[n] = (u_char *) ptr;
   1425  1.1     pk 		ptr = (sc->align)(ptr + IE_TBUF_SIZE);
   1426  1.1     pk 	}
   1427  1.1     pk 
   1428  1.1     pk 	/* Pointers to last packet sent and next available transmit buffer. */
   1429  1.1     pk 	sc->xchead = sc->xctail = 0;
   1430  1.1     pk 
   1431  1.1     pk 	/* Clear transmit-busy flag and set number of free transmit buffers. */
   1432  1.1     pk 	sc->xmit_busy = 0;
   1433  1.1     pk 
   1434  1.1     pk 	for (n = 0; n < sc->nrxbuf; n++) {
   1435  1.1     pk 		sc->cbuffs[n] = (char *) ptr;	/* XXX why char vs uchar? */
   1436  1.1     pk 		sc->rbuffs[n]->ie_rbd_length = SWAP(IE_RBUF_SIZE);
   1437  1.1     pk 		ST_24(sc->rbuffs[n]->ie_rbd_buffer, MK_24(sc->sc_iobase, ptr));
   1438  1.1     pk 		ptr = (sc->align)(ptr + IE_RBUF_SIZE);
   1439  1.1     pk 	}
   1440  1.1     pk 
   1441  1.1     pk 	/*
   1442  1.1     pk 	 * step 4: set the head and tail pointers on receive to keep track of
   1443  1.1     pk 	 * the order in which RFDs and RBDs are used.   link in recv frames
   1444  1.1     pk 	 * and buffer into the scb.
   1445  1.1     pk 	 */
   1446  1.1     pk 
   1447  1.1     pk 	sc->rfhead = 0;
   1448  1.1     pk 	sc->rftail = sc->nframes - 1;
   1449  1.1     pk 	sc->rbhead = 0;
   1450  1.1     pk 	sc->rbtail = sc->nrxbuf - 1;
   1451  1.1     pk 
   1452  1.7     pk #ifdef I82586_DEBUG
   1453  1.7     pk 	printf("%s: reserved %d bytes\n", sc->sc_dev.dv_xname, ptr - sc->buf_area);
   1454  1.1     pk #endif
   1455  1.1     pk }
   1456  1.1     pk 
   1457  1.1     pk /*
   1458  1.1     pk  * Run the multicast setup command.
   1459  1.1     pk  * Called at splnet().
   1460  1.1     pk  */
   1461  1.1     pk static int
   1462  1.1     pk mc_setup(sc, ptr)
   1463  1.1     pk 	struct ie_softc *sc;
   1464  1.1     pk 	void *ptr;
   1465  1.1     pk {
   1466  1.1     pk 	volatile struct ie_mcast_cmd *cmd = ptr;
   1467  1.1     pk 
   1468  1.1     pk 	cmd->com.ie_cmd_status = SWAP(0);
   1469  1.1     pk 	cmd->com.ie_cmd_cmd = SWAP(IE_CMD_MCAST | IE_CMD_LAST);
   1470  1.1     pk 	cmd->com.ie_cmd_link = SWAP(0xffff);
   1471  1.1     pk 
   1472  1.1     pk 	(sc->memcopy)((caddr_t)sc->mcast_addrs, (caddr_t)cmd->ie_mcast_addrs,
   1473  1.6     pk 		       sc->mcast_count * sizeof *sc->mcast_addrs);
   1474  1.1     pk 
   1475  1.1     pk 	cmd->ie_mcast_bytes =
   1476  1.6     pk 		SWAP(sc->mcast_count * ETHER_ADDR_LEN); /* grrr... */
   1477  1.1     pk 
   1478  1.1     pk 	sc->scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
   1479  1.1     pk 	if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1480  1.3     pk 	    (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0) {
   1481  1.1     pk 		printf("%s: multicast address setup command failed\n",
   1482  1.1     pk 		    sc->sc_dev.dv_xname);
   1483  1.1     pk 		return 0;
   1484  1.1     pk 	}
   1485  1.6     pk 
   1486  1.6     pk 	i82586_start_transceiver(sc);
   1487  1.1     pk 	return 1;
   1488  1.1     pk }
   1489  1.1     pk 
   1490  1.1     pk /*
   1491  1.1     pk  * This routine takes the environment generated by check_ie_present() and adds
   1492  1.1     pk  * to it all the other structures we need to operate the adapter.  This
   1493  1.1     pk  * includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands, starting
   1494  1.1     pk  * the receiver unit, and clearing interrupts.
   1495  1.1     pk  *
   1496  1.1     pk  * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
   1497  1.1     pk  */
   1498  1.1     pk int
   1499  1.7     pk i82586_init(sc)
   1500  1.1     pk 	struct ie_softc *sc;
   1501  1.1     pk {
   1502  1.1     pk 	volatile struct ie_sys_ctl_block *scb = sc->scb;
   1503  1.3     pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1504  1.1     pk 	void *ptr;
   1505  1.1     pk 
   1506  1.1     pk 	ptr = sc->buf_area;
   1507  1.1     pk 
   1508  1.1     pk 	/*
   1509  1.1     pk 	 * Send the configure command first.
   1510  1.1     pk 	 */
   1511  1.1     pk 	{
   1512  1.1     pk 		volatile struct ie_config_cmd *cmd = ptr;
   1513  1.1     pk 
   1514  1.1     pk 		scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
   1515  1.1     pk 		cmd->com.ie_cmd_status = SWAP(0);
   1516  1.1     pk 		cmd->com.ie_cmd_cmd = SWAP(IE_CMD_CONFIG | IE_CMD_LAST);
   1517  1.1     pk 		cmd->com.ie_cmd_link = SWAP(0xffff);
   1518  1.1     pk 
   1519  1.1     pk 		ie_setup_config(cmd, sc->promisc, 0);
   1520  1.1     pk 
   1521  1.1     pk 		if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1522  1.3     pk 		    (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0) {
   1523  1.1     pk 			printf("%s: configure command failed\n",
   1524  1.1     pk 			    sc->sc_dev.dv_xname);
   1525  1.1     pk 			return 0;
   1526  1.1     pk 		}
   1527  1.1     pk 	}
   1528  1.1     pk 
   1529  1.1     pk 	/*
   1530  1.1     pk 	 * Now send the Individual Address Setup command.
   1531  1.1     pk 	 */
   1532  1.1     pk 	{
   1533  1.1     pk 		volatile struct ie_iasetup_cmd *cmd = ptr;
   1534  1.1     pk 
   1535  1.1     pk 		scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
   1536  1.1     pk 		cmd->com.ie_cmd_status = SWAP(0);
   1537  1.1     pk 		cmd->com.ie_cmd_cmd = SWAP(IE_CMD_IASETUP | IE_CMD_LAST);
   1538  1.1     pk 		cmd->com.ie_cmd_link = SWAP(0xffff);
   1539  1.1     pk 
   1540  1.3     pk 		(sc->memcopy)(LLADDR(ifp->if_sadl),
   1541  1.1     pk 		      (caddr_t)&cmd->ie_address, sizeof cmd->ie_address);
   1542  1.1     pk 
   1543  1.1     pk 		if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
   1544  1.3     pk 		    (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0) {
   1545  1.1     pk 			printf("%s: individual address setup command failed\n",
   1546  1.1     pk 			    sc->sc_dev.dv_xname);
   1547  1.1     pk 			return 0;
   1548  1.1     pk 		}
   1549  1.1     pk 	}
   1550  1.1     pk 
   1551  1.1     pk 	/*
   1552  1.1     pk 	 * Now run the time-domain reflectometer.
   1553  1.1     pk 	 */
   1554  1.1     pk 	run_tdr(sc, ptr);
   1555  1.1     pk 
   1556  1.1     pk 	/*
   1557  1.1     pk 	 * Acknowledge any interrupts we have generated thus far.
   1558  1.1     pk 	 */
   1559  1.1     pk 	ie_ack(sc, IE_ST_WHENCE);
   1560  1.1     pk 
   1561  1.1     pk 	/*
   1562  1.1     pk 	 * Set up the transmit and recv buffers.
   1563  1.1     pk 	 */
   1564  1.1     pk 	setup_bufs(sc);
   1565  1.1     pk 
   1566  1.6     pk 	ie_ack(sc, IE_ST_WHENCE);
   1567  1.6     pk 
   1568  1.6     pk 	if (sc->hwinit)
   1569  1.6     pk 		(sc->hwinit)(sc);
   1570  1.6     pk 
   1571  1.3     pk 	ifp->if_flags |= IFF_RUNNING;
   1572  1.3     pk 	ifp->if_flags &= ~IFF_OACTIVE;
   1573  1.1     pk 
   1574  1.6     pk 	if (NTXBUF < 2)
   1575  1.7     pk 		sc->do_xmitnopchain = 0;
   1576  1.6     pk 
   1577  1.6     pk 	i82586_start_transceiver(sc);
   1578  1.6     pk 	return 0;
   1579  1.6     pk }
   1580  1.6     pk 
   1581  1.6     pk static void
   1582  1.6     pk i82586_start_transceiver(sc)
   1583  1.6     pk 	struct ie_softc *sc;
   1584  1.6     pk {
   1585  1.6     pk 	sc->rframes[0]->ie_fd_buf_desc = MK_16(sc->sc_maddr, sc->rbuffs[0]);
   1586  1.1     pk 	sc->scb->ie_recv_list = MK_16(sc->sc_maddr, sc->rframes[0]);
   1587  1.1     pk 
   1588  1.7     pk 	if (sc->do_xmitnopchain) {
   1589  1.6     pk 		/* Stop transmit command chain */
   1590  1.6     pk 		if (command_and_wait(sc, IE_CU_STOP|IE_RU_DISABLE, 0, 0))
   1591  1.6     pk 			printf("%s: CU/RU stop command timed out\n",
   1592  1.6     pk 				sc->sc_dev.dv_xname);
   1593  1.1     pk 
   1594  1.6     pk 		/* Start the receiver & transmitter chain */
   1595  1.6     pk 		sc->scb->ie_command_list =
   1596  1.6     pk 			MK_16(sc->sc_maddr,
   1597  1.6     pk 			      sc->nop_cmds[(sc->xctail + NTXBUF - 1) % NTXBUF]);
   1598  1.6     pk 		if (command_and_wait(sc, IE_CU_START|IE_RU_START, 0, 0))
   1599  1.6     pk 			printf("%s: CU/RU command timed out\n",
   1600  1.6     pk 				sc->sc_dev.dv_xname);
   1601  1.6     pk 	} else {
   1602  1.6     pk 		if (command_and_wait(sc, IE_RU_START, 0, 0))
   1603  1.6     pk 			printf("%s: RU command timed out\n",
   1604  1.6     pk 				sc->sc_dev.dv_xname);
   1605  1.6     pk 	}
   1606  1.1     pk }
   1607  1.1     pk 
   1608  1.1     pk static void
   1609  1.1     pk iestop(sc)
   1610  1.1     pk 	struct ie_softc *sc;
   1611  1.1     pk {
   1612  1.1     pk 
   1613  1.6     pk 	if (command_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
   1614  1.6     pk 		printf("%s: disable commands timed out\n", sc->sc_dev.dv_xname);
   1615  1.1     pk }
   1616  1.1     pk 
   1617  1.1     pk int
   1618  1.7     pk i82586_ioctl(ifp, cmd, data)
   1619  1.1     pk 	register struct ifnet *ifp;
   1620  1.1     pk 	u_long cmd;
   1621  1.1     pk 	caddr_t data;
   1622  1.1     pk {
   1623  1.1     pk 	struct ie_softc *sc = ifp->if_softc;
   1624  1.1     pk 	struct ifaddr *ifa = (struct ifaddr *)data;
   1625  1.1     pk 	struct ifreq *ifr = (struct ifreq *)data;
   1626  1.1     pk 	int s, error = 0;
   1627  1.1     pk 
   1628  1.1     pk 	s = splnet();
   1629  1.1     pk 
   1630  1.1     pk 	switch(cmd) {
   1631  1.1     pk 
   1632  1.1     pk 	case SIOCSIFADDR:
   1633  1.1     pk 		ifp->if_flags |= IFF_UP;
   1634  1.1     pk 
   1635  1.1     pk 		switch(ifa->ifa_addr->sa_family) {
   1636  1.1     pk #ifdef INET
   1637  1.1     pk 		case AF_INET:
   1638  1.7     pk 			i82586_init(sc);
   1639  1.1     pk 			arp_ifinit(ifp, ifa);
   1640  1.1     pk 			break;
   1641  1.1     pk #endif
   1642  1.1     pk #ifdef NS
   1643  1.1     pk 		/* XXX - This code is probably wrong. */
   1644  1.1     pk 		case AF_NS:
   1645  1.1     pk 		    {
   1646  1.1     pk 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1647  1.1     pk 
   1648  1.1     pk 			if (ns_nullhost(*ina))
   1649  1.1     pk 				ina->x_host =
   1650  1.1     pk 				    *(union ns_host *)LLADDR(ifp->if_sadl);
   1651  1.1     pk 			else
   1652  1.1     pk 				bcopy(ina->x_host.c_host,
   1653  1.1     pk 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1654  1.1     pk 			/* Set new address. */
   1655  1.7     pk 			i82586_init(sc);
   1656  1.1     pk 			break;
   1657  1.1     pk 		    }
   1658  1.1     pk #endif /* NS */
   1659  1.1     pk 		default:
   1660  1.7     pk 			i82586_init(sc);
   1661  1.1     pk 			break;
   1662  1.1     pk 		}
   1663  1.1     pk 		break;
   1664  1.1     pk 
   1665  1.1     pk 	case SIOCSIFFLAGS:
   1666  1.1     pk 		sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
   1667  1.1     pk 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1668  1.1     pk 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1669  1.1     pk 			/*
   1670  1.1     pk 			 * If interface is marked down and it is running, then
   1671  1.1     pk 			 * stop it.
   1672  1.1     pk 			 */
   1673  1.1     pk 			iestop(sc);
   1674  1.1     pk 			ifp->if_flags &= ~IFF_RUNNING;
   1675  1.1     pk 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1676  1.1     pk 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1677  1.1     pk 			/*
   1678  1.1     pk 			 * If interface is marked up and it is stopped, then
   1679  1.1     pk 			 * start it.
   1680  1.1     pk 			 */
   1681  1.7     pk 			i82586_init(sc);
   1682  1.1     pk 		} else {
   1683  1.1     pk 			/*
   1684  1.1     pk 			 * Reset the interface to pick up changes in any other
   1685  1.1     pk 			 * flags that affect hardware registers.
   1686  1.1     pk 			 */
   1687  1.1     pk 			iestop(sc);
   1688  1.7     pk 			i82586_init(sc);
   1689  1.1     pk 		}
   1690  1.7     pk #ifdef I82586_DEBUG
   1691  1.1     pk 		if (ifp->if_flags & IFF_DEBUG)
   1692  1.1     pk 			sc->sc_debug = IED_ALL;
   1693  1.1     pk 		else
   1694  1.1     pk 			sc->sc_debug = 0;
   1695  1.1     pk #endif
   1696  1.1     pk 		break;
   1697  1.1     pk 
   1698  1.1     pk 	case SIOCADDMULTI:
   1699  1.1     pk 	case SIOCDELMULTI:
   1700  1.1     pk 		error = (cmd == SIOCADDMULTI) ?
   1701  1.1     pk 		    ether_addmulti(ifr, &sc->sc_ethercom):
   1702  1.1     pk 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1703  1.1     pk 
   1704  1.1     pk 		if (error == ENETRESET) {
   1705  1.1     pk 			/*
   1706  1.1     pk 			 * Multicast list has changed; set the hardware filter
   1707  1.1     pk 			 * accordingly.
   1708  1.1     pk 			 */
   1709  1.1     pk 			mc_reset(sc);
   1710  1.1     pk 			error = 0;
   1711  1.1     pk 		}
   1712  1.1     pk 		break;
   1713  1.1     pk 
   1714  1.7     pk         case SIOCGIFMEDIA:
   1715  1.7     pk         case SIOCSIFMEDIA:
   1716  1.7     pk                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1717  1.7     pk                 break;
   1718  1.7     pk 
   1719  1.1     pk 	default:
   1720  1.1     pk 		error = EINVAL;
   1721  1.1     pk 	}
   1722  1.1     pk 	splx(s);
   1723  1.1     pk 	return error;
   1724  1.1     pk }
   1725  1.1     pk 
   1726  1.1     pk static void
   1727  1.1     pk mc_reset(sc)
   1728  1.1     pk 	struct ie_softc *sc;
   1729  1.1     pk {
   1730  1.1     pk 	struct ether_multi *enm;
   1731  1.1     pk 	struct ether_multistep step;
   1732  1.1     pk 
   1733  1.1     pk 	/*
   1734  1.1     pk 	 * Step through the list of addresses.
   1735  1.1     pk 	 */
   1736  1.1     pk 	sc->mcast_count = 0;
   1737  1.1     pk 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1738  1.1     pk 	while (enm) {
   1739  1.1     pk 		if (sc->mcast_count >= MAXMCAST ||
   1740  1.1     pk 		    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
   1741  1.1     pk 			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
   1742  1.7     pk 			i82586_ioctl(&sc->sc_ethercom.ec_if,
   1743  1.7     pk 					SIOCSIFFLAGS, (void *)0);
   1744  1.1     pk 			goto setflag;
   1745  1.1     pk 		}
   1746  1.1     pk 
   1747  1.1     pk 		bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
   1748  1.1     pk 		sc->mcast_count++;
   1749  1.1     pk 		ETHER_NEXT_MULTI(step, enm);
   1750  1.1     pk 	}
   1751  1.1     pk setflag:
   1752  1.1     pk 	sc->want_mcsetup = 1;
   1753  1.1     pk }
   1754  1.1     pk 
   1755  1.7     pk /*
   1756  1.7     pk  * Media change callback.
   1757  1.7     pk  */
   1758  1.7     pk int
   1759  1.7     pk i82586_mediachange(ifp)
   1760  1.7     pk         struct ifnet *ifp;
   1761  1.7     pk {
   1762  1.7     pk         struct ie_softc *sc = ifp->if_softc;
   1763  1.7     pk 
   1764  1.7     pk         if (sc->sc_mediachange)
   1765  1.7     pk                 return ((*sc->sc_mediachange)(sc));
   1766  1.7     pk         return (EINVAL);
   1767  1.7     pk }
   1768  1.7     pk 
   1769  1.7     pk /*
   1770  1.7     pk  * Media status callback.
   1771  1.7     pk  */
   1772  1.7     pk void
   1773  1.7     pk i82586_mediastatus(ifp, ifmr)
   1774  1.7     pk         struct ifnet *ifp;
   1775  1.7     pk         struct ifmediareq *ifmr;
   1776  1.7     pk {
   1777  1.7     pk         struct ie_softc *sc = ifp->if_softc;
   1778  1.7     pk 
   1779  1.7     pk         if (sc->sc_mediastatus)
   1780  1.7     pk                 (*sc->sc_mediastatus)(sc, ifmr);
   1781  1.7     pk }
   1782  1.7     pk 
   1783  1.7     pk #ifdef I82586_DEBUG
   1784  1.1     pk void
   1785  1.1     pk print_rbd(rbd)
   1786  1.1     pk 	volatile struct ie_recv_buf_desc *rbd;
   1787  1.1     pk {
   1788  1.3     pk 	u_long bufval;
   1789  1.3     pk 
   1790  1.3     pk 	bcopy((char *)&rbd->ie_rbd_buffer, &bufval, 4); /*XXX*/
   1791  1.1     pk 
   1792  1.3     pk 	printf("RBD at %08lx:\nactual %04x, next %04x, buffer %lx\n"
   1793  1.1     pk 		"length %04x, mbz %04x\n", (u_long)rbd,
   1794  1.3     pk 		SWAP(rbd->ie_rbd_actual),
   1795  1.1     pk 		SWAP(rbd->ie_rbd_next),
   1796  1.3     pk 		bufval,
   1797  1.1     pk 		SWAP(rbd->ie_rbd_length),
   1798  1.1     pk 		rbd->mbz);
   1799  1.1     pk }
   1800  1.1     pk #endif
   1801