2 * drivers/net/gianfar.c
4 * Gianfar Ethernet Driver
5 * Driver for FEC on MPC8540 and TSEC on MPC8540/MPC8560
6 * Based on 8260_io/fcc_enet.c
9 * Maintainer: Kumar Gala
11 * Copyright (c) 2002-2004 Freescale Semiconductor, Inc.
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.
18 * Gianfar: AKA Lambda Draconis, "Dragon"
25 * This driver is designed for the non-CPM ethernet controllers
26 * on the 85xx and 83xx family of integrated processors
28 * The driver is initialized through platform_device. Structures which
29 * define the configuration needed by the board are defined in a
30 * board structure in arch/ppc/platforms (though I do not
31 * discount the possibility that other architectures could one
34 * The Gianfar Ethernet Controller uses a ring of buffer
35 * descriptors. The beginning is indicated by a register
36 * pointing to the physical address of the start of the ring.
37 * The end is determined by a "wrap" bit being set in the
38 * last descriptor of the ring.
40 * When a packet is received, the RXF bit in the
41 * IEVENT register is set, triggering an interrupt when the
42 * corresponding bit in the IMASK register is also set (if
43 * interrupt coalescing is active, then the interrupt may not
44 * happen immediately, but will wait until either a set number
45 * of frames or amount of time have passed). In NAPI, the
46 * interrupt handler will signal there is work to be done, and
47 * exit. Without NAPI, the packet(s) will be handled
48 * immediately. Both methods will start at the last known empty
49 * descriptor, and process every subsequent descriptor until there
50 * are none left with data (NAPI will stop after a set number of
51 * packets to give time to other tasks, but will eventually
52 * process all the packets). The data arrives inside a
53 * pre-allocated skb, and so after the skb is passed up to the
54 * stack, a new skb must be allocated, and the address field in
55 * the buffer descriptor must be updated to indicate this new
58 * When the kernel requests that a packet be transmitted, the
59 * driver starts where it left off last time, and points the
60 * descriptor at the buffer which was passed in. The driver
61 * then informs the DMA engine that there are packets ready to
62 * be transmitted. Once the controller is finished transmitting
63 * the packet, an interrupt may be triggered (under the same
64 * conditions as for reception, but depending on the TXF bit).
65 * The driver then cleans up the buffer.
68 #include <linux/config.h>
69 #include <linux/kernel.h>
70 #include <linux/sched.h>
71 #include <linux/string.h>
72 #include <linux/errno.h>
73 #include <linux/unistd.h>
74 #include <linux/slab.h>
75 #include <linux/interrupt.h>
76 #include <linux/init.h>
77 #include <linux/delay.h>
78 #include <linux/netdevice.h>
79 #include <linux/etherdevice.h>
80 #include <linux/skbuff.h>
81 #include <linux/if_vlan.h>
82 #include <linux/spinlock.h>
84 #include <linux/platform_device.h>
86 #include <linux/tcp.h>
87 #include <linux/udp.h>
91 #include <asm/uaccess.h>
92 #include <linux/module.h>
93 #include <linux/dma-mapping.h>
94 #include <linux/crc32.h>
95 #include <linux/mii.h>
96 #include <linux/phy.h>
99 #include "gianfar_mii.h"
101 #define TX_TIMEOUT (1*HZ)
102 #define SKB_ALLOC_TIMEOUT 1000000
103 #undef BRIEF_GFAR_ERRORS
104 #undef VERBOSE_GFAR_ERRORS
106 #ifdef CONFIG_GFAR_NAPI
107 #define RECEIVE(x) netif_receive_skb(x)
109 #define RECEIVE(x) netif_rx(x)
112 const char gfar_driver_name[] = "Gianfar Ethernet";
113 const char gfar_driver_version[] = "1.2";
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 static irqreturn_t gfar_interrupt(int irq, void *dev_id, struct pt_regs *regs);
126 static void adjust_link(struct net_device *dev);
127 static void init_registers(struct net_device *dev);
128 static int init_phy(struct net_device *dev);
129 static int gfar_probe(struct platform_device *pdev);
130 static int gfar_remove(struct platform_device *pdev);
131 static void free_skb_resources(struct gfar_private *priv);
132 static void gfar_set_multi(struct net_device *dev);
133 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
134 #ifdef CONFIG_GFAR_NAPI
135 static int gfar_poll(struct net_device *dev, int *budget);
137 int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
138 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
139 static void gfar_vlan_rx_register(struct net_device *netdev,
140 struct vlan_group *grp);
141 static void gfar_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
143 extern struct ethtool_ops gfar_ethtool_ops;
145 MODULE_AUTHOR("Freescale Semiconductor, Inc");
146 MODULE_DESCRIPTION("Gianfar Ethernet Driver");
147 MODULE_LICENSE("GPL");
149 int gfar_uses_fcb(struct gfar_private *priv)
151 if (priv->vlan_enable || priv->rx_csum_enable)
157 /* Set up the ethernet device structure, private data,
158 * and anything else we need before we start */
159 static int gfar_probe(struct platform_device *pdev)
162 struct net_device *dev = NULL;
163 struct gfar_private *priv = NULL;
164 struct gianfar_platform_data *einfo;
169 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
172 printk(KERN_ERR "gfar %d: Missing additional data!\n",
178 /* Create an ethernet device instance */
179 dev = alloc_etherdev(sizeof (*priv));
184 priv = netdev_priv(dev);
186 /* Set the info in the priv to the current info */
189 /* fill out IRQ fields */
190 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
191 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
192 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
193 priv->interruptError = platform_get_irq_byname(pdev, "error");
195 priv->interruptTransmit = platform_get_irq(pdev, 0);
198 /* get a pointer to the register memory */
199 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 priv->regs = (struct gfar *)
201 ioremap(r->start, sizeof (struct gfar));
203 if (NULL == priv->regs) {
208 spin_lock_init(&priv->lock);
210 platform_set_drvdata(pdev, dev);
212 /* Stop the DMA engine now, in case it was running before */
213 /* (The firmware could have used it, and left it running). */
214 /* To do this, we write Graceful Receive Stop and Graceful */
215 /* Transmit Stop, and then wait until the corresponding bits */
216 /* in IEVENT indicate the stops have completed. */
217 tempval = gfar_read(&priv->regs->dmactrl);
218 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
219 gfar_write(&priv->regs->dmactrl, tempval);
221 tempval = gfar_read(&priv->regs->dmactrl);
222 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
223 gfar_write(&priv->regs->dmactrl, tempval);
225 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
228 /* Reset MAC layer */
229 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
231 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
232 gfar_write(&priv->regs->maccfg1, tempval);
234 /* Initialize MACCFG2. */
235 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
237 /* Initialize ECNTRL */
238 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
240 /* Copy the station address into the dev structure, */
241 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
243 /* Set the dev->base_addr to the gfar reg region */
244 dev->base_addr = (unsigned long) (priv->regs);
246 SET_MODULE_OWNER(dev);
247 SET_NETDEV_DEV(dev, &pdev->dev);
249 /* Fill in the dev structure */
250 dev->open = gfar_enet_open;
251 dev->hard_start_xmit = gfar_start_xmit;
252 dev->tx_timeout = gfar_timeout;
253 dev->watchdog_timeo = TX_TIMEOUT;
254 #ifdef CONFIG_GFAR_NAPI
255 dev->poll = gfar_poll;
256 dev->weight = GFAR_DEV_WEIGHT;
258 dev->stop = gfar_close;
259 dev->get_stats = gfar_get_stats;
260 dev->change_mtu = gfar_change_mtu;
262 dev->set_multicast_list = gfar_set_multi;
264 dev->ethtool_ops = &gfar_ethtool_ops;
266 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
267 priv->rx_csum_enable = 1;
268 dev->features |= NETIF_F_IP_CSUM;
270 priv->rx_csum_enable = 0;
274 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
275 dev->vlan_rx_register = gfar_vlan_rx_register;
276 dev->vlan_rx_kill_vid = gfar_vlan_rx_kill_vid;
278 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
280 priv->vlan_enable = 1;
283 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
284 priv->extended_hash = 1;
285 priv->hash_width = 9;
287 priv->hash_regs[0] = &priv->regs->igaddr0;
288 priv->hash_regs[1] = &priv->regs->igaddr1;
289 priv->hash_regs[2] = &priv->regs->igaddr2;
290 priv->hash_regs[3] = &priv->regs->igaddr3;
291 priv->hash_regs[4] = &priv->regs->igaddr4;
292 priv->hash_regs[5] = &priv->regs->igaddr5;
293 priv->hash_regs[6] = &priv->regs->igaddr6;
294 priv->hash_regs[7] = &priv->regs->igaddr7;
295 priv->hash_regs[8] = &priv->regs->gaddr0;
296 priv->hash_regs[9] = &priv->regs->gaddr1;
297 priv->hash_regs[10] = &priv->regs->gaddr2;
298 priv->hash_regs[11] = &priv->regs->gaddr3;
299 priv->hash_regs[12] = &priv->regs->gaddr4;
300 priv->hash_regs[13] = &priv->regs->gaddr5;
301 priv->hash_regs[14] = &priv->regs->gaddr6;
302 priv->hash_regs[15] = &priv->regs->gaddr7;
305 priv->extended_hash = 0;
306 priv->hash_width = 8;
308 priv->hash_regs[0] = &priv->regs->gaddr0;
309 priv->hash_regs[1] = &priv->regs->gaddr1;
310 priv->hash_regs[2] = &priv->regs->gaddr2;
311 priv->hash_regs[3] = &priv->regs->gaddr3;
312 priv->hash_regs[4] = &priv->regs->gaddr4;
313 priv->hash_regs[5] = &priv->regs->gaddr5;
314 priv->hash_regs[6] = &priv->regs->gaddr6;
315 priv->hash_regs[7] = &priv->regs->gaddr7;
318 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
319 priv->padding = DEFAULT_PADDING;
323 dev->hard_header_len += priv->padding;
325 if (dev->features & NETIF_F_IP_CSUM)
326 dev->hard_header_len += GMAC_FCB_LEN;
328 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
329 #ifdef CONFIG_GFAR_BUFSTASH
330 priv->rx_stash_size = STASH_LENGTH;
332 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
333 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
335 priv->txcoalescing = DEFAULT_TX_COALESCE;
336 priv->txcount = DEFAULT_TXCOUNT;
337 priv->txtime = DEFAULT_TXTIME;
338 priv->rxcoalescing = DEFAULT_RX_COALESCE;
339 priv->rxcount = DEFAULT_RXCOUNT;
340 priv->rxtime = DEFAULT_RXTIME;
342 /* Enable most messages by default */
343 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
345 err = register_netdev(dev);
348 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
353 /* Print out the device info */
354 printk(KERN_INFO DEVICE_NAME, dev->name);
355 for (idx = 0; idx < 6; idx++)
356 printk("%2.2x%c", dev->dev_addr[idx], idx == 5 ? ' ' : ':');
359 /* Even more device info helps when determining which kernel */
360 /* provided which set of benchmarks. Since this is global for all */
361 /* devices, we only print it once */
362 #ifdef CONFIG_GFAR_NAPI
363 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
365 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
367 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
368 dev->name, priv->rx_ring_size, priv->tx_ring_size);
373 iounmap((void *) priv->regs);
379 static int gfar_remove(struct platform_device *pdev)
381 struct net_device *dev = platform_get_drvdata(pdev);
382 struct gfar_private *priv = netdev_priv(dev);
384 platform_set_drvdata(pdev, NULL);
386 iounmap((void *) priv->regs);
393 /* Initializes driver's PHY state, and attaches to the PHY.
394 * Returns 0 on success.
396 static int init_phy(struct net_device *dev)
398 struct gfar_private *priv = netdev_priv(dev);
399 uint gigabit_support =
400 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
401 SUPPORTED_1000baseT_Full : 0;
402 struct phy_device *phydev;
406 priv->oldduplex = -1;
408 phydev = phy_connect(dev, priv->einfo->bus_id, &adjust_link, 0);
410 if (IS_ERR(phydev)) {
411 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
412 return PTR_ERR(phydev);
415 /* Remove any features not supported by the controller */
416 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
417 phydev->advertising = phydev->supported;
419 priv->phydev = phydev;
424 static void init_registers(struct net_device *dev)
426 struct gfar_private *priv = netdev_priv(dev);
429 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
431 /* Initialize IMASK */
432 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
434 /* Init hash registers to zero */
435 gfar_write(&priv->regs->igaddr0, 0);
436 gfar_write(&priv->regs->igaddr1, 0);
437 gfar_write(&priv->regs->igaddr2, 0);
438 gfar_write(&priv->regs->igaddr3, 0);
439 gfar_write(&priv->regs->igaddr4, 0);
440 gfar_write(&priv->regs->igaddr5, 0);
441 gfar_write(&priv->regs->igaddr6, 0);
442 gfar_write(&priv->regs->igaddr7, 0);
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);
453 /* Zero out the rmon mib registers if it has them */
454 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
455 memset((void *) &(priv->regs->rmon), 0,
456 sizeof (struct rmon_mib));
458 /* Mask off the CAM interrupts */
459 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
460 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
463 /* Initialize the max receive buffer length */
464 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
466 #ifdef CONFIG_GFAR_BUFSTASH
467 /* If we are stashing buffers, we need to set the
468 * extraction length to the size of the buffer */
469 gfar_write(&priv->regs->attreli, priv->rx_stash_size << 16);
472 /* Initialize the Minimum Frame Length Register */
473 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
475 /* Setup Attributes so that snooping is on for rx */
476 gfar_write(&priv->regs->attr, ATTR_INIT_SETTINGS);
477 gfar_write(&priv->regs->attreli, ATTRELI_INIT_SETTINGS);
479 /* Assign the TBI an address which won't conflict with the PHYs */
480 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
484 /* Halt the receive and transmit queues */
485 void gfar_halt(struct net_device *dev)
487 struct gfar_private *priv = netdev_priv(dev);
488 struct gfar *regs = priv->regs;
491 /* Mask all interrupts */
492 gfar_write(®s->imask, IMASK_INIT_CLEAR);
494 /* Clear all interrupts */
495 gfar_write(®s->ievent, IEVENT_INIT_CLEAR);
497 /* Stop the DMA, and wait for it to stop */
498 tempval = gfar_read(&priv->regs->dmactrl);
499 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
500 != (DMACTRL_GRS | DMACTRL_GTS)) {
501 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
502 gfar_write(&priv->regs->dmactrl, tempval);
504 while (!(gfar_read(&priv->regs->ievent) &
505 (IEVENT_GRSC | IEVENT_GTSC)))
509 /* Disable Rx and Tx */
510 tempval = gfar_read(®s->maccfg1);
511 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
512 gfar_write(®s->maccfg1, tempval);
515 void stop_gfar(struct net_device *dev)
517 struct gfar_private *priv = netdev_priv(dev);
518 struct gfar *regs = priv->regs;
521 phy_stop(priv->phydev);
524 spin_lock_irqsave(&priv->lock, flags);
528 spin_unlock_irqrestore(&priv->lock, flags);
531 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
532 free_irq(priv->interruptError, dev);
533 free_irq(priv->interruptTransmit, dev);
534 free_irq(priv->interruptReceive, dev);
536 free_irq(priv->interruptTransmit, dev);
539 free_skb_resources(priv);
541 dma_free_coherent(NULL,
542 sizeof(struct txbd8)*priv->tx_ring_size
543 + sizeof(struct rxbd8)*priv->rx_ring_size,
545 gfar_read(®s->tbase0));
548 /* If there are any tx skbs or rx skbs still around, free them.
549 * Then free tx_skbuff and rx_skbuff */
550 static void free_skb_resources(struct gfar_private *priv)
556 /* Go through all the buffer descriptors and free their data buffers */
557 txbdp = priv->tx_bd_base;
559 for (i = 0; i < priv->tx_ring_size; i++) {
561 if (priv->tx_skbuff[i]) {
562 dma_unmap_single(NULL, txbdp->bufPtr,
565 dev_kfree_skb_any(priv->tx_skbuff[i]);
566 priv->tx_skbuff[i] = NULL;
570 kfree(priv->tx_skbuff);
572 rxbdp = priv->rx_bd_base;
574 /* rx_skbuff is not guaranteed to be allocated, so only
575 * free it and its contents if it is allocated */
576 if(priv->rx_skbuff != NULL) {
577 for (i = 0; i < priv->rx_ring_size; i++) {
578 if (priv->rx_skbuff[i]) {
579 dma_unmap_single(NULL, rxbdp->bufPtr,
584 dev_kfree_skb_any(priv->rx_skbuff[i]);
585 priv->rx_skbuff[i] = NULL;
595 kfree(priv->rx_skbuff);
599 void gfar_start(struct net_device *dev)
601 struct gfar_private *priv = netdev_priv(dev);
602 struct gfar *regs = priv->regs;
605 /* Enable Rx and Tx in MACCFG1 */
606 tempval = gfar_read(®s->maccfg1);
607 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
608 gfar_write(®s->maccfg1, tempval);
610 /* Initialize DMACTRL to have WWR and WOP */
611 tempval = gfar_read(&priv->regs->dmactrl);
612 tempval |= DMACTRL_INIT_SETTINGS;
613 gfar_write(&priv->regs->dmactrl, tempval);
615 /* Clear THLT, so that the DMA starts polling now */
616 gfar_write(®s->tstat, TSTAT_CLEAR_THALT);
618 /* Make sure we aren't stopped */
619 tempval = gfar_read(&priv->regs->dmactrl);
620 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
621 gfar_write(&priv->regs->dmactrl, tempval);
623 /* Unmask the interrupts we look for */
624 gfar_write(®s->imask, IMASK_DEFAULT);
627 /* Bring the controller up and running */
628 int startup_gfar(struct net_device *dev)
635 struct gfar_private *priv = netdev_priv(dev);
636 struct gfar *regs = priv->regs;
640 gfar_write(®s->imask, IMASK_INIT_CLEAR);
642 /* Allocate memory for the buffer descriptors */
643 vaddr = (unsigned long) dma_alloc_coherent(NULL,
644 sizeof (struct txbd8) * priv->tx_ring_size +
645 sizeof (struct rxbd8) * priv->rx_ring_size,
649 if (netif_msg_ifup(priv))
650 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
655 priv->tx_bd_base = (struct txbd8 *) vaddr;
657 /* enet DMA only understands physical addresses */
658 gfar_write(®s->tbase0, addr);
660 /* Start the rx descriptor ring where the tx ring leaves off */
661 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
662 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
663 priv->rx_bd_base = (struct rxbd8 *) vaddr;
664 gfar_write(®s->rbase0, addr);
666 /* Setup the skbuff rings */
668 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
669 priv->tx_ring_size, GFP_KERNEL);
671 if (NULL == priv->tx_skbuff) {
672 if (netif_msg_ifup(priv))
673 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
679 for (i = 0; i < priv->tx_ring_size; i++)
680 priv->tx_skbuff[i] = NULL;
683 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
684 priv->rx_ring_size, GFP_KERNEL);
686 if (NULL == priv->rx_skbuff) {
687 if (netif_msg_ifup(priv))
688 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
694 for (i = 0; i < priv->rx_ring_size; i++)
695 priv->rx_skbuff[i] = NULL;
697 /* Initialize some variables in our dev structure */
698 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
699 priv->cur_rx = priv->rx_bd_base;
700 priv->skb_curtx = priv->skb_dirtytx = 0;
703 /* Initialize Transmit Descriptor Ring */
704 txbdp = priv->tx_bd_base;
705 for (i = 0; i < priv->tx_ring_size; i++) {
712 /* Set the last descriptor in the ring to indicate wrap */
714 txbdp->status |= TXBD_WRAP;
716 rxbdp = priv->rx_bd_base;
717 for (i = 0; i < priv->rx_ring_size; i++) {
718 struct sk_buff *skb = NULL;
722 skb = gfar_new_skb(dev, rxbdp);
724 priv->rx_skbuff[i] = skb;
729 /* Set the last descriptor in the ring to wrap */
731 rxbdp->status |= RXBD_WRAP;
733 /* If the device has multiple interrupts, register for
734 * them. Otherwise, only register for the one */
735 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
736 /* Install our interrupt handlers for Error,
737 * Transmit, and Receive */
738 if (request_irq(priv->interruptError, gfar_error,
739 0, "enet_error", dev) < 0) {
740 if (netif_msg_intr(priv))
741 printk(KERN_ERR "%s: Can't get IRQ %d\n",
742 dev->name, priv->interruptError);
748 if (request_irq(priv->interruptTransmit, gfar_transmit,
749 0, "enet_tx", dev) < 0) {
750 if (netif_msg_intr(priv))
751 printk(KERN_ERR "%s: Can't get IRQ %d\n",
752 dev->name, priv->interruptTransmit);
759 if (request_irq(priv->interruptReceive, gfar_receive,
760 0, "enet_rx", dev) < 0) {
761 if (netif_msg_intr(priv))
762 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
763 dev->name, priv->interruptReceive);
769 if (request_irq(priv->interruptTransmit, gfar_interrupt,
770 0, "gfar_interrupt", dev) < 0) {
771 if (netif_msg_intr(priv))
772 printk(KERN_ERR "%s: Can't get IRQ %d\n",
773 dev->name, priv->interruptError);
780 phy_start(priv->phydev);
782 /* Configure the coalescing support */
783 if (priv->txcoalescing)
784 gfar_write(®s->txic,
785 mk_ic_value(priv->txcount, priv->txtime));
787 gfar_write(®s->txic, 0);
789 if (priv->rxcoalescing)
790 gfar_write(®s->rxic,
791 mk_ic_value(priv->rxcount, priv->rxtime));
793 gfar_write(®s->rxic, 0);
795 if (priv->rx_csum_enable)
796 rctrl |= RCTRL_CHECKSUMMING;
798 if (priv->extended_hash)
799 rctrl |= RCTRL_EXTHASH;
801 if (priv->vlan_enable)
804 /* Init rctrl based on our settings */
805 gfar_write(&priv->regs->rctrl, rctrl);
807 if (dev->features & NETIF_F_IP_CSUM)
808 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
815 free_irq(priv->interruptTransmit, dev);
817 free_irq(priv->interruptError, dev);
820 free_skb_resources(priv);
822 dma_free_coherent(NULL,
823 sizeof(struct txbd8)*priv->tx_ring_size
824 + sizeof(struct rxbd8)*priv->rx_ring_size,
826 gfar_read(®s->tbase0));
831 /* Called when something needs to use the ethernet device */
832 /* Returns 0 for success. */
833 static int gfar_enet_open(struct net_device *dev)
837 /* Initialize a bunch of registers */
840 gfar_set_mac_address(dev);
847 err = startup_gfar(dev);
849 netif_start_queue(dev);
854 static struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
856 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
858 memset(fcb, 0, GMAC_FCB_LEN);
860 /* Flag the bd so the controller looks for the FCB */
861 bdp->status |= TXBD_TOE;
866 static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
870 /* If we're here, it's a IP packet with a TCP or UDP
871 * payload. We set it to checksum, using a pseudo-header
879 /* Notify the controller what the protocol is */
880 if (skb->nh.iph->protocol == IPPROTO_UDP)
883 /* l3os is the distance between the start of the
884 * frame (skb->data) and the start of the IP hdr.
885 * l4os is the distance between the start of the
886 * l3 hdr and the l4 hdr */
887 fcb->l3os = (u16)(skb->nh.raw - skb->data - GMAC_FCB_LEN);
888 fcb->l4os = (u16)(skb->h.raw - skb->nh.raw);
890 len = skb->nh.iph->tot_len - fcb->l4os;
892 /* Provide the pseudoheader csum */
893 fcb->phcs = ~csum_tcpudp_magic(skb->nh.iph->saddr,
894 skb->nh.iph->daddr, len,
895 skb->nh.iph->protocol, 0);
898 void gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
901 fcb->vlctl = vlan_tx_tag_get(skb);
904 /* This is called by the kernel when a frame is ready for transmission. */
905 /* It is pointed to by the dev->hard_start_xmit function pointer */
906 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
908 struct gfar_private *priv = netdev_priv(dev);
909 struct txfcb *fcb = NULL;
912 /* Update transmit stats */
913 priv->stats.tx_bytes += skb->len;
916 spin_lock_irq(&priv->lock);
918 /* Point at the first free tx descriptor */
919 txbdp = priv->cur_tx;
921 /* Clear all but the WRAP status flags */
922 txbdp->status &= TXBD_WRAP;
924 /* Set up checksumming */
925 if ((dev->features & NETIF_F_IP_CSUM)
926 && (CHECKSUM_HW == skb->ip_summed)) {
927 fcb = gfar_add_fcb(skb, txbdp);
928 gfar_tx_checksum(skb, fcb);
931 if (priv->vlan_enable &&
932 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
934 fcb = gfar_add_fcb(skb, txbdp);
936 gfar_tx_vlan(skb, fcb);
939 /* Set buffer length and pointer */
940 txbdp->length = skb->len;
941 txbdp->bufPtr = dma_map_single(NULL, skb->data,
942 skb->len, DMA_TO_DEVICE);
944 /* Save the skb pointer so we can free it later */
945 priv->tx_skbuff[priv->skb_curtx] = skb;
947 /* Update the current skb pointer (wrapping if this was the last) */
949 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
951 /* Flag the BD as interrupt-causing */
952 txbdp->status |= TXBD_INTERRUPT;
954 /* Flag the BD as ready to go, last in frame, and */
956 txbdp->status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
958 dev->trans_start = jiffies;
960 /* If this was the last BD in the ring, the next one */
961 /* is at the beginning of the ring */
962 if (txbdp->status & TXBD_WRAP)
963 txbdp = priv->tx_bd_base;
967 /* If the next BD still needs to be cleaned up, then the bds
968 are full. We need to tell the kernel to stop sending us stuff. */
969 if (txbdp == priv->dirty_tx) {
970 netif_stop_queue(dev);
972 priv->stats.tx_fifo_errors++;
975 /* Update the current txbd to the next one */
976 priv->cur_tx = txbdp;
978 /* Tell the DMA to go go go */
979 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
982 spin_unlock_irq(&priv->lock);
987 /* Stops the kernel queue, and halts the controller */
988 static int gfar_close(struct net_device *dev)
990 struct gfar_private *priv = netdev_priv(dev);
993 /* Disconnect from the PHY */
994 phy_disconnect(priv->phydev);
997 netif_stop_queue(dev);
1002 /* returns a net_device_stats structure pointer */
1003 static struct net_device_stats * gfar_get_stats(struct net_device *dev)
1005 struct gfar_private *priv = netdev_priv(dev);
1007 return &(priv->stats);
1010 /* Changes the mac address if the controller is not running. */
1011 int gfar_set_mac_address(struct net_device *dev)
1013 struct gfar_private *priv = netdev_priv(dev);
1015 char tmpbuf[MAC_ADDR_LEN];
1018 /* Now copy it into the mac registers backwards, cuz */
1019 /* little endian is silly */
1020 for (i = 0; i < MAC_ADDR_LEN; i++)
1021 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->dev_addr[i];
1023 gfar_write(&priv->regs->macstnaddr1, *((u32 *) (tmpbuf)));
1025 tempval = *((u32 *) (tmpbuf + 4));
1027 gfar_write(&priv->regs->macstnaddr2, tempval);
1033 /* Enables and disables VLAN insertion/extraction */
1034 static void gfar_vlan_rx_register(struct net_device *dev,
1035 struct vlan_group *grp)
1037 struct gfar_private *priv = netdev_priv(dev);
1038 unsigned long flags;
1041 spin_lock_irqsave(&priv->lock, flags);
1046 /* Enable VLAN tag insertion */
1047 tempval = gfar_read(&priv->regs->tctrl);
1048 tempval |= TCTRL_VLINS;
1050 gfar_write(&priv->regs->tctrl, tempval);
1052 /* Enable VLAN tag extraction */
1053 tempval = gfar_read(&priv->regs->rctrl);
1054 tempval |= RCTRL_VLEX;
1055 gfar_write(&priv->regs->rctrl, tempval);
1057 /* Disable VLAN tag insertion */
1058 tempval = gfar_read(&priv->regs->tctrl);
1059 tempval &= ~TCTRL_VLINS;
1060 gfar_write(&priv->regs->tctrl, tempval);
1062 /* Disable VLAN tag extraction */
1063 tempval = gfar_read(&priv->regs->rctrl);
1064 tempval &= ~RCTRL_VLEX;
1065 gfar_write(&priv->regs->rctrl, tempval);
1068 spin_unlock_irqrestore(&priv->lock, flags);
1072 static void gfar_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
1074 struct gfar_private *priv = netdev_priv(dev);
1075 unsigned long flags;
1077 spin_lock_irqsave(&priv->lock, flags);
1080 priv->vlgrp->vlan_devices[vid] = NULL;
1082 spin_unlock_irqrestore(&priv->lock, flags);
1086 static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1088 int tempsize, tempval;
1089 struct gfar_private *priv = netdev_priv(dev);
1090 int oldsize = priv->rx_buffer_size;
1091 int frame_size = new_mtu + ETH_HLEN;
1093 if (priv->vlan_enable)
1094 frame_size += VLAN_ETH_HLEN;
1096 if (gfar_uses_fcb(priv))
1097 frame_size += GMAC_FCB_LEN;
1099 frame_size += priv->padding;
1101 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
1102 if (netif_msg_drv(priv))
1103 printk(KERN_ERR "%s: Invalid MTU setting\n",
1109 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1110 INCREMENTAL_BUFFER_SIZE;
1112 /* Only stop and start the controller if it isn't already
1114 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1117 priv->rx_buffer_size = tempsize;
1121 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1122 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1124 /* If the mtu is larger than the max size for standard
1125 * ethernet frames (ie, a jumbo frame), then set maccfg2
1126 * to allow huge frames, and to check the length */
1127 tempval = gfar_read(&priv->regs->maccfg2);
1129 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1130 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1132 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1134 gfar_write(&priv->regs->maccfg2, tempval);
1136 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1142 /* gfar_timeout gets called when a packet has not been
1143 * transmitted after a set amount of time.
1144 * For now, assume that clearing out all the structures, and
1145 * starting over will fix the problem. */
1146 static void gfar_timeout(struct net_device *dev)
1148 struct gfar_private *priv = netdev_priv(dev);
1150 priv->stats.tx_errors++;
1152 if (dev->flags & IFF_UP) {
1157 netif_schedule(dev);
1160 /* Interrupt Handler for Transmit complete */
1161 static irqreturn_t gfar_transmit(int irq, void *dev_id, struct pt_regs *regs)
1163 struct net_device *dev = (struct net_device *) dev_id;
1164 struct gfar_private *priv = netdev_priv(dev);
1168 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1171 spin_lock(&priv->lock);
1172 bdp = priv->dirty_tx;
1173 while ((bdp->status & TXBD_READY) == 0) {
1174 /* If dirty_tx and cur_tx are the same, then either the */
1175 /* ring is empty or full now (it could only be full in the beginning, */
1176 /* obviously). If it is empty, we are done. */
1177 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1180 priv->stats.tx_packets++;
1182 /* Deferred means some collisions occurred during transmit, */
1183 /* but we eventually sent the packet. */
1184 if (bdp->status & TXBD_DEF)
1185 priv->stats.collisions++;
1187 /* Free the sk buffer associated with this TxBD */
1188 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1189 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1191 (priv->skb_dirtytx +
1192 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1194 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1195 if (bdp->status & TXBD_WRAP)
1196 bdp = priv->tx_bd_base;
1200 /* Move dirty_tx to be the next bd */
1201 priv->dirty_tx = bdp;
1203 /* We freed a buffer, so now we can restart transmission */
1204 if (netif_queue_stopped(dev))
1205 netif_wake_queue(dev);
1206 } /* while ((bdp->status & TXBD_READY) == 0) */
1208 /* If we are coalescing the interrupts, reset the timer */
1209 /* Otherwise, clear it */
1210 if (priv->txcoalescing)
1211 gfar_write(&priv->regs->txic,
1212 mk_ic_value(priv->txcount, priv->txtime));
1214 gfar_write(&priv->regs->txic, 0);
1216 spin_unlock(&priv->lock);
1221 struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1223 struct gfar_private *priv = netdev_priv(dev);
1224 struct sk_buff *skb = NULL;
1225 unsigned int timeout = SKB_ALLOC_TIMEOUT;
1227 /* We have to allocate the skb, so keep trying till we succeed */
1228 while ((!skb) && timeout--)
1229 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1234 /* We need the data buffer to be aligned properly. We will reserve
1235 * as many bytes as needed to align the data properly
1239 (((unsigned) skb->data) & (RXBUF_ALIGNMENT - 1)));
1243 bdp->bufPtr = dma_map_single(NULL, skb->data,
1244 priv->rx_buffer_size + RXBUF_ALIGNMENT,
1249 /* Mark the buffer empty */
1250 bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1255 static inline void count_errors(unsigned short status, struct gfar_private *priv)
1257 struct net_device_stats *stats = &priv->stats;
1258 struct gfar_extra_stats *estats = &priv->extra_stats;
1260 /* If the packet was truncated, none of the other errors
1262 if (status & RXBD_TRUNCATED) {
1263 stats->rx_length_errors++;
1269 /* Count the errors, if there were any */
1270 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1271 stats->rx_length_errors++;
1273 if (status & RXBD_LARGE)
1278 if (status & RXBD_NONOCTET) {
1279 stats->rx_frame_errors++;
1280 estats->rx_nonoctet++;
1282 if (status & RXBD_CRCERR) {
1283 estats->rx_crcerr++;
1284 stats->rx_crc_errors++;
1286 if (status & RXBD_OVERRUN) {
1287 estats->rx_overrun++;
1288 stats->rx_crc_errors++;
1292 irqreturn_t gfar_receive(int irq, void *dev_id, struct pt_regs *regs)
1294 struct net_device *dev = (struct net_device *) dev_id;
1295 struct gfar_private *priv = netdev_priv(dev);
1297 #ifdef CONFIG_GFAR_NAPI
1301 /* Clear IEVENT, so rx interrupt isn't called again
1302 * because of this interrupt */
1303 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1306 #ifdef CONFIG_GFAR_NAPI
1307 if (netif_rx_schedule_prep(dev)) {
1308 tempval = gfar_read(&priv->regs->imask);
1309 tempval &= IMASK_RX_DISABLED;
1310 gfar_write(&priv->regs->imask, tempval);
1312 __netif_rx_schedule(dev);
1314 if (netif_msg_rx_err(priv))
1315 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1316 dev->name, gfar_read(&priv->regs->ievent),
1317 gfar_read(&priv->regs->imask));
1321 spin_lock(&priv->lock);
1322 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1324 /* If we are coalescing interrupts, update the timer */
1325 /* Otherwise, clear it */
1326 if (priv->rxcoalescing)
1327 gfar_write(&priv->regs->rxic,
1328 mk_ic_value(priv->rxcount, priv->rxtime));
1330 gfar_write(&priv->regs->rxic, 0);
1332 spin_unlock(&priv->lock);
1338 static inline int gfar_rx_vlan(struct sk_buff *skb,
1339 struct vlan_group *vlgrp, unsigned short vlctl)
1341 #ifdef CONFIG_GFAR_NAPI
1342 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1344 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1348 static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1350 /* If valid headers were found, and valid sums
1351 * were verified, then we tell the kernel that no
1352 * checksumming is necessary. Otherwise, it is */
1353 if (fcb->cip && !fcb->eip && fcb->ctu && !fcb->etu)
1354 skb->ip_summed = CHECKSUM_UNNECESSARY;
1356 skb->ip_summed = CHECKSUM_NONE;
1360 static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1362 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1364 /* Remove the FCB from the skb */
1365 skb_pull(skb, GMAC_FCB_LEN);
1370 /* gfar_process_frame() -- handle one incoming packet if skb
1372 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1375 struct gfar_private *priv = netdev_priv(dev);
1376 struct rxfcb *fcb = NULL;
1379 if (netif_msg_rx_err(priv))
1380 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
1381 priv->stats.rx_dropped++;
1382 priv->extra_stats.rx_skbmissing++;
1386 /* Prep the skb for the packet */
1387 skb_put(skb, length);
1389 /* Grab the FCB if there is one */
1390 if (gfar_uses_fcb(priv))
1391 fcb = gfar_get_fcb(skb);
1393 /* Remove the padded bytes, if there are any */
1395 skb_pull(skb, priv->padding);
1397 if (priv->rx_csum_enable)
1398 gfar_rx_checksum(skb, fcb);
1400 /* Tell the skb what kind of packet this is */
1401 skb->protocol = eth_type_trans(skb, dev);
1403 /* Send the packet up the stack */
1404 if (unlikely(priv->vlgrp && fcb->vln))
1405 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1409 if (NET_RX_DROP == ret)
1410 priv->extra_stats.kernel_dropped++;
1416 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
1417 * until the budget/quota has been reached. Returns the number
1420 int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
1423 struct sk_buff *skb;
1426 struct gfar_private *priv = netdev_priv(dev);
1428 /* Get the first full descriptor */
1431 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
1432 skb = priv->rx_skbuff[priv->skb_currx];
1435 (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1436 | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1437 /* Increment the number of packets */
1438 priv->stats.rx_packets++;
1441 /* Remove the FCS from the packet length */
1442 pkt_len = bdp->length - 4;
1444 gfar_process_frame(dev, skb, pkt_len);
1446 priv->stats.rx_bytes += pkt_len;
1448 count_errors(bdp->status, priv);
1451 dev_kfree_skb_any(skb);
1453 priv->rx_skbuff[priv->skb_currx] = NULL;
1456 dev->last_rx = jiffies;
1458 /* Clear the status flags for this buffer */
1459 bdp->status &= ~RXBD_STATS;
1461 /* Add another skb for the future */
1462 skb = gfar_new_skb(dev, bdp);
1463 priv->rx_skbuff[priv->skb_currx] = skb;
1465 /* Update to the next pointer */
1466 if (bdp->status & RXBD_WRAP)
1467 bdp = priv->rx_bd_base;
1471 /* update to point at the next skb */
1474 1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1478 /* Update the current rxbd pointer to be the next one */
1481 /* If no packets have arrived since the
1482 * last one we processed, clear the IEVENT RX and
1483 * BSY bits so that another interrupt won't be
1484 * generated when we set IMASK */
1485 if (bdp->status & RXBD_EMPTY)
1486 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1491 #ifdef CONFIG_GFAR_NAPI
1492 static int gfar_poll(struct net_device *dev, int *budget)
1495 struct gfar_private *priv = netdev_priv(dev);
1496 int rx_work_limit = *budget;
1498 if (rx_work_limit > dev->quota)
1499 rx_work_limit = dev->quota;
1501 howmany = gfar_clean_rx_ring(dev, rx_work_limit);
1503 dev->quota -= howmany;
1504 rx_work_limit -= howmany;
1507 if (rx_work_limit >= 0) {
1508 netif_rx_complete(dev);
1510 /* Clear the halt bit in RSTAT */
1511 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1513 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1515 /* If we are coalescing interrupts, update the timer */
1516 /* Otherwise, clear it */
1517 if (priv->rxcoalescing)
1518 gfar_write(&priv->regs->rxic,
1519 mk_ic_value(priv->rxcount, priv->rxtime));
1521 gfar_write(&priv->regs->rxic, 0);
1524 return (rx_work_limit < 0) ? 1 : 0;
1528 /* The interrupt handler for devices with one interrupt */
1529 static irqreturn_t gfar_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1531 struct net_device *dev = dev_id;
1532 struct gfar_private *priv = netdev_priv(dev);
1534 /* Save ievent for future reference */
1535 u32 events = gfar_read(&priv->regs->ievent);
1538 gfar_write(&priv->regs->ievent, events);
1540 /* Check for reception */
1541 if ((events & IEVENT_RXF0) || (events & IEVENT_RXB0))
1542 gfar_receive(irq, dev_id, regs);
1544 /* Check for transmit completion */
1545 if ((events & IEVENT_TXF) || (events & IEVENT_TXB))
1546 gfar_transmit(irq, dev_id, regs);
1548 /* Update error statistics */
1549 if (events & IEVENT_TXE) {
1550 priv->stats.tx_errors++;
1552 if (events & IEVENT_LC)
1553 priv->stats.tx_window_errors++;
1554 if (events & IEVENT_CRL)
1555 priv->stats.tx_aborted_errors++;
1556 if (events & IEVENT_XFUN) {
1557 if (netif_msg_tx_err(priv))
1558 printk(KERN_WARNING "%s: tx underrun. dropped packet\n", dev->name);
1559 priv->stats.tx_dropped++;
1560 priv->extra_stats.tx_underrun++;
1562 /* Reactivate the Tx Queues */
1563 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1566 if (events & IEVENT_BSY) {
1567 priv->stats.rx_errors++;
1568 priv->extra_stats.rx_bsy++;
1570 gfar_receive(irq, dev_id, regs);
1572 #ifndef CONFIG_GFAR_NAPI
1573 /* Clear the halt bit in RSTAT */
1574 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1577 if (netif_msg_rx_err(priv))
1578 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n",
1580 gfar_read(&priv->regs->rstat));
1582 if (events & IEVENT_BABR) {
1583 priv->stats.rx_errors++;
1584 priv->extra_stats.rx_babr++;
1586 if (netif_msg_rx_err(priv))
1587 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
1589 if (events & IEVENT_EBERR) {
1590 priv->extra_stats.eberr++;
1591 if (netif_msg_rx_err(priv))
1592 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
1594 if ((events & IEVENT_RXC) && (netif_msg_rx_err(priv)))
1595 printk(KERN_DEBUG "%s: control frame\n", dev->name);
1597 if (events & IEVENT_BABT) {
1598 priv->extra_stats.tx_babt++;
1599 if (netif_msg_rx_err(priv))
1600 printk(KERN_DEBUG "%s: babt error\n", dev->name);
1606 /* Called every time the controller might need to be made
1607 * aware of new link state. The PHY code conveys this
1608 * information through variables in the phydev structure, and this
1609 * function converts those variables into the appropriate
1610 * register values, and can bring down the device if needed.
1612 static void adjust_link(struct net_device *dev)
1614 struct gfar_private *priv = netdev_priv(dev);
1615 struct gfar *regs = priv->regs;
1616 unsigned long flags;
1617 struct phy_device *phydev = priv->phydev;
1620 spin_lock_irqsave(&priv->lock, flags);
1622 u32 tempval = gfar_read(®s->maccfg2);
1624 /* Now we make sure that we can be in full duplex mode.
1625 * If not, we operate in half-duplex mode. */
1626 if (phydev->duplex != priv->oldduplex) {
1628 if (!(phydev->duplex))
1629 tempval &= ~(MACCFG2_FULL_DUPLEX);
1631 tempval |= MACCFG2_FULL_DUPLEX;
1633 priv->oldduplex = phydev->duplex;
1636 if (phydev->speed != priv->oldspeed) {
1638 switch (phydev->speed) {
1641 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
1646 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
1649 if (netif_msg_link(priv))
1651 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1652 dev->name, phydev->speed);
1656 priv->oldspeed = phydev->speed;
1659 gfar_write(®s->maccfg2, tempval);
1661 if (!priv->oldlink) {
1664 netif_schedule(dev);
1666 } else if (priv->oldlink) {
1670 priv->oldduplex = -1;
1673 if (new_state && netif_msg_link(priv))
1674 phy_print_status(phydev);
1676 spin_unlock_irqrestore(&priv->lock, flags);
1679 /* Update the hash table based on the current list of multicast
1680 * addresses we subscribe to. Also, change the promiscuity of
1681 * the device based on the flags (this function is called
1682 * whenever dev->flags is changed */
1683 static void gfar_set_multi(struct net_device *dev)
1685 struct dev_mc_list *mc_ptr;
1686 struct gfar_private *priv = netdev_priv(dev);
1687 struct gfar *regs = priv->regs;
1690 if(dev->flags & IFF_PROMISC) {
1691 if (netif_msg_drv(priv))
1692 printk(KERN_INFO "%s: Entering promiscuous mode.\n",
1694 /* Set RCTRL to PROM */
1695 tempval = gfar_read(®s->rctrl);
1696 tempval |= RCTRL_PROM;
1697 gfar_write(®s->rctrl, tempval);
1699 /* Set RCTRL to not PROM */
1700 tempval = gfar_read(®s->rctrl);
1701 tempval &= ~(RCTRL_PROM);
1702 gfar_write(®s->rctrl, tempval);
1705 if(dev->flags & IFF_ALLMULTI) {
1706 /* Set the hash to rx all multicast frames */
1707 gfar_write(®s->igaddr0, 0xffffffff);
1708 gfar_write(®s->igaddr1, 0xffffffff);
1709 gfar_write(®s->igaddr2, 0xffffffff);
1710 gfar_write(®s->igaddr3, 0xffffffff);
1711 gfar_write(®s->igaddr4, 0xffffffff);
1712 gfar_write(®s->igaddr5, 0xffffffff);
1713 gfar_write(®s->igaddr6, 0xffffffff);
1714 gfar_write(®s->igaddr7, 0xffffffff);
1715 gfar_write(®s->gaddr0, 0xffffffff);
1716 gfar_write(®s->gaddr1, 0xffffffff);
1717 gfar_write(®s->gaddr2, 0xffffffff);
1718 gfar_write(®s->gaddr3, 0xffffffff);
1719 gfar_write(®s->gaddr4, 0xffffffff);
1720 gfar_write(®s->gaddr5, 0xffffffff);
1721 gfar_write(®s->gaddr6, 0xffffffff);
1722 gfar_write(®s->gaddr7, 0xffffffff);
1724 /* zero out the hash */
1725 gfar_write(®s->igaddr0, 0x0);
1726 gfar_write(®s->igaddr1, 0x0);
1727 gfar_write(®s->igaddr2, 0x0);
1728 gfar_write(®s->igaddr3, 0x0);
1729 gfar_write(®s->igaddr4, 0x0);
1730 gfar_write(®s->igaddr5, 0x0);
1731 gfar_write(®s->igaddr6, 0x0);
1732 gfar_write(®s->igaddr7, 0x0);
1733 gfar_write(®s->gaddr0, 0x0);
1734 gfar_write(®s->gaddr1, 0x0);
1735 gfar_write(®s->gaddr2, 0x0);
1736 gfar_write(®s->gaddr3, 0x0);
1737 gfar_write(®s->gaddr4, 0x0);
1738 gfar_write(®s->gaddr5, 0x0);
1739 gfar_write(®s->gaddr6, 0x0);
1740 gfar_write(®s->gaddr7, 0x0);
1742 if(dev->mc_count == 0)
1745 /* Parse the list, and set the appropriate bits */
1746 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
1747 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
1754 /* Set the appropriate hash bit for the given addr */
1755 /* The algorithm works like so:
1756 * 1) Take the Destination Address (ie the multicast address), and
1757 * do a CRC on it (little endian), and reverse the bits of the
1759 * 2) Use the 8 most significant bits as a hash into a 256-entry
1760 * table. The table is controlled through 8 32-bit registers:
1761 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1762 * gaddr7. This means that the 3 most significant bits in the
1763 * hash index which gaddr register to use, and the 5 other bits
1764 * indicate which bit (assuming an IBM numbering scheme, which
1765 * for PowerPC (tm) is usually the case) in the register holds
1767 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1770 struct gfar_private *priv = netdev_priv(dev);
1771 u32 result = ether_crc(MAC_ADDR_LEN, addr);
1772 int width = priv->hash_width;
1773 u8 whichbit = (result >> (32 - width)) & 0x1f;
1774 u8 whichreg = result >> (32 - width + 5);
1775 u32 value = (1 << (31-whichbit));
1777 tempval = gfar_read(priv->hash_regs[whichreg]);
1779 gfar_write(priv->hash_regs[whichreg], tempval);
1784 /* GFAR error interrupt handler */
1785 static irqreturn_t gfar_error(int irq, void *dev_id, struct pt_regs *regs)
1787 struct net_device *dev = dev_id;
1788 struct gfar_private *priv = netdev_priv(dev);
1790 /* Save ievent for future reference */
1791 u32 events = gfar_read(&priv->regs->ievent);
1794 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1797 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1798 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
1799 dev->name, events, gfar_read(&priv->regs->imask));
1801 /* Update the error counters */
1802 if (events & IEVENT_TXE) {
1803 priv->stats.tx_errors++;
1805 if (events & IEVENT_LC)
1806 priv->stats.tx_window_errors++;
1807 if (events & IEVENT_CRL)
1808 priv->stats.tx_aborted_errors++;
1809 if (events & IEVENT_XFUN) {
1810 if (netif_msg_tx_err(priv))
1811 printk(KERN_DEBUG "%s: underrun. packet dropped.\n",
1813 priv->stats.tx_dropped++;
1814 priv->extra_stats.tx_underrun++;
1816 /* Reactivate the Tx Queues */
1817 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1819 if (netif_msg_tx_err(priv))
1820 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
1822 if (events & IEVENT_BSY) {
1823 priv->stats.rx_errors++;
1824 priv->extra_stats.rx_bsy++;
1826 gfar_receive(irq, dev_id, regs);
1828 #ifndef CONFIG_GFAR_NAPI
1829 /* Clear the halt bit in RSTAT */
1830 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1833 if (netif_msg_rx_err(priv))
1834 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n",
1836 gfar_read(&priv->regs->rstat));
1838 if (events & IEVENT_BABR) {
1839 priv->stats.rx_errors++;
1840 priv->extra_stats.rx_babr++;
1842 if (netif_msg_rx_err(priv))
1843 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
1845 if (events & IEVENT_EBERR) {
1846 priv->extra_stats.eberr++;
1847 if (netif_msg_rx_err(priv))
1848 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
1850 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
1851 if (netif_msg_rx_status(priv))
1852 printk(KERN_DEBUG "%s: control frame\n", dev->name);
1854 if (events & IEVENT_BABT) {
1855 priv->extra_stats.tx_babt++;
1856 if (netif_msg_tx_err(priv))
1857 printk(KERN_DEBUG "%s: babt error\n", dev->name);
1862 /* Structure for a device driver */
1863 static struct platform_driver gfar_driver = {
1864 .probe = gfar_probe,
1865 .remove = gfar_remove,
1867 .name = "fsl-gianfar",
1871 static int __init gfar_init(void)
1873 int err = gfar_mdio_init();
1878 err = platform_driver_register(&gfar_driver);
1886 static void __exit gfar_exit(void)
1888 platform_driver_unregister(&gfar_driver);
1892 module_init(gfar_init);
1893 module_exit(gfar_exit);