This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / net / gianfar.c
1 /* 
2  * drivers/net/gianfar.c
3  *
4  * Gianfar Ethernet Driver
5  * Driver for FEC on MPC8540 and TSEC on MPC8540/MPC8560
6  * Based on 8260_io/fcc_enet.c
7  *
8  * Author: Andy Fleming
9  * Maintainer: Kumar Gala (kumar.gala@freescale.com)
10  *
11  * Copyright 2004 Freescale Semiconductor, Inc
12  *
13  * This program is free software; you can redistribute  it and/or modify it
14  * under  the terms of  the GNU General  Public License as published by the
15  * Free Software Foundation;  either version 2 of the  License, or (at your
16  * option) any later version.
17  *
18  *  Gianfar:  AKA Lambda Draconis, "Dragon"
19  *  RA 11 31 24.2
20  *  Dec +69 19 52
21  *  V 3.84
22  *  B-V +1.62
23  *
24  *  Theory of operation
25  *  This driver is designed for the Triple-speed Ethernet
26  *  controllers on the Freescale 8540/8560 integrated processors,
27  *  as well as the Fast Ethernet Controller on the 8540.  
28  *  
29  *  The driver is initialized through OCP.  Structures which
30  *  define the configuration needed by the board are defined in a
31  *  board structure in arch/ppc/platforms (though I do not
32  *  discount the possibility that other architectures could one
33  *  day be supported.  One assumption the driver currently makes
34  *  is that the PHY is configured in such a way to advertise all
35  *  capabilities.  This is a sensible default, and on certain
36  *  PHYs, changing this default encounters substantial errata
37  *  issues.  Future versions may remove this requirement, but for
38  *  now, it is best for the firmware to ensure this is the case.
39  *
40  *  The Gianfar Ethernet Controller uses a ring of buffer
41  *  descriptors.  The beginning is indicated by a register
42  *  pointing to the physical address of the start of the ring. 
43  *  The end is determined by a "wrap" bit being set in the 
44  *  last descriptor of the ring.
45  *
46  *  When a packet is received, the RXF bit in the
47  *  IEVENT register is set, triggering an interrupt when the 
48  *  corresponding bit in the IMASK register is also set (if
49  *  interrupt coalescing is active, then the interrupt may not
50  *  happen immediately, but will wait until either a set number
51  *  of frames or amount of time have passed.).  In NAPI, the
52  *  interrupt handler will signal there is work to be done, and
53  *  exit.  Without NAPI, the packet(s) will be handled
54  *  immediately.  Both methods will start at the last known empty
55  *  descriptor, and process every subsequent descriptor until there 
56  *  are none left with data (NAPI will stop after a set number of
57  *  packets to give time to other tasks, but will eventually
58  *  process all the packets).  The data arrives inside a
59  *  pre-allocated skb, and so after the skb is passed up to the
60  *  stack, a new skb must be allocated, and the address field in
61  *  the buffer descriptor must be updated to indicate this new
62  *  skb.
63  *
64  *  When the kernel requests that a packet be transmitted, the
65  *  driver starts where it left off last time, and points the
66  *  descriptor at the buffer which was passed in.  The driver
67  *  then informs the DMA engine that there are packets ready to
68  *  be transmitted.  Once the controller is finished transmitting
69  *  the packet, an interrupt may be triggered (under the same
70  *  conditions as for reception, but depending on the TXF bit).
71  *  The driver then cleans up the buffer.
72  */
73
74 #include <linux/config.h>
75 #include <linux/kernel.h>
76 #include <linux/sched.h>
77 #include <linux/string.h>
78 #include <linux/errno.h>
79 #include <linux/slab.h>
80 #include <linux/interrupt.h>
81 #include <linux/init.h>
82 #include <linux/delay.h>
83 #include <linux/netdevice.h>
84 #include <linux/etherdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/spinlock.h>
87 #include <linux/mm.h>
88
89 #include <asm/io.h>
90 #include <asm/irq.h>
91 #include <asm/uaccess.h>
92 #include <linux/module.h>
93 #include <linux/version.h>
94 #include <linux/dma-mapping.h>
95 #include <linux/crc32.h>
96
97 #include "gianfar.h"
98 #include "gianfar_phy.h"
99 #ifdef CONFIG_NET_FASTROUTE
100 #include <linux/if_arp.h>
101 #include <net/ip.h>
102 #endif
103
104 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,41)
105 #define irqreturn_t void
106 #define IRQ_HANDLED 
107 #endif
108
109 #define TX_TIMEOUT      (1*HZ)
110 #define SKB_ALLOC_TIMEOUT 1000000
111 #undef BRIEF_GFAR_ERRORS
112 #define VERBOSE_GFAR_ERRORS
113
114 #ifdef CONFIG_GFAR_NAPI
115 #define RECEIVE(x) netif_receive_skb(x)
116 #else
117 #define RECEIVE(x) netif_rx(x)
118 #endif
119
120 #define DEVICE_NAME "%s: Gianfar Ethernet Controller Version 1.0, "
121 char gfar_driver_name[] = "Gianfar Ethernet";
122 char gfar_driver_version[] = "1.0";
123
124 int startup_gfar(struct net_device *dev);
125 static int gfar_enet_open(struct net_device *dev);
126 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
127 static void gfar_timeout(struct net_device *dev);
128 static int gfar_close(struct net_device *dev);
129 struct sk_buff *gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp);
130 static struct net_device_stats *gfar_get_stats(struct net_device *dev);
131 static int gfar_set_mac_address(struct net_device *dev);
132 static int gfar_change_mtu(struct net_device *dev, int new_mtu);
133 static irqreturn_t gfar_error(int irq, void *dev_id, struct pt_regs *regs);
134 static irqreturn_t gfar_transmit(int irq, void *dev_id, struct pt_regs *regs);
135 irqreturn_t gfar_receive(int irq, void *dev_id, struct pt_regs *regs);
136 static irqreturn_t gfar_interrupt(int irq, void *dev_id, struct pt_regs *regs);
137 static irqreturn_t phy_interrupt(int irq, void *dev_id, struct pt_regs *regs);
138 static void gfar_phy_change(void *data);
139 static void gfar_phy_timer(unsigned long data);
140 static void adjust_link(struct net_device *dev);
141 static void init_registers(struct net_device *dev);
142 static int init_phy(struct net_device *dev);
143 static int gfar_probe(struct ocp_device *ocpdev);
144 static void gfar_remove(struct ocp_device *ocpdev);
145 void free_skb_resources(struct gfar_private *priv);
146 static void gfar_set_multi(struct net_device *dev);
147 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
148 #ifdef CONFIG_GFAR_NAPI
149 static int gfar_poll(struct net_device *dev, int *budget);
150 #endif
151 #ifdef CONFIG_NET_FASTROUTE
152 static int gfar_accept_fastpath(struct net_device *dev, struct dst_entry *dst);
153 #endif
154 static inline int try_fastroute(struct sk_buff *skb, struct net_device *dev, int length);
155 #ifdef CONFIG_GFAR_NAPI
156 static int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
157 #else
158 static int gfar_clean_rx_ring(struct net_device *dev);
159 #endif
160 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
161
162 extern struct ethtool_ops gfar_ethtool_ops;
163 extern void gfar_gstrings_normon(struct net_device *dev, u32 stringset, 
164                 u8 * buf);
165 extern void gfar_fill_stats_normon(struct net_device *dev, 
166                 struct ethtool_stats *dummy, u64 * buf);
167 extern int gfar_stats_count_normon(struct net_device *dev);
168
169
170 MODULE_AUTHOR("Freescale Semiconductor, Inc");
171 MODULE_DESCRIPTION("Gianfar Ethernet Driver");
172 MODULE_LICENSE("GPL");
173
174 /* Called by the ocp code to initialize device data structures
175  * required for bringing up the device
176  * returns 0 on success */
177 static int gfar_probe(struct ocp_device *ocpdev)
178 {
179         u32 tempval;
180         struct ocp_device *mdiodev;
181         struct net_device *dev = NULL;
182         struct gfar_private *priv = NULL;
183         struct ocp_gfar_data *einfo;
184         int idx;
185         int err = 0;
186         struct ethtool_ops *dev_ethtool_ops;
187
188         einfo = (struct ocp_gfar_data *) ocpdev->def->additions;
189
190         if (einfo == NULL) {
191                 printk(KERN_ERR "gfar %d: Missing additional data!\n",
192                        ocpdev->def->index);
193
194                 return -ENODEV;
195         }
196
197         /* get a pointer to the register memory which can
198          * configure the PHYs.  If it's different from this set,
199          * get the device which has those regs */
200         if ((einfo->phyregidx >= 0) && (einfo->phyregidx != ocpdev->def->index)) {
201                 mdiodev = ocp_find_device(OCP_ANY_ID,
202                                           OCP_FUNC_GFAR, einfo->phyregidx);
203
204                 /* If the device which holds the MDIO regs isn't
205                  * up, wait for it to come up */
206                 if (mdiodev == NULL)
207                         return -EAGAIN;
208         } else {
209                 mdiodev = ocpdev;
210         }
211
212         /* Create an ethernet device instance */
213         dev = alloc_etherdev(sizeof (*priv));
214
215         if (dev == NULL)
216                 return -ENOMEM;
217
218         priv = netdev_priv(dev);
219
220         /* Set the info in the priv to the current info */
221         priv->einfo = einfo;
222
223         /* get a pointer to the register memory */
224         priv->regs = (struct gfar *)
225             ioremap(ocpdev->def->paddr, sizeof (struct gfar));
226
227         if (priv->regs == NULL) {
228                 err = -ENOMEM;
229                 goto regs_fail;
230         }
231
232         /* Set the PHY base address */
233         priv->phyregs = (struct gfar *)
234             ioremap(mdiodev->def->paddr, sizeof (struct gfar));
235
236         if (priv->phyregs == NULL) {
237                 err = -ENOMEM;
238                 goto phy_regs_fail;
239         }
240
241         ocp_set_drvdata(ocpdev, dev);
242
243         /* Stop the DMA engine now, in case it was running before */
244         /* (The firmware could have used it, and left it running). */
245         /* To do this, we write Graceful Receive Stop and Graceful */
246         /* Transmit Stop, and then wait until the corresponding bits */
247         /* in IEVENT indicate the stops have completed. */
248         tempval = gfar_read(&priv->regs->dmactrl);
249         tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
250         gfar_write(&priv->regs->dmactrl, tempval);
251
252         tempval = gfar_read(&priv->regs->dmactrl);
253         tempval |= (DMACTRL_GRS | DMACTRL_GTS);
254         gfar_write(&priv->regs->dmactrl, tempval);
255
256         while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
257                 cpu_relax();
258
259         /* Reset MAC layer */
260         gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
261
262         tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
263         gfar_write(&priv->regs->maccfg1, tempval);
264
265         /* Initialize MACCFG2. */
266         gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
267
268         /* Initialize ECNTRL */
269         gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
270
271         /* Copy the station address into the dev structure, */
272         /* and into the address registers MAC_STNADDR1,2. */
273         /* Backwards, because little endian MACs are dumb. */
274         /* Don't set the regs if the firmware already did */
275         memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
276
277         /* Set the dev->base_addr to the gfar reg region */
278         dev->base_addr = (unsigned long) (priv->regs);
279
280         SET_MODULE_OWNER(dev);
281
282         /* Fill in the dev structure */
283         dev->open = gfar_enet_open;
284         dev->hard_start_xmit = gfar_start_xmit;
285         dev->tx_timeout = gfar_timeout;
286         dev->watchdog_timeo = TX_TIMEOUT;
287 #ifdef CONFIG_GFAR_NAPI
288         dev->poll = gfar_poll;
289         dev->weight = GFAR_DEV_WEIGHT;
290 #endif
291         dev->stop = gfar_close;
292         dev->get_stats = gfar_get_stats;
293         dev->change_mtu = gfar_change_mtu;
294         dev->mtu = 1500;
295         dev->set_multicast_list = gfar_set_multi;
296         dev->flags |= IFF_MULTICAST;
297
298         dev_ethtool_ops = 
299                 (struct ethtool_ops *)kmalloc(sizeof(struct ethtool_ops), 
300                                               GFP_KERNEL);
301
302         if(dev_ethtool_ops == NULL) {
303                 err = -ENOMEM;
304                 goto ethtool_fail;
305         }
306
307         memcpy(dev_ethtool_ops, &gfar_ethtool_ops, sizeof(gfar_ethtool_ops));
308
309         /* If there is no RMON support in this device, we don't
310          * want to expose non-existant statistics */
311         if((priv->einfo->flags & GFAR_HAS_RMON) == 0) {
312                 dev_ethtool_ops->get_strings = gfar_gstrings_normon;
313                 dev_ethtool_ops->get_stats_count = gfar_stats_count_normon;
314                 dev_ethtool_ops->get_ethtool_stats = gfar_fill_stats_normon;
315         }
316
317         if((priv->einfo->flags & GFAR_HAS_COALESCE) == 0) {
318                 dev_ethtool_ops->set_coalesce = NULL;
319                 dev_ethtool_ops->get_coalesce = NULL;
320         }
321
322         dev->ethtool_ops = dev_ethtool_ops;
323
324 #ifdef CONFIG_NET_FASTROUTE
325         dev->accept_fastpath = gfar_accept_fastpath;
326 #endif
327
328         priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
329 #ifdef CONFIG_GFAR_BUFSTASH
330         priv->rx_stash_size = STASH_LENGTH;
331 #endif
332         priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
333         priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
334
335         /* Initially, coalescing is disabled */
336         priv->txcoalescing = 0;
337         priv->txcount = 0;
338         priv->txtime = 0;
339         priv->rxcoalescing = 0;
340         priv->rxcount = 0;
341         priv->rxtime = 0;
342
343         err = register_netdev(dev);
344
345         if (err) {
346                 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
347                        dev->name);
348                 goto register_fail;
349         }
350
351         /* Print out the device info */
352         printk(DEVICE_NAME, dev->name);
353         for (idx = 0; idx < 6; idx++)
354                 printk("%2.2x%c", dev->dev_addr[idx], idx == 5 ? ' ' : ':');
355         printk("\n");
356
357         /* Even more device info helps when determining which kernel */
358         /* provided which set of benchmarks.  Since this is global for all */
359         /* devices, we only print it once */
360 #ifdef CONFIG_GFAR_NAPI
361         printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
362 #else
363         printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
364 #endif
365         printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
366                dev->name, priv->rx_ring_size, priv->tx_ring_size);
367
368         return 0;
369
370
371 register_fail:
372         kfree(dev_ethtool_ops);
373 ethtool_fail:
374         iounmap((void *) priv->phyregs);
375 phy_regs_fail:
376         iounmap((void *) priv->regs);
377 regs_fail:
378         free_netdev(dev);
379         return -ENOMEM;
380 }
381
382 static void gfar_remove(struct ocp_device *ocpdev)
383 {
384         struct net_device *dev = ocp_get_drvdata(ocpdev);
385         struct gfar_private *priv = netdev_priv(dev);
386
387         ocp_set_drvdata(ocpdev, NULL);
388
389         kfree(dev->ethtool_ops);
390         iounmap((void *) priv->regs);
391         iounmap((void *) priv->phyregs);
392         free_netdev(dev);
393 }
394
395 /* Configure the PHY for dev.
396  * returns 0 if success.  -1 if failure
397  */
398 static int init_phy(struct net_device *dev)
399 {
400         struct gfar_private *priv = netdev_priv(dev);
401         struct phy_info *curphy;
402
403         priv->link = 1;
404         priv->oldlink = 0;
405         priv->oldspeed = 0;
406         priv->olddplx = -1;
407
408         /* get info for this PHY */
409         curphy = get_phy_info(dev);
410
411         if (curphy == NULL) {
412                 printk(KERN_ERR "%s: No PHY found\n", dev->name);
413                 return -1;
414         }
415
416         priv->phyinfo = curphy;
417
418         /* Run the commands which configure the PHY */
419         phy_run_commands(dev, curphy->config);
420
421         return 0;
422 }
423
424 static void init_registers(struct net_device *dev)
425 {
426         struct gfar_private *priv = netdev_priv(dev);
427
428         /* Clear IEVENT */
429         gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
430
431         /* Initialize IMASK */
432         gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
433
434         /* Init hash registers to zero */
435         gfar_write(&priv->regs->iaddr0, 0);
436         gfar_write(&priv->regs->iaddr1, 0);
437         gfar_write(&priv->regs->iaddr2, 0);
438         gfar_write(&priv->regs->iaddr3, 0);
439         gfar_write(&priv->regs->iaddr4, 0);
440         gfar_write(&priv->regs->iaddr5, 0);
441         gfar_write(&priv->regs->iaddr6, 0);
442         gfar_write(&priv->regs->iaddr7, 0);
443
444         gfar_write(&priv->regs->gaddr0, 0);
445         gfar_write(&priv->regs->gaddr1, 0);
446         gfar_write(&priv->regs->gaddr2, 0);
447         gfar_write(&priv->regs->gaddr3, 0);
448         gfar_write(&priv->regs->gaddr4, 0);
449         gfar_write(&priv->regs->gaddr5, 0);
450         gfar_write(&priv->regs->gaddr6, 0);
451         gfar_write(&priv->regs->gaddr7, 0);
452
453         /* Zero out rctrl */
454         gfar_write(&priv->regs->rctrl, 0x00000000);
455
456         /* Zero out the rmon mib registers if it has them */
457         if (priv->einfo->flags & GFAR_HAS_RMON) {
458                 memset((void *) &(priv->regs->rmon), 0,
459                        sizeof (struct rmon_mib));
460
461                 /* Mask off the CAM interrupts */
462                 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
463                 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
464         }
465
466         /* Initialize the max receive buffer length */
467         gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
468
469 #ifdef CONFIG_GFAR_BUFSTASH
470         /* If we are stashing buffers, we need to set the
471          * extraction length to the size of the buffer */
472         gfar_write(&priv->regs->attreli, priv->rx_stash_size << 16);
473 #endif
474
475         /* Initialize the Minimum Frame Length Register */
476         gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
477
478         /* Setup Attributes so that snooping is on for rx */
479         gfar_write(&priv->regs->attr, ATTR_INIT_SETTINGS);
480         gfar_write(&priv->regs->attreli, ATTRELI_INIT_SETTINGS);
481
482         /* Assign the TBI an address which won't conflict with the PHYs */
483         gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
484 }
485
486 void stop_gfar(struct net_device *dev)
487 {
488         struct gfar_private *priv = netdev_priv(dev);
489         struct gfar *regs = priv->regs;
490         unsigned long flags;
491         u32 tempval;
492
493         /* Lock it down */
494         spin_lock_irqsave(&priv->lock, flags);
495
496         /* Tell the kernel the link is down */
497         priv->link = 0;
498         adjust_link(dev);
499
500         /* Mask all interrupts */
501         gfar_write(&regs->imask, IMASK_INIT_CLEAR);
502
503         /* Clear all interrupts */
504         gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
505
506         /* Stop the DMA, and wait for it to stop */
507         tempval = gfar_read(&priv->regs->dmactrl);
508         if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
509             != (DMACTRL_GRS | DMACTRL_GTS)) {
510                 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
511                 gfar_write(&priv->regs->dmactrl, tempval);
512
513                 while (!(gfar_read(&priv->regs->ievent) &
514                          (IEVENT_GRSC | IEVENT_GTSC)))
515                         cpu_relax();
516         }
517
518         /* Disable Rx and Tx */
519         tempval = gfar_read(&regs->maccfg1);
520         tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
521         gfar_write(&regs->maccfg1, tempval);
522
523         if (priv->einfo->flags & GFAR_HAS_PHY_INTR) {
524                 phy_run_commands(dev, priv->phyinfo->shutdown);
525         }
526
527         spin_unlock_irqrestore(&priv->lock, flags);
528
529         /* Free the IRQs */
530         if (priv->einfo->flags & GFAR_HAS_MULTI_INTR) {
531                 free_irq(priv->einfo->interruptError, dev);
532                 free_irq(priv->einfo->interruptTransmit, dev);
533                 free_irq(priv->einfo->interruptReceive, dev);
534         } else {
535                 free_irq(priv->einfo->interruptTransmit, dev);
536         }
537
538         if (priv->einfo->flags & GFAR_HAS_PHY_INTR) {
539                 free_irq(priv->einfo->interruptPHY, dev);
540         } else {
541                 del_timer_sync(&priv->phy_info_timer);
542         }
543
544         free_skb_resources(priv);
545
546         dma_unmap_single(NULL, gfar_read(&regs->tbase),
547                         sizeof(struct txbd)*priv->tx_ring_size,
548                         DMA_BIDIRECTIONAL);
549         dma_unmap_single(NULL, gfar_read(&regs->rbase),
550                         sizeof(struct rxbd)*priv->rx_ring_size,
551                         DMA_BIDIRECTIONAL);
552
553         /* Free the buffer descriptors */
554         kfree(priv->tx_bd_base);
555 }
556
557 /* If there are any tx skbs or rx skbs still around, free them.
558  * Then free tx_skbuff and rx_skbuff */
559 void free_skb_resources(struct gfar_private *priv)
560 {
561         struct rxbd8 *rxbdp;
562         struct txbd8 *txbdp;
563         int i;
564
565         /* Go through all the buffer descriptors and free their data buffers */
566         txbdp = priv->tx_bd_base;
567
568         for (i = 0; i < priv->tx_ring_size; i++) {
569
570                 if (priv->tx_skbuff[i]) {
571                         dma_unmap_single(NULL, txbdp->bufPtr,
572                                         txbdp->length,
573                                         DMA_TO_DEVICE);
574                         dev_kfree_skb_any(priv->tx_skbuff[i]);
575                         priv->tx_skbuff[i] = NULL;
576                 }
577         }
578
579         kfree(priv->tx_skbuff);
580
581         rxbdp = priv->rx_bd_base;
582
583         /* rx_skbuff is not guaranteed to be allocated, so only
584          * free it and its contents if it is allocated */
585         if(priv->rx_skbuff != NULL) {
586                 for (i = 0; i < priv->rx_ring_size; i++) {
587                         if (priv->rx_skbuff[i]) {
588                                 dma_unmap_single(NULL, rxbdp->bufPtr,
589                                                 priv->rx_buffer_size
590                                                 + RXBUF_ALIGNMENT,
591                                                 DMA_FROM_DEVICE);
592
593                                 dev_kfree_skb_any(priv->rx_skbuff[i]);
594                                 priv->rx_skbuff[i] = NULL;
595                         }
596
597                         rxbdp->status = 0;
598                         rxbdp->length = 0;
599                         rxbdp->bufPtr = 0;
600
601                         rxbdp++;
602                 }
603
604                 kfree(priv->rx_skbuff);
605         }
606 }
607
608 /* Bring the controller up and running */
609 int startup_gfar(struct net_device *dev)
610 {
611         struct txbd8 *txbdp;
612         struct rxbd8 *rxbdp;
613         unsigned long addr;
614         int i;
615         struct gfar_private *priv = netdev_priv(dev);
616         struct gfar *regs = priv->regs;
617         u32 tempval;
618         int err = 0;
619
620         gfar_write(&regs->imask, IMASK_INIT_CLEAR);
621
622         /* Allocate memory for the buffer descriptors */
623         addr =
624             (unsigned int) kmalloc(sizeof (struct txbd8) * priv->tx_ring_size +
625                                    sizeof (struct rxbd8) * priv->rx_ring_size,
626                                    GFP_KERNEL);
627
628         if (addr == 0) {
629                 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
630                        dev->name);
631                 return -ENOMEM;
632         }
633
634         priv->tx_bd_base = (struct txbd8 *) addr;
635
636         /* enet DMA only understands physical addresses */
637         gfar_write(&regs->tbase, 
638                         dma_map_single(NULL, (void *)addr, 
639                                 sizeof(struct txbd8) * priv->tx_ring_size,
640                                 DMA_BIDIRECTIONAL));
641
642         /* Start the rx descriptor ring where the tx ring leaves off */
643         addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
644         priv->rx_bd_base = (struct rxbd8 *) addr;
645         gfar_write(&regs->rbase, 
646                         dma_map_single(NULL, (void *)addr, 
647                                 sizeof(struct rxbd8) * priv->rx_ring_size,
648                                 DMA_BIDIRECTIONAL));
649
650         /* Setup the skbuff rings */
651         priv->tx_skbuff =
652             (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
653                                         priv->tx_ring_size, GFP_KERNEL);
654
655         if (priv->tx_skbuff == NULL) {
656                 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
657                        dev->name);
658                 err = -ENOMEM;
659                 goto tx_skb_fail;
660         }
661
662         for (i = 0; i < priv->tx_ring_size; i++)
663                 priv->tx_skbuff[i] = NULL;
664
665         priv->rx_skbuff =
666             (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
667                                         priv->rx_ring_size, GFP_KERNEL);
668
669         if (priv->rx_skbuff == NULL) {
670                 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
671                        dev->name);
672                 err = -ENOMEM;
673                 goto rx_skb_fail;
674         }
675
676         for (i = 0; i < priv->rx_ring_size; i++)
677                 priv->rx_skbuff[i] = NULL;
678
679         /* Initialize some variables in our dev structure */
680         priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
681         priv->cur_rx = priv->rx_bd_base;
682         priv->skb_curtx = priv->skb_dirtytx = 0;
683         priv->skb_currx = 0;
684
685         /* Initialize Transmit Descriptor Ring */
686         txbdp = priv->tx_bd_base;
687         for (i = 0; i < priv->tx_ring_size; i++) {
688                 txbdp->status = 0;
689                 txbdp->length = 0;
690                 txbdp->bufPtr = 0;
691                 txbdp++;
692         }
693
694         /* Set the last descriptor in the ring to indicate wrap */
695         txbdp--;
696         txbdp->status |= TXBD_WRAP;
697
698         rxbdp = priv->rx_bd_base;
699         for (i = 0; i < priv->rx_ring_size; i++) {
700                 struct sk_buff *skb = NULL;
701
702                 rxbdp->status = 0;
703
704                 skb = gfar_new_skb(dev, rxbdp);
705
706                 priv->rx_skbuff[i] = skb;
707
708                 rxbdp++;
709         }
710
711         /* Set the last descriptor in the ring to wrap */
712         rxbdp--;
713         rxbdp->status |= RXBD_WRAP;
714
715         /* If the device has multiple interrupts, register for
716          * them.  Otherwise, only register for the one */
717         if (priv->einfo->flags & GFAR_HAS_MULTI_INTR) {
718                 /* Install our interrupt handlers for Error, 
719                  * Transmit, and Receive */
720                 if (request_irq(priv->einfo->interruptError, gfar_error,
721                                 SA_SHIRQ, "enet_error", dev) < 0) {
722                         printk(KERN_ERR "%s: Can't get IRQ %d\n",
723                                dev->name, priv->einfo->interruptError);
724
725                         err = -1;
726                         goto err_irq_fail;
727                 }
728
729                 if (request_irq(priv->einfo->interruptTransmit, gfar_transmit,
730                                 SA_SHIRQ, "enet_tx", dev) < 0) {
731                         printk(KERN_ERR "%s: Can't get IRQ %d\n",
732                                dev->name, priv->einfo->interruptTransmit);
733
734                         err = -1;
735
736                         goto tx_irq_fail;
737                 }
738
739                 if (request_irq(priv->einfo->interruptReceive, gfar_receive,
740                                 SA_SHIRQ, "enet_rx", dev) < 0) {
741                         printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
742                                dev->name, priv->einfo->interruptReceive);
743
744                         err = -1;
745                         goto rx_irq_fail;
746                 }
747         } else {
748                 if (request_irq(priv->einfo->interruptTransmit, gfar_interrupt,
749                                 SA_SHIRQ, "gfar_interrupt", dev) < 0) {
750                         printk(KERN_ERR "%s: Can't get IRQ %d\n",
751                                dev->name, priv->einfo->interruptError);
752
753                         err = -1;
754                         goto err_irq_fail;
755                 }
756         }
757
758         /* Grab the PHY interrupt */
759         if (priv->einfo->flags & GFAR_HAS_PHY_INTR) {
760                 if (request_irq(priv->einfo->interruptPHY, phy_interrupt,
761                                 SA_SHIRQ, "phy_interrupt", dev) < 0) {
762                         printk(KERN_ERR "%s: Can't get IRQ %d (PHY)\n",
763                                dev->name, priv->einfo->interruptPHY);
764
765                         err = -1;
766
767                         if (priv->einfo->flags & GFAR_HAS_MULTI_INTR)
768                                 goto phy_irq_fail;
769                         else
770                                 goto tx_irq_fail;
771                 }
772         } else {
773                 init_timer(&priv->phy_info_timer);
774                 priv->phy_info_timer.function = &gfar_phy_timer;
775                 priv->phy_info_timer.data = (unsigned long) dev;
776                 mod_timer(&priv->phy_info_timer, jiffies + 2 * HZ);
777         }
778
779         /* Set up the bottom half queue */
780         INIT_WORK(&priv->tq, (void (*)(void *))gfar_phy_change, dev);
781
782         /* Configure the PHY interrupt */
783         phy_run_commands(dev, priv->phyinfo->startup);
784
785         /* Tell the kernel the link is up, and determine the
786          * negotiated features (speed, duplex) */
787         adjust_link(dev);
788
789         if (priv->link == 0)
790                 printk(KERN_INFO "%s: No link detected\n", dev->name);
791
792         /* Configure the coalescing support */
793         if (priv->txcoalescing)
794                 gfar_write(&regs->txic,
795                            mk_ic_value(priv->txcount, priv->txtime));
796         else
797                 gfar_write(&regs->txic, 0);
798
799         if (priv->rxcoalescing)
800                 gfar_write(&regs->rxic,
801                            mk_ic_value(priv->rxcount, priv->rxtime));
802         else
803                 gfar_write(&regs->rxic, 0);
804
805         init_waitqueue_head(&priv->rxcleanupq);
806
807         /* Enable Rx and Tx in MACCFG1 */
808         tempval = gfar_read(&regs->maccfg1);
809         tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
810         gfar_write(&regs->maccfg1, tempval);
811
812         /* Initialize DMACTRL to have WWR and WOP */
813         tempval = gfar_read(&priv->regs->dmactrl);
814         tempval |= DMACTRL_INIT_SETTINGS;
815         gfar_write(&priv->regs->dmactrl, tempval);
816
817         /* Clear THLT, so that the DMA starts polling now */
818         gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
819
820         /* Make sure we aren't stopped */
821         tempval = gfar_read(&priv->regs->dmactrl);
822         tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
823         gfar_write(&priv->regs->dmactrl, tempval);
824
825         /* Unmask the interrupts we look for */
826         gfar_write(&regs->imask, IMASK_DEFAULT);
827
828         return 0;
829
830 phy_irq_fail:
831         free_irq(priv->einfo->interruptReceive, dev);
832 rx_irq_fail:
833         free_irq(priv->einfo->interruptTransmit, dev);
834 tx_irq_fail:
835         free_irq(priv->einfo->interruptError, dev);
836 err_irq_fail:
837 rx_skb_fail:
838         free_skb_resources(priv);
839 tx_skb_fail:
840         kfree(priv->tx_bd_base);
841         return err;
842 }
843
844 /* Called when something needs to use the ethernet device */
845 /* Returns 0 for success. */
846 static int gfar_enet_open(struct net_device *dev)
847 {
848         int err;
849
850         /* Initialize a bunch of registers */
851         init_registers(dev);
852
853         gfar_set_mac_address(dev);
854
855         err = init_phy(dev);
856
857         if (err)
858                 return err;
859
860         err = startup_gfar(dev);
861
862         netif_start_queue(dev);
863
864         return err;
865 }
866
867 /* This is called by the kernel when a frame is ready for transmission. */
868 /* It is pointed to by the dev->hard_start_xmit function pointer */
869 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
870 {
871         struct gfar_private *priv = netdev_priv(dev);
872         struct txbd8 *txbdp;
873
874         /* Update transmit stats */
875         priv->stats.tx_bytes += skb->len;
876
877         /* Lock priv now */
878         spin_lock_irq(&priv->lock);
879
880         /* Point at the first free tx descriptor */
881         txbdp = priv->cur_tx;
882
883         /* Clear all but the WRAP status flags */
884         txbdp->status &= TXBD_WRAP;
885
886         /* Set buffer length and pointer */
887         txbdp->length = skb->len;
888         txbdp->bufPtr = dma_map_single(NULL, skb->data, 
889                         skb->len, DMA_TO_DEVICE);
890
891         /* Save the skb pointer so we can free it later */
892         priv->tx_skbuff[priv->skb_curtx] = skb;
893
894         /* Update the current skb pointer (wrapping if this was the last) */
895         priv->skb_curtx =
896             (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
897
898         /* Flag the BD as interrupt-causing */
899         txbdp->status |= TXBD_INTERRUPT;
900
901         /* Flag the BD as ready to go, last in frame, and  */
902         /* in need of CRC */
903         txbdp->status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
904
905         dev->trans_start = jiffies;
906
907         /* If this was the last BD in the ring, the next one */
908         /* is at the beginning of the ring */
909         if (txbdp->status & TXBD_WRAP)
910                 txbdp = priv->tx_bd_base;
911         else
912                 txbdp++;
913
914         /* If the next BD still needs to be cleaned up, then the bds
915            are full.  We need to tell the kernel to stop sending us stuff. */
916         if (txbdp == priv->dirty_tx) {
917                 netif_stop_queue(dev);
918
919                 priv->stats.tx_fifo_errors++;
920         }
921
922         /* Update the current txbd to the next one */
923         priv->cur_tx = txbdp;
924
925         /* Tell the DMA to go go go */
926         gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
927
928         /* Unlock priv */
929         spin_unlock_irq(&priv->lock);
930
931         return 0;
932 }
933
934 /* Stops the kernel queue, and halts the controller */
935 static int gfar_close(struct net_device *dev)
936 {
937         stop_gfar(dev);
938
939         netif_stop_queue(dev);
940
941         return 0;
942 }
943
944 /* returns a net_device_stats structure pointer */
945 static struct net_device_stats * gfar_get_stats(struct net_device *dev)
946 {
947         struct gfar_private *priv = netdev_priv(dev);
948
949         return &(priv->stats);
950 }
951
952 /* Changes the mac address if the controller is not running. */
953 int gfar_set_mac_address(struct net_device *dev)
954 {
955         struct gfar_private *priv = netdev_priv(dev);
956         int i;
957         char tmpbuf[MAC_ADDR_LEN];
958         u32 tempval;
959
960         /* Now copy it into the mac registers backwards, cuz */
961         /* little endian is silly */
962         for (i = 0; i < MAC_ADDR_LEN; i++)
963                 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->dev_addr[i];
964
965         gfar_write(&priv->regs->macstnaddr1, *((u32 *) (tmpbuf)));
966
967         tempval = *((u32 *) (tmpbuf + 4));
968
969         gfar_write(&priv->regs->macstnaddr2, tempval);
970
971         return 0;
972 }
973
974 /**********************************************************************
975  * gfar_accept_fastpath
976  *
977  * Used to authenticate to the kernel that a fast path entry can be
978  * added to device's routing table cache
979  *
980  * Input : pointer to ethernet interface network device structure and
981  *         a pointer to the designated entry to be added to the cache.
982  * Output : zero upon success, negative upon failure
983  **********************************************************************/
984 #ifdef CONFIG_NET_FASTROUTE
985 static int gfar_accept_fastpath(struct net_device *dev, struct dst_entry *dst)
986 {
987         struct net_device *odev = dst->dev;
988
989         if ((dst->ops->protocol != __constant_htons(ETH_P_IP))
990                         || (odev->type != ARPHRD_ETHER) 
991                         || (odev->accept_fastpath == NULL)) {
992                 return -1;
993         }
994
995         return 0;
996 }
997 #endif
998
999 /* try_fastroute() -- Checks the fastroute cache to see if a given packet
1000  *   can be routed immediately to another device.  If it can, we send it.
1001  *   If we used a fastroute, we return 1.  Otherwise, we return 0.
1002  *   Returns 0 if CONFIG_NET_FASTROUTE is not on
1003  */
1004 static inline int try_fastroute(struct sk_buff *skb, struct net_device *dev, int length)
1005 {
1006 #ifdef CONFIG_NET_FASTROUTE
1007         struct ethhdr *eth;
1008         struct iphdr *iph;
1009         unsigned int hash;
1010         struct rtable *rt;
1011         struct net_device *odev;
1012         struct gfar_private *priv = netdev_priv(dev);
1013         unsigned int CPU_ID = smp_processor_id();
1014
1015         eth = (struct ethhdr *) (skb->data);
1016
1017         /* Only route ethernet IP packets */
1018         if (eth->h_proto == __constant_htons(ETH_P_IP)) {
1019                 iph = (struct iphdr *) (skb->data + ETH_HLEN);
1020
1021                 /* Generate the hash value */
1022                 hash = ((*(u8 *) &iph->daddr) ^ (*(u8 *) & iph->saddr)) & NETDEV_FASTROUTE_HMASK;
1023
1024                 rt = (struct rtable *) (dev->fastpath[hash]);
1025                 if (rt != NULL 
1026                                 && ((*(u32 *) &iph->daddr) == (*(u32 *) &rt->key.dst))
1027                                 && ((*(u32 *) &iph->saddr) == (*(u32 *) &rt->key.src))
1028                                 && !(rt->u.dst.obsolete)) {
1029                         odev = rt->u.dst.dev;
1030                         netdev_rx_stat[CPU_ID].fastroute_hit++;
1031
1032                         /* Make sure the packet is:
1033                          * 1) IPv4
1034                          * 2) without any options (header length of 5)
1035                          * 3) Not a multicast packet
1036                          * 4) going to a valid destination
1037                          * 5) Not out of time-to-live
1038                          */
1039                         if (iph->version == 4 
1040                                         && iph->ihl == 5 
1041                                         && (!(eth->h_dest[0] & 0x01)) 
1042                                         && neigh_is_valid(rt->u.dst.neighbour) 
1043                                         && iph->ttl > 1) {
1044
1045                                 /* Fast Route Path: Taken if the outgoing device is ready to transmit the packet now */
1046                                 if ((!netif_queue_stopped(odev)) 
1047                                                 && (!spin_is_locked(odev->xmit_lock)) 
1048                                                 && (skb->len <= (odev->mtu + ETH_HLEN + 2 + 4))) {
1049
1050                                         skb->pkt_type = PACKET_FASTROUTE;
1051                                         skb->protocol = __constant_htons(ETH_P_IP);
1052                                         ip_decrease_ttl(iph);
1053                                         memcpy(eth->h_source, odev->dev_addr, MAC_ADDR_LEN);
1054                                         memcpy(eth->h_dest, rt->u.dst.neighbour->ha, MAC_ADDR_LEN);
1055                                         skb->dev = odev;
1056
1057                                         /* Prep the skb for the packet */
1058                                         skb_put(skb, length);
1059
1060                                         if (odev->hard_start_xmit(skb, odev) != 0) {
1061                                                 panic("%s: FastRoute path corrupted", dev->name);
1062                                         }
1063                                         netdev_rx_stat[CPU_ID].fastroute_success++;
1064                                 }
1065
1066                                 /* Semi Fast Route Path: Mark the packet as needing fast routing, but let the
1067                                  * stack handle getting it to the device */
1068                                 else {
1069                                         skb->pkt_type = PACKET_FASTROUTE;
1070                                         skb->nh.raw = skb->data + ETH_HLEN;
1071                                         skb->protocol = __constant_htons(ETH_P_IP);
1072                                         netdev_rx_stat[CPU_ID].fastroute_defer++;
1073
1074                                         /* Prep the skb for the packet */
1075                                         skb_put(skb, length);
1076
1077                                         if(RECEIVE(skb) == NET_RX_DROP) {
1078                                                 priv->extra_stats.kernel_dropped++;
1079                                         }
1080                                 }
1081
1082                                 return 1;
1083                         }
1084                 }
1085         }
1086 #endif /* CONFIG_NET_FASTROUTE */
1087         return 0;
1088 }
1089
1090 static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1091 {
1092         int tempsize, tempval;
1093         struct gfar_private *priv = netdev_priv(dev);
1094         int oldsize = priv->rx_buffer_size;
1095         int frame_size = new_mtu + 18;
1096
1097         if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
1098                 printk(KERN_ERR "%s: Invalid MTU setting\n", dev->name);
1099                 return -EINVAL;
1100         }
1101
1102         tempsize =
1103             (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1104             INCREMENTAL_BUFFER_SIZE;
1105
1106         /* Only stop and start the controller if it isn't already
1107          * stopped */
1108         if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1109                 stop_gfar(dev);
1110
1111         priv->rx_buffer_size = tempsize;
1112
1113         dev->mtu = new_mtu;
1114
1115         gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1116         gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1117
1118         /* If the mtu is larger than the max size for standard
1119          * ethernet frames (ie, a jumbo frame), then set maccfg2
1120          * to allow huge frames, and to check the length */
1121         tempval = gfar_read(&priv->regs->maccfg2);
1122
1123         if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1124                 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1125         else
1126                 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1127
1128         gfar_write(&priv->regs->maccfg2, tempval);
1129
1130         if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1131                 startup_gfar(dev);
1132
1133         return 0;
1134 }
1135
1136 /* gfar_timeout gets called when a packet has not been
1137  * transmitted after a set amount of time.
1138  * For now, assume that clearing out all the structures, and
1139  * starting over will fix the problem. */
1140 static void gfar_timeout(struct net_device *dev)
1141 {
1142         struct gfar_private *priv = netdev_priv(dev);
1143
1144         priv->stats.tx_errors++;
1145
1146         if (dev->flags & IFF_UP) {
1147                 stop_gfar(dev);
1148                 startup_gfar(dev);
1149         }
1150
1151         if (!netif_queue_stopped(dev))
1152                 netif_schedule(dev);
1153 }
1154
1155 /* Interrupt Handler for Transmit complete */
1156 static irqreturn_t gfar_transmit(int irq, void *dev_id, struct pt_regs *regs)
1157 {
1158         struct net_device *dev = (struct net_device *) dev_id;
1159         struct gfar_private *priv = netdev_priv(dev);
1160         struct txbd8 *bdp;
1161
1162         /* Clear IEVENT */
1163         gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1164
1165         /* Lock priv */
1166         spin_lock(&priv->lock);
1167         bdp = priv->dirty_tx;
1168         while ((bdp->status & TXBD_READY) == 0) {
1169                 /* If dirty_tx and cur_tx are the same, then either the */
1170                 /* ring is empty or full now (it could only be full in the beginning, */
1171                 /* obviously).  If it is empty, we are done. */
1172                 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1173                         break;
1174
1175                 priv->stats.tx_packets++;
1176
1177                 /* Deferred means some collisions occurred during transmit, */
1178                 /* but we eventually sent the packet. */
1179                 if (bdp->status & TXBD_DEF)
1180                         priv->stats.collisions++;
1181
1182                 /* Free the sk buffer associated with this TxBD */
1183                 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1184                 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1185                 priv->skb_dirtytx =
1186                     (priv->skb_dirtytx +
1187                      1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1188
1189                 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1190                 if (bdp->status & TXBD_WRAP)
1191                         bdp = priv->tx_bd_base;
1192                 else
1193                         bdp++;
1194
1195                 /* Move dirty_tx to be the next bd */
1196                 priv->dirty_tx = bdp;
1197
1198                 /* We freed a buffer, so now we can restart transmission */
1199                 if (netif_queue_stopped(dev))
1200                         netif_wake_queue(dev);
1201         } /* while ((bdp->status & TXBD_READY) == 0) */
1202
1203         /* If we are coalescing the interrupts, reset the timer */
1204         /* Otherwise, clear it */
1205         if (priv->txcoalescing)
1206                 gfar_write(&priv->regs->txic,
1207                            mk_ic_value(priv->txcount, priv->txtime));
1208         else
1209                 gfar_write(&priv->regs->txic, 0);
1210
1211         spin_unlock(&priv->lock);
1212
1213         return IRQ_HANDLED;
1214 }
1215
1216 struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1217 {
1218         struct gfar_private *priv = netdev_priv(dev);
1219         struct sk_buff *skb = NULL;
1220         unsigned int timeout = SKB_ALLOC_TIMEOUT;
1221
1222         /* We have to allocate the skb, so keep trying till we succeed */
1223         while ((!skb) && timeout--)
1224                 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1225
1226         if (skb == NULL)
1227                 return NULL;
1228
1229         /* We need the data buffer to be aligned properly.  We will reserve
1230          * as many bytes as needed to align the data properly
1231          */
1232         skb_reserve(skb,
1233                     RXBUF_ALIGNMENT -
1234                     (((unsigned) skb->data) & (RXBUF_ALIGNMENT - 1)));
1235
1236         skb->dev = dev;
1237
1238         bdp->bufPtr = dma_map_single(NULL, skb->data,
1239                         priv->rx_buffer_size + RXBUF_ALIGNMENT, 
1240                         DMA_FROM_DEVICE);
1241
1242         bdp->length = 0;
1243
1244         /* Mark the buffer empty */
1245         bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1246
1247         return skb;
1248 }
1249
1250 static inline void count_errors(unsigned short status, struct gfar_private *priv)
1251 {
1252         struct net_device_stats *stats = &priv->stats;
1253         struct gfar_extra_stats *estats = &priv->extra_stats;
1254
1255         /* If the packet was truncated, none of the other errors
1256          * matter */
1257         if (status & RXBD_TRUNCATED) {
1258                 stats->rx_length_errors++;
1259
1260                 estats->rx_trunc++;
1261
1262                 return;
1263         }
1264         /* Count the errors, if there were any */
1265         if (status & (RXBD_LARGE | RXBD_SHORT)) {
1266                 stats->rx_length_errors++;
1267
1268                 if (status & RXBD_LARGE)
1269                         estats->rx_large++;
1270                 else
1271                         estats->rx_short++;
1272         }
1273         if (status & RXBD_NONOCTET) {
1274                 stats->rx_frame_errors++;
1275                 estats->rx_nonoctet++;
1276         }
1277         if (status & RXBD_CRCERR) {
1278                 estats->rx_crcerr++;
1279                 stats->rx_crc_errors++;
1280         }
1281         if (status & RXBD_OVERRUN) {
1282                 estats->rx_overrun++;
1283                 stats->rx_crc_errors++;
1284         }
1285 }
1286
1287 irqreturn_t gfar_receive(int irq, void *dev_id, struct pt_regs *regs)
1288 {
1289         struct net_device *dev = (struct net_device *) dev_id;
1290         struct gfar_private *priv = netdev_priv(dev);
1291
1292 #ifdef CONFIG_GFAR_NAPI
1293         u32 tempval;
1294 #endif
1295
1296         /* Clear IEVENT, so rx interrupt isn't called again
1297          * because of this interrupt */
1298         gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1299
1300         /* support NAPI */
1301 #ifdef CONFIG_GFAR_NAPI
1302         if (netif_rx_schedule_prep(dev)) {
1303                 tempval = gfar_read(&priv->regs->imask);
1304                 tempval &= IMASK_RX_DISABLED;
1305                 gfar_write(&priv->regs->imask, tempval);
1306
1307                 __netif_rx_schedule(dev);
1308         } else {
1309 #ifdef VERBOSE_GFAR_ERRORS
1310                 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1311                        dev->name, gfar_read(priv->regs->ievent),
1312                        gfar_read(priv->regs->imask));
1313 #endif
1314         }
1315 #else
1316
1317         spin_lock(&priv->lock);
1318         gfar_clean_rx_ring(dev);
1319
1320         /* If we are coalescing interrupts, update the timer */
1321         /* Otherwise, clear it */
1322         if (priv->rxcoalescing)
1323                 gfar_write(&priv->regs->rxic,
1324                            mk_ic_value(priv->rxcount, priv->rxtime));
1325         else
1326                 gfar_write(&priv->regs->rxic, 0);
1327
1328         /* Just in case we need to wake the ring param changer */
1329         priv->rxclean = 1;
1330
1331         spin_unlock(&priv->lock);
1332 #endif
1333
1334         return IRQ_HANDLED;
1335 }
1336
1337
1338 /* gfar_process_frame() -- handle one incoming packet if skb
1339  * isn't NULL.  Try the fastroute before using the stack */
1340 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1341                 int length)
1342 {
1343         struct gfar_private *priv = netdev_priv(dev);
1344
1345         if (skb == NULL) {
1346 #ifdef BRIEF_GFAR_ERRORS
1347                 printk(KERN_WARNING "%s: Missing skb!!.\n",
1348                                 dev->name);
1349 #endif
1350                 priv->stats.rx_dropped++;
1351                 priv->extra_stats.rx_skbmissing++;
1352         } else {
1353                 if(try_fastroute(skb, dev, length) == 0) {
1354                         /* Prep the skb for the packet */
1355                         skb_put(skb, length);
1356
1357                         /* Tell the skb what kind of packet this is */
1358                         skb->protocol = eth_type_trans(skb, dev);
1359
1360                         /* Send the packet up the stack */
1361                         if (RECEIVE(skb) == NET_RX_DROP) {
1362                                 priv->extra_stats.kernel_dropped++;
1363                         }
1364                 }
1365         }
1366
1367         return 0;
1368 }
1369
1370 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
1371  *   until all are gone (or, in the case of NAPI, the budget/quota
1372  *   has been reached). Returns the number of frames handled
1373  */
1374 #ifdef CONFIG_GFAR_NAPI
1375 static int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
1376 #else
1377 static int gfar_clean_rx_ring(struct net_device *dev)
1378 #endif
1379 {
1380         struct rxbd8 *bdp;
1381         struct sk_buff *skb;
1382         u16 pkt_len;
1383         int howmany = 0;
1384         struct gfar_private *priv = netdev_priv(dev);
1385
1386         /* Get the first full descriptor */
1387         bdp = priv->cur_rx;
1388
1389 #ifdef CONFIG_GFAR_NAPI
1390 #define GFAR_RXDONE() ((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))
1391 #else
1392 #define GFAR_RXDONE() (bdp->status & RXBD_EMPTY)
1393 #endif
1394         while (!GFAR_RXDONE()) {
1395                 skb = priv->rx_skbuff[priv->skb_currx];
1396
1397                 if (!(bdp->status &
1398                       (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1399                        | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1400                         /* Increment the number of packets */
1401                         priv->stats.rx_packets++;
1402                         howmany++;
1403
1404                         /* Remove the FCS from the packet length */
1405                         pkt_len = bdp->length - 4;
1406
1407                         gfar_process_frame(dev, skb, pkt_len);
1408
1409                         priv->stats.rx_bytes += pkt_len;
1410
1411                 } else {
1412                         count_errors(bdp->status, priv);
1413
1414                         if (skb)
1415                                 dev_kfree_skb_any(skb);
1416
1417                         priv->rx_skbuff[priv->skb_currx] = NULL;
1418                 }
1419
1420                 dev->last_rx = jiffies;
1421
1422                 /* Clear the status flags for this buffer */
1423                 bdp->status &= ~RXBD_STATS;
1424
1425                 /* Add another skb for the future */
1426                 skb = gfar_new_skb(dev, bdp);
1427                 priv->rx_skbuff[priv->skb_currx] = skb;
1428
1429                 /* Update to the next pointer */
1430                 if (bdp->status & RXBD_WRAP)
1431                         bdp = priv->rx_bd_base;
1432                 else
1433                         bdp++;
1434
1435                 /* update to point at the next skb */
1436                 priv->skb_currx =
1437                     (priv->skb_currx +
1438                      1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1439
1440         }
1441
1442         /* Update the current rxbd pointer to be the next one */
1443         priv->cur_rx = bdp;
1444
1445         /* If no packets have arrived since the
1446          * last one we processed, clear the IEVENT RX and
1447          * BSY bits so that another interrupt won't be
1448          * generated when we set IMASK */
1449         if (bdp->status & RXBD_EMPTY)
1450                 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1451
1452         return howmany;
1453 }
1454
1455 #ifdef CONFIG_GFAR_NAPI
1456 static int gfar_poll(struct net_device *dev, int *budget)
1457 {
1458         int howmany;
1459         struct gfar_private *priv = netdev_priv(dev);
1460         int rx_work_limit = *budget;
1461
1462         if (rx_work_limit > dev->quota)
1463                 rx_work_limit = dev->quota;
1464
1465         spin_lock(&priv->lock);
1466         howmany = gfar_clean_rx_ring(dev, rx_work_limit);
1467
1468         dev->quota -= howmany;
1469         rx_work_limit -= howmany;
1470         *budget -= howmany;
1471
1472         if (rx_work_limit >= 0) {
1473                 netif_rx_complete(dev);
1474
1475                 /* Clear the halt bit in RSTAT */
1476                 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1477
1478                 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1479
1480                 /* If we are coalescing interrupts, update the timer */
1481                 /* Otherwise, clear it */
1482                 if (priv->rxcoalescing)
1483                         gfar_write(&priv->regs->rxic,
1484                                    mk_ic_value(priv->rxcount, priv->rxtime));
1485                 else
1486                         gfar_write(&priv->regs->rxic, 0);
1487
1488                 /* Signal to the ring size changer that it's safe to go */
1489                 priv->rxclean = 1;
1490         }
1491
1492         spin_unlock(priv->lock);
1493
1494         return (rx_work_limit < 0) ? 1 : 0;
1495 }
1496 #endif
1497
1498 /* The interrupt handler for devices with one interrupt */
1499 static irqreturn_t gfar_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1500 {
1501         struct net_device *dev = dev_id;
1502         struct gfar_private *priv = netdev_priv(dev);
1503
1504         /* Save ievent for future reference */
1505         u32 events = gfar_read(&priv->regs->ievent);
1506
1507         /* Clear IEVENT */
1508         gfar_write(&priv->regs->ievent, events);
1509
1510         /* Check for reception */
1511         if ((events & IEVENT_RXF0) || (events & IEVENT_RXB0))
1512                 gfar_receive(irq, dev_id, regs);
1513
1514         /* Check for transmit completion */
1515         if ((events & IEVENT_TXF) || (events & IEVENT_TXB))
1516                 gfar_transmit(irq, dev_id, regs);
1517
1518         /* Update error statistics */
1519         if (events & IEVENT_TXE) {
1520                 priv->stats.tx_errors++;
1521
1522                 if (events & IEVENT_LC)
1523                         priv->stats.tx_window_errors++;
1524                 if (events & IEVENT_CRL)
1525                         priv->stats.tx_aborted_errors++;
1526                 if (events & IEVENT_XFUN) {
1527 #ifdef VERBOSE_GFAR_ERRORS
1528                         printk(KERN_WARNING "%s: tx underrun. dropped packet\n",
1529                                dev->name);
1530 #endif
1531                         priv->stats.tx_dropped++;
1532                         priv->extra_stats.tx_underrun++;
1533
1534                         /* Reactivate the Tx Queues */
1535                         gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1536                 }
1537         }
1538         if (events & IEVENT_BSY) {
1539                 priv->stats.rx_errors++;
1540                 priv->extra_stats.rx_bsy++;
1541
1542                 gfar_receive(irq, dev_id, regs);
1543
1544 #ifndef CONFIG_GFAR_NAPI
1545                 /* Clear the halt bit in RSTAT */
1546                 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1547 #endif
1548
1549 #ifdef VERBOSE_GFAR_ERRORS
1550                 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n", dev->name,
1551                        gfar_read(priv->regs->rstat));
1552 #endif
1553         }
1554         if (events & IEVENT_BABR) {
1555                 priv->stats.rx_errors++;
1556                 priv->extra_stats.rx_babr++;
1557
1558 #ifdef VERBOSE_GFAR_ERRORS
1559                 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
1560 #endif
1561         }
1562         if (events & IEVENT_EBERR) {
1563                 priv->extra_stats.eberr++;
1564 #ifdef VERBOSE_GFAR_ERRORS
1565                 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
1566 #endif
1567         }
1568         if (events & IEVENT_RXC) {
1569 #ifdef VERBOSE_GFAR_ERRORS
1570                 printk(KERN_DEBUG "%s: control frame\n", dev->name);
1571 #endif
1572         }
1573
1574         if (events & IEVENT_BABT) {
1575                 priv->extra_stats.tx_babt++;
1576 #ifdef VERBOSE_GFAR_ERRORS
1577                 printk(KERN_DEBUG "%s: babt error\n", dev->name);
1578 #endif
1579         }
1580
1581         return IRQ_HANDLED;
1582 }
1583
1584 static irqreturn_t phy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1585 {
1586         struct net_device *dev = (struct net_device *) dev_id;
1587         struct gfar_private *priv = netdev_priv(dev);
1588
1589         /* Run the commands which acknowledge the interrupt */
1590         phy_run_commands(dev, priv->phyinfo->ack_int);
1591
1592         /* Schedule the bottom half */
1593         schedule_work(&priv->tq);
1594
1595         return IRQ_HANDLED;
1596 }
1597
1598 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
1599 static void gfar_phy_change(void *data)
1600 {
1601         struct net_device *dev = (struct net_device *) data;
1602         struct gfar_private *priv = netdev_priv(dev);
1603         int timeout = HZ / 1000 + 1;
1604
1605         /* Delay to give the PHY a chance to change the
1606          * register state */
1607         set_current_state(TASK_UNINTERRUPTIBLE);
1608         schedule_timeout(timeout);
1609
1610         /* Run the commands which check the link state */
1611         phy_run_commands(dev, priv->phyinfo->handle_int);
1612
1613         /* React to the change in state */
1614         adjust_link(dev);
1615 }
1616
1617 /* Called every so often on systems that don't interrupt
1618  * the core for PHY changes */
1619 static void gfar_phy_timer(unsigned long data)
1620 {
1621         struct net_device *dev = (struct net_device *) data;
1622         struct gfar_private *priv = netdev_priv(dev);
1623
1624         schedule_work(&priv->tq);
1625
1626         mod_timer(&priv->phy_info_timer, jiffies + 2 * HZ);
1627 }
1628
1629 /* Called every time the controller might need to be made
1630  * aware of new link state.  The PHY code conveys this
1631  * information through variables in the priv structure, and this
1632  * function converts those variables into the appropriate
1633  * register values, and can bring down the device if needed.
1634  */
1635 static void adjust_link(struct net_device *dev)
1636 {
1637         struct gfar_private *priv = netdev_priv(dev);
1638         struct gfar *regs = priv->regs;
1639         u32 tempval;
1640
1641         if (priv->link) {
1642                 /* Now we make sure that we can be in full duplex mode.
1643                  * If not, we operate in half-duplex mode. */
1644                 if (priv->duplexity != priv->olddplx) {
1645                         if (!(priv->duplexity)) {
1646                                 tempval = gfar_read(&regs->maccfg2);
1647                                 tempval &= ~(MACCFG2_FULL_DUPLEX);
1648                                 gfar_write(&regs->maccfg2, tempval);
1649
1650                                 printk(KERN_INFO "%s: Half Duplex\n",
1651                                        dev->name);
1652                         } else {
1653                                 tempval = gfar_read(&regs->maccfg2);
1654                                 tempval |= MACCFG2_FULL_DUPLEX;
1655                                 gfar_write(&regs->maccfg2, tempval);
1656
1657                                 printk(KERN_INFO "%s: Full Duplex\n",
1658                                        dev->name);
1659                         }
1660
1661                         priv->olddplx = priv->duplexity;
1662                 }
1663
1664                 if (priv->speed != priv->oldspeed) {
1665                         switch (priv->speed) {
1666                         case 1000:
1667                                 tempval = gfar_read(&regs->maccfg2);
1668                                 tempval =
1669                                     ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
1670                                 gfar_write(&regs->maccfg2, tempval);
1671                                 break;
1672                         case 100:
1673                         case 10:
1674                                 tempval = gfar_read(&regs->maccfg2);
1675                                 tempval =
1676                                     ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
1677                                 gfar_write(&regs->maccfg2, tempval);
1678                                 break;
1679                         default:
1680                                 printk(KERN_WARNING
1681                                        "%s: Ack!  Speed (%d) is not 10/100/1000!\n",
1682                                        dev->name, priv->speed);
1683                                 break;
1684                         }
1685
1686                         printk(KERN_INFO "%s: Speed %dBT\n", dev->name,
1687                                priv->speed);
1688
1689                         priv->oldspeed = priv->speed;
1690                 }
1691
1692                 if (!priv->oldlink) {
1693                         printk(KERN_INFO "%s: Link is up\n", dev->name);
1694                         priv->oldlink = 1;
1695                         netif_carrier_on(dev);
1696                         netif_schedule(dev);
1697                 }
1698         } else {
1699                 if (priv->oldlink) {
1700                         printk(KERN_INFO "%s: Link is down\n", dev->name);
1701                         priv->oldlink = 0;
1702                         priv->oldspeed = 0;
1703                         priv->olddplx = -1;
1704                         netif_carrier_off(dev);
1705                 }
1706         }
1707
1708 #ifdef VERBOSE_GFAR_ERRORS
1709         printk(KERN_INFO "%s: Link now %s; %dBT %s-duplex\n",
1710                dev->name, priv->link ? "up" : "down", priv->speed, priv->duplexity ? "full" : "half");
1711 #endif
1712 }
1713
1714
1715 /* Update the hash table based on the current list of multicast
1716  * addresses we subscribe to.  Also, change the promiscuity of
1717  * the device based on the flags (this function is called
1718  * whenever dev->flags is changed */
1719 static void gfar_set_multi(struct net_device *dev)
1720 {
1721         struct dev_mc_list *mc_ptr;
1722         struct gfar_private *priv = netdev_priv(dev);
1723         struct gfar *regs = priv->regs;
1724         u32 tempval;
1725
1726         if(dev->flags & IFF_PROMISC) {
1727                 printk(KERN_INFO "%s: Entering promiscuous mode.\n",
1728                                 dev->name);
1729                 /* Set RCTRL to PROM */
1730                 tempval = gfar_read(&regs->rctrl);
1731                 tempval |= RCTRL_PROM;
1732                 gfar_write(&regs->rctrl, tempval);
1733         } else {
1734                 /* Set RCTRL to not PROM */
1735                 tempval = gfar_read(&regs->rctrl);
1736                 tempval &= ~(RCTRL_PROM);
1737                 gfar_write(&regs->rctrl, tempval);
1738         }
1739         
1740         if(dev->flags & IFF_ALLMULTI) {
1741                 /* Set the hash to rx all multicast frames */
1742                 gfar_write(&regs->gaddr0, 0xffffffff);
1743                 gfar_write(&regs->gaddr1, 0xffffffff);
1744                 gfar_write(&regs->gaddr2, 0xffffffff);
1745                 gfar_write(&regs->gaddr3, 0xffffffff);
1746                 gfar_write(&regs->gaddr4, 0xffffffff);
1747                 gfar_write(&regs->gaddr5, 0xffffffff);
1748                 gfar_write(&regs->gaddr6, 0xffffffff);
1749                 gfar_write(&regs->gaddr7, 0xffffffff);
1750         } else {
1751                 /* zero out the hash */
1752                 gfar_write(&regs->gaddr0, 0x0);
1753                 gfar_write(&regs->gaddr1, 0x0);
1754                 gfar_write(&regs->gaddr2, 0x0);
1755                 gfar_write(&regs->gaddr3, 0x0);
1756                 gfar_write(&regs->gaddr4, 0x0);
1757                 gfar_write(&regs->gaddr5, 0x0);
1758                 gfar_write(&regs->gaddr6, 0x0);
1759                 gfar_write(&regs->gaddr7, 0x0);
1760
1761                 if(dev->mc_count == 0)
1762                         return;
1763
1764                 /* Parse the list, and set the appropriate bits */
1765                 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
1766                         gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
1767                 }
1768         }
1769
1770         return;
1771 }
1772
1773 /* Set the appropriate hash bit for the given addr */
1774 /* The algorithm works like so:
1775  * 1) Take the Destination Address (ie the multicast address), and
1776  * do a CRC on it (little endian), and reverse the bits of the
1777  * result.
1778  * 2) Use the 8 most significant bits as a hash into a 256-entry
1779  * table.  The table is controlled through 8 32-bit registers:
1780  * gaddr0-7.  gaddr0's MSB is entry 0, and gaddr7's LSB is
1781  * gaddr7.  This means that the 3 most significant bits in the
1782  * hash index which gaddr register to use, and the 5 other bits
1783  * indicate which bit (assuming an IBM numbering scheme, which
1784  * for PowerPC (tm) is usually the case) in the register holds
1785  * the entry. */
1786 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1787 {
1788         u32 tempval;
1789         struct gfar_private *priv = netdev_priv(dev);
1790         struct gfar *regs = priv->regs;
1791         u32 *hash = &regs->gaddr0;
1792         u32 result = ether_crc(MAC_ADDR_LEN, addr);
1793         u8 whichreg = ((result >> 29) & 0x7);
1794         u8 whichbit = ((result >> 24) & 0x1f);
1795         u32 value = (1 << (31-whichbit));
1796
1797         tempval = gfar_read(&hash[whichreg]);
1798         tempval |= value;
1799         gfar_write(&hash[whichreg], tempval);
1800
1801         return;
1802 }
1803
1804 /* GFAR error interrupt handler */
1805 static irqreturn_t gfar_error(int irq, void *dev_id, struct pt_regs *regs)
1806 {
1807         struct net_device *dev = dev_id;
1808         struct gfar_private *priv = netdev_priv(dev);
1809
1810         /* Save ievent for future reference */
1811         u32 events = gfar_read(&priv->regs->ievent);
1812
1813         /* Clear IEVENT */
1814         gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1815
1816         /* Hmm... */
1817 #if defined (BRIEF_GFAR_ERRORS) || defined (VERBOSE_GFAR_ERRORS)
1818         printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
1819                dev->name, events, gfar_read(priv->regs->imask));
1820 #endif
1821
1822         /* Update the error counters */
1823         if (events & IEVENT_TXE) {
1824                 priv->stats.tx_errors++;
1825
1826                 if (events & IEVENT_LC)
1827                         priv->stats.tx_window_errors++;
1828                 if (events & IEVENT_CRL)
1829                         priv->stats.tx_aborted_errors++;
1830                 if (events & IEVENT_XFUN) {
1831 #ifdef VERBOSE_GFAR_ERRORS
1832                         printk(KERN_DEBUG "%s: underrun.  packet dropped.\n",
1833                                dev->name);
1834 #endif
1835                         priv->stats.tx_dropped++;
1836                         priv->extra_stats.tx_underrun++;
1837
1838                         /* Reactivate the Tx Queues */
1839                         gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1840                 }
1841 #ifdef VERBOSE_GFAR_ERRORS
1842                 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
1843 #endif
1844         }
1845         if (events & IEVENT_BSY) {
1846                 priv->stats.rx_errors++;
1847                 priv->extra_stats.rx_bsy++;
1848
1849                 gfar_receive(irq, dev_id, regs);
1850
1851 #ifndef CONFIG_GFAR_NAPI
1852                 /* Clear the halt bit in RSTAT */
1853                 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1854 #endif
1855
1856 #ifdef VERBOSE_GFAR_ERRORS
1857                 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n", dev->name,
1858                        gfar_read(priv->regs->rstat));
1859 #endif
1860         }
1861         if (events & IEVENT_BABR) {
1862                 priv->stats.rx_errors++;
1863                 priv->extra_stats.rx_babr++;
1864
1865 #ifdef VERBOSE_GFAR_ERRORS
1866                 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
1867 #endif
1868         }
1869         if (events & IEVENT_EBERR) {
1870                 priv->extra_stats.eberr++;
1871 #ifdef VERBOSE_GFAR_ERRORS
1872                 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
1873 #endif
1874         }
1875         if (events & IEVENT_RXC)
1876 #ifdef VERBOSE_GFAR_ERRORS
1877                 printk(KERN_DEBUG "%s: control frame\n", dev->name);
1878 #endif
1879
1880         if (events & IEVENT_BABT) {
1881                 priv->extra_stats.tx_babt++;
1882 #ifdef VERBOSE_GFAR_ERRORS
1883                 printk(KERN_DEBUG "%s: babt error\n", dev->name);
1884 #endif
1885         }
1886         return IRQ_HANDLED;
1887 }
1888
1889 /* Structure for a device driver */
1890 static struct ocp_device_id gfar_ids[] = {
1891         {.vendor = OCP_ANY_ID,.function = OCP_FUNC_GFAR},
1892         {.vendor = OCP_VENDOR_INVALID}
1893 };
1894
1895 static struct ocp_driver gfar_driver = {
1896         .name = "gianfar",
1897         .id_table = gfar_ids,
1898
1899         .probe = gfar_probe,
1900         .remove = gfar_remove,
1901 };
1902
1903 static int __init gfar_init(void)
1904 {
1905         int rc;
1906
1907         rc = ocp_register_driver(&gfar_driver);
1908 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,41)
1909         if (rc != 0) {
1910 #else
1911         if (rc == 0) {
1912 #endif
1913                 ocp_unregister_driver(&gfar_driver);
1914                 return -ENODEV;
1915         }
1916
1917         return 0;
1918 }
1919
1920 static void __exit gfar_exit(void)
1921 {
1922         ocp_unregister_driver(&gfar_driver);
1923 }
1924
1925 module_init(gfar_init);
1926 module_exit(gfar_exit);