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