2 * drivers/net/gianfar.c
4 * Gianfar Ethernet Driver
5 * This driver is designed for the non-CPM ethernet controllers
6 * on the 85xx and 83xx family of integrated processors
7 * Based on 8260_io/fcc_enet.c
10 * Maintainer: Kumar Gala
12 * Copyright (c) 2002-2006 Freescale Semiconductor, Inc.
13 * Copyright (c) 2007 MontaVista Software, Inc.
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
20 * Gianfar: AKA Lambda Draconis, "Dragon"
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/kernel.h>
69 #include <linux/string.h>
70 #include <linux/errno.h>
71 #include <linux/unistd.h>
72 #include <linux/slab.h>
73 #include <linux/interrupt.h>
74 #include <linux/init.h>
75 #include <linux/delay.h>
76 #include <linux/netdevice.h>
77 #include <linux/etherdevice.h>
78 #include <linux/skbuff.h>
79 #include <linux/if_vlan.h>
80 #include <linux/spinlock.h>
82 #include <linux/platform_device.h>
84 #include <linux/tcp.h>
85 #include <linux/udp.h>
90 #include <asm/uaccess.h>
91 #include <linux/module.h>
92 #include <linux/dma-mapping.h>
93 #include <linux/crc32.h>
94 #include <linux/mii.h>
95 #include <linux/phy.h>
98 #include "gianfar_mii.h"
100 #define TX_TIMEOUT (1*HZ)
101 #undef BRIEF_GFAR_ERRORS
102 #undef VERBOSE_GFAR_ERRORS
104 #ifdef CONFIG_GFAR_NAPI
105 #define RECEIVE(x) netif_receive_skb(x)
107 #define RECEIVE(x) netif_rx(x)
110 const char gfar_driver_name[] = "Gianfar Ethernet";
111 const char gfar_driver_version[] = "1.3";
113 static int gfar_enet_open(struct net_device *dev);
114 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
115 static void gfar_timeout(struct net_device *dev);
116 static int gfar_close(struct net_device *dev);
117 struct sk_buff *gfar_new_skb(struct net_device *dev);
118 static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
119 struct sk_buff *skb);
120 static int gfar_set_mac_address(struct net_device *dev);
121 static int gfar_change_mtu(struct net_device *dev, int new_mtu);
122 static irqreturn_t gfar_error(int irq, void *dev_id);
123 static irqreturn_t gfar_transmit(int irq, void *dev_id);
124 static irqreturn_t gfar_interrupt(int irq, void *dev_id);
125 static void adjust_link(struct net_device *dev);
126 static void init_registers(struct net_device *dev);
127 static int init_phy(struct net_device *dev);
128 static int gfar_probe(struct platform_device *pdev);
129 static int gfar_remove(struct platform_device *pdev);
130 static void free_skb_resources(struct gfar_private *priv);
131 static void gfar_set_multi(struct net_device *dev);
132 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
133 static void gfar_configure_serdes(struct net_device *dev);
134 #ifdef CONFIG_GFAR_NAPI
135 static int gfar_poll(struct napi_struct *napi, int budget);
137 #ifdef CONFIG_NET_POLL_CONTROLLER
138 static void gfar_netpoll(struct net_device *dev);
140 int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
141 static int gfar_clean_tx_ring(struct net_device *dev);
142 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
143 static void gfar_vlan_rx_register(struct net_device *netdev,
144 struct vlan_group *grp);
145 void gfar_halt(struct net_device *dev);
147 static void gfar_halt_nodisable(struct net_device *dev);
149 void gfar_start(struct net_device *dev);
150 static void gfar_clear_exact_match(struct net_device *dev);
151 static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
153 extern const struct ethtool_ops gfar_ethtool_ops;
155 MODULE_AUTHOR("Freescale Semiconductor, Inc");
156 MODULE_DESCRIPTION("Gianfar Ethernet Driver");
157 MODULE_LICENSE("GPL");
159 /* Returns 1 if incoming frames use an FCB */
160 static inline int gfar_uses_fcb(struct gfar_private *priv)
162 return (priv->vlan_enable || priv->rx_csum_enable);
165 /* Set up the ethernet device structure, private data,
166 * and anything else we need before we start */
167 static int gfar_probe(struct platform_device *pdev)
170 struct net_device *dev = NULL;
171 struct gfar_private *priv = NULL;
172 struct gianfar_platform_data *einfo;
175 DECLARE_MAC_BUF(mac);
177 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
180 printk(KERN_ERR "gfar %d: Missing additional data!\n",
186 /* Create an ethernet device instance */
187 dev = alloc_etherdev(sizeof (*priv));
192 priv = netdev_priv(dev);
195 /* Set the info in the priv to the current info */
198 /* fill out IRQ fields */
199 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
200 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
201 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
202 priv->interruptError = platform_get_irq_byname(pdev, "error");
203 if (priv->interruptTransmit < 0 || priv->interruptReceive < 0 || priv->interruptError < 0)
206 priv->interruptTransmit = platform_get_irq(pdev, 0);
207 if (priv->interruptTransmit < 0)
211 /* get a pointer to the register memory */
212 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
213 priv->regs = ioremap(r->start, sizeof (struct gfar));
215 if (NULL == priv->regs) {
220 spin_lock_init(&priv->txlock);
221 spin_lock_init(&priv->rxlock);
222 spin_lock_init(&priv->bflock);
224 platform_set_drvdata(pdev, dev);
226 /* Stop the DMA engine now, in case it was running before */
227 /* (The firmware could have used it, and left it running). */
228 /* To do this, we write Graceful Receive Stop and Graceful */
229 /* Transmit Stop, and then wait until the corresponding bits */
230 /* in IEVENT indicate the stops have completed. */
231 tempval = gfar_read(&priv->regs->dmactrl);
232 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
233 gfar_write(&priv->regs->dmactrl, tempval);
235 tempval = gfar_read(&priv->regs->dmactrl);
236 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
237 gfar_write(&priv->regs->dmactrl, tempval);
239 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
242 /* Reset MAC layer */
243 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
245 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
246 gfar_write(&priv->regs->maccfg1, tempval);
248 /* Initialize MACCFG2. */
249 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
251 /* Initialize ECNTRL */
252 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
254 /* Copy the station address into the dev structure, */
255 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
257 /* Set the dev->base_addr to the gfar reg region */
258 dev->base_addr = (unsigned long) (priv->regs);
260 SET_NETDEV_DEV(dev, &pdev->dev);
262 /* Fill in the dev structure */
263 dev->open = gfar_enet_open;
264 dev->hard_start_xmit = gfar_start_xmit;
265 dev->tx_timeout = gfar_timeout;
266 dev->watchdog_timeo = TX_TIMEOUT;
267 #ifdef CONFIG_GFAR_NAPI
268 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
270 #ifdef CONFIG_NET_POLL_CONTROLLER
271 dev->poll_controller = gfar_netpoll;
273 dev->stop = gfar_close;
274 dev->change_mtu = gfar_change_mtu;
276 dev->set_multicast_list = gfar_set_multi;
278 dev->ethtool_ops = &gfar_ethtool_ops;
280 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
281 priv->rx_csum_enable = 1;
282 dev->features |= NETIF_F_IP_CSUM;
284 priv->rx_csum_enable = 0;
288 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
289 dev->vlan_rx_register = gfar_vlan_rx_register;
291 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
293 priv->vlan_enable = 1;
296 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
297 priv->extended_hash = 1;
298 priv->hash_width = 9;
300 priv->hash_regs[0] = &priv->regs->igaddr0;
301 priv->hash_regs[1] = &priv->regs->igaddr1;
302 priv->hash_regs[2] = &priv->regs->igaddr2;
303 priv->hash_regs[3] = &priv->regs->igaddr3;
304 priv->hash_regs[4] = &priv->regs->igaddr4;
305 priv->hash_regs[5] = &priv->regs->igaddr5;
306 priv->hash_regs[6] = &priv->regs->igaddr6;
307 priv->hash_regs[7] = &priv->regs->igaddr7;
308 priv->hash_regs[8] = &priv->regs->gaddr0;
309 priv->hash_regs[9] = &priv->regs->gaddr1;
310 priv->hash_regs[10] = &priv->regs->gaddr2;
311 priv->hash_regs[11] = &priv->regs->gaddr3;
312 priv->hash_regs[12] = &priv->regs->gaddr4;
313 priv->hash_regs[13] = &priv->regs->gaddr5;
314 priv->hash_regs[14] = &priv->regs->gaddr6;
315 priv->hash_regs[15] = &priv->regs->gaddr7;
318 priv->extended_hash = 0;
319 priv->hash_width = 8;
321 priv->hash_regs[0] = &priv->regs->gaddr0;
322 priv->hash_regs[1] = &priv->regs->gaddr1;
323 priv->hash_regs[2] = &priv->regs->gaddr2;
324 priv->hash_regs[3] = &priv->regs->gaddr3;
325 priv->hash_regs[4] = &priv->regs->gaddr4;
326 priv->hash_regs[5] = &priv->regs->gaddr5;
327 priv->hash_regs[6] = &priv->regs->gaddr6;
328 priv->hash_regs[7] = &priv->regs->gaddr7;
331 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
332 priv->padding = DEFAULT_PADDING;
336 if (dev->features & NETIF_F_IP_CSUM)
337 dev->hard_header_len += GMAC_FCB_LEN;
339 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
340 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
341 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
343 priv->txcoalescing = DEFAULT_TX_COALESCE;
344 priv->txcount = DEFAULT_TXCOUNT;
345 priv->txtime = DEFAULT_TXTIME;
346 priv->rxcoalescing = DEFAULT_RX_COALESCE;
347 priv->rxcount = DEFAULT_RXCOUNT;
348 priv->rxtime = DEFAULT_RXTIME;
350 /* Enable most messages by default */
351 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
353 err = register_netdev(dev);
356 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
361 /* Create all the sysfs files */
362 gfar_init_sysfs(dev);
364 /* Print out the device info */
365 printk(KERN_INFO DEVICE_NAME "%s\n",
366 dev->name, print_mac(mac, dev->dev_addr));
368 /* Even more device info helps when determining which kernel */
369 /* provided which set of benchmarks. */
370 #ifdef CONFIG_GFAR_NAPI
371 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
373 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
375 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
376 dev->name, priv->rx_ring_size, priv->tx_ring_size);
387 static int gfar_remove(struct platform_device *pdev)
389 struct net_device *dev = platform_get_drvdata(pdev);
390 struct gfar_private *priv = netdev_priv(dev);
392 platform_set_drvdata(pdev, NULL);
401 static int gfar_suspend(struct platform_device *pdev, pm_message_t state)
403 struct net_device *dev = platform_get_drvdata(pdev);
404 struct gfar_private *priv = netdev_priv(dev);
408 int magic_packet = priv->wol_en &&
409 (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
411 netif_device_detach(dev);
413 if (netif_running(dev)) {
414 spin_lock_irqsave(&priv->txlock, flags);
415 spin_lock(&priv->rxlock);
417 gfar_halt_nodisable(dev);
419 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
420 tempval = gfar_read(&priv->regs->maccfg1);
422 tempval &= ~MACCFG1_TX_EN;
425 tempval &= ~MACCFG1_RX_EN;
427 gfar_write(&priv->regs->maccfg1, tempval);
429 spin_unlock(&priv->rxlock);
430 spin_unlock_irqrestore(&priv->txlock, flags);
432 #ifdef CONFIG_GFAR_NAPI
433 napi_disable(&priv->napi);
437 /* Enable interrupt on Magic Packet */
438 gfar_write(&priv->regs->imask, IMASK_MAG);
440 /* Enable Magic Packet mode */
441 tempval = gfar_read(&priv->regs->maccfg2);
442 tempval |= MACCFG2_MPEN;
443 gfar_write(&priv->regs->maccfg2, tempval);
445 phy_stop(priv->phydev);
452 static int gfar_resume(struct platform_device *pdev)
454 struct net_device *dev = platform_get_drvdata(pdev);
455 struct gfar_private *priv = netdev_priv(dev);
458 int magic_packet = priv->wol_en &&
459 (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
461 if (!netif_running(dev)) {
462 netif_device_attach(dev);
466 if (!magic_packet && priv->phydev)
467 phy_start(priv->phydev);
469 /* Disable Magic Packet mode, in case something
473 spin_lock_irqsave(&priv->txlock, flags);
474 spin_lock(&priv->rxlock);
476 tempval = gfar_read(&priv->regs->maccfg2);
477 tempval &= ~MACCFG2_MPEN;
478 gfar_write(&priv->regs->maccfg2, tempval);
482 spin_unlock(&priv->rxlock);
483 spin_unlock_irqrestore(&priv->txlock, flags);
485 netif_device_attach(dev);
487 #ifdef CONFIG_GFAR_NAPI
488 napi_enable(&priv->napi);
494 #define gfar_suspend NULL
495 #define gfar_resume NULL
498 /* Reads the controller's registers to determine what interface
499 * connects it to the PHY.
501 static phy_interface_t gfar_get_interface(struct net_device *dev)
503 struct gfar_private *priv = netdev_priv(dev);
504 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
506 if (ecntrl & ECNTRL_SGMII_MODE)
507 return PHY_INTERFACE_MODE_SGMII;
509 if (ecntrl & ECNTRL_TBI_MODE) {
510 if (ecntrl & ECNTRL_REDUCED_MODE)
511 return PHY_INTERFACE_MODE_RTBI;
513 return PHY_INTERFACE_MODE_TBI;
516 if (ecntrl & ECNTRL_REDUCED_MODE) {
517 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
518 return PHY_INTERFACE_MODE_RMII;
520 phy_interface_t interface = priv->einfo->interface;
523 * This isn't autodetected right now, so it must
524 * be set by the device tree or platform code.
526 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
527 return PHY_INTERFACE_MODE_RGMII_ID;
529 return PHY_INTERFACE_MODE_RGMII;
533 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
534 return PHY_INTERFACE_MODE_GMII;
536 return PHY_INTERFACE_MODE_MII;
540 /* Initializes driver's PHY state, and attaches to the PHY.
541 * Returns 0 on success.
543 static int init_phy(struct net_device *dev)
545 struct gfar_private *priv = netdev_priv(dev);
546 uint gigabit_support =
547 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
548 SUPPORTED_1000baseT_Full : 0;
549 struct phy_device *phydev;
550 char phy_id[BUS_ID_SIZE];
551 phy_interface_t interface;
555 priv->oldduplex = -1;
557 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
559 interface = gfar_get_interface(dev);
561 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
563 if (interface == PHY_INTERFACE_MODE_SGMII)
564 gfar_configure_serdes(dev);
566 if (IS_ERR(phydev)) {
567 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
568 return PTR_ERR(phydev);
571 /* Remove any features not supported by the controller */
572 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
573 phydev->advertising = phydev->supported;
575 priv->phydev = phydev;
581 * Initialize TBI PHY interface for communicating with the
582 * SERDES lynx PHY on the chip. We communicate with this PHY
583 * through the MDIO bus on each controller, treating it as a
584 * "normal" PHY at the address found in the TBIPA register. We assume
585 * that the TBIPA register is valid. Either the MDIO bus code will set
586 * it to a value that doesn't conflict with other PHYs on the bus, or the
587 * value doesn't matter, as there are no other PHYs on the bus.
589 static void gfar_configure_serdes(struct net_device *dev)
591 struct gfar_private *priv = netdev_priv(dev);
592 struct gfar_mii __iomem *regs =
593 (void __iomem *)&priv->regs->gfar_mii_regs;
594 int tbipa = gfar_read(&priv->regs->tbipa);
596 /* Single clk mode, mii mode off(for serdes communication) */
597 gfar_local_mdio_write(regs, tbipa, MII_TBICON, TBICON_CLK_SELECT);
599 gfar_local_mdio_write(regs, tbipa, MII_ADVERTISE,
600 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
601 ADVERTISE_1000XPSE_ASYM);
603 gfar_local_mdio_write(regs, tbipa, MII_BMCR, BMCR_ANENABLE |
604 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
607 static void init_registers(struct net_device *dev)
609 struct gfar_private *priv = netdev_priv(dev);
612 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
614 /* Initialize IMASK */
615 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
617 /* Init hash registers to zero */
618 gfar_write(&priv->regs->igaddr0, 0);
619 gfar_write(&priv->regs->igaddr1, 0);
620 gfar_write(&priv->regs->igaddr2, 0);
621 gfar_write(&priv->regs->igaddr3, 0);
622 gfar_write(&priv->regs->igaddr4, 0);
623 gfar_write(&priv->regs->igaddr5, 0);
624 gfar_write(&priv->regs->igaddr6, 0);
625 gfar_write(&priv->regs->igaddr7, 0);
627 gfar_write(&priv->regs->gaddr0, 0);
628 gfar_write(&priv->regs->gaddr1, 0);
629 gfar_write(&priv->regs->gaddr2, 0);
630 gfar_write(&priv->regs->gaddr3, 0);
631 gfar_write(&priv->regs->gaddr4, 0);
632 gfar_write(&priv->regs->gaddr5, 0);
633 gfar_write(&priv->regs->gaddr6, 0);
634 gfar_write(&priv->regs->gaddr7, 0);
636 /* Zero out the rmon mib registers if it has them */
637 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
638 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
640 /* Mask off the CAM interrupts */
641 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
642 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
645 /* Initialize the max receive buffer length */
646 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
648 /* Initialize the Minimum Frame Length Register */
649 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
654 /* Halt the receive and transmit queues */
655 static void gfar_halt_nodisable(struct net_device *dev)
657 struct gfar_private *priv = netdev_priv(dev);
658 struct gfar __iomem *regs = priv->regs;
661 /* Mask all interrupts */
662 gfar_write(®s->imask, IMASK_INIT_CLEAR);
664 /* Clear all interrupts */
665 gfar_write(®s->ievent, IEVENT_INIT_CLEAR);
667 /* Stop the DMA, and wait for it to stop */
668 tempval = gfar_read(&priv->regs->dmactrl);
669 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
670 != (DMACTRL_GRS | DMACTRL_GTS)) {
671 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
672 gfar_write(&priv->regs->dmactrl, tempval);
674 while (!(gfar_read(&priv->regs->ievent) &
675 (IEVENT_GRSC | IEVENT_GTSC)))
681 /* Halt the receive and transmit queues */
682 void gfar_halt(struct net_device *dev)
684 struct gfar_private *priv = netdev_priv(dev);
685 struct gfar __iomem *regs = priv->regs;
688 /* Disable Rx and Tx */
689 tempval = gfar_read(®s->maccfg1);
690 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
691 gfar_write(®s->maccfg1, tempval);
694 void stop_gfar(struct net_device *dev)
696 struct gfar_private *priv = netdev_priv(dev);
697 struct gfar __iomem *regs = priv->regs;
700 phy_stop(priv->phydev);
703 spin_lock_irqsave(&priv->txlock, flags);
704 spin_lock(&priv->rxlock);
708 spin_unlock(&priv->rxlock);
709 spin_unlock_irqrestore(&priv->txlock, flags);
712 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
713 free_irq(priv->interruptError, dev);
714 free_irq(priv->interruptTransmit, dev);
715 free_irq(priv->interruptReceive, dev);
717 free_irq(priv->interruptTransmit, dev);
720 free_skb_resources(priv);
722 dma_free_coherent(&dev->dev,
723 sizeof(struct txbd8)*priv->tx_ring_size
724 + sizeof(struct rxbd8)*priv->rx_ring_size,
726 gfar_read(®s->tbase0));
729 /* If there are any tx skbs or rx skbs still around, free them.
730 * Then free tx_skbuff and rx_skbuff */
731 static void free_skb_resources(struct gfar_private *priv)
737 /* Go through all the buffer descriptors and free their data buffers */
738 txbdp = priv->tx_bd_base;
740 for (i = 0; i < priv->tx_ring_size; i++) {
742 if (priv->tx_skbuff[i]) {
743 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
746 dev_kfree_skb_any(priv->tx_skbuff[i]);
747 priv->tx_skbuff[i] = NULL;
753 kfree(priv->tx_skbuff);
755 rxbdp = priv->rx_bd_base;
757 /* rx_skbuff is not guaranteed to be allocated, so only
758 * free it and its contents if it is allocated */
759 if(priv->rx_skbuff != NULL) {
760 for (i = 0; i < priv->rx_ring_size; i++) {
761 if (priv->rx_skbuff[i]) {
762 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
763 priv->rx_buffer_size,
766 dev_kfree_skb_any(priv->rx_skbuff[i]);
767 priv->rx_skbuff[i] = NULL;
777 kfree(priv->rx_skbuff);
781 void gfar_start(struct net_device *dev)
783 struct gfar_private *priv = netdev_priv(dev);
784 struct gfar __iomem *regs = priv->regs;
787 /* Enable Rx and Tx in MACCFG1 */
788 tempval = gfar_read(®s->maccfg1);
789 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
790 gfar_write(®s->maccfg1, tempval);
792 /* Initialize DMACTRL to have WWR and WOP */
793 tempval = gfar_read(&priv->regs->dmactrl);
794 tempval |= DMACTRL_INIT_SETTINGS;
795 gfar_write(&priv->regs->dmactrl, tempval);
797 /* Make sure we aren't stopped */
798 tempval = gfar_read(&priv->regs->dmactrl);
799 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
800 gfar_write(&priv->regs->dmactrl, tempval);
802 /* Clear THLT/RHLT, so that the DMA starts polling now */
803 gfar_write(®s->tstat, TSTAT_CLEAR_THALT);
804 gfar_write(®s->rstat, RSTAT_CLEAR_RHALT);
806 /* Unmask the interrupts we look for */
807 gfar_write(®s->imask, IMASK_DEFAULT);
810 /* Bring the controller up and running */
811 int startup_gfar(struct net_device *dev)
818 struct gfar_private *priv = netdev_priv(dev);
819 struct gfar __iomem *regs = priv->regs;
824 gfar_write(®s->imask, IMASK_INIT_CLEAR);
826 /* Allocate memory for the buffer descriptors */
827 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
828 sizeof (struct txbd8) * priv->tx_ring_size +
829 sizeof (struct rxbd8) * priv->rx_ring_size,
833 if (netif_msg_ifup(priv))
834 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
839 priv->tx_bd_base = (struct txbd8 *) vaddr;
841 /* enet DMA only understands physical addresses */
842 gfar_write(®s->tbase0, addr);
844 /* Start the rx descriptor ring where the tx ring leaves off */
845 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
846 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
847 priv->rx_bd_base = (struct rxbd8 *) vaddr;
848 gfar_write(®s->rbase0, addr);
850 /* Setup the skbuff rings */
852 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
853 priv->tx_ring_size, GFP_KERNEL);
855 if (NULL == priv->tx_skbuff) {
856 if (netif_msg_ifup(priv))
857 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
863 for (i = 0; i < priv->tx_ring_size; i++)
864 priv->tx_skbuff[i] = NULL;
867 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
868 priv->rx_ring_size, GFP_KERNEL);
870 if (NULL == priv->rx_skbuff) {
871 if (netif_msg_ifup(priv))
872 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
878 for (i = 0; i < priv->rx_ring_size; i++)
879 priv->rx_skbuff[i] = NULL;
881 /* Initialize some variables in our dev structure */
882 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
883 priv->cur_rx = priv->rx_bd_base;
884 priv->skb_curtx = priv->skb_dirtytx = 0;
887 /* Initialize Transmit Descriptor Ring */
888 txbdp = priv->tx_bd_base;
889 for (i = 0; i < priv->tx_ring_size; i++) {
896 /* Set the last descriptor in the ring to indicate wrap */
898 txbdp->status |= TXBD_WRAP;
900 rxbdp = priv->rx_bd_base;
901 for (i = 0; i < priv->rx_ring_size; i++) {
904 skb = gfar_new_skb(dev);
907 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
910 goto err_rxalloc_fail;
913 priv->rx_skbuff[i] = skb;
915 gfar_new_rxbdp(dev, rxbdp, skb);
920 /* Set the last descriptor in the ring to wrap */
922 rxbdp->status |= RXBD_WRAP;
924 /* If the device has multiple interrupts, register for
925 * them. Otherwise, only register for the one */
926 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
927 /* Install our interrupt handlers for Error,
928 * Transmit, and Receive */
929 if (request_irq(priv->interruptError, gfar_error,
930 0, "enet_error", dev) < 0) {
931 if (netif_msg_intr(priv))
932 printk(KERN_ERR "%s: Can't get IRQ %d\n",
933 dev->name, priv->interruptError);
939 if (request_irq(priv->interruptTransmit, gfar_transmit,
940 0, "enet_tx", dev) < 0) {
941 if (netif_msg_intr(priv))
942 printk(KERN_ERR "%s: Can't get IRQ %d\n",
943 dev->name, priv->interruptTransmit);
950 if (request_irq(priv->interruptReceive, gfar_receive,
951 0, "enet_rx", dev) < 0) {
952 if (netif_msg_intr(priv))
953 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
954 dev->name, priv->interruptReceive);
960 if (request_irq(priv->interruptTransmit, gfar_interrupt,
961 0, "gfar_interrupt", dev) < 0) {
962 if (netif_msg_intr(priv))
963 printk(KERN_ERR "%s: Can't get IRQ %d\n",
964 dev->name, priv->interruptError);
971 phy_start(priv->phydev);
973 /* Configure the coalescing support */
974 if (priv->txcoalescing)
975 gfar_write(®s->txic,
976 mk_ic_value(priv->txcount, priv->txtime));
978 gfar_write(®s->txic, 0);
980 if (priv->rxcoalescing)
981 gfar_write(®s->rxic,
982 mk_ic_value(priv->rxcount, priv->rxtime));
984 gfar_write(®s->rxic, 0);
986 if (priv->rx_csum_enable)
987 rctrl |= RCTRL_CHECKSUMMING;
989 if (priv->extended_hash) {
990 rctrl |= RCTRL_EXTHASH;
992 gfar_clear_exact_match(dev);
996 if (priv->vlan_enable)
1000 rctrl &= ~RCTRL_PAL_MASK;
1001 rctrl |= RCTRL_PADDING(priv->padding);
1004 /* Init rctrl based on our settings */
1005 gfar_write(&priv->regs->rctrl, rctrl);
1007 if (dev->features & NETIF_F_IP_CSUM)
1008 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
1010 /* Set the extraction length and index */
1011 attrs = ATTRELI_EL(priv->rx_stash_size) |
1012 ATTRELI_EI(priv->rx_stash_index);
1014 gfar_write(&priv->regs->attreli, attrs);
1016 /* Start with defaults, and add stashing or locking
1017 * depending on the approprate variables */
1018 attrs = ATTR_INIT_SETTINGS;
1020 if (priv->bd_stash_en)
1021 attrs |= ATTR_BDSTASH;
1023 if (priv->rx_stash_size != 0)
1024 attrs |= ATTR_BUFSTASH;
1026 gfar_write(&priv->regs->attr, attrs);
1028 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
1029 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
1030 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
1032 /* Start the controller */
1038 free_irq(priv->interruptTransmit, dev);
1040 free_irq(priv->interruptError, dev);
1044 free_skb_resources(priv);
1046 dma_free_coherent(&dev->dev,
1047 sizeof(struct txbd8)*priv->tx_ring_size
1048 + sizeof(struct rxbd8)*priv->rx_ring_size,
1050 gfar_read(®s->tbase0));
1055 /* Called when something needs to use the ethernet device */
1056 /* Returns 0 for success. */
1057 static int gfar_enet_open(struct net_device *dev)
1059 #ifdef CONFIG_GFAR_NAPI
1060 struct gfar_private *priv = netdev_priv(dev);
1064 #ifdef CONFIG_GFAR_NAPI
1065 napi_enable(&priv->napi);
1068 /* Initialize a bunch of registers */
1069 init_registers(dev);
1071 gfar_set_mac_address(dev);
1073 err = init_phy(dev);
1076 #ifdef CONFIG_GFAR_NAPI
1077 napi_disable(&priv->napi);
1082 err = startup_gfar(dev);
1084 #ifdef CONFIG_GFAR_NAPI
1085 napi_disable(&priv->napi);
1090 netif_start_queue(dev);
1095 static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
1097 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
1099 memset(fcb, 0, GMAC_FCB_LEN);
1104 static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
1108 /* If we're here, it's a IP packet with a TCP or UDP
1109 * payload. We set it to checksum, using a pseudo-header
1112 flags = TXFCB_DEFAULT;
1114 /* Tell the controller what the protocol is */
1115 /* And provide the already calculated phcs */
1116 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
1118 fcb->phcs = udp_hdr(skb)->check;
1120 fcb->phcs = tcp_hdr(skb)->check;
1122 /* l3os is the distance between the start of the
1123 * frame (skb->data) and the start of the IP hdr.
1124 * l4os is the distance between the start of the
1125 * l3 hdr and the l4 hdr */
1126 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
1127 fcb->l4os = skb_network_header_len(skb);
1132 void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
1134 fcb->flags |= TXFCB_VLN;
1135 fcb->vlctl = vlan_tx_tag_get(skb);
1138 /* This is called by the kernel when a frame is ready for transmission. */
1139 /* It is pointed to by the dev->hard_start_xmit function pointer */
1140 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1142 struct gfar_private *priv = netdev_priv(dev);
1143 struct txfcb *fcb = NULL;
1144 struct txbd8 *txbdp;
1146 unsigned long flags;
1148 /* Update transmit stats */
1149 dev->stats.tx_bytes += skb->len;
1152 spin_lock_irqsave(&priv->txlock, flags);
1154 /* Point at the first free tx descriptor */
1155 txbdp = priv->cur_tx;
1157 /* Clear all but the WRAP status flags */
1158 status = txbdp->status & TXBD_WRAP;
1160 /* Set up checksumming */
1161 if (likely((dev->features & NETIF_F_IP_CSUM)
1162 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
1163 fcb = gfar_add_fcb(skb, txbdp);
1165 gfar_tx_checksum(skb, fcb);
1168 if (priv->vlan_enable &&
1169 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
1170 if (unlikely(NULL == fcb)) {
1171 fcb = gfar_add_fcb(skb, txbdp);
1175 gfar_tx_vlan(skb, fcb);
1178 /* Set buffer length and pointer */
1179 txbdp->length = skb->len;
1180 txbdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1181 skb->len, DMA_TO_DEVICE);
1183 /* Save the skb pointer so we can free it later */
1184 priv->tx_skbuff[priv->skb_curtx] = skb;
1186 /* Update the current skb pointer (wrapping if this was the last) */
1188 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1190 /* Flag the BD as interrupt-causing */
1191 status |= TXBD_INTERRUPT;
1193 /* Flag the BD as ready to go, last in frame, and */
1194 /* in need of CRC */
1195 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
1197 dev->trans_start = jiffies;
1199 /* The powerpc-specific eieio() is used, as wmb() has too strong
1200 * semantics (it requires synchronization between cacheable and
1201 * uncacheable mappings, which eieio doesn't provide and which we
1202 * don't need), thus requiring a more expensive sync instruction. At
1203 * some point, the set of architecture-independent barrier functions
1204 * should be expanded to include weaker barriers.
1208 txbdp->status = status;
1210 /* If this was the last BD in the ring, the next one */
1211 /* is at the beginning of the ring */
1212 if (txbdp->status & TXBD_WRAP)
1213 txbdp = priv->tx_bd_base;
1217 /* If the next BD still needs to be cleaned up, then the bds
1218 are full. We need to tell the kernel to stop sending us stuff. */
1219 if (txbdp == priv->dirty_tx) {
1220 netif_stop_queue(dev);
1222 dev->stats.tx_fifo_errors++;
1225 /* Update the current txbd to the next one */
1226 priv->cur_tx = txbdp;
1228 /* Tell the DMA to go go go */
1229 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1232 spin_unlock_irqrestore(&priv->txlock, flags);
1237 /* Stops the kernel queue, and halts the controller */
1238 static int gfar_close(struct net_device *dev)
1240 struct gfar_private *priv = netdev_priv(dev);
1242 #ifdef CONFIG_GFAR_NAPI
1243 napi_disable(&priv->napi);
1248 /* Disconnect from the PHY */
1249 phy_disconnect(priv->phydev);
1250 priv->phydev = NULL;
1252 netif_stop_queue(dev);
1257 /* Changes the mac address if the controller is not running. */
1258 static int gfar_set_mac_address(struct net_device *dev)
1260 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
1266 /* Enables and disables VLAN insertion/extraction */
1267 static void gfar_vlan_rx_register(struct net_device *dev,
1268 struct vlan_group *grp)
1270 struct gfar_private *priv = netdev_priv(dev);
1271 unsigned long flags;
1274 spin_lock_irqsave(&priv->rxlock, flags);
1279 /* Enable VLAN tag insertion */
1280 tempval = gfar_read(&priv->regs->tctrl);
1281 tempval |= TCTRL_VLINS;
1283 gfar_write(&priv->regs->tctrl, tempval);
1285 /* Enable VLAN tag extraction */
1286 tempval = gfar_read(&priv->regs->rctrl);
1287 tempval |= RCTRL_VLEX;
1288 gfar_write(&priv->regs->rctrl, tempval);
1290 /* Disable VLAN tag insertion */
1291 tempval = gfar_read(&priv->regs->tctrl);
1292 tempval &= ~TCTRL_VLINS;
1293 gfar_write(&priv->regs->tctrl, tempval);
1295 /* Disable VLAN tag extraction */
1296 tempval = gfar_read(&priv->regs->rctrl);
1297 tempval &= ~RCTRL_VLEX;
1298 gfar_write(&priv->regs->rctrl, tempval);
1301 spin_unlock_irqrestore(&priv->rxlock, flags);
1304 static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1306 int tempsize, tempval;
1307 struct gfar_private *priv = netdev_priv(dev);
1308 int oldsize = priv->rx_buffer_size;
1309 int frame_size = new_mtu + ETH_HLEN;
1311 if (priv->vlan_enable)
1312 frame_size += VLAN_HLEN;
1314 if (gfar_uses_fcb(priv))
1315 frame_size += GMAC_FCB_LEN;
1317 frame_size += priv->padding;
1319 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
1320 if (netif_msg_drv(priv))
1321 printk(KERN_ERR "%s: Invalid MTU setting\n",
1327 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1328 INCREMENTAL_BUFFER_SIZE;
1330 /* Only stop and start the controller if it isn't already
1331 * stopped, and we changed something */
1332 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1335 priv->rx_buffer_size = tempsize;
1339 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1340 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1342 /* If the mtu is larger than the max size for standard
1343 * ethernet frames (ie, a jumbo frame), then set maccfg2
1344 * to allow huge frames, and to check the length */
1345 tempval = gfar_read(&priv->regs->maccfg2);
1347 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1348 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1350 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1352 gfar_write(&priv->regs->maccfg2, tempval);
1354 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1360 /* gfar_timeout gets called when a packet has not been
1361 * transmitted after a set amount of time.
1362 * For now, assume that clearing out all the structures, and
1363 * starting over will fix the problem. */
1364 static void gfar_timeout(struct net_device *dev)
1366 dev->stats.tx_errors++;
1368 if (dev->flags & IFF_UP) {
1373 netif_schedule(dev);
1376 /* Interrupt Handler for Transmit complete */
1377 static int gfar_clean_tx_ring(struct net_device *dev)
1380 struct gfar_private *priv = netdev_priv(dev);
1383 bdp = priv->dirty_tx;
1384 while ((bdp->status & TXBD_READY) == 0) {
1385 /* If dirty_tx and cur_tx are the same, then either the */
1386 /* ring is empty or full now (it could only be full in the beginning, */
1387 /* obviously). If it is empty, we are done. */
1388 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1393 /* Deferred means some collisions occurred during transmit, */
1394 /* but we eventually sent the packet. */
1395 if (bdp->status & TXBD_DEF)
1396 dev->stats.collisions++;
1398 /* Free the sk buffer associated with this TxBD */
1399 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1401 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1403 (priv->skb_dirtytx +
1404 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1406 /* Clean BD length for empty detection */
1409 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1410 if (bdp->status & TXBD_WRAP)
1411 bdp = priv->tx_bd_base;
1415 /* Move dirty_tx to be the next bd */
1416 priv->dirty_tx = bdp;
1418 /* We freed a buffer, so now we can restart transmission */
1419 if (netif_queue_stopped(dev))
1420 netif_wake_queue(dev);
1421 } /* while ((bdp->status & TXBD_READY) == 0) */
1423 dev->stats.tx_packets += howmany;
1428 /* Interrupt Handler for Transmit complete */
1429 static irqreturn_t gfar_transmit(int irq, void *dev_id)
1431 struct net_device *dev = (struct net_device *) dev_id;
1432 struct gfar_private *priv = netdev_priv(dev);
1435 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1438 spin_lock(&priv->txlock);
1440 gfar_clean_tx_ring(dev);
1442 /* If we are coalescing the interrupts, reset the timer */
1443 /* Otherwise, clear it */
1444 if (likely(priv->txcoalescing)) {
1445 gfar_write(&priv->regs->txic, 0);
1446 gfar_write(&priv->regs->txic,
1447 mk_ic_value(priv->txcount, priv->txtime));
1450 spin_unlock(&priv->txlock);
1455 static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1456 struct sk_buff *skb)
1458 struct gfar_private *priv = netdev_priv(dev);
1459 u32 * status_len = (u32 *)bdp;
1462 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1463 priv->rx_buffer_size, DMA_FROM_DEVICE);
1465 flags = RXBD_EMPTY | RXBD_INTERRUPT;
1467 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
1472 *status_len = (u32)flags << 16;
1476 struct sk_buff * gfar_new_skb(struct net_device *dev)
1478 unsigned int alignamount;
1479 struct gfar_private *priv = netdev_priv(dev);
1480 struct sk_buff *skb = NULL;
1482 /* We have to allocate the skb, so keep trying till we succeed */
1483 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
1488 alignamount = RXBUF_ALIGNMENT -
1489 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
1491 /* We need the data buffer to be aligned properly. We will reserve
1492 * as many bytes as needed to align the data properly
1494 skb_reserve(skb, alignamount);
1499 static inline void count_errors(unsigned short status, struct net_device *dev)
1501 struct gfar_private *priv = netdev_priv(dev);
1502 struct net_device_stats *stats = &dev->stats;
1503 struct gfar_extra_stats *estats = &priv->extra_stats;
1505 /* If the packet was truncated, none of the other errors
1507 if (status & RXBD_TRUNCATED) {
1508 stats->rx_length_errors++;
1514 /* Count the errors, if there were any */
1515 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1516 stats->rx_length_errors++;
1518 if (status & RXBD_LARGE)
1523 if (status & RXBD_NONOCTET) {
1524 stats->rx_frame_errors++;
1525 estats->rx_nonoctet++;
1527 if (status & RXBD_CRCERR) {
1528 estats->rx_crcerr++;
1529 stats->rx_crc_errors++;
1531 if (status & RXBD_OVERRUN) {
1532 estats->rx_overrun++;
1533 stats->rx_crc_errors++;
1537 irqreturn_t gfar_receive(int irq, void *dev_id)
1539 struct net_device *dev = (struct net_device *) dev_id;
1540 struct gfar_private *priv = netdev_priv(dev);
1541 #ifdef CONFIG_GFAR_NAPI
1544 unsigned long flags;
1548 #ifdef CONFIG_GFAR_NAPI
1549 /* Clear IEVENT, so interrupts aren't called again
1550 * because of the packets that have already arrived */
1551 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1553 if (netif_rx_schedule_prep(dev, &priv->napi)) {
1554 tempval = gfar_read(&priv->regs->imask);
1555 tempval &= IMASK_RTX_DISABLED;
1556 gfar_write(&priv->regs->imask, tempval);
1558 __netif_rx_schedule(dev, &priv->napi);
1560 if (netif_msg_rx_err(priv))
1561 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1562 dev->name, gfar_read(&priv->regs->ievent),
1563 gfar_read(&priv->regs->imask));
1566 /* Clear IEVENT, so rx interrupt isn't called again
1567 * because of this interrupt */
1568 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1570 spin_lock_irqsave(&priv->rxlock, flags);
1571 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1573 /* If we are coalescing interrupts, update the timer */
1574 /* Otherwise, clear it */
1575 if (likely(priv->rxcoalescing)) {
1576 gfar_write(&priv->regs->rxic, 0);
1577 gfar_write(&priv->regs->rxic,
1578 mk_ic_value(priv->rxcount, priv->rxtime));
1581 spin_unlock_irqrestore(&priv->rxlock, flags);
1587 static inline int gfar_rx_vlan(struct sk_buff *skb,
1588 struct vlan_group *vlgrp, unsigned short vlctl)
1590 #ifdef CONFIG_GFAR_NAPI
1591 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1593 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1597 static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1599 /* If valid headers were found, and valid sums
1600 * were verified, then we tell the kernel that no
1601 * checksumming is necessary. Otherwise, it is */
1602 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
1603 skb->ip_summed = CHECKSUM_UNNECESSARY;
1605 skb->ip_summed = CHECKSUM_NONE;
1609 static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1611 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1613 /* Remove the FCB from the skb */
1614 skb_pull(skb, GMAC_FCB_LEN);
1619 /* gfar_process_frame() -- handle one incoming packet if skb
1621 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1624 struct gfar_private *priv = netdev_priv(dev);
1625 struct rxfcb *fcb = NULL;
1628 if (netif_msg_rx_err(priv))
1629 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
1630 dev->stats.rx_dropped++;
1631 priv->extra_stats.rx_skbmissing++;
1635 /* Prep the skb for the packet */
1636 skb_put(skb, length);
1638 /* Grab the FCB if there is one */
1639 if (gfar_uses_fcb(priv))
1640 fcb = gfar_get_fcb(skb);
1642 /* Remove the padded bytes, if there are any */
1644 skb_pull(skb, priv->padding);
1646 if (priv->rx_csum_enable)
1647 gfar_rx_checksum(skb, fcb);
1649 /* Tell the skb what kind of packet this is */
1650 skb->protocol = eth_type_trans(skb, dev);
1652 /* Send the packet up the stack */
1653 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1654 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1658 if (NET_RX_DROP == ret)
1659 priv->extra_stats.kernel_dropped++;
1665 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
1666 * until the budget/quota has been reached. Returns the number
1669 int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
1672 struct sk_buff *skb;
1675 struct gfar_private *priv = netdev_priv(dev);
1677 /* Get the first full descriptor */
1680 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
1681 struct sk_buff *newskb;
1684 /* Add another skb for the future */
1685 newskb = gfar_new_skb(dev);
1687 skb = priv->rx_skbuff[priv->skb_currx];
1689 /* We drop the frame if we failed to allocate a new buffer */
1690 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1691 bdp->status & RXBD_ERR)) {
1692 count_errors(bdp->status, dev);
1694 if (unlikely(!newskb))
1698 dma_unmap_single(&priv->dev->dev,
1700 priv->rx_buffer_size,
1703 dev_kfree_skb_any(skb);
1706 /* Increment the number of packets */
1707 dev->stats.rx_packets++;
1710 /* Remove the FCS from the packet length */
1711 pkt_len = bdp->length - 4;
1713 gfar_process_frame(dev, skb, pkt_len);
1715 dev->stats.rx_bytes += pkt_len;
1718 dev->last_rx = jiffies;
1720 priv->rx_skbuff[priv->skb_currx] = newskb;
1722 /* Setup the new bdp */
1723 gfar_new_rxbdp(dev, bdp, newskb);
1725 /* Update to the next pointer */
1726 if (bdp->status & RXBD_WRAP)
1727 bdp = priv->rx_bd_base;
1731 /* update to point at the next skb */
1733 (priv->skb_currx + 1) &
1734 RX_RING_MOD_MASK(priv->rx_ring_size);
1737 /* Update the current rxbd pointer to be the next one */
1743 #ifdef CONFIG_GFAR_NAPI
1744 static int gfar_poll(struct napi_struct *napi, int budget)
1746 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1747 struct net_device *dev = priv->dev;
1749 unsigned long flags;
1751 /* If we fail to get the lock, don't bother with the TX BDs */
1752 if (spin_trylock_irqsave(&priv->txlock, flags)) {
1753 gfar_clean_tx_ring(dev);
1754 spin_unlock_irqrestore(&priv->txlock, flags);
1757 howmany = gfar_clean_rx_ring(dev, budget);
1759 if (howmany < budget) {
1760 netif_rx_complete(dev, napi);
1762 /* Clear the halt bit in RSTAT */
1763 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1765 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1767 /* If we are coalescing interrupts, update the timer */
1768 /* Otherwise, clear it */
1769 if (likely(priv->rxcoalescing)) {
1770 gfar_write(&priv->regs->rxic, 0);
1771 gfar_write(&priv->regs->rxic,
1772 mk_ic_value(priv->rxcount, priv->rxtime));
1780 #ifdef CONFIG_NET_POLL_CONTROLLER
1782 * Polling 'interrupt' - used by things like netconsole to send skbs
1783 * without having to re-enable interrupts. It's not called while
1784 * the interrupt routine is executing.
1786 static void gfar_netpoll(struct net_device *dev)
1788 struct gfar_private *priv = netdev_priv(dev);
1790 /* If the device has multiple interrupts, run tx/rx */
1791 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1792 disable_irq(priv->interruptTransmit);
1793 disable_irq(priv->interruptReceive);
1794 disable_irq(priv->interruptError);
1795 gfar_interrupt(priv->interruptTransmit, dev);
1796 enable_irq(priv->interruptError);
1797 enable_irq(priv->interruptReceive);
1798 enable_irq(priv->interruptTransmit);
1800 disable_irq(priv->interruptTransmit);
1801 gfar_interrupt(priv->interruptTransmit, dev);
1802 enable_irq(priv->interruptTransmit);
1807 /* The interrupt handler for devices with one interrupt */
1808 static irqreturn_t gfar_interrupt(int irq, void *dev_id)
1810 struct net_device *dev = dev_id;
1811 struct gfar_private *priv = netdev_priv(dev);
1813 /* Save ievent for future reference */
1814 u32 events = gfar_read(&priv->regs->ievent);
1816 /* Check for reception */
1817 if (events & IEVENT_RX_MASK)
1818 gfar_receive(irq, dev_id);
1820 /* Check for transmit completion */
1821 if (events & IEVENT_TX_MASK)
1822 gfar_transmit(irq, dev_id);
1824 /* Check for errors */
1825 if (events & IEVENT_ERR_MASK)
1826 gfar_error(irq, dev_id);
1831 /* Called every time the controller might need to be made
1832 * aware of new link state. The PHY code conveys this
1833 * information through variables in the phydev structure, and this
1834 * function converts those variables into the appropriate
1835 * register values, and can bring down the device if needed.
1837 static void adjust_link(struct net_device *dev)
1839 struct gfar_private *priv = netdev_priv(dev);
1840 struct gfar __iomem *regs = priv->regs;
1841 unsigned long flags;
1842 struct phy_device *phydev = priv->phydev;
1845 spin_lock_irqsave(&priv->txlock, flags);
1847 u32 tempval = gfar_read(®s->maccfg2);
1848 u32 ecntrl = gfar_read(®s->ecntrl);
1850 /* Now we make sure that we can be in full duplex mode.
1851 * If not, we operate in half-duplex mode. */
1852 if (phydev->duplex != priv->oldduplex) {
1854 if (!(phydev->duplex))
1855 tempval &= ~(MACCFG2_FULL_DUPLEX);
1857 tempval |= MACCFG2_FULL_DUPLEX;
1859 priv->oldduplex = phydev->duplex;
1862 if (phydev->speed != priv->oldspeed) {
1864 switch (phydev->speed) {
1867 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
1872 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
1874 /* Reduced mode distinguishes
1875 * between 10 and 100 */
1876 if (phydev->speed == SPEED_100)
1877 ecntrl |= ECNTRL_R100;
1879 ecntrl &= ~(ECNTRL_R100);
1882 if (netif_msg_link(priv))
1884 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1885 dev->name, phydev->speed);
1889 priv->oldspeed = phydev->speed;
1892 gfar_write(®s->maccfg2, tempval);
1893 gfar_write(®s->ecntrl, ecntrl);
1895 if (!priv->oldlink) {
1898 netif_schedule(dev);
1900 } else if (priv->oldlink) {
1904 priv->oldduplex = -1;
1907 if (new_state && netif_msg_link(priv))
1908 phy_print_status(phydev);
1910 spin_unlock_irqrestore(&priv->txlock, flags);
1913 /* Update the hash table based on the current list of multicast
1914 * addresses we subscribe to. Also, change the promiscuity of
1915 * the device based on the flags (this function is called
1916 * whenever dev->flags is changed */
1917 static void gfar_set_multi(struct net_device *dev)
1919 struct dev_mc_list *mc_ptr;
1920 struct gfar_private *priv = netdev_priv(dev);
1921 struct gfar __iomem *regs = priv->regs;
1924 if(dev->flags & IFF_PROMISC) {
1925 /* Set RCTRL to PROM */
1926 tempval = gfar_read(®s->rctrl);
1927 tempval |= RCTRL_PROM;
1928 gfar_write(®s->rctrl, tempval);
1930 /* Set RCTRL to not PROM */
1931 tempval = gfar_read(®s->rctrl);
1932 tempval &= ~(RCTRL_PROM);
1933 gfar_write(®s->rctrl, tempval);
1936 if(dev->flags & IFF_ALLMULTI) {
1937 /* Set the hash to rx all multicast frames */
1938 gfar_write(®s->igaddr0, 0xffffffff);
1939 gfar_write(®s->igaddr1, 0xffffffff);
1940 gfar_write(®s->igaddr2, 0xffffffff);
1941 gfar_write(®s->igaddr3, 0xffffffff);
1942 gfar_write(®s->igaddr4, 0xffffffff);
1943 gfar_write(®s->igaddr5, 0xffffffff);
1944 gfar_write(®s->igaddr6, 0xffffffff);
1945 gfar_write(®s->igaddr7, 0xffffffff);
1946 gfar_write(®s->gaddr0, 0xffffffff);
1947 gfar_write(®s->gaddr1, 0xffffffff);
1948 gfar_write(®s->gaddr2, 0xffffffff);
1949 gfar_write(®s->gaddr3, 0xffffffff);
1950 gfar_write(®s->gaddr4, 0xffffffff);
1951 gfar_write(®s->gaddr5, 0xffffffff);
1952 gfar_write(®s->gaddr6, 0xffffffff);
1953 gfar_write(®s->gaddr7, 0xffffffff);
1958 /* zero out the hash */
1959 gfar_write(®s->igaddr0, 0x0);
1960 gfar_write(®s->igaddr1, 0x0);
1961 gfar_write(®s->igaddr2, 0x0);
1962 gfar_write(®s->igaddr3, 0x0);
1963 gfar_write(®s->igaddr4, 0x0);
1964 gfar_write(®s->igaddr5, 0x0);
1965 gfar_write(®s->igaddr6, 0x0);
1966 gfar_write(®s->igaddr7, 0x0);
1967 gfar_write(®s->gaddr0, 0x0);
1968 gfar_write(®s->gaddr1, 0x0);
1969 gfar_write(®s->gaddr2, 0x0);
1970 gfar_write(®s->gaddr3, 0x0);
1971 gfar_write(®s->gaddr4, 0x0);
1972 gfar_write(®s->gaddr5, 0x0);
1973 gfar_write(®s->gaddr6, 0x0);
1974 gfar_write(®s->gaddr7, 0x0);
1976 /* If we have extended hash tables, we need to
1977 * clear the exact match registers to prepare for
1979 if (priv->extended_hash) {
1980 em_num = GFAR_EM_NUM + 1;
1981 gfar_clear_exact_match(dev);
1988 if(dev->mc_count == 0)
1991 /* Parse the list, and set the appropriate bits */
1992 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
1994 gfar_set_mac_for_addr(dev, idx,
1998 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
2006 /* Clears each of the exact match registers to zero, so they
2007 * don't interfere with normal reception */
2008 static void gfar_clear_exact_match(struct net_device *dev)
2011 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
2013 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
2014 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
2017 /* Set the appropriate hash bit for the given addr */
2018 /* The algorithm works like so:
2019 * 1) Take the Destination Address (ie the multicast address), and
2020 * do a CRC on it (little endian), and reverse the bits of the
2022 * 2) Use the 8 most significant bits as a hash into a 256-entry
2023 * table. The table is controlled through 8 32-bit registers:
2024 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
2025 * gaddr7. This means that the 3 most significant bits in the
2026 * hash index which gaddr register to use, and the 5 other bits
2027 * indicate which bit (assuming an IBM numbering scheme, which
2028 * for PowerPC (tm) is usually the case) in the register holds
2030 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
2033 struct gfar_private *priv = netdev_priv(dev);
2034 u32 result = ether_crc(MAC_ADDR_LEN, addr);
2035 int width = priv->hash_width;
2036 u8 whichbit = (result >> (32 - width)) & 0x1f;
2037 u8 whichreg = result >> (32 - width + 5);
2038 u32 value = (1 << (31-whichbit));
2040 tempval = gfar_read(priv->hash_regs[whichreg]);
2042 gfar_write(priv->hash_regs[whichreg], tempval);
2048 /* There are multiple MAC Address register pairs on some controllers
2049 * This function sets the numth pair to a given address
2051 static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
2053 struct gfar_private *priv = netdev_priv(dev);
2055 char tmpbuf[MAC_ADDR_LEN];
2057 u32 __iomem *macptr = &priv->regs->macstnaddr1;
2061 /* Now copy it into the mac registers backwards, cuz */
2062 /* little endian is silly */
2063 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
2064 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
2066 gfar_write(macptr, *((u32 *) (tmpbuf)));
2068 tempval = *((u32 *) (tmpbuf + 4));
2070 gfar_write(macptr+1, tempval);
2073 /* GFAR error interrupt handler */
2074 static irqreturn_t gfar_error(int irq, void *dev_id)
2076 struct net_device *dev = dev_id;
2077 struct gfar_private *priv = netdev_priv(dev);
2079 /* Save ievent for future reference */
2080 u32 events = gfar_read(&priv->regs->ievent);
2083 gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
2085 /* Magic Packet is not an error. */
2086 if ((priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
2087 (events & IEVENT_MAG))
2088 events &= ~IEVENT_MAG;
2091 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2092 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
2093 dev->name, events, gfar_read(&priv->regs->imask));
2095 /* Update the error counters */
2096 if (events & IEVENT_TXE) {
2097 dev->stats.tx_errors++;
2099 if (events & IEVENT_LC)
2100 dev->stats.tx_window_errors++;
2101 if (events & IEVENT_CRL)
2102 dev->stats.tx_aborted_errors++;
2103 if (events & IEVENT_XFUN) {
2104 if (netif_msg_tx_err(priv))
2105 printk(KERN_DEBUG "%s: TX FIFO underrun, "
2106 "packet dropped.\n", dev->name);
2107 dev->stats.tx_dropped++;
2108 priv->extra_stats.tx_underrun++;
2110 /* Reactivate the Tx Queues */
2111 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
2113 if (netif_msg_tx_err(priv))
2114 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
2116 if (events & IEVENT_BSY) {
2117 dev->stats.rx_errors++;
2118 priv->extra_stats.rx_bsy++;
2120 gfar_receive(irq, dev_id);
2122 #ifndef CONFIG_GFAR_NAPI
2123 /* Clear the halt bit in RSTAT */
2124 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
2127 if (netif_msg_rx_err(priv))
2128 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2129 dev->name, gfar_read(&priv->regs->rstat));
2131 if (events & IEVENT_BABR) {
2132 dev->stats.rx_errors++;
2133 priv->extra_stats.rx_babr++;
2135 if (netif_msg_rx_err(priv))
2136 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
2138 if (events & IEVENT_EBERR) {
2139 priv->extra_stats.eberr++;
2140 if (netif_msg_rx_err(priv))
2141 printk(KERN_DEBUG "%s: bus error\n", dev->name);
2143 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
2144 printk(KERN_DEBUG "%s: control frame\n", dev->name);
2146 if (events & IEVENT_BABT) {
2147 priv->extra_stats.tx_babt++;
2148 if (netif_msg_tx_err(priv))
2149 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
2154 /* work with hotplug and coldplug */
2155 MODULE_ALIAS("platform:fsl-gianfar");
2157 /* Structure for a device driver */
2158 static struct platform_driver gfar_driver = {
2159 .probe = gfar_probe,
2160 .remove = gfar_remove,
2161 .suspend = gfar_suspend,
2162 .resume = gfar_resume,
2164 .name = "fsl-gianfar",
2165 .owner = THIS_MODULE,
2169 static int __init gfar_init(void)
2171 int err = gfar_mdio_init();
2176 err = platform_driver_register(&gfar_driver);
2184 static void __exit gfar_exit(void)
2186 platform_driver_unregister(&gfar_driver);
2190 module_init(gfar_init);
2191 module_exit(gfar_exit);