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