File: /usr/src/linux/drivers/net/sun3lance.c

1     /* sun3lance.c: Ethernet driver for SUN3 Lance chip */
2     /*
3     
4       Sun3 Lance ethernet driver, by Sam Creasey (sammy@users.qual.net).  
5       This driver is a part of the linux kernel, and is thus distributed
6       under the GNU General Public License.
7       
8       The values used in LANCE_OBIO and LANCE_IRQ seem to be empirically
9       true for the correct IRQ and address of the lance registers.  They
10       have not been widely tested, however.  What we probably need is a
11       "proper" way to search for a device in the sun3's prom, but, alas,
12       linux has no such thing.  
13     
14       This driver is largely based on atarilance.c, by Roman Hodek.  Other
15       sources of inspiration were the NetBSD sun3 am7990 driver, and the
16       linux sparc lance driver (sunlance.c).  
17     
18       There are more assumptions made throughout this driver, it almost
19       certainly still needs work, but it does work at least for RARP/BOOTP and
20       mounting the root NFS filesystem.
21       
22     */
23     
24     static char *version = "sun3lance.c: v1.1 11/17/1999  Sam Creasey (sammy@oh.verio.com)\n";
25     
26     #include <linux/module.h>
27     
28     #include <linux/stddef.h>
29     #include <linux/kernel.h>
30     #include <linux/sched.h>
31     #include <linux/string.h>
32     #include <linux/ptrace.h>
33     #include <linux/errno.h>
34     #include <linux/slab.h>
35     #include <linux/interrupt.h>
36     #include <linux/init.h>
37     #include <linux/ioport.h>
38     
39     #include <asm/setup.h>
40     #include <asm/irq.h>
41     
42     #include <asm/bitops.h>
43     #include <asm/io.h>
44     #include <asm/idprom.h>
45     #include <asm/pgtable.h>
46     #include <asm/sun3mmu.h>
47     #include <asm/dvma.h>
48     
49     #include <linux/netdevice.h>
50     #include <linux/etherdevice.h>
51     #include <linux/skbuff.h>
52     
53     /* sun3/60 addr/irq for the lance chip.  If your sun is different,
54        change this. */
55     #define LANCE_OBIO 0x120000
56     #define LANCE_IRQ IRQ3
57     
58     /* Debug level:
59      *  0 = silent, print only serious errors
60      *  1 = normal, print error messages
61      *  2 = debug, print debug infos
62      *  3 = debug, print even more debug infos (packet data)
63      */
64     
65     #define	LANCE_DEBUG	1
66     
67     #ifdef LANCE_DEBUG
68     static int lance_debug = LANCE_DEBUG;
69     #else
70     static int lance_debug = 1;
71     #endif
72     MODULE_PARM(lance_debug, "i");
73     MODULE_PARM_DESC(lance_debug, "SUN3 Lance debug level (0-3)");
74     
75     #define	DPRINTK(n,a) \
76     	do {  \
77     		if (lance_debug >= n)  \
78     			printk a; \
79     	} while( 0 )
80     
81     
82     /* we're only using 32k of memory, so we use 4 TX
83        buffers and 16 RX buffers.  These values are expressed as log2. */
84     
85     #define TX_LOG_RING_SIZE			3
86     #define RX_LOG_RING_SIZE			5
87     
88     /* These are the derived values */
89     
90     #define TX_RING_SIZE			(1 << TX_LOG_RING_SIZE)
91     #define TX_RING_LEN_BITS		(TX_LOG_RING_SIZE << 5)
92     #define	TX_RING_MOD_MASK		(TX_RING_SIZE - 1)
93     
94     #define RX_RING_SIZE			(1 << RX_LOG_RING_SIZE)
95     #define RX_RING_LEN_BITS		(RX_LOG_RING_SIZE << 5)
96     #define	RX_RING_MOD_MASK		(RX_RING_SIZE - 1)
97     
98     /* Definitions for packet buffer access: */
99     #define PKT_BUF_SZ		1544
100     
101     /* Get the address of a packet buffer corresponding to a given buffer head */
102     #define	PKTBUF_ADDR(head)	(void *)((unsigned long)(MEM) | (head)->base)
103     
104     
105     /* The LANCE Rx and Tx ring descriptors. */
106     struct lance_rx_head {
107     	unsigned short	base;		/* Low word of base addr */
108     	volatile unsigned char	flag;
109     	unsigned char  base_hi;	/* High word of base addr (unused) */
110     	short buf_length;	/* This length is 2s complement! */
111     	volatile short msg_length;	/* This length is "normal". */
112     };
113     
114     struct lance_tx_head {
115     	unsigned short base;		/* Low word of base addr */
116     	volatile unsigned char	flag;
117     	unsigned char base_hi;	/* High word of base addr (unused) */
118     	short length;		/* Length is 2s complement! */
119     	volatile short misc;
120     };
121     
122     /* The LANCE initialization block, described in databook. */
123     struct lance_init_block {
124     	unsigned short	mode;		/* Pre-set mode */
125     	unsigned char	hwaddr[6];	/* Physical ethernet address */
126     	unsigned int    filter[2];	/* Multicast filter (unused). */
127     	/* Receive and transmit ring base, along with length bits. */
128     	unsigned short rdra;
129     	unsigned short rlen;
130     	unsigned short tdra;
131     	unsigned short tlen;
132     	unsigned short pad[4]; /* is thie needed? */
133     };
134     
135     /* The whole layout of the Lance shared memory */
136     struct lance_memory {
137     	struct lance_init_block	init;
138     	struct lance_tx_head	tx_head[TX_RING_SIZE];
139     	struct lance_rx_head	rx_head[RX_RING_SIZE];
140     	char   rx_data[RX_RING_SIZE][PKT_BUF_SZ];
141     	char   tx_data[RX_RING_SIZE][PKT_BUF_SZ];
142     };
143     
144     /* The driver's private device structure */
145     
146     struct lance_private {
147     	volatile unsigned short	*iobase;
148     	struct lance_memory	*mem;
149          	int new_rx, new_tx;	/* The next free ring entry */
150     	int old_tx, old_rx;     /* ring entry to be processed */
151     	struct net_device_stats stats;
152     /* These two must be longs for set_bit() */
153     	long					tx_full;
154     	long					lock;
155     };
156     
157     /* I/O register access macros */
158     
159     #define	MEM	lp->mem
160     #define	DREG	lp->iobase[0]
161     #define	AREG	lp->iobase[1]
162     #define	REGA(a)	( AREG = (a), DREG )
163     
164     /* Definitions for the Lance */
165     
166     /* tx_head flags */
167     #define TMD1_ENP		0x01	/* end of packet */
168     #define TMD1_STP		0x02	/* start of packet */
169     #define TMD1_DEF		0x04	/* deferred */
170     #define TMD1_ONE		0x08	/* one retry needed */
171     #define TMD1_MORE		0x10	/* more than one retry needed */
172     #define TMD1_ERR		0x40	/* error summary */
173     #define TMD1_OWN 		0x80	/* ownership (set: chip owns) */
174     
175     #define TMD1_OWN_CHIP	TMD1_OWN
176     #define TMD1_OWN_HOST	0
177     
178     /* tx_head misc field */
179     #define TMD3_TDR		0x03FF	/* Time Domain Reflectometry counter */
180     #define TMD3_RTRY		0x0400	/* failed after 16 retries */
181     #define TMD3_LCAR		0x0800	/* carrier lost */
182     #define TMD3_LCOL		0x1000	/* late collision */
183     #define TMD3_UFLO		0x4000	/* underflow (late memory) */
184     #define TMD3_BUFF		0x8000	/* buffering error (no ENP) */
185     
186     /* rx_head flags */
187     #define RMD1_ENP		0x01	/* end of packet */
188     #define RMD1_STP		0x02	/* start of packet */
189     #define RMD1_BUFF		0x04	/* buffer error */
190     #define RMD1_CRC		0x08	/* CRC error */
191     #define RMD1_OFLO		0x10	/* overflow */
192     #define RMD1_FRAM		0x20	/* framing error */
193     #define RMD1_ERR		0x40	/* error summary */
194     #define RMD1_OWN 		0x80	/* ownership (set: ship owns) */
195     
196     #define RMD1_OWN_CHIP	RMD1_OWN
197     #define RMD1_OWN_HOST	0
198     
199     /* register names */
200     #define CSR0	0		/* mode/status */
201     #define CSR1	1		/* init block addr (low) */
202     #define CSR2	2		/* init block addr (high) */
203     #define CSR3	3		/* misc */
204     #define CSR8	8	  	/* address filter */
205     #define CSR15	15		/* promiscuous mode */
206     
207     /* CSR0 */
208     /* (R=readable, W=writeable, S=set on write, C=clear on write) */
209     #define CSR0_INIT	0x0001		/* initialize (RS) */
210     #define CSR0_STRT	0x0002		/* start (RS) */
211     #define CSR0_STOP	0x0004		/* stop (RS) */
212     #define CSR0_TDMD	0x0008		/* transmit demand (RS) */
213     #define CSR0_TXON	0x0010		/* transmitter on (R) */
214     #define CSR0_RXON	0x0020		/* receiver on (R) */
215     #define CSR0_INEA	0x0040		/* interrupt enable (RW) */
216     #define CSR0_INTR	0x0080		/* interrupt active (R) */
217     #define CSR0_IDON	0x0100		/* initialization done (RC) */
218     #define CSR0_TINT	0x0200		/* transmitter interrupt (RC) */
219     #define CSR0_RINT	0x0400		/* receiver interrupt (RC) */
220     #define CSR0_MERR	0x0800		/* memory error (RC) */
221     #define CSR0_MISS	0x1000		/* missed frame (RC) */
222     #define CSR0_CERR	0x2000		/* carrier error (no heartbeat :-) (RC) */
223     #define CSR0_BABL	0x4000		/* babble: tx-ed too many bits (RC) */
224     #define CSR0_ERR	0x8000		/* error (RC) */
225     
226     /* CSR3 */
227     #define CSR3_BCON	0x0001		/* byte control */
228     #define CSR3_ACON	0x0002		/* ALE control */
229     #define CSR3_BSWP	0x0004		/* byte swap (1=big endian) */
230     
231     /***************************** Prototypes *****************************/
232     
233     static int lance_probe( struct net_device *dev);
234     static int lance_open( struct net_device *dev );
235     static void lance_init_ring( struct net_device *dev );
236     static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev );
237     static void lance_interrupt( int irq, void *dev_id, struct pt_regs *fp );
238     static int lance_rx( struct net_device *dev );
239     static int lance_close( struct net_device *dev );
240     static struct net_device_stats *lance_get_stats( struct net_device *dev );
241     static void set_multicast_list( struct net_device *dev );
242     
243     /************************* End of Prototypes **************************/
244     
245     int __init sun3lance_probe( struct net_device *dev )
246     {	
247     	static int found;
248     
249     	if(found)
250     		return(ENODEV);
251     
252     	if (lance_probe(dev)) {
253     			found = 1;
254     			return( 0 );
255     	}
256     
257     	return( ENODEV );
258     }
259     
260     static int __init lance_probe( struct net_device *dev)
261     {	
262     	unsigned long ioaddr, iopte;
263     	
264     	struct lance_private	*lp;
265     	int 			i;
266     	static int 		did_version;
267     	int found = 0;
268     	volatile unsigned short *ioaddr_probe;
269     	unsigned short tmp1, tmp2;
270     
271     	/* LANCE_OBIO can be found within the IO pmeg with some effort */
272     	for(ioaddr = 0xfe00000; ioaddr < (0xfe00000 +
273     	    SUN3_PMEG_SIZE); ioaddr += SUN3_PTE_SIZE) {
274     
275     		iopte = sun3_get_pte(ioaddr);
276     		if(!(iopte & SUN3_PAGE_TYPE_IO)) /* this an io page? */
277     			continue;
278     
279     		if(((iopte & SUN3_PAGE_PGNUM_MASK) << PAGE_SHIFT) ==
280     		   LANCE_OBIO) {
281     			found = 1;
282     			break;
283     		}
284     	}
285     	
286     	if(!found)
287     		return 0;
288     
289     	/* test to see if there's really a lance here */
290     	/* (CSRO_INIT shouldn't be readable) */
291     	
292     	ioaddr_probe = (volatile unsigned short *)ioaddr;
293     	tmp1 = ioaddr_probe[0];
294     	tmp2 = ioaddr_probe[1];
295     
296     	ioaddr_probe[1] = CSR0;
297     	ioaddr_probe[0] = CSR0_INIT | CSR0_STOP;
298     
299     	if(ioaddr_probe[0] != CSR0_STOP) {
300     		ioaddr_probe[0] = tmp1;
301     		ioaddr_probe[1] = tmp2;
302     
303     		return 0;
304     	}
305     
306     	init_etherdev( dev, sizeof(struct lance_private) );
307     	if (!dev->priv) {
308     		dev->priv = kmalloc( sizeof(struct lance_private), GFP_KERNEL );
309     		if (!dev->priv)
310     			return 0;
311     	}
312     	lp = (struct lance_private *)dev->priv;
313     	MEM = (struct lance_memory *)sun3_dvma_malloc(sizeof(struct
314     							     lance_memory)); 
315     	lp->iobase = (volatile unsigned short *)ioaddr;
316     	dev->base_addr = (unsigned long)ioaddr; /* informational only */
317     
318     	REGA(CSR0) = CSR0_STOP; 
319     
320     	request_irq(LANCE_IRQ, lance_interrupt, 0, "SUN3 Lance", dev);
321     	dev->irq = (unsigned short)LANCE_IRQ;
322     
323     
324     	printk("%s: SUN3 Lance at io %#lx, mem %#lx, irq %d, hwaddr ",
325     		   dev->name,
326     		   (unsigned long)ioaddr,
327     		   (unsigned long)MEM,
328     		   dev->irq);
329     
330     	/* copy in the ethernet address from the prom */
331     	for(i = 0; i < 6 ; i++)
332     	     dev->dev_addr[i] = idprom->id_ethaddr[i];
333     	
334     	/* tell the card it's ether address, bytes swapped */
335     	MEM->init.hwaddr[0] = dev->dev_addr[1];
336     	MEM->init.hwaddr[1] = dev->dev_addr[0];
337     	MEM->init.hwaddr[2] = dev->dev_addr[3];
338     	MEM->init.hwaddr[3] = dev->dev_addr[2];
339     	MEM->init.hwaddr[4] = dev->dev_addr[5];
340     	MEM->init.hwaddr[5] = dev->dev_addr[4];
341     
342     	for( i = 0; i < 6; ++i )
343     		printk( "%02x%s", dev->dev_addr[i], (i < 5) ? ":" : "\n" );
344     
345     	MEM->init.mode = 0x0000;
346     	MEM->init.filter[0] = 0x00000000;
347     	MEM->init.filter[1] = 0x00000000;
348     	MEM->init.rdra = sun3_dvma_vtop(MEM->rx_head);
349     	MEM->init.rlen    = (RX_LOG_RING_SIZE << 13) |
350     		(sun3_dvma_vtop(MEM->rx_head) >> 16);
351     	MEM->init.tdra = sun3_dvma_vtop(MEM->tx_head);
352     	MEM->init.tlen    = (TX_LOG_RING_SIZE << 13) |
353     		(sun3_dvma_vtop(MEM->tx_head) >> 16);
354     
355     	DPRINTK(2, ("initaddr: %08lx rx_ring: %08lx tx_ring: %08lx\n",
356     	       sun3_dvma_vtop(&(MEM->init)), sun3_dvma_vtop(MEM->rx_head),
357     	       (sun3_dvma_vtop(MEM->tx_head))));  
358     
359     
360     	if (did_version++ == 0)
361     		DPRINTK( 1, ( version ));
362     
363     	/* The LANCE-specific entries in the device structure. */
364     	dev->open = &lance_open;
365     	dev->hard_start_xmit = &lance_start_xmit;
366     	dev->stop = &lance_close;
367     	dev->get_stats = &lance_get_stats;
368     	dev->set_multicast_list = &set_multicast_list;
369     	dev->set_mac_address = 0;
370     //	KLUDGE -- REMOVE ME
371     	set_bit(__LINK_STATE_PRESENT, &dev->state);
372     
373     
374     	memset( &lp->stats, 0, sizeof(lp->stats) );
375     
376     	return 1;
377     }
378     
379     static int lance_open( struct net_device *dev )
380     {
381     	struct lance_private *lp = (struct lance_private *)dev->priv;
382     	int i;
383     
384     	DPRINTK( 2, ( "%s: lance_open()\n", dev->name ));
385     
386     	REGA(CSR0) = CSR0_STOP;
387     
388     	/* tell the lance the address of its init block */
389     	REGA(CSR1) = sun3_dvma_vtop(&(MEM->init));
390     	REGA(CSR2) = sun3_dvma_vtop(&(MEM->init)) >> 16;
391     
392     	lance_init_ring(dev);
393     
394     	/* Re-initialize the LANCE, and start it when done. */
395     
396     	REGA(CSR3) = CSR3_BSWP;
397     
398     	/* From now on, AREG is kept to point to CSR0 */
399     	REGA(CSR0) = CSR0_INIT;
400     
401     	i = 1000000;
402     	while (--i > 0)
403     		if (DREG & CSR0_IDON)
404     			break;
405     	if (i < 0 || (DREG & CSR0_ERR)) {
406     		DPRINTK( 2, ( "lance_open(): opening %s failed, i=%d, csr0=%04x\n",
407     					  dev->name, i, DREG ));
408     		DREG = CSR0_STOP;
409     		return( -EIO );
410     	}
411     
412     	DREG = CSR0_IDON | CSR0_STRT | CSR0_INEA;
413     
414     	netif_start_queue(dev);
415     	
416     	DPRINTK( 2, ( "%s: LANCE is open, csr0 %04x\n", dev->name, DREG ));
417     	MOD_INC_USE_COUNT;
418     
419     	return( 0 );
420     }
421     
422     
423     /* Initialize the LANCE Rx and Tx rings. */
424     
425     static void lance_init_ring( struct net_device *dev )
426     {
427     	struct lance_private *lp = (struct lance_private *)dev->priv;
428     	int i;
429     
430     	lp->lock = 0;
431     	lp->tx_full = 0;
432     	lp->new_rx = lp->new_tx = 0;
433     	lp->old_rx = lp->old_tx = 0;
434     
435     	for( i = 0; i < TX_RING_SIZE; i++ ) {
436     		MEM->tx_head[i].base = sun3_dvma_vtop(MEM->tx_data[i]);
437     		MEM->tx_head[i].flag = 0;
438      		MEM->tx_head[i].base_hi = 
439     			(sun3_dvma_vtop(MEM->tx_data[i])) >>16;
440     		MEM->tx_head[i].length = 0;
441     		MEM->tx_head[i].misc = 0;
442     	}
443     
444     	for( i = 0; i < RX_RING_SIZE; i++ ) {
445     		MEM->rx_head[i].base = sun3_dvma_vtop(MEM->rx_data[i]);
446     		MEM->rx_head[i].flag = TMD1_OWN_CHIP;
447     		MEM->rx_head[i].base_hi = 
448     			(sun3_dvma_vtop(MEM->rx_data[i])) >> 16;
449     		MEM->rx_head[i].buf_length = -PKT_BUF_SZ | 0xf000;
450     		MEM->rx_head[i].msg_length = 0;
451     	}
452     
453     }
454     
455     
456     static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev )
457     {
458     	struct lance_private *lp = (struct lance_private *)dev->priv;
459     	int entry, len;
460     	struct lance_tx_head *head;
461     	unsigned long flags;
462     
463     	/* Transmitter timeout, serious problems. */
464     	if (netif_queue_stopped(dev)) {
465     		int tickssofar = jiffies - dev->trans_start;
466     		if (tickssofar < 20)
467     			return( 1 );
468     
469     		DPRINTK( 1, ( "%s: transmit timed out, status %04x, resetting.\n",
470     					  dev->name, DREG ));
471     		DREG = CSR0_STOP;
472     		/*
473     		 * Always set BSWP after a STOP as STOP puts it back into
474     		 * little endian mode.
475     		 */
476     		REGA(CSR3) = CSR3_BSWP;
477     		lp->stats.tx_errors++;
478     
479     		if(lance_debug >= 2) {
480     			int i;
481     			printk("Ring data: old_tx %d new_tx %d%s new_rx %d\n",
482     			       lp->old_tx, lp->new_tx,
483     			       lp->tx_full ? " (full)" : "",
484     			       lp->new_rx );
485     			for( i = 0 ; i < RX_RING_SIZE; i++ )
486     				printk( "rx #%d: base=%04x blen=%04x mlen=%04x\n",
487     					i, MEM->rx_head[i].base,
488     					-MEM->rx_head[i].buf_length,
489     					MEM->rx_head[i].msg_length);
490     			for( i = 0 ; i < TX_RING_SIZE; i++ )
491     				printk("tx #%d: base=%04x len=%04x misc=%04x\n",
492     				       i, MEM->tx_head[i].base,
493     				       -MEM->tx_head[i].length,
494     				       MEM->tx_head[i].misc );
495     		}
496     
497     		lance_init_ring(dev);
498     		REGA( CSR0 ) = CSR0_INEA | CSR0_INIT | CSR0_STRT;
499     		
500     		netif_start_queue(dev);
501     		dev->trans_start = jiffies;
502     		
503     		return 0;
504     	}
505     
506     	
507     	/* Block a timer-based transmit from overlapping.  This could better be
508     	   done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
509     
510     	/* Block a timer-based transmit from overlapping with us by
511     	   stopping the queue for a bit... */
512          
513     	netif_stop_queue(dev);
514     
515     	if (test_and_set_bit( 0, (void*)&lp->lock ) != 0) {
516     		printk( "%s: tx queue lock!.\n", dev->name);
517     		/* don't clear dev->tbusy flag. */
518     		return 1;
519     	}
520     
521     	AREG = CSR0;
522     //	DPRINTK( 2, ( "%s: lance_start_xmit() called, csr0 %4.4x.\n",
523     //				  dev->name, DREG ));
524     	
525     
526     	/* Fill in a Tx ring entry */
527     #if 0
528     	if (lance_debug >= 3) {
529     		u_char *p;
530     		int i;
531     		printk( "%s: TX pkt %d type 0x%04x from ", dev->name,
532     			lp->new_tx, ((u_short *)skb->data)[6]);
533     		for( p = &((u_char *)skb->data)[6], i = 0; i < 6; i++ )
534     			printk("%02x%s", *p++, i != 5 ? ":" : "" );
535     		printk(" to ");
536     		for( p = (u_char *)skb->data, i = 0; i < 6; i++ )
537     			printk("%02x%s", *p++, i != 5 ? ":" : "" );
538     		printk(" data at 0x%08x len %d\n", (int)skb->data,
539     		       (int)skb->len );
540     	}
541     #endif	
542     	/* We're not prepared for the int until the last flags are set/reset.
543     	 * And the int may happen already after setting the OWN_CHIP... */
544     	save_and_cli(flags);
545     
546     	/* Mask to ring buffer boundary. */
547     	entry = lp->new_tx;
548     	head  = &(MEM->tx_head[entry]);
549     
550     	/* Caution: the write order is important here, set the "ownership" bits
551     	 * last.
552     	 */
553     
554     	/* the sun3's lance needs it's buffer padded to the minimum
555     	   size */
556     	len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
557     
558     //	head->length = -len;
559     	head->length = (-len) | 0xf000;
560     	head->misc = 0;
561     
562     	memcpy( PKTBUF_ADDR(head), (void *)skb->data, skb->len );
563     	head->flag = TMD1_OWN_CHIP | TMD1_ENP | TMD1_STP;
564     	lp->new_tx = (lp->new_tx + 1) & TX_RING_MOD_MASK;
565     	lp->stats.tx_bytes += skb->len;
566     
567     	/* Trigger an immediate send poll. */
568     	REGA(CSR0) = CSR0_INEA | CSR0_TDMD;
569     	dev->trans_start = jiffies;
570     	dev_kfree_skb( skb );
571     
572     	lp->lock = 0;
573     	if ((MEM->tx_head[(entry+1) & TX_RING_MOD_MASK].flag & TMD1_OWN) ==
574     	    TMD1_OWN_HOST) 
575     		netif_start_queue(dev);
576     
577     	restore_flags(flags);
578     
579     	return 0;
580     }
581     
582     /* The LANCE interrupt handler. */
583     
584     static void lance_interrupt( int irq, void *dev_id, struct pt_regs *fp)
585     {
586     	struct net_device *dev = dev_id;
587     	struct lance_private *lp = dev->priv;
588     	int csr0;
589     	static int in_interrupt;
590     
591     	if (dev == NULL) {
592     		DPRINTK( 1, ( "lance_interrupt(): invalid dev_id\n" ));
593     		return;
594     	}
595     
596     	if (in_interrupt)
597     		DPRINTK( 2, ( "%s: Re-entering the interrupt handler.\n", dev->name ));
598     	in_interrupt = 1;
599     
600      still_more:
601     
602     	AREG = CSR0;
603     	csr0 = DREG;
604     
605     	/* ack interrupts */
606     	DREG = csr0 & (CSR0_TINT | CSR0_RINT);
607     
608     	/* clear errors */
609     	if(csr0 & CSR0_ERR)
610     		DREG = CSR0_BABL | CSR0_MERR | CSR0_CERR | CSR0_MISS;
611     
612     
613     	DPRINTK( 2, ( "%s: interrupt  csr0=%04x new csr=%04x.\n",
614     		      dev->name, csr0, DREG ));
615     
616     	if (csr0 & CSR0_TINT) {			/* Tx-done interrupt */
617     		int old_tx = lp->old_tx;
618     
619     //		if(lance_debug >= 3) {
620     //			int i;
621     //			
622     //			printk("%s: tx int\n", dev->name);
623     //			
624     //			for(i = 0; i < TX_RING_SIZE; i++)
625     //				printk("ring %d flag=%04x\n", i,
626     //				       MEM->tx_head[i].flag);
627     //		}
628     		
629     		while( old_tx != lp->new_tx) {
630     			struct lance_tx_head *head = &(MEM->tx_head[old_tx]); 
631     			
632     			DPRINTK(3, ("on tx_ring %d\n", old_tx));
633     
634     			if (head->flag & TMD1_OWN_CHIP)
635     				break; /* It still hasn't been Txed */
636     				
637     			if (head->flag & TMD1_ERR) {
638     				int status = head->misc;
639     				lp->stats.tx_errors++;
640     				if (status & TMD3_RTRY) lp->stats.tx_aborted_errors++;
641     				if (status & TMD3_LCAR) lp->stats.tx_carrier_errors++;
642     				if (status & TMD3_LCOL) lp->stats.tx_window_errors++;
643     				if (status & (TMD3_UFLO | TMD3_BUFF)) {
644     					lp->stats.tx_fifo_errors++;
645     					printk("%s: Tx FIFO error\n",
646     					       dev->name); 
647     					REGA(CSR0) = CSR0_STOP;
648     					REGA(CSR3) = CSR3_BSWP;
649     					lance_init_ring(dev);
650     					REGA(CSR0) = CSR0_STRT | CSR0_INEA;
651     					return;
652     				}
653     			} else if(head->flag & (TMD1_ENP | TMD1_STP)) {
654     				
655     				head->flag &= ~(TMD1_ENP | TMD1_STP);
656     				if(head->flag & (TMD1_ONE | TMD1_MORE))
657     					lp->stats.collisions++;
658     				
659     				lp->stats.tx_packets++;
660     				DPRINTK(3, ("cleared tx ring %d\n", old_tx));
661     			}
662     			old_tx = (old_tx +1) & TX_RING_MOD_MASK;
663     		}
664     
665     		lp->old_tx = old_tx;
666     	}
667     
668     
669     	if (netif_queue_stopped(dev)) {
670     		/* The ring is no longer full, clear tbusy. */
671     		netif_start_queue(dev);
672     		netif_wake_queue(dev);
673     	}
674     
675     	if (csr0 & CSR0_RINT)			/* Rx interrupt */
676     		lance_rx( dev );
677     	
678     	/* Log misc errors. */
679     	if (csr0 & CSR0_BABL) lp->stats.tx_errors++; /* Tx babble. */
680     	if (csr0 & CSR0_MISS) lp->stats.rx_errors++; /* Missed a Rx frame. */
681     	if (csr0 & CSR0_MERR) {
682     		DPRINTK( 1, ( "%s: Bus master arbitration failure (?!?), "
683     			      "status %04x.\n", dev->name, csr0 ));
684     		/* Restart the chip. */
685     		REGA(CSR0) = CSR0_STOP;
686     		REGA(CSR3) = CSR3_BSWP;
687     		lance_init_ring(dev);
688     		REGA(CSR0) = CSR0_STRT | CSR0_INEA;
689     	}
690     
691     
692         /* Clear any other interrupt, and set interrupt enable. */
693     //	DREG = CSR0_BABL | CSR0_CERR | CSR0_MISS | CSR0_MERR |
694     //		   CSR0_IDON | CSR0_INEA;
695     
696     	REGA(CSR0) = CSR0_INEA;
697     
698     	if(DREG & (CSR0_RINT | CSR0_TINT)) {
699     	     DPRINTK(2, ("restarting interrupt, csr0=%#04x\n", DREG));
700     	     goto still_more;
701     	}
702     
703     	DPRINTK( 2, ( "%s: exiting interrupt, csr0=%#04x.\n",
704     				  dev->name, DREG ));
705     	in_interrupt = 0;
706     	return;
707     }
708     
709     /* get packet, toss into skbuff */
710     static int lance_rx( struct net_device *dev )
711     {
712     	struct lance_private *lp = (struct lance_private *)dev->priv;
713     	int entry = lp->new_rx;
714     
715     	/* If we own the next entry, it's a new packet. Send it up. */
716     	while( (MEM->rx_head[entry].flag & RMD1_OWN) == RMD1_OWN_HOST ) {
717     		struct lance_rx_head *head = &(MEM->rx_head[entry]);
718     		int status = head->flag;
719     		
720     		if (status != (RMD1_ENP|RMD1_STP)) {  /* There was an error. */
721     			/* There is a tricky error noted by John Murphy,
722     			   <murf@perftech.com> to Russ Nelson: Even with 
723     			   full-sized buffers it's possible for a jabber packet to use two
724     			   buffers, with only the last correctly noting the error. */
725     			if (status & RMD1_ENP)	/* Only count a general error at the */
726     				lp->stats.rx_errors++; /* end of a packet.*/
727     			if (status & RMD1_FRAM) lp->stats.rx_frame_errors++;
728     			if (status & RMD1_OFLO) lp->stats.rx_over_errors++;
729     			if (status & RMD1_CRC) lp->stats.rx_crc_errors++;
730     			if (status & RMD1_BUFF) lp->stats.rx_fifo_errors++;
731     			head->flag &= (RMD1_ENP|RMD1_STP);
732     		} else {
733     			/* Malloc up new buffer, compatible with net-3. */
734     //			short pkt_len = head->msg_length;// & 0xfff;
735     			short pkt_len = (head->msg_length & 0xfff) - 4;
736     			struct sk_buff *skb;
737     
738     			if (pkt_len < 60) {
739     				printk( "%s: Runt packet!\n", dev->name );
740     				lp->stats.rx_errors++;
741     			}
742     			else {
743     				skb = dev_alloc_skb( pkt_len+2 );
744     				if (skb == NULL) {
745     					DPRINTK( 1, ( "%s: Memory squeeze, deferring packet.\n",
746     						      dev->name ));
747     					
748     					lp->stats.rx_dropped++;
749     					head->msg_length = 0;
750     					head->flag |= RMD1_OWN_CHIP;
751     					lp->new_rx = (lp->new_rx+1) &
752     					     RX_RING_MOD_MASK;
753     				}
754     
755     #if 0
756     				if (lance_debug >= 3) {
757     					u_char *data = PKTBUF_ADDR(head), *p;
758     					printk( "%s: RX pkt %d type 0x%04x from ", dev->name, entry, ((u_short *)data)[6]);
759     					for( p = &data[6], i = 0; i < 6; i++ )
760     						printk("%02x%s", *p++, i != 5 ? ":" : "" );
761     					printk(" to ");
762     					for( p = data, i = 0; i < 6; i++ )
763     						printk("%02x%s", *p++, i != 5 ? ":" : "" );
764     					printk(" data %02x %02x %02x %02x %02x %02x %02x %02x "
765     					       "len %d at %08x\n",
766     					       data[15], data[16], data[17], data[18],
767     					       data[19], data[20], data[21], data[22],
768     					       pkt_len, data);
769     				}
770     #endif
771     				if (lance_debug >= 3) {
772     					u_char *data = PKTBUF_ADDR(head);
773     					printk( "%s: RX pkt %d type 0x%04x len %d\n ", dev->name, entry, ((u_short *)data)[6], pkt_len);
774     				}				
775     
776     
777     				skb->dev = dev;
778     				skb_reserve( skb, 2 );	/* 16 byte align */
779     				skb_put( skb, pkt_len );	/* Make room */
780     //			        memcpy( skb->data, PKTBUF_ADDR(head), pkt_len );
781     				eth_copy_and_sum(skb,
782     						 PKTBUF_ADDR(head),
783     						 pkt_len, 0);
784     
785     				skb->protocol = eth_type_trans( skb, dev );
786     				netif_rx( skb );
787     				dev->last_rx = jiffies;
788     				lp->stats.rx_packets++;
789     				lp->stats.rx_bytes += pkt_len;
790     			}
791     		}
792     
793     //		head->buf_length = -PKT_BUF_SZ | 0xf000;
794     		head->msg_length = 0;
795     		head->flag = RMD1_OWN_CHIP;
796     
797     		entry = lp->new_rx = (lp->new_rx +1) & RX_RING_MOD_MASK;
798     	}
799     
800     	/* From lance.c (Donald Becker): */
801     	/* We should check that at least two ring entries are free.
802     	   If not, we should free one and mark stats->rx_dropped++. */
803     
804     	return 0;
805     }
806     
807     
808     static int lance_close( struct net_device *dev )
809     {
810     	struct lance_private *lp = (struct lance_private *)dev->priv;
811     
812     	netif_stop_queue(dev);
813     
814     	AREG = CSR0;
815     
816     	DPRINTK( 2, ( "%s: Shutting down ethercard, status was %2.2x.\n",
817     				  dev->name, DREG ));
818     
819     	/* We stop the LANCE here -- it occasionally polls
820     	   memory if we don't. */
821     	DREG = CSR0_STOP;
822     
823     	MOD_DEC_USE_COUNT;
824     	return 0;
825     }
826     
827     
828     static struct net_device_stats *lance_get_stats( struct net_device *dev )
829     {
830     	struct lance_private *lp = (struct lance_private *)dev->priv;
831     
832     	return &lp->stats;
833     }
834     
835     
836     /* Set or clear the multicast filter for this adaptor.
837        num_addrs == -1		Promiscuous mode, receive all packets
838        num_addrs == 0		Normal mode, clear multicast list
839        num_addrs > 0		Multicast mode, receive normal and MC packets, and do
840     						best-effort filtering.
841      */
842     
843     /* completely untested on a sun3 */
844     static void set_multicast_list( struct net_device *dev )
845     {
846     	struct lance_private *lp = (struct lance_private *)dev->priv;
847     
848     	if(netif_queue_stopped(dev))
849     		/* Only possible if board is already started */
850     		return;
851     
852     	/* We take the simple way out and always enable promiscuous mode. */
853     	DREG = CSR0_STOP; /* Temporarily stop the lance. */
854     
855     	if (dev->flags & IFF_PROMISC) {
856     		/* Log any net taps. */
857     		DPRINTK( 1, ( "%s: Promiscuous mode enabled.\n", dev->name ));
858     		REGA( CSR15 ) = 0x8000; /* Set promiscuous mode */
859     	} else {
860     		short multicast_table[4];
861     		int num_addrs = dev->mc_count;
862     		int i;
863     		/* We don't use the multicast table, but rely on upper-layer
864     		 * filtering. */
865     		memset( multicast_table, (num_addrs == 0) ? 0 : -1,
866     				sizeof(multicast_table) );
867     		for( i = 0; i < 4; i++ )
868     			REGA( CSR8+i ) = multicast_table[i];
869     		REGA( CSR15 ) = 0; /* Unset promiscuous mode */
870     	}
871     
872     	/*
873     	 * Always set BSWP after a STOP as STOP puts it back into
874     	 * little endian mode.
875     	 */
876     	REGA( CSR3 ) = CSR3_BSWP;
877     
878     	/* Resume normal operation and reset AREG to CSR0 */
879     	REGA( CSR0 ) = CSR0_IDON | CSR0_INEA | CSR0_STRT;
880     }
881     
882     
883     #ifdef MODULE
884     static char devicename[9];
885     
886     static struct net_device sun3lance_dev =
887     {
888     	devicename,	/* filled in by register_netdev() */
889     	0, 0, 0, 0,	/* memory */
890     	0, 0,		/* base, irq */
891     	0, 0, 0, NULL, sun3lance_probe,
892     };
893     
894     int init_module(void)
895     {
896     	int err;
897     
898     	if ((err = register_netdev( &sun3lance_dev ))) {
899     		if (err == -EIO)  {
900     			printk( "SUN3 Lance not detected.  Module not loaded.\n");
901     		}
902     		return( err );
903     	}
904     	return( 0 );
905     }
906     
907     void cleanup_module(void)
908     {
909     	unregister_netdev( &sun3lance_dev );
910     }
911     
912     #endif /* MODULE */
913     
914