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