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.
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
19 * Gianfar: AKA Lambda Draconis, "Dragon"
27 * The driver is initialized through platform_device. Structures which
28 * define the configuration needed by the board are defined in a
29 * board structure in arch/ppc/platforms (though I do not
30 * discount the possibility that other architectures could one
33 * The Gianfar Ethernet Controller uses a ring of buffer
34 * descriptors. The beginning is indicated by a register
35 * pointing to the physical address of the start of the ring.
36 * The end is determined by a "wrap" bit being set in the
37 * last descriptor of the ring.
39 * When a packet is received, the RXF bit in the
40 * IEVENT register is set, triggering an interrupt when the
41 * corresponding bit in the IMASK register is also set (if
42 * interrupt coalescing is active, then the interrupt may not
43 * happen immediately, but will wait until either a set number
44 * of frames or amount of time have passed). In NAPI, the
45 * interrupt handler will signal there is work to be done, and
46 * exit. Without NAPI, the packet(s) will be handled
47 * immediately. Both methods will start at the last known empty
48 * descriptor, and process every subsequent descriptor until there
49 * are none left with data (NAPI will stop after a set number of
50 * packets to give time to other tasks, but will eventually
51 * process all the packets). The data arrives inside a
52 * pre-allocated skb, and so after the skb is passed up to the
53 * stack, a new skb must be allocated, and the address field in
54 * the buffer descriptor must be updated to indicate this new
57 * When the kernel requests that a packet be transmitted, the
58 * driver starts where it left off last time, and points the
59 * descriptor at the buffer which was passed in. The driver
60 * then informs the DMA engine that there are packets ready to
61 * be transmitted. Once the controller is finished transmitting
62 * the packet, an interrupt may be triggered (under the same
63 * conditions as for reception, but depending on the TXF bit).
64 * The driver then cleans up the buffer.
67 #include <linux/kernel.h>
68 #include <linux/string.h>
69 #include <linux/errno.h>
70 #include <linux/unistd.h>
71 #include <linux/slab.h>
72 #include <linux/interrupt.h>
73 #include <linux/init.h>
74 #include <linux/delay.h>
75 #include <linux/netdevice.h>
76 #include <linux/etherdevice.h>
77 #include <linux/skbuff.h>
78 #include <linux/if_vlan.h>
79 #include <linux/spinlock.h>
81 #include <linux/platform_device.h>
83 #include <linux/tcp.h>
84 #include <linux/udp.h>
89 #include <asm/uaccess.h>
90 #include <linux/module.h>
91 #include <linux/dma-mapping.h>
92 #include <linux/crc32.h>
93 #include <linux/mii.h>
94 #include <linux/phy.h>
97 #include "gianfar_mii.h"
99 #define TX_TIMEOUT (1*HZ)
100 #define SKB_ALLOC_TIMEOUT 1000000
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, struct rxbd8 *bdp);
118 static struct net_device_stats *gfar_get_stats(struct net_device *dev);
119 static int gfar_set_mac_address(struct net_device *dev);
120 static int gfar_change_mtu(struct net_device *dev, int new_mtu);
121 static irqreturn_t gfar_error(int irq, void *dev_id);
122 static irqreturn_t gfar_transmit(int irq, void *dev_id);
123 static irqreturn_t gfar_interrupt(int irq, void *dev_id);
124 static void adjust_link(struct net_device *dev);
125 static void init_registers(struct net_device *dev);
126 static int init_phy(struct net_device *dev);
127 static int gfar_probe(struct platform_device *pdev);
128 static int gfar_remove(struct platform_device *pdev);
129 static void free_skb_resources(struct gfar_private *priv);
130 static void gfar_set_multi(struct net_device *dev);
131 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
132 #ifdef CONFIG_GFAR_NAPI
133 static int gfar_poll(struct net_device *dev, int *budget);
135 #ifdef CONFIG_NET_POLL_CONTROLLER
136 static void gfar_netpoll(struct net_device *dev);
138 int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
139 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
140 static void gfar_vlan_rx_register(struct net_device *netdev,
141 struct vlan_group *grp);
142 static void gfar_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
143 void gfar_halt(struct net_device *dev);
144 void gfar_start(struct net_device *dev);
145 static void gfar_clear_exact_match(struct net_device *dev);
146 static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
148 extern const struct ethtool_ops gfar_ethtool_ops;
150 MODULE_AUTHOR("Freescale Semiconductor, Inc");
151 MODULE_DESCRIPTION("Gianfar Ethernet Driver");
152 MODULE_LICENSE("GPL");
154 /* Returns 1 if incoming frames use an FCB */
155 static inline int gfar_uses_fcb(struct gfar_private *priv)
157 return (priv->vlan_enable || priv->rx_csum_enable);
160 /* Set up the ethernet device structure, private data,
161 * and anything else we need before we start */
162 static int gfar_probe(struct platform_device *pdev)
165 struct net_device *dev = NULL;
166 struct gfar_private *priv = NULL;
167 struct gianfar_platform_data *einfo;
172 einfo = (struct gianfar_platform_data *) pdev->dev.platform_data;
175 printk(KERN_ERR "gfar %d: Missing additional data!\n",
181 /* Create an ethernet device instance */
182 dev = alloc_etherdev(sizeof (*priv));
187 priv = netdev_priv(dev);
189 /* Set the info in the priv to the current info */
192 /* fill out IRQ fields */
193 if (einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
194 priv->interruptTransmit = platform_get_irq_byname(pdev, "tx");
195 priv->interruptReceive = platform_get_irq_byname(pdev, "rx");
196 priv->interruptError = platform_get_irq_byname(pdev, "error");
197 if (priv->interruptTransmit < 0 || priv->interruptReceive < 0 || priv->interruptError < 0)
200 priv->interruptTransmit = platform_get_irq(pdev, 0);
201 if (priv->interruptTransmit < 0)
205 /* get a pointer to the register memory */
206 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
207 priv->regs = ioremap(r->start, sizeof (struct gfar));
209 if (NULL == priv->regs) {
214 spin_lock_init(&priv->txlock);
215 spin_lock_init(&priv->rxlock);
217 platform_set_drvdata(pdev, dev);
219 /* Stop the DMA engine now, in case it was running before */
220 /* (The firmware could have used it, and left it running). */
221 /* To do this, we write Graceful Receive Stop and Graceful */
222 /* Transmit Stop, and then wait until the corresponding bits */
223 /* in IEVENT indicate the stops have completed. */
224 tempval = gfar_read(&priv->regs->dmactrl);
225 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
226 gfar_write(&priv->regs->dmactrl, tempval);
228 tempval = gfar_read(&priv->regs->dmactrl);
229 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
230 gfar_write(&priv->regs->dmactrl, tempval);
232 while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
235 /* Reset MAC layer */
236 gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
238 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
239 gfar_write(&priv->regs->maccfg1, tempval);
241 /* Initialize MACCFG2. */
242 gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
244 /* Initialize ECNTRL */
245 gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
247 /* Copy the station address into the dev structure, */
248 memcpy(dev->dev_addr, einfo->mac_addr, MAC_ADDR_LEN);
250 /* Set the dev->base_addr to the gfar reg region */
251 dev->base_addr = (unsigned long) (priv->regs);
253 SET_MODULE_OWNER(dev);
254 SET_NETDEV_DEV(dev, &pdev->dev);
256 /* Fill in the dev structure */
257 dev->open = gfar_enet_open;
258 dev->hard_start_xmit = gfar_start_xmit;
259 dev->tx_timeout = gfar_timeout;
260 dev->watchdog_timeo = TX_TIMEOUT;
261 #ifdef CONFIG_GFAR_NAPI
262 dev->poll = gfar_poll;
263 dev->weight = GFAR_DEV_WEIGHT;
265 #ifdef CONFIG_NET_POLL_CONTROLLER
266 dev->poll_controller = gfar_netpoll;
268 dev->stop = gfar_close;
269 dev->get_stats = gfar_get_stats;
270 dev->change_mtu = gfar_change_mtu;
272 dev->set_multicast_list = gfar_set_multi;
274 dev->ethtool_ops = &gfar_ethtool_ops;
276 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
277 priv->rx_csum_enable = 1;
278 dev->features |= NETIF_F_IP_CSUM;
280 priv->rx_csum_enable = 0;
284 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
285 dev->vlan_rx_register = gfar_vlan_rx_register;
286 dev->vlan_rx_kill_vid = gfar_vlan_rx_kill_vid;
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, dev->name);
363 for (idx = 0; idx < 6; idx++)
364 printk("%2.2x%c", dev->dev_addr[idx], idx == 5 ? ' ' : ':');
367 /* Even more device info helps when determining which kernel */
368 /* provided which set of benchmarks. */
369 #ifdef CONFIG_GFAR_NAPI
370 printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
372 printk(KERN_INFO "%s: Running with NAPI disabled\n", dev->name);
374 printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
375 dev->name, priv->rx_ring_size, priv->tx_ring_size);
386 static int gfar_remove(struct platform_device *pdev)
388 struct net_device *dev = platform_get_drvdata(pdev);
389 struct gfar_private *priv = netdev_priv(dev);
391 platform_set_drvdata(pdev, NULL);
400 /* Reads the controller's registers to determine what interface
401 * connects it to the PHY.
403 static phy_interface_t gfar_get_interface(struct net_device *dev)
405 struct gfar_private *priv = netdev_priv(dev);
406 u32 ecntrl = gfar_read(&priv->regs->ecntrl);
408 if (ecntrl & ECNTRL_SGMII_MODE)
409 return PHY_INTERFACE_MODE_SGMII;
411 if (ecntrl & ECNTRL_TBI_MODE) {
412 if (ecntrl & ECNTRL_REDUCED_MODE)
413 return PHY_INTERFACE_MODE_RTBI;
415 return PHY_INTERFACE_MODE_TBI;
418 if (ecntrl & ECNTRL_REDUCED_MODE) {
419 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
420 return PHY_INTERFACE_MODE_RMII;
422 return PHY_INTERFACE_MODE_RGMII;
425 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
426 return PHY_INTERFACE_MODE_GMII;
428 return PHY_INTERFACE_MODE_MII;
432 /* Initializes driver's PHY state, and attaches to the PHY.
433 * Returns 0 on success.
435 static int init_phy(struct net_device *dev)
437 struct gfar_private *priv = netdev_priv(dev);
438 uint gigabit_support =
439 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
440 SUPPORTED_1000baseT_Full : 0;
441 struct phy_device *phydev;
442 char phy_id[BUS_ID_SIZE];
443 phy_interface_t interface;
447 priv->oldduplex = -1;
449 snprintf(phy_id, BUS_ID_SIZE, PHY_ID_FMT, priv->einfo->bus_id, priv->einfo->phy_id);
451 interface = gfar_get_interface(dev);
453 phydev = phy_connect(dev, phy_id, &adjust_link, 0, interface);
455 if (IS_ERR(phydev)) {
456 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
457 return PTR_ERR(phydev);
460 /* Remove any features not supported by the controller */
461 phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
462 phydev->advertising = phydev->supported;
464 priv->phydev = phydev;
469 static void init_registers(struct net_device *dev)
471 struct gfar_private *priv = netdev_priv(dev);
474 gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
476 /* Initialize IMASK */
477 gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
479 /* Init hash registers to zero */
480 gfar_write(&priv->regs->igaddr0, 0);
481 gfar_write(&priv->regs->igaddr1, 0);
482 gfar_write(&priv->regs->igaddr2, 0);
483 gfar_write(&priv->regs->igaddr3, 0);
484 gfar_write(&priv->regs->igaddr4, 0);
485 gfar_write(&priv->regs->igaddr5, 0);
486 gfar_write(&priv->regs->igaddr6, 0);
487 gfar_write(&priv->regs->igaddr7, 0);
489 gfar_write(&priv->regs->gaddr0, 0);
490 gfar_write(&priv->regs->gaddr1, 0);
491 gfar_write(&priv->regs->gaddr2, 0);
492 gfar_write(&priv->regs->gaddr3, 0);
493 gfar_write(&priv->regs->gaddr4, 0);
494 gfar_write(&priv->regs->gaddr5, 0);
495 gfar_write(&priv->regs->gaddr6, 0);
496 gfar_write(&priv->regs->gaddr7, 0);
498 /* Zero out the rmon mib registers if it has them */
499 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
500 memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
502 /* Mask off the CAM interrupts */
503 gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
504 gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
507 /* Initialize the max receive buffer length */
508 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
510 /* Initialize the Minimum Frame Length Register */
511 gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
513 /* Assign the TBI an address which won't conflict with the PHYs */
514 gfar_write(&priv->regs->tbipa, TBIPA_VALUE);
518 /* Halt the receive and transmit queues */
519 void gfar_halt(struct net_device *dev)
521 struct gfar_private *priv = netdev_priv(dev);
522 struct gfar __iomem *regs = priv->regs;
525 /* Mask all interrupts */
526 gfar_write(®s->imask, IMASK_INIT_CLEAR);
528 /* Clear all interrupts */
529 gfar_write(®s->ievent, IEVENT_INIT_CLEAR);
531 /* Stop the DMA, and wait for it to stop */
532 tempval = gfar_read(&priv->regs->dmactrl);
533 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
534 != (DMACTRL_GRS | DMACTRL_GTS)) {
535 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
536 gfar_write(&priv->regs->dmactrl, tempval);
538 while (!(gfar_read(&priv->regs->ievent) &
539 (IEVENT_GRSC | IEVENT_GTSC)))
543 /* Disable Rx and Tx */
544 tempval = gfar_read(®s->maccfg1);
545 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
546 gfar_write(®s->maccfg1, tempval);
549 void stop_gfar(struct net_device *dev)
551 struct gfar_private *priv = netdev_priv(dev);
552 struct gfar __iomem *regs = priv->regs;
555 phy_stop(priv->phydev);
558 spin_lock_irqsave(&priv->txlock, flags);
559 spin_lock(&priv->rxlock);
563 spin_unlock(&priv->rxlock);
564 spin_unlock_irqrestore(&priv->txlock, flags);
567 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
568 free_irq(priv->interruptError, dev);
569 free_irq(priv->interruptTransmit, dev);
570 free_irq(priv->interruptReceive, dev);
572 free_irq(priv->interruptTransmit, dev);
575 free_skb_resources(priv);
577 dma_free_coherent(NULL,
578 sizeof(struct txbd8)*priv->tx_ring_size
579 + sizeof(struct rxbd8)*priv->rx_ring_size,
581 gfar_read(®s->tbase0));
584 /* If there are any tx skbs or rx skbs still around, free them.
585 * Then free tx_skbuff and rx_skbuff */
586 static void free_skb_resources(struct gfar_private *priv)
592 /* Go through all the buffer descriptors and free their data buffers */
593 txbdp = priv->tx_bd_base;
595 for (i = 0; i < priv->tx_ring_size; i++) {
597 if (priv->tx_skbuff[i]) {
598 dma_unmap_single(NULL, txbdp->bufPtr,
601 dev_kfree_skb_any(priv->tx_skbuff[i]);
602 priv->tx_skbuff[i] = NULL;
606 kfree(priv->tx_skbuff);
608 rxbdp = priv->rx_bd_base;
610 /* rx_skbuff is not guaranteed to be allocated, so only
611 * free it and its contents if it is allocated */
612 if(priv->rx_skbuff != NULL) {
613 for (i = 0; i < priv->rx_ring_size; i++) {
614 if (priv->rx_skbuff[i]) {
615 dma_unmap_single(NULL, rxbdp->bufPtr,
616 priv->rx_buffer_size,
619 dev_kfree_skb_any(priv->rx_skbuff[i]);
620 priv->rx_skbuff[i] = NULL;
630 kfree(priv->rx_skbuff);
634 void gfar_start(struct net_device *dev)
636 struct gfar_private *priv = netdev_priv(dev);
637 struct gfar __iomem *regs = priv->regs;
640 /* Enable Rx and Tx in MACCFG1 */
641 tempval = gfar_read(®s->maccfg1);
642 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
643 gfar_write(®s->maccfg1, tempval);
645 /* Initialize DMACTRL to have WWR and WOP */
646 tempval = gfar_read(&priv->regs->dmactrl);
647 tempval |= DMACTRL_INIT_SETTINGS;
648 gfar_write(&priv->regs->dmactrl, tempval);
650 /* Make sure we aren't stopped */
651 tempval = gfar_read(&priv->regs->dmactrl);
652 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
653 gfar_write(&priv->regs->dmactrl, tempval);
655 /* Clear THLT/RHLT, so that the DMA starts polling now */
656 gfar_write(®s->tstat, TSTAT_CLEAR_THALT);
657 gfar_write(®s->rstat, RSTAT_CLEAR_RHALT);
659 /* Unmask the interrupts we look for */
660 gfar_write(®s->imask, IMASK_DEFAULT);
663 /* Bring the controller up and running */
664 int startup_gfar(struct net_device *dev)
671 struct gfar_private *priv = netdev_priv(dev);
672 struct gfar __iomem *regs = priv->regs;
677 gfar_write(®s->imask, IMASK_INIT_CLEAR);
679 /* Allocate memory for the buffer descriptors */
680 vaddr = (unsigned long) dma_alloc_coherent(NULL,
681 sizeof (struct txbd8) * priv->tx_ring_size +
682 sizeof (struct rxbd8) * priv->rx_ring_size,
686 if (netif_msg_ifup(priv))
687 printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
692 priv->tx_bd_base = (struct txbd8 *) vaddr;
694 /* enet DMA only understands physical addresses */
695 gfar_write(®s->tbase0, addr);
697 /* Start the rx descriptor ring where the tx ring leaves off */
698 addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
699 vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
700 priv->rx_bd_base = (struct rxbd8 *) vaddr;
701 gfar_write(®s->rbase0, addr);
703 /* Setup the skbuff rings */
705 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
706 priv->tx_ring_size, GFP_KERNEL);
708 if (NULL == priv->tx_skbuff) {
709 if (netif_msg_ifup(priv))
710 printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
716 for (i = 0; i < priv->tx_ring_size; i++)
717 priv->tx_skbuff[i] = NULL;
720 (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
721 priv->rx_ring_size, GFP_KERNEL);
723 if (NULL == priv->rx_skbuff) {
724 if (netif_msg_ifup(priv))
725 printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
731 for (i = 0; i < priv->rx_ring_size; i++)
732 priv->rx_skbuff[i] = NULL;
734 /* Initialize some variables in our dev structure */
735 priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
736 priv->cur_rx = priv->rx_bd_base;
737 priv->skb_curtx = priv->skb_dirtytx = 0;
740 /* Initialize Transmit Descriptor Ring */
741 txbdp = priv->tx_bd_base;
742 for (i = 0; i < priv->tx_ring_size; i++) {
749 /* Set the last descriptor in the ring to indicate wrap */
751 txbdp->status |= TXBD_WRAP;
753 rxbdp = priv->rx_bd_base;
754 for (i = 0; i < priv->rx_ring_size; i++) {
755 struct sk_buff *skb = NULL;
759 skb = gfar_new_skb(dev, rxbdp);
761 priv->rx_skbuff[i] = skb;
766 /* Set the last descriptor in the ring to wrap */
768 rxbdp->status |= RXBD_WRAP;
770 /* If the device has multiple interrupts, register for
771 * them. Otherwise, only register for the one */
772 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
773 /* Install our interrupt handlers for Error,
774 * Transmit, and Receive */
775 if (request_irq(priv->interruptError, gfar_error,
776 0, "enet_error", dev) < 0) {
777 if (netif_msg_intr(priv))
778 printk(KERN_ERR "%s: Can't get IRQ %d\n",
779 dev->name, priv->interruptError);
785 if (request_irq(priv->interruptTransmit, gfar_transmit,
786 0, "enet_tx", dev) < 0) {
787 if (netif_msg_intr(priv))
788 printk(KERN_ERR "%s: Can't get IRQ %d\n",
789 dev->name, priv->interruptTransmit);
796 if (request_irq(priv->interruptReceive, gfar_receive,
797 0, "enet_rx", dev) < 0) {
798 if (netif_msg_intr(priv))
799 printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
800 dev->name, priv->interruptReceive);
806 if (request_irq(priv->interruptTransmit, gfar_interrupt,
807 0, "gfar_interrupt", dev) < 0) {
808 if (netif_msg_intr(priv))
809 printk(KERN_ERR "%s: Can't get IRQ %d\n",
810 dev->name, priv->interruptError);
817 phy_start(priv->phydev);
819 /* Configure the coalescing support */
820 if (priv->txcoalescing)
821 gfar_write(®s->txic,
822 mk_ic_value(priv->txcount, priv->txtime));
824 gfar_write(®s->txic, 0);
826 if (priv->rxcoalescing)
827 gfar_write(®s->rxic,
828 mk_ic_value(priv->rxcount, priv->rxtime));
830 gfar_write(®s->rxic, 0);
832 if (priv->rx_csum_enable)
833 rctrl |= RCTRL_CHECKSUMMING;
835 if (priv->extended_hash) {
836 rctrl |= RCTRL_EXTHASH;
838 gfar_clear_exact_match(dev);
842 if (priv->vlan_enable)
846 rctrl &= ~RCTRL_PAL_MASK;
847 rctrl |= RCTRL_PADDING(priv->padding);
850 /* Init rctrl based on our settings */
851 gfar_write(&priv->regs->rctrl, rctrl);
853 if (dev->features & NETIF_F_IP_CSUM)
854 gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
856 /* Set the extraction length and index */
857 attrs = ATTRELI_EL(priv->rx_stash_size) |
858 ATTRELI_EI(priv->rx_stash_index);
860 gfar_write(&priv->regs->attreli, attrs);
862 /* Start with defaults, and add stashing or locking
863 * depending on the approprate variables */
864 attrs = ATTR_INIT_SETTINGS;
866 if (priv->bd_stash_en)
867 attrs |= ATTR_BDSTASH;
869 if (priv->rx_stash_size != 0)
870 attrs |= ATTR_BUFSTASH;
872 gfar_write(&priv->regs->attr, attrs);
874 gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
875 gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
876 gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
878 /* Start the controller */
884 free_irq(priv->interruptTransmit, dev);
886 free_irq(priv->interruptError, dev);
889 free_skb_resources(priv);
891 dma_free_coherent(NULL,
892 sizeof(struct txbd8)*priv->tx_ring_size
893 + sizeof(struct rxbd8)*priv->rx_ring_size,
895 gfar_read(®s->tbase0));
900 /* Called when something needs to use the ethernet device */
901 /* Returns 0 for success. */
902 static int gfar_enet_open(struct net_device *dev)
906 /* Initialize a bunch of registers */
909 gfar_set_mac_address(dev);
916 err = startup_gfar(dev);
918 netif_start_queue(dev);
923 static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
925 struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
927 memset(fcb, 0, GMAC_FCB_LEN);
932 static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
936 /* If we're here, it's a IP packet with a TCP or UDP
937 * payload. We set it to checksum, using a pseudo-header
940 flags = TXFCB_DEFAULT;
942 /* Tell the controller what the protocol is */
943 /* And provide the already calculated phcs */
944 if (skb->nh.iph->protocol == IPPROTO_UDP) {
946 fcb->phcs = skb->h.uh->check;
948 fcb->phcs = skb->h.th->check;
950 /* l3os is the distance between the start of the
951 * frame (skb->data) and the start of the IP hdr.
952 * l4os is the distance between the start of the
953 * l3 hdr and the l4 hdr */
954 fcb->l3os = (u16)(skb->nh.raw - skb->data - GMAC_FCB_LEN);
955 fcb->l4os = (u16)(skb->h.raw - skb->nh.raw);
960 void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
962 fcb->flags |= TXFCB_VLN;
963 fcb->vlctl = vlan_tx_tag_get(skb);
966 /* This is called by the kernel when a frame is ready for transmission. */
967 /* It is pointed to by the dev->hard_start_xmit function pointer */
968 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
970 struct gfar_private *priv = netdev_priv(dev);
971 struct txfcb *fcb = NULL;
976 /* Update transmit stats */
977 priv->stats.tx_bytes += skb->len;
980 spin_lock_irqsave(&priv->txlock, flags);
982 /* Point at the first free tx descriptor */
983 txbdp = priv->cur_tx;
985 /* Clear all but the WRAP status flags */
986 status = txbdp->status & TXBD_WRAP;
988 /* Set up checksumming */
989 if (likely((dev->features & NETIF_F_IP_CSUM)
990 && (CHECKSUM_PARTIAL == skb->ip_summed))) {
991 fcb = gfar_add_fcb(skb, txbdp);
993 gfar_tx_checksum(skb, fcb);
996 if (priv->vlan_enable &&
997 unlikely(priv->vlgrp && vlan_tx_tag_present(skb))) {
998 if (unlikely(NULL == fcb)) {
999 fcb = gfar_add_fcb(skb, txbdp);
1003 gfar_tx_vlan(skb, fcb);
1006 /* Set buffer length and pointer */
1007 txbdp->length = skb->len;
1008 txbdp->bufPtr = dma_map_single(NULL, skb->data,
1009 skb->len, DMA_TO_DEVICE);
1011 /* Save the skb pointer so we can free it later */
1012 priv->tx_skbuff[priv->skb_curtx] = skb;
1014 /* Update the current skb pointer (wrapping if this was the last) */
1016 (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1018 /* Flag the BD as interrupt-causing */
1019 status |= TXBD_INTERRUPT;
1021 /* Flag the BD as ready to go, last in frame, and */
1022 /* in need of CRC */
1023 status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
1025 dev->trans_start = jiffies;
1027 txbdp->status = status;
1029 /* If this was the last BD in the ring, the next one */
1030 /* is at the beginning of the ring */
1031 if (txbdp->status & TXBD_WRAP)
1032 txbdp = priv->tx_bd_base;
1036 /* If the next BD still needs to be cleaned up, then the bds
1037 are full. We need to tell the kernel to stop sending us stuff. */
1038 if (txbdp == priv->dirty_tx) {
1039 netif_stop_queue(dev);
1041 priv->stats.tx_fifo_errors++;
1044 /* Update the current txbd to the next one */
1045 priv->cur_tx = txbdp;
1047 /* Tell the DMA to go go go */
1048 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1051 spin_unlock_irqrestore(&priv->txlock, flags);
1056 /* Stops the kernel queue, and halts the controller */
1057 static int gfar_close(struct net_device *dev)
1059 struct gfar_private *priv = netdev_priv(dev);
1062 /* Disconnect from the PHY */
1063 phy_disconnect(priv->phydev);
1064 priv->phydev = NULL;
1066 netif_stop_queue(dev);
1071 /* returns a net_device_stats structure pointer */
1072 static struct net_device_stats * gfar_get_stats(struct net_device *dev)
1074 struct gfar_private *priv = netdev_priv(dev);
1076 return &(priv->stats);
1079 /* Changes the mac address if the controller is not running. */
1080 int gfar_set_mac_address(struct net_device *dev)
1082 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
1088 /* Enables and disables VLAN insertion/extraction */
1089 static void gfar_vlan_rx_register(struct net_device *dev,
1090 struct vlan_group *grp)
1092 struct gfar_private *priv = netdev_priv(dev);
1093 unsigned long flags;
1096 spin_lock_irqsave(&priv->rxlock, flags);
1101 /* Enable VLAN tag insertion */
1102 tempval = gfar_read(&priv->regs->tctrl);
1103 tempval |= TCTRL_VLINS;
1105 gfar_write(&priv->regs->tctrl, tempval);
1107 /* Enable VLAN tag extraction */
1108 tempval = gfar_read(&priv->regs->rctrl);
1109 tempval |= RCTRL_VLEX;
1110 gfar_write(&priv->regs->rctrl, tempval);
1112 /* Disable VLAN tag insertion */
1113 tempval = gfar_read(&priv->regs->tctrl);
1114 tempval &= ~TCTRL_VLINS;
1115 gfar_write(&priv->regs->tctrl, tempval);
1117 /* Disable VLAN tag extraction */
1118 tempval = gfar_read(&priv->regs->rctrl);
1119 tempval &= ~RCTRL_VLEX;
1120 gfar_write(&priv->regs->rctrl, tempval);
1123 spin_unlock_irqrestore(&priv->rxlock, flags);
1127 static void gfar_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
1129 struct gfar_private *priv = netdev_priv(dev);
1130 unsigned long flags;
1132 spin_lock_irqsave(&priv->rxlock, flags);
1135 priv->vlgrp->vlan_devices[vid] = NULL;
1137 spin_unlock_irqrestore(&priv->rxlock, flags);
1141 static int gfar_change_mtu(struct net_device *dev, int new_mtu)
1143 int tempsize, tempval;
1144 struct gfar_private *priv = netdev_priv(dev);
1145 int oldsize = priv->rx_buffer_size;
1146 int frame_size = new_mtu + ETH_HLEN;
1148 if (priv->vlan_enable)
1149 frame_size += VLAN_ETH_HLEN;
1151 if (gfar_uses_fcb(priv))
1152 frame_size += GMAC_FCB_LEN;
1154 frame_size += priv->padding;
1156 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
1157 if (netif_msg_drv(priv))
1158 printk(KERN_ERR "%s: Invalid MTU setting\n",
1164 (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
1165 INCREMENTAL_BUFFER_SIZE;
1167 /* Only stop and start the controller if it isn't already
1168 * stopped, and we changed something */
1169 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1172 priv->rx_buffer_size = tempsize;
1176 gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
1177 gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
1179 /* If the mtu is larger than the max size for standard
1180 * ethernet frames (ie, a jumbo frame), then set maccfg2
1181 * to allow huge frames, and to check the length */
1182 tempval = gfar_read(&priv->regs->maccfg2);
1184 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
1185 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1187 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
1189 gfar_write(&priv->regs->maccfg2, tempval);
1191 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
1197 /* gfar_timeout gets called when a packet has not been
1198 * transmitted after a set amount of time.
1199 * For now, assume that clearing out all the structures, and
1200 * starting over will fix the problem. */
1201 static void gfar_timeout(struct net_device *dev)
1203 struct gfar_private *priv = netdev_priv(dev);
1205 priv->stats.tx_errors++;
1207 if (dev->flags & IFF_UP) {
1212 netif_schedule(dev);
1215 /* Interrupt Handler for Transmit complete */
1216 static irqreturn_t gfar_transmit(int irq, void *dev_id)
1218 struct net_device *dev = (struct net_device *) dev_id;
1219 struct gfar_private *priv = netdev_priv(dev);
1223 gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
1226 spin_lock(&priv->txlock);
1227 bdp = priv->dirty_tx;
1228 while ((bdp->status & TXBD_READY) == 0) {
1229 /* If dirty_tx and cur_tx are the same, then either the */
1230 /* ring is empty or full now (it could only be full in the beginning, */
1231 /* obviously). If it is empty, we are done. */
1232 if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
1235 priv->stats.tx_packets++;
1237 /* Deferred means some collisions occurred during transmit, */
1238 /* but we eventually sent the packet. */
1239 if (bdp->status & TXBD_DEF)
1240 priv->stats.collisions++;
1242 /* Free the sk buffer associated with this TxBD */
1243 dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
1244 priv->tx_skbuff[priv->skb_dirtytx] = NULL;
1246 (priv->skb_dirtytx +
1247 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
1249 /* update bdp to point at next bd in the ring (wrapping if necessary) */
1250 if (bdp->status & TXBD_WRAP)
1251 bdp = priv->tx_bd_base;
1255 /* Move dirty_tx to be the next bd */
1256 priv->dirty_tx = bdp;
1258 /* We freed a buffer, so now we can restart transmission */
1259 if (netif_queue_stopped(dev))
1260 netif_wake_queue(dev);
1261 } /* while ((bdp->status & TXBD_READY) == 0) */
1263 /* If we are coalescing the interrupts, reset the timer */
1264 /* Otherwise, clear it */
1265 if (priv->txcoalescing)
1266 gfar_write(&priv->regs->txic,
1267 mk_ic_value(priv->txcount, priv->txtime));
1269 gfar_write(&priv->regs->txic, 0);
1271 spin_unlock(&priv->txlock);
1276 struct sk_buff * gfar_new_skb(struct net_device *dev, struct rxbd8 *bdp)
1278 unsigned int alignamount;
1279 struct gfar_private *priv = netdev_priv(dev);
1280 struct sk_buff *skb = NULL;
1281 unsigned int timeout = SKB_ALLOC_TIMEOUT;
1283 /* We have to allocate the skb, so keep trying till we succeed */
1284 while ((!skb) && timeout--)
1285 skb = dev_alloc_skb(priv->rx_buffer_size + RXBUF_ALIGNMENT);
1290 alignamount = RXBUF_ALIGNMENT -
1291 (((unsigned) skb->data) & (RXBUF_ALIGNMENT - 1));
1293 /* We need the data buffer to be aligned properly. We will reserve
1294 * as many bytes as needed to align the data properly
1296 skb_reserve(skb, alignamount);
1300 bdp->bufPtr = dma_map_single(NULL, skb->data,
1301 priv->rx_buffer_size, DMA_FROM_DEVICE);
1305 /* Mark the buffer empty */
1306 bdp->status |= (RXBD_EMPTY | RXBD_INTERRUPT);
1311 static inline void count_errors(unsigned short status, struct gfar_private *priv)
1313 struct net_device_stats *stats = &priv->stats;
1314 struct gfar_extra_stats *estats = &priv->extra_stats;
1316 /* If the packet was truncated, none of the other errors
1318 if (status & RXBD_TRUNCATED) {
1319 stats->rx_length_errors++;
1325 /* Count the errors, if there were any */
1326 if (status & (RXBD_LARGE | RXBD_SHORT)) {
1327 stats->rx_length_errors++;
1329 if (status & RXBD_LARGE)
1334 if (status & RXBD_NONOCTET) {
1335 stats->rx_frame_errors++;
1336 estats->rx_nonoctet++;
1338 if (status & RXBD_CRCERR) {
1339 estats->rx_crcerr++;
1340 stats->rx_crc_errors++;
1342 if (status & RXBD_OVERRUN) {
1343 estats->rx_overrun++;
1344 stats->rx_crc_errors++;
1348 irqreturn_t gfar_receive(int irq, void *dev_id)
1350 struct net_device *dev = (struct net_device *) dev_id;
1351 struct gfar_private *priv = netdev_priv(dev);
1352 #ifdef CONFIG_GFAR_NAPI
1355 unsigned long flags;
1358 /* Clear IEVENT, so rx interrupt isn't called again
1359 * because of this interrupt */
1360 gfar_write(&priv->regs->ievent, IEVENT_RX_MASK);
1363 #ifdef CONFIG_GFAR_NAPI
1364 if (netif_rx_schedule_prep(dev)) {
1365 tempval = gfar_read(&priv->regs->imask);
1366 tempval &= IMASK_RX_DISABLED;
1367 gfar_write(&priv->regs->imask, tempval);
1369 __netif_rx_schedule(dev);
1371 if (netif_msg_rx_err(priv))
1372 printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
1373 dev->name, gfar_read(&priv->regs->ievent),
1374 gfar_read(&priv->regs->imask));
1378 spin_lock_irqsave(&priv->rxlock, flags);
1379 gfar_clean_rx_ring(dev, priv->rx_ring_size);
1381 /* If we are coalescing interrupts, update the timer */
1382 /* Otherwise, clear it */
1383 if (priv->rxcoalescing)
1384 gfar_write(&priv->regs->rxic,
1385 mk_ic_value(priv->rxcount, priv->rxtime));
1387 gfar_write(&priv->regs->rxic, 0);
1389 spin_unlock_irqrestore(&priv->rxlock, flags);
1395 static inline int gfar_rx_vlan(struct sk_buff *skb,
1396 struct vlan_group *vlgrp, unsigned short vlctl)
1398 #ifdef CONFIG_GFAR_NAPI
1399 return vlan_hwaccel_receive_skb(skb, vlgrp, vlctl);
1401 return vlan_hwaccel_rx(skb, vlgrp, vlctl);
1405 static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
1407 /* If valid headers were found, and valid sums
1408 * were verified, then we tell the kernel that no
1409 * checksumming is necessary. Otherwise, it is */
1410 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
1411 skb->ip_summed = CHECKSUM_UNNECESSARY;
1413 skb->ip_summed = CHECKSUM_NONE;
1417 static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
1419 struct rxfcb *fcb = (struct rxfcb *)skb->data;
1421 /* Remove the FCB from the skb */
1422 skb_pull(skb, GMAC_FCB_LEN);
1427 /* gfar_process_frame() -- handle one incoming packet if skb
1429 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
1432 struct gfar_private *priv = netdev_priv(dev);
1433 struct rxfcb *fcb = NULL;
1436 if (netif_msg_rx_err(priv))
1437 printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
1438 priv->stats.rx_dropped++;
1439 priv->extra_stats.rx_skbmissing++;
1443 /* Prep the skb for the packet */
1444 skb_put(skb, length);
1446 /* Grab the FCB if there is one */
1447 if (gfar_uses_fcb(priv))
1448 fcb = gfar_get_fcb(skb);
1450 /* Remove the padded bytes, if there are any */
1452 skb_pull(skb, priv->padding);
1454 if (priv->rx_csum_enable)
1455 gfar_rx_checksum(skb, fcb);
1457 /* Tell the skb what kind of packet this is */
1458 skb->protocol = eth_type_trans(skb, dev);
1460 /* Send the packet up the stack */
1461 if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
1462 ret = gfar_rx_vlan(skb, priv->vlgrp, fcb->vlctl);
1466 if (NET_RX_DROP == ret)
1467 priv->extra_stats.kernel_dropped++;
1473 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
1474 * until the budget/quota has been reached. Returns the number
1477 int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
1480 struct sk_buff *skb;
1483 struct gfar_private *priv = netdev_priv(dev);
1485 /* Get the first full descriptor */
1488 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
1489 skb = priv->rx_skbuff[priv->skb_currx];
1492 (RXBD_LARGE | RXBD_SHORT | RXBD_NONOCTET
1493 | RXBD_CRCERR | RXBD_OVERRUN | RXBD_TRUNCATED))) {
1494 /* Increment the number of packets */
1495 priv->stats.rx_packets++;
1498 /* Remove the FCS from the packet length */
1499 pkt_len = bdp->length - 4;
1501 gfar_process_frame(dev, skb, pkt_len);
1503 priv->stats.rx_bytes += pkt_len;
1505 count_errors(bdp->status, priv);
1508 dev_kfree_skb_any(skb);
1510 priv->rx_skbuff[priv->skb_currx] = NULL;
1513 dev->last_rx = jiffies;
1515 /* Clear the status flags for this buffer */
1516 bdp->status &= ~RXBD_STATS;
1518 /* Add another skb for the future */
1519 skb = gfar_new_skb(dev, bdp);
1520 priv->rx_skbuff[priv->skb_currx] = skb;
1522 /* Update to the next pointer */
1523 if (bdp->status & RXBD_WRAP)
1524 bdp = priv->rx_bd_base;
1528 /* update to point at the next skb */
1531 1) & RX_RING_MOD_MASK(priv->rx_ring_size);
1535 /* Update the current rxbd pointer to be the next one */
1541 #ifdef CONFIG_GFAR_NAPI
1542 static int gfar_poll(struct net_device *dev, int *budget)
1545 struct gfar_private *priv = netdev_priv(dev);
1546 int rx_work_limit = *budget;
1548 if (rx_work_limit > dev->quota)
1549 rx_work_limit = dev->quota;
1551 howmany = gfar_clean_rx_ring(dev, rx_work_limit);
1553 dev->quota -= howmany;
1554 rx_work_limit -= howmany;
1557 if (rx_work_limit > 0) {
1558 netif_rx_complete(dev);
1560 /* Clear the halt bit in RSTAT */
1561 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1563 gfar_write(&priv->regs->imask, IMASK_DEFAULT);
1565 /* If we are coalescing interrupts, update the timer */
1566 /* Otherwise, clear it */
1567 if (priv->rxcoalescing)
1568 gfar_write(&priv->regs->rxic,
1569 mk_ic_value(priv->rxcount, priv->rxtime));
1571 gfar_write(&priv->regs->rxic, 0);
1574 /* Return 1 if there's more work to do */
1575 return (rx_work_limit > 0) ? 0 : 1;
1579 #ifdef CONFIG_NET_POLL_CONTROLLER
1581 * Polling 'interrupt' - used by things like netconsole to send skbs
1582 * without having to re-enable interrupts. It's not called while
1583 * the interrupt routine is executing.
1585 static void gfar_netpoll(struct net_device *dev)
1587 struct gfar_private *priv = netdev_priv(dev);
1589 /* If the device has multiple interrupts, run tx/rx */
1590 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1591 disable_irq(priv->interruptTransmit);
1592 disable_irq(priv->interruptReceive);
1593 disable_irq(priv->interruptError);
1594 gfar_interrupt(priv->interruptTransmit, dev);
1595 enable_irq(priv->interruptError);
1596 enable_irq(priv->interruptReceive);
1597 enable_irq(priv->interruptTransmit);
1599 disable_irq(priv->interruptTransmit);
1600 gfar_interrupt(priv->interruptTransmit, dev);
1601 enable_irq(priv->interruptTransmit);
1606 /* The interrupt handler for devices with one interrupt */
1607 static irqreturn_t gfar_interrupt(int irq, void *dev_id)
1609 struct net_device *dev = dev_id;
1610 struct gfar_private *priv = netdev_priv(dev);
1612 /* Save ievent for future reference */
1613 u32 events = gfar_read(&priv->regs->ievent);
1616 gfar_write(&priv->regs->ievent, events);
1618 /* Check for reception */
1619 if ((events & IEVENT_RXF0) || (events & IEVENT_RXB0))
1620 gfar_receive(irq, dev_id);
1622 /* Check for transmit completion */
1623 if ((events & IEVENT_TXF) || (events & IEVENT_TXB))
1624 gfar_transmit(irq, dev_id);
1626 /* Update error statistics */
1627 if (events & IEVENT_TXE) {
1628 priv->stats.tx_errors++;
1630 if (events & IEVENT_LC)
1631 priv->stats.tx_window_errors++;
1632 if (events & IEVENT_CRL)
1633 priv->stats.tx_aborted_errors++;
1634 if (events & IEVENT_XFUN) {
1635 if (netif_msg_tx_err(priv))
1636 printk(KERN_WARNING "%s: tx underrun. dropped packet\n", dev->name);
1637 priv->stats.tx_dropped++;
1638 priv->extra_stats.tx_underrun++;
1640 /* Reactivate the Tx Queues */
1641 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1644 if (events & IEVENT_BSY) {
1645 priv->stats.rx_errors++;
1646 priv->extra_stats.rx_bsy++;
1648 gfar_receive(irq, dev_id);
1650 #ifndef CONFIG_GFAR_NAPI
1651 /* Clear the halt bit in RSTAT */
1652 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1655 if (netif_msg_rx_err(priv))
1656 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n",
1658 gfar_read(&priv->regs->rstat));
1660 if (events & IEVENT_BABR) {
1661 priv->stats.rx_errors++;
1662 priv->extra_stats.rx_babr++;
1664 if (netif_msg_rx_err(priv))
1665 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
1667 if (events & IEVENT_EBERR) {
1668 priv->extra_stats.eberr++;
1669 if (netif_msg_rx_err(priv))
1670 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
1672 if ((events & IEVENT_RXC) && (netif_msg_rx_err(priv)))
1673 printk(KERN_DEBUG "%s: control frame\n", dev->name);
1675 if (events & IEVENT_BABT) {
1676 priv->extra_stats.tx_babt++;
1677 if (netif_msg_rx_err(priv))
1678 printk(KERN_DEBUG "%s: babt error\n", dev->name);
1684 /* Called every time the controller might need to be made
1685 * aware of new link state. The PHY code conveys this
1686 * information through variables in the phydev structure, and this
1687 * function converts those variables into the appropriate
1688 * register values, and can bring down the device if needed.
1690 static void adjust_link(struct net_device *dev)
1692 struct gfar_private *priv = netdev_priv(dev);
1693 struct gfar __iomem *regs = priv->regs;
1694 unsigned long flags;
1695 struct phy_device *phydev = priv->phydev;
1698 spin_lock_irqsave(&priv->txlock, flags);
1700 u32 tempval = gfar_read(®s->maccfg2);
1701 u32 ecntrl = gfar_read(®s->ecntrl);
1703 /* Now we make sure that we can be in full duplex mode.
1704 * If not, we operate in half-duplex mode. */
1705 if (phydev->duplex != priv->oldduplex) {
1707 if (!(phydev->duplex))
1708 tempval &= ~(MACCFG2_FULL_DUPLEX);
1710 tempval |= MACCFG2_FULL_DUPLEX;
1712 priv->oldduplex = phydev->duplex;
1715 if (phydev->speed != priv->oldspeed) {
1717 switch (phydev->speed) {
1720 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
1725 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
1727 /* Reduced mode distinguishes
1728 * between 10 and 100 */
1729 if (phydev->speed == SPEED_100)
1730 ecntrl |= ECNTRL_R100;
1732 ecntrl &= ~(ECNTRL_R100);
1735 if (netif_msg_link(priv))
1737 "%s: Ack! Speed (%d) is not 10/100/1000!\n",
1738 dev->name, phydev->speed);
1742 priv->oldspeed = phydev->speed;
1745 gfar_write(®s->maccfg2, tempval);
1746 gfar_write(®s->ecntrl, ecntrl);
1748 if (!priv->oldlink) {
1751 netif_schedule(dev);
1753 } else if (priv->oldlink) {
1757 priv->oldduplex = -1;
1760 if (new_state && netif_msg_link(priv))
1761 phy_print_status(phydev);
1763 spin_unlock_irqrestore(&priv->txlock, flags);
1766 /* Update the hash table based on the current list of multicast
1767 * addresses we subscribe to. Also, change the promiscuity of
1768 * the device based on the flags (this function is called
1769 * whenever dev->flags is changed */
1770 static void gfar_set_multi(struct net_device *dev)
1772 struct dev_mc_list *mc_ptr;
1773 struct gfar_private *priv = netdev_priv(dev);
1774 struct gfar __iomem *regs = priv->regs;
1777 if(dev->flags & IFF_PROMISC) {
1778 /* Set RCTRL to PROM */
1779 tempval = gfar_read(®s->rctrl);
1780 tempval |= RCTRL_PROM;
1781 gfar_write(®s->rctrl, tempval);
1783 /* Set RCTRL to not PROM */
1784 tempval = gfar_read(®s->rctrl);
1785 tempval &= ~(RCTRL_PROM);
1786 gfar_write(®s->rctrl, tempval);
1789 if(dev->flags & IFF_ALLMULTI) {
1790 /* Set the hash to rx all multicast frames */
1791 gfar_write(®s->igaddr0, 0xffffffff);
1792 gfar_write(®s->igaddr1, 0xffffffff);
1793 gfar_write(®s->igaddr2, 0xffffffff);
1794 gfar_write(®s->igaddr3, 0xffffffff);
1795 gfar_write(®s->igaddr4, 0xffffffff);
1796 gfar_write(®s->igaddr5, 0xffffffff);
1797 gfar_write(®s->igaddr6, 0xffffffff);
1798 gfar_write(®s->igaddr7, 0xffffffff);
1799 gfar_write(®s->gaddr0, 0xffffffff);
1800 gfar_write(®s->gaddr1, 0xffffffff);
1801 gfar_write(®s->gaddr2, 0xffffffff);
1802 gfar_write(®s->gaddr3, 0xffffffff);
1803 gfar_write(®s->gaddr4, 0xffffffff);
1804 gfar_write(®s->gaddr5, 0xffffffff);
1805 gfar_write(®s->gaddr6, 0xffffffff);
1806 gfar_write(®s->gaddr7, 0xffffffff);
1811 /* zero out the hash */
1812 gfar_write(®s->igaddr0, 0x0);
1813 gfar_write(®s->igaddr1, 0x0);
1814 gfar_write(®s->igaddr2, 0x0);
1815 gfar_write(®s->igaddr3, 0x0);
1816 gfar_write(®s->igaddr4, 0x0);
1817 gfar_write(®s->igaddr5, 0x0);
1818 gfar_write(®s->igaddr6, 0x0);
1819 gfar_write(®s->igaddr7, 0x0);
1820 gfar_write(®s->gaddr0, 0x0);
1821 gfar_write(®s->gaddr1, 0x0);
1822 gfar_write(®s->gaddr2, 0x0);
1823 gfar_write(®s->gaddr3, 0x0);
1824 gfar_write(®s->gaddr4, 0x0);
1825 gfar_write(®s->gaddr5, 0x0);
1826 gfar_write(®s->gaddr6, 0x0);
1827 gfar_write(®s->gaddr7, 0x0);
1829 /* If we have extended hash tables, we need to
1830 * clear the exact match registers to prepare for
1832 if (priv->extended_hash) {
1833 em_num = GFAR_EM_NUM + 1;
1834 gfar_clear_exact_match(dev);
1841 if(dev->mc_count == 0)
1844 /* Parse the list, and set the appropriate bits */
1845 for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
1847 gfar_set_mac_for_addr(dev, idx,
1851 gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
1859 /* Clears each of the exact match registers to zero, so they
1860 * don't interfere with normal reception */
1861 static void gfar_clear_exact_match(struct net_device *dev)
1864 u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
1866 for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
1867 gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
1870 /* Set the appropriate hash bit for the given addr */
1871 /* The algorithm works like so:
1872 * 1) Take the Destination Address (ie the multicast address), and
1873 * do a CRC on it (little endian), and reverse the bits of the
1875 * 2) Use the 8 most significant bits as a hash into a 256-entry
1876 * table. The table is controlled through 8 32-bit registers:
1877 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1878 * gaddr7. This means that the 3 most significant bits in the
1879 * hash index which gaddr register to use, and the 5 other bits
1880 * indicate which bit (assuming an IBM numbering scheme, which
1881 * for PowerPC (tm) is usually the case) in the register holds
1883 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
1886 struct gfar_private *priv = netdev_priv(dev);
1887 u32 result = ether_crc(MAC_ADDR_LEN, addr);
1888 int width = priv->hash_width;
1889 u8 whichbit = (result >> (32 - width)) & 0x1f;
1890 u8 whichreg = result >> (32 - width + 5);
1891 u32 value = (1 << (31-whichbit));
1893 tempval = gfar_read(priv->hash_regs[whichreg]);
1895 gfar_write(priv->hash_regs[whichreg], tempval);
1901 /* There are multiple MAC Address register pairs on some controllers
1902 * This function sets the numth pair to a given address
1904 static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
1906 struct gfar_private *priv = netdev_priv(dev);
1908 char tmpbuf[MAC_ADDR_LEN];
1910 u32 __iomem *macptr = &priv->regs->macstnaddr1;
1914 /* Now copy it into the mac registers backwards, cuz */
1915 /* little endian is silly */
1916 for (idx = 0; idx < MAC_ADDR_LEN; idx++)
1917 tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
1919 gfar_write(macptr, *((u32 *) (tmpbuf)));
1921 tempval = *((u32 *) (tmpbuf + 4));
1923 gfar_write(macptr+1, tempval);
1926 /* GFAR error interrupt handler */
1927 static irqreturn_t gfar_error(int irq, void *dev_id)
1929 struct net_device *dev = dev_id;
1930 struct gfar_private *priv = netdev_priv(dev);
1932 /* Save ievent for future reference */
1933 u32 events = gfar_read(&priv->regs->ievent);
1936 gfar_write(&priv->regs->ievent, IEVENT_ERR_MASK);
1939 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
1940 printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
1941 dev->name, events, gfar_read(&priv->regs->imask));
1943 /* Update the error counters */
1944 if (events & IEVENT_TXE) {
1945 priv->stats.tx_errors++;
1947 if (events & IEVENT_LC)
1948 priv->stats.tx_window_errors++;
1949 if (events & IEVENT_CRL)
1950 priv->stats.tx_aborted_errors++;
1951 if (events & IEVENT_XFUN) {
1952 if (netif_msg_tx_err(priv))
1953 printk(KERN_DEBUG "%s: underrun. packet dropped.\n",
1955 priv->stats.tx_dropped++;
1956 priv->extra_stats.tx_underrun++;
1958 /* Reactivate the Tx Queues */
1959 gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
1961 if (netif_msg_tx_err(priv))
1962 printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
1964 if (events & IEVENT_BSY) {
1965 priv->stats.rx_errors++;
1966 priv->extra_stats.rx_bsy++;
1968 gfar_receive(irq, dev_id);
1970 #ifndef CONFIG_GFAR_NAPI
1971 /* Clear the halt bit in RSTAT */
1972 gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
1975 if (netif_msg_rx_err(priv))
1976 printk(KERN_DEBUG "%s: busy error (rhalt: %x)\n",
1978 gfar_read(&priv->regs->rstat));
1980 if (events & IEVENT_BABR) {
1981 priv->stats.rx_errors++;
1982 priv->extra_stats.rx_babr++;
1984 if (netif_msg_rx_err(priv))
1985 printk(KERN_DEBUG "%s: babbling error\n", dev->name);
1987 if (events & IEVENT_EBERR) {
1988 priv->extra_stats.eberr++;
1989 if (netif_msg_rx_err(priv))
1990 printk(KERN_DEBUG "%s: EBERR\n", dev->name);
1992 if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
1993 if (netif_msg_rx_status(priv))
1994 printk(KERN_DEBUG "%s: control frame\n", dev->name);
1996 if (events & IEVENT_BABT) {
1997 priv->extra_stats.tx_babt++;
1998 if (netif_msg_tx_err(priv))
1999 printk(KERN_DEBUG "%s: babt error\n", dev->name);
2004 /* Structure for a device driver */
2005 static struct platform_driver gfar_driver = {
2006 .probe = gfar_probe,
2007 .remove = gfar_remove,
2009 .name = "fsl-gianfar",
2013 static int __init gfar_init(void)
2015 int err = gfar_mdio_init();
2020 err = platform_driver_register(&gfar_driver);
2028 static void __exit gfar_exit(void)
2030 platform_driver_unregister(&gfar_driver);
2034 module_init(gfar_init);
2035 module_exit(gfar_exit);