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