1 /*	$NetBSD: i82586.c,v 1.74 2016/07/14 10:19:06 msaitoh 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.74 2016/07/14 10:19:06 msaitoh 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
i82586_attach(struct ie_softc * sc,const char * name,u_int8_t * etheraddr,int * media,int nmedia,int defmedia)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
i82586_watchdog(struct ifnet * ifp)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
i82586_cmd_wait(struct ie_softc * sc)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
i82586_start_cmd(struct ie_softc * sc,int cmd,int iecmdbuf,int mask,int async)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
ie_ack(struct ie_softc * sc,u_int mask)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
i82586_count_errors(struct ie_softc * sc)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
i82586_rx_errors(struct ie_softc * sc,int fn,int status)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
i82586_intr(void * v)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
i82586_rint(struct ie_softc * sc,int scbstatus)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
i82586_tint(struct ie_softc * sc,int scbstatus)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
i82586_get_rbd_list(struct ie_softc * sc,u_int16_t * start,u_int16_t * end,int * pktlen)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
i82586_release_rbd_list(struct ie_softc * sc,u_int16_t start,u_int16_t end)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
i82586_drop_frames(struct ie_softc * sc)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
i82586_chk_rx_ring(struct ie_softc * sc)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 *
ieget(struct ie_softc * sc,int head,int totlen)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
ie_readframe(struct ie_softc * sc,int num)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 	/* Pass it up. */
1042 	bpf_mtap(&sc->sc_ethercom.ec_if, m);
1043 
1044 	/*
1045 	 * Finally pass this packet up to higher layers.
1046 	 */
1047 	if_percpuq_enqueue((&sc->sc_ethercom.ec_if)->if_percpuq, m);
1048 	sc->sc_ethercom.ec_if.if_ipackets++;
1049 	return (0);
1050 }
1051 
1052 
1053 /*
1054  * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
1055  * command to the chip to be executed.
1056  */
1057 static inline void
iexmit(struct ie_softc * sc)1058 iexmit(struct ie_softc *sc)
1059 {
1060 	int off;
1061 	int cur, prev;
1062 
1063 	cur = sc->xctail;
1064 
1065 #if I82586_DEBUG
1066 	if (sc->sc_debug & IED_XMIT)
1067 		printf("%s: xmit buffer %d\n", device_xname(sc->sc_dev), cur);
1068 #endif
1069 
1070 	/*
1071 	 * Setup the transmit command.
1072 	 */
1073 	sc->ie_bus_write16(sc, IE_CMD_XMIT_DESC(sc->xmit_cmds, cur),
1074 			       IE_XBD_ADDR(sc->xbds, cur));
1075 
1076 	sc->ie_bus_write16(sc, IE_CMD_XMIT_STATUS(sc->xmit_cmds, cur), 0);
1077 
1078 	if (sc->do_xmitnopchain) {
1079 		/*
1080 		 * Gate this XMIT command to the following NOP
1081 		 */
1082 		sc->ie_bus_write16(sc, IE_CMD_XMIT_LINK(sc->xmit_cmds, cur),
1083 				       IE_CMD_NOP_ADDR(sc->nop_cmds, cur));
1084 		sc->ie_bus_write16(sc, IE_CMD_XMIT_CMD(sc->xmit_cmds, cur),
1085 				       IE_CMD_XMIT | IE_CMD_INTR);
1086 
1087 		/*
1088 		 * Loopback at following NOP
1089 		 */
1090 		sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, cur), 0);
1091 		sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, cur),
1092 				       IE_CMD_NOP_ADDR(sc->nop_cmds, cur));
1093 
1094 		/*
1095 		 * Gate preceding NOP to this XMIT command
1096 		 */
1097 		prev = (cur + NTXBUF - 1) % NTXBUF;
1098 		sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, prev),
1099 		    0);
1100 		sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, prev),
1101 				       IE_CMD_XMIT_ADDR(sc->xmit_cmds, cur));
1102 
1103 		off = IE_SCB_STATUS(sc->scb);
1104 		IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
1105 		if ((sc->ie_bus_read16(sc, off) & IE_CUS_ACTIVE) == 0) {
1106 			printf("iexmit: CU not active\n");
1107 			i82586_start_transceiver(sc);
1108 		}
1109 	} else {
1110 		sc->ie_bus_write16(sc, IE_CMD_XMIT_LINK(sc->xmit_cmds,cur),
1111 				       0xffff);
1112 
1113 		sc->ie_bus_write16(sc, IE_CMD_XMIT_CMD(sc->xmit_cmds, cur),
1114 				       IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST);
1115 
1116 		off = IE_SCB_CMDLST(sc->scb);
1117 		sc->ie_bus_write16(sc, off, IE_CMD_XMIT_ADDR(sc->xmit_cmds,
1118 			cur));
1119 		IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
1120 
1121 		if (i82586_start_cmd(sc, IE_CUC_START, 0, 0, 1))
1122 			aprint_error_dev(sc->sc_dev,
1123 			    "iexmit: start xmit command timed out\n");
1124 	}
1125 
1126 	sc->sc_ethercom.ec_if.if_timer = 5;
1127 }
1128 
1129 
1130 /*
1131  * Start transmission on an interface.
1132  */
1133 void
i82586_start(struct ifnet * ifp)1134 i82586_start(struct ifnet *ifp)
1135 {
1136 	struct ie_softc *sc = ifp->if_softc;
1137 	struct mbuf *m0, *m;
1138 	int	buffer, head, xbase;
1139 	u_short	len;
1140 	int	s;
1141 
1142 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1143 		return;
1144 
1145 	for (;;) {
1146 		if (sc->xmit_busy == NTXBUF) {
1147 			ifp->if_flags |= IFF_OACTIVE;
1148 			break;
1149 		}
1150 
1151 		head = sc->xchead;
1152 		xbase = sc->xbds;
1153 
1154 		IFQ_DEQUEUE(&ifp->if_snd, m0);
1155 		if (m0 == 0)
1156 			break;
1157 
1158 		/* We need to use m->m_pkthdr.len, so require the header */
1159 		if ((m0->m_flags & M_PKTHDR) == 0)
1160 			panic("i82586_start: no header mbuf");
1161 
1162 		/* Tap off here if there is a BPF listener. */
1163 		bpf_mtap(ifp, m0);
1164 
1165 #if I82586_DEBUG
1166 		if (sc->sc_debug & IED_ENQ)
1167 			printf("%s: fill buffer %d\n", device_xname(sc->sc_dev),
1168 				sc->xchead);
1169 #endif
1170 
1171 		if (m0->m_pkthdr.len > IE_TBUF_SIZE)
1172 			printf("%s: tbuf overflow\n", device_xname(sc->sc_dev));
1173 
1174 		buffer = IE_XBUF_ADDR(sc, head);
1175 		for (m = m0; m != 0; m = m->m_next) {
1176 			(sc->memcopyout)(sc, mtod(m,void *), buffer, m->m_len);
1177 			buffer += m->m_len;
1178 		}
1179 		len = m0->m_pkthdr.len;
1180 		if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1181 			(sc->memcopyout)(sc, padbuf, buffer,
1182 			    ETHER_MIN_LEN - ETHER_CRC_LEN - len);
1183 			buffer += ETHER_MIN_LEN -ETHER_CRC_LEN - len;
1184 			len = ETHER_MIN_LEN - ETHER_CRC_LEN;
1185 		}
1186 		m_freem(m0);
1187 
1188 		/*
1189 		 * Setup the transmit buffer descriptor here, while we
1190 		 * know the packet's length.
1191 		 */
1192 		sc->ie_bus_write16(sc, IE_XBD_FLAGS(xbase, head),
1193 				       len | IE_TBD_EOL);
1194 		sc->ie_bus_write16(sc, IE_XBD_NEXT(xbase, head), 0xffff);
1195 		sc->ie_bus_write24(sc, IE_XBD_BUF(xbase, head),
1196 				       IE_XBUF_ADDR(sc, head));
1197 
1198 		if (++head == NTXBUF)
1199 			head = 0;
1200 		sc->xchead = head;
1201 
1202 		s = splnet();
1203 		/* Start the first packet transmitting. */
1204 		if (sc->xmit_busy == 0)
1205 			iexmit(sc);
1206 
1207 		sc->xmit_busy++;
1208 		splx(s);
1209 	}
1210 }
1211 
1212 /*
1213  * Probe IE's ram setup   [ Move all this into MD front-end!? ]
1214  * Use only if SCP and ISCP represent offsets into shared ram space.
1215  */
1216 int
i82586_proberam(struct ie_softc * sc)1217 i82586_proberam(struct ie_softc *sc)
1218 {
1219 	int result, off;
1220 
1221 	/* Put in 16-bit mode */
1222 	off = IE_SCP_BUS_USE(sc->scp);
1223 	sc->ie_bus_write16(sc, off, IE_SYSBUS_16BIT);
1224 	IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_WRITE);
1225 
1226 	/* Set the ISCP `busy' bit */
1227 	off = IE_ISCP_BUSY(sc->iscp);
1228 	sc->ie_bus_write16(sc, off, 1);
1229 	IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_WRITE);
1230 
1231 	if (sc->hwreset)
1232 		(sc->hwreset)(sc, CHIP_PROBE);
1233 
1234 	(sc->chan_attn) (sc, CHIP_PROBE);
1235 
1236 	delay(100);		/* wait a while... */
1237 
1238 	/* Read back the ISCP `busy' bit; it should be clear by now */
1239 	off = IE_ISCP_BUSY(sc->iscp);
1240 	IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
1241 	result = sc->ie_bus_read16(sc, off) == 0;
1242 
1243 	/* Acknowledge any interrupts we may have caused. */
1244 	ie_ack(sc, IE_ST_WHENCE);
1245 
1246 	return (result);
1247 }
1248 
1249 void
i82586_reset(struct ie_softc * sc,int hard)1250 i82586_reset(struct ie_softc *sc, int hard)
1251 {
1252 	int s = splnet();
1253 
1254 	if (hard)
1255 		printf("%s: reset\n", device_xname(sc->sc_dev));
1256 
1257 	/* Clear OACTIVE in case we're called from watchdog (frozen xmit). */
1258 	sc->sc_ethercom.ec_if.if_timer = 0;
1259 	sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1260 
1261 	/*
1262 	 * Stop i82586 dead in its tracks.
1263 	 */
1264 	if (i82586_start_cmd(sc, IE_RUC_ABORT | IE_CUC_ABORT, 0, 0, 0))
1265 		aprint_error_dev(sc->sc_dev, "abort commands timed out\n");
1266 
1267 	/*
1268 	 * This can really slow down the i82586_reset() on some cards, but it's
1269 	 * necessary to unwedge other ones (eg, the Sun VME ones) from certain
1270 	 * lockups.
1271 	 */
1272 	if (hard && sc->hwreset)
1273 		(sc->hwreset)(sc, CARD_RESET);
1274 
1275 	delay(100);
1276 	ie_ack(sc, IE_ST_WHENCE);
1277 
1278 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0) {
1279 		int retries=0;	/* XXX - find out why init sometimes fails */
1280 		while (retries++ < 2)
1281 			if (i82586_init(&sc->sc_ethercom.ec_if) == 0)
1282 				break;
1283 	}
1284 
1285 	splx(s);
1286 }
1287 
1288 
1289 static void
setup_simple_command(struct ie_softc * sc,int cmd,int cmdbuf)1290 setup_simple_command(struct ie_softc *sc, int cmd, int cmdbuf)
1291 {
1292 	/* Setup a simple command */
1293 	sc->ie_bus_write16(sc, IE_CMD_COMMON_STATUS(cmdbuf), 0);
1294 	sc->ie_bus_write16(sc, IE_CMD_COMMON_CMD(cmdbuf), cmd | IE_CMD_LAST);
1295 	sc->ie_bus_write16(sc, IE_CMD_COMMON_LINK(cmdbuf), 0xffff);
1296 
1297 	/* Assign the command buffer to the SCB command list */
1298 	sc->ie_bus_write16(sc, IE_SCB_CMDLST(sc->scb), cmdbuf);
1299 }
1300 
1301 /*
1302  * Run the time-domain reflectometer.
1303  */
1304 static void
ie_run_tdr(struct ie_softc * sc,int cmd)1305 ie_run_tdr(struct ie_softc *sc, int cmd)
1306 {
1307 	int result;
1308 
1309 	setup_simple_command(sc, IE_CMD_TDR, cmd);
1310 	sc->ie_bus_write16(sc, IE_CMD_TDR_TIME(cmd), 0);
1311 
1312 	if (i82586_start_cmd(sc, IE_CUC_START, cmd, IE_STAT_COMPL, 0) ||
1313 	    (sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmd)) & IE_STAT_OK) == 0)
1314 		result = 0x10000; /* XXX */
1315 	else
1316 		result = sc->ie_bus_read16(sc, IE_CMD_TDR_TIME(cmd));
1317 
1318 	/* Squash any pending interrupts */
1319 	ie_ack(sc, IE_ST_WHENCE);
1320 
1321 	if (result & IE_TDR_SUCCESS)
1322 		return;
1323 
1324 	if (result & 0x10000)
1325 		aprint_error_dev(sc->sc_dev, "TDR command failed\n");
1326 	else if (result & IE_TDR_XCVR)
1327 		aprint_error_dev(sc->sc_dev, "transceiver problem\n");
1328 	else if (result & IE_TDR_OPEN)
1329 		aprint_error_dev(sc->sc_dev, "TDR detected incorrect "
1330 		    "termination %d clocks away\n", result & IE_TDR_TIME);
1331 	else if (result & IE_TDR_SHORT)
1332 		aprint_error_dev(sc->sc_dev, "TDR detected a short circuit "
1333 		    "%d clocks away\n", result & IE_TDR_TIME);
1334 	else
1335 		aprint_error_dev(sc->sc_dev,
1336 		    "TDR returned unknown status 0x%x\n", result);
1337 }
1338 
1339 
1340 /*
1341  * i82586_setup_bufs: set up the buffers
1342  *
1343  * We have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
1344  * this is to be used for the buffers.  The chip indexs its control data
1345  * structures with 16 bit offsets, and it indexes actual buffers with
1346  * 24 bit addresses.   So we should allocate control buffers first so that
1347  * we don't overflow the 16 bit offset field.   The number of transmit
1348  * buffers is fixed at compile time.
1349  *
1350  */
1351 static void
i82586_setup_bufs(struct ie_softc * sc)1352 i82586_setup_bufs(struct ie_softc *sc)
1353 {
1354 	int	ptr = sc->buf_area;	/* memory pool */
1355 	int     n, r;
1356 
1357 	/*
1358 	 * step 0: zero memory and figure out how many recv buffers and
1359 	 * frames we can have.
1360 	 */
1361 	ptr = (ptr + 3) & ~3;	/* set alignment and stick with it */
1362 
1363 
1364 	/*
1365 	 *  step 1: lay out data structures in the shared-memory area
1366 	 */
1367 
1368 	/* The no-op commands; used if "nop-chaining" is in effect */
1369 	sc->nop_cmds = ptr;
1370 	ptr += NTXBUF * IE_CMD_NOP_SZ;
1371 
1372 	/* The transmit commands */
1373 	sc->xmit_cmds = ptr;
1374 	ptr += NTXBUF * IE_CMD_XMIT_SZ;
1375 
1376 	/* The transmit buffers descriptors */
1377 	sc->xbds = ptr;
1378 	ptr += NTXBUF * IE_XBD_SZ;
1379 
1380 	/* The transmit buffers */
1381 	sc->xbufs = ptr;
1382 	ptr += NTXBUF * IE_TBUF_SIZE;
1383 
1384 	ptr = (ptr + 3) & ~3;		/* re-align.. just in case */
1385 
1386 	/* Compute free space for RECV stuff */
1387 	n = sc->buf_area_sz - (ptr - sc->buf_area);
1388 
1389 	/* Compute size of one RECV frame */
1390 	r = IE_RFRAME_SZ + ((IE_RBD_SZ + IE_RBUF_SIZE) * B_PER_F);
1391 
1392 	sc->nframes = n / r;
1393 
1394 	if (sc->nframes <= 0)
1395 		panic("ie: bogus buffer calc");
1396 
1397 	sc->nrxbuf = sc->nframes * B_PER_F;
1398 
1399 	/* The receive frame descriptors */
1400 	sc->rframes = ptr;
1401 	ptr += sc->nframes * IE_RFRAME_SZ;
1402 
1403 	/* The receive buffer descriptors */
1404 	sc->rbds = ptr;
1405 	ptr += sc->nrxbuf * IE_RBD_SZ;
1406 
1407 	/* The receive buffers */
1408 	sc->rbufs = ptr;
1409 	ptr += sc->nrxbuf * IE_RBUF_SIZE;
1410 
1411 #if I82586_DEBUG
1412 	printf("%s: %d frames %d bufs\n", device_xname(sc->sc_dev),
1413 	    sc->nframes, sc->nrxbuf);
1414 #endif
1415 
1416 	/*
1417 	 * step 2: link together the recv frames and set EOL on last one
1418 	 */
1419 	for (n = 0; n < sc->nframes; n++) {
1420 		int m = (n == sc->nframes - 1) ? 0 : n + 1;
1421 
1422 		/* Clear status */
1423 		sc->ie_bus_write16(sc, IE_RFRAME_STATUS(sc->rframes,n), 0);
1424 
1425 		/* RBD link = NULL */
1426 		sc->ie_bus_write16(sc, IE_RFRAME_BUFDESC(sc->rframes,n),
1427 				       0xffff);
1428 
1429 		/* Make a circular list */
1430 		sc->ie_bus_write16(sc, IE_RFRAME_NEXT(sc->rframes,n),
1431 				       IE_RFRAME_ADDR(sc->rframes,m));
1432 
1433 		/* Mark last as EOL */
1434 		sc->ie_bus_write16(sc, IE_RFRAME_LAST(sc->rframes,n),
1435 				       ((m==0)? (IE_FD_EOL|IE_FD_SUSP) : 0));
1436 	}
1437 
1438 	/*
1439 	 * step 3: link the RBDs and set EOL on last one
1440 	 */
1441 	for (n = 0; n < sc->nrxbuf; n++) {
1442 		int m = (n == sc->nrxbuf - 1) ? 0 : n + 1;
1443 
1444 		/* Clear status */
1445 		sc->ie_bus_write16(sc, IE_RBD_STATUS(sc->rbds,n), 0);
1446 
1447 		/* Make a circular list */
1448 		sc->ie_bus_write16(sc, IE_RBD_NEXT(sc->rbds,n),
1449 				       IE_RBD_ADDR(sc->rbds,m));
1450 
1451 		/* Link to data buffers */
1452 		sc->ie_bus_write24(sc, IE_RBD_BUFADDR(sc->rbds, n),
1453 				       IE_RBUF_ADDR(sc, n));
1454 		sc->ie_bus_write16(sc, IE_RBD_BUFLEN(sc->rbds,n),
1455 				       IE_RBUF_SIZE | ((m==0)?IE_RBD_EOL:0));
1456 	}
1457 
1458 	/*
1459 	 * step 4: all xmit no-op commands loopback onto themselves
1460 	 */
1461 	for (n = 0; n < NTXBUF; n++) {
1462 		sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, n), 0);
1463 
1464 		sc->ie_bus_write16(sc, IE_CMD_NOP_CMD(sc->nop_cmds, n),
1465 					 IE_CMD_NOP);
1466 
1467 		sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, n),
1468 					 IE_CMD_NOP_ADDR(sc->nop_cmds, n));
1469 	}
1470 
1471 
1472 	/*
1473 	 * step 6: set the head and tail pointers on receive to keep track of
1474 	 * the order in which RFDs and RBDs are used.
1475 	 */
1476 
1477 	/* Pointers to last packet sent and next available transmit buffer. */
1478 	sc->xchead = sc->xctail = 0;
1479 
1480 	/* Clear transmit-busy flag and set number of free transmit buffers. */
1481 	sc->xmit_busy = 0;
1482 
1483 	/*
1484 	 * Pointers to first and last receive frame.
1485 	 * The RFD pointed to by rftail is the only one that has EOL set.
1486 	 */
1487 	sc->rfhead = 0;
1488 	sc->rftail = sc->nframes - 1;
1489 
1490 	/*
1491 	 * Pointers to first and last receive descriptor buffer.
1492 	 * The RBD pointed to by rbtail is the only one that has EOL set.
1493 	 */
1494 	sc->rbhead = 0;
1495 	sc->rbtail = sc->nrxbuf - 1;
1496 
1497 /* link in recv frames * and buffer into the scb. */
1498 #if I82586_DEBUG
1499 	printf("%s: reserved %d bytes\n",
1500 		device_xname(sc->sc_dev), ptr - sc->buf_area);
1501 #endif
1502 }
1503 
1504 static int
ie_cfg_setup(struct ie_softc * sc,int cmd,int promiscuous,int manchester)1505 ie_cfg_setup(struct ie_softc *sc, int cmd, int promiscuous, int manchester)
1506 {
1507 	int cmdresult, status;
1508 	u_int8_t buf[IE_CMD_CFG_SZ]; /* XXX malloc? */
1509 
1510 	*IE_CMD_CFG_CNT(buf)       = 0x0c;
1511 	*IE_CMD_CFG_FIFO(buf)      = 8;
1512         *IE_CMD_CFG_SAVEBAD(buf)   = 0x40;
1513 	*IE_CMD_CFG_ADDRLEN(buf)   = 0x2e;
1514 	*IE_CMD_CFG_PRIORITY(buf)  = 0;
1515 	*IE_CMD_CFG_IFS(buf)       = 0x60;
1516 	*IE_CMD_CFG_SLOT_LOW(buf)  = 0;
1517 	*IE_CMD_CFG_SLOT_HIGH(buf) = 0xf2;
1518 	*IE_CMD_CFG_PROMISC(buf)   = (!!promiscuous) | manchester << 2;
1519 	*IE_CMD_CFG_CRSCDT(buf)    = 0;
1520 	*IE_CMD_CFG_MINLEN(buf)    = 64;
1521 	*IE_CMD_CFG_JUNK(buf)      = 0xff;
1522 	sc->memcopyout(sc, buf, cmd, IE_CMD_CFG_SZ);
1523 	setup_simple_command(sc, IE_CMD_CONFIG, cmd);
1524 	IE_BUS_BARRIER(sc, cmd, IE_CMD_CFG_SZ, BUS_SPACE_BARRIER_WRITE);
1525 
1526 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmd, IE_STAT_COMPL, 0);
1527 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmd));
1528 	if (cmdresult != 0) {
1529 		aprint_error_dev(sc->sc_dev,
1530 		    "configure command timed out; status %x\n", status);
1531 		return (0);
1532 	}
1533 	if ((status & IE_STAT_OK) == 0) {
1534 		aprint_error_dev(sc->sc_dev,
1535 		    "configure command failed; status %x\n", status);
1536 		return (0);
1537 	}
1538 
1539 	/* Squash any pending interrupts */
1540 	ie_ack(sc, IE_ST_WHENCE);
1541 	return (1);
1542 }
1543 
1544 static int
ie_ia_setup(struct ie_softc * sc,int cmdbuf)1545 ie_ia_setup(struct ie_softc *sc, int cmdbuf)
1546 {
1547 	int cmdresult, status;
1548 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1549 
1550 	setup_simple_command(sc, IE_CMD_IASETUP, cmdbuf);
1551 
1552 	(sc->memcopyout)(sc, CLLADDR(ifp->if_sadl),
1553 			 IE_CMD_IAS_EADDR(cmdbuf), ETHER_ADDR_LEN);
1554 
1555 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmdbuf, IE_STAT_COMPL, 0);
1556 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmdbuf));
1557 	if (cmdresult != 0) {
1558 		aprint_error_dev(sc->sc_dev,
1559 		    "individual address command timed out; status %x\n",
1560 		    status);
1561 		return (0);
1562 	}
1563 	if ((status & IE_STAT_OK) == 0) {
1564 		aprint_error_dev(sc->sc_dev,
1565 		    "individual address command failed; status %x\n", status);
1566 		return (0);
1567 	}
1568 
1569 	/* Squash any pending interrupts */
1570 	ie_ack(sc, IE_ST_WHENCE);
1571 	return (1);
1572 }
1573 
1574 /*
1575  * Run the multicast setup command.
1576  * Called at splnet().
1577  */
1578 static int
ie_mc_setup(struct ie_softc * sc,int cmdbuf)1579 ie_mc_setup(struct ie_softc *sc, int cmdbuf)
1580 {
1581 	int cmdresult, status;
1582 
1583 	if (sc->mcast_count == 0)
1584 		return (1);
1585 
1586 	setup_simple_command(sc, IE_CMD_MCAST, cmdbuf);
1587 
1588 	(sc->memcopyout)(sc, (void *)sc->mcast_addrs,
1589 			 IE_CMD_MCAST_MADDR(cmdbuf),
1590 			 sc->mcast_count * ETHER_ADDR_LEN);
1591 
1592 	sc->ie_bus_write16(sc, IE_CMD_MCAST_BYTES(cmdbuf),
1593 			       sc->mcast_count * ETHER_ADDR_LEN);
1594 
1595 	/* Start the command */
1596 	cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmdbuf, IE_STAT_COMPL,
1597 	    0);
1598 	status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmdbuf));
1599 	if (cmdresult != 0) {
1600 		aprint_error_dev(sc->sc_dev,
1601 		    "multicast setup command timed out; status %x\n", status);
1602 		return (0);
1603 	}
1604 	if ((status & IE_STAT_OK) == 0) {
1605 		aprint_error_dev(sc->sc_dev,
1606 		    "multicast setup command failed; status %x\n", status);
1607 		return (0);
1608 	}
1609 
1610 	/* Squash any pending interrupts */
1611 	ie_ack(sc, IE_ST_WHENCE);
1612 	return (1);
1613 }
1614 
1615 /*
1616  * This routine takes the environment generated by check_ie_present() and adds
1617  * to it all the other structures we need to operate the adapter.  This
1618  * includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands, starting
1619  * the receiver unit, and clearing interrupts.
1620  *
1621  * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
1622  */
1623 int
i82586_init(struct ifnet * ifp)1624 i82586_init(struct ifnet *ifp)
1625 {
1626 	struct ie_softc *sc = ifp->if_softc;
1627 	int cmd;
1628 
1629 	sc->async_cmd_inprogress = 0;
1630 
1631 	cmd = sc->buf_area;
1632 
1633 	/*
1634 	 * Send the configure command first.
1635 	 */
1636 	if (ie_cfg_setup(sc, cmd, sc->promisc, 0) == 0)
1637 		return EIO;
1638 
1639 	/*
1640 	 * Send the Individual Address Setup command.
1641 	 */
1642 	if (ie_ia_setup(sc, cmd) == 0)
1643 		return EIO;
1644 
1645 	/*
1646 	 * Run the time-domain reflectometer.
1647 	 */
1648 	ie_run_tdr(sc, cmd);
1649 
1650 	/*
1651 	 * Set the multi-cast filter, if any
1652 	 */
1653 	if (ie_mc_setup(sc, cmd) == 0)
1654 		return EIO;
1655 
1656 	/*
1657 	 * Acknowledge any interrupts we have generated thus far.
1658 	 */
1659 	ie_ack(sc, IE_ST_WHENCE);
1660 
1661 	/*
1662 	 * Set up the transmit and recv buffers.
1663 	 */
1664 	i82586_setup_bufs(sc);
1665 
1666 	if (sc->hwinit)
1667 		(sc->hwinit)(sc);
1668 
1669 	ifp->if_flags |= IFF_RUNNING;
1670 	ifp->if_flags &= ~IFF_OACTIVE;
1671 
1672 	if (NTXBUF < 2)
1673 		sc->do_xmitnopchain = 0;
1674 
1675 	i82586_start_transceiver(sc);
1676 	return (0);
1677 }
1678 
1679 /*
1680  * Start the RU and possibly the CU unit
1681  */
1682 static void
i82586_start_transceiver(struct ie_softc * sc)1683 i82586_start_transceiver(struct ie_softc *sc)
1684 {
1685 
1686 	/*
1687 	 * Start RU at current position in frame & RBD lists.
1688 	 */
1689 	sc->ie_bus_write16(sc, IE_RFRAME_BUFDESC(sc->rframes,sc->rfhead),
1690 			       IE_RBD_ADDR(sc->rbds, sc->rbhead));
1691 
1692 	sc->ie_bus_write16(sc, IE_SCB_RCVLST(sc->scb),
1693 			       IE_RFRAME_ADDR(sc->rframes,sc->rfhead));
1694 
1695 	if (sc->do_xmitnopchain) {
1696 		/* Stop transmit command chain */
1697 		if (i82586_start_cmd(sc, IE_CUC_SUSPEND|IE_RUC_SUSPEND, 0, 0, 0))
1698 			aprint_error_dev(sc->sc_dev,
1699 			    "CU/RU stop command timed out\n");
1700 
1701 		/* Start the receiver & transmitter chain */
1702 		/* sc->scb->ie_command_list =
1703 			IEADDR(sc->nop_cmds[(sc->xctail+NTXBUF-1) % NTXBUF]);*/
1704 		sc->ie_bus_write16(sc, IE_SCB_CMDLST(sc->scb),
1705 				   IE_CMD_NOP_ADDR(
1706 					sc->nop_cmds,
1707 					(sc->xctail + NTXBUF - 1) % NTXBUF));
1708 
1709 		if (i82586_start_cmd(sc, IE_CUC_START|IE_RUC_START, 0, 0, 0))
1710 			aprint_error_dev(sc->sc_dev,
1711 			    "CU/RU command timed out\n");
1712 	} else {
1713 		if (i82586_start_cmd(sc, IE_RUC_START, 0, 0, 0))
1714 			aprint_error_dev(sc->sc_dev, "RU command timed out\n");
1715 	}
1716 }
1717 
1718 void
i82586_stop(struct ifnet * ifp,int disable)1719 i82586_stop(struct ifnet *ifp, int disable)
1720 {
1721 	struct ie_softc *sc = ifp->if_softc;
1722 
1723 	if (i82586_start_cmd(sc, IE_RUC_SUSPEND | IE_CUC_SUSPEND, 0, 0, 0))
1724 		aprint_error_dev(sc->sc_dev,
1725 		    "iestop: disable commands timed out\n");
1726 }
1727 
1728 int
i82586_ioctl(struct ifnet * ifp,unsigned long cmd,void * data)1729 i82586_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
1730 {
1731 	struct ie_softc *sc = ifp->if_softc;
1732 	struct ifreq *ifr = (struct ifreq *)data;
1733 	int s, error = 0;
1734 
1735 	s = splnet();
1736 	switch(cmd) {
1737         case SIOCGIFMEDIA:
1738         case SIOCSIFMEDIA:
1739                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1740                 break;
1741 	default:
1742 		error = ether_ioctl(ifp, cmd, data);
1743 		if (error == ENETRESET) {
1744 			/*
1745 			 * Multicast list has changed; set the hardware filter
1746 			 * accordingly.
1747 			 */
1748 			if (ifp->if_flags & IFF_RUNNING)
1749 				ie_mc_reset(sc);
1750 			error = 0;
1751 		}
1752 		break;
1753 	}
1754 #if I82586_DEBUG
1755 	if (cmd == SIOCSIFFLAGS)
1756 		sc->sc_debug = (ifp->if_flags & IFF_DEBUG) ? IED_ALL : 0;
1757 #endif
1758 	splx(s);
1759 	return (error);
1760 }
1761 
1762 static void
ie_mc_reset(struct ie_softc * sc)1763 ie_mc_reset(struct ie_softc *sc)
1764 {
1765 	struct ether_multi *enm;
1766 	struct ether_multistep step;
1767 	int size;
1768 
1769 	/*
1770 	 * Step through the list of addresses.
1771 	 */
1772 again:
1773 	size = 0;
1774 	sc->mcast_count = 0;
1775 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1776 	while (enm) {
1777 		size += 6;
1778 		if (sc->mcast_count >= IE_MAXMCAST ||
1779 		    memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
1780 			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
1781 			i82586_ioctl(&sc->sc_ethercom.ec_if,
1782 				     SIOCSIFFLAGS, NULL);
1783 			return;
1784 		}
1785 		ETHER_NEXT_MULTI(step, enm);
1786 	}
1787 
1788 	if (size > sc->mcast_addrs_size) {
1789 		/* Need to allocate more space */
1790 		if (sc->mcast_addrs_size)
1791 			free(sc->mcast_addrs, M_IFMADDR);
1792 		sc->mcast_addrs = (char *)
1793 			malloc(size, M_IFMADDR, M_WAITOK);
1794 		sc->mcast_addrs_size = size;
1795 	}
1796 
1797 	/*
1798 	 * We've got the space; now copy the addresses
1799 	 */
1800 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1801 	while (enm) {
1802 		if (sc->mcast_count >= IE_MAXMCAST)
1803 			goto again; /* Just in case */
1804 
1805 		memcpy(&sc->mcast_addrs[sc->mcast_count], enm->enm_addrlo, 6);
1806 		sc->mcast_count++;
1807 		ETHER_NEXT_MULTI(step, enm);
1808 	}
1809 	sc->want_mcsetup = 1;
1810 }
1811 
1812 /*
1813  * Media change callback.
1814  */
1815 int
i82586_mediachange(struct ifnet * ifp)1816 i82586_mediachange(struct ifnet *ifp)
1817 {
1818         struct ie_softc *sc = ifp->if_softc;
1819 
1820         if (sc->sc_mediachange)
1821                 return ((*sc->sc_mediachange)(sc));
1822         return (0);
1823 }
1824 
1825 /*
1826  * Media status callback.
1827  */
1828 void
i82586_mediastatus(struct ifnet * ifp,struct ifmediareq * ifmr)1829 i82586_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1830 {
1831         struct ie_softc *sc = ifp->if_softc;
1832 
1833         if (sc->sc_mediastatus)
1834                 (*sc->sc_mediastatus)(sc, ifmr);
1835 }
1836 
1837 #if I82586_DEBUG
1838 void
print_rbd(struct ie_softc * sc,int n)1839 print_rbd(struct ie_softc *sc, int n)
1840 {
1841 
1842 	printf("RBD at %08x:\n  status %04x, next %04x, buffer %lx\n"
1843 		"length/EOL %04x\n", IE_RBD_ADDR(sc->rbds,n),
1844 		sc->ie_bus_read16(sc, IE_RBD_STATUS(sc->rbds,n)),
1845 		sc->ie_bus_read16(sc, IE_RBD_NEXT(sc->rbds,n)),
1846 		(u_long)0,/*bus_space_read_4(sc->bt, sc->bh, IE_RBD_BUFADDR(sc->rbds,n)),-* XXX */
1847 		sc->ie_bus_read16(sc, IE_RBD_BUFLEN(sc->rbds,n)));
1848 }
1849 #endif
1850