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