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 (kumar.gala@freescale.com)
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/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/version.h>
94 #include <linux/dma-mapping.h>
95 #include <linux/crc32.h>
96 #include <linux/mii.h>
97 #include <linux/phy.h>
100 #include "gianfar_mii.h"
102 #define TX_TIMEOUT (1*HZ)
103 #define SKB_ALLOC_TIMEOUT 1000000
104 #undef BRIEF_GFAR_ERRORS
105 #undef VERBOSE_GFAR_ERRORS
107 #ifdef CONFIG_GFAR_NAPI
108 #define RECEIVE(x) netif_receive_skb(x)
110 #define RECEIVE(x) netif_rx(x)
113 const char gfar_driver_name[] = "Gianfar Ethernet";
114 const char gfar_driver_version[] = "1.2";
116 static int gfar_enet_open(struct net_device *dev);
117 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
118 static void gfar_timeout(struct net_device *dev);
119 static int gfar_close(struct net_device *dev);
120 struct sk_buff *gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp);
121 static struct net_device_stats *gfar_get_stats(struct net_device *dev);
122 static int gfar_set_mac_address(struct net_device *dev);
123 static int gfar_change_mtu(struct net_device *dev, int new_mtu);
124 static irqreturn_t gfar_error(int irq, void *dev_id, struct pt_regs *regs);
125 static irqreturn_t gfar_transmit(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 void adjust_link(struct net_device *dev);
128 static void init_registers(struct net_device *dev);
129 static int init_phy(struct net_device *dev);
130 static int gfar_probe(struct device *device);
131 static int gfar_remove(struct device *device);
132 static void free_skb_resources(struct gfar_private *priv);
133 static void gfar_set_multi(struct net_device *dev);
134 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
135 #ifdef CONFIG_GFAR_NAPI
136 static int gfar_poll(struct net_device *dev, int *budget);
138 int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
139 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
140 static void gfar_vlan_rx_register(struct net_device *netdev,
141 struct vlan_group *grp);
142 static void gfar_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
144 extern struct ethtool_ops gfar_ethtool_ops;
146 MODULE_AUTHOR("Freescale Semiconductor, Inc");
147 MODULE_DESCRIPTION("Gianfar Ethernet Driver");
148 MODULE_LICENSE("GPL");
150 int gfar_uses_fcb(struct gfar_private *priv)
152 if (priv->vlan_enable || priv->rx_csum_enable)
158 /* Set up the ethernet device structure, private data,
159 * and anything else we need before we start */
160 static int gfar_probe(struct device *device)
163 struct net_device *dev = NULL;
164 struct gfar_private *priv = NULL;
165 struct platform_device *pdev = to_platform_device(device);
166 struct gianfar_platform_data *einfo;
171 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
174 printk(KERN_ERR "gfar %d: Missing additional data!\n",
180 /* Create an ethernet device instance */
181 dev = alloc_etherdev(sizeof (*priv));
186 priv = netdev_priv(dev);
188 /* Set the info in the priv to the current info */
191 /* fill out IRQ fields */
192 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
193 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
194 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
195 priv->interruptError = platform_get_irq_byname(pdev, "error");
197 priv->interruptTransmit = platform_get_irq(pdev, 0);
200 /* get a pointer to the register memory */
201 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
202 priv->regs = (struct gfar *)
203 ioremap(r->start, sizeof (struct gfar));
205 if (NULL == priv->regs) {
210 spin_lock_init(&priv->lock);
212 dev_set_drvdata(device, dev);
214 /* Stop the DMA engine now, in case it was running before */
215 /* (The firmware could have used it, and left it running). */
216 /* To do this, we write Graceful Receive Stop and Graceful */
217 /* Transmit Stop, and then wait until the corresponding bits */
218 /* in IEVENT indicate the stops have completed. */
219 tempval = gfar_read(&priv->regs->dmactrl);
220 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
221 gfar_write(&priv->regs->dmactrl, tempval);
223 tempval = gfar_read(&priv->regs->dmactrl);
224 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
225 gfar_write(&priv->regs->dmactrl, tempval);
227 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
230 /* Reset MAC layer */
231 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
233 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
234 gfar_write(&priv->regs->maccfg1, tempval);
236 /* Initialize MACCFG2. */
237 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
239 /* Initialize ECNTRL */
240 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
242 /* Copy the station address into the dev structure, */
243 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
245 /* Set the dev->base_addr to the gfar reg region */
246 dev->base_addr = (unsigned long) (priv->regs);
248 SET_MODULE_OWNER(dev);
249 SET_NETDEV_DEV(dev, device);
251 /* Fill in the dev structure */
252 dev->open = gfar_enet_open;
253 dev->hard_start_xmit = gfar_start_xmit;
254 dev->tx_timeout = gfar_timeout;
255 dev->watchdog_timeo = TX_TIMEOUT;
256 #ifdef CONFIG_GFAR_NAPI
257 dev->poll = gfar_poll;
258 dev->weight = GFAR_DEV_WEIGHT;
260 dev->stop = gfar_close;
261 dev->get_stats = gfar_get_stats;
262 dev->change_mtu = gfar_change_mtu;
264 dev->set_multicast_list = gfar_set_multi;
266 dev->ethtool_ops = &gfar_ethtool_ops;
268 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
269 priv->rx_csum_enable = 1;
270 dev->features |= NETIF_F_IP_CSUM;
272 priv->rx_csum_enable = 0;
276 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
277 dev->vlan_rx_register = gfar_vlan_rx_register;
278 dev->vlan_rx_kill_vid = gfar_vlan_rx_kill_vid;
280 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
282 priv->vlan_enable = 1;
285 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
286 priv->extended_hash = 1;
287 priv->hash_width = 9;
289 priv->hash_regs[0] = &priv->regs->igaddr0;
290 priv->hash_regs[1] = &priv->regs->igaddr1;
291 priv->hash_regs[2] = &priv->regs->igaddr2;
292 priv->hash_regs[3] = &priv->regs->igaddr3;
293 priv->hash_regs[4] = &priv->regs->igaddr4;
294 priv->hash_regs[5] = &priv->regs->igaddr5;
295 priv->hash_regs[6] = &priv->regs->igaddr6;
296 priv->hash_regs[7] = &priv->regs->igaddr7;
297 priv->hash_regs[8] = &priv->regs->gaddr0;
298 priv->hash_regs[9] = &priv->regs->gaddr1;
299 priv->hash_regs[10] = &priv->regs->gaddr2;
300 priv->hash_regs[11] = &priv->regs->gaddr3;
301 priv->hash_regs[12] = &priv->regs->gaddr4;
302 priv->hash_regs[13] = &priv->regs->gaddr5;
303 priv->hash_regs[14] = &priv->regs->gaddr6;
304 priv->hash_regs[15] = &priv->regs->gaddr7;
307 priv->extended_hash = 0;
308 priv->hash_width = 8;
310 priv->hash_regs[0] = &priv->regs->gaddr0;
311 priv->hash_regs[1] = &priv->regs->gaddr1;
312 priv->hash_regs[2] = &priv->regs->gaddr2;
313 priv->hash_regs[3] = &priv->regs->gaddr3;
314 priv->hash_regs[4] = &priv->regs->gaddr4;
315 priv->hash_regs[5] = &priv->regs->gaddr5;
316 priv->hash_regs[6] = &priv->regs->gaddr6;
317 priv->hash_regs[7] = &priv->regs->gaddr7;
320 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
321 priv->padding = DEFAULT_PADDING;
325 dev->hard_header_len += priv->padding;
327 if (dev->features & NETIF_F_IP_CSUM)
328 dev->hard_header_len += GMAC_FCB_LEN;
330 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
331 #ifdef CONFIG_GFAR_BUFSTASH
332 priv->rx_stash_size = STASH_LENGTH;
334 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
335 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
337 priv->txcoalescing = DEFAULT_TX_COALESCE;
338 priv->txcount = DEFAULT_TXCOUNT;
339 priv->txtime = DEFAULT_TXTIME;
340 priv->rxcoalescing = DEFAULT_RX_COALESCE;
341 priv->rxcount = DEFAULT_RXCOUNT;
342 priv->rxtime = DEFAULT_RXTIME;
344 /* Enable most messages by default */
345 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
347 err = register_netdev(dev);
350 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
355 /* Print out the device info */
356 printk(KERN_INFO DEVICE_NAME, dev->name);
357 for (idx = 0; idx < 6; idx++)
358 printk("%2.2x%c", dev->dev_addr[idx], idx == 5 ? ' ' : ':');
361 /* Even more device info helps when determining which kernel */
362 /* provided which set of benchmarks. Since this is global for all */
363 /* devices, we only print it once */
364 #ifdef CONFIG_GFAR_NAPI
365 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
367 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
369 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
370 dev->name, priv->rx_ring_size, priv->tx_ring_size);
375 iounmap((void *) priv->regs);
381 static int gfar_remove(struct device *device)
383 struct net_device *dev = dev_get_drvdata(device);
384 struct gfar_private *priv = netdev_priv(dev);
386 dev_set_drvdata(device, NULL);
388 iounmap((void *) priv->regs);
395 /* Initializes driver's PHY state, and attaches to the PHY.
396 * Returns 0 on success.
398 static int init_phy(struct net_device *dev)
400 struct gfar_private *priv = netdev_priv(dev);
401 uint gigabit_support =
402 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
403 SUPPORTED_1000baseT_Full : 0;
404 struct phy_device *phydev;
408 priv->oldduplex = -1;
410 phydev = phy_connect(dev, priv->einfo->bus_id, &adjust_link, 0);
412 if (IS_ERR(phydev)) {
413 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
414 return PTR_ERR(phydev);
417 /* Remove any features not supported by the controller */
418 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
419 phydev->advertising = phydev->supported;
421 priv->phydev = phydev;
426 static void init_registers(struct net_device *dev)
428 struct gfar_private *priv = netdev_priv(dev);
431 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
433 /* Initialize IMASK */
434 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
436 /* Init hash registers to zero */
437 gfar_write(&priv->regs->igaddr0, 0);
438 gfar_write(&priv->regs->igaddr1, 0);
439 gfar_write(&priv->regs->igaddr2, 0);
440 gfar_write(&priv->regs->igaddr3, 0);
441 gfar_write(&priv->regs->igaddr4, 0);
442 gfar_write(&priv->regs->igaddr5, 0);
443 gfar_write(&priv->regs->igaddr6, 0);
444 gfar_write(&priv->regs->igaddr7, 0);
446 gfar_write(&priv->regs->gaddr0, 0);
447 gfar_write(&priv->regs->gaddr1, 0);
448 gfar_write(&priv->regs->gaddr2, 0);
449 gfar_write(&priv->regs->gaddr3, 0);
450 gfar_write(&priv->regs->gaddr4, 0);
451 gfar_write(&priv->regs->gaddr5, 0);
452 gfar_write(&priv->regs->gaddr6, 0);
453 gfar_write(&priv->regs->gaddr7, 0);
455 /* Zero out the rmon mib registers if it has them */
456 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
457 memset((void *) &(priv->regs->rmon), 0,
458 sizeof (struct rmon_mib));
460 /* Mask off the CAM interrupts */
461 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
462 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
465 /* Initialize the max receive buffer length */
466 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
468 #ifdef CONFIG_GFAR_BUFSTASH
469 /* If we are stashing buffers, we need to set the
470 * extraction length to the size of the buffer */
471 gfar_write(&priv->regs->attreli, priv->rx_stash_size << 16);
474 /* Initialize the Minimum Frame Length Register */
475 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
477 /* Setup Attributes so that snooping is on for rx */
478 gfar_write(&priv->regs->attr, ATTR_INIT_SETTINGS);
479 gfar_write(&priv->regs->attreli, ATTRELI_INIT_SETTINGS);
481 /* Assign the TBI an address which won't conflict with the PHYs */
482 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
486 /* Halt the receive and transmit queues */
487 void gfar_halt(struct net_device *dev)
489 struct gfar_private *priv = netdev_priv(dev);
490 struct gfar *regs = priv->regs;
493 /* Mask all interrupts */
494 gfar_write(®s->imask, IMASK_INIT_CLEAR);
496 /* Clear all interrupts */
497 gfar_write(®s->ievent, IEVENT_INIT_CLEAR);
499 /* Stop the DMA, and wait for it to stop */
500 tempval = gfar_read(&priv->regs->dmactrl);
501 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
502 != (DMACTRL_GRS | DMACTRL_GTS)) {
503 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
504 gfar_write(&priv->regs->dmactrl, tempval);
506 while (!(gfar_read(&priv->regs->ievent) &
507 (IEVENT_GRSC | IEVENT_GTSC)))
511 /* Disable Rx and Tx */
512 tempval = gfar_read(®s->maccfg1);
513 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
514 gfar_write(®s->maccfg1, tempval);
517 void stop_gfar(struct net_device *dev)
519 struct gfar_private *priv = netdev_priv(dev);
520 struct gfar *regs = priv->regs;
523 phy_stop(priv->phydev);
526 spin_lock_irqsave(&priv->lock, flags);
530 spin_unlock_irqrestore(&priv->lock, flags);
533 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
534 free_irq(priv->interruptError, dev);
535 free_irq(priv->interruptTransmit, dev);
536 free_irq(priv->interruptReceive, dev);
538 free_irq(priv->interruptTransmit, dev);
541 free_skb_resources(priv);
543 dma_free_coherent(NULL,
544 sizeof(struct txbd8)*priv->tx_ring_size
545 + sizeof(struct rxbd8)*priv->rx_ring_size,
547 gfar_read(®s->tbase0));
550 /* If there are any tx skbs or rx skbs still around, free them.
551 * Then free tx_skbuff and rx_skbuff */
552 static void free_skb_resources(struct gfar_private *priv)
558 /* Go through all the buffer descriptors and free their data buffers */
559 txbdp = priv->tx_bd_base;
561 for (i = 0; i < priv->tx_ring_size; i++) {
563 if (priv->tx_skbuff[i]) {
564 dma_unmap_single(NULL, txbdp->bufPtr,
567 dev_kfree_skb_any(priv->tx_skbuff[i]);
568 priv->tx_skbuff[i] = NULL;
572 kfree(priv->tx_skbuff);
574 rxbdp = priv->rx_bd_base;
576 /* rx_skbuff is not guaranteed to be allocated, so only
577 * free it and its contents if it is allocated */
578 if(priv->rx_skbuff != NULL) {
579 for (i = 0; i < priv->rx_ring_size; i++) {
580 if (priv->rx_skbuff[i]) {
581 dma_unmap_single(NULL, rxbdp->bufPtr,
586 dev_kfree_skb_any(priv->rx_skbuff[i]);
587 priv->rx_skbuff[i] = NULL;
597 kfree(priv->rx_skbuff);
601 void gfar_start(struct net_device *dev)
603 struct gfar_private *priv = netdev_priv(dev);
604 struct gfar *regs = priv->regs;
607 /* Enable Rx and Tx in MACCFG1 */
608 tempval = gfar_read(®s->maccfg1);
609 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
610 gfar_write(®s->maccfg1, tempval);
612 /* Initialize DMACTRL to have WWR and WOP */
613 tempval = gfar_read(&priv->regs->dmactrl);
614 tempval |= DMACTRL_INIT_SETTINGS;
615 gfar_write(&priv->regs->dmactrl, tempval);
617 /* Clear THLT, so that the DMA starts polling now */
618 gfar_write(®s->tstat, TSTAT_CLEAR_THALT);
620 /* Make sure we aren't stopped */
621 tempval = gfar_read(&priv->regs->dmactrl);
622 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
623 gfar_write(&priv->regs->dmactrl, tempval);
625 /* Unmask the interrupts we look for */
626 gfar_write(®s->imask, IMASK_DEFAULT);
629 /* Bring the controller up and running */
630 int startup_gfar(struct net_device *dev)
637 struct gfar_private *priv = netdev_priv(dev);
638 struct gfar *regs = priv->regs;
642 gfar_write(®s->imask, IMASK_INIT_CLEAR);
644 /* Allocate memory for the buffer descriptors */
645 vaddr = (unsigned long) dma_alloc_coherent(NULL,
646 sizeof (struct txbd8) * priv->tx_ring_size +
647 sizeof (struct rxbd8) * priv->rx_ring_size,
651 if (netif_msg_ifup(priv))
652 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
657 priv->tx_bd_base = (struct txbd8 *) vaddr;
659 /* enet DMA only understands physical addresses */
660 gfar_write(®s->tbase0, addr);
662 /* Start the rx descriptor ring where the tx ring leaves off */
663 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
664 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
665 priv->rx_bd_base = (struct rxbd8 *) vaddr;
666 gfar_write(®s->rbase0, addr);
668 /* Setup the skbuff rings */
670 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
671 priv->tx_ring_size, GFP_KERNEL);
673 if (NULL == priv->tx_skbuff) {
674 if (netif_msg_ifup(priv))
675 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
681 for (i = 0; i < priv->tx_ring_size; i++)
682 priv->tx_skbuff[i] = NULL;
685 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
686 priv->rx_ring_size, GFP_KERNEL);
688 if (NULL == priv->rx_skbuff) {
689 if (netif_msg_ifup(priv))
690 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
696 for (i = 0; i < priv->rx_ring_size; i++)
697 priv->rx_skbuff[i] = NULL;
699 /* Initialize some variables in our dev structure */
700 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
701 priv->cur_rx = priv->rx_bd_base;
702 priv->skb_curtx = priv->skb_dirtytx = 0;
705 /* Initialize Transmit Descriptor Ring */
706 txbdp = priv->tx_bd_base;
707 for (i = 0; i < priv->tx_ring_size; i++) {
714 /* Set the last descriptor in the ring to indicate wrap */
716 txbdp->status |= TXBD_WRAP;
718 rxbdp = priv->rx_bd_base;
719 for (i = 0; i < priv->rx_ring_size; i++) {
720 struct sk_buff *skb = NULL;
724 skb = gfar_new_skb(dev, rxbdp);
726 priv->rx_skbuff[i] = skb;
731 /* Set the last descriptor in the ring to wrap */
733 rxbdp->status |= RXBD_WRAP;
735 /* If the device has multiple interrupts, register for
736 * them. Otherwise, only register for the one */
737 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
738 /* Install our interrupt handlers for Error,
739 * Transmit, and Receive */
740 if (request_irq(priv->interruptError, gfar_error,
741 0, "enet_error", dev) < 0) {
742 if (netif_msg_intr(priv))
743 printk(KERN_ERR "%s: Can't get IRQ %d\n",
744 dev->name, priv->interruptError);
750 if (request_irq(priv->interruptTransmit, gfar_transmit,
751 0, "enet_tx", dev) < 0) {
752 if (netif_msg_intr(priv))
753 printk(KERN_ERR "%s: Can't get IRQ %d\n",
754 dev->name, priv->interruptTransmit);
761 if (request_irq(priv->interruptReceive, gfar_receive,
762 0, "enet_rx", dev) < 0) {
763 if (netif_msg_intr(priv))
764 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
765 dev->name, priv->interruptReceive);
771 if (request_irq(priv->interruptTransmit, gfar_interrupt,
772 0, "gfar_interrupt", dev) < 0) {
773 if (netif_msg_intr(priv))
774 printk(KERN_ERR "%s: Can't get IRQ %d\n",
775 dev->name, priv->interruptError);
782 phy_start(priv->phydev);
784 /* Configure the coalescing support */
785 if (priv->txcoalescing)
786 gfar_write(®s->txic,
787 mk_ic_value(priv->txcount, priv->txtime));
789 gfar_write(®s->txic, 0);
791 if (priv->rxcoalescing)
792 gfar_write(®s->rxic,
793 mk_ic_value(priv->rxcount, priv->rxtime));
795 gfar_write(®s->rxic, 0);
797 if (priv->rx_csum_enable)
798 rctrl |= RCTRL_CHECKSUMMING;
800 if (priv->extended_hash)
801 rctrl |= RCTRL_EXTHASH;
803 if (priv->vlan_enable)
806 /* Init rctrl based on our settings */
807 gfar_write(&priv->regs->rctrl, rctrl);
809 if (dev->features & NETIF_F_IP_CSUM)
810 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
817 free_irq(priv->interruptTransmit, dev);
819 free_irq(priv->interruptError, dev);
822 free_skb_resources(priv);
824 dma_free_coherent(NULL,
825 sizeof(struct txbd8)*priv->tx_ring_size
826 + sizeof(struct rxbd8)*priv->rx_ring_size,
828 gfar_read(®s->tbase0));
833 /* Called when something needs to use the ethernet device */
834 /* Returns 0 for success. */
835 static int gfar_enet_open(struct net_device *dev)
839 /* Initialize a bunch of registers */
842 gfar_set_mac_address(dev);
849 err = startup_gfar(dev);
851 netif_start_queue(dev);
856 static struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
858 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
860 memset(fcb, 0, GMAC_FCB_LEN);
862 /* Flag the bd so the controller looks for the FCB */
863 bdp->status |= TXBD_TOE;
868 static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
872 /* If we're here, it's a IP packet with a TCP or UDP
873 * payload. We set it to checksum, using a pseudo-header
881 /* Notify the controller what the protocol is */
882 if (skb->nh.iph->protocol == IPPROTO_UDP)
885 /* l3os is the distance between the start of the
886 * frame (skb->data) and the start of the IP hdr.
887 * l4os is the distance between the start of the
888 * l3 hdr and the l4 hdr */
889 fcb->l3os = (u16)(skb->nh.raw - skb->data - GMAC_FCB_LEN);
890 fcb->l4os = (u16)(skb->h.raw - skb->nh.raw);
892 len = skb->nh.iph->tot_len - fcb->l4os;
894 /* Provide the pseudoheader csum */
895 fcb->phcs = ~csum_tcpudp_magic(skb->nh.iph->saddr,
896 skb->nh.iph->daddr, len,
897 skb->nh.iph->protocol, 0);
900 void gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
903 fcb->vlctl = vlan_tx_tag_get(skb);
906 /* This is called by the kernel when a frame is ready for transmission. */
907 /* It is pointed to by the dev->hard_start_xmit function pointer */
908 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
910 struct gfar_private *priv = netdev_priv(dev);
911 struct txfcb *fcb = NULL;
914 /* Update transmit stats */
915 priv->stats.tx_bytes += skb->len;
918 spin_lock_irq(&priv->lock);
920 /* Point at the first free tx descriptor */
921 txbdp = priv->cur_tx;
923 /* Clear all but the WRAP status flags */
924 txbdp->status &= TXBD_WRAP;
926 /* Set up checksumming */
927 if ((dev->features & NETIF_F_IP_CSUM)
928 && (CHECKSUM_HW == skb->ip_summed)) {
929 fcb = gfar_add_fcb(skb, txbdp);
930 gfar_tx_checksum(skb, fcb);
933 if (priv->vlan_enable &&
934 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
936 fcb = gfar_add_fcb(skb, txbdp);
938 gfar_tx_vlan(skb, fcb);
941 /* Set buffer length and pointer */
942 txbdp->length = skb->len;
943 txbdp->bufPtr = dma_map_single(NULL, skb->data,
944 skb->len, DMA_TO_DEVICE);
946 /* Save the skb pointer so we can free it later */
947 priv->tx_skbuff[priv->skb_curtx] = skb;
949 /* Update the current skb pointer (wrapping if this was the last) */
951 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
953 /* Flag the BD as interrupt-causing */
954 txbdp->status |= TXBD_INTERRUPT;
956 /* Flag the BD as ready to go, last in frame, and */
958 txbdp->status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
960 dev->trans_start = jiffies;
962 /* If this was the last BD in the ring, the next one */
963 /* is at the beginning of the ring */
964 if (txbdp->status & TXBD_WRAP)
965 txbdp = priv->tx_bd_base;
969 /* If the next BD still needs to be cleaned up, then the bds
970 are full. We need to tell the kernel to stop sending us stuff. */
971 if (txbdp == priv->dirty_tx) {
972 netif_stop_queue(dev);
974 priv->stats.tx_fifo_errors++;
977 /* Update the current txbd to the next one */
978 priv->cur_tx = txbdp;
980 /* Tell the DMA to go go go */
981 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
984 spin_unlock_irq(&priv->lock);
989 /* Stops the kernel queue, and halts the controller */
990 static int gfar_close(struct net_device *dev)
992 struct gfar_private *priv = netdev_priv(dev);
995 /* Disconnect from the PHY */
996 phy_disconnect(priv->phydev);
999 netif_stop_queue(dev);
1004 /* returns a net_device_stats structure pointer */
1005 static struct net_device_stats * gfar_get_stats(struct net_device *dev)
1007 struct gfar_private *priv = netdev_priv(dev);
1009 return &(priv->stats);
1012 /* Changes the mac address if the controller is not running. */
1013 int gfar_set_mac_address(struct net_device *dev)
1015 struct gfar_private *priv = netdev_priv(dev);
1017 char tmpbuf[MAC_ADDR_LEN];
1020 /* Now copy it into the mac registers backwards, cuz */
1021 /* little endian is silly */
1022 for (i = 0; i < MAC_ADDR_LEN; i++)
1023 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->dev_addr[i];
1025 gfar_write(&priv->regs->macstnaddr1, *((u32 *) (tmpbuf)));
1027 tempval = *((u32 *) (tmpbuf + 4));
1029 gfar_write(&priv->regs->macstnaddr2, tempval);
1035 /* Enables and disables VLAN insertion/extraction */
1036 static void gfar_vlan_rx_register(struct net_device *dev,
1037 struct vlan_group *grp)
1039 struct gfar_private *priv = netdev_priv(dev);
1040 unsigned long flags;
1043 spin_lock_irqsave(&priv->lock, flags);
1048 /* Enable VLAN tag insertion */
1049 tempval = gfar_read(&priv->regs->tctrl);
1050 tempval |= TCTRL_VLINS;
1052 gfar_write(&priv->regs->tctrl, tempval);
1054 /* Enable VLAN tag extraction */
1055 tempval = gfar_read(&priv->regs->rctrl);
1056 tempval |= RCTRL_VLEX;
1057 gfar_write(&priv->regs->rctrl, tempval);
1059 /* Disable VLAN tag insertion */
1060 tempval = gfar_read(&priv->regs->tctrl);
1061 tempval &= ~TCTRL_VLINS;
1062 gfar_write(&priv->regs->tctrl, tempval);
1064 /* Disable VLAN tag extraction */
1065 tempval = gfar_read(&priv->regs->rctrl);
1066 tempval &= ~RCTRL_VLEX;
1067 gfar_write(&priv->regs->rctrl, tempval);
1070 spin_unlock_irqrestore(&priv->lock, flags);
1074 static void gfar_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
1076 struct gfar_private *priv = netdev_priv(dev);
1077 unsigned long flags;
1079 spin_lock_irqsave(&priv->lock, flags);
1082 priv->vlgrp->vlan_devices[vid] = NULL;
1084 spin_unlock_irqrestore(&priv->lock, flags);
1088 static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1090 int tempsize, tempval;
1091 struct gfar_private *priv = netdev_priv(dev);
1092 int oldsize = priv->rx_buffer_size;
1093 int frame_size = new_mtu + ETH_HLEN;
1095 if (priv->vlan_enable)
1096 frame_size += VLAN_ETH_HLEN;
1098 if (gfar_uses_fcb(priv))
1099 frame_size += GMAC_FCB_LEN;
1101 frame_size += priv->padding;
1103 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
1104 if (netif_msg_drv(priv))
1105 printk(KERN_ERR "%s: Invalid MTU setting\n",
1111 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1112 INCREMENTAL_BUFFER_SIZE;
1114 /* Only stop and start the controller if it isn't already
1116 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1119 priv->rx_buffer_size = tempsize;
1123 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1124 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1126 /* If the mtu is larger than the max size for standard
1127 * ethernet frames (ie, a jumbo frame), then set maccfg2
1128 * to allow huge frames, and to check the length */
1129 tempval = gfar_read(&priv->regs->maccfg2);
1131 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1132 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1134 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1136 gfar_write(&priv->regs->maccfg2, tempval);
1138 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1144 /* gfar_timeout gets called when a packet has not been
1145 * transmitted after a set amount of time.
1146 * For now, assume that clearing out all the structures, and
1147 * starting over will fix the problem. */
1148 static void gfar_timeout(struct net_device *dev)
1150 struct gfar_private *priv = netdev_priv(dev);
1152 priv->stats.tx_errors++;
1154 if (dev->flags & IFF_UP) {
1159 netif_schedule(dev);
1162 /* Interrupt Handler for Transmit complete */
1163 static irqreturn_t gfar_transmit(int irq, void *dev_id, struct pt_regs *regs)
1165 struct net_device *dev = (struct net_device *) dev_id;
1166 struct gfar_private *priv = netdev_priv(dev);
1170 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1173 spin_lock(&priv->lock);
1174 bdp = priv->dirty_tx;
1175 while ((bdp->status & TXBD_READY) == 0) {
1176 /* If dirty_tx and cur_tx are the same, then either the */
1177 /* ring is empty or full now (it could only be full in the beginning, */
1178 /* obviously). If it is empty, we are done. */
1179 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1182 priv->stats.tx_packets++;
1184 /* Deferred means some collisions occurred during transmit, */
1185 /* but we eventually sent the packet. */
1186 if (bdp->status & TXBD_DEF)
1187 priv->stats.collisions++;
1189 /* Free the sk buffer associated with this TxBD */
1190 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1191 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1193 (priv->skb_dirtytx +
1194 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1196 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1197 if (bdp->status & TXBD_WRAP)
1198 bdp = priv->tx_bd_base;
1202 /* Move dirty_tx to be the next bd */
1203 priv->dirty_tx = bdp;
1205 /* We freed a buffer, so now we can restart transmission */
1206 if (netif_queue_stopped(dev))
1207 netif_wake_queue(dev);
1208 } /* while ((bdp->status & TXBD_READY) == 0) */
1210 /* If we are coalescing the interrupts, reset the timer */
1211 /* Otherwise, clear it */
1212 if (priv->txcoalescing)
1213 gfar_write(&priv->regs->txic,
1214 mk_ic_value(priv->txcount, priv->txtime));
1216 gfar_write(&priv->regs->txic, 0);
1218 spin_unlock(&priv->lock);
1223 struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1225 struct gfar_private *priv = netdev_priv(dev);
1226 struct sk_buff *skb = NULL;
1227 unsigned int timeout = SKB_ALLOC_TIMEOUT;
1229 /* We have to allocate the skb, so keep trying till we succeed */
1230 while ((!skb) && timeout--)
1231 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1236 /* We need the data buffer to be aligned properly. We will reserve
1237 * as many bytes as needed to align the data properly
1241 (((unsigned) skb->data) & (RXBUF_ALIGNMENT - 1)));
1245 bdp->bufPtr = dma_map_single(NULL, skb->data,
1246 priv->rx_buffer_size + RXBUF_ALIGNMENT,
1251 /* Mark the buffer empty */
1252 bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1257 static inline void count_errors(unsigned short status, struct gfar_private *priv)
1259 struct net_device_stats *stats = &priv->stats;
1260 struct gfar_extra_stats *estats = &priv->extra_stats;
1262 /* If the packet was truncated, none of the other errors
1264 if (status & RXBD_TRUNCATED) {
1265 stats->rx_length_errors++;
1271 /* Count the errors, if there were any */
1272 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1273 stats->rx_length_errors++;
1275 if (status & RXBD_LARGE)
1280 if (status & RXBD_NONOCTET) {
1281 stats->rx_frame_errors++;
1282 estats->rx_nonoctet++;
1284 if (status & RXBD_CRCERR) {
1285 estats->rx_crcerr++;
1286 stats->rx_crc_errors++;
1288 if (status & RXBD_OVERRUN) {
1289 estats->rx_overrun++;
1290 stats->rx_crc_errors++;
1294 irqreturn_t gfar_receive(int irq, void *dev_id, struct pt_regs *regs)
1296 struct net_device *dev = (struct net_device *) dev_id;
1297 struct gfar_private *priv = netdev_priv(dev);
1299 #ifdef CONFIG_GFAR_NAPI
1303 /* Clear IEVENT, so rx interrupt isn't called again
1304 * because of this interrupt */
1305 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1308 #ifdef CONFIG_GFAR_NAPI
1309 if (netif_rx_schedule_prep(dev)) {
1310 tempval = gfar_read(&priv->regs->imask);
1311 tempval &= IMASK_RX_DISABLED;
1312 gfar_write(&priv->regs->imask, tempval);
1314 __netif_rx_schedule(dev);
1316 if (netif_msg_rx_err(priv))
1317 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1318 dev->name, gfar_read(&priv->regs->ievent),
1319 gfar_read(&priv->regs->imask));
1323 spin_lock(&priv->lock);
1324 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1326 /* If we are coalescing interrupts, update the timer */
1327 /* Otherwise, clear it */
1328 if (priv->rxcoalescing)
1329 gfar_write(&priv->regs->rxic,
1330 mk_ic_value(priv->rxcount, priv->rxtime));
1332 gfar_write(&priv->regs->rxic, 0);
1334 spin_unlock(&priv->lock);
1340 static inline int gfar_rx_vlan(struct sk_buff *skb,
1341 struct vlan_group *vlgrp, unsigned short vlctl)
1343 #ifdef CONFIG_GFAR_NAPI
1344 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1346 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1350 static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1352 /* If valid headers were found, and valid sums
1353 * were verified, then we tell the kernel that no
1354 * checksumming is necessary. Otherwise, it is */
1355 if (fcb->cip && !fcb->eip && fcb->ctu && !fcb->etu)
1356 skb->ip_summed = CHECKSUM_UNNECESSARY;
1358 skb->ip_summed = CHECKSUM_NONE;
1362 static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1364 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1366 /* Remove the FCB from the skb */
1367 skb_pull(skb, GMAC_FCB_LEN);
1372 /* gfar_process_frame() -- handle one incoming packet if skb
1374 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1377 struct gfar_private *priv = netdev_priv(dev);
1378 struct rxfcb *fcb = NULL;
1381 if (netif_msg_rx_err(priv))
1382 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
1383 priv->stats.rx_dropped++;
1384 priv->extra_stats.rx_skbmissing++;
1388 /* Prep the skb for the packet */
1389 skb_put(skb, length);
1391 /* Grab the FCB if there is one */
1392 if (gfar_uses_fcb(priv))
1393 fcb = gfar_get_fcb(skb);
1395 /* Remove the padded bytes, if there are any */
1397 skb_pull(skb, priv->padding);
1399 if (priv->rx_csum_enable)
1400 gfar_rx_checksum(skb, fcb);
1402 /* Tell the skb what kind of packet this is */
1403 skb->protocol = eth_type_trans(skb, dev);
1405 /* Send the packet up the stack */
1406 if (unlikely(priv->vlgrp && fcb->vln))
1407 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1411 if (NET_RX_DROP == ret)
1412 priv->extra_stats.kernel_dropped++;
1418 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
1419 * until the budget/quota has been reached. Returns the number
1422 int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
1425 struct sk_buff *skb;
1428 struct gfar_private *priv = netdev_priv(dev);
1430 /* Get the first full descriptor */
1433 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
1434 skb = priv->rx_skbuff[priv->skb_currx];
1437 (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1438 | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1439 /* Increment the number of packets */
1440 priv->stats.rx_packets++;
1443 /* Remove the FCS from the packet length */
1444 pkt_len = bdp->length - 4;
1446 gfar_process_frame(dev, skb, pkt_len);
1448 priv->stats.rx_bytes += pkt_len;
1450 count_errors(bdp->status, priv);
1453 dev_kfree_skb_any(skb);
1455 priv->rx_skbuff[priv->skb_currx] = NULL;
1458 dev->last_rx = jiffies;
1460 /* Clear the status flags for this buffer */
1461 bdp->status &= ~RXBD_STATS;
1463 /* Add another skb for the future */
1464 skb = gfar_new_skb(dev, bdp);
1465 priv->rx_skbuff[priv->skb_currx] = skb;
1467 /* Update to the next pointer */
1468 if (bdp->status & RXBD_WRAP)
1469 bdp = priv->rx_bd_base;
1473 /* update to point at the next skb */
1476 1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1480 /* Update the current rxbd pointer to be the next one */
1483 /* If no packets have arrived since the
1484 * last one we processed, clear the IEVENT RX and
1485 * BSY bits so that another interrupt won't be
1486 * generated when we set IMASK */
1487 if (bdp->status & RXBD_EMPTY)
1488 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1493 #ifdef CONFIG_GFAR_NAPI
1494 static int gfar_poll(struct net_device *dev, int *budget)
1497 struct gfar_private *priv = netdev_priv(dev);
1498 int rx_work_limit = *budget;
1500 if (rx_work_limit > dev->quota)
1501 rx_work_limit = dev->quota;
1503 howmany = gfar_clean_rx_ring(dev, rx_work_limit);
1505 dev->quota -= howmany;
1506 rx_work_limit -= howmany;
1509 if (rx_work_limit >= 0) {
1510 netif_rx_complete(dev);
1512 /* Clear the halt bit in RSTAT */
1513 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1515 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1517 /* If we are coalescing interrupts, update the timer */
1518 /* Otherwise, clear it */
1519 if (priv->rxcoalescing)
1520 gfar_write(&priv->regs->rxic,
1521 mk_ic_value(priv->rxcount, priv->rxtime));
1523 gfar_write(&priv->regs->rxic, 0);
1526 return (rx_work_limit < 0) ? 1 : 0;
1530 /* The interrupt handler for devices with one interrupt */
1531 static irqreturn_t gfar_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1533 struct net_device *dev = dev_id;
1534 struct gfar_private *priv = netdev_priv(dev);
1536 /* Save ievent for future reference */
1537 u32 events = gfar_read(&priv->regs->ievent);
1540 gfar_write(&priv->regs->ievent, events);
1542 /* Check for reception */
1543 if ((events & IEVENT_RXF0) || (events & IEVENT_RXB0))
1544 gfar_receive(irq, dev_id, regs);
1546 /* Check for transmit completion */
1547 if ((events & IEVENT_TXF) || (events & IEVENT_TXB))
1548 gfar_transmit(irq, dev_id, regs);
1550 /* Update error statistics */
1551 if (events & IEVENT_TXE) {
1552 priv->stats.tx_errors++;
1554 if (events & IEVENT_LC)
1555 priv->stats.tx_window_errors++;
1556 if (events & IEVENT_CRL)
1557 priv->stats.tx_aborted_errors++;
1558 if (events & IEVENT_XFUN) {
1559 if (netif_msg_tx_err(priv))
1560 printk(KERN_WARNING "%s: tx underrun. dropped packet\n", dev->name);
1561 priv->stats.tx_dropped++;
1562 priv->extra_stats.tx_underrun++;
1564 /* Reactivate the Tx Queues */
1565 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1568 if (events & IEVENT_BSY) {
1569 priv->stats.rx_errors++;
1570 priv->extra_stats.rx_bsy++;
1572 gfar_receive(irq, dev_id, regs);
1574 #ifndef CONFIG_GFAR_NAPI
1575 /* Clear the halt bit in RSTAT */
1576 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1579 if (netif_msg_rx_err(priv))
1580 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n",
1582 gfar_read(&priv->regs->rstat));
1584 if (events & IEVENT_BABR) {
1585 priv->stats.rx_errors++;
1586 priv->extra_stats.rx_babr++;
1588 if (netif_msg_rx_err(priv))
1589 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
1591 if (events & IEVENT_EBERR) {
1592 priv->extra_stats.eberr++;
1593 if (netif_msg_rx_err(priv))
1594 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
1596 if ((events & IEVENT_RXC) && (netif_msg_rx_err(priv)))
1597 printk(KERN_DEBUG "%s: control frame\n", dev->name);
1599 if (events & IEVENT_BABT) {
1600 priv->extra_stats.tx_babt++;
1601 if (netif_msg_rx_err(priv))
1602 printk(KERN_DEBUG "%s: babt error\n", dev->name);
1608 /* Called every time the controller might need to be made
1609 * aware of new link state. The PHY code conveys this
1610 * information through variables in the phydev structure, and this
1611 * function converts those variables into the appropriate
1612 * register values, and can bring down the device if needed.
1614 static void adjust_link(struct net_device *dev)
1616 struct gfar_private *priv = netdev_priv(dev);
1617 struct gfar *regs = priv->regs;
1618 unsigned long flags;
1619 struct phy_device *phydev = priv->phydev;
1622 spin_lock_irqsave(&priv->lock, flags);
1624 u32 tempval = gfar_read(®s->maccfg2);
1626 /* Now we make sure that we can be in full duplex mode.
1627 * If not, we operate in half-duplex mode. */
1628 if (phydev->duplex != priv->oldduplex) {
1630 if (!(phydev->duplex))
1631 tempval &= ~(MACCFG2_FULL_DUPLEX);
1633 tempval |= MACCFG2_FULL_DUPLEX;
1635 priv->oldduplex = phydev->duplex;
1638 if (phydev->speed != priv->oldspeed) {
1640 switch (phydev->speed) {
1643 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
1648 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
1651 if (netif_msg_link(priv))
1653 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1654 dev->name, phydev->speed);
1658 priv->oldspeed = phydev->speed;
1661 gfar_write(®s->maccfg2, tempval);
1663 if (!priv->oldlink) {
1666 netif_schedule(dev);
1668 } else if (priv->oldlink) {
1672 priv->oldduplex = -1;
1675 if (new_state && netif_msg_link(priv))
1676 phy_print_status(phydev);
1678 spin_unlock_irqrestore(&priv->lock, flags);
1681 /* Update the hash table based on the current list of multicast
1682 * addresses we subscribe to. Also, change the promiscuity of
1683 * the device based on the flags (this function is called
1684 * whenever dev->flags is changed */
1685 static void gfar_set_multi(struct net_device *dev)
1687 struct dev_mc_list *mc_ptr;
1688 struct gfar_private *priv = netdev_priv(dev);
1689 struct gfar *regs = priv->regs;
1692 if(dev->flags & IFF_PROMISC) {
1693 if (netif_msg_drv(priv))
1694 printk(KERN_INFO "%s: Entering promiscuous mode.\n",
1696 /* Set RCTRL to PROM */
1697 tempval = gfar_read(®s->rctrl);
1698 tempval |= RCTRL_PROM;
1699 gfar_write(®s->rctrl, tempval);
1701 /* Set RCTRL to not PROM */
1702 tempval = gfar_read(®s->rctrl);
1703 tempval &= ~(RCTRL_PROM);
1704 gfar_write(®s->rctrl, tempval);
1707 if(dev->flags & IFF_ALLMULTI) {
1708 /* Set the hash to rx all multicast frames */
1709 gfar_write(®s->igaddr0, 0xffffffff);
1710 gfar_write(®s->igaddr1, 0xffffffff);
1711 gfar_write(®s->igaddr2, 0xffffffff);
1712 gfar_write(®s->igaddr3, 0xffffffff);
1713 gfar_write(®s->igaddr4, 0xffffffff);
1714 gfar_write(®s->igaddr5, 0xffffffff);
1715 gfar_write(®s->igaddr6, 0xffffffff);
1716 gfar_write(®s->igaddr7, 0xffffffff);
1717 gfar_write(®s->gaddr0, 0xffffffff);
1718 gfar_write(®s->gaddr1, 0xffffffff);
1719 gfar_write(®s->gaddr2, 0xffffffff);
1720 gfar_write(®s->gaddr3, 0xffffffff);
1721 gfar_write(®s->gaddr4, 0xffffffff);
1722 gfar_write(®s->gaddr5, 0xffffffff);
1723 gfar_write(®s->gaddr6, 0xffffffff);
1724 gfar_write(®s->gaddr7, 0xffffffff);
1726 /* zero out the hash */
1727 gfar_write(®s->igaddr0, 0x0);
1728 gfar_write(®s->igaddr1, 0x0);
1729 gfar_write(®s->igaddr2, 0x0);
1730 gfar_write(®s->igaddr3, 0x0);
1731 gfar_write(®s->igaddr4, 0x0);
1732 gfar_write(®s->igaddr5, 0x0);
1733 gfar_write(®s->igaddr6, 0x0);
1734 gfar_write(®s->igaddr7, 0x0);
1735 gfar_write(®s->gaddr0, 0x0);
1736 gfar_write(®s->gaddr1, 0x0);
1737 gfar_write(®s->gaddr2, 0x0);
1738 gfar_write(®s->gaddr3, 0x0);
1739 gfar_write(®s->gaddr4, 0x0);
1740 gfar_write(®s->gaddr5, 0x0);
1741 gfar_write(®s->gaddr6, 0x0);
1742 gfar_write(®s->gaddr7, 0x0);
1744 if(dev->mc_count == 0)
1747 /* Parse the list, and set the appropriate bits */
1748 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
1749 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
1756 /* Set the appropriate hash bit for the given addr */
1757 /* The algorithm works like so:
1758 * 1) Take the Destination Address (ie the multicast address), and
1759 * do a CRC on it (little endian), and reverse the bits of the
1761 * 2) Use the 8 most significant bits as a hash into a 256-entry
1762 * table. The table is controlled through 8 32-bit registers:
1763 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1764 * gaddr7. This means that the 3 most significant bits in the
1765 * hash index which gaddr register to use, and the 5 other bits
1766 * indicate which bit (assuming an IBM numbering scheme, which
1767 * for PowerPC (tm) is usually the case) in the register holds
1769 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1772 struct gfar_private *priv = netdev_priv(dev);
1773 u32 result = ether_crc(MAC_ADDR_LEN, addr);
1774 int width = priv->hash_width;
1775 u8 whichbit = (result >> (32 - width)) & 0x1f;
1776 u8 whichreg = result >> (32 - width + 5);
1777 u32 value = (1 << (31-whichbit));
1779 tempval = gfar_read(priv->hash_regs[whichreg]);
1781 gfar_write(priv->hash_regs[whichreg], tempval);
1786 /* GFAR error interrupt handler */
1787 static irqreturn_t gfar_error(int irq, void *dev_id, struct pt_regs *regs)
1789 struct net_device *dev = dev_id;
1790 struct gfar_private *priv = netdev_priv(dev);
1792 /* Save ievent for future reference */
1793 u32 events = gfar_read(&priv->regs->ievent);
1796 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1799 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1800 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
1801 dev->name, events, gfar_read(&priv->regs->imask));
1803 /* Update the error counters */
1804 if (events & IEVENT_TXE) {
1805 priv->stats.tx_errors++;
1807 if (events & IEVENT_LC)
1808 priv->stats.tx_window_errors++;
1809 if (events & IEVENT_CRL)
1810 priv->stats.tx_aborted_errors++;
1811 if (events & IEVENT_XFUN) {
1812 if (netif_msg_tx_err(priv))
1813 printk(KERN_DEBUG "%s: underrun. packet dropped.\n",
1815 priv->stats.tx_dropped++;
1816 priv->extra_stats.tx_underrun++;
1818 /* Reactivate the Tx Queues */
1819 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1821 if (netif_msg_tx_err(priv))
1822 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
1824 if (events & IEVENT_BSY) {
1825 priv->stats.rx_errors++;
1826 priv->extra_stats.rx_bsy++;
1828 gfar_receive(irq, dev_id, regs);
1830 #ifndef CONFIG_GFAR_NAPI
1831 /* Clear the halt bit in RSTAT */
1832 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1835 if (netif_msg_rx_err(priv))
1836 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n",
1838 gfar_read(&priv->regs->rstat));
1840 if (events & IEVENT_BABR) {
1841 priv->stats.rx_errors++;
1842 priv->extra_stats.rx_babr++;
1844 if (netif_msg_rx_err(priv))
1845 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
1847 if (events & IEVENT_EBERR) {
1848 priv->extra_stats.eberr++;
1849 if (netif_msg_rx_err(priv))
1850 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
1852 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
1853 if (netif_msg_rx_status(priv))
1854 printk(KERN_DEBUG "%s: control frame\n", dev->name);
1856 if (events & IEVENT_BABT) {
1857 priv->extra_stats.tx_babt++;
1858 if (netif_msg_tx_err(priv))
1859 printk(KERN_DEBUG "%s: babt error\n", dev->name);
1864 /* Structure for a device driver */
1865 static struct device_driver gfar_driver = {
1866 .name = "fsl-gianfar",
1867 .bus = &platform_bus_type,
1868 .probe = gfar_probe,
1869 .remove = gfar_remove,
1872 static int __init gfar_init(void)
1874 int err = gfar_mdio_init();
1879 err = driver_register(&gfar_driver);
1887 static void __exit gfar_exit(void)
1889 driver_unregister(&gfar_driver);
1893 module_init(gfar_init);
1894 module_exit(gfar_exit);