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