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