xref: /netbsd-src/sys/arch/sun3/dev/if_ie.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: if_ie.c,v 1.54 2010/01/19 22:06:22 pooka Exp $ */
2 
3 /*-
4  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
5  * Copyright (c) 1992, 1993, University of Vermont and State
6  *  Agricultural College.
7  * Copyright (c) 1992, 1993, Garrett A. Wollman.
8  *
9  * Portions:
10  * Copyright (c) 1994, 1995, Rafal K. Boni
11  * Copyright (c) 1990, 1991, William F. Jolitz
12  * Copyright (c) 1990, The Regents of the University of California
13  *
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by Charles M. Hannum, by the
27  *	University of Vermont and State Agricultural College and Garrett A.
28  *	Wollman, by William F. Jolitz, and by the University of California,
29  *	Berkeley, Lawrence Berkeley Laboratory, and its contributors.
30  * 4. Neither the names of the Universities nor the names of the authors
31  *    may be used to endorse or promote products derived from this software
32  *    without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  */
46 
47 /*
48  * Intel 82586 Ethernet chip
49  * Register, bit, and structure definitions.
50  *
51  * Original StarLAN driver written by Garrett Wollman with reference to the
52  * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
53  *
54  * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
55  *
56  * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
57  *
58  * Majorly cleaned up and 3C507 code merged by Charles Hannum.
59  *
60  * Converted to SUN ie driver by Charles D. Cranor,
61  *		October 1994, January 1995.
62  * This sun version based on i386 version 1.30.
63  * [ see sys/dev/isa/if_ie.c ]
64  */
65 
66 /*
67  * The i82586 is a very painful chip, found in sun3's, sun-4/100's
68  * sun-4/200's, and VME based suns.  The byte order is all wrong for a
69  * SUN, making life difficult.  Programming this chip is mostly the same,
70  * but certain details differ from system to system.  This driver is
71  * written so that different "ie" interfaces can be controled by the same
72  * driver.
73  */
74 
75 /*
76    Mode of operation:
77 
78    We run the 82586 in a standard Ethernet mode.  We keep NFRAMES
79    received frame descriptors around for the receiver to use, and
80    NRXBUF associated receive buffer descriptors, both in a circular
81    list.  Whenever a frame is received, we rotate both lists as
82    necessary.  (The 586 treats both lists as a simple queue.)  We also
83    keep a transmit command around so that packets can be sent off
84    quickly.
85 
86    We configure the adapter in AL-LOC = 1 mode, which means that the
87    Ethernet/802.3 MAC header is placed at the beginning of the receive
88    buffer rather than being split off into various fields in the RFD.
89    This also means that we must include this header in the transmit
90    buffer as well.
91 
92    By convention, all transmit commands, and only transmit commands,
93    shall have the I (IE_CMD_INTR) bit set in the command.  This way,
94    when an interrupt arrives at ieintr(), it is immediately possible
95    to tell what precisely caused it.  ANY OTHER command-sending
96    routines should run at splnet(), and should post an acknowledgement
97    to every interrupt they generate.
98 */
99 
100 #include <sys/cdefs.h>
101 __KERNEL_RCSID(0, "$NetBSD: if_ie.c,v 1.54 2010/01/19 22:06:22 pooka Exp $");
102 
103 #include "opt_inet.h"
104 #include "opt_ns.h"
105 
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/mbuf.h>
109 #include <sys/buf.h>
110 #include <sys/protosw.h>
111 #include <sys/socket.h>
112 #include <sys/ioctl.h>
113 #include <sys/errno.h>
114 #include <sys/syslog.h>
115 #include <sys/device.h>
116 
117 #include <net/if.h>
118 #include <net/if_types.h>
119 #include <net/if_dl.h>
120 #include <net/if_ether.h>
121 
122 #include <net/bpf.h>
123 #include <net/bpfdesc.h>
124 
125 #ifdef INET
126 #include <netinet/in.h>
127 #include <netinet/in_systm.h>
128 #include <netinet/in_var.h>
129 #include <netinet/ip.h>
130 #include <netinet/if_inarp.h>
131 #endif
132 
133 #ifdef NS
134 #include <netns/ns.h>
135 #include <netns/ns_if.h>
136 #endif
137 
138 #include <uvm/uvm_extern.h>
139 
140 #include <machine/autoconf.h>
141 #include <machine/cpu.h>
142 #include <machine/pmap.h>
143 
144 /*
145  * ugly byte-order hack for SUNs
146  */
147 
148 #define XSWAP(y)	( (((y) & 0xff00) >> 8) | (((y) & 0xff) << 8) )
149 #define SWAP(x)		((u_short)(XSWAP((u_short)(x))))
150 
151 #include "i82586.h"
152 #include "if_iereg.h"
153 #include "if_ievar.h"
154 
155 /* #define	IEDEBUG	XXX */
156 
157 /*
158  * IED: ie debug flags
159  */
160 
161 #define	IED_RINT	0x01
162 #define	IED_TINT	0x02
163 #define	IED_RNR		0x04
164 #define	IED_CNA		0x08
165 #define	IED_READFRAME	0x10
166 #define	IED_ENQ		0x20
167 #define	IED_XMIT	0x40
168 #define	IED_ALL		0x7f
169 
170 #ifdef	IEDEBUG
171 #define	inline	/* not */
172 void print_rbd(volatile struct ie_recv_buf_desc *);
173 int     in_ierint = 0;
174 int     in_ietint = 0;
175 int     ie_debug_flags = 0;
176 #endif
177 
178 /* XXX - Skip TDR for now - it always complains... */
179 int 	ie_run_tdr = 0;
180 
181 static void iewatchdog(struct ifnet *);
182 static int ieinit(struct ie_softc *);
183 static int ieioctl(struct ifnet *, u_long, void *);
184 static void iestart(struct ifnet *);
185 static void iereset(struct ie_softc *);
186 static int ie_setupram(struct ie_softc *);
187 
188 static int cmd_and_wait(struct ie_softc *, int, void *, int);
189 
190 static void ie_drop_packet_buffer(struct ie_softc *);
191 static void ie_readframe(struct ie_softc *, int);
192 static inline void ie_setup_config(struct ie_config_cmd *, int, int);
193 
194 static void ierint(struct ie_softc *);
195 static void iestop(struct ie_softc *);
196 static void ietint(struct ie_softc *);
197 static void iexmit(struct ie_softc *);
198 
199 static int mc_setup(struct ie_softc *, void *);
200 static void mc_reset(struct ie_softc *);
201 static void run_tdr(struct ie_softc *, struct ie_tdr_cmd *);
202 static void iememinit(struct ie_softc *);
203 
204 static inline uint8_t *Align(char *);
205 static inline u_int Swap32(u_int);
206 static inline u_int vtop24(struct ie_softc *, void *);
207 static inline uint16_t vtop16sw(struct ie_softc *, void *);
208 
209 static inline void ie_ack(struct ie_softc *, u_int);
210 static inline u_short ether_cmp(u_char *, uint8_t *);
211 static inline int check_eh(struct ie_softc *, struct ether_header *, int *);
212 static inline int ie_buflen(struct ie_softc *, int);
213 static inline int ie_packet_len(struct ie_softc *);
214 static inline struct mbuf * ieget(struct ie_softc *, int *);
215 
216 
217 /*
218  * Here are a few useful functions.  We could have done these as macros,
219  * but since we have the inline facility, it makes sense to use that
220  * instead.
221  */
222 
223 /* KVA to 24 bit device address */
224 static inline u_int
225 vtop24(struct ie_softc *sc, void *ptr)
226 {
227 	u_int pa;
228 
229 	pa = (vaddr_t)ptr - (vaddr_t)sc->sc_iobase;
230 #ifdef	IEDEBUG
231 	if (pa & ~0xffFFff)
232 		panic("ie:vtop24");
233 #endif
234 	return pa;
235 }
236 
237 /* KVA to 16 bit offset, swapped */
238 static inline u_short
239 vtop16sw(struct ie_softc *sc, void *ptr)
240 {
241 	u_int pa;
242 
243 	pa = (vaddr_t)ptr - (vaddr_t)sc->sc_maddr;
244 #ifdef	IEDEBUG
245 	if (pa & ~0xFFff)
246 		panic("ie:vtop16");
247 #endif
248 
249 	return SWAP(pa);
250 }
251 
252 static inline u_int
253 Swap32(u_int x)
254 {
255 	u_int y;
256 
257 	y = x & 0xFF;
258 	y <<= 8; x >>= 8;
259 	y |= x & 0xFF;
260 	y <<= 8; x >>= 8;
261 	y |= x & 0xFF;
262 	y <<= 8; x >>= 8;
263 	y |= x & 0xFF;
264 
265 	return y;
266 }
267 
268 static inline uint8_t *
269 Align(char *ptr)
270 {
271 	u_long  l = (u_long)ptr;
272 
273 	l = (l + 3) & ~3L;
274 	return (uint8_t *)l;
275 }
276 
277 
278 static inline void
279 ie_ack(struct ie_softc *sc, u_int mask)
280 {
281 	volatile struct ie_sys_ctl_block *scb = sc->scb;
282 
283 	cmd_and_wait(sc, scb->ie_status & mask, 0, 0);
284 }
285 
286 
287 /*
288  * Taken almost exactly from Bill's if_is.c,
289  * then modified beyond recognition...
290  */
291 void
292 ie_attach(struct ie_softc *sc)
293 {
294 	struct ifnet *ifp = &sc->sc_if;
295 
296 	/* MD code has done its part before calling this. */
297 	printf(": macaddr %s\n", ether_sprintf(sc->sc_addr));
298 
299 	/*
300 	 * Compute number of transmit and receive buffers.
301 	 * Tx buffers take 1536 bytes, and fixed in number.
302 	 * Rx buffers are 512 bytes each, variable number.
303 	 * Need at least 1 frame for each 3 rx buffers.
304 	 * The ratio 3bufs:2frames is a compromise.
305 	 */
306 	sc->ntxbuf = NTXBUF;	/* XXX - Fix me... */
307 	switch (sc->sc_msize) {
308 	case 16384:
309 		sc->nframes = 8 * 4;
310 		sc->nrxbuf  = 8 * 6;
311 		break;
312 	case 32768:
313 		sc->nframes = 16 * 4;
314 		sc->nrxbuf  = 16 * 6;
315 		break;
316 	case 65536:
317 		sc->nframes = 32 * 4;
318 		sc->nrxbuf  = 32 * 6;
319 		break;
320 	default:
321 		sc->nframes = 0;
322 	}
323 	if (sc->nframes > MXFRAMES)
324 		sc->nframes = MXFRAMES;
325 	if (sc->nrxbuf > MXRXBUF)
326 		sc->nrxbuf = MXRXBUF;
327 
328 #ifdef	IEDEBUG
329 	aprint_debug_dev(sc->sc_dev,
330 	    "%dK memory, %d tx frames, %d rx frames, %d rx bufs\n",
331 	    (sc->sc_msize >> 10), sc->ntxbuf, sc->nframes, sc->nrxbuf);
332 #endif
333 
334 	if ((sc->nframes <= 0) || (sc->nrxbuf <= 0))
335 		panic("%s: weird memory size", __func__);
336 
337 	/*
338 	 * Setup RAM for transmit/receive
339 	 */
340 	if (ie_setupram(sc) == 0) {
341 		aprint_error(": RAM CONFIG FAILED!\n");
342 		/* XXX should reclaim resources? */
343 		return;
344 	}
345 
346 	/*
347 	 * Initialize and attach S/W interface
348 	 */
349 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
350 	ifp->if_softc = sc;
351 	ifp->if_start = iestart;
352 	ifp->if_ioctl = ieioctl;
353 	ifp->if_watchdog = iewatchdog;
354 	ifp->if_flags =
355 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
356 
357 	/* Attach the interface. */
358 	if_attach(ifp);
359 	ether_ifattach(ifp, sc->sc_addr);
360 }
361 
362 /*
363  * Setup IE's ram space.
364  */
365 static int
366 ie_setupram(struct ie_softc *sc)
367 {
368 	volatile struct ie_sys_conf_ptr *scp;
369 	volatile struct ie_int_sys_conf_ptr *iscp;
370 	volatile struct ie_sys_ctl_block *scb;
371 	int off;
372 
373 	/*
374 	 * Allocate from end of buffer space for
375 	 * ISCP, SCB, and other small stuff.
376 	 */
377 	off = sc->buf_area_sz;
378 	off &= ~3;
379 
380 	/* SCP (address already chosen). */
381 	scp = sc->scp;
382 	(sc->sc_memset)(__UNVOLATILE(scp), 0, sizeof(*scp));
383 
384 	/* ISCP */
385 	off -= sizeof(*iscp);
386 	iscp = (volatile void *)(sc->buf_area + off);
387 	(sc->sc_memset)(__UNVOLATILE(iscp), 0, sizeof(*iscp));
388 	sc->iscp = iscp;
389 
390 	/* SCB */
391 	off -= sizeof(*scb);
392 	scb  = (volatile void *)(sc->buf_area + off);
393 	(sc->sc_memset)(__UNVOLATILE(scb), 0, sizeof(*scb));
394 	sc->scb = scb;
395 
396 	/* Remainder is for buffers, etc. */
397 	sc->buf_area_sz = off;
398 
399 	/*
400 	 * Now fill in the structures we just allocated.
401 	 */
402 
403 	/* SCP: main thing is 24-bit ptr to ISCP */
404 	scp->ie_bus_use = 0;	/* 16-bit */
405 	scp->ie_iscp_ptr = Swap32(vtop24(sc, __UNVOLATILE(iscp)));
406 
407 	/* ISCP */
408 	iscp->ie_busy = 1;	/* ie_busy == char */
409 	iscp->ie_scb_offset = vtop16sw(sc, __UNVOLATILE(scb));
410 	iscp->ie_base = Swap32(vtop24(sc, sc->sc_maddr));
411 
412 	/* SCB */
413 	scb->ie_command_list = SWAP(0xffff);
414 	scb->ie_recv_list    = SWAP(0xffff);
415 
416 	/* Other stuff is done in ieinit() */
417 	(sc->reset_586)(sc);
418 	(sc->chan_attn)(sc);
419 
420 	delay(100);		/* wait a while... */
421 
422 	if (iscp->ie_busy) {
423 		return 0;
424 	}
425 	/*
426 	 * Acknowledge any interrupts we may have caused...
427 	 */
428 	ie_ack(sc, IE_ST_WHENCE);
429 
430 	return 1;
431 }
432 
433 /*
434  * Device timeout/watchdog routine.  Entered if the device neglects to
435  * generate an interrupt after a transmit has been started on it.
436  */
437 static void
438 iewatchdog(struct ifnet *ifp)
439 {
440 	struct ie_softc *sc = ifp->if_softc;
441 
442 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
443 	++ifp->if_oerrors;
444 	iereset(sc);
445 }
446 
447 /*
448  * What to do upon receipt of an interrupt.
449  */
450 int
451 ie_intr(void *arg)
452 {
453 	struct ie_softc *sc = arg;
454 	uint16_t status;
455 	int loopcnt;
456 
457 	/*
458 	 * check for parity error
459 	 */
460 	if (sc->hard_type == IE_VME) {
461 		volatile struct ievme *iev =
462 		    (volatile struct ievme *)sc->sc_reg;
463 
464 		if (iev->status & IEVME_PERR) {
465 			printf("%s: parity error (ctrl 0x%x @ 0x%02x%04x)\n",
466 			    device_xname(sc->sc_dev), iev->pectrl,
467 			    iev->pectrl & IEVME_HADDR, iev->peaddr);
468 			iev->pectrl = iev->pectrl | IEVME_PARACK;
469 		}
470 	}
471 
472 	status = sc->scb->ie_status;
473 	if ((status & IE_ST_WHENCE) == 0)
474 		return 0;
475 
476 	loopcnt = sc->nframes;
477  loop:
478 	/* Ack interrupts FIRST in case we receive more during the ISR. */
479 	ie_ack(sc, IE_ST_WHENCE & status);
480 
481 	if (status & (IE_ST_RECV | IE_ST_RNR)) {
482 #ifdef IEDEBUG
483 		in_ierint++;
484 		if (sc->sc_debug & IED_RINT)
485 			printf("%s: rint\n", device_xname(sc->sc_dev));
486 #endif
487 		ierint(sc);
488 #ifdef IEDEBUG
489 		in_ierint--;
490 #endif
491 	}
492 
493 	if (status & IE_ST_DONE) {
494 #ifdef IEDEBUG
495 		in_ietint++;
496 		if (sc->sc_debug & IED_TINT)
497 			printf("%s: tint\n", device_xname(sc->sc_dev));
498 #endif
499 		ietint(sc);
500 #ifdef IEDEBUG
501 		in_ietint--;
502 #endif
503 	}
504 
505 	/*
506 	 * Receiver not ready (RNR) just means it has
507 	 * run out of resources (buffers or frames).
508 	 * One can easily cause this with (i.e.) spray.
509 	 * This is not a serious error, so be silent.
510 	 */
511 	if (status & IE_ST_RNR) {
512 #ifdef IEDEBUG
513 		printf("%s: receiver not ready\n", device_xname(sc->sc_dev));
514 #endif
515 		sc->sc_if.if_ierrors++;
516 		iereset(sc);
517 	}
518 
519 #ifdef IEDEBUG
520 	if ((status & IE_ST_ALLDONE) && (sc->sc_debug & IED_CNA))
521 		printf("%s: cna\n", device_xname(sc->sc_dev));
522 #endif
523 
524 	status = sc->scb->ie_status;
525 	if (status & IE_ST_WHENCE) {
526 		/* It still wants service... */
527 		if (--loopcnt > 0)
528 			goto loop;
529 		/* ... but we've been here long enough. */
530 		log(LOG_ERR, "%s: interrupt stuck?\n",
531 		    device_xname(sc->sc_dev));
532 		iereset(sc);
533 	}
534 	return 1;
535 }
536 
537 /*
538  * Process a received-frame interrupt.
539  */
540 void
541 ierint(struct ie_softc *sc)
542 {
543 	volatile struct ie_sys_ctl_block *scb = sc->scb;
544 	int i, status;
545 	static int timesthru = 1024;
546 
547 	i = sc->rfhead;
548 	for (;;) {
549 		status = sc->rframes[i]->ie_fd_status;
550 
551 		if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
552 			if (!--timesthru) {
553 				sc->sc_if.if_ierrors +=
554 				    SWAP(scb->ie_err_crc) +
555 				    SWAP(scb->ie_err_align) +
556 				    SWAP(scb->ie_err_resource) +
557 				    SWAP(scb->ie_err_overrun);
558 				scb->ie_err_crc = 0;
559 				scb->ie_err_align = 0;
560 				scb->ie_err_resource = 0;
561 				scb->ie_err_overrun = 0;
562 				timesthru = 1024;
563 			}
564 			ie_readframe(sc, i);
565 		} else {
566 			if ((status & IE_FD_RNR) != 0 &&
567 			    (scb->ie_status & IE_RU_READY) == 0) {
568 				sc->rframes[0]->ie_fd_buf_desc = vtop16sw(sc,
569 				    __UNVOLATILE(sc->rbuffs[0]));
570 				scb->ie_recv_list = vtop16sw(sc,
571 				    __UNVOLATILE(sc->rframes[0]));
572 				cmd_and_wait(sc, IE_RU_START, 0, 0);
573 			}
574 			break;
575 		}
576 		i = (i + 1) % sc->nframes;
577 	}
578 }
579 
580 /*
581  * Process a command-complete interrupt.  These are only generated by the
582  * transmission of frames.  This routine is deceptively simple, since most
583  * of the real work is done by iestart().
584  */
585 void
586 ietint(struct ie_softc *sc)
587 {
588 	struct ifnet *ifp;
589 	int status;
590 
591 	ifp = &sc->sc_if;
592 
593 	ifp->if_timer = 0;
594 	ifp->if_flags &= ~IFF_OACTIVE;
595 
596 	status = sc->xmit_cmds[sc->xctail]->ie_xmit_status;
597 
598 	if (!(status & IE_STAT_COMPL) || (status & IE_STAT_BUSY))
599 		printf("%s: command still busy!\n", __func__);
600 
601 	if (status & IE_STAT_OK) {
602 		ifp->if_opackets++;
603 		ifp->if_collisions +=
604 		  SWAP(status & IE_XS_MAXCOLL);
605 	} else {
606 		ifp->if_oerrors++;
607 		/*
608 		 * XXX
609 		 * Check SQE and DEFERRED?
610 		 * What if more than one bit is set?
611 		 */
612 		if (status & IE_STAT_ABORT)
613 			printf("%s: send aborted\n", device_xname(sc->sc_dev));
614 		if (status & IE_XS_LATECOLL)
615 			printf("%s: late collision\n",
616 			    device_xname(sc->sc_dev));
617 		if (status & IE_XS_NOCARRIER)
618 			printf("%s: no carrier\n", device_xname(sc->sc_dev));
619 		if (status & IE_XS_LOSTCTS)
620 			printf("%s: lost CTS\n", device_xname(sc->sc_dev));
621 		if (status & IE_XS_UNDERRUN)
622 			printf("%s: DMA underrun\n", device_xname(sc->sc_dev));
623 		if (status & IE_XS_EXCMAX) {
624 			/* Do not print this one (too noisy). */
625 			ifp->if_collisions += 16;
626 		}
627 	}
628 
629 	/*
630 	 * If multicast addresses were added or deleted while we
631 	 * were transmitting, mc_reset() set the want_mcsetup flag
632 	 * indicating that we should do it.
633 	 */
634 	if (sc->want_mcsetup) {
635 		mc_setup(sc, (void *)sc->xmit_cbuffs[sc->xctail]);
636 		sc->want_mcsetup = 0;
637 	}
638 
639 	/* Done with the buffer. */
640 	sc->xmit_busy--;
641 	sc->xctail = (sc->xctail + 1) % NTXBUF;
642 
643 	/* Start the next packet, if any, transmitting. */
644 	if (sc->xmit_busy > 0)
645 		iexmit(sc);
646 
647 	iestart(ifp);
648 }
649 
650 /*
651  * Compare two Ether/802 addresses for equality, inlined and
652  * unrolled for speed.  I'd love to have an inline assembler
653  * version of this...   XXX: Who wanted that? mycroft?
654  * I wrote one, but the following is just as efficient.
655  * This expands to 10 short m68k instructions! -gwr
656  * Note: use this like memcmp()
657  */
658 static inline uint16_t
659 ether_cmp(uint8_t *one, uint8_t *two)
660 {
661 	uint16_t *a = (uint16_t *)one;
662 	uint16_t *b = (uint16_t *)two;
663 	uint16_t diff;
664 
665 	diff  = *a++ - *b++;
666 	diff |= *a++ - *b++;
667 	diff |= *a++ - *b++;
668 
669 	return diff;
670 }
671 #define	ether_equal !ether_cmp
672 
673 /*
674  * Check for a valid address.  to_bpf is filled in with one of the following:
675  *   0 -> BPF doesn't get this packet
676  *   1 -> BPF does get this packet
677  *   2 -> BPF does get this packet, but we don't
678  * Return value is true if the packet is for us, and false otherwise.
679  *
680  * This routine is a mess, but it's also critical that it be as fast
681  * as possible.  It could be made cleaner if we can assume that the
682  * only client which will fiddle with IFF_PROMISC is BPF.  This is
683  * probably a good assumption, but we do not make it here.  (Yet.)
684  */
685 static inline int
686 check_eh(struct ie_softc *sc, struct ether_header *eh, int *to_bpf)
687 {
688 	struct ifnet *ifp;
689 
690 	ifp = &sc->sc_if;
691 	*to_bpf = (ifp->if_bpf != 0);
692 
693 	/*
694 	 * This is all handled at a higher level now.
695 	 */
696 	return 1;
697 }
698 
699 /*
700  * We want to isolate the bits that have meaning...  This assumes that
701  * IE_RBUF_SIZE is an even power of two.  If somehow the act_len exceeds
702  * the size of the buffer, then we are screwed anyway.
703  */
704 static inline int
705 ie_buflen(struct ie_softc *sc, int head)
706 {
707 	int len;
708 
709 	len = SWAP(sc->rbuffs[head]->ie_rbd_actual);
710 	len &= (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1));
711 	return len;
712 }
713 
714 static inline int
715 ie_packet_len(struct ie_softc *sc)
716 {
717 	int i;
718 	int head = sc->rbhead;
719 	int acc = 0;
720 
721 	do {
722 		if ((sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)
723 		    == 0) {
724 #ifdef IEDEBUG
725 			print_rbd(sc->rbuffs[sc->rbhead]);
726 #endif
727 			log(LOG_ERR,
728 			    "%s: receive descriptors out of sync at %d\n",
729 			    device_xname(sc->sc_dev), sc->rbhead);
730 			iereset(sc);
731 			return -1;
732 		}
733 
734 		i = sc->rbuffs[head]->ie_rbd_actual & IE_RBD_LAST;
735 
736 		acc += ie_buflen(sc, head);
737 		head = (head + 1) % sc->nrxbuf;
738 	} while (i == 0);
739 
740 	return acc;
741 }
742 
743 /*
744  * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
745  * command to the chip to be executed.  On the way, if we have a BPF listener
746  * also give him a copy.
747  */
748 static void
749 iexmit(struct ie_softc *sc)
750 {
751 	struct ifnet *ifp;
752 
753 	ifp = &sc->sc_if;
754 
755 #ifdef IEDEBUG
756 	if (sc->sc_debug & IED_XMIT)
757 		printf("%s: xmit buffer %d\n", device_xname(sc->sc_dev),
758 		    sc->xctail);
759 #endif
760 
761 	/*
762 	 * If BPF is listening on this interface, let it see the packet before
763 	 * we push it on the wire.
764 	 */
765 	if (ifp->if_bpf)
766 		bpf_ops->bpf_tap(ifp->if_bpf,
767 		    sc->xmit_cbuffs[sc->xctail],
768 		    SWAP(sc->xmit_buffs[sc->xctail]->ie_xmit_flags));
769 
770 	sc->xmit_buffs[sc->xctail]->ie_xmit_flags |= IE_XMIT_LAST;
771 	sc->xmit_buffs[sc->xctail]->ie_xmit_next = SWAP(0xffff);
772 	sc->xmit_buffs[sc->xctail]->ie_xmit_buf =
773 	    Swap32(vtop24(sc, sc->xmit_cbuffs[sc->xctail]));
774 
775 	sc->xmit_cmds[sc->xctail]->com.ie_cmd_link = SWAP(0xffff);
776 	sc->xmit_cmds[sc->xctail]->com.ie_cmd_cmd =
777 	    IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST;
778 
779 	sc->xmit_cmds[sc->xctail]->ie_xmit_status = SWAP(0);
780 	sc->xmit_cmds[sc->xctail]->ie_xmit_desc =
781 	    vtop16sw(sc, __UNVOLATILE(sc->xmit_buffs[sc->xctail]));
782 
783 	sc->scb->ie_command_list =
784 	    vtop16sw(sc, __UNVOLATILE(sc->xmit_cmds[sc->xctail]));
785 	cmd_and_wait(sc, IE_CU_START, 0, 0);
786 
787 	ifp->if_timer = 5;
788 }
789 
790 /*
791  * Read data off the interface, and turn it into an mbuf chain.
792  *
793  * This code is DRAMATICALLY different from the previous version; this
794  * version tries to allocate the entire mbuf chain up front, given the
795  * length of the data available.  This enables us to allocate mbuf
796  * clusters in many situations where before we would have had a long
797  * chain of partially-full mbufs.  This should help to speed up the
798  * operation considerably.  (Provided that it works, of course.)
799  */
800 static inline struct mbuf *
801 ieget(struct ie_softc *sc, int *to_bpf)
802 {
803 	struct mbuf *top, **mp, *m;
804 	int len, totlen, resid;
805 	int thisrboff, thismboff;
806 	int head;
807 	struct ether_header eh;
808 
809 	totlen = ie_packet_len(sc);
810 	if (totlen <= 0)
811 		return 0;
812 
813 	head = sc->rbhead;
814 
815 	/*
816 	 * Snarf the Ethernet header.
817 	 */
818 	(sc->sc_memcpy)((void *)&eh, (void *)sc->cbuffs[head],
819 	    sizeof(struct ether_header));
820 
821 	/*
822 	 * As quickly as possible, check if this packet is for us.
823 	 * If not, don't waste a single cycle copying the rest of the
824 	 * packet in.
825 	 * This is only a consideration when FILTER is defined; i.e., when
826 	 * we are either running BPF or doing multicasting.
827 	 */
828 	if (check_eh(sc, &eh, to_bpf) == 0) {
829 		/* just this case, it's not an error */
830 		sc->sc_if.if_ierrors--;
831 		return 0;
832 	}
833 
834 	resid = totlen;
835 
836 	MGETHDR(m, M_DONTWAIT, MT_DATA);
837 	if (m == 0)
838 		return 0;
839 
840 	m->m_pkthdr.rcvif = &sc->sc_if;
841 	m->m_pkthdr.len = totlen;
842 	len = MHLEN;
843 	top = 0;
844 	mp = &top;
845 
846 	/*
847 	 * This loop goes through and allocates mbufs for all the data we will
848 	 * be copying in.  It does not actually do the copying yet.
849 	 */
850 	while (totlen > 0) {
851 		if (top) {
852 			MGET(m, M_DONTWAIT, MT_DATA);
853 			if (m == 0) {
854 				m_freem(top);
855 				return 0;
856 			}
857 			len = MLEN;
858 		}
859 		if (totlen >= MINCLSIZE) {
860 			MCLGET(m, M_DONTWAIT);
861 			if (m->m_flags & M_EXT)
862 				len = MCLBYTES;
863 		}
864 
865 		if (mp == &top) {
866 			char *newdata = (char *)
867 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
868 			    sizeof(struct ether_header);
869 			len -= newdata - m->m_data;
870 			m->m_data = newdata;
871 		}
872 
873 		m->m_len = len = min(totlen, len);
874 
875 		totlen -= len;
876 		*mp = m;
877 		mp = &m->m_next;
878 	}
879 
880 	m = top;
881 	thismboff = 0;
882 
883 	/*
884 	 * Copy the Ethernet header into the mbuf chain.
885 	 */
886 	memcpy(mtod(m, void *), &eh, sizeof(struct ether_header));
887 	thismboff = sizeof(struct ether_header);
888 	thisrboff = sizeof(struct ether_header);
889 	resid -= sizeof(struct ether_header);
890 
891 	/*
892 	 * Now we take the mbuf chain (hopefully only one mbuf most of the
893 	 * time) and stuff the data into it.  There are no possible failures
894 	 * at or after this point.
895 	 */
896 	while (resid > 0) {
897 		int thisrblen = ie_buflen(sc, head) - thisrboff;
898 		int thismblen = m->m_len - thismboff;
899 
900 		len = min(thisrblen, thismblen);
901 		(sc->sc_memcpy)(mtod(m, char *) + thismboff,
902 		    (void *)(sc->cbuffs[head] + thisrboff),
903 		    (u_int)len);
904 		resid -= len;
905 
906 		if (len == thismblen) {
907 			m = m->m_next;
908 			thismboff = 0;
909 		} else
910 			thismboff += len;
911 
912 		if (len == thisrblen) {
913 			head = (head + 1) % sc->nrxbuf;
914 			thisrboff = 0;
915 		} else
916 			thisrboff += len;
917 	}
918 
919 	/*
920 	 * Unless something changed strangely while we were doing the copy,
921 	 * we have now copied everything in from the shared memory.
922 	 * This means that we are done.
923 	 */
924 	return top;
925 }
926 
927 /*
928  * Read frame NUM from unit UNIT (pre-cached as IE).
929  *
930  * This routine reads the RFD at NUM, and copies in the buffers from
931  * the list of RBD, then rotates the RBD and RFD lists so that the receiver
932  * doesn't start complaining.  Trailers are DROPPED---there's no point
933  * in wasting time on confusing code to deal with them.  Hopefully,
934  * this machine will never ARP for trailers anyway.
935  */
936 static void
937 ie_readframe(struct ie_softc *sc, int num)
938 {
939 	int status;
940 	struct mbuf *m = 0;
941 	int bpf_gets_it = 0;
942 
943 	status = sc->rframes[num]->ie_fd_status;
944 
945 	/* Advance the RFD list, since we're done with this descriptor. */
946 	sc->rframes[num]->ie_fd_status = SWAP(0);
947 	sc->rframes[num]->ie_fd_last |= IE_FD_LAST;
948 	sc->rframes[sc->rftail]->ie_fd_last &= ~IE_FD_LAST;
949 	sc->rftail = (sc->rftail + 1) % sc->nframes;
950 	sc->rfhead = (sc->rfhead + 1) % sc->nframes;
951 
952 	if (status & IE_FD_OK) {
953 		m = ieget(sc, &bpf_gets_it);
954 		ie_drop_packet_buffer(sc);
955 	}
956 	if (m == 0) {
957 		sc->sc_if.if_ierrors++;
958 		return;
959 	}
960 
961 #ifdef IEDEBUG
962 	if (sc->sc_debug & IED_READFRAME) {
963 		struct ether_header *eh = mtod(m, struct ether_header *);
964 
965 		printf("%s: frame from ether %s type 0x%x\n",
966 		    device_xname(sc->sc_dev),
967 		    ether_sprintf(eh->ether_shost), (u_int)eh->ether_type);
968 	}
969 #endif
970 
971 	/*
972 	 * Check for a BPF filter; if so, hand it up.
973 	 * Note that we have to stick an extra mbuf up front, because
974 	 * bpf_mtap expects to have the ether header at the front.
975 	 * It doesn't matter that this results in an ill-formatted mbuf chain,
976 	 * since BPF just looks at the data.  (It doesn't try to free the mbuf,
977 	 * tho' it will make a copy for tcpdump.)
978 	 */
979 	if (bpf_gets_it) {
980 		/* Pass it up. */
981 		bpf_ops->bpf_mtap(sc->sc_if.if_bpf, m);
982 
983 		/*
984 		 * A signal passed up from the filtering code indicating that
985 		 * the packet is intended for BPF but not for the protocol
986 		 * machinery.  We can save a few cycles by not handing it off
987 		 * to them.
988 		 */
989 		if (bpf_gets_it == 2) {
990 			m_freem(m);
991 			return;
992 		}
993 	}
994 
995 	/*
996 	 * In here there used to be code to check destination addresses upon
997 	 * receipt of a packet.  We have deleted that code, and replaced it
998 	 * with code to check the address much earlier in the cycle, before
999 	 * copying the data in; this saves us valuable cycles when operating
1000 	 * as a multicast router or when using BPF.
1001 	 */
1002 
1003 	/*
1004 	 * Finally pass this packet up to higher layers.
1005 	 */
1006 	(*sc->sc_if.if_input)(&sc->sc_if, m);
1007 	sc->sc_if.if_ipackets++;
1008 }
1009 
1010 static void
1011 ie_drop_packet_buffer(struct ie_softc *sc)
1012 {
1013 	int i;
1014 
1015 	do {
1016 		/*
1017 		 * This means we are somehow out of sync.  So, we reset the
1018 		 * adapter.
1019 		 */
1020 		if ((sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)
1021 		    == 0) {
1022 #ifdef IEDEBUG
1023 			print_rbd(sc->rbuffs[sc->rbhead]);
1024 #endif
1025 			log(LOG_ERR,
1026 			    "%s: receive descriptors out of sync at %d\n",
1027 			    device_xname(sc->sc_dev), sc->rbhead);
1028 			iereset(sc);
1029 			return;
1030 		}
1031 
1032 		i = sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_LAST;
1033 
1034 		sc->rbuffs[sc->rbhead]->ie_rbd_length |= IE_RBD_LAST;
1035 		sc->rbuffs[sc->rbhead]->ie_rbd_actual = SWAP(0);
1036 		sc->rbhead = (sc->rbhead + 1) % sc->nrxbuf;
1037 		sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
1038 		sc->rbtail = (sc->rbtail + 1) % sc->nrxbuf;
1039 	} while (i == 0);
1040 }
1041 
1042 /*
1043  * Start transmission on an interface.
1044  */
1045 static void
1046 iestart(struct ifnet *ifp)
1047 {
1048 	struct ie_softc *sc = ifp->if_softc;
1049 	struct mbuf *m0, *m;
1050 	uint8_t *buffer;
1051 	uint16_t len;
1052 
1053 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1054 		return;
1055 
1056 	for (;;) {
1057 		if (sc->xmit_busy == sc->ntxbuf) {
1058 			ifp->if_flags |= IFF_OACTIVE;
1059 			break;
1060 		}
1061 
1062 		IF_DEQUEUE(&ifp->if_snd, m0);
1063 		if (m0 == 0)
1064 			break;
1065 
1066 		/* We need to use m->m_pkthdr.len, so require the header */
1067 		if ((m0->m_flags & M_PKTHDR) == 0)
1068 			panic("%s: no header mbuf", __func__);
1069 
1070 		/* Tap off here if there is a BPF listener. */
1071 		if (ifp->if_bpf)
1072 			bpf_ops->bpf_mtap(ifp->if_bpf, m0);
1073 
1074 #ifdef IEDEBUG
1075 		if (sc->sc_debug & IED_ENQ)
1076 			printf("%s: fill buffer %d\n", device_xname(sc->sc_dev),
1077 			    sc->xchead);
1078 #endif
1079 
1080 		buffer = sc->xmit_cbuffs[sc->xchead];
1081 		for (m = m0; m != 0; m = m->m_next) {
1082 			(sc->sc_memcpy)(buffer, mtod(m, void *), m->m_len);
1083 			buffer += m->m_len;
1084 		}
1085 		if (m0->m_pkthdr.len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1086 			sc->sc_memset(buffer, 0,
1087 			    ETHER_MIN_LEN - ETHER_CRC_LEN - m0->m_pkthdr.len);
1088 			len = ETHER_MIN_LEN - ETHER_CRC_LEN;
1089 		} else
1090 			len = m0->m_pkthdr.len;
1091 
1092 		m_freem(m0);
1093 		sc->xmit_buffs[sc->xchead]->ie_xmit_flags = SWAP(len);
1094 
1095 		/* Start the first packet transmitting. */
1096 		if (sc->xmit_busy == 0)
1097 			iexmit(sc);
1098 
1099 		sc->xchead = (sc->xchead + 1) % sc->ntxbuf;
1100 		sc->xmit_busy++;
1101 	}
1102 }
1103 
1104 static void
1105 iereset(struct ie_softc *sc)
1106 {
1107 	int s;
1108 
1109 	s = splnet();
1110 
1111 	/* No message here.  The caller does that. */
1112 	iestop(sc);
1113 
1114 	/*
1115 	 * Stop i82586 dead in its tracks.
1116 	 */
1117 	if (cmd_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
1118 		printf("%s: abort commands timed out\n",
1119 		    device_xname(sc->sc_dev));
1120 
1121 	if (cmd_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
1122 		printf("%s: disable commands timed out\n",
1123 		    device_xname(sc->sc_dev));
1124 
1125 	ieinit(sc);
1126 
1127 	splx(s);
1128 }
1129 
1130 /*
1131  * Send a command to the controller and wait for it to either
1132  * complete or be accepted, depending on the command.  If the
1133  * command pointer is null, then pretend that the command is
1134  * not an action command.  If the command pointer is not null,
1135  * and the command is an action command, wait for
1136  * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
1137  * to become true.
1138  */
1139 static int
1140 cmd_and_wait(struct ie_softc *sc, int cmd, void *pcmd, int mask)
1141 {
1142 	volatile struct ie_cmd_common *cc = pcmd;
1143 	volatile struct ie_sys_ctl_block *scb = sc->scb;
1144 	int tmo;
1145 
1146 	scb->ie_command = (uint16_t)cmd;
1147 	(sc->chan_attn)(sc);
1148 
1149 	/* Wait for the command to be accepted by the CU. */
1150 	tmo = 10;
1151 	while (scb->ie_command && --tmo)
1152 		delay(10);
1153 	if (scb->ie_command) {
1154 #ifdef	IEDEBUG
1155 		printf("%s: cmd_and_wait, CU stuck (1)\n",
1156 		    device_xname(sc->sc_dev));
1157 #endif
1158 		return -1;	/* timed out */
1159 	}
1160 
1161 	/*
1162 	 * If asked, also wait for it to finish.
1163 	 */
1164 	if (IE_ACTION_COMMAND(cmd) && pcmd) {
1165 
1166 		/*
1167 		 * According to the packet driver, the minimum timeout should
1168 		 * be .369 seconds, which we round up to .4.
1169 		 */
1170 		tmo = 36900;
1171 
1172 		/*
1173 		 * Now spin-lock waiting for status.  This is not a very nice
1174 		 * thing to do, but I haven't figured out how, or indeed if, we
1175 		 * can put the process waiting for action to sleep.  (We may
1176 		 * be getting called through some other timeout running in the
1177 		 * kernel.)
1178 		 */
1179 		while (((cc->ie_cmd_status & mask) == 0) && --tmo)
1180 			delay(10);
1181 
1182 		if ((cc->ie_cmd_status & mask) == 0) {
1183 #ifdef	IEDEBUG
1184 			printf("%s: cmd_and_wait, CU stuck (2)\n",
1185 			    device_xname(sc->sc_dev));
1186 #endif
1187 			return -1;	/* timed out */
1188 		}
1189 	}
1190 	return 0;
1191 }
1192 
1193 /*
1194  * Run the time-domain reflectometer.
1195  */
1196 static void
1197 run_tdr(struct ie_softc *sc, struct ie_tdr_cmd *cmd)
1198 {
1199 	int result;
1200 
1201 	cmd->com.ie_cmd_status = SWAP(0);
1202 	cmd->com.ie_cmd_cmd = IE_CMD_TDR | IE_CMD_LAST;
1203 	cmd->com.ie_cmd_link = SWAP(0xffff);
1204 
1205 	sc->scb->ie_command_list = vtop16sw(sc, cmd);
1206 	cmd->ie_tdr_time = SWAP(0);
1207 
1208 	if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1209 	    (cmd->com.ie_cmd_status & IE_STAT_OK) == 0)
1210 		result = 0x10000;	/* impossible value */
1211 	else
1212 		result = cmd->ie_tdr_time;
1213 
1214 	ie_ack(sc, IE_ST_WHENCE);
1215 
1216 	if (result & IE_TDR_SUCCESS)
1217 		return;
1218 
1219 	if (result & 0x10000) {
1220 		printf("%s: TDR command failed\n", device_xname(sc->sc_dev));
1221 	} else if (result & IE_TDR_XCVR) {
1222 		printf("%s: transceiver problem\n", device_xname(sc->sc_dev));
1223 	} else if (result & IE_TDR_OPEN) {
1224 		printf("%s: TDR detected an open %d clocks away\n",
1225 		    device_xname(sc->sc_dev), SWAP(result & IE_TDR_TIME));
1226 	} else if (result & IE_TDR_SHORT) {
1227 		printf("%s: TDR detected a short %d clocks away\n",
1228 		    device_xname(sc->sc_dev), SWAP(result & IE_TDR_TIME));
1229 	} else {
1230 		printf("%s: TDR returned unknown status 0x%x\n",
1231 		    device_xname(sc->sc_dev), result);
1232 	}
1233 }
1234 
1235 /*
1236  * iememinit: set up the buffers
1237  *
1238  * we have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
1239  * this is to be used for the buffers.  the chip indexs its control data
1240  * structures with 16 bit offsets, and it indexes actual buffers with
1241  * 24 bit addresses.   so we should allocate control buffers first so that
1242  * we don't overflow the 16 bit offset field.   The number of transmit
1243  * buffers is fixed at compile time.
1244  *
1245  * note: this function was written to be easy to understand, rather than
1246  *       highly efficient (it isn't in the critical path).
1247  *
1248  * The memory layout is: tbufs, rbufs, (gap), control blocks
1249  * [tbuf0, tbuf1] [rbuf0,...rbufN] gap [rframes] [tframes]
1250  * XXX - This needs review...
1251  */
1252 static void
1253 iememinit(struct ie_softc *sc)
1254 {
1255 	uint8_t *ptr;
1256 	int i;
1257 	uint16_t nxt;
1258 
1259 	/* First, zero all the memory. */
1260 	ptr = sc->buf_area;
1261 	(sc->sc_memset)(ptr, 0, sc->buf_area_sz);
1262 
1263 	/* Allocate tx/rx buffers. */
1264 	for (i = 0; i < NTXBUF; i++) {
1265 		sc->xmit_cbuffs[i] = ptr;
1266 		ptr += IE_TBUF_SIZE;
1267 	}
1268 	for (i = 0; i < sc->nrxbuf; i++) {
1269 		sc->cbuffs[i] = ptr;
1270 		ptr += IE_RBUF_SIZE;
1271 	}
1272 
1273 	/* Small pad (Don't trust the chip...) */
1274 	ptr += 16;
1275 
1276 	/* Allocate and fill in xmit buffer descriptors. */
1277 	for (i = 0; i < NTXBUF; i++) {
1278 		sc->xmit_buffs[i] = (volatile void *)ptr;
1279 		ptr = Align(ptr + sizeof(*sc->xmit_buffs[i]));
1280 		sc->xmit_buffs[i]->ie_xmit_buf =
1281 		    Swap32(vtop24(sc, sc->xmit_cbuffs[i]));
1282 		sc->xmit_buffs[i]->ie_xmit_next = SWAP(0xffff);
1283 	}
1284 
1285 	/* Allocate and fill in recv buffer descriptors. */
1286 	for (i = 0; i < sc->nrxbuf; i++) {
1287 		sc->rbuffs[i] = (volatile void *)ptr;
1288 		ptr = Align(ptr + sizeof(*sc->rbuffs[i]));
1289 		sc->rbuffs[i]->ie_rbd_buffer =
1290 		    Swap32(vtop24(sc, sc->cbuffs[i]));
1291 		sc->rbuffs[i]->ie_rbd_length = SWAP(IE_RBUF_SIZE);
1292 	}
1293 
1294 	/* link together recv bufs and set EOL on last */
1295 	i = sc->nrxbuf - 1;
1296 	sc->rbuffs[i]->ie_rbd_length |= IE_RBD_LAST;
1297 	nxt = vtop16sw(sc, __UNVOLATILE(sc->rbuffs[0]));
1298 	do {
1299 		sc->rbuffs[i]->ie_rbd_next = nxt;
1300 		nxt = vtop16sw(sc, __UNVOLATILE(sc->rbuffs[i]));
1301 	} while (--i >= 0);
1302 
1303 	/* Allocate transmit commands. */
1304 	for (i = 0; i < NTXBUF; i++) {
1305 		sc->xmit_cmds[i] = (volatile void *)ptr;
1306 		ptr = Align(ptr + sizeof(*sc->xmit_cmds[i]));
1307 		sc->xmit_cmds[i]->com.ie_cmd_link = SWAP(0xffff);
1308 	}
1309 
1310 	/* Allocate receive frames. */
1311 	for (i = 0; i < sc->nframes; i++) {
1312 		sc->rframes[i] = (volatile void *)ptr;
1313 		ptr = Align(ptr + sizeof(*sc->rframes[i]));
1314 	}
1315 
1316 	/* Link together recv frames and set EOL on last */
1317 	i = sc->nframes - 1;
1318 	sc->rframes[i]->ie_fd_last |= IE_FD_LAST;
1319 	nxt = vtop16sw(sc, __UNVOLATILE(sc->rframes[0]));
1320 	do {
1321 		sc->rframes[i]->ie_fd_next = nxt;
1322 		nxt = vtop16sw(sc, __UNVOLATILE(sc->rframes[i]));
1323 	} while (--i >= 0);
1324 
1325 
1326 	/* Pointers to last packet sent and next available transmit buffer. */
1327 	sc->xchead = sc->xctail = 0;
1328 
1329 	/* Clear transmit-busy flag. */
1330 	sc->xmit_busy = 0;
1331 
1332 	/*
1333 	 * Set the head and tail pointers on receive to keep track of
1334 	 * the order in which RFDs and RBDs are used.   link the
1335 	 * recv frames and buffer into the scb.
1336 	 */
1337 	sc->rfhead = 0;
1338 	sc->rftail = sc->nframes - 1;
1339 	sc->rbhead = 0;
1340 	sc->rbtail = sc->nrxbuf - 1;
1341 
1342 	sc->scb->ie_recv_list =
1343 	    vtop16sw(sc, __UNVOLATILE(sc->rframes[0]));
1344 	sc->rframes[0]->ie_fd_buf_desc =
1345 	    vtop16sw(sc, __UNVOLATILE(sc->rbuffs[0]));
1346 
1347 	i = (ptr - sc->buf_area);
1348 #ifdef IEDEBUG
1349 	printf("IE_DEBUG: used %d of %d bytes\n", i, sc->buf_area_sz);
1350 #endif
1351 	if (i > sc->buf_area_sz)
1352 		panic("ie: iememinit, out of space");
1353 }
1354 
1355 /*
1356  * Run the multicast setup command.
1357  * Called at splnet().
1358  */
1359 static int
1360 mc_setup(struct ie_softc *sc, void *ptr)
1361 {
1362 	struct ie_mcast_cmd *cmd = ptr;	/* XXX - Was volatile */
1363 
1364 	cmd->com.ie_cmd_status = SWAP(0);
1365 	cmd->com.ie_cmd_cmd = IE_CMD_MCAST | IE_CMD_LAST;
1366 	cmd->com.ie_cmd_link = SWAP(0xffff);
1367 
1368 	(sc->sc_memcpy)((void *)cmd->ie_mcast_addrs,
1369 	    (void *)sc->mcast_addrs,
1370 	    sc->mcast_count * sizeof *sc->mcast_addrs);
1371 
1372 	cmd->ie_mcast_bytes =
1373 	    SWAP(sc->mcast_count * ETHER_ADDR_LEN);	/* grrr... */
1374 
1375 	sc->scb->ie_command_list = vtop16sw(sc, cmd);
1376 	if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1377 	    (cmd->com.ie_cmd_status & IE_STAT_OK) == 0) {
1378 		printf("%s: multicast address setup command failed\n",
1379 		    device_xname(sc->sc_dev));
1380 		return 0;
1381 	}
1382 	return 1;
1383 }
1384 
1385 static inline void
1386 ie_setup_config(struct ie_config_cmd *cmd, int promiscuous, int manchester)
1387 {
1388 
1389 	/*
1390 	 * these are all char's so no need to byte-swap
1391 	 */
1392 	cmd->ie_config_count = 0x0c;
1393 	cmd->ie_fifo = 8;
1394 	cmd->ie_save_bad = 0x40;
1395 	cmd->ie_addr_len = 0x2e;
1396 	cmd->ie_priority = 0;
1397 	cmd->ie_ifs = 0x60;
1398 	cmd->ie_slot_low = 0;
1399 	cmd->ie_slot_high = 0xf2;
1400 	cmd->ie_promisc = promiscuous | manchester << 2;
1401 	cmd->ie_crs_cdt = 0;
1402 	cmd->ie_min_len = 64;
1403 	cmd->ie_junk = 0xff;
1404 }
1405 
1406 /*
1407  * This routine inits the ie.
1408  * This includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands,
1409  * starting the receiver unit, and clearing interrupts.
1410  *
1411  * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
1412  */
1413 static int
1414 ieinit(struct ie_softc *sc)
1415 {
1416 	volatile struct ie_sys_ctl_block *scb = sc->scb;
1417 	void *ptr;
1418 	struct ifnet *ifp;
1419 
1420 	ifp = &sc->sc_if;
1421 	ptr = sc->buf_area;	/* XXX - Use scb instead? */
1422 
1423 	/*
1424 	 * Send the configure command first.
1425 	 */
1426 	{
1427 		struct ie_config_cmd *cmd = ptr;	/* XXX - Was volatile */
1428 
1429 		scb->ie_command_list = vtop16sw(sc, cmd);
1430 		cmd->com.ie_cmd_status = SWAP(0);
1431 		cmd->com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
1432 		cmd->com.ie_cmd_link = SWAP(0xffff);
1433 
1434 		ie_setup_config(cmd, (sc->promisc != 0), 0);
1435 
1436 		if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1437 		    (cmd->com.ie_cmd_status & IE_STAT_OK) == 0) {
1438 			printf("%s: configure command failed\n",
1439 			    device_xname(sc->sc_dev));
1440 			return 0;
1441 		}
1442 	}
1443 
1444 	/*
1445 	 * Now send the Individual Address Setup command.
1446 	 */
1447 	{
1448 		struct ie_iasetup_cmd *cmd = ptr;	/* XXX - Was volatile */
1449 
1450 		scb->ie_command_list = vtop16sw(sc, cmd);
1451 		cmd->com.ie_cmd_status = SWAP(0);
1452 		cmd->com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
1453 		cmd->com.ie_cmd_link = SWAP(0xffff);
1454 
1455 		(sc->sc_memcpy)((void *)&cmd->ie_address,
1456 		    CLLADDR(ifp->if_sadl), sizeof(cmd->ie_address));
1457 
1458 		if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1459 		    (cmd->com.ie_cmd_status & IE_STAT_OK) == 0) {
1460 			printf("%s: individual address setup command failed\n",
1461 			    device_xname(sc->sc_dev));
1462 			return 0;
1463 		}
1464 	}
1465 
1466 	/*
1467 	 * Now run the time-domain reflectometer.
1468 	 */
1469 	if (ie_run_tdr)
1470 		run_tdr(sc, ptr);
1471 
1472 	/*
1473 	 * Acknowledge any interrupts we have generated thus far.
1474 	 */
1475 	ie_ack(sc, IE_ST_WHENCE);
1476 
1477 	/*
1478 	 * Set up the transmit and recv buffers.
1479 	 */
1480 	iememinit(sc);
1481 
1482 	/* tell higher levels that we are here */
1483 	ifp->if_flags |= IFF_RUNNING;
1484 	ifp->if_flags &= ~IFF_OACTIVE;
1485 
1486 	sc->scb->ie_recv_list =
1487 	    vtop16sw(sc, __UNVOLATILE(sc->rframes[0]));
1488 	cmd_and_wait(sc, IE_RU_START, 0, 0);
1489 
1490 	ie_ack(sc, IE_ST_WHENCE);
1491 
1492 	if (sc->run_586)
1493 		(sc->run_586)(sc);
1494 
1495 	return 0;
1496 }
1497 
1498 static void
1499 iestop(struct ie_softc *sc)
1500 {
1501 
1502 	cmd_and_wait(sc, IE_RU_DISABLE, 0, 0);
1503 }
1504 
1505 static int
1506 ieioctl(struct ifnet *ifp, u_long cmd, void *data)
1507 {
1508 	struct ie_softc *sc = ifp->if_softc;
1509 	struct ifaddr *ifa = (struct ifaddr *)data;
1510 	int s, error = 0;
1511 
1512 	s = splnet();
1513 
1514 	switch (cmd) {
1515 
1516 	case SIOCINITIFADDR:
1517 		ifp->if_flags |= IFF_UP;
1518 
1519 		switch (ifa->ifa_addr->sa_family) {
1520 #ifdef INET
1521 		case AF_INET:
1522 			ieinit(sc);
1523 			arp_ifinit(ifp, ifa);
1524 			break;
1525 #endif
1526 #ifdef NS
1527 		/* XXX - This code is probably wrong. */
1528 		case AF_NS:
1529 		    {
1530 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1531 
1532 			if (ns_nullhost(*ina))
1533 				ina->x_host =
1534 				    *(union ns_host *)LLADDR(ifp->if_sadl);
1535 			else
1536 				memcpy(LLADDR(ifp->if_sadl),
1537 				    ina->x_host.c_host, ETHER_ADDR_LEN);
1538 			/* Set new address. */
1539 			ieinit(sc);
1540 			break;
1541 		    }
1542 #endif /* NS */
1543 		default:
1544 			ieinit(sc);
1545 			break;
1546 		}
1547 		break;
1548 
1549 	case SIOCSIFFLAGS:
1550 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1551 			break;
1552 		sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
1553 
1554 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1555 		case IFF_RUNNING:
1556 			/*
1557 			 * If interface is marked down and it is running, then
1558 			 * stop it.
1559 			 */
1560 			iestop(sc);
1561 			ifp->if_flags &= ~IFF_RUNNING;
1562 			break;
1563 		case IFF_UP:
1564 			/*
1565 			 * If interface is marked up and it is stopped, then
1566 			 * start it.
1567 			 */
1568 			ieinit(sc);
1569 			break;
1570 		default:
1571 			/*
1572 			 * Reset the interface to pick up changes in any other
1573 			 * flags that affect hardware registers.
1574 			 */
1575 			iestop(sc);
1576 			ieinit(sc);
1577 			break;
1578 		}
1579 #ifdef IEDEBUG
1580 		if (ifp->if_flags & IFF_DEBUG)
1581 			sc->sc_debug = IED_ALL;
1582 		else
1583 			sc->sc_debug = ie_debug_flags;
1584 #endif
1585 		break;
1586 
1587 	case SIOCADDMULTI:
1588 	case SIOCDELMULTI:
1589 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1590 			/*
1591 			 * Multicast list has changed; set the hardware filter
1592 			 * accordingly.
1593 			 */
1594 			if (ifp->if_flags & IFF_RUNNING)
1595 				mc_reset(sc);
1596 			error = 0;
1597 		}
1598 		break;
1599 
1600 	default:
1601 		error = ether_ioctl(ifp, cmd, data);
1602 		break;
1603 	}
1604 	splx(s);
1605 	return error;
1606 }
1607 
1608 static void
1609 mc_reset(struct ie_softc *sc)
1610 {
1611 	struct ether_multi *enm;
1612 	struct ether_multistep step;
1613 	struct ifnet *ifp;
1614 
1615 	ifp = &sc->sc_if;
1616 
1617 	/*
1618 	 * Step through the list of addresses.
1619 	 */
1620 	sc->mcast_count = 0;
1621 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1622 	while (enm) {
1623 		if (sc->mcast_count >= MAXMCAST ||
1624 		    ether_cmp(enm->enm_addrlo, enm->enm_addrhi) != 0) {
1625 			ifp->if_flags |= IFF_ALLMULTI;
1626 			ieioctl(ifp, SIOCSIFFLAGS, NULL);
1627 			goto setflag;
1628 		}
1629 		memcpy(&sc->mcast_addrs[sc->mcast_count], enm->enm_addrlo,
1630 		    ETHER_ADDR_LEN);
1631 		sc->mcast_count++;
1632 		ETHER_NEXT_MULTI(step, enm);
1633 	}
1634 setflag:
1635 	sc->want_mcsetup = 1;
1636 }
1637 
1638 #ifdef IEDEBUG
1639 void
1640 print_rbd(volatile struct ie_recv_buf_desc *rbd)
1641 {
1642 
1643 	printf("RBD at %08lx:\nactual %04x, next %04x, buffer %08x\n"
1644 	    "length %04x, mbz %04x\n", (u_long)rbd, rbd->ie_rbd_actual,
1645 	    rbd->ie_rbd_next, rbd->ie_rbd_buffer, rbd->ie_rbd_length,
1646 	    rbd->mbz);
1647 }
1648 #endif
1649