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