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