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