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