xref: /openbmc/linux/drivers/net/ethernet/freescale/gianfar.c (revision 1de65a5ea32de7b335ab505366d45cefadbbdf71)
10977f817SJan Ceuleers /* drivers/net/ethernet/freescale/gianfar.c
2ec21e2ecSJeff Kirsher  *
3ec21e2ecSJeff Kirsher  * Gianfar Ethernet Driver
4ec21e2ecSJeff Kirsher  * This driver is designed for the non-CPM ethernet controllers
5ec21e2ecSJeff Kirsher  * on the 85xx and 83xx family of integrated processors
6ec21e2ecSJeff Kirsher  * Based on 8260_io/fcc_enet.c
7ec21e2ecSJeff Kirsher  *
8ec21e2ecSJeff Kirsher  * Author: Andy Fleming
9ec21e2ecSJeff Kirsher  * Maintainer: Kumar Gala
10ec21e2ecSJeff Kirsher  * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
11ec21e2ecSJeff Kirsher  *
1220862788SClaudiu Manoil  * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc.
13ec21e2ecSJeff Kirsher  * Copyright 2007 MontaVista Software, Inc.
14ec21e2ecSJeff Kirsher  *
15ec21e2ecSJeff Kirsher  * This program is free software; you can redistribute  it and/or modify it
16ec21e2ecSJeff Kirsher  * under  the terms of  the GNU General  Public License as published by the
17ec21e2ecSJeff Kirsher  * Free Software Foundation;  either version 2 of the  License, or (at your
18ec21e2ecSJeff Kirsher  * option) any later version.
19ec21e2ecSJeff Kirsher  *
20ec21e2ecSJeff Kirsher  *  Gianfar:  AKA Lambda Draconis, "Dragon"
21ec21e2ecSJeff Kirsher  *  RA 11 31 24.2
22ec21e2ecSJeff Kirsher  *  Dec +69 19 52
23ec21e2ecSJeff Kirsher  *  V 3.84
24ec21e2ecSJeff Kirsher  *  B-V +1.62
25ec21e2ecSJeff Kirsher  *
26ec21e2ecSJeff Kirsher  *  Theory of operation
27ec21e2ecSJeff Kirsher  *
28ec21e2ecSJeff Kirsher  *  The driver is initialized through of_device. Configuration information
29ec21e2ecSJeff Kirsher  *  is therefore conveyed through an OF-style device tree.
30ec21e2ecSJeff Kirsher  *
31ec21e2ecSJeff Kirsher  *  The Gianfar Ethernet Controller uses a ring of buffer
32ec21e2ecSJeff Kirsher  *  descriptors.  The beginning is indicated by a register
33ec21e2ecSJeff Kirsher  *  pointing to the physical address of the start of the ring.
34ec21e2ecSJeff Kirsher  *  The end is determined by a "wrap" bit being set in the
35ec21e2ecSJeff Kirsher  *  last descriptor of the ring.
36ec21e2ecSJeff Kirsher  *
37ec21e2ecSJeff Kirsher  *  When a packet is received, the RXF bit in the
38ec21e2ecSJeff Kirsher  *  IEVENT register is set, triggering an interrupt when the
39ec21e2ecSJeff Kirsher  *  corresponding bit in the IMASK register is also set (if
40ec21e2ecSJeff Kirsher  *  interrupt coalescing is active, then the interrupt may not
41ec21e2ecSJeff Kirsher  *  happen immediately, but will wait until either a set number
42ec21e2ecSJeff Kirsher  *  of frames or amount of time have passed).  In NAPI, the
43ec21e2ecSJeff Kirsher  *  interrupt handler will signal there is work to be done, and
44ec21e2ecSJeff Kirsher  *  exit. This method will start at the last known empty
45ec21e2ecSJeff Kirsher  *  descriptor, and process every subsequent descriptor until there
46ec21e2ecSJeff Kirsher  *  are none left with data (NAPI will stop after a set number of
47ec21e2ecSJeff Kirsher  *  packets to give time to other tasks, but will eventually
48ec21e2ecSJeff Kirsher  *  process all the packets).  The data arrives inside a
49ec21e2ecSJeff Kirsher  *  pre-allocated skb, and so after the skb is passed up to the
50ec21e2ecSJeff Kirsher  *  stack, a new skb must be allocated, and the address field in
51ec21e2ecSJeff Kirsher  *  the buffer descriptor must be updated to indicate this new
52ec21e2ecSJeff Kirsher  *  skb.
53ec21e2ecSJeff Kirsher  *
54ec21e2ecSJeff Kirsher  *  When the kernel requests that a packet be transmitted, the
55ec21e2ecSJeff Kirsher  *  driver starts where it left off last time, and points the
56ec21e2ecSJeff Kirsher  *  descriptor at the buffer which was passed in.  The driver
57ec21e2ecSJeff Kirsher  *  then informs the DMA engine that there are packets ready to
58ec21e2ecSJeff Kirsher  *  be transmitted.  Once the controller is finished transmitting
59ec21e2ecSJeff Kirsher  *  the packet, an interrupt may be triggered (under the same
60ec21e2ecSJeff Kirsher  *  conditions as for reception, but depending on the TXF bit).
61ec21e2ecSJeff Kirsher  *  The driver then cleans up the buffer.
62ec21e2ecSJeff Kirsher  */
63ec21e2ecSJeff Kirsher 
64ec21e2ecSJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
65ec21e2ecSJeff Kirsher #define DEBUG
66ec21e2ecSJeff Kirsher 
67ec21e2ecSJeff Kirsher #include <linux/kernel.h>
68ec21e2ecSJeff Kirsher #include <linux/string.h>
69ec21e2ecSJeff Kirsher #include <linux/errno.h>
70ec21e2ecSJeff Kirsher #include <linux/unistd.h>
71ec21e2ecSJeff Kirsher #include <linux/slab.h>
72ec21e2ecSJeff Kirsher #include <linux/interrupt.h>
73ec21e2ecSJeff Kirsher #include <linux/delay.h>
74ec21e2ecSJeff Kirsher #include <linux/netdevice.h>
75ec21e2ecSJeff Kirsher #include <linux/etherdevice.h>
76ec21e2ecSJeff Kirsher #include <linux/skbuff.h>
77ec21e2ecSJeff Kirsher #include <linux/if_vlan.h>
78ec21e2ecSJeff Kirsher #include <linux/spinlock.h>
79ec21e2ecSJeff Kirsher #include <linux/mm.h>
805af50730SRob Herring #include <linux/of_address.h>
815af50730SRob Herring #include <linux/of_irq.h>
82ec21e2ecSJeff Kirsher #include <linux/of_mdio.h>
83ec21e2ecSJeff Kirsher #include <linux/of_platform.h>
84ec21e2ecSJeff Kirsher #include <linux/ip.h>
85ec21e2ecSJeff Kirsher #include <linux/tcp.h>
86ec21e2ecSJeff Kirsher #include <linux/udp.h>
87ec21e2ecSJeff Kirsher #include <linux/in.h>
88ec21e2ecSJeff Kirsher #include <linux/net_tstamp.h>
89ec21e2ecSJeff Kirsher 
90ec21e2ecSJeff Kirsher #include <asm/io.h>
91d6ef0bccSClaudiu Manoil #ifdef CONFIG_PPC
92ec21e2ecSJeff Kirsher #include <asm/reg.h>
932969b1f7SClaudiu Manoil #include <asm/mpc85xx.h>
94d6ef0bccSClaudiu Manoil #endif
95ec21e2ecSJeff Kirsher #include <asm/irq.h>
96ec21e2ecSJeff Kirsher #include <asm/uaccess.h>
97ec21e2ecSJeff Kirsher #include <linux/module.h>
98ec21e2ecSJeff Kirsher #include <linux/dma-mapping.h>
99ec21e2ecSJeff Kirsher #include <linux/crc32.h>
100ec21e2ecSJeff Kirsher #include <linux/mii.h>
101ec21e2ecSJeff Kirsher #include <linux/phy.h>
102ec21e2ecSJeff Kirsher #include <linux/phy_fixed.h>
103ec21e2ecSJeff Kirsher #include <linux/of.h>
104ec21e2ecSJeff Kirsher #include <linux/of_net.h>
105fd31a952SClaudiu Manoil #include <linux/of_address.h>
106fd31a952SClaudiu Manoil #include <linux/of_irq.h>
107ec21e2ecSJeff Kirsher 
108ec21e2ecSJeff Kirsher #include "gianfar.h"
109ec21e2ecSJeff Kirsher 
110ec21e2ecSJeff Kirsher #define TX_TIMEOUT      (1*HZ)
111ec21e2ecSJeff Kirsher 
11275354148SClaudiu Manoil const char gfar_driver_version[] = "2.0";
113ec21e2ecSJeff Kirsher 
114ec21e2ecSJeff Kirsher static int gfar_enet_open(struct net_device *dev);
115ec21e2ecSJeff Kirsher static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
116ec21e2ecSJeff Kirsher static void gfar_reset_task(struct work_struct *work);
117ec21e2ecSJeff Kirsher static void gfar_timeout(struct net_device *dev);
118ec21e2ecSJeff Kirsher static int gfar_close(struct net_device *dev);
11976f31e8bSClaudiu Manoil static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue,
12076f31e8bSClaudiu Manoil 				int alloc_cnt);
121ec21e2ecSJeff Kirsher static int gfar_set_mac_address(struct net_device *dev);
122ec21e2ecSJeff Kirsher static int gfar_change_mtu(struct net_device *dev, int new_mtu);
123ec21e2ecSJeff Kirsher static irqreturn_t gfar_error(int irq, void *dev_id);
124ec21e2ecSJeff Kirsher static irqreturn_t gfar_transmit(int irq, void *dev_id);
125ec21e2ecSJeff Kirsher static irqreturn_t gfar_interrupt(int irq, void *dev_id);
126ec21e2ecSJeff Kirsher static void adjust_link(struct net_device *dev);
1276ce29b0eSClaudiu Manoil static noinline void gfar_update_link_state(struct gfar_private *priv);
128ec21e2ecSJeff Kirsher static int init_phy(struct net_device *dev);
129ec21e2ecSJeff Kirsher static int gfar_probe(struct platform_device *ofdev);
130ec21e2ecSJeff Kirsher static int gfar_remove(struct platform_device *ofdev);
131ec21e2ecSJeff Kirsher static void free_skb_resources(struct gfar_private *priv);
132ec21e2ecSJeff Kirsher static void gfar_set_multi(struct net_device *dev);
133ec21e2ecSJeff Kirsher static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
134ec21e2ecSJeff Kirsher static void gfar_configure_serdes(struct net_device *dev);
135aeb12c5eSClaudiu Manoil static int gfar_poll_rx(struct napi_struct *napi, int budget);
136aeb12c5eSClaudiu Manoil static int gfar_poll_tx(struct napi_struct *napi, int budget);
137aeb12c5eSClaudiu Manoil static int gfar_poll_rx_sq(struct napi_struct *napi, int budget);
138aeb12c5eSClaudiu Manoil static int gfar_poll_tx_sq(struct napi_struct *napi, int budget);
139ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
140ec21e2ecSJeff Kirsher static void gfar_netpoll(struct net_device *dev);
141ec21e2ecSJeff Kirsher #endif
142ec21e2ecSJeff Kirsher int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
143c233cf40SClaudiu Manoil static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
144f23223f1SClaudiu Manoil static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb);
145c10650b6SClaudiu Manoil static void gfar_halt_nodisable(struct gfar_private *priv);
146ec21e2ecSJeff Kirsher static void gfar_clear_exact_match(struct net_device *dev);
147ec21e2ecSJeff Kirsher static void gfar_set_mac_for_addr(struct net_device *dev, int num,
148ec21e2ecSJeff Kirsher 				  const u8 *addr);
149ec21e2ecSJeff Kirsher static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
150ec21e2ecSJeff Kirsher 
151ec21e2ecSJeff Kirsher MODULE_AUTHOR("Freescale Semiconductor, Inc");
152ec21e2ecSJeff Kirsher MODULE_DESCRIPTION("Gianfar Ethernet Driver");
153ec21e2ecSJeff Kirsher MODULE_LICENSE("GPL");
154ec21e2ecSJeff Kirsher 
155ec21e2ecSJeff Kirsher static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
156ec21e2ecSJeff Kirsher 			    dma_addr_t buf)
157ec21e2ecSJeff Kirsher {
158ec21e2ecSJeff Kirsher 	u32 lstatus;
159ec21e2ecSJeff Kirsher 
160a7312d58SClaudiu Manoil 	bdp->bufPtr = cpu_to_be32(buf);
161ec21e2ecSJeff Kirsher 
162ec21e2ecSJeff Kirsher 	lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
163ec21e2ecSJeff Kirsher 	if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
164ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(RXBD_WRAP);
165ec21e2ecSJeff Kirsher 
166d55398baSClaudiu Manoil 	gfar_wmb();
167ec21e2ecSJeff Kirsher 
168a7312d58SClaudiu Manoil 	bdp->lstatus = cpu_to_be32(lstatus);
169ec21e2ecSJeff Kirsher }
170ec21e2ecSJeff Kirsher 
17176f31e8bSClaudiu Manoil static void gfar_init_bds(struct net_device *ndev)
172ec21e2ecSJeff Kirsher {
173ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(ndev);
17445b679c9SMatei Pavaluca 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
175ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
176ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
177ec21e2ecSJeff Kirsher 	struct txbd8 *txbdp;
17803366a33SKevin Hao 	u32 __iomem *rfbptr;
179ec21e2ecSJeff Kirsher 	int i, j;
180ec21e2ecSJeff Kirsher 
181ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
182ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
183ec21e2ecSJeff Kirsher 		/* Initialize some variables in our dev structure */
184ec21e2ecSJeff Kirsher 		tx_queue->num_txbdfree = tx_queue->tx_ring_size;
185ec21e2ecSJeff Kirsher 		tx_queue->dirty_tx = tx_queue->tx_bd_base;
186ec21e2ecSJeff Kirsher 		tx_queue->cur_tx = tx_queue->tx_bd_base;
187ec21e2ecSJeff Kirsher 		tx_queue->skb_curtx = 0;
188ec21e2ecSJeff Kirsher 		tx_queue->skb_dirtytx = 0;
189ec21e2ecSJeff Kirsher 
190ec21e2ecSJeff Kirsher 		/* Initialize Transmit Descriptor Ring */
191ec21e2ecSJeff Kirsher 		txbdp = tx_queue->tx_bd_base;
192ec21e2ecSJeff Kirsher 		for (j = 0; j < tx_queue->tx_ring_size; j++) {
193ec21e2ecSJeff Kirsher 			txbdp->lstatus = 0;
194ec21e2ecSJeff Kirsher 			txbdp->bufPtr = 0;
195ec21e2ecSJeff Kirsher 			txbdp++;
196ec21e2ecSJeff Kirsher 		}
197ec21e2ecSJeff Kirsher 
198ec21e2ecSJeff Kirsher 		/* Set the last descriptor in the ring to indicate wrap */
199ec21e2ecSJeff Kirsher 		txbdp--;
200a7312d58SClaudiu Manoil 		txbdp->status = cpu_to_be16(be16_to_cpu(txbdp->status) |
201a7312d58SClaudiu Manoil 					    TXBD_WRAP);
202ec21e2ecSJeff Kirsher 	}
203ec21e2ecSJeff Kirsher 
20445b679c9SMatei Pavaluca 	rfbptr = &regs->rfbptr0;
205ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
206ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
207ec21e2ecSJeff Kirsher 
20876f31e8bSClaudiu Manoil 		rx_queue->next_to_clean = 0;
20976f31e8bSClaudiu Manoil 		rx_queue->next_to_use = 0;
21075354148SClaudiu Manoil 		rx_queue->next_to_alloc = 0;
211ec21e2ecSJeff Kirsher 
21276f31e8bSClaudiu Manoil 		/* make sure next_to_clean != next_to_use after this
21376f31e8bSClaudiu Manoil 		 * by leaving at least 1 unused descriptor
21476f31e8bSClaudiu Manoil 		 */
21576f31e8bSClaudiu Manoil 		gfar_alloc_rx_buffs(rx_queue, gfar_rxbd_unused(rx_queue));
216ec21e2ecSJeff Kirsher 
21745b679c9SMatei Pavaluca 		rx_queue->rfbptr = rfbptr;
21845b679c9SMatei Pavaluca 		rfbptr += 2;
219ec21e2ecSJeff Kirsher 	}
220ec21e2ecSJeff Kirsher }
221ec21e2ecSJeff Kirsher 
222ec21e2ecSJeff Kirsher static int gfar_alloc_skb_resources(struct net_device *ndev)
223ec21e2ecSJeff Kirsher {
224ec21e2ecSJeff Kirsher 	void *vaddr;
225ec21e2ecSJeff Kirsher 	dma_addr_t addr;
22675354148SClaudiu Manoil 	int i, j;
227ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(ndev);
228369ec162SClaudiu Manoil 	struct device *dev = priv->dev;
229ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
230ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
231ec21e2ecSJeff Kirsher 
232ec21e2ecSJeff Kirsher 	priv->total_tx_ring_size = 0;
233ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
234ec21e2ecSJeff Kirsher 		priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
235ec21e2ecSJeff Kirsher 
236ec21e2ecSJeff Kirsher 	priv->total_rx_ring_size = 0;
237ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
238ec21e2ecSJeff Kirsher 		priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
239ec21e2ecSJeff Kirsher 
240ec21e2ecSJeff Kirsher 	/* Allocate memory for the buffer descriptors */
241ec21e2ecSJeff Kirsher 	vaddr = dma_alloc_coherent(dev,
242d0320f75SJoe Perches 				   (priv->total_tx_ring_size *
243d0320f75SJoe Perches 				    sizeof(struct txbd8)) +
244d0320f75SJoe Perches 				   (priv->total_rx_ring_size *
245d0320f75SJoe Perches 				    sizeof(struct rxbd8)),
246ec21e2ecSJeff Kirsher 				   &addr, GFP_KERNEL);
247d0320f75SJoe Perches 	if (!vaddr)
248ec21e2ecSJeff Kirsher 		return -ENOMEM;
249ec21e2ecSJeff Kirsher 
250ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
251ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
252ec21e2ecSJeff Kirsher 		tx_queue->tx_bd_base = vaddr;
253ec21e2ecSJeff Kirsher 		tx_queue->tx_bd_dma_base = addr;
254ec21e2ecSJeff Kirsher 		tx_queue->dev = ndev;
255ec21e2ecSJeff Kirsher 		/* enet DMA only understands physical addresses */
256ec21e2ecSJeff Kirsher 		addr  += sizeof(struct txbd8) * tx_queue->tx_ring_size;
257ec21e2ecSJeff Kirsher 		vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
258ec21e2ecSJeff Kirsher 	}
259ec21e2ecSJeff Kirsher 
260ec21e2ecSJeff Kirsher 	/* Start the rx descriptor ring where the tx ring leaves off */
261ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
262ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
263ec21e2ecSJeff Kirsher 		rx_queue->rx_bd_base = vaddr;
264ec21e2ecSJeff Kirsher 		rx_queue->rx_bd_dma_base = addr;
265f23223f1SClaudiu Manoil 		rx_queue->ndev = ndev;
26675354148SClaudiu Manoil 		rx_queue->dev = dev;
267ec21e2ecSJeff Kirsher 		addr  += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
268ec21e2ecSJeff Kirsher 		vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
269ec21e2ecSJeff Kirsher 	}
270ec21e2ecSJeff Kirsher 
271ec21e2ecSJeff Kirsher 	/* Setup the skbuff rings */
272ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
273ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
27414f8dc49SJoe Perches 		tx_queue->tx_skbuff =
27514f8dc49SJoe Perches 			kmalloc_array(tx_queue->tx_ring_size,
27614f8dc49SJoe Perches 				      sizeof(*tx_queue->tx_skbuff),
277bc4598bcSJan Ceuleers 				      GFP_KERNEL);
27814f8dc49SJoe Perches 		if (!tx_queue->tx_skbuff)
279ec21e2ecSJeff Kirsher 			goto cleanup;
280ec21e2ecSJeff Kirsher 
28175354148SClaudiu Manoil 		for (j = 0; j < tx_queue->tx_ring_size; j++)
28275354148SClaudiu Manoil 			tx_queue->tx_skbuff[j] = NULL;
283ec21e2ecSJeff Kirsher 	}
284ec21e2ecSJeff Kirsher 
285ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
286ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
28775354148SClaudiu Manoil 		rx_queue->rx_buff = kcalloc(rx_queue->rx_ring_size,
28875354148SClaudiu Manoil 					    sizeof(*rx_queue->rx_buff),
289bc4598bcSJan Ceuleers 					    GFP_KERNEL);
29075354148SClaudiu Manoil 		if (!rx_queue->rx_buff)
291ec21e2ecSJeff Kirsher 			goto cleanup;
292ec21e2ecSJeff Kirsher 	}
293ec21e2ecSJeff Kirsher 
29476f31e8bSClaudiu Manoil 	gfar_init_bds(ndev);
295ec21e2ecSJeff Kirsher 
296ec21e2ecSJeff Kirsher 	return 0;
297ec21e2ecSJeff Kirsher 
298ec21e2ecSJeff Kirsher cleanup:
299ec21e2ecSJeff Kirsher 	free_skb_resources(priv);
300ec21e2ecSJeff Kirsher 	return -ENOMEM;
301ec21e2ecSJeff Kirsher }
302ec21e2ecSJeff Kirsher 
303ec21e2ecSJeff Kirsher static void gfar_init_tx_rx_base(struct gfar_private *priv)
304ec21e2ecSJeff Kirsher {
305ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
306ec21e2ecSJeff Kirsher 	u32 __iomem *baddr;
307ec21e2ecSJeff Kirsher 	int i;
308ec21e2ecSJeff Kirsher 
309ec21e2ecSJeff Kirsher 	baddr = &regs->tbase0;
310ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
311ec21e2ecSJeff Kirsher 		gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
312ec21e2ecSJeff Kirsher 		baddr += 2;
313ec21e2ecSJeff Kirsher 	}
314ec21e2ecSJeff Kirsher 
315ec21e2ecSJeff Kirsher 	baddr = &regs->rbase0;
316ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
317ec21e2ecSJeff Kirsher 		gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
318ec21e2ecSJeff Kirsher 		baddr += 2;
319ec21e2ecSJeff Kirsher 	}
320ec21e2ecSJeff Kirsher }
321ec21e2ecSJeff Kirsher 
32245b679c9SMatei Pavaluca static void gfar_init_rqprm(struct gfar_private *priv)
32345b679c9SMatei Pavaluca {
32445b679c9SMatei Pavaluca 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
32545b679c9SMatei Pavaluca 	u32 __iomem *baddr;
32645b679c9SMatei Pavaluca 	int i;
32745b679c9SMatei Pavaluca 
32845b679c9SMatei Pavaluca 	baddr = &regs->rqprm0;
32945b679c9SMatei Pavaluca 	for (i = 0; i < priv->num_rx_queues; i++) {
33045b679c9SMatei Pavaluca 		gfar_write(baddr, priv->rx_queue[i]->rx_ring_size |
33145b679c9SMatei Pavaluca 			   (DEFAULT_RX_LFC_THR << FBTHR_SHIFT));
33245b679c9SMatei Pavaluca 		baddr++;
33345b679c9SMatei Pavaluca 	}
33445b679c9SMatei Pavaluca }
33545b679c9SMatei Pavaluca 
33675354148SClaudiu Manoil static void gfar_rx_offload_en(struct gfar_private *priv)
33788302648SClaudiu Manoil {
33888302648SClaudiu Manoil 	/* set this when rx hw offload (TOE) functions are being used */
33988302648SClaudiu Manoil 	priv->uses_rxfcb = 0;
34088302648SClaudiu Manoil 
34188302648SClaudiu Manoil 	if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
34288302648SClaudiu Manoil 		priv->uses_rxfcb = 1;
34388302648SClaudiu Manoil 
34415bf176dSClaudiu Manoil 	if (priv->hwts_rx_en || priv->rx_filer_enable)
34588302648SClaudiu Manoil 		priv->uses_rxfcb = 1;
34688302648SClaudiu Manoil }
34788302648SClaudiu Manoil 
348a328ac92SClaudiu Manoil static void gfar_mac_rx_config(struct gfar_private *priv)
349ec21e2ecSJeff Kirsher {
350ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
351ec21e2ecSJeff Kirsher 	u32 rctrl = 0;
352ec21e2ecSJeff Kirsher 
353ec21e2ecSJeff Kirsher 	if (priv->rx_filer_enable) {
35415bf176dSClaudiu Manoil 		rctrl |= RCTRL_FILREN | RCTRL_PRSDEP_INIT;
355ec21e2ecSJeff Kirsher 		/* Program the RIR0 reg with the required distribution */
35671ff9e3dSClaudiu Manoil 		if (priv->poll_mode == GFAR_SQ_POLLING)
35771ff9e3dSClaudiu Manoil 			gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
35871ff9e3dSClaudiu Manoil 		else /* GFAR_MQ_POLLING */
35971ff9e3dSClaudiu Manoil 			gfar_write(&regs->rir0, DEFAULT_8RXQ_RIR0);
360ec21e2ecSJeff Kirsher 	}
361ec21e2ecSJeff Kirsher 
362f5ae6279SClaudiu Manoil 	/* Restore PROMISC mode */
363a328ac92SClaudiu Manoil 	if (priv->ndev->flags & IFF_PROMISC)
364f5ae6279SClaudiu Manoil 		rctrl |= RCTRL_PROM;
365f5ae6279SClaudiu Manoil 
36688302648SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_RXCSUM)
367ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_CHECKSUMMING;
368ec21e2ecSJeff Kirsher 
36988302648SClaudiu Manoil 	if (priv->extended_hash)
37088302648SClaudiu Manoil 		rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
371ec21e2ecSJeff Kirsher 
372ec21e2ecSJeff Kirsher 	if (priv->padding) {
373ec21e2ecSJeff Kirsher 		rctrl &= ~RCTRL_PAL_MASK;
374ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_PADDING(priv->padding);
375ec21e2ecSJeff Kirsher 	}
376ec21e2ecSJeff Kirsher 
377ec21e2ecSJeff Kirsher 	/* Enable HW time stamping if requested from user space */
37888302648SClaudiu Manoil 	if (priv->hwts_rx_en)
379ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
380ec21e2ecSJeff Kirsher 
38188302648SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
382ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
383ec21e2ecSJeff Kirsher 
38445b679c9SMatei Pavaluca 	/* Clear the LFC bit */
38545b679c9SMatei Pavaluca 	gfar_write(&regs->rctrl, rctrl);
38645b679c9SMatei Pavaluca 	/* Init flow control threshold values */
38745b679c9SMatei Pavaluca 	gfar_init_rqprm(priv);
38845b679c9SMatei Pavaluca 	gfar_write(&regs->ptv, DEFAULT_LFC_PTVVAL);
38945b679c9SMatei Pavaluca 	rctrl |= RCTRL_LFC;
39045b679c9SMatei Pavaluca 
391ec21e2ecSJeff Kirsher 	/* Init rctrl based on our settings */
392ec21e2ecSJeff Kirsher 	gfar_write(&regs->rctrl, rctrl);
393a328ac92SClaudiu Manoil }
394ec21e2ecSJeff Kirsher 
395a328ac92SClaudiu Manoil static void gfar_mac_tx_config(struct gfar_private *priv)
396a328ac92SClaudiu Manoil {
397a328ac92SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
398a328ac92SClaudiu Manoil 	u32 tctrl = 0;
399a328ac92SClaudiu Manoil 
400a328ac92SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_IP_CSUM)
401ec21e2ecSJeff Kirsher 		tctrl |= TCTRL_INIT_CSUM;
402ec21e2ecSJeff Kirsher 
403b98b8babSClaudiu Manoil 	if (priv->prio_sched_en)
404ec21e2ecSJeff Kirsher 		tctrl |= TCTRL_TXSCHED_PRIO;
405b98b8babSClaudiu Manoil 	else {
406b98b8babSClaudiu Manoil 		tctrl |= TCTRL_TXSCHED_WRRS;
407b98b8babSClaudiu Manoil 		gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
408b98b8babSClaudiu Manoil 		gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
409b98b8babSClaudiu Manoil 	}
410ec21e2ecSJeff Kirsher 
41188302648SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
41288302648SClaudiu Manoil 		tctrl |= TCTRL_VLINS;
41388302648SClaudiu Manoil 
414ec21e2ecSJeff Kirsher 	gfar_write(&regs->tctrl, tctrl);
415ec21e2ecSJeff Kirsher }
416ec21e2ecSJeff Kirsher 
417f19015baSClaudiu Manoil static void gfar_configure_coalescing(struct gfar_private *priv,
418f19015baSClaudiu Manoil 			       unsigned long tx_mask, unsigned long rx_mask)
419f19015baSClaudiu Manoil {
420f19015baSClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
421f19015baSClaudiu Manoil 	u32 __iomem *baddr;
422f19015baSClaudiu Manoil 
423f19015baSClaudiu Manoil 	if (priv->mode == MQ_MG_MODE) {
424f19015baSClaudiu Manoil 		int i = 0;
425f19015baSClaudiu Manoil 
426f19015baSClaudiu Manoil 		baddr = &regs->txic0;
427f19015baSClaudiu Manoil 		for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
428f19015baSClaudiu Manoil 			gfar_write(baddr + i, 0);
429f19015baSClaudiu Manoil 			if (likely(priv->tx_queue[i]->txcoalescing))
430f19015baSClaudiu Manoil 				gfar_write(baddr + i, priv->tx_queue[i]->txic);
431f19015baSClaudiu Manoil 		}
432f19015baSClaudiu Manoil 
433f19015baSClaudiu Manoil 		baddr = &regs->rxic0;
434f19015baSClaudiu Manoil 		for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
435f19015baSClaudiu Manoil 			gfar_write(baddr + i, 0);
436f19015baSClaudiu Manoil 			if (likely(priv->rx_queue[i]->rxcoalescing))
437f19015baSClaudiu Manoil 				gfar_write(baddr + i, priv->rx_queue[i]->rxic);
438f19015baSClaudiu Manoil 		}
439f19015baSClaudiu Manoil 	} else {
440f19015baSClaudiu Manoil 		/* Backward compatible case -- even if we enable
441f19015baSClaudiu Manoil 		 * multiple queues, there's only single reg to program
442f19015baSClaudiu Manoil 		 */
443f19015baSClaudiu Manoil 		gfar_write(&regs->txic, 0);
444f19015baSClaudiu Manoil 		if (likely(priv->tx_queue[0]->txcoalescing))
445f19015baSClaudiu Manoil 			gfar_write(&regs->txic, priv->tx_queue[0]->txic);
446f19015baSClaudiu Manoil 
447f19015baSClaudiu Manoil 		gfar_write(&regs->rxic, 0);
448f19015baSClaudiu Manoil 		if (unlikely(priv->rx_queue[0]->rxcoalescing))
449f19015baSClaudiu Manoil 			gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
450f19015baSClaudiu Manoil 	}
451f19015baSClaudiu Manoil }
452f19015baSClaudiu Manoil 
453f19015baSClaudiu Manoil void gfar_configure_coalescing_all(struct gfar_private *priv)
454f19015baSClaudiu Manoil {
455f19015baSClaudiu Manoil 	gfar_configure_coalescing(priv, 0xFF, 0xFF);
456f19015baSClaudiu Manoil }
457f19015baSClaudiu Manoil 
458ec21e2ecSJeff Kirsher static struct net_device_stats *gfar_get_stats(struct net_device *dev)
459ec21e2ecSJeff Kirsher {
460ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
461ec21e2ecSJeff Kirsher 	unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
462ec21e2ecSJeff Kirsher 	unsigned long tx_packets = 0, tx_bytes = 0;
4633a2e16c8SJan Ceuleers 	int i;
464ec21e2ecSJeff Kirsher 
465ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
466ec21e2ecSJeff Kirsher 		rx_packets += priv->rx_queue[i]->stats.rx_packets;
467ec21e2ecSJeff Kirsher 		rx_bytes   += priv->rx_queue[i]->stats.rx_bytes;
468ec21e2ecSJeff Kirsher 		rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
469ec21e2ecSJeff Kirsher 	}
470ec21e2ecSJeff Kirsher 
471ec21e2ecSJeff Kirsher 	dev->stats.rx_packets = rx_packets;
472ec21e2ecSJeff Kirsher 	dev->stats.rx_bytes   = rx_bytes;
473ec21e2ecSJeff Kirsher 	dev->stats.rx_dropped = rx_dropped;
474ec21e2ecSJeff Kirsher 
475ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
476ec21e2ecSJeff Kirsher 		tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
477ec21e2ecSJeff Kirsher 		tx_packets += priv->tx_queue[i]->stats.tx_packets;
478ec21e2ecSJeff Kirsher 	}
479ec21e2ecSJeff Kirsher 
480ec21e2ecSJeff Kirsher 	dev->stats.tx_bytes   = tx_bytes;
481ec21e2ecSJeff Kirsher 	dev->stats.tx_packets = tx_packets;
482ec21e2ecSJeff Kirsher 
483ec21e2ecSJeff Kirsher 	return &dev->stats;
484ec21e2ecSJeff Kirsher }
485ec21e2ecSJeff Kirsher 
4863d23a05cSClaudiu Manoil static int gfar_set_mac_addr(struct net_device *dev, void *p)
4873d23a05cSClaudiu Manoil {
4883d23a05cSClaudiu Manoil 	eth_mac_addr(dev, p);
4893d23a05cSClaudiu Manoil 
4903d23a05cSClaudiu Manoil 	gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
4913d23a05cSClaudiu Manoil 
4923d23a05cSClaudiu Manoil 	return 0;
4933d23a05cSClaudiu Manoil }
4943d23a05cSClaudiu Manoil 
495ec21e2ecSJeff Kirsher static const struct net_device_ops gfar_netdev_ops = {
496ec21e2ecSJeff Kirsher 	.ndo_open = gfar_enet_open,
497ec21e2ecSJeff Kirsher 	.ndo_start_xmit = gfar_start_xmit,
498ec21e2ecSJeff Kirsher 	.ndo_stop = gfar_close,
499ec21e2ecSJeff Kirsher 	.ndo_change_mtu = gfar_change_mtu,
500ec21e2ecSJeff Kirsher 	.ndo_set_features = gfar_set_features,
501afc4b13dSJiri Pirko 	.ndo_set_rx_mode = gfar_set_multi,
502ec21e2ecSJeff Kirsher 	.ndo_tx_timeout = gfar_timeout,
503ec21e2ecSJeff Kirsher 	.ndo_do_ioctl = gfar_ioctl,
504ec21e2ecSJeff Kirsher 	.ndo_get_stats = gfar_get_stats,
5053d23a05cSClaudiu Manoil 	.ndo_set_mac_address = gfar_set_mac_addr,
506ec21e2ecSJeff Kirsher 	.ndo_validate_addr = eth_validate_addr,
507ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
508ec21e2ecSJeff Kirsher 	.ndo_poll_controller = gfar_netpoll,
509ec21e2ecSJeff Kirsher #endif
510ec21e2ecSJeff Kirsher };
511ec21e2ecSJeff Kirsher 
512efeddce7SClaudiu Manoil static void gfar_ints_disable(struct gfar_private *priv)
513efeddce7SClaudiu Manoil {
514efeddce7SClaudiu Manoil 	int i;
515efeddce7SClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
516efeddce7SClaudiu Manoil 		struct gfar __iomem *regs = priv->gfargrp[i].regs;
517efeddce7SClaudiu Manoil 		/* Clear IEVENT */
518efeddce7SClaudiu Manoil 		gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
519efeddce7SClaudiu Manoil 
520efeddce7SClaudiu Manoil 		/* Initialize IMASK */
521efeddce7SClaudiu Manoil 		gfar_write(&regs->imask, IMASK_INIT_CLEAR);
522efeddce7SClaudiu Manoil 	}
523efeddce7SClaudiu Manoil }
524efeddce7SClaudiu Manoil 
525efeddce7SClaudiu Manoil static void gfar_ints_enable(struct gfar_private *priv)
526efeddce7SClaudiu Manoil {
527efeddce7SClaudiu Manoil 	int i;
528efeddce7SClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
529efeddce7SClaudiu Manoil 		struct gfar __iomem *regs = priv->gfargrp[i].regs;
530efeddce7SClaudiu Manoil 		/* Unmask the interrupts we look for */
531efeddce7SClaudiu Manoil 		gfar_write(&regs->imask, IMASK_DEFAULT);
532efeddce7SClaudiu Manoil 	}
533efeddce7SClaudiu Manoil }
534efeddce7SClaudiu Manoil 
53520862788SClaudiu Manoil static int gfar_alloc_tx_queues(struct gfar_private *priv)
53620862788SClaudiu Manoil {
53720862788SClaudiu Manoil 	int i;
53820862788SClaudiu Manoil 
53920862788SClaudiu Manoil 	for (i = 0; i < priv->num_tx_queues; i++) {
54020862788SClaudiu Manoil 		priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
54120862788SClaudiu Manoil 					    GFP_KERNEL);
54220862788SClaudiu Manoil 		if (!priv->tx_queue[i])
54320862788SClaudiu Manoil 			return -ENOMEM;
54420862788SClaudiu Manoil 
54520862788SClaudiu Manoil 		priv->tx_queue[i]->tx_skbuff = NULL;
54620862788SClaudiu Manoil 		priv->tx_queue[i]->qindex = i;
54720862788SClaudiu Manoil 		priv->tx_queue[i]->dev = priv->ndev;
54820862788SClaudiu Manoil 		spin_lock_init(&(priv->tx_queue[i]->txlock));
54920862788SClaudiu Manoil 	}
55020862788SClaudiu Manoil 	return 0;
55120862788SClaudiu Manoil }
55220862788SClaudiu Manoil 
55320862788SClaudiu Manoil static int gfar_alloc_rx_queues(struct gfar_private *priv)
55420862788SClaudiu Manoil {
55520862788SClaudiu Manoil 	int i;
55620862788SClaudiu Manoil 
55720862788SClaudiu Manoil 	for (i = 0; i < priv->num_rx_queues; i++) {
55820862788SClaudiu Manoil 		priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
55920862788SClaudiu Manoil 					    GFP_KERNEL);
56020862788SClaudiu Manoil 		if (!priv->rx_queue[i])
56120862788SClaudiu Manoil 			return -ENOMEM;
56220862788SClaudiu Manoil 
56320862788SClaudiu Manoil 		priv->rx_queue[i]->qindex = i;
564f23223f1SClaudiu Manoil 		priv->rx_queue[i]->ndev = priv->ndev;
56520862788SClaudiu Manoil 	}
56620862788SClaudiu Manoil 	return 0;
56720862788SClaudiu Manoil }
56820862788SClaudiu Manoil 
56920862788SClaudiu Manoil static void gfar_free_tx_queues(struct gfar_private *priv)
570ec21e2ecSJeff Kirsher {
5713a2e16c8SJan Ceuleers 	int i;
572ec21e2ecSJeff Kirsher 
573ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
574ec21e2ecSJeff Kirsher 		kfree(priv->tx_queue[i]);
575ec21e2ecSJeff Kirsher }
576ec21e2ecSJeff Kirsher 
57720862788SClaudiu Manoil static void gfar_free_rx_queues(struct gfar_private *priv)
578ec21e2ecSJeff Kirsher {
5793a2e16c8SJan Ceuleers 	int i;
580ec21e2ecSJeff Kirsher 
581ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
582ec21e2ecSJeff Kirsher 		kfree(priv->rx_queue[i]);
583ec21e2ecSJeff Kirsher }
584ec21e2ecSJeff Kirsher 
585ec21e2ecSJeff Kirsher static void unmap_group_regs(struct gfar_private *priv)
586ec21e2ecSJeff Kirsher {
5873a2e16c8SJan Ceuleers 	int i;
588ec21e2ecSJeff Kirsher 
589ec21e2ecSJeff Kirsher 	for (i = 0; i < MAXGROUPS; i++)
590ec21e2ecSJeff Kirsher 		if (priv->gfargrp[i].regs)
591ec21e2ecSJeff Kirsher 			iounmap(priv->gfargrp[i].regs);
592ec21e2ecSJeff Kirsher }
593ec21e2ecSJeff Kirsher 
594ee873fdaSClaudiu Manoil static void free_gfar_dev(struct gfar_private *priv)
595ee873fdaSClaudiu Manoil {
596ee873fdaSClaudiu Manoil 	int i, j;
597ee873fdaSClaudiu Manoil 
598ee873fdaSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++)
599ee873fdaSClaudiu Manoil 		for (j = 0; j < GFAR_NUM_IRQS; j++) {
600ee873fdaSClaudiu Manoil 			kfree(priv->gfargrp[i].irqinfo[j]);
601ee873fdaSClaudiu Manoil 			priv->gfargrp[i].irqinfo[j] = NULL;
602ee873fdaSClaudiu Manoil 		}
603ee873fdaSClaudiu Manoil 
604ee873fdaSClaudiu Manoil 	free_netdev(priv->ndev);
605ee873fdaSClaudiu Manoil }
606ee873fdaSClaudiu Manoil 
607ec21e2ecSJeff Kirsher static void disable_napi(struct gfar_private *priv)
608ec21e2ecSJeff Kirsher {
6093a2e16c8SJan Ceuleers 	int i;
610ec21e2ecSJeff Kirsher 
611aeb12c5eSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
612aeb12c5eSClaudiu Manoil 		napi_disable(&priv->gfargrp[i].napi_rx);
613aeb12c5eSClaudiu Manoil 		napi_disable(&priv->gfargrp[i].napi_tx);
614aeb12c5eSClaudiu Manoil 	}
615ec21e2ecSJeff Kirsher }
616ec21e2ecSJeff Kirsher 
617ec21e2ecSJeff Kirsher static void enable_napi(struct gfar_private *priv)
618ec21e2ecSJeff Kirsher {
6193a2e16c8SJan Ceuleers 	int i;
620ec21e2ecSJeff Kirsher 
621aeb12c5eSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
622aeb12c5eSClaudiu Manoil 		napi_enable(&priv->gfargrp[i].napi_rx);
623aeb12c5eSClaudiu Manoil 		napi_enable(&priv->gfargrp[i].napi_tx);
624aeb12c5eSClaudiu Manoil 	}
625ec21e2ecSJeff Kirsher }
626ec21e2ecSJeff Kirsher 
627ec21e2ecSJeff Kirsher static int gfar_parse_group(struct device_node *np,
628ec21e2ecSJeff Kirsher 			    struct gfar_private *priv, const char *model)
629ec21e2ecSJeff Kirsher {
6305fedcc14SClaudiu Manoil 	struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
631ee873fdaSClaudiu Manoil 	int i;
632ee873fdaSClaudiu Manoil 
633ee873fdaSClaudiu Manoil 	for (i = 0; i < GFAR_NUM_IRQS; i++) {
634ee873fdaSClaudiu Manoil 		grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
635ee873fdaSClaudiu Manoil 					  GFP_KERNEL);
636ee873fdaSClaudiu Manoil 		if (!grp->irqinfo[i])
637ee873fdaSClaudiu Manoil 			return -ENOMEM;
638ee873fdaSClaudiu Manoil 	}
639ec21e2ecSJeff Kirsher 
6405fedcc14SClaudiu Manoil 	grp->regs = of_iomap(np, 0);
6415fedcc14SClaudiu Manoil 	if (!grp->regs)
642ec21e2ecSJeff Kirsher 		return -ENOMEM;
643ec21e2ecSJeff Kirsher 
644ee873fdaSClaudiu Manoil 	gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
645ec21e2ecSJeff Kirsher 
646ec21e2ecSJeff Kirsher 	/* If we aren't the FEC we have multiple interrupts */
647ec21e2ecSJeff Kirsher 	if (model && strcasecmp(model, "FEC")) {
648ee873fdaSClaudiu Manoil 		gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
649ee873fdaSClaudiu Manoil 		gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
650ee873fdaSClaudiu Manoil 		if (gfar_irq(grp, TX)->irq == NO_IRQ ||
651ee873fdaSClaudiu Manoil 		    gfar_irq(grp, RX)->irq == NO_IRQ ||
652ee873fdaSClaudiu Manoil 		    gfar_irq(grp, ER)->irq == NO_IRQ)
653ec21e2ecSJeff Kirsher 			return -EINVAL;
654ec21e2ecSJeff Kirsher 	}
655ec21e2ecSJeff Kirsher 
6565fedcc14SClaudiu Manoil 	grp->priv = priv;
6575fedcc14SClaudiu Manoil 	spin_lock_init(&grp->grplock);
658ec21e2ecSJeff Kirsher 	if (priv->mode == MQ_MG_MODE) {
65955917641SJingchang Lu 		u32 rxq_mask, txq_mask;
66055917641SJingchang Lu 		int ret;
66155917641SJingchang Lu 
66255917641SJingchang Lu 		grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
66355917641SJingchang Lu 		grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
66455917641SJingchang Lu 
66555917641SJingchang Lu 		ret = of_property_read_u32(np, "fsl,rx-bit-map", &rxq_mask);
66655917641SJingchang Lu 		if (!ret) {
66755917641SJingchang Lu 			grp->rx_bit_map = rxq_mask ?
66855917641SJingchang Lu 			rxq_mask : (DEFAULT_MAPPING >> priv->num_grps);
66955917641SJingchang Lu 		}
67055917641SJingchang Lu 
67155917641SJingchang Lu 		ret = of_property_read_u32(np, "fsl,tx-bit-map", &txq_mask);
67255917641SJingchang Lu 		if (!ret) {
67355917641SJingchang Lu 			grp->tx_bit_map = txq_mask ?
67455917641SJingchang Lu 			txq_mask : (DEFAULT_MAPPING >> priv->num_grps);
67555917641SJingchang Lu 		}
67671ff9e3dSClaudiu Manoil 
67771ff9e3dSClaudiu Manoil 		if (priv->poll_mode == GFAR_SQ_POLLING) {
67871ff9e3dSClaudiu Manoil 			/* One Q per interrupt group: Q0 to G0, Q1 to G1 */
67971ff9e3dSClaudiu Manoil 			grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
68071ff9e3dSClaudiu Manoil 			grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
68171ff9e3dSClaudiu Manoil 		}
682ec21e2ecSJeff Kirsher 	} else {
6835fedcc14SClaudiu Manoil 		grp->rx_bit_map = 0xFF;
6845fedcc14SClaudiu Manoil 		grp->tx_bit_map = 0xFF;
685ec21e2ecSJeff Kirsher 	}
68620862788SClaudiu Manoil 
68720862788SClaudiu Manoil 	/* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
68820862788SClaudiu Manoil 	 * right to left, so we need to revert the 8 bits to get the q index
68920862788SClaudiu Manoil 	 */
69020862788SClaudiu Manoil 	grp->rx_bit_map = bitrev8(grp->rx_bit_map);
69120862788SClaudiu Manoil 	grp->tx_bit_map = bitrev8(grp->tx_bit_map);
69220862788SClaudiu Manoil 
69320862788SClaudiu Manoil 	/* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
69420862788SClaudiu Manoil 	 * also assign queues to groups
69520862788SClaudiu Manoil 	 */
69620862788SClaudiu Manoil 	for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
69771ff9e3dSClaudiu Manoil 		if (!grp->rx_queue)
69871ff9e3dSClaudiu Manoil 			grp->rx_queue = priv->rx_queue[i];
69920862788SClaudiu Manoil 		grp->num_rx_queues++;
70020862788SClaudiu Manoil 		grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
70120862788SClaudiu Manoil 		priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
70220862788SClaudiu Manoil 		priv->rx_queue[i]->grp = grp;
70320862788SClaudiu Manoil 	}
70420862788SClaudiu Manoil 
70520862788SClaudiu Manoil 	for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
70671ff9e3dSClaudiu Manoil 		if (!grp->tx_queue)
70771ff9e3dSClaudiu Manoil 			grp->tx_queue = priv->tx_queue[i];
70820862788SClaudiu Manoil 		grp->num_tx_queues++;
70920862788SClaudiu Manoil 		grp->tstat |= (TSTAT_CLEAR_THALT >> i);
71020862788SClaudiu Manoil 		priv->tqueue |= (TQUEUE_EN0 >> i);
71120862788SClaudiu Manoil 		priv->tx_queue[i]->grp = grp;
71220862788SClaudiu Manoil 	}
71320862788SClaudiu Manoil 
714ec21e2ecSJeff Kirsher 	priv->num_grps++;
715ec21e2ecSJeff Kirsher 
716ec21e2ecSJeff Kirsher 	return 0;
717ec21e2ecSJeff Kirsher }
718ec21e2ecSJeff Kirsher 
719f50724cdSTobias Waldekranz static int gfar_of_group_count(struct device_node *np)
720f50724cdSTobias Waldekranz {
721f50724cdSTobias Waldekranz 	struct device_node *child;
722f50724cdSTobias Waldekranz 	int num = 0;
723f50724cdSTobias Waldekranz 
724f50724cdSTobias Waldekranz 	for_each_available_child_of_node(np, child)
725f50724cdSTobias Waldekranz 		if (!of_node_cmp(child->name, "queue-group"))
726f50724cdSTobias Waldekranz 			num++;
727f50724cdSTobias Waldekranz 
728f50724cdSTobias Waldekranz 	return num;
729f50724cdSTobias Waldekranz }
730f50724cdSTobias Waldekranz 
731ec21e2ecSJeff Kirsher static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
732ec21e2ecSJeff Kirsher {
733ec21e2ecSJeff Kirsher 	const char *model;
734ec21e2ecSJeff Kirsher 	const char *ctype;
735ec21e2ecSJeff Kirsher 	const void *mac_addr;
736ec21e2ecSJeff Kirsher 	int err = 0, i;
737ec21e2ecSJeff Kirsher 	struct net_device *dev = NULL;
738ec21e2ecSJeff Kirsher 	struct gfar_private *priv = NULL;
739ec21e2ecSJeff Kirsher 	struct device_node *np = ofdev->dev.of_node;
740ec21e2ecSJeff Kirsher 	struct device_node *child = NULL;
74155917641SJingchang Lu 	struct property *stash;
74255917641SJingchang Lu 	u32 stash_len = 0;
74355917641SJingchang Lu 	u32 stash_idx = 0;
744ec21e2ecSJeff Kirsher 	unsigned int num_tx_qs, num_rx_qs;
745b338ce27SClaudiu Manoil 	unsigned short mode, poll_mode;
746ec21e2ecSJeff Kirsher 
7474b222ca6SKevin Hao 	if (!np)
748ec21e2ecSJeff Kirsher 		return -ENODEV;
749ec21e2ecSJeff Kirsher 
750b338ce27SClaudiu Manoil 	if (of_device_is_compatible(np, "fsl,etsec2")) {
751b338ce27SClaudiu Manoil 		mode = MQ_MG_MODE;
752b338ce27SClaudiu Manoil 		poll_mode = GFAR_SQ_POLLING;
753b338ce27SClaudiu Manoil 	} else {
754b338ce27SClaudiu Manoil 		mode = SQ_SG_MODE;
755b338ce27SClaudiu Manoil 		poll_mode = GFAR_SQ_POLLING;
756b338ce27SClaudiu Manoil 	}
757b338ce27SClaudiu Manoil 
758b338ce27SClaudiu Manoil 	if (mode == SQ_SG_MODE) {
75971ff9e3dSClaudiu Manoil 		num_tx_qs = 1;
76071ff9e3dSClaudiu Manoil 		num_rx_qs = 1;
76171ff9e3dSClaudiu Manoil 	} else { /* MQ_MG_MODE */
762c65d7533SClaudiu Manoil 		/* get the actual number of supported groups */
763f50724cdSTobias Waldekranz 		unsigned int num_grps = gfar_of_group_count(np);
764c65d7533SClaudiu Manoil 
765c65d7533SClaudiu Manoil 		if (num_grps == 0 || num_grps > MAXGROUPS) {
766c65d7533SClaudiu Manoil 			dev_err(&ofdev->dev, "Invalid # of int groups(%d)\n",
767c65d7533SClaudiu Manoil 				num_grps);
768c65d7533SClaudiu Manoil 			pr_err("Cannot do alloc_etherdev, aborting\n");
769c65d7533SClaudiu Manoil 			return -EINVAL;
770c65d7533SClaudiu Manoil 		}
771c65d7533SClaudiu Manoil 
772b338ce27SClaudiu Manoil 		if (poll_mode == GFAR_SQ_POLLING) {
773c65d7533SClaudiu Manoil 			num_tx_qs = num_grps; /* one txq per int group */
774c65d7533SClaudiu Manoil 			num_rx_qs = num_grps; /* one rxq per int group */
77571ff9e3dSClaudiu Manoil 		} else { /* GFAR_MQ_POLLING */
77655917641SJingchang Lu 			u32 tx_queues, rx_queues;
77755917641SJingchang Lu 			int ret;
77855917641SJingchang Lu 
77955917641SJingchang Lu 			/* parse the num of HW tx and rx queues */
78055917641SJingchang Lu 			ret = of_property_read_u32(np, "fsl,num_tx_queues",
78155917641SJingchang Lu 						   &tx_queues);
78255917641SJingchang Lu 			num_tx_qs = ret ? 1 : tx_queues;
78355917641SJingchang Lu 
78455917641SJingchang Lu 			ret = of_property_read_u32(np, "fsl,num_rx_queues",
78555917641SJingchang Lu 						   &rx_queues);
78655917641SJingchang Lu 			num_rx_qs = ret ? 1 : rx_queues;
78771ff9e3dSClaudiu Manoil 		}
78871ff9e3dSClaudiu Manoil 	}
789ec21e2ecSJeff Kirsher 
790ec21e2ecSJeff Kirsher 	if (num_tx_qs > MAX_TX_QS) {
791ec21e2ecSJeff Kirsher 		pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
792ec21e2ecSJeff Kirsher 		       num_tx_qs, MAX_TX_QS);
793ec21e2ecSJeff Kirsher 		pr_err("Cannot do alloc_etherdev, aborting\n");
794ec21e2ecSJeff Kirsher 		return -EINVAL;
795ec21e2ecSJeff Kirsher 	}
796ec21e2ecSJeff Kirsher 
797ec21e2ecSJeff Kirsher 	if (num_rx_qs > MAX_RX_QS) {
798ec21e2ecSJeff Kirsher 		pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
799ec21e2ecSJeff Kirsher 		       num_rx_qs, MAX_RX_QS);
800ec21e2ecSJeff Kirsher 		pr_err("Cannot do alloc_etherdev, aborting\n");
801ec21e2ecSJeff Kirsher 		return -EINVAL;
802ec21e2ecSJeff Kirsher 	}
803ec21e2ecSJeff Kirsher 
804ec21e2ecSJeff Kirsher 	*pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
805ec21e2ecSJeff Kirsher 	dev = *pdev;
806ec21e2ecSJeff Kirsher 	if (NULL == dev)
807ec21e2ecSJeff Kirsher 		return -ENOMEM;
808ec21e2ecSJeff Kirsher 
809ec21e2ecSJeff Kirsher 	priv = netdev_priv(dev);
810ec21e2ecSJeff Kirsher 	priv->ndev = dev;
811ec21e2ecSJeff Kirsher 
812b338ce27SClaudiu Manoil 	priv->mode = mode;
813b338ce27SClaudiu Manoil 	priv->poll_mode = poll_mode;
814b338ce27SClaudiu Manoil 
815ec21e2ecSJeff Kirsher 	priv->num_tx_queues = num_tx_qs;
816ec21e2ecSJeff Kirsher 	netif_set_real_num_rx_queues(dev, num_rx_qs);
817ec21e2ecSJeff Kirsher 	priv->num_rx_queues = num_rx_qs;
81820862788SClaudiu Manoil 
81920862788SClaudiu Manoil 	err = gfar_alloc_tx_queues(priv);
82020862788SClaudiu Manoil 	if (err)
82120862788SClaudiu Manoil 		goto tx_alloc_failed;
82220862788SClaudiu Manoil 
82320862788SClaudiu Manoil 	err = gfar_alloc_rx_queues(priv);
82420862788SClaudiu Manoil 	if (err)
82520862788SClaudiu Manoil 		goto rx_alloc_failed;
826ec21e2ecSJeff Kirsher 
82755917641SJingchang Lu 	err = of_property_read_string(np, "model", &model);
82855917641SJingchang Lu 	if (err) {
82955917641SJingchang Lu 		pr_err("Device model property missing, aborting\n");
83055917641SJingchang Lu 		goto rx_alloc_failed;
83155917641SJingchang Lu 	}
83255917641SJingchang Lu 
833ec21e2ecSJeff Kirsher 	/* Init Rx queue filer rule set linked list */
834ec21e2ecSJeff Kirsher 	INIT_LIST_HEAD(&priv->rx_list.list);
835ec21e2ecSJeff Kirsher 	priv->rx_list.count = 0;
836ec21e2ecSJeff Kirsher 	mutex_init(&priv->rx_queue_access);
837ec21e2ecSJeff Kirsher 
838ec21e2ecSJeff Kirsher 	for (i = 0; i < MAXGROUPS; i++)
839ec21e2ecSJeff Kirsher 		priv->gfargrp[i].regs = NULL;
840ec21e2ecSJeff Kirsher 
841ec21e2ecSJeff Kirsher 	/* Parse and initialize group specific information */
842b338ce27SClaudiu Manoil 	if (priv->mode == MQ_MG_MODE) {
843f50724cdSTobias Waldekranz 		for_each_available_child_of_node(np, child) {
844f50724cdSTobias Waldekranz 			if (of_node_cmp(child->name, "queue-group"))
845f50724cdSTobias Waldekranz 				continue;
846f50724cdSTobias Waldekranz 
847ec21e2ecSJeff Kirsher 			err = gfar_parse_group(child, priv, model);
848ec21e2ecSJeff Kirsher 			if (err)
849ec21e2ecSJeff Kirsher 				goto err_grp_init;
850ec21e2ecSJeff Kirsher 		}
851b338ce27SClaudiu Manoil 	} else { /* SQ_SG_MODE */
852ec21e2ecSJeff Kirsher 		err = gfar_parse_group(np, priv, model);
853ec21e2ecSJeff Kirsher 		if (err)
854ec21e2ecSJeff Kirsher 			goto err_grp_init;
855ec21e2ecSJeff Kirsher 	}
856ec21e2ecSJeff Kirsher 
85755917641SJingchang Lu 	stash = of_find_property(np, "bd-stash", NULL);
858ec21e2ecSJeff Kirsher 
859ec21e2ecSJeff Kirsher 	if (stash) {
860ec21e2ecSJeff Kirsher 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
861ec21e2ecSJeff Kirsher 		priv->bd_stash_en = 1;
862ec21e2ecSJeff Kirsher 	}
863ec21e2ecSJeff Kirsher 
86455917641SJingchang Lu 	err = of_property_read_u32(np, "rx-stash-len", &stash_len);
865ec21e2ecSJeff Kirsher 
86655917641SJingchang Lu 	if (err == 0)
86755917641SJingchang Lu 		priv->rx_stash_size = stash_len;
868ec21e2ecSJeff Kirsher 
86955917641SJingchang Lu 	err = of_property_read_u32(np, "rx-stash-idx", &stash_idx);
870ec21e2ecSJeff Kirsher 
87155917641SJingchang Lu 	if (err == 0)
87255917641SJingchang Lu 		priv->rx_stash_index = stash_idx;
873ec21e2ecSJeff Kirsher 
874ec21e2ecSJeff Kirsher 	if (stash_len || stash_idx)
875ec21e2ecSJeff Kirsher 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
876ec21e2ecSJeff Kirsher 
877ec21e2ecSJeff Kirsher 	mac_addr = of_get_mac_address(np);
878bc4598bcSJan Ceuleers 
879ec21e2ecSJeff Kirsher 	if (mac_addr)
8806a3c910cSJoe Perches 		memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
881ec21e2ecSJeff Kirsher 
882ec21e2ecSJeff Kirsher 	if (model && !strcasecmp(model, "TSEC"))
88334018fd4SClaudiu Manoil 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
884ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_COALESCE |
885ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_RMON |
886ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_MULTI_INTR;
887bc4598bcSJan Ceuleers 
888ec21e2ecSJeff Kirsher 	if (model && !strcasecmp(model, "eTSEC"))
88934018fd4SClaudiu Manoil 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
890ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_COALESCE |
891ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_RMON |
892ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_MULTI_INTR |
893ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_CSUM |
894ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_VLAN |
895ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
896ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
897ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_TIMER;
898ec21e2ecSJeff Kirsher 
89955917641SJingchang Lu 	err = of_property_read_string(np, "phy-connection-type", &ctype);
900ec21e2ecSJeff Kirsher 
901ec21e2ecSJeff Kirsher 	/* We only care about rgmii-id.  The rest are autodetected */
90255917641SJingchang Lu 	if (err == 0 && !strcmp(ctype, "rgmii-id"))
903ec21e2ecSJeff Kirsher 		priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
904ec21e2ecSJeff Kirsher 	else
905ec21e2ecSJeff Kirsher 		priv->interface = PHY_INTERFACE_MODE_MII;
906ec21e2ecSJeff Kirsher 
90755917641SJingchang Lu 	if (of_find_property(np, "fsl,magic-packet", NULL))
908ec21e2ecSJeff Kirsher 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
909ec21e2ecSJeff Kirsher 
910ec21e2ecSJeff Kirsher 	priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
911ec21e2ecSJeff Kirsher 
912be403645SFlorian Fainelli 	/* In the case of a fixed PHY, the DT node associated
913be403645SFlorian Fainelli 	 * to the PHY is the Ethernet MAC DT node.
914be403645SFlorian Fainelli 	 */
9156f2c9bd8SUwe Kleine-König 	if (!priv->phy_node && of_phy_is_fixed_link(np)) {
916be403645SFlorian Fainelli 		err = of_phy_register_fixed_link(np);
917be403645SFlorian Fainelli 		if (err)
918be403645SFlorian Fainelli 			goto err_grp_init;
919be403645SFlorian Fainelli 
9206f2c9bd8SUwe Kleine-König 		priv->phy_node = of_node_get(np);
921be403645SFlorian Fainelli 	}
922be403645SFlorian Fainelli 
923ec21e2ecSJeff Kirsher 	/* Find the TBI PHY.  If it's not there, we don't support SGMII */
924ec21e2ecSJeff Kirsher 	priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
925ec21e2ecSJeff Kirsher 
926ec21e2ecSJeff Kirsher 	return 0;
927ec21e2ecSJeff Kirsher 
928ec21e2ecSJeff Kirsher err_grp_init:
929ec21e2ecSJeff Kirsher 	unmap_group_regs(priv);
93020862788SClaudiu Manoil rx_alloc_failed:
93120862788SClaudiu Manoil 	gfar_free_rx_queues(priv);
93220862788SClaudiu Manoil tx_alloc_failed:
93320862788SClaudiu Manoil 	gfar_free_tx_queues(priv);
934ee873fdaSClaudiu Manoil 	free_gfar_dev(priv);
935ec21e2ecSJeff Kirsher 	return err;
936ec21e2ecSJeff Kirsher }
937ec21e2ecSJeff Kirsher 
938ca0c88c2SBen Hutchings static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
939ec21e2ecSJeff Kirsher {
940ec21e2ecSJeff Kirsher 	struct hwtstamp_config config;
941ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(netdev);
942ec21e2ecSJeff Kirsher 
943ec21e2ecSJeff Kirsher 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
944ec21e2ecSJeff Kirsher 		return -EFAULT;
945ec21e2ecSJeff Kirsher 
946ec21e2ecSJeff Kirsher 	/* reserved for future extensions */
947ec21e2ecSJeff Kirsher 	if (config.flags)
948ec21e2ecSJeff Kirsher 		return -EINVAL;
949ec21e2ecSJeff Kirsher 
950ec21e2ecSJeff Kirsher 	switch (config.tx_type) {
951ec21e2ecSJeff Kirsher 	case HWTSTAMP_TX_OFF:
952ec21e2ecSJeff Kirsher 		priv->hwts_tx_en = 0;
953ec21e2ecSJeff Kirsher 		break;
954ec21e2ecSJeff Kirsher 	case HWTSTAMP_TX_ON:
955ec21e2ecSJeff Kirsher 		if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
956ec21e2ecSJeff Kirsher 			return -ERANGE;
957ec21e2ecSJeff Kirsher 		priv->hwts_tx_en = 1;
958ec21e2ecSJeff Kirsher 		break;
959ec21e2ecSJeff Kirsher 	default:
960ec21e2ecSJeff Kirsher 		return -ERANGE;
961ec21e2ecSJeff Kirsher 	}
962ec21e2ecSJeff Kirsher 
963ec21e2ecSJeff Kirsher 	switch (config.rx_filter) {
964ec21e2ecSJeff Kirsher 	case HWTSTAMP_FILTER_NONE:
965ec21e2ecSJeff Kirsher 		if (priv->hwts_rx_en) {
966ec21e2ecSJeff Kirsher 			priv->hwts_rx_en = 0;
9670851133bSClaudiu Manoil 			reset_gfar(netdev);
968ec21e2ecSJeff Kirsher 		}
969ec21e2ecSJeff Kirsher 		break;
970ec21e2ecSJeff Kirsher 	default:
971ec21e2ecSJeff Kirsher 		if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
972ec21e2ecSJeff Kirsher 			return -ERANGE;
973ec21e2ecSJeff Kirsher 		if (!priv->hwts_rx_en) {
974ec21e2ecSJeff Kirsher 			priv->hwts_rx_en = 1;
9750851133bSClaudiu Manoil 			reset_gfar(netdev);
976ec21e2ecSJeff Kirsher 		}
977ec21e2ecSJeff Kirsher 		config.rx_filter = HWTSTAMP_FILTER_ALL;
978ec21e2ecSJeff Kirsher 		break;
979ec21e2ecSJeff Kirsher 	}
980ec21e2ecSJeff Kirsher 
981ec21e2ecSJeff Kirsher 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
982ec21e2ecSJeff Kirsher 		-EFAULT : 0;
983ec21e2ecSJeff Kirsher }
984ec21e2ecSJeff Kirsher 
985ca0c88c2SBen Hutchings static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
986ca0c88c2SBen Hutchings {
987ca0c88c2SBen Hutchings 	struct hwtstamp_config config;
988ca0c88c2SBen Hutchings 	struct gfar_private *priv = netdev_priv(netdev);
989ca0c88c2SBen Hutchings 
990ca0c88c2SBen Hutchings 	config.flags = 0;
991ca0c88c2SBen Hutchings 	config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
992ca0c88c2SBen Hutchings 	config.rx_filter = (priv->hwts_rx_en ?
993ca0c88c2SBen Hutchings 			    HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
994ca0c88c2SBen Hutchings 
995ca0c88c2SBen Hutchings 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
996ca0c88c2SBen Hutchings 		-EFAULT : 0;
997ca0c88c2SBen Hutchings }
998ca0c88c2SBen Hutchings 
999ec21e2ecSJeff Kirsher static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1000ec21e2ecSJeff Kirsher {
1001ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1002ec21e2ecSJeff Kirsher 
1003ec21e2ecSJeff Kirsher 	if (!netif_running(dev))
1004ec21e2ecSJeff Kirsher 		return -EINVAL;
1005ec21e2ecSJeff Kirsher 
1006ec21e2ecSJeff Kirsher 	if (cmd == SIOCSHWTSTAMP)
1007ca0c88c2SBen Hutchings 		return gfar_hwtstamp_set(dev, rq);
1008ca0c88c2SBen Hutchings 	if (cmd == SIOCGHWTSTAMP)
1009ca0c88c2SBen Hutchings 		return gfar_hwtstamp_get(dev, rq);
1010ec21e2ecSJeff Kirsher 
1011ec21e2ecSJeff Kirsher 	if (!priv->phydev)
1012ec21e2ecSJeff Kirsher 		return -ENODEV;
1013ec21e2ecSJeff Kirsher 
1014ec21e2ecSJeff Kirsher 	return phy_mii_ioctl(priv->phydev, rq, cmd);
1015ec21e2ecSJeff Kirsher }
1016ec21e2ecSJeff Kirsher 
1017ec21e2ecSJeff Kirsher static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
1018ec21e2ecSJeff Kirsher 				   u32 class)
1019ec21e2ecSJeff Kirsher {
1020ec21e2ecSJeff Kirsher 	u32 rqfpr = FPR_FILER_MASK;
1021ec21e2ecSJeff Kirsher 	u32 rqfcr = 0x0;
1022ec21e2ecSJeff Kirsher 
1023ec21e2ecSJeff Kirsher 	rqfar--;
1024ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
1025ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
1026ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
1027ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1028ec21e2ecSJeff Kirsher 
1029ec21e2ecSJeff Kirsher 	rqfar--;
1030ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_NOMATCH;
1031ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
1032ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
1033ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1034ec21e2ecSJeff Kirsher 
1035ec21e2ecSJeff Kirsher 	rqfar--;
1036ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
1037ec21e2ecSJeff Kirsher 	rqfpr = class;
1038ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
1039ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
1040ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1041ec21e2ecSJeff Kirsher 
1042ec21e2ecSJeff Kirsher 	rqfar--;
1043ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
1044ec21e2ecSJeff Kirsher 	rqfpr = class;
1045ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
1046ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
1047ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1048ec21e2ecSJeff Kirsher 
1049ec21e2ecSJeff Kirsher 	return rqfar;
1050ec21e2ecSJeff Kirsher }
1051ec21e2ecSJeff Kirsher 
1052ec21e2ecSJeff Kirsher static void gfar_init_filer_table(struct gfar_private *priv)
1053ec21e2ecSJeff Kirsher {
1054ec21e2ecSJeff Kirsher 	int i = 0x0;
1055ec21e2ecSJeff Kirsher 	u32 rqfar = MAX_FILER_IDX;
1056ec21e2ecSJeff Kirsher 	u32 rqfcr = 0x0;
1057ec21e2ecSJeff Kirsher 	u32 rqfpr = FPR_FILER_MASK;
1058ec21e2ecSJeff Kirsher 
1059ec21e2ecSJeff Kirsher 	/* Default rule */
1060ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_MATCH;
1061ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
1062ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
1063ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1064ec21e2ecSJeff Kirsher 
1065ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
1066ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
1067ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
1068ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
1069ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
1070ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
1071ec21e2ecSJeff Kirsher 
1072ec21e2ecSJeff Kirsher 	/* cur_filer_idx indicated the first non-masked rule */
1073ec21e2ecSJeff Kirsher 	priv->cur_filer_idx = rqfar;
1074ec21e2ecSJeff Kirsher 
1075ec21e2ecSJeff Kirsher 	/* Rest are masked rules */
1076ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_NOMATCH;
1077ec21e2ecSJeff Kirsher 	for (i = 0; i < rqfar; i++) {
1078ec21e2ecSJeff Kirsher 		priv->ftp_rqfcr[i] = rqfcr;
1079ec21e2ecSJeff Kirsher 		priv->ftp_rqfpr[i] = rqfpr;
1080ec21e2ecSJeff Kirsher 		gfar_write_filer(priv, i, rqfcr, rqfpr);
1081ec21e2ecSJeff Kirsher 	}
1082ec21e2ecSJeff Kirsher }
1083ec21e2ecSJeff Kirsher 
1084d6ef0bccSClaudiu Manoil #ifdef CONFIG_PPC
10852969b1f7SClaudiu Manoil static void __gfar_detect_errata_83xx(struct gfar_private *priv)
1086ec21e2ecSJeff Kirsher {
1087ec21e2ecSJeff Kirsher 	unsigned int pvr = mfspr(SPRN_PVR);
1088ec21e2ecSJeff Kirsher 	unsigned int svr = mfspr(SPRN_SVR);
1089ec21e2ecSJeff Kirsher 	unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
1090ec21e2ecSJeff Kirsher 	unsigned int rev = svr & 0xffff;
1091ec21e2ecSJeff Kirsher 
1092ec21e2ecSJeff Kirsher 	/* MPC8313 Rev 2.0 and higher; All MPC837x */
1093ec21e2ecSJeff Kirsher 	if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
1094ec21e2ecSJeff Kirsher 	    (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
1095ec21e2ecSJeff Kirsher 		priv->errata |= GFAR_ERRATA_74;
1096ec21e2ecSJeff Kirsher 
1097ec21e2ecSJeff Kirsher 	/* MPC8313 and MPC837x all rev */
1098ec21e2ecSJeff Kirsher 	if ((pvr == 0x80850010 && mod == 0x80b0) ||
1099ec21e2ecSJeff Kirsher 	    (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
1100ec21e2ecSJeff Kirsher 		priv->errata |= GFAR_ERRATA_76;
1101ec21e2ecSJeff Kirsher 
11022969b1f7SClaudiu Manoil 	/* MPC8313 Rev < 2.0 */
11032969b1f7SClaudiu Manoil 	if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
1104ec21e2ecSJeff Kirsher 		priv->errata |= GFAR_ERRATA_12;
11052969b1f7SClaudiu Manoil }
11062969b1f7SClaudiu Manoil 
11072969b1f7SClaudiu Manoil static void __gfar_detect_errata_85xx(struct gfar_private *priv)
11082969b1f7SClaudiu Manoil {
11092969b1f7SClaudiu Manoil 	unsigned int svr = mfspr(SPRN_SVR);
11102969b1f7SClaudiu Manoil 
11112969b1f7SClaudiu Manoil 	if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
11122969b1f7SClaudiu Manoil 		priv->errata |= GFAR_ERRATA_12;
111353fad773SClaudiu Manoil 	if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
111453fad773SClaudiu Manoil 	    ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
111553fad773SClaudiu Manoil 		priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
11162969b1f7SClaudiu Manoil }
1117d6ef0bccSClaudiu Manoil #endif
11182969b1f7SClaudiu Manoil 
11192969b1f7SClaudiu Manoil static void gfar_detect_errata(struct gfar_private *priv)
11202969b1f7SClaudiu Manoil {
11212969b1f7SClaudiu Manoil 	struct device *dev = &priv->ofdev->dev;
11222969b1f7SClaudiu Manoil 
11232969b1f7SClaudiu Manoil 	/* no plans to fix */
11242969b1f7SClaudiu Manoil 	priv->errata |= GFAR_ERRATA_A002;
11252969b1f7SClaudiu Manoil 
1126d6ef0bccSClaudiu Manoil #ifdef CONFIG_PPC
11272969b1f7SClaudiu Manoil 	if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
11282969b1f7SClaudiu Manoil 		__gfar_detect_errata_85xx(priv);
11292969b1f7SClaudiu Manoil 	else /* non-mpc85xx parts, i.e. e300 core based */
11302969b1f7SClaudiu Manoil 		__gfar_detect_errata_83xx(priv);
1131d6ef0bccSClaudiu Manoil #endif
1132ec21e2ecSJeff Kirsher 
1133ec21e2ecSJeff Kirsher 	if (priv->errata)
1134ec21e2ecSJeff Kirsher 		dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1135ec21e2ecSJeff Kirsher 			 priv->errata);
1136ec21e2ecSJeff Kirsher }
1137ec21e2ecSJeff Kirsher 
11380851133bSClaudiu Manoil void gfar_mac_reset(struct gfar_private *priv)
1139ec21e2ecSJeff Kirsher {
114020862788SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1141a328ac92SClaudiu Manoil 	u32 tempval;
1142ec21e2ecSJeff Kirsher 
1143ec21e2ecSJeff Kirsher 	/* Reset MAC layer */
1144ec21e2ecSJeff Kirsher 	gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
1145ec21e2ecSJeff Kirsher 
1146ec21e2ecSJeff Kirsher 	/* We need to delay at least 3 TX clocks */
1147a328ac92SClaudiu Manoil 	udelay(3);
1148ec21e2ecSJeff Kirsher 
114923402bddSClaudiu Manoil 	/* the soft reset bit is not self-resetting, so we need to
115023402bddSClaudiu Manoil 	 * clear it before resuming normal operation
115123402bddSClaudiu Manoil 	 */
115220862788SClaudiu Manoil 	gfar_write(&regs->maccfg1, 0);
1153ec21e2ecSJeff Kirsher 
1154a328ac92SClaudiu Manoil 	udelay(3);
1155a328ac92SClaudiu Manoil 
115675354148SClaudiu Manoil 	gfar_rx_offload_en(priv);
115788302648SClaudiu Manoil 
115888302648SClaudiu Manoil 	/* Initialize the max receive frame/buffer lengths */
115975354148SClaudiu Manoil 	gfar_write(&regs->maxfrm, GFAR_JUMBO_FRAME_SIZE);
116075354148SClaudiu Manoil 	gfar_write(&regs->mrblr, GFAR_RXB_SIZE);
1161a328ac92SClaudiu Manoil 
1162a328ac92SClaudiu Manoil 	/* Initialize the Minimum Frame Length Register */
1163a328ac92SClaudiu Manoil 	gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1164a328ac92SClaudiu Manoil 
1165ec21e2ecSJeff Kirsher 	/* Initialize MACCFG2. */
1166ec21e2ecSJeff Kirsher 	tempval = MACCFG2_INIT_SETTINGS;
116788302648SClaudiu Manoil 
116875354148SClaudiu Manoil 	/* eTSEC74 erratum: Rx frames of length MAXFRM or MAXFRM-1
116975354148SClaudiu Manoil 	 * are marked as truncated.  Avoid this by MACCFG2[Huge Frame]=1,
117075354148SClaudiu Manoil 	 * and by checking RxBD[LG] and discarding larger than MAXFRM.
117188302648SClaudiu Manoil 	 */
117275354148SClaudiu Manoil 	if (gfar_has_errata(priv, GFAR_ERRATA_74))
1173ec21e2ecSJeff Kirsher 		tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
117488302648SClaudiu Manoil 
1175ec21e2ecSJeff Kirsher 	gfar_write(&regs->maccfg2, tempval);
1176ec21e2ecSJeff Kirsher 
1177a328ac92SClaudiu Manoil 	/* Clear mac addr hash registers */
1178a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr0, 0);
1179a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr1, 0);
1180a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr2, 0);
1181a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr3, 0);
1182a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr4, 0);
1183a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr5, 0);
1184a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr6, 0);
1185a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr7, 0);
1186a328ac92SClaudiu Manoil 
1187a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr0, 0);
1188a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr1, 0);
1189a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr2, 0);
1190a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr3, 0);
1191a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr4, 0);
1192a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr5, 0);
1193a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr6, 0);
1194a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr7, 0);
1195a328ac92SClaudiu Manoil 
1196a328ac92SClaudiu Manoil 	if (priv->extended_hash)
1197a328ac92SClaudiu Manoil 		gfar_clear_exact_match(priv->ndev);
1198a328ac92SClaudiu Manoil 
1199a328ac92SClaudiu Manoil 	gfar_mac_rx_config(priv);
1200a328ac92SClaudiu Manoil 
1201a328ac92SClaudiu Manoil 	gfar_mac_tx_config(priv);
1202a328ac92SClaudiu Manoil 
1203a328ac92SClaudiu Manoil 	gfar_set_mac_address(priv->ndev);
1204a328ac92SClaudiu Manoil 
1205a328ac92SClaudiu Manoil 	gfar_set_multi(priv->ndev);
1206a328ac92SClaudiu Manoil 
1207a328ac92SClaudiu Manoil 	/* clear ievent and imask before configuring coalescing */
1208a328ac92SClaudiu Manoil 	gfar_ints_disable(priv);
1209a328ac92SClaudiu Manoil 
1210a328ac92SClaudiu Manoil 	/* Configure the coalescing support */
1211a328ac92SClaudiu Manoil 	gfar_configure_coalescing_all(priv);
1212a328ac92SClaudiu Manoil }
1213a328ac92SClaudiu Manoil 
1214a328ac92SClaudiu Manoil static void gfar_hw_init(struct gfar_private *priv)
1215a328ac92SClaudiu Manoil {
1216a328ac92SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1217a328ac92SClaudiu Manoil 	u32 attrs;
1218a328ac92SClaudiu Manoil 
1219a328ac92SClaudiu Manoil 	/* Stop the DMA engine now, in case it was running before
1220a328ac92SClaudiu Manoil 	 * (The firmware could have used it, and left it running).
1221a328ac92SClaudiu Manoil 	 */
1222a328ac92SClaudiu Manoil 	gfar_halt(priv);
1223a328ac92SClaudiu Manoil 
1224a328ac92SClaudiu Manoil 	gfar_mac_reset(priv);
1225a328ac92SClaudiu Manoil 
1226a328ac92SClaudiu Manoil 	/* Zero out the rmon mib registers if it has them */
1227a328ac92SClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1228a328ac92SClaudiu Manoil 		memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
1229a328ac92SClaudiu Manoil 
1230a328ac92SClaudiu Manoil 		/* Mask off the CAM interrupts */
1231a328ac92SClaudiu Manoil 		gfar_write(&regs->rmon.cam1, 0xffffffff);
1232a328ac92SClaudiu Manoil 		gfar_write(&regs->rmon.cam2, 0xffffffff);
1233a328ac92SClaudiu Manoil 	}
1234a328ac92SClaudiu Manoil 
1235ec21e2ecSJeff Kirsher 	/* Initialize ECNTRL */
1236ec21e2ecSJeff Kirsher 	gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
1237ec21e2ecSJeff Kirsher 
123834018fd4SClaudiu Manoil 	/* Set the extraction length and index */
123934018fd4SClaudiu Manoil 	attrs = ATTRELI_EL(priv->rx_stash_size) |
124034018fd4SClaudiu Manoil 		ATTRELI_EI(priv->rx_stash_index);
124134018fd4SClaudiu Manoil 
124234018fd4SClaudiu Manoil 	gfar_write(&regs->attreli, attrs);
124334018fd4SClaudiu Manoil 
124434018fd4SClaudiu Manoil 	/* Start with defaults, and add stashing
124534018fd4SClaudiu Manoil 	 * depending on driver parameters
124634018fd4SClaudiu Manoil 	 */
124734018fd4SClaudiu Manoil 	attrs = ATTR_INIT_SETTINGS;
124834018fd4SClaudiu Manoil 
124934018fd4SClaudiu Manoil 	if (priv->bd_stash_en)
125034018fd4SClaudiu Manoil 		attrs |= ATTR_BDSTASH;
125134018fd4SClaudiu Manoil 
125234018fd4SClaudiu Manoil 	if (priv->rx_stash_size != 0)
125334018fd4SClaudiu Manoil 		attrs |= ATTR_BUFSTASH;
125434018fd4SClaudiu Manoil 
125534018fd4SClaudiu Manoil 	gfar_write(&regs->attr, attrs);
125634018fd4SClaudiu Manoil 
125734018fd4SClaudiu Manoil 	/* FIFO configs */
125834018fd4SClaudiu Manoil 	gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
125934018fd4SClaudiu Manoil 	gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
126034018fd4SClaudiu Manoil 	gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
126134018fd4SClaudiu Manoil 
126220862788SClaudiu Manoil 	/* Program the interrupt steering regs, only for MG devices */
126320862788SClaudiu Manoil 	if (priv->num_grps > 1)
126420862788SClaudiu Manoil 		gfar_write_isrg(priv);
1265ec21e2ecSJeff Kirsher }
1266ec21e2ecSJeff Kirsher 
1267898157edSXiubo Li static void gfar_init_addr_hash_table(struct gfar_private *priv)
126820862788SClaudiu Manoil {
126920862788SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1270ec21e2ecSJeff Kirsher 
1271ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
1272ec21e2ecSJeff Kirsher 		priv->extended_hash = 1;
1273ec21e2ecSJeff Kirsher 		priv->hash_width = 9;
1274ec21e2ecSJeff Kirsher 
1275ec21e2ecSJeff Kirsher 		priv->hash_regs[0] = &regs->igaddr0;
1276ec21e2ecSJeff Kirsher 		priv->hash_regs[1] = &regs->igaddr1;
1277ec21e2ecSJeff Kirsher 		priv->hash_regs[2] = &regs->igaddr2;
1278ec21e2ecSJeff Kirsher 		priv->hash_regs[3] = &regs->igaddr3;
1279ec21e2ecSJeff Kirsher 		priv->hash_regs[4] = &regs->igaddr4;
1280ec21e2ecSJeff Kirsher 		priv->hash_regs[5] = &regs->igaddr5;
1281ec21e2ecSJeff Kirsher 		priv->hash_regs[6] = &regs->igaddr6;
1282ec21e2ecSJeff Kirsher 		priv->hash_regs[7] = &regs->igaddr7;
1283ec21e2ecSJeff Kirsher 		priv->hash_regs[8] = &regs->gaddr0;
1284ec21e2ecSJeff Kirsher 		priv->hash_regs[9] = &regs->gaddr1;
1285ec21e2ecSJeff Kirsher 		priv->hash_regs[10] = &regs->gaddr2;
1286ec21e2ecSJeff Kirsher 		priv->hash_regs[11] = &regs->gaddr3;
1287ec21e2ecSJeff Kirsher 		priv->hash_regs[12] = &regs->gaddr4;
1288ec21e2ecSJeff Kirsher 		priv->hash_regs[13] = &regs->gaddr5;
1289ec21e2ecSJeff Kirsher 		priv->hash_regs[14] = &regs->gaddr6;
1290ec21e2ecSJeff Kirsher 		priv->hash_regs[15] = &regs->gaddr7;
1291ec21e2ecSJeff Kirsher 
1292ec21e2ecSJeff Kirsher 	} else {
1293ec21e2ecSJeff Kirsher 		priv->extended_hash = 0;
1294ec21e2ecSJeff Kirsher 		priv->hash_width = 8;
1295ec21e2ecSJeff Kirsher 
1296ec21e2ecSJeff Kirsher 		priv->hash_regs[0] = &regs->gaddr0;
1297ec21e2ecSJeff Kirsher 		priv->hash_regs[1] = &regs->gaddr1;
1298ec21e2ecSJeff Kirsher 		priv->hash_regs[2] = &regs->gaddr2;
1299ec21e2ecSJeff Kirsher 		priv->hash_regs[3] = &regs->gaddr3;
1300ec21e2ecSJeff Kirsher 		priv->hash_regs[4] = &regs->gaddr4;
1301ec21e2ecSJeff Kirsher 		priv->hash_regs[5] = &regs->gaddr5;
1302ec21e2ecSJeff Kirsher 		priv->hash_regs[6] = &regs->gaddr6;
1303ec21e2ecSJeff Kirsher 		priv->hash_regs[7] = &regs->gaddr7;
1304ec21e2ecSJeff Kirsher 	}
130520862788SClaudiu Manoil }
130620862788SClaudiu Manoil 
130720862788SClaudiu Manoil /* Set up the ethernet device structure, private data,
130820862788SClaudiu Manoil  * and anything else we need before we start
130920862788SClaudiu Manoil  */
131020862788SClaudiu Manoil static int gfar_probe(struct platform_device *ofdev)
131120862788SClaudiu Manoil {
131220862788SClaudiu Manoil 	struct net_device *dev = NULL;
131320862788SClaudiu Manoil 	struct gfar_private *priv = NULL;
131420862788SClaudiu Manoil 	int err = 0, i;
131520862788SClaudiu Manoil 
131620862788SClaudiu Manoil 	err = gfar_of_init(ofdev, &dev);
131720862788SClaudiu Manoil 
131820862788SClaudiu Manoil 	if (err)
131920862788SClaudiu Manoil 		return err;
132020862788SClaudiu Manoil 
132120862788SClaudiu Manoil 	priv = netdev_priv(dev);
132220862788SClaudiu Manoil 	priv->ndev = dev;
132320862788SClaudiu Manoil 	priv->ofdev = ofdev;
132420862788SClaudiu Manoil 	priv->dev = &ofdev->dev;
132520862788SClaudiu Manoil 	SET_NETDEV_DEV(dev, &ofdev->dev);
132620862788SClaudiu Manoil 
132720862788SClaudiu Manoil 	INIT_WORK(&priv->reset_task, gfar_reset_task);
132820862788SClaudiu Manoil 
132920862788SClaudiu Manoil 	platform_set_drvdata(ofdev, priv);
133020862788SClaudiu Manoil 
133120862788SClaudiu Manoil 	gfar_detect_errata(priv);
133220862788SClaudiu Manoil 
133320862788SClaudiu Manoil 	/* Set the dev->base_addr to the gfar reg region */
133420862788SClaudiu Manoil 	dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
133520862788SClaudiu Manoil 
133620862788SClaudiu Manoil 	/* Fill in the dev structure */
133720862788SClaudiu Manoil 	dev->watchdog_timeo = TX_TIMEOUT;
133820862788SClaudiu Manoil 	dev->mtu = 1500;
133920862788SClaudiu Manoil 	dev->netdev_ops = &gfar_netdev_ops;
134020862788SClaudiu Manoil 	dev->ethtool_ops = &gfar_ethtool_ops;
134120862788SClaudiu Manoil 
134220862788SClaudiu Manoil 	/* Register for napi ...We are registering NAPI for each grp */
1343aeb12c5eSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
134471ff9e3dSClaudiu Manoil 		if (priv->poll_mode == GFAR_SQ_POLLING) {
134571ff9e3dSClaudiu Manoil 			netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
134671ff9e3dSClaudiu Manoil 				       gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
134771ff9e3dSClaudiu Manoil 			netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
134871ff9e3dSClaudiu Manoil 				       gfar_poll_tx_sq, 2);
134971ff9e3dSClaudiu Manoil 		} else {
1350aeb12c5eSClaudiu Manoil 			netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1351aeb12c5eSClaudiu Manoil 				       gfar_poll_rx, GFAR_DEV_WEIGHT);
1352aeb12c5eSClaudiu Manoil 			netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1353aeb12c5eSClaudiu Manoil 				       gfar_poll_tx, 2);
1354aeb12c5eSClaudiu Manoil 		}
1355aeb12c5eSClaudiu Manoil 	}
135620862788SClaudiu Manoil 
135720862788SClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
135820862788SClaudiu Manoil 		dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
135920862788SClaudiu Manoil 				   NETIF_F_RXCSUM;
136020862788SClaudiu Manoil 		dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
136120862788SClaudiu Manoil 				 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
136220862788SClaudiu Manoil 	}
136320862788SClaudiu Manoil 
136420862788SClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
136520862788SClaudiu Manoil 		dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
136620862788SClaudiu Manoil 				    NETIF_F_HW_VLAN_CTAG_RX;
136720862788SClaudiu Manoil 		dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
136820862788SClaudiu Manoil 	}
136920862788SClaudiu Manoil 
13703d23a05cSClaudiu Manoil 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
13713d23a05cSClaudiu Manoil 
137220862788SClaudiu Manoil 	gfar_init_addr_hash_table(priv);
1373ec21e2ecSJeff Kirsher 
1374532c37bcSClaudiu Manoil 	/* Insert receive time stamps into padding alignment bytes */
1375532c37bcSClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1376532c37bcSClaudiu Manoil 		priv->padding = 8;
1377ec21e2ecSJeff Kirsher 
1378ec21e2ecSJeff Kirsher 	if (dev->features & NETIF_F_IP_CSUM ||
1379ec21e2ecSJeff Kirsher 	    priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1380bee9e58cSWu Jiajun-B06378 		dev->needed_headroom = GMAC_FCB_LEN;
1381ec21e2ecSJeff Kirsher 
1382ec21e2ecSJeff Kirsher 	/* Initializing some of the rx/tx queue level parameters */
1383ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
1384ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1385ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1386ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1387ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->txic = DEFAULT_TXIC;
1388ec21e2ecSJeff Kirsher 	}
1389ec21e2ecSJeff Kirsher 
1390ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
1391ec21e2ecSJeff Kirsher 		priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1392ec21e2ecSJeff Kirsher 		priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1393ec21e2ecSJeff Kirsher 		priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1394ec21e2ecSJeff Kirsher 	}
1395ec21e2ecSJeff Kirsher 
1396ec21e2ecSJeff Kirsher 	/* always enable rx filer */
1397ec21e2ecSJeff Kirsher 	priv->rx_filer_enable = 1;
1398ec21e2ecSJeff Kirsher 	/* Enable most messages by default */
1399ec21e2ecSJeff Kirsher 	priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1400b98b8babSClaudiu Manoil 	/* use pritority h/w tx queue scheduling for single queue devices */
1401b98b8babSClaudiu Manoil 	if (priv->num_tx_queues == 1)
1402b98b8babSClaudiu Manoil 		priv->prio_sched_en = 1;
1403ec21e2ecSJeff Kirsher 
14040851133bSClaudiu Manoil 	set_bit(GFAR_DOWN, &priv->state);
14050851133bSClaudiu Manoil 
1406a328ac92SClaudiu Manoil 	gfar_hw_init(priv);
1407ec21e2ecSJeff Kirsher 
1408d4c642eaSFabio Estevam 	/* Carrier starts down, phylib will bring it up */
1409d4c642eaSFabio Estevam 	netif_carrier_off(dev);
1410d4c642eaSFabio Estevam 
1411ec21e2ecSJeff Kirsher 	err = register_netdev(dev);
1412ec21e2ecSJeff Kirsher 
1413ec21e2ecSJeff Kirsher 	if (err) {
1414ec21e2ecSJeff Kirsher 		pr_err("%s: Cannot register net device, aborting\n", dev->name);
1415ec21e2ecSJeff Kirsher 		goto register_fail;
1416ec21e2ecSJeff Kirsher 	}
1417ec21e2ecSJeff Kirsher 
1418b0734b6dSClaudiu Manoil 	device_set_wakeup_capable(&dev->dev, priv->device_flags &
1419bc4598bcSJan Ceuleers 				  FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1420ec21e2ecSJeff Kirsher 
1421ec21e2ecSJeff Kirsher 	/* fill out IRQ number and name fields */
1422ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_grps; i++) {
1423ee873fdaSClaudiu Manoil 		struct gfar_priv_grp *grp = &priv->gfargrp[i];
1424ec21e2ecSJeff Kirsher 		if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1425ee873fdaSClaudiu Manoil 			sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
14260015e551SJoe Perches 				dev->name, "_g", '0' + i, "_tx");
1427ee873fdaSClaudiu Manoil 			sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
14280015e551SJoe Perches 				dev->name, "_g", '0' + i, "_rx");
1429ee873fdaSClaudiu Manoil 			sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
14300015e551SJoe Perches 				dev->name, "_g", '0' + i, "_er");
1431ec21e2ecSJeff Kirsher 		} else
1432ee873fdaSClaudiu Manoil 			strcpy(gfar_irq(grp, TX)->name, dev->name);
1433ec21e2ecSJeff Kirsher 	}
1434ec21e2ecSJeff Kirsher 
1435ec21e2ecSJeff Kirsher 	/* Initialize the filer table */
1436ec21e2ecSJeff Kirsher 	gfar_init_filer_table(priv);
1437ec21e2ecSJeff Kirsher 
1438ec21e2ecSJeff Kirsher 	/* Print out the device info */
1439ec21e2ecSJeff Kirsher 	netdev_info(dev, "mac: %pM\n", dev->dev_addr);
1440ec21e2ecSJeff Kirsher 
14410977f817SJan Ceuleers 	/* Even more device info helps when determining which kernel
14420977f817SJan Ceuleers 	 * provided which set of benchmarks.
14430977f817SJan Ceuleers 	 */
1444ec21e2ecSJeff Kirsher 	netdev_info(dev, "Running with NAPI enabled\n");
1445ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
1446ec21e2ecSJeff Kirsher 		netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1447ec21e2ecSJeff Kirsher 			    i, priv->rx_queue[i]->rx_ring_size);
1448ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
1449ec21e2ecSJeff Kirsher 		netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1450ec21e2ecSJeff Kirsher 			    i, priv->tx_queue[i]->tx_ring_size);
1451ec21e2ecSJeff Kirsher 
1452ec21e2ecSJeff Kirsher 	return 0;
1453ec21e2ecSJeff Kirsher 
1454ec21e2ecSJeff Kirsher register_fail:
1455ec21e2ecSJeff Kirsher 	unmap_group_regs(priv);
145620862788SClaudiu Manoil 	gfar_free_rx_queues(priv);
145720862788SClaudiu Manoil 	gfar_free_tx_queues(priv);
1458ec21e2ecSJeff Kirsher 	of_node_put(priv->phy_node);
1459ec21e2ecSJeff Kirsher 	of_node_put(priv->tbi_node);
1460ee873fdaSClaudiu Manoil 	free_gfar_dev(priv);
1461ec21e2ecSJeff Kirsher 	return err;
1462ec21e2ecSJeff Kirsher }
1463ec21e2ecSJeff Kirsher 
1464ec21e2ecSJeff Kirsher static int gfar_remove(struct platform_device *ofdev)
1465ec21e2ecSJeff Kirsher {
14668513fbd8SJingoo Han 	struct gfar_private *priv = platform_get_drvdata(ofdev);
1467ec21e2ecSJeff Kirsher 
1468ec21e2ecSJeff Kirsher 	of_node_put(priv->phy_node);
1469ec21e2ecSJeff Kirsher 	of_node_put(priv->tbi_node);
1470ec21e2ecSJeff Kirsher 
1471ec21e2ecSJeff Kirsher 	unregister_netdev(priv->ndev);
1472ec21e2ecSJeff Kirsher 	unmap_group_regs(priv);
147320862788SClaudiu Manoil 	gfar_free_rx_queues(priv);
147420862788SClaudiu Manoil 	gfar_free_tx_queues(priv);
1475ee873fdaSClaudiu Manoil 	free_gfar_dev(priv);
1476ec21e2ecSJeff Kirsher 
1477ec21e2ecSJeff Kirsher 	return 0;
1478ec21e2ecSJeff Kirsher }
1479ec21e2ecSJeff Kirsher 
1480ec21e2ecSJeff Kirsher #ifdef CONFIG_PM
1481ec21e2ecSJeff Kirsher 
1482ec21e2ecSJeff Kirsher static int gfar_suspend(struct device *dev)
1483ec21e2ecSJeff Kirsher {
1484ec21e2ecSJeff Kirsher 	struct gfar_private *priv = dev_get_drvdata(dev);
1485ec21e2ecSJeff Kirsher 	struct net_device *ndev = priv->ndev;
1486ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1487ec21e2ecSJeff Kirsher 	u32 tempval;
1488ec21e2ecSJeff Kirsher 	int magic_packet = priv->wol_en &&
1489bc4598bcSJan Ceuleers 			   (priv->device_flags &
1490bc4598bcSJan Ceuleers 			    FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1491ec21e2ecSJeff Kirsher 
1492614b4242SClaudiu Manoil 	if (!netif_running(ndev))
1493614b4242SClaudiu Manoil 		return 0;
1494ec21e2ecSJeff Kirsher 
1495ec21e2ecSJeff Kirsher 	disable_napi(priv);
1496614b4242SClaudiu Manoil 	netif_tx_lock(ndev);
1497614b4242SClaudiu Manoil 	netif_device_detach(ndev);
1498614b4242SClaudiu Manoil 	netif_tx_unlock(ndev);
1499614b4242SClaudiu Manoil 
1500614b4242SClaudiu Manoil 	gfar_halt(priv);
1501ec21e2ecSJeff Kirsher 
1502ec21e2ecSJeff Kirsher 	if (magic_packet) {
1503ec21e2ecSJeff Kirsher 		/* Enable interrupt on Magic Packet */
1504ec21e2ecSJeff Kirsher 		gfar_write(&regs->imask, IMASK_MAG);
1505ec21e2ecSJeff Kirsher 
1506ec21e2ecSJeff Kirsher 		/* Enable Magic Packet mode */
1507ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->maccfg2);
1508ec21e2ecSJeff Kirsher 		tempval |= MACCFG2_MPEN;
1509ec21e2ecSJeff Kirsher 		gfar_write(&regs->maccfg2, tempval);
1510614b4242SClaudiu Manoil 
1511614b4242SClaudiu Manoil 		/* re-enable the Rx block */
1512614b4242SClaudiu Manoil 		tempval = gfar_read(&regs->maccfg1);
1513614b4242SClaudiu Manoil 		tempval |= MACCFG1_RX_EN;
1514614b4242SClaudiu Manoil 		gfar_write(&regs->maccfg1, tempval);
1515614b4242SClaudiu Manoil 
1516ec21e2ecSJeff Kirsher 	} else {
1517ec21e2ecSJeff Kirsher 		phy_stop(priv->phydev);
1518ec21e2ecSJeff Kirsher 	}
1519ec21e2ecSJeff Kirsher 
1520ec21e2ecSJeff Kirsher 	return 0;
1521ec21e2ecSJeff Kirsher }
1522ec21e2ecSJeff Kirsher 
1523ec21e2ecSJeff Kirsher static int gfar_resume(struct device *dev)
1524ec21e2ecSJeff Kirsher {
1525ec21e2ecSJeff Kirsher 	struct gfar_private *priv = dev_get_drvdata(dev);
1526ec21e2ecSJeff Kirsher 	struct net_device *ndev = priv->ndev;
1527ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1528ec21e2ecSJeff Kirsher 	u32 tempval;
1529ec21e2ecSJeff Kirsher 	int magic_packet = priv->wol_en &&
1530bc4598bcSJan Ceuleers 			   (priv->device_flags &
1531bc4598bcSJan Ceuleers 			    FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1532ec21e2ecSJeff Kirsher 
1533614b4242SClaudiu Manoil 	if (!netif_running(ndev))
1534ec21e2ecSJeff Kirsher 		return 0;
1535ec21e2ecSJeff Kirsher 
1536614b4242SClaudiu Manoil 	if (magic_packet) {
1537614b4242SClaudiu Manoil 		/* Disable Magic Packet mode */
1538ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->maccfg2);
1539ec21e2ecSJeff Kirsher 		tempval &= ~MACCFG2_MPEN;
1540ec21e2ecSJeff Kirsher 		gfar_write(&regs->maccfg2, tempval);
1541614b4242SClaudiu Manoil 	} else {
1542614b4242SClaudiu Manoil 		phy_start(priv->phydev);
1543614b4242SClaudiu Manoil 	}
1544ec21e2ecSJeff Kirsher 
1545c10650b6SClaudiu Manoil 	gfar_start(priv);
1546ec21e2ecSJeff Kirsher 
1547ec21e2ecSJeff Kirsher 	netif_device_attach(ndev);
1548ec21e2ecSJeff Kirsher 	enable_napi(priv);
1549ec21e2ecSJeff Kirsher 
1550ec21e2ecSJeff Kirsher 	return 0;
1551ec21e2ecSJeff Kirsher }
1552ec21e2ecSJeff Kirsher 
1553ec21e2ecSJeff Kirsher static int gfar_restore(struct device *dev)
1554ec21e2ecSJeff Kirsher {
1555ec21e2ecSJeff Kirsher 	struct gfar_private *priv = dev_get_drvdata(dev);
1556ec21e2ecSJeff Kirsher 	struct net_device *ndev = priv->ndev;
1557ec21e2ecSJeff Kirsher 
1558103cdd1dSWang Dongsheng 	if (!netif_running(ndev)) {
1559103cdd1dSWang Dongsheng 		netif_device_attach(ndev);
1560103cdd1dSWang Dongsheng 
1561ec21e2ecSJeff Kirsher 		return 0;
1562103cdd1dSWang Dongsheng 	}
1563ec21e2ecSJeff Kirsher 
156476f31e8bSClaudiu Manoil 	gfar_init_bds(ndev);
15651eb8f7a7SClaudiu Manoil 
1566a328ac92SClaudiu Manoil 	gfar_mac_reset(priv);
1567a328ac92SClaudiu Manoil 
1568a328ac92SClaudiu Manoil 	gfar_init_tx_rx_base(priv);
1569a328ac92SClaudiu Manoil 
1570c10650b6SClaudiu Manoil 	gfar_start(priv);
1571ec21e2ecSJeff Kirsher 
1572ec21e2ecSJeff Kirsher 	priv->oldlink = 0;
1573ec21e2ecSJeff Kirsher 	priv->oldspeed = 0;
1574ec21e2ecSJeff Kirsher 	priv->oldduplex = -1;
1575ec21e2ecSJeff Kirsher 
1576ec21e2ecSJeff Kirsher 	if (priv->phydev)
1577ec21e2ecSJeff Kirsher 		phy_start(priv->phydev);
1578ec21e2ecSJeff Kirsher 
1579ec21e2ecSJeff Kirsher 	netif_device_attach(ndev);
1580ec21e2ecSJeff Kirsher 	enable_napi(priv);
1581ec21e2ecSJeff Kirsher 
1582ec21e2ecSJeff Kirsher 	return 0;
1583ec21e2ecSJeff Kirsher }
1584ec21e2ecSJeff Kirsher 
1585ec21e2ecSJeff Kirsher static struct dev_pm_ops gfar_pm_ops = {
1586ec21e2ecSJeff Kirsher 	.suspend = gfar_suspend,
1587ec21e2ecSJeff Kirsher 	.resume = gfar_resume,
1588ec21e2ecSJeff Kirsher 	.freeze = gfar_suspend,
1589ec21e2ecSJeff Kirsher 	.thaw = gfar_resume,
1590ec21e2ecSJeff Kirsher 	.restore = gfar_restore,
1591ec21e2ecSJeff Kirsher };
1592ec21e2ecSJeff Kirsher 
1593ec21e2ecSJeff Kirsher #define GFAR_PM_OPS (&gfar_pm_ops)
1594ec21e2ecSJeff Kirsher 
1595ec21e2ecSJeff Kirsher #else
1596ec21e2ecSJeff Kirsher 
1597ec21e2ecSJeff Kirsher #define GFAR_PM_OPS NULL
1598ec21e2ecSJeff Kirsher 
1599ec21e2ecSJeff Kirsher #endif
1600ec21e2ecSJeff Kirsher 
1601ec21e2ecSJeff Kirsher /* Reads the controller's registers to determine what interface
1602ec21e2ecSJeff Kirsher  * connects it to the PHY.
1603ec21e2ecSJeff Kirsher  */
1604ec21e2ecSJeff Kirsher static phy_interface_t gfar_get_interface(struct net_device *dev)
1605ec21e2ecSJeff Kirsher {
1606ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1607ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1608ec21e2ecSJeff Kirsher 	u32 ecntrl;
1609ec21e2ecSJeff Kirsher 
1610ec21e2ecSJeff Kirsher 	ecntrl = gfar_read(&regs->ecntrl);
1611ec21e2ecSJeff Kirsher 
1612ec21e2ecSJeff Kirsher 	if (ecntrl & ECNTRL_SGMII_MODE)
1613ec21e2ecSJeff Kirsher 		return PHY_INTERFACE_MODE_SGMII;
1614ec21e2ecSJeff Kirsher 
1615ec21e2ecSJeff Kirsher 	if (ecntrl & ECNTRL_TBI_MODE) {
1616ec21e2ecSJeff Kirsher 		if (ecntrl & ECNTRL_REDUCED_MODE)
1617ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_RTBI;
1618ec21e2ecSJeff Kirsher 		else
1619ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_TBI;
1620ec21e2ecSJeff Kirsher 	}
1621ec21e2ecSJeff Kirsher 
1622ec21e2ecSJeff Kirsher 	if (ecntrl & ECNTRL_REDUCED_MODE) {
1623bc4598bcSJan Ceuleers 		if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
1624ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_RMII;
1625bc4598bcSJan Ceuleers 		}
1626ec21e2ecSJeff Kirsher 		else {
1627ec21e2ecSJeff Kirsher 			phy_interface_t interface = priv->interface;
1628ec21e2ecSJeff Kirsher 
16290977f817SJan Ceuleers 			/* This isn't autodetected right now, so it must
1630ec21e2ecSJeff Kirsher 			 * be set by the device tree or platform code.
1631ec21e2ecSJeff Kirsher 			 */
1632ec21e2ecSJeff Kirsher 			if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1633ec21e2ecSJeff Kirsher 				return PHY_INTERFACE_MODE_RGMII_ID;
1634ec21e2ecSJeff Kirsher 
1635ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_RGMII;
1636ec21e2ecSJeff Kirsher 		}
1637ec21e2ecSJeff Kirsher 	}
1638ec21e2ecSJeff Kirsher 
1639ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
1640ec21e2ecSJeff Kirsher 		return PHY_INTERFACE_MODE_GMII;
1641ec21e2ecSJeff Kirsher 
1642ec21e2ecSJeff Kirsher 	return PHY_INTERFACE_MODE_MII;
1643ec21e2ecSJeff Kirsher }
1644ec21e2ecSJeff Kirsher 
1645ec21e2ecSJeff Kirsher 
1646ec21e2ecSJeff Kirsher /* Initializes driver's PHY state, and attaches to the PHY.
1647ec21e2ecSJeff Kirsher  * Returns 0 on success.
1648ec21e2ecSJeff Kirsher  */
1649ec21e2ecSJeff Kirsher static int init_phy(struct net_device *dev)
1650ec21e2ecSJeff Kirsher {
1651ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1652ec21e2ecSJeff Kirsher 	uint gigabit_support =
1653ec21e2ecSJeff Kirsher 		priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
165423402bddSClaudiu Manoil 		GFAR_SUPPORTED_GBIT : 0;
1655ec21e2ecSJeff Kirsher 	phy_interface_t interface;
1656ec21e2ecSJeff Kirsher 
1657ec21e2ecSJeff Kirsher 	priv->oldlink = 0;
1658ec21e2ecSJeff Kirsher 	priv->oldspeed = 0;
1659ec21e2ecSJeff Kirsher 	priv->oldduplex = -1;
1660ec21e2ecSJeff Kirsher 
1661ec21e2ecSJeff Kirsher 	interface = gfar_get_interface(dev);
1662ec21e2ecSJeff Kirsher 
1663ec21e2ecSJeff Kirsher 	priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1664ec21e2ecSJeff Kirsher 				      interface);
1665ec21e2ecSJeff Kirsher 	if (!priv->phydev) {
1666ec21e2ecSJeff Kirsher 		dev_err(&dev->dev, "could not attach to PHY\n");
1667ec21e2ecSJeff Kirsher 		return -ENODEV;
1668ec21e2ecSJeff Kirsher 	}
1669ec21e2ecSJeff Kirsher 
1670ec21e2ecSJeff Kirsher 	if (interface == PHY_INTERFACE_MODE_SGMII)
1671ec21e2ecSJeff Kirsher 		gfar_configure_serdes(dev);
1672ec21e2ecSJeff Kirsher 
1673ec21e2ecSJeff Kirsher 	/* Remove any features not supported by the controller */
1674ec21e2ecSJeff Kirsher 	priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1675ec21e2ecSJeff Kirsher 	priv->phydev->advertising = priv->phydev->supported;
1676ec21e2ecSJeff Kirsher 
1677cf987afcSPavaluca Matei-B46610 	/* Add support for flow control, but don't advertise it by default */
1678cf987afcSPavaluca Matei-B46610 	priv->phydev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1679cf987afcSPavaluca Matei-B46610 
1680ec21e2ecSJeff Kirsher 	return 0;
1681ec21e2ecSJeff Kirsher }
1682ec21e2ecSJeff Kirsher 
16830977f817SJan Ceuleers /* Initialize TBI PHY interface for communicating with the
1684ec21e2ecSJeff Kirsher  * SERDES lynx PHY on the chip.  We communicate with this PHY
1685ec21e2ecSJeff Kirsher  * through the MDIO bus on each controller, treating it as a
1686ec21e2ecSJeff Kirsher  * "normal" PHY at the address found in the TBIPA register.  We assume
1687ec21e2ecSJeff Kirsher  * that the TBIPA register is valid.  Either the MDIO bus code will set
1688ec21e2ecSJeff Kirsher  * it to a value that doesn't conflict with other PHYs on the bus, or the
1689ec21e2ecSJeff Kirsher  * value doesn't matter, as there are no other PHYs on the bus.
1690ec21e2ecSJeff Kirsher  */
1691ec21e2ecSJeff Kirsher static void gfar_configure_serdes(struct net_device *dev)
1692ec21e2ecSJeff Kirsher {
1693ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1694ec21e2ecSJeff Kirsher 	struct phy_device *tbiphy;
1695ec21e2ecSJeff Kirsher 
1696ec21e2ecSJeff Kirsher 	if (!priv->tbi_node) {
1697ec21e2ecSJeff Kirsher 		dev_warn(&dev->dev, "error: SGMII mode requires that the "
1698ec21e2ecSJeff Kirsher 				    "device tree specify a tbi-handle\n");
1699ec21e2ecSJeff Kirsher 		return;
1700ec21e2ecSJeff Kirsher 	}
1701ec21e2ecSJeff Kirsher 
1702ec21e2ecSJeff Kirsher 	tbiphy = of_phy_find_device(priv->tbi_node);
1703ec21e2ecSJeff Kirsher 	if (!tbiphy) {
1704ec21e2ecSJeff Kirsher 		dev_err(&dev->dev, "error: Could not get TBI device\n");
1705ec21e2ecSJeff Kirsher 		return;
1706ec21e2ecSJeff Kirsher 	}
1707ec21e2ecSJeff Kirsher 
17080977f817SJan Ceuleers 	/* If the link is already up, we must already be ok, and don't need to
1709ec21e2ecSJeff Kirsher 	 * configure and reset the TBI<->SerDes link.  Maybe U-Boot configured
1710ec21e2ecSJeff Kirsher 	 * everything for us?  Resetting it takes the link down and requires
1711ec21e2ecSJeff Kirsher 	 * several seconds for it to come back.
1712ec21e2ecSJeff Kirsher 	 */
171338737e49SRussell King 	if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS) {
171438737e49SRussell King 		put_device(&tbiphy->dev);
1715ec21e2ecSJeff Kirsher 		return;
171638737e49SRussell King 	}
1717ec21e2ecSJeff Kirsher 
1718ec21e2ecSJeff Kirsher 	/* Single clk mode, mii mode off(for serdes communication) */
1719ec21e2ecSJeff Kirsher 	phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
1720ec21e2ecSJeff Kirsher 
1721ec21e2ecSJeff Kirsher 	phy_write(tbiphy, MII_ADVERTISE,
1722ec21e2ecSJeff Kirsher 		  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1723ec21e2ecSJeff Kirsher 		  ADVERTISE_1000XPSE_ASYM);
1724ec21e2ecSJeff Kirsher 
1725bc4598bcSJan Ceuleers 	phy_write(tbiphy, MII_BMCR,
1726bc4598bcSJan Ceuleers 		  BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1727bc4598bcSJan Ceuleers 		  BMCR_SPEED1000);
172804d53b20SRussell King 
172904d53b20SRussell King 	put_device(&tbiphy->dev);
1730ec21e2ecSJeff Kirsher }
1731ec21e2ecSJeff Kirsher 
1732ec21e2ecSJeff Kirsher static int __gfar_is_rx_idle(struct gfar_private *priv)
1733ec21e2ecSJeff Kirsher {
1734ec21e2ecSJeff Kirsher 	u32 res;
1735ec21e2ecSJeff Kirsher 
17360977f817SJan Ceuleers 	/* Normaly TSEC should not hang on GRS commands, so we should
1737ec21e2ecSJeff Kirsher 	 * actually wait for IEVENT_GRSC flag.
1738ec21e2ecSJeff Kirsher 	 */
1739ad3660c2SClaudiu Manoil 	if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
1740ec21e2ecSJeff Kirsher 		return 0;
1741ec21e2ecSJeff Kirsher 
17420977f817SJan Ceuleers 	/* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
1743ec21e2ecSJeff Kirsher 	 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1744ec21e2ecSJeff Kirsher 	 * and the Rx can be safely reset.
1745ec21e2ecSJeff Kirsher 	 */
1746ec21e2ecSJeff Kirsher 	res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1747ec21e2ecSJeff Kirsher 	res &= 0x7f807f80;
1748ec21e2ecSJeff Kirsher 	if ((res & 0xffff) == (res >> 16))
1749ec21e2ecSJeff Kirsher 		return 1;
1750ec21e2ecSJeff Kirsher 
1751ec21e2ecSJeff Kirsher 	return 0;
1752ec21e2ecSJeff Kirsher }
1753ec21e2ecSJeff Kirsher 
1754ec21e2ecSJeff Kirsher /* Halt the receive and transmit queues */
1755c10650b6SClaudiu Manoil static void gfar_halt_nodisable(struct gfar_private *priv)
1756ec21e2ecSJeff Kirsher {
1757efeddce7SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1758ec21e2ecSJeff Kirsher 	u32 tempval;
1759a4feee89SClaudiu Manoil 	unsigned int timeout;
1760a4feee89SClaudiu Manoil 	int stopped;
1761ec21e2ecSJeff Kirsher 
1762efeddce7SClaudiu Manoil 	gfar_ints_disable(priv);
1763ec21e2ecSJeff Kirsher 
1764a4feee89SClaudiu Manoil 	if (gfar_is_dma_stopped(priv))
1765a4feee89SClaudiu Manoil 		return;
1766a4feee89SClaudiu Manoil 
1767ec21e2ecSJeff Kirsher 	/* Stop the DMA, and wait for it to stop */
1768ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->dmactrl);
1769ec21e2ecSJeff Kirsher 	tempval |= (DMACTRL_GRS | DMACTRL_GTS);
1770ec21e2ecSJeff Kirsher 	gfar_write(&regs->dmactrl, tempval);
1771ec21e2ecSJeff Kirsher 
1772a4feee89SClaudiu Manoil retry:
1773a4feee89SClaudiu Manoil 	timeout = 1000;
1774a4feee89SClaudiu Manoil 	while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) {
1775a4feee89SClaudiu Manoil 		cpu_relax();
1776a4feee89SClaudiu Manoil 		timeout--;
1777ec21e2ecSJeff Kirsher 	}
1778a4feee89SClaudiu Manoil 
1779a4feee89SClaudiu Manoil 	if (!timeout)
1780a4feee89SClaudiu Manoil 		stopped = gfar_is_dma_stopped(priv);
1781a4feee89SClaudiu Manoil 
1782a4feee89SClaudiu Manoil 	if (!stopped && !gfar_is_rx_dma_stopped(priv) &&
1783a4feee89SClaudiu Manoil 	    !__gfar_is_rx_idle(priv))
1784a4feee89SClaudiu Manoil 		goto retry;
1785ec21e2ecSJeff Kirsher }
1786ec21e2ecSJeff Kirsher 
1787ec21e2ecSJeff Kirsher /* Halt the receive and transmit queues */
1788c10650b6SClaudiu Manoil void gfar_halt(struct gfar_private *priv)
1789ec21e2ecSJeff Kirsher {
1790ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1791ec21e2ecSJeff Kirsher 	u32 tempval;
1792ec21e2ecSJeff Kirsher 
1793c10650b6SClaudiu Manoil 	/* Dissable the Rx/Tx hw queues */
1794c10650b6SClaudiu Manoil 	gfar_write(&regs->rqueue, 0);
1795c10650b6SClaudiu Manoil 	gfar_write(&regs->tqueue, 0);
1796ec21e2ecSJeff Kirsher 
1797c10650b6SClaudiu Manoil 	mdelay(10);
1798c10650b6SClaudiu Manoil 
1799c10650b6SClaudiu Manoil 	gfar_halt_nodisable(priv);
1800c10650b6SClaudiu Manoil 
1801c10650b6SClaudiu Manoil 	/* Disable Rx/Tx DMA */
1802ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->maccfg1);
1803ec21e2ecSJeff Kirsher 	tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1804ec21e2ecSJeff Kirsher 	gfar_write(&regs->maccfg1, tempval);
1805ec21e2ecSJeff Kirsher }
1806ec21e2ecSJeff Kirsher 
1807ec21e2ecSJeff Kirsher void stop_gfar(struct net_device *dev)
1808ec21e2ecSJeff Kirsher {
1809ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1810ec21e2ecSJeff Kirsher 
18110851133bSClaudiu Manoil 	netif_tx_stop_all_queues(dev);
1812ec21e2ecSJeff Kirsher 
18134e857c58SPeter Zijlstra 	smp_mb__before_atomic();
18140851133bSClaudiu Manoil 	set_bit(GFAR_DOWN, &priv->state);
18154e857c58SPeter Zijlstra 	smp_mb__after_atomic();
1816ec21e2ecSJeff Kirsher 
18170851133bSClaudiu Manoil 	disable_napi(priv);
1818ec21e2ecSJeff Kirsher 
18190851133bSClaudiu Manoil 	/* disable ints and gracefully shut down Rx/Tx DMA */
1820c10650b6SClaudiu Manoil 	gfar_halt(priv);
1821ec21e2ecSJeff Kirsher 
18220851133bSClaudiu Manoil 	phy_stop(priv->phydev);
1823ec21e2ecSJeff Kirsher 
1824ec21e2ecSJeff Kirsher 	free_skb_resources(priv);
1825ec21e2ecSJeff Kirsher }
1826ec21e2ecSJeff Kirsher 
1827ec21e2ecSJeff Kirsher static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
1828ec21e2ecSJeff Kirsher {
1829ec21e2ecSJeff Kirsher 	struct txbd8 *txbdp;
1830ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(tx_queue->dev);
1831ec21e2ecSJeff Kirsher 	int i, j;
1832ec21e2ecSJeff Kirsher 
1833ec21e2ecSJeff Kirsher 	txbdp = tx_queue->tx_bd_base;
1834ec21e2ecSJeff Kirsher 
1835ec21e2ecSJeff Kirsher 	for (i = 0; i < tx_queue->tx_ring_size; i++) {
1836ec21e2ecSJeff Kirsher 		if (!tx_queue->tx_skbuff[i])
1837ec21e2ecSJeff Kirsher 			continue;
1838ec21e2ecSJeff Kirsher 
1839a7312d58SClaudiu Manoil 		dma_unmap_single(priv->dev, be32_to_cpu(txbdp->bufPtr),
1840a7312d58SClaudiu Manoil 				 be16_to_cpu(txbdp->length), DMA_TO_DEVICE);
1841ec21e2ecSJeff Kirsher 		txbdp->lstatus = 0;
1842ec21e2ecSJeff Kirsher 		for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
1843ec21e2ecSJeff Kirsher 		     j++) {
1844ec21e2ecSJeff Kirsher 			txbdp++;
1845a7312d58SClaudiu Manoil 			dma_unmap_page(priv->dev, be32_to_cpu(txbdp->bufPtr),
1846a7312d58SClaudiu Manoil 				       be16_to_cpu(txbdp->length),
1847a7312d58SClaudiu Manoil 				       DMA_TO_DEVICE);
1848ec21e2ecSJeff Kirsher 		}
1849ec21e2ecSJeff Kirsher 		txbdp++;
1850ec21e2ecSJeff Kirsher 		dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1851ec21e2ecSJeff Kirsher 		tx_queue->tx_skbuff[i] = NULL;
1852ec21e2ecSJeff Kirsher 	}
1853ec21e2ecSJeff Kirsher 	kfree(tx_queue->tx_skbuff);
18541eb8f7a7SClaudiu Manoil 	tx_queue->tx_skbuff = NULL;
1855ec21e2ecSJeff Kirsher }
1856ec21e2ecSJeff Kirsher 
1857ec21e2ecSJeff Kirsher static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1858ec21e2ecSJeff Kirsher {
1859ec21e2ecSJeff Kirsher 	int i;
1860ec21e2ecSJeff Kirsher 
186175354148SClaudiu Manoil 	struct rxbd8 *rxbdp = rx_queue->rx_bd_base;
186275354148SClaudiu Manoil 
186375354148SClaudiu Manoil 	if (rx_queue->skb)
186475354148SClaudiu Manoil 		dev_kfree_skb(rx_queue->skb);
1865ec21e2ecSJeff Kirsher 
1866ec21e2ecSJeff Kirsher 	for (i = 0; i < rx_queue->rx_ring_size; i++) {
186775354148SClaudiu Manoil 		struct	gfar_rx_buff *rxb = &rx_queue->rx_buff[i];
186875354148SClaudiu Manoil 
1869ec21e2ecSJeff Kirsher 		rxbdp->lstatus = 0;
1870ec21e2ecSJeff Kirsher 		rxbdp->bufPtr = 0;
1871ec21e2ecSJeff Kirsher 		rxbdp++;
187275354148SClaudiu Manoil 
187375354148SClaudiu Manoil 		if (!rxb->page)
187475354148SClaudiu Manoil 			continue;
187575354148SClaudiu Manoil 
187675354148SClaudiu Manoil 		dma_unmap_single(rx_queue->dev, rxb->dma,
187775354148SClaudiu Manoil 				 PAGE_SIZE, DMA_FROM_DEVICE);
187875354148SClaudiu Manoil 		__free_page(rxb->page);
187975354148SClaudiu Manoil 
188075354148SClaudiu Manoil 		rxb->page = NULL;
1881ec21e2ecSJeff Kirsher 	}
188275354148SClaudiu Manoil 
188375354148SClaudiu Manoil 	kfree(rx_queue->rx_buff);
188475354148SClaudiu Manoil 	rx_queue->rx_buff = NULL;
1885ec21e2ecSJeff Kirsher }
1886ec21e2ecSJeff Kirsher 
1887ec21e2ecSJeff Kirsher /* If there are any tx skbs or rx skbs still around, free them.
18880977f817SJan Ceuleers  * Then free tx_skbuff and rx_skbuff
18890977f817SJan Ceuleers  */
1890ec21e2ecSJeff Kirsher static void free_skb_resources(struct gfar_private *priv)
1891ec21e2ecSJeff Kirsher {
1892ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
1893ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
1894ec21e2ecSJeff Kirsher 	int i;
1895ec21e2ecSJeff Kirsher 
1896ec21e2ecSJeff Kirsher 	/* Go through all the buffer descriptors and free their data buffers */
1897ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
1898d8a0f1b0SPaul Gortmaker 		struct netdev_queue *txq;
1899bc4598bcSJan Ceuleers 
1900ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
1901d8a0f1b0SPaul Gortmaker 		txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
1902ec21e2ecSJeff Kirsher 		if (tx_queue->tx_skbuff)
1903ec21e2ecSJeff Kirsher 			free_skb_tx_queue(tx_queue);
1904d8a0f1b0SPaul Gortmaker 		netdev_tx_reset_queue(txq);
1905ec21e2ecSJeff Kirsher 	}
1906ec21e2ecSJeff Kirsher 
1907ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
1908ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
190975354148SClaudiu Manoil 		if (rx_queue->rx_buff)
1910ec21e2ecSJeff Kirsher 			free_skb_rx_queue(rx_queue);
1911ec21e2ecSJeff Kirsher 	}
1912ec21e2ecSJeff Kirsher 
1913369ec162SClaudiu Manoil 	dma_free_coherent(priv->dev,
1914ec21e2ecSJeff Kirsher 			  sizeof(struct txbd8) * priv->total_tx_ring_size +
1915ec21e2ecSJeff Kirsher 			  sizeof(struct rxbd8) * priv->total_rx_ring_size,
1916ec21e2ecSJeff Kirsher 			  priv->tx_queue[0]->tx_bd_base,
1917ec21e2ecSJeff Kirsher 			  priv->tx_queue[0]->tx_bd_dma_base);
1918ec21e2ecSJeff Kirsher }
1919ec21e2ecSJeff Kirsher 
1920c10650b6SClaudiu Manoil void gfar_start(struct gfar_private *priv)
1921ec21e2ecSJeff Kirsher {
1922ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1923ec21e2ecSJeff Kirsher 	u32 tempval;
1924ec21e2ecSJeff Kirsher 	int i = 0;
1925ec21e2ecSJeff Kirsher 
1926c10650b6SClaudiu Manoil 	/* Enable Rx/Tx hw queues */
1927c10650b6SClaudiu Manoil 	gfar_write(&regs->rqueue, priv->rqueue);
1928c10650b6SClaudiu Manoil 	gfar_write(&regs->tqueue, priv->tqueue);
1929ec21e2ecSJeff Kirsher 
1930ec21e2ecSJeff Kirsher 	/* Initialize DMACTRL to have WWR and WOP */
1931ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->dmactrl);
1932ec21e2ecSJeff Kirsher 	tempval |= DMACTRL_INIT_SETTINGS;
1933ec21e2ecSJeff Kirsher 	gfar_write(&regs->dmactrl, tempval);
1934ec21e2ecSJeff Kirsher 
1935ec21e2ecSJeff Kirsher 	/* Make sure we aren't stopped */
1936ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->dmactrl);
1937ec21e2ecSJeff Kirsher 	tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
1938ec21e2ecSJeff Kirsher 	gfar_write(&regs->dmactrl, tempval);
1939ec21e2ecSJeff Kirsher 
1940ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_grps; i++) {
1941ec21e2ecSJeff Kirsher 		regs = priv->gfargrp[i].regs;
1942ec21e2ecSJeff Kirsher 		/* Clear THLT/RHLT, so that the DMA starts polling now */
1943ec21e2ecSJeff Kirsher 		gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1944ec21e2ecSJeff Kirsher 		gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
1945ec21e2ecSJeff Kirsher 	}
1946ec21e2ecSJeff Kirsher 
1947c10650b6SClaudiu Manoil 	/* Enable Rx/Tx DMA */
1948c10650b6SClaudiu Manoil 	tempval = gfar_read(&regs->maccfg1);
1949c10650b6SClaudiu Manoil 	tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1950c10650b6SClaudiu Manoil 	gfar_write(&regs->maccfg1, tempval);
1951c10650b6SClaudiu Manoil 
1952efeddce7SClaudiu Manoil 	gfar_ints_enable(priv);
1953efeddce7SClaudiu Manoil 
1954c10650b6SClaudiu Manoil 	priv->ndev->trans_start = jiffies; /* prevent tx timeout */
1955ec21e2ecSJeff Kirsher }
1956ec21e2ecSJeff Kirsher 
195780ec396cSClaudiu Manoil static void free_grp_irqs(struct gfar_priv_grp *grp)
195880ec396cSClaudiu Manoil {
195980ec396cSClaudiu Manoil 	free_irq(gfar_irq(grp, TX)->irq, grp);
196080ec396cSClaudiu Manoil 	free_irq(gfar_irq(grp, RX)->irq, grp);
196180ec396cSClaudiu Manoil 	free_irq(gfar_irq(grp, ER)->irq, grp);
196280ec396cSClaudiu Manoil }
196380ec396cSClaudiu Manoil 
1964ec21e2ecSJeff Kirsher static int register_grp_irqs(struct gfar_priv_grp *grp)
1965ec21e2ecSJeff Kirsher {
1966ec21e2ecSJeff Kirsher 	struct gfar_private *priv = grp->priv;
1967ec21e2ecSJeff Kirsher 	struct net_device *dev = priv->ndev;
1968ec21e2ecSJeff Kirsher 	int err;
1969ec21e2ecSJeff Kirsher 
1970ec21e2ecSJeff Kirsher 	/* If the device has multiple interrupts, register for
19710977f817SJan Ceuleers 	 * them.  Otherwise, only register for the one
19720977f817SJan Ceuleers 	 */
1973ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1974ec21e2ecSJeff Kirsher 		/* Install our interrupt handlers for Error,
19750977f817SJan Ceuleers 		 * Transmit, and Receive
19760977f817SJan Ceuleers 		 */
1977d5b8d640SSudeep Holla 		err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
1978ee873fdaSClaudiu Manoil 				  gfar_irq(grp, ER)->name, grp);
1979ee873fdaSClaudiu Manoil 		if (err < 0) {
1980ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1981ee873fdaSClaudiu Manoil 				  gfar_irq(grp, ER)->irq);
1982ec21e2ecSJeff Kirsher 
1983ec21e2ecSJeff Kirsher 			goto err_irq_fail;
1984ec21e2ecSJeff Kirsher 		}
1985d5b8d640SSudeep Holla 		enable_irq_wake(gfar_irq(grp, ER)->irq);
1986d5b8d640SSudeep Holla 
1987ee873fdaSClaudiu Manoil 		err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
1988ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->name, grp);
1989ee873fdaSClaudiu Manoil 		if (err < 0) {
1990ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1991ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->irq);
1992ec21e2ecSJeff Kirsher 			goto tx_irq_fail;
1993ec21e2ecSJeff Kirsher 		}
1994ee873fdaSClaudiu Manoil 		err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
1995ee873fdaSClaudiu Manoil 				  gfar_irq(grp, RX)->name, grp);
1996ee873fdaSClaudiu Manoil 		if (err < 0) {
1997ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1998ee873fdaSClaudiu Manoil 				  gfar_irq(grp, RX)->irq);
1999ec21e2ecSJeff Kirsher 			goto rx_irq_fail;
2000ec21e2ecSJeff Kirsher 		}
2001ec21e2ecSJeff Kirsher 	} else {
2002d5b8d640SSudeep Holla 		err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
2003ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->name, grp);
2004ee873fdaSClaudiu Manoil 		if (err < 0) {
2005ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
2006ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->irq);
2007ec21e2ecSJeff Kirsher 			goto err_irq_fail;
2008ec21e2ecSJeff Kirsher 		}
2009d5b8d640SSudeep Holla 		enable_irq_wake(gfar_irq(grp, TX)->irq);
2010ec21e2ecSJeff Kirsher 	}
2011ec21e2ecSJeff Kirsher 
2012ec21e2ecSJeff Kirsher 	return 0;
2013ec21e2ecSJeff Kirsher 
2014ec21e2ecSJeff Kirsher rx_irq_fail:
2015ee873fdaSClaudiu Manoil 	free_irq(gfar_irq(grp, TX)->irq, grp);
2016ec21e2ecSJeff Kirsher tx_irq_fail:
2017ee873fdaSClaudiu Manoil 	free_irq(gfar_irq(grp, ER)->irq, grp);
2018ec21e2ecSJeff Kirsher err_irq_fail:
2019ec21e2ecSJeff Kirsher 	return err;
2020ec21e2ecSJeff Kirsher 
2021ec21e2ecSJeff Kirsher }
2022ec21e2ecSJeff Kirsher 
202380ec396cSClaudiu Manoil static void gfar_free_irq(struct gfar_private *priv)
202480ec396cSClaudiu Manoil {
202580ec396cSClaudiu Manoil 	int i;
202680ec396cSClaudiu Manoil 
202780ec396cSClaudiu Manoil 	/* Free the IRQs */
202880ec396cSClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
202980ec396cSClaudiu Manoil 		for (i = 0; i < priv->num_grps; i++)
203080ec396cSClaudiu Manoil 			free_grp_irqs(&priv->gfargrp[i]);
203180ec396cSClaudiu Manoil 	} else {
203280ec396cSClaudiu Manoil 		for (i = 0; i < priv->num_grps; i++)
203380ec396cSClaudiu Manoil 			free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
203480ec396cSClaudiu Manoil 				 &priv->gfargrp[i]);
203580ec396cSClaudiu Manoil 	}
203680ec396cSClaudiu Manoil }
203780ec396cSClaudiu Manoil 
203880ec396cSClaudiu Manoil static int gfar_request_irq(struct gfar_private *priv)
203980ec396cSClaudiu Manoil {
204080ec396cSClaudiu Manoil 	int err, i, j;
204180ec396cSClaudiu Manoil 
204280ec396cSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
204380ec396cSClaudiu Manoil 		err = register_grp_irqs(&priv->gfargrp[i]);
204480ec396cSClaudiu Manoil 		if (err) {
204580ec396cSClaudiu Manoil 			for (j = 0; j < i; j++)
204680ec396cSClaudiu Manoil 				free_grp_irqs(&priv->gfargrp[j]);
204780ec396cSClaudiu Manoil 			return err;
204880ec396cSClaudiu Manoil 		}
204980ec396cSClaudiu Manoil 	}
205080ec396cSClaudiu Manoil 
205180ec396cSClaudiu Manoil 	return 0;
205280ec396cSClaudiu Manoil }
205380ec396cSClaudiu Manoil 
2054ec21e2ecSJeff Kirsher /* Bring the controller up and running */
2055ec21e2ecSJeff Kirsher int startup_gfar(struct net_device *ndev)
2056ec21e2ecSJeff Kirsher {
2057ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(ndev);
205880ec396cSClaudiu Manoil 	int err;
2059ec21e2ecSJeff Kirsher 
2060a328ac92SClaudiu Manoil 	gfar_mac_reset(priv);
2061ec21e2ecSJeff Kirsher 
2062ec21e2ecSJeff Kirsher 	err = gfar_alloc_skb_resources(ndev);
2063ec21e2ecSJeff Kirsher 	if (err)
2064ec21e2ecSJeff Kirsher 		return err;
2065ec21e2ecSJeff Kirsher 
2066a328ac92SClaudiu Manoil 	gfar_init_tx_rx_base(priv);
2067ec21e2ecSJeff Kirsher 
20684e857c58SPeter Zijlstra 	smp_mb__before_atomic();
20690851133bSClaudiu Manoil 	clear_bit(GFAR_DOWN, &priv->state);
20704e857c58SPeter Zijlstra 	smp_mb__after_atomic();
20710851133bSClaudiu Manoil 
20720851133bSClaudiu Manoil 	/* Start Rx/Tx DMA and enable the interrupts */
2073c10650b6SClaudiu Manoil 	gfar_start(priv);
2074ec21e2ecSJeff Kirsher 
20752a4eebf0SClaudiu Manoil 	/* force link state update after mac reset */
20762a4eebf0SClaudiu Manoil 	priv->oldlink = 0;
20772a4eebf0SClaudiu Manoil 	priv->oldspeed = 0;
20782a4eebf0SClaudiu Manoil 	priv->oldduplex = -1;
20792a4eebf0SClaudiu Manoil 
2080ec21e2ecSJeff Kirsher 	phy_start(priv->phydev);
2081ec21e2ecSJeff Kirsher 
20820851133bSClaudiu Manoil 	enable_napi(priv);
20830851133bSClaudiu Manoil 
20840851133bSClaudiu Manoil 	netif_tx_wake_all_queues(ndev);
20850851133bSClaudiu Manoil 
2086ec21e2ecSJeff Kirsher 	return 0;
2087ec21e2ecSJeff Kirsher }
2088ec21e2ecSJeff Kirsher 
20890977f817SJan Ceuleers /* Called when something needs to use the ethernet device
20900977f817SJan Ceuleers  * Returns 0 for success.
20910977f817SJan Ceuleers  */
2092ec21e2ecSJeff Kirsher static int gfar_enet_open(struct net_device *dev)
2093ec21e2ecSJeff Kirsher {
2094ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2095ec21e2ecSJeff Kirsher 	int err;
2096ec21e2ecSJeff Kirsher 
2097ec21e2ecSJeff Kirsher 	err = init_phy(dev);
20980851133bSClaudiu Manoil 	if (err)
2099ec21e2ecSJeff Kirsher 		return err;
2100ec21e2ecSJeff Kirsher 
210180ec396cSClaudiu Manoil 	err = gfar_request_irq(priv);
210280ec396cSClaudiu Manoil 	if (err)
210380ec396cSClaudiu Manoil 		return err;
210480ec396cSClaudiu Manoil 
2105ec21e2ecSJeff Kirsher 	err = startup_gfar(dev);
21060851133bSClaudiu Manoil 	if (err)
2107ec21e2ecSJeff Kirsher 		return err;
2108ec21e2ecSJeff Kirsher 
2109ec21e2ecSJeff Kirsher 	return err;
2110ec21e2ecSJeff Kirsher }
2111ec21e2ecSJeff Kirsher 
2112ec21e2ecSJeff Kirsher static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
2113ec21e2ecSJeff Kirsher {
2114ec21e2ecSJeff Kirsher 	struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
2115ec21e2ecSJeff Kirsher 
2116ec21e2ecSJeff Kirsher 	memset(fcb, 0, GMAC_FCB_LEN);
2117ec21e2ecSJeff Kirsher 
2118ec21e2ecSJeff Kirsher 	return fcb;
2119ec21e2ecSJeff Kirsher }
2120ec21e2ecSJeff Kirsher 
21219c4886e5SManfred Rudigier static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
21229c4886e5SManfred Rudigier 				    int fcb_length)
2123ec21e2ecSJeff Kirsher {
2124ec21e2ecSJeff Kirsher 	/* If we're here, it's a IP packet with a TCP or UDP
2125ec21e2ecSJeff Kirsher 	 * payload.  We set it to checksum, using a pseudo-header
2126ec21e2ecSJeff Kirsher 	 * we provide
2127ec21e2ecSJeff Kirsher 	 */
21283a2e16c8SJan Ceuleers 	u8 flags = TXFCB_DEFAULT;
2129ec21e2ecSJeff Kirsher 
21300977f817SJan Ceuleers 	/* Tell the controller what the protocol is
21310977f817SJan Ceuleers 	 * And provide the already calculated phcs
21320977f817SJan Ceuleers 	 */
2133ec21e2ecSJeff Kirsher 	if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
2134ec21e2ecSJeff Kirsher 		flags |= TXFCB_UDP;
213526eb9374SClaudiu Manoil 		fcb->phcs = (__force __be16)(udp_hdr(skb)->check);
2136ec21e2ecSJeff Kirsher 	} else
213726eb9374SClaudiu Manoil 		fcb->phcs = (__force __be16)(tcp_hdr(skb)->check);
2138ec21e2ecSJeff Kirsher 
2139ec21e2ecSJeff Kirsher 	/* l3os is the distance between the start of the
2140ec21e2ecSJeff Kirsher 	 * frame (skb->data) and the start of the IP hdr.
2141ec21e2ecSJeff Kirsher 	 * l4os is the distance between the start of the
21420977f817SJan Ceuleers 	 * l3 hdr and the l4 hdr
21430977f817SJan Ceuleers 	 */
214426eb9374SClaudiu Manoil 	fcb->l3os = (u8)(skb_network_offset(skb) - fcb_length);
2145ec21e2ecSJeff Kirsher 	fcb->l4os = skb_network_header_len(skb);
2146ec21e2ecSJeff Kirsher 
2147ec21e2ecSJeff Kirsher 	fcb->flags = flags;
2148ec21e2ecSJeff Kirsher }
2149ec21e2ecSJeff Kirsher 
2150ec21e2ecSJeff Kirsher void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
2151ec21e2ecSJeff Kirsher {
2152ec21e2ecSJeff Kirsher 	fcb->flags |= TXFCB_VLN;
215326eb9374SClaudiu Manoil 	fcb->vlctl = cpu_to_be16(skb_vlan_tag_get(skb));
2154ec21e2ecSJeff Kirsher }
2155ec21e2ecSJeff Kirsher 
2156ec21e2ecSJeff Kirsher static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
2157ec21e2ecSJeff Kirsher 				      struct txbd8 *base, int ring_size)
2158ec21e2ecSJeff Kirsher {
2159ec21e2ecSJeff Kirsher 	struct txbd8 *new_bd = bdp + stride;
2160ec21e2ecSJeff Kirsher 
2161ec21e2ecSJeff Kirsher 	return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2162ec21e2ecSJeff Kirsher }
2163ec21e2ecSJeff Kirsher 
2164ec21e2ecSJeff Kirsher static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
2165ec21e2ecSJeff Kirsher 				      int ring_size)
2166ec21e2ecSJeff Kirsher {
2167ec21e2ecSJeff Kirsher 	return skip_txbd(bdp, 1, base, ring_size);
2168ec21e2ecSJeff Kirsher }
2169ec21e2ecSJeff Kirsher 
217002d88fb4SClaudiu Manoil /* eTSEC12: csum generation not supported for some fcb offsets */
217102d88fb4SClaudiu Manoil static inline bool gfar_csum_errata_12(struct gfar_private *priv,
217202d88fb4SClaudiu Manoil 				       unsigned long fcb_addr)
217302d88fb4SClaudiu Manoil {
217402d88fb4SClaudiu Manoil 	return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
217502d88fb4SClaudiu Manoil 	       (fcb_addr % 0x20) > 0x18);
217602d88fb4SClaudiu Manoil }
217702d88fb4SClaudiu Manoil 
217802d88fb4SClaudiu Manoil /* eTSEC76: csum generation for frames larger than 2500 may
217902d88fb4SClaudiu Manoil  * cause excess delays before start of transmission
218002d88fb4SClaudiu Manoil  */
218102d88fb4SClaudiu Manoil static inline bool gfar_csum_errata_76(struct gfar_private *priv,
218202d88fb4SClaudiu Manoil 				       unsigned int len)
218302d88fb4SClaudiu Manoil {
218402d88fb4SClaudiu Manoil 	return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
218502d88fb4SClaudiu Manoil 	       (len > 2500));
218602d88fb4SClaudiu Manoil }
218702d88fb4SClaudiu Manoil 
21880977f817SJan Ceuleers /* This is called by the kernel when a frame is ready for transmission.
21890977f817SJan Ceuleers  * It is pointed to by the dev->hard_start_xmit function pointer
21900977f817SJan Ceuleers  */
2191ec21e2ecSJeff Kirsher static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2192ec21e2ecSJeff Kirsher {
2193ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2194ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
2195ec21e2ecSJeff Kirsher 	struct netdev_queue *txq;
2196ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = NULL;
2197ec21e2ecSJeff Kirsher 	struct txfcb *fcb = NULL;
2198ec21e2ecSJeff Kirsher 	struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
2199ec21e2ecSJeff Kirsher 	u32 lstatus;
22000d0cffdcSClaudiu Manoil 	int i, rq = 0;
22010d0cffdcSClaudiu Manoil 	int do_tstamp, do_csum, do_vlan;
2202ec21e2ecSJeff Kirsher 	u32 bufaddr;
220350ad076bSClaudiu Manoil 	unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
2204ec21e2ecSJeff Kirsher 
2205ec21e2ecSJeff Kirsher 	rq = skb->queue_mapping;
2206ec21e2ecSJeff Kirsher 	tx_queue = priv->tx_queue[rq];
2207ec21e2ecSJeff Kirsher 	txq = netdev_get_tx_queue(dev, rq);
2208ec21e2ecSJeff Kirsher 	base = tx_queue->tx_bd_base;
2209ec21e2ecSJeff Kirsher 	regs = tx_queue->grp->regs;
2210ec21e2ecSJeff Kirsher 
22110d0cffdcSClaudiu Manoil 	do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
2212df8a39deSJiri Pirko 	do_vlan = skb_vlan_tag_present(skb);
22130d0cffdcSClaudiu Manoil 	do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
22140d0cffdcSClaudiu Manoil 		    priv->hwts_tx_en;
22150d0cffdcSClaudiu Manoil 
22160d0cffdcSClaudiu Manoil 	if (do_csum || do_vlan)
22170d0cffdcSClaudiu Manoil 		fcb_len = GMAC_FCB_LEN;
22180d0cffdcSClaudiu Manoil 
2219ec21e2ecSJeff Kirsher 	/* check if time stamp should be generated */
22200d0cffdcSClaudiu Manoil 	if (unlikely(do_tstamp))
22210d0cffdcSClaudiu Manoil 		fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2222ec21e2ecSJeff Kirsher 
2223ec21e2ecSJeff Kirsher 	/* make space for additional header when fcb is needed */
22240d0cffdcSClaudiu Manoil 	if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
2225ec21e2ecSJeff Kirsher 		struct sk_buff *skb_new;
2226ec21e2ecSJeff Kirsher 
22270d0cffdcSClaudiu Manoil 		skb_new = skb_realloc_headroom(skb, fcb_len);
2228ec21e2ecSJeff Kirsher 		if (!skb_new) {
2229ec21e2ecSJeff Kirsher 			dev->stats.tx_errors++;
2230c9974ad4SEric W. Biederman 			dev_kfree_skb_any(skb);
2231ec21e2ecSJeff Kirsher 			return NETDEV_TX_OK;
2232ec21e2ecSJeff Kirsher 		}
2233db83d136SManfred Rudigier 
2234313b037cSEric Dumazet 		if (skb->sk)
2235313b037cSEric Dumazet 			skb_set_owner_w(skb_new, skb->sk);
2236c9974ad4SEric W. Biederman 		dev_consume_skb_any(skb);
2237ec21e2ecSJeff Kirsher 		skb = skb_new;
2238ec21e2ecSJeff Kirsher 	}
2239ec21e2ecSJeff Kirsher 
2240ec21e2ecSJeff Kirsher 	/* total number of fragments in the SKB */
2241ec21e2ecSJeff Kirsher 	nr_frags = skb_shinfo(skb)->nr_frags;
2242ec21e2ecSJeff Kirsher 
2243ec21e2ecSJeff Kirsher 	/* calculate the required number of TxBDs for this skb */
2244ec21e2ecSJeff Kirsher 	if (unlikely(do_tstamp))
2245ec21e2ecSJeff Kirsher 		nr_txbds = nr_frags + 2;
2246ec21e2ecSJeff Kirsher 	else
2247ec21e2ecSJeff Kirsher 		nr_txbds = nr_frags + 1;
2248ec21e2ecSJeff Kirsher 
2249ec21e2ecSJeff Kirsher 	/* check if there is space to queue this packet */
2250ec21e2ecSJeff Kirsher 	if (nr_txbds > tx_queue->num_txbdfree) {
2251ec21e2ecSJeff Kirsher 		/* no space, stop the queue */
2252ec21e2ecSJeff Kirsher 		netif_tx_stop_queue(txq);
2253ec21e2ecSJeff Kirsher 		dev->stats.tx_fifo_errors++;
2254ec21e2ecSJeff Kirsher 		return NETDEV_TX_BUSY;
2255ec21e2ecSJeff Kirsher 	}
2256ec21e2ecSJeff Kirsher 
2257ec21e2ecSJeff Kirsher 	/* Update transmit stats */
225850ad076bSClaudiu Manoil 	bytes_sent = skb->len;
225950ad076bSClaudiu Manoil 	tx_queue->stats.tx_bytes += bytes_sent;
226050ad076bSClaudiu Manoil 	/* keep Tx bytes on wire for BQL accounting */
226150ad076bSClaudiu Manoil 	GFAR_CB(skb)->bytes_sent = bytes_sent;
2262ec21e2ecSJeff Kirsher 	tx_queue->stats.tx_packets++;
2263ec21e2ecSJeff Kirsher 
2264ec21e2ecSJeff Kirsher 	txbdp = txbdp_start = tx_queue->cur_tx;
2265a7312d58SClaudiu Manoil 	lstatus = be32_to_cpu(txbdp->lstatus);
2266ec21e2ecSJeff Kirsher 
2267ec21e2ecSJeff Kirsher 	/* Time stamp insertion requires one additional TxBD */
2268ec21e2ecSJeff Kirsher 	if (unlikely(do_tstamp))
2269ec21e2ecSJeff Kirsher 		txbdp_tstamp = txbdp = next_txbd(txbdp, base,
2270ec21e2ecSJeff Kirsher 						 tx_queue->tx_ring_size);
2271ec21e2ecSJeff Kirsher 
2272ec21e2ecSJeff Kirsher 	if (nr_frags == 0) {
2273a7312d58SClaudiu Manoil 		if (unlikely(do_tstamp)) {
2274a7312d58SClaudiu Manoil 			u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus);
2275a7312d58SClaudiu Manoil 
2276a7312d58SClaudiu Manoil 			lstatus_ts |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2277a7312d58SClaudiu Manoil 			txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts);
2278a7312d58SClaudiu Manoil 		} else {
2279ec21e2ecSJeff Kirsher 			lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2280a7312d58SClaudiu Manoil 		}
2281ec21e2ecSJeff Kirsher 	} else {
2282ec21e2ecSJeff Kirsher 		/* Place the fragment addresses and lengths into the TxBDs */
2283ec21e2ecSJeff Kirsher 		for (i = 0; i < nr_frags; i++) {
228450ad076bSClaudiu Manoil 			unsigned int frag_len;
2285ec21e2ecSJeff Kirsher 			/* Point at the next BD, wrapping as needed */
2286ec21e2ecSJeff Kirsher 			txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2287ec21e2ecSJeff Kirsher 
228850ad076bSClaudiu Manoil 			frag_len = skb_shinfo(skb)->frags[i].size;
2289ec21e2ecSJeff Kirsher 
2290a7312d58SClaudiu Manoil 			lstatus = be32_to_cpu(txbdp->lstatus) | frag_len |
2291ec21e2ecSJeff Kirsher 				  BD_LFLAG(TXBD_READY);
2292ec21e2ecSJeff Kirsher 
2293ec21e2ecSJeff Kirsher 			/* Handle the last BD specially */
2294ec21e2ecSJeff Kirsher 			if (i == nr_frags - 1)
2295ec21e2ecSJeff Kirsher 				lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2296ec21e2ecSJeff Kirsher 
2297369ec162SClaudiu Manoil 			bufaddr = skb_frag_dma_map(priv->dev,
22982234a722SIan Campbell 						   &skb_shinfo(skb)->frags[i],
22992234a722SIan Campbell 						   0,
230050ad076bSClaudiu Manoil 						   frag_len,
2301ec21e2ecSJeff Kirsher 						   DMA_TO_DEVICE);
23020a4b5a24SKevin Hao 			if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
23030a4b5a24SKevin Hao 				goto dma_map_err;
2304ec21e2ecSJeff Kirsher 
2305ec21e2ecSJeff Kirsher 			/* set the TxBD length and buffer pointer */
2306a7312d58SClaudiu Manoil 			txbdp->bufPtr = cpu_to_be32(bufaddr);
2307a7312d58SClaudiu Manoil 			txbdp->lstatus = cpu_to_be32(lstatus);
2308ec21e2ecSJeff Kirsher 		}
2309ec21e2ecSJeff Kirsher 
2310a7312d58SClaudiu Manoil 		lstatus = be32_to_cpu(txbdp_start->lstatus);
2311ec21e2ecSJeff Kirsher 	}
2312ec21e2ecSJeff Kirsher 
23139c4886e5SManfred Rudigier 	/* Add TxPAL between FCB and frame if required */
23149c4886e5SManfred Rudigier 	if (unlikely(do_tstamp)) {
23159c4886e5SManfred Rudigier 		skb_push(skb, GMAC_TXPAL_LEN);
23169c4886e5SManfred Rudigier 		memset(skb->data, 0, GMAC_TXPAL_LEN);
23179c4886e5SManfred Rudigier 	}
23189c4886e5SManfred Rudigier 
23190d0cffdcSClaudiu Manoil 	/* Add TxFCB if required */
23200d0cffdcSClaudiu Manoil 	if (fcb_len) {
2321ec21e2ecSJeff Kirsher 		fcb = gfar_add_fcb(skb);
2322ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(TXBD_TOE);
23230d0cffdcSClaudiu Manoil 	}
23240d0cffdcSClaudiu Manoil 
23250d0cffdcSClaudiu Manoil 	/* Set up checksumming */
23260d0cffdcSClaudiu Manoil 	if (do_csum) {
23270d0cffdcSClaudiu Manoil 		gfar_tx_checksum(skb, fcb, fcb_len);
232802d88fb4SClaudiu Manoil 
232902d88fb4SClaudiu Manoil 		if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
233002d88fb4SClaudiu Manoil 		    unlikely(gfar_csum_errata_76(priv, skb->len))) {
233102d88fb4SClaudiu Manoil 			__skb_pull(skb, GMAC_FCB_LEN);
233202d88fb4SClaudiu Manoil 			skb_checksum_help(skb);
23330d0cffdcSClaudiu Manoil 			if (do_vlan || do_tstamp) {
23340d0cffdcSClaudiu Manoil 				/* put back a new fcb for vlan/tstamp TOE */
23350d0cffdcSClaudiu Manoil 				fcb = gfar_add_fcb(skb);
23360d0cffdcSClaudiu Manoil 			} else {
23370d0cffdcSClaudiu Manoil 				/* Tx TOE not used */
233802d88fb4SClaudiu Manoil 				lstatus &= ~(BD_LFLAG(TXBD_TOE));
233902d88fb4SClaudiu Manoil 				fcb = NULL;
2340ec21e2ecSJeff Kirsher 			}
2341ec21e2ecSJeff Kirsher 		}
2342ec21e2ecSJeff Kirsher 	}
2343ec21e2ecSJeff Kirsher 
23440d0cffdcSClaudiu Manoil 	if (do_vlan)
2345ec21e2ecSJeff Kirsher 		gfar_tx_vlan(skb, fcb);
2346ec21e2ecSJeff Kirsher 
2347ec21e2ecSJeff Kirsher 	/* Setup tx hardware time stamping if requested */
2348ec21e2ecSJeff Kirsher 	if (unlikely(do_tstamp)) {
2349ec21e2ecSJeff Kirsher 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2350ec21e2ecSJeff Kirsher 		fcb->ptp = 1;
2351ec21e2ecSJeff Kirsher 	}
2352ec21e2ecSJeff Kirsher 
23530a4b5a24SKevin Hao 	bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb),
23540a4b5a24SKevin Hao 				 DMA_TO_DEVICE);
23550a4b5a24SKevin Hao 	if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
23560a4b5a24SKevin Hao 		goto dma_map_err;
23570a4b5a24SKevin Hao 
2358a7312d58SClaudiu Manoil 	txbdp_start->bufPtr = cpu_to_be32(bufaddr);
2359ec21e2ecSJeff Kirsher 
23600977f817SJan Ceuleers 	/* If time stamping is requested one additional TxBD must be set up. The
2361ec21e2ecSJeff Kirsher 	 * first TxBD points to the FCB and must have a data length of
2362ec21e2ecSJeff Kirsher 	 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2363ec21e2ecSJeff Kirsher 	 * the full frame length.
2364ec21e2ecSJeff Kirsher 	 */
2365ec21e2ecSJeff Kirsher 	if (unlikely(do_tstamp)) {
2366a7312d58SClaudiu Manoil 		u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus);
2367a7312d58SClaudiu Manoil 
2368a7312d58SClaudiu Manoil 		bufaddr = be32_to_cpu(txbdp_start->bufPtr);
2369a7312d58SClaudiu Manoil 		bufaddr += fcb_len;
2370a7312d58SClaudiu Manoil 		lstatus_ts |= BD_LFLAG(TXBD_READY) |
23710d0cffdcSClaudiu Manoil 			      (skb_headlen(skb) - fcb_len);
2372a7312d58SClaudiu Manoil 
2373a7312d58SClaudiu Manoil 		txbdp_tstamp->bufPtr = cpu_to_be32(bufaddr);
2374a7312d58SClaudiu Manoil 		txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts);
2375ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2376ec21e2ecSJeff Kirsher 	} else {
2377ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2378ec21e2ecSJeff Kirsher 	}
2379ec21e2ecSJeff Kirsher 
238050ad076bSClaudiu Manoil 	netdev_tx_sent_queue(txq, bytes_sent);
2381d8a0f1b0SPaul Gortmaker 
2382d55398baSClaudiu Manoil 	gfar_wmb();
2383ec21e2ecSJeff Kirsher 
2384a7312d58SClaudiu Manoil 	txbdp_start->lstatus = cpu_to_be32(lstatus);
2385ec21e2ecSJeff Kirsher 
2386d55398baSClaudiu Manoil 	gfar_wmb(); /* force lstatus write before tx_skbuff */
2387ec21e2ecSJeff Kirsher 
2388ec21e2ecSJeff Kirsher 	tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2389ec21e2ecSJeff Kirsher 
2390ec21e2ecSJeff Kirsher 	/* Update the current skb pointer to the next entry we will use
23910977f817SJan Ceuleers 	 * (wrapping if necessary)
23920977f817SJan Ceuleers 	 */
2393ec21e2ecSJeff Kirsher 	tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
2394ec21e2ecSJeff Kirsher 			      TX_RING_MOD_MASK(tx_queue->tx_ring_size);
2395ec21e2ecSJeff Kirsher 
2396ec21e2ecSJeff Kirsher 	tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2397ec21e2ecSJeff Kirsher 
2398bc602280SClaudiu Manoil 	/* We can work in parallel with gfar_clean_tx_ring(), except
2399bc602280SClaudiu Manoil 	 * when modifying num_txbdfree. Note that we didn't grab the lock
2400bc602280SClaudiu Manoil 	 * when we were reading the num_txbdfree and checking for available
2401bc602280SClaudiu Manoil 	 * space, that's because outside of this function it can only grow.
2402bc602280SClaudiu Manoil 	 */
2403bc602280SClaudiu Manoil 	spin_lock_bh(&tx_queue->txlock);
2404ec21e2ecSJeff Kirsher 	/* reduce TxBD free count */
2405ec21e2ecSJeff Kirsher 	tx_queue->num_txbdfree -= (nr_txbds);
2406bc602280SClaudiu Manoil 	spin_unlock_bh(&tx_queue->txlock);
2407ec21e2ecSJeff Kirsher 
2408ec21e2ecSJeff Kirsher 	/* If the next BD still needs to be cleaned up, then the bds
24090977f817SJan Ceuleers 	 * are full.  We need to tell the kernel to stop sending us stuff.
24100977f817SJan Ceuleers 	 */
2411ec21e2ecSJeff Kirsher 	if (!tx_queue->num_txbdfree) {
2412ec21e2ecSJeff Kirsher 		netif_tx_stop_queue(txq);
2413ec21e2ecSJeff Kirsher 
2414ec21e2ecSJeff Kirsher 		dev->stats.tx_fifo_errors++;
2415ec21e2ecSJeff Kirsher 	}
2416ec21e2ecSJeff Kirsher 
2417ec21e2ecSJeff Kirsher 	/* Tell the DMA to go go go */
2418ec21e2ecSJeff Kirsher 	gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
2419ec21e2ecSJeff Kirsher 
2420ec21e2ecSJeff Kirsher 	return NETDEV_TX_OK;
24210a4b5a24SKevin Hao 
24220a4b5a24SKevin Hao dma_map_err:
24230a4b5a24SKevin Hao 	txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size);
24240a4b5a24SKevin Hao 	if (do_tstamp)
24250a4b5a24SKevin Hao 		txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
24260a4b5a24SKevin Hao 	for (i = 0; i < nr_frags; i++) {
2427a7312d58SClaudiu Manoil 		lstatus = be32_to_cpu(txbdp->lstatus);
24280a4b5a24SKevin Hao 		if (!(lstatus & BD_LFLAG(TXBD_READY)))
24290a4b5a24SKevin Hao 			break;
24300a4b5a24SKevin Hao 
2431a7312d58SClaudiu Manoil 		lstatus &= ~BD_LFLAG(TXBD_READY);
2432a7312d58SClaudiu Manoil 		txbdp->lstatus = cpu_to_be32(lstatus);
2433a7312d58SClaudiu Manoil 		bufaddr = be32_to_cpu(txbdp->bufPtr);
2434a7312d58SClaudiu Manoil 		dma_unmap_page(priv->dev, bufaddr, be16_to_cpu(txbdp->length),
24350a4b5a24SKevin Hao 			       DMA_TO_DEVICE);
24360a4b5a24SKevin Hao 		txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
24370a4b5a24SKevin Hao 	}
24380a4b5a24SKevin Hao 	gfar_wmb();
24390a4b5a24SKevin Hao 	dev_kfree_skb_any(skb);
24400a4b5a24SKevin Hao 	return NETDEV_TX_OK;
2441ec21e2ecSJeff Kirsher }
2442ec21e2ecSJeff Kirsher 
2443ec21e2ecSJeff Kirsher /* Stops the kernel queue, and halts the controller */
2444ec21e2ecSJeff Kirsher static int gfar_close(struct net_device *dev)
2445ec21e2ecSJeff Kirsher {
2446ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2447ec21e2ecSJeff Kirsher 
2448ec21e2ecSJeff Kirsher 	cancel_work_sync(&priv->reset_task);
2449ec21e2ecSJeff Kirsher 	stop_gfar(dev);
2450ec21e2ecSJeff Kirsher 
2451ec21e2ecSJeff Kirsher 	/* Disconnect from the PHY */
2452ec21e2ecSJeff Kirsher 	phy_disconnect(priv->phydev);
2453ec21e2ecSJeff Kirsher 	priv->phydev = NULL;
2454ec21e2ecSJeff Kirsher 
245580ec396cSClaudiu Manoil 	gfar_free_irq(priv);
245680ec396cSClaudiu Manoil 
2457ec21e2ecSJeff Kirsher 	return 0;
2458ec21e2ecSJeff Kirsher }
2459ec21e2ecSJeff Kirsher 
2460ec21e2ecSJeff Kirsher /* Changes the mac address if the controller is not running. */
2461ec21e2ecSJeff Kirsher static int gfar_set_mac_address(struct net_device *dev)
2462ec21e2ecSJeff Kirsher {
2463ec21e2ecSJeff Kirsher 	gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
2464ec21e2ecSJeff Kirsher 
2465ec21e2ecSJeff Kirsher 	return 0;
2466ec21e2ecSJeff Kirsher }
2467ec21e2ecSJeff Kirsher 
2468ec21e2ecSJeff Kirsher static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2469ec21e2ecSJeff Kirsher {
2470ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2471ec21e2ecSJeff Kirsher 	int frame_size = new_mtu + ETH_HLEN;
2472ec21e2ecSJeff Kirsher 
247375354148SClaudiu Manoil 	if ((frame_size < 64) || (frame_size > GFAR_JUMBO_FRAME_SIZE)) {
2474ec21e2ecSJeff Kirsher 		netif_err(priv, drv, dev, "Invalid MTU setting\n");
2475ec21e2ecSJeff Kirsher 		return -EINVAL;
2476ec21e2ecSJeff Kirsher 	}
2477ec21e2ecSJeff Kirsher 
24780851133bSClaudiu Manoil 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
24790851133bSClaudiu Manoil 		cpu_relax();
24800851133bSClaudiu Manoil 
248188302648SClaudiu Manoil 	if (dev->flags & IFF_UP)
2482ec21e2ecSJeff Kirsher 		stop_gfar(dev);
2483ec21e2ecSJeff Kirsher 
2484ec21e2ecSJeff Kirsher 	dev->mtu = new_mtu;
2485ec21e2ecSJeff Kirsher 
248688302648SClaudiu Manoil 	if (dev->flags & IFF_UP)
2487ec21e2ecSJeff Kirsher 		startup_gfar(dev);
2488ec21e2ecSJeff Kirsher 
24890851133bSClaudiu Manoil 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
24900851133bSClaudiu Manoil 
2491ec21e2ecSJeff Kirsher 	return 0;
2492ec21e2ecSJeff Kirsher }
2493ec21e2ecSJeff Kirsher 
24940851133bSClaudiu Manoil void reset_gfar(struct net_device *ndev)
24950851133bSClaudiu Manoil {
24960851133bSClaudiu Manoil 	struct gfar_private *priv = netdev_priv(ndev);
24970851133bSClaudiu Manoil 
24980851133bSClaudiu Manoil 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
24990851133bSClaudiu Manoil 		cpu_relax();
25000851133bSClaudiu Manoil 
25010851133bSClaudiu Manoil 	stop_gfar(ndev);
25020851133bSClaudiu Manoil 	startup_gfar(ndev);
25030851133bSClaudiu Manoil 
25040851133bSClaudiu Manoil 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
25050851133bSClaudiu Manoil }
25060851133bSClaudiu Manoil 
2507ec21e2ecSJeff Kirsher /* gfar_reset_task gets scheduled when a packet has not been
2508ec21e2ecSJeff Kirsher  * transmitted after a set amount of time.
2509ec21e2ecSJeff Kirsher  * For now, assume that clearing out all the structures, and
2510ec21e2ecSJeff Kirsher  * starting over will fix the problem.
2511ec21e2ecSJeff Kirsher  */
2512ec21e2ecSJeff Kirsher static void gfar_reset_task(struct work_struct *work)
2513ec21e2ecSJeff Kirsher {
2514ec21e2ecSJeff Kirsher 	struct gfar_private *priv = container_of(work, struct gfar_private,
2515ec21e2ecSJeff Kirsher 						 reset_task);
25160851133bSClaudiu Manoil 	reset_gfar(priv->ndev);
2517ec21e2ecSJeff Kirsher }
2518ec21e2ecSJeff Kirsher 
2519ec21e2ecSJeff Kirsher static void gfar_timeout(struct net_device *dev)
2520ec21e2ecSJeff Kirsher {
2521ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2522ec21e2ecSJeff Kirsher 
2523ec21e2ecSJeff Kirsher 	dev->stats.tx_errors++;
2524ec21e2ecSJeff Kirsher 	schedule_work(&priv->reset_task);
2525ec21e2ecSJeff Kirsher }
2526ec21e2ecSJeff Kirsher 
2527ec21e2ecSJeff Kirsher /* Interrupt Handler for Transmit complete */
2528c233cf40SClaudiu Manoil static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
2529ec21e2ecSJeff Kirsher {
2530ec21e2ecSJeff Kirsher 	struct net_device *dev = tx_queue->dev;
2531d8a0f1b0SPaul Gortmaker 	struct netdev_queue *txq;
2532ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2533ec21e2ecSJeff Kirsher 	struct txbd8 *bdp, *next = NULL;
2534ec21e2ecSJeff Kirsher 	struct txbd8 *lbdp = NULL;
2535ec21e2ecSJeff Kirsher 	struct txbd8 *base = tx_queue->tx_bd_base;
2536ec21e2ecSJeff Kirsher 	struct sk_buff *skb;
2537ec21e2ecSJeff Kirsher 	int skb_dirtytx;
2538ec21e2ecSJeff Kirsher 	int tx_ring_size = tx_queue->tx_ring_size;
2539ec21e2ecSJeff Kirsher 	int frags = 0, nr_txbds = 0;
2540ec21e2ecSJeff Kirsher 	int i;
2541ec21e2ecSJeff Kirsher 	int howmany = 0;
2542d8a0f1b0SPaul Gortmaker 	int tqi = tx_queue->qindex;
2543d8a0f1b0SPaul Gortmaker 	unsigned int bytes_sent = 0;
2544ec21e2ecSJeff Kirsher 	u32 lstatus;
2545ec21e2ecSJeff Kirsher 	size_t buflen;
2546ec21e2ecSJeff Kirsher 
2547d8a0f1b0SPaul Gortmaker 	txq = netdev_get_tx_queue(dev, tqi);
2548ec21e2ecSJeff Kirsher 	bdp = tx_queue->dirty_tx;
2549ec21e2ecSJeff Kirsher 	skb_dirtytx = tx_queue->skb_dirtytx;
2550ec21e2ecSJeff Kirsher 
2551ec21e2ecSJeff Kirsher 	while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
2552ec21e2ecSJeff Kirsher 
2553ec21e2ecSJeff Kirsher 		frags = skb_shinfo(skb)->nr_frags;
2554ec21e2ecSJeff Kirsher 
25550977f817SJan Ceuleers 		/* When time stamping, one additional TxBD must be freed.
2556ec21e2ecSJeff Kirsher 		 * Also, we need to dma_unmap_single() the TxPAL.
2557ec21e2ecSJeff Kirsher 		 */
2558ec21e2ecSJeff Kirsher 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
2559ec21e2ecSJeff Kirsher 			nr_txbds = frags + 2;
2560ec21e2ecSJeff Kirsher 		else
2561ec21e2ecSJeff Kirsher 			nr_txbds = frags + 1;
2562ec21e2ecSJeff Kirsher 
2563ec21e2ecSJeff Kirsher 		lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
2564ec21e2ecSJeff Kirsher 
2565a7312d58SClaudiu Manoil 		lstatus = be32_to_cpu(lbdp->lstatus);
2566ec21e2ecSJeff Kirsher 
2567ec21e2ecSJeff Kirsher 		/* Only clean completed frames */
2568ec21e2ecSJeff Kirsher 		if ((lstatus & BD_LFLAG(TXBD_READY)) &&
2569ec21e2ecSJeff Kirsher 		    (lstatus & BD_LENGTH_MASK))
2570ec21e2ecSJeff Kirsher 			break;
2571ec21e2ecSJeff Kirsher 
2572ec21e2ecSJeff Kirsher 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
2573ec21e2ecSJeff Kirsher 			next = next_txbd(bdp, base, tx_ring_size);
2574a7312d58SClaudiu Manoil 			buflen = be16_to_cpu(next->length) +
2575a7312d58SClaudiu Manoil 				 GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2576ec21e2ecSJeff Kirsher 		} else
2577a7312d58SClaudiu Manoil 			buflen = be16_to_cpu(bdp->length);
2578ec21e2ecSJeff Kirsher 
2579a7312d58SClaudiu Manoil 		dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr),
2580ec21e2ecSJeff Kirsher 				 buflen, DMA_TO_DEVICE);
2581ec21e2ecSJeff Kirsher 
2582ec21e2ecSJeff Kirsher 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
2583ec21e2ecSJeff Kirsher 			struct skb_shared_hwtstamps shhwtstamps;
2584b4b67f26SScott Wood 			u64 *ns = (u64 *)(((uintptr_t)skb->data + 0x10) &
2585b4b67f26SScott Wood 					  ~0x7UL);
2586bc4598bcSJan Ceuleers 
2587ec21e2ecSJeff Kirsher 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2588ec21e2ecSJeff Kirsher 			shhwtstamps.hwtstamp = ns_to_ktime(*ns);
25899c4886e5SManfred Rudigier 			skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
2590ec21e2ecSJeff Kirsher 			skb_tstamp_tx(skb, &shhwtstamps);
2591a7312d58SClaudiu Manoil 			gfar_clear_txbd_status(bdp);
2592ec21e2ecSJeff Kirsher 			bdp = next;
2593ec21e2ecSJeff Kirsher 		}
2594ec21e2ecSJeff Kirsher 
2595a7312d58SClaudiu Manoil 		gfar_clear_txbd_status(bdp);
2596ec21e2ecSJeff Kirsher 		bdp = next_txbd(bdp, base, tx_ring_size);
2597ec21e2ecSJeff Kirsher 
2598ec21e2ecSJeff Kirsher 		for (i = 0; i < frags; i++) {
2599a7312d58SClaudiu Manoil 			dma_unmap_page(priv->dev, be32_to_cpu(bdp->bufPtr),
2600a7312d58SClaudiu Manoil 				       be16_to_cpu(bdp->length),
2601a7312d58SClaudiu Manoil 				       DMA_TO_DEVICE);
2602a7312d58SClaudiu Manoil 			gfar_clear_txbd_status(bdp);
2603ec21e2ecSJeff Kirsher 			bdp = next_txbd(bdp, base, tx_ring_size);
2604ec21e2ecSJeff Kirsher 		}
2605ec21e2ecSJeff Kirsher 
260650ad076bSClaudiu Manoil 		bytes_sent += GFAR_CB(skb)->bytes_sent;
2607d8a0f1b0SPaul Gortmaker 
2608ec21e2ecSJeff Kirsher 		dev_kfree_skb_any(skb);
2609ec21e2ecSJeff Kirsher 
2610ec21e2ecSJeff Kirsher 		tx_queue->tx_skbuff[skb_dirtytx] = NULL;
2611ec21e2ecSJeff Kirsher 
2612ec21e2ecSJeff Kirsher 		skb_dirtytx = (skb_dirtytx + 1) &
2613ec21e2ecSJeff Kirsher 			      TX_RING_MOD_MASK(tx_ring_size);
2614ec21e2ecSJeff Kirsher 
2615ec21e2ecSJeff Kirsher 		howmany++;
2616bc602280SClaudiu Manoil 		spin_lock(&tx_queue->txlock);
2617ec21e2ecSJeff Kirsher 		tx_queue->num_txbdfree += nr_txbds;
2618bc602280SClaudiu Manoil 		spin_unlock(&tx_queue->txlock);
2619ec21e2ecSJeff Kirsher 	}
2620ec21e2ecSJeff Kirsher 
2621ec21e2ecSJeff Kirsher 	/* If we freed a buffer, we can restart transmission, if necessary */
26220851133bSClaudiu Manoil 	if (tx_queue->num_txbdfree &&
26230851133bSClaudiu Manoil 	    netif_tx_queue_stopped(txq) &&
26240851133bSClaudiu Manoil 	    !(test_bit(GFAR_DOWN, &priv->state)))
26250851133bSClaudiu Manoil 		netif_wake_subqueue(priv->ndev, tqi);
2626ec21e2ecSJeff Kirsher 
2627ec21e2ecSJeff Kirsher 	/* Update dirty indicators */
2628ec21e2ecSJeff Kirsher 	tx_queue->skb_dirtytx = skb_dirtytx;
2629ec21e2ecSJeff Kirsher 	tx_queue->dirty_tx = bdp;
2630ec21e2ecSJeff Kirsher 
2631d8a0f1b0SPaul Gortmaker 	netdev_tx_completed_queue(txq, howmany, bytes_sent);
2632ec21e2ecSJeff Kirsher }
2633ec21e2ecSJeff Kirsher 
263475354148SClaudiu Manoil static bool gfar_new_page(struct gfar_priv_rx_q *rxq, struct gfar_rx_buff *rxb)
2635ec21e2ecSJeff Kirsher {
263675354148SClaudiu Manoil 	struct page *page;
263776f31e8bSClaudiu Manoil 	dma_addr_t addr;
2638ec21e2ecSJeff Kirsher 
263975354148SClaudiu Manoil 	page = dev_alloc_page();
264075354148SClaudiu Manoil 	if (unlikely(!page))
264175354148SClaudiu Manoil 		return false;
2642ec21e2ecSJeff Kirsher 
264375354148SClaudiu Manoil 	addr = dma_map_page(rxq->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
264475354148SClaudiu Manoil 	if (unlikely(dma_mapping_error(rxq->dev, addr))) {
264575354148SClaudiu Manoil 		__free_page(page);
2646ec21e2ecSJeff Kirsher 
264775354148SClaudiu Manoil 		return false;
26480a4b5a24SKevin Hao 	}
26490a4b5a24SKevin Hao 
265075354148SClaudiu Manoil 	rxb->dma = addr;
265175354148SClaudiu Manoil 	rxb->page = page;
265275354148SClaudiu Manoil 	rxb->page_offset = 0;
265375354148SClaudiu Manoil 
265475354148SClaudiu Manoil 	return true;
2655ec21e2ecSJeff Kirsher }
2656ec21e2ecSJeff Kirsher 
265776f31e8bSClaudiu Manoil static void gfar_rx_alloc_err(struct gfar_priv_rx_q *rx_queue)
265876f31e8bSClaudiu Manoil {
2659f23223f1SClaudiu Manoil 	struct gfar_private *priv = netdev_priv(rx_queue->ndev);
266076f31e8bSClaudiu Manoil 	struct gfar_extra_stats *estats = &priv->extra_stats;
266176f31e8bSClaudiu Manoil 
2662f23223f1SClaudiu Manoil 	netdev_err(rx_queue->ndev, "Can't alloc RX buffers\n");
266376f31e8bSClaudiu Manoil 	atomic64_inc(&estats->rx_alloc_err);
266476f31e8bSClaudiu Manoil }
266576f31e8bSClaudiu Manoil 
266676f31e8bSClaudiu Manoil static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue,
266776f31e8bSClaudiu Manoil 				int alloc_cnt)
266876f31e8bSClaudiu Manoil {
266975354148SClaudiu Manoil 	struct rxbd8 *bdp;
267075354148SClaudiu Manoil 	struct gfar_rx_buff *rxb;
267176f31e8bSClaudiu Manoil 	int i;
267276f31e8bSClaudiu Manoil 
267376f31e8bSClaudiu Manoil 	i = rx_queue->next_to_use;
267476f31e8bSClaudiu Manoil 	bdp = &rx_queue->rx_bd_base[i];
267575354148SClaudiu Manoil 	rxb = &rx_queue->rx_buff[i];
267676f31e8bSClaudiu Manoil 
267776f31e8bSClaudiu Manoil 	while (alloc_cnt--) {
267875354148SClaudiu Manoil 		/* try reuse page */
267975354148SClaudiu Manoil 		if (unlikely(!rxb->page)) {
268075354148SClaudiu Manoil 			if (unlikely(!gfar_new_page(rx_queue, rxb))) {
268176f31e8bSClaudiu Manoil 				gfar_rx_alloc_err(rx_queue);
268276f31e8bSClaudiu Manoil 				break;
268376f31e8bSClaudiu Manoil 			}
268476f31e8bSClaudiu Manoil 		}
268576f31e8bSClaudiu Manoil 
268676f31e8bSClaudiu Manoil 		/* Setup the new RxBD */
268775354148SClaudiu Manoil 		gfar_init_rxbdp(rx_queue, bdp,
268875354148SClaudiu Manoil 				rxb->dma + rxb->page_offset + RXBUF_ALIGNMENT);
268976f31e8bSClaudiu Manoil 
269076f31e8bSClaudiu Manoil 		/* Update to the next pointer */
269175354148SClaudiu Manoil 		bdp++;
269275354148SClaudiu Manoil 		rxb++;
269376f31e8bSClaudiu Manoil 
269475354148SClaudiu Manoil 		if (unlikely(++i == rx_queue->rx_ring_size)) {
269576f31e8bSClaudiu Manoil 			i = 0;
269675354148SClaudiu Manoil 			bdp = rx_queue->rx_bd_base;
269775354148SClaudiu Manoil 			rxb = rx_queue->rx_buff;
269875354148SClaudiu Manoil 		}
269976f31e8bSClaudiu Manoil 	}
270076f31e8bSClaudiu Manoil 
270176f31e8bSClaudiu Manoil 	rx_queue->next_to_use = i;
270275354148SClaudiu Manoil 	rx_queue->next_to_alloc = i;
270376f31e8bSClaudiu Manoil }
270476f31e8bSClaudiu Manoil 
2705f23223f1SClaudiu Manoil static void count_errors(u32 lstatus, struct net_device *ndev)
2706ec21e2ecSJeff Kirsher {
2707f23223f1SClaudiu Manoil 	struct gfar_private *priv = netdev_priv(ndev);
2708f23223f1SClaudiu Manoil 	struct net_device_stats *stats = &ndev->stats;
2709ec21e2ecSJeff Kirsher 	struct gfar_extra_stats *estats = &priv->extra_stats;
2710ec21e2ecSJeff Kirsher 
27110977f817SJan Ceuleers 	/* If the packet was truncated, none of the other errors matter */
2712f966082eSClaudiu Manoil 	if (lstatus & BD_LFLAG(RXBD_TRUNCATED)) {
2713ec21e2ecSJeff Kirsher 		stats->rx_length_errors++;
2714ec21e2ecSJeff Kirsher 
2715212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_trunc);
2716ec21e2ecSJeff Kirsher 
2717ec21e2ecSJeff Kirsher 		return;
2718ec21e2ecSJeff Kirsher 	}
2719ec21e2ecSJeff Kirsher 	/* Count the errors, if there were any */
2720f966082eSClaudiu Manoil 	if (lstatus & BD_LFLAG(RXBD_LARGE | RXBD_SHORT)) {
2721ec21e2ecSJeff Kirsher 		stats->rx_length_errors++;
2722ec21e2ecSJeff Kirsher 
2723f966082eSClaudiu Manoil 		if (lstatus & BD_LFLAG(RXBD_LARGE))
2724212079dfSPaul Gortmaker 			atomic64_inc(&estats->rx_large);
2725ec21e2ecSJeff Kirsher 		else
2726212079dfSPaul Gortmaker 			atomic64_inc(&estats->rx_short);
2727ec21e2ecSJeff Kirsher 	}
2728f966082eSClaudiu Manoil 	if (lstatus & BD_LFLAG(RXBD_NONOCTET)) {
2729ec21e2ecSJeff Kirsher 		stats->rx_frame_errors++;
2730212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_nonoctet);
2731ec21e2ecSJeff Kirsher 	}
2732f966082eSClaudiu Manoil 	if (lstatus & BD_LFLAG(RXBD_CRCERR)) {
2733212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_crcerr);
2734ec21e2ecSJeff Kirsher 		stats->rx_crc_errors++;
2735ec21e2ecSJeff Kirsher 	}
2736f966082eSClaudiu Manoil 	if (lstatus & BD_LFLAG(RXBD_OVERRUN)) {
2737212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_overrun);
2738f966082eSClaudiu Manoil 		stats->rx_over_errors++;
2739ec21e2ecSJeff Kirsher 	}
2740ec21e2ecSJeff Kirsher }
2741ec21e2ecSJeff Kirsher 
2742ec21e2ecSJeff Kirsher irqreturn_t gfar_receive(int irq, void *grp_id)
2743ec21e2ecSJeff Kirsher {
2744aeb12c5eSClaudiu Manoil 	struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2745aeb12c5eSClaudiu Manoil 	unsigned long flags;
2746aeb12c5eSClaudiu Manoil 	u32 imask;
2747aeb12c5eSClaudiu Manoil 
2748aeb12c5eSClaudiu Manoil 	if (likely(napi_schedule_prep(&grp->napi_rx))) {
2749aeb12c5eSClaudiu Manoil 		spin_lock_irqsave(&grp->grplock, flags);
2750aeb12c5eSClaudiu Manoil 		imask = gfar_read(&grp->regs->imask);
2751aeb12c5eSClaudiu Manoil 		imask &= IMASK_RX_DISABLED;
2752aeb12c5eSClaudiu Manoil 		gfar_write(&grp->regs->imask, imask);
2753aeb12c5eSClaudiu Manoil 		spin_unlock_irqrestore(&grp->grplock, flags);
2754aeb12c5eSClaudiu Manoil 		__napi_schedule(&grp->napi_rx);
2755aeb12c5eSClaudiu Manoil 	} else {
2756aeb12c5eSClaudiu Manoil 		/* Clear IEVENT, so interrupts aren't called again
2757aeb12c5eSClaudiu Manoil 		 * because of the packets that have already arrived.
2758aeb12c5eSClaudiu Manoil 		 */
2759aeb12c5eSClaudiu Manoil 		gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
2760aeb12c5eSClaudiu Manoil 	}
2761aeb12c5eSClaudiu Manoil 
2762aeb12c5eSClaudiu Manoil 	return IRQ_HANDLED;
2763aeb12c5eSClaudiu Manoil }
2764aeb12c5eSClaudiu Manoil 
2765aeb12c5eSClaudiu Manoil /* Interrupt Handler for Transmit complete */
2766aeb12c5eSClaudiu Manoil static irqreturn_t gfar_transmit(int irq, void *grp_id)
2767aeb12c5eSClaudiu Manoil {
2768aeb12c5eSClaudiu Manoil 	struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2769aeb12c5eSClaudiu Manoil 	unsigned long flags;
2770aeb12c5eSClaudiu Manoil 	u32 imask;
2771aeb12c5eSClaudiu Manoil 
2772aeb12c5eSClaudiu Manoil 	if (likely(napi_schedule_prep(&grp->napi_tx))) {
2773aeb12c5eSClaudiu Manoil 		spin_lock_irqsave(&grp->grplock, flags);
2774aeb12c5eSClaudiu Manoil 		imask = gfar_read(&grp->regs->imask);
2775aeb12c5eSClaudiu Manoil 		imask &= IMASK_TX_DISABLED;
2776aeb12c5eSClaudiu Manoil 		gfar_write(&grp->regs->imask, imask);
2777aeb12c5eSClaudiu Manoil 		spin_unlock_irqrestore(&grp->grplock, flags);
2778aeb12c5eSClaudiu Manoil 		__napi_schedule(&grp->napi_tx);
2779aeb12c5eSClaudiu Manoil 	} else {
2780aeb12c5eSClaudiu Manoil 		/* Clear IEVENT, so interrupts aren't called again
2781aeb12c5eSClaudiu Manoil 		 * because of the packets that have already arrived.
2782aeb12c5eSClaudiu Manoil 		 */
2783aeb12c5eSClaudiu Manoil 		gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
2784aeb12c5eSClaudiu Manoil 	}
2785aeb12c5eSClaudiu Manoil 
2786ec21e2ecSJeff Kirsher 	return IRQ_HANDLED;
2787ec21e2ecSJeff Kirsher }
2788ec21e2ecSJeff Kirsher 
278975354148SClaudiu Manoil static bool gfar_add_rx_frag(struct gfar_rx_buff *rxb, u32 lstatus,
279075354148SClaudiu Manoil 			     struct sk_buff *skb, bool first)
279175354148SClaudiu Manoil {
279275354148SClaudiu Manoil 	unsigned int size = lstatus & BD_LENGTH_MASK;
279375354148SClaudiu Manoil 	struct page *page = rxb->page;
279475354148SClaudiu Manoil 
279575354148SClaudiu Manoil 	/* Remove the FCS from the packet length */
279675354148SClaudiu Manoil 	if (likely(lstatus & BD_LFLAG(RXBD_LAST)))
279775354148SClaudiu Manoil 		size -= ETH_FCS_LEN;
279875354148SClaudiu Manoil 
279975354148SClaudiu Manoil 	if (likely(first))
280075354148SClaudiu Manoil 		skb_put(skb, size);
280175354148SClaudiu Manoil 	else
280275354148SClaudiu Manoil 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
280375354148SClaudiu Manoil 				rxb->page_offset + RXBUF_ALIGNMENT,
280475354148SClaudiu Manoil 				size, GFAR_RXB_TRUESIZE);
280575354148SClaudiu Manoil 
280675354148SClaudiu Manoil 	/* try reuse page */
280775354148SClaudiu Manoil 	if (unlikely(page_count(page) != 1))
280875354148SClaudiu Manoil 		return false;
280975354148SClaudiu Manoil 
281075354148SClaudiu Manoil 	/* change offset to the other half */
281175354148SClaudiu Manoil 	rxb->page_offset ^= GFAR_RXB_TRUESIZE;
281275354148SClaudiu Manoil 
281375354148SClaudiu Manoil 	atomic_inc(&page->_count);
281475354148SClaudiu Manoil 
281575354148SClaudiu Manoil 	return true;
281675354148SClaudiu Manoil }
281775354148SClaudiu Manoil 
281875354148SClaudiu Manoil static void gfar_reuse_rx_page(struct gfar_priv_rx_q *rxq,
281975354148SClaudiu Manoil 			       struct gfar_rx_buff *old_rxb)
282075354148SClaudiu Manoil {
282175354148SClaudiu Manoil 	struct gfar_rx_buff *new_rxb;
282275354148SClaudiu Manoil 	u16 nta = rxq->next_to_alloc;
282375354148SClaudiu Manoil 
282475354148SClaudiu Manoil 	new_rxb = &rxq->rx_buff[nta];
282575354148SClaudiu Manoil 
282675354148SClaudiu Manoil 	/* find next buf that can reuse a page */
282775354148SClaudiu Manoil 	nta++;
282875354148SClaudiu Manoil 	rxq->next_to_alloc = (nta < rxq->rx_ring_size) ? nta : 0;
282975354148SClaudiu Manoil 
283075354148SClaudiu Manoil 	/* copy page reference */
283175354148SClaudiu Manoil 	*new_rxb = *old_rxb;
283275354148SClaudiu Manoil 
283375354148SClaudiu Manoil 	/* sync for use by the device */
283475354148SClaudiu Manoil 	dma_sync_single_range_for_device(rxq->dev, old_rxb->dma,
283575354148SClaudiu Manoil 					 old_rxb->page_offset,
283675354148SClaudiu Manoil 					 GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE);
283775354148SClaudiu Manoil }
283875354148SClaudiu Manoil 
283975354148SClaudiu Manoil static struct sk_buff *gfar_get_next_rxbuff(struct gfar_priv_rx_q *rx_queue,
284075354148SClaudiu Manoil 					    u32 lstatus, struct sk_buff *skb)
284175354148SClaudiu Manoil {
284275354148SClaudiu Manoil 	struct gfar_rx_buff *rxb = &rx_queue->rx_buff[rx_queue->next_to_clean];
284375354148SClaudiu Manoil 	struct page *page = rxb->page;
284475354148SClaudiu Manoil 	bool first = false;
284575354148SClaudiu Manoil 
284675354148SClaudiu Manoil 	if (likely(!skb)) {
284775354148SClaudiu Manoil 		void *buff_addr = page_address(page) + rxb->page_offset;
284875354148SClaudiu Manoil 
284975354148SClaudiu Manoil 		skb = build_skb(buff_addr, GFAR_SKBFRAG_SIZE);
285075354148SClaudiu Manoil 		if (unlikely(!skb)) {
285175354148SClaudiu Manoil 			gfar_rx_alloc_err(rx_queue);
285275354148SClaudiu Manoil 			return NULL;
285375354148SClaudiu Manoil 		}
285475354148SClaudiu Manoil 		skb_reserve(skb, RXBUF_ALIGNMENT);
285575354148SClaudiu Manoil 		first = true;
285675354148SClaudiu Manoil 	}
285775354148SClaudiu Manoil 
285875354148SClaudiu Manoil 	dma_sync_single_range_for_cpu(rx_queue->dev, rxb->dma, rxb->page_offset,
285975354148SClaudiu Manoil 				      GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE);
286075354148SClaudiu Manoil 
286175354148SClaudiu Manoil 	if (gfar_add_rx_frag(rxb, lstatus, skb, first)) {
286275354148SClaudiu Manoil 		/* reuse the free half of the page */
286375354148SClaudiu Manoil 		gfar_reuse_rx_page(rx_queue, rxb);
286475354148SClaudiu Manoil 	} else {
286575354148SClaudiu Manoil 		/* page cannot be reused, unmap it */
286675354148SClaudiu Manoil 		dma_unmap_page(rx_queue->dev, rxb->dma,
286775354148SClaudiu Manoil 			       PAGE_SIZE, DMA_FROM_DEVICE);
286875354148SClaudiu Manoil 	}
286975354148SClaudiu Manoil 
287075354148SClaudiu Manoil 	/* clear rxb content */
287175354148SClaudiu Manoil 	rxb->page = NULL;
287275354148SClaudiu Manoil 
287375354148SClaudiu Manoil 	return skb;
287475354148SClaudiu Manoil }
287575354148SClaudiu Manoil 
2876ec21e2ecSJeff Kirsher static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2877ec21e2ecSJeff Kirsher {
2878ec21e2ecSJeff Kirsher 	/* If valid headers were found, and valid sums
2879ec21e2ecSJeff Kirsher 	 * were verified, then we tell the kernel that no
28800977f817SJan Ceuleers 	 * checksumming is necessary.  Otherwise, it is [FIXME]
28810977f817SJan Ceuleers 	 */
288226eb9374SClaudiu Manoil 	if ((be16_to_cpu(fcb->flags) & RXFCB_CSUM_MASK) ==
288326eb9374SClaudiu Manoil 	    (RXFCB_CIP | RXFCB_CTU))
2884ec21e2ecSJeff Kirsher 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2885ec21e2ecSJeff Kirsher 	else
2886ec21e2ecSJeff Kirsher 		skb_checksum_none_assert(skb);
2887ec21e2ecSJeff Kirsher }
2888ec21e2ecSJeff Kirsher 
28890977f817SJan Ceuleers /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
2890f23223f1SClaudiu Manoil static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb)
2891ec21e2ecSJeff Kirsher {
2892f23223f1SClaudiu Manoil 	struct gfar_private *priv = netdev_priv(ndev);
2893ec21e2ecSJeff Kirsher 	struct rxfcb *fcb = NULL;
2894ec21e2ecSJeff Kirsher 
2895ec21e2ecSJeff Kirsher 	/* fcb is at the beginning if exists */
2896ec21e2ecSJeff Kirsher 	fcb = (struct rxfcb *)skb->data;
2897ec21e2ecSJeff Kirsher 
28980977f817SJan Ceuleers 	/* Remove the FCB from the skb
28990977f817SJan Ceuleers 	 * Remove the padded bytes, if there are any
29000977f817SJan Ceuleers 	 */
2901f23223f1SClaudiu Manoil 	if (priv->uses_rxfcb)
290276f31e8bSClaudiu Manoil 		skb_pull(skb, GMAC_FCB_LEN);
2903ec21e2ecSJeff Kirsher 
2904ec21e2ecSJeff Kirsher 	/* Get receive timestamp from the skb */
2905ec21e2ecSJeff Kirsher 	if (priv->hwts_rx_en) {
2906ec21e2ecSJeff Kirsher 		struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2907ec21e2ecSJeff Kirsher 		u64 *ns = (u64 *) skb->data;
2908bc4598bcSJan Ceuleers 
2909ec21e2ecSJeff Kirsher 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2910ec21e2ecSJeff Kirsher 		shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2911ec21e2ecSJeff Kirsher 	}
2912ec21e2ecSJeff Kirsher 
2913ec21e2ecSJeff Kirsher 	if (priv->padding)
2914ec21e2ecSJeff Kirsher 		skb_pull(skb, priv->padding);
2915ec21e2ecSJeff Kirsher 
2916f23223f1SClaudiu Manoil 	if (ndev->features & NETIF_F_RXCSUM)
2917ec21e2ecSJeff Kirsher 		gfar_rx_checksum(skb, fcb);
2918ec21e2ecSJeff Kirsher 
2919ec21e2ecSJeff Kirsher 	/* Tell the skb what kind of packet this is */
2920f23223f1SClaudiu Manoil 	skb->protocol = eth_type_trans(skb, ndev);
2921ec21e2ecSJeff Kirsher 
2922f646968fSPatrick McHardy 	/* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
2923823dcd25SDavid S. Miller 	 * Even if vlan rx accel is disabled, on some chips
2924823dcd25SDavid S. Miller 	 * RXFCB_VLN is pseudo randomly set.
2925823dcd25SDavid S. Miller 	 */
2926f23223f1SClaudiu Manoil 	if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX &&
292726eb9374SClaudiu Manoil 	    be16_to_cpu(fcb->flags) & RXFCB_VLN)
292826eb9374SClaudiu Manoil 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
292926eb9374SClaudiu Manoil 				       be16_to_cpu(fcb->vlctl));
2930ec21e2ecSJeff Kirsher }
2931ec21e2ecSJeff Kirsher 
2932ec21e2ecSJeff Kirsher /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
2933ec21e2ecSJeff Kirsher  * until the budget/quota has been reached. Returns the number
2934ec21e2ecSJeff Kirsher  * of frames handled
2935ec21e2ecSJeff Kirsher  */
2936ec21e2ecSJeff Kirsher int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
2937ec21e2ecSJeff Kirsher {
2938f23223f1SClaudiu Manoil 	struct net_device *ndev = rx_queue->ndev;
2939f23223f1SClaudiu Manoil 	struct gfar_private *priv = netdev_priv(ndev);
294075354148SClaudiu Manoil 	struct rxbd8 *bdp;
294175354148SClaudiu Manoil 	int i, howmany = 0;
294275354148SClaudiu Manoil 	struct sk_buff *skb = rx_queue->skb;
294375354148SClaudiu Manoil 	int cleaned_cnt = gfar_rxbd_unused(rx_queue);
294475354148SClaudiu Manoil 	unsigned int total_bytes = 0, total_pkts = 0;
2945ec21e2ecSJeff Kirsher 
2946ec21e2ecSJeff Kirsher 	/* Get the first full descriptor */
294776f31e8bSClaudiu Manoil 	i = rx_queue->next_to_clean;
2948ec21e2ecSJeff Kirsher 
294976f31e8bSClaudiu Manoil 	while (rx_work_limit--) {
2950f966082eSClaudiu Manoil 		u32 lstatus;
2951ec21e2ecSJeff Kirsher 
295276f31e8bSClaudiu Manoil 		if (cleaned_cnt >= GFAR_RX_BUFF_ALLOC) {
295376f31e8bSClaudiu Manoil 			gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
295476f31e8bSClaudiu Manoil 			cleaned_cnt = 0;
295576f31e8bSClaudiu Manoil 		}
2956bc4598bcSJan Ceuleers 
295776f31e8bSClaudiu Manoil 		bdp = &rx_queue->rx_bd_base[i];
2958f966082eSClaudiu Manoil 		lstatus = be32_to_cpu(bdp->lstatus);
2959f966082eSClaudiu Manoil 		if (lstatus & BD_LFLAG(RXBD_EMPTY))
296076f31e8bSClaudiu Manoil 			break;
296176f31e8bSClaudiu Manoil 
296276f31e8bSClaudiu Manoil 		/* order rx buffer descriptor reads */
2963ec21e2ecSJeff Kirsher 		rmb();
2964ec21e2ecSJeff Kirsher 
296576f31e8bSClaudiu Manoil 		/* fetch next to clean buffer from the ring */
296675354148SClaudiu Manoil 		skb = gfar_get_next_rxbuff(rx_queue, lstatus, skb);
296775354148SClaudiu Manoil 		if (unlikely(!skb))
296875354148SClaudiu Manoil 			break;
2969ec21e2ecSJeff Kirsher 
297075354148SClaudiu Manoil 		cleaned_cnt++;
297175354148SClaudiu Manoil 		howmany++;
2972ec21e2ecSJeff Kirsher 
297375354148SClaudiu Manoil 		if (unlikely(++i == rx_queue->rx_ring_size))
297475354148SClaudiu Manoil 			i = 0;
2975ec21e2ecSJeff Kirsher 
297675354148SClaudiu Manoil 		rx_queue->next_to_clean = i;
297775354148SClaudiu Manoil 
297875354148SClaudiu Manoil 		/* fetch next buffer if not the last in frame */
297975354148SClaudiu Manoil 		if (!(lstatus & BD_LFLAG(RXBD_LAST)))
298075354148SClaudiu Manoil 			continue;
298175354148SClaudiu Manoil 
298275354148SClaudiu Manoil 		if (unlikely(lstatus & BD_LFLAG(RXBD_ERR))) {
2983f23223f1SClaudiu Manoil 			count_errors(lstatus, ndev);
2984ec21e2ecSJeff Kirsher 
298576f31e8bSClaudiu Manoil 			/* discard faulty buffer */
2986acb600deSEric Dumazet 			dev_kfree_skb(skb);
298775354148SClaudiu Manoil 			skb = NULL;
298875354148SClaudiu Manoil 			rx_queue->stats.rx_dropped++;
298975354148SClaudiu Manoil 			continue;
299075354148SClaudiu Manoil 		}
299176f31e8bSClaudiu Manoil 
2992ec21e2ecSJeff Kirsher 		/* Increment the number of packets */
299375354148SClaudiu Manoil 		total_pkts++;
299475354148SClaudiu Manoil 		total_bytes += skb->len;
2995ec21e2ecSJeff Kirsher 
2996ec21e2ecSJeff Kirsher 		skb_record_rx_queue(skb, rx_queue->qindex);
299775354148SClaudiu Manoil 
2998f23223f1SClaudiu Manoil 		gfar_process_frame(ndev, skb);
2999f23223f1SClaudiu Manoil 
3000f23223f1SClaudiu Manoil 		/* Send the packet up the stack */
3001f23223f1SClaudiu Manoil 		napi_gro_receive(&rx_queue->grp->napi_rx, skb);
3002ec21e2ecSJeff Kirsher 
300375354148SClaudiu Manoil 		skb = NULL;
3004ec21e2ecSJeff Kirsher 	}
3005ec21e2ecSJeff Kirsher 
300675354148SClaudiu Manoil 	/* Store incomplete frames for completion */
300775354148SClaudiu Manoil 	rx_queue->skb = skb;
3008ec21e2ecSJeff Kirsher 
300975354148SClaudiu Manoil 	rx_queue->stats.rx_packets += total_pkts;
301075354148SClaudiu Manoil 	rx_queue->stats.rx_bytes += total_bytes;
301176f31e8bSClaudiu Manoil 
301276f31e8bSClaudiu Manoil 	if (cleaned_cnt)
301376f31e8bSClaudiu Manoil 		gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
301476f31e8bSClaudiu Manoil 
301576f31e8bSClaudiu Manoil 	/* Update Last Free RxBD pointer for LFC */
301676f31e8bSClaudiu Manoil 	if (unlikely(priv->tx_actual_en)) {
3017b4b67f26SScott Wood 		u32 bdp_dma = gfar_rxbd_dma_lastfree(rx_queue);
3018b4b67f26SScott Wood 
3019b4b67f26SScott Wood 		gfar_write(rx_queue->rfbptr, bdp_dma);
302076f31e8bSClaudiu Manoil 	}
3021ec21e2ecSJeff Kirsher 
3022ec21e2ecSJeff Kirsher 	return howmany;
3023ec21e2ecSJeff Kirsher }
3024ec21e2ecSJeff Kirsher 
3025aeb12c5eSClaudiu Manoil static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
30265eaedf31SClaudiu Manoil {
30275eaedf31SClaudiu Manoil 	struct gfar_priv_grp *gfargrp =
3028aeb12c5eSClaudiu Manoil 		container_of(napi, struct gfar_priv_grp, napi_rx);
30295eaedf31SClaudiu Manoil 	struct gfar __iomem *regs = gfargrp->regs;
303071ff9e3dSClaudiu Manoil 	struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
30315eaedf31SClaudiu Manoil 	int work_done = 0;
30325eaedf31SClaudiu Manoil 
30335eaedf31SClaudiu Manoil 	/* Clear IEVENT, so interrupts aren't called again
30345eaedf31SClaudiu Manoil 	 * because of the packets that have already arrived
30355eaedf31SClaudiu Manoil 	 */
3036aeb12c5eSClaudiu Manoil 	gfar_write(&regs->ievent, IEVENT_RX_MASK);
30375eaedf31SClaudiu Manoil 
30385eaedf31SClaudiu Manoil 	work_done = gfar_clean_rx_ring(rx_queue, budget);
30395eaedf31SClaudiu Manoil 
30405eaedf31SClaudiu Manoil 	if (work_done < budget) {
3041aeb12c5eSClaudiu Manoil 		u32 imask;
30425eaedf31SClaudiu Manoil 		napi_complete(napi);
30435eaedf31SClaudiu Manoil 		/* Clear the halt bit in RSTAT */
30445eaedf31SClaudiu Manoil 		gfar_write(&regs->rstat, gfargrp->rstat);
30455eaedf31SClaudiu Manoil 
3046aeb12c5eSClaudiu Manoil 		spin_lock_irq(&gfargrp->grplock);
3047aeb12c5eSClaudiu Manoil 		imask = gfar_read(&regs->imask);
3048aeb12c5eSClaudiu Manoil 		imask |= IMASK_RX_DEFAULT;
3049aeb12c5eSClaudiu Manoil 		gfar_write(&regs->imask, imask);
3050aeb12c5eSClaudiu Manoil 		spin_unlock_irq(&gfargrp->grplock);
30515eaedf31SClaudiu Manoil 	}
30525eaedf31SClaudiu Manoil 
30535eaedf31SClaudiu Manoil 	return work_done;
30545eaedf31SClaudiu Manoil }
30555eaedf31SClaudiu Manoil 
3056aeb12c5eSClaudiu Manoil static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
3057ec21e2ecSJeff Kirsher {
3058bc4598bcSJan Ceuleers 	struct gfar_priv_grp *gfargrp =
3059aeb12c5eSClaudiu Manoil 		container_of(napi, struct gfar_priv_grp, napi_tx);
3060aeb12c5eSClaudiu Manoil 	struct gfar __iomem *regs = gfargrp->regs;
306171ff9e3dSClaudiu Manoil 	struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
3062aeb12c5eSClaudiu Manoil 	u32 imask;
3063aeb12c5eSClaudiu Manoil 
3064aeb12c5eSClaudiu Manoil 	/* Clear IEVENT, so interrupts aren't called again
3065aeb12c5eSClaudiu Manoil 	 * because of the packets that have already arrived
3066aeb12c5eSClaudiu Manoil 	 */
3067aeb12c5eSClaudiu Manoil 	gfar_write(&regs->ievent, IEVENT_TX_MASK);
3068aeb12c5eSClaudiu Manoil 
3069aeb12c5eSClaudiu Manoil 	/* run Tx cleanup to completion */
3070aeb12c5eSClaudiu Manoil 	if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
3071aeb12c5eSClaudiu Manoil 		gfar_clean_tx_ring(tx_queue);
3072aeb12c5eSClaudiu Manoil 
3073aeb12c5eSClaudiu Manoil 	napi_complete(napi);
3074aeb12c5eSClaudiu Manoil 
3075aeb12c5eSClaudiu Manoil 	spin_lock_irq(&gfargrp->grplock);
3076aeb12c5eSClaudiu Manoil 	imask = gfar_read(&regs->imask);
3077aeb12c5eSClaudiu Manoil 	imask |= IMASK_TX_DEFAULT;
3078aeb12c5eSClaudiu Manoil 	gfar_write(&regs->imask, imask);
3079aeb12c5eSClaudiu Manoil 	spin_unlock_irq(&gfargrp->grplock);
3080aeb12c5eSClaudiu Manoil 
3081aeb12c5eSClaudiu Manoil 	return 0;
3082aeb12c5eSClaudiu Manoil }
3083aeb12c5eSClaudiu Manoil 
3084aeb12c5eSClaudiu Manoil static int gfar_poll_rx(struct napi_struct *napi, int budget)
3085aeb12c5eSClaudiu Manoil {
3086aeb12c5eSClaudiu Manoil 	struct gfar_priv_grp *gfargrp =
3087aeb12c5eSClaudiu Manoil 		container_of(napi, struct gfar_priv_grp, napi_rx);
3088ec21e2ecSJeff Kirsher 	struct gfar_private *priv = gfargrp->priv;
3089ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = gfargrp->regs;
3090ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
3091c233cf40SClaudiu Manoil 	int work_done = 0, work_done_per_q = 0;
309239c0a0d5SClaudiu Manoil 	int i, budget_per_q = 0;
30936be5ed3fSClaudiu Manoil 	unsigned long rstat_rxf;
30946be5ed3fSClaudiu Manoil 	int num_act_queues;
3095ec21e2ecSJeff Kirsher 
3096ec21e2ecSJeff Kirsher 	/* Clear IEVENT, so interrupts aren't called again
30970977f817SJan Ceuleers 	 * because of the packets that have already arrived
30980977f817SJan Ceuleers 	 */
3099aeb12c5eSClaudiu Manoil 	gfar_write(&regs->ievent, IEVENT_RX_MASK);
3100ec21e2ecSJeff Kirsher 
31016be5ed3fSClaudiu Manoil 	rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
31026be5ed3fSClaudiu Manoil 
31036be5ed3fSClaudiu Manoil 	num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
31046be5ed3fSClaudiu Manoil 	if (num_act_queues)
31056be5ed3fSClaudiu Manoil 		budget_per_q = budget/num_act_queues;
31066be5ed3fSClaudiu Manoil 
3107ec21e2ecSJeff Kirsher 	for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
31086be5ed3fSClaudiu Manoil 		/* skip queue if not active */
31096be5ed3fSClaudiu Manoil 		if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
3110ec21e2ecSJeff Kirsher 			continue;
3111ec21e2ecSJeff Kirsher 
3112c233cf40SClaudiu Manoil 		rx_queue = priv->rx_queue[i];
3113c233cf40SClaudiu Manoil 		work_done_per_q =
3114c233cf40SClaudiu Manoil 			gfar_clean_rx_ring(rx_queue, budget_per_q);
3115c233cf40SClaudiu Manoil 		work_done += work_done_per_q;
3116c233cf40SClaudiu Manoil 
3117c233cf40SClaudiu Manoil 		/* finished processing this queue */
3118c233cf40SClaudiu Manoil 		if (work_done_per_q < budget_per_q) {
31196be5ed3fSClaudiu Manoil 			/* clear active queue hw indication */
31206be5ed3fSClaudiu Manoil 			gfar_write(&regs->rstat,
31216be5ed3fSClaudiu Manoil 				   RSTAT_CLEAR_RXF0 >> i);
31226be5ed3fSClaudiu Manoil 			num_act_queues--;
31236be5ed3fSClaudiu Manoil 
31246be5ed3fSClaudiu Manoil 			if (!num_act_queues)
3125c233cf40SClaudiu Manoil 				break;
3126ec21e2ecSJeff Kirsher 		}
3127ec21e2ecSJeff Kirsher 	}
3128ec21e2ecSJeff Kirsher 
3129aeb12c5eSClaudiu Manoil 	if (!num_act_queues) {
3130aeb12c5eSClaudiu Manoil 		u32 imask;
3131ec21e2ecSJeff Kirsher 		napi_complete(napi);
3132ec21e2ecSJeff Kirsher 
3133ec21e2ecSJeff Kirsher 		/* Clear the halt bit in RSTAT */
3134ec21e2ecSJeff Kirsher 		gfar_write(&regs->rstat, gfargrp->rstat);
3135ec21e2ecSJeff Kirsher 
3136aeb12c5eSClaudiu Manoil 		spin_lock_irq(&gfargrp->grplock);
3137aeb12c5eSClaudiu Manoil 		imask = gfar_read(&regs->imask);
3138aeb12c5eSClaudiu Manoil 		imask |= IMASK_RX_DEFAULT;
3139aeb12c5eSClaudiu Manoil 		gfar_write(&regs->imask, imask);
3140aeb12c5eSClaudiu Manoil 		spin_unlock_irq(&gfargrp->grplock);
3141ec21e2ecSJeff Kirsher 	}
3142ec21e2ecSJeff Kirsher 
3143c233cf40SClaudiu Manoil 	return work_done;
3144ec21e2ecSJeff Kirsher }
3145ec21e2ecSJeff Kirsher 
3146aeb12c5eSClaudiu Manoil static int gfar_poll_tx(struct napi_struct *napi, int budget)
3147aeb12c5eSClaudiu Manoil {
3148aeb12c5eSClaudiu Manoil 	struct gfar_priv_grp *gfargrp =
3149aeb12c5eSClaudiu Manoil 		container_of(napi, struct gfar_priv_grp, napi_tx);
3150aeb12c5eSClaudiu Manoil 	struct gfar_private *priv = gfargrp->priv;
3151aeb12c5eSClaudiu Manoil 	struct gfar __iomem *regs = gfargrp->regs;
3152aeb12c5eSClaudiu Manoil 	struct gfar_priv_tx_q *tx_queue = NULL;
3153aeb12c5eSClaudiu Manoil 	int has_tx_work = 0;
3154aeb12c5eSClaudiu Manoil 	int i;
3155aeb12c5eSClaudiu Manoil 
3156aeb12c5eSClaudiu Manoil 	/* Clear IEVENT, so interrupts aren't called again
3157aeb12c5eSClaudiu Manoil 	 * because of the packets that have already arrived
3158aeb12c5eSClaudiu Manoil 	 */
3159aeb12c5eSClaudiu Manoil 	gfar_write(&regs->ievent, IEVENT_TX_MASK);
3160aeb12c5eSClaudiu Manoil 
3161aeb12c5eSClaudiu Manoil 	for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
3162aeb12c5eSClaudiu Manoil 		tx_queue = priv->tx_queue[i];
3163aeb12c5eSClaudiu Manoil 		/* run Tx cleanup to completion */
3164aeb12c5eSClaudiu Manoil 		if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
3165aeb12c5eSClaudiu Manoil 			gfar_clean_tx_ring(tx_queue);
3166aeb12c5eSClaudiu Manoil 			has_tx_work = 1;
3167aeb12c5eSClaudiu Manoil 		}
3168aeb12c5eSClaudiu Manoil 	}
3169aeb12c5eSClaudiu Manoil 
3170aeb12c5eSClaudiu Manoil 	if (!has_tx_work) {
3171aeb12c5eSClaudiu Manoil 		u32 imask;
3172aeb12c5eSClaudiu Manoil 		napi_complete(napi);
3173aeb12c5eSClaudiu Manoil 
3174aeb12c5eSClaudiu Manoil 		spin_lock_irq(&gfargrp->grplock);
3175aeb12c5eSClaudiu Manoil 		imask = gfar_read(&regs->imask);
3176aeb12c5eSClaudiu Manoil 		imask |= IMASK_TX_DEFAULT;
3177aeb12c5eSClaudiu Manoil 		gfar_write(&regs->imask, imask);
3178aeb12c5eSClaudiu Manoil 		spin_unlock_irq(&gfargrp->grplock);
3179aeb12c5eSClaudiu Manoil 	}
3180aeb12c5eSClaudiu Manoil 
3181aeb12c5eSClaudiu Manoil 	return 0;
3182aeb12c5eSClaudiu Manoil }
3183aeb12c5eSClaudiu Manoil 
3184aeb12c5eSClaudiu Manoil 
3185ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
31860977f817SJan Ceuleers /* Polling 'interrupt' - used by things like netconsole to send skbs
3187ec21e2ecSJeff Kirsher  * without having to re-enable interrupts. It's not called while
3188ec21e2ecSJeff Kirsher  * the interrupt routine is executing.
3189ec21e2ecSJeff Kirsher  */
3190ec21e2ecSJeff Kirsher static void gfar_netpoll(struct net_device *dev)
3191ec21e2ecSJeff Kirsher {
3192ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
31933a2e16c8SJan Ceuleers 	int i;
3194ec21e2ecSJeff Kirsher 
3195ec21e2ecSJeff Kirsher 	/* If the device has multiple interrupts, run tx/rx */
3196ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
3197ec21e2ecSJeff Kirsher 		for (i = 0; i < priv->num_grps; i++) {
319862ed839dSPaul Gortmaker 			struct gfar_priv_grp *grp = &priv->gfargrp[i];
319962ed839dSPaul Gortmaker 
320062ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, TX)->irq);
320162ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, RX)->irq);
320262ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, ER)->irq);
320362ed839dSPaul Gortmaker 			gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
320462ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, ER)->irq);
320562ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, RX)->irq);
320662ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, TX)->irq);
3207ec21e2ecSJeff Kirsher 		}
3208ec21e2ecSJeff Kirsher 	} else {
3209ec21e2ecSJeff Kirsher 		for (i = 0; i < priv->num_grps; i++) {
321062ed839dSPaul Gortmaker 			struct gfar_priv_grp *grp = &priv->gfargrp[i];
321162ed839dSPaul Gortmaker 
321262ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, TX)->irq);
321362ed839dSPaul Gortmaker 			gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
321462ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, TX)->irq);
3215ec21e2ecSJeff Kirsher 		}
3216ec21e2ecSJeff Kirsher 	}
3217ec21e2ecSJeff Kirsher }
3218ec21e2ecSJeff Kirsher #endif
3219ec21e2ecSJeff Kirsher 
3220ec21e2ecSJeff Kirsher /* The interrupt handler for devices with one interrupt */
3221ec21e2ecSJeff Kirsher static irqreturn_t gfar_interrupt(int irq, void *grp_id)
3222ec21e2ecSJeff Kirsher {
3223ec21e2ecSJeff Kirsher 	struct gfar_priv_grp *gfargrp = grp_id;
3224ec21e2ecSJeff Kirsher 
3225ec21e2ecSJeff Kirsher 	/* Save ievent for future reference */
3226ec21e2ecSJeff Kirsher 	u32 events = gfar_read(&gfargrp->regs->ievent);
3227ec21e2ecSJeff Kirsher 
3228ec21e2ecSJeff Kirsher 	/* Check for reception */
3229ec21e2ecSJeff Kirsher 	if (events & IEVENT_RX_MASK)
3230ec21e2ecSJeff Kirsher 		gfar_receive(irq, grp_id);
3231ec21e2ecSJeff Kirsher 
3232ec21e2ecSJeff Kirsher 	/* Check for transmit completion */
3233ec21e2ecSJeff Kirsher 	if (events & IEVENT_TX_MASK)
3234ec21e2ecSJeff Kirsher 		gfar_transmit(irq, grp_id);
3235ec21e2ecSJeff Kirsher 
3236ec21e2ecSJeff Kirsher 	/* Check for errors */
3237ec21e2ecSJeff Kirsher 	if (events & IEVENT_ERR_MASK)
3238ec21e2ecSJeff Kirsher 		gfar_error(irq, grp_id);
3239ec21e2ecSJeff Kirsher 
3240ec21e2ecSJeff Kirsher 	return IRQ_HANDLED;
3241ec21e2ecSJeff Kirsher }
3242ec21e2ecSJeff Kirsher 
3243ec21e2ecSJeff Kirsher /* Called every time the controller might need to be made
3244ec21e2ecSJeff Kirsher  * aware of new link state.  The PHY code conveys this
3245ec21e2ecSJeff Kirsher  * information through variables in the phydev structure, and this
3246ec21e2ecSJeff Kirsher  * function converts those variables into the appropriate
3247ec21e2ecSJeff Kirsher  * register values, and can bring down the device if needed.
3248ec21e2ecSJeff Kirsher  */
3249ec21e2ecSJeff Kirsher static void adjust_link(struct net_device *dev)
3250ec21e2ecSJeff Kirsher {
3251ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
3252ec21e2ecSJeff Kirsher 	struct phy_device *phydev = priv->phydev;
3253ec21e2ecSJeff Kirsher 
32546ce29b0eSClaudiu Manoil 	if (unlikely(phydev->link != priv->oldlink ||
32550ae93b2cSGuenter Roeck 		     (phydev->link && (phydev->duplex != priv->oldduplex ||
32560ae93b2cSGuenter Roeck 				       phydev->speed != priv->oldspeed))))
32576ce29b0eSClaudiu Manoil 		gfar_update_link_state(priv);
3258ec21e2ecSJeff Kirsher }
3259ec21e2ecSJeff Kirsher 
3260ec21e2ecSJeff Kirsher /* Update the hash table based on the current list of multicast
3261ec21e2ecSJeff Kirsher  * addresses we subscribe to.  Also, change the promiscuity of
3262ec21e2ecSJeff Kirsher  * the device based on the flags (this function is called
32630977f817SJan Ceuleers  * whenever dev->flags is changed
32640977f817SJan Ceuleers  */
3265ec21e2ecSJeff Kirsher static void gfar_set_multi(struct net_device *dev)
3266ec21e2ecSJeff Kirsher {
3267ec21e2ecSJeff Kirsher 	struct netdev_hw_addr *ha;
3268ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
3269ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3270ec21e2ecSJeff Kirsher 	u32 tempval;
3271ec21e2ecSJeff Kirsher 
3272ec21e2ecSJeff Kirsher 	if (dev->flags & IFF_PROMISC) {
3273ec21e2ecSJeff Kirsher 		/* Set RCTRL to PROM */
3274ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->rctrl);
3275ec21e2ecSJeff Kirsher 		tempval |= RCTRL_PROM;
3276ec21e2ecSJeff Kirsher 		gfar_write(&regs->rctrl, tempval);
3277ec21e2ecSJeff Kirsher 	} else {
3278ec21e2ecSJeff Kirsher 		/* Set RCTRL to not PROM */
3279ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->rctrl);
3280ec21e2ecSJeff Kirsher 		tempval &= ~(RCTRL_PROM);
3281ec21e2ecSJeff Kirsher 		gfar_write(&regs->rctrl, tempval);
3282ec21e2ecSJeff Kirsher 	}
3283ec21e2ecSJeff Kirsher 
3284ec21e2ecSJeff Kirsher 	if (dev->flags & IFF_ALLMULTI) {
3285ec21e2ecSJeff Kirsher 		/* Set the hash to rx all multicast frames */
3286ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr0, 0xffffffff);
3287ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr1, 0xffffffff);
3288ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr2, 0xffffffff);
3289ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr3, 0xffffffff);
3290ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr4, 0xffffffff);
3291ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr5, 0xffffffff);
3292ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr6, 0xffffffff);
3293ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr7, 0xffffffff);
3294ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr0, 0xffffffff);
3295ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr1, 0xffffffff);
3296ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr2, 0xffffffff);
3297ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr3, 0xffffffff);
3298ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr4, 0xffffffff);
3299ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr5, 0xffffffff);
3300ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr6, 0xffffffff);
3301ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr7, 0xffffffff);
3302ec21e2ecSJeff Kirsher 	} else {
3303ec21e2ecSJeff Kirsher 		int em_num;
3304ec21e2ecSJeff Kirsher 		int idx;
3305ec21e2ecSJeff Kirsher 
3306ec21e2ecSJeff Kirsher 		/* zero out the hash */
3307ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr0, 0x0);
3308ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr1, 0x0);
3309ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr2, 0x0);
3310ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr3, 0x0);
3311ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr4, 0x0);
3312ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr5, 0x0);
3313ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr6, 0x0);
3314ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr7, 0x0);
3315ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr0, 0x0);
3316ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr1, 0x0);
3317ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr2, 0x0);
3318ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr3, 0x0);
3319ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr4, 0x0);
3320ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr5, 0x0);
3321ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr6, 0x0);
3322ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr7, 0x0);
3323ec21e2ecSJeff Kirsher 
3324ec21e2ecSJeff Kirsher 		/* If we have extended hash tables, we need to
3325ec21e2ecSJeff Kirsher 		 * clear the exact match registers to prepare for
33260977f817SJan Ceuleers 		 * setting them
33270977f817SJan Ceuleers 		 */
3328ec21e2ecSJeff Kirsher 		if (priv->extended_hash) {
3329ec21e2ecSJeff Kirsher 			em_num = GFAR_EM_NUM + 1;
3330ec21e2ecSJeff Kirsher 			gfar_clear_exact_match(dev);
3331ec21e2ecSJeff Kirsher 			idx = 1;
3332ec21e2ecSJeff Kirsher 		} else {
3333ec21e2ecSJeff Kirsher 			idx = 0;
3334ec21e2ecSJeff Kirsher 			em_num = 0;
3335ec21e2ecSJeff Kirsher 		}
3336ec21e2ecSJeff Kirsher 
3337ec21e2ecSJeff Kirsher 		if (netdev_mc_empty(dev))
3338ec21e2ecSJeff Kirsher 			return;
3339ec21e2ecSJeff Kirsher 
3340ec21e2ecSJeff Kirsher 		/* Parse the list, and set the appropriate bits */
3341ec21e2ecSJeff Kirsher 		netdev_for_each_mc_addr(ha, dev) {
3342ec21e2ecSJeff Kirsher 			if (idx < em_num) {
3343ec21e2ecSJeff Kirsher 				gfar_set_mac_for_addr(dev, idx, ha->addr);
3344ec21e2ecSJeff Kirsher 				idx++;
3345ec21e2ecSJeff Kirsher 			} else
3346ec21e2ecSJeff Kirsher 				gfar_set_hash_for_addr(dev, ha->addr);
3347ec21e2ecSJeff Kirsher 		}
3348ec21e2ecSJeff Kirsher 	}
3349ec21e2ecSJeff Kirsher }
3350ec21e2ecSJeff Kirsher 
3351ec21e2ecSJeff Kirsher 
3352ec21e2ecSJeff Kirsher /* Clears each of the exact match registers to zero, so they
33530977f817SJan Ceuleers  * don't interfere with normal reception
33540977f817SJan Ceuleers  */
3355ec21e2ecSJeff Kirsher static void gfar_clear_exact_match(struct net_device *dev)
3356ec21e2ecSJeff Kirsher {
3357ec21e2ecSJeff Kirsher 	int idx;
33586a3c910cSJoe Perches 	static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
3359ec21e2ecSJeff Kirsher 
3360ec21e2ecSJeff Kirsher 	for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
3361ec21e2ecSJeff Kirsher 		gfar_set_mac_for_addr(dev, idx, zero_arr);
3362ec21e2ecSJeff Kirsher }
3363ec21e2ecSJeff Kirsher 
3364ec21e2ecSJeff Kirsher /* Set the appropriate hash bit for the given addr */
3365ec21e2ecSJeff Kirsher /* The algorithm works like so:
3366ec21e2ecSJeff Kirsher  * 1) Take the Destination Address (ie the multicast address), and
3367ec21e2ecSJeff Kirsher  * do a CRC on it (little endian), and reverse the bits of the
3368ec21e2ecSJeff Kirsher  * result.
3369ec21e2ecSJeff Kirsher  * 2) Use the 8 most significant bits as a hash into a 256-entry
3370ec21e2ecSJeff Kirsher  * table.  The table is controlled through 8 32-bit registers:
3371ec21e2ecSJeff Kirsher  * gaddr0-7.  gaddr0's MSB is entry 0, and gaddr7's LSB is
3372ec21e2ecSJeff Kirsher  * gaddr7.  This means that the 3 most significant bits in the
3373ec21e2ecSJeff Kirsher  * hash index which gaddr register to use, and the 5 other bits
3374ec21e2ecSJeff Kirsher  * indicate which bit (assuming an IBM numbering scheme, which
3375ec21e2ecSJeff Kirsher  * for PowerPC (tm) is usually the case) in the register holds
33760977f817SJan Ceuleers  * the entry.
33770977f817SJan Ceuleers  */
3378ec21e2ecSJeff Kirsher static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3379ec21e2ecSJeff Kirsher {
3380ec21e2ecSJeff Kirsher 	u32 tempval;
3381ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
33826a3c910cSJoe Perches 	u32 result = ether_crc(ETH_ALEN, addr);
3383ec21e2ecSJeff Kirsher 	int width = priv->hash_width;
3384ec21e2ecSJeff Kirsher 	u8 whichbit = (result >> (32 - width)) & 0x1f;
3385ec21e2ecSJeff Kirsher 	u8 whichreg = result >> (32 - width + 5);
3386ec21e2ecSJeff Kirsher 	u32 value = (1 << (31-whichbit));
3387ec21e2ecSJeff Kirsher 
3388ec21e2ecSJeff Kirsher 	tempval = gfar_read(priv->hash_regs[whichreg]);
3389ec21e2ecSJeff Kirsher 	tempval |= value;
3390ec21e2ecSJeff Kirsher 	gfar_write(priv->hash_regs[whichreg], tempval);
3391ec21e2ecSJeff Kirsher }
3392ec21e2ecSJeff Kirsher 
3393ec21e2ecSJeff Kirsher 
3394ec21e2ecSJeff Kirsher /* There are multiple MAC Address register pairs on some controllers
3395ec21e2ecSJeff Kirsher  * This function sets the numth pair to a given address
3396ec21e2ecSJeff Kirsher  */
3397ec21e2ecSJeff Kirsher static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3398ec21e2ecSJeff Kirsher 				  const u8 *addr)
3399ec21e2ecSJeff Kirsher {
3400ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
3401ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3402ec21e2ecSJeff Kirsher 	u32 tempval;
3403ec21e2ecSJeff Kirsher 	u32 __iomem *macptr = &regs->macstnaddr1;
3404ec21e2ecSJeff Kirsher 
3405ec21e2ecSJeff Kirsher 	macptr += num*2;
3406ec21e2ecSJeff Kirsher 
340783bfc3c4SClaudiu Manoil 	/* For a station address of 0x12345678ABCD in transmission
340883bfc3c4SClaudiu Manoil 	 * order (BE), MACnADDR1 is set to 0xCDAB7856 and
340983bfc3c4SClaudiu Manoil 	 * MACnADDR2 is set to 0x34120000.
34100977f817SJan Ceuleers 	 */
341183bfc3c4SClaudiu Manoil 	tempval = (addr[5] << 24) | (addr[4] << 16) |
341283bfc3c4SClaudiu Manoil 		  (addr[3] << 8)  |  addr[2];
3413ec21e2ecSJeff Kirsher 
341483bfc3c4SClaudiu Manoil 	gfar_write(macptr, tempval);
3415ec21e2ecSJeff Kirsher 
341683bfc3c4SClaudiu Manoil 	tempval = (addr[1] << 24) | (addr[0] << 16);
3417ec21e2ecSJeff Kirsher 
3418ec21e2ecSJeff Kirsher 	gfar_write(macptr+1, tempval);
3419ec21e2ecSJeff Kirsher }
3420ec21e2ecSJeff Kirsher 
3421ec21e2ecSJeff Kirsher /* GFAR error interrupt handler */
3422ec21e2ecSJeff Kirsher static irqreturn_t gfar_error(int irq, void *grp_id)
3423ec21e2ecSJeff Kirsher {
3424ec21e2ecSJeff Kirsher 	struct gfar_priv_grp *gfargrp = grp_id;
3425ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = gfargrp->regs;
3426ec21e2ecSJeff Kirsher 	struct gfar_private *priv= gfargrp->priv;
3427ec21e2ecSJeff Kirsher 	struct net_device *dev = priv->ndev;
3428ec21e2ecSJeff Kirsher 
3429ec21e2ecSJeff Kirsher 	/* Save ievent for future reference */
3430ec21e2ecSJeff Kirsher 	u32 events = gfar_read(&regs->ievent);
3431ec21e2ecSJeff Kirsher 
3432ec21e2ecSJeff Kirsher 	/* Clear IEVENT */
3433ec21e2ecSJeff Kirsher 	gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
3434ec21e2ecSJeff Kirsher 
3435ec21e2ecSJeff Kirsher 	/* Magic Packet is not an error. */
3436ec21e2ecSJeff Kirsher 	if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
3437ec21e2ecSJeff Kirsher 	    (events & IEVENT_MAG))
3438ec21e2ecSJeff Kirsher 		events &= ~IEVENT_MAG;
3439ec21e2ecSJeff Kirsher 
3440ec21e2ecSJeff Kirsher 	/* Hmm... */
3441ec21e2ecSJeff Kirsher 	if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
3442bc4598bcSJan Ceuleers 		netdev_dbg(dev,
3443bc4598bcSJan Ceuleers 			   "error interrupt (ievent=0x%08x imask=0x%08x)\n",
3444ec21e2ecSJeff Kirsher 			   events, gfar_read(&regs->imask));
3445ec21e2ecSJeff Kirsher 
3446ec21e2ecSJeff Kirsher 	/* Update the error counters */
3447ec21e2ecSJeff Kirsher 	if (events & IEVENT_TXE) {
3448ec21e2ecSJeff Kirsher 		dev->stats.tx_errors++;
3449ec21e2ecSJeff Kirsher 
3450ec21e2ecSJeff Kirsher 		if (events & IEVENT_LC)
3451ec21e2ecSJeff Kirsher 			dev->stats.tx_window_errors++;
3452ec21e2ecSJeff Kirsher 		if (events & IEVENT_CRL)
3453ec21e2ecSJeff Kirsher 			dev->stats.tx_aborted_errors++;
3454ec21e2ecSJeff Kirsher 		if (events & IEVENT_XFUN) {
3455ec21e2ecSJeff Kirsher 			netif_dbg(priv, tx_err, dev,
3456ec21e2ecSJeff Kirsher 				  "TX FIFO underrun, packet dropped\n");
3457ec21e2ecSJeff Kirsher 			dev->stats.tx_dropped++;
3458212079dfSPaul Gortmaker 			atomic64_inc(&priv->extra_stats.tx_underrun);
3459ec21e2ecSJeff Kirsher 
3460bc602280SClaudiu Manoil 			schedule_work(&priv->reset_task);
3461ec21e2ecSJeff Kirsher 		}
3462ec21e2ecSJeff Kirsher 		netif_dbg(priv, tx_err, dev, "Transmit Error\n");
3463ec21e2ecSJeff Kirsher 	}
3464ec21e2ecSJeff Kirsher 	if (events & IEVENT_BSY) {
3465*1de65a5eSClaudiu Manoil 		dev->stats.rx_over_errors++;
3466212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.rx_bsy);
3467ec21e2ecSJeff Kirsher 
3468ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3469ec21e2ecSJeff Kirsher 			  gfar_read(&regs->rstat));
3470ec21e2ecSJeff Kirsher 	}
3471ec21e2ecSJeff Kirsher 	if (events & IEVENT_BABR) {
3472ec21e2ecSJeff Kirsher 		dev->stats.rx_errors++;
3473212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.rx_babr);
3474ec21e2ecSJeff Kirsher 
3475ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_err, dev, "babbling RX error\n");
3476ec21e2ecSJeff Kirsher 	}
3477ec21e2ecSJeff Kirsher 	if (events & IEVENT_EBERR) {
3478212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.eberr);
3479ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_err, dev, "bus error\n");
3480ec21e2ecSJeff Kirsher 	}
3481ec21e2ecSJeff Kirsher 	if (events & IEVENT_RXC)
3482ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_status, dev, "control frame\n");
3483ec21e2ecSJeff Kirsher 
3484ec21e2ecSJeff Kirsher 	if (events & IEVENT_BABT) {
3485212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.tx_babt);
3486ec21e2ecSJeff Kirsher 		netif_dbg(priv, tx_err, dev, "babbling TX error\n");
3487ec21e2ecSJeff Kirsher 	}
3488ec21e2ecSJeff Kirsher 	return IRQ_HANDLED;
3489ec21e2ecSJeff Kirsher }
3490ec21e2ecSJeff Kirsher 
34916ce29b0eSClaudiu Manoil static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
34926ce29b0eSClaudiu Manoil {
34936ce29b0eSClaudiu Manoil 	struct phy_device *phydev = priv->phydev;
34946ce29b0eSClaudiu Manoil 	u32 val = 0;
34956ce29b0eSClaudiu Manoil 
34966ce29b0eSClaudiu Manoil 	if (!phydev->duplex)
34976ce29b0eSClaudiu Manoil 		return val;
34986ce29b0eSClaudiu Manoil 
34996ce29b0eSClaudiu Manoil 	if (!priv->pause_aneg_en) {
35006ce29b0eSClaudiu Manoil 		if (priv->tx_pause_en)
35016ce29b0eSClaudiu Manoil 			val |= MACCFG1_TX_FLOW;
35026ce29b0eSClaudiu Manoil 		if (priv->rx_pause_en)
35036ce29b0eSClaudiu Manoil 			val |= MACCFG1_RX_FLOW;
35046ce29b0eSClaudiu Manoil 	} else {
35056ce29b0eSClaudiu Manoil 		u16 lcl_adv, rmt_adv;
35066ce29b0eSClaudiu Manoil 		u8 flowctrl;
35076ce29b0eSClaudiu Manoil 		/* get link partner capabilities */
35086ce29b0eSClaudiu Manoil 		rmt_adv = 0;
35096ce29b0eSClaudiu Manoil 		if (phydev->pause)
35106ce29b0eSClaudiu Manoil 			rmt_adv = LPA_PAUSE_CAP;
35116ce29b0eSClaudiu Manoil 		if (phydev->asym_pause)
35126ce29b0eSClaudiu Manoil 			rmt_adv |= LPA_PAUSE_ASYM;
35136ce29b0eSClaudiu Manoil 
351443ef8d29SPavaluca Matei-B46610 		lcl_adv = 0;
351543ef8d29SPavaluca Matei-B46610 		if (phydev->advertising & ADVERTISED_Pause)
351643ef8d29SPavaluca Matei-B46610 			lcl_adv |= ADVERTISE_PAUSE_CAP;
351743ef8d29SPavaluca Matei-B46610 		if (phydev->advertising & ADVERTISED_Asym_Pause)
351843ef8d29SPavaluca Matei-B46610 			lcl_adv |= ADVERTISE_PAUSE_ASYM;
35196ce29b0eSClaudiu Manoil 
35206ce29b0eSClaudiu Manoil 		flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
35216ce29b0eSClaudiu Manoil 		if (flowctrl & FLOW_CTRL_TX)
35226ce29b0eSClaudiu Manoil 			val |= MACCFG1_TX_FLOW;
35236ce29b0eSClaudiu Manoil 		if (flowctrl & FLOW_CTRL_RX)
35246ce29b0eSClaudiu Manoil 			val |= MACCFG1_RX_FLOW;
35256ce29b0eSClaudiu Manoil 	}
35266ce29b0eSClaudiu Manoil 
35276ce29b0eSClaudiu Manoil 	return val;
35286ce29b0eSClaudiu Manoil }
35296ce29b0eSClaudiu Manoil 
35306ce29b0eSClaudiu Manoil static noinline void gfar_update_link_state(struct gfar_private *priv)
35316ce29b0eSClaudiu Manoil {
35326ce29b0eSClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
35336ce29b0eSClaudiu Manoil 	struct phy_device *phydev = priv->phydev;
353445b679c9SMatei Pavaluca 	struct gfar_priv_rx_q *rx_queue = NULL;
353545b679c9SMatei Pavaluca 	int i;
35366ce29b0eSClaudiu Manoil 
35376ce29b0eSClaudiu Manoil 	if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
35386ce29b0eSClaudiu Manoil 		return;
35396ce29b0eSClaudiu Manoil 
35406ce29b0eSClaudiu Manoil 	if (phydev->link) {
35416ce29b0eSClaudiu Manoil 		u32 tempval1 = gfar_read(&regs->maccfg1);
35426ce29b0eSClaudiu Manoil 		u32 tempval = gfar_read(&regs->maccfg2);
35436ce29b0eSClaudiu Manoil 		u32 ecntrl = gfar_read(&regs->ecntrl);
354445b679c9SMatei Pavaluca 		u32 tx_flow_oldval = (tempval & MACCFG1_TX_FLOW);
35456ce29b0eSClaudiu Manoil 
35466ce29b0eSClaudiu Manoil 		if (phydev->duplex != priv->oldduplex) {
35476ce29b0eSClaudiu Manoil 			if (!(phydev->duplex))
35486ce29b0eSClaudiu Manoil 				tempval &= ~(MACCFG2_FULL_DUPLEX);
35496ce29b0eSClaudiu Manoil 			else
35506ce29b0eSClaudiu Manoil 				tempval |= MACCFG2_FULL_DUPLEX;
35516ce29b0eSClaudiu Manoil 
35526ce29b0eSClaudiu Manoil 			priv->oldduplex = phydev->duplex;
35536ce29b0eSClaudiu Manoil 		}
35546ce29b0eSClaudiu Manoil 
35556ce29b0eSClaudiu Manoil 		if (phydev->speed != priv->oldspeed) {
35566ce29b0eSClaudiu Manoil 			switch (phydev->speed) {
35576ce29b0eSClaudiu Manoil 			case 1000:
35586ce29b0eSClaudiu Manoil 				tempval =
35596ce29b0eSClaudiu Manoil 				    ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
35606ce29b0eSClaudiu Manoil 
35616ce29b0eSClaudiu Manoil 				ecntrl &= ~(ECNTRL_R100);
35626ce29b0eSClaudiu Manoil 				break;
35636ce29b0eSClaudiu Manoil 			case 100:
35646ce29b0eSClaudiu Manoil 			case 10:
35656ce29b0eSClaudiu Manoil 				tempval =
35666ce29b0eSClaudiu Manoil 				    ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
35676ce29b0eSClaudiu Manoil 
35686ce29b0eSClaudiu Manoil 				/* Reduced mode distinguishes
35696ce29b0eSClaudiu Manoil 				 * between 10 and 100
35706ce29b0eSClaudiu Manoil 				 */
35716ce29b0eSClaudiu Manoil 				if (phydev->speed == SPEED_100)
35726ce29b0eSClaudiu Manoil 					ecntrl |= ECNTRL_R100;
35736ce29b0eSClaudiu Manoil 				else
35746ce29b0eSClaudiu Manoil 					ecntrl &= ~(ECNTRL_R100);
35756ce29b0eSClaudiu Manoil 				break;
35766ce29b0eSClaudiu Manoil 			default:
35776ce29b0eSClaudiu Manoil 				netif_warn(priv, link, priv->ndev,
35786ce29b0eSClaudiu Manoil 					   "Ack!  Speed (%d) is not 10/100/1000!\n",
35796ce29b0eSClaudiu Manoil 					   phydev->speed);
35806ce29b0eSClaudiu Manoil 				break;
35816ce29b0eSClaudiu Manoil 			}
35826ce29b0eSClaudiu Manoil 
35836ce29b0eSClaudiu Manoil 			priv->oldspeed = phydev->speed;
35846ce29b0eSClaudiu Manoil 		}
35856ce29b0eSClaudiu Manoil 
35866ce29b0eSClaudiu Manoil 		tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
35876ce29b0eSClaudiu Manoil 		tempval1 |= gfar_get_flowctrl_cfg(priv);
35886ce29b0eSClaudiu Manoil 
358945b679c9SMatei Pavaluca 		/* Turn last free buffer recording on */
359045b679c9SMatei Pavaluca 		if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) {
359145b679c9SMatei Pavaluca 			for (i = 0; i < priv->num_rx_queues; i++) {
3592b4b67f26SScott Wood 				u32 bdp_dma;
3593b4b67f26SScott Wood 
359445b679c9SMatei Pavaluca 				rx_queue = priv->rx_queue[i];
3595b4b67f26SScott Wood 				bdp_dma = gfar_rxbd_dma_lastfree(rx_queue);
3596b4b67f26SScott Wood 				gfar_write(rx_queue->rfbptr, bdp_dma);
359745b679c9SMatei Pavaluca 			}
359845b679c9SMatei Pavaluca 
359945b679c9SMatei Pavaluca 			priv->tx_actual_en = 1;
360045b679c9SMatei Pavaluca 		}
360145b679c9SMatei Pavaluca 
360245b679c9SMatei Pavaluca 		if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval))
360345b679c9SMatei Pavaluca 			priv->tx_actual_en = 0;
360445b679c9SMatei Pavaluca 
36056ce29b0eSClaudiu Manoil 		gfar_write(&regs->maccfg1, tempval1);
36066ce29b0eSClaudiu Manoil 		gfar_write(&regs->maccfg2, tempval);
36076ce29b0eSClaudiu Manoil 		gfar_write(&regs->ecntrl, ecntrl);
36086ce29b0eSClaudiu Manoil 
36096ce29b0eSClaudiu Manoil 		if (!priv->oldlink)
36106ce29b0eSClaudiu Manoil 			priv->oldlink = 1;
36116ce29b0eSClaudiu Manoil 
36126ce29b0eSClaudiu Manoil 	} else if (priv->oldlink) {
36136ce29b0eSClaudiu Manoil 		priv->oldlink = 0;
36146ce29b0eSClaudiu Manoil 		priv->oldspeed = 0;
36156ce29b0eSClaudiu Manoil 		priv->oldduplex = -1;
36166ce29b0eSClaudiu Manoil 	}
36176ce29b0eSClaudiu Manoil 
36186ce29b0eSClaudiu Manoil 	if (netif_msg_link(priv))
36196ce29b0eSClaudiu Manoil 		phy_print_status(phydev);
36206ce29b0eSClaudiu Manoil }
36216ce29b0eSClaudiu Manoil 
362294e5a2a8SFabian Frederick static const struct of_device_id gfar_match[] =
3623ec21e2ecSJeff Kirsher {
3624ec21e2ecSJeff Kirsher 	{
3625ec21e2ecSJeff Kirsher 		.type = "network",
3626ec21e2ecSJeff Kirsher 		.compatible = "gianfar",
3627ec21e2ecSJeff Kirsher 	},
3628ec21e2ecSJeff Kirsher 	{
3629ec21e2ecSJeff Kirsher 		.compatible = "fsl,etsec2",
3630ec21e2ecSJeff Kirsher 	},
3631ec21e2ecSJeff Kirsher 	{},
3632ec21e2ecSJeff Kirsher };
3633ec21e2ecSJeff Kirsher MODULE_DEVICE_TABLE(of, gfar_match);
3634ec21e2ecSJeff Kirsher 
3635ec21e2ecSJeff Kirsher /* Structure for a device driver */
3636ec21e2ecSJeff Kirsher static struct platform_driver gfar_driver = {
3637ec21e2ecSJeff Kirsher 	.driver = {
3638ec21e2ecSJeff Kirsher 		.name = "fsl-gianfar",
3639ec21e2ecSJeff Kirsher 		.pm = GFAR_PM_OPS,
3640ec21e2ecSJeff Kirsher 		.of_match_table = gfar_match,
3641ec21e2ecSJeff Kirsher 	},
3642ec21e2ecSJeff Kirsher 	.probe = gfar_probe,
3643ec21e2ecSJeff Kirsher 	.remove = gfar_remove,
3644ec21e2ecSJeff Kirsher };
3645ec21e2ecSJeff Kirsher 
3646db62f684SAxel Lin module_platform_driver(gfar_driver);
3647