xref: /openbmc/linux/drivers/net/ethernet/freescale/gianfar.c (revision 80ec396cb6b522b23c69dfff32a2d12993e4bb30)
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>
91ec21e2ecSJeff Kirsher #include <asm/reg.h>
922969b1f7SClaudiu Manoil #include <asm/mpc85xx.h>
93ec21e2ecSJeff Kirsher #include <asm/irq.h>
94ec21e2ecSJeff Kirsher #include <asm/uaccess.h>
95ec21e2ecSJeff Kirsher #include <linux/module.h>
96ec21e2ecSJeff Kirsher #include <linux/dma-mapping.h>
97ec21e2ecSJeff Kirsher #include <linux/crc32.h>
98ec21e2ecSJeff Kirsher #include <linux/mii.h>
99ec21e2ecSJeff Kirsher #include <linux/phy.h>
100ec21e2ecSJeff Kirsher #include <linux/phy_fixed.h>
101ec21e2ecSJeff Kirsher #include <linux/of.h>
102ec21e2ecSJeff Kirsher #include <linux/of_net.h>
103ec21e2ecSJeff Kirsher 
104ec21e2ecSJeff Kirsher #include "gianfar.h"
105ec21e2ecSJeff Kirsher 
106ec21e2ecSJeff Kirsher #define TX_TIMEOUT      (1*HZ)
107ec21e2ecSJeff Kirsher 
108ec21e2ecSJeff Kirsher const char gfar_driver_version[] = "1.3";
109ec21e2ecSJeff Kirsher 
110ec21e2ecSJeff Kirsher static int gfar_enet_open(struct net_device *dev);
111ec21e2ecSJeff Kirsher static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
112ec21e2ecSJeff Kirsher static void gfar_reset_task(struct work_struct *work);
113ec21e2ecSJeff Kirsher static void gfar_timeout(struct net_device *dev);
114ec21e2ecSJeff Kirsher static int gfar_close(struct net_device *dev);
115ec21e2ecSJeff Kirsher struct sk_buff *gfar_new_skb(struct net_device *dev);
116ec21e2ecSJeff Kirsher static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
117ec21e2ecSJeff Kirsher 			   struct sk_buff *skb);
118ec21e2ecSJeff Kirsher static int gfar_set_mac_address(struct net_device *dev);
119ec21e2ecSJeff Kirsher static int gfar_change_mtu(struct net_device *dev, int new_mtu);
120ec21e2ecSJeff Kirsher static irqreturn_t gfar_error(int irq, void *dev_id);
121ec21e2ecSJeff Kirsher static irqreturn_t gfar_transmit(int irq, void *dev_id);
122ec21e2ecSJeff Kirsher static irqreturn_t gfar_interrupt(int irq, void *dev_id);
123ec21e2ecSJeff Kirsher static void adjust_link(struct net_device *dev);
124ec21e2ecSJeff Kirsher static int init_phy(struct net_device *dev);
125ec21e2ecSJeff Kirsher static int gfar_probe(struct platform_device *ofdev);
126ec21e2ecSJeff Kirsher static int gfar_remove(struct platform_device *ofdev);
127ec21e2ecSJeff Kirsher static void free_skb_resources(struct gfar_private *priv);
128ec21e2ecSJeff Kirsher static void gfar_set_multi(struct net_device *dev);
129ec21e2ecSJeff Kirsher static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
130ec21e2ecSJeff Kirsher static void gfar_configure_serdes(struct net_device *dev);
131ec21e2ecSJeff Kirsher static int gfar_poll(struct napi_struct *napi, int budget);
1325eaedf31SClaudiu Manoil static int gfar_poll_sq(struct napi_struct *napi, int budget);
133ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
134ec21e2ecSJeff Kirsher static void gfar_netpoll(struct net_device *dev);
135ec21e2ecSJeff Kirsher #endif
136ec21e2ecSJeff Kirsher int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
137c233cf40SClaudiu Manoil static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
13861db26c6SClaudiu Manoil static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
139cd754a57SWu Jiajun-B06378 			       int amount_pull, struct napi_struct *napi);
140c10650b6SClaudiu Manoil static void gfar_halt_nodisable(struct gfar_private *priv);
141ec21e2ecSJeff Kirsher static void gfar_clear_exact_match(struct net_device *dev);
142ec21e2ecSJeff Kirsher static void gfar_set_mac_for_addr(struct net_device *dev, int num,
143ec21e2ecSJeff Kirsher 				  const u8 *addr);
144ec21e2ecSJeff Kirsher static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
145ec21e2ecSJeff Kirsher 
146ec21e2ecSJeff Kirsher MODULE_AUTHOR("Freescale Semiconductor, Inc");
147ec21e2ecSJeff Kirsher MODULE_DESCRIPTION("Gianfar Ethernet Driver");
148ec21e2ecSJeff Kirsher MODULE_LICENSE("GPL");
149ec21e2ecSJeff Kirsher 
150ec21e2ecSJeff Kirsher static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
151ec21e2ecSJeff Kirsher 			    dma_addr_t buf)
152ec21e2ecSJeff Kirsher {
153ec21e2ecSJeff Kirsher 	u32 lstatus;
154ec21e2ecSJeff Kirsher 
155ec21e2ecSJeff Kirsher 	bdp->bufPtr = buf;
156ec21e2ecSJeff Kirsher 
157ec21e2ecSJeff Kirsher 	lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
158ec21e2ecSJeff Kirsher 	if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
159ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(RXBD_WRAP);
160ec21e2ecSJeff Kirsher 
161ec21e2ecSJeff Kirsher 	eieio();
162ec21e2ecSJeff Kirsher 
163ec21e2ecSJeff Kirsher 	bdp->lstatus = lstatus;
164ec21e2ecSJeff Kirsher }
165ec21e2ecSJeff Kirsher 
166ec21e2ecSJeff Kirsher static int gfar_init_bds(struct net_device *ndev)
167ec21e2ecSJeff Kirsher {
168ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(ndev);
169ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
170ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
171ec21e2ecSJeff Kirsher 	struct txbd8 *txbdp;
172ec21e2ecSJeff Kirsher 	struct rxbd8 *rxbdp;
173ec21e2ecSJeff Kirsher 	int i, j;
174ec21e2ecSJeff Kirsher 
175ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
176ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
177ec21e2ecSJeff Kirsher 		/* Initialize some variables in our dev structure */
178ec21e2ecSJeff Kirsher 		tx_queue->num_txbdfree = tx_queue->tx_ring_size;
179ec21e2ecSJeff Kirsher 		tx_queue->dirty_tx = tx_queue->tx_bd_base;
180ec21e2ecSJeff Kirsher 		tx_queue->cur_tx = tx_queue->tx_bd_base;
181ec21e2ecSJeff Kirsher 		tx_queue->skb_curtx = 0;
182ec21e2ecSJeff Kirsher 		tx_queue->skb_dirtytx = 0;
183ec21e2ecSJeff Kirsher 
184ec21e2ecSJeff Kirsher 		/* Initialize Transmit Descriptor Ring */
185ec21e2ecSJeff Kirsher 		txbdp = tx_queue->tx_bd_base;
186ec21e2ecSJeff Kirsher 		for (j = 0; j < tx_queue->tx_ring_size; j++) {
187ec21e2ecSJeff Kirsher 			txbdp->lstatus = 0;
188ec21e2ecSJeff Kirsher 			txbdp->bufPtr = 0;
189ec21e2ecSJeff Kirsher 			txbdp++;
190ec21e2ecSJeff Kirsher 		}
191ec21e2ecSJeff Kirsher 
192ec21e2ecSJeff Kirsher 		/* Set the last descriptor in the ring to indicate wrap */
193ec21e2ecSJeff Kirsher 		txbdp--;
194ec21e2ecSJeff Kirsher 		txbdp->status |= TXBD_WRAP;
195ec21e2ecSJeff Kirsher 	}
196ec21e2ecSJeff Kirsher 
197ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
198ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
199ec21e2ecSJeff Kirsher 		rx_queue->cur_rx = rx_queue->rx_bd_base;
200ec21e2ecSJeff Kirsher 		rx_queue->skb_currx = 0;
201ec21e2ecSJeff Kirsher 		rxbdp = rx_queue->rx_bd_base;
202ec21e2ecSJeff Kirsher 
203ec21e2ecSJeff Kirsher 		for (j = 0; j < rx_queue->rx_ring_size; j++) {
204ec21e2ecSJeff Kirsher 			struct sk_buff *skb = rx_queue->rx_skbuff[j];
205ec21e2ecSJeff Kirsher 
206ec21e2ecSJeff Kirsher 			if (skb) {
207ec21e2ecSJeff Kirsher 				gfar_init_rxbdp(rx_queue, rxbdp,
208ec21e2ecSJeff Kirsher 						rxbdp->bufPtr);
209ec21e2ecSJeff Kirsher 			} else {
210ec21e2ecSJeff Kirsher 				skb = gfar_new_skb(ndev);
211ec21e2ecSJeff Kirsher 				if (!skb) {
212ec21e2ecSJeff Kirsher 					netdev_err(ndev, "Can't allocate RX buffers\n");
2131eb8f7a7SClaudiu Manoil 					return -ENOMEM;
214ec21e2ecSJeff Kirsher 				}
215ec21e2ecSJeff Kirsher 				rx_queue->rx_skbuff[j] = skb;
216ec21e2ecSJeff Kirsher 
217ec21e2ecSJeff Kirsher 				gfar_new_rxbdp(rx_queue, rxbdp, skb);
218ec21e2ecSJeff Kirsher 			}
219ec21e2ecSJeff Kirsher 
220ec21e2ecSJeff Kirsher 			rxbdp++;
221ec21e2ecSJeff Kirsher 		}
222ec21e2ecSJeff Kirsher 
223ec21e2ecSJeff Kirsher 	}
224ec21e2ecSJeff Kirsher 
225ec21e2ecSJeff Kirsher 	return 0;
226ec21e2ecSJeff Kirsher }
227ec21e2ecSJeff Kirsher 
228ec21e2ecSJeff Kirsher static int gfar_alloc_skb_resources(struct net_device *ndev)
229ec21e2ecSJeff Kirsher {
230ec21e2ecSJeff Kirsher 	void *vaddr;
231ec21e2ecSJeff Kirsher 	dma_addr_t addr;
232ec21e2ecSJeff Kirsher 	int i, j, k;
233ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(ndev);
234369ec162SClaudiu Manoil 	struct device *dev = priv->dev;
235ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
236ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
237ec21e2ecSJeff Kirsher 
238ec21e2ecSJeff Kirsher 	priv->total_tx_ring_size = 0;
239ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
240ec21e2ecSJeff Kirsher 		priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
241ec21e2ecSJeff Kirsher 
242ec21e2ecSJeff Kirsher 	priv->total_rx_ring_size = 0;
243ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
244ec21e2ecSJeff Kirsher 		priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
245ec21e2ecSJeff Kirsher 
246ec21e2ecSJeff Kirsher 	/* Allocate memory for the buffer descriptors */
247ec21e2ecSJeff Kirsher 	vaddr = dma_alloc_coherent(dev,
248d0320f75SJoe Perches 				   (priv->total_tx_ring_size *
249d0320f75SJoe Perches 				    sizeof(struct txbd8)) +
250d0320f75SJoe Perches 				   (priv->total_rx_ring_size *
251d0320f75SJoe Perches 				    sizeof(struct rxbd8)),
252ec21e2ecSJeff Kirsher 				   &addr, GFP_KERNEL);
253d0320f75SJoe Perches 	if (!vaddr)
254ec21e2ecSJeff Kirsher 		return -ENOMEM;
255ec21e2ecSJeff Kirsher 
256ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
257ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
258ec21e2ecSJeff Kirsher 		tx_queue->tx_bd_base = vaddr;
259ec21e2ecSJeff Kirsher 		tx_queue->tx_bd_dma_base = addr;
260ec21e2ecSJeff Kirsher 		tx_queue->dev = ndev;
261ec21e2ecSJeff Kirsher 		/* enet DMA only understands physical addresses */
262ec21e2ecSJeff Kirsher 		addr  += sizeof(struct txbd8) * tx_queue->tx_ring_size;
263ec21e2ecSJeff Kirsher 		vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
264ec21e2ecSJeff Kirsher 	}
265ec21e2ecSJeff Kirsher 
266ec21e2ecSJeff Kirsher 	/* Start the rx descriptor ring where the tx ring leaves off */
267ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
268ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
269ec21e2ecSJeff Kirsher 		rx_queue->rx_bd_base = vaddr;
270ec21e2ecSJeff Kirsher 		rx_queue->rx_bd_dma_base = addr;
271ec21e2ecSJeff Kirsher 		rx_queue->dev = ndev;
272ec21e2ecSJeff Kirsher 		addr  += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
273ec21e2ecSJeff Kirsher 		vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
274ec21e2ecSJeff Kirsher 	}
275ec21e2ecSJeff Kirsher 
276ec21e2ecSJeff Kirsher 	/* Setup the skbuff rings */
277ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
278ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
27914f8dc49SJoe Perches 		tx_queue->tx_skbuff =
28014f8dc49SJoe Perches 			kmalloc_array(tx_queue->tx_ring_size,
28114f8dc49SJoe Perches 				      sizeof(*tx_queue->tx_skbuff),
282bc4598bcSJan Ceuleers 				      GFP_KERNEL);
28314f8dc49SJoe Perches 		if (!tx_queue->tx_skbuff)
284ec21e2ecSJeff Kirsher 			goto cleanup;
285ec21e2ecSJeff Kirsher 
286ec21e2ecSJeff Kirsher 		for (k = 0; k < tx_queue->tx_ring_size; k++)
287ec21e2ecSJeff Kirsher 			tx_queue->tx_skbuff[k] = NULL;
288ec21e2ecSJeff Kirsher 	}
289ec21e2ecSJeff Kirsher 
290ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
291ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
29214f8dc49SJoe Perches 		rx_queue->rx_skbuff =
29314f8dc49SJoe Perches 			kmalloc_array(rx_queue->rx_ring_size,
29414f8dc49SJoe Perches 				      sizeof(*rx_queue->rx_skbuff),
295bc4598bcSJan Ceuleers 				      GFP_KERNEL);
29614f8dc49SJoe Perches 		if (!rx_queue->rx_skbuff)
297ec21e2ecSJeff Kirsher 			goto cleanup;
298ec21e2ecSJeff Kirsher 
299ec21e2ecSJeff Kirsher 		for (j = 0; j < rx_queue->rx_ring_size; j++)
300ec21e2ecSJeff Kirsher 			rx_queue->rx_skbuff[j] = NULL;
301ec21e2ecSJeff Kirsher 	}
302ec21e2ecSJeff Kirsher 
303ec21e2ecSJeff Kirsher 	if (gfar_init_bds(ndev))
304ec21e2ecSJeff Kirsher 		goto cleanup;
305ec21e2ecSJeff Kirsher 
306ec21e2ecSJeff Kirsher 	return 0;
307ec21e2ecSJeff Kirsher 
308ec21e2ecSJeff Kirsher cleanup:
309ec21e2ecSJeff Kirsher 	free_skb_resources(priv);
310ec21e2ecSJeff Kirsher 	return -ENOMEM;
311ec21e2ecSJeff Kirsher }
312ec21e2ecSJeff Kirsher 
313ec21e2ecSJeff Kirsher static void gfar_init_tx_rx_base(struct gfar_private *priv)
314ec21e2ecSJeff Kirsher {
315ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
316ec21e2ecSJeff Kirsher 	u32 __iomem *baddr;
317ec21e2ecSJeff Kirsher 	int i;
318ec21e2ecSJeff Kirsher 
319ec21e2ecSJeff Kirsher 	baddr = &regs->tbase0;
320ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
321ec21e2ecSJeff Kirsher 		gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
322ec21e2ecSJeff Kirsher 		baddr += 2;
323ec21e2ecSJeff Kirsher 	}
324ec21e2ecSJeff Kirsher 
325ec21e2ecSJeff Kirsher 	baddr = &regs->rbase0;
326ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
327ec21e2ecSJeff Kirsher 		gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
328ec21e2ecSJeff Kirsher 		baddr += 2;
329ec21e2ecSJeff Kirsher 	}
330ec21e2ecSJeff Kirsher }
331ec21e2ecSJeff Kirsher 
33288302648SClaudiu Manoil static void gfar_rx_buff_size_config(struct gfar_private *priv)
33388302648SClaudiu Manoil {
33488302648SClaudiu Manoil 	int frame_size = priv->ndev->mtu + ETH_HLEN;
33588302648SClaudiu Manoil 
33688302648SClaudiu Manoil 	/* set this when rx hw offload (TOE) functions are being used */
33788302648SClaudiu Manoil 	priv->uses_rxfcb = 0;
33888302648SClaudiu Manoil 
33988302648SClaudiu Manoil 	if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
34088302648SClaudiu Manoil 		priv->uses_rxfcb = 1;
34188302648SClaudiu Manoil 
34288302648SClaudiu Manoil 	if (priv->hwts_rx_en)
34388302648SClaudiu Manoil 		priv->uses_rxfcb = 1;
34488302648SClaudiu Manoil 
34588302648SClaudiu Manoil 	if (priv->uses_rxfcb)
34688302648SClaudiu Manoil 		frame_size += GMAC_FCB_LEN;
34788302648SClaudiu Manoil 
34888302648SClaudiu Manoil 	frame_size += priv->padding;
34988302648SClaudiu Manoil 
35088302648SClaudiu Manoil 	frame_size = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
35188302648SClaudiu Manoil 		     INCREMENTAL_BUFFER_SIZE;
35288302648SClaudiu Manoil 
35388302648SClaudiu Manoil 	priv->rx_buffer_size = frame_size;
35488302648SClaudiu Manoil }
35588302648SClaudiu Manoil 
356a328ac92SClaudiu Manoil static void gfar_mac_rx_config(struct gfar_private *priv)
357ec21e2ecSJeff Kirsher {
358ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
359ec21e2ecSJeff Kirsher 	u32 rctrl = 0;
360ec21e2ecSJeff Kirsher 
361ec21e2ecSJeff Kirsher 	if (priv->rx_filer_enable) {
362ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_FILREN;
363ec21e2ecSJeff Kirsher 		/* Program the RIR0 reg with the required distribution */
364ec21e2ecSJeff Kirsher 		gfar_write(&regs->rir0, DEFAULT_RIR0);
365ec21e2ecSJeff Kirsher 	}
366ec21e2ecSJeff Kirsher 
367f5ae6279SClaudiu Manoil 	/* Restore PROMISC mode */
368a328ac92SClaudiu Manoil 	if (priv->ndev->flags & IFF_PROMISC)
369f5ae6279SClaudiu Manoil 		rctrl |= RCTRL_PROM;
370f5ae6279SClaudiu Manoil 
37188302648SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_RXCSUM)
372ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_CHECKSUMMING;
373ec21e2ecSJeff Kirsher 
37488302648SClaudiu Manoil 	if (priv->extended_hash)
37588302648SClaudiu Manoil 		rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
376ec21e2ecSJeff Kirsher 
377ec21e2ecSJeff Kirsher 	if (priv->padding) {
378ec21e2ecSJeff Kirsher 		rctrl &= ~RCTRL_PAL_MASK;
379ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_PADDING(priv->padding);
380ec21e2ecSJeff Kirsher 	}
381ec21e2ecSJeff Kirsher 
382ec21e2ecSJeff Kirsher 	/* Enable HW time stamping if requested from user space */
38388302648SClaudiu Manoil 	if (priv->hwts_rx_en)
384ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
385ec21e2ecSJeff Kirsher 
38688302648SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
387ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
388ec21e2ecSJeff Kirsher 
389ec21e2ecSJeff Kirsher 	/* Init rctrl based on our settings */
390ec21e2ecSJeff Kirsher 	gfar_write(&regs->rctrl, rctrl);
391a328ac92SClaudiu Manoil }
392ec21e2ecSJeff Kirsher 
393a328ac92SClaudiu Manoil static void gfar_mac_tx_config(struct gfar_private *priv)
394a328ac92SClaudiu Manoil {
395a328ac92SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
396a328ac92SClaudiu Manoil 	u32 tctrl = 0;
397a328ac92SClaudiu Manoil 
398a328ac92SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_IP_CSUM)
399ec21e2ecSJeff Kirsher 		tctrl |= TCTRL_INIT_CSUM;
400ec21e2ecSJeff Kirsher 
401b98b8babSClaudiu Manoil 	if (priv->prio_sched_en)
402ec21e2ecSJeff Kirsher 		tctrl |= TCTRL_TXSCHED_PRIO;
403b98b8babSClaudiu Manoil 	else {
404b98b8babSClaudiu Manoil 		tctrl |= TCTRL_TXSCHED_WRRS;
405b98b8babSClaudiu Manoil 		gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
406b98b8babSClaudiu Manoil 		gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
407b98b8babSClaudiu Manoil 	}
408ec21e2ecSJeff Kirsher 
40988302648SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
41088302648SClaudiu Manoil 		tctrl |= TCTRL_VLINS;
41188302648SClaudiu Manoil 
412ec21e2ecSJeff Kirsher 	gfar_write(&regs->tctrl, tctrl);
413ec21e2ecSJeff Kirsher }
414ec21e2ecSJeff Kirsher 
415ec21e2ecSJeff Kirsher static struct net_device_stats *gfar_get_stats(struct net_device *dev)
416ec21e2ecSJeff Kirsher {
417ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
418ec21e2ecSJeff Kirsher 	unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
419ec21e2ecSJeff Kirsher 	unsigned long tx_packets = 0, tx_bytes = 0;
4203a2e16c8SJan Ceuleers 	int i;
421ec21e2ecSJeff Kirsher 
422ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
423ec21e2ecSJeff Kirsher 		rx_packets += priv->rx_queue[i]->stats.rx_packets;
424ec21e2ecSJeff Kirsher 		rx_bytes   += priv->rx_queue[i]->stats.rx_bytes;
425ec21e2ecSJeff Kirsher 		rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
426ec21e2ecSJeff Kirsher 	}
427ec21e2ecSJeff Kirsher 
428ec21e2ecSJeff Kirsher 	dev->stats.rx_packets = rx_packets;
429ec21e2ecSJeff Kirsher 	dev->stats.rx_bytes   = rx_bytes;
430ec21e2ecSJeff Kirsher 	dev->stats.rx_dropped = rx_dropped;
431ec21e2ecSJeff Kirsher 
432ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
433ec21e2ecSJeff Kirsher 		tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
434ec21e2ecSJeff Kirsher 		tx_packets += priv->tx_queue[i]->stats.tx_packets;
435ec21e2ecSJeff Kirsher 	}
436ec21e2ecSJeff Kirsher 
437ec21e2ecSJeff Kirsher 	dev->stats.tx_bytes   = tx_bytes;
438ec21e2ecSJeff Kirsher 	dev->stats.tx_packets = tx_packets;
439ec21e2ecSJeff Kirsher 
440ec21e2ecSJeff Kirsher 	return &dev->stats;
441ec21e2ecSJeff Kirsher }
442ec21e2ecSJeff Kirsher 
443ec21e2ecSJeff Kirsher static const struct net_device_ops gfar_netdev_ops = {
444ec21e2ecSJeff Kirsher 	.ndo_open = gfar_enet_open,
445ec21e2ecSJeff Kirsher 	.ndo_start_xmit = gfar_start_xmit,
446ec21e2ecSJeff Kirsher 	.ndo_stop = gfar_close,
447ec21e2ecSJeff Kirsher 	.ndo_change_mtu = gfar_change_mtu,
448ec21e2ecSJeff Kirsher 	.ndo_set_features = gfar_set_features,
449afc4b13dSJiri Pirko 	.ndo_set_rx_mode = gfar_set_multi,
450ec21e2ecSJeff Kirsher 	.ndo_tx_timeout = gfar_timeout,
451ec21e2ecSJeff Kirsher 	.ndo_do_ioctl = gfar_ioctl,
452ec21e2ecSJeff Kirsher 	.ndo_get_stats = gfar_get_stats,
453ec21e2ecSJeff Kirsher 	.ndo_set_mac_address = eth_mac_addr,
454ec21e2ecSJeff Kirsher 	.ndo_validate_addr = eth_validate_addr,
455ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
456ec21e2ecSJeff Kirsher 	.ndo_poll_controller = gfar_netpoll,
457ec21e2ecSJeff Kirsher #endif
458ec21e2ecSJeff Kirsher };
459ec21e2ecSJeff Kirsher 
460efeddce7SClaudiu Manoil static void gfar_ints_disable(struct gfar_private *priv)
461efeddce7SClaudiu Manoil {
462efeddce7SClaudiu Manoil 	int i;
463efeddce7SClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
464efeddce7SClaudiu Manoil 		struct gfar __iomem *regs = priv->gfargrp[i].regs;
465efeddce7SClaudiu Manoil 		/* Clear IEVENT */
466efeddce7SClaudiu Manoil 		gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
467efeddce7SClaudiu Manoil 
468efeddce7SClaudiu Manoil 		/* Initialize IMASK */
469efeddce7SClaudiu Manoil 		gfar_write(&regs->imask, IMASK_INIT_CLEAR);
470efeddce7SClaudiu Manoil 	}
471efeddce7SClaudiu Manoil }
472efeddce7SClaudiu Manoil 
473efeddce7SClaudiu Manoil static void gfar_ints_enable(struct gfar_private *priv)
474efeddce7SClaudiu Manoil {
475efeddce7SClaudiu Manoil 	int i;
476efeddce7SClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
477efeddce7SClaudiu Manoil 		struct gfar __iomem *regs = priv->gfargrp[i].regs;
478efeddce7SClaudiu Manoil 		/* Unmask the interrupts we look for */
479efeddce7SClaudiu Manoil 		gfar_write(&regs->imask, IMASK_DEFAULT);
480efeddce7SClaudiu Manoil 	}
481efeddce7SClaudiu Manoil }
482efeddce7SClaudiu Manoil 
483ec21e2ecSJeff Kirsher void lock_rx_qs(struct gfar_private *priv)
484ec21e2ecSJeff Kirsher {
4853a2e16c8SJan Ceuleers 	int i;
486ec21e2ecSJeff Kirsher 
487ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
488ec21e2ecSJeff Kirsher 		spin_lock(&priv->rx_queue[i]->rxlock);
489ec21e2ecSJeff Kirsher }
490ec21e2ecSJeff Kirsher 
491ec21e2ecSJeff Kirsher void lock_tx_qs(struct gfar_private *priv)
492ec21e2ecSJeff Kirsher {
4933a2e16c8SJan Ceuleers 	int i;
494ec21e2ecSJeff Kirsher 
495ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
496ec21e2ecSJeff Kirsher 		spin_lock(&priv->tx_queue[i]->txlock);
497ec21e2ecSJeff Kirsher }
498ec21e2ecSJeff Kirsher 
499ec21e2ecSJeff Kirsher void unlock_rx_qs(struct gfar_private *priv)
500ec21e2ecSJeff Kirsher {
5013a2e16c8SJan Ceuleers 	int i;
502ec21e2ecSJeff Kirsher 
503ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
504ec21e2ecSJeff Kirsher 		spin_unlock(&priv->rx_queue[i]->rxlock);
505ec21e2ecSJeff Kirsher }
506ec21e2ecSJeff Kirsher 
507ec21e2ecSJeff Kirsher void unlock_tx_qs(struct gfar_private *priv)
508ec21e2ecSJeff Kirsher {
5093a2e16c8SJan Ceuleers 	int i;
510ec21e2ecSJeff Kirsher 
511ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
512ec21e2ecSJeff Kirsher 		spin_unlock(&priv->tx_queue[i]->txlock);
513ec21e2ecSJeff Kirsher }
514ec21e2ecSJeff Kirsher 
51520862788SClaudiu Manoil static int gfar_alloc_tx_queues(struct gfar_private *priv)
51620862788SClaudiu Manoil {
51720862788SClaudiu Manoil 	int i;
51820862788SClaudiu Manoil 
51920862788SClaudiu Manoil 	for (i = 0; i < priv->num_tx_queues; i++) {
52020862788SClaudiu Manoil 		priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
52120862788SClaudiu Manoil 					    GFP_KERNEL);
52220862788SClaudiu Manoil 		if (!priv->tx_queue[i])
52320862788SClaudiu Manoil 			return -ENOMEM;
52420862788SClaudiu Manoil 
52520862788SClaudiu Manoil 		priv->tx_queue[i]->tx_skbuff = NULL;
52620862788SClaudiu Manoil 		priv->tx_queue[i]->qindex = i;
52720862788SClaudiu Manoil 		priv->tx_queue[i]->dev = priv->ndev;
52820862788SClaudiu Manoil 		spin_lock_init(&(priv->tx_queue[i]->txlock));
52920862788SClaudiu Manoil 	}
53020862788SClaudiu Manoil 	return 0;
53120862788SClaudiu Manoil }
53220862788SClaudiu Manoil 
53320862788SClaudiu Manoil static int gfar_alloc_rx_queues(struct gfar_private *priv)
53420862788SClaudiu Manoil {
53520862788SClaudiu Manoil 	int i;
53620862788SClaudiu Manoil 
53720862788SClaudiu Manoil 	for (i = 0; i < priv->num_rx_queues; i++) {
53820862788SClaudiu Manoil 		priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
53920862788SClaudiu Manoil 					    GFP_KERNEL);
54020862788SClaudiu Manoil 		if (!priv->rx_queue[i])
54120862788SClaudiu Manoil 			return -ENOMEM;
54220862788SClaudiu Manoil 
54320862788SClaudiu Manoil 		priv->rx_queue[i]->rx_skbuff = NULL;
54420862788SClaudiu Manoil 		priv->rx_queue[i]->qindex = i;
54520862788SClaudiu Manoil 		priv->rx_queue[i]->dev = priv->ndev;
54620862788SClaudiu Manoil 		spin_lock_init(&(priv->rx_queue[i]->rxlock));
54720862788SClaudiu Manoil 	}
54820862788SClaudiu Manoil 	return 0;
54920862788SClaudiu Manoil }
55020862788SClaudiu Manoil 
55120862788SClaudiu Manoil static void gfar_free_tx_queues(struct gfar_private *priv)
552ec21e2ecSJeff Kirsher {
5533a2e16c8SJan Ceuleers 	int i;
554ec21e2ecSJeff Kirsher 
555ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
556ec21e2ecSJeff Kirsher 		kfree(priv->tx_queue[i]);
557ec21e2ecSJeff Kirsher }
558ec21e2ecSJeff Kirsher 
55920862788SClaudiu Manoil static void gfar_free_rx_queues(struct gfar_private *priv)
560ec21e2ecSJeff Kirsher {
5613a2e16c8SJan Ceuleers 	int i;
562ec21e2ecSJeff Kirsher 
563ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
564ec21e2ecSJeff Kirsher 		kfree(priv->rx_queue[i]);
565ec21e2ecSJeff Kirsher }
566ec21e2ecSJeff Kirsher 
567ec21e2ecSJeff Kirsher static void unmap_group_regs(struct gfar_private *priv)
568ec21e2ecSJeff Kirsher {
5693a2e16c8SJan Ceuleers 	int i;
570ec21e2ecSJeff Kirsher 
571ec21e2ecSJeff Kirsher 	for (i = 0; i < MAXGROUPS; i++)
572ec21e2ecSJeff Kirsher 		if (priv->gfargrp[i].regs)
573ec21e2ecSJeff Kirsher 			iounmap(priv->gfargrp[i].regs);
574ec21e2ecSJeff Kirsher }
575ec21e2ecSJeff Kirsher 
576ee873fdaSClaudiu Manoil static void free_gfar_dev(struct gfar_private *priv)
577ee873fdaSClaudiu Manoil {
578ee873fdaSClaudiu Manoil 	int i, j;
579ee873fdaSClaudiu Manoil 
580ee873fdaSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++)
581ee873fdaSClaudiu Manoil 		for (j = 0; j < GFAR_NUM_IRQS; j++) {
582ee873fdaSClaudiu Manoil 			kfree(priv->gfargrp[i].irqinfo[j]);
583ee873fdaSClaudiu Manoil 			priv->gfargrp[i].irqinfo[j] = NULL;
584ee873fdaSClaudiu Manoil 		}
585ee873fdaSClaudiu Manoil 
586ee873fdaSClaudiu Manoil 	free_netdev(priv->ndev);
587ee873fdaSClaudiu Manoil }
588ee873fdaSClaudiu Manoil 
589ec21e2ecSJeff Kirsher static void disable_napi(struct gfar_private *priv)
590ec21e2ecSJeff Kirsher {
5913a2e16c8SJan Ceuleers 	int i;
592ec21e2ecSJeff Kirsher 
593ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_grps; i++)
594ec21e2ecSJeff Kirsher 		napi_disable(&priv->gfargrp[i].napi);
595ec21e2ecSJeff Kirsher }
596ec21e2ecSJeff Kirsher 
597ec21e2ecSJeff Kirsher static void enable_napi(struct gfar_private *priv)
598ec21e2ecSJeff Kirsher {
5993a2e16c8SJan Ceuleers 	int i;
600ec21e2ecSJeff Kirsher 
601ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_grps; i++)
602ec21e2ecSJeff Kirsher 		napi_enable(&priv->gfargrp[i].napi);
603ec21e2ecSJeff Kirsher }
604ec21e2ecSJeff Kirsher 
605ec21e2ecSJeff Kirsher static int gfar_parse_group(struct device_node *np,
606ec21e2ecSJeff Kirsher 			    struct gfar_private *priv, const char *model)
607ec21e2ecSJeff Kirsher {
6085fedcc14SClaudiu Manoil 	struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
609ec21e2ecSJeff Kirsher 	u32 *queue_mask;
610ee873fdaSClaudiu Manoil 	int i;
611ee873fdaSClaudiu Manoil 
612ee873fdaSClaudiu Manoil 	for (i = 0; i < GFAR_NUM_IRQS; i++) {
613ee873fdaSClaudiu Manoil 		grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
614ee873fdaSClaudiu Manoil 					  GFP_KERNEL);
615ee873fdaSClaudiu Manoil 		if (!grp->irqinfo[i])
616ee873fdaSClaudiu Manoil 			return -ENOMEM;
617ee873fdaSClaudiu Manoil 	}
618ec21e2ecSJeff Kirsher 
6195fedcc14SClaudiu Manoil 	grp->regs = of_iomap(np, 0);
6205fedcc14SClaudiu Manoil 	if (!grp->regs)
621ec21e2ecSJeff Kirsher 		return -ENOMEM;
622ec21e2ecSJeff Kirsher 
623ee873fdaSClaudiu Manoil 	gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
624ec21e2ecSJeff Kirsher 
625ec21e2ecSJeff Kirsher 	/* If we aren't the FEC we have multiple interrupts */
626ec21e2ecSJeff Kirsher 	if (model && strcasecmp(model, "FEC")) {
627ee873fdaSClaudiu Manoil 		gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
628ee873fdaSClaudiu Manoil 		gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
629ee873fdaSClaudiu Manoil 		if (gfar_irq(grp, TX)->irq == NO_IRQ ||
630ee873fdaSClaudiu Manoil 		    gfar_irq(grp, RX)->irq == NO_IRQ ||
631ee873fdaSClaudiu Manoil 		    gfar_irq(grp, ER)->irq == NO_IRQ)
632ec21e2ecSJeff Kirsher 			return -EINVAL;
633ec21e2ecSJeff Kirsher 	}
634ec21e2ecSJeff Kirsher 
6355fedcc14SClaudiu Manoil 	grp->priv = priv;
6365fedcc14SClaudiu Manoil 	spin_lock_init(&grp->grplock);
637ec21e2ecSJeff Kirsher 	if (priv->mode == MQ_MG_MODE) {
638bc4598bcSJan Ceuleers 		queue_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL);
6395fedcc14SClaudiu Manoil 		grp->rx_bit_map = queue_mask ?
640bc4598bcSJan Ceuleers 			*queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
641bc4598bcSJan Ceuleers 		queue_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL);
6425fedcc14SClaudiu Manoil 		grp->tx_bit_map = queue_mask ?
643bc4598bcSJan Ceuleers 			*queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
644ec21e2ecSJeff Kirsher 	} else {
6455fedcc14SClaudiu Manoil 		grp->rx_bit_map = 0xFF;
6465fedcc14SClaudiu Manoil 		grp->tx_bit_map = 0xFF;
647ec21e2ecSJeff Kirsher 	}
64820862788SClaudiu Manoil 
64920862788SClaudiu Manoil 	/* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
65020862788SClaudiu Manoil 	 * right to left, so we need to revert the 8 bits to get the q index
65120862788SClaudiu Manoil 	 */
65220862788SClaudiu Manoil 	grp->rx_bit_map = bitrev8(grp->rx_bit_map);
65320862788SClaudiu Manoil 	grp->tx_bit_map = bitrev8(grp->tx_bit_map);
65420862788SClaudiu Manoil 
65520862788SClaudiu Manoil 	/* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
65620862788SClaudiu Manoil 	 * also assign queues to groups
65720862788SClaudiu Manoil 	 */
65820862788SClaudiu Manoil 	for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
65920862788SClaudiu Manoil 		grp->num_rx_queues++;
66020862788SClaudiu Manoil 		grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
66120862788SClaudiu Manoil 		priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
66220862788SClaudiu Manoil 		priv->rx_queue[i]->grp = grp;
66320862788SClaudiu Manoil 	}
66420862788SClaudiu Manoil 
66520862788SClaudiu Manoil 	for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
66620862788SClaudiu Manoil 		grp->num_tx_queues++;
66720862788SClaudiu Manoil 		grp->tstat |= (TSTAT_CLEAR_THALT >> i);
66820862788SClaudiu Manoil 		priv->tqueue |= (TQUEUE_EN0 >> i);
66920862788SClaudiu Manoil 		priv->tx_queue[i]->grp = grp;
67020862788SClaudiu Manoil 	}
67120862788SClaudiu Manoil 
672ec21e2ecSJeff Kirsher 	priv->num_grps++;
673ec21e2ecSJeff Kirsher 
674ec21e2ecSJeff Kirsher 	return 0;
675ec21e2ecSJeff Kirsher }
676ec21e2ecSJeff Kirsher 
677ec21e2ecSJeff Kirsher static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
678ec21e2ecSJeff Kirsher {
679ec21e2ecSJeff Kirsher 	const char *model;
680ec21e2ecSJeff Kirsher 	const char *ctype;
681ec21e2ecSJeff Kirsher 	const void *mac_addr;
682ec21e2ecSJeff Kirsher 	int err = 0, i;
683ec21e2ecSJeff Kirsher 	struct net_device *dev = NULL;
684ec21e2ecSJeff Kirsher 	struct gfar_private *priv = NULL;
685ec21e2ecSJeff Kirsher 	struct device_node *np = ofdev->dev.of_node;
686ec21e2ecSJeff Kirsher 	struct device_node *child = NULL;
687ec21e2ecSJeff Kirsher 	const u32 *stash;
688ec21e2ecSJeff Kirsher 	const u32 *stash_len;
689ec21e2ecSJeff Kirsher 	const u32 *stash_idx;
690ec21e2ecSJeff Kirsher 	unsigned int num_tx_qs, num_rx_qs;
691ec21e2ecSJeff Kirsher 	u32 *tx_queues, *rx_queues;
692ec21e2ecSJeff Kirsher 
693ec21e2ecSJeff Kirsher 	if (!np || !of_device_is_available(np))
694ec21e2ecSJeff Kirsher 		return -ENODEV;
695ec21e2ecSJeff Kirsher 
696ec21e2ecSJeff Kirsher 	/* parse the num of tx and rx queues */
697ec21e2ecSJeff Kirsher 	tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
698ec21e2ecSJeff Kirsher 	num_tx_qs = tx_queues ? *tx_queues : 1;
699ec21e2ecSJeff Kirsher 
700ec21e2ecSJeff Kirsher 	if (num_tx_qs > MAX_TX_QS) {
701ec21e2ecSJeff Kirsher 		pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
702ec21e2ecSJeff Kirsher 		       num_tx_qs, MAX_TX_QS);
703ec21e2ecSJeff Kirsher 		pr_err("Cannot do alloc_etherdev, aborting\n");
704ec21e2ecSJeff Kirsher 		return -EINVAL;
705ec21e2ecSJeff Kirsher 	}
706ec21e2ecSJeff Kirsher 
707ec21e2ecSJeff Kirsher 	rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
708ec21e2ecSJeff Kirsher 	num_rx_qs = rx_queues ? *rx_queues : 1;
709ec21e2ecSJeff Kirsher 
710ec21e2ecSJeff Kirsher 	if (num_rx_qs > MAX_RX_QS) {
711ec21e2ecSJeff Kirsher 		pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
712ec21e2ecSJeff Kirsher 		       num_rx_qs, MAX_RX_QS);
713ec21e2ecSJeff Kirsher 		pr_err("Cannot do alloc_etherdev, aborting\n");
714ec21e2ecSJeff Kirsher 		return -EINVAL;
715ec21e2ecSJeff Kirsher 	}
716ec21e2ecSJeff Kirsher 
717ec21e2ecSJeff Kirsher 	*pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
718ec21e2ecSJeff Kirsher 	dev = *pdev;
719ec21e2ecSJeff Kirsher 	if (NULL == dev)
720ec21e2ecSJeff Kirsher 		return -ENOMEM;
721ec21e2ecSJeff Kirsher 
722ec21e2ecSJeff Kirsher 	priv = netdev_priv(dev);
723ec21e2ecSJeff Kirsher 	priv->ndev = dev;
724ec21e2ecSJeff Kirsher 
725ec21e2ecSJeff Kirsher 	priv->num_tx_queues = num_tx_qs;
726ec21e2ecSJeff Kirsher 	netif_set_real_num_rx_queues(dev, num_rx_qs);
727ec21e2ecSJeff Kirsher 	priv->num_rx_queues = num_rx_qs;
72820862788SClaudiu Manoil 
72920862788SClaudiu Manoil 	err = gfar_alloc_tx_queues(priv);
73020862788SClaudiu Manoil 	if (err)
73120862788SClaudiu Manoil 		goto tx_alloc_failed;
73220862788SClaudiu Manoil 
73320862788SClaudiu Manoil 	err = gfar_alloc_rx_queues(priv);
73420862788SClaudiu Manoil 	if (err)
73520862788SClaudiu Manoil 		goto rx_alloc_failed;
736ec21e2ecSJeff Kirsher 
737ec21e2ecSJeff Kirsher 	/* Init Rx queue filer rule set linked list */
738ec21e2ecSJeff Kirsher 	INIT_LIST_HEAD(&priv->rx_list.list);
739ec21e2ecSJeff Kirsher 	priv->rx_list.count = 0;
740ec21e2ecSJeff Kirsher 	mutex_init(&priv->rx_queue_access);
741ec21e2ecSJeff Kirsher 
742ec21e2ecSJeff Kirsher 	model = of_get_property(np, "model", NULL);
743ec21e2ecSJeff Kirsher 
744ec21e2ecSJeff Kirsher 	for (i = 0; i < MAXGROUPS; i++)
745ec21e2ecSJeff Kirsher 		priv->gfargrp[i].regs = NULL;
746ec21e2ecSJeff Kirsher 
747ec21e2ecSJeff Kirsher 	/* Parse and initialize group specific information */
748ec21e2ecSJeff Kirsher 	if (of_device_is_compatible(np, "fsl,etsec2")) {
749ec21e2ecSJeff Kirsher 		priv->mode = MQ_MG_MODE;
750ec21e2ecSJeff Kirsher 		for_each_child_of_node(np, child) {
751ec21e2ecSJeff Kirsher 			err = gfar_parse_group(child, priv, model);
752ec21e2ecSJeff Kirsher 			if (err)
753ec21e2ecSJeff Kirsher 				goto err_grp_init;
754ec21e2ecSJeff Kirsher 		}
755ec21e2ecSJeff Kirsher 	} else {
756ec21e2ecSJeff Kirsher 		priv->mode = SQ_SG_MODE;
757ec21e2ecSJeff Kirsher 		err = gfar_parse_group(np, priv, model);
758ec21e2ecSJeff Kirsher 		if (err)
759ec21e2ecSJeff Kirsher 			goto err_grp_init;
760ec21e2ecSJeff Kirsher 	}
761ec21e2ecSJeff Kirsher 
762ec21e2ecSJeff Kirsher 	stash = of_get_property(np, "bd-stash", NULL);
763ec21e2ecSJeff Kirsher 
764ec21e2ecSJeff Kirsher 	if (stash) {
765ec21e2ecSJeff Kirsher 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
766ec21e2ecSJeff Kirsher 		priv->bd_stash_en = 1;
767ec21e2ecSJeff Kirsher 	}
768ec21e2ecSJeff Kirsher 
769ec21e2ecSJeff Kirsher 	stash_len = of_get_property(np, "rx-stash-len", NULL);
770ec21e2ecSJeff Kirsher 
771ec21e2ecSJeff Kirsher 	if (stash_len)
772ec21e2ecSJeff Kirsher 		priv->rx_stash_size = *stash_len;
773ec21e2ecSJeff Kirsher 
774ec21e2ecSJeff Kirsher 	stash_idx = of_get_property(np, "rx-stash-idx", NULL);
775ec21e2ecSJeff Kirsher 
776ec21e2ecSJeff Kirsher 	if (stash_idx)
777ec21e2ecSJeff Kirsher 		priv->rx_stash_index = *stash_idx;
778ec21e2ecSJeff Kirsher 
779ec21e2ecSJeff Kirsher 	if (stash_len || stash_idx)
780ec21e2ecSJeff Kirsher 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
781ec21e2ecSJeff Kirsher 
782ec21e2ecSJeff Kirsher 	mac_addr = of_get_mac_address(np);
783bc4598bcSJan Ceuleers 
784ec21e2ecSJeff Kirsher 	if (mac_addr)
7856a3c910cSJoe Perches 		memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
786ec21e2ecSJeff Kirsher 
787ec21e2ecSJeff Kirsher 	if (model && !strcasecmp(model, "TSEC"))
78834018fd4SClaudiu Manoil 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
789ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_COALESCE |
790ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_RMON |
791ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_MULTI_INTR;
792bc4598bcSJan Ceuleers 
793ec21e2ecSJeff Kirsher 	if (model && !strcasecmp(model, "eTSEC"))
79434018fd4SClaudiu Manoil 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
795ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_COALESCE |
796ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_RMON |
797ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_MULTI_INTR |
798ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_CSUM |
799ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_VLAN |
800ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
801ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
802ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_TIMER;
803ec21e2ecSJeff Kirsher 
804ec21e2ecSJeff Kirsher 	ctype = of_get_property(np, "phy-connection-type", NULL);
805ec21e2ecSJeff Kirsher 
806ec21e2ecSJeff Kirsher 	/* We only care about rgmii-id.  The rest are autodetected */
807ec21e2ecSJeff Kirsher 	if (ctype && !strcmp(ctype, "rgmii-id"))
808ec21e2ecSJeff Kirsher 		priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
809ec21e2ecSJeff Kirsher 	else
810ec21e2ecSJeff Kirsher 		priv->interface = PHY_INTERFACE_MODE_MII;
811ec21e2ecSJeff Kirsher 
812ec21e2ecSJeff Kirsher 	if (of_get_property(np, "fsl,magic-packet", NULL))
813ec21e2ecSJeff Kirsher 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
814ec21e2ecSJeff Kirsher 
815ec21e2ecSJeff Kirsher 	priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
816ec21e2ecSJeff Kirsher 
817ec21e2ecSJeff Kirsher 	/* Find the TBI PHY.  If it's not there, we don't support SGMII */
818ec21e2ecSJeff Kirsher 	priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
819ec21e2ecSJeff Kirsher 
820ec21e2ecSJeff Kirsher 	return 0;
821ec21e2ecSJeff Kirsher 
822ec21e2ecSJeff Kirsher err_grp_init:
823ec21e2ecSJeff Kirsher 	unmap_group_regs(priv);
82420862788SClaudiu Manoil rx_alloc_failed:
82520862788SClaudiu Manoil 	gfar_free_rx_queues(priv);
82620862788SClaudiu Manoil tx_alloc_failed:
82720862788SClaudiu Manoil 	gfar_free_tx_queues(priv);
828ee873fdaSClaudiu Manoil 	free_gfar_dev(priv);
829ec21e2ecSJeff Kirsher 	return err;
830ec21e2ecSJeff Kirsher }
831ec21e2ecSJeff Kirsher 
832ca0c88c2SBen Hutchings static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
833ec21e2ecSJeff Kirsher {
834ec21e2ecSJeff Kirsher 	struct hwtstamp_config config;
835ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(netdev);
836ec21e2ecSJeff Kirsher 
837ec21e2ecSJeff Kirsher 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
838ec21e2ecSJeff Kirsher 		return -EFAULT;
839ec21e2ecSJeff Kirsher 
840ec21e2ecSJeff Kirsher 	/* reserved for future extensions */
841ec21e2ecSJeff Kirsher 	if (config.flags)
842ec21e2ecSJeff Kirsher 		return -EINVAL;
843ec21e2ecSJeff Kirsher 
844ec21e2ecSJeff Kirsher 	switch (config.tx_type) {
845ec21e2ecSJeff Kirsher 	case HWTSTAMP_TX_OFF:
846ec21e2ecSJeff Kirsher 		priv->hwts_tx_en = 0;
847ec21e2ecSJeff Kirsher 		break;
848ec21e2ecSJeff Kirsher 	case HWTSTAMP_TX_ON:
849ec21e2ecSJeff Kirsher 		if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
850ec21e2ecSJeff Kirsher 			return -ERANGE;
851ec21e2ecSJeff Kirsher 		priv->hwts_tx_en = 1;
852ec21e2ecSJeff Kirsher 		break;
853ec21e2ecSJeff Kirsher 	default:
854ec21e2ecSJeff Kirsher 		return -ERANGE;
855ec21e2ecSJeff Kirsher 	}
856ec21e2ecSJeff Kirsher 
857ec21e2ecSJeff Kirsher 	switch (config.rx_filter) {
858ec21e2ecSJeff Kirsher 	case HWTSTAMP_FILTER_NONE:
859ec21e2ecSJeff Kirsher 		if (priv->hwts_rx_en) {
860ec21e2ecSJeff Kirsher 			stop_gfar(netdev);
861ec21e2ecSJeff Kirsher 			priv->hwts_rx_en = 0;
862ec21e2ecSJeff Kirsher 			startup_gfar(netdev);
863ec21e2ecSJeff Kirsher 		}
864ec21e2ecSJeff Kirsher 		break;
865ec21e2ecSJeff Kirsher 	default:
866ec21e2ecSJeff Kirsher 		if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
867ec21e2ecSJeff Kirsher 			return -ERANGE;
868ec21e2ecSJeff Kirsher 		if (!priv->hwts_rx_en) {
869ec21e2ecSJeff Kirsher 			stop_gfar(netdev);
870ec21e2ecSJeff Kirsher 			priv->hwts_rx_en = 1;
871ec21e2ecSJeff Kirsher 			startup_gfar(netdev);
872ec21e2ecSJeff Kirsher 		}
873ec21e2ecSJeff Kirsher 		config.rx_filter = HWTSTAMP_FILTER_ALL;
874ec21e2ecSJeff Kirsher 		break;
875ec21e2ecSJeff Kirsher 	}
876ec21e2ecSJeff Kirsher 
877ec21e2ecSJeff Kirsher 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
878ec21e2ecSJeff Kirsher 		-EFAULT : 0;
879ec21e2ecSJeff Kirsher }
880ec21e2ecSJeff Kirsher 
881ca0c88c2SBen Hutchings static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
882ca0c88c2SBen Hutchings {
883ca0c88c2SBen Hutchings 	struct hwtstamp_config config;
884ca0c88c2SBen Hutchings 	struct gfar_private *priv = netdev_priv(netdev);
885ca0c88c2SBen Hutchings 
886ca0c88c2SBen Hutchings 	config.flags = 0;
887ca0c88c2SBen Hutchings 	config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
888ca0c88c2SBen Hutchings 	config.rx_filter = (priv->hwts_rx_en ?
889ca0c88c2SBen Hutchings 			    HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
890ca0c88c2SBen Hutchings 
891ca0c88c2SBen Hutchings 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
892ca0c88c2SBen Hutchings 		-EFAULT : 0;
893ca0c88c2SBen Hutchings }
894ca0c88c2SBen Hutchings 
895ec21e2ecSJeff Kirsher static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
896ec21e2ecSJeff Kirsher {
897ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
898ec21e2ecSJeff Kirsher 
899ec21e2ecSJeff Kirsher 	if (!netif_running(dev))
900ec21e2ecSJeff Kirsher 		return -EINVAL;
901ec21e2ecSJeff Kirsher 
902ec21e2ecSJeff Kirsher 	if (cmd == SIOCSHWTSTAMP)
903ca0c88c2SBen Hutchings 		return gfar_hwtstamp_set(dev, rq);
904ca0c88c2SBen Hutchings 	if (cmd == SIOCGHWTSTAMP)
905ca0c88c2SBen Hutchings 		return gfar_hwtstamp_get(dev, rq);
906ec21e2ecSJeff Kirsher 
907ec21e2ecSJeff Kirsher 	if (!priv->phydev)
908ec21e2ecSJeff Kirsher 		return -ENODEV;
909ec21e2ecSJeff Kirsher 
910ec21e2ecSJeff Kirsher 	return phy_mii_ioctl(priv->phydev, rq, cmd);
911ec21e2ecSJeff Kirsher }
912ec21e2ecSJeff Kirsher 
913ec21e2ecSJeff Kirsher static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
914ec21e2ecSJeff Kirsher 				   u32 class)
915ec21e2ecSJeff Kirsher {
916ec21e2ecSJeff Kirsher 	u32 rqfpr = FPR_FILER_MASK;
917ec21e2ecSJeff Kirsher 	u32 rqfcr = 0x0;
918ec21e2ecSJeff Kirsher 
919ec21e2ecSJeff Kirsher 	rqfar--;
920ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
921ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
922ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
923ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
924ec21e2ecSJeff Kirsher 
925ec21e2ecSJeff Kirsher 	rqfar--;
926ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_NOMATCH;
927ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
928ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
929ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
930ec21e2ecSJeff Kirsher 
931ec21e2ecSJeff Kirsher 	rqfar--;
932ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
933ec21e2ecSJeff Kirsher 	rqfpr = class;
934ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
935ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
936ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
937ec21e2ecSJeff Kirsher 
938ec21e2ecSJeff Kirsher 	rqfar--;
939ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
940ec21e2ecSJeff Kirsher 	rqfpr = class;
941ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
942ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
943ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
944ec21e2ecSJeff Kirsher 
945ec21e2ecSJeff Kirsher 	return rqfar;
946ec21e2ecSJeff Kirsher }
947ec21e2ecSJeff Kirsher 
948ec21e2ecSJeff Kirsher static void gfar_init_filer_table(struct gfar_private *priv)
949ec21e2ecSJeff Kirsher {
950ec21e2ecSJeff Kirsher 	int i = 0x0;
951ec21e2ecSJeff Kirsher 	u32 rqfar = MAX_FILER_IDX;
952ec21e2ecSJeff Kirsher 	u32 rqfcr = 0x0;
953ec21e2ecSJeff Kirsher 	u32 rqfpr = FPR_FILER_MASK;
954ec21e2ecSJeff Kirsher 
955ec21e2ecSJeff Kirsher 	/* Default rule */
956ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_MATCH;
957ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
958ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
959ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
960ec21e2ecSJeff Kirsher 
961ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
962ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
963ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
964ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
965ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
966ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
967ec21e2ecSJeff Kirsher 
968ec21e2ecSJeff Kirsher 	/* cur_filer_idx indicated the first non-masked rule */
969ec21e2ecSJeff Kirsher 	priv->cur_filer_idx = rqfar;
970ec21e2ecSJeff Kirsher 
971ec21e2ecSJeff Kirsher 	/* Rest are masked rules */
972ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_NOMATCH;
973ec21e2ecSJeff Kirsher 	for (i = 0; i < rqfar; i++) {
974ec21e2ecSJeff Kirsher 		priv->ftp_rqfcr[i] = rqfcr;
975ec21e2ecSJeff Kirsher 		priv->ftp_rqfpr[i] = rqfpr;
976ec21e2ecSJeff Kirsher 		gfar_write_filer(priv, i, rqfcr, rqfpr);
977ec21e2ecSJeff Kirsher 	}
978ec21e2ecSJeff Kirsher }
979ec21e2ecSJeff Kirsher 
9802969b1f7SClaudiu Manoil static void __gfar_detect_errata_83xx(struct gfar_private *priv)
981ec21e2ecSJeff Kirsher {
982ec21e2ecSJeff Kirsher 	unsigned int pvr = mfspr(SPRN_PVR);
983ec21e2ecSJeff Kirsher 	unsigned int svr = mfspr(SPRN_SVR);
984ec21e2ecSJeff Kirsher 	unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
985ec21e2ecSJeff Kirsher 	unsigned int rev = svr & 0xffff;
986ec21e2ecSJeff Kirsher 
987ec21e2ecSJeff Kirsher 	/* MPC8313 Rev 2.0 and higher; All MPC837x */
988ec21e2ecSJeff Kirsher 	if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
989ec21e2ecSJeff Kirsher 	    (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
990ec21e2ecSJeff Kirsher 		priv->errata |= GFAR_ERRATA_74;
991ec21e2ecSJeff Kirsher 
992ec21e2ecSJeff Kirsher 	/* MPC8313 and MPC837x all rev */
993ec21e2ecSJeff Kirsher 	if ((pvr == 0x80850010 && mod == 0x80b0) ||
994ec21e2ecSJeff Kirsher 	    (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
995ec21e2ecSJeff Kirsher 		priv->errata |= GFAR_ERRATA_76;
996ec21e2ecSJeff Kirsher 
9972969b1f7SClaudiu Manoil 	/* MPC8313 Rev < 2.0 */
9982969b1f7SClaudiu Manoil 	if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
999ec21e2ecSJeff Kirsher 		priv->errata |= GFAR_ERRATA_12;
10002969b1f7SClaudiu Manoil }
10012969b1f7SClaudiu Manoil 
10022969b1f7SClaudiu Manoil static void __gfar_detect_errata_85xx(struct gfar_private *priv)
10032969b1f7SClaudiu Manoil {
10042969b1f7SClaudiu Manoil 	unsigned int svr = mfspr(SPRN_SVR);
10052969b1f7SClaudiu Manoil 
10062969b1f7SClaudiu Manoil 	if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
10072969b1f7SClaudiu Manoil 		priv->errata |= GFAR_ERRATA_12;
100853fad773SClaudiu Manoil 	if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
100953fad773SClaudiu Manoil 	    ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
101053fad773SClaudiu Manoil 		priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
10112969b1f7SClaudiu Manoil }
10122969b1f7SClaudiu Manoil 
10132969b1f7SClaudiu Manoil static void gfar_detect_errata(struct gfar_private *priv)
10142969b1f7SClaudiu Manoil {
10152969b1f7SClaudiu Manoil 	struct device *dev = &priv->ofdev->dev;
10162969b1f7SClaudiu Manoil 
10172969b1f7SClaudiu Manoil 	/* no plans to fix */
10182969b1f7SClaudiu Manoil 	priv->errata |= GFAR_ERRATA_A002;
10192969b1f7SClaudiu Manoil 
10202969b1f7SClaudiu Manoil 	if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
10212969b1f7SClaudiu Manoil 		__gfar_detect_errata_85xx(priv);
10222969b1f7SClaudiu Manoil 	else /* non-mpc85xx parts, i.e. e300 core based */
10232969b1f7SClaudiu Manoil 		__gfar_detect_errata_83xx(priv);
1024ec21e2ecSJeff Kirsher 
1025ec21e2ecSJeff Kirsher 	if (priv->errata)
1026ec21e2ecSJeff Kirsher 		dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1027ec21e2ecSJeff Kirsher 			 priv->errata);
1028ec21e2ecSJeff Kirsher }
1029ec21e2ecSJeff Kirsher 
1030a328ac92SClaudiu Manoil static void gfar_mac_reset(struct gfar_private *priv)
1031ec21e2ecSJeff Kirsher {
103220862788SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1033a328ac92SClaudiu Manoil 	u32 tempval;
1034ec21e2ecSJeff Kirsher 
1035ec21e2ecSJeff Kirsher 	/* Reset MAC layer */
1036ec21e2ecSJeff Kirsher 	gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
1037ec21e2ecSJeff Kirsher 
1038ec21e2ecSJeff Kirsher 	/* We need to delay at least 3 TX clocks */
1039a328ac92SClaudiu Manoil 	udelay(3);
1040ec21e2ecSJeff Kirsher 
104123402bddSClaudiu Manoil 	/* the soft reset bit is not self-resetting, so we need to
104223402bddSClaudiu Manoil 	 * clear it before resuming normal operation
104323402bddSClaudiu Manoil 	 */
104420862788SClaudiu Manoil 	gfar_write(&regs->maccfg1, 0);
1045ec21e2ecSJeff Kirsher 
1046a328ac92SClaudiu Manoil 	udelay(3);
1047a328ac92SClaudiu Manoil 
104888302648SClaudiu Manoil 	/* Compute rx_buff_size based on config flags */
104988302648SClaudiu Manoil 	gfar_rx_buff_size_config(priv);
105088302648SClaudiu Manoil 
105188302648SClaudiu Manoil 	/* Initialize the max receive frame/buffer lengths */
105288302648SClaudiu Manoil 	gfar_write(&regs->maxfrm, priv->rx_buffer_size);
1053a328ac92SClaudiu Manoil 	gfar_write(&regs->mrblr, priv->rx_buffer_size);
1054a328ac92SClaudiu Manoil 
1055a328ac92SClaudiu Manoil 	/* Initialize the Minimum Frame Length Register */
1056a328ac92SClaudiu Manoil 	gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1057a328ac92SClaudiu Manoil 
1058ec21e2ecSJeff Kirsher 	/* Initialize MACCFG2. */
1059ec21e2ecSJeff Kirsher 	tempval = MACCFG2_INIT_SETTINGS;
106088302648SClaudiu Manoil 
106188302648SClaudiu Manoil 	/* If the mtu is larger than the max size for standard
106288302648SClaudiu Manoil 	 * ethernet frames (ie, a jumbo frame), then set maccfg2
106388302648SClaudiu Manoil 	 * to allow huge frames, and to check the length
106488302648SClaudiu Manoil 	 */
106588302648SClaudiu Manoil 	if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
106688302648SClaudiu Manoil 	    gfar_has_errata(priv, GFAR_ERRATA_74))
1067ec21e2ecSJeff Kirsher 		tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
106888302648SClaudiu Manoil 
1069ec21e2ecSJeff Kirsher 	gfar_write(&regs->maccfg2, tempval);
1070ec21e2ecSJeff Kirsher 
1071a328ac92SClaudiu Manoil 	/* Clear mac addr hash registers */
1072a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr0, 0);
1073a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr1, 0);
1074a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr2, 0);
1075a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr3, 0);
1076a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr4, 0);
1077a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr5, 0);
1078a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr6, 0);
1079a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr7, 0);
1080a328ac92SClaudiu Manoil 
1081a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr0, 0);
1082a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr1, 0);
1083a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr2, 0);
1084a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr3, 0);
1085a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr4, 0);
1086a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr5, 0);
1087a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr6, 0);
1088a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr7, 0);
1089a328ac92SClaudiu Manoil 
1090a328ac92SClaudiu Manoil 	if (priv->extended_hash)
1091a328ac92SClaudiu Manoil 		gfar_clear_exact_match(priv->ndev);
1092a328ac92SClaudiu Manoil 
1093a328ac92SClaudiu Manoil 	gfar_mac_rx_config(priv);
1094a328ac92SClaudiu Manoil 
1095a328ac92SClaudiu Manoil 	gfar_mac_tx_config(priv);
1096a328ac92SClaudiu Manoil 
1097a328ac92SClaudiu Manoil 	gfar_set_mac_address(priv->ndev);
1098a328ac92SClaudiu Manoil 
1099a328ac92SClaudiu Manoil 	gfar_set_multi(priv->ndev);
1100a328ac92SClaudiu Manoil 
1101a328ac92SClaudiu Manoil 	/* clear ievent and imask before configuring coalescing */
1102a328ac92SClaudiu Manoil 	gfar_ints_disable(priv);
1103a328ac92SClaudiu Manoil 
1104a328ac92SClaudiu Manoil 	/* Configure the coalescing support */
1105a328ac92SClaudiu Manoil 	gfar_configure_coalescing_all(priv);
1106a328ac92SClaudiu Manoil }
1107a328ac92SClaudiu Manoil 
1108a328ac92SClaudiu Manoil static void gfar_hw_init(struct gfar_private *priv)
1109a328ac92SClaudiu Manoil {
1110a328ac92SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1111a328ac92SClaudiu Manoil 	u32 attrs;
1112a328ac92SClaudiu Manoil 
1113a328ac92SClaudiu Manoil 	/* Stop the DMA engine now, in case it was running before
1114a328ac92SClaudiu Manoil 	 * (The firmware could have used it, and left it running).
1115a328ac92SClaudiu Manoil 	 */
1116a328ac92SClaudiu Manoil 	gfar_halt(priv);
1117a328ac92SClaudiu Manoil 
1118a328ac92SClaudiu Manoil 	gfar_mac_reset(priv);
1119a328ac92SClaudiu Manoil 
1120a328ac92SClaudiu Manoil 	/* Zero out the rmon mib registers if it has them */
1121a328ac92SClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1122a328ac92SClaudiu Manoil 		memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
1123a328ac92SClaudiu Manoil 
1124a328ac92SClaudiu Manoil 		/* Mask off the CAM interrupts */
1125a328ac92SClaudiu Manoil 		gfar_write(&regs->rmon.cam1, 0xffffffff);
1126a328ac92SClaudiu Manoil 		gfar_write(&regs->rmon.cam2, 0xffffffff);
1127a328ac92SClaudiu Manoil 	}
1128a328ac92SClaudiu Manoil 
1129ec21e2ecSJeff Kirsher 	/* Initialize ECNTRL */
1130ec21e2ecSJeff Kirsher 	gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
1131ec21e2ecSJeff Kirsher 
113234018fd4SClaudiu Manoil 	/* Set the extraction length and index */
113334018fd4SClaudiu Manoil 	attrs = ATTRELI_EL(priv->rx_stash_size) |
113434018fd4SClaudiu Manoil 		ATTRELI_EI(priv->rx_stash_index);
113534018fd4SClaudiu Manoil 
113634018fd4SClaudiu Manoil 	gfar_write(&regs->attreli, attrs);
113734018fd4SClaudiu Manoil 
113834018fd4SClaudiu Manoil 	/* Start with defaults, and add stashing
113934018fd4SClaudiu Manoil 	 * depending on driver parameters
114034018fd4SClaudiu Manoil 	 */
114134018fd4SClaudiu Manoil 	attrs = ATTR_INIT_SETTINGS;
114234018fd4SClaudiu Manoil 
114334018fd4SClaudiu Manoil 	if (priv->bd_stash_en)
114434018fd4SClaudiu Manoil 		attrs |= ATTR_BDSTASH;
114534018fd4SClaudiu Manoil 
114634018fd4SClaudiu Manoil 	if (priv->rx_stash_size != 0)
114734018fd4SClaudiu Manoil 		attrs |= ATTR_BUFSTASH;
114834018fd4SClaudiu Manoil 
114934018fd4SClaudiu Manoil 	gfar_write(&regs->attr, attrs);
115034018fd4SClaudiu Manoil 
115134018fd4SClaudiu Manoil 	/* FIFO configs */
115234018fd4SClaudiu Manoil 	gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
115334018fd4SClaudiu Manoil 	gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
115434018fd4SClaudiu Manoil 	gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
115534018fd4SClaudiu Manoil 
115620862788SClaudiu Manoil 	/* Program the interrupt steering regs, only for MG devices */
115720862788SClaudiu Manoil 	if (priv->num_grps > 1)
115820862788SClaudiu Manoil 		gfar_write_isrg(priv);
1159ec21e2ecSJeff Kirsher }
1160ec21e2ecSJeff Kirsher 
116120862788SClaudiu Manoil static void __init gfar_init_addr_hash_table(struct gfar_private *priv)
116220862788SClaudiu Manoil {
116320862788SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1164ec21e2ecSJeff Kirsher 
1165ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
1166ec21e2ecSJeff Kirsher 		priv->extended_hash = 1;
1167ec21e2ecSJeff Kirsher 		priv->hash_width = 9;
1168ec21e2ecSJeff Kirsher 
1169ec21e2ecSJeff Kirsher 		priv->hash_regs[0] = &regs->igaddr0;
1170ec21e2ecSJeff Kirsher 		priv->hash_regs[1] = &regs->igaddr1;
1171ec21e2ecSJeff Kirsher 		priv->hash_regs[2] = &regs->igaddr2;
1172ec21e2ecSJeff Kirsher 		priv->hash_regs[3] = &regs->igaddr3;
1173ec21e2ecSJeff Kirsher 		priv->hash_regs[4] = &regs->igaddr4;
1174ec21e2ecSJeff Kirsher 		priv->hash_regs[5] = &regs->igaddr5;
1175ec21e2ecSJeff Kirsher 		priv->hash_regs[6] = &regs->igaddr6;
1176ec21e2ecSJeff Kirsher 		priv->hash_regs[7] = &regs->igaddr7;
1177ec21e2ecSJeff Kirsher 		priv->hash_regs[8] = &regs->gaddr0;
1178ec21e2ecSJeff Kirsher 		priv->hash_regs[9] = &regs->gaddr1;
1179ec21e2ecSJeff Kirsher 		priv->hash_regs[10] = &regs->gaddr2;
1180ec21e2ecSJeff Kirsher 		priv->hash_regs[11] = &regs->gaddr3;
1181ec21e2ecSJeff Kirsher 		priv->hash_regs[12] = &regs->gaddr4;
1182ec21e2ecSJeff Kirsher 		priv->hash_regs[13] = &regs->gaddr5;
1183ec21e2ecSJeff Kirsher 		priv->hash_regs[14] = &regs->gaddr6;
1184ec21e2ecSJeff Kirsher 		priv->hash_regs[15] = &regs->gaddr7;
1185ec21e2ecSJeff Kirsher 
1186ec21e2ecSJeff Kirsher 	} else {
1187ec21e2ecSJeff Kirsher 		priv->extended_hash = 0;
1188ec21e2ecSJeff Kirsher 		priv->hash_width = 8;
1189ec21e2ecSJeff Kirsher 
1190ec21e2ecSJeff Kirsher 		priv->hash_regs[0] = &regs->gaddr0;
1191ec21e2ecSJeff Kirsher 		priv->hash_regs[1] = &regs->gaddr1;
1192ec21e2ecSJeff Kirsher 		priv->hash_regs[2] = &regs->gaddr2;
1193ec21e2ecSJeff Kirsher 		priv->hash_regs[3] = &regs->gaddr3;
1194ec21e2ecSJeff Kirsher 		priv->hash_regs[4] = &regs->gaddr4;
1195ec21e2ecSJeff Kirsher 		priv->hash_regs[5] = &regs->gaddr5;
1196ec21e2ecSJeff Kirsher 		priv->hash_regs[6] = &regs->gaddr6;
1197ec21e2ecSJeff Kirsher 		priv->hash_regs[7] = &regs->gaddr7;
1198ec21e2ecSJeff Kirsher 	}
119920862788SClaudiu Manoil }
120020862788SClaudiu Manoil 
120120862788SClaudiu Manoil /* Set up the ethernet device structure, private data,
120220862788SClaudiu Manoil  * and anything else we need before we start
120320862788SClaudiu Manoil  */
120420862788SClaudiu Manoil static int gfar_probe(struct platform_device *ofdev)
120520862788SClaudiu Manoil {
120620862788SClaudiu Manoil 	struct net_device *dev = NULL;
120720862788SClaudiu Manoil 	struct gfar_private *priv = NULL;
120820862788SClaudiu Manoil 	int err = 0, i;
120920862788SClaudiu Manoil 
121020862788SClaudiu Manoil 	err = gfar_of_init(ofdev, &dev);
121120862788SClaudiu Manoil 
121220862788SClaudiu Manoil 	if (err)
121320862788SClaudiu Manoil 		return err;
121420862788SClaudiu Manoil 
121520862788SClaudiu Manoil 	priv = netdev_priv(dev);
121620862788SClaudiu Manoil 	priv->ndev = dev;
121720862788SClaudiu Manoil 	priv->ofdev = ofdev;
121820862788SClaudiu Manoil 	priv->dev = &ofdev->dev;
121920862788SClaudiu Manoil 	SET_NETDEV_DEV(dev, &ofdev->dev);
122020862788SClaudiu Manoil 
122120862788SClaudiu Manoil 	spin_lock_init(&priv->bflock);
122220862788SClaudiu Manoil 	INIT_WORK(&priv->reset_task, gfar_reset_task);
122320862788SClaudiu Manoil 
122420862788SClaudiu Manoil 	platform_set_drvdata(ofdev, priv);
122520862788SClaudiu Manoil 
122620862788SClaudiu Manoil 	gfar_detect_errata(priv);
122720862788SClaudiu Manoil 
122820862788SClaudiu Manoil 	/* Set the dev->base_addr to the gfar reg region */
122920862788SClaudiu Manoil 	dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
123020862788SClaudiu Manoil 
123120862788SClaudiu Manoil 	/* Fill in the dev structure */
123220862788SClaudiu Manoil 	dev->watchdog_timeo = TX_TIMEOUT;
123320862788SClaudiu Manoil 	dev->mtu = 1500;
123420862788SClaudiu Manoil 	dev->netdev_ops = &gfar_netdev_ops;
123520862788SClaudiu Manoil 	dev->ethtool_ops = &gfar_ethtool_ops;
123620862788SClaudiu Manoil 
123720862788SClaudiu Manoil 	/* Register for napi ...We are registering NAPI for each grp */
123820862788SClaudiu Manoil 	if (priv->mode == SQ_SG_MODE)
123920862788SClaudiu Manoil 		netif_napi_add(dev, &priv->gfargrp[0].napi, gfar_poll_sq,
124020862788SClaudiu Manoil 			       GFAR_DEV_WEIGHT);
124120862788SClaudiu Manoil 	else
124220862788SClaudiu Manoil 		for (i = 0; i < priv->num_grps; i++)
124320862788SClaudiu Manoil 			netif_napi_add(dev, &priv->gfargrp[i].napi, gfar_poll,
124420862788SClaudiu Manoil 				       GFAR_DEV_WEIGHT);
124520862788SClaudiu Manoil 
124620862788SClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
124720862788SClaudiu Manoil 		dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
124820862788SClaudiu Manoil 				   NETIF_F_RXCSUM;
124920862788SClaudiu Manoil 		dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
125020862788SClaudiu Manoil 				 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
125120862788SClaudiu Manoil 	}
125220862788SClaudiu Manoil 
125320862788SClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
125420862788SClaudiu Manoil 		dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
125520862788SClaudiu Manoil 				    NETIF_F_HW_VLAN_CTAG_RX;
125620862788SClaudiu Manoil 		dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
125720862788SClaudiu Manoil 	}
125820862788SClaudiu Manoil 
125920862788SClaudiu Manoil 	gfar_init_addr_hash_table(priv);
1260ec21e2ecSJeff Kirsher 
1261532c37bcSClaudiu Manoil 	/* Insert receive time stamps into padding alignment bytes */
1262532c37bcSClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1263532c37bcSClaudiu Manoil 		priv->padding = 8;
1264ec21e2ecSJeff Kirsher 
1265ec21e2ecSJeff Kirsher 	if (dev->features & NETIF_F_IP_CSUM ||
1266ec21e2ecSJeff Kirsher 	    priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1267bee9e58cSWu Jiajun-B06378 		dev->needed_headroom = GMAC_FCB_LEN;
1268ec21e2ecSJeff Kirsher 
1269ec21e2ecSJeff Kirsher 	priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
1270ec21e2ecSJeff Kirsher 
1271ec21e2ecSJeff Kirsher 	/* Initializing some of the rx/tx queue level parameters */
1272ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
1273ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1274ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1275ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1276ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->txic = DEFAULT_TXIC;
1277ec21e2ecSJeff Kirsher 	}
1278ec21e2ecSJeff Kirsher 
1279ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
1280ec21e2ecSJeff Kirsher 		priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1281ec21e2ecSJeff Kirsher 		priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1282ec21e2ecSJeff Kirsher 		priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1283ec21e2ecSJeff Kirsher 	}
1284ec21e2ecSJeff Kirsher 
1285ec21e2ecSJeff Kirsher 	/* always enable rx filer */
1286ec21e2ecSJeff Kirsher 	priv->rx_filer_enable = 1;
1287ec21e2ecSJeff Kirsher 	/* Enable most messages by default */
1288ec21e2ecSJeff Kirsher 	priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1289b98b8babSClaudiu Manoil 	/* use pritority h/w tx queue scheduling for single queue devices */
1290b98b8babSClaudiu Manoil 	if (priv->num_tx_queues == 1)
1291b98b8babSClaudiu Manoil 		priv->prio_sched_en = 1;
1292ec21e2ecSJeff Kirsher 
1293a328ac92SClaudiu Manoil 	gfar_hw_init(priv);
1294ec21e2ecSJeff Kirsher 
1295ec21e2ecSJeff Kirsher 	err = register_netdev(dev);
1296ec21e2ecSJeff Kirsher 
1297ec21e2ecSJeff Kirsher 	if (err) {
1298ec21e2ecSJeff Kirsher 		pr_err("%s: Cannot register net device, aborting\n", dev->name);
1299ec21e2ecSJeff Kirsher 		goto register_fail;
1300ec21e2ecSJeff Kirsher 	}
1301ec21e2ecSJeff Kirsher 
1302a328ac92SClaudiu Manoil 	/* Carrier starts down, phylib will bring it up */
1303a328ac92SClaudiu Manoil 	netif_carrier_off(dev);
1304a328ac92SClaudiu Manoil 
1305ec21e2ecSJeff Kirsher 	device_init_wakeup(&dev->dev,
1306bc4598bcSJan Ceuleers 			   priv->device_flags &
1307bc4598bcSJan Ceuleers 			   FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1308ec21e2ecSJeff Kirsher 
1309ec21e2ecSJeff Kirsher 	/* fill out IRQ number and name fields */
1310ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_grps; i++) {
1311ee873fdaSClaudiu Manoil 		struct gfar_priv_grp *grp = &priv->gfargrp[i];
1312ec21e2ecSJeff Kirsher 		if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1313ee873fdaSClaudiu Manoil 			sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
13140015e551SJoe Perches 				dev->name, "_g", '0' + i, "_tx");
1315ee873fdaSClaudiu Manoil 			sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
13160015e551SJoe Perches 				dev->name, "_g", '0' + i, "_rx");
1317ee873fdaSClaudiu Manoil 			sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
13180015e551SJoe Perches 				dev->name, "_g", '0' + i, "_er");
1319ec21e2ecSJeff Kirsher 		} else
1320ee873fdaSClaudiu Manoil 			strcpy(gfar_irq(grp, TX)->name, dev->name);
1321ec21e2ecSJeff Kirsher 	}
1322ec21e2ecSJeff Kirsher 
1323ec21e2ecSJeff Kirsher 	/* Initialize the filer table */
1324ec21e2ecSJeff Kirsher 	gfar_init_filer_table(priv);
1325ec21e2ecSJeff Kirsher 
1326ec21e2ecSJeff Kirsher 	/* Print out the device info */
1327ec21e2ecSJeff Kirsher 	netdev_info(dev, "mac: %pM\n", dev->dev_addr);
1328ec21e2ecSJeff Kirsher 
13290977f817SJan Ceuleers 	/* Even more device info helps when determining which kernel
13300977f817SJan Ceuleers 	 * provided which set of benchmarks.
13310977f817SJan Ceuleers 	 */
1332ec21e2ecSJeff Kirsher 	netdev_info(dev, "Running with NAPI enabled\n");
1333ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
1334ec21e2ecSJeff Kirsher 		netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1335ec21e2ecSJeff Kirsher 			    i, priv->rx_queue[i]->rx_ring_size);
1336ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
1337ec21e2ecSJeff Kirsher 		netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1338ec21e2ecSJeff Kirsher 			    i, priv->tx_queue[i]->tx_ring_size);
1339ec21e2ecSJeff Kirsher 
1340ec21e2ecSJeff Kirsher 	return 0;
1341ec21e2ecSJeff Kirsher 
1342ec21e2ecSJeff Kirsher register_fail:
1343ec21e2ecSJeff Kirsher 	unmap_group_regs(priv);
134420862788SClaudiu Manoil 	gfar_free_rx_queues(priv);
134520862788SClaudiu Manoil 	gfar_free_tx_queues(priv);
1346ec21e2ecSJeff Kirsher 	if (priv->phy_node)
1347ec21e2ecSJeff Kirsher 		of_node_put(priv->phy_node);
1348ec21e2ecSJeff Kirsher 	if (priv->tbi_node)
1349ec21e2ecSJeff Kirsher 		of_node_put(priv->tbi_node);
1350ee873fdaSClaudiu Manoil 	free_gfar_dev(priv);
1351ec21e2ecSJeff Kirsher 	return err;
1352ec21e2ecSJeff Kirsher }
1353ec21e2ecSJeff Kirsher 
1354ec21e2ecSJeff Kirsher static int gfar_remove(struct platform_device *ofdev)
1355ec21e2ecSJeff Kirsher {
13568513fbd8SJingoo Han 	struct gfar_private *priv = platform_get_drvdata(ofdev);
1357ec21e2ecSJeff Kirsher 
1358ec21e2ecSJeff Kirsher 	if (priv->phy_node)
1359ec21e2ecSJeff Kirsher 		of_node_put(priv->phy_node);
1360ec21e2ecSJeff Kirsher 	if (priv->tbi_node)
1361ec21e2ecSJeff Kirsher 		of_node_put(priv->tbi_node);
1362ec21e2ecSJeff Kirsher 
1363ec21e2ecSJeff Kirsher 	unregister_netdev(priv->ndev);
1364ec21e2ecSJeff Kirsher 	unmap_group_regs(priv);
136520862788SClaudiu Manoil 	gfar_free_rx_queues(priv);
136620862788SClaudiu Manoil 	gfar_free_tx_queues(priv);
1367ee873fdaSClaudiu Manoil 	free_gfar_dev(priv);
1368ec21e2ecSJeff Kirsher 
1369ec21e2ecSJeff Kirsher 	return 0;
1370ec21e2ecSJeff Kirsher }
1371ec21e2ecSJeff Kirsher 
1372ec21e2ecSJeff Kirsher #ifdef CONFIG_PM
1373ec21e2ecSJeff Kirsher 
1374ec21e2ecSJeff Kirsher static int gfar_suspend(struct device *dev)
1375ec21e2ecSJeff Kirsher {
1376ec21e2ecSJeff Kirsher 	struct gfar_private *priv = dev_get_drvdata(dev);
1377ec21e2ecSJeff Kirsher 	struct net_device *ndev = priv->ndev;
1378ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1379ec21e2ecSJeff Kirsher 	unsigned long flags;
1380ec21e2ecSJeff Kirsher 	u32 tempval;
1381ec21e2ecSJeff Kirsher 
1382ec21e2ecSJeff Kirsher 	int magic_packet = priv->wol_en &&
1383bc4598bcSJan Ceuleers 			   (priv->device_flags &
1384bc4598bcSJan Ceuleers 			    FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1385ec21e2ecSJeff Kirsher 
1386ec21e2ecSJeff Kirsher 	netif_device_detach(ndev);
1387ec21e2ecSJeff Kirsher 
1388ec21e2ecSJeff Kirsher 	if (netif_running(ndev)) {
1389ec21e2ecSJeff Kirsher 
1390ec21e2ecSJeff Kirsher 		local_irq_save(flags);
1391ec21e2ecSJeff Kirsher 		lock_tx_qs(priv);
1392ec21e2ecSJeff Kirsher 		lock_rx_qs(priv);
1393ec21e2ecSJeff Kirsher 
1394c10650b6SClaudiu Manoil 		gfar_halt_nodisable(priv);
1395ec21e2ecSJeff Kirsher 
1396ec21e2ecSJeff Kirsher 		/* Disable Tx, and Rx if wake-on-LAN is disabled. */
1397ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->maccfg1);
1398ec21e2ecSJeff Kirsher 
1399ec21e2ecSJeff Kirsher 		tempval &= ~MACCFG1_TX_EN;
1400ec21e2ecSJeff Kirsher 
1401ec21e2ecSJeff Kirsher 		if (!magic_packet)
1402ec21e2ecSJeff Kirsher 			tempval &= ~MACCFG1_RX_EN;
1403ec21e2ecSJeff Kirsher 
1404ec21e2ecSJeff Kirsher 		gfar_write(&regs->maccfg1, tempval);
1405ec21e2ecSJeff Kirsher 
1406ec21e2ecSJeff Kirsher 		unlock_rx_qs(priv);
1407ec21e2ecSJeff Kirsher 		unlock_tx_qs(priv);
1408ec21e2ecSJeff Kirsher 		local_irq_restore(flags);
1409ec21e2ecSJeff Kirsher 
1410ec21e2ecSJeff Kirsher 		disable_napi(priv);
1411ec21e2ecSJeff Kirsher 
1412ec21e2ecSJeff Kirsher 		if (magic_packet) {
1413ec21e2ecSJeff Kirsher 			/* Enable interrupt on Magic Packet */
1414ec21e2ecSJeff Kirsher 			gfar_write(&regs->imask, IMASK_MAG);
1415ec21e2ecSJeff Kirsher 
1416ec21e2ecSJeff Kirsher 			/* Enable Magic Packet mode */
1417ec21e2ecSJeff Kirsher 			tempval = gfar_read(&regs->maccfg2);
1418ec21e2ecSJeff Kirsher 			tempval |= MACCFG2_MPEN;
1419ec21e2ecSJeff Kirsher 			gfar_write(&regs->maccfg2, tempval);
1420ec21e2ecSJeff Kirsher 		} else {
1421ec21e2ecSJeff Kirsher 			phy_stop(priv->phydev);
1422ec21e2ecSJeff Kirsher 		}
1423ec21e2ecSJeff Kirsher 	}
1424ec21e2ecSJeff Kirsher 
1425ec21e2ecSJeff Kirsher 	return 0;
1426ec21e2ecSJeff Kirsher }
1427ec21e2ecSJeff Kirsher 
1428ec21e2ecSJeff Kirsher static int gfar_resume(struct device *dev)
1429ec21e2ecSJeff Kirsher {
1430ec21e2ecSJeff Kirsher 	struct gfar_private *priv = dev_get_drvdata(dev);
1431ec21e2ecSJeff Kirsher 	struct net_device *ndev = priv->ndev;
1432ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1433ec21e2ecSJeff Kirsher 	unsigned long flags;
1434ec21e2ecSJeff Kirsher 	u32 tempval;
1435ec21e2ecSJeff Kirsher 	int magic_packet = priv->wol_en &&
1436bc4598bcSJan Ceuleers 			   (priv->device_flags &
1437bc4598bcSJan Ceuleers 			    FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1438ec21e2ecSJeff Kirsher 
1439ec21e2ecSJeff Kirsher 	if (!netif_running(ndev)) {
1440ec21e2ecSJeff Kirsher 		netif_device_attach(ndev);
1441ec21e2ecSJeff Kirsher 		return 0;
1442ec21e2ecSJeff Kirsher 	}
1443ec21e2ecSJeff Kirsher 
1444ec21e2ecSJeff Kirsher 	if (!magic_packet && priv->phydev)
1445ec21e2ecSJeff Kirsher 		phy_start(priv->phydev);
1446ec21e2ecSJeff Kirsher 
1447ec21e2ecSJeff Kirsher 	/* Disable Magic Packet mode, in case something
1448ec21e2ecSJeff Kirsher 	 * else woke us up.
1449ec21e2ecSJeff Kirsher 	 */
1450ec21e2ecSJeff Kirsher 	local_irq_save(flags);
1451ec21e2ecSJeff Kirsher 	lock_tx_qs(priv);
1452ec21e2ecSJeff Kirsher 	lock_rx_qs(priv);
1453ec21e2ecSJeff Kirsher 
1454ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->maccfg2);
1455ec21e2ecSJeff Kirsher 	tempval &= ~MACCFG2_MPEN;
1456ec21e2ecSJeff Kirsher 	gfar_write(&regs->maccfg2, tempval);
1457ec21e2ecSJeff Kirsher 
1458c10650b6SClaudiu Manoil 	gfar_start(priv);
1459ec21e2ecSJeff Kirsher 
1460ec21e2ecSJeff Kirsher 	unlock_rx_qs(priv);
1461ec21e2ecSJeff Kirsher 	unlock_tx_qs(priv);
1462ec21e2ecSJeff Kirsher 	local_irq_restore(flags);
1463ec21e2ecSJeff Kirsher 
1464ec21e2ecSJeff Kirsher 	netif_device_attach(ndev);
1465ec21e2ecSJeff Kirsher 
1466ec21e2ecSJeff Kirsher 	enable_napi(priv);
1467ec21e2ecSJeff Kirsher 
1468ec21e2ecSJeff Kirsher 	return 0;
1469ec21e2ecSJeff Kirsher }
1470ec21e2ecSJeff Kirsher 
1471ec21e2ecSJeff Kirsher static int gfar_restore(struct device *dev)
1472ec21e2ecSJeff Kirsher {
1473ec21e2ecSJeff Kirsher 	struct gfar_private *priv = dev_get_drvdata(dev);
1474ec21e2ecSJeff Kirsher 	struct net_device *ndev = priv->ndev;
1475ec21e2ecSJeff Kirsher 
1476103cdd1dSWang Dongsheng 	if (!netif_running(ndev)) {
1477103cdd1dSWang Dongsheng 		netif_device_attach(ndev);
1478103cdd1dSWang Dongsheng 
1479ec21e2ecSJeff Kirsher 		return 0;
1480103cdd1dSWang Dongsheng 	}
1481ec21e2ecSJeff Kirsher 
14821eb8f7a7SClaudiu Manoil 	if (gfar_init_bds(ndev)) {
14831eb8f7a7SClaudiu Manoil 		free_skb_resources(priv);
14841eb8f7a7SClaudiu Manoil 		return -ENOMEM;
14851eb8f7a7SClaudiu Manoil 	}
14861eb8f7a7SClaudiu Manoil 
1487a328ac92SClaudiu Manoil 	gfar_mac_reset(priv);
1488a328ac92SClaudiu Manoil 
1489a328ac92SClaudiu Manoil 	gfar_init_tx_rx_base(priv);
1490a328ac92SClaudiu Manoil 
1491c10650b6SClaudiu Manoil 	gfar_start(priv);
1492ec21e2ecSJeff Kirsher 
1493ec21e2ecSJeff Kirsher 	priv->oldlink = 0;
1494ec21e2ecSJeff Kirsher 	priv->oldspeed = 0;
1495ec21e2ecSJeff Kirsher 	priv->oldduplex = -1;
1496ec21e2ecSJeff Kirsher 
1497ec21e2ecSJeff Kirsher 	if (priv->phydev)
1498ec21e2ecSJeff Kirsher 		phy_start(priv->phydev);
1499ec21e2ecSJeff Kirsher 
1500ec21e2ecSJeff Kirsher 	netif_device_attach(ndev);
1501ec21e2ecSJeff Kirsher 	enable_napi(priv);
1502ec21e2ecSJeff Kirsher 
1503ec21e2ecSJeff Kirsher 	return 0;
1504ec21e2ecSJeff Kirsher }
1505ec21e2ecSJeff Kirsher 
1506ec21e2ecSJeff Kirsher static struct dev_pm_ops gfar_pm_ops = {
1507ec21e2ecSJeff Kirsher 	.suspend = gfar_suspend,
1508ec21e2ecSJeff Kirsher 	.resume = gfar_resume,
1509ec21e2ecSJeff Kirsher 	.freeze = gfar_suspend,
1510ec21e2ecSJeff Kirsher 	.thaw = gfar_resume,
1511ec21e2ecSJeff Kirsher 	.restore = gfar_restore,
1512ec21e2ecSJeff Kirsher };
1513ec21e2ecSJeff Kirsher 
1514ec21e2ecSJeff Kirsher #define GFAR_PM_OPS (&gfar_pm_ops)
1515ec21e2ecSJeff Kirsher 
1516ec21e2ecSJeff Kirsher #else
1517ec21e2ecSJeff Kirsher 
1518ec21e2ecSJeff Kirsher #define GFAR_PM_OPS NULL
1519ec21e2ecSJeff Kirsher 
1520ec21e2ecSJeff Kirsher #endif
1521ec21e2ecSJeff Kirsher 
1522ec21e2ecSJeff Kirsher /* Reads the controller's registers to determine what interface
1523ec21e2ecSJeff Kirsher  * connects it to the PHY.
1524ec21e2ecSJeff Kirsher  */
1525ec21e2ecSJeff Kirsher static phy_interface_t gfar_get_interface(struct net_device *dev)
1526ec21e2ecSJeff Kirsher {
1527ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1528ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1529ec21e2ecSJeff Kirsher 	u32 ecntrl;
1530ec21e2ecSJeff Kirsher 
1531ec21e2ecSJeff Kirsher 	ecntrl = gfar_read(&regs->ecntrl);
1532ec21e2ecSJeff Kirsher 
1533ec21e2ecSJeff Kirsher 	if (ecntrl & ECNTRL_SGMII_MODE)
1534ec21e2ecSJeff Kirsher 		return PHY_INTERFACE_MODE_SGMII;
1535ec21e2ecSJeff Kirsher 
1536ec21e2ecSJeff Kirsher 	if (ecntrl & ECNTRL_TBI_MODE) {
1537ec21e2ecSJeff Kirsher 		if (ecntrl & ECNTRL_REDUCED_MODE)
1538ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_RTBI;
1539ec21e2ecSJeff Kirsher 		else
1540ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_TBI;
1541ec21e2ecSJeff Kirsher 	}
1542ec21e2ecSJeff Kirsher 
1543ec21e2ecSJeff Kirsher 	if (ecntrl & ECNTRL_REDUCED_MODE) {
1544bc4598bcSJan Ceuleers 		if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
1545ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_RMII;
1546bc4598bcSJan Ceuleers 		}
1547ec21e2ecSJeff Kirsher 		else {
1548ec21e2ecSJeff Kirsher 			phy_interface_t interface = priv->interface;
1549ec21e2ecSJeff Kirsher 
15500977f817SJan Ceuleers 			/* This isn't autodetected right now, so it must
1551ec21e2ecSJeff Kirsher 			 * be set by the device tree or platform code.
1552ec21e2ecSJeff Kirsher 			 */
1553ec21e2ecSJeff Kirsher 			if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1554ec21e2ecSJeff Kirsher 				return PHY_INTERFACE_MODE_RGMII_ID;
1555ec21e2ecSJeff Kirsher 
1556ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_RGMII;
1557ec21e2ecSJeff Kirsher 		}
1558ec21e2ecSJeff Kirsher 	}
1559ec21e2ecSJeff Kirsher 
1560ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
1561ec21e2ecSJeff Kirsher 		return PHY_INTERFACE_MODE_GMII;
1562ec21e2ecSJeff Kirsher 
1563ec21e2ecSJeff Kirsher 	return PHY_INTERFACE_MODE_MII;
1564ec21e2ecSJeff Kirsher }
1565ec21e2ecSJeff Kirsher 
1566ec21e2ecSJeff Kirsher 
1567ec21e2ecSJeff Kirsher /* Initializes driver's PHY state, and attaches to the PHY.
1568ec21e2ecSJeff Kirsher  * Returns 0 on success.
1569ec21e2ecSJeff Kirsher  */
1570ec21e2ecSJeff Kirsher static int init_phy(struct net_device *dev)
1571ec21e2ecSJeff Kirsher {
1572ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1573ec21e2ecSJeff Kirsher 	uint gigabit_support =
1574ec21e2ecSJeff Kirsher 		priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
157523402bddSClaudiu Manoil 		GFAR_SUPPORTED_GBIT : 0;
1576ec21e2ecSJeff Kirsher 	phy_interface_t interface;
1577ec21e2ecSJeff Kirsher 
1578ec21e2ecSJeff Kirsher 	priv->oldlink = 0;
1579ec21e2ecSJeff Kirsher 	priv->oldspeed = 0;
1580ec21e2ecSJeff Kirsher 	priv->oldduplex = -1;
1581ec21e2ecSJeff Kirsher 
1582ec21e2ecSJeff Kirsher 	interface = gfar_get_interface(dev);
1583ec21e2ecSJeff Kirsher 
1584ec21e2ecSJeff Kirsher 	priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1585ec21e2ecSJeff Kirsher 				      interface);
1586ec21e2ecSJeff Kirsher 	if (!priv->phydev)
1587ec21e2ecSJeff Kirsher 		priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1588ec21e2ecSJeff Kirsher 							 interface);
1589ec21e2ecSJeff Kirsher 	if (!priv->phydev) {
1590ec21e2ecSJeff Kirsher 		dev_err(&dev->dev, "could not attach to PHY\n");
1591ec21e2ecSJeff Kirsher 		return -ENODEV;
1592ec21e2ecSJeff Kirsher 	}
1593ec21e2ecSJeff Kirsher 
1594ec21e2ecSJeff Kirsher 	if (interface == PHY_INTERFACE_MODE_SGMII)
1595ec21e2ecSJeff Kirsher 		gfar_configure_serdes(dev);
1596ec21e2ecSJeff Kirsher 
1597ec21e2ecSJeff Kirsher 	/* Remove any features not supported by the controller */
1598ec21e2ecSJeff Kirsher 	priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1599ec21e2ecSJeff Kirsher 	priv->phydev->advertising = priv->phydev->supported;
1600ec21e2ecSJeff Kirsher 
1601ec21e2ecSJeff Kirsher 	return 0;
1602ec21e2ecSJeff Kirsher }
1603ec21e2ecSJeff Kirsher 
16040977f817SJan Ceuleers /* Initialize TBI PHY interface for communicating with the
1605ec21e2ecSJeff Kirsher  * SERDES lynx PHY on the chip.  We communicate with this PHY
1606ec21e2ecSJeff Kirsher  * through the MDIO bus on each controller, treating it as a
1607ec21e2ecSJeff Kirsher  * "normal" PHY at the address found in the TBIPA register.  We assume
1608ec21e2ecSJeff Kirsher  * that the TBIPA register is valid.  Either the MDIO bus code will set
1609ec21e2ecSJeff Kirsher  * it to a value that doesn't conflict with other PHYs on the bus, or the
1610ec21e2ecSJeff Kirsher  * value doesn't matter, as there are no other PHYs on the bus.
1611ec21e2ecSJeff Kirsher  */
1612ec21e2ecSJeff Kirsher static void gfar_configure_serdes(struct net_device *dev)
1613ec21e2ecSJeff Kirsher {
1614ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1615ec21e2ecSJeff Kirsher 	struct phy_device *tbiphy;
1616ec21e2ecSJeff Kirsher 
1617ec21e2ecSJeff Kirsher 	if (!priv->tbi_node) {
1618ec21e2ecSJeff Kirsher 		dev_warn(&dev->dev, "error: SGMII mode requires that the "
1619ec21e2ecSJeff Kirsher 				    "device tree specify a tbi-handle\n");
1620ec21e2ecSJeff Kirsher 		return;
1621ec21e2ecSJeff Kirsher 	}
1622ec21e2ecSJeff Kirsher 
1623ec21e2ecSJeff Kirsher 	tbiphy = of_phy_find_device(priv->tbi_node);
1624ec21e2ecSJeff Kirsher 	if (!tbiphy) {
1625ec21e2ecSJeff Kirsher 		dev_err(&dev->dev, "error: Could not get TBI device\n");
1626ec21e2ecSJeff Kirsher 		return;
1627ec21e2ecSJeff Kirsher 	}
1628ec21e2ecSJeff Kirsher 
16290977f817SJan Ceuleers 	/* If the link is already up, we must already be ok, and don't need to
1630ec21e2ecSJeff Kirsher 	 * configure and reset the TBI<->SerDes link.  Maybe U-Boot configured
1631ec21e2ecSJeff Kirsher 	 * everything for us?  Resetting it takes the link down and requires
1632ec21e2ecSJeff Kirsher 	 * several seconds for it to come back.
1633ec21e2ecSJeff Kirsher 	 */
1634ec21e2ecSJeff Kirsher 	if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
1635ec21e2ecSJeff Kirsher 		return;
1636ec21e2ecSJeff Kirsher 
1637ec21e2ecSJeff Kirsher 	/* Single clk mode, mii mode off(for serdes communication) */
1638ec21e2ecSJeff Kirsher 	phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
1639ec21e2ecSJeff Kirsher 
1640ec21e2ecSJeff Kirsher 	phy_write(tbiphy, MII_ADVERTISE,
1641ec21e2ecSJeff Kirsher 		  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1642ec21e2ecSJeff Kirsher 		  ADVERTISE_1000XPSE_ASYM);
1643ec21e2ecSJeff Kirsher 
1644bc4598bcSJan Ceuleers 	phy_write(tbiphy, MII_BMCR,
1645bc4598bcSJan Ceuleers 		  BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1646bc4598bcSJan Ceuleers 		  BMCR_SPEED1000);
1647ec21e2ecSJeff Kirsher }
1648ec21e2ecSJeff Kirsher 
1649ec21e2ecSJeff Kirsher static int __gfar_is_rx_idle(struct gfar_private *priv)
1650ec21e2ecSJeff Kirsher {
1651ec21e2ecSJeff Kirsher 	u32 res;
1652ec21e2ecSJeff Kirsher 
16530977f817SJan Ceuleers 	/* Normaly TSEC should not hang on GRS commands, so we should
1654ec21e2ecSJeff Kirsher 	 * actually wait for IEVENT_GRSC flag.
1655ec21e2ecSJeff Kirsher 	 */
1656ad3660c2SClaudiu Manoil 	if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
1657ec21e2ecSJeff Kirsher 		return 0;
1658ec21e2ecSJeff Kirsher 
16590977f817SJan Ceuleers 	/* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
1660ec21e2ecSJeff Kirsher 	 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1661ec21e2ecSJeff Kirsher 	 * and the Rx can be safely reset.
1662ec21e2ecSJeff Kirsher 	 */
1663ec21e2ecSJeff Kirsher 	res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1664ec21e2ecSJeff Kirsher 	res &= 0x7f807f80;
1665ec21e2ecSJeff Kirsher 	if ((res & 0xffff) == (res >> 16))
1666ec21e2ecSJeff Kirsher 		return 1;
1667ec21e2ecSJeff Kirsher 
1668ec21e2ecSJeff Kirsher 	return 0;
1669ec21e2ecSJeff Kirsher }
1670ec21e2ecSJeff Kirsher 
1671ec21e2ecSJeff Kirsher /* Halt the receive and transmit queues */
1672c10650b6SClaudiu Manoil static void gfar_halt_nodisable(struct gfar_private *priv)
1673ec21e2ecSJeff Kirsher {
1674efeddce7SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1675ec21e2ecSJeff Kirsher 	u32 tempval;
1676ec21e2ecSJeff Kirsher 
1677efeddce7SClaudiu Manoil 	gfar_ints_disable(priv);
1678ec21e2ecSJeff Kirsher 
1679ec21e2ecSJeff Kirsher 	/* Stop the DMA, and wait for it to stop */
1680ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->dmactrl);
1681bc4598bcSJan Ceuleers 	if ((tempval & (DMACTRL_GRS | DMACTRL_GTS)) !=
1682bc4598bcSJan Ceuleers 	    (DMACTRL_GRS | DMACTRL_GTS)) {
1683ec21e2ecSJeff Kirsher 		int ret;
1684ec21e2ecSJeff Kirsher 
1685ec21e2ecSJeff Kirsher 		tempval |= (DMACTRL_GRS | DMACTRL_GTS);
1686ec21e2ecSJeff Kirsher 		gfar_write(&regs->dmactrl, tempval);
1687ec21e2ecSJeff Kirsher 
1688ec21e2ecSJeff Kirsher 		do {
1689ec21e2ecSJeff Kirsher 			ret = spin_event_timeout(((gfar_read(&regs->ievent) &
1690ec21e2ecSJeff Kirsher 				 (IEVENT_GRSC | IEVENT_GTSC)) ==
1691ec21e2ecSJeff Kirsher 				 (IEVENT_GRSC | IEVENT_GTSC)), 1000000, 0);
1692ec21e2ecSJeff Kirsher 			if (!ret && !(gfar_read(&regs->ievent) & IEVENT_GRSC))
1693ec21e2ecSJeff Kirsher 				ret = __gfar_is_rx_idle(priv);
1694ec21e2ecSJeff Kirsher 		} while (!ret);
1695ec21e2ecSJeff Kirsher 	}
1696ec21e2ecSJeff Kirsher }
1697ec21e2ecSJeff Kirsher 
1698ec21e2ecSJeff Kirsher /* Halt the receive and transmit queues */
1699c10650b6SClaudiu Manoil void gfar_halt(struct gfar_private *priv)
1700ec21e2ecSJeff Kirsher {
1701ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1702ec21e2ecSJeff Kirsher 	u32 tempval;
1703ec21e2ecSJeff Kirsher 
1704c10650b6SClaudiu Manoil 	/* Dissable the Rx/Tx hw queues */
1705c10650b6SClaudiu Manoil 	gfar_write(&regs->rqueue, 0);
1706c10650b6SClaudiu Manoil 	gfar_write(&regs->tqueue, 0);
1707ec21e2ecSJeff Kirsher 
1708c10650b6SClaudiu Manoil 	mdelay(10);
1709c10650b6SClaudiu Manoil 
1710c10650b6SClaudiu Manoil 	gfar_halt_nodisable(priv);
1711c10650b6SClaudiu Manoil 
1712c10650b6SClaudiu Manoil 	/* Disable Rx/Tx DMA */
1713ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->maccfg1);
1714ec21e2ecSJeff Kirsher 	tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1715ec21e2ecSJeff Kirsher 	gfar_write(&regs->maccfg1, tempval);
1716ec21e2ecSJeff Kirsher }
1717ec21e2ecSJeff Kirsher 
1718ec21e2ecSJeff Kirsher void stop_gfar(struct net_device *dev)
1719ec21e2ecSJeff Kirsher {
1720ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1721ec21e2ecSJeff Kirsher 	unsigned long flags;
1722ec21e2ecSJeff Kirsher 
1723ec21e2ecSJeff Kirsher 	phy_stop(priv->phydev);
1724ec21e2ecSJeff Kirsher 
1725ec21e2ecSJeff Kirsher 
1726ec21e2ecSJeff Kirsher 	/* Lock it down */
1727ec21e2ecSJeff Kirsher 	local_irq_save(flags);
1728ec21e2ecSJeff Kirsher 	lock_tx_qs(priv);
1729ec21e2ecSJeff Kirsher 	lock_rx_qs(priv);
1730ec21e2ecSJeff Kirsher 
1731c10650b6SClaudiu Manoil 	gfar_halt(priv);
1732ec21e2ecSJeff Kirsher 
1733ec21e2ecSJeff Kirsher 	unlock_rx_qs(priv);
1734ec21e2ecSJeff Kirsher 	unlock_tx_qs(priv);
1735ec21e2ecSJeff Kirsher 	local_irq_restore(flags);
1736ec21e2ecSJeff Kirsher 
1737ec21e2ecSJeff Kirsher 	free_skb_resources(priv);
1738ec21e2ecSJeff Kirsher }
1739ec21e2ecSJeff Kirsher 
1740ec21e2ecSJeff Kirsher static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
1741ec21e2ecSJeff Kirsher {
1742ec21e2ecSJeff Kirsher 	struct txbd8 *txbdp;
1743ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(tx_queue->dev);
1744ec21e2ecSJeff Kirsher 	int i, j;
1745ec21e2ecSJeff Kirsher 
1746ec21e2ecSJeff Kirsher 	txbdp = tx_queue->tx_bd_base;
1747ec21e2ecSJeff Kirsher 
1748ec21e2ecSJeff Kirsher 	for (i = 0; i < tx_queue->tx_ring_size; i++) {
1749ec21e2ecSJeff Kirsher 		if (!tx_queue->tx_skbuff[i])
1750ec21e2ecSJeff Kirsher 			continue;
1751ec21e2ecSJeff Kirsher 
1752369ec162SClaudiu Manoil 		dma_unmap_single(priv->dev, txbdp->bufPtr,
1753ec21e2ecSJeff Kirsher 				 txbdp->length, DMA_TO_DEVICE);
1754ec21e2ecSJeff Kirsher 		txbdp->lstatus = 0;
1755ec21e2ecSJeff Kirsher 		for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
1756ec21e2ecSJeff Kirsher 		     j++) {
1757ec21e2ecSJeff Kirsher 			txbdp++;
1758369ec162SClaudiu Manoil 			dma_unmap_page(priv->dev, txbdp->bufPtr,
1759ec21e2ecSJeff Kirsher 				       txbdp->length, DMA_TO_DEVICE);
1760ec21e2ecSJeff Kirsher 		}
1761ec21e2ecSJeff Kirsher 		txbdp++;
1762ec21e2ecSJeff Kirsher 		dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1763ec21e2ecSJeff Kirsher 		tx_queue->tx_skbuff[i] = NULL;
1764ec21e2ecSJeff Kirsher 	}
1765ec21e2ecSJeff Kirsher 	kfree(tx_queue->tx_skbuff);
17661eb8f7a7SClaudiu Manoil 	tx_queue->tx_skbuff = NULL;
1767ec21e2ecSJeff Kirsher }
1768ec21e2ecSJeff Kirsher 
1769ec21e2ecSJeff Kirsher static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1770ec21e2ecSJeff Kirsher {
1771ec21e2ecSJeff Kirsher 	struct rxbd8 *rxbdp;
1772ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(rx_queue->dev);
1773ec21e2ecSJeff Kirsher 	int i;
1774ec21e2ecSJeff Kirsher 
1775ec21e2ecSJeff Kirsher 	rxbdp = rx_queue->rx_bd_base;
1776ec21e2ecSJeff Kirsher 
1777ec21e2ecSJeff Kirsher 	for (i = 0; i < rx_queue->rx_ring_size; i++) {
1778ec21e2ecSJeff Kirsher 		if (rx_queue->rx_skbuff[i]) {
1779369ec162SClaudiu Manoil 			dma_unmap_single(priv->dev, rxbdp->bufPtr,
1780369ec162SClaudiu Manoil 					 priv->rx_buffer_size,
1781ec21e2ecSJeff Kirsher 					 DMA_FROM_DEVICE);
1782ec21e2ecSJeff Kirsher 			dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1783ec21e2ecSJeff Kirsher 			rx_queue->rx_skbuff[i] = NULL;
1784ec21e2ecSJeff Kirsher 		}
1785ec21e2ecSJeff Kirsher 		rxbdp->lstatus = 0;
1786ec21e2ecSJeff Kirsher 		rxbdp->bufPtr = 0;
1787ec21e2ecSJeff Kirsher 		rxbdp++;
1788ec21e2ecSJeff Kirsher 	}
1789ec21e2ecSJeff Kirsher 	kfree(rx_queue->rx_skbuff);
17901eb8f7a7SClaudiu Manoil 	rx_queue->rx_skbuff = NULL;
1791ec21e2ecSJeff Kirsher }
1792ec21e2ecSJeff Kirsher 
1793ec21e2ecSJeff Kirsher /* If there are any tx skbs or rx skbs still around, free them.
17940977f817SJan Ceuleers  * Then free tx_skbuff and rx_skbuff
17950977f817SJan Ceuleers  */
1796ec21e2ecSJeff Kirsher static void free_skb_resources(struct gfar_private *priv)
1797ec21e2ecSJeff Kirsher {
1798ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
1799ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
1800ec21e2ecSJeff Kirsher 	int i;
1801ec21e2ecSJeff Kirsher 
1802ec21e2ecSJeff Kirsher 	/* Go through all the buffer descriptors and free their data buffers */
1803ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
1804d8a0f1b0SPaul Gortmaker 		struct netdev_queue *txq;
1805bc4598bcSJan Ceuleers 
1806ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
1807d8a0f1b0SPaul Gortmaker 		txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
1808ec21e2ecSJeff Kirsher 		if (tx_queue->tx_skbuff)
1809ec21e2ecSJeff Kirsher 			free_skb_tx_queue(tx_queue);
1810d8a0f1b0SPaul Gortmaker 		netdev_tx_reset_queue(txq);
1811ec21e2ecSJeff Kirsher 	}
1812ec21e2ecSJeff Kirsher 
1813ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
1814ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
1815ec21e2ecSJeff Kirsher 		if (rx_queue->rx_skbuff)
1816ec21e2ecSJeff Kirsher 			free_skb_rx_queue(rx_queue);
1817ec21e2ecSJeff Kirsher 	}
1818ec21e2ecSJeff Kirsher 
1819369ec162SClaudiu Manoil 	dma_free_coherent(priv->dev,
1820ec21e2ecSJeff Kirsher 			  sizeof(struct txbd8) * priv->total_tx_ring_size +
1821ec21e2ecSJeff Kirsher 			  sizeof(struct rxbd8) * priv->total_rx_ring_size,
1822ec21e2ecSJeff Kirsher 			  priv->tx_queue[0]->tx_bd_base,
1823ec21e2ecSJeff Kirsher 			  priv->tx_queue[0]->tx_bd_dma_base);
1824ec21e2ecSJeff Kirsher }
1825ec21e2ecSJeff Kirsher 
1826c10650b6SClaudiu Manoil void gfar_start(struct gfar_private *priv)
1827ec21e2ecSJeff Kirsher {
1828ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1829ec21e2ecSJeff Kirsher 	u32 tempval;
1830ec21e2ecSJeff Kirsher 	int i = 0;
1831ec21e2ecSJeff Kirsher 
1832c10650b6SClaudiu Manoil 	/* Enable Rx/Tx hw queues */
1833c10650b6SClaudiu Manoil 	gfar_write(&regs->rqueue, priv->rqueue);
1834c10650b6SClaudiu Manoil 	gfar_write(&regs->tqueue, priv->tqueue);
1835ec21e2ecSJeff Kirsher 
1836ec21e2ecSJeff Kirsher 	/* Initialize DMACTRL to have WWR and WOP */
1837ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->dmactrl);
1838ec21e2ecSJeff Kirsher 	tempval |= DMACTRL_INIT_SETTINGS;
1839ec21e2ecSJeff Kirsher 	gfar_write(&regs->dmactrl, tempval);
1840ec21e2ecSJeff Kirsher 
1841ec21e2ecSJeff Kirsher 	/* Make sure we aren't stopped */
1842ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->dmactrl);
1843ec21e2ecSJeff Kirsher 	tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
1844ec21e2ecSJeff Kirsher 	gfar_write(&regs->dmactrl, tempval);
1845ec21e2ecSJeff Kirsher 
1846ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_grps; i++) {
1847ec21e2ecSJeff Kirsher 		regs = priv->gfargrp[i].regs;
1848ec21e2ecSJeff Kirsher 		/* Clear THLT/RHLT, so that the DMA starts polling now */
1849ec21e2ecSJeff Kirsher 		gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1850ec21e2ecSJeff Kirsher 		gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
1851ec21e2ecSJeff Kirsher 	}
1852ec21e2ecSJeff Kirsher 
1853c10650b6SClaudiu Manoil 	/* Enable Rx/Tx DMA */
1854c10650b6SClaudiu Manoil 	tempval = gfar_read(&regs->maccfg1);
1855c10650b6SClaudiu Manoil 	tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1856c10650b6SClaudiu Manoil 	gfar_write(&regs->maccfg1, tempval);
1857c10650b6SClaudiu Manoil 
1858efeddce7SClaudiu Manoil 	gfar_ints_enable(priv);
1859efeddce7SClaudiu Manoil 
1860c10650b6SClaudiu Manoil 	priv->ndev->trans_start = jiffies; /* prevent tx timeout */
1861ec21e2ecSJeff Kirsher }
1862ec21e2ecSJeff Kirsher 
1863800c644bSClaudiu Manoil static void gfar_configure_coalescing(struct gfar_private *priv,
1864ec21e2ecSJeff Kirsher 			       unsigned long tx_mask, unsigned long rx_mask)
1865ec21e2ecSJeff Kirsher {
1866ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1867ec21e2ecSJeff Kirsher 	u32 __iomem *baddr;
1868ec21e2ecSJeff Kirsher 
1869ec21e2ecSJeff Kirsher 	if (priv->mode == MQ_MG_MODE) {
18705d9657d8SClaudiu Manoil 		int i = 0;
1871c6e1160eSClaudiu Manoil 
1872ec21e2ecSJeff Kirsher 		baddr = &regs->txic0;
1873ec21e2ecSJeff Kirsher 		for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
1874ec21e2ecSJeff Kirsher 			gfar_write(baddr + i, 0);
18759740e001SClaudiu Manoil 			if (likely(priv->tx_queue[i]->txcoalescing))
1876ec21e2ecSJeff Kirsher 				gfar_write(baddr + i, priv->tx_queue[i]->txic);
1877ec21e2ecSJeff Kirsher 		}
1878ec21e2ecSJeff Kirsher 
1879ec21e2ecSJeff Kirsher 		baddr = &regs->rxic0;
1880ec21e2ecSJeff Kirsher 		for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
1881ec21e2ecSJeff Kirsher 			gfar_write(baddr + i, 0);
18829740e001SClaudiu Manoil 			if (likely(priv->rx_queue[i]->rxcoalescing))
1883ec21e2ecSJeff Kirsher 				gfar_write(baddr + i, priv->rx_queue[i]->rxic);
1884ec21e2ecSJeff Kirsher 		}
18855d9657d8SClaudiu Manoil 	} else {
1886c6e1160eSClaudiu Manoil 		/* Backward compatible case -- even if we enable
18875d9657d8SClaudiu Manoil 		 * multiple queues, there's only single reg to program
18885d9657d8SClaudiu Manoil 		 */
18895d9657d8SClaudiu Manoil 		gfar_write(&regs->txic, 0);
18905d9657d8SClaudiu Manoil 		if (likely(priv->tx_queue[0]->txcoalescing))
18915d9657d8SClaudiu Manoil 			gfar_write(&regs->txic, priv->tx_queue[0]->txic);
18925d9657d8SClaudiu Manoil 
18935d9657d8SClaudiu Manoil 		gfar_write(&regs->rxic, 0);
18945d9657d8SClaudiu Manoil 		if (unlikely(priv->rx_queue[0]->rxcoalescing))
18955d9657d8SClaudiu Manoil 			gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
1896ec21e2ecSJeff Kirsher 	}
1897ec21e2ecSJeff Kirsher }
1898ec21e2ecSJeff Kirsher 
1899800c644bSClaudiu Manoil void gfar_configure_coalescing_all(struct gfar_private *priv)
1900800c644bSClaudiu Manoil {
1901800c644bSClaudiu Manoil 	gfar_configure_coalescing(priv, 0xFF, 0xFF);
1902800c644bSClaudiu Manoil }
1903800c644bSClaudiu Manoil 
1904*80ec396cSClaudiu Manoil static void free_grp_irqs(struct gfar_priv_grp *grp)
1905*80ec396cSClaudiu Manoil {
1906*80ec396cSClaudiu Manoil 	free_irq(gfar_irq(grp, TX)->irq, grp);
1907*80ec396cSClaudiu Manoil 	free_irq(gfar_irq(grp, RX)->irq, grp);
1908*80ec396cSClaudiu Manoil 	free_irq(gfar_irq(grp, ER)->irq, grp);
1909*80ec396cSClaudiu Manoil }
1910*80ec396cSClaudiu Manoil 
1911ec21e2ecSJeff Kirsher static int register_grp_irqs(struct gfar_priv_grp *grp)
1912ec21e2ecSJeff Kirsher {
1913ec21e2ecSJeff Kirsher 	struct gfar_private *priv = grp->priv;
1914ec21e2ecSJeff Kirsher 	struct net_device *dev = priv->ndev;
1915ec21e2ecSJeff Kirsher 	int err;
1916ec21e2ecSJeff Kirsher 
1917ec21e2ecSJeff Kirsher 	/* If the device has multiple interrupts, register for
19180977f817SJan Ceuleers 	 * them.  Otherwise, only register for the one
19190977f817SJan Ceuleers 	 */
1920ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1921ec21e2ecSJeff Kirsher 		/* Install our interrupt handlers for Error,
19220977f817SJan Ceuleers 		 * Transmit, and Receive
19230977f817SJan Ceuleers 		 */
1924ee873fdaSClaudiu Manoil 		err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
1925ee873fdaSClaudiu Manoil 				  gfar_irq(grp, ER)->name, grp);
1926ee873fdaSClaudiu Manoil 		if (err < 0) {
1927ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1928ee873fdaSClaudiu Manoil 				  gfar_irq(grp, ER)->irq);
1929ec21e2ecSJeff Kirsher 
1930ec21e2ecSJeff Kirsher 			goto err_irq_fail;
1931ec21e2ecSJeff Kirsher 		}
1932ee873fdaSClaudiu Manoil 		err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
1933ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->name, grp);
1934ee873fdaSClaudiu Manoil 		if (err < 0) {
1935ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1936ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->irq);
1937ec21e2ecSJeff Kirsher 			goto tx_irq_fail;
1938ec21e2ecSJeff Kirsher 		}
1939ee873fdaSClaudiu Manoil 		err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
1940ee873fdaSClaudiu Manoil 				  gfar_irq(grp, RX)->name, grp);
1941ee873fdaSClaudiu Manoil 		if (err < 0) {
1942ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1943ee873fdaSClaudiu Manoil 				  gfar_irq(grp, RX)->irq);
1944ec21e2ecSJeff Kirsher 			goto rx_irq_fail;
1945ec21e2ecSJeff Kirsher 		}
1946ec21e2ecSJeff Kirsher 	} else {
1947ee873fdaSClaudiu Manoil 		err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
1948ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->name, grp);
1949ee873fdaSClaudiu Manoil 		if (err < 0) {
1950ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1951ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->irq);
1952ec21e2ecSJeff Kirsher 			goto err_irq_fail;
1953ec21e2ecSJeff Kirsher 		}
1954ec21e2ecSJeff Kirsher 	}
1955ec21e2ecSJeff Kirsher 
1956ec21e2ecSJeff Kirsher 	return 0;
1957ec21e2ecSJeff Kirsher 
1958ec21e2ecSJeff Kirsher rx_irq_fail:
1959ee873fdaSClaudiu Manoil 	free_irq(gfar_irq(grp, TX)->irq, grp);
1960ec21e2ecSJeff Kirsher tx_irq_fail:
1961ee873fdaSClaudiu Manoil 	free_irq(gfar_irq(grp, ER)->irq, grp);
1962ec21e2ecSJeff Kirsher err_irq_fail:
1963ec21e2ecSJeff Kirsher 	return err;
1964ec21e2ecSJeff Kirsher 
1965ec21e2ecSJeff Kirsher }
1966ec21e2ecSJeff Kirsher 
1967*80ec396cSClaudiu Manoil static void gfar_free_irq(struct gfar_private *priv)
1968*80ec396cSClaudiu Manoil {
1969*80ec396cSClaudiu Manoil 	int i;
1970*80ec396cSClaudiu Manoil 
1971*80ec396cSClaudiu Manoil 	/* Free the IRQs */
1972*80ec396cSClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1973*80ec396cSClaudiu Manoil 		for (i = 0; i < priv->num_grps; i++)
1974*80ec396cSClaudiu Manoil 			free_grp_irqs(&priv->gfargrp[i]);
1975*80ec396cSClaudiu Manoil 	} else {
1976*80ec396cSClaudiu Manoil 		for (i = 0; i < priv->num_grps; i++)
1977*80ec396cSClaudiu Manoil 			free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
1978*80ec396cSClaudiu Manoil 				 &priv->gfargrp[i]);
1979*80ec396cSClaudiu Manoil 	}
1980*80ec396cSClaudiu Manoil }
1981*80ec396cSClaudiu Manoil 
1982*80ec396cSClaudiu Manoil static int gfar_request_irq(struct gfar_private *priv)
1983*80ec396cSClaudiu Manoil {
1984*80ec396cSClaudiu Manoil 	int err, i, j;
1985*80ec396cSClaudiu Manoil 
1986*80ec396cSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
1987*80ec396cSClaudiu Manoil 		err = register_grp_irqs(&priv->gfargrp[i]);
1988*80ec396cSClaudiu Manoil 		if (err) {
1989*80ec396cSClaudiu Manoil 			for (j = 0; j < i; j++)
1990*80ec396cSClaudiu Manoil 				free_grp_irqs(&priv->gfargrp[j]);
1991*80ec396cSClaudiu Manoil 			return err;
1992*80ec396cSClaudiu Manoil 		}
1993*80ec396cSClaudiu Manoil 	}
1994*80ec396cSClaudiu Manoil 
1995*80ec396cSClaudiu Manoil 	return 0;
1996*80ec396cSClaudiu Manoil }
1997*80ec396cSClaudiu Manoil 
1998ec21e2ecSJeff Kirsher /* Bring the controller up and running */
1999ec21e2ecSJeff Kirsher int startup_gfar(struct net_device *ndev)
2000ec21e2ecSJeff Kirsher {
2001ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(ndev);
2002*80ec396cSClaudiu Manoil 	int err;
2003ec21e2ecSJeff Kirsher 
2004a328ac92SClaudiu Manoil 	gfar_mac_reset(priv);
2005ec21e2ecSJeff Kirsher 
2006ec21e2ecSJeff Kirsher 	err = gfar_alloc_skb_resources(ndev);
2007ec21e2ecSJeff Kirsher 	if (err)
2008ec21e2ecSJeff Kirsher 		return err;
2009ec21e2ecSJeff Kirsher 
2010a328ac92SClaudiu Manoil 	gfar_init_tx_rx_base(priv);
2011ec21e2ecSJeff Kirsher 
2012ec21e2ecSJeff Kirsher 	/* Start the controller */
2013c10650b6SClaudiu Manoil 	gfar_start(priv);
2014ec21e2ecSJeff Kirsher 
2015ec21e2ecSJeff Kirsher 	phy_start(priv->phydev);
2016ec21e2ecSJeff Kirsher 
2017ec21e2ecSJeff Kirsher 	return 0;
2018ec21e2ecSJeff Kirsher }
2019ec21e2ecSJeff Kirsher 
20200977f817SJan Ceuleers /* Called when something needs to use the ethernet device
20210977f817SJan Ceuleers  * Returns 0 for success.
20220977f817SJan Ceuleers  */
2023ec21e2ecSJeff Kirsher static int gfar_enet_open(struct net_device *dev)
2024ec21e2ecSJeff Kirsher {
2025ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2026ec21e2ecSJeff Kirsher 	int err;
2027ec21e2ecSJeff Kirsher 
2028ec21e2ecSJeff Kirsher 	enable_napi(priv);
2029ec21e2ecSJeff Kirsher 
2030ec21e2ecSJeff Kirsher 	err = init_phy(dev);
2031ec21e2ecSJeff Kirsher 
2032ec21e2ecSJeff Kirsher 	if (err) {
2033ec21e2ecSJeff Kirsher 		disable_napi(priv);
2034ec21e2ecSJeff Kirsher 		return err;
2035ec21e2ecSJeff Kirsher 	}
2036ec21e2ecSJeff Kirsher 
2037*80ec396cSClaudiu Manoil 	err = gfar_request_irq(priv);
2038*80ec396cSClaudiu Manoil 	if (err)
2039*80ec396cSClaudiu Manoil 		return err;
2040*80ec396cSClaudiu Manoil 
2041ec21e2ecSJeff Kirsher 	err = startup_gfar(dev);
2042ec21e2ecSJeff Kirsher 	if (err) {
2043ec21e2ecSJeff Kirsher 		disable_napi(priv);
2044ec21e2ecSJeff Kirsher 		return err;
2045ec21e2ecSJeff Kirsher 	}
2046ec21e2ecSJeff Kirsher 
2047ec21e2ecSJeff Kirsher 	netif_tx_start_all_queues(dev);
2048ec21e2ecSJeff Kirsher 
2049ec21e2ecSJeff Kirsher 	device_set_wakeup_enable(&dev->dev, priv->wol_en);
2050ec21e2ecSJeff Kirsher 
2051ec21e2ecSJeff Kirsher 	return err;
2052ec21e2ecSJeff Kirsher }
2053ec21e2ecSJeff Kirsher 
2054ec21e2ecSJeff Kirsher static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
2055ec21e2ecSJeff Kirsher {
2056ec21e2ecSJeff Kirsher 	struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
2057ec21e2ecSJeff Kirsher 
2058ec21e2ecSJeff Kirsher 	memset(fcb, 0, GMAC_FCB_LEN);
2059ec21e2ecSJeff Kirsher 
2060ec21e2ecSJeff Kirsher 	return fcb;
2061ec21e2ecSJeff Kirsher }
2062ec21e2ecSJeff Kirsher 
20639c4886e5SManfred Rudigier static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
20649c4886e5SManfred Rudigier 				    int fcb_length)
2065ec21e2ecSJeff Kirsher {
2066ec21e2ecSJeff Kirsher 	/* If we're here, it's a IP packet with a TCP or UDP
2067ec21e2ecSJeff Kirsher 	 * payload.  We set it to checksum, using a pseudo-header
2068ec21e2ecSJeff Kirsher 	 * we provide
2069ec21e2ecSJeff Kirsher 	 */
20703a2e16c8SJan Ceuleers 	u8 flags = TXFCB_DEFAULT;
2071ec21e2ecSJeff Kirsher 
20720977f817SJan Ceuleers 	/* Tell the controller what the protocol is
20730977f817SJan Ceuleers 	 * And provide the already calculated phcs
20740977f817SJan Ceuleers 	 */
2075ec21e2ecSJeff Kirsher 	if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
2076ec21e2ecSJeff Kirsher 		flags |= TXFCB_UDP;
2077ec21e2ecSJeff Kirsher 		fcb->phcs = udp_hdr(skb)->check;
2078ec21e2ecSJeff Kirsher 	} else
2079ec21e2ecSJeff Kirsher 		fcb->phcs = tcp_hdr(skb)->check;
2080ec21e2ecSJeff Kirsher 
2081ec21e2ecSJeff Kirsher 	/* l3os is the distance between the start of the
2082ec21e2ecSJeff Kirsher 	 * frame (skb->data) and the start of the IP hdr.
2083ec21e2ecSJeff Kirsher 	 * l4os is the distance between the start of the
20840977f817SJan Ceuleers 	 * l3 hdr and the l4 hdr
20850977f817SJan Ceuleers 	 */
20869c4886e5SManfred Rudigier 	fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
2087ec21e2ecSJeff Kirsher 	fcb->l4os = skb_network_header_len(skb);
2088ec21e2ecSJeff Kirsher 
2089ec21e2ecSJeff Kirsher 	fcb->flags = flags;
2090ec21e2ecSJeff Kirsher }
2091ec21e2ecSJeff Kirsher 
2092ec21e2ecSJeff Kirsher void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
2093ec21e2ecSJeff Kirsher {
2094ec21e2ecSJeff Kirsher 	fcb->flags |= TXFCB_VLN;
2095ec21e2ecSJeff Kirsher 	fcb->vlctl = vlan_tx_tag_get(skb);
2096ec21e2ecSJeff Kirsher }
2097ec21e2ecSJeff Kirsher 
2098ec21e2ecSJeff Kirsher static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
2099ec21e2ecSJeff Kirsher 				      struct txbd8 *base, int ring_size)
2100ec21e2ecSJeff Kirsher {
2101ec21e2ecSJeff Kirsher 	struct txbd8 *new_bd = bdp + stride;
2102ec21e2ecSJeff Kirsher 
2103ec21e2ecSJeff Kirsher 	return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2104ec21e2ecSJeff Kirsher }
2105ec21e2ecSJeff Kirsher 
2106ec21e2ecSJeff Kirsher static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
2107ec21e2ecSJeff Kirsher 				      int ring_size)
2108ec21e2ecSJeff Kirsher {
2109ec21e2ecSJeff Kirsher 	return skip_txbd(bdp, 1, base, ring_size);
2110ec21e2ecSJeff Kirsher }
2111ec21e2ecSJeff Kirsher 
211202d88fb4SClaudiu Manoil /* eTSEC12: csum generation not supported for some fcb offsets */
211302d88fb4SClaudiu Manoil static inline bool gfar_csum_errata_12(struct gfar_private *priv,
211402d88fb4SClaudiu Manoil 				       unsigned long fcb_addr)
211502d88fb4SClaudiu Manoil {
211602d88fb4SClaudiu Manoil 	return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
211702d88fb4SClaudiu Manoil 	       (fcb_addr % 0x20) > 0x18);
211802d88fb4SClaudiu Manoil }
211902d88fb4SClaudiu Manoil 
212002d88fb4SClaudiu Manoil /* eTSEC76: csum generation for frames larger than 2500 may
212102d88fb4SClaudiu Manoil  * cause excess delays before start of transmission
212202d88fb4SClaudiu Manoil  */
212302d88fb4SClaudiu Manoil static inline bool gfar_csum_errata_76(struct gfar_private *priv,
212402d88fb4SClaudiu Manoil 				       unsigned int len)
212502d88fb4SClaudiu Manoil {
212602d88fb4SClaudiu Manoil 	return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
212702d88fb4SClaudiu Manoil 	       (len > 2500));
212802d88fb4SClaudiu Manoil }
212902d88fb4SClaudiu Manoil 
21300977f817SJan Ceuleers /* This is called by the kernel when a frame is ready for transmission.
21310977f817SJan Ceuleers  * It is pointed to by the dev->hard_start_xmit function pointer
21320977f817SJan Ceuleers  */
2133ec21e2ecSJeff Kirsher static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2134ec21e2ecSJeff Kirsher {
2135ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2136ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
2137ec21e2ecSJeff Kirsher 	struct netdev_queue *txq;
2138ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = NULL;
2139ec21e2ecSJeff Kirsher 	struct txfcb *fcb = NULL;
2140ec21e2ecSJeff Kirsher 	struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
2141ec21e2ecSJeff Kirsher 	u32 lstatus;
21420d0cffdcSClaudiu Manoil 	int i, rq = 0;
21430d0cffdcSClaudiu Manoil 	int do_tstamp, do_csum, do_vlan;
2144ec21e2ecSJeff Kirsher 	u32 bufaddr;
2145ec21e2ecSJeff Kirsher 	unsigned long flags;
214650ad076bSClaudiu Manoil 	unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
2147ec21e2ecSJeff Kirsher 
2148ec21e2ecSJeff Kirsher 	rq = skb->queue_mapping;
2149ec21e2ecSJeff Kirsher 	tx_queue = priv->tx_queue[rq];
2150ec21e2ecSJeff Kirsher 	txq = netdev_get_tx_queue(dev, rq);
2151ec21e2ecSJeff Kirsher 	base = tx_queue->tx_bd_base;
2152ec21e2ecSJeff Kirsher 	regs = tx_queue->grp->regs;
2153ec21e2ecSJeff Kirsher 
21540d0cffdcSClaudiu Manoil 	do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
21550d0cffdcSClaudiu Manoil 	do_vlan = vlan_tx_tag_present(skb);
21560d0cffdcSClaudiu Manoil 	do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
21570d0cffdcSClaudiu Manoil 		    priv->hwts_tx_en;
21580d0cffdcSClaudiu Manoil 
21590d0cffdcSClaudiu Manoil 	if (do_csum || do_vlan)
21600d0cffdcSClaudiu Manoil 		fcb_len = GMAC_FCB_LEN;
21610d0cffdcSClaudiu Manoil 
2162ec21e2ecSJeff Kirsher 	/* check if time stamp should be generated */
21630d0cffdcSClaudiu Manoil 	if (unlikely(do_tstamp))
21640d0cffdcSClaudiu Manoil 		fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2165ec21e2ecSJeff Kirsher 
2166ec21e2ecSJeff Kirsher 	/* make space for additional header when fcb is needed */
21670d0cffdcSClaudiu Manoil 	if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
2168ec21e2ecSJeff Kirsher 		struct sk_buff *skb_new;
2169ec21e2ecSJeff Kirsher 
21700d0cffdcSClaudiu Manoil 		skb_new = skb_realloc_headroom(skb, fcb_len);
2171ec21e2ecSJeff Kirsher 		if (!skb_new) {
2172ec21e2ecSJeff Kirsher 			dev->stats.tx_errors++;
2173ec21e2ecSJeff Kirsher 			kfree_skb(skb);
2174ec21e2ecSJeff Kirsher 			return NETDEV_TX_OK;
2175ec21e2ecSJeff Kirsher 		}
2176db83d136SManfred Rudigier 
2177313b037cSEric Dumazet 		if (skb->sk)
2178313b037cSEric Dumazet 			skb_set_owner_w(skb_new, skb->sk);
2179313b037cSEric Dumazet 		consume_skb(skb);
2180ec21e2ecSJeff Kirsher 		skb = skb_new;
2181ec21e2ecSJeff Kirsher 	}
2182ec21e2ecSJeff Kirsher 
2183ec21e2ecSJeff Kirsher 	/* total number of fragments in the SKB */
2184ec21e2ecSJeff Kirsher 	nr_frags = skb_shinfo(skb)->nr_frags;
2185ec21e2ecSJeff Kirsher 
2186ec21e2ecSJeff Kirsher 	/* calculate the required number of TxBDs for this skb */
2187ec21e2ecSJeff Kirsher 	if (unlikely(do_tstamp))
2188ec21e2ecSJeff Kirsher 		nr_txbds = nr_frags + 2;
2189ec21e2ecSJeff Kirsher 	else
2190ec21e2ecSJeff Kirsher 		nr_txbds = nr_frags + 1;
2191ec21e2ecSJeff Kirsher 
2192ec21e2ecSJeff Kirsher 	/* check if there is space to queue this packet */
2193ec21e2ecSJeff Kirsher 	if (nr_txbds > tx_queue->num_txbdfree) {
2194ec21e2ecSJeff Kirsher 		/* no space, stop the queue */
2195ec21e2ecSJeff Kirsher 		netif_tx_stop_queue(txq);
2196ec21e2ecSJeff Kirsher 		dev->stats.tx_fifo_errors++;
2197ec21e2ecSJeff Kirsher 		return NETDEV_TX_BUSY;
2198ec21e2ecSJeff Kirsher 	}
2199ec21e2ecSJeff Kirsher 
2200ec21e2ecSJeff Kirsher 	/* Update transmit stats */
220150ad076bSClaudiu Manoil 	bytes_sent = skb->len;
220250ad076bSClaudiu Manoil 	tx_queue->stats.tx_bytes += bytes_sent;
220350ad076bSClaudiu Manoil 	/* keep Tx bytes on wire for BQL accounting */
220450ad076bSClaudiu Manoil 	GFAR_CB(skb)->bytes_sent = bytes_sent;
2205ec21e2ecSJeff Kirsher 	tx_queue->stats.tx_packets++;
2206ec21e2ecSJeff Kirsher 
2207ec21e2ecSJeff Kirsher 	txbdp = txbdp_start = tx_queue->cur_tx;
2208ec21e2ecSJeff Kirsher 	lstatus = txbdp->lstatus;
2209ec21e2ecSJeff Kirsher 
2210ec21e2ecSJeff Kirsher 	/* Time stamp insertion requires one additional TxBD */
2211ec21e2ecSJeff Kirsher 	if (unlikely(do_tstamp))
2212ec21e2ecSJeff Kirsher 		txbdp_tstamp = txbdp = next_txbd(txbdp, base,
2213ec21e2ecSJeff Kirsher 						 tx_queue->tx_ring_size);
2214ec21e2ecSJeff Kirsher 
2215ec21e2ecSJeff Kirsher 	if (nr_frags == 0) {
2216ec21e2ecSJeff Kirsher 		if (unlikely(do_tstamp))
2217ec21e2ecSJeff Kirsher 			txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
2218ec21e2ecSJeff Kirsher 							  TXBD_INTERRUPT);
2219ec21e2ecSJeff Kirsher 		else
2220ec21e2ecSJeff Kirsher 			lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2221ec21e2ecSJeff Kirsher 	} else {
2222ec21e2ecSJeff Kirsher 		/* Place the fragment addresses and lengths into the TxBDs */
2223ec21e2ecSJeff Kirsher 		for (i = 0; i < nr_frags; i++) {
222450ad076bSClaudiu Manoil 			unsigned int frag_len;
2225ec21e2ecSJeff Kirsher 			/* Point at the next BD, wrapping as needed */
2226ec21e2ecSJeff Kirsher 			txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2227ec21e2ecSJeff Kirsher 
222850ad076bSClaudiu Manoil 			frag_len = skb_shinfo(skb)->frags[i].size;
2229ec21e2ecSJeff Kirsher 
223050ad076bSClaudiu Manoil 			lstatus = txbdp->lstatus | frag_len |
2231ec21e2ecSJeff Kirsher 				  BD_LFLAG(TXBD_READY);
2232ec21e2ecSJeff Kirsher 
2233ec21e2ecSJeff Kirsher 			/* Handle the last BD specially */
2234ec21e2ecSJeff Kirsher 			if (i == nr_frags - 1)
2235ec21e2ecSJeff Kirsher 				lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2236ec21e2ecSJeff Kirsher 
2237369ec162SClaudiu Manoil 			bufaddr = skb_frag_dma_map(priv->dev,
22382234a722SIan Campbell 						   &skb_shinfo(skb)->frags[i],
22392234a722SIan Campbell 						   0,
224050ad076bSClaudiu Manoil 						   frag_len,
2241ec21e2ecSJeff Kirsher 						   DMA_TO_DEVICE);
2242ec21e2ecSJeff Kirsher 
2243ec21e2ecSJeff Kirsher 			/* set the TxBD length and buffer pointer */
2244ec21e2ecSJeff Kirsher 			txbdp->bufPtr = bufaddr;
2245ec21e2ecSJeff Kirsher 			txbdp->lstatus = lstatus;
2246ec21e2ecSJeff Kirsher 		}
2247ec21e2ecSJeff Kirsher 
2248ec21e2ecSJeff Kirsher 		lstatus = txbdp_start->lstatus;
2249ec21e2ecSJeff Kirsher 	}
2250ec21e2ecSJeff Kirsher 
22519c4886e5SManfred Rudigier 	/* Add TxPAL between FCB and frame if required */
22529c4886e5SManfred Rudigier 	if (unlikely(do_tstamp)) {
22539c4886e5SManfred Rudigier 		skb_push(skb, GMAC_TXPAL_LEN);
22549c4886e5SManfred Rudigier 		memset(skb->data, 0, GMAC_TXPAL_LEN);
22559c4886e5SManfred Rudigier 	}
22569c4886e5SManfred Rudigier 
22570d0cffdcSClaudiu Manoil 	/* Add TxFCB if required */
22580d0cffdcSClaudiu Manoil 	if (fcb_len) {
2259ec21e2ecSJeff Kirsher 		fcb = gfar_add_fcb(skb);
2260ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(TXBD_TOE);
22610d0cffdcSClaudiu Manoil 	}
22620d0cffdcSClaudiu Manoil 
22630d0cffdcSClaudiu Manoil 	/* Set up checksumming */
22640d0cffdcSClaudiu Manoil 	if (do_csum) {
22650d0cffdcSClaudiu Manoil 		gfar_tx_checksum(skb, fcb, fcb_len);
226602d88fb4SClaudiu Manoil 
226702d88fb4SClaudiu Manoil 		if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
226802d88fb4SClaudiu Manoil 		    unlikely(gfar_csum_errata_76(priv, skb->len))) {
226902d88fb4SClaudiu Manoil 			__skb_pull(skb, GMAC_FCB_LEN);
227002d88fb4SClaudiu Manoil 			skb_checksum_help(skb);
22710d0cffdcSClaudiu Manoil 			if (do_vlan || do_tstamp) {
22720d0cffdcSClaudiu Manoil 				/* put back a new fcb for vlan/tstamp TOE */
22730d0cffdcSClaudiu Manoil 				fcb = gfar_add_fcb(skb);
22740d0cffdcSClaudiu Manoil 			} else {
22750d0cffdcSClaudiu Manoil 				/* Tx TOE not used */
227602d88fb4SClaudiu Manoil 				lstatus &= ~(BD_LFLAG(TXBD_TOE));
227702d88fb4SClaudiu Manoil 				fcb = NULL;
2278ec21e2ecSJeff Kirsher 			}
2279ec21e2ecSJeff Kirsher 		}
2280ec21e2ecSJeff Kirsher 	}
2281ec21e2ecSJeff Kirsher 
22820d0cffdcSClaudiu Manoil 	if (do_vlan)
2283ec21e2ecSJeff Kirsher 		gfar_tx_vlan(skb, fcb);
2284ec21e2ecSJeff Kirsher 
2285ec21e2ecSJeff Kirsher 	/* Setup tx hardware time stamping if requested */
2286ec21e2ecSJeff Kirsher 	if (unlikely(do_tstamp)) {
2287ec21e2ecSJeff Kirsher 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2288ec21e2ecSJeff Kirsher 		fcb->ptp = 1;
2289ec21e2ecSJeff Kirsher 	}
2290ec21e2ecSJeff Kirsher 
2291369ec162SClaudiu Manoil 	txbdp_start->bufPtr = dma_map_single(priv->dev, skb->data,
2292ec21e2ecSJeff Kirsher 					     skb_headlen(skb), DMA_TO_DEVICE);
2293ec21e2ecSJeff Kirsher 
22940977f817SJan Ceuleers 	/* If time stamping is requested one additional TxBD must be set up. The
2295ec21e2ecSJeff Kirsher 	 * first TxBD points to the FCB and must have a data length of
2296ec21e2ecSJeff Kirsher 	 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2297ec21e2ecSJeff Kirsher 	 * the full frame length.
2298ec21e2ecSJeff Kirsher 	 */
2299ec21e2ecSJeff Kirsher 	if (unlikely(do_tstamp)) {
23000d0cffdcSClaudiu Manoil 		txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_len;
2301ec21e2ecSJeff Kirsher 		txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
23020d0cffdcSClaudiu Manoil 					 (skb_headlen(skb) - fcb_len);
2303ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2304ec21e2ecSJeff Kirsher 	} else {
2305ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2306ec21e2ecSJeff Kirsher 	}
2307ec21e2ecSJeff Kirsher 
230850ad076bSClaudiu Manoil 	netdev_tx_sent_queue(txq, bytes_sent);
2309d8a0f1b0SPaul Gortmaker 
23100977f817SJan Ceuleers 	/* We can work in parallel with gfar_clean_tx_ring(), except
2311ec21e2ecSJeff Kirsher 	 * when modifying num_txbdfree. Note that we didn't grab the lock
2312ec21e2ecSJeff Kirsher 	 * when we were reading the num_txbdfree and checking for available
2313ec21e2ecSJeff Kirsher 	 * space, that's because outside of this function it can only grow,
2314ec21e2ecSJeff Kirsher 	 * and once we've got needed space, it cannot suddenly disappear.
2315ec21e2ecSJeff Kirsher 	 *
2316ec21e2ecSJeff Kirsher 	 * The lock also protects us from gfar_error(), which can modify
2317ec21e2ecSJeff Kirsher 	 * regs->tstat and thus retrigger the transfers, which is why we
2318ec21e2ecSJeff Kirsher 	 * also must grab the lock before setting ready bit for the first
2319ec21e2ecSJeff Kirsher 	 * to be transmitted BD.
2320ec21e2ecSJeff Kirsher 	 */
2321ec21e2ecSJeff Kirsher 	spin_lock_irqsave(&tx_queue->txlock, flags);
2322ec21e2ecSJeff Kirsher 
23230977f817SJan Ceuleers 	/* The powerpc-specific eieio() is used, as wmb() has too strong
2324ec21e2ecSJeff Kirsher 	 * semantics (it requires synchronization between cacheable and
2325ec21e2ecSJeff Kirsher 	 * uncacheable mappings, which eieio doesn't provide and which we
2326ec21e2ecSJeff Kirsher 	 * don't need), thus requiring a more expensive sync instruction.  At
2327ec21e2ecSJeff Kirsher 	 * some point, the set of architecture-independent barrier functions
2328ec21e2ecSJeff Kirsher 	 * should be expanded to include weaker barriers.
2329ec21e2ecSJeff Kirsher 	 */
2330ec21e2ecSJeff Kirsher 	eieio();
2331ec21e2ecSJeff Kirsher 
2332ec21e2ecSJeff Kirsher 	txbdp_start->lstatus = lstatus;
2333ec21e2ecSJeff Kirsher 
2334ec21e2ecSJeff Kirsher 	eieio(); /* force lstatus write before tx_skbuff */
2335ec21e2ecSJeff Kirsher 
2336ec21e2ecSJeff Kirsher 	tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2337ec21e2ecSJeff Kirsher 
2338ec21e2ecSJeff Kirsher 	/* Update the current skb pointer to the next entry we will use
23390977f817SJan Ceuleers 	 * (wrapping if necessary)
23400977f817SJan Ceuleers 	 */
2341ec21e2ecSJeff Kirsher 	tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
2342ec21e2ecSJeff Kirsher 			      TX_RING_MOD_MASK(tx_queue->tx_ring_size);
2343ec21e2ecSJeff Kirsher 
2344ec21e2ecSJeff Kirsher 	tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2345ec21e2ecSJeff Kirsher 
2346ec21e2ecSJeff Kirsher 	/* reduce TxBD free count */
2347ec21e2ecSJeff Kirsher 	tx_queue->num_txbdfree -= (nr_txbds);
2348ec21e2ecSJeff Kirsher 
2349ec21e2ecSJeff Kirsher 	/* If the next BD still needs to be cleaned up, then the bds
23500977f817SJan Ceuleers 	 * are full.  We need to tell the kernel to stop sending us stuff.
23510977f817SJan Ceuleers 	 */
2352ec21e2ecSJeff Kirsher 	if (!tx_queue->num_txbdfree) {
2353ec21e2ecSJeff Kirsher 		netif_tx_stop_queue(txq);
2354ec21e2ecSJeff Kirsher 
2355ec21e2ecSJeff Kirsher 		dev->stats.tx_fifo_errors++;
2356ec21e2ecSJeff Kirsher 	}
2357ec21e2ecSJeff Kirsher 
2358ec21e2ecSJeff Kirsher 	/* Tell the DMA to go go go */
2359ec21e2ecSJeff Kirsher 	gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
2360ec21e2ecSJeff Kirsher 
2361ec21e2ecSJeff Kirsher 	/* Unlock priv */
2362ec21e2ecSJeff Kirsher 	spin_unlock_irqrestore(&tx_queue->txlock, flags);
2363ec21e2ecSJeff Kirsher 
2364ec21e2ecSJeff Kirsher 	return NETDEV_TX_OK;
2365ec21e2ecSJeff Kirsher }
2366ec21e2ecSJeff Kirsher 
2367ec21e2ecSJeff Kirsher /* Stops the kernel queue, and halts the controller */
2368ec21e2ecSJeff Kirsher static int gfar_close(struct net_device *dev)
2369ec21e2ecSJeff Kirsher {
2370ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2371ec21e2ecSJeff Kirsher 
2372ec21e2ecSJeff Kirsher 	disable_napi(priv);
2373ec21e2ecSJeff Kirsher 
2374ec21e2ecSJeff Kirsher 	cancel_work_sync(&priv->reset_task);
2375ec21e2ecSJeff Kirsher 	stop_gfar(dev);
2376ec21e2ecSJeff Kirsher 
2377ec21e2ecSJeff Kirsher 	/* Disconnect from the PHY */
2378ec21e2ecSJeff Kirsher 	phy_disconnect(priv->phydev);
2379ec21e2ecSJeff Kirsher 	priv->phydev = NULL;
2380ec21e2ecSJeff Kirsher 
2381ec21e2ecSJeff Kirsher 	netif_tx_stop_all_queues(dev);
2382ec21e2ecSJeff Kirsher 
2383*80ec396cSClaudiu Manoil 	gfar_free_irq(priv);
2384*80ec396cSClaudiu Manoil 
2385ec21e2ecSJeff Kirsher 	return 0;
2386ec21e2ecSJeff Kirsher }
2387ec21e2ecSJeff Kirsher 
2388ec21e2ecSJeff Kirsher /* Changes the mac address if the controller is not running. */
2389ec21e2ecSJeff Kirsher static int gfar_set_mac_address(struct net_device *dev)
2390ec21e2ecSJeff Kirsher {
2391ec21e2ecSJeff Kirsher 	gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
2392ec21e2ecSJeff Kirsher 
2393ec21e2ecSJeff Kirsher 	return 0;
2394ec21e2ecSJeff Kirsher }
2395ec21e2ecSJeff Kirsher 
2396ec21e2ecSJeff Kirsher static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2397ec21e2ecSJeff Kirsher {
2398ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2399ec21e2ecSJeff Kirsher 	int frame_size = new_mtu + ETH_HLEN;
2400ec21e2ecSJeff Kirsher 
2401ec21e2ecSJeff Kirsher 	if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
2402ec21e2ecSJeff Kirsher 		netif_err(priv, drv, dev, "Invalid MTU setting\n");
2403ec21e2ecSJeff Kirsher 		return -EINVAL;
2404ec21e2ecSJeff Kirsher 	}
2405ec21e2ecSJeff Kirsher 
240688302648SClaudiu Manoil 	if (dev->flags & IFF_UP)
2407ec21e2ecSJeff Kirsher 		stop_gfar(dev);
2408ec21e2ecSJeff Kirsher 
2409ec21e2ecSJeff Kirsher 	dev->mtu = new_mtu;
2410ec21e2ecSJeff Kirsher 
241188302648SClaudiu Manoil 	if (dev->flags & IFF_UP)
2412ec21e2ecSJeff Kirsher 		startup_gfar(dev);
2413ec21e2ecSJeff Kirsher 
2414ec21e2ecSJeff Kirsher 	return 0;
2415ec21e2ecSJeff Kirsher }
2416ec21e2ecSJeff Kirsher 
2417ec21e2ecSJeff Kirsher /* gfar_reset_task gets scheduled when a packet has not been
2418ec21e2ecSJeff Kirsher  * transmitted after a set amount of time.
2419ec21e2ecSJeff Kirsher  * For now, assume that clearing out all the structures, and
2420ec21e2ecSJeff Kirsher  * starting over will fix the problem.
2421ec21e2ecSJeff Kirsher  */
2422ec21e2ecSJeff Kirsher static void gfar_reset_task(struct work_struct *work)
2423ec21e2ecSJeff Kirsher {
2424ec21e2ecSJeff Kirsher 	struct gfar_private *priv = container_of(work, struct gfar_private,
2425ec21e2ecSJeff Kirsher 						 reset_task);
2426ec21e2ecSJeff Kirsher 	struct net_device *dev = priv->ndev;
2427ec21e2ecSJeff Kirsher 
2428ec21e2ecSJeff Kirsher 	if (dev->flags & IFF_UP) {
2429ec21e2ecSJeff Kirsher 		netif_tx_stop_all_queues(dev);
2430ec21e2ecSJeff Kirsher 		stop_gfar(dev);
2431ec21e2ecSJeff Kirsher 		startup_gfar(dev);
2432ec21e2ecSJeff Kirsher 		netif_tx_start_all_queues(dev);
2433ec21e2ecSJeff Kirsher 	}
2434ec21e2ecSJeff Kirsher 
2435ec21e2ecSJeff Kirsher 	netif_tx_schedule_all(dev);
2436ec21e2ecSJeff Kirsher }
2437ec21e2ecSJeff Kirsher 
2438ec21e2ecSJeff Kirsher static void gfar_timeout(struct net_device *dev)
2439ec21e2ecSJeff Kirsher {
2440ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2441ec21e2ecSJeff Kirsher 
2442ec21e2ecSJeff Kirsher 	dev->stats.tx_errors++;
2443ec21e2ecSJeff Kirsher 	schedule_work(&priv->reset_task);
2444ec21e2ecSJeff Kirsher }
2445ec21e2ecSJeff Kirsher 
2446ec21e2ecSJeff Kirsher static void gfar_align_skb(struct sk_buff *skb)
2447ec21e2ecSJeff Kirsher {
2448ec21e2ecSJeff Kirsher 	/* We need the data buffer to be aligned properly.  We will reserve
2449ec21e2ecSJeff Kirsher 	 * as many bytes as needed to align the data properly
2450ec21e2ecSJeff Kirsher 	 */
2451ec21e2ecSJeff Kirsher 	skb_reserve(skb, RXBUF_ALIGNMENT -
2452ec21e2ecSJeff Kirsher 		    (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
2453ec21e2ecSJeff Kirsher }
2454ec21e2ecSJeff Kirsher 
2455ec21e2ecSJeff Kirsher /* Interrupt Handler for Transmit complete */
2456c233cf40SClaudiu Manoil static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
2457ec21e2ecSJeff Kirsher {
2458ec21e2ecSJeff Kirsher 	struct net_device *dev = tx_queue->dev;
2459d8a0f1b0SPaul Gortmaker 	struct netdev_queue *txq;
2460ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2461ec21e2ecSJeff Kirsher 	struct txbd8 *bdp, *next = NULL;
2462ec21e2ecSJeff Kirsher 	struct txbd8 *lbdp = NULL;
2463ec21e2ecSJeff Kirsher 	struct txbd8 *base = tx_queue->tx_bd_base;
2464ec21e2ecSJeff Kirsher 	struct sk_buff *skb;
2465ec21e2ecSJeff Kirsher 	int skb_dirtytx;
2466ec21e2ecSJeff Kirsher 	int tx_ring_size = tx_queue->tx_ring_size;
2467ec21e2ecSJeff Kirsher 	int frags = 0, nr_txbds = 0;
2468ec21e2ecSJeff Kirsher 	int i;
2469ec21e2ecSJeff Kirsher 	int howmany = 0;
2470d8a0f1b0SPaul Gortmaker 	int tqi = tx_queue->qindex;
2471d8a0f1b0SPaul Gortmaker 	unsigned int bytes_sent = 0;
2472ec21e2ecSJeff Kirsher 	u32 lstatus;
2473ec21e2ecSJeff Kirsher 	size_t buflen;
2474ec21e2ecSJeff Kirsher 
2475d8a0f1b0SPaul Gortmaker 	txq = netdev_get_tx_queue(dev, tqi);
2476ec21e2ecSJeff Kirsher 	bdp = tx_queue->dirty_tx;
2477ec21e2ecSJeff Kirsher 	skb_dirtytx = tx_queue->skb_dirtytx;
2478ec21e2ecSJeff Kirsher 
2479ec21e2ecSJeff Kirsher 	while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
2480ec21e2ecSJeff Kirsher 		unsigned long flags;
2481ec21e2ecSJeff Kirsher 
2482ec21e2ecSJeff Kirsher 		frags = skb_shinfo(skb)->nr_frags;
2483ec21e2ecSJeff Kirsher 
24840977f817SJan Ceuleers 		/* When time stamping, one additional TxBD must be freed.
2485ec21e2ecSJeff Kirsher 		 * Also, we need to dma_unmap_single() the TxPAL.
2486ec21e2ecSJeff Kirsher 		 */
2487ec21e2ecSJeff Kirsher 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
2488ec21e2ecSJeff Kirsher 			nr_txbds = frags + 2;
2489ec21e2ecSJeff Kirsher 		else
2490ec21e2ecSJeff Kirsher 			nr_txbds = frags + 1;
2491ec21e2ecSJeff Kirsher 
2492ec21e2ecSJeff Kirsher 		lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
2493ec21e2ecSJeff Kirsher 
2494ec21e2ecSJeff Kirsher 		lstatus = lbdp->lstatus;
2495ec21e2ecSJeff Kirsher 
2496ec21e2ecSJeff Kirsher 		/* Only clean completed frames */
2497ec21e2ecSJeff Kirsher 		if ((lstatus & BD_LFLAG(TXBD_READY)) &&
2498ec21e2ecSJeff Kirsher 		    (lstatus & BD_LENGTH_MASK))
2499ec21e2ecSJeff Kirsher 			break;
2500ec21e2ecSJeff Kirsher 
2501ec21e2ecSJeff Kirsher 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
2502ec21e2ecSJeff Kirsher 			next = next_txbd(bdp, base, tx_ring_size);
25039c4886e5SManfred Rudigier 			buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2504ec21e2ecSJeff Kirsher 		} else
2505ec21e2ecSJeff Kirsher 			buflen = bdp->length;
2506ec21e2ecSJeff Kirsher 
2507369ec162SClaudiu Manoil 		dma_unmap_single(priv->dev, bdp->bufPtr,
2508ec21e2ecSJeff Kirsher 				 buflen, DMA_TO_DEVICE);
2509ec21e2ecSJeff Kirsher 
2510ec21e2ecSJeff Kirsher 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
2511ec21e2ecSJeff Kirsher 			struct skb_shared_hwtstamps shhwtstamps;
2512ec21e2ecSJeff Kirsher 			u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
2513bc4598bcSJan Ceuleers 
2514ec21e2ecSJeff Kirsher 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2515ec21e2ecSJeff Kirsher 			shhwtstamps.hwtstamp = ns_to_ktime(*ns);
25169c4886e5SManfred Rudigier 			skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
2517ec21e2ecSJeff Kirsher 			skb_tstamp_tx(skb, &shhwtstamps);
2518ec21e2ecSJeff Kirsher 			bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2519ec21e2ecSJeff Kirsher 			bdp = next;
2520ec21e2ecSJeff Kirsher 		}
2521ec21e2ecSJeff Kirsher 
2522ec21e2ecSJeff Kirsher 		bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2523ec21e2ecSJeff Kirsher 		bdp = next_txbd(bdp, base, tx_ring_size);
2524ec21e2ecSJeff Kirsher 
2525ec21e2ecSJeff Kirsher 		for (i = 0; i < frags; i++) {
2526369ec162SClaudiu Manoil 			dma_unmap_page(priv->dev, bdp->bufPtr,
2527bc4598bcSJan Ceuleers 				       bdp->length, DMA_TO_DEVICE);
2528ec21e2ecSJeff Kirsher 			bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2529ec21e2ecSJeff Kirsher 			bdp = next_txbd(bdp, base, tx_ring_size);
2530ec21e2ecSJeff Kirsher 		}
2531ec21e2ecSJeff Kirsher 
253250ad076bSClaudiu Manoil 		bytes_sent += GFAR_CB(skb)->bytes_sent;
2533d8a0f1b0SPaul Gortmaker 
2534ec21e2ecSJeff Kirsher 		dev_kfree_skb_any(skb);
2535ec21e2ecSJeff Kirsher 
2536ec21e2ecSJeff Kirsher 		tx_queue->tx_skbuff[skb_dirtytx] = NULL;
2537ec21e2ecSJeff Kirsher 
2538ec21e2ecSJeff Kirsher 		skb_dirtytx = (skb_dirtytx + 1) &
2539ec21e2ecSJeff Kirsher 			      TX_RING_MOD_MASK(tx_ring_size);
2540ec21e2ecSJeff Kirsher 
2541ec21e2ecSJeff Kirsher 		howmany++;
2542ec21e2ecSJeff Kirsher 		spin_lock_irqsave(&tx_queue->txlock, flags);
2543ec21e2ecSJeff Kirsher 		tx_queue->num_txbdfree += nr_txbds;
2544ec21e2ecSJeff Kirsher 		spin_unlock_irqrestore(&tx_queue->txlock, flags);
2545ec21e2ecSJeff Kirsher 	}
2546ec21e2ecSJeff Kirsher 
2547ec21e2ecSJeff Kirsher 	/* If we freed a buffer, we can restart transmission, if necessary */
25485407b14cSPaul Gortmaker 	if (netif_tx_queue_stopped(txq) && tx_queue->num_txbdfree)
2549d8a0f1b0SPaul Gortmaker 		netif_wake_subqueue(dev, tqi);
2550ec21e2ecSJeff Kirsher 
2551ec21e2ecSJeff Kirsher 	/* Update dirty indicators */
2552ec21e2ecSJeff Kirsher 	tx_queue->skb_dirtytx = skb_dirtytx;
2553ec21e2ecSJeff Kirsher 	tx_queue->dirty_tx = bdp;
2554ec21e2ecSJeff Kirsher 
2555d8a0f1b0SPaul Gortmaker 	netdev_tx_completed_queue(txq, howmany, bytes_sent);
2556ec21e2ecSJeff Kirsher }
2557ec21e2ecSJeff Kirsher 
2558ec21e2ecSJeff Kirsher static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
2559ec21e2ecSJeff Kirsher {
2560ec21e2ecSJeff Kirsher 	unsigned long flags;
2561ec21e2ecSJeff Kirsher 
2562ec21e2ecSJeff Kirsher 	spin_lock_irqsave(&gfargrp->grplock, flags);
2563ec21e2ecSJeff Kirsher 	if (napi_schedule_prep(&gfargrp->napi)) {
2564ec21e2ecSJeff Kirsher 		gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
2565ec21e2ecSJeff Kirsher 		__napi_schedule(&gfargrp->napi);
2566ec21e2ecSJeff Kirsher 	} else {
25670977f817SJan Ceuleers 		/* Clear IEVENT, so interrupts aren't called again
2568ec21e2ecSJeff Kirsher 		 * because of the packets that have already arrived.
2569ec21e2ecSJeff Kirsher 		 */
2570ec21e2ecSJeff Kirsher 		gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
2571ec21e2ecSJeff Kirsher 	}
2572ec21e2ecSJeff Kirsher 	spin_unlock_irqrestore(&gfargrp->grplock, flags);
2573ec21e2ecSJeff Kirsher 
2574ec21e2ecSJeff Kirsher }
2575ec21e2ecSJeff Kirsher 
2576ec21e2ecSJeff Kirsher /* Interrupt Handler for Transmit complete */
2577ec21e2ecSJeff Kirsher static irqreturn_t gfar_transmit(int irq, void *grp_id)
2578ec21e2ecSJeff Kirsher {
2579ec21e2ecSJeff Kirsher 	gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
2580ec21e2ecSJeff Kirsher 	return IRQ_HANDLED;
2581ec21e2ecSJeff Kirsher }
2582ec21e2ecSJeff Kirsher 
2583ec21e2ecSJeff Kirsher static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
2584ec21e2ecSJeff Kirsher 			   struct sk_buff *skb)
2585ec21e2ecSJeff Kirsher {
2586ec21e2ecSJeff Kirsher 	struct net_device *dev = rx_queue->dev;
2587ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2588ec21e2ecSJeff Kirsher 	dma_addr_t buf;
2589ec21e2ecSJeff Kirsher 
2590369ec162SClaudiu Manoil 	buf = dma_map_single(priv->dev, skb->data,
2591ec21e2ecSJeff Kirsher 			     priv->rx_buffer_size, DMA_FROM_DEVICE);
2592ec21e2ecSJeff Kirsher 	gfar_init_rxbdp(rx_queue, bdp, buf);
2593ec21e2ecSJeff Kirsher }
2594ec21e2ecSJeff Kirsher 
2595ec21e2ecSJeff Kirsher static struct sk_buff *gfar_alloc_skb(struct net_device *dev)
2596ec21e2ecSJeff Kirsher {
2597ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2598acb600deSEric Dumazet 	struct sk_buff *skb;
2599ec21e2ecSJeff Kirsher 
2600ec21e2ecSJeff Kirsher 	skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
2601ec21e2ecSJeff Kirsher 	if (!skb)
2602ec21e2ecSJeff Kirsher 		return NULL;
2603ec21e2ecSJeff Kirsher 
2604ec21e2ecSJeff Kirsher 	gfar_align_skb(skb);
2605ec21e2ecSJeff Kirsher 
2606ec21e2ecSJeff Kirsher 	return skb;
2607ec21e2ecSJeff Kirsher }
2608ec21e2ecSJeff Kirsher 
2609ec21e2ecSJeff Kirsher struct sk_buff *gfar_new_skb(struct net_device *dev)
2610ec21e2ecSJeff Kirsher {
2611acb600deSEric Dumazet 	return gfar_alloc_skb(dev);
2612ec21e2ecSJeff Kirsher }
2613ec21e2ecSJeff Kirsher 
2614ec21e2ecSJeff Kirsher static inline void count_errors(unsigned short status, struct net_device *dev)
2615ec21e2ecSJeff Kirsher {
2616ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2617ec21e2ecSJeff Kirsher 	struct net_device_stats *stats = &dev->stats;
2618ec21e2ecSJeff Kirsher 	struct gfar_extra_stats *estats = &priv->extra_stats;
2619ec21e2ecSJeff Kirsher 
26200977f817SJan Ceuleers 	/* If the packet was truncated, none of the other errors matter */
2621ec21e2ecSJeff Kirsher 	if (status & RXBD_TRUNCATED) {
2622ec21e2ecSJeff Kirsher 		stats->rx_length_errors++;
2623ec21e2ecSJeff Kirsher 
2624212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_trunc);
2625ec21e2ecSJeff Kirsher 
2626ec21e2ecSJeff Kirsher 		return;
2627ec21e2ecSJeff Kirsher 	}
2628ec21e2ecSJeff Kirsher 	/* Count the errors, if there were any */
2629ec21e2ecSJeff Kirsher 	if (status & (RXBD_LARGE | RXBD_SHORT)) {
2630ec21e2ecSJeff Kirsher 		stats->rx_length_errors++;
2631ec21e2ecSJeff Kirsher 
2632ec21e2ecSJeff Kirsher 		if (status & RXBD_LARGE)
2633212079dfSPaul Gortmaker 			atomic64_inc(&estats->rx_large);
2634ec21e2ecSJeff Kirsher 		else
2635212079dfSPaul Gortmaker 			atomic64_inc(&estats->rx_short);
2636ec21e2ecSJeff Kirsher 	}
2637ec21e2ecSJeff Kirsher 	if (status & RXBD_NONOCTET) {
2638ec21e2ecSJeff Kirsher 		stats->rx_frame_errors++;
2639212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_nonoctet);
2640ec21e2ecSJeff Kirsher 	}
2641ec21e2ecSJeff Kirsher 	if (status & RXBD_CRCERR) {
2642212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_crcerr);
2643ec21e2ecSJeff Kirsher 		stats->rx_crc_errors++;
2644ec21e2ecSJeff Kirsher 	}
2645ec21e2ecSJeff Kirsher 	if (status & RXBD_OVERRUN) {
2646212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_overrun);
2647ec21e2ecSJeff Kirsher 		stats->rx_crc_errors++;
2648ec21e2ecSJeff Kirsher 	}
2649ec21e2ecSJeff Kirsher }
2650ec21e2ecSJeff Kirsher 
2651ec21e2ecSJeff Kirsher irqreturn_t gfar_receive(int irq, void *grp_id)
2652ec21e2ecSJeff Kirsher {
2653ec21e2ecSJeff Kirsher 	gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
2654ec21e2ecSJeff Kirsher 	return IRQ_HANDLED;
2655ec21e2ecSJeff Kirsher }
2656ec21e2ecSJeff Kirsher 
2657ec21e2ecSJeff Kirsher static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2658ec21e2ecSJeff Kirsher {
2659ec21e2ecSJeff Kirsher 	/* If valid headers were found, and valid sums
2660ec21e2ecSJeff Kirsher 	 * were verified, then we tell the kernel that no
26610977f817SJan Ceuleers 	 * checksumming is necessary.  Otherwise, it is [FIXME]
26620977f817SJan Ceuleers 	 */
2663ec21e2ecSJeff Kirsher 	if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
2664ec21e2ecSJeff Kirsher 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2665ec21e2ecSJeff Kirsher 	else
2666ec21e2ecSJeff Kirsher 		skb_checksum_none_assert(skb);
2667ec21e2ecSJeff Kirsher }
2668ec21e2ecSJeff Kirsher 
2669ec21e2ecSJeff Kirsher 
26700977f817SJan Ceuleers /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
267161db26c6SClaudiu Manoil static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
2672cd754a57SWu Jiajun-B06378 			       int amount_pull, struct napi_struct *napi)
2673ec21e2ecSJeff Kirsher {
2674ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2675ec21e2ecSJeff Kirsher 	struct rxfcb *fcb = NULL;
2676ec21e2ecSJeff Kirsher 
2677ec21e2ecSJeff Kirsher 	/* fcb is at the beginning if exists */
2678ec21e2ecSJeff Kirsher 	fcb = (struct rxfcb *)skb->data;
2679ec21e2ecSJeff Kirsher 
26800977f817SJan Ceuleers 	/* Remove the FCB from the skb
26810977f817SJan Ceuleers 	 * Remove the padded bytes, if there are any
26820977f817SJan Ceuleers 	 */
2683ec21e2ecSJeff Kirsher 	if (amount_pull) {
2684ec21e2ecSJeff Kirsher 		skb_record_rx_queue(skb, fcb->rq);
2685ec21e2ecSJeff Kirsher 		skb_pull(skb, amount_pull);
2686ec21e2ecSJeff Kirsher 	}
2687ec21e2ecSJeff Kirsher 
2688ec21e2ecSJeff Kirsher 	/* Get receive timestamp from the skb */
2689ec21e2ecSJeff Kirsher 	if (priv->hwts_rx_en) {
2690ec21e2ecSJeff Kirsher 		struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2691ec21e2ecSJeff Kirsher 		u64 *ns = (u64 *) skb->data;
2692bc4598bcSJan Ceuleers 
2693ec21e2ecSJeff Kirsher 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2694ec21e2ecSJeff Kirsher 		shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2695ec21e2ecSJeff Kirsher 	}
2696ec21e2ecSJeff Kirsher 
2697ec21e2ecSJeff Kirsher 	if (priv->padding)
2698ec21e2ecSJeff Kirsher 		skb_pull(skb, priv->padding);
2699ec21e2ecSJeff Kirsher 
2700ec21e2ecSJeff Kirsher 	if (dev->features & NETIF_F_RXCSUM)
2701ec21e2ecSJeff Kirsher 		gfar_rx_checksum(skb, fcb);
2702ec21e2ecSJeff Kirsher 
2703ec21e2ecSJeff Kirsher 	/* Tell the skb what kind of packet this is */
2704ec21e2ecSJeff Kirsher 	skb->protocol = eth_type_trans(skb, dev);
2705ec21e2ecSJeff Kirsher 
2706f646968fSPatrick McHardy 	/* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
2707823dcd25SDavid S. Miller 	 * Even if vlan rx accel is disabled, on some chips
2708823dcd25SDavid S. Miller 	 * RXFCB_VLN is pseudo randomly set.
2709823dcd25SDavid S. Miller 	 */
2710f646968fSPatrick McHardy 	if (dev->features & NETIF_F_HW_VLAN_CTAG_RX &&
2711823dcd25SDavid S. Miller 	    fcb->flags & RXFCB_VLN)
2712e5905c83SDavid S. Miller 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), fcb->vlctl);
2713ec21e2ecSJeff Kirsher 
2714ec21e2ecSJeff Kirsher 	/* Send the packet up the stack */
2715953d2768SClaudiu Manoil 	napi_gro_receive(napi, skb);
2716ec21e2ecSJeff Kirsher 
2717ec21e2ecSJeff Kirsher }
2718ec21e2ecSJeff Kirsher 
2719ec21e2ecSJeff Kirsher /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
2720ec21e2ecSJeff Kirsher  * until the budget/quota has been reached. Returns the number
2721ec21e2ecSJeff Kirsher  * of frames handled
2722ec21e2ecSJeff Kirsher  */
2723ec21e2ecSJeff Kirsher int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
2724ec21e2ecSJeff Kirsher {
2725ec21e2ecSJeff Kirsher 	struct net_device *dev = rx_queue->dev;
2726ec21e2ecSJeff Kirsher 	struct rxbd8 *bdp, *base;
2727ec21e2ecSJeff Kirsher 	struct sk_buff *skb;
2728ec21e2ecSJeff Kirsher 	int pkt_len;
2729ec21e2ecSJeff Kirsher 	int amount_pull;
2730ec21e2ecSJeff Kirsher 	int howmany = 0;
2731ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2732ec21e2ecSJeff Kirsher 
2733ec21e2ecSJeff Kirsher 	/* Get the first full descriptor */
2734ec21e2ecSJeff Kirsher 	bdp = rx_queue->cur_rx;
2735ec21e2ecSJeff Kirsher 	base = rx_queue->rx_bd_base;
2736ec21e2ecSJeff Kirsher 
2737ba779711SClaudiu Manoil 	amount_pull = priv->uses_rxfcb ? GMAC_FCB_LEN : 0;
2738ec21e2ecSJeff Kirsher 
2739ec21e2ecSJeff Kirsher 	while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
2740ec21e2ecSJeff Kirsher 		struct sk_buff *newskb;
2741bc4598bcSJan Ceuleers 
2742ec21e2ecSJeff Kirsher 		rmb();
2743ec21e2ecSJeff Kirsher 
2744ec21e2ecSJeff Kirsher 		/* Add another skb for the future */
2745ec21e2ecSJeff Kirsher 		newskb = gfar_new_skb(dev);
2746ec21e2ecSJeff Kirsher 
2747ec21e2ecSJeff Kirsher 		skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
2748ec21e2ecSJeff Kirsher 
2749369ec162SClaudiu Manoil 		dma_unmap_single(priv->dev, bdp->bufPtr,
2750ec21e2ecSJeff Kirsher 				 priv->rx_buffer_size, DMA_FROM_DEVICE);
2751ec21e2ecSJeff Kirsher 
2752ec21e2ecSJeff Kirsher 		if (unlikely(!(bdp->status & RXBD_ERR) &&
2753ec21e2ecSJeff Kirsher 			     bdp->length > priv->rx_buffer_size))
2754ec21e2ecSJeff Kirsher 			bdp->status = RXBD_LARGE;
2755ec21e2ecSJeff Kirsher 
2756ec21e2ecSJeff Kirsher 		/* We drop the frame if we failed to allocate a new buffer */
2757ec21e2ecSJeff Kirsher 		if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
2758ec21e2ecSJeff Kirsher 			     bdp->status & RXBD_ERR)) {
2759ec21e2ecSJeff Kirsher 			count_errors(bdp->status, dev);
2760ec21e2ecSJeff Kirsher 
2761ec21e2ecSJeff Kirsher 			if (unlikely(!newskb))
2762ec21e2ecSJeff Kirsher 				newskb = skb;
2763ec21e2ecSJeff Kirsher 			else if (skb)
2764acb600deSEric Dumazet 				dev_kfree_skb(skb);
2765ec21e2ecSJeff Kirsher 		} else {
2766ec21e2ecSJeff Kirsher 			/* Increment the number of packets */
2767ec21e2ecSJeff Kirsher 			rx_queue->stats.rx_packets++;
2768ec21e2ecSJeff Kirsher 			howmany++;
2769ec21e2ecSJeff Kirsher 
2770ec21e2ecSJeff Kirsher 			if (likely(skb)) {
2771ec21e2ecSJeff Kirsher 				pkt_len = bdp->length - ETH_FCS_LEN;
2772ec21e2ecSJeff Kirsher 				/* Remove the FCS from the packet length */
2773ec21e2ecSJeff Kirsher 				skb_put(skb, pkt_len);
2774ec21e2ecSJeff Kirsher 				rx_queue->stats.rx_bytes += pkt_len;
2775ec21e2ecSJeff Kirsher 				skb_record_rx_queue(skb, rx_queue->qindex);
2776cd754a57SWu Jiajun-B06378 				gfar_process_frame(dev, skb, amount_pull,
2777cd754a57SWu Jiajun-B06378 						   &rx_queue->grp->napi);
2778ec21e2ecSJeff Kirsher 
2779ec21e2ecSJeff Kirsher 			} else {
2780ec21e2ecSJeff Kirsher 				netif_warn(priv, rx_err, dev, "Missing skb!\n");
2781ec21e2ecSJeff Kirsher 				rx_queue->stats.rx_dropped++;
2782212079dfSPaul Gortmaker 				atomic64_inc(&priv->extra_stats.rx_skbmissing);
2783ec21e2ecSJeff Kirsher 			}
2784ec21e2ecSJeff Kirsher 
2785ec21e2ecSJeff Kirsher 		}
2786ec21e2ecSJeff Kirsher 
2787ec21e2ecSJeff Kirsher 		rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
2788ec21e2ecSJeff Kirsher 
2789ec21e2ecSJeff Kirsher 		/* Setup the new bdp */
2790ec21e2ecSJeff Kirsher 		gfar_new_rxbdp(rx_queue, bdp, newskb);
2791ec21e2ecSJeff Kirsher 
2792ec21e2ecSJeff Kirsher 		/* Update to the next pointer */
2793ec21e2ecSJeff Kirsher 		bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
2794ec21e2ecSJeff Kirsher 
2795ec21e2ecSJeff Kirsher 		/* update to point at the next skb */
2796bc4598bcSJan Ceuleers 		rx_queue->skb_currx = (rx_queue->skb_currx + 1) &
2797ec21e2ecSJeff Kirsher 				      RX_RING_MOD_MASK(rx_queue->rx_ring_size);
2798ec21e2ecSJeff Kirsher 	}
2799ec21e2ecSJeff Kirsher 
2800ec21e2ecSJeff Kirsher 	/* Update the current rxbd pointer to be the next one */
2801ec21e2ecSJeff Kirsher 	rx_queue->cur_rx = bdp;
2802ec21e2ecSJeff Kirsher 
2803ec21e2ecSJeff Kirsher 	return howmany;
2804ec21e2ecSJeff Kirsher }
2805ec21e2ecSJeff Kirsher 
28065eaedf31SClaudiu Manoil static int gfar_poll_sq(struct napi_struct *napi, int budget)
28075eaedf31SClaudiu Manoil {
28085eaedf31SClaudiu Manoil 	struct gfar_priv_grp *gfargrp =
28095eaedf31SClaudiu Manoil 		container_of(napi, struct gfar_priv_grp, napi);
28105eaedf31SClaudiu Manoil 	struct gfar __iomem *regs = gfargrp->regs;
28115eaedf31SClaudiu Manoil 	struct gfar_priv_tx_q *tx_queue = gfargrp->priv->tx_queue[0];
28125eaedf31SClaudiu Manoil 	struct gfar_priv_rx_q *rx_queue = gfargrp->priv->rx_queue[0];
28135eaedf31SClaudiu Manoil 	int work_done = 0;
28145eaedf31SClaudiu Manoil 
28155eaedf31SClaudiu Manoil 	/* Clear IEVENT, so interrupts aren't called again
28165eaedf31SClaudiu Manoil 	 * because of the packets that have already arrived
28175eaedf31SClaudiu Manoil 	 */
28185eaedf31SClaudiu Manoil 	gfar_write(&regs->ievent, IEVENT_RTX_MASK);
28195eaedf31SClaudiu Manoil 
28205eaedf31SClaudiu Manoil 	/* run Tx cleanup to completion */
28215eaedf31SClaudiu Manoil 	if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
28225eaedf31SClaudiu Manoil 		gfar_clean_tx_ring(tx_queue);
28235eaedf31SClaudiu Manoil 
28245eaedf31SClaudiu Manoil 	work_done = gfar_clean_rx_ring(rx_queue, budget);
28255eaedf31SClaudiu Manoil 
28265eaedf31SClaudiu Manoil 	if (work_done < budget) {
28275eaedf31SClaudiu Manoil 		napi_complete(napi);
28285eaedf31SClaudiu Manoil 		/* Clear the halt bit in RSTAT */
28295eaedf31SClaudiu Manoil 		gfar_write(&regs->rstat, gfargrp->rstat);
28305eaedf31SClaudiu Manoil 
28315eaedf31SClaudiu Manoil 		gfar_write(&regs->imask, IMASK_DEFAULT);
28325eaedf31SClaudiu Manoil 
28335eaedf31SClaudiu Manoil 		/* If we are coalescing interrupts, update the timer
28345eaedf31SClaudiu Manoil 		 * Otherwise, clear it
28355eaedf31SClaudiu Manoil 		 */
28365eaedf31SClaudiu Manoil 		gfar_write(&regs->txic, 0);
28375eaedf31SClaudiu Manoil 		if (likely(tx_queue->txcoalescing))
28385eaedf31SClaudiu Manoil 			gfar_write(&regs->txic, tx_queue->txic);
28395eaedf31SClaudiu Manoil 
28405eaedf31SClaudiu Manoil 		gfar_write(&regs->rxic, 0);
28415eaedf31SClaudiu Manoil 		if (unlikely(rx_queue->rxcoalescing))
28425eaedf31SClaudiu Manoil 			gfar_write(&regs->rxic, rx_queue->rxic);
28435eaedf31SClaudiu Manoil 	}
28445eaedf31SClaudiu Manoil 
28455eaedf31SClaudiu Manoil 	return work_done;
28465eaedf31SClaudiu Manoil }
28475eaedf31SClaudiu Manoil 
2848ec21e2ecSJeff Kirsher static int gfar_poll(struct napi_struct *napi, int budget)
2849ec21e2ecSJeff Kirsher {
2850bc4598bcSJan Ceuleers 	struct gfar_priv_grp *gfargrp =
2851bc4598bcSJan Ceuleers 		container_of(napi, struct gfar_priv_grp, napi);
2852ec21e2ecSJeff Kirsher 	struct gfar_private *priv = gfargrp->priv;
2853ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = gfargrp->regs;
2854ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
2855ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
2856c233cf40SClaudiu Manoil 	int work_done = 0, work_done_per_q = 0;
285739c0a0d5SClaudiu Manoil 	int i, budget_per_q = 0;
28583ba405dbSClaudiu Manoil 	int has_tx_work = 0;
28596be5ed3fSClaudiu Manoil 	unsigned long rstat_rxf;
28606be5ed3fSClaudiu Manoil 	int num_act_queues;
2861ec21e2ecSJeff Kirsher 
2862ec21e2ecSJeff Kirsher 	/* Clear IEVENT, so interrupts aren't called again
28630977f817SJan Ceuleers 	 * because of the packets that have already arrived
28640977f817SJan Ceuleers 	 */
2865ec21e2ecSJeff Kirsher 	gfar_write(&regs->ievent, IEVENT_RTX_MASK);
2866ec21e2ecSJeff Kirsher 
28676be5ed3fSClaudiu Manoil 	rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
28686be5ed3fSClaudiu Manoil 
28696be5ed3fSClaudiu Manoil 	num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
28706be5ed3fSClaudiu Manoil 	if (num_act_queues)
28716be5ed3fSClaudiu Manoil 		budget_per_q = budget/num_act_queues;
28726be5ed3fSClaudiu Manoil 
2873c233cf40SClaudiu Manoil 	for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
2874c233cf40SClaudiu Manoil 		tx_queue = priv->tx_queue[i];
2875c233cf40SClaudiu Manoil 		/* run Tx cleanup to completion */
2876c233cf40SClaudiu Manoil 		if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
2877c233cf40SClaudiu Manoil 			gfar_clean_tx_ring(tx_queue);
2878c233cf40SClaudiu Manoil 			has_tx_work = 1;
2879c233cf40SClaudiu Manoil 		}
2880c233cf40SClaudiu Manoil 	}
2881ec21e2ecSJeff Kirsher 
2882ec21e2ecSJeff Kirsher 	for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
28836be5ed3fSClaudiu Manoil 		/* skip queue if not active */
28846be5ed3fSClaudiu Manoil 		if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
2885ec21e2ecSJeff Kirsher 			continue;
2886ec21e2ecSJeff Kirsher 
2887c233cf40SClaudiu Manoil 		rx_queue = priv->rx_queue[i];
2888c233cf40SClaudiu Manoil 		work_done_per_q =
2889c233cf40SClaudiu Manoil 			gfar_clean_rx_ring(rx_queue, budget_per_q);
2890c233cf40SClaudiu Manoil 		work_done += work_done_per_q;
2891c233cf40SClaudiu Manoil 
2892c233cf40SClaudiu Manoil 		/* finished processing this queue */
2893c233cf40SClaudiu Manoil 		if (work_done_per_q < budget_per_q) {
28946be5ed3fSClaudiu Manoil 			/* clear active queue hw indication */
28956be5ed3fSClaudiu Manoil 			gfar_write(&regs->rstat,
28966be5ed3fSClaudiu Manoil 				   RSTAT_CLEAR_RXF0 >> i);
28976be5ed3fSClaudiu Manoil 			num_act_queues--;
28986be5ed3fSClaudiu Manoil 
28996be5ed3fSClaudiu Manoil 			if (!num_act_queues)
2900c233cf40SClaudiu Manoil 				break;
2901ec21e2ecSJeff Kirsher 		}
2902ec21e2ecSJeff Kirsher 	}
2903ec21e2ecSJeff Kirsher 
29046be5ed3fSClaudiu Manoil 	if (!num_act_queues && !has_tx_work) {
2905c233cf40SClaudiu Manoil 
2906ec21e2ecSJeff Kirsher 		napi_complete(napi);
2907ec21e2ecSJeff Kirsher 
2908ec21e2ecSJeff Kirsher 		/* Clear the halt bit in RSTAT */
2909ec21e2ecSJeff Kirsher 		gfar_write(&regs->rstat, gfargrp->rstat);
2910ec21e2ecSJeff Kirsher 
2911ec21e2ecSJeff Kirsher 		gfar_write(&regs->imask, IMASK_DEFAULT);
2912ec21e2ecSJeff Kirsher 
29130977f817SJan Ceuleers 		/* If we are coalescing interrupts, update the timer
29140977f817SJan Ceuleers 		 * Otherwise, clear it
29150977f817SJan Ceuleers 		 */
2916bc4598bcSJan Ceuleers 		gfar_configure_coalescing(priv, gfargrp->rx_bit_map,
2917bc4598bcSJan Ceuleers 					  gfargrp->tx_bit_map);
2918ec21e2ecSJeff Kirsher 	}
2919ec21e2ecSJeff Kirsher 
2920c233cf40SClaudiu Manoil 	return work_done;
2921ec21e2ecSJeff Kirsher }
2922ec21e2ecSJeff Kirsher 
2923ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
29240977f817SJan Ceuleers /* Polling 'interrupt' - used by things like netconsole to send skbs
2925ec21e2ecSJeff Kirsher  * without having to re-enable interrupts. It's not called while
2926ec21e2ecSJeff Kirsher  * the interrupt routine is executing.
2927ec21e2ecSJeff Kirsher  */
2928ec21e2ecSJeff Kirsher static void gfar_netpoll(struct net_device *dev)
2929ec21e2ecSJeff Kirsher {
2930ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
29313a2e16c8SJan Ceuleers 	int i;
2932ec21e2ecSJeff Kirsher 
2933ec21e2ecSJeff Kirsher 	/* If the device has multiple interrupts, run tx/rx */
2934ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2935ec21e2ecSJeff Kirsher 		for (i = 0; i < priv->num_grps; i++) {
293662ed839dSPaul Gortmaker 			struct gfar_priv_grp *grp = &priv->gfargrp[i];
293762ed839dSPaul Gortmaker 
293862ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, TX)->irq);
293962ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, RX)->irq);
294062ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, ER)->irq);
294162ed839dSPaul Gortmaker 			gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
294262ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, ER)->irq);
294362ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, RX)->irq);
294462ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, TX)->irq);
2945ec21e2ecSJeff Kirsher 		}
2946ec21e2ecSJeff Kirsher 	} else {
2947ec21e2ecSJeff Kirsher 		for (i = 0; i < priv->num_grps; i++) {
294862ed839dSPaul Gortmaker 			struct gfar_priv_grp *grp = &priv->gfargrp[i];
294962ed839dSPaul Gortmaker 
295062ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, TX)->irq);
295162ed839dSPaul Gortmaker 			gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
295262ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, TX)->irq);
2953ec21e2ecSJeff Kirsher 		}
2954ec21e2ecSJeff Kirsher 	}
2955ec21e2ecSJeff Kirsher }
2956ec21e2ecSJeff Kirsher #endif
2957ec21e2ecSJeff Kirsher 
2958ec21e2ecSJeff Kirsher /* The interrupt handler for devices with one interrupt */
2959ec21e2ecSJeff Kirsher static irqreturn_t gfar_interrupt(int irq, void *grp_id)
2960ec21e2ecSJeff Kirsher {
2961ec21e2ecSJeff Kirsher 	struct gfar_priv_grp *gfargrp = grp_id;
2962ec21e2ecSJeff Kirsher 
2963ec21e2ecSJeff Kirsher 	/* Save ievent for future reference */
2964ec21e2ecSJeff Kirsher 	u32 events = gfar_read(&gfargrp->regs->ievent);
2965ec21e2ecSJeff Kirsher 
2966ec21e2ecSJeff Kirsher 	/* Check for reception */
2967ec21e2ecSJeff Kirsher 	if (events & IEVENT_RX_MASK)
2968ec21e2ecSJeff Kirsher 		gfar_receive(irq, grp_id);
2969ec21e2ecSJeff Kirsher 
2970ec21e2ecSJeff Kirsher 	/* Check for transmit completion */
2971ec21e2ecSJeff Kirsher 	if (events & IEVENT_TX_MASK)
2972ec21e2ecSJeff Kirsher 		gfar_transmit(irq, grp_id);
2973ec21e2ecSJeff Kirsher 
2974ec21e2ecSJeff Kirsher 	/* Check for errors */
2975ec21e2ecSJeff Kirsher 	if (events & IEVENT_ERR_MASK)
2976ec21e2ecSJeff Kirsher 		gfar_error(irq, grp_id);
2977ec21e2ecSJeff Kirsher 
2978ec21e2ecSJeff Kirsher 	return IRQ_HANDLED;
2979ec21e2ecSJeff Kirsher }
2980ec21e2ecSJeff Kirsher 
298123402bddSClaudiu Manoil static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
298223402bddSClaudiu Manoil {
298323402bddSClaudiu Manoil 	struct phy_device *phydev = priv->phydev;
298423402bddSClaudiu Manoil 	u32 val = 0;
298523402bddSClaudiu Manoil 
298623402bddSClaudiu Manoil 	if (!phydev->duplex)
298723402bddSClaudiu Manoil 		return val;
298823402bddSClaudiu Manoil 
298923402bddSClaudiu Manoil 	if (!priv->pause_aneg_en) {
299023402bddSClaudiu Manoil 		if (priv->tx_pause_en)
299123402bddSClaudiu Manoil 			val |= MACCFG1_TX_FLOW;
299223402bddSClaudiu Manoil 		if (priv->rx_pause_en)
299323402bddSClaudiu Manoil 			val |= MACCFG1_RX_FLOW;
299423402bddSClaudiu Manoil 	} else {
299523402bddSClaudiu Manoil 		u16 lcl_adv, rmt_adv;
299623402bddSClaudiu Manoil 		u8 flowctrl;
299723402bddSClaudiu Manoil 		/* get link partner capabilities */
299823402bddSClaudiu Manoil 		rmt_adv = 0;
299923402bddSClaudiu Manoil 		if (phydev->pause)
300023402bddSClaudiu Manoil 			rmt_adv = LPA_PAUSE_CAP;
300123402bddSClaudiu Manoil 		if (phydev->asym_pause)
300223402bddSClaudiu Manoil 			rmt_adv |= LPA_PAUSE_ASYM;
300323402bddSClaudiu Manoil 
300423402bddSClaudiu Manoil 		lcl_adv = mii_advertise_flowctrl(phydev->advertising);
300523402bddSClaudiu Manoil 
300623402bddSClaudiu Manoil 		flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
300723402bddSClaudiu Manoil 		if (flowctrl & FLOW_CTRL_TX)
300823402bddSClaudiu Manoil 			val |= MACCFG1_TX_FLOW;
300923402bddSClaudiu Manoil 		if (flowctrl & FLOW_CTRL_RX)
301023402bddSClaudiu Manoil 			val |= MACCFG1_RX_FLOW;
301123402bddSClaudiu Manoil 	}
301223402bddSClaudiu Manoil 
301323402bddSClaudiu Manoil 	return val;
301423402bddSClaudiu Manoil }
301523402bddSClaudiu Manoil 
3016ec21e2ecSJeff Kirsher /* Called every time the controller might need to be made
3017ec21e2ecSJeff Kirsher  * aware of new link state.  The PHY code conveys this
3018ec21e2ecSJeff Kirsher  * information through variables in the phydev structure, and this
3019ec21e2ecSJeff Kirsher  * function converts those variables into the appropriate
3020ec21e2ecSJeff Kirsher  * register values, and can bring down the device if needed.
3021ec21e2ecSJeff Kirsher  */
3022ec21e2ecSJeff Kirsher static void adjust_link(struct net_device *dev)
3023ec21e2ecSJeff Kirsher {
3024ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
3025ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3026ec21e2ecSJeff Kirsher 	unsigned long flags;
3027ec21e2ecSJeff Kirsher 	struct phy_device *phydev = priv->phydev;
3028ec21e2ecSJeff Kirsher 	int new_state = 0;
3029ec21e2ecSJeff Kirsher 
3030ec21e2ecSJeff Kirsher 	local_irq_save(flags);
3031ec21e2ecSJeff Kirsher 	lock_tx_qs(priv);
3032ec21e2ecSJeff Kirsher 
3033ec21e2ecSJeff Kirsher 	if (phydev->link) {
303423402bddSClaudiu Manoil 		u32 tempval1 = gfar_read(&regs->maccfg1);
3035ec21e2ecSJeff Kirsher 		u32 tempval = gfar_read(&regs->maccfg2);
3036ec21e2ecSJeff Kirsher 		u32 ecntrl = gfar_read(&regs->ecntrl);
3037ec21e2ecSJeff Kirsher 
3038ec21e2ecSJeff Kirsher 		/* Now we make sure that we can be in full duplex mode.
30390977f817SJan Ceuleers 		 * If not, we operate in half-duplex mode.
30400977f817SJan Ceuleers 		 */
3041ec21e2ecSJeff Kirsher 		if (phydev->duplex != priv->oldduplex) {
3042ec21e2ecSJeff Kirsher 			new_state = 1;
3043ec21e2ecSJeff Kirsher 			if (!(phydev->duplex))
3044ec21e2ecSJeff Kirsher 				tempval &= ~(MACCFG2_FULL_DUPLEX);
3045ec21e2ecSJeff Kirsher 			else
3046ec21e2ecSJeff Kirsher 				tempval |= MACCFG2_FULL_DUPLEX;
3047ec21e2ecSJeff Kirsher 
3048ec21e2ecSJeff Kirsher 			priv->oldduplex = phydev->duplex;
3049ec21e2ecSJeff Kirsher 		}
3050ec21e2ecSJeff Kirsher 
3051ec21e2ecSJeff Kirsher 		if (phydev->speed != priv->oldspeed) {
3052ec21e2ecSJeff Kirsher 			new_state = 1;
3053ec21e2ecSJeff Kirsher 			switch (phydev->speed) {
3054ec21e2ecSJeff Kirsher 			case 1000:
3055ec21e2ecSJeff Kirsher 				tempval =
3056ec21e2ecSJeff Kirsher 				    ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
3057ec21e2ecSJeff Kirsher 
3058ec21e2ecSJeff Kirsher 				ecntrl &= ~(ECNTRL_R100);
3059ec21e2ecSJeff Kirsher 				break;
3060ec21e2ecSJeff Kirsher 			case 100:
3061ec21e2ecSJeff Kirsher 			case 10:
3062ec21e2ecSJeff Kirsher 				tempval =
3063ec21e2ecSJeff Kirsher 				    ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
3064ec21e2ecSJeff Kirsher 
3065ec21e2ecSJeff Kirsher 				/* Reduced mode distinguishes
30660977f817SJan Ceuleers 				 * between 10 and 100
30670977f817SJan Ceuleers 				 */
3068ec21e2ecSJeff Kirsher 				if (phydev->speed == SPEED_100)
3069ec21e2ecSJeff Kirsher 					ecntrl |= ECNTRL_R100;
3070ec21e2ecSJeff Kirsher 				else
3071ec21e2ecSJeff Kirsher 					ecntrl &= ~(ECNTRL_R100);
3072ec21e2ecSJeff Kirsher 				break;
3073ec21e2ecSJeff Kirsher 			default:
3074ec21e2ecSJeff Kirsher 				netif_warn(priv, link, dev,
3075ec21e2ecSJeff Kirsher 					   "Ack!  Speed (%d) is not 10/100/1000!\n",
3076ec21e2ecSJeff Kirsher 					   phydev->speed);
3077ec21e2ecSJeff Kirsher 				break;
3078ec21e2ecSJeff Kirsher 			}
3079ec21e2ecSJeff Kirsher 
3080ec21e2ecSJeff Kirsher 			priv->oldspeed = phydev->speed;
3081ec21e2ecSJeff Kirsher 		}
3082ec21e2ecSJeff Kirsher 
308323402bddSClaudiu Manoil 		tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
308423402bddSClaudiu Manoil 		tempval1 |= gfar_get_flowctrl_cfg(priv);
308523402bddSClaudiu Manoil 
308623402bddSClaudiu Manoil 		gfar_write(&regs->maccfg1, tempval1);
3087ec21e2ecSJeff Kirsher 		gfar_write(&regs->maccfg2, tempval);
3088ec21e2ecSJeff Kirsher 		gfar_write(&regs->ecntrl, ecntrl);
3089ec21e2ecSJeff Kirsher 
3090ec21e2ecSJeff Kirsher 		if (!priv->oldlink) {
3091ec21e2ecSJeff Kirsher 			new_state = 1;
3092ec21e2ecSJeff Kirsher 			priv->oldlink = 1;
3093ec21e2ecSJeff Kirsher 		}
3094ec21e2ecSJeff Kirsher 	} else if (priv->oldlink) {
3095ec21e2ecSJeff Kirsher 		new_state = 1;
3096ec21e2ecSJeff Kirsher 		priv->oldlink = 0;
3097ec21e2ecSJeff Kirsher 		priv->oldspeed = 0;
3098ec21e2ecSJeff Kirsher 		priv->oldduplex = -1;
3099ec21e2ecSJeff Kirsher 	}
3100ec21e2ecSJeff Kirsher 
3101ec21e2ecSJeff Kirsher 	if (new_state && netif_msg_link(priv))
3102ec21e2ecSJeff Kirsher 		phy_print_status(phydev);
3103ec21e2ecSJeff Kirsher 	unlock_tx_qs(priv);
3104ec21e2ecSJeff Kirsher 	local_irq_restore(flags);
3105ec21e2ecSJeff Kirsher }
3106ec21e2ecSJeff Kirsher 
3107ec21e2ecSJeff Kirsher /* Update the hash table based on the current list of multicast
3108ec21e2ecSJeff Kirsher  * addresses we subscribe to.  Also, change the promiscuity of
3109ec21e2ecSJeff Kirsher  * the device based on the flags (this function is called
31100977f817SJan Ceuleers  * whenever dev->flags is changed
31110977f817SJan Ceuleers  */
3112ec21e2ecSJeff Kirsher static void gfar_set_multi(struct net_device *dev)
3113ec21e2ecSJeff Kirsher {
3114ec21e2ecSJeff Kirsher 	struct netdev_hw_addr *ha;
3115ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
3116ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3117ec21e2ecSJeff Kirsher 	u32 tempval;
3118ec21e2ecSJeff Kirsher 
3119ec21e2ecSJeff Kirsher 	if (dev->flags & IFF_PROMISC) {
3120ec21e2ecSJeff Kirsher 		/* Set RCTRL to PROM */
3121ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->rctrl);
3122ec21e2ecSJeff Kirsher 		tempval |= RCTRL_PROM;
3123ec21e2ecSJeff Kirsher 		gfar_write(&regs->rctrl, tempval);
3124ec21e2ecSJeff Kirsher 	} else {
3125ec21e2ecSJeff Kirsher 		/* Set RCTRL to not PROM */
3126ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->rctrl);
3127ec21e2ecSJeff Kirsher 		tempval &= ~(RCTRL_PROM);
3128ec21e2ecSJeff Kirsher 		gfar_write(&regs->rctrl, tempval);
3129ec21e2ecSJeff Kirsher 	}
3130ec21e2ecSJeff Kirsher 
3131ec21e2ecSJeff Kirsher 	if (dev->flags & IFF_ALLMULTI) {
3132ec21e2ecSJeff Kirsher 		/* Set the hash to rx all multicast frames */
3133ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr0, 0xffffffff);
3134ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr1, 0xffffffff);
3135ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr2, 0xffffffff);
3136ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr3, 0xffffffff);
3137ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr4, 0xffffffff);
3138ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr5, 0xffffffff);
3139ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr6, 0xffffffff);
3140ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr7, 0xffffffff);
3141ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr0, 0xffffffff);
3142ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr1, 0xffffffff);
3143ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr2, 0xffffffff);
3144ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr3, 0xffffffff);
3145ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr4, 0xffffffff);
3146ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr5, 0xffffffff);
3147ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr6, 0xffffffff);
3148ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr7, 0xffffffff);
3149ec21e2ecSJeff Kirsher 	} else {
3150ec21e2ecSJeff Kirsher 		int em_num;
3151ec21e2ecSJeff Kirsher 		int idx;
3152ec21e2ecSJeff Kirsher 
3153ec21e2ecSJeff Kirsher 		/* zero out the hash */
3154ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr0, 0x0);
3155ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr1, 0x0);
3156ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr2, 0x0);
3157ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr3, 0x0);
3158ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr4, 0x0);
3159ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr5, 0x0);
3160ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr6, 0x0);
3161ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr7, 0x0);
3162ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr0, 0x0);
3163ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr1, 0x0);
3164ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr2, 0x0);
3165ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr3, 0x0);
3166ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr4, 0x0);
3167ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr5, 0x0);
3168ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr6, 0x0);
3169ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr7, 0x0);
3170ec21e2ecSJeff Kirsher 
3171ec21e2ecSJeff Kirsher 		/* If we have extended hash tables, we need to
3172ec21e2ecSJeff Kirsher 		 * clear the exact match registers to prepare for
31730977f817SJan Ceuleers 		 * setting them
31740977f817SJan Ceuleers 		 */
3175ec21e2ecSJeff Kirsher 		if (priv->extended_hash) {
3176ec21e2ecSJeff Kirsher 			em_num = GFAR_EM_NUM + 1;
3177ec21e2ecSJeff Kirsher 			gfar_clear_exact_match(dev);
3178ec21e2ecSJeff Kirsher 			idx = 1;
3179ec21e2ecSJeff Kirsher 		} else {
3180ec21e2ecSJeff Kirsher 			idx = 0;
3181ec21e2ecSJeff Kirsher 			em_num = 0;
3182ec21e2ecSJeff Kirsher 		}
3183ec21e2ecSJeff Kirsher 
3184ec21e2ecSJeff Kirsher 		if (netdev_mc_empty(dev))
3185ec21e2ecSJeff Kirsher 			return;
3186ec21e2ecSJeff Kirsher 
3187ec21e2ecSJeff Kirsher 		/* Parse the list, and set the appropriate bits */
3188ec21e2ecSJeff Kirsher 		netdev_for_each_mc_addr(ha, dev) {
3189ec21e2ecSJeff Kirsher 			if (idx < em_num) {
3190ec21e2ecSJeff Kirsher 				gfar_set_mac_for_addr(dev, idx, ha->addr);
3191ec21e2ecSJeff Kirsher 				idx++;
3192ec21e2ecSJeff Kirsher 			} else
3193ec21e2ecSJeff Kirsher 				gfar_set_hash_for_addr(dev, ha->addr);
3194ec21e2ecSJeff Kirsher 		}
3195ec21e2ecSJeff Kirsher 	}
3196ec21e2ecSJeff Kirsher }
3197ec21e2ecSJeff Kirsher 
3198ec21e2ecSJeff Kirsher 
3199ec21e2ecSJeff Kirsher /* Clears each of the exact match registers to zero, so they
32000977f817SJan Ceuleers  * don't interfere with normal reception
32010977f817SJan Ceuleers  */
3202ec21e2ecSJeff Kirsher static void gfar_clear_exact_match(struct net_device *dev)
3203ec21e2ecSJeff Kirsher {
3204ec21e2ecSJeff Kirsher 	int idx;
32056a3c910cSJoe Perches 	static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
3206ec21e2ecSJeff Kirsher 
3207ec21e2ecSJeff Kirsher 	for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
3208ec21e2ecSJeff Kirsher 		gfar_set_mac_for_addr(dev, idx, zero_arr);
3209ec21e2ecSJeff Kirsher }
3210ec21e2ecSJeff Kirsher 
3211ec21e2ecSJeff Kirsher /* Set the appropriate hash bit for the given addr */
3212ec21e2ecSJeff Kirsher /* The algorithm works like so:
3213ec21e2ecSJeff Kirsher  * 1) Take the Destination Address (ie the multicast address), and
3214ec21e2ecSJeff Kirsher  * do a CRC on it (little endian), and reverse the bits of the
3215ec21e2ecSJeff Kirsher  * result.
3216ec21e2ecSJeff Kirsher  * 2) Use the 8 most significant bits as a hash into a 256-entry
3217ec21e2ecSJeff Kirsher  * table.  The table is controlled through 8 32-bit registers:
3218ec21e2ecSJeff Kirsher  * gaddr0-7.  gaddr0's MSB is entry 0, and gaddr7's LSB is
3219ec21e2ecSJeff Kirsher  * gaddr7.  This means that the 3 most significant bits in the
3220ec21e2ecSJeff Kirsher  * hash index which gaddr register to use, and the 5 other bits
3221ec21e2ecSJeff Kirsher  * indicate which bit (assuming an IBM numbering scheme, which
3222ec21e2ecSJeff Kirsher  * for PowerPC (tm) is usually the case) in the register holds
32230977f817SJan Ceuleers  * the entry.
32240977f817SJan Ceuleers  */
3225ec21e2ecSJeff Kirsher static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3226ec21e2ecSJeff Kirsher {
3227ec21e2ecSJeff Kirsher 	u32 tempval;
3228ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
32296a3c910cSJoe Perches 	u32 result = ether_crc(ETH_ALEN, addr);
3230ec21e2ecSJeff Kirsher 	int width = priv->hash_width;
3231ec21e2ecSJeff Kirsher 	u8 whichbit = (result >> (32 - width)) & 0x1f;
3232ec21e2ecSJeff Kirsher 	u8 whichreg = result >> (32 - width + 5);
3233ec21e2ecSJeff Kirsher 	u32 value = (1 << (31-whichbit));
3234ec21e2ecSJeff Kirsher 
3235ec21e2ecSJeff Kirsher 	tempval = gfar_read(priv->hash_regs[whichreg]);
3236ec21e2ecSJeff Kirsher 	tempval |= value;
3237ec21e2ecSJeff Kirsher 	gfar_write(priv->hash_regs[whichreg], tempval);
3238ec21e2ecSJeff Kirsher }
3239ec21e2ecSJeff Kirsher 
3240ec21e2ecSJeff Kirsher 
3241ec21e2ecSJeff Kirsher /* There are multiple MAC Address register pairs on some controllers
3242ec21e2ecSJeff Kirsher  * This function sets the numth pair to a given address
3243ec21e2ecSJeff Kirsher  */
3244ec21e2ecSJeff Kirsher static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3245ec21e2ecSJeff Kirsher 				  const u8 *addr)
3246ec21e2ecSJeff Kirsher {
3247ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
3248ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3249ec21e2ecSJeff Kirsher 	int idx;
32506a3c910cSJoe Perches 	char tmpbuf[ETH_ALEN];
3251ec21e2ecSJeff Kirsher 	u32 tempval;
3252ec21e2ecSJeff Kirsher 	u32 __iomem *macptr = &regs->macstnaddr1;
3253ec21e2ecSJeff Kirsher 
3254ec21e2ecSJeff Kirsher 	macptr += num*2;
3255ec21e2ecSJeff Kirsher 
32560977f817SJan Ceuleers 	/* Now copy it into the mac registers backwards, cuz
32570977f817SJan Ceuleers 	 * little endian is silly
32580977f817SJan Ceuleers 	 */
32596a3c910cSJoe Perches 	for (idx = 0; idx < ETH_ALEN; idx++)
32606a3c910cSJoe Perches 		tmpbuf[ETH_ALEN - 1 - idx] = addr[idx];
3261ec21e2ecSJeff Kirsher 
3262ec21e2ecSJeff Kirsher 	gfar_write(macptr, *((u32 *) (tmpbuf)));
3263ec21e2ecSJeff Kirsher 
3264ec21e2ecSJeff Kirsher 	tempval = *((u32 *) (tmpbuf + 4));
3265ec21e2ecSJeff Kirsher 
3266ec21e2ecSJeff Kirsher 	gfar_write(macptr+1, tempval);
3267ec21e2ecSJeff Kirsher }
3268ec21e2ecSJeff Kirsher 
3269ec21e2ecSJeff Kirsher /* GFAR error interrupt handler */
3270ec21e2ecSJeff Kirsher static irqreturn_t gfar_error(int irq, void *grp_id)
3271ec21e2ecSJeff Kirsher {
3272ec21e2ecSJeff Kirsher 	struct gfar_priv_grp *gfargrp = grp_id;
3273ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = gfargrp->regs;
3274ec21e2ecSJeff Kirsher 	struct gfar_private *priv= gfargrp->priv;
3275ec21e2ecSJeff Kirsher 	struct net_device *dev = priv->ndev;
3276ec21e2ecSJeff Kirsher 
3277ec21e2ecSJeff Kirsher 	/* Save ievent for future reference */
3278ec21e2ecSJeff Kirsher 	u32 events = gfar_read(&regs->ievent);
3279ec21e2ecSJeff Kirsher 
3280ec21e2ecSJeff Kirsher 	/* Clear IEVENT */
3281ec21e2ecSJeff Kirsher 	gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
3282ec21e2ecSJeff Kirsher 
3283ec21e2ecSJeff Kirsher 	/* Magic Packet is not an error. */
3284ec21e2ecSJeff Kirsher 	if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
3285ec21e2ecSJeff Kirsher 	    (events & IEVENT_MAG))
3286ec21e2ecSJeff Kirsher 		events &= ~IEVENT_MAG;
3287ec21e2ecSJeff Kirsher 
3288ec21e2ecSJeff Kirsher 	/* Hmm... */
3289ec21e2ecSJeff Kirsher 	if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
3290bc4598bcSJan Ceuleers 		netdev_dbg(dev,
3291bc4598bcSJan Ceuleers 			   "error interrupt (ievent=0x%08x imask=0x%08x)\n",
3292ec21e2ecSJeff Kirsher 			   events, gfar_read(&regs->imask));
3293ec21e2ecSJeff Kirsher 
3294ec21e2ecSJeff Kirsher 	/* Update the error counters */
3295ec21e2ecSJeff Kirsher 	if (events & IEVENT_TXE) {
3296ec21e2ecSJeff Kirsher 		dev->stats.tx_errors++;
3297ec21e2ecSJeff Kirsher 
3298ec21e2ecSJeff Kirsher 		if (events & IEVENT_LC)
3299ec21e2ecSJeff Kirsher 			dev->stats.tx_window_errors++;
3300ec21e2ecSJeff Kirsher 		if (events & IEVENT_CRL)
3301ec21e2ecSJeff Kirsher 			dev->stats.tx_aborted_errors++;
3302ec21e2ecSJeff Kirsher 		if (events & IEVENT_XFUN) {
3303ec21e2ecSJeff Kirsher 			unsigned long flags;
3304ec21e2ecSJeff Kirsher 
3305ec21e2ecSJeff Kirsher 			netif_dbg(priv, tx_err, dev,
3306ec21e2ecSJeff Kirsher 				  "TX FIFO underrun, packet dropped\n");
3307ec21e2ecSJeff Kirsher 			dev->stats.tx_dropped++;
3308212079dfSPaul Gortmaker 			atomic64_inc(&priv->extra_stats.tx_underrun);
3309ec21e2ecSJeff Kirsher 
3310ec21e2ecSJeff Kirsher 			local_irq_save(flags);
3311ec21e2ecSJeff Kirsher 			lock_tx_qs(priv);
3312ec21e2ecSJeff Kirsher 
3313ec21e2ecSJeff Kirsher 			/* Reactivate the Tx Queues */
3314ec21e2ecSJeff Kirsher 			gfar_write(&regs->tstat, gfargrp->tstat);
3315ec21e2ecSJeff Kirsher 
3316ec21e2ecSJeff Kirsher 			unlock_tx_qs(priv);
3317ec21e2ecSJeff Kirsher 			local_irq_restore(flags);
3318ec21e2ecSJeff Kirsher 		}
3319ec21e2ecSJeff Kirsher 		netif_dbg(priv, tx_err, dev, "Transmit Error\n");
3320ec21e2ecSJeff Kirsher 	}
3321ec21e2ecSJeff Kirsher 	if (events & IEVENT_BSY) {
3322ec21e2ecSJeff Kirsher 		dev->stats.rx_errors++;
3323212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.rx_bsy);
3324ec21e2ecSJeff Kirsher 
3325ec21e2ecSJeff Kirsher 		gfar_receive(irq, grp_id);
3326ec21e2ecSJeff Kirsher 
3327ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3328ec21e2ecSJeff Kirsher 			  gfar_read(&regs->rstat));
3329ec21e2ecSJeff Kirsher 	}
3330ec21e2ecSJeff Kirsher 	if (events & IEVENT_BABR) {
3331ec21e2ecSJeff Kirsher 		dev->stats.rx_errors++;
3332212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.rx_babr);
3333ec21e2ecSJeff Kirsher 
3334ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_err, dev, "babbling RX error\n");
3335ec21e2ecSJeff Kirsher 	}
3336ec21e2ecSJeff Kirsher 	if (events & IEVENT_EBERR) {
3337212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.eberr);
3338ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_err, dev, "bus error\n");
3339ec21e2ecSJeff Kirsher 	}
3340ec21e2ecSJeff Kirsher 	if (events & IEVENT_RXC)
3341ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_status, dev, "control frame\n");
3342ec21e2ecSJeff Kirsher 
3343ec21e2ecSJeff Kirsher 	if (events & IEVENT_BABT) {
3344212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.tx_babt);
3345ec21e2ecSJeff Kirsher 		netif_dbg(priv, tx_err, dev, "babbling TX error\n");
3346ec21e2ecSJeff Kirsher 	}
3347ec21e2ecSJeff Kirsher 	return IRQ_HANDLED;
3348ec21e2ecSJeff Kirsher }
3349ec21e2ecSJeff Kirsher 
3350ec21e2ecSJeff Kirsher static struct of_device_id gfar_match[] =
3351ec21e2ecSJeff Kirsher {
3352ec21e2ecSJeff Kirsher 	{
3353ec21e2ecSJeff Kirsher 		.type = "network",
3354ec21e2ecSJeff Kirsher 		.compatible = "gianfar",
3355ec21e2ecSJeff Kirsher 	},
3356ec21e2ecSJeff Kirsher 	{
3357ec21e2ecSJeff Kirsher 		.compatible = "fsl,etsec2",
3358ec21e2ecSJeff Kirsher 	},
3359ec21e2ecSJeff Kirsher 	{},
3360ec21e2ecSJeff Kirsher };
3361ec21e2ecSJeff Kirsher MODULE_DEVICE_TABLE(of, gfar_match);
3362ec21e2ecSJeff Kirsher 
3363ec21e2ecSJeff Kirsher /* Structure for a device driver */
3364ec21e2ecSJeff Kirsher static struct platform_driver gfar_driver = {
3365ec21e2ecSJeff Kirsher 	.driver = {
3366ec21e2ecSJeff Kirsher 		.name = "fsl-gianfar",
3367ec21e2ecSJeff Kirsher 		.owner = THIS_MODULE,
3368ec21e2ecSJeff Kirsher 		.pm = GFAR_PM_OPS,
3369ec21e2ecSJeff Kirsher 		.of_match_table = gfar_match,
3370ec21e2ecSJeff Kirsher 	},
3371ec21e2ecSJeff Kirsher 	.probe = gfar_probe,
3372ec21e2ecSJeff Kirsher 	.remove = gfar_remove,
3373ec21e2ecSJeff Kirsher };
3374ec21e2ecSJeff Kirsher 
3375db62f684SAxel Lin module_platform_driver(gfar_driver);
3376