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