1 /* drivers/net/ethernet/freescale/gianfar.c
3 * Gianfar Ethernet Driver
4 * This driver is designed for the non-CPM ethernet controllers
5 * on the 85xx and 83xx family of integrated processors
6 * Based on 8260_io/fcc_enet.c
9 * Maintainer: Kumar Gala
10 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
12 * Copyright 2002-2009, 2011 Freescale Semiconductor, Inc.
13 * Copyright 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 of_device. Configuration information
29 * is therefore conveyed through an OF-style device tree.
31 * The Gianfar Ethernet Controller uses a ring of buffer
32 * descriptors. The beginning is indicated by a register
33 * pointing to the physical address of the start of the ring.
34 * The end is determined by a "wrap" bit being set in the
35 * last descriptor of the ring.
37 * When a packet is received, the RXF bit in the
38 * IEVENT register is set, triggering an interrupt when the
39 * corresponding bit in the IMASK register is also set (if
40 * interrupt coalescing is active, then the interrupt may not
41 * happen immediately, but will wait until either a set number
42 * of frames or amount of time have passed). In NAPI, the
43 * interrupt handler will signal there is work to be done, and
44 * exit. This method will start at the last known empty
45 * descriptor, and process every subsequent descriptor until there
46 * are none left with data (NAPI will stop after a set number of
47 * packets to give time to other tasks, but will eventually
48 * process all the packets). The data arrives inside a
49 * pre-allocated skb, and so after the skb is passed up to the
50 * stack, a new skb must be allocated, and the address field in
51 * the buffer descriptor must be updated to indicate this new
54 * When the kernel requests that a packet be transmitted, the
55 * driver starts where it left off last time, and points the
56 * descriptor at the buffer which was passed in. The driver
57 * then informs the DMA engine that there are packets ready to
58 * be transmitted. Once the controller is finished transmitting
59 * the packet, an interrupt may be triggered (under the same
60 * conditions as for reception, but depending on the TXF bit).
61 * The driver then cleans up the buffer.
64 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/of_mdio.h>
82 #include <linux/of_platform.h>
84 #include <linux/tcp.h>
85 #include <linux/udp.h>
87 #include <linux/net_tstamp.h>
92 #include <asm/uaccess.h>
93 #include <linux/module.h>
94 #include <linux/dma-mapping.h>
95 #include <linux/crc32.h>
96 #include <linux/mii.h>
97 #include <linux/phy.h>
98 #include <linux/phy_fixed.h>
100 #include <linux/of_net.h>
104 #define TX_TIMEOUT (1*HZ)
106 const char gfar_driver_version[] = "1.3";
108 static int gfar_enet_open(struct net_device *dev);
109 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
110 static void gfar_reset_task(struct work_struct *work);
111 static void gfar_timeout(struct net_device *dev);
112 static int gfar_close(struct net_device *dev);
113 struct sk_buff *gfar_new_skb(struct net_device *dev);
114 static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
115 struct sk_buff *skb);
116 static int gfar_set_mac_address(struct net_device *dev);
117 static int gfar_change_mtu(struct net_device *dev, int new_mtu);
118 static irqreturn_t gfar_error(int irq, void *dev_id);
119 static irqreturn_t gfar_transmit(int irq, void *dev_id);
120 static irqreturn_t gfar_interrupt(int irq, void *dev_id);
121 static void adjust_link(struct net_device *dev);
122 static void init_registers(struct net_device *dev);
123 static int init_phy(struct net_device *dev);
124 static int gfar_probe(struct platform_device *ofdev);
125 static int gfar_remove(struct platform_device *ofdev);
126 static void free_skb_resources(struct gfar_private *priv);
127 static void gfar_set_multi(struct net_device *dev);
128 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
129 static void gfar_configure_serdes(struct net_device *dev);
130 static int gfar_poll(struct napi_struct *napi, int budget);
131 #ifdef CONFIG_NET_POLL_CONTROLLER
132 static void gfar_netpoll(struct net_device *dev);
134 int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
135 static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
136 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
137 int amount_pull, struct napi_struct *napi);
138 void gfar_halt(struct net_device *dev);
139 static void gfar_halt_nodisable(struct net_device *dev);
140 void gfar_start(struct net_device *dev);
141 static void gfar_clear_exact_match(struct net_device *dev);
142 static void gfar_set_mac_for_addr(struct net_device *dev, int num,
144 static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
146 MODULE_AUTHOR("Freescale Semiconductor, Inc");
147 MODULE_DESCRIPTION("Gianfar Ethernet Driver");
148 MODULE_LICENSE("GPL");
150 static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
157 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
158 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
159 lstatus |= BD_LFLAG(RXBD_WRAP);
163 bdp->lstatus = lstatus;
166 static int gfar_init_bds(struct net_device *ndev)
168 struct gfar_private *priv = netdev_priv(ndev);
169 struct gfar_priv_tx_q *tx_queue = NULL;
170 struct gfar_priv_rx_q *rx_queue = NULL;
175 for (i = 0; i < priv->num_tx_queues; i++) {
176 tx_queue = priv->tx_queue[i];
177 /* Initialize some variables in our dev structure */
178 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
179 tx_queue->dirty_tx = tx_queue->tx_bd_base;
180 tx_queue->cur_tx = tx_queue->tx_bd_base;
181 tx_queue->skb_curtx = 0;
182 tx_queue->skb_dirtytx = 0;
184 /* Initialize Transmit Descriptor Ring */
185 txbdp = tx_queue->tx_bd_base;
186 for (j = 0; j < tx_queue->tx_ring_size; j++) {
192 /* Set the last descriptor in the ring to indicate wrap */
194 txbdp->status |= TXBD_WRAP;
197 for (i = 0; i < priv->num_rx_queues; i++) {
198 rx_queue = priv->rx_queue[i];
199 rx_queue->cur_rx = rx_queue->rx_bd_base;
200 rx_queue->skb_currx = 0;
201 rxbdp = rx_queue->rx_bd_base;
203 for (j = 0; j < rx_queue->rx_ring_size; j++) {
204 struct sk_buff *skb = rx_queue->rx_skbuff[j];
207 gfar_init_rxbdp(rx_queue, rxbdp,
210 skb = gfar_new_skb(ndev);
212 netdev_err(ndev, "Can't allocate RX buffers\n");
215 rx_queue->rx_skbuff[j] = skb;
217 gfar_new_rxbdp(rx_queue, rxbdp, skb);
228 static int gfar_alloc_skb_resources(struct net_device *ndev)
233 struct gfar_private *priv = netdev_priv(ndev);
234 struct device *dev = &priv->ofdev->dev;
235 struct gfar_priv_tx_q *tx_queue = NULL;
236 struct gfar_priv_rx_q *rx_queue = NULL;
238 priv->total_tx_ring_size = 0;
239 for (i = 0; i < priv->num_tx_queues; i++)
240 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
242 priv->total_rx_ring_size = 0;
243 for (i = 0; i < priv->num_rx_queues; i++)
244 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
246 /* Allocate memory for the buffer descriptors */
247 vaddr = dma_alloc_coherent(dev,
248 sizeof(struct txbd8) * priv->total_tx_ring_size +
249 sizeof(struct rxbd8) * priv->total_rx_ring_size,
252 netif_err(priv, ifup, ndev,
253 "Could not allocate buffer descriptors!\n");
257 for (i = 0; i < priv->num_tx_queues; i++) {
258 tx_queue = priv->tx_queue[i];
259 tx_queue->tx_bd_base = vaddr;
260 tx_queue->tx_bd_dma_base = addr;
261 tx_queue->dev = ndev;
262 /* enet DMA only understands physical addresses */
263 addr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
264 vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
267 /* Start the rx descriptor ring where the tx ring leaves off */
268 for (i = 0; i < priv->num_rx_queues; i++) {
269 rx_queue = priv->rx_queue[i];
270 rx_queue->rx_bd_base = vaddr;
271 rx_queue->rx_bd_dma_base = addr;
272 rx_queue->dev = ndev;
273 addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
274 vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
277 /* Setup the skbuff rings */
278 for (i = 0; i < priv->num_tx_queues; i++) {
279 tx_queue = priv->tx_queue[i];
280 tx_queue->tx_skbuff = kmalloc(sizeof(*tx_queue->tx_skbuff) *
281 tx_queue->tx_ring_size,
283 if (!tx_queue->tx_skbuff) {
284 netif_err(priv, ifup, ndev,
285 "Could not allocate tx_skbuff\n");
289 for (k = 0; k < tx_queue->tx_ring_size; k++)
290 tx_queue->tx_skbuff[k] = NULL;
293 for (i = 0; i < priv->num_rx_queues; i++) {
294 rx_queue = priv->rx_queue[i];
295 rx_queue->rx_skbuff = kmalloc(sizeof(*rx_queue->rx_skbuff) *
296 rx_queue->rx_ring_size,
299 if (!rx_queue->rx_skbuff) {
300 netif_err(priv, ifup, ndev,
301 "Could not allocate rx_skbuff\n");
305 for (j = 0; j < rx_queue->rx_ring_size; j++)
306 rx_queue->rx_skbuff[j] = NULL;
309 if (gfar_init_bds(ndev))
315 free_skb_resources(priv);
319 static void gfar_init_tx_rx_base(struct gfar_private *priv)
321 struct gfar __iomem *regs = priv->gfargrp[0].regs;
325 baddr = ®s->tbase0;
326 for (i = 0; i < priv->num_tx_queues; i++) {
327 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
331 baddr = ®s->rbase0;
332 for (i = 0; i < priv->num_rx_queues; i++) {
333 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
338 static void gfar_init_mac(struct net_device *ndev)
340 struct gfar_private *priv = netdev_priv(ndev);
341 struct gfar __iomem *regs = priv->gfargrp[0].regs;
346 /* write the tx/rx base registers */
347 gfar_init_tx_rx_base(priv);
349 /* Configure the coalescing support */
350 gfar_configure_coalescing(priv, 0xFF, 0xFF);
352 if (priv->rx_filer_enable) {
353 rctrl |= RCTRL_FILREN;
354 /* Program the RIR0 reg with the required distribution */
355 gfar_write(®s->rir0, DEFAULT_RIR0);
358 /* Restore PROMISC mode */
359 if (ndev->flags & IFF_PROMISC)
362 if (ndev->features & NETIF_F_RXCSUM)
363 rctrl |= RCTRL_CHECKSUMMING;
365 if (priv->extended_hash) {
366 rctrl |= RCTRL_EXTHASH;
368 gfar_clear_exact_match(ndev);
373 rctrl &= ~RCTRL_PAL_MASK;
374 rctrl |= RCTRL_PADDING(priv->padding);
377 /* Insert receive time stamps into padding alignment bytes */
378 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER) {
379 rctrl &= ~RCTRL_PAL_MASK;
380 rctrl |= RCTRL_PADDING(8);
384 /* Enable HW time stamping if requested from user space */
385 if (priv->hwts_rx_en)
386 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
388 if (ndev->features & NETIF_F_HW_VLAN_RX)
389 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
391 /* Init rctrl based on our settings */
392 gfar_write(®s->rctrl, rctrl);
394 if (ndev->features & NETIF_F_IP_CSUM)
395 tctrl |= TCTRL_INIT_CSUM;
397 if (priv->prio_sched_en)
398 tctrl |= TCTRL_TXSCHED_PRIO;
400 tctrl |= TCTRL_TXSCHED_WRRS;
401 gfar_write(®s->tr03wt, DEFAULT_WRRS_WEIGHT);
402 gfar_write(®s->tr47wt, DEFAULT_WRRS_WEIGHT);
405 gfar_write(®s->tctrl, tctrl);
407 /* Set the extraction length and index */
408 attrs = ATTRELI_EL(priv->rx_stash_size) |
409 ATTRELI_EI(priv->rx_stash_index);
411 gfar_write(®s->attreli, attrs);
413 /* Start with defaults, and add stashing or locking
414 * depending on the approprate variables
416 attrs = ATTR_INIT_SETTINGS;
418 if (priv->bd_stash_en)
419 attrs |= ATTR_BDSTASH;
421 if (priv->rx_stash_size != 0)
422 attrs |= ATTR_BUFSTASH;
424 gfar_write(®s->attr, attrs);
426 gfar_write(®s->fifo_tx_thr, priv->fifo_threshold);
427 gfar_write(®s->fifo_tx_starve, priv->fifo_starve);
428 gfar_write(®s->fifo_tx_starve_shutoff, priv->fifo_starve_off);
431 static struct net_device_stats *gfar_get_stats(struct net_device *dev)
433 struct gfar_private *priv = netdev_priv(dev);
434 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
435 unsigned long tx_packets = 0, tx_bytes = 0;
438 for (i = 0; i < priv->num_rx_queues; i++) {
439 rx_packets += priv->rx_queue[i]->stats.rx_packets;
440 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
441 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
444 dev->stats.rx_packets = rx_packets;
445 dev->stats.rx_bytes = rx_bytes;
446 dev->stats.rx_dropped = rx_dropped;
448 for (i = 0; i < priv->num_tx_queues; i++) {
449 tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
450 tx_packets += priv->tx_queue[i]->stats.tx_packets;
453 dev->stats.tx_bytes = tx_bytes;
454 dev->stats.tx_packets = tx_packets;
459 static const struct net_device_ops gfar_netdev_ops = {
460 .ndo_open = gfar_enet_open,
461 .ndo_start_xmit = gfar_start_xmit,
462 .ndo_stop = gfar_close,
463 .ndo_change_mtu = gfar_change_mtu,
464 .ndo_set_features = gfar_set_features,
465 .ndo_set_rx_mode = gfar_set_multi,
466 .ndo_tx_timeout = gfar_timeout,
467 .ndo_do_ioctl = gfar_ioctl,
468 .ndo_get_stats = gfar_get_stats,
469 .ndo_set_mac_address = eth_mac_addr,
470 .ndo_validate_addr = eth_validate_addr,
471 #ifdef CONFIG_NET_POLL_CONTROLLER
472 .ndo_poll_controller = gfar_netpoll,
476 void lock_rx_qs(struct gfar_private *priv)
480 for (i = 0; i < priv->num_rx_queues; i++)
481 spin_lock(&priv->rx_queue[i]->rxlock);
484 void lock_tx_qs(struct gfar_private *priv)
488 for (i = 0; i < priv->num_tx_queues; i++)
489 spin_lock(&priv->tx_queue[i]->txlock);
492 void unlock_rx_qs(struct gfar_private *priv)
496 for (i = 0; i < priv->num_rx_queues; i++)
497 spin_unlock(&priv->rx_queue[i]->rxlock);
500 void unlock_tx_qs(struct gfar_private *priv)
504 for (i = 0; i < priv->num_tx_queues; i++)
505 spin_unlock(&priv->tx_queue[i]->txlock);
508 static bool gfar_is_vlan_on(struct gfar_private *priv)
510 return (priv->ndev->features & NETIF_F_HW_VLAN_RX) ||
511 (priv->ndev->features & NETIF_F_HW_VLAN_TX);
514 /* Returns 1 if incoming frames use an FCB */
515 static inline int gfar_uses_fcb(struct gfar_private *priv)
517 return gfar_is_vlan_on(priv) ||
518 (priv->ndev->features & NETIF_F_RXCSUM) ||
519 (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER);
522 static void free_tx_pointers(struct gfar_private *priv)
526 for (i = 0; i < priv->num_tx_queues; i++)
527 kfree(priv->tx_queue[i]);
530 static void free_rx_pointers(struct gfar_private *priv)
534 for (i = 0; i < priv->num_rx_queues; i++)
535 kfree(priv->rx_queue[i]);
538 static void unmap_group_regs(struct gfar_private *priv)
542 for (i = 0; i < MAXGROUPS; i++)
543 if (priv->gfargrp[i].regs)
544 iounmap(priv->gfargrp[i].regs);
547 static void free_gfar_dev(struct gfar_private *priv)
551 for (i = 0; i < priv->num_grps; i++)
552 for (j = 0; j < GFAR_NUM_IRQS; j++) {
553 kfree(priv->gfargrp[i].irqinfo[j]);
554 priv->gfargrp[i].irqinfo[j] = NULL;
557 free_netdev(priv->ndev);
560 static void disable_napi(struct gfar_private *priv)
564 for (i = 0; i < priv->num_grps; i++)
565 napi_disable(&priv->gfargrp[i].napi);
568 static void enable_napi(struct gfar_private *priv)
572 for (i = 0; i < priv->num_grps; i++)
573 napi_enable(&priv->gfargrp[i].napi);
576 static int gfar_parse_group(struct device_node *np,
577 struct gfar_private *priv, const char *model)
579 struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
583 for (i = 0; i < GFAR_NUM_IRQS; i++) {
584 grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
586 if (!grp->irqinfo[i])
590 grp->regs = of_iomap(np, 0);
594 gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
596 /* If we aren't the FEC we have multiple interrupts */
597 if (model && strcasecmp(model, "FEC")) {
598 gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
599 gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
600 if (gfar_irq(grp, TX)->irq == NO_IRQ ||
601 gfar_irq(grp, RX)->irq == NO_IRQ ||
602 gfar_irq(grp, ER)->irq == NO_IRQ)
606 grp->grp_id = priv->num_grps;
608 spin_lock_init(&grp->grplock);
609 if (priv->mode == MQ_MG_MODE) {
610 queue_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL);
611 grp->rx_bit_map = queue_mask ?
612 *queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
613 queue_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL);
614 grp->tx_bit_map = queue_mask ?
615 *queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
617 grp->rx_bit_map = 0xFF;
618 grp->tx_bit_map = 0xFF;
625 static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
629 const void *mac_addr;
631 struct net_device *dev = NULL;
632 struct gfar_private *priv = NULL;
633 struct device_node *np = ofdev->dev.of_node;
634 struct device_node *child = NULL;
636 const u32 *stash_len;
637 const u32 *stash_idx;
638 unsigned int num_tx_qs, num_rx_qs;
639 u32 *tx_queues, *rx_queues;
641 if (!np || !of_device_is_available(np))
644 /* parse the num of tx and rx queues */
645 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
646 num_tx_qs = tx_queues ? *tx_queues : 1;
648 if (num_tx_qs > MAX_TX_QS) {
649 pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
650 num_tx_qs, MAX_TX_QS);
651 pr_err("Cannot do alloc_etherdev, aborting\n");
655 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
656 num_rx_qs = rx_queues ? *rx_queues : 1;
658 if (num_rx_qs > MAX_RX_QS) {
659 pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
660 num_rx_qs, MAX_RX_QS);
661 pr_err("Cannot do alloc_etherdev, aborting\n");
665 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
670 priv = netdev_priv(dev);
671 priv->node = ofdev->dev.of_node;
674 priv->num_tx_queues = num_tx_qs;
675 netif_set_real_num_rx_queues(dev, num_rx_qs);
676 priv->num_rx_queues = num_rx_qs;
677 priv->num_grps = 0x0;
679 /* Init Rx queue filer rule set linked list */
680 INIT_LIST_HEAD(&priv->rx_list.list);
681 priv->rx_list.count = 0;
682 mutex_init(&priv->rx_queue_access);
684 model = of_get_property(np, "model", NULL);
686 for (i = 0; i < MAXGROUPS; i++)
687 priv->gfargrp[i].regs = NULL;
689 /* Parse and initialize group specific information */
690 if (of_device_is_compatible(np, "fsl,etsec2")) {
691 priv->mode = MQ_MG_MODE;
692 for_each_child_of_node(np, child) {
693 err = gfar_parse_group(child, priv, model);
698 priv->mode = SQ_SG_MODE;
699 err = gfar_parse_group(np, priv, model);
704 for (i = 0; i < priv->num_tx_queues; i++)
705 priv->tx_queue[i] = NULL;
706 for (i = 0; i < priv->num_rx_queues; i++)
707 priv->rx_queue[i] = NULL;
709 for (i = 0; i < priv->num_tx_queues; i++) {
710 priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
712 if (!priv->tx_queue[i]) {
714 goto tx_alloc_failed;
716 priv->tx_queue[i]->tx_skbuff = NULL;
717 priv->tx_queue[i]->qindex = i;
718 priv->tx_queue[i]->dev = dev;
719 spin_lock_init(&(priv->tx_queue[i]->txlock));
722 for (i = 0; i < priv->num_rx_queues; i++) {
723 priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
725 if (!priv->rx_queue[i]) {
727 goto rx_alloc_failed;
729 priv->rx_queue[i]->rx_skbuff = NULL;
730 priv->rx_queue[i]->qindex = i;
731 priv->rx_queue[i]->dev = dev;
732 spin_lock_init(&(priv->rx_queue[i]->rxlock));
736 stash = of_get_property(np, "bd-stash", NULL);
739 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
740 priv->bd_stash_en = 1;
743 stash_len = of_get_property(np, "rx-stash-len", NULL);
746 priv->rx_stash_size = *stash_len;
748 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
751 priv->rx_stash_index = *stash_idx;
753 if (stash_len || stash_idx)
754 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
756 mac_addr = of_get_mac_address(np);
759 memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
761 if (model && !strcasecmp(model, "TSEC"))
762 priv->device_flags = FSL_GIANFAR_DEV_HAS_GIGABIT |
763 FSL_GIANFAR_DEV_HAS_COALESCE |
764 FSL_GIANFAR_DEV_HAS_RMON |
765 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
767 if (model && !strcasecmp(model, "eTSEC"))
768 priv->device_flags = FSL_GIANFAR_DEV_HAS_GIGABIT |
769 FSL_GIANFAR_DEV_HAS_COALESCE |
770 FSL_GIANFAR_DEV_HAS_RMON |
771 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
772 FSL_GIANFAR_DEV_HAS_PADDING |
773 FSL_GIANFAR_DEV_HAS_CSUM |
774 FSL_GIANFAR_DEV_HAS_VLAN |
775 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
776 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
777 FSL_GIANFAR_DEV_HAS_TIMER;
779 ctype = of_get_property(np, "phy-connection-type", NULL);
781 /* We only care about rgmii-id. The rest are autodetected */
782 if (ctype && !strcmp(ctype, "rgmii-id"))
783 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
785 priv->interface = PHY_INTERFACE_MODE_MII;
787 if (of_get_property(np, "fsl,magic-packet", NULL))
788 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
790 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
792 /* Find the TBI PHY. If it's not there, we don't support SGMII */
793 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
798 free_rx_pointers(priv);
800 free_tx_pointers(priv);
802 unmap_group_regs(priv);
807 static int gfar_hwtstamp_ioctl(struct net_device *netdev,
808 struct ifreq *ifr, int cmd)
810 struct hwtstamp_config config;
811 struct gfar_private *priv = netdev_priv(netdev);
813 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
816 /* reserved for future extensions */
820 switch (config.tx_type) {
821 case HWTSTAMP_TX_OFF:
822 priv->hwts_tx_en = 0;
825 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
827 priv->hwts_tx_en = 1;
833 switch (config.rx_filter) {
834 case HWTSTAMP_FILTER_NONE:
835 if (priv->hwts_rx_en) {
837 priv->hwts_rx_en = 0;
838 startup_gfar(netdev);
842 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
844 if (!priv->hwts_rx_en) {
846 priv->hwts_rx_en = 1;
847 startup_gfar(netdev);
849 config.rx_filter = HWTSTAMP_FILTER_ALL;
853 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
857 /* Ioctl MII Interface */
858 static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
860 struct gfar_private *priv = netdev_priv(dev);
862 if (!netif_running(dev))
865 if (cmd == SIOCSHWTSTAMP)
866 return gfar_hwtstamp_ioctl(dev, rq, cmd);
871 return phy_mii_ioctl(priv->phydev, rq, cmd);
874 static unsigned int reverse_bitmap(unsigned int bit_map, unsigned int max_qs)
876 unsigned int new_bit_map = 0x0;
877 int mask = 0x1 << (max_qs - 1), i;
879 for (i = 0; i < max_qs; i++) {
881 new_bit_map = new_bit_map + (1 << i);
887 static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
890 u32 rqfpr = FPR_FILER_MASK;
894 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
895 priv->ftp_rqfpr[rqfar] = rqfpr;
896 priv->ftp_rqfcr[rqfar] = rqfcr;
897 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
900 rqfcr = RQFCR_CMP_NOMATCH;
901 priv->ftp_rqfpr[rqfar] = rqfpr;
902 priv->ftp_rqfcr[rqfar] = rqfcr;
903 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
906 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
908 priv->ftp_rqfcr[rqfar] = rqfcr;
909 priv->ftp_rqfpr[rqfar] = rqfpr;
910 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
913 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
915 priv->ftp_rqfcr[rqfar] = rqfcr;
916 priv->ftp_rqfpr[rqfar] = rqfpr;
917 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
922 static void gfar_init_filer_table(struct gfar_private *priv)
925 u32 rqfar = MAX_FILER_IDX;
927 u32 rqfpr = FPR_FILER_MASK;
930 rqfcr = RQFCR_CMP_MATCH;
931 priv->ftp_rqfcr[rqfar] = rqfcr;
932 priv->ftp_rqfpr[rqfar] = rqfpr;
933 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
935 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
936 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
937 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
938 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
939 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
940 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
942 /* cur_filer_idx indicated the first non-masked rule */
943 priv->cur_filer_idx = rqfar;
945 /* Rest are masked rules */
946 rqfcr = RQFCR_CMP_NOMATCH;
947 for (i = 0; i < rqfar; i++) {
948 priv->ftp_rqfcr[i] = rqfcr;
949 priv->ftp_rqfpr[i] = rqfpr;
950 gfar_write_filer(priv, i, rqfcr, rqfpr);
954 static void gfar_detect_errata(struct gfar_private *priv)
956 struct device *dev = &priv->ofdev->dev;
957 unsigned int pvr = mfspr(SPRN_PVR);
958 unsigned int svr = mfspr(SPRN_SVR);
959 unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
960 unsigned int rev = svr & 0xffff;
962 /* MPC8313 Rev 2.0 and higher; All MPC837x */
963 if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
964 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
965 priv->errata |= GFAR_ERRATA_74;
967 /* MPC8313 and MPC837x all rev */
968 if ((pvr == 0x80850010 && mod == 0x80b0) ||
969 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
970 priv->errata |= GFAR_ERRATA_76;
972 /* MPC8313 and MPC837x all rev */
973 if ((pvr == 0x80850010 && mod == 0x80b0) ||
974 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
975 priv->errata |= GFAR_ERRATA_A002;
977 /* MPC8313 Rev < 2.0, MPC8548 rev 2.0 */
978 if ((pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020) ||
979 (pvr == 0x80210020 && mod == 0x8030 && rev == 0x0020))
980 priv->errata |= GFAR_ERRATA_12;
983 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
987 /* Set up the ethernet device structure, private data,
988 * and anything else we need before we start
990 static int gfar_probe(struct platform_device *ofdev)
993 struct net_device *dev = NULL;
994 struct gfar_private *priv = NULL;
995 struct gfar __iomem *regs = NULL;
996 int err = 0, i, grp_idx = 0;
997 u32 rstat = 0, tstat = 0, rqueue = 0, tqueue = 0;
1001 err = gfar_of_init(ofdev, &dev);
1006 priv = netdev_priv(dev);
1008 priv->ofdev = ofdev;
1009 priv->node = ofdev->dev.of_node;
1010 SET_NETDEV_DEV(dev, &ofdev->dev);
1012 spin_lock_init(&priv->bflock);
1013 INIT_WORK(&priv->reset_task, gfar_reset_task);
1015 dev_set_drvdata(&ofdev->dev, priv);
1016 regs = priv->gfargrp[0].regs;
1018 gfar_detect_errata(priv);
1020 /* Stop the DMA engine now, in case it was running before
1021 * (The firmware could have used it, and left it running).
1025 /* Reset MAC layer */
1026 gfar_write(®s->maccfg1, MACCFG1_SOFT_RESET);
1028 /* We need to delay at least 3 TX clocks */
1031 tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
1032 gfar_write(®s->maccfg1, tempval);
1034 /* Initialize MACCFG2. */
1035 tempval = MACCFG2_INIT_SETTINGS;
1036 if (gfar_has_errata(priv, GFAR_ERRATA_74))
1037 tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
1038 gfar_write(®s->maccfg2, tempval);
1040 /* Initialize ECNTRL */
1041 gfar_write(®s->ecntrl, ECNTRL_INIT_SETTINGS);
1043 /* Set the dev->base_addr to the gfar reg region */
1044 dev->base_addr = (unsigned long) regs;
1046 SET_NETDEV_DEV(dev, &ofdev->dev);
1048 /* Fill in the dev structure */
1049 dev->watchdog_timeo = TX_TIMEOUT;
1051 dev->netdev_ops = &gfar_netdev_ops;
1052 dev->ethtool_ops = &gfar_ethtool_ops;
1054 /* Register for napi ...We are registering NAPI for each grp */
1055 for (i = 0; i < priv->num_grps; i++)
1056 netif_napi_add(dev, &priv->gfargrp[i].napi, gfar_poll,
1059 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
1060 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
1062 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
1063 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
1066 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
1067 dev->hw_features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
1068 dev->features |= NETIF_F_HW_VLAN_RX;
1071 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
1072 priv->extended_hash = 1;
1073 priv->hash_width = 9;
1075 priv->hash_regs[0] = ®s->igaddr0;
1076 priv->hash_regs[1] = ®s->igaddr1;
1077 priv->hash_regs[2] = ®s->igaddr2;
1078 priv->hash_regs[3] = ®s->igaddr3;
1079 priv->hash_regs[4] = ®s->igaddr4;
1080 priv->hash_regs[5] = ®s->igaddr5;
1081 priv->hash_regs[6] = ®s->igaddr6;
1082 priv->hash_regs[7] = ®s->igaddr7;
1083 priv->hash_regs[8] = ®s->gaddr0;
1084 priv->hash_regs[9] = ®s->gaddr1;
1085 priv->hash_regs[10] = ®s->gaddr2;
1086 priv->hash_regs[11] = ®s->gaddr3;
1087 priv->hash_regs[12] = ®s->gaddr4;
1088 priv->hash_regs[13] = ®s->gaddr5;
1089 priv->hash_regs[14] = ®s->gaddr6;
1090 priv->hash_regs[15] = ®s->gaddr7;
1093 priv->extended_hash = 0;
1094 priv->hash_width = 8;
1096 priv->hash_regs[0] = ®s->gaddr0;
1097 priv->hash_regs[1] = ®s->gaddr1;
1098 priv->hash_regs[2] = ®s->gaddr2;
1099 priv->hash_regs[3] = ®s->gaddr3;
1100 priv->hash_regs[4] = ®s->gaddr4;
1101 priv->hash_regs[5] = ®s->gaddr5;
1102 priv->hash_regs[6] = ®s->gaddr6;
1103 priv->hash_regs[7] = ®s->gaddr7;
1106 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
1107 priv->padding = DEFAULT_PADDING;
1111 if (dev->features & NETIF_F_IP_CSUM ||
1112 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1113 dev->needed_headroom = GMAC_FCB_LEN;
1115 /* Program the isrg regs only if number of grps > 1 */
1116 if (priv->num_grps > 1) {
1117 baddr = ®s->isrg0;
1118 for (i = 0; i < priv->num_grps; i++) {
1119 isrg |= (priv->gfargrp[i].rx_bit_map << ISRG_SHIFT_RX);
1120 isrg |= (priv->gfargrp[i].tx_bit_map << ISRG_SHIFT_TX);
1121 gfar_write(baddr, isrg);
1127 /* Need to reverse the bit maps as bit_map's MSB is q0
1128 * but, for_each_set_bit parses from right to left, which
1129 * basically reverses the queue numbers
1131 for (i = 0; i< priv->num_grps; i++) {
1132 priv->gfargrp[i].tx_bit_map =
1133 reverse_bitmap(priv->gfargrp[i].tx_bit_map, MAX_TX_QS);
1134 priv->gfargrp[i].rx_bit_map =
1135 reverse_bitmap(priv->gfargrp[i].rx_bit_map, MAX_RX_QS);
1138 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
1139 * also assign queues to groups
1141 for (grp_idx = 0; grp_idx < priv->num_grps; grp_idx++) {
1142 priv->gfargrp[grp_idx].num_rx_queues = 0x0;
1144 for_each_set_bit(i, &priv->gfargrp[grp_idx].rx_bit_map,
1145 priv->num_rx_queues) {
1146 priv->gfargrp[grp_idx].num_rx_queues++;
1147 priv->rx_queue[i]->grp = &priv->gfargrp[grp_idx];
1148 rstat = rstat | (RSTAT_CLEAR_RHALT >> i);
1149 rqueue = rqueue | ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
1151 priv->gfargrp[grp_idx].num_tx_queues = 0x0;
1153 for_each_set_bit(i, &priv->gfargrp[grp_idx].tx_bit_map,
1154 priv->num_tx_queues) {
1155 priv->gfargrp[grp_idx].num_tx_queues++;
1156 priv->tx_queue[i]->grp = &priv->gfargrp[grp_idx];
1157 tstat = tstat | (TSTAT_CLEAR_THALT >> i);
1158 tqueue = tqueue | (TQUEUE_EN0 >> i);
1160 priv->gfargrp[grp_idx].rstat = rstat;
1161 priv->gfargrp[grp_idx].tstat = tstat;
1165 gfar_write(®s->rqueue, rqueue);
1166 gfar_write(®s->tqueue, tqueue);
1168 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
1170 /* Initializing some of the rx/tx queue level parameters */
1171 for (i = 0; i < priv->num_tx_queues; i++) {
1172 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1173 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1174 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1175 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1178 for (i = 0; i < priv->num_rx_queues; i++) {
1179 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1180 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1181 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1184 /* always enable rx filer */
1185 priv->rx_filer_enable = 1;
1186 /* Enable most messages by default */
1187 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1188 /* use pritority h/w tx queue scheduling for single queue devices */
1189 if (priv->num_tx_queues == 1)
1190 priv->prio_sched_en = 1;
1192 /* Carrier starts down, phylib will bring it up */
1193 netif_carrier_off(dev);
1195 err = register_netdev(dev);
1198 pr_err("%s: Cannot register net device, aborting\n", dev->name);
1202 device_init_wakeup(&dev->dev,
1203 priv->device_flags &
1204 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1206 /* fill out IRQ number and name fields */
1207 for (i = 0; i < priv->num_grps; i++) {
1208 struct gfar_priv_grp *grp = &priv->gfargrp[i];
1209 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1210 sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
1211 dev->name, "_g", '0' + i, "_tx");
1212 sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
1213 dev->name, "_g", '0' + i, "_rx");
1214 sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
1215 dev->name, "_g", '0' + i, "_er");
1217 strcpy(gfar_irq(grp, TX)->name, dev->name);
1220 /* Initialize the filer table */
1221 gfar_init_filer_table(priv);
1223 /* Create all the sysfs files */
1224 gfar_init_sysfs(dev);
1226 /* Print out the device info */
1227 netdev_info(dev, "mac: %pM\n", dev->dev_addr);
1229 /* Even more device info helps when determining which kernel
1230 * provided which set of benchmarks.
1232 netdev_info(dev, "Running with NAPI enabled\n");
1233 for (i = 0; i < priv->num_rx_queues; i++)
1234 netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1235 i, priv->rx_queue[i]->rx_ring_size);
1236 for (i = 0; i < priv->num_tx_queues; i++)
1237 netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1238 i, priv->tx_queue[i]->tx_ring_size);
1243 unmap_group_regs(priv);
1244 free_tx_pointers(priv);
1245 free_rx_pointers(priv);
1247 of_node_put(priv->phy_node);
1249 of_node_put(priv->tbi_node);
1250 free_gfar_dev(priv);
1254 static int gfar_remove(struct platform_device *ofdev)
1256 struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
1259 of_node_put(priv->phy_node);
1261 of_node_put(priv->tbi_node);
1263 dev_set_drvdata(&ofdev->dev, NULL);
1265 unregister_netdev(priv->ndev);
1266 unmap_group_regs(priv);
1267 free_gfar_dev(priv);
1274 static int gfar_suspend(struct device *dev)
1276 struct gfar_private *priv = dev_get_drvdata(dev);
1277 struct net_device *ndev = priv->ndev;
1278 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1279 unsigned long flags;
1282 int magic_packet = priv->wol_en &&
1283 (priv->device_flags &
1284 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1286 netif_device_detach(ndev);
1288 if (netif_running(ndev)) {
1290 local_irq_save(flags);
1294 gfar_halt_nodisable(ndev);
1296 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
1297 tempval = gfar_read(®s->maccfg1);
1299 tempval &= ~MACCFG1_TX_EN;
1302 tempval &= ~MACCFG1_RX_EN;
1304 gfar_write(®s->maccfg1, tempval);
1308 local_irq_restore(flags);
1313 /* Enable interrupt on Magic Packet */
1314 gfar_write(®s->imask, IMASK_MAG);
1316 /* Enable Magic Packet mode */
1317 tempval = gfar_read(®s->maccfg2);
1318 tempval |= MACCFG2_MPEN;
1319 gfar_write(®s->maccfg2, tempval);
1321 phy_stop(priv->phydev);
1328 static int gfar_resume(struct device *dev)
1330 struct gfar_private *priv = dev_get_drvdata(dev);
1331 struct net_device *ndev = priv->ndev;
1332 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1333 unsigned long flags;
1335 int magic_packet = priv->wol_en &&
1336 (priv->device_flags &
1337 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1339 if (!netif_running(ndev)) {
1340 netif_device_attach(ndev);
1344 if (!magic_packet && priv->phydev)
1345 phy_start(priv->phydev);
1347 /* Disable Magic Packet mode, in case something
1350 local_irq_save(flags);
1354 tempval = gfar_read(®s->maccfg2);
1355 tempval &= ~MACCFG2_MPEN;
1356 gfar_write(®s->maccfg2, tempval);
1362 local_irq_restore(flags);
1364 netif_device_attach(ndev);
1371 static int gfar_restore(struct device *dev)
1373 struct gfar_private *priv = dev_get_drvdata(dev);
1374 struct net_device *ndev = priv->ndev;
1376 if (!netif_running(ndev)) {
1377 netif_device_attach(ndev);
1382 if (gfar_init_bds(ndev)) {
1383 free_skb_resources(priv);
1387 init_registers(ndev);
1388 gfar_set_mac_address(ndev);
1389 gfar_init_mac(ndev);
1394 priv->oldduplex = -1;
1397 phy_start(priv->phydev);
1399 netif_device_attach(ndev);
1405 static struct dev_pm_ops gfar_pm_ops = {
1406 .suspend = gfar_suspend,
1407 .resume = gfar_resume,
1408 .freeze = gfar_suspend,
1409 .thaw = gfar_resume,
1410 .restore = gfar_restore,
1413 #define GFAR_PM_OPS (&gfar_pm_ops)
1417 #define GFAR_PM_OPS NULL
1421 /* Reads the controller's registers to determine what interface
1422 * connects it to the PHY.
1424 static phy_interface_t gfar_get_interface(struct net_device *dev)
1426 struct gfar_private *priv = netdev_priv(dev);
1427 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1430 ecntrl = gfar_read(®s->ecntrl);
1432 if (ecntrl & ECNTRL_SGMII_MODE)
1433 return PHY_INTERFACE_MODE_SGMII;
1435 if (ecntrl & ECNTRL_TBI_MODE) {
1436 if (ecntrl & ECNTRL_REDUCED_MODE)
1437 return PHY_INTERFACE_MODE_RTBI;
1439 return PHY_INTERFACE_MODE_TBI;
1442 if (ecntrl & ECNTRL_REDUCED_MODE) {
1443 if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
1444 return PHY_INTERFACE_MODE_RMII;
1447 phy_interface_t interface = priv->interface;
1449 /* This isn't autodetected right now, so it must
1450 * be set by the device tree or platform code.
1452 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1453 return PHY_INTERFACE_MODE_RGMII_ID;
1455 return PHY_INTERFACE_MODE_RGMII;
1459 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
1460 return PHY_INTERFACE_MODE_GMII;
1462 return PHY_INTERFACE_MODE_MII;
1466 /* Initializes driver's PHY state, and attaches to the PHY.
1467 * Returns 0 on success.
1469 static int init_phy(struct net_device *dev)
1471 struct gfar_private *priv = netdev_priv(dev);
1472 uint gigabit_support =
1473 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
1474 SUPPORTED_1000baseT_Full : 0;
1475 phy_interface_t interface;
1479 priv->oldduplex = -1;
1481 interface = gfar_get_interface(dev);
1483 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1486 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1488 if (!priv->phydev) {
1489 dev_err(&dev->dev, "could not attach to PHY\n");
1493 if (interface == PHY_INTERFACE_MODE_SGMII)
1494 gfar_configure_serdes(dev);
1496 /* Remove any features not supported by the controller */
1497 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1498 priv->phydev->advertising = priv->phydev->supported;
1503 /* Initialize TBI PHY interface for communicating with the
1504 * SERDES lynx PHY on the chip. We communicate with this PHY
1505 * through the MDIO bus on each controller, treating it as a
1506 * "normal" PHY at the address found in the TBIPA register. We assume
1507 * that the TBIPA register is valid. Either the MDIO bus code will set
1508 * it to a value that doesn't conflict with other PHYs on the bus, or the
1509 * value doesn't matter, as there are no other PHYs on the bus.
1511 static void gfar_configure_serdes(struct net_device *dev)
1513 struct gfar_private *priv = netdev_priv(dev);
1514 struct phy_device *tbiphy;
1516 if (!priv->tbi_node) {
1517 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1518 "device tree specify a tbi-handle\n");
1522 tbiphy = of_phy_find_device(priv->tbi_node);
1524 dev_err(&dev->dev, "error: Could not get TBI device\n");
1528 /* If the link is already up, we must already be ok, and don't need to
1529 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1530 * everything for us? Resetting it takes the link down and requires
1531 * several seconds for it to come back.
1533 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
1536 /* Single clk mode, mii mode off(for serdes communication) */
1537 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
1539 phy_write(tbiphy, MII_ADVERTISE,
1540 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1541 ADVERTISE_1000XPSE_ASYM);
1543 phy_write(tbiphy, MII_BMCR,
1544 BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1548 static void init_registers(struct net_device *dev)
1550 struct gfar_private *priv = netdev_priv(dev);
1551 struct gfar __iomem *regs = NULL;
1554 for (i = 0; i < priv->num_grps; i++) {
1555 regs = priv->gfargrp[i].regs;
1557 gfar_write(®s->ievent, IEVENT_INIT_CLEAR);
1559 /* Initialize IMASK */
1560 gfar_write(®s->imask, IMASK_INIT_CLEAR);
1563 regs = priv->gfargrp[0].regs;
1564 /* Init hash registers to zero */
1565 gfar_write(®s->igaddr0, 0);
1566 gfar_write(®s->igaddr1, 0);
1567 gfar_write(®s->igaddr2, 0);
1568 gfar_write(®s->igaddr3, 0);
1569 gfar_write(®s->igaddr4, 0);
1570 gfar_write(®s->igaddr5, 0);
1571 gfar_write(®s->igaddr6, 0);
1572 gfar_write(®s->igaddr7, 0);
1574 gfar_write(®s->gaddr0, 0);
1575 gfar_write(®s->gaddr1, 0);
1576 gfar_write(®s->gaddr2, 0);
1577 gfar_write(®s->gaddr3, 0);
1578 gfar_write(®s->gaddr4, 0);
1579 gfar_write(®s->gaddr5, 0);
1580 gfar_write(®s->gaddr6, 0);
1581 gfar_write(®s->gaddr7, 0);
1583 /* Zero out the rmon mib registers if it has them */
1584 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1585 memset_io(&(regs->rmon), 0, sizeof (struct rmon_mib));
1587 /* Mask off the CAM interrupts */
1588 gfar_write(®s->rmon.cam1, 0xffffffff);
1589 gfar_write(®s->rmon.cam2, 0xffffffff);
1592 /* Initialize the max receive buffer length */
1593 gfar_write(®s->mrblr, priv->rx_buffer_size);
1595 /* Initialize the Minimum Frame Length Register */
1596 gfar_write(®s->minflr, MINFLR_INIT_SETTINGS);
1599 static int __gfar_is_rx_idle(struct gfar_private *priv)
1603 /* Normaly TSEC should not hang on GRS commands, so we should
1604 * actually wait for IEVENT_GRSC flag.
1606 if (likely(!gfar_has_errata(priv, GFAR_ERRATA_A002)))
1609 /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
1610 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1611 * and the Rx can be safely reset.
1613 res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1615 if ((res & 0xffff) == (res >> 16))
1621 /* Halt the receive and transmit queues */
1622 static void gfar_halt_nodisable(struct net_device *dev)
1624 struct gfar_private *priv = netdev_priv(dev);
1625 struct gfar __iomem *regs = NULL;
1629 for (i = 0; i < priv->num_grps; i++) {
1630 regs = priv->gfargrp[i].regs;
1631 /* Mask all interrupts */
1632 gfar_write(®s->imask, IMASK_INIT_CLEAR);
1634 /* Clear all interrupts */
1635 gfar_write(®s->ievent, IEVENT_INIT_CLEAR);
1638 regs = priv->gfargrp[0].regs;
1639 /* Stop the DMA, and wait for it to stop */
1640 tempval = gfar_read(®s->dmactrl);
1641 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS)) !=
1642 (DMACTRL_GRS | DMACTRL_GTS)) {
1645 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
1646 gfar_write(®s->dmactrl, tempval);
1649 ret = spin_event_timeout(((gfar_read(®s->ievent) &
1650 (IEVENT_GRSC | IEVENT_GTSC)) ==
1651 (IEVENT_GRSC | IEVENT_GTSC)), 1000000, 0);
1652 if (!ret && !(gfar_read(®s->ievent) & IEVENT_GRSC))
1653 ret = __gfar_is_rx_idle(priv);
1658 /* Halt the receive and transmit queues */
1659 void gfar_halt(struct net_device *dev)
1661 struct gfar_private *priv = netdev_priv(dev);
1662 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1665 gfar_halt_nodisable(dev);
1667 /* Disable Rx and Tx */
1668 tempval = gfar_read(®s->maccfg1);
1669 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1670 gfar_write(®s->maccfg1, tempval);
1673 static void free_grp_irqs(struct gfar_priv_grp *grp)
1675 free_irq(gfar_irq(grp, TX)->irq, grp);
1676 free_irq(gfar_irq(grp, RX)->irq, grp);
1677 free_irq(gfar_irq(grp, ER)->irq, grp);
1680 void stop_gfar(struct net_device *dev)
1682 struct gfar_private *priv = netdev_priv(dev);
1683 unsigned long flags;
1686 phy_stop(priv->phydev);
1690 local_irq_save(flags);
1698 local_irq_restore(flags);
1701 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1702 for (i = 0; i < priv->num_grps; i++)
1703 free_grp_irqs(&priv->gfargrp[i]);
1705 for (i = 0; i < priv->num_grps; i++)
1706 free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
1710 free_skb_resources(priv);
1713 static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
1715 struct txbd8 *txbdp;
1716 struct gfar_private *priv = netdev_priv(tx_queue->dev);
1719 txbdp = tx_queue->tx_bd_base;
1721 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1722 if (!tx_queue->tx_skbuff[i])
1725 dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
1726 txbdp->length, DMA_TO_DEVICE);
1728 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
1731 dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
1732 txbdp->length, DMA_TO_DEVICE);
1735 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1736 tx_queue->tx_skbuff[i] = NULL;
1738 kfree(tx_queue->tx_skbuff);
1739 tx_queue->tx_skbuff = NULL;
1742 static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1744 struct rxbd8 *rxbdp;
1745 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1748 rxbdp = rx_queue->rx_bd_base;
1750 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1751 if (rx_queue->rx_skbuff[i]) {
1752 dma_unmap_single(&priv->ofdev->dev,
1753 rxbdp->bufPtr, priv->rx_buffer_size,
1755 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1756 rx_queue->rx_skbuff[i] = NULL;
1762 kfree(rx_queue->rx_skbuff);
1763 rx_queue->rx_skbuff = NULL;
1766 /* If there are any tx skbs or rx skbs still around, free them.
1767 * Then free tx_skbuff and rx_skbuff
1769 static void free_skb_resources(struct gfar_private *priv)
1771 struct gfar_priv_tx_q *tx_queue = NULL;
1772 struct gfar_priv_rx_q *rx_queue = NULL;
1775 /* Go through all the buffer descriptors and free their data buffers */
1776 for (i = 0; i < priv->num_tx_queues; i++) {
1777 struct netdev_queue *txq;
1779 tx_queue = priv->tx_queue[i];
1780 txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
1781 if (tx_queue->tx_skbuff)
1782 free_skb_tx_queue(tx_queue);
1783 netdev_tx_reset_queue(txq);
1786 for (i = 0; i < priv->num_rx_queues; i++) {
1787 rx_queue = priv->rx_queue[i];
1788 if (rx_queue->rx_skbuff)
1789 free_skb_rx_queue(rx_queue);
1792 dma_free_coherent(&priv->ofdev->dev,
1793 sizeof(struct txbd8) * priv->total_tx_ring_size +
1794 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1795 priv->tx_queue[0]->tx_bd_base,
1796 priv->tx_queue[0]->tx_bd_dma_base);
1799 void gfar_start(struct net_device *dev)
1801 struct gfar_private *priv = netdev_priv(dev);
1802 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1806 /* Enable Rx and Tx in MACCFG1 */
1807 tempval = gfar_read(®s->maccfg1);
1808 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1809 gfar_write(®s->maccfg1, tempval);
1811 /* Initialize DMACTRL to have WWR and WOP */
1812 tempval = gfar_read(®s->dmactrl);
1813 tempval |= DMACTRL_INIT_SETTINGS;
1814 gfar_write(®s->dmactrl, tempval);
1816 /* Make sure we aren't stopped */
1817 tempval = gfar_read(®s->dmactrl);
1818 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
1819 gfar_write(®s->dmactrl, tempval);
1821 for (i = 0; i < priv->num_grps; i++) {
1822 regs = priv->gfargrp[i].regs;
1823 /* Clear THLT/RHLT, so that the DMA starts polling now */
1824 gfar_write(®s->tstat, priv->gfargrp[i].tstat);
1825 gfar_write(®s->rstat, priv->gfargrp[i].rstat);
1826 /* Unmask the interrupts we look for */
1827 gfar_write(®s->imask, IMASK_DEFAULT);
1830 dev->trans_start = jiffies; /* prevent tx timeout */
1833 void gfar_configure_coalescing(struct gfar_private *priv,
1834 unsigned long tx_mask, unsigned long rx_mask)
1836 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1840 /* Backward compatible case ---- even if we enable
1841 * multiple queues, there's only single reg to program
1843 gfar_write(®s->txic, 0);
1844 if (likely(priv->tx_queue[0]->txcoalescing))
1845 gfar_write(®s->txic, priv->tx_queue[0]->txic);
1847 gfar_write(®s->rxic, 0);
1848 if (unlikely(priv->rx_queue[0]->rxcoalescing))
1849 gfar_write(®s->rxic, priv->rx_queue[0]->rxic);
1851 if (priv->mode == MQ_MG_MODE) {
1852 baddr = ®s->txic0;
1853 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
1854 gfar_write(baddr + i, 0);
1855 if (likely(priv->tx_queue[i]->txcoalescing))
1856 gfar_write(baddr + i, priv->tx_queue[i]->txic);
1859 baddr = ®s->rxic0;
1860 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
1861 gfar_write(baddr + i, 0);
1862 if (likely(priv->rx_queue[i]->rxcoalescing))
1863 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
1868 static int register_grp_irqs(struct gfar_priv_grp *grp)
1870 struct gfar_private *priv = grp->priv;
1871 struct net_device *dev = priv->ndev;
1874 /* If the device has multiple interrupts, register for
1875 * them. Otherwise, only register for the one
1877 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1878 /* Install our interrupt handlers for Error,
1879 * Transmit, and Receive
1881 err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
1882 gfar_irq(grp, ER)->name, grp);
1884 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1885 gfar_irq(grp, ER)->irq);
1889 err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
1890 gfar_irq(grp, TX)->name, grp);
1892 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1893 gfar_irq(grp, TX)->irq);
1896 err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
1897 gfar_irq(grp, RX)->name, grp);
1899 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1900 gfar_irq(grp, RX)->irq);
1904 err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
1905 gfar_irq(grp, TX)->name, grp);
1907 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1908 gfar_irq(grp, TX)->irq);
1916 free_irq(gfar_irq(grp, TX)->irq, grp);
1918 free_irq(gfar_irq(grp, ER)->irq, grp);
1924 /* Bring the controller up and running */
1925 int startup_gfar(struct net_device *ndev)
1927 struct gfar_private *priv = netdev_priv(ndev);
1928 struct gfar __iomem *regs = NULL;
1931 for (i = 0; i < priv->num_grps; i++) {
1932 regs= priv->gfargrp[i].regs;
1933 gfar_write(®s->imask, IMASK_INIT_CLEAR);
1936 regs= priv->gfargrp[0].regs;
1937 err = gfar_alloc_skb_resources(ndev);
1941 gfar_init_mac(ndev);
1943 for (i = 0; i < priv->num_grps; i++) {
1944 err = register_grp_irqs(&priv->gfargrp[i]);
1946 for (j = 0; j < i; j++)
1947 free_grp_irqs(&priv->gfargrp[j]);
1952 /* Start the controller */
1955 phy_start(priv->phydev);
1957 gfar_configure_coalescing(priv, 0xFF, 0xFF);
1962 free_skb_resources(priv);
1966 /* Called when something needs to use the ethernet device
1967 * Returns 0 for success.
1969 static int gfar_enet_open(struct net_device *dev)
1971 struct gfar_private *priv = netdev_priv(dev);
1976 /* Initialize a bunch of registers */
1977 init_registers(dev);
1979 gfar_set_mac_address(dev);
1981 err = init_phy(dev);
1988 err = startup_gfar(dev);
1994 netif_tx_start_all_queues(dev);
1996 device_set_wakeup_enable(&dev->dev, priv->wol_en);
2001 static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
2003 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
2005 memset(fcb, 0, GMAC_FCB_LEN);
2010 static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
2013 /* If we're here, it's a IP packet with a TCP or UDP
2014 * payload. We set it to checksum, using a pseudo-header
2017 u8 flags = TXFCB_DEFAULT;
2019 /* Tell the controller what the protocol is
2020 * And provide the already calculated phcs
2022 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
2024 fcb->phcs = udp_hdr(skb)->check;
2026 fcb->phcs = tcp_hdr(skb)->check;
2028 /* l3os is the distance between the start of the
2029 * frame (skb->data) and the start of the IP hdr.
2030 * l4os is the distance between the start of the
2031 * l3 hdr and the l4 hdr
2033 fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
2034 fcb->l4os = skb_network_header_len(skb);
2039 void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
2041 fcb->flags |= TXFCB_VLN;
2042 fcb->vlctl = vlan_tx_tag_get(skb);
2045 static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
2046 struct txbd8 *base, int ring_size)
2048 struct txbd8 *new_bd = bdp + stride;
2050 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2053 static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
2056 return skip_txbd(bdp, 1, base, ring_size);
2059 /* This is called by the kernel when a frame is ready for transmission.
2060 * It is pointed to by the dev->hard_start_xmit function pointer
2062 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2064 struct gfar_private *priv = netdev_priv(dev);
2065 struct gfar_priv_tx_q *tx_queue = NULL;
2066 struct netdev_queue *txq;
2067 struct gfar __iomem *regs = NULL;
2068 struct txfcb *fcb = NULL;
2069 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
2071 int i, rq = 0, do_tstamp = 0;
2073 unsigned long flags;
2074 unsigned int nr_frags, nr_txbds, length, fcb_length = GMAC_FCB_LEN;
2076 /* TOE=1 frames larger than 2500 bytes may see excess delays
2077 * before start of transmission.
2079 if (unlikely(gfar_has_errata(priv, GFAR_ERRATA_76) &&
2080 skb->ip_summed == CHECKSUM_PARTIAL &&
2084 ret = skb_checksum_help(skb);
2089 rq = skb->queue_mapping;
2090 tx_queue = priv->tx_queue[rq];
2091 txq = netdev_get_tx_queue(dev, rq);
2092 base = tx_queue->tx_bd_base;
2093 regs = tx_queue->grp->regs;
2095 /* check if time stamp should be generated */
2096 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
2097 priv->hwts_tx_en)) {
2099 fcb_length = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2102 /* make space for additional header when fcb is needed */
2103 if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
2104 vlan_tx_tag_present(skb) ||
2105 unlikely(do_tstamp)) &&
2106 (skb_headroom(skb) < fcb_length)) {
2107 struct sk_buff *skb_new;
2109 skb_new = skb_realloc_headroom(skb, fcb_length);
2111 dev->stats.tx_errors++;
2113 return NETDEV_TX_OK;
2117 skb_set_owner_w(skb_new, skb->sk);
2122 /* total number of fragments in the SKB */
2123 nr_frags = skb_shinfo(skb)->nr_frags;
2125 /* calculate the required number of TxBDs for this skb */
2126 if (unlikely(do_tstamp))
2127 nr_txbds = nr_frags + 2;
2129 nr_txbds = nr_frags + 1;
2131 /* check if there is space to queue this packet */
2132 if (nr_txbds > tx_queue->num_txbdfree) {
2133 /* no space, stop the queue */
2134 netif_tx_stop_queue(txq);
2135 dev->stats.tx_fifo_errors++;
2136 return NETDEV_TX_BUSY;
2139 /* Update transmit stats */
2140 tx_queue->stats.tx_bytes += skb->len;
2141 tx_queue->stats.tx_packets++;
2143 txbdp = txbdp_start = tx_queue->cur_tx;
2144 lstatus = txbdp->lstatus;
2146 /* Time stamp insertion requires one additional TxBD */
2147 if (unlikely(do_tstamp))
2148 txbdp_tstamp = txbdp = next_txbd(txbdp, base,
2149 tx_queue->tx_ring_size);
2151 if (nr_frags == 0) {
2152 if (unlikely(do_tstamp))
2153 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
2156 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2158 /* Place the fragment addresses and lengths into the TxBDs */
2159 for (i = 0; i < nr_frags; i++) {
2160 /* Point at the next BD, wrapping as needed */
2161 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2163 length = skb_shinfo(skb)->frags[i].size;
2165 lstatus = txbdp->lstatus | length |
2166 BD_LFLAG(TXBD_READY);
2168 /* Handle the last BD specially */
2169 if (i == nr_frags - 1)
2170 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2172 bufaddr = skb_frag_dma_map(&priv->ofdev->dev,
2173 &skb_shinfo(skb)->frags[i],
2178 /* set the TxBD length and buffer pointer */
2179 txbdp->bufPtr = bufaddr;
2180 txbdp->lstatus = lstatus;
2183 lstatus = txbdp_start->lstatus;
2186 /* Add TxPAL between FCB and frame if required */
2187 if (unlikely(do_tstamp)) {
2188 skb_push(skb, GMAC_TXPAL_LEN);
2189 memset(skb->data, 0, GMAC_TXPAL_LEN);
2192 /* Set up checksumming */
2193 if (CHECKSUM_PARTIAL == skb->ip_summed) {
2194 fcb = gfar_add_fcb(skb);
2195 /* as specified by errata */
2196 if (unlikely(gfar_has_errata(priv, GFAR_ERRATA_12) &&
2197 ((unsigned long)fcb % 0x20) > 0x18)) {
2198 __skb_pull(skb, GMAC_FCB_LEN);
2199 skb_checksum_help(skb);
2201 lstatus |= BD_LFLAG(TXBD_TOE);
2202 gfar_tx_checksum(skb, fcb, fcb_length);
2206 if (vlan_tx_tag_present(skb)) {
2207 if (unlikely(NULL == fcb)) {
2208 fcb = gfar_add_fcb(skb);
2209 lstatus |= BD_LFLAG(TXBD_TOE);
2212 gfar_tx_vlan(skb, fcb);
2215 /* Setup tx hardware time stamping if requested */
2216 if (unlikely(do_tstamp)) {
2217 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2219 fcb = gfar_add_fcb(skb);
2221 lstatus |= BD_LFLAG(TXBD_TOE);
2224 txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
2225 skb_headlen(skb), DMA_TO_DEVICE);
2227 /* If time stamping is requested one additional TxBD must be set up. The
2228 * first TxBD points to the FCB and must have a data length of
2229 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2230 * the full frame length.
2232 if (unlikely(do_tstamp)) {
2233 txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_length;
2234 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
2235 (skb_headlen(skb) - fcb_length);
2236 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2238 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2241 netdev_tx_sent_queue(txq, skb->len);
2243 /* We can work in parallel with gfar_clean_tx_ring(), except
2244 * when modifying num_txbdfree. Note that we didn't grab the lock
2245 * when we were reading the num_txbdfree and checking for available
2246 * space, that's because outside of this function it can only grow,
2247 * and once we've got needed space, it cannot suddenly disappear.
2249 * The lock also protects us from gfar_error(), which can modify
2250 * regs->tstat and thus retrigger the transfers, which is why we
2251 * also must grab the lock before setting ready bit for the first
2252 * to be transmitted BD.
2254 spin_lock_irqsave(&tx_queue->txlock, flags);
2256 /* The powerpc-specific eieio() is used, as wmb() has too strong
2257 * semantics (it requires synchronization between cacheable and
2258 * uncacheable mappings, which eieio doesn't provide and which we
2259 * don't need), thus requiring a more expensive sync instruction. At
2260 * some point, the set of architecture-independent barrier functions
2261 * should be expanded to include weaker barriers.
2265 txbdp_start->lstatus = lstatus;
2267 eieio(); /* force lstatus write before tx_skbuff */
2269 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2271 /* Update the current skb pointer to the next entry we will use
2272 * (wrapping if necessary)
2274 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
2275 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
2277 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2279 /* reduce TxBD free count */
2280 tx_queue->num_txbdfree -= (nr_txbds);
2282 /* If the next BD still needs to be cleaned up, then the bds
2283 * are full. We need to tell the kernel to stop sending us stuff.
2285 if (!tx_queue->num_txbdfree) {
2286 netif_tx_stop_queue(txq);
2288 dev->stats.tx_fifo_errors++;
2291 /* Tell the DMA to go go go */
2292 gfar_write(®s->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
2295 spin_unlock_irqrestore(&tx_queue->txlock, flags);
2297 return NETDEV_TX_OK;
2300 /* Stops the kernel queue, and halts the controller */
2301 static int gfar_close(struct net_device *dev)
2303 struct gfar_private *priv = netdev_priv(dev);
2307 cancel_work_sync(&priv->reset_task);
2310 /* Disconnect from the PHY */
2311 phy_disconnect(priv->phydev);
2312 priv->phydev = NULL;
2314 netif_tx_stop_all_queues(dev);
2319 /* Changes the mac address if the controller is not running. */
2320 static int gfar_set_mac_address(struct net_device *dev)
2322 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
2327 /* Check if rx parser should be activated */
2328 void gfar_check_rx_parser_mode(struct gfar_private *priv)
2330 struct gfar __iomem *regs;
2333 regs = priv->gfargrp[0].regs;
2335 tempval = gfar_read(®s->rctrl);
2336 /* If parse is no longer required, then disable parser */
2337 if (tempval & RCTRL_REQ_PARSER)
2338 tempval |= RCTRL_PRSDEP_INIT;
2340 tempval &= ~RCTRL_PRSDEP_INIT;
2341 gfar_write(®s->rctrl, tempval);
2344 /* Enables and disables VLAN insertion/extraction */
2345 void gfar_vlan_mode(struct net_device *dev, netdev_features_t features)
2347 struct gfar_private *priv = netdev_priv(dev);
2348 struct gfar __iomem *regs = NULL;
2349 unsigned long flags;
2352 regs = priv->gfargrp[0].regs;
2353 local_irq_save(flags);
2356 if (features & NETIF_F_HW_VLAN_TX) {
2357 /* Enable VLAN tag insertion */
2358 tempval = gfar_read(®s->tctrl);
2359 tempval |= TCTRL_VLINS;
2360 gfar_write(®s->tctrl, tempval);
2362 /* Disable VLAN tag insertion */
2363 tempval = gfar_read(®s->tctrl);
2364 tempval &= ~TCTRL_VLINS;
2365 gfar_write(®s->tctrl, tempval);
2368 if (features & NETIF_F_HW_VLAN_RX) {
2369 /* Enable VLAN tag extraction */
2370 tempval = gfar_read(®s->rctrl);
2371 tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
2372 gfar_write(®s->rctrl, tempval);
2374 /* Disable VLAN tag extraction */
2375 tempval = gfar_read(®s->rctrl);
2376 tempval &= ~RCTRL_VLEX;
2377 gfar_write(®s->rctrl, tempval);
2379 gfar_check_rx_parser_mode(priv);
2382 gfar_change_mtu(dev, dev->mtu);
2385 local_irq_restore(flags);
2388 static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2390 int tempsize, tempval;
2391 struct gfar_private *priv = netdev_priv(dev);
2392 struct gfar __iomem *regs = priv->gfargrp[0].regs;
2393 int oldsize = priv->rx_buffer_size;
2394 int frame_size = new_mtu + ETH_HLEN;
2396 if (gfar_is_vlan_on(priv))
2397 frame_size += VLAN_HLEN;
2399 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
2400 netif_err(priv, drv, dev, "Invalid MTU setting\n");
2404 if (gfar_uses_fcb(priv))
2405 frame_size += GMAC_FCB_LEN;
2407 frame_size += priv->padding;
2409 tempsize = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
2410 INCREMENTAL_BUFFER_SIZE;
2412 /* Only stop and start the controller if it isn't already
2413 * stopped, and we changed something
2415 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2418 priv->rx_buffer_size = tempsize;
2422 gfar_write(®s->mrblr, priv->rx_buffer_size);
2423 gfar_write(®s->maxfrm, priv->rx_buffer_size);
2425 /* If the mtu is larger than the max size for standard
2426 * ethernet frames (ie, a jumbo frame), then set maccfg2
2427 * to allow huge frames, and to check the length
2429 tempval = gfar_read(®s->maccfg2);
2431 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
2432 gfar_has_errata(priv, GFAR_ERRATA_74))
2433 tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2435 tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
2437 gfar_write(®s->maccfg2, tempval);
2439 if ((oldsize != tempsize) && (dev->flags & IFF_UP))
2445 /* gfar_reset_task gets scheduled when a packet has not been
2446 * transmitted after a set amount of time.
2447 * For now, assume that clearing out all the structures, and
2448 * starting over will fix the problem.
2450 static void gfar_reset_task(struct work_struct *work)
2452 struct gfar_private *priv = container_of(work, struct gfar_private,
2454 struct net_device *dev = priv->ndev;
2456 if (dev->flags & IFF_UP) {
2457 netif_tx_stop_all_queues(dev);
2460 netif_tx_start_all_queues(dev);
2463 netif_tx_schedule_all(dev);
2466 static void gfar_timeout(struct net_device *dev)
2468 struct gfar_private *priv = netdev_priv(dev);
2470 dev->stats.tx_errors++;
2471 schedule_work(&priv->reset_task);
2474 static void gfar_align_skb(struct sk_buff *skb)
2476 /* We need the data buffer to be aligned properly. We will reserve
2477 * as many bytes as needed to align the data properly
2479 skb_reserve(skb, RXBUF_ALIGNMENT -
2480 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
2483 /* Interrupt Handler for Transmit complete */
2484 static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
2486 struct net_device *dev = tx_queue->dev;
2487 struct netdev_queue *txq;
2488 struct gfar_private *priv = netdev_priv(dev);
2489 struct gfar_priv_rx_q *rx_queue = NULL;
2490 struct txbd8 *bdp, *next = NULL;
2491 struct txbd8 *lbdp = NULL;
2492 struct txbd8 *base = tx_queue->tx_bd_base;
2493 struct sk_buff *skb;
2495 int tx_ring_size = tx_queue->tx_ring_size;
2496 int frags = 0, nr_txbds = 0;
2499 int tqi = tx_queue->qindex;
2500 unsigned int bytes_sent = 0;
2504 rx_queue = priv->rx_queue[tqi];
2505 txq = netdev_get_tx_queue(dev, tqi);
2506 bdp = tx_queue->dirty_tx;
2507 skb_dirtytx = tx_queue->skb_dirtytx;
2509 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
2510 unsigned long flags;
2512 frags = skb_shinfo(skb)->nr_frags;
2514 /* When time stamping, one additional TxBD must be freed.
2515 * Also, we need to dma_unmap_single() the TxPAL.
2517 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
2518 nr_txbds = frags + 2;
2520 nr_txbds = frags + 1;
2522 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
2524 lstatus = lbdp->lstatus;
2526 /* Only clean completed frames */
2527 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
2528 (lstatus & BD_LENGTH_MASK))
2531 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
2532 next = next_txbd(bdp, base, tx_ring_size);
2533 buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2535 buflen = bdp->length;
2537 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
2538 buflen, DMA_TO_DEVICE);
2540 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
2541 struct skb_shared_hwtstamps shhwtstamps;
2542 u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
2544 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2545 shhwtstamps.hwtstamp = ns_to_ktime(*ns);
2546 skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
2547 skb_tstamp_tx(skb, &shhwtstamps);
2548 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2552 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2553 bdp = next_txbd(bdp, base, tx_ring_size);
2555 for (i = 0; i < frags; i++) {
2556 dma_unmap_page(&priv->ofdev->dev, bdp->bufPtr,
2557 bdp->length, DMA_TO_DEVICE);
2558 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2559 bdp = next_txbd(bdp, base, tx_ring_size);
2562 bytes_sent += skb->len;
2564 dev_kfree_skb_any(skb);
2566 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
2568 skb_dirtytx = (skb_dirtytx + 1) &
2569 TX_RING_MOD_MASK(tx_ring_size);
2572 spin_lock_irqsave(&tx_queue->txlock, flags);
2573 tx_queue->num_txbdfree += nr_txbds;
2574 spin_unlock_irqrestore(&tx_queue->txlock, flags);
2577 /* If we freed a buffer, we can restart transmission, if necessary */
2578 if (netif_tx_queue_stopped(txq) && tx_queue->num_txbdfree)
2579 netif_wake_subqueue(dev, tqi);
2581 /* Update dirty indicators */
2582 tx_queue->skb_dirtytx = skb_dirtytx;
2583 tx_queue->dirty_tx = bdp;
2585 netdev_tx_completed_queue(txq, howmany, bytes_sent);
2590 static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
2592 unsigned long flags;
2594 spin_lock_irqsave(&gfargrp->grplock, flags);
2595 if (napi_schedule_prep(&gfargrp->napi)) {
2596 gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
2597 __napi_schedule(&gfargrp->napi);
2599 /* Clear IEVENT, so interrupts aren't called again
2600 * because of the packets that have already arrived.
2602 gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
2604 spin_unlock_irqrestore(&gfargrp->grplock, flags);
2608 /* Interrupt Handler for Transmit complete */
2609 static irqreturn_t gfar_transmit(int irq, void *grp_id)
2611 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
2615 static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
2616 struct sk_buff *skb)
2618 struct net_device *dev = rx_queue->dev;
2619 struct gfar_private *priv = netdev_priv(dev);
2622 buf = dma_map_single(&priv->ofdev->dev, skb->data,
2623 priv->rx_buffer_size, DMA_FROM_DEVICE);
2624 gfar_init_rxbdp(rx_queue, bdp, buf);
2627 static struct sk_buff *gfar_alloc_skb(struct net_device *dev)
2629 struct gfar_private *priv = netdev_priv(dev);
2630 struct sk_buff *skb;
2632 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
2636 gfar_align_skb(skb);
2641 struct sk_buff *gfar_new_skb(struct net_device *dev)
2643 return gfar_alloc_skb(dev);
2646 static inline void count_errors(unsigned short status, struct net_device *dev)
2648 struct gfar_private *priv = netdev_priv(dev);
2649 struct net_device_stats *stats = &dev->stats;
2650 struct gfar_extra_stats *estats = &priv->extra_stats;
2652 /* If the packet was truncated, none of the other errors matter */
2653 if (status & RXBD_TRUNCATED) {
2654 stats->rx_length_errors++;
2660 /* Count the errors, if there were any */
2661 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2662 stats->rx_length_errors++;
2664 if (status & RXBD_LARGE)
2669 if (status & RXBD_NONOCTET) {
2670 stats->rx_frame_errors++;
2671 estats->rx_nonoctet++;
2673 if (status & RXBD_CRCERR) {
2674 estats->rx_crcerr++;
2675 stats->rx_crc_errors++;
2677 if (status & RXBD_OVERRUN) {
2678 estats->rx_overrun++;
2679 stats->rx_crc_errors++;
2683 irqreturn_t gfar_receive(int irq, void *grp_id)
2685 gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
2689 static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2691 /* If valid headers were found, and valid sums
2692 * were verified, then we tell the kernel that no
2693 * checksumming is necessary. Otherwise, it is [FIXME]
2695 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
2696 skb->ip_summed = CHECKSUM_UNNECESSARY;
2698 skb_checksum_none_assert(skb);
2702 /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
2703 static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
2704 int amount_pull, struct napi_struct *napi)
2706 struct gfar_private *priv = netdev_priv(dev);
2707 struct rxfcb *fcb = NULL;
2711 /* fcb is at the beginning if exists */
2712 fcb = (struct rxfcb *)skb->data;
2714 /* Remove the FCB from the skb
2715 * Remove the padded bytes, if there are any
2718 skb_record_rx_queue(skb, fcb->rq);
2719 skb_pull(skb, amount_pull);
2722 /* Get receive timestamp from the skb */
2723 if (priv->hwts_rx_en) {
2724 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2725 u64 *ns = (u64 *) skb->data;
2727 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2728 shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2732 skb_pull(skb, priv->padding);
2734 if (dev->features & NETIF_F_RXCSUM)
2735 gfar_rx_checksum(skb, fcb);
2737 /* Tell the skb what kind of packet this is */
2738 skb->protocol = eth_type_trans(skb, dev);
2740 /* There's need to check for NETIF_F_HW_VLAN_RX here.
2741 * Even if vlan rx accel is disabled, on some chips
2742 * RXFCB_VLN is pseudo randomly set.
2744 if (dev->features & NETIF_F_HW_VLAN_RX &&
2745 fcb->flags & RXFCB_VLN)
2746 __vlan_hwaccel_put_tag(skb, fcb->vlctl);
2748 /* Send the packet up the stack */
2749 ret = napi_gro_receive(napi, skb);
2751 if (GRO_DROP == ret)
2752 priv->extra_stats.kernel_dropped++;
2757 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
2758 * until the budget/quota has been reached. Returns the number
2761 int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
2763 struct net_device *dev = rx_queue->dev;
2764 struct rxbd8 *bdp, *base;
2765 struct sk_buff *skb;
2769 struct gfar_private *priv = netdev_priv(dev);
2771 /* Get the first full descriptor */
2772 bdp = rx_queue->cur_rx;
2773 base = rx_queue->rx_bd_base;
2775 amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0);
2777 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
2778 struct sk_buff *newskb;
2782 /* Add another skb for the future */
2783 newskb = gfar_new_skb(dev);
2785 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
2787 dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
2788 priv->rx_buffer_size, DMA_FROM_DEVICE);
2790 if (unlikely(!(bdp->status & RXBD_ERR) &&
2791 bdp->length > priv->rx_buffer_size))
2792 bdp->status = RXBD_LARGE;
2794 /* We drop the frame if we failed to allocate a new buffer */
2795 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
2796 bdp->status & RXBD_ERR)) {
2797 count_errors(bdp->status, dev);
2799 if (unlikely(!newskb))
2804 /* Increment the number of packets */
2805 rx_queue->stats.rx_packets++;
2809 pkt_len = bdp->length - ETH_FCS_LEN;
2810 /* Remove the FCS from the packet length */
2811 skb_put(skb, pkt_len);
2812 rx_queue->stats.rx_bytes += pkt_len;
2813 skb_record_rx_queue(skb, rx_queue->qindex);
2814 gfar_process_frame(dev, skb, amount_pull,
2815 &rx_queue->grp->napi);
2818 netif_warn(priv, rx_err, dev, "Missing skb!\n");
2819 rx_queue->stats.rx_dropped++;
2820 priv->extra_stats.rx_skbmissing++;
2825 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
2827 /* Setup the new bdp */
2828 gfar_new_rxbdp(rx_queue, bdp, newskb);
2830 /* Update to the next pointer */
2831 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
2833 /* update to point at the next skb */
2834 rx_queue->skb_currx = (rx_queue->skb_currx + 1) &
2835 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
2838 /* Update the current rxbd pointer to be the next one */
2839 rx_queue->cur_rx = bdp;
2844 static int gfar_poll(struct napi_struct *napi, int budget)
2846 struct gfar_priv_grp *gfargrp =
2847 container_of(napi, struct gfar_priv_grp, napi);
2848 struct gfar_private *priv = gfargrp->priv;
2849 struct gfar __iomem *regs = gfargrp->regs;
2850 struct gfar_priv_tx_q *tx_queue = NULL;
2851 struct gfar_priv_rx_q *rx_queue = NULL;
2852 int rx_cleaned = 0, budget_per_queue = 0, rx_cleaned_per_queue = 0;
2853 int tx_cleaned = 0, i, left_over_budget = budget;
2854 unsigned long serviced_queues = 0;
2857 num_queues = gfargrp->num_rx_queues;
2858 budget_per_queue = budget/num_queues;
2860 /* Clear IEVENT, so interrupts aren't called again
2861 * because of the packets that have already arrived
2863 gfar_write(®s->ievent, IEVENT_RTX_MASK);
2865 while (num_queues && left_over_budget) {
2866 budget_per_queue = left_over_budget/num_queues;
2867 left_over_budget = 0;
2869 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
2870 if (test_bit(i, &serviced_queues))
2872 rx_queue = priv->rx_queue[i];
2873 tx_queue = priv->tx_queue[rx_queue->qindex];
2875 tx_cleaned += gfar_clean_tx_ring(tx_queue);
2876 rx_cleaned_per_queue =
2877 gfar_clean_rx_ring(rx_queue, budget_per_queue);
2878 rx_cleaned += rx_cleaned_per_queue;
2879 if (rx_cleaned_per_queue < budget_per_queue) {
2880 left_over_budget = left_over_budget +
2882 rx_cleaned_per_queue);
2883 set_bit(i, &serviced_queues);
2892 if (rx_cleaned < budget) {
2893 napi_complete(napi);
2895 /* Clear the halt bit in RSTAT */
2896 gfar_write(®s->rstat, gfargrp->rstat);
2898 gfar_write(®s->imask, IMASK_DEFAULT);
2900 /* If we are coalescing interrupts, update the timer
2901 * Otherwise, clear it
2903 gfar_configure_coalescing(priv, gfargrp->rx_bit_map,
2904 gfargrp->tx_bit_map);
2910 #ifdef CONFIG_NET_POLL_CONTROLLER
2911 /* Polling 'interrupt' - used by things like netconsole to send skbs
2912 * without having to re-enable interrupts. It's not called while
2913 * the interrupt routine is executing.
2915 static void gfar_netpoll(struct net_device *dev)
2917 struct gfar_private *priv = netdev_priv(dev);
2920 /* If the device has multiple interrupts, run tx/rx */
2921 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2922 for (i = 0; i < priv->num_grps; i++) {
2923 disable_irq(priv->gfargrp[i].interruptTransmit);
2924 disable_irq(priv->gfargrp[i].interruptReceive);
2925 disable_irq(priv->gfargrp[i].interruptError);
2926 gfar_interrupt(priv->gfargrp[i].interruptTransmit,
2928 enable_irq(priv->gfargrp[i].interruptError);
2929 enable_irq(priv->gfargrp[i].interruptReceive);
2930 enable_irq(priv->gfargrp[i].interruptTransmit);
2933 for (i = 0; i < priv->num_grps; i++) {
2934 disable_irq(priv->gfargrp[i].interruptTransmit);
2935 gfar_interrupt(priv->gfargrp[i].interruptTransmit,
2937 enable_irq(priv->gfargrp[i].interruptTransmit);
2943 /* The interrupt handler for devices with one interrupt */
2944 static irqreturn_t gfar_interrupt(int irq, void *grp_id)
2946 struct gfar_priv_grp *gfargrp = grp_id;
2948 /* Save ievent for future reference */
2949 u32 events = gfar_read(&gfargrp->regs->ievent);
2951 /* Check for reception */
2952 if (events & IEVENT_RX_MASK)
2953 gfar_receive(irq, grp_id);
2955 /* Check for transmit completion */
2956 if (events & IEVENT_TX_MASK)
2957 gfar_transmit(irq, grp_id);
2959 /* Check for errors */
2960 if (events & IEVENT_ERR_MASK)
2961 gfar_error(irq, grp_id);
2966 /* Called every time the controller might need to be made
2967 * aware of new link state. The PHY code conveys this
2968 * information through variables in the phydev structure, and this
2969 * function converts those variables into the appropriate
2970 * register values, and can bring down the device if needed.
2972 static void adjust_link(struct net_device *dev)
2974 struct gfar_private *priv = netdev_priv(dev);
2975 struct gfar __iomem *regs = priv->gfargrp[0].regs;
2976 unsigned long flags;
2977 struct phy_device *phydev = priv->phydev;
2980 local_irq_save(flags);
2984 u32 tempval = gfar_read(®s->maccfg2);
2985 u32 ecntrl = gfar_read(®s->ecntrl);
2987 /* Now we make sure that we can be in full duplex mode.
2988 * If not, we operate in half-duplex mode.
2990 if (phydev->duplex != priv->oldduplex) {
2992 if (!(phydev->duplex))
2993 tempval &= ~(MACCFG2_FULL_DUPLEX);
2995 tempval |= MACCFG2_FULL_DUPLEX;
2997 priv->oldduplex = phydev->duplex;
3000 if (phydev->speed != priv->oldspeed) {
3002 switch (phydev->speed) {
3005 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
3007 ecntrl &= ~(ECNTRL_R100);
3012 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
3014 /* Reduced mode distinguishes
3015 * between 10 and 100
3017 if (phydev->speed == SPEED_100)
3018 ecntrl |= ECNTRL_R100;
3020 ecntrl &= ~(ECNTRL_R100);
3023 netif_warn(priv, link, dev,
3024 "Ack! Speed (%d) is not 10/100/1000!\n",
3029 priv->oldspeed = phydev->speed;
3032 gfar_write(®s->maccfg2, tempval);
3033 gfar_write(®s->ecntrl, ecntrl);
3035 if (!priv->oldlink) {
3039 } else if (priv->oldlink) {
3043 priv->oldduplex = -1;
3046 if (new_state && netif_msg_link(priv))
3047 phy_print_status(phydev);
3049 local_irq_restore(flags);
3052 /* Update the hash table based on the current list of multicast
3053 * addresses we subscribe to. Also, change the promiscuity of
3054 * the device based on the flags (this function is called
3055 * whenever dev->flags is changed
3057 static void gfar_set_multi(struct net_device *dev)
3059 struct netdev_hw_addr *ha;
3060 struct gfar_private *priv = netdev_priv(dev);
3061 struct gfar __iomem *regs = priv->gfargrp[0].regs;
3064 if (dev->flags & IFF_PROMISC) {
3065 /* Set RCTRL to PROM */
3066 tempval = gfar_read(®s->rctrl);
3067 tempval |= RCTRL_PROM;
3068 gfar_write(®s->rctrl, tempval);
3070 /* Set RCTRL to not PROM */
3071 tempval = gfar_read(®s->rctrl);
3072 tempval &= ~(RCTRL_PROM);
3073 gfar_write(®s->rctrl, tempval);
3076 if (dev->flags & IFF_ALLMULTI) {
3077 /* Set the hash to rx all multicast frames */
3078 gfar_write(®s->igaddr0, 0xffffffff);
3079 gfar_write(®s->igaddr1, 0xffffffff);
3080 gfar_write(®s->igaddr2, 0xffffffff);
3081 gfar_write(®s->igaddr3, 0xffffffff);
3082 gfar_write(®s->igaddr4, 0xffffffff);
3083 gfar_write(®s->igaddr5, 0xffffffff);
3084 gfar_write(®s->igaddr6, 0xffffffff);
3085 gfar_write(®s->igaddr7, 0xffffffff);
3086 gfar_write(®s->gaddr0, 0xffffffff);
3087 gfar_write(®s->gaddr1, 0xffffffff);
3088 gfar_write(®s->gaddr2, 0xffffffff);
3089 gfar_write(®s->gaddr3, 0xffffffff);
3090 gfar_write(®s->gaddr4, 0xffffffff);
3091 gfar_write(®s->gaddr5, 0xffffffff);
3092 gfar_write(®s->gaddr6, 0xffffffff);
3093 gfar_write(®s->gaddr7, 0xffffffff);
3098 /* zero out the hash */
3099 gfar_write(®s->igaddr0, 0x0);
3100 gfar_write(®s->igaddr1, 0x0);
3101 gfar_write(®s->igaddr2, 0x0);
3102 gfar_write(®s->igaddr3, 0x0);
3103 gfar_write(®s->igaddr4, 0x0);
3104 gfar_write(®s->igaddr5, 0x0);
3105 gfar_write(®s->igaddr6, 0x0);
3106 gfar_write(®s->igaddr7, 0x0);
3107 gfar_write(®s->gaddr0, 0x0);
3108 gfar_write(®s->gaddr1, 0x0);
3109 gfar_write(®s->gaddr2, 0x0);
3110 gfar_write(®s->gaddr3, 0x0);
3111 gfar_write(®s->gaddr4, 0x0);
3112 gfar_write(®s->gaddr5, 0x0);
3113 gfar_write(®s->gaddr6, 0x0);
3114 gfar_write(®s->gaddr7, 0x0);
3116 /* If we have extended hash tables, we need to
3117 * clear the exact match registers to prepare for
3120 if (priv->extended_hash) {
3121 em_num = GFAR_EM_NUM + 1;
3122 gfar_clear_exact_match(dev);
3129 if (netdev_mc_empty(dev))
3132 /* Parse the list, and set the appropriate bits */
3133 netdev_for_each_mc_addr(ha, dev) {
3135 gfar_set_mac_for_addr(dev, idx, ha->addr);
3138 gfar_set_hash_for_addr(dev, ha->addr);
3144 /* Clears each of the exact match registers to zero, so they
3145 * don't interfere with normal reception
3147 static void gfar_clear_exact_match(struct net_device *dev)
3150 static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
3152 for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
3153 gfar_set_mac_for_addr(dev, idx, zero_arr);
3156 /* Set the appropriate hash bit for the given addr */
3157 /* The algorithm works like so:
3158 * 1) Take the Destination Address (ie the multicast address), and
3159 * do a CRC on it (little endian), and reverse the bits of the
3161 * 2) Use the 8 most significant bits as a hash into a 256-entry
3162 * table. The table is controlled through 8 32-bit registers:
3163 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
3164 * gaddr7. This means that the 3 most significant bits in the
3165 * hash index which gaddr register to use, and the 5 other bits
3166 * indicate which bit (assuming an IBM numbering scheme, which
3167 * for PowerPC (tm) is usually the case) in the register holds
3170 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3173 struct gfar_private *priv = netdev_priv(dev);
3174 u32 result = ether_crc(ETH_ALEN, addr);
3175 int width = priv->hash_width;
3176 u8 whichbit = (result >> (32 - width)) & 0x1f;
3177 u8 whichreg = result >> (32 - width + 5);
3178 u32 value = (1 << (31-whichbit));
3180 tempval = gfar_read(priv->hash_regs[whichreg]);
3182 gfar_write(priv->hash_regs[whichreg], tempval);
3186 /* There are multiple MAC Address register pairs on some controllers
3187 * This function sets the numth pair to a given address
3189 static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3192 struct gfar_private *priv = netdev_priv(dev);
3193 struct gfar __iomem *regs = priv->gfargrp[0].regs;
3195 char tmpbuf[ETH_ALEN];
3197 u32 __iomem *macptr = ®s->macstnaddr1;
3201 /* Now copy it into the mac registers backwards, cuz
3202 * little endian is silly
3204 for (idx = 0; idx < ETH_ALEN; idx++)
3205 tmpbuf[ETH_ALEN - 1 - idx] = addr[idx];
3207 gfar_write(macptr, *((u32 *) (tmpbuf)));
3209 tempval = *((u32 *) (tmpbuf + 4));
3211 gfar_write(macptr+1, tempval);
3214 /* GFAR error interrupt handler */
3215 static irqreturn_t gfar_error(int irq, void *grp_id)
3217 struct gfar_priv_grp *gfargrp = grp_id;
3218 struct gfar __iomem *regs = gfargrp->regs;
3219 struct gfar_private *priv= gfargrp->priv;
3220 struct net_device *dev = priv->ndev;
3222 /* Save ievent for future reference */
3223 u32 events = gfar_read(®s->ievent);
3226 gfar_write(®s->ievent, events & IEVENT_ERR_MASK);
3228 /* Magic Packet is not an error. */
3229 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
3230 (events & IEVENT_MAG))
3231 events &= ~IEVENT_MAG;
3234 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
3236 "error interrupt (ievent=0x%08x imask=0x%08x)\n",
3237 events, gfar_read(®s->imask));
3239 /* Update the error counters */
3240 if (events & IEVENT_TXE) {
3241 dev->stats.tx_errors++;
3243 if (events & IEVENT_LC)
3244 dev->stats.tx_window_errors++;
3245 if (events & IEVENT_CRL)
3246 dev->stats.tx_aborted_errors++;
3247 if (events & IEVENT_XFUN) {
3248 unsigned long flags;
3250 netif_dbg(priv, tx_err, dev,
3251 "TX FIFO underrun, packet dropped\n");
3252 dev->stats.tx_dropped++;
3253 priv->extra_stats.tx_underrun++;
3255 local_irq_save(flags);
3258 /* Reactivate the Tx Queues */
3259 gfar_write(®s->tstat, gfargrp->tstat);
3262 local_irq_restore(flags);
3264 netif_dbg(priv, tx_err, dev, "Transmit Error\n");
3266 if (events & IEVENT_BSY) {
3267 dev->stats.rx_errors++;
3268 priv->extra_stats.rx_bsy++;
3270 gfar_receive(irq, grp_id);
3272 netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3273 gfar_read(®s->rstat));
3275 if (events & IEVENT_BABR) {
3276 dev->stats.rx_errors++;
3277 priv->extra_stats.rx_babr++;
3279 netif_dbg(priv, rx_err, dev, "babbling RX error\n");
3281 if (events & IEVENT_EBERR) {
3282 priv->extra_stats.eberr++;
3283 netif_dbg(priv, rx_err, dev, "bus error\n");
3285 if (events & IEVENT_RXC)
3286 netif_dbg(priv, rx_status, dev, "control frame\n");
3288 if (events & IEVENT_BABT) {
3289 priv->extra_stats.tx_babt++;
3290 netif_dbg(priv, tx_err, dev, "babbling TX error\n");
3295 static struct of_device_id gfar_match[] =
3299 .compatible = "gianfar",
3302 .compatible = "fsl,etsec2",
3306 MODULE_DEVICE_TABLE(of, gfar_match);
3308 /* Structure for a device driver */
3309 static struct platform_driver gfar_driver = {
3311 .name = "fsl-gianfar",
3312 .owner = THIS_MODULE,
3314 .of_match_table = gfar_match,
3316 .probe = gfar_probe,
3317 .remove = gfar_remove,
3320 module_platform_driver(gfar_driver);