clean
[linux-2.4.21-pre4.git] / drivers / net / mace.c
1 /*
2  * Network device driver for the MACE ethernet controller on
3  * Apple Powermacs.  Assumes it's under a DBDMA controller.
4  *
5  * Copyright (C) 1996 Paul Mackerras.
6  */
7
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/version.h>
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/delay.h>
15 #include <linux/string.h>
16 #include <linux/timer.h>
17 #include <linux/init.h>
18 #include <linux/crc32.h>
19 #include <asm/prom.h>
20 #include <asm/dbdma.h>
21 #include <asm/io.h>
22 #include <asm/pgtable.h>
23 #include "mace.h"
24
25 static struct net_device *mace_devs;
26 static int port_aaui = -1;
27
28 #define N_RX_RING               8
29 #define N_TX_RING               6
30 #define MAX_TX_ACTIVE           1
31 #define NCMDS_TX                1       /* dma commands per element in tx ring */
32 #define RX_BUFLEN               (ETH_FRAME_LEN + 8)
33 #define TX_TIMEOUT              HZ      /* 1 second */
34
35 /* Chip rev needs workaround on HW & multicast addr change */
36 #define BROKEN_ADDRCHG_REV      0x0941
37
38 /* Bits in transmit DMA status */
39 #define TX_DMA_ERR              0x80
40
41 struct mace_data {
42     volatile struct mace *mace;
43     volatile struct dbdma_regs *tx_dma;
44     int tx_dma_intr;
45     volatile struct dbdma_regs *rx_dma;
46     int rx_dma_intr;
47     volatile struct dbdma_cmd *tx_cmds; /* xmit dma command list */
48     volatile struct dbdma_cmd *rx_cmds; /* recv dma command list */
49     struct sk_buff *rx_bufs[N_RX_RING];
50     int rx_fill;
51     int rx_empty;
52     struct sk_buff *tx_bufs[N_TX_RING];
53     int tx_fill;
54     int tx_empty;
55     unsigned char maccc;
56     unsigned char tx_fullup;
57     unsigned char tx_active;
58     unsigned char tx_bad_runt;
59     struct net_device_stats stats;
60     struct timer_list tx_timeout;
61     int timeout_active;
62     int port_aaui;
63     int chipid;
64     struct device_node* of_node;
65     struct net_device *next_mace;
66 };
67
68 /*
69  * Number of bytes of private data per MACE: allow enough for
70  * the rx and tx dma commands plus a branch dma command each,
71  * and another 16 bytes to allow us to align the dma command
72  * buffers on a 16 byte boundary.
73  */
74 #define PRIV_BYTES      (sizeof(struct mace_data) \
75         + (N_RX_RING + NCMDS_TX * N_TX_RING + 3) * sizeof(struct dbdma_cmd))
76
77 static int bitrev(int);
78 static int mace_probe(void);
79 static void mace_probe1(struct device_node *mace);
80 static int mace_open(struct net_device *dev);
81 static int mace_close(struct net_device *dev);
82 static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev);
83 static struct net_device_stats *mace_stats(struct net_device *dev);
84 static void mace_set_multicast(struct net_device *dev);
85 static void mace_reset(struct net_device *dev);
86 static int mace_set_address(struct net_device *dev, void *addr);
87 static void mace_interrupt(int irq, void *dev_id, struct pt_regs *regs);
88 static void mace_txdma_intr(int irq, void *dev_id, struct pt_regs *regs);
89 static void mace_rxdma_intr(int irq, void *dev_id, struct pt_regs *regs);
90 static void mace_set_timeout(struct net_device *dev);
91 static void mace_tx_timeout(unsigned long data);
92 static inline void dbdma_reset(volatile struct dbdma_regs *dma);
93 static inline void mace_clean_rings(struct mace_data *mp);
94 static void __mace_set_address(struct net_device *dev, void *addr);
95
96 /*
97  * If we can't get a skbuff when we need it, we use this area for DMA.
98  */
99 static unsigned char *dummy_buf;
100
101 /* Bit-reverse one byte of an ethernet hardware address. */
102 static inline int
103 bitrev(int b)
104 {
105     int d = 0, i;
106
107     for (i = 0; i < 8; ++i, b >>= 1)
108         d = (d << 1) | (b & 1);
109     return d;
110 }
111
112 static int __init mace_probe(void)
113 {
114         struct device_node *mace;
115
116         for (mace = find_devices("mace"); mace != NULL; mace = mace->next)
117                 mace_probe1(mace);
118         return mace_devs? 0: -ENODEV;
119 }
120
121 static void __init mace_probe1(struct device_node *mace)
122 {
123         int j, rev;
124         struct net_device *dev;
125         struct mace_data *mp;
126         unsigned char *addr;
127
128         if (mace->n_addrs != 3 || mace->n_intrs != 3) {
129                 printk(KERN_ERR "can't use MACE %s: need 3 addrs and 3 irqs\n",
130                        mace->full_name);
131                 return;
132         }
133
134         addr = get_property(mace, "mac-address", NULL);
135         if (addr == NULL) {
136                 addr = get_property(mace, "local-mac-address", NULL);
137                 if (addr == NULL) {
138                         printk(KERN_ERR "Can't get mac-address for MACE %s\n",
139                                mace->full_name);
140                         return;
141                 }
142         }
143
144         if (dummy_buf == NULL) {
145                 dummy_buf = kmalloc(RX_BUFLEN+2, GFP_KERNEL);
146                 if (dummy_buf == NULL) {
147                         printk(KERN_ERR "MACE: couldn't allocate dummy buffer\n");
148                         return;
149                 }
150         }
151
152         dev = init_etherdev(0, PRIV_BYTES);
153         if (!dev)
154                 return;
155         SET_MODULE_OWNER(dev);
156
157         mp = dev->priv;
158         mp->of_node = mace;
159         
160         if (!request_OF_resource(mace, 0, " (mace)")) {
161                 printk(KERN_ERR "MACE: can't request IO resource !\n");
162                 goto err_out;
163         }
164         if (!request_OF_resource(mace, 1, " (mace tx dma)")) {
165                 printk(KERN_ERR "MACE: can't request TX DMA resource !\n");
166                 goto err_out;
167         }
168
169         if (!request_OF_resource(mace, 2, " (mace tx dma)")) {
170                 printk(KERN_ERR "MACE: can't request RX DMA resource !\n");
171                 goto err_out;
172         }
173
174         dev->base_addr = mace->addrs[0].address;
175         mp->mace = (volatile struct mace *)
176                                 ioremap(mace->addrs[0].address, 0x1000);
177         dev->irq = mace->intrs[0].line;
178
179         printk(KERN_INFO "%s: MACE at", dev->name);
180         rev = addr[0] == 0 && addr[1] == 0xA0;
181         for (j = 0; j < 6; ++j) {
182                 dev->dev_addr[j] = rev? bitrev(addr[j]): addr[j];
183                 printk("%c%.2x", (j? ':': ' '), dev->dev_addr[j]);
184         }
185         mp->chipid = (in_8(&mp->mace->chipid_hi) << 8) |
186                         in_8(&mp->mace->chipid_lo);
187         printk(", chip revision %d.%d\n", mp->chipid >> 8, mp->chipid & 0xff);
188                 
189
190         mp = (struct mace_data *) dev->priv;
191         mp->maccc = ENXMT | ENRCV;
192         mp->tx_dma = (volatile struct dbdma_regs *)
193                 ioremap(mace->addrs[1].address, 0x1000);
194         mp->tx_dma_intr = mace->intrs[1].line;
195         mp->rx_dma = (volatile struct dbdma_regs *)
196                 ioremap(mace->addrs[2].address, 0x1000);
197         mp->rx_dma_intr = mace->intrs[2].line;
198
199         mp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(mp + 1);
200         mp->rx_cmds = mp->tx_cmds + NCMDS_TX * N_TX_RING + 1;
201
202         memset(&mp->stats, 0, sizeof(mp->stats));
203         memset((char *) mp->tx_cmds, 0,
204                (NCMDS_TX*N_TX_RING + N_RX_RING + 2) * sizeof(struct dbdma_cmd));
205         init_timer(&mp->tx_timeout);
206         mp->timeout_active = 0;
207
208         if (port_aaui >= 0)
209                 mp->port_aaui = port_aaui;
210         else {
211                 /* Apple Network Server uses the AAUI port */
212                 if (machine_is_compatible("AAPL,ShinerESB"))
213                         mp->port_aaui = 1;
214                 else {
215 #ifdef CONFIG_MACE_AAUI_PORT
216                         mp->port_aaui = 1;
217 #else
218                         mp->port_aaui = 0;
219 #endif                  
220                 }
221         }
222
223         dev->open = mace_open;
224         dev->stop = mace_close;
225         dev->hard_start_xmit = mace_xmit_start;
226         dev->get_stats = mace_stats;
227         dev->set_multicast_list = mace_set_multicast;
228         dev->set_mac_address = mace_set_address;
229
230         ether_setup(dev);
231
232         mace_reset(dev);
233
234         if (request_irq(dev->irq, mace_interrupt, 0, "MACE", dev))
235                 printk(KERN_ERR "MACE: can't get irq %d\n", dev->irq);
236         if (request_irq(mace->intrs[1].line, mace_txdma_intr, 0, "MACE-txdma",
237                         dev))
238                 printk(KERN_ERR "MACE: can't get irq %d\n", mace->intrs[1].line);
239         if (request_irq(mace->intrs[2].line, mace_rxdma_intr, 0, "MACE-rxdma",
240                         dev))
241                 printk(KERN_ERR "MACE: can't get irq %d\n", mace->intrs[2].line);
242
243         mp->next_mace = mace_devs;
244         mace_devs = dev;
245         return;
246         
247 err_out:
248         unregister_netdev(dev);
249         if (mp->of_node) {
250                 release_OF_resource(mp->of_node, 0);
251                 release_OF_resource(mp->of_node, 1);
252                 release_OF_resource(mp->of_node, 2);
253         }
254         kfree(dev);
255 }
256
257 static void dbdma_reset(volatile struct dbdma_regs *dma)
258 {
259     int i;
260
261     out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);
262
263     /*
264      * Yes this looks peculiar, but apparently it needs to be this
265      * way on some machines.
266      */
267     for (i = 200; i > 0; --i)
268         if (ld_le32(&dma->control) & RUN)
269             udelay(1);
270 }
271
272 static void mace_reset(struct net_device *dev)
273 {
274     struct mace_data *mp = (struct mace_data *) dev->priv;
275     volatile struct mace *mb = mp->mace;
276     int i;
277
278     /* soft-reset the chip */
279     i = 200;
280     while (--i) {
281         out_8(&mb->biucc, SWRST);
282         if (in_8(&mb->biucc) & SWRST) {
283             udelay(10);
284             continue;
285         }
286         break;
287     }
288     if (!i) {
289         printk(KERN_ERR "mace: cannot reset chip!\n");
290         return;
291     }
292
293     out_8(&mb->imr, 0xff);      /* disable all intrs for now */
294     i = in_8(&mb->ir);
295     out_8(&mb->maccc, 0);       /* turn off tx, rx */
296
297     out_8(&mb->biucc, XMTSP_64);
298     out_8(&mb->utr, RTRD);
299     out_8(&mb->fifocc, RCVFW_32 | XMTFW_16 | XMTFWU | RCVFWU | XMTBRST);
300     out_8(&mb->xmtfc, AUTO_PAD_XMIT); /* auto-pad short frames */
301     out_8(&mb->rcvfc, 0);
302
303     /* load up the hardware address */
304     __mace_set_address(dev, dev->dev_addr);
305
306     /* clear the multicast filter */
307     if (mp->chipid == BROKEN_ADDRCHG_REV)
308         out_8(&mb->iac, LOGADDR);
309     else {
310         out_8(&mb->iac, ADDRCHG | LOGADDR);
311         while ((in_8(&mb->iac) & ADDRCHG) != 0)
312                 ;
313     }
314     for (i = 0; i < 8; ++i)
315         out_8(&mb->ladrf, 0);
316
317     /* done changing address */
318     if (mp->chipid != BROKEN_ADDRCHG_REV)
319         out_8(&mb->iac, 0);
320
321     if (mp->port_aaui)
322         out_8(&mb->plscc, PORTSEL_AUI + ENPLSIO);
323     else
324         out_8(&mb->plscc, PORTSEL_GPSI + ENPLSIO);
325 }
326
327 static void __mace_set_address(struct net_device *dev, void *addr)
328 {
329     struct mace_data *mp = (struct mace_data *) dev->priv;
330     volatile struct mace *mb = mp->mace;
331     unsigned char *p = addr;
332     int i;
333
334     /* load up the hardware address */
335     if (mp->chipid == BROKEN_ADDRCHG_REV)
336         out_8(&mb->iac, PHYADDR);
337     else {
338         out_8(&mb->iac, ADDRCHG | PHYADDR);
339         while ((in_8(&mb->iac) & ADDRCHG) != 0)
340             ;
341     }
342     for (i = 0; i < 6; ++i)
343         out_8(&mb->padr, dev->dev_addr[i] = p[i]);
344     if (mp->chipid != BROKEN_ADDRCHG_REV)
345         out_8(&mb->iac, 0);
346 }
347
348 static int mace_set_address(struct net_device *dev, void *addr)
349 {
350     struct mace_data *mp = (struct mace_data *) dev->priv;
351     volatile struct mace *mb = mp->mace;
352     unsigned long flags;
353
354     save_flags(flags); cli();
355
356     __mace_set_address(dev, addr);
357
358     /* note: setting ADDRCHG clears ENRCV */
359     out_8(&mb->maccc, mp->maccc);
360
361     restore_flags(flags);
362     return 0;
363 }
364
365 static int mace_open(struct net_device *dev)
366 {
367     struct mace_data *mp = (struct mace_data *) dev->priv;
368     volatile struct mace *mb = mp->mace;
369     volatile struct dbdma_regs *rd = mp->rx_dma;
370     volatile struct dbdma_regs *td = mp->tx_dma;
371     volatile struct dbdma_cmd *cp;
372     int i;
373     struct sk_buff *skb;
374     unsigned char *data;
375
376     /* reset the chip */
377     mace_reset(dev);
378
379     /* initialize list of sk_buffs for receiving and set up recv dma */
380     mace_clean_rings(mp);
381     memset((char *)mp->rx_cmds, 0, N_RX_RING * sizeof(struct dbdma_cmd));
382     cp = mp->rx_cmds;
383     for (i = 0; i < N_RX_RING - 1; ++i) {
384         skb = dev_alloc_skb(RX_BUFLEN + 2);
385         if (skb == 0) {
386             data = dummy_buf;
387         } else {
388             skb_reserve(skb, 2);        /* so IP header lands on 4-byte bdry */
389             data = skb->data;
390         }
391         mp->rx_bufs[i] = skb;
392         st_le16(&cp->req_count, RX_BUFLEN);
393         st_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
394         st_le32(&cp->phy_addr, virt_to_bus(data));
395         cp->xfer_status = 0;
396         ++cp;
397     }
398     mp->rx_bufs[i] = 0;
399     st_le16(&cp->command, DBDMA_STOP);
400     mp->rx_fill = i;
401     mp->rx_empty = 0;
402
403     /* Put a branch back to the beginning of the receive command list */
404     ++cp;
405     st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
406     st_le32(&cp->cmd_dep, virt_to_bus(mp->rx_cmds));
407
408     /* start rx dma */
409     out_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
410     out_le32(&rd->cmdptr, virt_to_bus(mp->rx_cmds));
411     out_le32(&rd->control, (RUN << 16) | RUN);
412
413     /* put a branch at the end of the tx command list */
414     cp = mp->tx_cmds + NCMDS_TX * N_TX_RING;
415     st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
416     st_le32(&cp->cmd_dep, virt_to_bus(mp->tx_cmds));
417
418     /* reset tx dma */
419     out_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
420     out_le32(&td->cmdptr, virt_to_bus(mp->tx_cmds));
421     mp->tx_fill = 0;
422     mp->tx_empty = 0;
423     mp->tx_fullup = 0;
424     mp->tx_active = 0;
425     mp->tx_bad_runt = 0;
426
427     /* turn it on! */
428     out_8(&mb->maccc, mp->maccc);
429     /* enable all interrupts except receive interrupts */
430     out_8(&mb->imr, RCVINT);
431
432     return 0;
433 }
434
435 static inline void mace_clean_rings(struct mace_data *mp)
436 {
437     int i;
438
439     /* free some skb's */
440     for (i = 0; i < N_RX_RING; ++i) {
441         if (mp->rx_bufs[i] != 0) {
442             dev_kfree_skb(mp->rx_bufs[i]);
443             mp->rx_bufs[i] = 0;
444         }
445     }
446     for (i = mp->tx_empty; i != mp->tx_fill; ) {
447         dev_kfree_skb(mp->tx_bufs[i]);
448         if (++i >= N_TX_RING)
449             i = 0;
450     }
451 }
452
453 static int mace_close(struct net_device *dev)
454 {
455     struct mace_data *mp = (struct mace_data *) dev->priv;
456     volatile struct mace *mb = mp->mace;
457     volatile struct dbdma_regs *rd = mp->rx_dma;
458     volatile struct dbdma_regs *td = mp->tx_dma;
459
460     /* disable rx and tx */
461     out_8(&mb->maccc, 0);
462     out_8(&mb->imr, 0xff);              /* disable all intrs */
463
464     /* disable rx and tx dma */
465     st_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
466     st_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
467
468     mace_clean_rings(mp);
469
470     return 0;
471 }
472
473 static inline void mace_set_timeout(struct net_device *dev)
474 {
475     struct mace_data *mp = (struct mace_data *) dev->priv;
476     unsigned long flags;
477
478     save_flags(flags);
479     cli();
480     if (mp->timeout_active)
481         del_timer(&mp->tx_timeout);
482     mp->tx_timeout.expires = jiffies + TX_TIMEOUT;
483     mp->tx_timeout.function = mace_tx_timeout;
484     mp->tx_timeout.data = (unsigned long) dev;
485     add_timer(&mp->tx_timeout);
486     mp->timeout_active = 1;
487     restore_flags(flags);
488 }
489
490 static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev)
491 {
492     struct mace_data *mp = (struct mace_data *) dev->priv;
493     volatile struct dbdma_regs *td = mp->tx_dma;
494     volatile struct dbdma_cmd *cp, *np;
495     unsigned long flags;
496     int fill, next, len;
497
498     /* see if there's a free slot in the tx ring */
499     save_flags(flags); cli();
500     fill = mp->tx_fill;
501     next = fill + 1;
502     if (next >= N_TX_RING)
503         next = 0;
504     if (next == mp->tx_empty) {
505         netif_stop_queue(dev);
506         mp->tx_fullup = 1;
507         restore_flags(flags);
508         return 1;               /* can't take it at the moment */
509     }
510     restore_flags(flags);
511
512     /* partially fill in the dma command block */
513     len = skb->len;
514     if (len > ETH_FRAME_LEN) {
515         printk(KERN_DEBUG "mace: xmit frame too long (%d)\n", len);
516         len = ETH_FRAME_LEN;
517     }
518     mp->tx_bufs[fill] = skb;
519     cp = mp->tx_cmds + NCMDS_TX * fill;
520     st_le16(&cp->req_count, len);
521     st_le32(&cp->phy_addr, virt_to_bus(skb->data));
522
523     np = mp->tx_cmds + NCMDS_TX * next;
524     out_le16(&np->command, DBDMA_STOP);
525
526     /* poke the tx dma channel */
527     save_flags(flags);
528     cli();
529     mp->tx_fill = next;
530     if (!mp->tx_bad_runt && mp->tx_active < MAX_TX_ACTIVE) {
531         out_le16(&cp->xfer_status, 0);
532         out_le16(&cp->command, OUTPUT_LAST);
533         out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
534         ++mp->tx_active;
535         mace_set_timeout(dev);
536     }
537     if (++next >= N_TX_RING)
538         next = 0;
539     if (next == mp->tx_empty)
540         netif_stop_queue(dev);
541     restore_flags(flags);
542
543     return 0;
544 }
545
546 static struct net_device_stats *mace_stats(struct net_device *dev)
547 {
548     struct mace_data *p = (struct mace_data *) dev->priv;
549
550     return &p->stats;
551 }
552
553 static void mace_set_multicast(struct net_device *dev)
554 {
555     struct mace_data *mp = (struct mace_data *) dev->priv;
556     volatile struct mace *mb = mp->mace;
557     int i, j;
558     u32 crc;
559
560     mp->maccc &= ~PROM;
561     if (dev->flags & IFF_PROMISC) {
562         mp->maccc |= PROM;
563     } else {
564         unsigned char multicast_filter[8];
565         struct dev_mc_list *dmi = dev->mc_list;
566
567         if (dev->flags & IFF_ALLMULTI) {
568             for (i = 0; i < 8; i++)
569                 multicast_filter[i] = 0xff;
570         } else {
571             for (i = 0; i < 8; i++)
572                 multicast_filter[i] = 0;
573             for (i = 0; i < dev->mc_count; i++) {
574                 crc = ether_crc_le(6, dmi->dmi_addr);
575                 j = crc >> 26;  /* bit number in multicast_filter */
576                 multicast_filter[j >> 3] |= 1 << (j & 7);
577                 dmi = dmi->next;
578             }
579         }
580 #if 0
581         printk("Multicast filter :");
582         for (i = 0; i < 8; i++)
583             printk("%02x ", multicast_filter[i]);
584         printk("\n");
585 #endif
586
587         if (mp->chipid == BROKEN_ADDRCHG_REV)
588             out_8(&mb->iac, LOGADDR);
589         else {
590             out_8(&mb->iac, ADDRCHG | LOGADDR);
591             while ((in_8(&mb->iac) & ADDRCHG) != 0)
592                 ;
593         }
594         for (i = 0; i < 8; ++i)
595             out_8(&mb->ladrf, multicast_filter[i]);
596         if (mp->chipid != BROKEN_ADDRCHG_REV)
597             out_8(&mb->iac, 0);
598     }
599     /* reset maccc */
600     out_8(&mb->maccc, mp->maccc);
601 }
602
603 static void mace_handle_misc_intrs(struct mace_data *mp, int intr)
604 {
605     volatile struct mace *mb = mp->mace;
606     static int mace_babbles, mace_jabbers;
607
608     if (intr & MPCO)
609         mp->stats.rx_missed_errors += 256;
610     mp->stats.rx_missed_errors += in_8(&mb->mpc);   /* reading clears it */
611     if (intr & RNTPCO)
612         mp->stats.rx_length_errors += 256;
613     mp->stats.rx_length_errors += in_8(&mb->rntpc); /* reading clears it */
614     if (intr & CERR)
615         ++mp->stats.tx_heartbeat_errors;
616     if (intr & BABBLE)
617         if (mace_babbles++ < 4)
618             printk(KERN_DEBUG "mace: babbling transmitter\n");
619     if (intr & JABBER)
620         if (mace_jabbers++ < 4)
621             printk(KERN_DEBUG "mace: jabbering transceiver\n");
622 }
623
624 static void mace_interrupt(int irq, void *dev_id, struct pt_regs *regs)
625 {
626     struct net_device *dev = (struct net_device *) dev_id;
627     struct mace_data *mp = (struct mace_data *) dev->priv;
628     volatile struct mace *mb = mp->mace;
629     volatile struct dbdma_regs *td = mp->tx_dma;
630     volatile struct dbdma_cmd *cp;
631     int intr, fs, i, stat, x;
632     int xcount, dstat;
633     /* static int mace_last_fs, mace_last_xcount; */
634
635     intr = in_8(&mb->ir);               /* read interrupt register */
636     in_8(&mb->xmtrc);                   /* get retries */
637     mace_handle_misc_intrs(mp, intr);
638
639     i = mp->tx_empty;
640     while (in_8(&mb->pr) & XMTSV) {
641         del_timer(&mp->tx_timeout);
642         mp->timeout_active = 0;
643         /*
644          * Clear any interrupt indication associated with this status
645          * word.  This appears to unlatch any error indication from
646          * the DMA controller.
647          */
648         intr = in_8(&mb->ir);
649         if (intr != 0)
650             mace_handle_misc_intrs(mp, intr);
651         if (mp->tx_bad_runt) {
652             fs = in_8(&mb->xmtfs);
653             mp->tx_bad_runt = 0;
654             out_8(&mb->xmtfc, AUTO_PAD_XMIT);
655             continue;
656         }
657         dstat = ld_le32(&td->status);
658         /* stop DMA controller */
659         out_le32(&td->control, RUN << 16);
660         /*
661          * xcount is the number of complete frames which have been
662          * written to the fifo but for which status has not been read.
663          */
664         xcount = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
665         if (xcount == 0 || (dstat & DEAD)) {
666             /*
667              * If a packet was aborted before the DMA controller has
668              * finished transferring it, it seems that there are 2 bytes
669              * which are stuck in some buffer somewhere.  These will get
670              * transmitted as soon as we read the frame status (which
671              * reenables the transmit data transfer request).  Turning
672              * off the DMA controller and/or resetting the MACE doesn't
673              * help.  So we disable auto-padding and FCS transmission
674              * so the two bytes will only be a runt packet which should
675              * be ignored by other stations.
676              */
677             out_8(&mb->xmtfc, DXMTFCS);
678         }
679         fs = in_8(&mb->xmtfs);
680         if ((fs & XMTSV) == 0) {
681             printk(KERN_ERR "mace: xmtfs not valid! (fs=%x xc=%d ds=%x)\n",
682                    fs, xcount, dstat);
683             mace_reset(dev);
684                 /*
685                  * XXX mace likes to hang the machine after a xmtfs error.
686                  * This is hard to reproduce, reseting *may* help
687                  */
688         }
689         cp = mp->tx_cmds + NCMDS_TX * i;
690         stat = ld_le16(&cp->xfer_status);
691         if ((fs & (UFLO|LCOL|LCAR|RTRY)) || (dstat & DEAD) || xcount == 0) {
692             /*
693              * Check whether there were in fact 2 bytes written to
694              * the transmit FIFO.
695              */
696             udelay(1);
697             x = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
698             if (x != 0) {
699                 /* there were two bytes with an end-of-packet indication */
700                 mp->tx_bad_runt = 1;
701                 mace_set_timeout(dev);
702             } else {
703                 /*
704                  * Either there weren't the two bytes buffered up, or they
705                  * didn't have an end-of-packet indication.
706                  * We flush the transmit FIFO just in case (by setting the
707                  * XMTFWU bit with the transmitter disabled).
708                  */
709                 out_8(&mb->maccc, in_8(&mb->maccc) & ~ENXMT);
710                 out_8(&mb->fifocc, in_8(&mb->fifocc) | XMTFWU);
711                 udelay(1);
712                 out_8(&mb->maccc, in_8(&mb->maccc) | ENXMT);
713                 out_8(&mb->xmtfc, AUTO_PAD_XMIT);
714             }
715         }
716         /* dma should have finished */
717         if (i == mp->tx_fill) {
718             printk(KERN_DEBUG "mace: tx ring ran out? (fs=%x xc=%d ds=%x)\n",
719                    fs, xcount, dstat);
720             continue;
721         }
722         /* Update stats */
723         if (fs & (UFLO|LCOL|LCAR|RTRY)) {
724             ++mp->stats.tx_errors;
725             if (fs & LCAR)
726                 ++mp->stats.tx_carrier_errors;
727             if (fs & (UFLO|LCOL|RTRY))
728                 ++mp->stats.tx_aborted_errors;
729         } else {
730             mp->stats.tx_bytes += mp->tx_bufs[i]->len;
731             ++mp->stats.tx_packets;
732         }
733         dev_kfree_skb_irq(mp->tx_bufs[i]);
734         --mp->tx_active;
735         if (++i >= N_TX_RING)
736             i = 0;
737 #if 0
738         mace_last_fs = fs;
739         mace_last_xcount = xcount;
740 #endif
741     }
742
743     if (i != mp->tx_empty) {
744         mp->tx_fullup = 0;
745         netif_wake_queue(dev);
746     }
747     mp->tx_empty = i;
748     i += mp->tx_active;
749     if (i >= N_TX_RING)
750         i -= N_TX_RING;
751     if (!mp->tx_bad_runt && i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE) {
752         do {
753             /* set up the next one */
754             cp = mp->tx_cmds + NCMDS_TX * i;
755             out_le16(&cp->xfer_status, 0);
756             out_le16(&cp->command, OUTPUT_LAST);
757             ++mp->tx_active;
758             if (++i >= N_TX_RING)
759                 i = 0;
760         } while (i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE);
761         out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
762         mace_set_timeout(dev);
763     }
764 }
765
766 static void mace_tx_timeout(unsigned long data)
767 {
768     struct net_device *dev = (struct net_device *) data;
769     struct mace_data *mp = (struct mace_data *) dev->priv;
770     volatile struct mace *mb = mp->mace;
771     volatile struct dbdma_regs *td = mp->tx_dma;
772     volatile struct dbdma_regs *rd = mp->rx_dma;
773     volatile struct dbdma_cmd *cp;
774     unsigned long flags;
775     int i;
776
777     save_flags(flags);
778     cli();
779     mp->timeout_active = 0;
780     if (mp->tx_active == 0 && !mp->tx_bad_runt)
781         goto out;
782
783     /* update various counters */
784     mace_handle_misc_intrs(mp, in_8(&mb->ir));
785
786     cp = mp->tx_cmds + NCMDS_TX * mp->tx_empty;
787
788     /* turn off both tx and rx and reset the chip */
789     out_8(&mb->maccc, 0);
790     printk(KERN_ERR "mace: transmit timeout - resetting\n");
791     dbdma_reset(td);
792     mace_reset(dev);
793
794     /* restart rx dma */
795     cp = bus_to_virt(ld_le32(&rd->cmdptr));
796     dbdma_reset(rd);
797     out_le16(&cp->xfer_status, 0);
798     out_le32(&rd->cmdptr, virt_to_bus(cp));
799     out_le32(&rd->control, (RUN << 16) | RUN);
800
801     /* fix up the transmit side */
802     i = mp->tx_empty;
803     mp->tx_active = 0;
804     ++mp->stats.tx_errors;
805     if (mp->tx_bad_runt) {
806         mp->tx_bad_runt = 0;
807     } else if (i != mp->tx_fill) {
808         dev_kfree_skb(mp->tx_bufs[i]);
809         if (++i >= N_TX_RING)
810             i = 0;
811         mp->tx_empty = i;
812     }
813     mp->tx_fullup = 0;
814     netif_wake_queue(dev);
815     if (i != mp->tx_fill) {
816         cp = mp->tx_cmds + NCMDS_TX * i;
817         out_le16(&cp->xfer_status, 0);
818         out_le16(&cp->command, OUTPUT_LAST);
819         out_le32(&td->cmdptr, virt_to_bus(cp));
820         out_le32(&td->control, (RUN << 16) | RUN);
821         ++mp->tx_active;
822         mace_set_timeout(dev);
823     }
824
825     /* turn it back on */
826     out_8(&mb->imr, RCVINT);
827     out_8(&mb->maccc, mp->maccc);
828
829 out:
830     restore_flags(flags);
831 }
832
833 static void mace_txdma_intr(int irq, void *dev_id, struct pt_regs *regs)
834 {
835 }
836
837 static void mace_rxdma_intr(int irq, void *dev_id, struct pt_regs *regs)
838 {
839     struct net_device *dev = (struct net_device *) dev_id;
840     struct mace_data *mp = (struct mace_data *) dev->priv;
841     volatile struct dbdma_regs *rd = mp->rx_dma;
842     volatile struct dbdma_cmd *cp, *np;
843     int i, nb, stat, next;
844     struct sk_buff *skb;
845     unsigned frame_status;
846     static int mace_lost_status;
847     unsigned char *data;
848
849     for (i = mp->rx_empty; i != mp->rx_fill; ) {
850         cp = mp->rx_cmds + i;
851         stat = ld_le16(&cp->xfer_status);
852         if ((stat & ACTIVE) == 0) {
853             next = i + 1;
854             if (next >= N_RX_RING)
855                 next = 0;
856             np = mp->rx_cmds + next;
857             if (next != mp->rx_fill
858                 && (ld_le16(&np->xfer_status) & ACTIVE) != 0) {
859                 printk(KERN_DEBUG "mace: lost a status word\n");
860                 ++mace_lost_status;
861             } else
862                 break;
863         }
864         nb = ld_le16(&cp->req_count) - ld_le16(&cp->res_count);
865         out_le16(&cp->command, DBDMA_STOP);
866         /* got a packet, have a look at it */
867         skb = mp->rx_bufs[i];
868         if (skb == 0) {
869             ++mp->stats.rx_dropped;
870         } else if (nb > 8) {
871             data = skb->data;
872             frame_status = (data[nb-3] << 8) + data[nb-4];
873             if (frame_status & (RS_OFLO|RS_CLSN|RS_FRAMERR|RS_FCSERR)) {
874                 ++mp->stats.rx_errors;
875                 if (frame_status & RS_OFLO)
876                     ++mp->stats.rx_over_errors;
877                 if (frame_status & RS_FRAMERR)
878                     ++mp->stats.rx_frame_errors;
879                 if (frame_status & RS_FCSERR)
880                     ++mp->stats.rx_crc_errors;
881             } else {
882                 /* Mace feature AUTO_STRIP_RCV is on by default, dropping the
883                  * FCS on frames with 802.3 headers. This means that Ethernet
884                  * frames have 8 extra octets at the end, while 802.3 frames
885                  * have only 4. We need to correctly account for this. */
886                 if (*(unsigned short *)(data+12) < 1536) /* 802.3 header */
887                     nb -= 4;
888                 else    /* Ethernet header; mace includes FCS */
889                     nb -= 8;
890                 skb_put(skb, nb);
891                 skb->dev = dev;
892                 skb->protocol = eth_type_trans(skb, dev);
893                 mp->stats.rx_bytes += skb->len;
894                 netif_rx(skb);
895                 dev->last_rx = jiffies;
896                 mp->rx_bufs[i] = 0;
897                 ++mp->stats.rx_packets;
898             }
899         } else {
900             ++mp->stats.rx_errors;
901             ++mp->stats.rx_length_errors;
902         }
903
904         /* advance to next */
905         if (++i >= N_RX_RING)
906             i = 0;
907     }
908     mp->rx_empty = i;
909
910     i = mp->rx_fill;
911     for (;;) {
912         next = i + 1;
913         if (next >= N_RX_RING)
914             next = 0;
915         if (next == mp->rx_empty)
916             break;
917         cp = mp->rx_cmds + i;
918         skb = mp->rx_bufs[i];
919         if (skb == 0) {
920             skb = dev_alloc_skb(RX_BUFLEN + 2);
921             if (skb != 0) {
922                 skb_reserve(skb, 2);
923                 mp->rx_bufs[i] = skb;
924             }
925         }
926         st_le16(&cp->req_count, RX_BUFLEN);
927         data = skb? skb->data: dummy_buf;
928         st_le32(&cp->phy_addr, virt_to_bus(data));
929         out_le16(&cp->xfer_status, 0);
930         out_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
931 #if 0
932         if ((ld_le32(&rd->status) & ACTIVE) != 0) {
933             out_le32(&rd->control, (PAUSE << 16) | PAUSE);
934             while ((in_le32(&rd->status) & ACTIVE) != 0)
935                 ;
936         }
937 #endif
938         i = next;
939     }
940     if (i != mp->rx_fill) {
941         out_le32(&rd->control, ((RUN|WAKE) << 16) | (RUN|WAKE));
942         mp->rx_fill = i;
943     }
944 }
945
946 MODULE_AUTHOR("Paul Mackerras");
947 MODULE_DESCRIPTION("PowerMac MACE driver.");
948 MODULE_PARM(port_aaui, "i");
949 MODULE_PARM_DESC(port_aaui, "MACE uses AAUI port (0-1)");
950 MODULE_LICENSE("GPL");
951 EXPORT_NO_SYMBOLS;
952
953 static void __exit mace_cleanup (void)
954 {
955     struct net_device *dev;
956     struct mace_data *mp;
957
958     while ((dev = mace_devs) != 0) {
959                 mp = (struct mace_data *) mace_devs->priv;
960                 mace_devs = mp->next_mace;
961
962                 unregister_netdev(dev);
963                 free_irq(dev->irq, dev);
964                 free_irq(mp->tx_dma_intr, dev);
965                 free_irq(mp->rx_dma_intr, dev);
966
967                 release_OF_resource(mp->of_node, 0);
968                 release_OF_resource(mp->of_node, 1);
969                 release_OF_resource(mp->of_node, 2);
970
971                 kfree(dev);
972     }
973     if (dummy_buf != NULL) {
974                 kfree(dummy_buf);
975                 dummy_buf = NULL;
976     }
977 }
978
979 module_init(mace_probe);
980 module_exit(mace_cleanup);