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 extern int gfar_local_mdio_write(struct gfar_mii __iomem *regs, int mii_id, int regnum, u16 value);
135 extern int gfar_local_mdio_read(struct gfar_mii __iomem *regs, int mii_id, int regnum);
136 #ifdef CONFIG_GFAR_NAPI
137 static int gfar_poll(struct napi_struct *napi, int budget);
139 #ifdef CONFIG_NET_POLL_CONTROLLER
140 static void gfar_netpoll(struct net_device *dev);
142 int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
143 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
144 static void gfar_vlan_rx_register(struct net_device *netdev,
145 struct vlan_group *grp);
146 void gfar_halt(struct net_device *dev);
147 void gfar_start(struct net_device *dev);
148 static void gfar_clear_exact_match(struct net_device *dev);
149 static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
151 extern const struct ethtool_ops gfar_ethtool_ops;
153 MODULE_AUTHOR("Freescale Semiconductor, Inc");
154 MODULE_DESCRIPTION("Gianfar Ethernet Driver");
155 MODULE_LICENSE("GPL");
157 /* Returns 1 if incoming frames use an FCB */
158 static inline int gfar_uses_fcb(struct gfar_private *priv)
160 return (priv->vlan_enable || priv->rx_csum_enable);
163 /* Set up the ethernet device structure, private data,
164 * and anything else we need before we start */
165 static int gfar_probe(struct platform_device *pdev)
168 struct net_device *dev = NULL;
169 struct gfar_private *priv = NULL;
170 struct gianfar_platform_data *einfo;
173 DECLARE_MAC_BUF(mac);
175 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
178 printk(KERN_ERR "gfar %d: Missing additional data!\n",
184 /* Create an ethernet device instance */
185 dev = alloc_etherdev(sizeof (*priv));
190 priv = netdev_priv(dev);
193 /* Set the info in the priv to the current info */
196 /* fill out IRQ fields */
197 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
198 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
199 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
200 priv->interruptError = platform_get_irq_byname(pdev, "error");
201 if (priv->interruptTransmit < 0 || priv->interruptReceive < 0 || priv->interruptError < 0)
204 priv->interruptTransmit = platform_get_irq(pdev, 0);
205 if (priv->interruptTransmit < 0)
209 /* get a pointer to the register memory */
210 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
211 priv->regs = ioremap(r->start, sizeof (struct gfar));
213 if (NULL == priv->regs) {
218 spin_lock_init(&priv->txlock);
219 spin_lock_init(&priv->rxlock);
221 platform_set_drvdata(pdev, dev);
223 /* Stop the DMA engine now, in case it was running before */
224 /* (The firmware could have used it, and left it running). */
225 /* To do this, we write Graceful Receive Stop and Graceful */
226 /* Transmit Stop, and then wait until the corresponding bits */
227 /* in IEVENT indicate the stops have completed. */
228 tempval = gfar_read(&priv->regs->dmactrl);
229 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
230 gfar_write(&priv->regs->dmactrl, tempval);
232 tempval = gfar_read(&priv->regs->dmactrl);
233 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
234 gfar_write(&priv->regs->dmactrl, tempval);
236 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
239 /* Reset MAC layer */
240 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
242 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
243 gfar_write(&priv->regs->maccfg1, tempval);
245 /* Initialize MACCFG2. */
246 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
248 /* Initialize ECNTRL */
249 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
251 /* Copy the station address into the dev structure, */
252 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
254 /* Set the dev->base_addr to the gfar reg region */
255 dev->base_addr = (unsigned long) (priv->regs);
257 SET_NETDEV_DEV(dev, &pdev->dev);
259 /* Fill in the dev structure */
260 dev->open = gfar_enet_open;
261 dev->hard_start_xmit = gfar_start_xmit;
262 dev->tx_timeout = gfar_timeout;
263 dev->watchdog_timeo = TX_TIMEOUT;
264 #ifdef CONFIG_GFAR_NAPI
265 netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
267 #ifdef CONFIG_NET_POLL_CONTROLLER
268 dev->poll_controller = gfar_netpoll;
270 dev->stop = gfar_close;
271 dev->change_mtu = gfar_change_mtu;
273 dev->set_multicast_list = gfar_set_multi;
275 dev->ethtool_ops = &gfar_ethtool_ops;
277 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
278 priv->rx_csum_enable = 1;
279 dev->features |= NETIF_F_IP_CSUM;
281 priv->rx_csum_enable = 0;
285 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
286 dev->vlan_rx_register = gfar_vlan_rx_register;
288 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
290 priv->vlan_enable = 1;
293 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
294 priv->extended_hash = 1;
295 priv->hash_width = 9;
297 priv->hash_regs[0] = &priv->regs->igaddr0;
298 priv->hash_regs[1] = &priv->regs->igaddr1;
299 priv->hash_regs[2] = &priv->regs->igaddr2;
300 priv->hash_regs[3] = &priv->regs->igaddr3;
301 priv->hash_regs[4] = &priv->regs->igaddr4;
302 priv->hash_regs[5] = &priv->regs->igaddr5;
303 priv->hash_regs[6] = &priv->regs->igaddr6;
304 priv->hash_regs[7] = &priv->regs->igaddr7;
305 priv->hash_regs[8] = &priv->regs->gaddr0;
306 priv->hash_regs[9] = &priv->regs->gaddr1;
307 priv->hash_regs[10] = &priv->regs->gaddr2;
308 priv->hash_regs[11] = &priv->regs->gaddr3;
309 priv->hash_regs[12] = &priv->regs->gaddr4;
310 priv->hash_regs[13] = &priv->regs->gaddr5;
311 priv->hash_regs[14] = &priv->regs->gaddr6;
312 priv->hash_regs[15] = &priv->regs->gaddr7;
315 priv->extended_hash = 0;
316 priv->hash_width = 8;
318 priv->hash_regs[0] = &priv->regs->gaddr0;
319 priv->hash_regs[1] = &priv->regs->gaddr1;
320 priv->hash_regs[2] = &priv->regs->gaddr2;
321 priv->hash_regs[3] = &priv->regs->gaddr3;
322 priv->hash_regs[4] = &priv->regs->gaddr4;
323 priv->hash_regs[5] = &priv->regs->gaddr5;
324 priv->hash_regs[6] = &priv->regs->gaddr6;
325 priv->hash_regs[7] = &priv->regs->gaddr7;
328 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
329 priv->padding = DEFAULT_PADDING;
333 if (dev->features & NETIF_F_IP_CSUM)
334 dev->hard_header_len += GMAC_FCB_LEN;
336 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
337 priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
338 priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
340 priv->txcoalescing = DEFAULT_TX_COALESCE;
341 priv->txcount = DEFAULT_TXCOUNT;
342 priv->txtime = DEFAULT_TXTIME;
343 priv->rxcoalescing = DEFAULT_RX_COALESCE;
344 priv->rxcount = DEFAULT_RXCOUNT;
345 priv->rxtime = DEFAULT_RXTIME;
347 /* Enable most messages by default */
348 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
350 err = register_netdev(dev);
353 printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
358 /* Create all the sysfs files */
359 gfar_init_sysfs(dev);
361 /* Print out the device info */
362 printk(KERN_INFO DEVICE_NAME "%s\n",
363 dev->name, print_mac(mac, dev->dev_addr));
365 /* Even more device info helps when determining which kernel */
366 /* provided which set of benchmarks. */
367 #ifdef CONFIG_GFAR_NAPI
368 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
370 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
372 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
373 dev->name, priv->rx_ring_size, priv->tx_ring_size);
384 static int gfar_remove(struct platform_device *pdev)
386 struct net_device *dev = platform_get_drvdata(pdev);
387 struct gfar_private *priv = netdev_priv(dev);
389 platform_set_drvdata(pdev, NULL);
398 /* Reads the controller's registers to determine what interface
399 * connects it to the PHY.
401 static phy_interface_t gfar_get_interface(struct net_device *dev)
403 struct gfar_private *priv = netdev_priv(dev);
404 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
406 if (ecntrl & ECNTRL_SGMII_MODE)
407 return PHY_INTERFACE_MODE_SGMII;
409 if (ecntrl & ECNTRL_TBI_MODE) {
410 if (ecntrl & ECNTRL_REDUCED_MODE)
411 return PHY_INTERFACE_MODE_RTBI;
413 return PHY_INTERFACE_MODE_TBI;
416 if (ecntrl & ECNTRL_REDUCED_MODE) {
417 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
418 return PHY_INTERFACE_MODE_RMII;
420 phy_interface_t interface = priv->einfo->interface;
423 * This isn't autodetected right now, so it must
424 * be set by the device tree or platform code.
426 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
427 return PHY_INTERFACE_MODE_RGMII_ID;
429 return PHY_INTERFACE_MODE_RGMII;
433 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
434 return PHY_INTERFACE_MODE_GMII;
436 return PHY_INTERFACE_MODE_MII;
440 /* Initializes driver's PHY state, and attaches to the PHY.
441 * Returns 0 on success.
443 static int init_phy(struct net_device *dev)
445 struct gfar_private *priv = netdev_priv(dev);
446 uint gigabit_support =
447 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
448 SUPPORTED_1000baseT_Full : 0;
449 struct phy_device *phydev;
450 char phy_id[BUS_ID_SIZE];
451 phy_interface_t interface;
455 priv->oldduplex = -1;
457 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
459 interface = gfar_get_interface(dev);
461 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
463 if (interface == PHY_INTERFACE_MODE_SGMII)
464 gfar_configure_serdes(dev);
466 if (IS_ERR(phydev)) {
467 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
468 return PTR_ERR(phydev);
471 /* Remove any features not supported by the controller */
472 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
473 phydev->advertising = phydev->supported;
475 priv->phydev = phydev;
480 static void gfar_configure_serdes(struct net_device *dev)
482 struct gfar_private *priv = netdev_priv(dev);
483 struct gfar_mii __iomem *regs =
484 (void __iomem *)&priv->regs->gfar_mii_regs;
486 /* Initialise TBI i/f to communicate with serdes (lynx phy) */
488 /* Single clk mode, mii mode off(for aerdes communication) */
489 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_TBICON, TBICON_CLK_SELECT);
491 /* Supported pause and full-duplex, no half-duplex */
492 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_ADVERTISE,
493 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
494 ADVERTISE_1000XPSE_ASYM);
496 /* ANEG enable, restart ANEG, full duplex mode, speed[1] set */
497 gfar_local_mdio_write(regs, TBIPA_VALUE, MII_BMCR, BMCR_ANENABLE |
498 BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
501 static void init_registers(struct net_device *dev)
503 struct gfar_private *priv = netdev_priv(dev);
506 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
508 /* Initialize IMASK */
509 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
511 /* Init hash registers to zero */
512 gfar_write(&priv->regs->igaddr0, 0);
513 gfar_write(&priv->regs->igaddr1, 0);
514 gfar_write(&priv->regs->igaddr2, 0);
515 gfar_write(&priv->regs->igaddr3, 0);
516 gfar_write(&priv->regs->igaddr4, 0);
517 gfar_write(&priv->regs->igaddr5, 0);
518 gfar_write(&priv->regs->igaddr6, 0);
519 gfar_write(&priv->regs->igaddr7, 0);
521 gfar_write(&priv->regs->gaddr0, 0);
522 gfar_write(&priv->regs->gaddr1, 0);
523 gfar_write(&priv->regs->gaddr2, 0);
524 gfar_write(&priv->regs->gaddr3, 0);
525 gfar_write(&priv->regs->gaddr4, 0);
526 gfar_write(&priv->regs->gaddr5, 0);
527 gfar_write(&priv->regs->gaddr6, 0);
528 gfar_write(&priv->regs->gaddr7, 0);
530 /* Zero out the rmon mib registers if it has them */
531 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
532 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
534 /* Mask off the CAM interrupts */
535 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
536 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
539 /* Initialize the max receive buffer length */
540 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
542 /* Initialize the Minimum Frame Length Register */
543 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
545 /* Assign the TBI an address which won't conflict with the PHYs */
546 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
550 /* Halt the receive and transmit queues */
551 void gfar_halt(struct net_device *dev)
553 struct gfar_private *priv = netdev_priv(dev);
554 struct gfar __iomem *regs = priv->regs;
557 /* Mask all interrupts */
558 gfar_write(®s->imask, IMASK_INIT_CLEAR);
560 /* Clear all interrupts */
561 gfar_write(®s->ievent, IEVENT_INIT_CLEAR);
563 /* Stop the DMA, and wait for it to stop */
564 tempval = gfar_read(&priv->regs->dmactrl);
565 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
566 != (DMACTRL_GRS | DMACTRL_GTS)) {
567 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
568 gfar_write(&priv->regs->dmactrl, tempval);
570 while (!(gfar_read(&priv->regs->ievent) &
571 (IEVENT_GRSC | IEVENT_GTSC)))
575 /* Disable Rx and Tx */
576 tempval = gfar_read(®s->maccfg1);
577 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
578 gfar_write(®s->maccfg1, tempval);
581 void stop_gfar(struct net_device *dev)
583 struct gfar_private *priv = netdev_priv(dev);
584 struct gfar __iomem *regs = priv->regs;
587 phy_stop(priv->phydev);
590 spin_lock_irqsave(&priv->txlock, flags);
591 spin_lock(&priv->rxlock);
595 spin_unlock(&priv->rxlock);
596 spin_unlock_irqrestore(&priv->txlock, flags);
599 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
600 free_irq(priv->interruptError, dev);
601 free_irq(priv->interruptTransmit, dev);
602 free_irq(priv->interruptReceive, dev);
604 free_irq(priv->interruptTransmit, dev);
607 free_skb_resources(priv);
609 dma_free_coherent(&dev->dev,
610 sizeof(struct txbd8)*priv->tx_ring_size
611 + sizeof(struct rxbd8)*priv->rx_ring_size,
613 gfar_read(®s->tbase0));
616 /* If there are any tx skbs or rx skbs still around, free them.
617 * Then free tx_skbuff and rx_skbuff */
618 static void free_skb_resources(struct gfar_private *priv)
624 /* Go through all the buffer descriptors and free their data buffers */
625 txbdp = priv->tx_bd_base;
627 for (i = 0; i < priv->tx_ring_size; i++) {
629 if (priv->tx_skbuff[i]) {
630 dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
633 dev_kfree_skb_any(priv->tx_skbuff[i]);
634 priv->tx_skbuff[i] = NULL;
638 kfree(priv->tx_skbuff);
640 rxbdp = priv->rx_bd_base;
642 /* rx_skbuff is not guaranteed to be allocated, so only
643 * free it and its contents if it is allocated */
644 if(priv->rx_skbuff != NULL) {
645 for (i = 0; i < priv->rx_ring_size; i++) {
646 if (priv->rx_skbuff[i]) {
647 dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
648 priv->rx_buffer_size,
651 dev_kfree_skb_any(priv->rx_skbuff[i]);
652 priv->rx_skbuff[i] = NULL;
662 kfree(priv->rx_skbuff);
666 void gfar_start(struct net_device *dev)
668 struct gfar_private *priv = netdev_priv(dev);
669 struct gfar __iomem *regs = priv->regs;
672 /* Enable Rx and Tx in MACCFG1 */
673 tempval = gfar_read(®s->maccfg1);
674 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
675 gfar_write(®s->maccfg1, tempval);
677 /* Initialize DMACTRL to have WWR and WOP */
678 tempval = gfar_read(&priv->regs->dmactrl);
679 tempval |= DMACTRL_INIT_SETTINGS;
680 gfar_write(&priv->regs->dmactrl, tempval);
682 /* Make sure we aren't stopped */
683 tempval = gfar_read(&priv->regs->dmactrl);
684 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
685 gfar_write(&priv->regs->dmactrl, tempval);
687 /* Clear THLT/RHLT, so that the DMA starts polling now */
688 gfar_write(®s->tstat, TSTAT_CLEAR_THALT);
689 gfar_write(®s->rstat, RSTAT_CLEAR_RHALT);
691 /* Unmask the interrupts we look for */
692 gfar_write(®s->imask, IMASK_DEFAULT);
695 /* Bring the controller up and running */
696 int startup_gfar(struct net_device *dev)
703 struct gfar_private *priv = netdev_priv(dev);
704 struct gfar __iomem *regs = priv->regs;
709 gfar_write(®s->imask, IMASK_INIT_CLEAR);
711 /* Allocate memory for the buffer descriptors */
712 vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
713 sizeof (struct txbd8) * priv->tx_ring_size +
714 sizeof (struct rxbd8) * priv->rx_ring_size,
718 if (netif_msg_ifup(priv))
719 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
724 priv->tx_bd_base = (struct txbd8 *) vaddr;
726 /* enet DMA only understands physical addresses */
727 gfar_write(®s->tbase0, addr);
729 /* Start the rx descriptor ring where the tx ring leaves off */
730 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
731 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
732 priv->rx_bd_base = (struct rxbd8 *) vaddr;
733 gfar_write(®s->rbase0, addr);
735 /* Setup the skbuff rings */
737 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
738 priv->tx_ring_size, GFP_KERNEL);
740 if (NULL == priv->tx_skbuff) {
741 if (netif_msg_ifup(priv))
742 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
748 for (i = 0; i < priv->tx_ring_size; i++)
749 priv->tx_skbuff[i] = NULL;
752 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
753 priv->rx_ring_size, GFP_KERNEL);
755 if (NULL == priv->rx_skbuff) {
756 if (netif_msg_ifup(priv))
757 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
763 for (i = 0; i < priv->rx_ring_size; i++)
764 priv->rx_skbuff[i] = NULL;
766 /* Initialize some variables in our dev structure */
767 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
768 priv->cur_rx = priv->rx_bd_base;
769 priv->skb_curtx = priv->skb_dirtytx = 0;
772 /* Initialize Transmit Descriptor Ring */
773 txbdp = priv->tx_bd_base;
774 for (i = 0; i < priv->tx_ring_size; i++) {
781 /* Set the last descriptor in the ring to indicate wrap */
783 txbdp->status |= TXBD_WRAP;
785 rxbdp = priv->rx_bd_base;
786 for (i = 0; i < priv->rx_ring_size; i++) {
789 skb = gfar_new_skb(dev);
792 printk(KERN_ERR "%s: Can't allocate RX buffers\n",
795 goto err_rxalloc_fail;
798 priv->rx_skbuff[i] = skb;
800 gfar_new_rxbdp(dev, rxbdp, skb);
805 /* Set the last descriptor in the ring to wrap */
807 rxbdp->status |= RXBD_WRAP;
809 /* If the device has multiple interrupts, register for
810 * them. Otherwise, only register for the one */
811 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
812 /* Install our interrupt handlers for Error,
813 * Transmit, and Receive */
814 if (request_irq(priv->interruptError, gfar_error,
815 0, "enet_error", dev) < 0) {
816 if (netif_msg_intr(priv))
817 printk(KERN_ERR "%s: Can't get IRQ %d\n",
818 dev->name, priv->interruptError);
824 if (request_irq(priv->interruptTransmit, gfar_transmit,
825 0, "enet_tx", dev) < 0) {
826 if (netif_msg_intr(priv))
827 printk(KERN_ERR "%s: Can't get IRQ %d\n",
828 dev->name, priv->interruptTransmit);
835 if (request_irq(priv->interruptReceive, gfar_receive,
836 0, "enet_rx", dev) < 0) {
837 if (netif_msg_intr(priv))
838 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
839 dev->name, priv->interruptReceive);
845 if (request_irq(priv->interruptTransmit, gfar_interrupt,
846 0, "gfar_interrupt", dev) < 0) {
847 if (netif_msg_intr(priv))
848 printk(KERN_ERR "%s: Can't get IRQ %d\n",
849 dev->name, priv->interruptError);
856 phy_start(priv->phydev);
858 /* Configure the coalescing support */
859 if (priv->txcoalescing)
860 gfar_write(®s->txic,
861 mk_ic_value(priv->txcount, priv->txtime));
863 gfar_write(®s->txic, 0);
865 if (priv->rxcoalescing)
866 gfar_write(®s->rxic,
867 mk_ic_value(priv->rxcount, priv->rxtime));
869 gfar_write(®s->rxic, 0);
871 if (priv->rx_csum_enable)
872 rctrl |= RCTRL_CHECKSUMMING;
874 if (priv->extended_hash) {
875 rctrl |= RCTRL_EXTHASH;
877 gfar_clear_exact_match(dev);
881 if (priv->vlan_enable)
885 rctrl &= ~RCTRL_PAL_MASK;
886 rctrl |= RCTRL_PADDING(priv->padding);
889 /* Init rctrl based on our settings */
890 gfar_write(&priv->regs->rctrl, rctrl);
892 if (dev->features & NETIF_F_IP_CSUM)
893 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
895 /* Set the extraction length and index */
896 attrs = ATTRELI_EL(priv->rx_stash_size) |
897 ATTRELI_EI(priv->rx_stash_index);
899 gfar_write(&priv->regs->attreli, attrs);
901 /* Start with defaults, and add stashing or locking
902 * depending on the approprate variables */
903 attrs = ATTR_INIT_SETTINGS;
905 if (priv->bd_stash_en)
906 attrs |= ATTR_BDSTASH;
908 if (priv->rx_stash_size != 0)
909 attrs |= ATTR_BUFSTASH;
911 gfar_write(&priv->regs->attr, attrs);
913 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
914 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
915 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
917 /* Start the controller */
923 free_irq(priv->interruptTransmit, dev);
925 free_irq(priv->interruptError, dev);
929 free_skb_resources(priv);
931 dma_free_coherent(&dev->dev,
932 sizeof(struct txbd8)*priv->tx_ring_size
933 + sizeof(struct rxbd8)*priv->rx_ring_size,
935 gfar_read(®s->tbase0));
940 /* Called when something needs to use the ethernet device */
941 /* Returns 0 for success. */
942 static int gfar_enet_open(struct net_device *dev)
944 #ifdef CONFIG_GFAR_NAPI
945 struct gfar_private *priv = netdev_priv(dev);
949 #ifdef CONFIG_GFAR_NAPI
950 napi_enable(&priv->napi);
953 /* Initialize a bunch of registers */
956 gfar_set_mac_address(dev);
961 #ifdef CONFIG_GFAR_NAPI
962 napi_disable(&priv->napi);
967 err = startup_gfar(dev);
969 #ifdef CONFIG_GFAR_NAPI
970 napi_disable(&priv->napi);
975 netif_start_queue(dev);
980 static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
982 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
984 memset(fcb, 0, GMAC_FCB_LEN);
989 static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
993 /* If we're here, it's a IP packet with a TCP or UDP
994 * payload. We set it to checksum, using a pseudo-header
997 flags = TXFCB_DEFAULT;
999 /* Tell the controller what the protocol is */
1000 /* And provide the already calculated phcs */
1001 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
1003 fcb->phcs = udp_hdr(skb)->check;
1005 fcb->phcs = tcp_hdr(skb)->check;
1007 /* l3os is the distance between the start of the
1008 * frame (skb->data) and the start of the IP hdr.
1009 * l4os is the distance between the start of the
1010 * l3 hdr and the l4 hdr */
1011 fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
1012 fcb->l4os = skb_network_header_len(skb);
1017 void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
1019 fcb->flags |= TXFCB_VLN;
1020 fcb->vlctl = vlan_tx_tag_get(skb);
1023 /* This is called by the kernel when a frame is ready for transmission. */
1024 /* It is pointed to by the dev->hard_start_xmit function pointer */
1025 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1027 struct gfar_private *priv = netdev_priv(dev);
1028 struct txfcb *fcb = NULL;
1029 struct txbd8 *txbdp;
1031 unsigned long flags;
1033 /* Update transmit stats */
1034 dev->stats.tx_bytes += skb->len;
1037 spin_lock_irqsave(&priv->txlock, flags);
1039 /* Point at the first free tx descriptor */
1040 txbdp = priv->cur_tx;
1042 /* Clear all but the WRAP status flags */
1043 status = txbdp->status & TXBD_WRAP;
1045 /* Set up checksumming */
1046 if (likely((dev->features & NETIF_F_IP_CSUM)
1047 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
1048 fcb = gfar_add_fcb(skb, txbdp);
1050 gfar_tx_checksum(skb, fcb);
1053 if (priv->vlan_enable &&
1054 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
1055 if (unlikely(NULL == fcb)) {
1056 fcb = gfar_add_fcb(skb, txbdp);
1060 gfar_tx_vlan(skb, fcb);
1063 /* Set buffer length and pointer */
1064 txbdp->length = skb->len;
1065 txbdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1066 skb->len, DMA_TO_DEVICE);
1068 /* Save the skb pointer so we can free it later */
1069 priv->tx_skbuff[priv->skb_curtx] = skb;
1071 /* Update the current skb pointer (wrapping if this was the last) */
1073 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1075 /* Flag the BD as interrupt-causing */
1076 status |= TXBD_INTERRUPT;
1078 /* Flag the BD as ready to go, last in frame, and */
1079 /* in need of CRC */
1080 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
1082 dev->trans_start = jiffies;
1084 /* The powerpc-specific eieio() is used, as wmb() has too strong
1085 * semantics (it requires synchronization between cacheable and
1086 * uncacheable mappings, which eieio doesn't provide and which we
1087 * don't need), thus requiring a more expensive sync instruction. At
1088 * some point, the set of architecture-independent barrier functions
1089 * should be expanded to include weaker barriers.
1093 txbdp->status = status;
1095 /* If this was the last BD in the ring, the next one */
1096 /* is at the beginning of the ring */
1097 if (txbdp->status & TXBD_WRAP)
1098 txbdp = priv->tx_bd_base;
1102 /* If the next BD still needs to be cleaned up, then the bds
1103 are full. We need to tell the kernel to stop sending us stuff. */
1104 if (txbdp == priv->dirty_tx) {
1105 netif_stop_queue(dev);
1107 dev->stats.tx_fifo_errors++;
1110 /* Update the current txbd to the next one */
1111 priv->cur_tx = txbdp;
1113 /* Tell the DMA to go go go */
1114 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1117 spin_unlock_irqrestore(&priv->txlock, flags);
1122 /* Stops the kernel queue, and halts the controller */
1123 static int gfar_close(struct net_device *dev)
1125 struct gfar_private *priv = netdev_priv(dev);
1127 #ifdef CONFIG_GFAR_NAPI
1128 napi_disable(&priv->napi);
1133 /* Disconnect from the PHY */
1134 phy_disconnect(priv->phydev);
1135 priv->phydev = NULL;
1137 netif_stop_queue(dev);
1142 /* Changes the mac address if the controller is not running. */
1143 int gfar_set_mac_address(struct net_device *dev)
1145 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
1151 /* Enables and disables VLAN insertion/extraction */
1152 static void gfar_vlan_rx_register(struct net_device *dev,
1153 struct vlan_group *grp)
1155 struct gfar_private *priv = netdev_priv(dev);
1156 unsigned long flags;
1159 spin_lock_irqsave(&priv->rxlock, flags);
1164 /* Enable VLAN tag insertion */
1165 tempval = gfar_read(&priv->regs->tctrl);
1166 tempval |= TCTRL_VLINS;
1168 gfar_write(&priv->regs->tctrl, tempval);
1170 /* Enable VLAN tag extraction */
1171 tempval = gfar_read(&priv->regs->rctrl);
1172 tempval |= RCTRL_VLEX;
1173 gfar_write(&priv->regs->rctrl, tempval);
1175 /* Disable VLAN tag insertion */
1176 tempval = gfar_read(&priv->regs->tctrl);
1177 tempval &= ~TCTRL_VLINS;
1178 gfar_write(&priv->regs->tctrl, tempval);
1180 /* Disable VLAN tag extraction */
1181 tempval = gfar_read(&priv->regs->rctrl);
1182 tempval &= ~RCTRL_VLEX;
1183 gfar_write(&priv->regs->rctrl, tempval);
1186 spin_unlock_irqrestore(&priv->rxlock, flags);
1189 static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1191 int tempsize, tempval;
1192 struct gfar_private *priv = netdev_priv(dev);
1193 int oldsize = priv->rx_buffer_size;
1194 int frame_size = new_mtu + ETH_HLEN;
1196 if (priv->vlan_enable)
1197 frame_size += VLAN_HLEN;
1199 if (gfar_uses_fcb(priv))
1200 frame_size += GMAC_FCB_LEN;
1202 frame_size += priv->padding;
1204 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
1205 if (netif_msg_drv(priv))
1206 printk(KERN_ERR "%s: Invalid MTU setting\n",
1212 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1213 INCREMENTAL_BUFFER_SIZE;
1215 /* Only stop and start the controller if it isn't already
1216 * stopped, and we changed something */
1217 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1220 priv->rx_buffer_size = tempsize;
1224 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1225 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1227 /* If the mtu is larger than the max size for standard
1228 * ethernet frames (ie, a jumbo frame), then set maccfg2
1229 * to allow huge frames, and to check the length */
1230 tempval = gfar_read(&priv->regs->maccfg2);
1232 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1233 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1235 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1237 gfar_write(&priv->regs->maccfg2, tempval);
1239 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1245 /* gfar_timeout gets called when a packet has not been
1246 * transmitted after a set amount of time.
1247 * For now, assume that clearing out all the structures, and
1248 * starting over will fix the problem. */
1249 static void gfar_timeout(struct net_device *dev)
1251 dev->stats.tx_errors++;
1253 if (dev->flags & IFF_UP) {
1258 netif_schedule(dev);
1261 /* Interrupt Handler for Transmit complete */
1262 int gfar_clean_tx_ring(struct net_device *dev)
1265 struct gfar_private *priv = netdev_priv(dev);
1268 bdp = priv->dirty_tx;
1269 while ((bdp->status & TXBD_READY) == 0) {
1270 /* If dirty_tx and cur_tx are the same, then either the */
1271 /* ring is empty or full now (it could only be full in the beginning, */
1272 /* obviously). If it is empty, we are done. */
1273 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1278 /* Deferred means some collisions occurred during transmit, */
1279 /* but we eventually sent the packet. */
1280 if (bdp->status & TXBD_DEF)
1281 dev->stats.collisions++;
1283 /* Free the sk buffer associated with this TxBD */
1284 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1286 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1288 (priv->skb_dirtytx +
1289 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1291 /* Clean BD length for empty detection */
1294 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1295 if (bdp->status & TXBD_WRAP)
1296 bdp = priv->tx_bd_base;
1300 /* Move dirty_tx to be the next bd */
1301 priv->dirty_tx = bdp;
1303 /* We freed a buffer, so now we can restart transmission */
1304 if (netif_queue_stopped(dev))
1305 netif_wake_queue(dev);
1306 } /* while ((bdp->status & TXBD_READY) == 0) */
1308 dev->stats.tx_packets += howmany;
1313 /* Interrupt Handler for Transmit complete */
1314 static irqreturn_t gfar_transmit(int irq, void *dev_id)
1316 struct net_device *dev = (struct net_device *) dev_id;
1317 struct gfar_private *priv = netdev_priv(dev);
1320 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1323 spin_lock(&priv->txlock);
1325 gfar_clean_tx_ring(dev);
1327 /* If we are coalescing the interrupts, reset the timer */
1328 /* Otherwise, clear it */
1329 if (likely(priv->txcoalescing)) {
1330 gfar_write(&priv->regs->txic, 0);
1331 gfar_write(&priv->regs->txic,
1332 mk_ic_value(priv->txcount, priv->txtime));
1335 spin_unlock(&priv->txlock);
1340 static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
1341 struct sk_buff *skb)
1343 struct gfar_private *priv = netdev_priv(dev);
1344 u32 * status_len = (u32 *)bdp;
1347 bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
1348 priv->rx_buffer_size, DMA_FROM_DEVICE);
1350 flags = RXBD_EMPTY | RXBD_INTERRUPT;
1352 if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
1357 *status_len = (u32)flags << 16;
1361 struct sk_buff * gfar_new_skb(struct net_device *dev)
1363 unsigned int alignamount;
1364 struct gfar_private *priv = netdev_priv(dev);
1365 struct sk_buff *skb = NULL;
1367 /* We have to allocate the skb, so keep trying till we succeed */
1368 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
1373 alignamount = RXBUF_ALIGNMENT -
1374 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
1376 /* We need the data buffer to be aligned properly. We will reserve
1377 * as many bytes as needed to align the data properly
1379 skb_reserve(skb, alignamount);
1384 static inline void count_errors(unsigned short status, struct net_device *dev)
1386 struct gfar_private *priv = netdev_priv(dev);
1387 struct net_device_stats *stats = &dev->stats;
1388 struct gfar_extra_stats *estats = &priv->extra_stats;
1390 /* If the packet was truncated, none of the other errors
1392 if (status & RXBD_TRUNCATED) {
1393 stats->rx_length_errors++;
1399 /* Count the errors, if there were any */
1400 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1401 stats->rx_length_errors++;
1403 if (status & RXBD_LARGE)
1408 if (status & RXBD_NONOCTET) {
1409 stats->rx_frame_errors++;
1410 estats->rx_nonoctet++;
1412 if (status & RXBD_CRCERR) {
1413 estats->rx_crcerr++;
1414 stats->rx_crc_errors++;
1416 if (status & RXBD_OVERRUN) {
1417 estats->rx_overrun++;
1418 stats->rx_crc_errors++;
1422 irqreturn_t gfar_receive(int irq, void *dev_id)
1424 struct net_device *dev = (struct net_device *) dev_id;
1425 struct gfar_private *priv = netdev_priv(dev);
1426 #ifdef CONFIG_GFAR_NAPI
1429 unsigned long flags;
1433 #ifdef CONFIG_GFAR_NAPI
1434 /* Clear IEVENT, so interrupts aren't called again
1435 * because of the packets that have already arrived */
1436 gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
1438 if (netif_rx_schedule_prep(dev, &priv->napi)) {
1439 tempval = gfar_read(&priv->regs->imask);
1440 tempval &= IMASK_RTX_DISABLED;
1441 gfar_write(&priv->regs->imask, tempval);
1443 __netif_rx_schedule(dev, &priv->napi);
1445 if (netif_msg_rx_err(priv))
1446 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1447 dev->name, gfar_read(&priv->regs->ievent),
1448 gfar_read(&priv->regs->imask));
1451 /* Clear IEVENT, so rx interrupt isn't called again
1452 * because of this interrupt */
1453 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1455 spin_lock_irqsave(&priv->rxlock, flags);
1456 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1458 /* If we are coalescing interrupts, update the timer */
1459 /* Otherwise, clear it */
1460 if (likely(priv->rxcoalescing)) {
1461 gfar_write(&priv->regs->rxic, 0);
1462 gfar_write(&priv->regs->rxic,
1463 mk_ic_value(priv->rxcount, priv->rxtime));
1466 spin_unlock_irqrestore(&priv->rxlock, flags);
1472 static inline int gfar_rx_vlan(struct sk_buff *skb,
1473 struct vlan_group *vlgrp, unsigned short vlctl)
1475 #ifdef CONFIG_GFAR_NAPI
1476 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1478 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1482 static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1484 /* If valid headers were found, and valid sums
1485 * were verified, then we tell the kernel that no
1486 * checksumming is necessary. Otherwise, it is */
1487 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
1488 skb->ip_summed = CHECKSUM_UNNECESSARY;
1490 skb->ip_summed = CHECKSUM_NONE;
1494 static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1496 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1498 /* Remove the FCB from the skb */
1499 skb_pull(skb, GMAC_FCB_LEN);
1504 /* gfar_process_frame() -- handle one incoming packet if skb
1506 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1509 struct gfar_private *priv = netdev_priv(dev);
1510 struct rxfcb *fcb = NULL;
1513 if (netif_msg_rx_err(priv))
1514 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
1515 dev->stats.rx_dropped++;
1516 priv->extra_stats.rx_skbmissing++;
1520 /* Prep the skb for the packet */
1521 skb_put(skb, length);
1523 /* Grab the FCB if there is one */
1524 if (gfar_uses_fcb(priv))
1525 fcb = gfar_get_fcb(skb);
1527 /* Remove the padded bytes, if there are any */
1529 skb_pull(skb, priv->padding);
1531 if (priv->rx_csum_enable)
1532 gfar_rx_checksum(skb, fcb);
1534 /* Tell the skb what kind of packet this is */
1535 skb->protocol = eth_type_trans(skb, dev);
1537 /* Send the packet up the stack */
1538 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1539 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1543 if (NET_RX_DROP == ret)
1544 priv->extra_stats.kernel_dropped++;
1550 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
1551 * until the budget/quota has been reached. Returns the number
1554 int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
1557 struct sk_buff *skb;
1560 struct gfar_private *priv = netdev_priv(dev);
1562 /* Get the first full descriptor */
1565 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
1566 struct sk_buff *newskb;
1569 /* Add another skb for the future */
1570 newskb = gfar_new_skb(dev);
1572 skb = priv->rx_skbuff[priv->skb_currx];
1574 /* We drop the frame if we failed to allocate a new buffer */
1575 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
1576 bdp->status & RXBD_ERR)) {
1577 count_errors(bdp->status, dev);
1579 if (unlikely(!newskb))
1583 dma_unmap_single(&priv->dev->dev,
1585 priv->rx_buffer_size,
1588 dev_kfree_skb_any(skb);
1591 /* Increment the number of packets */
1592 dev->stats.rx_packets++;
1595 /* Remove the FCS from the packet length */
1596 pkt_len = bdp->length - 4;
1598 gfar_process_frame(dev, skb, pkt_len);
1600 dev->stats.rx_bytes += pkt_len;
1603 dev->last_rx = jiffies;
1605 priv->rx_skbuff[priv->skb_currx] = newskb;
1607 /* Setup the new bdp */
1608 gfar_new_rxbdp(dev, bdp, newskb);
1610 /* Update to the next pointer */
1611 if (bdp->status & RXBD_WRAP)
1612 bdp = priv->rx_bd_base;
1616 /* update to point at the next skb */
1618 (priv->skb_currx + 1) &
1619 RX_RING_MOD_MASK(priv->rx_ring_size);
1622 /* Update the current rxbd pointer to be the next one */
1628 #ifdef CONFIG_GFAR_NAPI
1629 static int gfar_poll(struct napi_struct *napi, int budget)
1631 struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
1632 struct net_device *dev = priv->dev;
1634 unsigned long flags;
1636 /* If we fail to get the lock, don't bother with the TX BDs */
1637 if (spin_trylock_irqsave(&priv->txlock, flags)) {
1638 gfar_clean_tx_ring(dev);
1639 spin_unlock_irqrestore(&priv->txlock, flags);
1642 howmany = gfar_clean_rx_ring(dev, budget);
1644 if (howmany < budget) {
1645 netif_rx_complete(dev, napi);
1647 /* Clear the halt bit in RSTAT */
1648 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1650 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1652 /* If we are coalescing interrupts, update the timer */
1653 /* Otherwise, clear it */
1654 if (likely(priv->rxcoalescing)) {
1655 gfar_write(&priv->regs->rxic, 0);
1656 gfar_write(&priv->regs->rxic,
1657 mk_ic_value(priv->rxcount, priv->rxtime));
1665 #ifdef CONFIG_NET_POLL_CONTROLLER
1667 * Polling 'interrupt' - used by things like netconsole to send skbs
1668 * without having to re-enable interrupts. It's not called while
1669 * the interrupt routine is executing.
1671 static void gfar_netpoll(struct net_device *dev)
1673 struct gfar_private *priv = netdev_priv(dev);
1675 /* If the device has multiple interrupts, run tx/rx */
1676 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1677 disable_irq(priv->interruptTransmit);
1678 disable_irq(priv->interruptReceive);
1679 disable_irq(priv->interruptError);
1680 gfar_interrupt(priv->interruptTransmit, dev);
1681 enable_irq(priv->interruptError);
1682 enable_irq(priv->interruptReceive);
1683 enable_irq(priv->interruptTransmit);
1685 disable_irq(priv->interruptTransmit);
1686 gfar_interrupt(priv->interruptTransmit, dev);
1687 enable_irq(priv->interruptTransmit);
1692 /* The interrupt handler for devices with one interrupt */
1693 static irqreturn_t gfar_interrupt(int irq, void *dev_id)
1695 struct net_device *dev = dev_id;
1696 struct gfar_private *priv = netdev_priv(dev);
1698 /* Save ievent for future reference */
1699 u32 events = gfar_read(&priv->regs->ievent);
1701 /* Check for reception */
1702 if (events & IEVENT_RX_MASK)
1703 gfar_receive(irq, dev_id);
1705 /* Check for transmit completion */
1706 if (events & IEVENT_TX_MASK)
1707 gfar_transmit(irq, dev_id);
1709 /* Check for errors */
1710 if (events & IEVENT_ERR_MASK)
1711 gfar_error(irq, dev_id);
1716 /* Called every time the controller might need to be made
1717 * aware of new link state. The PHY code conveys this
1718 * information through variables in the phydev structure, and this
1719 * function converts those variables into the appropriate
1720 * register values, and can bring down the device if needed.
1722 static void adjust_link(struct net_device *dev)
1724 struct gfar_private *priv = netdev_priv(dev);
1725 struct gfar __iomem *regs = priv->regs;
1726 unsigned long flags;
1727 struct phy_device *phydev = priv->phydev;
1730 spin_lock_irqsave(&priv->txlock, flags);
1732 u32 tempval = gfar_read(®s->maccfg2);
1733 u32 ecntrl = gfar_read(®s->ecntrl);
1735 /* Now we make sure that we can be in full duplex mode.
1736 * If not, we operate in half-duplex mode. */
1737 if (phydev->duplex != priv->oldduplex) {
1739 if (!(phydev->duplex))
1740 tempval &= ~(MACCFG2_FULL_DUPLEX);
1742 tempval |= MACCFG2_FULL_DUPLEX;
1744 priv->oldduplex = phydev->duplex;
1747 if (phydev->speed != priv->oldspeed) {
1749 switch (phydev->speed) {
1752 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
1757 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
1759 /* Reduced mode distinguishes
1760 * between 10 and 100 */
1761 if (phydev->speed == SPEED_100)
1762 ecntrl |= ECNTRL_R100;
1764 ecntrl &= ~(ECNTRL_R100);
1767 if (netif_msg_link(priv))
1769 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1770 dev->name, phydev->speed);
1774 priv->oldspeed = phydev->speed;
1777 gfar_write(®s->maccfg2, tempval);
1778 gfar_write(®s->ecntrl, ecntrl);
1780 if (!priv->oldlink) {
1783 netif_schedule(dev);
1785 } else if (priv->oldlink) {
1789 priv->oldduplex = -1;
1792 if (new_state && netif_msg_link(priv))
1793 phy_print_status(phydev);
1795 spin_unlock_irqrestore(&priv->txlock, flags);
1798 /* Update the hash table based on the current list of multicast
1799 * addresses we subscribe to. Also, change the promiscuity of
1800 * the device based on the flags (this function is called
1801 * whenever dev->flags is changed */
1802 static void gfar_set_multi(struct net_device *dev)
1804 struct dev_mc_list *mc_ptr;
1805 struct gfar_private *priv = netdev_priv(dev);
1806 struct gfar __iomem *regs = priv->regs;
1809 if(dev->flags & IFF_PROMISC) {
1810 /* Set RCTRL to PROM */
1811 tempval = gfar_read(®s->rctrl);
1812 tempval |= RCTRL_PROM;
1813 gfar_write(®s->rctrl, tempval);
1815 /* Set RCTRL to not PROM */
1816 tempval = gfar_read(®s->rctrl);
1817 tempval &= ~(RCTRL_PROM);
1818 gfar_write(®s->rctrl, tempval);
1821 if(dev->flags & IFF_ALLMULTI) {
1822 /* Set the hash to rx all multicast frames */
1823 gfar_write(®s->igaddr0, 0xffffffff);
1824 gfar_write(®s->igaddr1, 0xffffffff);
1825 gfar_write(®s->igaddr2, 0xffffffff);
1826 gfar_write(®s->igaddr3, 0xffffffff);
1827 gfar_write(®s->igaddr4, 0xffffffff);
1828 gfar_write(®s->igaddr5, 0xffffffff);
1829 gfar_write(®s->igaddr6, 0xffffffff);
1830 gfar_write(®s->igaddr7, 0xffffffff);
1831 gfar_write(®s->gaddr0, 0xffffffff);
1832 gfar_write(®s->gaddr1, 0xffffffff);
1833 gfar_write(®s->gaddr2, 0xffffffff);
1834 gfar_write(®s->gaddr3, 0xffffffff);
1835 gfar_write(®s->gaddr4, 0xffffffff);
1836 gfar_write(®s->gaddr5, 0xffffffff);
1837 gfar_write(®s->gaddr6, 0xffffffff);
1838 gfar_write(®s->gaddr7, 0xffffffff);
1843 /* zero out the hash */
1844 gfar_write(®s->igaddr0, 0x0);
1845 gfar_write(®s->igaddr1, 0x0);
1846 gfar_write(®s->igaddr2, 0x0);
1847 gfar_write(®s->igaddr3, 0x0);
1848 gfar_write(®s->igaddr4, 0x0);
1849 gfar_write(®s->igaddr5, 0x0);
1850 gfar_write(®s->igaddr6, 0x0);
1851 gfar_write(®s->igaddr7, 0x0);
1852 gfar_write(®s->gaddr0, 0x0);
1853 gfar_write(®s->gaddr1, 0x0);
1854 gfar_write(®s->gaddr2, 0x0);
1855 gfar_write(®s->gaddr3, 0x0);
1856 gfar_write(®s->gaddr4, 0x0);
1857 gfar_write(®s->gaddr5, 0x0);
1858 gfar_write(®s->gaddr6, 0x0);
1859 gfar_write(®s->gaddr7, 0x0);
1861 /* If we have extended hash tables, we need to
1862 * clear the exact match registers to prepare for
1864 if (priv->extended_hash) {
1865 em_num = GFAR_EM_NUM + 1;
1866 gfar_clear_exact_match(dev);
1873 if(dev->mc_count == 0)
1876 /* Parse the list, and set the appropriate bits */
1877 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
1879 gfar_set_mac_for_addr(dev, idx,
1883 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
1891 /* Clears each of the exact match registers to zero, so they
1892 * don't interfere with normal reception */
1893 static void gfar_clear_exact_match(struct net_device *dev)
1896 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1898 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1899 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1902 /* Set the appropriate hash bit for the given addr */
1903 /* The algorithm works like so:
1904 * 1) Take the Destination Address (ie the multicast address), and
1905 * do a CRC on it (little endian), and reverse the bits of the
1907 * 2) Use the 8 most significant bits as a hash into a 256-entry
1908 * table. The table is controlled through 8 32-bit registers:
1909 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1910 * gaddr7. This means that the 3 most significant bits in the
1911 * hash index which gaddr register to use, and the 5 other bits
1912 * indicate which bit (assuming an IBM numbering scheme, which
1913 * for PowerPC (tm) is usually the case) in the register holds
1915 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1918 struct gfar_private *priv = netdev_priv(dev);
1919 u32 result = ether_crc(MAC_ADDR_LEN, addr);
1920 int width = priv->hash_width;
1921 u8 whichbit = (result >> (32 - width)) & 0x1f;
1922 u8 whichreg = result >> (32 - width + 5);
1923 u32 value = (1 << (31-whichbit));
1925 tempval = gfar_read(priv->hash_regs[whichreg]);
1927 gfar_write(priv->hash_regs[whichreg], tempval);
1933 /* There are multiple MAC Address register pairs on some controllers
1934 * This function sets the numth pair to a given address
1936 static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1938 struct gfar_private *priv = netdev_priv(dev);
1940 char tmpbuf[MAC_ADDR_LEN];
1942 u32 __iomem *macptr = &priv->regs->macstnaddr1;
1946 /* Now copy it into the mac registers backwards, cuz */
1947 /* little endian is silly */
1948 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1949 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1951 gfar_write(macptr, *((u32 *) (tmpbuf)));
1953 tempval = *((u32 *) (tmpbuf + 4));
1955 gfar_write(macptr+1, tempval);
1958 /* GFAR error interrupt handler */
1959 static irqreturn_t gfar_error(int irq, void *dev_id)
1961 struct net_device *dev = dev_id;
1962 struct gfar_private *priv = netdev_priv(dev);
1964 /* Save ievent for future reference */
1965 u32 events = gfar_read(&priv->regs->ievent);
1968 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1971 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1972 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
1973 dev->name, events, gfar_read(&priv->regs->imask));
1975 /* Update the error counters */
1976 if (events & IEVENT_TXE) {
1977 dev->stats.tx_errors++;
1979 if (events & IEVENT_LC)
1980 dev->stats.tx_window_errors++;
1981 if (events & IEVENT_CRL)
1982 dev->stats.tx_aborted_errors++;
1983 if (events & IEVENT_XFUN) {
1984 if (netif_msg_tx_err(priv))
1985 printk(KERN_DEBUG "%s: TX FIFO underrun, "
1986 "packet dropped.\n", dev->name);
1987 dev->stats.tx_dropped++;
1988 priv->extra_stats.tx_underrun++;
1990 /* Reactivate the Tx Queues */
1991 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1993 if (netif_msg_tx_err(priv))
1994 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
1996 if (events & IEVENT_BSY) {
1997 dev->stats.rx_errors++;
1998 priv->extra_stats.rx_bsy++;
2000 gfar_receive(irq, dev_id);
2002 #ifndef CONFIG_GFAR_NAPI
2003 /* Clear the halt bit in RSTAT */
2004 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
2007 if (netif_msg_rx_err(priv))
2008 printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
2009 dev->name, gfar_read(&priv->regs->rstat));
2011 if (events & IEVENT_BABR) {
2012 dev->stats.rx_errors++;
2013 priv->extra_stats.rx_babr++;
2015 if (netif_msg_rx_err(priv))
2016 printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
2018 if (events & IEVENT_EBERR) {
2019 priv->extra_stats.eberr++;
2020 if (netif_msg_rx_err(priv))
2021 printk(KERN_DEBUG "%s: bus error\n", dev->name);
2023 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
2024 printk(KERN_DEBUG "%s: control frame\n", dev->name);
2026 if (events & IEVENT_BABT) {
2027 priv->extra_stats.tx_babt++;
2028 if (netif_msg_tx_err(priv))
2029 printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
2034 /* work with hotplug and coldplug */
2035 MODULE_ALIAS("platform:fsl-gianfar");
2037 /* Structure for a device driver */
2038 static struct platform_driver gfar_driver = {
2039 .probe = gfar_probe,
2040 .remove = gfar_remove,
2042 .name = "fsl-gianfar",
2043 .owner = THIS_MODULE,
2047 static int __init gfar_init(void)
2049 int err = gfar_mdio_init();
2054 err = platform_driver_register(&gfar_driver);
2062 static void __exit gfar_exit(void)
2064 platform_driver_unregister(&gfar_driver);
2068 module_init(gfar_init);
2069 module_exit(gfar_exit);