Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / net / sun3lance.c
blob52b62ee9f8f01aeb3fb6c38e009cd25b6a57c856
1 /* sun3lance.c: Ethernet driver for SUN3 Lance chip */
2 /*
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 Public License.
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.
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).
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.
24 static char *version = "sun3lance.c: v1.1 11/17/1999 Sam Creasey (sammy@oh.verio.com)\n";
26 #include <linux/module.h>
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/malloc.h>
35 #include <linux/interrupt.h>
36 #include <linux/init.h>
37 #include <linux/ioport.h>
39 #include <asm/setup.h>
40 #include <asm/irq.h>
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>
49 #include <linux/netdevice.h>
50 #include <linux/etherdevice.h>
51 #include <linux/skbuff.h>
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
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)
65 #define LANCE_DEBUG 1
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");
74 #define DPRINTK(n,a) \
75 do { \
76 if (lance_debug >= n) \
77 printk a; \
78 } while( 0 )
81 /* we're only using 32k of memory, so we use 4 TX
82 buffers and 16 RX buffers. These values are expressed as log2. */
84 #define TX_LOG_RING_SIZE 3
85 #define RX_LOG_RING_SIZE 5
87 /* These are the derived values */
89 #define TX_RING_SIZE (1 << TX_LOG_RING_SIZE)
90 #define TX_RING_LEN_BITS (TX_LOG_RING_SIZE << 5)
91 #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
93 #define RX_RING_SIZE (1 << RX_LOG_RING_SIZE)
94 #define RX_RING_LEN_BITS (RX_LOG_RING_SIZE << 5)
95 #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
97 /* Definitions for packet buffer access: */
98 #define PKT_BUF_SZ 1544
100 /* Get the address of a packet buffer corresponding to a given buffer head */
101 #define PKTBUF_ADDR(head) (void *)((unsigned long)(MEM) | (head)->base)
104 /* The LANCE Rx and Tx ring descriptors. */
105 struct lance_rx_head {
106 unsigned short base; /* Low word of base addr */
107 volatile unsigned char flag;
108 unsigned char base_hi; /* High word of base addr (unused) */
109 short buf_length; /* This length is 2s complement! */
110 volatile short msg_length; /* This length is "normal". */
113 struct lance_tx_head {
114 unsigned short base; /* Low word of base addr */
115 volatile unsigned char flag;
116 unsigned char base_hi; /* High word of base addr (unused) */
117 short length; /* Length is 2s complement! */
118 volatile short misc;
121 /* The LANCE initialization block, described in databook. */
122 struct lance_init_block {
123 unsigned short mode; /* Pre-set mode */
124 unsigned char hwaddr[6]; /* Physical ethernet address */
125 unsigned int filter[2]; /* Multicast filter (unused). */
126 /* Receive and transmit ring base, along with length bits. */
127 unsigned short rdra;
128 unsigned short rlen;
129 unsigned short tdra;
130 unsigned short tlen;
131 unsigned short pad[4]; /* is thie needed? */
134 /* The whole layout of the Lance shared memory */
135 struct lance_memory {
136 struct lance_init_block init;
137 struct lance_tx_head tx_head[TX_RING_SIZE];
138 struct lance_rx_head rx_head[RX_RING_SIZE];
139 char rx_data[RX_RING_SIZE][PKT_BUF_SZ];
140 char tx_data[RX_RING_SIZE][PKT_BUF_SZ];
143 /* The driver's private device structure */
145 struct lance_private {
146 volatile unsigned short *iobase;
147 struct lance_memory *mem;
148 int new_rx, new_tx; /* The next free ring entry */
149 int old_tx, old_rx; /* ring entry to be processed */
150 struct net_device_stats stats;
151 /* These two must be ints for set_bit() */
152 int tx_full;
153 int lock;
156 /* I/O register access macros */
158 #define MEM lp->mem
159 #define DREG lp->iobase[0]
160 #define AREG lp->iobase[1]
161 #define REGA(a) ( AREG = (a), DREG )
163 /* Definitions for the Lance */
165 /* tx_head flags */
166 #define TMD1_ENP 0x01 /* end of packet */
167 #define TMD1_STP 0x02 /* start of packet */
168 #define TMD1_DEF 0x04 /* deferred */
169 #define TMD1_ONE 0x08 /* one retry needed */
170 #define TMD1_MORE 0x10 /* more than one retry needed */
171 #define TMD1_ERR 0x40 /* error summary */
172 #define TMD1_OWN 0x80 /* ownership (set: chip owns) */
174 #define TMD1_OWN_CHIP TMD1_OWN
175 #define TMD1_OWN_HOST 0
177 /* tx_head misc field */
178 #define TMD3_TDR 0x03FF /* Time Domain Reflectometry counter */
179 #define TMD3_RTRY 0x0400 /* failed after 16 retries */
180 #define TMD3_LCAR 0x0800 /* carrier lost */
181 #define TMD3_LCOL 0x1000 /* late collision */
182 #define TMD3_UFLO 0x4000 /* underflow (late memory) */
183 #define TMD3_BUFF 0x8000 /* buffering error (no ENP) */
185 /* rx_head flags */
186 #define RMD1_ENP 0x01 /* end of packet */
187 #define RMD1_STP 0x02 /* start of packet */
188 #define RMD1_BUFF 0x04 /* buffer error */
189 #define RMD1_CRC 0x08 /* CRC error */
190 #define RMD1_OFLO 0x10 /* overflow */
191 #define RMD1_FRAM 0x20 /* framing error */
192 #define RMD1_ERR 0x40 /* error summary */
193 #define RMD1_OWN 0x80 /* ownership (set: ship owns) */
195 #define RMD1_OWN_CHIP RMD1_OWN
196 #define RMD1_OWN_HOST 0
198 /* register names */
199 #define CSR0 0 /* mode/status */
200 #define CSR1 1 /* init block addr (low) */
201 #define CSR2 2 /* init block addr (high) */
202 #define CSR3 3 /* misc */
203 #define CSR8 8 /* address filter */
204 #define CSR15 15 /* promiscuous mode */
206 /* CSR0 */
207 /* (R=readable, W=writeable, S=set on write, C=clear on write) */
208 #define CSR0_INIT 0x0001 /* initialize (RS) */
209 #define CSR0_STRT 0x0002 /* start (RS) */
210 #define CSR0_STOP 0x0004 /* stop (RS) */
211 #define CSR0_TDMD 0x0008 /* transmit demand (RS) */
212 #define CSR0_TXON 0x0010 /* transmitter on (R) */
213 #define CSR0_RXON 0x0020 /* receiver on (R) */
214 #define CSR0_INEA 0x0040 /* interrupt enable (RW) */
215 #define CSR0_INTR 0x0080 /* interrupt active (R) */
216 #define CSR0_IDON 0x0100 /* initialization done (RC) */
217 #define CSR0_TINT 0x0200 /* transmitter interrupt (RC) */
218 #define CSR0_RINT 0x0400 /* receiver interrupt (RC) */
219 #define CSR0_MERR 0x0800 /* memory error (RC) */
220 #define CSR0_MISS 0x1000 /* missed frame (RC) */
221 #define CSR0_CERR 0x2000 /* carrier error (no heartbeat :-) (RC) */
222 #define CSR0_BABL 0x4000 /* babble: tx-ed too many bits (RC) */
223 #define CSR0_ERR 0x8000 /* error (RC) */
225 /* CSR3 */
226 #define CSR3_BCON 0x0001 /* byte control */
227 #define CSR3_ACON 0x0002 /* ALE control */
228 #define CSR3_BSWP 0x0004 /* byte swap (1=big endian) */
230 /***************************** Prototypes *****************************/
232 static int lance_probe( struct net_device *dev);
233 static int lance_open( struct net_device *dev );
234 static void lance_init_ring( struct net_device *dev );
235 static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev );
236 static void lance_interrupt( int irq, void *dev_id, struct pt_regs *fp );
237 static int lance_rx( struct net_device *dev );
238 static int lance_close( struct net_device *dev );
239 static struct net_device_stats *lance_get_stats( struct net_device *dev );
240 static void set_multicast_list( struct net_device *dev );
242 /************************* End of Prototypes **************************/
244 int __init sun3lance_probe( struct net_device *dev )
246 static int found = 0;
248 if(found)
249 return(ENODEV);
251 if (lance_probe(dev)) {
252 found = 1;
253 return( 0 );
256 return( ENODEV );
259 static int __init lance_probe( struct net_device *dev)
261 unsigned long ioaddr, iopte;
263 struct lance_private *lp;
264 int i;
265 static int did_version = 0;
266 int found = 0;
267 volatile unsigned short *ioaddr_probe;
268 unsigned short tmp1, tmp2;
270 /* LANCE_OBIO can be found within the IO pmeg with some effort */
271 for(ioaddr = 0xfe00000; ioaddr < (0xfe00000 +
272 SUN3_PMEG_SIZE); ioaddr += SUN3_PTE_SIZE) {
274 iopte = sun3_get_pte(ioaddr);
275 if(!(iopte & SUN3_PAGE_TYPE_IO)) /* this an io page? */
276 continue;
278 if(((iopte & SUN3_PAGE_PGNUM_MASK) << PAGE_SHIFT) ==
279 LANCE_OBIO) {
280 found = 1;
281 break;
285 if(!found)
286 return 0;
288 /* test to see if there's really a lance here */
289 /* (CSRO_INIT shouldn't be readable) */
291 ioaddr_probe = (volatile unsigned short *)ioaddr;
292 tmp1 = ioaddr_probe[0];
293 tmp2 = ioaddr_probe[1];
295 ioaddr_probe[1] = CSR0;
296 ioaddr_probe[0] = CSR0_INIT | CSR0_STOP;
298 if(ioaddr_probe[0] != CSR0_STOP) {
299 ioaddr_probe[0] = tmp1;
300 ioaddr_probe[1] = tmp2;
302 return 0;
305 init_etherdev( dev, sizeof(struct lance_private) );
306 if (!dev->priv)
307 dev->priv = kmalloc( sizeof(struct lance_private), GFP_KERNEL );
308 lp = (struct lance_private *)dev->priv;
309 MEM = (struct lance_memory *)sun3_dvma_malloc(sizeof(struct
310 lance_memory));
311 lp->iobase = (volatile unsigned short *)ioaddr;
312 dev->base_addr = (unsigned long)ioaddr; /* informational only */
314 REGA(CSR0) = CSR0_STOP;
316 request_irq(LANCE_IRQ, lance_interrupt, 0, "SUN3 Lance", dev);
317 dev->irq = (unsigned short)LANCE_IRQ;
320 printk("%s: SUN3 Lance at io %#lx, mem %#lx, irq %d, hwaddr ",
321 dev->name,
322 (unsigned long)ioaddr,
323 (unsigned long)MEM,
324 dev->irq);
326 /* copy in the ethernet address from the prom */
327 for(i = 0; i < 6 ; i++)
328 dev->dev_addr[i] = idprom->id_ethaddr[i];
330 /* tell the card it's ether address, bytes swapped */
331 MEM->init.hwaddr[0] = dev->dev_addr[1];
332 MEM->init.hwaddr[1] = dev->dev_addr[0];
333 MEM->init.hwaddr[2] = dev->dev_addr[3];
334 MEM->init.hwaddr[3] = dev->dev_addr[2];
335 MEM->init.hwaddr[4] = dev->dev_addr[5];
336 MEM->init.hwaddr[5] = dev->dev_addr[4];
338 for( i = 0; i < 6; ++i )
339 printk( "%02x%s", dev->dev_addr[i], (i < 5) ? ":" : "\n" );
341 MEM->init.mode = 0x0000;
342 MEM->init.filter[0] = 0x00000000;
343 MEM->init.filter[1] = 0x00000000;
344 MEM->init.rdra = sun3_dvma_vtop(MEM->rx_head);
345 MEM->init.rlen = (RX_LOG_RING_SIZE << 13) |
346 (sun3_dvma_vtop(MEM->rx_head) >> 16);
347 MEM->init.tdra = sun3_dvma_vtop(MEM->tx_head);
348 MEM->init.tlen = (TX_LOG_RING_SIZE << 13) |
349 (sun3_dvma_vtop(MEM->tx_head) >> 16);
351 DPRINTK(2, ("initaddr: %08lx rx_ring: %08lx tx_ring: %08lx\n",
352 sun3_dvma_vtop(&(MEM->init)), sun3_dvma_vtop(MEM->rx_head),
353 (sun3_dvma_vtop(MEM->tx_head))));
356 if (did_version++ == 0)
357 DPRINTK( 1, ( version ));
359 /* The LANCE-specific entries in the device structure. */
360 dev->open = &lance_open;
361 dev->hard_start_xmit = &lance_start_xmit;
362 dev->stop = &lance_close;
363 dev->get_stats = &lance_get_stats;
364 dev->set_multicast_list = &set_multicast_list;
365 dev->set_mac_address = 0;
366 // KLUDGE -- REMOVE ME
367 set_bit(__LINK_STATE_PRESENT, &dev->state);
370 memset( &lp->stats, 0, sizeof(lp->stats) );
372 return 1;
375 static int lance_open( struct net_device *dev )
377 struct lance_private *lp = (struct lance_private *)dev->priv;
378 int i;
380 DPRINTK( 2, ( "%s: lance_open()\n", dev->name ));
382 REGA(CSR0) = CSR0_STOP;
384 /* tell the lance the address of its init block */
385 REGA(CSR1) = sun3_dvma_vtop(&(MEM->init));
386 REGA(CSR2) = sun3_dvma_vtop(&(MEM->init)) >> 16;
388 lance_init_ring(dev);
390 /* Re-initialize the LANCE, and start it when done. */
392 REGA(CSR3) = CSR3_BSWP;
394 /* From now on, AREG is kept to point to CSR0 */
395 REGA(CSR0) = CSR0_INIT;
397 i = 1000000;
398 while (--i > 0)
399 if (DREG & CSR0_IDON)
400 break;
401 if (i < 0 || (DREG & CSR0_ERR)) {
402 DPRINTK( 2, ( "lance_open(): opening %s failed, i=%d, csr0=%04x\n",
403 dev->name, i, DREG ));
404 DREG = CSR0_STOP;
405 return( -EIO );
408 DREG = CSR0_IDON | CSR0_STRT | CSR0_INEA;
410 netif_start_queue(dev);
412 DPRINTK( 2, ( "%s: LANCE is open, csr0 %04x\n", dev->name, DREG ));
413 MOD_INC_USE_COUNT;
415 return( 0 );
419 /* Initialize the LANCE Rx and Tx rings. */
421 static void lance_init_ring( struct net_device *dev )
423 struct lance_private *lp = (struct lance_private *)dev->priv;
424 int i;
426 lp->lock = 0;
427 lp->tx_full = 0;
428 lp->new_rx = lp->new_tx = 0;
429 lp->old_rx = lp->old_tx = 0;
431 for( i = 0; i < TX_RING_SIZE; i++ ) {
432 MEM->tx_head[i].base = sun3_dvma_vtop(MEM->tx_data[i]);
433 MEM->tx_head[i].flag = 0;
434 MEM->tx_head[i].base_hi =
435 (sun3_dvma_vtop(MEM->tx_data[i])) >>16;
436 MEM->tx_head[i].length = 0;
437 MEM->tx_head[i].misc = 0;
440 for( i = 0; i < RX_RING_SIZE; i++ ) {
441 MEM->rx_head[i].base = sun3_dvma_vtop(MEM->rx_data[i]);
442 MEM->rx_head[i].flag = TMD1_OWN_CHIP;
443 MEM->rx_head[i].base_hi =
444 (sun3_dvma_vtop(MEM->rx_data[i])) >> 16;
445 MEM->rx_head[i].buf_length = -PKT_BUF_SZ | 0xf000;
446 MEM->rx_head[i].msg_length = 0;
452 static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev )
454 struct lance_private *lp = (struct lance_private *)dev->priv;
455 int entry, len;
456 struct lance_tx_head *head;
457 unsigned long flags;
459 /* Transmitter timeout, serious problems. */
460 if (netif_queue_stopped(dev)) {
461 int tickssofar = jiffies - dev->trans_start;
462 if (tickssofar < 20)
463 return( 1 );
465 DPRINTK( 1, ( "%s: transmit timed out, status %04x, resetting.\n",
466 dev->name, DREG ));
467 DREG = CSR0_STOP;
469 * Always set BSWP after a STOP as STOP puts it back into
470 * little endian mode.
472 REGA(CSR3) = CSR3_BSWP;
473 lp->stats.tx_errors++;
475 if(lance_debug >= 2) {
476 int i;
477 printk("Ring data: old_tx %d new_tx %d%s new_rx %d\n",
478 lp->old_tx, lp->new_tx,
479 lp->tx_full ? " (full)" : "",
480 lp->new_rx );
481 for( i = 0 ; i < RX_RING_SIZE; i++ )
482 printk( "rx #%d: base=%04x blen=%04x mlen=%04x\n",
483 i, MEM->rx_head[i].base,
484 -MEM->rx_head[i].buf_length,
485 MEM->rx_head[i].msg_length);
486 for( i = 0 ; i < TX_RING_SIZE; i++ )
487 printk("tx #%d: base=%04x len=%04x misc=%04x\n",
488 i, MEM->tx_head[i].base,
489 -MEM->tx_head[i].length,
490 MEM->tx_head[i].misc );
493 lance_init_ring(dev);
494 REGA( CSR0 ) = CSR0_INEA | CSR0_INIT | CSR0_STRT;
496 netif_start_queue(dev);
497 dev->trans_start = jiffies;
499 return 0;
503 /* Block a timer-based transmit from overlapping. This could better be
504 done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
506 /* Block a timer-based transmit from overlapping with us by
507 stopping the queue for a bit... */
509 netif_stop_queue(dev);
511 if (test_and_set_bit( 0, (void*)&lp->lock ) != 0) {
512 printk( "%s: tx queue lock!.\n", dev->name);
513 /* don't clear dev->tbusy flag. */
514 return 1;
517 AREG = CSR0;
518 // DPRINTK( 2, ( "%s: lance_start_xmit() called, csr0 %4.4x.\n",
519 // dev->name, DREG ));
522 /* Fill in a Tx ring entry */
523 #if 0
524 if (lance_debug >= 3) {
525 u_char *p;
526 int i;
527 printk( "%s: TX pkt %d type 0x%04x from ", dev->name,
528 lp->new_tx, ((u_short *)skb->data)[6]);
529 for( p = &((u_char *)skb->data)[6], i = 0; i < 6; i++ )
530 printk("%02x%s", *p++, i != 5 ? ":" : "" );
531 printk(" to ");
532 for( p = (u_char *)skb->data, i = 0; i < 6; i++ )
533 printk("%02x%s", *p++, i != 5 ? ":" : "" );
534 printk(" data at 0x%08x len %d\n", (int)skb->data,
535 (int)skb->len );
537 #endif
538 /* We're not prepared for the int until the last flags are set/reset.
539 * And the int may happen already after setting the OWN_CHIP... */
540 save_and_cli(flags);
542 /* Mask to ring buffer boundary. */
543 entry = lp->new_tx;
544 head = &(MEM->tx_head[entry]);
546 /* Caution: the write order is important here, set the "ownership" bits
547 * last.
550 /* the sun3's lance needs it's buffer padded to the minimum
551 size */
552 len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
554 // head->length = -len;
555 head->length = (-len) | 0xf000;
556 head->misc = 0;
558 memcpy( PKTBUF_ADDR(head), (void *)skb->data, skb->len );
559 head->flag = TMD1_OWN_CHIP | TMD1_ENP | TMD1_STP;
560 lp->new_tx = (lp->new_tx + 1) & TX_RING_MOD_MASK;
561 lp->stats.tx_bytes += skb->len;
563 /* Trigger an immediate send poll. */
564 REGA(CSR0) = CSR0_INEA | CSR0_TDMD;
565 dev->trans_start = jiffies;
566 dev_kfree_skb( skb );
568 lp->lock = 0;
569 if ((MEM->tx_head[(entry+1) & TX_RING_MOD_MASK].flag & TMD1_OWN) ==
570 TMD1_OWN_HOST)
571 netif_start_queue(dev);
573 restore_flags(flags);
575 return 0;
578 /* The LANCE interrupt handler. */
580 static void lance_interrupt( int irq, void *dev_id, struct pt_regs *fp)
582 struct net_device *dev = dev_id;
583 struct lance_private *lp = dev->priv;
584 int csr0;
585 static int in_interrupt = 0;
587 if (dev == NULL) {
588 DPRINTK( 1, ( "lance_interrupt(): invalid dev_id\n" ));
589 return;
592 if (in_interrupt)
593 DPRINTK( 2, ( "%s: Re-entering the interrupt handler.\n", dev->name ));
594 in_interrupt = 1;
596 still_more:
598 AREG = CSR0;
599 csr0 = DREG;
601 /* ack interrupts */
602 DREG = csr0 & (CSR0_TINT | CSR0_RINT);
604 /* clear errors */
605 if(csr0 & CSR0_ERR)
606 DREG = CSR0_BABL | CSR0_MERR | CSR0_CERR | CSR0_MISS;
609 DPRINTK( 2, ( "%s: interrupt csr0=%04x new csr=%04x.\n",
610 dev->name, csr0, DREG ));
612 if (csr0 & CSR0_TINT) { /* Tx-done interrupt */
613 int old_tx = lp->old_tx;
615 // if(lance_debug >= 3) {
616 // int i;
618 // printk("%s: tx int\n", dev->name);
620 // for(i = 0; i < TX_RING_SIZE; i++)
621 // printk("ring %d flag=%04x\n", i,
622 // MEM->tx_head[i].flag);
623 // }
625 while( old_tx != lp->new_tx) {
626 struct lance_tx_head *head = &(MEM->tx_head[old_tx]);
628 DPRINTK(3, ("on tx_ring %d\n", old_tx));
630 if (head->flag & TMD1_OWN_CHIP)
631 break; /* It still hasn't been Txed */
633 if (head->flag & TMD1_ERR) {
634 int status = head->misc;
635 lp->stats.tx_errors++;
636 if (status & TMD3_RTRY) lp->stats.tx_aborted_errors++;
637 if (status & TMD3_LCAR) lp->stats.tx_carrier_errors++;
638 if (status & TMD3_LCOL) lp->stats.tx_window_errors++;
639 if (status & (TMD3_UFLO | TMD3_BUFF)) {
640 lp->stats.tx_fifo_errors++;
641 printk("%s: Tx FIFO error\n",
642 dev->name);
643 REGA(CSR0) = CSR0_STOP;
644 REGA(CSR3) = CSR3_BSWP;
645 lance_init_ring(dev);
646 REGA(CSR0) = CSR0_STRT | CSR0_INEA;
647 return;
649 } else if(head->flag & (TMD1_ENP | TMD1_STP)) {
651 head->flag &= ~(TMD1_ENP | TMD1_STP);
652 if(head->flag & (TMD1_ONE | TMD1_MORE))
653 lp->stats.collisions++;
655 lp->stats.tx_packets++;
656 DPRINTK(3, ("cleared tx ring %d\n", old_tx));
658 old_tx = (old_tx +1) & TX_RING_MOD_MASK;
661 lp->old_tx = old_tx;
665 if (netif_queue_stopped(dev)) {
666 /* The ring is no longer full, clear tbusy. */
667 netif_start_queue(dev);
668 netif_wake_queue(dev);
671 if (csr0 & CSR0_RINT) /* Rx interrupt */
672 lance_rx( dev );
674 /* Log misc errors. */
675 if (csr0 & CSR0_BABL) lp->stats.tx_errors++; /* Tx babble. */
676 if (csr0 & CSR0_MISS) lp->stats.rx_errors++; /* Missed a Rx frame. */
677 if (csr0 & CSR0_MERR) {
678 DPRINTK( 1, ( "%s: Bus master arbitration failure (?!?), "
679 "status %04x.\n", dev->name, csr0 ));
680 /* Restart the chip. */
681 REGA(CSR0) = CSR0_STOP;
682 REGA(CSR3) = CSR3_BSWP;
683 lance_init_ring(dev);
684 REGA(CSR0) = CSR0_STRT | CSR0_INEA;
688 /* Clear any other interrupt, and set interrupt enable. */
689 // DREG = CSR0_BABL | CSR0_CERR | CSR0_MISS | CSR0_MERR |
690 // CSR0_IDON | CSR0_INEA;
692 REGA(CSR0) = CSR0_INEA;
694 if(DREG & (CSR0_RINT | CSR0_TINT)) {
695 DPRINTK(2, ("restarting interrupt, csr0=%#04x\n", DREG));
696 goto still_more;
699 DPRINTK( 2, ( "%s: exiting interrupt, csr0=%#04x.\n",
700 dev->name, DREG ));
701 in_interrupt = 0;
702 return;
705 /* get packet, toss into skbuff */
706 static int lance_rx( struct net_device *dev )
708 struct lance_private *lp = (struct lance_private *)dev->priv;
709 int entry = lp->new_rx;
711 /* If we own the next entry, it's a new packet. Send it up. */
712 while( (MEM->rx_head[entry].flag & RMD1_OWN) == RMD1_OWN_HOST ) {
713 struct lance_rx_head *head = &(MEM->rx_head[entry]);
714 int status = head->flag;
716 if (status != (RMD1_ENP|RMD1_STP)) { /* There was an error. */
717 /* There is a tricky error noted by John Murphy,
718 <murf@perftech.com> to Russ Nelson: Even with
719 full-sized buffers it's possible for a jabber packet to use two
720 buffers, with only the last correctly noting the error. */
721 if (status & RMD1_ENP) /* Only count a general error at the */
722 lp->stats.rx_errors++; /* end of a packet.*/
723 if (status & RMD1_FRAM) lp->stats.rx_frame_errors++;
724 if (status & RMD1_OFLO) lp->stats.rx_over_errors++;
725 if (status & RMD1_CRC) lp->stats.rx_crc_errors++;
726 if (status & RMD1_BUFF) lp->stats.rx_fifo_errors++;
727 head->flag &= (RMD1_ENP|RMD1_STP);
728 } else {
729 /* Malloc up new buffer, compatible with net-3. */
730 // short pkt_len = head->msg_length;// & 0xfff;
731 short pkt_len = (head->msg_length & 0xfff) - 4;
732 struct sk_buff *skb;
734 if (pkt_len < 60) {
735 printk( "%s: Runt packet!\n", dev->name );
736 lp->stats.rx_errors++;
738 else {
739 skb = dev_alloc_skb( pkt_len+2 );
740 if (skb == NULL) {
741 DPRINTK( 1, ( "%s: Memory squeeze, deferring packet.\n",
742 dev->name ));
744 lp->stats.rx_dropped++;
745 head->msg_length = 0;
746 head->flag |= RMD1_OWN_CHIP;
747 lp->new_rx = (lp->new_rx+1) &
748 RX_RING_MOD_MASK;
751 #if 0
752 if (lance_debug >= 3) {
753 u_char *data = PKTBUF_ADDR(head), *p;
754 printk( "%s: RX pkt %d type 0x%04x from ", dev->name, entry, ((u_short *)data)[6]);
755 for( p = &data[6], i = 0; i < 6; i++ )
756 printk("%02x%s", *p++, i != 5 ? ":" : "" );
757 printk(" to ");
758 for( p = data, i = 0; i < 6; i++ )
759 printk("%02x%s", *p++, i != 5 ? ":" : "" );
760 printk(" data %02x %02x %02x %02x %02x %02x %02x %02x "
761 "len %d at %08x\n",
762 data[15], data[16], data[17], data[18],
763 data[19], data[20], data[21], data[22],
764 pkt_len, data);
766 #endif
767 if (lance_debug >= 3) {
768 u_char *data = PKTBUF_ADDR(head);
769 printk( "%s: RX pkt %d type 0x%04x len %d\n ", dev->name, entry, ((u_short *)data)[6], pkt_len);
773 skb->dev = dev;
774 skb_reserve( skb, 2 ); /* 16 byte align */
775 skb_put( skb, pkt_len ); /* Make room */
776 // memcpy( skb->data, PKTBUF_ADDR(head), pkt_len );
777 eth_copy_and_sum(skb,
778 PKTBUF_ADDR(head),
779 pkt_len, 0);
781 skb->protocol = eth_type_trans( skb, dev );
782 netif_rx( skb );
783 lp->stats.rx_packets++;
784 lp->stats.rx_bytes += skb->len;
788 // head->buf_length = -PKT_BUF_SZ | 0xf000;
789 head->msg_length = 0;
790 head->flag = RMD1_OWN_CHIP;
792 entry = lp->new_rx = (lp->new_rx +1) & RX_RING_MOD_MASK;
795 /* From lance.c (Donald Becker): */
796 /* We should check that at least two ring entries are free.
797 If not, we should free one and mark stats->rx_dropped++. */
799 return 0;
803 static int lance_close( struct net_device *dev )
805 struct lance_private *lp = (struct lance_private *)dev->priv;
807 netif_stop_queue(dev);
809 AREG = CSR0;
811 DPRINTK( 2, ( "%s: Shutting down ethercard, status was %2.2x.\n",
812 dev->name, DREG ));
814 /* We stop the LANCE here -- it occasionally polls
815 memory if we don't. */
816 DREG = CSR0_STOP;
818 MOD_DEC_USE_COUNT;
819 return 0;
823 static struct net_device_stats *lance_get_stats( struct net_device *dev )
825 struct lance_private *lp = (struct lance_private *)dev->priv;
827 return &lp->stats;
831 /* Set or clear the multicast filter for this adaptor.
832 num_addrs == -1 Promiscuous mode, receive all packets
833 num_addrs == 0 Normal mode, clear multicast list
834 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
835 best-effort filtering.
838 /* completely untested on a sun3 */
839 static void set_multicast_list( struct net_device *dev )
841 struct lance_private *lp = (struct lance_private *)dev->priv;
843 if(netif_queue_stopped(dev))
844 /* Only possible if board is already started */
845 return;
847 /* We take the simple way out and always enable promiscuous mode. */
848 DREG = CSR0_STOP; /* Temporarily stop the lance. */
850 if (dev->flags & IFF_PROMISC) {
851 /* Log any net taps. */
852 DPRINTK( 1, ( "%s: Promiscuous mode enabled.\n", dev->name ));
853 REGA( CSR15 ) = 0x8000; /* Set promiscuous mode */
854 } else {
855 short multicast_table[4];
856 int num_addrs = dev->mc_count;
857 int i;
858 /* We don't use the multicast table, but rely on upper-layer
859 * filtering. */
860 memset( multicast_table, (num_addrs == 0) ? 0 : -1,
861 sizeof(multicast_table) );
862 for( i = 0; i < 4; i++ )
863 REGA( CSR8+i ) = multicast_table[i];
864 REGA( CSR15 ) = 0; /* Unset promiscuous mode */
868 * Always set BSWP after a STOP as STOP puts it back into
869 * little endian mode.
871 REGA( CSR3 ) = CSR3_BSWP;
873 /* Resume normal operation and reset AREG to CSR0 */
874 REGA( CSR0 ) = CSR0_IDON | CSR0_INEA | CSR0_STRT;
878 #ifdef MODULE
879 static char devicename[9] = { 0, };
881 static struct net_device sun3lance_dev =
883 devicename, /* filled in by register_netdev() */
884 0, 0, 0, 0, /* memory */
885 0, 0, /* base, irq */
886 0, 0, 0, NULL, sun3lance_probe,
889 int init_module(void)
891 int err;
893 if ((err = register_netdev( &sun3lance_dev ))) {
894 if (err == -EIO) {
895 printk( "SUN3 Lance not detected. Module not loaded.\n");
897 return( err );
899 return( 0 );
902 void cleanup_module(void)
904 unregister_netdev( &sun3lance_dev );
907 #endif /* MODULE */