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