Home | History | Annotate | Line # | Download | only in ic
i82586.c revision 1.80
      1  1.80   msaitoh /*	$NetBSD: i82586.c,v 1.80 2019/02/05 06:17:02 msaitoh Exp $	*/
      2   1.1        pk 
      3   1.1        pk /*-
      4   1.9        pk  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.9        pk  * All rights reserved.
      6   1.9        pk  *
      7   1.9        pk  * This code is derived from software contributed to The NetBSD Foundation
      8  1.17   mycroft  * by Paul Kranenburg and Charles M. Hannum.
      9   1.9        pk  *
     10   1.9        pk  * Redistribution and use in source and binary forms, with or without
     11   1.9        pk  * modification, are permitted provided that the following conditions
     12   1.9        pk  * are met:
     13   1.9        pk  * 1. Redistributions of source code must retain the above copyright
     14   1.9        pk  *    notice, this list of conditions and the following disclaimer.
     15   1.9        pk  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.9        pk  *    notice, this list of conditions and the following disclaimer in the
     17   1.9        pk  *    documentation and/or other materials provided with the distribution.
     18   1.9        pk  *
     19   1.9        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.9        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.9        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.9        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.9        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.9        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.9        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.9        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.9        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.9        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.9        pk  * POSSIBILITY OF SUCH DAMAGE.
     30   1.9        pk  */
     31   1.9        pk 
     32   1.9        pk /*-
     33   1.1        pk  * Copyright (c) 1997 Paul Kranenburg.
     34   1.1        pk  * Copyright (c) 1992, 1993, University of Vermont and State
     35   1.1        pk  *  Agricultural College.
     36   1.1        pk  * Copyright (c) 1992, 1993, Garrett A. Wollman.
     37   1.1        pk  *
     38   1.1        pk  * Portions:
     39   1.1        pk  * Copyright (c) 1994, 1995, Rafal K. Boni
     40   1.1        pk  * Copyright (c) 1990, 1991, William F. Jolitz
     41   1.1        pk  * Copyright (c) 1990, The Regents of the University of California
     42   1.1        pk  *
     43   1.1        pk  * All rights reserved.
     44   1.1        pk  *
     45   1.1        pk  * Redistribution and use in source and binary forms, with or without
     46   1.1        pk  * modification, are permitted provided that the following conditions
     47   1.1        pk  * are met:
     48   1.1        pk  * 1. Redistributions of source code must retain the above copyright
     49   1.1        pk  *    notice, this list of conditions and the following disclaimer.
     50   1.1        pk  * 2. Redistributions in binary form must reproduce the above copyright
     51   1.1        pk  *    notice, this list of conditions and the following disclaimer in the
     52   1.1        pk  *    documentation and/or other materials provided with the distribution.
     53   1.1        pk  * 3. All advertising materials mentioning features or use of this software
     54   1.1        pk  *    must display the following acknowledgement:
     55  1.18   mycroft  *	This product includes software developed by the University of Vermont
     56  1.18   mycroft  *	and State Agricultural College and Garrett A. Wollman, by William F.
     57  1.18   mycroft  *	Jolitz, and by the University of California, Berkeley, Lawrence
     58  1.18   mycroft  *	Berkeley Laboratory, and its contributors.
     59   1.1        pk  * 4. Neither the names of the Universities nor the names of the authors
     60   1.1        pk  *    may be used to endorse or promote products derived from this software
     61   1.1        pk  *    without specific prior written permission.
     62   1.1        pk  *
     63   1.1        pk  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64   1.1        pk  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65   1.1        pk  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66   1.1        pk  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
     67   1.1        pk  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68   1.1        pk  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69   1.1        pk  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70   1.1        pk  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71   1.1        pk  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72   1.1        pk  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73   1.1        pk  * SUCH DAMAGE.
     74   1.1        pk  */
     75   1.1        pk 
     76   1.1        pk /*
     77   1.1        pk  * Intel 82586 Ethernet chip
     78   1.1        pk  * Register, bit, and structure definitions.
     79   1.1        pk  *
     80   1.1        pk  * Original StarLAN driver written by Garrett Wollman with reference to the
     81   1.1        pk  * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
     82   1.1        pk  *
     83   1.1        pk  * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
     84   1.1        pk  *
     85   1.1        pk  * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
     86   1.1        pk  *
     87   1.1        pk  * Majorly cleaned up and 3C507 code merged by Charles Hannum.
     88   1.1        pk  *
     89   1.1        pk  * Converted to SUN ie driver by Charles D. Cranor,
     90   1.1        pk  *		October 1994, January 1995.
     91   1.1        pk  * This sun version based on i386 version 1.30.
     92   1.1        pk  */
     93   1.1        pk 
     94   1.1        pk /*
     95   1.1        pk  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
     96   1.1        pk  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
     97   1.1        pk  * SUN, making life difficult.  Programming this chip is mostly the same,
     98   1.1        pk  * but certain details differ from system to system.  This driver is
     99   1.1        pk  * written so that different "ie" interfaces can be controled by the same
    100   1.1        pk  * driver.
    101   1.1        pk  */
    102   1.1        pk 
    103   1.1        pk /*
    104   1.1        pk Mode of operation:
    105   1.1        pk 
    106   1.1        pk    We run the 82586 in a standard Ethernet mode.  We keep NFRAMES
    107   1.1        pk    received frame descriptors around for the receiver to use, and
    108   1.1        pk    NRXBUF associated receive buffer descriptors, both in a circular
    109   1.1        pk    list.  Whenever a frame is received, we rotate both lists as
    110   1.1        pk    necessary.  (The 586 treats both lists as a simple queue.)  We also
    111   1.1        pk    keep a transmit command around so that packets can be sent off
    112   1.1        pk    quickly.
    113   1.1        pk 
    114   1.1        pk    We configure the adapter in AL-LOC = 1 mode, which means that the
    115   1.1        pk    Ethernet/802.3 MAC header is placed at the beginning of the receive
    116   1.1        pk    buffer rather than being split off into various fields in the RFD.
    117   1.1        pk    This also means that we must include this header in the transmit
    118   1.1        pk    buffer as well.
    119   1.1        pk 
    120   1.1        pk    By convention, all transmit commands, and only transmit commands,
    121   1.1        pk    shall have the I (IE_CMD_INTR) bit set in the command.  This way,
    122   1.9        pk    when an interrupt arrives at i82586_intr(), it is immediately possible
    123   1.1        pk    to tell what precisely caused it.  ANY OTHER command-sending
    124   1.1        pk    routines should run at splnet(), and should post an acknowledgement
    125   1.1        pk    to every interrupt they generate.
    126   1.1        pk 
    127   1.6        pk    To save the expense of shipping a command to 82586 every time we
    128   1.6        pk    want to send a frame, we use a linked list of commands consisting
    129   1.6        pk    of alternate XMIT and NOP commands. The links of these elements
    130   1.6        pk    are manipulated (in iexmit()) such that the NOP command loops back
    131   1.6        pk    to itself whenever the following XMIT command is not yet ready to
    132   1.6        pk    go. Whenever an XMIT is ready, the preceding NOP link is pointed
    133   1.6        pk    at it, while its own link field points to the following NOP command.
    134   1.6        pk    Thus, a single transmit command sets off an interlocked traversal
    135   1.6        pk    of the xmit command chain, with the host processor in control of
    136   1.6        pk    the synchronization.
    137   1.1        pk */
    138   1.1        pk 
    139  1.41     lukem #include <sys/cdefs.h>
    140  1.80   msaitoh __KERNEL_RCSID(0, "$NetBSD: i82586.c,v 1.80 2019/02/05 06:17:02 msaitoh Exp $");
    141  1.41     lukem 
    142  1.40     lukem 
    143   1.1        pk #include <sys/param.h>
    144   1.1        pk #include <sys/systm.h>
    145   1.1        pk #include <sys/mbuf.h>
    146   1.1        pk #include <sys/socket.h>
    147   1.1        pk #include <sys/ioctl.h>
    148   1.1        pk #include <sys/errno.h>
    149   1.1        pk #include <sys/syslog.h>
    150   1.1        pk #include <sys/device.h>
    151   1.1        pk 
    152   1.1        pk #include <net/if.h>
    153   1.7        pk #include <net/if_dl.h>
    154   1.1        pk #include <net/if_types.h>
    155   1.7        pk #include <net/if_media.h>
    156   1.1        pk #include <net/if_ether.h>
    157   1.1        pk #include <net/bpf.h>
    158   1.1        pk 
    159  1.60        ad #include <sys/bus.h>
    160   1.5        pk 
    161   1.1        pk #include <dev/ic/i82586reg.h>
    162   1.1        pk #include <dev/ic/i82586var.h>
    163   1.1        pk 
    164  1.50     perry void	 	i82586_reset(struct ie_softc *, int);
    165  1.50     perry void 		i82586_watchdog(struct ifnet *);
    166  1.50     perry int 		i82586_init(struct ifnet *);
    167  1.58  christos int 		i82586_ioctl(struct ifnet *, u_long, void *);
    168  1.50     perry void 		i82586_start(struct ifnet *);
    169  1.50     perry void 		i82586_stop(struct ifnet *, int);
    170  1.50     perry 
    171  1.50     perry 
    172  1.50     perry int 		i82586_rint(struct ie_softc *, int);
    173  1.50     perry int 		i82586_tint(struct ie_softc *, int);
    174  1.50     perry 
    175  1.50     perry int     	i82586_mediachange(struct ifnet *);
    176  1.50     perry void    	i82586_mediastatus(struct ifnet *, struct ifmediareq *);
    177  1.50     perry 
    178  1.50     perry static int 	ie_readframe(struct ie_softc *, int);
    179  1.50     perry static struct mbuf *ieget(struct ie_softc *, int, int);
    180  1.50     perry static int	i82586_get_rbd_list(struct ie_softc *,
    181  1.50     perry 					     u_int16_t *, u_int16_t *, int *);
    182  1.50     perry static void	i82586_release_rbd_list(struct ie_softc *,
    183  1.50     perry 					     u_int16_t, u_int16_t);
    184  1.50     perry static int	i82586_drop_frames(struct ie_softc *);
    185  1.50     perry static int	i82586_chk_rx_ring(struct ie_softc *);
    186  1.50     perry 
    187  1.55     perry static inline void 	ie_ack(struct ie_softc *, u_int);
    188  1.55     perry static inline void 	iexmit(struct ie_softc *);
    189  1.50     perry static void 		i82586_start_transceiver(struct ie_softc *);
    190  1.50     perry 
    191  1.50     perry static void	i82586_count_errors(struct ie_softc *);
    192  1.50     perry static void	i82586_rx_errors(struct ie_softc *, int, int);
    193  1.50     perry static void 	i82586_setup_bufs(struct ie_softc *);
    194  1.50     perry static void	setup_simple_command(struct ie_softc *, int, int);
    195  1.50     perry static int 	ie_cfg_setup(struct ie_softc *, int, int, int);
    196  1.50     perry static int	ie_ia_setup(struct ie_softc *, int);
    197  1.50     perry static void 	ie_run_tdr(struct ie_softc *, int);
    198  1.50     perry static int 	ie_mc_setup(struct ie_softc *, int);
    199  1.50     perry static void 	ie_mc_reset(struct ie_softc *);
    200  1.50     perry static int 	i82586_start_cmd(struct ie_softc *, int, int, int, int);
    201  1.50     perry static int	i82586_cmd_wait(struct ie_softc *);
    202   1.7        pk 
    203  1.25        pk #if I82586_DEBUG
    204  1.50     perry void 		print_rbd(struct ie_softc *, int);
    205   1.7        pk #endif
    206   1.1        pk 
    207  1.45    bouyer static char* padbuf = NULL;
    208   1.1        pk 
    209   1.1        pk /*
    210   1.9        pk  * Front-ends call this function to attach to the MI driver.
    211   1.9        pk  *
    212   1.9        pk  * The front-end has responsibility for managing the ICP and ISCP
    213   1.9        pk  * structures. Both of these are opaque to us.  Also, the front-end
    214   1.9        pk  * chooses a location for the SCB which is expected to be addressable
    215   1.9        pk  * (through `sc->scb') as an offset against the shared-memory bus handle.
    216   1.9        pk  *
    217   1.9        pk  * The following MD interface function must be setup by the front-end
    218   1.9        pk  * before calling here:
    219   1.9        pk  *
    220   1.9        pk  *	hwreset			- board dependent reset
    221   1.9        pk  *	hwinit			- board dependent initialization
    222   1.9        pk  *	chan_attn		- channel attention
    223   1.9        pk  *	intrhook		- board dependent interrupt processing
    224   1.9        pk  *	memcopyin		- shared memory copy: board to KVA
    225   1.9        pk  *	memcopyout		- shared memory copy: KVA to board
    226  1.11    kleink  *	ie_bus_read16		- read a sixteen-bit i82586 pointer
    227   1.9        pk  *	ie_bus_write16		- write a sixteen-bit i82586 pointer
    228  1.11    kleink  *	ie_bus_write24		- write a twenty-four-bit i82586 pointer
    229   1.9        pk  *
    230   1.1        pk  */
    231   1.1        pk void
    232  1.73   msaitoh i82586_attach(struct ie_softc *sc, const char *name, u_int8_t *etheraddr,
    233  1.73   msaitoh     int *media, int nmedia, int defmedia)
    234   1.1        pk {
    235   1.7        pk 	int i;
    236   1.1        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    237   1.1        pk 
    238  1.69   tsutsui 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    239   1.1        pk 	ifp->if_softc = sc;
    240   1.7        pk 	ifp->if_start = i82586_start;
    241   1.7        pk 	ifp->if_ioctl = i82586_ioctl;
    242  1.29     bjh21 	ifp->if_init = i82586_init;
    243  1.29     bjh21 	ifp->if_stop = i82586_stop;
    244   1.7        pk 	ifp->if_watchdog = i82586_watchdog;
    245  1.80   msaitoh 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    246  1.33   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    247   1.1        pk 
    248   1.9        pk         /* Initialize media goo. */
    249   1.7        pk         ifmedia_init(&sc->sc_media, 0, i82586_mediachange, i82586_mediastatus);
    250   1.7        pk         if (media != NULL) {
    251   1.7        pk                 for (i = 0; i < nmedia; i++)
    252   1.7        pk                         ifmedia_add(&sc->sc_media, media[i], 0, NULL);
    253   1.7        pk                 ifmedia_set(&sc->sc_media, defmedia);
    254   1.7        pk         } else {
    255   1.7        pk                 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    256   1.7        pk                 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    257   1.7        pk         }
    258   1.7        pk 
    259  1.45    bouyer 	if (padbuf == NULL) {
    260  1.45    bouyer 		padbuf = malloc(ETHER_MIN_LEN - ETHER_CRC_LEN, M_DEVBUF,
    261  1.45    bouyer 		    M_ZERO | M_NOWAIT);
    262  1.45    bouyer 		if (padbuf == NULL) {
    263  1.73   msaitoh 			 aprint_error_dev(sc->sc_dev,
    264  1.73   msaitoh 			     "can't allocate pad buffer\n");
    265  1.45    bouyer 			 return;
    266  1.45    bouyer 		}
    267  1.45    bouyer 	}
    268  1.45    bouyer 
    269   1.1        pk 	/* Attach the interface. */
    270   1.1        pk 	if_attach(ifp);
    271  1.76     ozaki 	if_deferred_start_init(ifp, NULL);
    272   1.1        pk 	ether_ifattach(ifp, etheraddr);
    273   1.1        pk 
    274  1.74   msaitoh 	aprint_normal(" address %s, type %s\n", ether_sprintf(etheraddr),name);
    275   1.1        pk }
    276   1.1        pk 
    277   1.1        pk 
    278   1.1        pk /*
    279   1.9        pk  * Device timeout/watchdog routine.
    280   1.9        pk  * Entered if the device neglects to generate an interrupt after a
    281   1.9        pk  * transmit has been started on it.
    282   1.1        pk  */
    283   1.1        pk void
    284  1.65       dsl i82586_watchdog(struct ifnet *ifp)
    285   1.1        pk {
    286   1.1        pk 	struct ie_softc *sc = ifp->if_softc;
    287   1.1        pk 
    288  1.69   tsutsui 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    289   1.1        pk 	++ifp->if_oerrors;
    290   1.1        pk 
    291   1.7        pk 	i82586_reset(sc, 1);
    292   1.1        pk }
    293   1.1        pk 
    294   1.9        pk static int
    295  1.65       dsl i82586_cmd_wait(struct ie_softc *sc)
    296   1.9        pk {
    297   1.9        pk 	/* spin on i82586 command acknowledge; wait at most 0.9 (!) seconds */
    298   1.9        pk 	int i, off;
    299  1.34     bjh21 	u_int16_t cmd;
    300   1.9        pk 
    301   1.9        pk 	for (i = 0; i < 900000; i++) {
    302   1.9        pk 		/* Read the command word */
    303   1.9        pk 		off = IE_SCB_CMD(sc->scb);
    304  1.34     bjh21 
    305  1.34     bjh21 		IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
    306  1.34     bjh21 		if ((cmd = sc->ie_bus_read16(sc, off)) == 0)
    307   1.9        pk 			return (0);
    308   1.9        pk 		delay(1);
    309   1.9        pk 	}
    310   1.9        pk 
    311  1.34     bjh21 	off = IE_SCB_STATUS(sc->scb);
    312  1.34     bjh21 	printf("i82586_cmd_wait: timo(%ssync): scb status: 0x%x, cmd: 0x%x\n",
    313  1.51     perry 		sc->async_cmd_inprogress?"a":"",
    314  1.34     bjh21 		sc->ie_bus_read16(sc, off), cmd);
    315  1.34     bjh21 
    316   1.9        pk 	return (1);	/* Timeout */
    317   1.9        pk }
    318   1.9        pk 
    319   1.1        pk /*
    320   1.9        pk  * Send a command to the controller and wait for it to either complete
    321   1.9        pk  * or be accepted, depending on the command.  If the command pointer
    322   1.9        pk  * is null, then pretend that the command is not an action command.
    323   1.9        pk  * If the command pointer is not null, and the command is an action
    324   1.9        pk  * command, wait for one of the MASK bits to turn on in the command's
    325   1.9        pk  * status field.
    326   1.9        pk  * If ASYNC is set, we just call the chip's attention and return.
    327   1.9        pk  * We may have to wait for the command's acceptance later though.
    328   1.1        pk  */
    329   1.9        pk static int
    330  1.73   msaitoh i82586_start_cmd(struct ie_softc *sc, int cmd, int iecmdbuf, int mask,
    331  1.73   msaitoh     int async)
    332   1.9        pk {
    333   1.9        pk 	int i;
    334   1.9        pk 	int off;
    335   1.9        pk 
    336  1.10        pk 	if (sc->async_cmd_inprogress != 0) {
    337   1.9        pk 		/*
    338   1.9        pk 		 * If previous command was issued asynchronously, wait
    339   1.9        pk 		 * for it now.
    340   1.9        pk 		 */
    341   1.9        pk 		if (i82586_cmd_wait(sc) != 0)
    342   1.9        pk 			return (1);
    343  1.10        pk 		sc->async_cmd_inprogress = 0;
    344   1.9        pk 	}
    345   1.9        pk 
    346   1.9        pk 	off = IE_SCB_CMD(sc->scb);
    347  1.34     bjh21 	sc->ie_bus_write16(sc, off, cmd);
    348  1.34     bjh21 	IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_WRITE);
    349  1.36  jdolecek 	(sc->chan_attn)(sc, CARD_RESET);
    350   1.9        pk 
    351   1.9        pk 	if (async != 0) {
    352  1.10        pk 		sc->async_cmd_inprogress = 1;
    353   1.9        pk 		return (0);
    354   1.9        pk 	}
    355   1.9        pk 
    356   1.9        pk 	if (IE_ACTION_COMMAND(cmd) && iecmdbuf) {
    357   1.9        pk 		int status;
    358   1.9        pk 		/*
    359   1.9        pk 		 * Now spin-lock waiting for status.  This is not a very nice
    360   1.9        pk 		 * thing to do, and can kill performance pretty well...
    361   1.9        pk 		 * According to the packet driver, the minimum timeout
    362   1.9        pk 		 * should be .369 seconds.
    363   1.9        pk 		 */
    364   1.9        pk 		for (i = 0; i < 369000; i++) {
    365   1.9        pk 			/* Read the command status */
    366   1.9        pk 			off = IE_CMD_COMMON_STATUS(iecmdbuf);
    367  1.34     bjh21 			IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
    368  1.34     bjh21 			status = sc->ie_bus_read16(sc, off);
    369   1.9        pk 			if (status & mask)
    370   1.9        pk 				return (0);
    371   1.9        pk 			delay(1);
    372   1.9        pk 		}
    373   1.9        pk 
    374   1.9        pk 	} else {
    375   1.9        pk 		/*
    376   1.9        pk 		 * Otherwise, just wait for the command to be accepted.
    377   1.9        pk 		 */
    378   1.9        pk 		return (i82586_cmd_wait(sc));
    379   1.9        pk 	}
    380   1.9        pk 
    381   1.9        pk 	/* Timeout */
    382   1.9        pk 	return (1);
    383   1.9        pk }
    384   1.9        pk 
    385   1.9        pk /*
    386   1.9        pk  * Interrupt Acknowledge.
    387   1.9        pk  */
    388  1.55     perry static inline void
    389  1.66       dsl ie_ack(struct ie_softc *sc, u_int mask)
    390  1.66       dsl 	/* mask:	 in native byte-order */
    391   1.1        pk {
    392  1.10        pk 	u_int status;
    393   1.1        pk 
    394  1.34     bjh21 	IE_BUS_BARRIER(sc, 0, 0, BUS_SPACE_BARRIER_READ);
    395  1.34     bjh21 	status = sc->ie_bus_read16(sc, IE_SCB_STATUS(sc->scb));
    396  1.10        pk 	i82586_start_cmd(sc, status & mask, 0, 0, 0);
    397  1.27     bjh21 	if (sc->intrhook)
    398  1.27     bjh21 		sc->intrhook(sc, INTR_ACK);
    399   1.1        pk }
    400   1.1        pk 
    401   1.9        pk /*
    402   1.9        pk  * Transfer accumulated chip error counters to IF.
    403   1.9        pk  */
    404  1.54     perry static inline void
    405  1.65       dsl i82586_count_errors(struct ie_softc *sc)
    406   1.9        pk {
    407   1.9        pk 	int scb = sc->scb;
    408   1.9        pk 
    409   1.9        pk 	sc->sc_ethercom.ec_if.if_ierrors +=
    410   1.9        pk 	    sc->ie_bus_read16(sc, IE_SCB_ERRCRC(scb)) +
    411   1.9        pk 	    sc->ie_bus_read16(sc, IE_SCB_ERRALN(scb)) +
    412   1.9        pk 	    sc->ie_bus_read16(sc, IE_SCB_ERRRES(scb)) +
    413   1.9        pk 	    sc->ie_bus_read16(sc, IE_SCB_ERROVR(scb));
    414   1.3        pk 
    415   1.9        pk 	/* Clear error counters */
    416   1.9        pk 	sc->ie_bus_write16(sc, IE_SCB_ERRCRC(scb), 0);
    417   1.9        pk 	sc->ie_bus_write16(sc, IE_SCB_ERRALN(scb), 0);
    418   1.9        pk 	sc->ie_bus_write16(sc, IE_SCB_ERRRES(scb), 0);
    419   1.9        pk 	sc->ie_bus_write16(sc, IE_SCB_ERROVR(scb), 0);
    420   1.9        pk }
    421   1.9        pk 
    422   1.9        pk static void
    423  1.65       dsl i82586_rx_errors(struct ie_softc *sc, int fn, int status)
    424   1.9        pk {
    425   1.9        pk 	char bits[128];
    426  1.64  christos 	snprintb(bits, sizeof(bits), IE_FD_STATUSBITS, status);
    427  1.64  christos 	log(LOG_ERR, "%s: rx error (frame# %d): %s\n",
    428  1.69   tsutsui 	    device_xname(sc->sc_dev), fn, bits);
    429   1.9        pk }
    430   1.9        pk 
    431   1.9        pk /*
    432   1.9        pk  * i82586 interrupt entry point.
    433   1.9        pk  */
    434   1.9        pk int
    435  1.65       dsl i82586_intr(void *v)
    436   1.1        pk {
    437   1.9        pk 	struct ie_softc *sc = v;
    438   1.9        pk 	u_int status;
    439   1.9        pk 	int off;
    440   1.9        pk 
    441   1.9        pk         /*
    442   1.9        pk          * Implementation dependent interrupt handling.
    443   1.9        pk          */
    444   1.9        pk 	if (sc->intrhook)
    445   1.9        pk 		(sc->intrhook)(sc, INTR_ENTER);
    446   1.9        pk 
    447   1.9        pk 	off = IE_SCB_STATUS(sc->scb);
    448  1.34     bjh21 	IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
    449   1.9        pk 	status = sc->ie_bus_read16(sc, off) & IE_ST_WHENCE;
    450   1.9        pk 
    451   1.9        pk 	if ((status & IE_ST_WHENCE) == 0) {
    452   1.9        pk 		if (sc->intrhook)
    453   1.9        pk 			(sc->intrhook)(sc, INTR_EXIT);
    454   1.9        pk 
    455   1.9        pk 		return (0);
    456   1.9        pk 	}
    457   1.9        pk 
    458   1.9        pk loop:
    459   1.9        pk 	/* Ack interrupts FIRST in case we receive more during the ISR. */
    460   1.9        pk #if 0
    461   1.9        pk 	ie_ack(sc, status & IE_ST_WHENCE);
    462   1.9        pk #endif
    463   1.9        pk 	i82586_start_cmd(sc, status & IE_ST_WHENCE, 0, 0, 1);
    464   1.9        pk 
    465   1.9        pk 	if (status & (IE_ST_FR | IE_ST_RNR))
    466   1.9        pk 		if (i82586_rint(sc, status) != 0)
    467   1.9        pk 			goto reset;
    468   1.9        pk 
    469   1.9        pk 	if (status & IE_ST_CX)
    470   1.9        pk 		if (i82586_tint(sc, status) != 0)
    471   1.9        pk 			goto reset;
    472   1.9        pk 
    473  1.25        pk #if I82586_DEBUG
    474   1.9        pk 	if ((status & IE_ST_CNA) && (sc->sc_debug & IED_CNA))
    475  1.73   msaitoh 		printf("%s: cna; status=0x%x\n", device_xname(sc->sc_dev),
    476  1.73   msaitoh 		    status);
    477   1.9        pk #endif
    478   1.9        pk 	if (sc->intrhook)
    479   1.9        pk 		(sc->intrhook)(sc, INTR_LOOP);
    480   1.9        pk 
    481   1.9        pk 	/*
    482   1.9        pk 	 * Interrupt ACK was posted asynchronously; wait for
    483   1.9        pk 	 * completion here before reading SCB status again.
    484  1.51     perry 	 *
    485  1.34     bjh21 	 * If ACK fails, try to reset the chip, in hopes that
    486  1.34     bjh21 	 * it helps.
    487   1.9        pk 	 */
    488  1.34     bjh21 	if (i82586_cmd_wait(sc) != 0)
    489  1.34     bjh21 		goto reset;
    490   1.9        pk 
    491  1.34     bjh21 	IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
    492   1.9        pk 	status = sc->ie_bus_read16(sc, off);
    493   1.9        pk 	if ((status & IE_ST_WHENCE) != 0)
    494   1.9        pk 		goto loop;
    495   1.9        pk 
    496  1.10        pk out:
    497   1.9        pk 	if (sc->intrhook)
    498   1.9        pk 		(sc->intrhook)(sc, INTR_EXIT);
    499   1.9        pk 	return (1);
    500   1.9        pk 
    501   1.9        pk reset:
    502   1.9        pk 	i82586_cmd_wait(sc);
    503   1.9        pk 	i82586_reset(sc, 1);
    504  1.10        pk 	goto out;
    505  1.10        pk 
    506   1.9        pk }
    507   1.9        pk 
    508   1.9        pk /*
    509   1.9        pk  * Process a received-frame interrupt.
    510   1.9        pk  */
    511   1.9        pk int
    512  1.65       dsl i82586_rint(struct ie_softc *sc, int scbstatus)
    513   1.9        pk {
    514   1.9        pk static	int timesthru = 1024;
    515   1.9        pk 	int i, status, off;
    516   1.9        pk 
    517  1.25        pk #if I82586_DEBUG
    518   1.9        pk 	if (sc->sc_debug & IED_RINT)
    519   1.9        pk 		printf("%s: rint: status 0x%x\n",
    520  1.69   tsutsui 			device_xname(sc->sc_dev), scbstatus);
    521   1.9        pk #endif
    522   1.9        pk 
    523   1.9        pk 	for (;;) {
    524   1.9        pk 		int drop = 0;
    525   1.9        pk 
    526   1.9        pk 		i = sc->rfhead;
    527   1.9        pk 		off = IE_RFRAME_STATUS(sc->rframes, i);
    528  1.34     bjh21 		IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
    529   1.9        pk 		status = sc->ie_bus_read16(sc, off);
    530   1.1        pk 
    531  1.25        pk #if I82586_DEBUG
    532   1.9        pk 		if (sc->sc_debug & IED_RINT)
    533   1.9        pk 			printf("%s: rint: frame(%d) status 0x%x\n",
    534  1.69   tsutsui 				device_xname(sc->sc_dev), i, status);
    535   1.1        pk #endif
    536   1.9        pk 		if ((status & IE_FD_COMPLETE) == 0) {
    537   1.9        pk 			if ((status & IE_FD_OK) != 0) {
    538  1.10        pk 				printf("%s: rint: weird: ",
    539  1.69   tsutsui 					device_xname(sc->sc_dev));
    540   1.9        pk 				i82586_rx_errors(sc, i, status);
    541   1.9        pk 				break;
    542   1.9        pk 			}
    543   1.9        pk 			if (--timesthru == 0) {
    544   1.9        pk 				/* Account the accumulated errors */
    545   1.9        pk 				i82586_count_errors(sc);
    546   1.9        pk 				timesthru = 1024;
    547   1.9        pk 			}
    548   1.9        pk 			break;
    549   1.9        pk 		} else if ((status & IE_FD_OK) == 0) {
    550   1.9        pk 			/*
    551   1.9        pk 			 * If the chip is configured to automatically
    552   1.9        pk 			 * discard bad frames, the only reason we can
    553   1.9        pk 			 * get here is an "out-of-resource" condition.
    554   1.9        pk 			 */
    555   1.9        pk 			i82586_rx_errors(sc, i, status);
    556   1.9        pk 			drop = 1;
    557   1.1        pk 		}
    558   1.1        pk 
    559  1.25        pk #if I82586_DEBUG
    560   1.9        pk 		if ((status & IE_FD_BUSY) != 0)
    561   1.9        pk 			printf("%s: rint: frame(%d) busy; status=0x%x\n",
    562  1.69   tsutsui 				device_xname(sc->sc_dev), i, status);
    563   1.9        pk #endif
    564   1.9        pk 
    565   1.9        pk 
    566   1.9        pk 		/*
    567   1.9        pk 		 * Advance the RFD list, since we're done with
    568   1.9        pk 		 * this descriptor.
    569   1.9        pk 		 */
    570   1.9        pk 
    571   1.9        pk 		/* Clear frame status */
    572   1.9        pk 		sc->ie_bus_write16(sc, off, 0);
    573   1.1        pk 
    574   1.9        pk 		/* Put fence at this frame (the head) */
    575   1.9        pk 		off = IE_RFRAME_LAST(sc->rframes, i);
    576   1.9        pk 		sc->ie_bus_write16(sc, off, IE_FD_EOL|IE_FD_SUSP);
    577   1.9        pk 
    578   1.9        pk 		/* and clear RBD field */
    579   1.9        pk 		off = IE_RFRAME_BUFDESC(sc->rframes, i);
    580   1.9        pk 		sc->ie_bus_write16(sc, off, 0xffff);
    581   1.9        pk 
    582   1.9        pk 		/* Remove fence from current tail */
    583   1.9        pk 		off = IE_RFRAME_LAST(sc->rframes, sc->rftail);
    584   1.9        pk 		sc->ie_bus_write16(sc, off, 0);
    585   1.9        pk 
    586   1.9        pk 		if (++sc->rftail == sc->nframes)
    587   1.9        pk 			sc->rftail = 0;
    588   1.9        pk 		if (++sc->rfhead == sc->nframes)
    589   1.9        pk 			sc->rfhead = 0;
    590   1.9        pk 
    591   1.9        pk 		/* Pull the frame off the board */
    592   1.9        pk 		if (drop) {
    593   1.9        pk 			i82586_drop_frames(sc);
    594  1.10        pk 			if ((status & IE_FD_RNR) != 0)
    595   1.9        pk 				sc->rnr_expect = 1;
    596   1.9        pk 			sc->sc_ethercom.ec_if.if_ierrors++;
    597   1.9        pk 		} else if (ie_readframe(sc, i) != 0)
    598   1.9        pk 			return (1);
    599   1.9        pk 	}
    600   1.9        pk 
    601  1.10        pk 	if ((scbstatus & IE_ST_RNR) != 0) {
    602  1.10        pk 
    603  1.10        pk 		/*
    604  1.10        pk 		 * Receiver went "Not Ready". We try to figure out
    605  1.10        pk 		 * whether this was an expected event based on past
    606  1.10        pk 		 * frame status values.
    607  1.10        pk 		 */
    608   1.9        pk 
    609  1.10        pk 		if ((scbstatus & IE_RUS_SUSPEND) != 0) {
    610  1.10        pk 			/*
    611  1.10        pk 			 * We use the "suspend on last frame" flag.
    612  1.10        pk 			 * Send a RU RESUME command in response, since
    613  1.10        pk 			 * we should have dealt with all completed frames
    614  1.10        pk 			 * by now.
    615  1.10        pk 			 */
    616  1.10        pk 			printf("RINT: SUSPENDED; scbstatus=0x%x\n",
    617  1.10        pk 				scbstatus);
    618   1.9        pk 			if (i82586_start_cmd(sc, IE_RUC_RESUME, 0, 0, 0) == 0)
    619   1.9        pk 				return (0);
    620  1.73   msaitoh 			aprint_error_dev(sc->sc_dev,
    621  1.73   msaitoh 			    "RU RESUME command timed out\n");
    622  1.10        pk 			return (1);	/* Ask for a reset */
    623   1.1        pk 		}
    624  1.10        pk 
    625  1.10        pk 		if (sc->rnr_expect != 0) {
    626  1.10        pk 			/*
    627  1.10        pk 			 * The RNR condition was announced in the previously
    628  1.10        pk 			 * completed frame.  Assume the receive ring is Ok,
    629  1.10        pk 			 * so restart the receiver without further delay.
    630  1.10        pk 			 */
    631   1.9        pk 			i82586_start_transceiver(sc);
    632   1.9        pk 			sc->rnr_expect = 0;
    633   1.9        pk 			return (0);
    634  1.10        pk 
    635  1.10        pk 		} else if ((scbstatus & IE_RUS_NOSPACE) != 0) {
    636  1.10        pk 			/*
    637  1.10        pk 			 * We saw no previous IF_FD_RNR flag.
    638  1.10        pk 			 * We check our ring invariants and, if ok,
    639  1.10        pk 			 * just restart the receiver at the current
    640  1.10        pk 			 * point in the ring.
    641  1.10        pk 			 */
    642  1.10        pk 			if (i82586_chk_rx_ring(sc) != 0)
    643  1.10        pk 				return (1);
    644  1.10        pk 
    645  1.10        pk 			i82586_start_transceiver(sc);
    646  1.10        pk 			sc->sc_ethercom.ec_if.if_ierrors++;
    647  1.10        pk 			return (0);
    648   1.9        pk 		} else
    649   1.9        pk 			printf("%s: receiver not ready; scbstatus=0x%x\n",
    650  1.69   tsutsui 				device_xname(sc->sc_dev), scbstatus);
    651  1.10        pk 
    652   1.9        pk 		sc->sc_ethercom.ec_if.if_ierrors++;
    653  1.10        pk 		return (1);	/* Ask for a reset */
    654   1.9        pk 	}
    655  1.10        pk 
    656   1.9        pk 	return (0);
    657   1.1        pk }
    658   1.1        pk 
    659   1.1        pk /*
    660   1.9        pk  * Process a command-complete interrupt.  These are only generated by the
    661  1.10        pk  * transmission of frames.  This routine is deceptively simple, since most
    662  1.10        pk  * of the real work is done by i82586_start().
    663   1.1        pk  */
    664   1.9        pk int
    665  1.65       dsl i82586_tint(struct ie_softc *sc, int scbstatus)
    666   1.1        pk {
    667   1.9        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    668   1.9        pk 	int status;
    669   1.6        pk 
    670   1.9        pk 	ifp->if_timer = 0;
    671   1.9        pk 	ifp->if_flags &= ~IFF_OACTIVE;
    672   1.1        pk 
    673  1.25        pk #if I82586_DEBUG
    674   1.9        pk 	if (sc->xmit_busy <= 0) {
    675   1.9        pk 	    printf("i82586_tint: WEIRD: xmit_busy=%d, xctail=%d, xchead=%d\n",
    676   1.9        pk 		   sc->xmit_busy, sc->xctail, sc->xchead);
    677  1.10        pk 		return (0);
    678   1.9        pk 	}
    679   1.1        pk #endif
    680   1.1        pk 
    681   1.9        pk 	status = sc->ie_bus_read16(sc, IE_CMD_XMIT_STATUS(sc->xmit_cmds,
    682   1.9        pk 							  sc->xctail));
    683   1.6        pk 
    684  1.25        pk #if I82586_DEBUG
    685   1.9        pk 	if (sc->sc_debug & IED_TINT)
    686   1.9        pk 		printf("%s: tint: SCB status 0x%x; xmit status 0x%x\n",
    687  1.69   tsutsui 			device_xname(sc->sc_dev), scbstatus, status);
    688   1.9        pk #endif
    689   1.6        pk 
    690   1.9        pk 	if ((status & IE_STAT_COMPL) == 0 || (status & IE_STAT_BUSY)) {
    691   1.9        pk 	    printf("i82586_tint: command still busy; status=0x%x; tail=%d\n",
    692   1.9        pk 		   status, sc->xctail);
    693   1.9        pk 	    printf("iestatus = 0x%x\n", scbstatus);
    694   1.9        pk 	}
    695   1.9        pk 
    696   1.9        pk 	if (status & IE_STAT_OK) {
    697   1.9        pk 		ifp->if_opackets++;
    698   1.9        pk 		ifp->if_collisions += (status & IE_XS_MAXCOLL);
    699   1.9        pk 	} else {
    700   1.9        pk 		ifp->if_oerrors++;
    701   1.9        pk 		/*
    702   1.9        pk 		 * Check SQE and DEFERRED?
    703   1.9        pk 		 * What if more than one bit is set?
    704   1.9        pk 		 */
    705   1.9        pk 		if (status & IE_STAT_ABORT)
    706  1.69   tsutsui 			aprint_error_dev(sc->sc_dev, "send aborted\n");
    707   1.9        pk 		else if (status & IE_XS_NOCARRIER)
    708  1.69   tsutsui 			aprint_error_dev(sc->sc_dev, "no carrier\n");
    709   1.9        pk 		else if (status & IE_XS_LOSTCTS)
    710  1.69   tsutsui 			aprint_error_dev(sc->sc_dev, "lost CTS\n");
    711   1.9        pk 		else if (status & IE_XS_UNDERRUN)
    712  1.69   tsutsui 			aprint_error_dev(sc->sc_dev, "DMA underrun\n");
    713   1.9        pk 		else if (status & IE_XS_EXCMAX) {
    714  1.69   tsutsui 			aprint_error_dev(sc->sc_dev, "too many collisions\n");
    715   1.9        pk 			sc->sc_ethercom.ec_if.if_collisions += 16;
    716   1.9        pk 		}
    717   1.9        pk 	}
    718   1.9        pk 
    719   1.9        pk 	/*
    720   1.9        pk 	 * If multicast addresses were added or deleted while transmitting,
    721   1.9        pk 	 * ie_mc_reset() set the want_mcsetup flag indicating that we
    722   1.9        pk 	 * should do it.
    723   1.9        pk 	 */
    724   1.9        pk 	if (sc->want_mcsetup) {
    725   1.9        pk 		ie_mc_setup(sc, IE_XBUF_ADDR(sc, sc->xctail));
    726   1.9        pk 		sc->want_mcsetup = 0;
    727   1.9        pk 	}
    728   1.9        pk 
    729   1.9        pk 	/* Done with the buffer. */
    730   1.9        pk 	sc->xmit_busy--;
    731   1.9        pk 	sc->xctail = (sc->xctail + 1) % NTXBUF;
    732   1.9        pk 
    733   1.9        pk 	/* Start the next packet, if any, transmitting. */
    734   1.9        pk 	if (sc->xmit_busy > 0)
    735   1.9        pk 		iexmit(sc);
    736   1.9        pk 
    737  1.76     ozaki 	if_schedule_deferred_start(ifp);
    738   1.9        pk 	return (0);
    739   1.9        pk }
    740   1.9        pk 
    741   1.9        pk /*
    742   1.9        pk  * Get a range of receive buffer descriptors that represent one packet.
    743   1.9        pk  */
    744   1.9        pk static int
    745  1.73   msaitoh i82586_get_rbd_list(struct ie_softc *sc, u_int16_t *start, u_int16_t *end,
    746  1.73   msaitoh     int *pktlen)
    747   1.9        pk {
    748   1.9        pk 	int	off, rbbase = sc->rbds;
    749   1.9        pk 	int	rbindex, count = 0;
    750   1.9        pk 	int	plen = 0;
    751   1.9        pk 	int	rbdstatus;
    752   1.9        pk 
    753   1.9        pk 	*start = rbindex = sc->rbhead;
    754   1.9        pk 
    755   1.9        pk 	do {
    756   1.9        pk 		off = IE_RBD_STATUS(rbbase, rbindex);
    757  1.34     bjh21 		IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
    758   1.9        pk 		rbdstatus = sc->ie_bus_read16(sc, off);
    759   1.9        pk 		if ((rbdstatus & IE_RBD_USED) == 0) {
    760   1.9        pk 			/*
    761   1.9        pk 			 * This means we are somehow out of sync.  So, we
    762   1.9        pk 			 * reset the adapter.
    763   1.9        pk 			 */
    764  1.25        pk #if I82586_DEBUG
    765   1.9        pk 			print_rbd(sc, rbindex);
    766   1.9        pk #endif
    767   1.9        pk 			log(LOG_ERR,
    768   1.9        pk 			    "%s: receive descriptors out of sync at %d\n",
    769  1.69   tsutsui 			    device_xname(sc->sc_dev), rbindex);
    770   1.9        pk 			return (0);
    771   1.9        pk 		}
    772   1.9        pk 		plen += (rbdstatus & IE_RBD_CNTMASK);
    773   1.9        pk 
    774   1.9        pk 		if (++rbindex == sc->nrxbuf)
    775   1.9        pk 			rbindex = 0;
    776   1.9        pk 
    777   1.9        pk 		++count;
    778   1.9        pk 	} while ((rbdstatus & IE_RBD_LAST) == 0);
    779   1.9        pk 	*end = rbindex;
    780   1.9        pk 	*pktlen = plen;
    781   1.9        pk 	return (count);
    782   1.9        pk }
    783   1.6        pk 
    784   1.6        pk 
    785   1.9        pk /*
    786   1.9        pk  * Release a range of receive buffer descriptors after we've copied the packet.
    787   1.9        pk  */
    788   1.9        pk static void
    789  1.65       dsl i82586_release_rbd_list(struct ie_softc *sc, u_int16_t start, u_int16_t end)
    790   1.9        pk {
    791   1.9        pk 	int	off, rbbase = sc->rbds;
    792   1.9        pk 	int	rbindex = start;
    793   1.6        pk 
    794   1.9        pk 	do {
    795   1.9        pk 		/* Clear buffer status */
    796   1.9        pk 		off = IE_RBD_STATUS(rbbase, rbindex);
    797   1.9        pk 		sc->ie_bus_write16(sc, off, 0);
    798   1.9        pk 		if (++rbindex == sc->nrxbuf)
    799   1.9        pk 			rbindex = 0;
    800   1.9        pk 	} while (rbindex != end);
    801   1.9        pk 
    802   1.9        pk 	/* Mark EOL at new tail */
    803   1.9        pk 	rbindex = ((rbindex == 0) ? sc->nrxbuf : rbindex) - 1;
    804   1.9        pk 	off = IE_RBD_BUFLEN(rbbase, rbindex);
    805   1.9        pk 	sc->ie_bus_write16(sc, off, IE_RBUF_SIZE|IE_RBD_EOL);
    806   1.9        pk 
    807   1.9        pk 	/* Remove EOL from current tail */
    808   1.9        pk 	off = IE_RBD_BUFLEN(rbbase, sc->rbtail);
    809   1.9        pk 	sc->ie_bus_write16(sc, off, IE_RBUF_SIZE);
    810   1.9        pk 
    811   1.9        pk 	/* New head & tail pointer */
    812   1.9        pk /* hmm, why have both? head is always (tail + 1) % NRXBUF */
    813   1.9        pk 	sc->rbhead = end;
    814   1.9        pk 	sc->rbtail = rbindex;
    815   1.9        pk }
    816   1.6        pk 
    817  1.10        pk /*
    818  1.10        pk  * Drop the packet at the head of the RX buffer ring.
    819  1.10        pk  * Called if the frame descriptor reports an error on this packet.
    820  1.10        pk  * Returns 1 if the buffer descriptor ring appears to be corrupt;
    821  1.10        pk  * and 0 otherwise.
    822  1.10        pk  */
    823   1.9        pk static int
    824  1.65       dsl i82586_drop_frames(struct ie_softc *sc)
    825   1.9        pk {
    826   1.9        pk 	u_int16_t bstart, bend;
    827   1.9        pk 	int pktlen;
    828   1.1        pk 
    829   1.9        pk 	if (i82586_get_rbd_list(sc, &bstart, &bend, &pktlen) == 0)
    830   1.9        pk 		return (1);
    831   1.9        pk 	i82586_release_rbd_list(sc, bstart, bend);
    832   1.9        pk 	return (0);
    833   1.1        pk }
    834   1.1        pk 
    835   1.1        pk /*
    836  1.10        pk  * Check the RX frame & buffer descriptor lists for our invariants,
    837  1.10        pk  * i.e.: EOL bit set iff. it is pointed at by the r*tail pointer.
    838  1.10        pk  *
    839  1.10        pk  * Called when the receive unit has stopped unexpectedly.
    840  1.10        pk  * Returns 1 if an inconsistency is detected; 0 otherwise.
    841  1.10        pk  *
    842  1.10        pk  * The Receive Unit is expected to be NOT RUNNING.
    843  1.10        pk  */
    844  1.10        pk static int
    845  1.65       dsl i82586_chk_rx_ring(struct ie_softc *sc)
    846  1.10        pk {
    847  1.10        pk 	int n, off, val;
    848  1.10        pk 
    849  1.10        pk 	for (n = 0; n < sc->nrxbuf; n++) {
    850  1.10        pk 		off = IE_RBD_BUFLEN(sc->rbds, n);
    851  1.10        pk 		val = sc->ie_bus_read16(sc, off);
    852  1.10        pk 		if ((n == sc->rbtail) ^ ((val & IE_RBD_EOL) != 0)) {
    853  1.10        pk 			/* `rbtail' and EOL flag out of sync */
    854  1.10        pk 			log(LOG_ERR,
    855  1.10        pk 			    "%s: rx buffer descriptors out of sync at %d\n",
    856  1.69   tsutsui 			    device_xname(sc->sc_dev), n);
    857  1.10        pk 			return (1);
    858  1.10        pk 		}
    859  1.10        pk 
    860  1.10        pk 		/* Take the opportunity to clear the status fields here ? */
    861  1.10        pk 	}
    862  1.10        pk 
    863  1.10        pk 	for (n = 0; n < sc->nframes; n++) {
    864  1.10        pk 		off = IE_RFRAME_LAST(sc->rframes, n);
    865  1.10        pk 		val = sc->ie_bus_read16(sc, off);
    866  1.10        pk 		if ((n == sc->rftail) ^ ((val & (IE_FD_EOL|IE_FD_SUSP)) != 0)) {
    867  1.10        pk 			/* `rftail' and EOL flag out of sync */
    868  1.10        pk 			log(LOG_ERR,
    869  1.10        pk 			    "%s: rx frame list out of sync at %d\n",
    870  1.69   tsutsui 			    device_xname(sc->sc_dev), n);
    871  1.10        pk 			return (1);
    872  1.10        pk 		}
    873  1.10        pk 	}
    874  1.10        pk 
    875  1.10        pk 	return (0);
    876  1.10        pk }
    877  1.10        pk 
    878  1.10        pk /*
    879   1.1        pk  * Read data off the interface, and turn it into an mbuf chain.
    880   1.1        pk  *
    881   1.1        pk  * This code is DRAMATICALLY different from the previous version; this
    882   1.1        pk  * version tries to allocate the entire mbuf chain up front, given the
    883   1.1        pk  * length of the data available.  This enables us to allocate mbuf
    884   1.1        pk  * clusters in many situations where before we would have had a long
    885   1.1        pk  * chain of partially-full mbufs.  This should help to speed up the
    886   1.1        pk  * operation considerably.  (Provided that it works, of course.)
    887   1.1        pk  */
    888  1.54     perry static inline struct mbuf *
    889  1.65       dsl ieget(struct ie_softc *sc, int head, int totlen)
    890   1.1        pk {
    891  1.19   mycroft 	struct mbuf *m, *m0, *newm;
    892   1.9        pk 	int len, resid;
    893   1.3        pk 	int thisrboff, thismboff;
    894  1.21   thorpej 	struct ether_header eh;
    895   1.1        pk 
    896   1.1        pk 	/*
    897   1.1        pk 	 * Snarf the Ethernet header.
    898   1.1        pk 	 */
    899  1.21   thorpej 	(sc->memcopyin)(sc, &eh, IE_RBUF_ADDR(sc, head),
    900  1.21   thorpej 	    sizeof(struct ether_header));
    901   1.1        pk 
    902  1.21   thorpej 	resid = totlen;
    903   1.1        pk 
    904  1.19   mycroft 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    905  1.19   mycroft 	if (m0 == 0)
    906  1.10        pk 		return (0);
    907  1.72     ozaki 	m_set_rcvif(m0, &sc->sc_ethercom.ec_if);
    908  1.19   mycroft 	m0->m_pkthdr.len = totlen;
    909   1.3        pk 	len = MHLEN;
    910  1.19   mycroft 	m = m0;
    911   1.1        pk 
    912   1.1        pk 	/*
    913   1.1        pk 	 * This loop goes through and allocates mbufs for all the data we will
    914   1.1        pk 	 * be copying in.  It does not actually do the copying yet.
    915   1.1        pk 	 */
    916   1.3        pk 	while (totlen > 0) {
    917   1.3        pk 		if (totlen >= MINCLSIZE) {
    918   1.1        pk 			MCLGET(m, M_DONTWAIT);
    919  1.19   mycroft 			if ((m->m_flags & M_EXT) == 0)
    920  1.19   mycroft 				goto bad;
    921   1.3        pk 			len = MCLBYTES;
    922  1.22        pk 		}
    923  1.22        pk 
    924  1.22        pk 		if (m == m0) {
    925  1.58  christos 			char *newdata = (char *)
    926  1.22        pk 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    927  1.22        pk 			    sizeof(struct ether_header);
    928  1.22        pk 			len -= newdata - m->m_data;
    929  1.22        pk 			m->m_data = newdata;
    930   1.1        pk 		}
    931  1.19   mycroft 
    932  1.79  riastrad 		m->m_len = len = uimin(totlen, len);
    933  1.19   mycroft 
    934   1.3        pk 		totlen -= len;
    935  1.19   mycroft 		if (totlen > 0) {
    936  1.19   mycroft 			MGET(newm, M_DONTWAIT, MT_DATA);
    937  1.19   mycroft 			if (newm == 0)
    938  1.19   mycroft 				goto bad;
    939  1.19   mycroft 			len = MLEN;
    940  1.19   mycroft 			m = m->m_next = newm;
    941  1.19   mycroft 		}
    942   1.3        pk 	}
    943   1.1        pk 
    944  1.19   mycroft 	m = m0;
    945   1.1        pk 	thismboff = 0;
    946   1.1        pk 
    947   1.1        pk 	/*
    948  1.21   thorpej 	 * Copy the Ethernet header into the mbuf chain.
    949  1.21   thorpej 	 */
    950  1.58  christos 	memcpy(mtod(m, void *), &eh, sizeof(struct ether_header));
    951  1.21   thorpej 	thismboff = sizeof(struct ether_header);
    952  1.21   thorpej 	thisrboff = sizeof(struct ether_header);
    953  1.21   thorpej 	resid -= sizeof(struct ether_header);
    954  1.21   thorpej 
    955  1.21   thorpej 	/*
    956   1.1        pk 	 * Now we take the mbuf chain (hopefully only one mbuf most of the
    957   1.9        pk 	 * time) and stuff the data into it.  There are no possible failures
    958   1.9        pk 	 * at or after this point.
    959   1.1        pk 	 */
    960   1.3        pk 	while (resid > 0) {
    961   1.9        pk 		int thisrblen = IE_RBUF_SIZE - thisrboff,
    962   1.3        pk 		    thismblen = m->m_len - thismboff;
    963  1.79  riastrad 		len = uimin(thisrblen, thismblen);
    964   1.3        pk 
    965  1.58  christos 		(sc->memcopyin)(sc, mtod(m, char *) + thismboff,
    966   1.9        pk 				IE_RBUF_ADDR(sc,head) + thisrboff,
    967   1.9        pk 				(u_int)len);
    968   1.3        pk 		resid -= len;
    969   1.1        pk 
    970   1.3        pk 		if (len == thismblen) {
    971   1.1        pk 			m = m->m_next;
    972   1.3        pk 			thismboff = 0;
    973   1.3        pk 		} else
    974   1.3        pk 			thismboff += len;
    975   1.3        pk 
    976   1.3        pk 		if (len == thisrblen) {
    977   1.9        pk 			if (++head == sc->nrxbuf)
    978   1.9        pk 				head = 0;
    979   1.3        pk 			thisrboff = 0;
    980   1.3        pk 		} else
    981   1.3        pk 			thisrboff += len;
    982   1.1        pk 	}
    983   1.1        pk 
    984   1.1        pk 	/*
    985   1.9        pk 	 * Unless something changed strangely while we were doing the copy,
    986   1.9        pk 	 * we have now copied everything in from the shared memory.
    987   1.1        pk 	 * This means that we are done.
    988   1.1        pk 	 */
    989  1.19   mycroft 	return (m0);
    990  1.19   mycroft 
    991  1.19   mycroft bad:
    992  1.19   mycroft 	m_freem(m0);
    993  1.19   mycroft 	return (0);
    994   1.1        pk }
    995   1.1        pk 
    996   1.1        pk /*
    997   1.1        pk  * Read frame NUM from unit UNIT (pre-cached as IE).
    998   1.1        pk  *
    999   1.1        pk  * This routine reads the RFD at NUM, and copies in the buffers from the list
   1000   1.9        pk  * of RBD, then rotates the RBD list so that the receiver doesn't start
   1001   1.9        pk  * complaining.  Trailers are DROPPED---there's no point in wasting time
   1002   1.9        pk  * on confusing code to deal with them.  Hopefully, this machine will
   1003   1.9        pk  * never ARP for trailers anyway.
   1004   1.1        pk  */
   1005   1.9        pk static int
   1006  1.56  christos ie_readframe(
   1007  1.56  christos     struct ie_softc *sc,
   1008  1.57  christos     int num)		/* frame number to read */
   1009   1.1        pk {
   1010   1.9        pk 	struct mbuf *m;
   1011   1.9        pk 	u_int16_t bstart, bend;
   1012   1.9        pk 	int pktlen;
   1013   1.1        pk 
   1014   1.9        pk 	if (i82586_get_rbd_list(sc, &bstart, &bend, &pktlen) == 0) {
   1015   1.9        pk 		sc->sc_ethercom.ec_if.if_ierrors++;
   1016   1.9        pk 		return (1);
   1017   1.9        pk 	}
   1018   1.1        pk 
   1019  1.29     bjh21 	m = ieget(sc, bstart, pktlen);
   1020   1.9        pk 	i82586_release_rbd_list(sc, bstart, bend);
   1021   1.9        pk 
   1022   1.3        pk 	if (m == 0) {
   1023   1.3        pk 		sc->sc_ethercom.ec_if.if_ierrors++;
   1024   1.9        pk 		return (0);
   1025   1.1        pk 	}
   1026   1.1        pk 
   1027  1.25        pk #if I82586_DEBUG
   1028  1.21   thorpej 	if (sc->sc_debug & IED_READFRAME) {
   1029  1.21   thorpej 		struct ether_header *eh = mtod(m, struct ether_header *);
   1030  1.21   thorpej 
   1031   1.9        pk 		printf("%s: frame from ether %s type 0x%x len %d\n",
   1032  1.69   tsutsui 			device_xname(sc->sc_dev),
   1033  1.21   thorpej 			ether_sprintf(eh->ether_shost),
   1034  1.27     bjh21 			(u_int)ntohs(eh->ether_type),
   1035   1.9        pk 			pktlen);
   1036  1.21   thorpej 	}
   1037   1.1        pk #endif
   1038   1.1        pk 
   1039   1.1        pk 	/*
   1040   1.1        pk 	 * Finally pass this packet up to higher layers.
   1041   1.1        pk 	 */
   1042  1.71     ozaki 	if_percpuq_enqueue((&sc->sc_ethercom.ec_if)->if_percpuq, m);
   1043   1.3        pk 	sc->sc_ethercom.ec_if.if_ipackets++;
   1044   1.9        pk 	return (0);
   1045   1.1        pk }
   1046   1.1        pk 
   1047   1.9        pk 
   1048   1.9        pk /*
   1049   1.9        pk  * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
   1050   1.9        pk  * command to the chip to be executed.
   1051   1.9        pk  */
   1052  1.55     perry static inline void
   1053  1.65       dsl iexmit(struct ie_softc *sc)
   1054   1.1        pk {
   1055   1.9        pk 	int off;
   1056   1.9        pk 	int cur, prev;
   1057   1.9        pk 
   1058   1.9        pk 	cur = sc->xctail;
   1059   1.1        pk 
   1060  1.25        pk #if I82586_DEBUG
   1061   1.9        pk 	if (sc->sc_debug & IED_XMIT)
   1062  1.69   tsutsui 		printf("%s: xmit buffer %d\n", device_xname(sc->sc_dev), cur);
   1063   1.1        pk #endif
   1064   1.9        pk 
   1065   1.9        pk 	/*
   1066   1.9        pk 	 * Setup the transmit command.
   1067   1.9        pk 	 */
   1068   1.9        pk 	sc->ie_bus_write16(sc, IE_CMD_XMIT_DESC(sc->xmit_cmds, cur),
   1069   1.9        pk 			       IE_XBD_ADDR(sc->xbds, cur));
   1070   1.9        pk 
   1071   1.9        pk 	sc->ie_bus_write16(sc, IE_CMD_XMIT_STATUS(sc->xmit_cmds, cur), 0);
   1072   1.9        pk 
   1073   1.9        pk 	if (sc->do_xmitnopchain) {
   1074   1.9        pk 		/*
   1075   1.9        pk 		 * Gate this XMIT command to the following NOP
   1076   1.9        pk 		 */
   1077   1.9        pk 		sc->ie_bus_write16(sc, IE_CMD_XMIT_LINK(sc->xmit_cmds, cur),
   1078   1.9        pk 				       IE_CMD_NOP_ADDR(sc->nop_cmds, cur));
   1079   1.9        pk 		sc->ie_bus_write16(sc, IE_CMD_XMIT_CMD(sc->xmit_cmds, cur),
   1080   1.9        pk 				       IE_CMD_XMIT | IE_CMD_INTR);
   1081   1.9        pk 
   1082   1.9        pk 		/*
   1083   1.9        pk 		 * Loopback at following NOP
   1084   1.9        pk 		 */
   1085   1.9        pk 		sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, cur), 0);
   1086   1.9        pk 		sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, cur),
   1087   1.9        pk 				       IE_CMD_NOP_ADDR(sc->nop_cmds, cur));
   1088   1.9        pk 
   1089   1.9        pk 		/*
   1090   1.9        pk 		 * Gate preceding NOP to this XMIT command
   1091   1.9        pk 		 */
   1092   1.9        pk 		prev = (cur + NTXBUF - 1) % NTXBUF;
   1093  1.73   msaitoh 		sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, prev),
   1094  1.73   msaitoh 		    0);
   1095   1.9        pk 		sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, prev),
   1096   1.9        pk 				       IE_CMD_XMIT_ADDR(sc->xmit_cmds, cur));
   1097   1.9        pk 
   1098   1.9        pk 		off = IE_SCB_STATUS(sc->scb);
   1099  1.34     bjh21 		IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
   1100   1.9        pk 		if ((sc->ie_bus_read16(sc, off) & IE_CUS_ACTIVE) == 0) {
   1101   1.9        pk 			printf("iexmit: CU not active\n");
   1102   1.9        pk 			i82586_start_transceiver(sc);
   1103   1.1        pk 		}
   1104   1.9        pk 	} else {
   1105   1.9        pk 		sc->ie_bus_write16(sc, IE_CMD_XMIT_LINK(sc->xmit_cmds,cur),
   1106   1.9        pk 				       0xffff);
   1107   1.9        pk 
   1108   1.9        pk 		sc->ie_bus_write16(sc, IE_CMD_XMIT_CMD(sc->xmit_cmds, cur),
   1109   1.9        pk 				       IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST);
   1110   1.1        pk 
   1111   1.9        pk 		off = IE_SCB_CMDLST(sc->scb);
   1112  1.73   msaitoh 		sc->ie_bus_write16(sc, off, IE_CMD_XMIT_ADDR(sc->xmit_cmds,
   1113  1.73   msaitoh 			cur));
   1114  1.34     bjh21 		IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
   1115   1.1        pk 
   1116   1.9        pk 		if (i82586_start_cmd(sc, IE_CUC_START, 0, 0, 1))
   1117  1.73   msaitoh 			aprint_error_dev(sc->sc_dev,
   1118  1.73   msaitoh 			    "iexmit: start xmit command timed out\n");
   1119   1.9        pk 	}
   1120   1.9        pk 
   1121   1.9        pk 	sc->sc_ethercom.ec_if.if_timer = 5;
   1122   1.1        pk }
   1123   1.1        pk 
   1124   1.1        pk 
   1125   1.1        pk /*
   1126   1.1        pk  * Start transmission on an interface.
   1127   1.1        pk  */
   1128   1.1        pk void
   1129  1.65       dsl i82586_start(struct ifnet *ifp)
   1130   1.1        pk {
   1131   1.1        pk 	struct ie_softc *sc = ifp->if_softc;
   1132   1.1        pk 	struct mbuf *m0, *m;
   1133   1.9        pk 	int	buffer, head, xbase;
   1134   1.9        pk 	u_short	len;
   1135   1.9        pk 	int	s;
   1136   1.1        pk 
   1137   1.3        pk 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1138   1.1        pk 		return;
   1139   1.1        pk 
   1140   1.3        pk 	for (;;) {
   1141   1.3        pk 		if (sc->xmit_busy == NTXBUF) {
   1142   1.3        pk 			ifp->if_flags |= IFF_OACTIVE;
   1143   1.3        pk 			break;
   1144   1.3        pk 		}
   1145   1.1        pk 
   1146   1.9        pk 		head = sc->xchead;
   1147   1.9        pk 		xbase = sc->xbds;
   1148   1.9        pk 
   1149  1.33   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1150   1.3        pk 		if (m0 == 0)
   1151   1.1        pk 			break;
   1152   1.1        pk 
   1153   1.3        pk 		/* We need to use m->m_pkthdr.len, so require the header */
   1154   1.3        pk 		if ((m0->m_flags & M_PKTHDR) == 0)
   1155   1.7        pk 			panic("i82586_start: no header mbuf");
   1156   1.3        pk 
   1157   1.3        pk 		/* Tap off here if there is a BPF listener. */
   1158  1.78   msaitoh 		bpf_mtap(ifp, m0, BPF_D_OUT);
   1159   1.3        pk 
   1160  1.25        pk #if I82586_DEBUG
   1161   1.3        pk 		if (sc->sc_debug & IED_ENQ)
   1162  1.69   tsutsui 			printf("%s: fill buffer %d\n", device_xname(sc->sc_dev),
   1163   1.3        pk 				sc->xchead);
   1164   1.3        pk #endif
   1165   1.3        pk 
   1166   1.3        pk 		if (m0->m_pkthdr.len > IE_TBUF_SIZE)
   1167  1.69   tsutsui 			printf("%s: tbuf overflow\n", device_xname(sc->sc_dev));
   1168   1.3        pk 
   1169   1.9        pk 		buffer = IE_XBUF_ADDR(sc, head);
   1170   1.3        pk 		for (m = m0; m != 0; m = m->m_next) {
   1171  1.58  christos 			(sc->memcopyout)(sc, mtod(m,void *), buffer, m->m_len);
   1172   1.1        pk 			buffer += m->m_len;
   1173   1.1        pk 		}
   1174  1.46    bouyer 		len = m0->m_pkthdr.len;
   1175  1.46    bouyer 		if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
   1176  1.45    bouyer 			(sc->memcopyout)(sc, padbuf, buffer,
   1177  1.46    bouyer 			    ETHER_MIN_LEN - ETHER_CRC_LEN - len);
   1178  1.46    bouyer 			buffer += ETHER_MIN_LEN -ETHER_CRC_LEN - len;
   1179  1.46    bouyer 			len = ETHER_MIN_LEN - ETHER_CRC_LEN;
   1180  1.45    bouyer 		}
   1181   1.1        pk 		m_freem(m0);
   1182   1.3        pk 
   1183   1.9        pk 		/*
   1184   1.9        pk 		 * Setup the transmit buffer descriptor here, while we
   1185   1.9        pk 		 * know the packet's length.
   1186   1.9        pk 		 */
   1187   1.9        pk 		sc->ie_bus_write16(sc, IE_XBD_FLAGS(xbase, head),
   1188   1.9        pk 				       len | IE_TBD_EOL);
   1189   1.9        pk 		sc->ie_bus_write16(sc, IE_XBD_NEXT(xbase, head), 0xffff);
   1190   1.9        pk 		sc->ie_bus_write24(sc, IE_XBD_BUF(xbase, head),
   1191   1.9        pk 				       IE_XBUF_ADDR(sc, head));
   1192   1.9        pk 
   1193   1.9        pk 		if (++head == NTXBUF)
   1194   1.9        pk 			head = 0;
   1195   1.9        pk 		sc->xchead = head;
   1196   1.6        pk 
   1197   1.6        pk 		s = splnet();
   1198   1.3        pk 		/* Start the first packet transmitting. */
   1199   1.3        pk 		if (sc->xmit_busy == 0)
   1200   1.3        pk 			iexmit(sc);
   1201   1.3        pk 
   1202   1.3        pk 		sc->xmit_busy++;
   1203   1.6        pk 		splx(s);
   1204   1.3        pk 	}
   1205   1.1        pk }
   1206   1.1        pk 
   1207   1.1        pk /*
   1208   1.9        pk  * Probe IE's ram setup   [ Move all this into MD front-end!? ]
   1209   1.9        pk  * Use only if SCP and ISCP represent offsets into shared ram space.
   1210   1.1        pk  */
   1211   1.1        pk int
   1212  1.65       dsl i82586_proberam(struct ie_softc *sc)
   1213   1.1        pk {
   1214  1.10        pk 	int result, off;
   1215   1.1        pk 
   1216   1.9        pk 	/* Put in 16-bit mode */
   1217  1.10        pk 	off = IE_SCP_BUS_USE(sc->scp);
   1218  1.42  fredette 	sc->ie_bus_write16(sc, off, IE_SYSBUS_16BIT);
   1219  1.34     bjh21 	IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_WRITE);
   1220  1.10        pk 
   1221  1.10        pk 	/* Set the ISCP `busy' bit */
   1222  1.10        pk 	off = IE_ISCP_BUSY(sc->iscp);
   1223  1.34     bjh21 	sc->ie_bus_write16(sc, off, 1);
   1224  1.34     bjh21 	IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_WRITE);
   1225   1.1        pk 
   1226   1.1        pk 	if (sc->hwreset)
   1227   1.7        pk 		(sc->hwreset)(sc, CHIP_PROBE);
   1228   1.1        pk 
   1229  1.36  jdolecek 	(sc->chan_attn) (sc, CHIP_PROBE);
   1230   1.1        pk 
   1231   1.1        pk 	delay(100);		/* wait a while... */
   1232   1.1        pk 
   1233  1.10        pk 	/* Read back the ISCP `busy' bit; it should be clear by now */
   1234  1.10        pk 	off = IE_ISCP_BUSY(sc->iscp);
   1235  1.34     bjh21 	IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
   1236  1.34     bjh21 	result = sc->ie_bus_read16(sc, off) == 0;
   1237   1.9        pk 
   1238  1.10        pk 	/* Acknowledge any interrupts we may have caused. */
   1239   1.1        pk 	ie_ack(sc, IE_ST_WHENCE);
   1240  1.10        pk 
   1241  1.10        pk 	return (result);
   1242   1.1        pk }
   1243   1.1        pk 
   1244   1.1        pk void
   1245  1.65       dsl i82586_reset(struct ie_softc *sc, int hard)
   1246   1.1        pk {
   1247   1.1        pk 	int s = splnet();
   1248   1.1        pk 
   1249  1.10        pk 	if (hard)
   1250  1.69   tsutsui 		printf("%s: reset\n", device_xname(sc->sc_dev));
   1251   1.1        pk 
   1252   1.1        pk 	/* Clear OACTIVE in case we're called from watchdog (frozen xmit). */
   1253   1.7        pk 	sc->sc_ethercom.ec_if.if_timer = 0;
   1254   1.1        pk 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1255   1.1        pk 
   1256   1.1        pk 	/*
   1257   1.1        pk 	 * Stop i82586 dead in its tracks.
   1258   1.1        pk 	 */
   1259   1.9        pk 	if (i82586_start_cmd(sc, IE_RUC_ABORT | IE_CUC_ABORT, 0, 0, 0))
   1260  1.69   tsutsui 		aprint_error_dev(sc->sc_dev, "abort commands timed out\n");
   1261   1.1        pk 
   1262   1.9        pk 	/*
   1263   1.7        pk 	 * This can really slow down the i82586_reset() on some cards, but it's
   1264   1.9        pk 	 * necessary to unwedge other ones (eg, the Sun VME ones) from certain
   1265   1.7        pk 	 * lockups.
   1266   1.7        pk 	 */
   1267  1.10        pk 	if (hard && sc->hwreset)
   1268   1.7        pk 		(sc->hwreset)(sc, CARD_RESET);
   1269   1.6        pk 
   1270  1.10        pk 	delay(100);
   1271   1.5        pk 	ie_ack(sc, IE_ST_WHENCE);
   1272   1.6        pk 
   1273   1.9        pk 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0) {
   1274   1.9        pk 		int retries=0;	/* XXX - find out why init sometimes fails */
   1275   1.9        pk 		while (retries++ < 2)
   1276  1.31       scw 			if (i82586_init(&sc->sc_ethercom.ec_if) == 0)
   1277   1.9        pk 				break;
   1278   1.9        pk 	}
   1279   1.1        pk 
   1280   1.1        pk 	splx(s);
   1281   1.1        pk }
   1282   1.1        pk 
   1283   1.9        pk 
   1284   1.9        pk static void
   1285  1.65       dsl setup_simple_command(struct ie_softc *sc, int cmd, int cmdbuf)
   1286   1.1        pk {
   1287   1.9        pk 	/* Setup a simple command */
   1288   1.9        pk 	sc->ie_bus_write16(sc, IE_CMD_COMMON_STATUS(cmdbuf), 0);
   1289   1.9        pk 	sc->ie_bus_write16(sc, IE_CMD_COMMON_CMD(cmdbuf), cmd | IE_CMD_LAST);
   1290   1.9        pk 	sc->ie_bus_write16(sc, IE_CMD_COMMON_LINK(cmdbuf), 0xffff);
   1291   1.1        pk 
   1292   1.9        pk 	/* Assign the command buffer to the SCB command list */
   1293   1.9        pk 	sc->ie_bus_write16(sc, IE_SCB_CMDLST(sc->scb), cmdbuf);
   1294   1.1        pk }
   1295   1.1        pk 
   1296   1.1        pk /*
   1297   1.1        pk  * Run the time-domain reflectometer.
   1298   1.1        pk  */
   1299   1.1        pk static void
   1300  1.65       dsl ie_run_tdr(struct ie_softc *sc, int cmd)
   1301   1.1        pk {
   1302   1.1        pk 	int result;
   1303   1.1        pk 
   1304   1.9        pk 	setup_simple_command(sc, IE_CMD_TDR, cmd);
   1305  1.34     bjh21 	sc->ie_bus_write16(sc, IE_CMD_TDR_TIME(cmd), 0);
   1306   1.1        pk 
   1307   1.9        pk 	if (i82586_start_cmd(sc, IE_CUC_START, cmd, IE_STAT_COMPL, 0) ||
   1308   1.9        pk 	    (sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmd)) & IE_STAT_OK) == 0)
   1309   1.1        pk 		result = 0x10000; /* XXX */
   1310   1.1        pk 	else
   1311   1.9        pk 		result = sc->ie_bus_read16(sc, IE_CMD_TDR_TIME(cmd));
   1312   1.1        pk 
   1313   1.9        pk 	/* Squash any pending interrupts */
   1314   1.1        pk 	ie_ack(sc, IE_ST_WHENCE);
   1315   1.1        pk 
   1316   1.1        pk 	if (result & IE_TDR_SUCCESS)
   1317   1.1        pk 		return;
   1318   1.1        pk 
   1319   1.1        pk 	if (result & 0x10000)
   1320  1.69   tsutsui 		aprint_error_dev(sc->sc_dev, "TDR command failed\n");
   1321   1.1        pk 	else if (result & IE_TDR_XCVR)
   1322  1.69   tsutsui 		aprint_error_dev(sc->sc_dev, "transceiver problem\n");
   1323   1.1        pk 	else if (result & IE_TDR_OPEN)
   1324  1.73   msaitoh 		aprint_error_dev(sc->sc_dev, "TDR detected incorrect "
   1325  1.73   msaitoh 		    "termination %d clocks away\n", result & IE_TDR_TIME);
   1326   1.1        pk 	else if (result & IE_TDR_SHORT)
   1327  1.73   msaitoh 		aprint_error_dev(sc->sc_dev, "TDR detected a short circuit "
   1328  1.73   msaitoh 		    "%d clocks away\n", result & IE_TDR_TIME);
   1329   1.1        pk 	else
   1330  1.73   msaitoh 		aprint_error_dev(sc->sc_dev,
   1331  1.73   msaitoh 		    "TDR returned unknown status 0x%x\n", result);
   1332   1.1        pk }
   1333   1.1        pk 
   1334   1.1        pk 
   1335   1.1        pk /*
   1336   1.9        pk  * i82586_setup_bufs: set up the buffers
   1337   1.1        pk  *
   1338   1.9        pk  * We have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
   1339   1.9        pk  * this is to be used for the buffers.  The chip indexs its control data
   1340   1.1        pk  * structures with 16 bit offsets, and it indexes actual buffers with
   1341   1.9        pk  * 24 bit addresses.   So we should allocate control buffers first so that
   1342   1.1        pk  * we don't overflow the 16 bit offset field.   The number of transmit
   1343   1.1        pk  * buffers is fixed at compile time.
   1344   1.1        pk  *
   1345   1.1        pk  */
   1346   1.1        pk static void
   1347  1.65       dsl i82586_setup_bufs(struct ie_softc *sc)
   1348   1.1        pk {
   1349   1.9        pk 	int	ptr = sc->buf_area;	/* memory pool */
   1350   1.1        pk 	int     n, r;
   1351   1.1        pk 
   1352   1.1        pk 	/*
   1353   1.1        pk 	 * step 0: zero memory and figure out how many recv buffers and
   1354   1.3        pk 	 * frames we can have.
   1355   1.1        pk 	 */
   1356   1.9        pk 	ptr = (ptr + 3) & ~3;	/* set alignment and stick with it */
   1357   1.9        pk 
   1358   1.9        pk 
   1359   1.9        pk 	/*
   1360   1.9        pk 	 *  step 1: lay out data structures in the shared-memory area
   1361   1.9        pk 	 */
   1362   1.9        pk 
   1363   1.9        pk 	/* The no-op commands; used if "nop-chaining" is in effect */
   1364   1.9        pk 	sc->nop_cmds = ptr;
   1365   1.9        pk 	ptr += NTXBUF * IE_CMD_NOP_SZ;
   1366   1.9        pk 
   1367   1.9        pk 	/* The transmit commands */
   1368   1.9        pk 	sc->xmit_cmds = ptr;
   1369   1.9        pk 	ptr += NTXBUF * IE_CMD_XMIT_SZ;
   1370   1.1        pk 
   1371   1.9        pk 	/* The transmit buffers descriptors */
   1372   1.9        pk 	sc->xbds = ptr;
   1373   1.9        pk 	ptr += NTXBUF * IE_XBD_SZ;
   1374   1.1        pk 
   1375   1.9        pk 	/* The transmit buffers */
   1376   1.9        pk 	sc->xbufs = ptr;
   1377   1.9        pk 	ptr += NTXBUF * IE_TBUF_SIZE;
   1378   1.1        pk 
   1379   1.9        pk 	ptr = (ptr + 3) & ~3;		/* re-align.. just in case */
   1380   1.1        pk 
   1381   1.9        pk 	/* Compute free space for RECV stuff */
   1382   1.9        pk 	n = sc->buf_area_sz - (ptr - sc->buf_area);
   1383   1.9        pk 
   1384   1.9        pk 	/* Compute size of one RECV frame */
   1385   1.9        pk 	r = IE_RFRAME_SZ + ((IE_RBD_SZ + IE_RBUF_SIZE) * B_PER_F);
   1386   1.1        pk 
   1387   1.1        pk 	sc->nframes = n / r;
   1388   1.9        pk 
   1389   1.1        pk 	if (sc->nframes <= 0)
   1390  1.44    provos 		panic("ie: bogus buffer calc");
   1391   1.1        pk 
   1392   1.1        pk 	sc->nrxbuf = sc->nframes * B_PER_F;
   1393   1.1        pk 
   1394  1.48       wiz 	/* The receive frame descriptors */
   1395   1.9        pk 	sc->rframes = ptr;
   1396   1.9        pk 	ptr += sc->nframes * IE_RFRAME_SZ;
   1397   1.9        pk 
   1398   1.9        pk 	/* The receive buffer descriptors */
   1399   1.9        pk 	sc->rbds = ptr;
   1400   1.9        pk 	ptr += sc->nrxbuf * IE_RBD_SZ;
   1401   1.9        pk 
   1402   1.9        pk 	/* The receive buffers */
   1403   1.9        pk 	sc->rbufs = ptr;
   1404   1.9        pk 	ptr += sc->nrxbuf * IE_RBUF_SIZE;
   1405   1.9        pk 
   1406  1.25        pk #if I82586_DEBUG
   1407  1.73   msaitoh 	printf("%s: %d frames %d bufs\n", device_xname(sc->sc_dev),
   1408  1.73   msaitoh 	    sc->nframes, sc->nrxbuf);
   1409   1.1        pk #endif
   1410   1.1        pk 
   1411   1.1        pk 	/*
   1412   1.9        pk 	 * step 2: link together the recv frames and set EOL on last one
   1413   1.1        pk 	 */
   1414   1.9        pk 	for (n = 0; n < sc->nframes; n++) {
   1415   1.9        pk 		int m = (n == sc->nframes - 1) ? 0 : n + 1;
   1416   1.9        pk 
   1417   1.9        pk 		/* Clear status */
   1418   1.9        pk 		sc->ie_bus_write16(sc, IE_RFRAME_STATUS(sc->rframes,n), 0);
   1419   1.9        pk 
   1420   1.9        pk 		/* RBD link = NULL */
   1421   1.9        pk 		sc->ie_bus_write16(sc, IE_RFRAME_BUFDESC(sc->rframes,n),
   1422   1.9        pk 				       0xffff);
   1423   1.9        pk 
   1424   1.9        pk 		/* Make a circular list */
   1425   1.9        pk 		sc->ie_bus_write16(sc, IE_RFRAME_NEXT(sc->rframes,n),
   1426   1.9        pk 				       IE_RFRAME_ADDR(sc->rframes,m));
   1427   1.1        pk 
   1428   1.9        pk 		/* Mark last as EOL */
   1429   1.9        pk 		sc->ie_bus_write16(sc, IE_RFRAME_LAST(sc->rframes,n),
   1430   1.9        pk 				       ((m==0)? (IE_FD_EOL|IE_FD_SUSP) : 0));
   1431   1.1        pk 	}
   1432   1.1        pk 
   1433   1.1        pk 	/*
   1434   1.9        pk 	 * step 3: link the RBDs and set EOL on last one
   1435   1.1        pk 	 */
   1436   1.9        pk 	for (n = 0; n < sc->nrxbuf; n++) {
   1437   1.9        pk 		int m = (n == sc->nrxbuf - 1) ? 0 : n + 1;
   1438   1.9        pk 
   1439   1.9        pk 		/* Clear status */
   1440   1.9        pk 		sc->ie_bus_write16(sc, IE_RBD_STATUS(sc->rbds,n), 0);
   1441   1.9        pk 
   1442   1.9        pk 		/* Make a circular list */
   1443   1.9        pk 		sc->ie_bus_write16(sc, IE_RBD_NEXT(sc->rbds,n),
   1444   1.9        pk 				       IE_RBD_ADDR(sc->rbds,m));
   1445   1.9        pk 
   1446   1.9        pk 		/* Link to data buffers */
   1447   1.9        pk 		sc->ie_bus_write24(sc, IE_RBD_BUFADDR(sc->rbds, n),
   1448   1.9        pk 				       IE_RBUF_ADDR(sc, n));
   1449   1.9        pk 		sc->ie_bus_write16(sc, IE_RBD_BUFLEN(sc->rbds,n),
   1450   1.9        pk 				       IE_RBUF_SIZE | ((m==0)?IE_RBD_EOL:0));
   1451   1.1        pk 	}
   1452   1.1        pk 
   1453   1.1        pk 	/*
   1454   1.9        pk 	 * step 4: all xmit no-op commands loopback onto themselves
   1455   1.6        pk 	 */
   1456   1.6        pk 	for (n = 0; n < NTXBUF; n++) {
   1457  1.34     bjh21 		sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, n), 0);
   1458   1.6        pk 
   1459  1.34     bjh21 		sc->ie_bus_write16(sc, IE_CMD_NOP_CMD(sc->nop_cmds, n),
   1460   1.9        pk 					 IE_CMD_NOP);
   1461   1.1        pk 
   1462  1.34     bjh21 		sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, n),
   1463   1.9        pk 					 IE_CMD_NOP_ADDR(sc->nop_cmds, n));
   1464   1.1        pk 	}
   1465   1.1        pk 
   1466   1.1        pk 
   1467   1.1        pk 	/*
   1468   1.9        pk 	 * step 6: set the head and tail pointers on receive to keep track of
   1469   1.9        pk 	 * the order in which RFDs and RBDs are used.
   1470   1.1        pk 	 */
   1471   1.1        pk 
   1472   1.1        pk 	/* Pointers to last packet sent and next available transmit buffer. */
   1473   1.1        pk 	sc->xchead = sc->xctail = 0;
   1474   1.1        pk 
   1475   1.1        pk 	/* Clear transmit-busy flag and set number of free transmit buffers. */
   1476   1.1        pk 	sc->xmit_busy = 0;
   1477   1.1        pk 
   1478   1.1        pk 	/*
   1479   1.9        pk 	 * Pointers to first and last receive frame.
   1480   1.9        pk 	 * The RFD pointed to by rftail is the only one that has EOL set.
   1481   1.1        pk 	 */
   1482   1.1        pk 	sc->rfhead = 0;
   1483   1.1        pk 	sc->rftail = sc->nframes - 1;
   1484   1.9        pk 
   1485   1.9        pk 	/*
   1486   1.9        pk 	 * Pointers to first and last receive descriptor buffer.
   1487   1.9        pk 	 * The RBD pointed to by rbtail is the only one that has EOL set.
   1488   1.9        pk 	 */
   1489   1.1        pk 	sc->rbhead = 0;
   1490   1.1        pk 	sc->rbtail = sc->nrxbuf - 1;
   1491   1.1        pk 
   1492   1.9        pk /* link in recv frames * and buffer into the scb. */
   1493  1.25        pk #if I82586_DEBUG
   1494   1.9        pk 	printf("%s: reserved %d bytes\n",
   1495  1.69   tsutsui 		device_xname(sc->sc_dev), ptr - sc->buf_area);
   1496   1.1        pk #endif
   1497   1.1        pk }
   1498   1.1        pk 
   1499   1.9        pk static int
   1500  1.66       dsl ie_cfg_setup(struct ie_softc *sc, int cmd, int promiscuous, int manchester)
   1501   1.9        pk {
   1502   1.9        pk 	int cmdresult, status;
   1503  1.27     bjh21 	u_int8_t buf[IE_CMD_CFG_SZ]; /* XXX malloc? */
   1504   1.9        pk 
   1505  1.27     bjh21 	*IE_CMD_CFG_CNT(buf)       = 0x0c;
   1506  1.27     bjh21 	*IE_CMD_CFG_FIFO(buf)      = 8;
   1507  1.27     bjh21         *IE_CMD_CFG_SAVEBAD(buf)   = 0x40;
   1508  1.27     bjh21 	*IE_CMD_CFG_ADDRLEN(buf)   = 0x2e;
   1509  1.27     bjh21 	*IE_CMD_CFG_PRIORITY(buf)  = 0;
   1510  1.27     bjh21 	*IE_CMD_CFG_IFS(buf)       = 0x60;
   1511  1.27     bjh21 	*IE_CMD_CFG_SLOT_LOW(buf)  = 0;
   1512  1.27     bjh21 	*IE_CMD_CFG_SLOT_HIGH(buf) = 0xf2;
   1513  1.70       mrg 	*IE_CMD_CFG_PROMISC(buf)   = (!!promiscuous) | manchester << 2;
   1514  1.27     bjh21 	*IE_CMD_CFG_CRSCDT(buf)    = 0;
   1515  1.27     bjh21 	*IE_CMD_CFG_MINLEN(buf)    = 64;
   1516  1.27     bjh21 	*IE_CMD_CFG_JUNK(buf)      = 0xff;
   1517  1.27     bjh21 	sc->memcopyout(sc, buf, cmd, IE_CMD_CFG_SZ);
   1518   1.9        pk 	setup_simple_command(sc, IE_CMD_CONFIG, cmd);
   1519  1.34     bjh21 	IE_BUS_BARRIER(sc, cmd, IE_CMD_CFG_SZ, BUS_SPACE_BARRIER_WRITE);
   1520   1.9        pk 
   1521   1.9        pk 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmd, IE_STAT_COMPL, 0);
   1522   1.9        pk 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmd));
   1523   1.9        pk 	if (cmdresult != 0) {
   1524  1.73   msaitoh 		aprint_error_dev(sc->sc_dev,
   1525  1.73   msaitoh 		    "configure command timed out; status %x\n", status);
   1526   1.9        pk 		return (0);
   1527   1.9        pk 	}
   1528   1.9        pk 	if ((status & IE_STAT_OK) == 0) {
   1529  1.73   msaitoh 		aprint_error_dev(sc->sc_dev,
   1530  1.73   msaitoh 		    "configure command failed; status %x\n", status);
   1531   1.9        pk 		return (0);
   1532   1.9        pk 	}
   1533  1.10        pk 
   1534  1.10        pk 	/* Squash any pending interrupts */
   1535  1.10        pk 	ie_ack(sc, IE_ST_WHENCE);
   1536   1.9        pk 	return (1);
   1537   1.9        pk }
   1538   1.9        pk 
   1539   1.9        pk static int
   1540  1.65       dsl ie_ia_setup(struct ie_softc *sc, int cmdbuf)
   1541   1.9        pk {
   1542   1.9        pk 	int cmdresult, status;
   1543   1.9        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1544   1.9        pk 
   1545   1.9        pk 	setup_simple_command(sc, IE_CMD_IASETUP, cmdbuf);
   1546   1.9        pk 
   1547  1.59    dyoung 	(sc->memcopyout)(sc, CLLADDR(ifp->if_sadl),
   1548   1.9        pk 			 IE_CMD_IAS_EADDR(cmdbuf), ETHER_ADDR_LEN);
   1549   1.9        pk 
   1550   1.9        pk 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmdbuf, IE_STAT_COMPL, 0);
   1551   1.9        pk 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmdbuf));
   1552   1.9        pk 	if (cmdresult != 0) {
   1553  1.73   msaitoh 		aprint_error_dev(sc->sc_dev,
   1554  1.73   msaitoh 		    "individual address command timed out; status %x\n",
   1555  1.73   msaitoh 		    status);
   1556   1.9        pk 		return (0);
   1557   1.9        pk 	}
   1558   1.9        pk 	if ((status & IE_STAT_OK) == 0) {
   1559  1.73   msaitoh 		aprint_error_dev(sc->sc_dev,
   1560  1.73   msaitoh 		    "individual address command failed; status %x\n", status);
   1561   1.9        pk 		return (0);
   1562   1.9        pk 	}
   1563  1.10        pk 
   1564  1.10        pk 	/* Squash any pending interrupts */
   1565  1.10        pk 	ie_ack(sc, IE_ST_WHENCE);
   1566   1.9        pk 	return (1);
   1567   1.9        pk }
   1568   1.9        pk 
   1569   1.1        pk /*
   1570   1.1        pk  * Run the multicast setup command.
   1571   1.1        pk  * Called at splnet().
   1572   1.1        pk  */
   1573   1.1        pk static int
   1574  1.65       dsl ie_mc_setup(struct ie_softc *sc, int cmdbuf)
   1575   1.1        pk {
   1576   1.9        pk 	int cmdresult, status;
   1577   1.1        pk 
   1578   1.9        pk 	if (sc->mcast_count == 0)
   1579   1.9        pk 		return (1);
   1580   1.1        pk 
   1581   1.9        pk 	setup_simple_command(sc, IE_CMD_MCAST, cmdbuf);
   1582   1.1        pk 
   1583  1.58  christos 	(sc->memcopyout)(sc, (void *)sc->mcast_addrs,
   1584   1.9        pk 			 IE_CMD_MCAST_MADDR(cmdbuf),
   1585   1.9        pk 			 sc->mcast_count * ETHER_ADDR_LEN);
   1586   1.9        pk 
   1587   1.9        pk 	sc->ie_bus_write16(sc, IE_CMD_MCAST_BYTES(cmdbuf),
   1588   1.9        pk 			       sc->mcast_count * ETHER_ADDR_LEN);
   1589   1.9        pk 
   1590   1.9        pk 	/* Start the command */
   1591  1.73   msaitoh 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmdbuf, IE_STAT_COMPL,
   1592  1.73   msaitoh 	    0);
   1593   1.9        pk 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmdbuf));
   1594   1.9        pk 	if (cmdresult != 0) {
   1595  1.73   msaitoh 		aprint_error_dev(sc->sc_dev,
   1596  1.73   msaitoh 		    "multicast setup command timed out; status %x\n", status);
   1597   1.9        pk 		return (0);
   1598   1.9        pk 	}
   1599   1.9        pk 	if ((status & IE_STAT_OK) == 0) {
   1600  1.73   msaitoh 		aprint_error_dev(sc->sc_dev,
   1601  1.73   msaitoh 		    "multicast setup command failed; status %x\n", status);
   1602   1.9        pk 		return (0);
   1603   1.1        pk 	}
   1604   1.6        pk 
   1605  1.10        pk 	/* Squash any pending interrupts */
   1606  1.10        pk 	ie_ack(sc, IE_ST_WHENCE);
   1607   1.9        pk 	return (1);
   1608   1.1        pk }
   1609   1.1        pk 
   1610   1.1        pk /*
   1611   1.1        pk  * This routine takes the environment generated by check_ie_present() and adds
   1612   1.1        pk  * to it all the other structures we need to operate the adapter.  This
   1613   1.1        pk  * includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands, starting
   1614   1.1        pk  * the receiver unit, and clearing interrupts.
   1615   1.1        pk  *
   1616   1.1        pk  * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
   1617   1.1        pk  */
   1618   1.1        pk int
   1619  1.65       dsl i82586_init(struct ifnet *ifp)
   1620   1.1        pk {
   1621  1.29     bjh21 	struct ie_softc *sc = ifp->if_softc;
   1622   1.9        pk 	int cmd;
   1623   1.9        pk 
   1624  1.10        pk 	sc->async_cmd_inprogress = 0;
   1625   1.1        pk 
   1626   1.9        pk 	cmd = sc->buf_area;
   1627   1.1        pk 
   1628   1.1        pk 	/*
   1629   1.1        pk 	 * Send the configure command first.
   1630   1.1        pk 	 */
   1631   1.9        pk 	if (ie_cfg_setup(sc, cmd, sc->promisc, 0) == 0)
   1632  1.37     bjh21 		return EIO;
   1633   1.1        pk 
   1634   1.9        pk 	/*
   1635   1.9        pk 	 * Send the Individual Address Setup command.
   1636   1.9        pk 	 */
   1637   1.9        pk 	if (ie_ia_setup(sc, cmd) == 0)
   1638  1.37     bjh21 		return EIO;
   1639   1.1        pk 
   1640   1.1        pk 	/*
   1641   1.9        pk 	 * Run the time-domain reflectometer.
   1642   1.1        pk 	 */
   1643   1.9        pk 	ie_run_tdr(sc, cmd);
   1644   1.1        pk 
   1645   1.1        pk 	/*
   1646   1.9        pk 	 * Set the multi-cast filter, if any
   1647   1.1        pk 	 */
   1648   1.9        pk 	if (ie_mc_setup(sc, cmd) == 0)
   1649  1.37     bjh21 		return EIO;
   1650   1.1        pk 
   1651   1.1        pk 	/*
   1652   1.1        pk 	 * Acknowledge any interrupts we have generated thus far.
   1653   1.1        pk 	 */
   1654   1.1        pk 	ie_ack(sc, IE_ST_WHENCE);
   1655   1.1        pk 
   1656   1.1        pk 	/*
   1657   1.1        pk 	 * Set up the transmit and recv buffers.
   1658   1.1        pk 	 */
   1659   1.9        pk 	i82586_setup_bufs(sc);
   1660   1.1        pk 
   1661   1.6        pk 	if (sc->hwinit)
   1662   1.6        pk 		(sc->hwinit)(sc);
   1663   1.6        pk 
   1664   1.3        pk 	ifp->if_flags |= IFF_RUNNING;
   1665   1.3        pk 	ifp->if_flags &= ~IFF_OACTIVE;
   1666   1.1        pk 
   1667   1.6        pk 	if (NTXBUF < 2)
   1668   1.7        pk 		sc->do_xmitnopchain = 0;
   1669   1.6        pk 
   1670   1.6        pk 	i82586_start_transceiver(sc);
   1671  1.31       scw 	return (0);
   1672   1.6        pk }
   1673   1.6        pk 
   1674   1.9        pk /*
   1675   1.9        pk  * Start the RU and possibly the CU unit
   1676   1.9        pk  */
   1677   1.6        pk static void
   1678  1.65       dsl i82586_start_transceiver(struct ie_softc *sc)
   1679   1.6        pk {
   1680   1.9        pk 
   1681   1.9        pk 	/*
   1682   1.9        pk 	 * Start RU at current position in frame & RBD lists.
   1683   1.9        pk 	 */
   1684   1.9        pk 	sc->ie_bus_write16(sc, IE_RFRAME_BUFDESC(sc->rframes,sc->rfhead),
   1685   1.9        pk 			       IE_RBD_ADDR(sc->rbds, sc->rbhead));
   1686   1.9        pk 
   1687   1.9        pk 	sc->ie_bus_write16(sc, IE_SCB_RCVLST(sc->scb),
   1688   1.9        pk 			       IE_RFRAME_ADDR(sc->rframes,sc->rfhead));
   1689   1.1        pk 
   1690   1.7        pk 	if (sc->do_xmitnopchain) {
   1691   1.6        pk 		/* Stop transmit command chain */
   1692   1.9        pk 		if (i82586_start_cmd(sc, IE_CUC_SUSPEND|IE_RUC_SUSPEND, 0, 0, 0))
   1693  1.73   msaitoh 			aprint_error_dev(sc->sc_dev,
   1694  1.73   msaitoh 			    "CU/RU stop command timed out\n");
   1695   1.1        pk 
   1696   1.6        pk 		/* Start the receiver & transmitter chain */
   1697   1.9        pk 		/* sc->scb->ie_command_list =
   1698   1.9        pk 			IEADDR(sc->nop_cmds[(sc->xctail+NTXBUF-1) % NTXBUF]);*/
   1699   1.9        pk 		sc->ie_bus_write16(sc, IE_SCB_CMDLST(sc->scb),
   1700   1.9        pk 				   IE_CMD_NOP_ADDR(
   1701   1.9        pk 					sc->nop_cmds,
   1702   1.9        pk 					(sc->xctail + NTXBUF - 1) % NTXBUF));
   1703   1.9        pk 
   1704   1.9        pk 		if (i82586_start_cmd(sc, IE_CUC_START|IE_RUC_START, 0, 0, 0))
   1705  1.73   msaitoh 			aprint_error_dev(sc->sc_dev,
   1706  1.73   msaitoh 			    "CU/RU command timed out\n");
   1707   1.6        pk 	} else {
   1708   1.9        pk 		if (i82586_start_cmd(sc, IE_RUC_START, 0, 0, 0))
   1709  1.69   tsutsui 			aprint_error_dev(sc->sc_dev, "RU command timed out\n");
   1710   1.6        pk 	}
   1711   1.1        pk }
   1712   1.1        pk 
   1713  1.29     bjh21 void
   1714  1.74   msaitoh i82586_stop(struct ifnet *ifp, int disable)
   1715   1.1        pk {
   1716  1.29     bjh21 	struct ie_softc *sc = ifp->if_softc;
   1717   1.1        pk 
   1718   1.9        pk 	if (i82586_start_cmd(sc, IE_RUC_SUSPEND | IE_CUC_SUSPEND, 0, 0, 0))
   1719  1.73   msaitoh 		aprint_error_dev(sc->sc_dev,
   1720  1.73   msaitoh 		    "iestop: disable commands timed out\n");
   1721   1.1        pk }
   1722   1.1        pk 
   1723   1.1        pk int
   1724  1.63    dyoung i82586_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
   1725   1.1        pk {
   1726   1.1        pk 	struct ie_softc *sc = ifp->if_softc;
   1727   1.1        pk 	struct ifreq *ifr = (struct ifreq *)data;
   1728   1.1        pk 	int s, error = 0;
   1729   1.1        pk 
   1730   1.1        pk 	s = splnet();
   1731   1.1        pk 	switch(cmd) {
   1732  1.29     bjh21         case SIOCGIFMEDIA:
   1733  1.29     bjh21         case SIOCSIFMEDIA:
   1734  1.29     bjh21                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1735  1.29     bjh21                 break;
   1736  1.29     bjh21 	default:
   1737  1.29     bjh21 		error = ether_ioctl(ifp, cmd, data);
   1738   1.1        pk 		if (error == ENETRESET) {
   1739   1.1        pk 			/*
   1740   1.1        pk 			 * Multicast list has changed; set the hardware filter
   1741   1.1        pk 			 * accordingly.
   1742   1.1        pk 			 */
   1743  1.49   thorpej 			if (ifp->if_flags & IFF_RUNNING)
   1744  1.49   thorpej 				ie_mc_reset(sc);
   1745   1.1        pk 			error = 0;
   1746   1.1        pk 		}
   1747   1.1        pk 		break;
   1748   1.1        pk 	}
   1749  1.29     bjh21 #if I82586_DEBUG
   1750  1.29     bjh21 	if (cmd == SIOCSIFFLAGS)
   1751  1.29     bjh21 		sc->sc_debug = (ifp->if_flags & IFF_DEBUG) ? IED_ALL : 0;
   1752  1.29     bjh21 #endif
   1753   1.1        pk 	splx(s);
   1754  1.10        pk 	return (error);
   1755   1.1        pk }
   1756   1.1        pk 
   1757   1.1        pk static void
   1758  1.65       dsl ie_mc_reset(struct ie_softc *sc)
   1759   1.1        pk {
   1760   1.1        pk 	struct ether_multi *enm;
   1761   1.1        pk 	struct ether_multistep step;
   1762   1.9        pk 	int size;
   1763   1.1        pk 
   1764   1.1        pk 	/*
   1765   1.1        pk 	 * Step through the list of addresses.
   1766   1.1        pk 	 */
   1767   1.9        pk again:
   1768   1.9        pk 	size = 0;
   1769   1.1        pk 	sc->mcast_count = 0;
   1770   1.1        pk 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1771   1.1        pk 	while (enm) {
   1772   1.9        pk 		size += 6;
   1773   1.9        pk 		if (sc->mcast_count >= IE_MAXMCAST ||
   1774  1.38   thorpej 		    memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
   1775   1.1        pk 			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
   1776   1.9        pk 			i82586_ioctl(&sc->sc_ethercom.ec_if,
   1777  1.63    dyoung 				     SIOCSIFFLAGS, NULL);
   1778   1.9        pk 			return;
   1779   1.1        pk 		}
   1780   1.9        pk 		ETHER_NEXT_MULTI(step, enm);
   1781   1.9        pk 	}
   1782   1.9        pk 
   1783   1.9        pk 	if (size > sc->mcast_addrs_size) {
   1784   1.9        pk 		/* Need to allocate more space */
   1785   1.9        pk 		if (sc->mcast_addrs_size)
   1786  1.43   thorpej 			free(sc->mcast_addrs, M_IFMADDR);
   1787   1.9        pk 		sc->mcast_addrs = (char *)
   1788  1.43   thorpej 			malloc(size, M_IFMADDR, M_WAITOK);
   1789   1.9        pk 		sc->mcast_addrs_size = size;
   1790   1.9        pk 	}
   1791   1.9        pk 
   1792   1.9        pk 	/*
   1793   1.9        pk 	 * We've got the space; now copy the addresses
   1794   1.9        pk 	 */
   1795   1.9        pk 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1796   1.9        pk 	while (enm) {
   1797   1.9        pk 		if (sc->mcast_count >= IE_MAXMCAST)
   1798   1.9        pk 			goto again; /* Just in case */
   1799   1.1        pk 
   1800  1.39   thorpej 		memcpy(&sc->mcast_addrs[sc->mcast_count], enm->enm_addrlo, 6);
   1801   1.1        pk 		sc->mcast_count++;
   1802   1.1        pk 		ETHER_NEXT_MULTI(step, enm);
   1803   1.1        pk 	}
   1804   1.1        pk 	sc->want_mcsetup = 1;
   1805   1.1        pk }
   1806   1.1        pk 
   1807   1.7        pk /*
   1808   1.7        pk  * Media change callback.
   1809   1.7        pk  */
   1810   1.7        pk int
   1811  1.65       dsl i82586_mediachange(struct ifnet *ifp)
   1812   1.7        pk {
   1813   1.7        pk         struct ie_softc *sc = ifp->if_softc;
   1814   1.7        pk 
   1815   1.7        pk         if (sc->sc_mediachange)
   1816   1.7        pk                 return ((*sc->sc_mediachange)(sc));
   1817  1.23       abs         return (0);
   1818   1.7        pk }
   1819   1.7        pk 
   1820   1.7        pk /*
   1821   1.7        pk  * Media status callback.
   1822   1.7        pk  */
   1823   1.7        pk void
   1824  1.65       dsl i82586_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1825   1.9        pk {
   1826   1.7        pk         struct ie_softc *sc = ifp->if_softc;
   1827   1.7        pk 
   1828   1.9        pk         if (sc->sc_mediastatus)
   1829   1.7        pk                 (*sc->sc_mediastatus)(sc, ifmr);
   1830   1.7        pk }
   1831   1.7        pk 
   1832  1.25        pk #if I82586_DEBUG
   1833   1.1        pk void
   1834  1.65       dsl print_rbd(struct ie_softc *sc, int n)
   1835   1.1        pk {
   1836   1.1        pk 
   1837  1.10        pk 	printf("RBD at %08x:\n  status %04x, next %04x, buffer %lx\n"
   1838   1.9        pk 		"length/EOL %04x\n", IE_RBD_ADDR(sc->rbds,n),
   1839   1.9        pk 		sc->ie_bus_read16(sc, IE_RBD_STATUS(sc->rbds,n)),
   1840   1.9        pk 		sc->ie_bus_read16(sc, IE_RBD_NEXT(sc->rbds,n)),
   1841   1.9        pk 		(u_long)0,/*bus_space_read_4(sc->bt, sc->bh, IE_RBD_BUFADDR(sc->rbds,n)),-* XXX */
   1842   1.9        pk 		sc->ie_bus_read16(sc, IE_RBD_BUFLEN(sc->rbds,n)));
   1843   1.1        pk }
   1844   1.1        pk #endif
   1845