phy: Add LSI ET1011C PHY driver
[pandora-kernel.git] / drivers / net / ariadne.c
1 /*
2  *  Amiga Linux/m68k Ariadne Ethernet Driver
3  *
4  *  © Copyright 1995-2003 by Geert Uytterhoeven (geert@linux-m68k.org)
5  *                           Peter De Schrijver (p2@mind.be)
6  *
7  *  ---------------------------------------------------------------------------
8  *
9  *  This program is based on
10  *
11  *      lance.c:        An AMD LANCE ethernet driver for linux.
12  *                      Written 1993-94 by Donald Becker.
13  *
14  *      Am79C960:       PCnet(tm)-ISA Single-Chip Ethernet Controller
15  *                      Advanced Micro Devices
16  *                      Publication #16907, Rev. B, Amendment/0, May 1994
17  *
18  *      MC68230:        Parallel Interface/Timer (PI/T)
19  *                      Motorola Semiconductors, December, 1983
20  *
21  *  ---------------------------------------------------------------------------
22  *
23  *  This file is subject to the terms and conditions of the GNU General Public
24  *  License.  See the file COPYING in the main directory of the Linux
25  *  distribution for more details.
26  *
27  *  ---------------------------------------------------------------------------
28  *
29  *  The Ariadne is a Zorro-II board made by Village Tronic. It contains:
30  *
31  *      - an Am79C960 PCnet-ISA Single-Chip Ethernet Controller with both
32  *        10BASE-2 (thin coax) and 10BASE-T (UTP) connectors
33  *
34  *      - an MC68230 Parallel Interface/Timer configured as 2 parallel ports
35  */
36
37 #include <linux/module.h>
38 #include <linux/stddef.h>
39 #include <linux/kernel.h>
40 #include <linux/string.h>
41 #include <linux/errno.h>
42 #include <linux/ioport.h>
43 #include <linux/slab.h>
44 #include <linux/netdevice.h>
45 #include <linux/etherdevice.h>
46 #include <linux/interrupt.h>
47 #include <linux/skbuff.h>
48 #include <linux/init.h>
49 #include <linux/zorro.h>
50 #include <linux/bitops.h>
51
52 #include <asm/amigaints.h>
53 #include <asm/amigahw.h>
54 #include <asm/irq.h>
55
56 #include "ariadne.h"
57
58
59 #ifdef ARIADNE_DEBUG
60 int ariadne_debug = ARIADNE_DEBUG;
61 #else
62 int ariadne_debug = 1;
63 #endif
64
65
66     /*
67      *  Macros to Fix Endianness problems
68      */
69
70                                 /* Swap the Bytes in a WORD */
71 #define swapw(x)        (((x>>8)&0x00ff)|((x<<8)&0xff00))
72                                 /* Get the Low BYTE in a WORD */
73 #define lowb(x)         (x&0xff)
74                                 /* Get the Swapped High WORD in a LONG */
75 #define swhighw(x)      ((((x)>>8)&0xff00)|(((x)>>24)&0x00ff))
76                                 /* Get the Swapped Low WORD in a LONG */
77 #define swloww(x)       ((((x)<<8)&0xff00)|(((x)>>8)&0x00ff))
78
79
80     /*
81      *  Transmit/Receive Ring Definitions
82      */
83
84 #define TX_RING_SIZE    5
85 #define RX_RING_SIZE    16
86
87 #define PKT_BUF_SIZE    1520
88
89
90     /*
91      *  Private Device Data
92      */
93
94 struct ariadne_private {
95     volatile struct TDRE *tx_ring[TX_RING_SIZE];
96     volatile struct RDRE *rx_ring[RX_RING_SIZE];
97     volatile u_short *tx_buff[TX_RING_SIZE];
98     volatile u_short *rx_buff[RX_RING_SIZE];
99     int cur_tx, cur_rx;                 /* The next free ring entry */
100     int dirty_tx;                       /* The ring entries to be free()ed. */
101     char tx_full;
102 };
103
104
105     /*
106      *  Structure Created in the Ariadne's RAM Buffer
107      */
108
109 struct lancedata {
110     struct TDRE tx_ring[TX_RING_SIZE];
111     struct RDRE rx_ring[RX_RING_SIZE];
112     u_short tx_buff[TX_RING_SIZE][PKT_BUF_SIZE/sizeof(u_short)];
113     u_short rx_buff[RX_RING_SIZE][PKT_BUF_SIZE/sizeof(u_short)];
114 };
115
116 static int ariadne_open(struct net_device *dev);
117 static void ariadne_init_ring(struct net_device *dev);
118 static int ariadne_start_xmit(struct sk_buff *skb, struct net_device *dev);
119 static void ariadne_tx_timeout(struct net_device *dev);
120 static int ariadne_rx(struct net_device *dev);
121 static void ariadne_reset(struct net_device *dev);
122 static irqreturn_t ariadne_interrupt(int irq, void *data);
123 static int ariadne_close(struct net_device *dev);
124 static struct net_device_stats *ariadne_get_stats(struct net_device *dev);
125 #ifdef HAVE_MULTICAST
126 static void set_multicast_list(struct net_device *dev);
127 #endif
128
129
130 static void memcpyw(volatile u_short *dest, u_short *src, int len)
131 {
132     while (len >= 2) {
133         *(dest++) = *(src++);
134         len -= 2;
135     }
136     if (len == 1)
137         *dest = (*(u_char *)src)<<8;
138 }
139
140
141 static int __devinit ariadne_init_one(struct zorro_dev *z,
142                                       const struct zorro_device_id *ent);
143 static void __devexit ariadne_remove_one(struct zorro_dev *z);
144
145
146 static struct zorro_device_id ariadne_zorro_tbl[] __devinitdata = {
147     { ZORRO_PROD_VILLAGE_TRONIC_ARIADNE },
148     { 0 }
149 };
150
151 static struct zorro_driver ariadne_driver = {
152     .name       = "ariadne",
153     .id_table   = ariadne_zorro_tbl,
154     .probe      = ariadne_init_one,
155     .remove     = __devexit_p(ariadne_remove_one),
156 };
157
158 static int __devinit ariadne_init_one(struct zorro_dev *z,
159                                       const struct zorro_device_id *ent)
160 {
161     unsigned long board = z->resource.start;
162     unsigned long base_addr = board+ARIADNE_LANCE;
163     unsigned long mem_start = board+ARIADNE_RAM;
164     struct resource *r1, *r2;
165     struct net_device *dev;
166     struct ariadne_private *priv;
167     int err;
168
169     r1 = request_mem_region(base_addr, sizeof(struct Am79C960), "Am79C960");
170     if (!r1)
171         return -EBUSY;
172     r2 = request_mem_region(mem_start, ARIADNE_RAM_SIZE, "RAM");
173     if (!r2) {
174         release_resource(r1);
175         return -EBUSY;
176     }
177
178     dev = alloc_etherdev(sizeof(struct ariadne_private));
179     if (dev == NULL) {
180         release_resource(r1);
181         release_resource(r2);
182         return -ENOMEM;
183     }
184
185     priv = netdev_priv(dev);
186
187     r1->name = dev->name;
188     r2->name = dev->name;
189
190     dev->dev_addr[0] = 0x00;
191     dev->dev_addr[1] = 0x60;
192     dev->dev_addr[2] = 0x30;
193     dev->dev_addr[3] = (z->rom.er_SerialNumber>>16) & 0xff;
194     dev->dev_addr[4] = (z->rom.er_SerialNumber>>8) & 0xff;
195     dev->dev_addr[5] = z->rom.er_SerialNumber & 0xff;
196     dev->base_addr = ZTWO_VADDR(base_addr);
197     dev->mem_start = ZTWO_VADDR(mem_start);
198     dev->mem_end = dev->mem_start+ARIADNE_RAM_SIZE;
199
200     dev->open = &ariadne_open;
201     dev->stop = &ariadne_close;
202     dev->hard_start_xmit = &ariadne_start_xmit;
203     dev->tx_timeout = &ariadne_tx_timeout;
204     dev->watchdog_timeo = 5*HZ;
205     dev->get_stats = &ariadne_get_stats;
206     dev->set_multicast_list = &set_multicast_list;
207
208     err = register_netdev(dev);
209     if (err) {
210         release_resource(r1);
211         release_resource(r2);
212         free_netdev(dev);
213         return err;
214     }
215     zorro_set_drvdata(z, dev);
216
217     printk(KERN_INFO "%s: Ariadne at 0x%08lx, Ethernet Address %pM\n",
218            dev->name, board, dev->dev_addr);
219
220     return 0;
221 }
222
223
224 static int ariadne_open(struct net_device *dev)
225 {
226     volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
227     u_short in;
228     u_long version;
229     int i;
230
231     /* Reset the LANCE */
232     in = lance->Reset;
233
234     /* Stop the LANCE */
235     lance->RAP = CSR0;          /* PCnet-ISA Controller Status */
236     lance->RDP = STOP;
237
238     /* Check the LANCE version */
239     lance->RAP = CSR88;         /* Chip ID */
240     version = swapw(lance->RDP);
241     lance->RAP = CSR89;         /* Chip ID */
242     version |= swapw(lance->RDP)<<16;
243     if ((version & 0x00000fff) != 0x00000003) {
244         printk(KERN_WARNING "ariadne_open: Couldn't find AMD Ethernet Chip\n");
245         return -EAGAIN;
246     }
247     if ((version & 0x0ffff000) != 0x00003000) {
248         printk(KERN_WARNING "ariadne_open: Couldn't find Am79C960 (Wrong part "
249                "number = %ld)\n", (version & 0x0ffff000)>>12);
250         return -EAGAIN;
251     }
252 #if 0
253     printk(KERN_DEBUG "ariadne_open: Am79C960 (PCnet-ISA) Revision %ld\n",
254            (version & 0xf0000000)>>28);
255 #endif
256
257     ariadne_init_ring(dev);
258
259     /* Miscellaneous Stuff */
260     lance->RAP = CSR3;          /* Interrupt Masks and Deferral Control */
261     lance->RDP = 0x0000;
262     lance->RAP = CSR4;          /* Test and Features Control */
263     lance->RDP = DPOLL|APAD_XMT|MFCOM|RCVCCOM|TXSTRTM|JABM;
264
265     /* Set the Multicast Table */
266     lance->RAP = CSR8;          /* Logical Address Filter, LADRF[15:0] */
267     lance->RDP = 0x0000;
268     lance->RAP = CSR9;          /* Logical Address Filter, LADRF[31:16] */
269     lance->RDP = 0x0000;
270     lance->RAP = CSR10;         /* Logical Address Filter, LADRF[47:32] */
271     lance->RDP = 0x0000;
272     lance->RAP = CSR11;         /* Logical Address Filter, LADRF[63:48] */
273     lance->RDP = 0x0000;
274
275     /* Set the Ethernet Hardware Address */
276     lance->RAP = CSR12;         /* Physical Address Register, PADR[15:0] */
277     lance->RDP = ((u_short *)&dev->dev_addr[0])[0];
278     lance->RAP = CSR13;         /* Physical Address Register, PADR[31:16] */
279     lance->RDP = ((u_short *)&dev->dev_addr[0])[1];
280     lance->RAP = CSR14;         /* Physical Address Register, PADR[47:32] */
281     lance->RDP = ((u_short *)&dev->dev_addr[0])[2];
282
283     /* Set the Init Block Mode */
284     lance->RAP = CSR15;         /* Mode Register */
285     lance->RDP = 0x0000;
286
287     /* Set the Transmit Descriptor Ring Pointer */
288     lance->RAP = CSR30;         /* Base Address of Transmit Ring */
289     lance->RDP = swloww(ARIADNE_RAM+offsetof(struct lancedata, tx_ring));
290     lance->RAP = CSR31;         /* Base Address of transmit Ring */
291     lance->RDP = swhighw(ARIADNE_RAM+offsetof(struct lancedata, tx_ring));
292
293     /* Set the Receive Descriptor Ring Pointer */
294     lance->RAP = CSR24;         /* Base Address of Receive Ring */
295     lance->RDP = swloww(ARIADNE_RAM+offsetof(struct lancedata, rx_ring));
296     lance->RAP = CSR25;         /* Base Address of Receive Ring */
297     lance->RDP = swhighw(ARIADNE_RAM+offsetof(struct lancedata, rx_ring));
298
299     /* Set the Number of RX and TX Ring Entries */
300     lance->RAP = CSR76;         /* Receive Ring Length */
301     lance->RDP = swapw(((u_short)-RX_RING_SIZE));
302     lance->RAP = CSR78;         /* Transmit Ring Length */
303     lance->RDP = swapw(((u_short)-TX_RING_SIZE));
304
305     /* Enable Media Interface Port Auto Select (10BASE-2/10BASE-T) */
306     lance->RAP = ISACSR2;       /* Miscellaneous Configuration */
307     lance->IDP = ASEL;
308
309     /* LED Control */
310     lance->RAP = ISACSR5;       /* LED1 Status */
311     lance->IDP = PSE|XMTE;
312     lance->RAP = ISACSR6;       /* LED2 Status */
313     lance->IDP = PSE|COLE;
314     lance->RAP = ISACSR7;       /* LED3 Status */
315     lance->IDP = PSE|RCVE;
316
317     netif_start_queue(dev);
318
319     i = request_irq(IRQ_AMIGA_PORTS, ariadne_interrupt, IRQF_SHARED,
320                     dev->name, dev);
321     if (i) return i;
322
323     lance->RAP = CSR0;          /* PCnet-ISA Controller Status */
324     lance->RDP = INEA|STRT;
325
326     return 0;
327 }
328
329
330 static void ariadne_init_ring(struct net_device *dev)
331 {
332     struct ariadne_private *priv = netdev_priv(dev);
333     volatile struct lancedata *lancedata = (struct lancedata *)dev->mem_start;
334     int i;
335
336     netif_stop_queue(dev);
337
338     priv->tx_full = 0;
339     priv->cur_rx = priv->cur_tx = 0;
340     priv->dirty_tx = 0;
341
342     /* Set up TX Ring */
343     for (i = 0; i < TX_RING_SIZE; i++) {
344         volatile struct TDRE *t = &lancedata->tx_ring[i];
345         t->TMD0 = swloww(ARIADNE_RAM+offsetof(struct lancedata, tx_buff[i]));
346         t->TMD1 = swhighw(ARIADNE_RAM+offsetof(struct lancedata, tx_buff[i])) |
347                   TF_STP | TF_ENP;
348         t->TMD2 = swapw((u_short)-PKT_BUF_SIZE);
349         t->TMD3 = 0;
350         priv->tx_ring[i] = &lancedata->tx_ring[i];
351         priv->tx_buff[i] = lancedata->tx_buff[i];
352 #if 0
353         printk(KERN_DEBUG "TX Entry %2d at %p, Buf at %p\n", i,
354                &lancedata->tx_ring[i], lancedata->tx_buff[i]);
355 #endif
356     }
357
358     /* Set up RX Ring */
359     for (i = 0; i < RX_RING_SIZE; i++) {
360         volatile struct RDRE *r = &lancedata->rx_ring[i];
361         r->RMD0 = swloww(ARIADNE_RAM+offsetof(struct lancedata, rx_buff[i]));
362         r->RMD1 = swhighw(ARIADNE_RAM+offsetof(struct lancedata, rx_buff[i])) |
363                   RF_OWN;
364         r->RMD2 = swapw((u_short)-PKT_BUF_SIZE);
365         r->RMD3 = 0x0000;
366         priv->rx_ring[i] = &lancedata->rx_ring[i];
367         priv->rx_buff[i] = lancedata->rx_buff[i];
368 #if 0
369         printk(KERN_DEBUG "RX Entry %2d at %p, Buf at %p\n", i,
370                &lancedata->rx_ring[i], lancedata->rx_buff[i]);
371 #endif
372     }
373 }
374
375
376 static int ariadne_close(struct net_device *dev)
377 {
378     volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
379
380     netif_stop_queue(dev);
381
382     lance->RAP = CSR112;        /* Missed Frame Count */
383     dev->stats.rx_missed_errors = swapw(lance->RDP);
384     lance->RAP = CSR0;          /* PCnet-ISA Controller Status */
385
386     if (ariadne_debug > 1) {
387         printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
388                dev->name, lance->RDP);
389         printk(KERN_DEBUG "%s: %lu packets missed\n", dev->name,
390                dev->stats.rx_missed_errors);
391     }
392
393     /* We stop the LANCE here -- it occasionally polls memory if we don't. */
394     lance->RDP = STOP;
395
396     free_irq(IRQ_AMIGA_PORTS, dev);
397
398     return 0;
399 }
400
401
402 static inline void ariadne_reset(struct net_device *dev)
403 {
404     volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
405
406     lance->RAP = CSR0;  /* PCnet-ISA Controller Status */
407     lance->RDP = STOP;
408     ariadne_init_ring(dev);
409     lance->RDP = INEA|STRT;
410     netif_start_queue(dev);
411 }
412
413
414 static irqreturn_t ariadne_interrupt(int irq, void *data)
415 {
416     struct net_device *dev = (struct net_device *)data;
417     volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
418     struct ariadne_private *priv;
419     int csr0, boguscnt;
420     int handled = 0;
421
422     if (dev == NULL) {
423         printk(KERN_WARNING "ariadne_interrupt(): irq for unknown device.\n");
424         return IRQ_NONE;
425     }
426
427     lance->RAP = CSR0;                  /* PCnet-ISA Controller Status */
428
429     if (!(lance->RDP & INTR))           /* Check if any interrupt has been */
430         return IRQ_NONE;                /* generated by the board. */
431
432     priv = netdev_priv(dev);
433
434     boguscnt = 10;
435     while ((csr0 = lance->RDP) & (ERR|RINT|TINT) && --boguscnt >= 0) {
436         /* Acknowledge all of the current interrupt sources ASAP. */
437         lance->RDP = csr0 & ~(INEA|TDMD|STOP|STRT|INIT);
438
439 #if 0
440         if (ariadne_debug > 5) {
441             printk(KERN_DEBUG "%s: interrupt  csr0=%#2.2x new csr=%#2.2x.",
442                    dev->name, csr0, lance->RDP);
443             printk("[");
444             if (csr0 & INTR)
445                 printk(" INTR");
446             if (csr0 & INEA)
447                 printk(" INEA");
448             if (csr0 & RXON)
449                 printk(" RXON");
450             if (csr0 & TXON)
451                 printk(" TXON");
452             if (csr0 & TDMD)
453                 printk(" TDMD");
454             if (csr0 & STOP)
455                 printk(" STOP");
456             if (csr0 & STRT)
457                 printk(" STRT");
458             if (csr0 & INIT)
459                 printk(" INIT");
460             if (csr0 & ERR)
461                 printk(" ERR");
462             if (csr0 & BABL)
463                 printk(" BABL");
464             if (csr0 & CERR)
465                 printk(" CERR");
466             if (csr0 & MISS)
467                 printk(" MISS");
468             if (csr0 & MERR)
469                 printk(" MERR");
470             if (csr0 & RINT)
471                 printk(" RINT");
472             if (csr0 & TINT)
473                 printk(" TINT");
474             if (csr0 & IDON)
475                 printk(" IDON");
476             printk(" ]\n");
477         }
478 #endif
479
480         if (csr0 & RINT) {      /* Rx interrupt */
481             handled = 1;
482             ariadne_rx(dev);
483         }
484
485         if (csr0 & TINT) {      /* Tx-done interrupt */
486             int dirty_tx = priv->dirty_tx;
487
488             handled = 1;
489             while (dirty_tx < priv->cur_tx) {
490                 int entry = dirty_tx % TX_RING_SIZE;
491                 int status = lowb(priv->tx_ring[entry]->TMD1);
492
493                 if (status & TF_OWN)
494                     break;      /* It still hasn't been Txed */
495
496                 priv->tx_ring[entry]->TMD1 &= 0xff00;
497
498                 if (status & TF_ERR) {
499                     /* There was an major error, log it. */
500                     int err_status = priv->tx_ring[entry]->TMD3;
501                     dev->stats.tx_errors++;
502                     if (err_status & EF_RTRY)
503                         dev->stats.tx_aborted_errors++;
504                     if (err_status & EF_LCAR)
505                         dev->stats.tx_carrier_errors++;
506                     if (err_status & EF_LCOL)
507                         dev->stats.tx_window_errors++;
508                     if (err_status & EF_UFLO) {
509                         /* Ackk!  On FIFO errors the Tx unit is turned off! */
510                         dev->stats.tx_fifo_errors++;
511                         /* Remove this verbosity later! */
512                         printk(KERN_ERR "%s: Tx FIFO error! Status %4.4x.\n",
513                                dev->name, csr0);
514                         /* Restart the chip. */
515                         lance->RDP = STRT;
516                     }
517                 } else {
518                     if (status & (TF_MORE|TF_ONE))
519                         dev->stats.collisions++;
520                     dev->stats.tx_packets++;
521                 }
522                 dirty_tx++;
523             }
524
525 #ifndef final_version
526             if (priv->cur_tx - dirty_tx >= TX_RING_SIZE) {
527                 printk(KERN_ERR "out-of-sync dirty pointer, %d vs. %d, "
528                        "full=%d.\n", dirty_tx, priv->cur_tx, priv->tx_full);
529                 dirty_tx += TX_RING_SIZE;
530             }
531 #endif
532
533             if (priv->tx_full && netif_queue_stopped(dev) &&
534                 dirty_tx > priv->cur_tx - TX_RING_SIZE + 2) {
535                 /* The ring is no longer full. */
536                 priv->tx_full = 0;
537                 netif_wake_queue(dev);
538             }
539
540             priv->dirty_tx = dirty_tx;
541         }
542
543         /* Log misc errors. */
544         if (csr0 & BABL) {
545             handled = 1;
546             dev->stats.tx_errors++;     /* Tx babble. */
547         }
548         if (csr0 & MISS) {
549             handled = 1;
550             dev->stats.rx_errors++;     /* Missed a Rx frame. */
551         }
552         if (csr0 & MERR) {
553             handled = 1;
554             printk(KERN_ERR "%s: Bus master arbitration failure, status "
555                    "%4.4x.\n", dev->name, csr0);
556             /* Restart the chip. */
557             lance->RDP = STRT;
558         }
559     }
560
561     /* Clear any other interrupt, and set interrupt enable. */
562     lance->RAP = CSR0;          /* PCnet-ISA Controller Status */
563     lance->RDP = INEA|BABL|CERR|MISS|MERR|IDON;
564
565 #if 0
566     if (ariadne_debug > 4)
567         printk(KERN_DEBUG "%s: exiting interrupt, csr%d=%#4.4x.\n", dev->name,
568                lance->RAP, lance->RDP);
569 #endif
570     return IRQ_RETVAL(handled);
571 }
572
573
574 static void ariadne_tx_timeout(struct net_device *dev)
575 {
576     volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
577
578     printk(KERN_ERR "%s: transmit timed out, status %4.4x, resetting.\n",
579            dev->name, lance->RDP);
580     ariadne_reset(dev);
581     netif_wake_queue(dev);
582 }
583
584
585 static int ariadne_start_xmit(struct sk_buff *skb, struct net_device *dev)
586 {
587     struct ariadne_private *priv = netdev_priv(dev);
588     volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
589     int entry;
590     unsigned long flags;
591     int len = skb->len;
592
593 #if 0
594     if (ariadne_debug > 3) {
595         lance->RAP = CSR0;      /* PCnet-ISA Controller Status */
596         printk(KERN_DEBUG "%s: ariadne_start_xmit() called, csr0 %4.4x.\n",
597                dev->name, lance->RDP);
598         lance->RDP = 0x0000;
599     }
600 #endif
601
602     /* FIXME: is the 79C960 new enough to do its own padding right ? */
603     if (skb->len < ETH_ZLEN)
604     {
605         if (skb_padto(skb, ETH_ZLEN))
606             return 0;
607         len = ETH_ZLEN;
608     }
609
610     /* Fill in a Tx ring entry */
611
612 #if 0
613 {
614     printk(KERN_DEBUG "TX pkt type 0x%04x from %pM to %pM "
615            " data 0x%08x len %d\n",
616            ((u_short *)skb->data)[6],
617            skb->data + 6, skb->data,
618            (int)skb->data, (int)skb->len);
619 }
620 #endif
621
622     local_irq_save(flags);
623
624     entry = priv->cur_tx % TX_RING_SIZE;
625
626     /* Caution: the write order is important here, set the base address with
627                 the "ownership" bits last. */
628
629     priv->tx_ring[entry]->TMD2 = swapw((u_short)-skb->len);
630     priv->tx_ring[entry]->TMD3 = 0x0000;
631     memcpyw(priv->tx_buff[entry], (u_short *)skb->data, len);
632
633 #if 0
634     {
635         int i, len;
636
637         len = skb->len > 64 ? 64 : skb->len;
638         len >>= 1;
639         for (i = 0; i < len; i += 8) {
640             int j;
641             printk(KERN_DEBUG "%04x:", i);
642             for (j = 0; (j < 8) && ((i+j) < len); j++) {
643                 if (!(j & 1))
644                     printk(" ");
645                 printk("%04x", priv->tx_buff[entry][i+j]);
646             }
647             printk("\n");
648         }
649     }
650 #endif
651
652     priv->tx_ring[entry]->TMD1 = (priv->tx_ring[entry]->TMD1&0xff00)|TF_OWN|TF_STP|TF_ENP;
653
654     dev_kfree_skb(skb);
655
656     priv->cur_tx++;
657     if ((priv->cur_tx >= TX_RING_SIZE) && (priv->dirty_tx >= TX_RING_SIZE)) {
658
659 #if 0
660         printk(KERN_DEBUG "*** Subtracting TX_RING_SIZE from cur_tx (%d) and "
661                "dirty_tx (%d)\n", priv->cur_tx, priv->dirty_tx);
662 #endif
663
664         priv->cur_tx -= TX_RING_SIZE;
665         priv->dirty_tx -= TX_RING_SIZE;
666     }
667     dev->stats.tx_bytes += len;
668
669     /* Trigger an immediate send poll. */
670     lance->RAP = CSR0;          /* PCnet-ISA Controller Status */
671     lance->RDP = INEA|TDMD;
672
673     dev->trans_start = jiffies;
674
675     if (lowb(priv->tx_ring[(entry+1) % TX_RING_SIZE]->TMD1) != 0) {
676         netif_stop_queue(dev);
677         priv->tx_full = 1;
678     }
679     local_irq_restore(flags);
680
681     return 0;
682 }
683
684
685 static int ariadne_rx(struct net_device *dev)
686 {
687     struct ariadne_private *priv = netdev_priv(dev);
688     int entry = priv->cur_rx % RX_RING_SIZE;
689     int i;
690
691     /* If we own the next entry, it's a new packet. Send it up. */
692     while (!(lowb(priv->rx_ring[entry]->RMD1) & RF_OWN)) {
693         int status = lowb(priv->rx_ring[entry]->RMD1);
694
695         if (status != (RF_STP|RF_ENP)) {        /* There was an error. */
696             /* There is a tricky error noted by John Murphy,
697                 <murf@perftech.com> to Russ Nelson: Even with full-sized
698                 buffers it's possible for a jabber packet to use two
699                 buffers, with only the last correctly noting the error. */
700             if (status & RF_ENP)
701                 /* Only count a general error at the end of a packet.*/
702                 dev->stats.rx_errors++;
703             if (status & RF_FRAM)
704                 dev->stats.rx_frame_errors++;
705             if (status & RF_OFLO)
706                 dev->stats.rx_over_errors++;
707             if (status & RF_CRC)
708                 dev->stats.rx_crc_errors++;
709             if (status & RF_BUFF)
710                 dev->stats.rx_fifo_errors++;
711             priv->rx_ring[entry]->RMD1 &= 0xff00|RF_STP|RF_ENP;
712         } else {
713             /* Malloc up new buffer, compatible with net-3. */
714             short pkt_len = swapw(priv->rx_ring[entry]->RMD3);
715             struct sk_buff *skb;
716
717             skb = dev_alloc_skb(pkt_len+2);
718             if (skb == NULL) {
719                 printk(KERN_WARNING "%s: Memory squeeze, deferring packet.\n",
720                        dev->name);
721                 for (i = 0; i < RX_RING_SIZE; i++)
722                     if (lowb(priv->rx_ring[(entry+i) % RX_RING_SIZE]->RMD1) & RF_OWN)
723                         break;
724
725                 if (i > RX_RING_SIZE-2) {
726                     dev->stats.rx_dropped++;
727                     priv->rx_ring[entry]->RMD1 |= RF_OWN;
728                     priv->cur_rx++;
729                 }
730                 break;
731             }
732
733
734             skb_reserve(skb,2);         /* 16 byte align */
735             skb_put(skb,pkt_len);       /* Make room */
736             skb_copy_to_linear_data(skb, (char *)priv->rx_buff[entry], pkt_len);
737             skb->protocol=eth_type_trans(skb,dev);
738 #if 0
739 {
740             printk(KERN_DEBUG "RX pkt type 0x%04x from ",
741                    ((u_short *)skb->data)[6]);
742             {
743                 u_char *ptr = &((u_char *)skb->data)[6];
744                 printk("%pM", ptr);
745             }
746             printk(" to ");
747             {
748                 u_char *ptr = (u_char *)skb->data;
749                 printk("%pM", ptr);
750             }
751             printk(" data 0x%08x len %d\n", (int)skb->data, (int)skb->len);
752 }
753 #endif
754
755             netif_rx(skb);
756             dev->stats.rx_packets++;
757             dev->stats.rx_bytes += pkt_len;
758         }
759
760         priv->rx_ring[entry]->RMD1 |= RF_OWN;
761         entry = (++priv->cur_rx) % RX_RING_SIZE;
762     }
763
764     priv->cur_rx = priv->cur_rx % RX_RING_SIZE;
765
766     /* We should check that at least two ring entries are free.  If not,
767        we should free one and mark stats->rx_dropped++. */
768
769     return 0;
770 }
771
772
773 static struct net_device_stats *ariadne_get_stats(struct net_device *dev)
774 {
775     volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
776     short saved_addr;
777     unsigned long flags;
778
779     local_irq_save(flags);
780     saved_addr = lance->RAP;
781     lance->RAP = CSR112;                /* Missed Frame Count */
782     dev->stats.rx_missed_errors = swapw(lance->RDP);
783     lance->RAP = saved_addr;
784     local_irq_restore(flags);
785
786     return &dev->stats;
787 }
788
789
790 /* Set or clear the multicast filter for this adaptor.
791     num_addrs == -1     Promiscuous mode, receive all packets
792     num_addrs == 0      Normal mode, clear multicast list
793     num_addrs > 0       Multicast mode, receive normal and MC packets, and do
794                         best-effort filtering.
795  */
796 static void set_multicast_list(struct net_device *dev)
797 {
798     volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
799
800     if (!netif_running(dev))
801         return;
802
803     netif_stop_queue(dev);
804
805     /* We take the simple way out and always enable promiscuous mode. */
806     lance->RAP = CSR0;                  /* PCnet-ISA Controller Status */
807     lance->RDP = STOP;                  /* Temporarily stop the lance. */
808     ariadne_init_ring(dev);
809
810     if (dev->flags & IFF_PROMISC) {
811         lance->RAP = CSR15;             /* Mode Register */
812         lance->RDP = PROM;              /* Set promiscuous mode */
813     } else {
814         short multicast_table[4];
815         int num_addrs = dev->mc_count;
816         int i;
817         /* We don't use the multicast table, but rely on upper-layer filtering. */
818         memset(multicast_table, (num_addrs == 0) ? 0 : -1,
819                sizeof(multicast_table));
820         for (i = 0; i < 4; i++) {
821             lance->RAP = CSR8+(i<<8);   /* Logical Address Filter */
822             lance->RDP = swapw(multicast_table[i]);
823         }
824         lance->RAP = CSR15;             /* Mode Register */
825         lance->RDP = 0x0000;            /* Unset promiscuous mode */
826     }
827
828     lance->RAP = CSR0;                  /* PCnet-ISA Controller Status */
829     lance->RDP = INEA|STRT|IDON;        /* Resume normal operation. */
830
831     netif_wake_queue(dev);
832 }
833
834
835 static void __devexit ariadne_remove_one(struct zorro_dev *z)
836 {
837     struct net_device *dev = zorro_get_drvdata(z);
838
839     unregister_netdev(dev);
840     release_mem_region(ZTWO_PADDR(dev->base_addr), sizeof(struct Am79C960));
841     release_mem_region(ZTWO_PADDR(dev->mem_start), ARIADNE_RAM_SIZE);
842     free_netdev(dev);
843 }
844
845 static int __init ariadne_init_module(void)
846 {
847     return zorro_register_driver(&ariadne_driver);
848 }
849
850 static void __exit ariadne_cleanup_module(void)
851 {
852     zorro_unregister_driver(&ariadne_driver);
853 }
854
855 module_init(ariadne_init_module);
856 module_exit(ariadne_cleanup_module);
857
858 MODULE_LICENSE("GPL");