xref: /netbsd-src/sys/arch/acorn32/podulebus/if_ie.c (revision 200d779b75dbeafa7bc01fd0f60bc61185f6967b)
1 /* $NetBSD: if_ie.c,v 1.35 2015/05/20 09:17:17 ozaki-r Exp $ */
2 
3 /*
4  * Copyright (c) 1995 Melvin Tang-Richardson.
5  * All rights reserved.
6  *
7  * This driver is a major hash up of src/sys/dev/isa/if_ie.c and
8  * src/sys/arch/acorn32/podulebus/kgdb_ie.c  Please refer to copyright
9  * notices from them too.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by RiscBSD.
22  * 4. The name of the company nor the name of the author may be used to
23  *    endorse or promote products derived from this software without specific
24  *    prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY RISCBSD ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL RISCBSD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * RiscBSD kernel project
39  *
40  * if_ie.c
41  *
42  * Ether 1 podule driver
43  *
44  * Created      : 26/06/95
45  */
46 
47 /*
48  *	This driver is at its last beta release.  It should not cause
49  *	any problems (Touch wood)
50  *
51  * 	If it passes field tests again.  This will constitute the realse
52  *	version.
53  */
54 
55 #include <sys/cdefs.h>
56 __KERNEL_RCSID(0, "$NetBSD: if_ie.c,v 1.35 2015/05/20 09:17:17 ozaki-r Exp $");
57 
58 #define IGNORE_ETHER1_IDROM_CHECKSUM
59 
60 /* Standard podule includes */
61 
62 #include "opt_inet.h"
63 #include "opt_ns.h"
64 
65 #include <sys/param.h>
66 
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/conf.h>
70 #include <sys/malloc.h>
71 #include <sys/device.h>
72 #include <machine/io.h>
73 #include <machine/intr.h>
74 #include <acorn32/podulebus/podulebus.h>
75 #include <dev/podulebus/podules.h>
76 
77 /* Include for interface to the net and ethernet subsystems */
78 
79 #include <sys/socket.h>
80 #include <sys/syslog.h>
81 #include <sys/ioctl.h>
82 #include <sys/mbuf.h>
83 
84 #include <net/if.h>
85 #include <net/if_types.h>
86 #include <net/if_dl.h>
87 #include <net/if_ether.h>
88 
89 #ifdef INET
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/in_var.h>
93 #include <netinet/ip.h>
94 #include <netinet/if_inarp.h>
95 #endif
96 
97 /* Import our data structres */
98 
99 #include "if_iereg.h"
100 
101 /* BPF support */
102 
103 #include <net/bpf.h>
104 #include <net/bpfdesc.h>
105 
106 /* Some useful defines and macros */
107 
108 #define PODULE_IRQ_PENDING (1)
109 #define NFRAMES	(16)		/* number of frame to allow for receive */
110 #define NRXBUF	(48)		/* number of receive buffers to allocate */
111 #define IE_RXBUF_SIZE (256) 	/* receive buf size */
112 #define NTXBUF  (2)		/* number of transmit buffers to allocate */
113 #define IE_TXBUF_SIZE (1522) 	/* size of tx buffer */
114 
115 #define PWriteShort(a,b)	WriteWord(a,(b)<<16|(b))
116 
117 #define	xoffsetof(type, member)	(offsetof(type, member) << 1)
118 
119 /* Some data structres local to this file */
120 
121 struct ie_softc {
122 	device_t	sc_dev;
123 	int 		sc_podule_number;
124 	podule_t	*sc_podule;
125 	irqhandler_t 	sc_ih;
126 	int		sc_flags;
127 #define IE_BROKEN	1
128 	int		sc_iobase;
129 	int		sc_fastbase;
130 	int		sc_rom;
131 	int		sc_ram;
132 	int		sc_control;
133 	struct ethercom	sc_ethercom;
134 	int		promisc;
135 	int		sc_irqmode;
136 
137 	u_long	rframes[NFRAMES];
138 	u_long	rbuffs[NRXBUF];
139 	u_long	cbuffs[NRXBUF];
140 	int 	rfhead, rftail, rbhead, rbtail;
141 
142 	u_long 	xmit_cmds[NTXBUF];
143 	u_long 	xmit_buffs[NTXBUF];
144 	u_long 	xmit_cbuffs[NTXBUF];
145 	int	xmit_count;
146 	int	xmit_free;
147 	int	xchead;
148 	int 	xctail;
149 };
150 
151 /* Function and data prototypes */
152 
153 static void host2ie( struct ie_softc *sc, void *src, u_long dest, int size );
154 static void ie2host( struct ie_softc *sc, u_long src, void *dest, int size );
155 static void iezero( struct ie_softc *sc, u_long p, int size );
156 void        iereset( struct ie_softc *sc );
157 void        iewatchdog( struct ifnet *ifp );
158 int         ieioctl( struct ifnet *ifp, u_long cmd, void *data );
159 void        iestart( struct ifnet *ifp );
160 int 	    iestop( struct ie_softc *sc );
161 int         ieinit( struct ie_softc *sc );
162 int 	    ieintr( void *arg );
163 void 	    ietint( struct ie_softc *sc );
164 
165 /* A whopper of a function */
166 static int command_and_wait( struct ie_softc *sc, u_short cmd,
167 			      struct ie_sys_ctl_block *pscb,
168 			      void *pcmd, int ocmd, int scmd, int mask );
169 
170 int ieprobe(device_t, cfdata_t, void *);
171 void ieattach(device_t, device_t, void *);
172 
173 static inline void ie_cli(struct ie_softc *);
174 static inline void ieattn(struct ie_softc *);
175 static inline void setpage(struct ie_softc *, u_long);
176 static void ie_ack(struct ie_softc *, u_short);
177 void PWriteShorts(char *, char *, int);
178 void ReadShorts(char *, char *, int);
179 static void run_tdr(struct ie_softc *);
180 u_long setup_rfa(struct ie_softc *, u_long);
181 static inline int ie_buflen(struct ie_softc *, int);
182 static inline int ie_packet_len(struct ie_softc *);
183 struct mbuf *ieget(struct ie_softc *, int *);
184 void ie_drop_packet_buffer(struct ie_softc *);
185 void ie_read_frame(struct ie_softc *, int num);
186 void ierint(struct ie_softc *);
187 void iexmit(struct ie_softc *);
188 static void start_receiver(struct ie_softc *);
189 
190 
191 /*
192  * Our cfattach structure for the autoconfig system to chew on
193  */
194 
195 CFATTACH_DECL_NEW(ie, sizeof(struct ie_softc),
196     ieprobe, ieattach, NULL, NULL);
197 
198 /* Let's go! */
199 
200 /*
201  * Clear all pending interrupts from the i82586 chip
202  */
203 
204 static inline void
205 ie_cli(struct ie_softc *sc)
206 {
207 	WriteByte(sc->sc_fastbase + (IE_CONTROL<<2), IE_CONT_CLI);
208 }
209 
210 /*
211  * Wake the i82586 chip up and get it to do something
212  */
213 
214 static inline void
215 ieattn(struct ie_softc *sc)
216 {
217 	WriteByte ( sc->sc_control + (IE_CONTROL<<2), IE_CONT_ATTN );
218 }
219 
220 /*
221  * Set the podule page register to bring a given address into view
222  */
223 
224 static inline void
225 setpage(struct ie_softc *sc, u_long off)
226 {
227 	WriteByte ( sc->sc_control + (IE_PAGE<<2), IE_COFF2PAGE(off) );
228 }
229 
230 /*
231  * Ack the i82586
232  */
233 
234 static void
235 ie_ack(struct ie_softc *sc, u_short mask)
236 {
237 	u_short stat;
238 	int i;
239 	setpage(sc, IE_IBASE + IE_SCB_OFF );
240 
241 	stat = ReadShort ( sc->sc_ram + IE_COFF2POFF(IE_IBASE+IE_SCB_OFF) +
242 		(xoffsetof(struct ie_sys_ctl_block, ie_status)) );
243 
244 	PWriteShort ( sc->sc_ram + IE_COFF2POFF(IE_IBASE+IE_SCB_OFF) +
245 		(xoffsetof(struct ie_sys_ctl_block, ie_command)),
246 		stat & mask );
247 
248 	ieattn(sc);
249 
250 	for ( i=4000; --i>=0; ) {
251 		if ( !ReadShort(sc->sc_ram + IE_COFF2POFF(IE_IBASE+IE_SCB_OFF) +
252 		    (xoffsetof(struct ie_sys_ctl_block, ie_command))) )
253 			break;
254 		delay(100);
255 	}
256 
257 	if ( i<=0 )
258 		printf ( "ie: command timed out\n" );
259 	ie_cli(sc);
260 }
261 
262 /*
263  * This routine does the checksumming for the idrom
264  */
265 
266 #ifndef IGNORE_ETHER1_IDROM_CHECKSUM
267 static u_long
268 crc32(u_char *p, int l)
269 {
270 	u_long crc=-1;
271 	int i, b;
272 	while ( --l >= 0 ) {
273 		b = *p++;
274 		for ( i=8; --i >= 0; b>>=1 )
275 			if ((b&1)^(crc>>31))
276 				crc=(crc<<1)^0x4c11db7;
277 			else
278 				crc<<=1;
279 	}
280 	return crc;
281 }
282 #endif
283 
284 /*
285  * Probe for the ether1 card.  return 1 on success 0 on failure
286  */
287 
288 int
289 ieprobe(device_t parent, cfdata_t cf, void *aux)
290 {
291 	struct podule_attach_args *pa = aux;
292 
293 /* Look for a network slot interface */
294 
295 	return (pa->pa_product == PODULE_ETHER1);
296 }
297 
298 /*
299  * Attach our driver to the interfaces it uses
300  */
301 
302 void
303 ieattach(device_t parent, device_t self, void *aux)
304 {
305 	struct ie_softc *sc = device_private(self);
306 	struct podule_attach_args *pa = aux;
307 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
308 	int i;
309 	char idrom[32];
310 	uint8_t hwaddr[ETHER_ADDR_LEN];
311 
312 	/* Check a few things about the attach args */
313 
314 	if (pa->pa_podule_number == -1)
315 		panic("Podule has disappeared !");
316 
317 	sc->sc_dev = self;
318 	sc->sc_podule_number = pa->pa_podule_number;
319 	sc->sc_podule = pa->pa_podule;
320 	podules[sc->sc_podule_number].attached = 1;
321 
322 	/*
323 	 * MESS MESS MESS
324 	 *
325 	 * This needs a serious clean up. Alot of this code was in the probe function
326 	 * but required the softc structure. As a temporary measure until I rewrite it
327 	 * I have just bolted in the probe code here.
328 	 */
329 
330 	/* Index some podule areas */
331 	sc->sc_iobase   = sc->sc_podule->sync_base;	/* OBSOLETE */
332 	sc->sc_fastbase = sc->sc_podule->fast_base;	/* OBSOLETE */
333 	sc->sc_rom      = sc->sc_podule->sync_base;
334 	sc->sc_control  = sc->sc_podule->fast_base;
335 	sc->sc_ram      = sc->sc_podule->fast_base + IE_MEMOFF;
336 
337 	/* Set the page mask to something know and neutral */
338 	setpage(sc, IE_SCB_OFF);
339 
340 	/* Fetch the first part of the idrom */
341 	for ( i=0; i<16; i++ )
342 		idrom[i] = ReadByte ( sc->sc_rom + (i<<2) );
343 
344 	/* Verify the podulebus probe incase RiscOS lied */
345         if ( ReadByte ( sc->sc_rom + (3<<2) ) != 0x03 ) {
346 		printf(": Ether1 ROM probablly broken.  ECID corrupt\n");
347 		sc->sc_flags |= IE_BROKEN;
348 		return;
349 	}
350 
351 	/* Reset the 82586 */
352 	WriteByte ( sc->sc_fastbase + (IE_CONTROL<<2), IE_CONT_RESET );
353  	delay(1000);
354 	WriteByte ( sc->sc_fastbase + (IE_CONTROL<<2), 0 );
355 	delay(10000);
356 
357 	/* Clear pending interrupts */
358 	ie_cli (sc);
359 
360 	/* Setup SCP */
361 	{
362 		struct ie_sys_conf_ptr scp;
363 		bzero (&scp, sizeof(scp) );
364 		scp.ie_iscp_ptr = (void *)IE_ISCP_ADDR;
365 		host2ie(sc, &scp, IE_SCP_ADDR, sizeof (scp) );
366 	}
367 
368 	/* Setup ISCP */
369 	{
370 		struct ie_int_sys_conf_ptr iscp;
371 		bzero ( &iscp, sizeof(iscp) );
372 		iscp.ie_busy = 1;
373 		iscp.ie_base = (void *)IE_IBASE;
374 		iscp.ie_scb_offset = IE_SCB_OFF;
375 		host2ie(sc, &iscp, IE_ISCP_ADDR, sizeof(iscp) );
376 	}
377 
378 	/* Initialise the control block */
379 	iezero ( sc, IE_IBASE + IE_SCB_OFF, sizeof(struct ie_sys_ctl_block) );
380 	ieattn(sc);
381 
382 	/* Wait for not busy */
383 	setpage ( sc, IE_ISCP_ADDR );
384 	for ( i=10000; --i>=0; ) {
385 		if ( !ReadShort( sc->sc_ram + IE_COFF2POFF(IE_ISCP_ADDR) +
386 		    ( xoffsetof(struct ie_int_sys_conf_ptr, ie_busy)) ) )
387 			break;
388 		delay (10);
389 	}
390 
391 	/* If the busy didn't go low, the i82586 is broken or too slow */
392         if ( i<=0 ) {
393 		printf ( ": ether1 chipset didn't respond\n" );
394 		sc->sc_flags |= IE_BROKEN;
395 		return;
396 	}
397 
398 	/* Ensure that the podule sends interrupts */
399         for ( i=1000; --i>=0 ; ) {
400 		if ( ReadByte(sc->sc_rom + 0) & PODULE_IRQ_PENDING )
401 			break;
402 		delay (10);
403 	}
404 
405 	/* If we didn't see the interrupt then the IRQ line is broken */
406 	if ( i<=0 ) {
407 		printf ( ": interrupt from chipset didn't reach host\n" );
408 		sc->sc_flags |= IE_BROKEN;
409 		return;
410 	}
411 
412 	/* Ack our little test operation */
413 	ie_ack(sc,IE_ST_WHENCE);
414         ie_cli (sc);
415 
416 	/* Get second part of idrom */
417 	for ( i=16; i<32; i++ )
418 		idrom[i] = ReadByte ( sc->sc_rom + (i<<2) );
419 
420 	/* This checksum always fails.  For some reason the first 16 */
421 	/* bytes are duplicated in the second 16 bytes, the checksum */
422 	/* should be at location 28 it is clearly not		     */
423 
424 	/* It is possible that this ether1 card is buggered	     */
425 
426 #ifndef IGNORE_ETHER1_IDROM_CHECKSUM
427 	if ( crc32(idrom,28) != *(u_long *)(idrom+28) )
428 	{
429 	    printf ( "ie: ether1 idrom failed checksum %08x!=%08x\n",
430 					crc32(idrom,28), *(u_long *)(idrom+28));
431             for ( i=0; i<32; i+=8 ) {
432 	        printf ( "IDROM: %02x %02x %02x %02x %02x %02x %02x %02x\n",
433 		    idrom[0+i], idrom[1+i], idrom[2+i], idrom[3+i],
434 		    idrom[4+i], idrom[5+i], idrom[6+i], idrom[7+i] );
435 	    }
436 	    printf ( "ie: I'll ignore this fact for now!\n" );
437 	}
438 #endif
439 
440 	/* Get our ethernet address.  Do explicit copy */
441 	for ( i=0; i<ETHER_ADDR_LEN; i++ )
442 	    hwaddr[i] = idrom[9+i];
443 
444 	/* Fill in my application form to attach to the inet system */
445 
446 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
447 	ifp->if_softc = sc;
448 	ifp->if_start = iestart;
449 	ifp->if_ioctl = ieioctl;
450 	ifp->if_watchdog = iewatchdog;
451 	ifp->if_flags = IFF_BROADCAST | IFF_NOTRAILERS;
452 
453 	/* Signed, dated then sent */
454         if_attach (ifp);
455 	ether_ifattach(ifp, hwaddr);
456 
457 	/* "Hmm," said nuts, "what if the attach fails" */
458 
459 	/* Write some pretty things on the annoucement line */
460 	printf ( ": %s using %dk card ram",
461 	    ether_sprintf(hwaddr),
462 	    ((NRXBUF*IE_RXBUF_SIZE)+(NTXBUF*IE_TXBUF_SIZE))/1024 );
463 
464 	sc->sc_ih.ih_func = ieintr;
465 	sc->sc_ih.ih_arg = sc;
466 	sc->sc_ih.ih_level = IPL_NET;
467 	sc->sc_ih.ih_name = "net: ie";
468 	sc->sc_ih.ih_maskaddr = sc->sc_podule->irq_addr;
469 	sc->sc_ih.ih_maskbits = sc->sc_podule->irq_mask;
470 
471 	if (irq_claim(sc->sc_podule->interrupt, &sc->sc_ih)) {
472 		sc->sc_irqmode = 0;
473 		printf(" POLLED");
474 		panic("%s: Cannot install IRQ handler", device_xname(sc->sc_dev));
475 	} else {
476 		sc->sc_irqmode = 1;
477 		printf(" IRQ");
478 	}
479 
480 	printf("\n");
481 }
482 
483 
484 /*
485  * Oh no!! Where's my shorts!!! I'm sure I put them on this morning
486  */
487 
488 void
489 PWriteShorts(char *src, char *dest, int cnt)
490 {
491 	for (cnt /= 2; --cnt >= 0; ) {
492 		PWriteShort(dest, *(u_short *)src);
493 		src+=2;
494 		dest+=4;
495 	}
496 }
497 
498 void
499 ReadShorts(char *src, char *dest, int cnt)
500 {
501 	for (cnt /= 2; --cnt >= 0; ) {
502 		*(u_short *)dest = ReadShort(src);
503 		src+=4;
504 		dest+=2;
505 	}
506 }
507 
508 /*
509  * A bcopy or memcpy to adapter ram.  It handles the page register for you
510  * so you dont have to worry about the ram windowing
511  */
512 
513 static void
514 host2ie(struct ie_softc *sc, void *src, u_long dest, int size)
515 {
516 	int cnt;
517 	char *sptr = src;
518 
519 #ifdef DIAGNOSTIC
520 	if (size & 1)
521 		panic("host2ie");
522 #endif
523 
524 	while (size > 0) {
525 		cnt = IE_PAGESIZE - dest % IE_PAGESIZE;
526 		if (cnt > size)
527 			cnt = size;
528 		setpage(sc, dest);
529 		PWriteShorts(sptr, (char *)sc->sc_ram + IE_COFF2POFF(dest), cnt);
530 		sptr+=cnt;
531 		dest+=cnt;
532 		size-=cnt;
533 	}
534 }
535 
536 static void
537 ie2host(struct ie_softc *sc, u_long src, void *dest, int size)
538 {
539 	int cnt;
540 	char *dptr = dest;
541 
542 #ifdef DIAGNOSTIC
543 	if (size & 1)
544 		panic ( "ie2host" );
545 #endif
546 
547 	while (size > 0) {
548 		cnt = IE_PAGESIZE - src % IE_PAGESIZE;
549 		if (cnt > size)
550 			cnt = size;
551 		setpage(sc, src);
552 		ReadShorts((char *)sc->sc_ram + IE_COFF2POFF(src), dptr, cnt);
553 		src+=cnt;
554 		dptr+=cnt;
555 		size-=cnt;
556 	}
557 }
558 
559 /*
560  * Like a bzero or memset 0 for adapter memory.  It handles the page
561  * register so you dont have to worry about it
562  */
563 
564 static void
565 iezero(struct ie_softc *sc, u_long p, int size)
566 {
567 	int cnt;
568 
569 	while (size > 0) {
570 		cnt = IE_PAGESIZE - p % IE_PAGESIZE;
571 		if (cnt > size)
572 			cnt=size;
573 		setpage(sc, p);
574 		memset((char *)sc->sc_ram + IE_COFF2POFF(p), 0, 2*cnt);
575 		p += cnt;
576 		size -= cnt;
577 	}
578 }
579 
580 /*
581  * I/O Control interface to the kernel, entry point here
582  */
583 
584 int
585 ieioctl(struct ifnet *ifp, unsigned long cmd, void *data)
586 {
587     struct ie_softc *sc = ifp->if_softc;
588     struct ifaddr *ifa = (struct ifaddr *)data;
589     int s;
590     int error=0;
591 
592     s=splnet();
593 
594     switch ( cmd )
595     {
596 	case SIOCINITIFADDR:
597 	    ifp->if_flags |= IFF_UP;
598 	    switch (ifa->ifa_addr->sa_family ) {
599 #ifdef INET
600 		case AF_INET:
601 		    ieinit(sc);
602 		    arp_ifinit(ifp, ifa );
603 		    break;
604 #endif
605 		default:
606 		    ieinit(sc);
607 		    break;
608 	    }
609 	    break;
610 
611 #define IZSET(a,b) ((a->if_flags&b)!=0)
612 #define IZCLR(a,b) ((a->if_flags&b)==0)
613 #define DOSET(a,b) (a->if_flags|=b)
614 #define DOCLR(a,b) (a->if_flags&=~b)
615 
616 	case SIOCSIFFLAGS:
617 	    if ((error = ifioctl_common(ifp, cmd, data)) != 0)
618 		return error;
619 	    sc->promisc = ifp->if_flags & ( IFF_PROMISC | IFF_ALLMULTI );
620 
621 	    if ( IZCLR(ifp,IFF_UP) && IZSET(ifp,IFF_RUNNING) )
622 	    {
623 		/* Interface was marked down and its running so stop it */
624 		iestop(sc);
625 		DOCLR(ifp,IFF_RUNNING);
626 	    }
627 	    else if ( IZSET(ifp,IFF_UP) && IZCLR(ifp,IFF_RUNNING) )
628 	    {
629 		/* Just marked up and we're not running so start it */
630 		ieinit(sc);
631 	    }
632 	    else
633 	    {
634 		/* else reset to invoke changes in other registers */
635 		iestop(sc);
636 		ieinit(sc);
637             }
638 
639 	default:
640 	    error = ether_ioctl(ifp, cmd, data);
641 	    break;
642     }
643     (void)splx(s);
644     return error;
645 }
646 
647 /*
648  * Reset the card.  Completely.
649  */
650 
651 void
652 iereset(struct ie_softc *sc)
653 {
654 	struct ie_sys_ctl_block scb;
655 	int s = splnet();
656 
657 	iestop(sc);
658 
659 	ie2host(sc, IE_IBASE + IE_SCB_OFF, &scb, sizeof scb);
660 
661 	if (command_and_wait(sc, IE_RU_ABORT|IE_CU_ABORT, 0, 0, 0, 0, 0))
662 	        printf("ie0: abort commands timed out\n");
663 
664 	if (command_and_wait(sc, IE_RU_DISABLE|IE_CU_STOP, 0, 0, 0, 0, 0))
665 	        printf("ie0: abort commands timed out\n");
666 
667 	ieinit(sc);
668 
669 	(void)splx(s);
670 }
671 
672 /*
673  * Watchdog entry point.  This is the entry for the kernel to call us
674  */
675 
676 void
677 iewatchdog(struct ifnet *ifp)
678 {
679 	struct ie_softc *sc = ifp->if_softc;
680 
681 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
682 	++ifp->if_oerrors;
683 	iereset(sc);
684 }
685 
686 /*
687  * Start the time-domain-refloctometer running
688  */
689 
690 static void
691 run_tdr(struct ie_softc *sc)
692 {
693     struct ie_sys_ctl_block scb;
694     u_long ptr = IE_IBASE + IE_SCB_OFF + sizeof scb;
695     struct ie_tdr_cmd cmd;
696     int result;
697 
698     bzero ( &scb, sizeof(scb) );
699     bzero ( &cmd, sizeof(cmd) );
700 
701     cmd.com.ie_cmd_status = 0;
702     cmd.com.ie_cmd_cmd = IE_CMD_TDR | IE_CMD_LAST;
703     cmd.com.ie_cmd_link = 0xffff;
704     cmd.ie_tdr_time = 0;
705 
706     scb.ie_command_list = (u_short)ptr;
707 
708     result=0;
709     if ( command_and_wait(sc, IE_CU_START, &scb, &cmd, ptr, sizeof cmd,
710 	IE_STAT_COMPL) )
711     {
712 	    result = 0x10000;
713     }
714     else if ( !(cmd.com.ie_cmd_status & IE_STAT_OK) )
715     {
716 	result = 0x10000;
717     }
718 
719     if ( result==0 )
720 	result = cmd.ie_tdr_time;
721 
722     ie_ack ( sc, IE_ST_WHENCE );
723 
724     if (result & IE_TDR_SUCCESS )
725 	return;
726 
727     /* Very messy.  I'll tidy it later */
728 
729     if ( result & 0x10000 )
730     {
731 	printf ( "ie: TDR command failed\n" );
732     }
733     else if ( result & IE_TDR_XCVR )
734     {
735 	printf ( "ie: tranceiver problem. Is it plugged in?\n" );
736     }
737     else if ( result & IE_TDR_OPEN )
738     {
739 	if ((result & IE_TDR_TIME)>0)
740 	    printf ( "ie: TDR detected an open %d clocks away.\n",
741 			result & IE_TDR_TIME );
742     }
743     else if ( result & IE_TDR_SHORT )
744     {
745 	if ((result & IE_TDR_TIME)>0)
746 	    printf ( "ie: TDR detected a short %d clock away.\n",
747 			result & IE_TDR_TIME );
748     }
749     else
750     {
751 	printf ( "ie: TDR returned unknown status %x\n", result );
752     }
753 }
754 
755 u_long
756 setup_rfa(struct ie_softc *sc, u_long ptr)
757 {
758     int i;
759     {
760 	/* Receive frame descriptors */
761         struct ie_recv_frame_desc rfd;
762 	memset( &rfd, 0, sizeof rfd );
763 	for ( i=0; i<NFRAMES; i++ )
764 	{
765 	    sc->rframes[i] = ptr;
766 	    rfd.ie_fd_next = ptr + sizeof rfd;
767 	    host2ie(sc, (char *)&rfd, ptr, sizeof rfd);
768 	    ptr += sizeof rfd;
769 	}
770 	rfd.ie_fd_next = sc->rframes[0];
771 	rfd.ie_fd_last |= IE_FD_LAST;
772 	host2ie(sc, (char *)&rfd, sc->rframes[NFRAMES-1], sizeof rfd );
773 
774 	ie2host(sc, sc->rframes[0], (char *)&rfd, sizeof rfd );
775 	rfd.ie_fd_buf_desc = (u_short) ptr;
776 	host2ie(sc, (char *)&rfd, sc->rframes[0], sizeof rfd );
777     }
778 
779     {
780 	/* Receive frame descriptors */
781 	struct ie_recv_buf_desc rbd;
782 	memset(&rbd, 0, sizeof rbd);
783 	for ( i=0; i<NRXBUF; i++ )
784 	{
785 	    sc->rbuffs[i] = ptr;
786 	    rbd.ie_rbd_length = IE_RXBUF_SIZE;
787 	    rbd.ie_rbd_buffer = (void *)(ptr + sizeof rbd);
788 	    rbd.ie_rbd_next = (u_short)(ptr + sizeof rbd + IE_RXBUF_SIZE);
789 	    host2ie(sc, &rbd, ptr, sizeof rbd);
790 	    ptr+=sizeof rbd;
791 
792 	    sc->cbuffs[i] = ptr;
793 	    ptr+=IE_RXBUF_SIZE;
794 	}
795 	rbd.ie_rbd_next = sc->rbuffs[0];
796 	rbd.ie_rbd_length |= IE_RBD_LAST;
797 	host2ie(sc, &rbd, sc->rbuffs[NRXBUF-1], sizeof rbd);
798     }
799 
800     sc->rfhead = 0;
801     sc->rftail = NFRAMES-1;
802     sc->rbhead = 0;
803     sc->rbtail = NRXBUF-1;
804 
805     {
806 	struct ie_sys_ctl_block scb;
807 	bzero ( &scb, sizeof scb );
808 	scb.ie_recv_list = (u_short)sc->rframes[0];
809 	host2ie(sc, (char *)&scb, (IE_IBASE + IE_SCB_OFF), sizeof scb );
810     }
811     return ptr;
812 }
813 
814 static void
815 start_receiver(struct ie_softc *sc)
816 {
817     struct ie_sys_ctl_block scb;
818     ie2host ( sc, IE_IBASE + IE_SCB_OFF, &scb, sizeof scb );
819     scb.ie_recv_list = (u_short)sc->rframes[0];
820     command_and_wait(sc, IE_RU_START, &scb, 0, 0, 0, 0);
821     ie_ack(sc, IE_ST_WHENCE );
822 }
823 
824 /*
825  * Take our configuration and update all the other data structures that
826  * require information from the driver.
827  *
828  * CALL AT SPLIMP OR HIGHER
829  */
830 
831 int
832 ieinit(struct ie_softc *sc)
833 {
834     struct ifnet *ifp;
835     struct ie_sys_ctl_block scb;
836     struct ie_config_cmd cmd;
837     struct ie_iasetup_cmd iasetup_cmd;
838     u_long ptr = IE_IBASE + IE_SCB_OFF + sizeof scb;
839     int n;
840 
841     ifp = &sc->sc_ethercom.ec_if;
842 
843     bzero ( &scb, sizeof(scb) );
844 
845     /* Send the configure command */
846 
847     cmd.com.ie_cmd_status = 0;
848     cmd.com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
849     cmd.com.ie_cmd_link = 0xffff;
850 
851     cmd.ie_config_count = 0x0c;
852     cmd.ie_fifo = 8;
853     cmd.ie_save_bad = 0x40;
854     cmd.ie_addr_len = 0x2e;
855     cmd.ie_priority = 0;
856     cmd.ie_ifs = 0x60;
857     cmd.ie_slot_low = 0;
858     cmd.ie_slot_high = 0xf2;
859     cmd.ie_promisc = 0;		/* Hey nuts, look at this! */
860     cmd.ie_crs_cdt = 0;
861     cmd.ie_min_len = 64;
862     cmd.ie_junk = 0xff;
863 
864     scb.ie_command_list = (u_short)ptr;
865 
866     if ( command_and_wait(sc, IE_CU_START, &scb, &cmd, ptr, sizeof cmd,
867 	IE_STAT_COMPL) )
868     {
869 	printf ( "%s: command failed: timeout\n", device_xname(sc->sc_dev));
870 	return 0;
871     }
872 
873     if ( !(cmd.com.ie_cmd_status & IE_STAT_OK) )
874     {
875 	printf ( "%s: command failed: !IE_STAT_OK\n", device_xname(sc->sc_dev));
876 	return 0;
877     }
878 
879     /* Individual address setup command */
880 
881     iasetup_cmd.com.ie_cmd_status = 0;
882     iasetup_cmd.com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
883     iasetup_cmd.com.ie_cmd_link = 0xffff;
884 
885     bcopy ( CLLADDR(ifp->if_sadl), (void *) &iasetup_cmd.ie_address,
886 	 	sizeof (iasetup_cmd.ie_address) );
887 
888     if ( command_and_wait(sc, IE_CU_START, &scb, &iasetup_cmd, ptr, sizeof cmd,
889 	IE_STAT_COMPL) )
890     {
891 	printf ( "%s: iasetup failed : timeout\n", device_xname(sc->sc_dev));
892 	return 0;
893     }
894 
895     if ( !(cmd.com.ie_cmd_status & IE_STAT_OK) )
896     {
897 	printf ( "%s: iasetup failed : !IE_STAT_OK\n", device_xname(sc->sc_dev));
898 	return 0;
899     }
900 
901     ie_ack ( sc, IE_ST_WHENCE );
902 
903     /* Run the time-domain refloctometer */
904     run_tdr ( sc );
905 
906     ie_ack ( sc, IE_ST_WHENCE );
907 
908     /* meminit */
909     ptr = setup_rfa(sc, ptr);
910 
911     ifp->if_flags |= IFF_RUNNING;
912     ifp->if_flags &= ~IFF_OACTIVE;
913 
914     /* Setup transmit buffers */
915 
916     for ( n=0; n<NTXBUF; n++ ) {
917 	sc->xmit_cmds[n] = ptr;
918 	iezero(sc, ptr, sizeof(struct ie_xmit_cmd) );
919 	ptr += sizeof(struct ie_xmit_cmd);
920 
921 	sc->xmit_buffs[n] = ptr;
922 	iezero(sc, ptr, sizeof(struct ie_xmit_buf));
923 	ptr += sizeof(struct ie_xmit_buf);
924     }
925 
926     for ( n=0; n<NTXBUF; n++ ) {
927 	sc->xmit_cbuffs[n] = ptr;
928 	ptr += IE_TXBUF_SIZE;
929     }
930 
931     sc->xmit_free = NTXBUF;
932     sc->xchead = sc->xctail = 0;
933 
934     {
935 	struct ie_xmit_cmd xmcmd;
936 	bzero ( &xmcmd, sizeof xmcmd );
937 	xmcmd.ie_xmit_status = IE_STAT_COMPL;
938 	host2ie(sc, &xmcmd, sc->xmit_cmds[0], sizeof xmcmd);
939     }
940 
941     start_receiver (sc);
942 
943     return 0;
944 }
945 
946 int
947 iestop(struct ie_softc *sc)
948 {
949     struct ie_sys_ctl_block scb;
950     int s = splnet();
951 
952     ie2host ( sc, IE_IBASE + IE_SCB_OFF, &scb, sizeof scb );
953 
954     if ( command_and_wait(sc, IE_RU_DISABLE, &scb, 0, 0, 0, 0) )
955         printf ( "ie0: abort commands timed out\n" );
956 
957     (void)splx(s);
958     return(0);
959 }
960 
961 /*
962  * Send a command to the card and await its completion.
963  * Timeout if it's taking too long
964  */
965 
966 /*CAW*/
967 
968 static int
969 command_and_wait(struct ie_softc *sc, u_short cmd, struct ie_sys_ctl_block *pscb, void *pcmd, int ocmd, int scmd, int mask)
970 {
971     int i=0;
972 
973     /* Copy the command to the card */
974 
975     if ( pcmd )
976 	host2ie(sc, pcmd, ocmd, scmd); /* transfer the command to the card */
977 
978     /* Copy the scb to the card */
979 
980     if ( pscb ) {
981 	pscb->ie_command = cmd;
982 	host2ie(sc, pscb, IE_IBASE + IE_SCB_OFF, sizeof *pscb);
983     }
984     else
985     {
986 	setpage ( sc, IE_IBASE + IE_SCB_OFF );
987 	PWriteShort ( sc->sc_ram + IE_COFF2POFF(IE_IBASE+IE_SCB_OFF) +
988 		(xoffsetof(struct ie_sys_ctl_block, ie_command)), cmd );
989     }
990 
991     /* Prod the card to act on the newly loaded command */
992     ieattn(sc);
993 
994     /* Wait for the command to complete */
995     if ( IE_ACTION_COMMAND(cmd) && pcmd )
996     {
997 	setpage(sc,ocmd);
998 	for ( i=4000; --i>=0; ) {
999 	    if ( ReadShort(sc->sc_ram + IE_COFF2POFF(ocmd) +
1000 		(xoffsetof(struct ie_config_cmd, ie_config_status))) & mask)
1001 		break;
1002 	    delay(100);
1003 	}
1004     }
1005     else
1006     {
1007 	for ( i=4000; --i>=0; ) {
1008 	    if ( !ReadShort(sc->sc_ram + IE_COFF2POFF(IE_IBASE+IE_SCB_OFF) +
1009 		(xoffsetof(struct ie_sys_ctl_block, ie_command))) )
1010 		break;
1011 	    delay(100);
1012 	}
1013     }
1014 
1015     /* Update the host structures to reflect the state on the card */
1016     if ( pscb )
1017 	ie2host(sc, IE_IBASE + IE_SCB_OFF, pscb, sizeof *pscb );
1018     if ( pcmd )
1019 	ie2host(sc, ocmd, pcmd, scmd);
1020 
1021     return i < 0;
1022 }
1023 
1024 #define READ_MEMBER(sc,type,member,ptr,dest)			\
1025 	setpage(sc, ptr);					\
1026 	dest = ReadShort(sc->sc_ram + IE_COFF2POFF(ptr) +	\
1027 	       (xoffsetof(type, member)) );
1028 
1029 #define WRITE_MEMBER(sc,type,member,ptr,dest)			\
1030 	setpage(sc, ptr);					\
1031 	PWriteShort(sc->sc_ram + IE_COFF2POFF(ptr) +	\
1032 	       (xoffsetof(type, member)), dest );
1033 
1034 static inline int
1035 ie_buflen(struct ie_softc *sc, int head)
1036 {
1037 	int actual;
1038 
1039 	READ_MEMBER(sc,struct ie_recv_buf_desc, ie_rbd_actual,
1040 	    sc->rbuffs[head], actual );
1041 
1042 	return(actual & (IE_RXBUF_SIZE | (IE_RXBUF_SIZE-1))) ;
1043 }
1044 
1045 static inline int
1046 ie_packet_len(struct ie_softc *sc)
1047 {
1048     int i;
1049     int actual;
1050     int head = sc->rbhead;
1051     int acc=0;
1052 
1053     do {
1054 	READ_MEMBER(sc,struct ie_recv_buf_desc, ie_rbd_actual,
1055 			sc->rbuffs[sc->rbhead], actual );
1056 	if (!(actual&IE_RBD_USED))
1057 	{
1058 	    return (-1);
1059 	}
1060 
1061 	READ_MEMBER(sc,struct ie_recv_buf_desc, ie_rbd_actual,
1062 			sc->rbuffs[head], i );
1063         i = i & IE_RBD_LAST;
1064 
1065 	acc += ie_buflen(sc, head);
1066 	head = (head+1) % NRXBUF;
1067     } while (!i);
1068 
1069     return acc;
1070 }
1071 
1072 struct mbuf *
1073 ieget(struct ie_softc *sc, int *to_bpf )
1074 {
1075     struct mbuf *top, **mp, *m;
1076     int head;
1077     int resid, totlen, thisrboff, thismboff;
1078     int len;
1079     struct ether_header eh;
1080 
1081     totlen = ie_packet_len(sc);
1082 
1083     if ( totlen > ETHER_MAX_LEN )
1084     {
1085 	printf ( "ie: Gosh that packet was s-o-o-o big.\n" );
1086 	return 0;
1087     }
1088 
1089     if ( totlen<=0 )
1090 	return 0;
1091 
1092     head = sc->rbhead;
1093 
1094     /* Read the ethernet header */
1095     ie2host ( sc, sc->cbuffs[head], (void *)&eh, sizeof eh );
1096 
1097     /* Check if the packet is for us */
1098 
1099     resid = totlen;
1100 
1101     MGETHDR ( m, M_DONTWAIT, MT_DATA );
1102     if ( m==0 )
1103 	return 0;
1104 
1105     m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
1106     m->m_pkthdr.len = totlen;
1107     len = MHLEN;
1108     top = 0;
1109     mp = &top;
1110 
1111     /*
1112      * This loop goes through and allocates mbufs for all the data we will
1113      * be copying in.  It does not actually do the copying yet.
1114      */
1115     while (totlen > 0) {
1116 	if (top) {
1117 	    MGET(m, M_DONTWAIT, MT_DATA);
1118 	    if (m == 0) {
1119 		m_freem(top);
1120 		return 0;
1121 	    }
1122 	    len = MLEN;
1123 	}
1124 	if (totlen >= MINCLSIZE) {
1125 	    MCLGET(m, M_DONTWAIT);
1126 	    if (m->m_flags & M_EXT)
1127 		len = MCLBYTES;
1128 	}
1129 
1130 	if (mp == &top) {
1131 		void *newdata = (void *)
1132 		    ALIGN(m->m_data + sizeof(struct ether_header)) -
1133 		    sizeof(struct ether_header);
1134 		len -= newdata - m->m_data;
1135 		m->m_data = newdata;
1136 	}
1137 
1138         m->m_len = len = min(totlen, len);
1139 
1140         totlen -= len;
1141         *mp = m;
1142         mp = &m->m_next;
1143     }
1144 
1145     m = top;
1146     thismboff = 0;
1147 
1148     /*
1149      * Copy the Ethernet header into the mbuf chain.
1150      */
1151     memcpy(mtod(m, void *), &eh, sizeof(struct ether_header));
1152     thismboff = sizeof(struct ether_header);
1153     thisrboff = sizeof(struct ether_header);
1154     resid -= sizeof(struct ether_header);
1155 
1156     /*
1157      * Now we take the mbuf chain (hopefully only one mbuf most of the
1158      * time) and stuff the data into it.  There are no possible failures at
1159      * or after this point.
1160      */
1161     while (resid > 0) {
1162 	int thisrblen = ie_buflen(sc, head) - thisrboff,
1163 	    thismblen = m->m_len - thismboff;
1164 	len = min(thisrblen, thismblen);
1165 
1166 /*      bcopy((void *)(sc->cbuffs[head] + thisrboff),
1167 	    mtod(m, void *) + thismboff, (u_int)len);	 */
1168 
1169 
1170 	if ( len&1 )
1171 	{
1172  	    ie2host(sc, sc->cbuffs[head]+thisrboff,
1173 		mtod(m, void *) + thismboff, (u_int)len+1);
1174   	}
1175 	else
1176 	{
1177  	    ie2host(sc, sc->cbuffs[head]+thisrboff,
1178 		mtod(m, void *) + thismboff, (u_int)len);
1179 	}
1180 
1181 	resid -= len;
1182 
1183 	if (len == thismblen) {
1184 		m = m->m_next;
1185 		thismboff = 0;
1186 	} else
1187 		thismboff += len;
1188 
1189 	if (len == thisrblen) {
1190 		head = (head + 1) % NRXBUF;
1191 		thisrboff = 0;
1192 	} else
1193 		thisrboff += len;
1194     }
1195 
1196 
1197     return top;
1198 }
1199 
1200 void
1201 ie_drop_packet_buffer(struct ie_softc *sc)
1202 {
1203     int i, actual, last;
1204 
1205     do {
1206 	READ_MEMBER(sc,struct ie_recv_buf_desc, ie_rbd_actual,
1207 			sc->rbuffs[sc->rbhead], actual );
1208 	if (!(actual&IE_RBD_USED))
1209 	{
1210 	    iereset(sc);
1211 	    return;
1212 	}
1213 
1214 	i = actual & IE_RBD_LAST;
1215 
1216         READ_MEMBER(sc,struct ie_recv_buf_desc,ie_rbd_length,
1217 			sc->rbuffs[sc->rbhead], last );
1218         last |= IE_RBD_LAST;
1219         WRITE_MEMBER(sc,struct ie_recv_buf_desc,ie_rbd_length,
1220 			sc->rbuffs[sc->rbhead], last );
1221 
1222         WRITE_MEMBER(sc,struct ie_recv_buf_desc,ie_rbd_actual,
1223 			sc->rbuffs[sc->rbhead], 0 );
1224 
1225 	sc->rbhead = ( sc->rbhead + 1 ) % NRXBUF;
1226 
1227         READ_MEMBER(sc,struct ie_recv_buf_desc,ie_rbd_length,
1228 			sc->rbuffs[sc->rbtail], last );
1229         last &= ~IE_RBD_LAST;
1230         WRITE_MEMBER(sc,struct ie_recv_buf_desc,ie_rbd_length,
1231 			sc->rbuffs[sc->rbtail], last );
1232 
1233 	sc->rbtail = ( sc->rbtail + 1 ) % NRXBUF;
1234     } while (!i);
1235 }
1236 
1237 void
1238 ie_read_frame(struct ie_softc *sc, int num)
1239 {
1240     int status;
1241     struct ie_recv_frame_desc rfd;
1242     struct mbuf *m=0;
1243     struct ifnet *ifp;
1244     int last;
1245 
1246     ifp = &sc->sc_ethercom.ec_if;
1247 
1248     ie2host(sc, sc->rframes[num], &rfd, sizeof rfd );
1249     status = rfd.ie_fd_status;
1250 
1251     /* Advance the RFD list, since we're done with this descriptor */
1252 
1253     WRITE_MEMBER(sc,struct ie_recv_frame_desc,ie_fd_status,
1254 			sc->rframes[num], 0 );
1255 
1256     READ_MEMBER(sc,struct ie_recv_frame_desc,ie_fd_last,
1257 			sc->rframes[num], last );
1258     last |= IE_FD_LAST;
1259     WRITE_MEMBER(sc,struct ie_recv_frame_desc,ie_fd_last,
1260 			sc->rframes[num], last );
1261 
1262     READ_MEMBER(sc,struct ie_recv_frame_desc,ie_fd_last,
1263 			sc->rframes[sc->rftail], last );
1264     last &= ~IE_FD_LAST;
1265     WRITE_MEMBER(sc,struct ie_recv_frame_desc,ie_fd_last,
1266 			sc->rframes[sc->rftail], last );
1267 
1268     sc->rftail = ( sc->rftail + 1 ) % NFRAMES;
1269     sc->rfhead = ( sc->rfhead + 1 ) % NFRAMES;
1270 
1271     if ( status & IE_FD_OK ) {
1272 	m = ieget(sc, 0);
1273 	ie_drop_packet_buffer(sc);
1274     }
1275 
1276     if ( m==0 ) {
1277 	ifp->if_ierrors++;
1278 	return;
1279     }
1280 
1281     ifp->if_ipackets++;
1282 
1283     bpf_mtap(ifp, m);
1284 
1285     (*ifp->if_input)(ifp, m);
1286 }
1287 
1288 void
1289 ierint(struct ie_softc *sc)
1290 {
1291     int i;
1292     int times_thru = 1024;
1293     struct ie_sys_ctl_block scb;
1294     int status;
1295     int safety_catch = 0;
1296 
1297     i = sc->rfhead;
1298     for (;;) {
1299 
1300 	if ( (safety_catch++)>100 )
1301 	{
1302 	    printf ( "ie: ierint safety catch tripped\n" );
1303 	    iereset(sc);
1304 	    return;
1305 	}
1306 
1307 	READ_MEMBER(sc,struct ie_recv_frame_desc,ie_fd_status,
1308 				sc->rframes[i],status);
1309 
1310 	if ((status&IE_FD_COMPLETE)&&(status&IE_FD_OK)) {
1311 	    if ( !--times_thru ) {
1312 		printf ( "IERINT: Uh oh. Nuts, look at this bit!!!\n" );
1313     		ie2host ( sc, IE_IBASE + IE_SCB_OFF, &scb, sizeof scb );
1314 		sc->sc_ethercom.ec_if.if_ierrors += scb.ie_err_crc +
1315 						  scb.ie_err_align +
1316 						  scb.ie_err_resource +
1317 						  scb.ie_err_overrun;
1318 		scb.ie_err_crc      = scb.ie_err_align   = 0;
1319 		scb.ie_err_resource = scb.ie_err_overrun = 0;
1320 	        host2ie(sc, &scb, IE_SCP_ADDR, sizeof (scb) );
1321 	    }
1322 	    ie_read_frame(sc, i);
1323 	} else {
1324     	    ie2host ( sc, IE_IBASE + IE_SCB_OFF, &scb, sizeof scb );
1325 
1326 	    if ( ((status&IE_FD_RNR)!=0) && ((scb.ie_status&IE_RU_READY)==0) )
1327 	    {
1328 		WRITE_MEMBER(sc,struct ie_recv_frame_desc, ie_fd_buf_desc,
1329 				sc->rframes[0], sc->rbuffs[0] );
1330 
1331 		scb.ie_recv_list = sc->rframes[0];
1332 	        host2ie(sc, (char *)&scb, IE_IBASE + IE_SCB_OFF, sizeof (scb) );
1333     		command_and_wait(sc, IE_RU_START, &scb, 0, 0, 0, 0);
1334 	    }
1335 	    break;
1336 	}
1337 	i = (i + 1) % NFRAMES;
1338     }
1339 }
1340 
1341 static int in_intr = 0;
1342 
1343 int
1344 ieintr(void *arg)
1345 {
1346     struct ie_softc *sc = arg;
1347     u_short status;
1348     int safety_catch = 0;
1349     static int safety_net = 0;
1350 
1351     if (in_intr == 1)
1352 	panic ( "ie: INTERRUPT REENTERED\n" );
1353 
1354     /* Clear the interrrupt */
1355     ie_cli (sc);
1356 
1357     setpage(sc, IE_IBASE + IE_SCB_OFF );
1358     status = ReadShort ( sc->sc_ram + IE_COFF2POFF(IE_IBASE+IE_SCB_OFF) +
1359 		(xoffsetof(struct ie_sys_ctl_block, ie_status)) );
1360 
1361     status = status & IE_ST_WHENCE;
1362 
1363     if (status == 0) {
1364 	in_intr = 0;
1365 	return(0);
1366     }
1367 
1368 loop:
1369 
1370     ie_ack(sc, status);
1371 
1372     if (status & (IE_ST_FR | IE_ST_RNR))
1373 	ierint(sc);
1374 
1375     if (status & IE_ST_CX)
1376 	ietint(sc);
1377 
1378     if (status & IE_ST_RNR) {
1379 	printf ( "ie: receiver not ready\n" );
1380 	sc->sc_ethercom.ec_if.if_ierrors++;
1381 	iereset(sc);
1382     }
1383 
1384     setpage(sc, IE_IBASE + IE_SCB_OFF );
1385     status = ReadShort ( sc->sc_ram + IE_COFF2POFF(IE_IBASE+IE_SCB_OFF) +
1386 		(xoffsetof(struct ie_sys_ctl_block, ie_status)) );
1387     status = status & IE_ST_WHENCE;
1388 
1389     ie_cli(sc);
1390 
1391     if (status == 0) {
1392 	in_intr = 0;
1393 	return(0);
1394     }
1395 
1396     /* This is prehaps a little over cautious */
1397     if ( safety_catch++ > 10 )
1398     {
1399 	printf ( "ie: Interrupt couldn't be cleared\n" );
1400 	delay ( 1000 );
1401 	ie_cli(sc);
1402 	if ( safety_net++ > 50 )
1403 	{
1404 /*	    printf ( "ie: safety net catches driver, shutting down\n" );
1405 	    disable_irq ( IRQ_PODULE );*/
1406 	}
1407 	in_intr = 0;
1408 	return(0);
1409     }
1410 
1411     goto loop;
1412 }
1413 
1414 void
1415 iexmit(struct ie_softc *sc)
1416 {
1417 /*    int actual;*/
1418     struct ie_sys_ctl_block scb;
1419 
1420     struct ie_xmit_cmd xc;
1421     struct ie_xmit_buf xb;
1422 
1423     ie2host(sc, sc->xmit_buffs[sc->xctail], (char *)&xb, sizeof xb );
1424     xb.ie_xmit_flags |= IE_XMIT_LAST;
1425     xb.ie_xmit_next = 0xffff;
1426     xb.ie_xmit_buf = (void *)sc->xmit_cbuffs[sc->xctail];
1427     host2ie(sc, &xb, sc->xmit_buffs[sc->xctail], sizeof xb );
1428 
1429     bzero ( &xc, sizeof xc );
1430     xc.com.ie_cmd_link = 0xffff;
1431     xc.com.ie_cmd_cmd = IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST;
1432     xc.ie_xmit_status = 0x0000;
1433     xc.ie_xmit_desc = sc->xmit_buffs[sc->xctail];
1434     host2ie(sc, (char *)&xc, sc->xmit_cmds[sc->xctail], sizeof xc );
1435 
1436     ie2host ( sc, IE_IBASE + IE_SCB_OFF, &scb, sizeof scb );
1437     scb.ie_command_list = sc->xmit_cmds[sc->xctail];
1438     host2ie(sc, (char *)&scb, (IE_IBASE + IE_SCB_OFF), sizeof scb );
1439 
1440     command_and_wait(sc, IE_CU_START, &scb, &xc, sc->xmit_cmds[sc->xctail]
1441 			, sizeof xc, IE_STAT_COMPL);
1442 
1443     sc->sc_ethercom.ec_if.if_timer = 5;
1444 }
1445 /*
1446  * Start sending all the queued buffers.
1447  */
1448 
1449 void
1450 iestart(struct ifnet *ifp)
1451 {
1452 	struct ie_softc *sc = ifp->if_softc;
1453 	struct mbuf *m0, *m;
1454 	u_char *buffer;
1455 	u_short len;
1456 	char txbuf[IE_TXBUF_SIZE];
1457 	int safety_catch = 0;
1458 
1459 	if ((ifp->if_flags & IFF_OACTIVE) != 0)
1460 		return;
1461 
1462 	for (;;) {
1463 		if ( (safety_catch++)>100 )
1464 		{
1465 		    printf ( "ie: iestart safety catch tripped\n" );
1466 		    iereset(sc);
1467 		    return;
1468 		}
1469 		if (sc->xmit_free == 0) {
1470 			ifp->if_flags |= IFF_OACTIVE;
1471 			break;
1472 		}
1473 
1474 		IF_DEQUEUE(&ifp->if_snd, m);
1475 		if (!m)
1476 			break;
1477 
1478 		/* TODO: Write directly to the card */
1479 		len = 0;
1480 		/* buffer = sc->xmit_cbuffs[sc->xchead]; */
1481 		buffer = txbuf;
1482 
1483 		for (m0 = m; m && (len + m->m_len) < IE_TXBUF_SIZE;
1484 		     m = m->m_next) {
1485 			memcpy(buffer, mtod(m, void *), m->m_len);
1486 			buffer += m->m_len;
1487 			len += m->m_len;
1488 		}
1489 
1490 		bpf_mtap(ifp, m0);
1491 
1492 		m_freem(m0);
1493 		if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1494 			memset(buffer, 0, ETHER_MIN_LEN - ETHER_CRC_LEN - len);
1495 			len = ETHER_MIN_LEN - ETHER_CRC_LEN;
1496 			buffer += ETHER_MIN_LEN - ETHER_CRC_LEN;
1497 		}
1498 
1499 		/* When we write directly to the card we dont need this */
1500     		if (len&1)
1501    		    host2ie(sc, txbuf, sc->xmit_cbuffs[sc->xchead], len+1 );
1502 		else
1503    		    host2ie(sc, txbuf, sc->xmit_cbuffs[sc->xchead], len );
1504 
1505 		/* sc->xmit_buffs[sc->xchead]->ie_xmit_flags = len; */
1506 
1507 		WRITE_MEMBER(sc,struct ie_xmit_buf, ie_xmit_flags,
1508 				sc->xmit_buffs[sc->xchead], len)
1509 
1510 		/* Start the first packet transmitting. */
1511 		if (sc->xmit_free == NTXBUF)
1512 			iexmit(sc);
1513 
1514 		sc->xchead = (sc->xchead + 1) % NTXBUF;
1515 		sc->xmit_free--;
1516 	}
1517 }
1518 
1519 void
1520 ietint(struct ie_softc *sc)
1521 {
1522     struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1523 
1524     int status;
1525 
1526     ifp->if_timer=0;
1527     ifp->if_flags &= ~IFF_OACTIVE;
1528 
1529     READ_MEMBER(sc,struct ie_xmit_cmd, ie_xmit_status,
1530 	sc->xmit_cmds[sc->xctail], status );
1531 
1532     if (!(status&IE_STAT_COMPL) || (status & IE_STAT_BUSY) )
1533 	printf ( "ietint: command still busy!\n" );
1534 
1535     if ( status & IE_STAT_OK ) {
1536 	ifp->if_opackets++;
1537 	ifp->if_collisions += status & IE_XS_MAXCOLL;
1538     } else {
1539 	ifp->if_oerrors++;
1540 	if ( status & IE_STAT_ABORT )
1541 	    printf ( "ie: send aborted\n" );
1542 	if ( status & IE_XS_LATECOLL )
1543 	    printf ( "ie: late collision\n" );
1544 	if ( status & IE_XS_NOCARRIER )
1545 	    printf ( "ie: no carrier\n" );
1546 	if ( status & IE_XS_LOSTCTS )
1547 	    printf ( "ie: lost CTS\n" );
1548 	if ( status & IE_XS_UNDERRUN )
1549 	    printf ( "ie: DMA underrun\n" );
1550 	if ( status & IE_XS_EXCMAX )
1551 	    printf ( "ie: too many collisions\n" );
1552 	ifp->if_collisions+=16;
1553     }
1554     /* Done with the buffer */
1555     sc->xmit_free++;
1556     sc->xctail = (sc->xctail + 1 ) % NTXBUF;
1557 
1558     /* Start the next packet transmitting, if any */
1559     if ( sc->xmit_free<NTXBUF )
1560 	iexmit(sc);
1561 
1562     iestart(ifp);
1563 }
1564 
1565 /* End of if_ie.c */
1566