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