xref: /openbmc/linux/drivers/net/ethernet/freescale/gianfar.c (revision 42f397adfc0a062a17119c08c82ed710b143a006)
10977f817SJan Ceuleers /* drivers/net/ethernet/freescale/gianfar.c
2ec21e2ecSJeff Kirsher  *
3ec21e2ecSJeff Kirsher  * Gianfar Ethernet Driver
4ec21e2ecSJeff Kirsher  * This driver is designed for the non-CPM ethernet controllers
5ec21e2ecSJeff Kirsher  * on the 85xx and 83xx family of integrated processors
6ec21e2ecSJeff Kirsher  * Based on 8260_io/fcc_enet.c
7ec21e2ecSJeff Kirsher  *
8ec21e2ecSJeff Kirsher  * Author: Andy Fleming
9ec21e2ecSJeff Kirsher  * Maintainer: Kumar Gala
10ec21e2ecSJeff Kirsher  * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
11ec21e2ecSJeff Kirsher  *
1220862788SClaudiu Manoil  * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc.
13ec21e2ecSJeff Kirsher  * Copyright 2007 MontaVista Software, Inc.
14ec21e2ecSJeff Kirsher  *
15ec21e2ecSJeff Kirsher  * This program is free software; you can redistribute  it and/or modify it
16ec21e2ecSJeff Kirsher  * under  the terms of  the GNU General  Public License as published by the
17ec21e2ecSJeff Kirsher  * Free Software Foundation;  either version 2 of the  License, or (at your
18ec21e2ecSJeff Kirsher  * option) any later version.
19ec21e2ecSJeff Kirsher  *
20ec21e2ecSJeff Kirsher  *  Gianfar:  AKA Lambda Draconis, "Dragon"
21ec21e2ecSJeff Kirsher  *  RA 11 31 24.2
22ec21e2ecSJeff Kirsher  *  Dec +69 19 52
23ec21e2ecSJeff Kirsher  *  V 3.84
24ec21e2ecSJeff Kirsher  *  B-V +1.62
25ec21e2ecSJeff Kirsher  *
26ec21e2ecSJeff Kirsher  *  Theory of operation
27ec21e2ecSJeff Kirsher  *
28ec21e2ecSJeff Kirsher  *  The driver is initialized through of_device. Configuration information
29ec21e2ecSJeff Kirsher  *  is therefore conveyed through an OF-style device tree.
30ec21e2ecSJeff Kirsher  *
31ec21e2ecSJeff Kirsher  *  The Gianfar Ethernet Controller uses a ring of buffer
32ec21e2ecSJeff Kirsher  *  descriptors.  The beginning is indicated by a register
33ec21e2ecSJeff Kirsher  *  pointing to the physical address of the start of the ring.
34ec21e2ecSJeff Kirsher  *  The end is determined by a "wrap" bit being set in the
35ec21e2ecSJeff Kirsher  *  last descriptor of the ring.
36ec21e2ecSJeff Kirsher  *
37ec21e2ecSJeff Kirsher  *  When a packet is received, the RXF bit in the
38ec21e2ecSJeff Kirsher  *  IEVENT register is set, triggering an interrupt when the
39ec21e2ecSJeff Kirsher  *  corresponding bit in the IMASK register is also set (if
40ec21e2ecSJeff Kirsher  *  interrupt coalescing is active, then the interrupt may not
41ec21e2ecSJeff Kirsher  *  happen immediately, but will wait until either a set number
42ec21e2ecSJeff Kirsher  *  of frames or amount of time have passed).  In NAPI, the
43ec21e2ecSJeff Kirsher  *  interrupt handler will signal there is work to be done, and
44ec21e2ecSJeff Kirsher  *  exit. This method will start at the last known empty
45ec21e2ecSJeff Kirsher  *  descriptor, and process every subsequent descriptor until there
46ec21e2ecSJeff Kirsher  *  are none left with data (NAPI will stop after a set number of
47ec21e2ecSJeff Kirsher  *  packets to give time to other tasks, but will eventually
48ec21e2ecSJeff Kirsher  *  process all the packets).  The data arrives inside a
49ec21e2ecSJeff Kirsher  *  pre-allocated skb, and so after the skb is passed up to the
50ec21e2ecSJeff Kirsher  *  stack, a new skb must be allocated, and the address field in
51ec21e2ecSJeff Kirsher  *  the buffer descriptor must be updated to indicate this new
52ec21e2ecSJeff Kirsher  *  skb.
53ec21e2ecSJeff Kirsher  *
54ec21e2ecSJeff Kirsher  *  When the kernel requests that a packet be transmitted, the
55ec21e2ecSJeff Kirsher  *  driver starts where it left off last time, and points the
56ec21e2ecSJeff Kirsher  *  descriptor at the buffer which was passed in.  The driver
57ec21e2ecSJeff Kirsher  *  then informs the DMA engine that there are packets ready to
58ec21e2ecSJeff Kirsher  *  be transmitted.  Once the controller is finished transmitting
59ec21e2ecSJeff Kirsher  *  the packet, an interrupt may be triggered (under the same
60ec21e2ecSJeff Kirsher  *  conditions as for reception, but depending on the TXF bit).
61ec21e2ecSJeff Kirsher  *  The driver then cleans up the buffer.
62ec21e2ecSJeff Kirsher  */
63ec21e2ecSJeff Kirsher 
64ec21e2ecSJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
65ec21e2ecSJeff Kirsher #define DEBUG
66ec21e2ecSJeff Kirsher 
67ec21e2ecSJeff Kirsher #include <linux/kernel.h>
68ec21e2ecSJeff Kirsher #include <linux/string.h>
69ec21e2ecSJeff Kirsher #include <linux/errno.h>
70ec21e2ecSJeff Kirsher #include <linux/unistd.h>
71ec21e2ecSJeff Kirsher #include <linux/slab.h>
72ec21e2ecSJeff Kirsher #include <linux/interrupt.h>
73ec21e2ecSJeff Kirsher #include <linux/delay.h>
74ec21e2ecSJeff Kirsher #include <linux/netdevice.h>
75ec21e2ecSJeff Kirsher #include <linux/etherdevice.h>
76ec21e2ecSJeff Kirsher #include <linux/skbuff.h>
77ec21e2ecSJeff Kirsher #include <linux/if_vlan.h>
78ec21e2ecSJeff Kirsher #include <linux/spinlock.h>
79ec21e2ecSJeff Kirsher #include <linux/mm.h>
805af50730SRob Herring #include <linux/of_address.h>
815af50730SRob Herring #include <linux/of_irq.h>
82ec21e2ecSJeff Kirsher #include <linux/of_mdio.h>
83ec21e2ecSJeff Kirsher #include <linux/of_platform.h>
84ec21e2ecSJeff Kirsher #include <linux/ip.h>
85ec21e2ecSJeff Kirsher #include <linux/tcp.h>
86ec21e2ecSJeff Kirsher #include <linux/udp.h>
87ec21e2ecSJeff Kirsher #include <linux/in.h>
88ec21e2ecSJeff Kirsher #include <linux/net_tstamp.h>
89ec21e2ecSJeff Kirsher 
90ec21e2ecSJeff Kirsher #include <asm/io.h>
91d6ef0bccSClaudiu Manoil #ifdef CONFIG_PPC
92ec21e2ecSJeff Kirsher #include <asm/reg.h>
932969b1f7SClaudiu Manoil #include <asm/mpc85xx.h>
94d6ef0bccSClaudiu Manoil #endif
95ec21e2ecSJeff Kirsher #include <asm/irq.h>
96ec21e2ecSJeff Kirsher #include <asm/uaccess.h>
97ec21e2ecSJeff Kirsher #include <linux/module.h>
98ec21e2ecSJeff Kirsher #include <linux/dma-mapping.h>
99ec21e2ecSJeff Kirsher #include <linux/crc32.h>
100ec21e2ecSJeff Kirsher #include <linux/mii.h>
101ec21e2ecSJeff Kirsher #include <linux/phy.h>
102ec21e2ecSJeff Kirsher #include <linux/phy_fixed.h>
103ec21e2ecSJeff Kirsher #include <linux/of.h>
104ec21e2ecSJeff Kirsher #include <linux/of_net.h>
105fd31a952SClaudiu Manoil #include <linux/of_address.h>
106fd31a952SClaudiu Manoil #include <linux/of_irq.h>
107ec21e2ecSJeff Kirsher 
108ec21e2ecSJeff Kirsher #include "gianfar.h"
109ec21e2ecSJeff Kirsher 
1108fcc6033SAbhimanyu #define TX_TIMEOUT      (5*HZ)
111ec21e2ecSJeff Kirsher 
11275354148SClaudiu Manoil const char gfar_driver_version[] = "2.0";
113ec21e2ecSJeff Kirsher 
114ec21e2ecSJeff Kirsher static int gfar_enet_open(struct net_device *dev);
115ec21e2ecSJeff Kirsher static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
116ec21e2ecSJeff Kirsher static void gfar_reset_task(struct work_struct *work);
117ec21e2ecSJeff Kirsher static void gfar_timeout(struct net_device *dev);
118ec21e2ecSJeff Kirsher static int gfar_close(struct net_device *dev);
11976f31e8bSClaudiu Manoil static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue,
12076f31e8bSClaudiu Manoil 				int alloc_cnt);
121ec21e2ecSJeff Kirsher static int gfar_set_mac_address(struct net_device *dev);
122ec21e2ecSJeff Kirsher static int gfar_change_mtu(struct net_device *dev, int new_mtu);
123ec21e2ecSJeff Kirsher static irqreturn_t gfar_error(int irq, void *dev_id);
124ec21e2ecSJeff Kirsher static irqreturn_t gfar_transmit(int irq, void *dev_id);
125ec21e2ecSJeff Kirsher static irqreturn_t gfar_interrupt(int irq, void *dev_id);
126ec21e2ecSJeff Kirsher static void adjust_link(struct net_device *dev);
1276ce29b0eSClaudiu Manoil static noinline void gfar_update_link_state(struct gfar_private *priv);
128ec21e2ecSJeff Kirsher static int init_phy(struct net_device *dev);
129ec21e2ecSJeff Kirsher static int gfar_probe(struct platform_device *ofdev);
130ec21e2ecSJeff Kirsher static int gfar_remove(struct platform_device *ofdev);
131ec21e2ecSJeff Kirsher static void free_skb_resources(struct gfar_private *priv);
132ec21e2ecSJeff Kirsher static void gfar_set_multi(struct net_device *dev);
133ec21e2ecSJeff Kirsher static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
134ec21e2ecSJeff Kirsher static void gfar_configure_serdes(struct net_device *dev);
135aeb12c5eSClaudiu Manoil static int gfar_poll_rx(struct napi_struct *napi, int budget);
136aeb12c5eSClaudiu Manoil static int gfar_poll_tx(struct napi_struct *napi, int budget);
137aeb12c5eSClaudiu Manoil static int gfar_poll_rx_sq(struct napi_struct *napi, int budget);
138aeb12c5eSClaudiu Manoil static int gfar_poll_tx_sq(struct napi_struct *napi, int budget);
139ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
140ec21e2ecSJeff Kirsher static void gfar_netpoll(struct net_device *dev);
141ec21e2ecSJeff Kirsher #endif
142ec21e2ecSJeff Kirsher int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
143c233cf40SClaudiu Manoil static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
144f23223f1SClaudiu Manoil static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb);
145c10650b6SClaudiu Manoil static void gfar_halt_nodisable(struct gfar_private *priv);
146ec21e2ecSJeff Kirsher static void gfar_clear_exact_match(struct net_device *dev);
147ec21e2ecSJeff Kirsher static void gfar_set_mac_for_addr(struct net_device *dev, int num,
148ec21e2ecSJeff Kirsher 				  const u8 *addr);
149ec21e2ecSJeff Kirsher static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
150ec21e2ecSJeff Kirsher 
151ec21e2ecSJeff Kirsher MODULE_AUTHOR("Freescale Semiconductor, Inc");
152ec21e2ecSJeff Kirsher MODULE_DESCRIPTION("Gianfar Ethernet Driver");
153ec21e2ecSJeff Kirsher MODULE_LICENSE("GPL");
154ec21e2ecSJeff Kirsher 
155ec21e2ecSJeff Kirsher static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
156ec21e2ecSJeff Kirsher 			    dma_addr_t buf)
157ec21e2ecSJeff Kirsher {
158ec21e2ecSJeff Kirsher 	u32 lstatus;
159ec21e2ecSJeff Kirsher 
160a7312d58SClaudiu Manoil 	bdp->bufPtr = cpu_to_be32(buf);
161ec21e2ecSJeff Kirsher 
162ec21e2ecSJeff Kirsher 	lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
163ec21e2ecSJeff Kirsher 	if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
164ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(RXBD_WRAP);
165ec21e2ecSJeff Kirsher 
166d55398baSClaudiu Manoil 	gfar_wmb();
167ec21e2ecSJeff Kirsher 
168a7312d58SClaudiu Manoil 	bdp->lstatus = cpu_to_be32(lstatus);
169ec21e2ecSJeff Kirsher }
170ec21e2ecSJeff Kirsher 
17176f31e8bSClaudiu Manoil static void gfar_init_bds(struct net_device *ndev)
172ec21e2ecSJeff Kirsher {
173ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(ndev);
17445b679c9SMatei Pavaluca 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
175ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
176ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
177ec21e2ecSJeff Kirsher 	struct txbd8 *txbdp;
17803366a33SKevin Hao 	u32 __iomem *rfbptr;
179ec21e2ecSJeff Kirsher 	int i, j;
180ec21e2ecSJeff Kirsher 
181ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
182ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
183ec21e2ecSJeff Kirsher 		/* Initialize some variables in our dev structure */
184ec21e2ecSJeff Kirsher 		tx_queue->num_txbdfree = tx_queue->tx_ring_size;
185ec21e2ecSJeff Kirsher 		tx_queue->dirty_tx = tx_queue->tx_bd_base;
186ec21e2ecSJeff Kirsher 		tx_queue->cur_tx = tx_queue->tx_bd_base;
187ec21e2ecSJeff Kirsher 		tx_queue->skb_curtx = 0;
188ec21e2ecSJeff Kirsher 		tx_queue->skb_dirtytx = 0;
189ec21e2ecSJeff Kirsher 
190ec21e2ecSJeff Kirsher 		/* Initialize Transmit Descriptor Ring */
191ec21e2ecSJeff Kirsher 		txbdp = tx_queue->tx_bd_base;
192ec21e2ecSJeff Kirsher 		for (j = 0; j < tx_queue->tx_ring_size; j++) {
193ec21e2ecSJeff Kirsher 			txbdp->lstatus = 0;
194ec21e2ecSJeff Kirsher 			txbdp->bufPtr = 0;
195ec21e2ecSJeff Kirsher 			txbdp++;
196ec21e2ecSJeff Kirsher 		}
197ec21e2ecSJeff Kirsher 
198ec21e2ecSJeff Kirsher 		/* Set the last descriptor in the ring to indicate wrap */
199ec21e2ecSJeff Kirsher 		txbdp--;
200a7312d58SClaudiu Manoil 		txbdp->status = cpu_to_be16(be16_to_cpu(txbdp->status) |
201a7312d58SClaudiu Manoil 					    TXBD_WRAP);
202ec21e2ecSJeff Kirsher 	}
203ec21e2ecSJeff Kirsher 
20445b679c9SMatei Pavaluca 	rfbptr = &regs->rfbptr0;
205ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
206ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
207ec21e2ecSJeff Kirsher 
20876f31e8bSClaudiu Manoil 		rx_queue->next_to_clean = 0;
20976f31e8bSClaudiu Manoil 		rx_queue->next_to_use = 0;
21075354148SClaudiu Manoil 		rx_queue->next_to_alloc = 0;
211ec21e2ecSJeff Kirsher 
21276f31e8bSClaudiu Manoil 		/* make sure next_to_clean != next_to_use after this
21376f31e8bSClaudiu Manoil 		 * by leaving at least 1 unused descriptor
21476f31e8bSClaudiu Manoil 		 */
21576f31e8bSClaudiu Manoil 		gfar_alloc_rx_buffs(rx_queue, gfar_rxbd_unused(rx_queue));
216ec21e2ecSJeff Kirsher 
21745b679c9SMatei Pavaluca 		rx_queue->rfbptr = rfbptr;
21845b679c9SMatei Pavaluca 		rfbptr += 2;
219ec21e2ecSJeff Kirsher 	}
220ec21e2ecSJeff Kirsher }
221ec21e2ecSJeff Kirsher 
222ec21e2ecSJeff Kirsher static int gfar_alloc_skb_resources(struct net_device *ndev)
223ec21e2ecSJeff Kirsher {
224ec21e2ecSJeff Kirsher 	void *vaddr;
225ec21e2ecSJeff Kirsher 	dma_addr_t addr;
22675354148SClaudiu Manoil 	int i, j;
227ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(ndev);
228369ec162SClaudiu Manoil 	struct device *dev = priv->dev;
229ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
230ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
231ec21e2ecSJeff Kirsher 
232ec21e2ecSJeff Kirsher 	priv->total_tx_ring_size = 0;
233ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
234ec21e2ecSJeff Kirsher 		priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
235ec21e2ecSJeff Kirsher 
236ec21e2ecSJeff Kirsher 	priv->total_rx_ring_size = 0;
237ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
238ec21e2ecSJeff Kirsher 		priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
239ec21e2ecSJeff Kirsher 
240ec21e2ecSJeff Kirsher 	/* Allocate memory for the buffer descriptors */
241ec21e2ecSJeff Kirsher 	vaddr = dma_alloc_coherent(dev,
242d0320f75SJoe Perches 				   (priv->total_tx_ring_size *
243d0320f75SJoe Perches 				    sizeof(struct txbd8)) +
244d0320f75SJoe Perches 				   (priv->total_rx_ring_size *
245d0320f75SJoe Perches 				    sizeof(struct rxbd8)),
246ec21e2ecSJeff Kirsher 				   &addr, GFP_KERNEL);
247d0320f75SJoe Perches 	if (!vaddr)
248ec21e2ecSJeff Kirsher 		return -ENOMEM;
249ec21e2ecSJeff Kirsher 
250ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
251ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
252ec21e2ecSJeff Kirsher 		tx_queue->tx_bd_base = vaddr;
253ec21e2ecSJeff Kirsher 		tx_queue->tx_bd_dma_base = addr;
254ec21e2ecSJeff Kirsher 		tx_queue->dev = ndev;
255ec21e2ecSJeff Kirsher 		/* enet DMA only understands physical addresses */
256ec21e2ecSJeff Kirsher 		addr  += sizeof(struct txbd8) * tx_queue->tx_ring_size;
257ec21e2ecSJeff Kirsher 		vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
258ec21e2ecSJeff Kirsher 	}
259ec21e2ecSJeff Kirsher 
260ec21e2ecSJeff Kirsher 	/* Start the rx descriptor ring where the tx ring leaves off */
261ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
262ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
263ec21e2ecSJeff Kirsher 		rx_queue->rx_bd_base = vaddr;
264ec21e2ecSJeff Kirsher 		rx_queue->rx_bd_dma_base = addr;
265f23223f1SClaudiu Manoil 		rx_queue->ndev = ndev;
26675354148SClaudiu Manoil 		rx_queue->dev = dev;
267ec21e2ecSJeff Kirsher 		addr  += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
268ec21e2ecSJeff Kirsher 		vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
269ec21e2ecSJeff Kirsher 	}
270ec21e2ecSJeff Kirsher 
271ec21e2ecSJeff Kirsher 	/* Setup the skbuff rings */
272ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
273ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
27414f8dc49SJoe Perches 		tx_queue->tx_skbuff =
27514f8dc49SJoe Perches 			kmalloc_array(tx_queue->tx_ring_size,
27614f8dc49SJoe Perches 				      sizeof(*tx_queue->tx_skbuff),
277bc4598bcSJan Ceuleers 				      GFP_KERNEL);
27814f8dc49SJoe Perches 		if (!tx_queue->tx_skbuff)
279ec21e2ecSJeff Kirsher 			goto cleanup;
280ec21e2ecSJeff Kirsher 
28175354148SClaudiu Manoil 		for (j = 0; j < tx_queue->tx_ring_size; j++)
28275354148SClaudiu Manoil 			tx_queue->tx_skbuff[j] = NULL;
283ec21e2ecSJeff Kirsher 	}
284ec21e2ecSJeff Kirsher 
285ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
286ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
28775354148SClaudiu Manoil 		rx_queue->rx_buff = kcalloc(rx_queue->rx_ring_size,
28875354148SClaudiu Manoil 					    sizeof(*rx_queue->rx_buff),
289bc4598bcSJan Ceuleers 					    GFP_KERNEL);
29075354148SClaudiu Manoil 		if (!rx_queue->rx_buff)
291ec21e2ecSJeff Kirsher 			goto cleanup;
292ec21e2ecSJeff Kirsher 	}
293ec21e2ecSJeff Kirsher 
29476f31e8bSClaudiu Manoil 	gfar_init_bds(ndev);
295ec21e2ecSJeff Kirsher 
296ec21e2ecSJeff Kirsher 	return 0;
297ec21e2ecSJeff Kirsher 
298ec21e2ecSJeff Kirsher cleanup:
299ec21e2ecSJeff Kirsher 	free_skb_resources(priv);
300ec21e2ecSJeff Kirsher 	return -ENOMEM;
301ec21e2ecSJeff Kirsher }
302ec21e2ecSJeff Kirsher 
303ec21e2ecSJeff Kirsher static void gfar_init_tx_rx_base(struct gfar_private *priv)
304ec21e2ecSJeff Kirsher {
305ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
306ec21e2ecSJeff Kirsher 	u32 __iomem *baddr;
307ec21e2ecSJeff Kirsher 	int i;
308ec21e2ecSJeff Kirsher 
309ec21e2ecSJeff Kirsher 	baddr = &regs->tbase0;
310ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
311ec21e2ecSJeff Kirsher 		gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
312ec21e2ecSJeff Kirsher 		baddr += 2;
313ec21e2ecSJeff Kirsher 	}
314ec21e2ecSJeff Kirsher 
315ec21e2ecSJeff Kirsher 	baddr = &regs->rbase0;
316ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
317ec21e2ecSJeff Kirsher 		gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
318ec21e2ecSJeff Kirsher 		baddr += 2;
319ec21e2ecSJeff Kirsher 	}
320ec21e2ecSJeff Kirsher }
321ec21e2ecSJeff Kirsher 
32245b679c9SMatei Pavaluca static void gfar_init_rqprm(struct gfar_private *priv)
32345b679c9SMatei Pavaluca {
32445b679c9SMatei Pavaluca 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
32545b679c9SMatei Pavaluca 	u32 __iomem *baddr;
32645b679c9SMatei Pavaluca 	int i;
32745b679c9SMatei Pavaluca 
32845b679c9SMatei Pavaluca 	baddr = &regs->rqprm0;
32945b679c9SMatei Pavaluca 	for (i = 0; i < priv->num_rx_queues; i++) {
33045b679c9SMatei Pavaluca 		gfar_write(baddr, priv->rx_queue[i]->rx_ring_size |
33145b679c9SMatei Pavaluca 			   (DEFAULT_RX_LFC_THR << FBTHR_SHIFT));
33245b679c9SMatei Pavaluca 		baddr++;
33345b679c9SMatei Pavaluca 	}
33445b679c9SMatei Pavaluca }
33545b679c9SMatei Pavaluca 
33675354148SClaudiu Manoil static void gfar_rx_offload_en(struct gfar_private *priv)
33788302648SClaudiu Manoil {
33888302648SClaudiu Manoil 	/* set this when rx hw offload (TOE) functions are being used */
33988302648SClaudiu Manoil 	priv->uses_rxfcb = 0;
34088302648SClaudiu Manoil 
34188302648SClaudiu Manoil 	if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
34288302648SClaudiu Manoil 		priv->uses_rxfcb = 1;
34388302648SClaudiu Manoil 
34415bf176dSClaudiu Manoil 	if (priv->hwts_rx_en || priv->rx_filer_enable)
34588302648SClaudiu Manoil 		priv->uses_rxfcb = 1;
34688302648SClaudiu Manoil }
34788302648SClaudiu Manoil 
348a328ac92SClaudiu Manoil static void gfar_mac_rx_config(struct gfar_private *priv)
349ec21e2ecSJeff Kirsher {
350ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
351ec21e2ecSJeff Kirsher 	u32 rctrl = 0;
352ec21e2ecSJeff Kirsher 
353ec21e2ecSJeff Kirsher 	if (priv->rx_filer_enable) {
35415bf176dSClaudiu Manoil 		rctrl |= RCTRL_FILREN | RCTRL_PRSDEP_INIT;
355ec21e2ecSJeff Kirsher 		/* Program the RIR0 reg with the required distribution */
35671ff9e3dSClaudiu Manoil 		if (priv->poll_mode == GFAR_SQ_POLLING)
35771ff9e3dSClaudiu Manoil 			gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
35871ff9e3dSClaudiu Manoil 		else /* GFAR_MQ_POLLING */
35971ff9e3dSClaudiu Manoil 			gfar_write(&regs->rir0, DEFAULT_8RXQ_RIR0);
360ec21e2ecSJeff Kirsher 	}
361ec21e2ecSJeff Kirsher 
362f5ae6279SClaudiu Manoil 	/* Restore PROMISC mode */
363a328ac92SClaudiu Manoil 	if (priv->ndev->flags & IFF_PROMISC)
364f5ae6279SClaudiu Manoil 		rctrl |= RCTRL_PROM;
365f5ae6279SClaudiu Manoil 
36688302648SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_RXCSUM)
367ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_CHECKSUMMING;
368ec21e2ecSJeff Kirsher 
36988302648SClaudiu Manoil 	if (priv->extended_hash)
37088302648SClaudiu Manoil 		rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
371ec21e2ecSJeff Kirsher 
372ec21e2ecSJeff Kirsher 	if (priv->padding) {
373ec21e2ecSJeff Kirsher 		rctrl &= ~RCTRL_PAL_MASK;
374ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_PADDING(priv->padding);
375ec21e2ecSJeff Kirsher 	}
376ec21e2ecSJeff Kirsher 
377ec21e2ecSJeff Kirsher 	/* Enable HW time stamping if requested from user space */
37888302648SClaudiu Manoil 	if (priv->hwts_rx_en)
379ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
380ec21e2ecSJeff Kirsher 
38188302648SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
382ec21e2ecSJeff Kirsher 		rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
383ec21e2ecSJeff Kirsher 
38445b679c9SMatei Pavaluca 	/* Clear the LFC bit */
38545b679c9SMatei Pavaluca 	gfar_write(&regs->rctrl, rctrl);
38645b679c9SMatei Pavaluca 	/* Init flow control threshold values */
38745b679c9SMatei Pavaluca 	gfar_init_rqprm(priv);
38845b679c9SMatei Pavaluca 	gfar_write(&regs->ptv, DEFAULT_LFC_PTVVAL);
38945b679c9SMatei Pavaluca 	rctrl |= RCTRL_LFC;
39045b679c9SMatei Pavaluca 
391ec21e2ecSJeff Kirsher 	/* Init rctrl based on our settings */
392ec21e2ecSJeff Kirsher 	gfar_write(&regs->rctrl, rctrl);
393a328ac92SClaudiu Manoil }
394ec21e2ecSJeff Kirsher 
395a328ac92SClaudiu Manoil static void gfar_mac_tx_config(struct gfar_private *priv)
396a328ac92SClaudiu Manoil {
397a328ac92SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
398a328ac92SClaudiu Manoil 	u32 tctrl = 0;
399a328ac92SClaudiu Manoil 
400a328ac92SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_IP_CSUM)
401ec21e2ecSJeff Kirsher 		tctrl |= TCTRL_INIT_CSUM;
402ec21e2ecSJeff Kirsher 
403b98b8babSClaudiu Manoil 	if (priv->prio_sched_en)
404ec21e2ecSJeff Kirsher 		tctrl |= TCTRL_TXSCHED_PRIO;
405b98b8babSClaudiu Manoil 	else {
406b98b8babSClaudiu Manoil 		tctrl |= TCTRL_TXSCHED_WRRS;
407b98b8babSClaudiu Manoil 		gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
408b98b8babSClaudiu Manoil 		gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
409b98b8babSClaudiu Manoil 	}
410ec21e2ecSJeff Kirsher 
41188302648SClaudiu Manoil 	if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
41288302648SClaudiu Manoil 		tctrl |= TCTRL_VLINS;
41388302648SClaudiu Manoil 
414ec21e2ecSJeff Kirsher 	gfar_write(&regs->tctrl, tctrl);
415ec21e2ecSJeff Kirsher }
416ec21e2ecSJeff Kirsher 
417f19015baSClaudiu Manoil static void gfar_configure_coalescing(struct gfar_private *priv,
418f19015baSClaudiu Manoil 			       unsigned long tx_mask, unsigned long rx_mask)
419f19015baSClaudiu Manoil {
420f19015baSClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
421f19015baSClaudiu Manoil 	u32 __iomem *baddr;
422f19015baSClaudiu Manoil 
423f19015baSClaudiu Manoil 	if (priv->mode == MQ_MG_MODE) {
424f19015baSClaudiu Manoil 		int i = 0;
425f19015baSClaudiu Manoil 
426f19015baSClaudiu Manoil 		baddr = &regs->txic0;
427f19015baSClaudiu Manoil 		for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
428f19015baSClaudiu Manoil 			gfar_write(baddr + i, 0);
429f19015baSClaudiu Manoil 			if (likely(priv->tx_queue[i]->txcoalescing))
430f19015baSClaudiu Manoil 				gfar_write(baddr + i, priv->tx_queue[i]->txic);
431f19015baSClaudiu Manoil 		}
432f19015baSClaudiu Manoil 
433f19015baSClaudiu Manoil 		baddr = &regs->rxic0;
434f19015baSClaudiu Manoil 		for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
435f19015baSClaudiu Manoil 			gfar_write(baddr + i, 0);
436f19015baSClaudiu Manoil 			if (likely(priv->rx_queue[i]->rxcoalescing))
437f19015baSClaudiu Manoil 				gfar_write(baddr + i, priv->rx_queue[i]->rxic);
438f19015baSClaudiu Manoil 		}
439f19015baSClaudiu Manoil 	} else {
440f19015baSClaudiu Manoil 		/* Backward compatible case -- even if we enable
441f19015baSClaudiu Manoil 		 * multiple queues, there's only single reg to program
442f19015baSClaudiu Manoil 		 */
443f19015baSClaudiu Manoil 		gfar_write(&regs->txic, 0);
444f19015baSClaudiu Manoil 		if (likely(priv->tx_queue[0]->txcoalescing))
445f19015baSClaudiu Manoil 			gfar_write(&regs->txic, priv->tx_queue[0]->txic);
446f19015baSClaudiu Manoil 
447f19015baSClaudiu Manoil 		gfar_write(&regs->rxic, 0);
448f19015baSClaudiu Manoil 		if (unlikely(priv->rx_queue[0]->rxcoalescing))
449f19015baSClaudiu Manoil 			gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
450f19015baSClaudiu Manoil 	}
451f19015baSClaudiu Manoil }
452f19015baSClaudiu Manoil 
453f19015baSClaudiu Manoil void gfar_configure_coalescing_all(struct gfar_private *priv)
454f19015baSClaudiu Manoil {
455f19015baSClaudiu Manoil 	gfar_configure_coalescing(priv, 0xFF, 0xFF);
456f19015baSClaudiu Manoil }
457f19015baSClaudiu Manoil 
458ec21e2ecSJeff Kirsher static struct net_device_stats *gfar_get_stats(struct net_device *dev)
459ec21e2ecSJeff Kirsher {
460ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
461ec21e2ecSJeff Kirsher 	unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
462ec21e2ecSJeff Kirsher 	unsigned long tx_packets = 0, tx_bytes = 0;
4633a2e16c8SJan Ceuleers 	int i;
464ec21e2ecSJeff Kirsher 
465ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
466ec21e2ecSJeff Kirsher 		rx_packets += priv->rx_queue[i]->stats.rx_packets;
467ec21e2ecSJeff Kirsher 		rx_bytes   += priv->rx_queue[i]->stats.rx_bytes;
468ec21e2ecSJeff Kirsher 		rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
469ec21e2ecSJeff Kirsher 	}
470ec21e2ecSJeff Kirsher 
471ec21e2ecSJeff Kirsher 	dev->stats.rx_packets = rx_packets;
472ec21e2ecSJeff Kirsher 	dev->stats.rx_bytes   = rx_bytes;
473ec21e2ecSJeff Kirsher 	dev->stats.rx_dropped = rx_dropped;
474ec21e2ecSJeff Kirsher 
475ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
476ec21e2ecSJeff Kirsher 		tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
477ec21e2ecSJeff Kirsher 		tx_packets += priv->tx_queue[i]->stats.tx_packets;
478ec21e2ecSJeff Kirsher 	}
479ec21e2ecSJeff Kirsher 
480ec21e2ecSJeff Kirsher 	dev->stats.tx_bytes   = tx_bytes;
481ec21e2ecSJeff Kirsher 	dev->stats.tx_packets = tx_packets;
482ec21e2ecSJeff Kirsher 
483ec21e2ecSJeff Kirsher 	return &dev->stats;
484ec21e2ecSJeff Kirsher }
485ec21e2ecSJeff Kirsher 
4863d23a05cSClaudiu Manoil static int gfar_set_mac_addr(struct net_device *dev, void *p)
4873d23a05cSClaudiu Manoil {
4883d23a05cSClaudiu Manoil 	eth_mac_addr(dev, p);
4893d23a05cSClaudiu Manoil 
4903d23a05cSClaudiu Manoil 	gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
4913d23a05cSClaudiu Manoil 
4923d23a05cSClaudiu Manoil 	return 0;
4933d23a05cSClaudiu Manoil }
4943d23a05cSClaudiu Manoil 
495ec21e2ecSJeff Kirsher static const struct net_device_ops gfar_netdev_ops = {
496ec21e2ecSJeff Kirsher 	.ndo_open = gfar_enet_open,
497ec21e2ecSJeff Kirsher 	.ndo_start_xmit = gfar_start_xmit,
498ec21e2ecSJeff Kirsher 	.ndo_stop = gfar_close,
499ec21e2ecSJeff Kirsher 	.ndo_change_mtu = gfar_change_mtu,
500ec21e2ecSJeff Kirsher 	.ndo_set_features = gfar_set_features,
501afc4b13dSJiri Pirko 	.ndo_set_rx_mode = gfar_set_multi,
502ec21e2ecSJeff Kirsher 	.ndo_tx_timeout = gfar_timeout,
503ec21e2ecSJeff Kirsher 	.ndo_do_ioctl = gfar_ioctl,
504ec21e2ecSJeff Kirsher 	.ndo_get_stats = gfar_get_stats,
5053d23a05cSClaudiu Manoil 	.ndo_set_mac_address = gfar_set_mac_addr,
506ec21e2ecSJeff Kirsher 	.ndo_validate_addr = eth_validate_addr,
507ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
508ec21e2ecSJeff Kirsher 	.ndo_poll_controller = gfar_netpoll,
509ec21e2ecSJeff Kirsher #endif
510ec21e2ecSJeff Kirsher };
511ec21e2ecSJeff Kirsher 
512efeddce7SClaudiu Manoil static void gfar_ints_disable(struct gfar_private *priv)
513efeddce7SClaudiu Manoil {
514efeddce7SClaudiu Manoil 	int i;
515efeddce7SClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
516efeddce7SClaudiu Manoil 		struct gfar __iomem *regs = priv->gfargrp[i].regs;
517efeddce7SClaudiu Manoil 		/* Clear IEVENT */
518efeddce7SClaudiu Manoil 		gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
519efeddce7SClaudiu Manoil 
520efeddce7SClaudiu Manoil 		/* Initialize IMASK */
521efeddce7SClaudiu Manoil 		gfar_write(&regs->imask, IMASK_INIT_CLEAR);
522efeddce7SClaudiu Manoil 	}
523efeddce7SClaudiu Manoil }
524efeddce7SClaudiu Manoil 
525efeddce7SClaudiu Manoil static void gfar_ints_enable(struct gfar_private *priv)
526efeddce7SClaudiu Manoil {
527efeddce7SClaudiu Manoil 	int i;
528efeddce7SClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
529efeddce7SClaudiu Manoil 		struct gfar __iomem *regs = priv->gfargrp[i].regs;
530efeddce7SClaudiu Manoil 		/* Unmask the interrupts we look for */
531efeddce7SClaudiu Manoil 		gfar_write(&regs->imask, IMASK_DEFAULT);
532efeddce7SClaudiu Manoil 	}
533efeddce7SClaudiu Manoil }
534efeddce7SClaudiu Manoil 
53520862788SClaudiu Manoil static int gfar_alloc_tx_queues(struct gfar_private *priv)
53620862788SClaudiu Manoil {
53720862788SClaudiu Manoil 	int i;
53820862788SClaudiu Manoil 
53920862788SClaudiu Manoil 	for (i = 0; i < priv->num_tx_queues; i++) {
54020862788SClaudiu Manoil 		priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
54120862788SClaudiu Manoil 					    GFP_KERNEL);
54220862788SClaudiu Manoil 		if (!priv->tx_queue[i])
54320862788SClaudiu Manoil 			return -ENOMEM;
54420862788SClaudiu Manoil 
54520862788SClaudiu Manoil 		priv->tx_queue[i]->tx_skbuff = NULL;
54620862788SClaudiu Manoil 		priv->tx_queue[i]->qindex = i;
54720862788SClaudiu Manoil 		priv->tx_queue[i]->dev = priv->ndev;
54820862788SClaudiu Manoil 		spin_lock_init(&(priv->tx_queue[i]->txlock));
54920862788SClaudiu Manoil 	}
55020862788SClaudiu Manoil 	return 0;
55120862788SClaudiu Manoil }
55220862788SClaudiu Manoil 
55320862788SClaudiu Manoil static int gfar_alloc_rx_queues(struct gfar_private *priv)
55420862788SClaudiu Manoil {
55520862788SClaudiu Manoil 	int i;
55620862788SClaudiu Manoil 
55720862788SClaudiu Manoil 	for (i = 0; i < priv->num_rx_queues; i++) {
55820862788SClaudiu Manoil 		priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
55920862788SClaudiu Manoil 					    GFP_KERNEL);
56020862788SClaudiu Manoil 		if (!priv->rx_queue[i])
56120862788SClaudiu Manoil 			return -ENOMEM;
56220862788SClaudiu Manoil 
56320862788SClaudiu Manoil 		priv->rx_queue[i]->qindex = i;
564f23223f1SClaudiu Manoil 		priv->rx_queue[i]->ndev = priv->ndev;
56520862788SClaudiu Manoil 	}
56620862788SClaudiu Manoil 	return 0;
56720862788SClaudiu Manoil }
56820862788SClaudiu Manoil 
56920862788SClaudiu Manoil static void gfar_free_tx_queues(struct gfar_private *priv)
570ec21e2ecSJeff Kirsher {
5713a2e16c8SJan Ceuleers 	int i;
572ec21e2ecSJeff Kirsher 
573ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
574ec21e2ecSJeff Kirsher 		kfree(priv->tx_queue[i]);
575ec21e2ecSJeff Kirsher }
576ec21e2ecSJeff Kirsher 
57720862788SClaudiu Manoil static void gfar_free_rx_queues(struct gfar_private *priv)
578ec21e2ecSJeff Kirsher {
5793a2e16c8SJan Ceuleers 	int i;
580ec21e2ecSJeff Kirsher 
581ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
582ec21e2ecSJeff Kirsher 		kfree(priv->rx_queue[i]);
583ec21e2ecSJeff Kirsher }
584ec21e2ecSJeff Kirsher 
585ec21e2ecSJeff Kirsher static void unmap_group_regs(struct gfar_private *priv)
586ec21e2ecSJeff Kirsher {
5873a2e16c8SJan Ceuleers 	int i;
588ec21e2ecSJeff Kirsher 
589ec21e2ecSJeff Kirsher 	for (i = 0; i < MAXGROUPS; i++)
590ec21e2ecSJeff Kirsher 		if (priv->gfargrp[i].regs)
591ec21e2ecSJeff Kirsher 			iounmap(priv->gfargrp[i].regs);
592ec21e2ecSJeff Kirsher }
593ec21e2ecSJeff Kirsher 
594ee873fdaSClaudiu Manoil static void free_gfar_dev(struct gfar_private *priv)
595ee873fdaSClaudiu Manoil {
596ee873fdaSClaudiu Manoil 	int i, j;
597ee873fdaSClaudiu Manoil 
598ee873fdaSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++)
599ee873fdaSClaudiu Manoil 		for (j = 0; j < GFAR_NUM_IRQS; j++) {
600ee873fdaSClaudiu Manoil 			kfree(priv->gfargrp[i].irqinfo[j]);
601ee873fdaSClaudiu Manoil 			priv->gfargrp[i].irqinfo[j] = NULL;
602ee873fdaSClaudiu Manoil 		}
603ee873fdaSClaudiu Manoil 
604ee873fdaSClaudiu Manoil 	free_netdev(priv->ndev);
605ee873fdaSClaudiu Manoil }
606ee873fdaSClaudiu Manoil 
607ec21e2ecSJeff Kirsher static void disable_napi(struct gfar_private *priv)
608ec21e2ecSJeff Kirsher {
6093a2e16c8SJan Ceuleers 	int i;
610ec21e2ecSJeff Kirsher 
611aeb12c5eSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
612aeb12c5eSClaudiu Manoil 		napi_disable(&priv->gfargrp[i].napi_rx);
613aeb12c5eSClaudiu Manoil 		napi_disable(&priv->gfargrp[i].napi_tx);
614aeb12c5eSClaudiu Manoil 	}
615ec21e2ecSJeff Kirsher }
616ec21e2ecSJeff Kirsher 
617ec21e2ecSJeff Kirsher static void enable_napi(struct gfar_private *priv)
618ec21e2ecSJeff Kirsher {
6193a2e16c8SJan Ceuleers 	int i;
620ec21e2ecSJeff Kirsher 
621aeb12c5eSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
622aeb12c5eSClaudiu Manoil 		napi_enable(&priv->gfargrp[i].napi_rx);
623aeb12c5eSClaudiu Manoil 		napi_enable(&priv->gfargrp[i].napi_tx);
624aeb12c5eSClaudiu Manoil 	}
625ec21e2ecSJeff Kirsher }
626ec21e2ecSJeff Kirsher 
627ec21e2ecSJeff Kirsher static int gfar_parse_group(struct device_node *np,
628ec21e2ecSJeff Kirsher 			    struct gfar_private *priv, const char *model)
629ec21e2ecSJeff Kirsher {
6305fedcc14SClaudiu Manoil 	struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
631ee873fdaSClaudiu Manoil 	int i;
632ee873fdaSClaudiu Manoil 
633ee873fdaSClaudiu Manoil 	for (i = 0; i < GFAR_NUM_IRQS; i++) {
634ee873fdaSClaudiu Manoil 		grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
635ee873fdaSClaudiu Manoil 					  GFP_KERNEL);
636ee873fdaSClaudiu Manoil 		if (!grp->irqinfo[i])
637ee873fdaSClaudiu Manoil 			return -ENOMEM;
638ee873fdaSClaudiu Manoil 	}
639ec21e2ecSJeff Kirsher 
6405fedcc14SClaudiu Manoil 	grp->regs = of_iomap(np, 0);
6415fedcc14SClaudiu Manoil 	if (!grp->regs)
642ec21e2ecSJeff Kirsher 		return -ENOMEM;
643ec21e2ecSJeff Kirsher 
644ee873fdaSClaudiu Manoil 	gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
645ec21e2ecSJeff Kirsher 
646ec21e2ecSJeff Kirsher 	/* If we aren't the FEC we have multiple interrupts */
647ec21e2ecSJeff Kirsher 	if (model && strcasecmp(model, "FEC")) {
648ee873fdaSClaudiu Manoil 		gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
649ee873fdaSClaudiu Manoil 		gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
650fea0f665SMark Brown 		if (!gfar_irq(grp, TX)->irq ||
651fea0f665SMark Brown 		    !gfar_irq(grp, RX)->irq ||
652fea0f665SMark Brown 		    !gfar_irq(grp, ER)->irq)
653ec21e2ecSJeff Kirsher 			return -EINVAL;
654ec21e2ecSJeff Kirsher 	}
655ec21e2ecSJeff Kirsher 
6565fedcc14SClaudiu Manoil 	grp->priv = priv;
6575fedcc14SClaudiu Manoil 	spin_lock_init(&grp->grplock);
658ec21e2ecSJeff Kirsher 	if (priv->mode == MQ_MG_MODE) {
65955917641SJingchang Lu 		u32 rxq_mask, txq_mask;
66055917641SJingchang Lu 		int ret;
66155917641SJingchang Lu 
66255917641SJingchang Lu 		grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
66355917641SJingchang Lu 		grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
66455917641SJingchang Lu 
66555917641SJingchang Lu 		ret = of_property_read_u32(np, "fsl,rx-bit-map", &rxq_mask);
66655917641SJingchang Lu 		if (!ret) {
66755917641SJingchang Lu 			grp->rx_bit_map = rxq_mask ?
66855917641SJingchang Lu 			rxq_mask : (DEFAULT_MAPPING >> priv->num_grps);
66955917641SJingchang Lu 		}
67055917641SJingchang Lu 
67155917641SJingchang Lu 		ret = of_property_read_u32(np, "fsl,tx-bit-map", &txq_mask);
67255917641SJingchang Lu 		if (!ret) {
67355917641SJingchang Lu 			grp->tx_bit_map = txq_mask ?
67455917641SJingchang Lu 			txq_mask : (DEFAULT_MAPPING >> priv->num_grps);
67555917641SJingchang Lu 		}
67671ff9e3dSClaudiu Manoil 
67771ff9e3dSClaudiu Manoil 		if (priv->poll_mode == GFAR_SQ_POLLING) {
67871ff9e3dSClaudiu Manoil 			/* One Q per interrupt group: Q0 to G0, Q1 to G1 */
67971ff9e3dSClaudiu Manoil 			grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
68071ff9e3dSClaudiu Manoil 			grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
68171ff9e3dSClaudiu Manoil 		}
682ec21e2ecSJeff Kirsher 	} else {
6835fedcc14SClaudiu Manoil 		grp->rx_bit_map = 0xFF;
6845fedcc14SClaudiu Manoil 		grp->tx_bit_map = 0xFF;
685ec21e2ecSJeff Kirsher 	}
68620862788SClaudiu Manoil 
68720862788SClaudiu Manoil 	/* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
68820862788SClaudiu Manoil 	 * right to left, so we need to revert the 8 bits to get the q index
68920862788SClaudiu Manoil 	 */
69020862788SClaudiu Manoil 	grp->rx_bit_map = bitrev8(grp->rx_bit_map);
69120862788SClaudiu Manoil 	grp->tx_bit_map = bitrev8(grp->tx_bit_map);
69220862788SClaudiu Manoil 
69320862788SClaudiu Manoil 	/* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
69420862788SClaudiu Manoil 	 * also assign queues to groups
69520862788SClaudiu Manoil 	 */
69620862788SClaudiu Manoil 	for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
69771ff9e3dSClaudiu Manoil 		if (!grp->rx_queue)
69871ff9e3dSClaudiu Manoil 			grp->rx_queue = priv->rx_queue[i];
69920862788SClaudiu Manoil 		grp->num_rx_queues++;
70020862788SClaudiu Manoil 		grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
70120862788SClaudiu Manoil 		priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
70220862788SClaudiu Manoil 		priv->rx_queue[i]->grp = grp;
70320862788SClaudiu Manoil 	}
70420862788SClaudiu Manoil 
70520862788SClaudiu Manoil 	for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
70671ff9e3dSClaudiu Manoil 		if (!grp->tx_queue)
70771ff9e3dSClaudiu Manoil 			grp->tx_queue = priv->tx_queue[i];
70820862788SClaudiu Manoil 		grp->num_tx_queues++;
70920862788SClaudiu Manoil 		grp->tstat |= (TSTAT_CLEAR_THALT >> i);
71020862788SClaudiu Manoil 		priv->tqueue |= (TQUEUE_EN0 >> i);
71120862788SClaudiu Manoil 		priv->tx_queue[i]->grp = grp;
71220862788SClaudiu Manoil 	}
71320862788SClaudiu Manoil 
714ec21e2ecSJeff Kirsher 	priv->num_grps++;
715ec21e2ecSJeff Kirsher 
716ec21e2ecSJeff Kirsher 	return 0;
717ec21e2ecSJeff Kirsher }
718ec21e2ecSJeff Kirsher 
719f50724cdSTobias Waldekranz static int gfar_of_group_count(struct device_node *np)
720f50724cdSTobias Waldekranz {
721f50724cdSTobias Waldekranz 	struct device_node *child;
722f50724cdSTobias Waldekranz 	int num = 0;
723f50724cdSTobias Waldekranz 
724f50724cdSTobias Waldekranz 	for_each_available_child_of_node(np, child)
725f50724cdSTobias Waldekranz 		if (!of_node_cmp(child->name, "queue-group"))
726f50724cdSTobias Waldekranz 			num++;
727f50724cdSTobias Waldekranz 
728f50724cdSTobias Waldekranz 	return num;
729f50724cdSTobias Waldekranz }
730f50724cdSTobias Waldekranz 
731ec21e2ecSJeff Kirsher static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
732ec21e2ecSJeff Kirsher {
733ec21e2ecSJeff Kirsher 	const char *model;
734ec21e2ecSJeff Kirsher 	const char *ctype;
735ec21e2ecSJeff Kirsher 	const void *mac_addr;
736ec21e2ecSJeff Kirsher 	int err = 0, i;
737ec21e2ecSJeff Kirsher 	struct net_device *dev = NULL;
738ec21e2ecSJeff Kirsher 	struct gfar_private *priv = NULL;
739ec21e2ecSJeff Kirsher 	struct device_node *np = ofdev->dev.of_node;
740ec21e2ecSJeff Kirsher 	struct device_node *child = NULL;
74155917641SJingchang Lu 	u32 stash_len = 0;
74255917641SJingchang Lu 	u32 stash_idx = 0;
743ec21e2ecSJeff Kirsher 	unsigned int num_tx_qs, num_rx_qs;
744b338ce27SClaudiu Manoil 	unsigned short mode, poll_mode;
745ec21e2ecSJeff Kirsher 
7464b222ca6SKevin Hao 	if (!np)
747ec21e2ecSJeff Kirsher 		return -ENODEV;
748ec21e2ecSJeff Kirsher 
749b338ce27SClaudiu Manoil 	if (of_device_is_compatible(np, "fsl,etsec2")) {
750b338ce27SClaudiu Manoil 		mode = MQ_MG_MODE;
751b338ce27SClaudiu Manoil 		poll_mode = GFAR_SQ_POLLING;
752b338ce27SClaudiu Manoil 	} else {
753b338ce27SClaudiu Manoil 		mode = SQ_SG_MODE;
754b338ce27SClaudiu Manoil 		poll_mode = GFAR_SQ_POLLING;
755b338ce27SClaudiu Manoil 	}
756b338ce27SClaudiu Manoil 
757b338ce27SClaudiu Manoil 	if (mode == SQ_SG_MODE) {
75871ff9e3dSClaudiu Manoil 		num_tx_qs = 1;
75971ff9e3dSClaudiu Manoil 		num_rx_qs = 1;
76071ff9e3dSClaudiu Manoil 	} else { /* MQ_MG_MODE */
761c65d7533SClaudiu Manoil 		/* get the actual number of supported groups */
762f50724cdSTobias Waldekranz 		unsigned int num_grps = gfar_of_group_count(np);
763c65d7533SClaudiu Manoil 
764c65d7533SClaudiu Manoil 		if (num_grps == 0 || num_grps > MAXGROUPS) {
765c65d7533SClaudiu Manoil 			dev_err(&ofdev->dev, "Invalid # of int groups(%d)\n",
766c65d7533SClaudiu Manoil 				num_grps);
767c65d7533SClaudiu Manoil 			pr_err("Cannot do alloc_etherdev, aborting\n");
768c65d7533SClaudiu Manoil 			return -EINVAL;
769c65d7533SClaudiu Manoil 		}
770c65d7533SClaudiu Manoil 
771b338ce27SClaudiu Manoil 		if (poll_mode == GFAR_SQ_POLLING) {
772c65d7533SClaudiu Manoil 			num_tx_qs = num_grps; /* one txq per int group */
773c65d7533SClaudiu Manoil 			num_rx_qs = num_grps; /* one rxq per int group */
77471ff9e3dSClaudiu Manoil 		} else { /* GFAR_MQ_POLLING */
77555917641SJingchang Lu 			u32 tx_queues, rx_queues;
77655917641SJingchang Lu 			int ret;
77755917641SJingchang Lu 
77855917641SJingchang Lu 			/* parse the num of HW tx and rx queues */
77955917641SJingchang Lu 			ret = of_property_read_u32(np, "fsl,num_tx_queues",
78055917641SJingchang Lu 						   &tx_queues);
78155917641SJingchang Lu 			num_tx_qs = ret ? 1 : tx_queues;
78255917641SJingchang Lu 
78355917641SJingchang Lu 			ret = of_property_read_u32(np, "fsl,num_rx_queues",
78455917641SJingchang Lu 						   &rx_queues);
78555917641SJingchang Lu 			num_rx_qs = ret ? 1 : rx_queues;
78671ff9e3dSClaudiu Manoil 		}
78771ff9e3dSClaudiu Manoil 	}
788ec21e2ecSJeff Kirsher 
789ec21e2ecSJeff Kirsher 	if (num_tx_qs > MAX_TX_QS) {
790ec21e2ecSJeff Kirsher 		pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
791ec21e2ecSJeff Kirsher 		       num_tx_qs, MAX_TX_QS);
792ec21e2ecSJeff Kirsher 		pr_err("Cannot do alloc_etherdev, aborting\n");
793ec21e2ecSJeff Kirsher 		return -EINVAL;
794ec21e2ecSJeff Kirsher 	}
795ec21e2ecSJeff Kirsher 
796ec21e2ecSJeff Kirsher 	if (num_rx_qs > MAX_RX_QS) {
797ec21e2ecSJeff Kirsher 		pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
798ec21e2ecSJeff Kirsher 		       num_rx_qs, MAX_RX_QS);
799ec21e2ecSJeff Kirsher 		pr_err("Cannot do alloc_etherdev, aborting\n");
800ec21e2ecSJeff Kirsher 		return -EINVAL;
801ec21e2ecSJeff Kirsher 	}
802ec21e2ecSJeff Kirsher 
803ec21e2ecSJeff Kirsher 	*pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
804ec21e2ecSJeff Kirsher 	dev = *pdev;
805ec21e2ecSJeff Kirsher 	if (NULL == dev)
806ec21e2ecSJeff Kirsher 		return -ENOMEM;
807ec21e2ecSJeff Kirsher 
808ec21e2ecSJeff Kirsher 	priv = netdev_priv(dev);
809ec21e2ecSJeff Kirsher 	priv->ndev = dev;
810ec21e2ecSJeff Kirsher 
811b338ce27SClaudiu Manoil 	priv->mode = mode;
812b338ce27SClaudiu Manoil 	priv->poll_mode = poll_mode;
813b338ce27SClaudiu Manoil 
814ec21e2ecSJeff Kirsher 	priv->num_tx_queues = num_tx_qs;
815ec21e2ecSJeff Kirsher 	netif_set_real_num_rx_queues(dev, num_rx_qs);
816ec21e2ecSJeff Kirsher 	priv->num_rx_queues = num_rx_qs;
81720862788SClaudiu Manoil 
81820862788SClaudiu Manoil 	err = gfar_alloc_tx_queues(priv);
81920862788SClaudiu Manoil 	if (err)
82020862788SClaudiu Manoil 		goto tx_alloc_failed;
82120862788SClaudiu Manoil 
82220862788SClaudiu Manoil 	err = gfar_alloc_rx_queues(priv);
82320862788SClaudiu Manoil 	if (err)
82420862788SClaudiu Manoil 		goto rx_alloc_failed;
825ec21e2ecSJeff Kirsher 
82655917641SJingchang Lu 	err = of_property_read_string(np, "model", &model);
82755917641SJingchang Lu 	if (err) {
82855917641SJingchang Lu 		pr_err("Device model property missing, aborting\n");
82955917641SJingchang Lu 		goto rx_alloc_failed;
83055917641SJingchang Lu 	}
83155917641SJingchang Lu 
832ec21e2ecSJeff Kirsher 	/* Init Rx queue filer rule set linked list */
833ec21e2ecSJeff Kirsher 	INIT_LIST_HEAD(&priv->rx_list.list);
834ec21e2ecSJeff Kirsher 	priv->rx_list.count = 0;
835ec21e2ecSJeff Kirsher 	mutex_init(&priv->rx_queue_access);
836ec21e2ecSJeff Kirsher 
837ec21e2ecSJeff Kirsher 	for (i = 0; i < MAXGROUPS; i++)
838ec21e2ecSJeff Kirsher 		priv->gfargrp[i].regs = NULL;
839ec21e2ecSJeff Kirsher 
840ec21e2ecSJeff Kirsher 	/* Parse and initialize group specific information */
841b338ce27SClaudiu Manoil 	if (priv->mode == MQ_MG_MODE) {
842f50724cdSTobias Waldekranz 		for_each_available_child_of_node(np, child) {
843f50724cdSTobias Waldekranz 			if (of_node_cmp(child->name, "queue-group"))
844f50724cdSTobias Waldekranz 				continue;
845f50724cdSTobias Waldekranz 
846ec21e2ecSJeff Kirsher 			err = gfar_parse_group(child, priv, model);
847ec21e2ecSJeff Kirsher 			if (err)
848ec21e2ecSJeff Kirsher 				goto err_grp_init;
849ec21e2ecSJeff Kirsher 		}
850b338ce27SClaudiu Manoil 	} else { /* SQ_SG_MODE */
851ec21e2ecSJeff Kirsher 		err = gfar_parse_group(np, priv, model);
852ec21e2ecSJeff Kirsher 		if (err)
853ec21e2ecSJeff Kirsher 			goto err_grp_init;
854ec21e2ecSJeff Kirsher 	}
855ec21e2ecSJeff Kirsher 
8563f8c0f7eSSaurabh Sengar 	if (of_property_read_bool(np, "bd-stash")) {
857ec21e2ecSJeff Kirsher 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
858ec21e2ecSJeff Kirsher 		priv->bd_stash_en = 1;
859ec21e2ecSJeff Kirsher 	}
860ec21e2ecSJeff Kirsher 
86155917641SJingchang Lu 	err = of_property_read_u32(np, "rx-stash-len", &stash_len);
862ec21e2ecSJeff Kirsher 
86355917641SJingchang Lu 	if (err == 0)
86455917641SJingchang Lu 		priv->rx_stash_size = stash_len;
865ec21e2ecSJeff Kirsher 
86655917641SJingchang Lu 	err = of_property_read_u32(np, "rx-stash-idx", &stash_idx);
867ec21e2ecSJeff Kirsher 
86855917641SJingchang Lu 	if (err == 0)
86955917641SJingchang Lu 		priv->rx_stash_index = stash_idx;
870ec21e2ecSJeff Kirsher 
871ec21e2ecSJeff Kirsher 	if (stash_len || stash_idx)
872ec21e2ecSJeff Kirsher 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
873ec21e2ecSJeff Kirsher 
874ec21e2ecSJeff Kirsher 	mac_addr = of_get_mac_address(np);
875bc4598bcSJan Ceuleers 
876ec21e2ecSJeff Kirsher 	if (mac_addr)
8776a3c910cSJoe Perches 		memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
878ec21e2ecSJeff Kirsher 
879ec21e2ecSJeff Kirsher 	if (model && !strcasecmp(model, "TSEC"))
88034018fd4SClaudiu Manoil 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
881ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_COALESCE |
882ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_RMON |
883ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_MULTI_INTR;
884bc4598bcSJan Ceuleers 
885ec21e2ecSJeff Kirsher 	if (model && !strcasecmp(model, "eTSEC"))
88634018fd4SClaudiu Manoil 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
887ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_COALESCE |
888ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_RMON |
889ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_MULTI_INTR |
890ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_CSUM |
891ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_VLAN |
892ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
893ec21e2ecSJeff Kirsher 				     FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
8947bff47daSHamish Martin 				     FSL_GIANFAR_DEV_HAS_TIMER |
8957bff47daSHamish Martin 				     FSL_GIANFAR_DEV_HAS_RX_FILER;
896ec21e2ecSJeff Kirsher 
89755917641SJingchang Lu 	err = of_property_read_string(np, "phy-connection-type", &ctype);
898ec21e2ecSJeff Kirsher 
899ec21e2ecSJeff Kirsher 	/* We only care about rgmii-id.  The rest are autodetected */
90055917641SJingchang Lu 	if (err == 0 && !strcmp(ctype, "rgmii-id"))
901ec21e2ecSJeff Kirsher 		priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
902ec21e2ecSJeff Kirsher 	else
903ec21e2ecSJeff Kirsher 		priv->interface = PHY_INTERFACE_MODE_MII;
904ec21e2ecSJeff Kirsher 
90555917641SJingchang Lu 	if (of_find_property(np, "fsl,magic-packet", NULL))
906ec21e2ecSJeff Kirsher 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
907ec21e2ecSJeff Kirsher 
9083e905b80SClaudiu Manoil 	if (of_get_property(np, "fsl,wake-on-filer", NULL))
9093e905b80SClaudiu Manoil 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_WAKE_ON_FILER;
9103e905b80SClaudiu Manoil 
911ec21e2ecSJeff Kirsher 	priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
912ec21e2ecSJeff Kirsher 
913be403645SFlorian Fainelli 	/* In the case of a fixed PHY, the DT node associated
914be403645SFlorian Fainelli 	 * to the PHY is the Ethernet MAC DT node.
915be403645SFlorian Fainelli 	 */
9166f2c9bd8SUwe Kleine-König 	if (!priv->phy_node && of_phy_is_fixed_link(np)) {
917be403645SFlorian Fainelli 		err = of_phy_register_fixed_link(np);
918be403645SFlorian Fainelli 		if (err)
919be403645SFlorian Fainelli 			goto err_grp_init;
920be403645SFlorian Fainelli 
9216f2c9bd8SUwe Kleine-König 		priv->phy_node = of_node_get(np);
922be403645SFlorian Fainelli 	}
923be403645SFlorian Fainelli 
924ec21e2ecSJeff Kirsher 	/* Find the TBI PHY.  If it's not there, we don't support SGMII */
925ec21e2ecSJeff Kirsher 	priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
926ec21e2ecSJeff Kirsher 
927ec21e2ecSJeff Kirsher 	return 0;
928ec21e2ecSJeff Kirsher 
929ec21e2ecSJeff Kirsher err_grp_init:
930ec21e2ecSJeff Kirsher 	unmap_group_regs(priv);
93120862788SClaudiu Manoil rx_alloc_failed:
93220862788SClaudiu Manoil 	gfar_free_rx_queues(priv);
93320862788SClaudiu Manoil tx_alloc_failed:
93420862788SClaudiu Manoil 	gfar_free_tx_queues(priv);
935ee873fdaSClaudiu Manoil 	free_gfar_dev(priv);
936ec21e2ecSJeff Kirsher 	return err;
937ec21e2ecSJeff Kirsher }
938ec21e2ecSJeff Kirsher 
939ca0c88c2SBen Hutchings static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
940ec21e2ecSJeff Kirsher {
941ec21e2ecSJeff Kirsher 	struct hwtstamp_config config;
942ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(netdev);
943ec21e2ecSJeff Kirsher 
944ec21e2ecSJeff Kirsher 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
945ec21e2ecSJeff Kirsher 		return -EFAULT;
946ec21e2ecSJeff Kirsher 
947ec21e2ecSJeff Kirsher 	/* reserved for future extensions */
948ec21e2ecSJeff Kirsher 	if (config.flags)
949ec21e2ecSJeff Kirsher 		return -EINVAL;
950ec21e2ecSJeff Kirsher 
951ec21e2ecSJeff Kirsher 	switch (config.tx_type) {
952ec21e2ecSJeff Kirsher 	case HWTSTAMP_TX_OFF:
953ec21e2ecSJeff Kirsher 		priv->hwts_tx_en = 0;
954ec21e2ecSJeff Kirsher 		break;
955ec21e2ecSJeff Kirsher 	case HWTSTAMP_TX_ON:
956ec21e2ecSJeff Kirsher 		if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
957ec21e2ecSJeff Kirsher 			return -ERANGE;
958ec21e2ecSJeff Kirsher 		priv->hwts_tx_en = 1;
959ec21e2ecSJeff Kirsher 		break;
960ec21e2ecSJeff Kirsher 	default:
961ec21e2ecSJeff Kirsher 		return -ERANGE;
962ec21e2ecSJeff Kirsher 	}
963ec21e2ecSJeff Kirsher 
964ec21e2ecSJeff Kirsher 	switch (config.rx_filter) {
965ec21e2ecSJeff Kirsher 	case HWTSTAMP_FILTER_NONE:
966ec21e2ecSJeff Kirsher 		if (priv->hwts_rx_en) {
967ec21e2ecSJeff Kirsher 			priv->hwts_rx_en = 0;
9680851133bSClaudiu Manoil 			reset_gfar(netdev);
969ec21e2ecSJeff Kirsher 		}
970ec21e2ecSJeff Kirsher 		break;
971ec21e2ecSJeff Kirsher 	default:
972ec21e2ecSJeff Kirsher 		if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
973ec21e2ecSJeff Kirsher 			return -ERANGE;
974ec21e2ecSJeff Kirsher 		if (!priv->hwts_rx_en) {
975ec21e2ecSJeff Kirsher 			priv->hwts_rx_en = 1;
9760851133bSClaudiu Manoil 			reset_gfar(netdev);
977ec21e2ecSJeff Kirsher 		}
978ec21e2ecSJeff Kirsher 		config.rx_filter = HWTSTAMP_FILTER_ALL;
979ec21e2ecSJeff Kirsher 		break;
980ec21e2ecSJeff Kirsher 	}
981ec21e2ecSJeff Kirsher 
982ec21e2ecSJeff Kirsher 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
983ec21e2ecSJeff Kirsher 		-EFAULT : 0;
984ec21e2ecSJeff Kirsher }
985ec21e2ecSJeff Kirsher 
986ca0c88c2SBen Hutchings static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
987ca0c88c2SBen Hutchings {
988ca0c88c2SBen Hutchings 	struct hwtstamp_config config;
989ca0c88c2SBen Hutchings 	struct gfar_private *priv = netdev_priv(netdev);
990ca0c88c2SBen Hutchings 
991ca0c88c2SBen Hutchings 	config.flags = 0;
992ca0c88c2SBen Hutchings 	config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
993ca0c88c2SBen Hutchings 	config.rx_filter = (priv->hwts_rx_en ?
994ca0c88c2SBen Hutchings 			    HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
995ca0c88c2SBen Hutchings 
996ca0c88c2SBen Hutchings 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
997ca0c88c2SBen Hutchings 		-EFAULT : 0;
998ca0c88c2SBen Hutchings }
999ca0c88c2SBen Hutchings 
1000ec21e2ecSJeff Kirsher static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1001ec21e2ecSJeff Kirsher {
1002ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1003ec21e2ecSJeff Kirsher 
1004ec21e2ecSJeff Kirsher 	if (!netif_running(dev))
1005ec21e2ecSJeff Kirsher 		return -EINVAL;
1006ec21e2ecSJeff Kirsher 
1007ec21e2ecSJeff Kirsher 	if (cmd == SIOCSHWTSTAMP)
1008ca0c88c2SBen Hutchings 		return gfar_hwtstamp_set(dev, rq);
1009ca0c88c2SBen Hutchings 	if (cmd == SIOCGHWTSTAMP)
1010ca0c88c2SBen Hutchings 		return gfar_hwtstamp_get(dev, rq);
1011ec21e2ecSJeff Kirsher 
1012ec21e2ecSJeff Kirsher 	if (!priv->phydev)
1013ec21e2ecSJeff Kirsher 		return -ENODEV;
1014ec21e2ecSJeff Kirsher 
1015ec21e2ecSJeff Kirsher 	return phy_mii_ioctl(priv->phydev, rq, cmd);
1016ec21e2ecSJeff Kirsher }
1017ec21e2ecSJeff Kirsher 
1018ec21e2ecSJeff Kirsher static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
1019ec21e2ecSJeff Kirsher 				   u32 class)
1020ec21e2ecSJeff Kirsher {
1021ec21e2ecSJeff Kirsher 	u32 rqfpr = FPR_FILER_MASK;
1022ec21e2ecSJeff Kirsher 	u32 rqfcr = 0x0;
1023ec21e2ecSJeff Kirsher 
1024ec21e2ecSJeff Kirsher 	rqfar--;
1025ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
1026ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
1027ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
1028ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1029ec21e2ecSJeff Kirsher 
1030ec21e2ecSJeff Kirsher 	rqfar--;
1031ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_NOMATCH;
1032ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
1033ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
1034ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1035ec21e2ecSJeff Kirsher 
1036ec21e2ecSJeff Kirsher 	rqfar--;
1037ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
1038ec21e2ecSJeff Kirsher 	rqfpr = class;
1039ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
1040ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
1041ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1042ec21e2ecSJeff Kirsher 
1043ec21e2ecSJeff Kirsher 	rqfar--;
1044ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
1045ec21e2ecSJeff Kirsher 	rqfpr = class;
1046ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
1047ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
1048ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1049ec21e2ecSJeff Kirsher 
1050ec21e2ecSJeff Kirsher 	return rqfar;
1051ec21e2ecSJeff Kirsher }
1052ec21e2ecSJeff Kirsher 
1053ec21e2ecSJeff Kirsher static void gfar_init_filer_table(struct gfar_private *priv)
1054ec21e2ecSJeff Kirsher {
1055ec21e2ecSJeff Kirsher 	int i = 0x0;
1056ec21e2ecSJeff Kirsher 	u32 rqfar = MAX_FILER_IDX;
1057ec21e2ecSJeff Kirsher 	u32 rqfcr = 0x0;
1058ec21e2ecSJeff Kirsher 	u32 rqfpr = FPR_FILER_MASK;
1059ec21e2ecSJeff Kirsher 
1060ec21e2ecSJeff Kirsher 	/* Default rule */
1061ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_MATCH;
1062ec21e2ecSJeff Kirsher 	priv->ftp_rqfcr[rqfar] = rqfcr;
1063ec21e2ecSJeff Kirsher 	priv->ftp_rqfpr[rqfar] = rqfpr;
1064ec21e2ecSJeff Kirsher 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1065ec21e2ecSJeff Kirsher 
1066ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
1067ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
1068ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
1069ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
1070ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
1071ec21e2ecSJeff Kirsher 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
1072ec21e2ecSJeff Kirsher 
1073ec21e2ecSJeff Kirsher 	/* cur_filer_idx indicated the first non-masked rule */
1074ec21e2ecSJeff Kirsher 	priv->cur_filer_idx = rqfar;
1075ec21e2ecSJeff Kirsher 
1076ec21e2ecSJeff Kirsher 	/* Rest are masked rules */
1077ec21e2ecSJeff Kirsher 	rqfcr = RQFCR_CMP_NOMATCH;
1078ec21e2ecSJeff Kirsher 	for (i = 0; i < rqfar; i++) {
1079ec21e2ecSJeff Kirsher 		priv->ftp_rqfcr[i] = rqfcr;
1080ec21e2ecSJeff Kirsher 		priv->ftp_rqfpr[i] = rqfpr;
1081ec21e2ecSJeff Kirsher 		gfar_write_filer(priv, i, rqfcr, rqfpr);
1082ec21e2ecSJeff Kirsher 	}
1083ec21e2ecSJeff Kirsher }
1084ec21e2ecSJeff Kirsher 
1085d6ef0bccSClaudiu Manoil #ifdef CONFIG_PPC
10862969b1f7SClaudiu Manoil static void __gfar_detect_errata_83xx(struct gfar_private *priv)
1087ec21e2ecSJeff Kirsher {
1088ec21e2ecSJeff Kirsher 	unsigned int pvr = mfspr(SPRN_PVR);
1089ec21e2ecSJeff Kirsher 	unsigned int svr = mfspr(SPRN_SVR);
1090ec21e2ecSJeff Kirsher 	unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
1091ec21e2ecSJeff Kirsher 	unsigned int rev = svr & 0xffff;
1092ec21e2ecSJeff Kirsher 
1093ec21e2ecSJeff Kirsher 	/* MPC8313 Rev 2.0 and higher; All MPC837x */
1094ec21e2ecSJeff Kirsher 	if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
1095ec21e2ecSJeff Kirsher 	    (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
1096ec21e2ecSJeff Kirsher 		priv->errata |= GFAR_ERRATA_74;
1097ec21e2ecSJeff Kirsher 
1098ec21e2ecSJeff Kirsher 	/* MPC8313 and MPC837x all rev */
1099ec21e2ecSJeff Kirsher 	if ((pvr == 0x80850010 && mod == 0x80b0) ||
1100ec21e2ecSJeff Kirsher 	    (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
1101ec21e2ecSJeff Kirsher 		priv->errata |= GFAR_ERRATA_76;
1102ec21e2ecSJeff Kirsher 
11032969b1f7SClaudiu Manoil 	/* MPC8313 Rev < 2.0 */
11042969b1f7SClaudiu Manoil 	if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
1105ec21e2ecSJeff Kirsher 		priv->errata |= GFAR_ERRATA_12;
11062969b1f7SClaudiu Manoil }
11072969b1f7SClaudiu Manoil 
11082969b1f7SClaudiu Manoil static void __gfar_detect_errata_85xx(struct gfar_private *priv)
11092969b1f7SClaudiu Manoil {
11102969b1f7SClaudiu Manoil 	unsigned int svr = mfspr(SPRN_SVR);
11112969b1f7SClaudiu Manoil 
11122969b1f7SClaudiu Manoil 	if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
11132969b1f7SClaudiu Manoil 		priv->errata |= GFAR_ERRATA_12;
111453fad773SClaudiu Manoil 	if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
111553fad773SClaudiu Manoil 	    ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
111653fad773SClaudiu Manoil 		priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
11172969b1f7SClaudiu Manoil }
1118d6ef0bccSClaudiu Manoil #endif
11192969b1f7SClaudiu Manoil 
11202969b1f7SClaudiu Manoil static void gfar_detect_errata(struct gfar_private *priv)
11212969b1f7SClaudiu Manoil {
11222969b1f7SClaudiu Manoil 	struct device *dev = &priv->ofdev->dev;
11232969b1f7SClaudiu Manoil 
11242969b1f7SClaudiu Manoil 	/* no plans to fix */
11252969b1f7SClaudiu Manoil 	priv->errata |= GFAR_ERRATA_A002;
11262969b1f7SClaudiu Manoil 
1127d6ef0bccSClaudiu Manoil #ifdef CONFIG_PPC
11282969b1f7SClaudiu Manoil 	if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
11292969b1f7SClaudiu Manoil 		__gfar_detect_errata_85xx(priv);
11302969b1f7SClaudiu Manoil 	else /* non-mpc85xx parts, i.e. e300 core based */
11312969b1f7SClaudiu Manoil 		__gfar_detect_errata_83xx(priv);
1132d6ef0bccSClaudiu Manoil #endif
1133ec21e2ecSJeff Kirsher 
1134ec21e2ecSJeff Kirsher 	if (priv->errata)
1135ec21e2ecSJeff Kirsher 		dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1136ec21e2ecSJeff Kirsher 			 priv->errata);
1137ec21e2ecSJeff Kirsher }
1138ec21e2ecSJeff Kirsher 
11390851133bSClaudiu Manoil void gfar_mac_reset(struct gfar_private *priv)
1140ec21e2ecSJeff Kirsher {
114120862788SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1142a328ac92SClaudiu Manoil 	u32 tempval;
1143ec21e2ecSJeff Kirsher 
1144ec21e2ecSJeff Kirsher 	/* Reset MAC layer */
1145ec21e2ecSJeff Kirsher 	gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
1146ec21e2ecSJeff Kirsher 
1147ec21e2ecSJeff Kirsher 	/* We need to delay at least 3 TX clocks */
1148a328ac92SClaudiu Manoil 	udelay(3);
1149ec21e2ecSJeff Kirsher 
115023402bddSClaudiu Manoil 	/* the soft reset bit is not self-resetting, so we need to
115123402bddSClaudiu Manoil 	 * clear it before resuming normal operation
115223402bddSClaudiu Manoil 	 */
115320862788SClaudiu Manoil 	gfar_write(&regs->maccfg1, 0);
1154ec21e2ecSJeff Kirsher 
1155a328ac92SClaudiu Manoil 	udelay(3);
1156a328ac92SClaudiu Manoil 
115775354148SClaudiu Manoil 	gfar_rx_offload_en(priv);
115888302648SClaudiu Manoil 
115988302648SClaudiu Manoil 	/* Initialize the max receive frame/buffer lengths */
116075354148SClaudiu Manoil 	gfar_write(&regs->maxfrm, GFAR_JUMBO_FRAME_SIZE);
116175354148SClaudiu Manoil 	gfar_write(&regs->mrblr, GFAR_RXB_SIZE);
1162a328ac92SClaudiu Manoil 
1163a328ac92SClaudiu Manoil 	/* Initialize the Minimum Frame Length Register */
1164a328ac92SClaudiu Manoil 	gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1165a328ac92SClaudiu Manoil 
1166ec21e2ecSJeff Kirsher 	/* Initialize MACCFG2. */
1167ec21e2ecSJeff Kirsher 	tempval = MACCFG2_INIT_SETTINGS;
116888302648SClaudiu Manoil 
116975354148SClaudiu Manoil 	/* eTSEC74 erratum: Rx frames of length MAXFRM or MAXFRM-1
117075354148SClaudiu Manoil 	 * are marked as truncated.  Avoid this by MACCFG2[Huge Frame]=1,
117175354148SClaudiu Manoil 	 * and by checking RxBD[LG] and discarding larger than MAXFRM.
117288302648SClaudiu Manoil 	 */
117375354148SClaudiu Manoil 	if (gfar_has_errata(priv, GFAR_ERRATA_74))
1174ec21e2ecSJeff Kirsher 		tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
117588302648SClaudiu Manoil 
1176ec21e2ecSJeff Kirsher 	gfar_write(&regs->maccfg2, tempval);
1177ec21e2ecSJeff Kirsher 
1178a328ac92SClaudiu Manoil 	/* Clear mac addr hash registers */
1179a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr0, 0);
1180a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr1, 0);
1181a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr2, 0);
1182a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr3, 0);
1183a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr4, 0);
1184a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr5, 0);
1185a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr6, 0);
1186a328ac92SClaudiu Manoil 	gfar_write(&regs->igaddr7, 0);
1187a328ac92SClaudiu Manoil 
1188a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr0, 0);
1189a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr1, 0);
1190a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr2, 0);
1191a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr3, 0);
1192a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr4, 0);
1193a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr5, 0);
1194a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr6, 0);
1195a328ac92SClaudiu Manoil 	gfar_write(&regs->gaddr7, 0);
1196a328ac92SClaudiu Manoil 
1197a328ac92SClaudiu Manoil 	if (priv->extended_hash)
1198a328ac92SClaudiu Manoil 		gfar_clear_exact_match(priv->ndev);
1199a328ac92SClaudiu Manoil 
1200a328ac92SClaudiu Manoil 	gfar_mac_rx_config(priv);
1201a328ac92SClaudiu Manoil 
1202a328ac92SClaudiu Manoil 	gfar_mac_tx_config(priv);
1203a328ac92SClaudiu Manoil 
1204a328ac92SClaudiu Manoil 	gfar_set_mac_address(priv->ndev);
1205a328ac92SClaudiu Manoil 
1206a328ac92SClaudiu Manoil 	gfar_set_multi(priv->ndev);
1207a328ac92SClaudiu Manoil 
1208a328ac92SClaudiu Manoil 	/* clear ievent and imask before configuring coalescing */
1209a328ac92SClaudiu Manoil 	gfar_ints_disable(priv);
1210a328ac92SClaudiu Manoil 
1211a328ac92SClaudiu Manoil 	/* Configure the coalescing support */
1212a328ac92SClaudiu Manoil 	gfar_configure_coalescing_all(priv);
1213a328ac92SClaudiu Manoil }
1214a328ac92SClaudiu Manoil 
1215a328ac92SClaudiu Manoil static void gfar_hw_init(struct gfar_private *priv)
1216a328ac92SClaudiu Manoil {
1217a328ac92SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1218a328ac92SClaudiu Manoil 	u32 attrs;
1219a328ac92SClaudiu Manoil 
1220a328ac92SClaudiu Manoil 	/* Stop the DMA engine now, in case it was running before
1221a328ac92SClaudiu Manoil 	 * (The firmware could have used it, and left it running).
1222a328ac92SClaudiu Manoil 	 */
1223a328ac92SClaudiu Manoil 	gfar_halt(priv);
1224a328ac92SClaudiu Manoil 
1225a328ac92SClaudiu Manoil 	gfar_mac_reset(priv);
1226a328ac92SClaudiu Manoil 
1227a328ac92SClaudiu Manoil 	/* Zero out the rmon mib registers if it has them */
1228a328ac92SClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1229a328ac92SClaudiu Manoil 		memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
1230a328ac92SClaudiu Manoil 
1231a328ac92SClaudiu Manoil 		/* Mask off the CAM interrupts */
1232a328ac92SClaudiu Manoil 		gfar_write(&regs->rmon.cam1, 0xffffffff);
1233a328ac92SClaudiu Manoil 		gfar_write(&regs->rmon.cam2, 0xffffffff);
1234a328ac92SClaudiu Manoil 	}
1235a328ac92SClaudiu Manoil 
1236ec21e2ecSJeff Kirsher 	/* Initialize ECNTRL */
1237ec21e2ecSJeff Kirsher 	gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
1238ec21e2ecSJeff Kirsher 
123934018fd4SClaudiu Manoil 	/* Set the extraction length and index */
124034018fd4SClaudiu Manoil 	attrs = ATTRELI_EL(priv->rx_stash_size) |
124134018fd4SClaudiu Manoil 		ATTRELI_EI(priv->rx_stash_index);
124234018fd4SClaudiu Manoil 
124334018fd4SClaudiu Manoil 	gfar_write(&regs->attreli, attrs);
124434018fd4SClaudiu Manoil 
124534018fd4SClaudiu Manoil 	/* Start with defaults, and add stashing
124634018fd4SClaudiu Manoil 	 * depending on driver parameters
124734018fd4SClaudiu Manoil 	 */
124834018fd4SClaudiu Manoil 	attrs = ATTR_INIT_SETTINGS;
124934018fd4SClaudiu Manoil 
125034018fd4SClaudiu Manoil 	if (priv->bd_stash_en)
125134018fd4SClaudiu Manoil 		attrs |= ATTR_BDSTASH;
125234018fd4SClaudiu Manoil 
125334018fd4SClaudiu Manoil 	if (priv->rx_stash_size != 0)
125434018fd4SClaudiu Manoil 		attrs |= ATTR_BUFSTASH;
125534018fd4SClaudiu Manoil 
125634018fd4SClaudiu Manoil 	gfar_write(&regs->attr, attrs);
125734018fd4SClaudiu Manoil 
125834018fd4SClaudiu Manoil 	/* FIFO configs */
125934018fd4SClaudiu Manoil 	gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
126034018fd4SClaudiu Manoil 	gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
126134018fd4SClaudiu Manoil 	gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
126234018fd4SClaudiu Manoil 
126320862788SClaudiu Manoil 	/* Program the interrupt steering regs, only for MG devices */
126420862788SClaudiu Manoil 	if (priv->num_grps > 1)
126520862788SClaudiu Manoil 		gfar_write_isrg(priv);
1266ec21e2ecSJeff Kirsher }
1267ec21e2ecSJeff Kirsher 
1268898157edSXiubo Li static void gfar_init_addr_hash_table(struct gfar_private *priv)
126920862788SClaudiu Manoil {
127020862788SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1271ec21e2ecSJeff Kirsher 
1272ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
1273ec21e2ecSJeff Kirsher 		priv->extended_hash = 1;
1274ec21e2ecSJeff Kirsher 		priv->hash_width = 9;
1275ec21e2ecSJeff Kirsher 
1276ec21e2ecSJeff Kirsher 		priv->hash_regs[0] = &regs->igaddr0;
1277ec21e2ecSJeff Kirsher 		priv->hash_regs[1] = &regs->igaddr1;
1278ec21e2ecSJeff Kirsher 		priv->hash_regs[2] = &regs->igaddr2;
1279ec21e2ecSJeff Kirsher 		priv->hash_regs[3] = &regs->igaddr3;
1280ec21e2ecSJeff Kirsher 		priv->hash_regs[4] = &regs->igaddr4;
1281ec21e2ecSJeff Kirsher 		priv->hash_regs[5] = &regs->igaddr5;
1282ec21e2ecSJeff Kirsher 		priv->hash_regs[6] = &regs->igaddr6;
1283ec21e2ecSJeff Kirsher 		priv->hash_regs[7] = &regs->igaddr7;
1284ec21e2ecSJeff Kirsher 		priv->hash_regs[8] = &regs->gaddr0;
1285ec21e2ecSJeff Kirsher 		priv->hash_regs[9] = &regs->gaddr1;
1286ec21e2ecSJeff Kirsher 		priv->hash_regs[10] = &regs->gaddr2;
1287ec21e2ecSJeff Kirsher 		priv->hash_regs[11] = &regs->gaddr3;
1288ec21e2ecSJeff Kirsher 		priv->hash_regs[12] = &regs->gaddr4;
1289ec21e2ecSJeff Kirsher 		priv->hash_regs[13] = &regs->gaddr5;
1290ec21e2ecSJeff Kirsher 		priv->hash_regs[14] = &regs->gaddr6;
1291ec21e2ecSJeff Kirsher 		priv->hash_regs[15] = &regs->gaddr7;
1292ec21e2ecSJeff Kirsher 
1293ec21e2ecSJeff Kirsher 	} else {
1294ec21e2ecSJeff Kirsher 		priv->extended_hash = 0;
1295ec21e2ecSJeff Kirsher 		priv->hash_width = 8;
1296ec21e2ecSJeff Kirsher 
1297ec21e2ecSJeff Kirsher 		priv->hash_regs[0] = &regs->gaddr0;
1298ec21e2ecSJeff Kirsher 		priv->hash_regs[1] = &regs->gaddr1;
1299ec21e2ecSJeff Kirsher 		priv->hash_regs[2] = &regs->gaddr2;
1300ec21e2ecSJeff Kirsher 		priv->hash_regs[3] = &regs->gaddr3;
1301ec21e2ecSJeff Kirsher 		priv->hash_regs[4] = &regs->gaddr4;
1302ec21e2ecSJeff Kirsher 		priv->hash_regs[5] = &regs->gaddr5;
1303ec21e2ecSJeff Kirsher 		priv->hash_regs[6] = &regs->gaddr6;
1304ec21e2ecSJeff Kirsher 		priv->hash_regs[7] = &regs->gaddr7;
1305ec21e2ecSJeff Kirsher 	}
130620862788SClaudiu Manoil }
130720862788SClaudiu Manoil 
130820862788SClaudiu Manoil /* Set up the ethernet device structure, private data,
130920862788SClaudiu Manoil  * and anything else we need before we start
131020862788SClaudiu Manoil  */
131120862788SClaudiu Manoil static int gfar_probe(struct platform_device *ofdev)
131220862788SClaudiu Manoil {
131320862788SClaudiu Manoil 	struct net_device *dev = NULL;
131420862788SClaudiu Manoil 	struct gfar_private *priv = NULL;
131520862788SClaudiu Manoil 	int err = 0, i;
131620862788SClaudiu Manoil 
131720862788SClaudiu Manoil 	err = gfar_of_init(ofdev, &dev);
131820862788SClaudiu Manoil 
131920862788SClaudiu Manoil 	if (err)
132020862788SClaudiu Manoil 		return err;
132120862788SClaudiu Manoil 
132220862788SClaudiu Manoil 	priv = netdev_priv(dev);
132320862788SClaudiu Manoil 	priv->ndev = dev;
132420862788SClaudiu Manoil 	priv->ofdev = ofdev;
132520862788SClaudiu Manoil 	priv->dev = &ofdev->dev;
132620862788SClaudiu Manoil 	SET_NETDEV_DEV(dev, &ofdev->dev);
132720862788SClaudiu Manoil 
132820862788SClaudiu Manoil 	INIT_WORK(&priv->reset_task, gfar_reset_task);
132920862788SClaudiu Manoil 
133020862788SClaudiu Manoil 	platform_set_drvdata(ofdev, priv);
133120862788SClaudiu Manoil 
133220862788SClaudiu Manoil 	gfar_detect_errata(priv);
133320862788SClaudiu Manoil 
133420862788SClaudiu Manoil 	/* Set the dev->base_addr to the gfar reg region */
133520862788SClaudiu Manoil 	dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
133620862788SClaudiu Manoil 
133720862788SClaudiu Manoil 	/* Fill in the dev structure */
133820862788SClaudiu Manoil 	dev->watchdog_timeo = TX_TIMEOUT;
133920862788SClaudiu Manoil 	dev->mtu = 1500;
134020862788SClaudiu Manoil 	dev->netdev_ops = &gfar_netdev_ops;
134120862788SClaudiu Manoil 	dev->ethtool_ops = &gfar_ethtool_ops;
134220862788SClaudiu Manoil 
134320862788SClaudiu Manoil 	/* Register for napi ...We are registering NAPI for each grp */
1344aeb12c5eSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
134571ff9e3dSClaudiu Manoil 		if (priv->poll_mode == GFAR_SQ_POLLING) {
134671ff9e3dSClaudiu Manoil 			netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
134771ff9e3dSClaudiu Manoil 				       gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
1348d64b5e85SEric Dumazet 			netif_tx_napi_add(dev, &priv->gfargrp[i].napi_tx,
134971ff9e3dSClaudiu Manoil 				       gfar_poll_tx_sq, 2);
135071ff9e3dSClaudiu Manoil 		} else {
1351aeb12c5eSClaudiu Manoil 			netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1352aeb12c5eSClaudiu Manoil 				       gfar_poll_rx, GFAR_DEV_WEIGHT);
1353d64b5e85SEric Dumazet 			netif_tx_napi_add(dev, &priv->gfargrp[i].napi_tx,
1354aeb12c5eSClaudiu Manoil 				       gfar_poll_tx, 2);
1355aeb12c5eSClaudiu Manoil 		}
1356aeb12c5eSClaudiu Manoil 	}
135720862788SClaudiu Manoil 
135820862788SClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
135920862788SClaudiu Manoil 		dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
136020862788SClaudiu Manoil 				   NETIF_F_RXCSUM;
136120862788SClaudiu Manoil 		dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
136220862788SClaudiu Manoil 				 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
136320862788SClaudiu Manoil 	}
136420862788SClaudiu Manoil 
136520862788SClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
136620862788SClaudiu Manoil 		dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
136720862788SClaudiu Manoil 				    NETIF_F_HW_VLAN_CTAG_RX;
136820862788SClaudiu Manoil 		dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
136920862788SClaudiu Manoil 	}
137020862788SClaudiu Manoil 
13713d23a05cSClaudiu Manoil 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
13723d23a05cSClaudiu Manoil 
137320862788SClaudiu Manoil 	gfar_init_addr_hash_table(priv);
1374ec21e2ecSJeff Kirsher 
1375532c37bcSClaudiu Manoil 	/* Insert receive time stamps into padding alignment bytes */
1376532c37bcSClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1377532c37bcSClaudiu Manoil 		priv->padding = 8;
1378ec21e2ecSJeff Kirsher 
1379ec21e2ecSJeff Kirsher 	if (dev->features & NETIF_F_IP_CSUM ||
1380ec21e2ecSJeff Kirsher 	    priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1381bee9e58cSWu Jiajun-B06378 		dev->needed_headroom = GMAC_FCB_LEN;
1382ec21e2ecSJeff Kirsher 
1383ec21e2ecSJeff Kirsher 	/* Initializing some of the rx/tx queue level parameters */
1384ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
1385ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1386ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1387ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1388ec21e2ecSJeff Kirsher 		priv->tx_queue[i]->txic = DEFAULT_TXIC;
1389ec21e2ecSJeff Kirsher 	}
1390ec21e2ecSJeff Kirsher 
1391ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
1392ec21e2ecSJeff Kirsher 		priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1393ec21e2ecSJeff Kirsher 		priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1394ec21e2ecSJeff Kirsher 		priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1395ec21e2ecSJeff Kirsher 	}
1396ec21e2ecSJeff Kirsher 
13977bff47daSHamish Martin 	/* Always enable rx filer if available */
13987bff47daSHamish Martin 	priv->rx_filer_enable =
13997bff47daSHamish Martin 	    (priv->device_flags & FSL_GIANFAR_DEV_HAS_RX_FILER) ? 1 : 0;
1400ec21e2ecSJeff Kirsher 	/* Enable most messages by default */
1401ec21e2ecSJeff Kirsher 	priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1402b98b8babSClaudiu Manoil 	/* use pritority h/w tx queue scheduling for single queue devices */
1403b98b8babSClaudiu Manoil 	if (priv->num_tx_queues == 1)
1404b98b8babSClaudiu Manoil 		priv->prio_sched_en = 1;
1405ec21e2ecSJeff Kirsher 
14060851133bSClaudiu Manoil 	set_bit(GFAR_DOWN, &priv->state);
14070851133bSClaudiu Manoil 
1408a328ac92SClaudiu Manoil 	gfar_hw_init(priv);
1409ec21e2ecSJeff Kirsher 
1410d4c642eaSFabio Estevam 	/* Carrier starts down, phylib will bring it up */
1411d4c642eaSFabio Estevam 	netif_carrier_off(dev);
1412d4c642eaSFabio Estevam 
1413ec21e2ecSJeff Kirsher 	err = register_netdev(dev);
1414ec21e2ecSJeff Kirsher 
1415ec21e2ecSJeff Kirsher 	if (err) {
1416ec21e2ecSJeff Kirsher 		pr_err("%s: Cannot register net device, aborting\n", dev->name);
1417ec21e2ecSJeff Kirsher 		goto register_fail;
1418ec21e2ecSJeff Kirsher 	}
1419ec21e2ecSJeff Kirsher 
14203e905b80SClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET)
14213e905b80SClaudiu Manoil 		priv->wol_supported |= GFAR_WOL_MAGIC;
14223e905b80SClaudiu Manoil 
14233e905b80SClaudiu Manoil 	if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_WAKE_ON_FILER) &&
14243e905b80SClaudiu Manoil 	    priv->rx_filer_enable)
14253e905b80SClaudiu Manoil 		priv->wol_supported |= GFAR_WOL_FILER_UCAST;
14263e905b80SClaudiu Manoil 
14273e905b80SClaudiu Manoil 	device_set_wakeup_capable(&ofdev->dev, priv->wol_supported);
1428ec21e2ecSJeff Kirsher 
1429ec21e2ecSJeff Kirsher 	/* fill out IRQ number and name fields */
1430ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_grps; i++) {
1431ee873fdaSClaudiu Manoil 		struct gfar_priv_grp *grp = &priv->gfargrp[i];
1432ec21e2ecSJeff Kirsher 		if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1433ee873fdaSClaudiu Manoil 			sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
14340015e551SJoe Perches 				dev->name, "_g", '0' + i, "_tx");
1435ee873fdaSClaudiu Manoil 			sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
14360015e551SJoe Perches 				dev->name, "_g", '0' + i, "_rx");
1437ee873fdaSClaudiu Manoil 			sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
14380015e551SJoe Perches 				dev->name, "_g", '0' + i, "_er");
1439ec21e2ecSJeff Kirsher 		} else
1440ee873fdaSClaudiu Manoil 			strcpy(gfar_irq(grp, TX)->name, dev->name);
1441ec21e2ecSJeff Kirsher 	}
1442ec21e2ecSJeff Kirsher 
1443ec21e2ecSJeff Kirsher 	/* Initialize the filer table */
1444ec21e2ecSJeff Kirsher 	gfar_init_filer_table(priv);
1445ec21e2ecSJeff Kirsher 
1446ec21e2ecSJeff Kirsher 	/* Print out the device info */
1447ec21e2ecSJeff Kirsher 	netdev_info(dev, "mac: %pM\n", dev->dev_addr);
1448ec21e2ecSJeff Kirsher 
14490977f817SJan Ceuleers 	/* Even more device info helps when determining which kernel
14500977f817SJan Ceuleers 	 * provided which set of benchmarks.
14510977f817SJan Ceuleers 	 */
1452ec21e2ecSJeff Kirsher 	netdev_info(dev, "Running with NAPI enabled\n");
1453ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++)
1454ec21e2ecSJeff Kirsher 		netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1455ec21e2ecSJeff Kirsher 			    i, priv->rx_queue[i]->rx_ring_size);
1456ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++)
1457ec21e2ecSJeff Kirsher 		netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1458ec21e2ecSJeff Kirsher 			    i, priv->tx_queue[i]->tx_ring_size);
1459ec21e2ecSJeff Kirsher 
1460ec21e2ecSJeff Kirsher 	return 0;
1461ec21e2ecSJeff Kirsher 
1462ec21e2ecSJeff Kirsher register_fail:
1463ec21e2ecSJeff Kirsher 	unmap_group_regs(priv);
146420862788SClaudiu Manoil 	gfar_free_rx_queues(priv);
146520862788SClaudiu Manoil 	gfar_free_tx_queues(priv);
1466ec21e2ecSJeff Kirsher 	of_node_put(priv->phy_node);
1467ec21e2ecSJeff Kirsher 	of_node_put(priv->tbi_node);
1468ee873fdaSClaudiu Manoil 	free_gfar_dev(priv);
1469ec21e2ecSJeff Kirsher 	return err;
1470ec21e2ecSJeff Kirsher }
1471ec21e2ecSJeff Kirsher 
1472ec21e2ecSJeff Kirsher static int gfar_remove(struct platform_device *ofdev)
1473ec21e2ecSJeff Kirsher {
14748513fbd8SJingoo Han 	struct gfar_private *priv = platform_get_drvdata(ofdev);
1475ec21e2ecSJeff Kirsher 
1476ec21e2ecSJeff Kirsher 	of_node_put(priv->phy_node);
1477ec21e2ecSJeff Kirsher 	of_node_put(priv->tbi_node);
1478ec21e2ecSJeff Kirsher 
1479ec21e2ecSJeff Kirsher 	unregister_netdev(priv->ndev);
1480ec21e2ecSJeff Kirsher 	unmap_group_regs(priv);
148120862788SClaudiu Manoil 	gfar_free_rx_queues(priv);
148220862788SClaudiu Manoil 	gfar_free_tx_queues(priv);
1483ee873fdaSClaudiu Manoil 	free_gfar_dev(priv);
1484ec21e2ecSJeff Kirsher 
1485ec21e2ecSJeff Kirsher 	return 0;
1486ec21e2ecSJeff Kirsher }
1487ec21e2ecSJeff Kirsher 
1488ec21e2ecSJeff Kirsher #ifdef CONFIG_PM
1489ec21e2ecSJeff Kirsher 
14903e905b80SClaudiu Manoil static void __gfar_filer_disable(struct gfar_private *priv)
14913e905b80SClaudiu Manoil {
14923e905b80SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
14933e905b80SClaudiu Manoil 	u32 temp;
14943e905b80SClaudiu Manoil 
14953e905b80SClaudiu Manoil 	temp = gfar_read(&regs->rctrl);
14963e905b80SClaudiu Manoil 	temp &= ~(RCTRL_FILREN | RCTRL_PRSDEP_INIT);
14973e905b80SClaudiu Manoil 	gfar_write(&regs->rctrl, temp);
14983e905b80SClaudiu Manoil }
14993e905b80SClaudiu Manoil 
15003e905b80SClaudiu Manoil static void __gfar_filer_enable(struct gfar_private *priv)
15013e905b80SClaudiu Manoil {
15023e905b80SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
15033e905b80SClaudiu Manoil 	u32 temp;
15043e905b80SClaudiu Manoil 
15053e905b80SClaudiu Manoil 	temp = gfar_read(&regs->rctrl);
15063e905b80SClaudiu Manoil 	temp |= RCTRL_FILREN | RCTRL_PRSDEP_INIT;
15073e905b80SClaudiu Manoil 	gfar_write(&regs->rctrl, temp);
15083e905b80SClaudiu Manoil }
15093e905b80SClaudiu Manoil 
15103e905b80SClaudiu Manoil /* Filer rules implementing wol capabilities */
15113e905b80SClaudiu Manoil static void gfar_filer_config_wol(struct gfar_private *priv)
15123e905b80SClaudiu Manoil {
15133e905b80SClaudiu Manoil 	unsigned int i;
15143e905b80SClaudiu Manoil 	u32 rqfcr;
15153e905b80SClaudiu Manoil 
15163e905b80SClaudiu Manoil 	__gfar_filer_disable(priv);
15173e905b80SClaudiu Manoil 
15183e905b80SClaudiu Manoil 	/* clear the filer table, reject any packet by default */
15193e905b80SClaudiu Manoil 	rqfcr = RQFCR_RJE | RQFCR_CMP_MATCH;
15203e905b80SClaudiu Manoil 	for (i = 0; i <= MAX_FILER_IDX; i++)
15213e905b80SClaudiu Manoil 		gfar_write_filer(priv, i, rqfcr, 0);
15223e905b80SClaudiu Manoil 
15233e905b80SClaudiu Manoil 	i = 0;
15243e905b80SClaudiu Manoil 	if (priv->wol_opts & GFAR_WOL_FILER_UCAST) {
15253e905b80SClaudiu Manoil 		/* unicast packet, accept it */
15263e905b80SClaudiu Manoil 		struct net_device *ndev = priv->ndev;
15273e905b80SClaudiu Manoil 		/* get the default rx queue index */
15283e905b80SClaudiu Manoil 		u8 qindex = (u8)priv->gfargrp[0].rx_queue->qindex;
15293e905b80SClaudiu Manoil 		u32 dest_mac_addr = (ndev->dev_addr[0] << 16) |
15303e905b80SClaudiu Manoil 				    (ndev->dev_addr[1] << 8) |
15313e905b80SClaudiu Manoil 				     ndev->dev_addr[2];
15323e905b80SClaudiu Manoil 
15333e905b80SClaudiu Manoil 		rqfcr = (qindex << 10) | RQFCR_AND |
15343e905b80SClaudiu Manoil 			RQFCR_CMP_EXACT | RQFCR_PID_DAH;
15353e905b80SClaudiu Manoil 
15363e905b80SClaudiu Manoil 		gfar_write_filer(priv, i++, rqfcr, dest_mac_addr);
15373e905b80SClaudiu Manoil 
15383e905b80SClaudiu Manoil 		dest_mac_addr = (ndev->dev_addr[3] << 16) |
15393e905b80SClaudiu Manoil 				(ndev->dev_addr[4] << 8) |
15403e905b80SClaudiu Manoil 				 ndev->dev_addr[5];
15413e905b80SClaudiu Manoil 		rqfcr = (qindex << 10) | RQFCR_GPI |
15423e905b80SClaudiu Manoil 			RQFCR_CMP_EXACT | RQFCR_PID_DAL;
15433e905b80SClaudiu Manoil 		gfar_write_filer(priv, i++, rqfcr, dest_mac_addr);
15443e905b80SClaudiu Manoil 	}
15453e905b80SClaudiu Manoil 
15463e905b80SClaudiu Manoil 	__gfar_filer_enable(priv);
15473e905b80SClaudiu Manoil }
15483e905b80SClaudiu Manoil 
15493e905b80SClaudiu Manoil static void gfar_filer_restore_table(struct gfar_private *priv)
15503e905b80SClaudiu Manoil {
15513e905b80SClaudiu Manoil 	u32 rqfcr, rqfpr;
15523e905b80SClaudiu Manoil 	unsigned int i;
15533e905b80SClaudiu Manoil 
15543e905b80SClaudiu Manoil 	__gfar_filer_disable(priv);
15553e905b80SClaudiu Manoil 
15563e905b80SClaudiu Manoil 	for (i = 0; i <= MAX_FILER_IDX; i++) {
15573e905b80SClaudiu Manoil 		rqfcr = priv->ftp_rqfcr[i];
15583e905b80SClaudiu Manoil 		rqfpr = priv->ftp_rqfpr[i];
15593e905b80SClaudiu Manoil 		gfar_write_filer(priv, i, rqfcr, rqfpr);
15603e905b80SClaudiu Manoil 	}
15613e905b80SClaudiu Manoil 
15623e905b80SClaudiu Manoil 	__gfar_filer_enable(priv);
15633e905b80SClaudiu Manoil }
15643e905b80SClaudiu Manoil 
15653e905b80SClaudiu Manoil /* gfar_start() for Rx only and with the FGPI filer interrupt enabled */
15663e905b80SClaudiu Manoil static void gfar_start_wol_filer(struct gfar_private *priv)
15673e905b80SClaudiu Manoil {
15683e905b80SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
15693e905b80SClaudiu Manoil 	u32 tempval;
15703e905b80SClaudiu Manoil 	int i = 0;
15713e905b80SClaudiu Manoil 
15723e905b80SClaudiu Manoil 	/* Enable Rx hw queues */
15733e905b80SClaudiu Manoil 	gfar_write(&regs->rqueue, priv->rqueue);
15743e905b80SClaudiu Manoil 
15753e905b80SClaudiu Manoil 	/* Initialize DMACTRL to have WWR and WOP */
15763e905b80SClaudiu Manoil 	tempval = gfar_read(&regs->dmactrl);
15773e905b80SClaudiu Manoil 	tempval |= DMACTRL_INIT_SETTINGS;
15783e905b80SClaudiu Manoil 	gfar_write(&regs->dmactrl, tempval);
15793e905b80SClaudiu Manoil 
15803e905b80SClaudiu Manoil 	/* Make sure we aren't stopped */
15813e905b80SClaudiu Manoil 	tempval = gfar_read(&regs->dmactrl);
15823e905b80SClaudiu Manoil 	tempval &= ~DMACTRL_GRS;
15833e905b80SClaudiu Manoil 	gfar_write(&regs->dmactrl, tempval);
15843e905b80SClaudiu Manoil 
15853e905b80SClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
15863e905b80SClaudiu Manoil 		regs = priv->gfargrp[i].regs;
15873e905b80SClaudiu Manoil 		/* Clear RHLT, so that the DMA starts polling now */
15883e905b80SClaudiu Manoil 		gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
15893e905b80SClaudiu Manoil 		/* enable the Filer General Purpose Interrupt */
15903e905b80SClaudiu Manoil 		gfar_write(&regs->imask, IMASK_FGPI);
15913e905b80SClaudiu Manoil 	}
15923e905b80SClaudiu Manoil 
15933e905b80SClaudiu Manoil 	/* Enable Rx DMA */
15943e905b80SClaudiu Manoil 	tempval = gfar_read(&regs->maccfg1);
15953e905b80SClaudiu Manoil 	tempval |= MACCFG1_RX_EN;
15963e905b80SClaudiu Manoil 	gfar_write(&regs->maccfg1, tempval);
15973e905b80SClaudiu Manoil }
15983e905b80SClaudiu Manoil 
1599ec21e2ecSJeff Kirsher static int gfar_suspend(struct device *dev)
1600ec21e2ecSJeff Kirsher {
1601ec21e2ecSJeff Kirsher 	struct gfar_private *priv = dev_get_drvdata(dev);
1602ec21e2ecSJeff Kirsher 	struct net_device *ndev = priv->ndev;
1603ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1604ec21e2ecSJeff Kirsher 	u32 tempval;
16053e905b80SClaudiu Manoil 	u16 wol = priv->wol_opts;
1606ec21e2ecSJeff Kirsher 
1607614b4242SClaudiu Manoil 	if (!netif_running(ndev))
1608614b4242SClaudiu Manoil 		return 0;
1609ec21e2ecSJeff Kirsher 
1610ec21e2ecSJeff Kirsher 	disable_napi(priv);
1611614b4242SClaudiu Manoil 	netif_tx_lock(ndev);
1612614b4242SClaudiu Manoil 	netif_device_detach(ndev);
1613614b4242SClaudiu Manoil 	netif_tx_unlock(ndev);
1614614b4242SClaudiu Manoil 
1615614b4242SClaudiu Manoil 	gfar_halt(priv);
1616ec21e2ecSJeff Kirsher 
16173e905b80SClaudiu Manoil 	if (wol & GFAR_WOL_MAGIC) {
1618ec21e2ecSJeff Kirsher 		/* Enable interrupt on Magic Packet */
1619ec21e2ecSJeff Kirsher 		gfar_write(&regs->imask, IMASK_MAG);
1620ec21e2ecSJeff Kirsher 
1621ec21e2ecSJeff Kirsher 		/* Enable Magic Packet mode */
1622ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->maccfg2);
1623ec21e2ecSJeff Kirsher 		tempval |= MACCFG2_MPEN;
1624ec21e2ecSJeff Kirsher 		gfar_write(&regs->maccfg2, tempval);
1625614b4242SClaudiu Manoil 
1626614b4242SClaudiu Manoil 		/* re-enable the Rx block */
1627614b4242SClaudiu Manoil 		tempval = gfar_read(&regs->maccfg1);
1628614b4242SClaudiu Manoil 		tempval |= MACCFG1_RX_EN;
1629614b4242SClaudiu Manoil 		gfar_write(&regs->maccfg1, tempval);
1630614b4242SClaudiu Manoil 
16313e905b80SClaudiu Manoil 	} else if (wol & GFAR_WOL_FILER_UCAST) {
16323e905b80SClaudiu Manoil 		gfar_filer_config_wol(priv);
16333e905b80SClaudiu Manoil 		gfar_start_wol_filer(priv);
16343e905b80SClaudiu Manoil 
1635ec21e2ecSJeff Kirsher 	} else {
1636ec21e2ecSJeff Kirsher 		phy_stop(priv->phydev);
1637ec21e2ecSJeff Kirsher 	}
1638ec21e2ecSJeff Kirsher 
1639ec21e2ecSJeff Kirsher 	return 0;
1640ec21e2ecSJeff Kirsher }
1641ec21e2ecSJeff Kirsher 
1642ec21e2ecSJeff Kirsher static int gfar_resume(struct device *dev)
1643ec21e2ecSJeff Kirsher {
1644ec21e2ecSJeff Kirsher 	struct gfar_private *priv = dev_get_drvdata(dev);
1645ec21e2ecSJeff Kirsher 	struct net_device *ndev = priv->ndev;
1646ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1647ec21e2ecSJeff Kirsher 	u32 tempval;
16483e905b80SClaudiu Manoil 	u16 wol = priv->wol_opts;
1649ec21e2ecSJeff Kirsher 
1650614b4242SClaudiu Manoil 	if (!netif_running(ndev))
1651ec21e2ecSJeff Kirsher 		return 0;
1652ec21e2ecSJeff Kirsher 
16533e905b80SClaudiu Manoil 	if (wol & GFAR_WOL_MAGIC) {
1654614b4242SClaudiu Manoil 		/* Disable Magic Packet mode */
1655ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->maccfg2);
1656ec21e2ecSJeff Kirsher 		tempval &= ~MACCFG2_MPEN;
1657ec21e2ecSJeff Kirsher 		gfar_write(&regs->maccfg2, tempval);
16583e905b80SClaudiu Manoil 
16593e905b80SClaudiu Manoil 	} else if (wol & GFAR_WOL_FILER_UCAST) {
16603e905b80SClaudiu Manoil 		/* need to stop rx only, tx is already down */
16613e905b80SClaudiu Manoil 		gfar_halt(priv);
16623e905b80SClaudiu Manoil 		gfar_filer_restore_table(priv);
16633e905b80SClaudiu Manoil 
1664614b4242SClaudiu Manoil 	} else {
1665614b4242SClaudiu Manoil 		phy_start(priv->phydev);
1666614b4242SClaudiu Manoil 	}
1667ec21e2ecSJeff Kirsher 
1668c10650b6SClaudiu Manoil 	gfar_start(priv);
1669ec21e2ecSJeff Kirsher 
1670ec21e2ecSJeff Kirsher 	netif_device_attach(ndev);
1671ec21e2ecSJeff Kirsher 	enable_napi(priv);
1672ec21e2ecSJeff Kirsher 
1673ec21e2ecSJeff Kirsher 	return 0;
1674ec21e2ecSJeff Kirsher }
1675ec21e2ecSJeff Kirsher 
1676ec21e2ecSJeff Kirsher static int gfar_restore(struct device *dev)
1677ec21e2ecSJeff Kirsher {
1678ec21e2ecSJeff Kirsher 	struct gfar_private *priv = dev_get_drvdata(dev);
1679ec21e2ecSJeff Kirsher 	struct net_device *ndev = priv->ndev;
1680ec21e2ecSJeff Kirsher 
1681103cdd1dSWang Dongsheng 	if (!netif_running(ndev)) {
1682103cdd1dSWang Dongsheng 		netif_device_attach(ndev);
1683103cdd1dSWang Dongsheng 
1684ec21e2ecSJeff Kirsher 		return 0;
1685103cdd1dSWang Dongsheng 	}
1686ec21e2ecSJeff Kirsher 
168776f31e8bSClaudiu Manoil 	gfar_init_bds(ndev);
16881eb8f7a7SClaudiu Manoil 
1689a328ac92SClaudiu Manoil 	gfar_mac_reset(priv);
1690a328ac92SClaudiu Manoil 
1691a328ac92SClaudiu Manoil 	gfar_init_tx_rx_base(priv);
1692a328ac92SClaudiu Manoil 
1693c10650b6SClaudiu Manoil 	gfar_start(priv);
1694ec21e2ecSJeff Kirsher 
1695ec21e2ecSJeff Kirsher 	priv->oldlink = 0;
1696ec21e2ecSJeff Kirsher 	priv->oldspeed = 0;
1697ec21e2ecSJeff Kirsher 	priv->oldduplex = -1;
1698ec21e2ecSJeff Kirsher 
1699ec21e2ecSJeff Kirsher 	if (priv->phydev)
1700ec21e2ecSJeff Kirsher 		phy_start(priv->phydev);
1701ec21e2ecSJeff Kirsher 
1702ec21e2ecSJeff Kirsher 	netif_device_attach(ndev);
1703ec21e2ecSJeff Kirsher 	enable_napi(priv);
1704ec21e2ecSJeff Kirsher 
1705ec21e2ecSJeff Kirsher 	return 0;
1706ec21e2ecSJeff Kirsher }
1707ec21e2ecSJeff Kirsher 
1708ec21e2ecSJeff Kirsher static struct dev_pm_ops gfar_pm_ops = {
1709ec21e2ecSJeff Kirsher 	.suspend = gfar_suspend,
1710ec21e2ecSJeff Kirsher 	.resume = gfar_resume,
1711ec21e2ecSJeff Kirsher 	.freeze = gfar_suspend,
1712ec21e2ecSJeff Kirsher 	.thaw = gfar_resume,
1713ec21e2ecSJeff Kirsher 	.restore = gfar_restore,
1714ec21e2ecSJeff Kirsher };
1715ec21e2ecSJeff Kirsher 
1716ec21e2ecSJeff Kirsher #define GFAR_PM_OPS (&gfar_pm_ops)
1717ec21e2ecSJeff Kirsher 
1718ec21e2ecSJeff Kirsher #else
1719ec21e2ecSJeff Kirsher 
1720ec21e2ecSJeff Kirsher #define GFAR_PM_OPS NULL
1721ec21e2ecSJeff Kirsher 
1722ec21e2ecSJeff Kirsher #endif
1723ec21e2ecSJeff Kirsher 
1724ec21e2ecSJeff Kirsher /* Reads the controller's registers to determine what interface
1725ec21e2ecSJeff Kirsher  * connects it to the PHY.
1726ec21e2ecSJeff Kirsher  */
1727ec21e2ecSJeff Kirsher static phy_interface_t gfar_get_interface(struct net_device *dev)
1728ec21e2ecSJeff Kirsher {
1729ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1730ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1731ec21e2ecSJeff Kirsher 	u32 ecntrl;
1732ec21e2ecSJeff Kirsher 
1733ec21e2ecSJeff Kirsher 	ecntrl = gfar_read(&regs->ecntrl);
1734ec21e2ecSJeff Kirsher 
1735ec21e2ecSJeff Kirsher 	if (ecntrl & ECNTRL_SGMII_MODE)
1736ec21e2ecSJeff Kirsher 		return PHY_INTERFACE_MODE_SGMII;
1737ec21e2ecSJeff Kirsher 
1738ec21e2ecSJeff Kirsher 	if (ecntrl & ECNTRL_TBI_MODE) {
1739ec21e2ecSJeff Kirsher 		if (ecntrl & ECNTRL_REDUCED_MODE)
1740ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_RTBI;
1741ec21e2ecSJeff Kirsher 		else
1742ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_TBI;
1743ec21e2ecSJeff Kirsher 	}
1744ec21e2ecSJeff Kirsher 
1745ec21e2ecSJeff Kirsher 	if (ecntrl & ECNTRL_REDUCED_MODE) {
1746bc4598bcSJan Ceuleers 		if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
1747ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_RMII;
1748bc4598bcSJan Ceuleers 		}
1749ec21e2ecSJeff Kirsher 		else {
1750ec21e2ecSJeff Kirsher 			phy_interface_t interface = priv->interface;
1751ec21e2ecSJeff Kirsher 
17520977f817SJan Ceuleers 			/* This isn't autodetected right now, so it must
1753ec21e2ecSJeff Kirsher 			 * be set by the device tree or platform code.
1754ec21e2ecSJeff Kirsher 			 */
1755ec21e2ecSJeff Kirsher 			if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1756ec21e2ecSJeff Kirsher 				return PHY_INTERFACE_MODE_RGMII_ID;
1757ec21e2ecSJeff Kirsher 
1758ec21e2ecSJeff Kirsher 			return PHY_INTERFACE_MODE_RGMII;
1759ec21e2ecSJeff Kirsher 		}
1760ec21e2ecSJeff Kirsher 	}
1761ec21e2ecSJeff Kirsher 
1762ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
1763ec21e2ecSJeff Kirsher 		return PHY_INTERFACE_MODE_GMII;
1764ec21e2ecSJeff Kirsher 
1765ec21e2ecSJeff Kirsher 	return PHY_INTERFACE_MODE_MII;
1766ec21e2ecSJeff Kirsher }
1767ec21e2ecSJeff Kirsher 
1768ec21e2ecSJeff Kirsher 
1769ec21e2ecSJeff Kirsher /* Initializes driver's PHY state, and attaches to the PHY.
1770ec21e2ecSJeff Kirsher  * Returns 0 on success.
1771ec21e2ecSJeff Kirsher  */
1772ec21e2ecSJeff Kirsher static int init_phy(struct net_device *dev)
1773ec21e2ecSJeff Kirsher {
1774ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1775ec21e2ecSJeff Kirsher 	uint gigabit_support =
1776ec21e2ecSJeff Kirsher 		priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
177723402bddSClaudiu Manoil 		GFAR_SUPPORTED_GBIT : 0;
1778ec21e2ecSJeff Kirsher 	phy_interface_t interface;
1779ec21e2ecSJeff Kirsher 
1780ec21e2ecSJeff Kirsher 	priv->oldlink = 0;
1781ec21e2ecSJeff Kirsher 	priv->oldspeed = 0;
1782ec21e2ecSJeff Kirsher 	priv->oldduplex = -1;
1783ec21e2ecSJeff Kirsher 
1784ec21e2ecSJeff Kirsher 	interface = gfar_get_interface(dev);
1785ec21e2ecSJeff Kirsher 
1786ec21e2ecSJeff Kirsher 	priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1787ec21e2ecSJeff Kirsher 				      interface);
1788ec21e2ecSJeff Kirsher 	if (!priv->phydev) {
1789ec21e2ecSJeff Kirsher 		dev_err(&dev->dev, "could not attach to PHY\n");
1790ec21e2ecSJeff Kirsher 		return -ENODEV;
1791ec21e2ecSJeff Kirsher 	}
1792ec21e2ecSJeff Kirsher 
1793ec21e2ecSJeff Kirsher 	if (interface == PHY_INTERFACE_MODE_SGMII)
1794ec21e2ecSJeff Kirsher 		gfar_configure_serdes(dev);
1795ec21e2ecSJeff Kirsher 
1796ec21e2ecSJeff Kirsher 	/* Remove any features not supported by the controller */
1797ec21e2ecSJeff Kirsher 	priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1798ec21e2ecSJeff Kirsher 	priv->phydev->advertising = priv->phydev->supported;
1799ec21e2ecSJeff Kirsher 
1800cf987afcSPavaluca Matei-B46610 	/* Add support for flow control, but don't advertise it by default */
1801cf987afcSPavaluca Matei-B46610 	priv->phydev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1802cf987afcSPavaluca Matei-B46610 
1803ec21e2ecSJeff Kirsher 	return 0;
1804ec21e2ecSJeff Kirsher }
1805ec21e2ecSJeff Kirsher 
18060977f817SJan Ceuleers /* Initialize TBI PHY interface for communicating with the
1807ec21e2ecSJeff Kirsher  * SERDES lynx PHY on the chip.  We communicate with this PHY
1808ec21e2ecSJeff Kirsher  * through the MDIO bus on each controller, treating it as a
1809ec21e2ecSJeff Kirsher  * "normal" PHY at the address found in the TBIPA register.  We assume
1810ec21e2ecSJeff Kirsher  * that the TBIPA register is valid.  Either the MDIO bus code will set
1811ec21e2ecSJeff Kirsher  * it to a value that doesn't conflict with other PHYs on the bus, or the
1812ec21e2ecSJeff Kirsher  * value doesn't matter, as there are no other PHYs on the bus.
1813ec21e2ecSJeff Kirsher  */
1814ec21e2ecSJeff Kirsher static void gfar_configure_serdes(struct net_device *dev)
1815ec21e2ecSJeff Kirsher {
1816ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1817ec21e2ecSJeff Kirsher 	struct phy_device *tbiphy;
1818ec21e2ecSJeff Kirsher 
1819ec21e2ecSJeff Kirsher 	if (!priv->tbi_node) {
1820ec21e2ecSJeff Kirsher 		dev_warn(&dev->dev, "error: SGMII mode requires that the "
1821ec21e2ecSJeff Kirsher 				    "device tree specify a tbi-handle\n");
1822ec21e2ecSJeff Kirsher 		return;
1823ec21e2ecSJeff Kirsher 	}
1824ec21e2ecSJeff Kirsher 
1825ec21e2ecSJeff Kirsher 	tbiphy = of_phy_find_device(priv->tbi_node);
1826ec21e2ecSJeff Kirsher 	if (!tbiphy) {
1827ec21e2ecSJeff Kirsher 		dev_err(&dev->dev, "error: Could not get TBI device\n");
1828ec21e2ecSJeff Kirsher 		return;
1829ec21e2ecSJeff Kirsher 	}
1830ec21e2ecSJeff Kirsher 
18310977f817SJan Ceuleers 	/* If the link is already up, we must already be ok, and don't need to
1832ec21e2ecSJeff Kirsher 	 * configure and reset the TBI<->SerDes link.  Maybe U-Boot configured
1833ec21e2ecSJeff Kirsher 	 * everything for us?  Resetting it takes the link down and requires
1834ec21e2ecSJeff Kirsher 	 * several seconds for it to come back.
1835ec21e2ecSJeff Kirsher 	 */
183638737e49SRussell King 	if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS) {
1837e5a03bfdSAndrew Lunn 		put_device(&tbiphy->mdio.dev);
1838ec21e2ecSJeff Kirsher 		return;
183938737e49SRussell King 	}
1840ec21e2ecSJeff Kirsher 
1841ec21e2ecSJeff Kirsher 	/* Single clk mode, mii mode off(for serdes communication) */
1842ec21e2ecSJeff Kirsher 	phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
1843ec21e2ecSJeff Kirsher 
1844ec21e2ecSJeff Kirsher 	phy_write(tbiphy, MII_ADVERTISE,
1845ec21e2ecSJeff Kirsher 		  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1846ec21e2ecSJeff Kirsher 		  ADVERTISE_1000XPSE_ASYM);
1847ec21e2ecSJeff Kirsher 
1848bc4598bcSJan Ceuleers 	phy_write(tbiphy, MII_BMCR,
1849bc4598bcSJan Ceuleers 		  BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1850bc4598bcSJan Ceuleers 		  BMCR_SPEED1000);
185104d53b20SRussell King 
1852e5a03bfdSAndrew Lunn 	put_device(&tbiphy->mdio.dev);
1853ec21e2ecSJeff Kirsher }
1854ec21e2ecSJeff Kirsher 
1855ec21e2ecSJeff Kirsher static int __gfar_is_rx_idle(struct gfar_private *priv)
1856ec21e2ecSJeff Kirsher {
1857ec21e2ecSJeff Kirsher 	u32 res;
1858ec21e2ecSJeff Kirsher 
18590977f817SJan Ceuleers 	/* Normaly TSEC should not hang on GRS commands, so we should
1860ec21e2ecSJeff Kirsher 	 * actually wait for IEVENT_GRSC flag.
1861ec21e2ecSJeff Kirsher 	 */
1862ad3660c2SClaudiu Manoil 	if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
1863ec21e2ecSJeff Kirsher 		return 0;
1864ec21e2ecSJeff Kirsher 
18650977f817SJan Ceuleers 	/* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
1866ec21e2ecSJeff Kirsher 	 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1867ec21e2ecSJeff Kirsher 	 * and the Rx can be safely reset.
1868ec21e2ecSJeff Kirsher 	 */
1869ec21e2ecSJeff Kirsher 	res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1870ec21e2ecSJeff Kirsher 	res &= 0x7f807f80;
1871ec21e2ecSJeff Kirsher 	if ((res & 0xffff) == (res >> 16))
1872ec21e2ecSJeff Kirsher 		return 1;
1873ec21e2ecSJeff Kirsher 
1874ec21e2ecSJeff Kirsher 	return 0;
1875ec21e2ecSJeff Kirsher }
1876ec21e2ecSJeff Kirsher 
1877ec21e2ecSJeff Kirsher /* Halt the receive and transmit queues */
1878c10650b6SClaudiu Manoil static void gfar_halt_nodisable(struct gfar_private *priv)
1879ec21e2ecSJeff Kirsher {
1880efeddce7SClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1881ec21e2ecSJeff Kirsher 	u32 tempval;
1882a4feee89SClaudiu Manoil 	unsigned int timeout;
1883a4feee89SClaudiu Manoil 	int stopped;
1884ec21e2ecSJeff Kirsher 
1885efeddce7SClaudiu Manoil 	gfar_ints_disable(priv);
1886ec21e2ecSJeff Kirsher 
1887a4feee89SClaudiu Manoil 	if (gfar_is_dma_stopped(priv))
1888a4feee89SClaudiu Manoil 		return;
1889a4feee89SClaudiu Manoil 
1890ec21e2ecSJeff Kirsher 	/* Stop the DMA, and wait for it to stop */
1891ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->dmactrl);
1892ec21e2ecSJeff Kirsher 	tempval |= (DMACTRL_GRS | DMACTRL_GTS);
1893ec21e2ecSJeff Kirsher 	gfar_write(&regs->dmactrl, tempval);
1894ec21e2ecSJeff Kirsher 
1895a4feee89SClaudiu Manoil retry:
1896a4feee89SClaudiu Manoil 	timeout = 1000;
1897a4feee89SClaudiu Manoil 	while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) {
1898a4feee89SClaudiu Manoil 		cpu_relax();
1899a4feee89SClaudiu Manoil 		timeout--;
1900ec21e2ecSJeff Kirsher 	}
1901a4feee89SClaudiu Manoil 
1902a4feee89SClaudiu Manoil 	if (!timeout)
1903a4feee89SClaudiu Manoil 		stopped = gfar_is_dma_stopped(priv);
1904a4feee89SClaudiu Manoil 
1905a4feee89SClaudiu Manoil 	if (!stopped && !gfar_is_rx_dma_stopped(priv) &&
1906a4feee89SClaudiu Manoil 	    !__gfar_is_rx_idle(priv))
1907a4feee89SClaudiu Manoil 		goto retry;
1908ec21e2ecSJeff Kirsher }
1909ec21e2ecSJeff Kirsher 
1910ec21e2ecSJeff Kirsher /* Halt the receive and transmit queues */
1911c10650b6SClaudiu Manoil void gfar_halt(struct gfar_private *priv)
1912ec21e2ecSJeff Kirsher {
1913ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1914ec21e2ecSJeff Kirsher 	u32 tempval;
1915ec21e2ecSJeff Kirsher 
1916c10650b6SClaudiu Manoil 	/* Dissable the Rx/Tx hw queues */
1917c10650b6SClaudiu Manoil 	gfar_write(&regs->rqueue, 0);
1918c10650b6SClaudiu Manoil 	gfar_write(&regs->tqueue, 0);
1919ec21e2ecSJeff Kirsher 
1920c10650b6SClaudiu Manoil 	mdelay(10);
1921c10650b6SClaudiu Manoil 
1922c10650b6SClaudiu Manoil 	gfar_halt_nodisable(priv);
1923c10650b6SClaudiu Manoil 
1924c10650b6SClaudiu Manoil 	/* Disable Rx/Tx DMA */
1925ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->maccfg1);
1926ec21e2ecSJeff Kirsher 	tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1927ec21e2ecSJeff Kirsher 	gfar_write(&regs->maccfg1, tempval);
1928ec21e2ecSJeff Kirsher }
1929ec21e2ecSJeff Kirsher 
1930ec21e2ecSJeff Kirsher void stop_gfar(struct net_device *dev)
1931ec21e2ecSJeff Kirsher {
1932ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
1933ec21e2ecSJeff Kirsher 
19340851133bSClaudiu Manoil 	netif_tx_stop_all_queues(dev);
1935ec21e2ecSJeff Kirsher 
19364e857c58SPeter Zijlstra 	smp_mb__before_atomic();
19370851133bSClaudiu Manoil 	set_bit(GFAR_DOWN, &priv->state);
19384e857c58SPeter Zijlstra 	smp_mb__after_atomic();
1939ec21e2ecSJeff Kirsher 
19400851133bSClaudiu Manoil 	disable_napi(priv);
1941ec21e2ecSJeff Kirsher 
19420851133bSClaudiu Manoil 	/* disable ints and gracefully shut down Rx/Tx DMA */
1943c10650b6SClaudiu Manoil 	gfar_halt(priv);
1944ec21e2ecSJeff Kirsher 
19450851133bSClaudiu Manoil 	phy_stop(priv->phydev);
1946ec21e2ecSJeff Kirsher 
1947ec21e2ecSJeff Kirsher 	free_skb_resources(priv);
1948ec21e2ecSJeff Kirsher }
1949ec21e2ecSJeff Kirsher 
1950ec21e2ecSJeff Kirsher static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
1951ec21e2ecSJeff Kirsher {
1952ec21e2ecSJeff Kirsher 	struct txbd8 *txbdp;
1953ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(tx_queue->dev);
1954ec21e2ecSJeff Kirsher 	int i, j;
1955ec21e2ecSJeff Kirsher 
1956ec21e2ecSJeff Kirsher 	txbdp = tx_queue->tx_bd_base;
1957ec21e2ecSJeff Kirsher 
1958ec21e2ecSJeff Kirsher 	for (i = 0; i < tx_queue->tx_ring_size; i++) {
1959ec21e2ecSJeff Kirsher 		if (!tx_queue->tx_skbuff[i])
1960ec21e2ecSJeff Kirsher 			continue;
1961ec21e2ecSJeff Kirsher 
1962a7312d58SClaudiu Manoil 		dma_unmap_single(priv->dev, be32_to_cpu(txbdp->bufPtr),
1963a7312d58SClaudiu Manoil 				 be16_to_cpu(txbdp->length), DMA_TO_DEVICE);
1964ec21e2ecSJeff Kirsher 		txbdp->lstatus = 0;
1965ec21e2ecSJeff Kirsher 		for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
1966ec21e2ecSJeff Kirsher 		     j++) {
1967ec21e2ecSJeff Kirsher 			txbdp++;
1968a7312d58SClaudiu Manoil 			dma_unmap_page(priv->dev, be32_to_cpu(txbdp->bufPtr),
1969a7312d58SClaudiu Manoil 				       be16_to_cpu(txbdp->length),
1970a7312d58SClaudiu Manoil 				       DMA_TO_DEVICE);
1971ec21e2ecSJeff Kirsher 		}
1972ec21e2ecSJeff Kirsher 		txbdp++;
1973ec21e2ecSJeff Kirsher 		dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1974ec21e2ecSJeff Kirsher 		tx_queue->tx_skbuff[i] = NULL;
1975ec21e2ecSJeff Kirsher 	}
1976ec21e2ecSJeff Kirsher 	kfree(tx_queue->tx_skbuff);
19771eb8f7a7SClaudiu Manoil 	tx_queue->tx_skbuff = NULL;
1978ec21e2ecSJeff Kirsher }
1979ec21e2ecSJeff Kirsher 
1980ec21e2ecSJeff Kirsher static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1981ec21e2ecSJeff Kirsher {
1982ec21e2ecSJeff Kirsher 	int i;
1983ec21e2ecSJeff Kirsher 
198475354148SClaudiu Manoil 	struct rxbd8 *rxbdp = rx_queue->rx_bd_base;
198575354148SClaudiu Manoil 
198675354148SClaudiu Manoil 	if (rx_queue->skb)
198775354148SClaudiu Manoil 		dev_kfree_skb(rx_queue->skb);
1988ec21e2ecSJeff Kirsher 
1989ec21e2ecSJeff Kirsher 	for (i = 0; i < rx_queue->rx_ring_size; i++) {
199075354148SClaudiu Manoil 		struct	gfar_rx_buff *rxb = &rx_queue->rx_buff[i];
199175354148SClaudiu Manoil 
1992ec21e2ecSJeff Kirsher 		rxbdp->lstatus = 0;
1993ec21e2ecSJeff Kirsher 		rxbdp->bufPtr = 0;
1994ec21e2ecSJeff Kirsher 		rxbdp++;
199575354148SClaudiu Manoil 
199675354148SClaudiu Manoil 		if (!rxb->page)
199775354148SClaudiu Manoil 			continue;
199875354148SClaudiu Manoil 
199975354148SClaudiu Manoil 		dma_unmap_single(rx_queue->dev, rxb->dma,
200075354148SClaudiu Manoil 				 PAGE_SIZE, DMA_FROM_DEVICE);
200175354148SClaudiu Manoil 		__free_page(rxb->page);
200275354148SClaudiu Manoil 
200375354148SClaudiu Manoil 		rxb->page = NULL;
2004ec21e2ecSJeff Kirsher 	}
200575354148SClaudiu Manoil 
200675354148SClaudiu Manoil 	kfree(rx_queue->rx_buff);
200775354148SClaudiu Manoil 	rx_queue->rx_buff = NULL;
2008ec21e2ecSJeff Kirsher }
2009ec21e2ecSJeff Kirsher 
2010ec21e2ecSJeff Kirsher /* If there are any tx skbs or rx skbs still around, free them.
20110977f817SJan Ceuleers  * Then free tx_skbuff and rx_skbuff
20120977f817SJan Ceuleers  */
2013ec21e2ecSJeff Kirsher static void free_skb_resources(struct gfar_private *priv)
2014ec21e2ecSJeff Kirsher {
2015ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
2016ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
2017ec21e2ecSJeff Kirsher 	int i;
2018ec21e2ecSJeff Kirsher 
2019ec21e2ecSJeff Kirsher 	/* Go through all the buffer descriptors and free their data buffers */
2020ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_tx_queues; i++) {
2021d8a0f1b0SPaul Gortmaker 		struct netdev_queue *txq;
2022bc4598bcSJan Ceuleers 
2023ec21e2ecSJeff Kirsher 		tx_queue = priv->tx_queue[i];
2024d8a0f1b0SPaul Gortmaker 		txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
2025ec21e2ecSJeff Kirsher 		if (tx_queue->tx_skbuff)
2026ec21e2ecSJeff Kirsher 			free_skb_tx_queue(tx_queue);
2027d8a0f1b0SPaul Gortmaker 		netdev_tx_reset_queue(txq);
2028ec21e2ecSJeff Kirsher 	}
2029ec21e2ecSJeff Kirsher 
2030ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_rx_queues; i++) {
2031ec21e2ecSJeff Kirsher 		rx_queue = priv->rx_queue[i];
203275354148SClaudiu Manoil 		if (rx_queue->rx_buff)
2033ec21e2ecSJeff Kirsher 			free_skb_rx_queue(rx_queue);
2034ec21e2ecSJeff Kirsher 	}
2035ec21e2ecSJeff Kirsher 
2036369ec162SClaudiu Manoil 	dma_free_coherent(priv->dev,
2037ec21e2ecSJeff Kirsher 			  sizeof(struct txbd8) * priv->total_tx_ring_size +
2038ec21e2ecSJeff Kirsher 			  sizeof(struct rxbd8) * priv->total_rx_ring_size,
2039ec21e2ecSJeff Kirsher 			  priv->tx_queue[0]->tx_bd_base,
2040ec21e2ecSJeff Kirsher 			  priv->tx_queue[0]->tx_bd_dma_base);
2041ec21e2ecSJeff Kirsher }
2042ec21e2ecSJeff Kirsher 
2043c10650b6SClaudiu Manoil void gfar_start(struct gfar_private *priv)
2044ec21e2ecSJeff Kirsher {
2045ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
2046ec21e2ecSJeff Kirsher 	u32 tempval;
2047ec21e2ecSJeff Kirsher 	int i = 0;
2048ec21e2ecSJeff Kirsher 
2049c10650b6SClaudiu Manoil 	/* Enable Rx/Tx hw queues */
2050c10650b6SClaudiu Manoil 	gfar_write(&regs->rqueue, priv->rqueue);
2051c10650b6SClaudiu Manoil 	gfar_write(&regs->tqueue, priv->tqueue);
2052ec21e2ecSJeff Kirsher 
2053ec21e2ecSJeff Kirsher 	/* Initialize DMACTRL to have WWR and WOP */
2054ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->dmactrl);
2055ec21e2ecSJeff Kirsher 	tempval |= DMACTRL_INIT_SETTINGS;
2056ec21e2ecSJeff Kirsher 	gfar_write(&regs->dmactrl, tempval);
2057ec21e2ecSJeff Kirsher 
2058ec21e2ecSJeff Kirsher 	/* Make sure we aren't stopped */
2059ec21e2ecSJeff Kirsher 	tempval = gfar_read(&regs->dmactrl);
2060ec21e2ecSJeff Kirsher 	tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
2061ec21e2ecSJeff Kirsher 	gfar_write(&regs->dmactrl, tempval);
2062ec21e2ecSJeff Kirsher 
2063ec21e2ecSJeff Kirsher 	for (i = 0; i < priv->num_grps; i++) {
2064ec21e2ecSJeff Kirsher 		regs = priv->gfargrp[i].regs;
2065ec21e2ecSJeff Kirsher 		/* Clear THLT/RHLT, so that the DMA starts polling now */
2066ec21e2ecSJeff Kirsher 		gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
2067ec21e2ecSJeff Kirsher 		gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
2068ec21e2ecSJeff Kirsher 	}
2069ec21e2ecSJeff Kirsher 
2070c10650b6SClaudiu Manoil 	/* Enable Rx/Tx DMA */
2071c10650b6SClaudiu Manoil 	tempval = gfar_read(&regs->maccfg1);
2072c10650b6SClaudiu Manoil 	tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
2073c10650b6SClaudiu Manoil 	gfar_write(&regs->maccfg1, tempval);
2074c10650b6SClaudiu Manoil 
2075efeddce7SClaudiu Manoil 	gfar_ints_enable(priv);
2076efeddce7SClaudiu Manoil 
2077c10650b6SClaudiu Manoil 	priv->ndev->trans_start = jiffies; /* prevent tx timeout */
2078ec21e2ecSJeff Kirsher }
2079ec21e2ecSJeff Kirsher 
208080ec396cSClaudiu Manoil static void free_grp_irqs(struct gfar_priv_grp *grp)
208180ec396cSClaudiu Manoil {
208280ec396cSClaudiu Manoil 	free_irq(gfar_irq(grp, TX)->irq, grp);
208380ec396cSClaudiu Manoil 	free_irq(gfar_irq(grp, RX)->irq, grp);
208480ec396cSClaudiu Manoil 	free_irq(gfar_irq(grp, ER)->irq, grp);
208580ec396cSClaudiu Manoil }
208680ec396cSClaudiu Manoil 
2087ec21e2ecSJeff Kirsher static int register_grp_irqs(struct gfar_priv_grp *grp)
2088ec21e2ecSJeff Kirsher {
2089ec21e2ecSJeff Kirsher 	struct gfar_private *priv = grp->priv;
2090ec21e2ecSJeff Kirsher 	struct net_device *dev = priv->ndev;
2091ec21e2ecSJeff Kirsher 	int err;
2092ec21e2ecSJeff Kirsher 
2093ec21e2ecSJeff Kirsher 	/* If the device has multiple interrupts, register for
20940977f817SJan Ceuleers 	 * them.  Otherwise, only register for the one
20950977f817SJan Ceuleers 	 */
2096ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2097ec21e2ecSJeff Kirsher 		/* Install our interrupt handlers for Error,
20980977f817SJan Ceuleers 		 * Transmit, and Receive
20990977f817SJan Ceuleers 		 */
2100d5b8d640SSudeep Holla 		err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
2101ee873fdaSClaudiu Manoil 				  gfar_irq(grp, ER)->name, grp);
2102ee873fdaSClaudiu Manoil 		if (err < 0) {
2103ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
2104ee873fdaSClaudiu Manoil 				  gfar_irq(grp, ER)->irq);
2105ec21e2ecSJeff Kirsher 
2106ec21e2ecSJeff Kirsher 			goto err_irq_fail;
2107ec21e2ecSJeff Kirsher 		}
2108d5b8d640SSudeep Holla 		enable_irq_wake(gfar_irq(grp, ER)->irq);
2109d5b8d640SSudeep Holla 
2110ee873fdaSClaudiu Manoil 		err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
2111ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->name, grp);
2112ee873fdaSClaudiu Manoil 		if (err < 0) {
2113ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
2114ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->irq);
2115ec21e2ecSJeff Kirsher 			goto tx_irq_fail;
2116ec21e2ecSJeff Kirsher 		}
2117ee873fdaSClaudiu Manoil 		err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
2118ee873fdaSClaudiu Manoil 				  gfar_irq(grp, RX)->name, grp);
2119ee873fdaSClaudiu Manoil 		if (err < 0) {
2120ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
2121ee873fdaSClaudiu Manoil 				  gfar_irq(grp, RX)->irq);
2122ec21e2ecSJeff Kirsher 			goto rx_irq_fail;
2123ec21e2ecSJeff Kirsher 		}
21243e905b80SClaudiu Manoil 		enable_irq_wake(gfar_irq(grp, RX)->irq);
21253e905b80SClaudiu Manoil 
2126ec21e2ecSJeff Kirsher 	} else {
2127d5b8d640SSudeep Holla 		err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
2128ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->name, grp);
2129ee873fdaSClaudiu Manoil 		if (err < 0) {
2130ec21e2ecSJeff Kirsher 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
2131ee873fdaSClaudiu Manoil 				  gfar_irq(grp, TX)->irq);
2132ec21e2ecSJeff Kirsher 			goto err_irq_fail;
2133ec21e2ecSJeff Kirsher 		}
2134d5b8d640SSudeep Holla 		enable_irq_wake(gfar_irq(grp, TX)->irq);
2135ec21e2ecSJeff Kirsher 	}
2136ec21e2ecSJeff Kirsher 
2137ec21e2ecSJeff Kirsher 	return 0;
2138ec21e2ecSJeff Kirsher 
2139ec21e2ecSJeff Kirsher rx_irq_fail:
2140ee873fdaSClaudiu Manoil 	free_irq(gfar_irq(grp, TX)->irq, grp);
2141ec21e2ecSJeff Kirsher tx_irq_fail:
2142ee873fdaSClaudiu Manoil 	free_irq(gfar_irq(grp, ER)->irq, grp);
2143ec21e2ecSJeff Kirsher err_irq_fail:
2144ec21e2ecSJeff Kirsher 	return err;
2145ec21e2ecSJeff Kirsher 
2146ec21e2ecSJeff Kirsher }
2147ec21e2ecSJeff Kirsher 
214880ec396cSClaudiu Manoil static void gfar_free_irq(struct gfar_private *priv)
214980ec396cSClaudiu Manoil {
215080ec396cSClaudiu Manoil 	int i;
215180ec396cSClaudiu Manoil 
215280ec396cSClaudiu Manoil 	/* Free the IRQs */
215380ec396cSClaudiu Manoil 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
215480ec396cSClaudiu Manoil 		for (i = 0; i < priv->num_grps; i++)
215580ec396cSClaudiu Manoil 			free_grp_irqs(&priv->gfargrp[i]);
215680ec396cSClaudiu Manoil 	} else {
215780ec396cSClaudiu Manoil 		for (i = 0; i < priv->num_grps; i++)
215880ec396cSClaudiu Manoil 			free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
215980ec396cSClaudiu Manoil 				 &priv->gfargrp[i]);
216080ec396cSClaudiu Manoil 	}
216180ec396cSClaudiu Manoil }
216280ec396cSClaudiu Manoil 
216380ec396cSClaudiu Manoil static int gfar_request_irq(struct gfar_private *priv)
216480ec396cSClaudiu Manoil {
216580ec396cSClaudiu Manoil 	int err, i, j;
216680ec396cSClaudiu Manoil 
216780ec396cSClaudiu Manoil 	for (i = 0; i < priv->num_grps; i++) {
216880ec396cSClaudiu Manoil 		err = register_grp_irqs(&priv->gfargrp[i]);
216980ec396cSClaudiu Manoil 		if (err) {
217080ec396cSClaudiu Manoil 			for (j = 0; j < i; j++)
217180ec396cSClaudiu Manoil 				free_grp_irqs(&priv->gfargrp[j]);
217280ec396cSClaudiu Manoil 			return err;
217380ec396cSClaudiu Manoil 		}
217480ec396cSClaudiu Manoil 	}
217580ec396cSClaudiu Manoil 
217680ec396cSClaudiu Manoil 	return 0;
217780ec396cSClaudiu Manoil }
217880ec396cSClaudiu Manoil 
2179ec21e2ecSJeff Kirsher /* Bring the controller up and running */
2180ec21e2ecSJeff Kirsher int startup_gfar(struct net_device *ndev)
2181ec21e2ecSJeff Kirsher {
2182ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(ndev);
218380ec396cSClaudiu Manoil 	int err;
2184ec21e2ecSJeff Kirsher 
2185a328ac92SClaudiu Manoil 	gfar_mac_reset(priv);
2186ec21e2ecSJeff Kirsher 
2187ec21e2ecSJeff Kirsher 	err = gfar_alloc_skb_resources(ndev);
2188ec21e2ecSJeff Kirsher 	if (err)
2189ec21e2ecSJeff Kirsher 		return err;
2190ec21e2ecSJeff Kirsher 
2191a328ac92SClaudiu Manoil 	gfar_init_tx_rx_base(priv);
2192ec21e2ecSJeff Kirsher 
21934e857c58SPeter Zijlstra 	smp_mb__before_atomic();
21940851133bSClaudiu Manoil 	clear_bit(GFAR_DOWN, &priv->state);
21954e857c58SPeter Zijlstra 	smp_mb__after_atomic();
21960851133bSClaudiu Manoil 
21970851133bSClaudiu Manoil 	/* Start Rx/Tx DMA and enable the interrupts */
2198c10650b6SClaudiu Manoil 	gfar_start(priv);
2199ec21e2ecSJeff Kirsher 
22002a4eebf0SClaudiu Manoil 	/* force link state update after mac reset */
22012a4eebf0SClaudiu Manoil 	priv->oldlink = 0;
22022a4eebf0SClaudiu Manoil 	priv->oldspeed = 0;
22032a4eebf0SClaudiu Manoil 	priv->oldduplex = -1;
22042a4eebf0SClaudiu Manoil 
2205ec21e2ecSJeff Kirsher 	phy_start(priv->phydev);
2206ec21e2ecSJeff Kirsher 
22070851133bSClaudiu Manoil 	enable_napi(priv);
22080851133bSClaudiu Manoil 
22090851133bSClaudiu Manoil 	netif_tx_wake_all_queues(ndev);
22100851133bSClaudiu Manoil 
2211ec21e2ecSJeff Kirsher 	return 0;
2212ec21e2ecSJeff Kirsher }
2213ec21e2ecSJeff Kirsher 
22140977f817SJan Ceuleers /* Called when something needs to use the ethernet device
22150977f817SJan Ceuleers  * Returns 0 for success.
22160977f817SJan Ceuleers  */
2217ec21e2ecSJeff Kirsher static int gfar_enet_open(struct net_device *dev)
2218ec21e2ecSJeff Kirsher {
2219ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2220ec21e2ecSJeff Kirsher 	int err;
2221ec21e2ecSJeff Kirsher 
2222ec21e2ecSJeff Kirsher 	err = init_phy(dev);
22230851133bSClaudiu Manoil 	if (err)
2224ec21e2ecSJeff Kirsher 		return err;
2225ec21e2ecSJeff Kirsher 
222680ec396cSClaudiu Manoil 	err = gfar_request_irq(priv);
222780ec396cSClaudiu Manoil 	if (err)
222880ec396cSClaudiu Manoil 		return err;
222980ec396cSClaudiu Manoil 
2230ec21e2ecSJeff Kirsher 	err = startup_gfar(dev);
22310851133bSClaudiu Manoil 	if (err)
2232ec21e2ecSJeff Kirsher 		return err;
2233ec21e2ecSJeff Kirsher 
2234ec21e2ecSJeff Kirsher 	return err;
2235ec21e2ecSJeff Kirsher }
2236ec21e2ecSJeff Kirsher 
2237ec21e2ecSJeff Kirsher static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
2238ec21e2ecSJeff Kirsher {
2239ec21e2ecSJeff Kirsher 	struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
2240ec21e2ecSJeff Kirsher 
2241ec21e2ecSJeff Kirsher 	memset(fcb, 0, GMAC_FCB_LEN);
2242ec21e2ecSJeff Kirsher 
2243ec21e2ecSJeff Kirsher 	return fcb;
2244ec21e2ecSJeff Kirsher }
2245ec21e2ecSJeff Kirsher 
22469c4886e5SManfred Rudigier static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
22479c4886e5SManfred Rudigier 				    int fcb_length)
2248ec21e2ecSJeff Kirsher {
2249ec21e2ecSJeff Kirsher 	/* If we're here, it's a IP packet with a TCP or UDP
2250ec21e2ecSJeff Kirsher 	 * payload.  We set it to checksum, using a pseudo-header
2251ec21e2ecSJeff Kirsher 	 * we provide
2252ec21e2ecSJeff Kirsher 	 */
22533a2e16c8SJan Ceuleers 	u8 flags = TXFCB_DEFAULT;
2254ec21e2ecSJeff Kirsher 
22550977f817SJan Ceuleers 	/* Tell the controller what the protocol is
22560977f817SJan Ceuleers 	 * And provide the already calculated phcs
22570977f817SJan Ceuleers 	 */
2258ec21e2ecSJeff Kirsher 	if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
2259ec21e2ecSJeff Kirsher 		flags |= TXFCB_UDP;
226026eb9374SClaudiu Manoil 		fcb->phcs = (__force __be16)(udp_hdr(skb)->check);
2261ec21e2ecSJeff Kirsher 	} else
226226eb9374SClaudiu Manoil 		fcb->phcs = (__force __be16)(tcp_hdr(skb)->check);
2263ec21e2ecSJeff Kirsher 
2264ec21e2ecSJeff Kirsher 	/* l3os is the distance between the start of the
2265ec21e2ecSJeff Kirsher 	 * frame (skb->data) and the start of the IP hdr.
2266ec21e2ecSJeff Kirsher 	 * l4os is the distance between the start of the
22670977f817SJan Ceuleers 	 * l3 hdr and the l4 hdr
22680977f817SJan Ceuleers 	 */
226926eb9374SClaudiu Manoil 	fcb->l3os = (u8)(skb_network_offset(skb) - fcb_length);
2270ec21e2ecSJeff Kirsher 	fcb->l4os = skb_network_header_len(skb);
2271ec21e2ecSJeff Kirsher 
2272ec21e2ecSJeff Kirsher 	fcb->flags = flags;
2273ec21e2ecSJeff Kirsher }
2274ec21e2ecSJeff Kirsher 
2275ec21e2ecSJeff Kirsher void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
2276ec21e2ecSJeff Kirsher {
2277ec21e2ecSJeff Kirsher 	fcb->flags |= TXFCB_VLN;
227826eb9374SClaudiu Manoil 	fcb->vlctl = cpu_to_be16(skb_vlan_tag_get(skb));
2279ec21e2ecSJeff Kirsher }
2280ec21e2ecSJeff Kirsher 
2281ec21e2ecSJeff Kirsher static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
2282ec21e2ecSJeff Kirsher 				      struct txbd8 *base, int ring_size)
2283ec21e2ecSJeff Kirsher {
2284ec21e2ecSJeff Kirsher 	struct txbd8 *new_bd = bdp + stride;
2285ec21e2ecSJeff Kirsher 
2286ec21e2ecSJeff Kirsher 	return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2287ec21e2ecSJeff Kirsher }
2288ec21e2ecSJeff Kirsher 
2289ec21e2ecSJeff Kirsher static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
2290ec21e2ecSJeff Kirsher 				      int ring_size)
2291ec21e2ecSJeff Kirsher {
2292ec21e2ecSJeff Kirsher 	return skip_txbd(bdp, 1, base, ring_size);
2293ec21e2ecSJeff Kirsher }
2294ec21e2ecSJeff Kirsher 
229502d88fb4SClaudiu Manoil /* eTSEC12: csum generation not supported for some fcb offsets */
229602d88fb4SClaudiu Manoil static inline bool gfar_csum_errata_12(struct gfar_private *priv,
229702d88fb4SClaudiu Manoil 				       unsigned long fcb_addr)
229802d88fb4SClaudiu Manoil {
229902d88fb4SClaudiu Manoil 	return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
230002d88fb4SClaudiu Manoil 	       (fcb_addr % 0x20) > 0x18);
230102d88fb4SClaudiu Manoil }
230202d88fb4SClaudiu Manoil 
230302d88fb4SClaudiu Manoil /* eTSEC76: csum generation for frames larger than 2500 may
230402d88fb4SClaudiu Manoil  * cause excess delays before start of transmission
230502d88fb4SClaudiu Manoil  */
230602d88fb4SClaudiu Manoil static inline bool gfar_csum_errata_76(struct gfar_private *priv,
230702d88fb4SClaudiu Manoil 				       unsigned int len)
230802d88fb4SClaudiu Manoil {
230902d88fb4SClaudiu Manoil 	return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
231002d88fb4SClaudiu Manoil 	       (len > 2500));
231102d88fb4SClaudiu Manoil }
231202d88fb4SClaudiu Manoil 
23130977f817SJan Ceuleers /* This is called by the kernel when a frame is ready for transmission.
23140977f817SJan Ceuleers  * It is pointed to by the dev->hard_start_xmit function pointer
23150977f817SJan Ceuleers  */
2316ec21e2ecSJeff Kirsher static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2317ec21e2ecSJeff Kirsher {
2318ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2319ec21e2ecSJeff Kirsher 	struct gfar_priv_tx_q *tx_queue = NULL;
2320ec21e2ecSJeff Kirsher 	struct netdev_queue *txq;
2321ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = NULL;
2322ec21e2ecSJeff Kirsher 	struct txfcb *fcb = NULL;
2323ec21e2ecSJeff Kirsher 	struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
2324ec21e2ecSJeff Kirsher 	u32 lstatus;
2325*42f397adSClaudiu Manoil 	skb_frag_t *frag;
23260d0cffdcSClaudiu Manoil 	int i, rq = 0;
23270d0cffdcSClaudiu Manoil 	int do_tstamp, do_csum, do_vlan;
2328ec21e2ecSJeff Kirsher 	u32 bufaddr;
232950ad076bSClaudiu Manoil 	unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
2330ec21e2ecSJeff Kirsher 
2331ec21e2ecSJeff Kirsher 	rq = skb->queue_mapping;
2332ec21e2ecSJeff Kirsher 	tx_queue = priv->tx_queue[rq];
2333ec21e2ecSJeff Kirsher 	txq = netdev_get_tx_queue(dev, rq);
2334ec21e2ecSJeff Kirsher 	base = tx_queue->tx_bd_base;
2335ec21e2ecSJeff Kirsher 	regs = tx_queue->grp->regs;
2336ec21e2ecSJeff Kirsher 
23370d0cffdcSClaudiu Manoil 	do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
2338df8a39deSJiri Pirko 	do_vlan = skb_vlan_tag_present(skb);
23390d0cffdcSClaudiu Manoil 	do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
23400d0cffdcSClaudiu Manoil 		    priv->hwts_tx_en;
23410d0cffdcSClaudiu Manoil 
23420d0cffdcSClaudiu Manoil 	if (do_csum || do_vlan)
23430d0cffdcSClaudiu Manoil 		fcb_len = GMAC_FCB_LEN;
23440d0cffdcSClaudiu Manoil 
2345ec21e2ecSJeff Kirsher 	/* check if time stamp should be generated */
23460d0cffdcSClaudiu Manoil 	if (unlikely(do_tstamp))
23470d0cffdcSClaudiu Manoil 		fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2348ec21e2ecSJeff Kirsher 
2349ec21e2ecSJeff Kirsher 	/* make space for additional header when fcb is needed */
23500d0cffdcSClaudiu Manoil 	if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
2351ec21e2ecSJeff Kirsher 		struct sk_buff *skb_new;
2352ec21e2ecSJeff Kirsher 
23530d0cffdcSClaudiu Manoil 		skb_new = skb_realloc_headroom(skb, fcb_len);
2354ec21e2ecSJeff Kirsher 		if (!skb_new) {
2355ec21e2ecSJeff Kirsher 			dev->stats.tx_errors++;
2356c9974ad4SEric W. Biederman 			dev_kfree_skb_any(skb);
2357ec21e2ecSJeff Kirsher 			return NETDEV_TX_OK;
2358ec21e2ecSJeff Kirsher 		}
2359db83d136SManfred Rudigier 
2360313b037cSEric Dumazet 		if (skb->sk)
2361313b037cSEric Dumazet 			skb_set_owner_w(skb_new, skb->sk);
2362c9974ad4SEric W. Biederman 		dev_consume_skb_any(skb);
2363ec21e2ecSJeff Kirsher 		skb = skb_new;
2364ec21e2ecSJeff Kirsher 	}
2365ec21e2ecSJeff Kirsher 
2366ec21e2ecSJeff Kirsher 	/* total number of fragments in the SKB */
2367ec21e2ecSJeff Kirsher 	nr_frags = skb_shinfo(skb)->nr_frags;
2368ec21e2ecSJeff Kirsher 
2369ec21e2ecSJeff Kirsher 	/* calculate the required number of TxBDs for this skb */
2370ec21e2ecSJeff Kirsher 	if (unlikely(do_tstamp))
2371ec21e2ecSJeff Kirsher 		nr_txbds = nr_frags + 2;
2372ec21e2ecSJeff Kirsher 	else
2373ec21e2ecSJeff Kirsher 		nr_txbds = nr_frags + 1;
2374ec21e2ecSJeff Kirsher 
2375ec21e2ecSJeff Kirsher 	/* check if there is space to queue this packet */
2376ec21e2ecSJeff Kirsher 	if (nr_txbds > tx_queue->num_txbdfree) {
2377ec21e2ecSJeff Kirsher 		/* no space, stop the queue */
2378ec21e2ecSJeff Kirsher 		netif_tx_stop_queue(txq);
2379ec21e2ecSJeff Kirsher 		dev->stats.tx_fifo_errors++;
2380ec21e2ecSJeff Kirsher 		return NETDEV_TX_BUSY;
2381ec21e2ecSJeff Kirsher 	}
2382ec21e2ecSJeff Kirsher 
2383ec21e2ecSJeff Kirsher 	/* Update transmit stats */
238450ad076bSClaudiu Manoil 	bytes_sent = skb->len;
238550ad076bSClaudiu Manoil 	tx_queue->stats.tx_bytes += bytes_sent;
238650ad076bSClaudiu Manoil 	/* keep Tx bytes on wire for BQL accounting */
238750ad076bSClaudiu Manoil 	GFAR_CB(skb)->bytes_sent = bytes_sent;
2388ec21e2ecSJeff Kirsher 	tx_queue->stats.tx_packets++;
2389ec21e2ecSJeff Kirsher 
2390ec21e2ecSJeff Kirsher 	txbdp = txbdp_start = tx_queue->cur_tx;
2391a7312d58SClaudiu Manoil 	lstatus = be32_to_cpu(txbdp->lstatus);
2392ec21e2ecSJeff Kirsher 
23939c4886e5SManfred Rudigier 	/* Add TxPAL between FCB and frame if required */
23949c4886e5SManfred Rudigier 	if (unlikely(do_tstamp)) {
23959c4886e5SManfred Rudigier 		skb_push(skb, GMAC_TXPAL_LEN);
23969c4886e5SManfred Rudigier 		memset(skb->data, 0, GMAC_TXPAL_LEN);
23979c4886e5SManfred Rudigier 	}
23989c4886e5SManfred Rudigier 
23990d0cffdcSClaudiu Manoil 	/* Add TxFCB if required */
24000d0cffdcSClaudiu Manoil 	if (fcb_len) {
2401ec21e2ecSJeff Kirsher 		fcb = gfar_add_fcb(skb);
2402ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(TXBD_TOE);
24030d0cffdcSClaudiu Manoil 	}
24040d0cffdcSClaudiu Manoil 
24050d0cffdcSClaudiu Manoil 	/* Set up checksumming */
24060d0cffdcSClaudiu Manoil 	if (do_csum) {
24070d0cffdcSClaudiu Manoil 		gfar_tx_checksum(skb, fcb, fcb_len);
240802d88fb4SClaudiu Manoil 
240902d88fb4SClaudiu Manoil 		if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
241002d88fb4SClaudiu Manoil 		    unlikely(gfar_csum_errata_76(priv, skb->len))) {
241102d88fb4SClaudiu Manoil 			__skb_pull(skb, GMAC_FCB_LEN);
241202d88fb4SClaudiu Manoil 			skb_checksum_help(skb);
24130d0cffdcSClaudiu Manoil 			if (do_vlan || do_tstamp) {
24140d0cffdcSClaudiu Manoil 				/* put back a new fcb for vlan/tstamp TOE */
24150d0cffdcSClaudiu Manoil 				fcb = gfar_add_fcb(skb);
24160d0cffdcSClaudiu Manoil 			} else {
24170d0cffdcSClaudiu Manoil 				/* Tx TOE not used */
241802d88fb4SClaudiu Manoil 				lstatus &= ~(BD_LFLAG(TXBD_TOE));
241902d88fb4SClaudiu Manoil 				fcb = NULL;
2420ec21e2ecSJeff Kirsher 			}
2421ec21e2ecSJeff Kirsher 		}
2422ec21e2ecSJeff Kirsher 	}
2423ec21e2ecSJeff Kirsher 
24240d0cffdcSClaudiu Manoil 	if (do_vlan)
2425ec21e2ecSJeff Kirsher 		gfar_tx_vlan(skb, fcb);
2426ec21e2ecSJeff Kirsher 
24270a4b5a24SKevin Hao 	bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb),
24280a4b5a24SKevin Hao 				 DMA_TO_DEVICE);
24290a4b5a24SKevin Hao 	if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
24300a4b5a24SKevin Hao 		goto dma_map_err;
24310a4b5a24SKevin Hao 
2432a7312d58SClaudiu Manoil 	txbdp_start->bufPtr = cpu_to_be32(bufaddr);
2433ec21e2ecSJeff Kirsher 
2434e19d0839SClaudiu Manoil 	/* Time stamp insertion requires one additional TxBD */
2435e19d0839SClaudiu Manoil 	if (unlikely(do_tstamp))
2436e19d0839SClaudiu Manoil 		txbdp_tstamp = txbdp = next_txbd(txbdp, base,
2437e19d0839SClaudiu Manoil 						 tx_queue->tx_ring_size);
2438e19d0839SClaudiu Manoil 
2439e19d0839SClaudiu Manoil 	if (nr_frags == 0) {
2440e19d0839SClaudiu Manoil 		if (unlikely(do_tstamp)) {
2441e19d0839SClaudiu Manoil 			u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus);
2442e19d0839SClaudiu Manoil 
2443e19d0839SClaudiu Manoil 			lstatus_ts |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2444e19d0839SClaudiu Manoil 			txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts);
2445e19d0839SClaudiu Manoil 		} else {
2446e19d0839SClaudiu Manoil 			lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2447e19d0839SClaudiu Manoil 		}
2448e19d0839SClaudiu Manoil 	} else {
2449e19d0839SClaudiu Manoil 		u32 lstatus_start = lstatus;
2450e19d0839SClaudiu Manoil 
2451e19d0839SClaudiu Manoil 		/* Place the fragment addresses and lengths into the TxBDs */
2452*42f397adSClaudiu Manoil 		frag = &skb_shinfo(skb)->frags[0];
2453*42f397adSClaudiu Manoil 		for (i = 0; i < nr_frags; i++, frag++) {
2454*42f397adSClaudiu Manoil 			unsigned int size;
2455*42f397adSClaudiu Manoil 
2456e19d0839SClaudiu Manoil 			/* Point at the next BD, wrapping as needed */
2457e19d0839SClaudiu Manoil 			txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2458e19d0839SClaudiu Manoil 
2459*42f397adSClaudiu Manoil 			size = skb_frag_size(frag);
2460e19d0839SClaudiu Manoil 
2461*42f397adSClaudiu Manoil 			lstatus = be32_to_cpu(txbdp->lstatus) | size |
2462e19d0839SClaudiu Manoil 				  BD_LFLAG(TXBD_READY);
2463e19d0839SClaudiu Manoil 
2464e19d0839SClaudiu Manoil 			/* Handle the last BD specially */
2465e19d0839SClaudiu Manoil 			if (i == nr_frags - 1)
2466e19d0839SClaudiu Manoil 				lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2467e19d0839SClaudiu Manoil 
2468*42f397adSClaudiu Manoil 			bufaddr = skb_frag_dma_map(priv->dev, frag, 0,
2469*42f397adSClaudiu Manoil 						   size, DMA_TO_DEVICE);
2470e19d0839SClaudiu Manoil 			if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
2471e19d0839SClaudiu Manoil 				goto dma_map_err;
2472e19d0839SClaudiu Manoil 
2473e19d0839SClaudiu Manoil 			/* set the TxBD length and buffer pointer */
2474e19d0839SClaudiu Manoil 			txbdp->bufPtr = cpu_to_be32(bufaddr);
2475e19d0839SClaudiu Manoil 			txbdp->lstatus = cpu_to_be32(lstatus);
2476e19d0839SClaudiu Manoil 		}
2477e19d0839SClaudiu Manoil 
2478e19d0839SClaudiu Manoil 		lstatus = lstatus_start;
2479e19d0839SClaudiu Manoil 	}
2480e19d0839SClaudiu Manoil 
24810977f817SJan Ceuleers 	/* If time stamping is requested one additional TxBD must be set up. The
2482ec21e2ecSJeff Kirsher 	 * first TxBD points to the FCB and must have a data length of
2483ec21e2ecSJeff Kirsher 	 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2484ec21e2ecSJeff Kirsher 	 * the full frame length.
2485ec21e2ecSJeff Kirsher 	 */
2486ec21e2ecSJeff Kirsher 	if (unlikely(do_tstamp)) {
2487a7312d58SClaudiu Manoil 		u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus);
2488a7312d58SClaudiu Manoil 
2489a7312d58SClaudiu Manoil 		bufaddr = be32_to_cpu(txbdp_start->bufPtr);
2490a7312d58SClaudiu Manoil 		bufaddr += fcb_len;
2491a7312d58SClaudiu Manoil 		lstatus_ts |= BD_LFLAG(TXBD_READY) |
24920d0cffdcSClaudiu Manoil 			      (skb_headlen(skb) - fcb_len);
2493a7312d58SClaudiu Manoil 
2494a7312d58SClaudiu Manoil 		txbdp_tstamp->bufPtr = cpu_to_be32(bufaddr);
2495a7312d58SClaudiu Manoil 		txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts);
2496ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2497e19d0839SClaudiu Manoil 
2498e19d0839SClaudiu Manoil 		/* Setup tx hardware time stamping */
2499e19d0839SClaudiu Manoil 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2500e19d0839SClaudiu Manoil 		fcb->ptp = 1;
2501ec21e2ecSJeff Kirsher 	} else {
2502ec21e2ecSJeff Kirsher 		lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2503ec21e2ecSJeff Kirsher 	}
2504ec21e2ecSJeff Kirsher 
250550ad076bSClaudiu Manoil 	netdev_tx_sent_queue(txq, bytes_sent);
2506d8a0f1b0SPaul Gortmaker 
2507d55398baSClaudiu Manoil 	gfar_wmb();
2508ec21e2ecSJeff Kirsher 
2509a7312d58SClaudiu Manoil 	txbdp_start->lstatus = cpu_to_be32(lstatus);
2510ec21e2ecSJeff Kirsher 
2511d55398baSClaudiu Manoil 	gfar_wmb(); /* force lstatus write before tx_skbuff */
2512ec21e2ecSJeff Kirsher 
2513ec21e2ecSJeff Kirsher 	tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2514ec21e2ecSJeff Kirsher 
2515ec21e2ecSJeff Kirsher 	/* Update the current skb pointer to the next entry we will use
25160977f817SJan Ceuleers 	 * (wrapping if necessary)
25170977f817SJan Ceuleers 	 */
2518ec21e2ecSJeff Kirsher 	tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
2519ec21e2ecSJeff Kirsher 			      TX_RING_MOD_MASK(tx_queue->tx_ring_size);
2520ec21e2ecSJeff Kirsher 
2521ec21e2ecSJeff Kirsher 	tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2522ec21e2ecSJeff Kirsher 
2523bc602280SClaudiu Manoil 	/* We can work in parallel with gfar_clean_tx_ring(), except
2524bc602280SClaudiu Manoil 	 * when modifying num_txbdfree. Note that we didn't grab the lock
2525bc602280SClaudiu Manoil 	 * when we were reading the num_txbdfree and checking for available
2526bc602280SClaudiu Manoil 	 * space, that's because outside of this function it can only grow.
2527bc602280SClaudiu Manoil 	 */
2528bc602280SClaudiu Manoil 	spin_lock_bh(&tx_queue->txlock);
2529ec21e2ecSJeff Kirsher 	/* reduce TxBD free count */
2530ec21e2ecSJeff Kirsher 	tx_queue->num_txbdfree -= (nr_txbds);
2531bc602280SClaudiu Manoil 	spin_unlock_bh(&tx_queue->txlock);
2532ec21e2ecSJeff Kirsher 
2533ec21e2ecSJeff Kirsher 	/* If the next BD still needs to be cleaned up, then the bds
25340977f817SJan Ceuleers 	 * are full.  We need to tell the kernel to stop sending us stuff.
25350977f817SJan Ceuleers 	 */
2536ec21e2ecSJeff Kirsher 	if (!tx_queue->num_txbdfree) {
2537ec21e2ecSJeff Kirsher 		netif_tx_stop_queue(txq);
2538ec21e2ecSJeff Kirsher 
2539ec21e2ecSJeff Kirsher 		dev->stats.tx_fifo_errors++;
2540ec21e2ecSJeff Kirsher 	}
2541ec21e2ecSJeff Kirsher 
2542ec21e2ecSJeff Kirsher 	/* Tell the DMA to go go go */
2543ec21e2ecSJeff Kirsher 	gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
2544ec21e2ecSJeff Kirsher 
2545ec21e2ecSJeff Kirsher 	return NETDEV_TX_OK;
25460a4b5a24SKevin Hao 
25470a4b5a24SKevin Hao dma_map_err:
25480a4b5a24SKevin Hao 	txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size);
25490a4b5a24SKevin Hao 	if (do_tstamp)
25500a4b5a24SKevin Hao 		txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
25510a4b5a24SKevin Hao 	for (i = 0; i < nr_frags; i++) {
2552a7312d58SClaudiu Manoil 		lstatus = be32_to_cpu(txbdp->lstatus);
25530a4b5a24SKevin Hao 		if (!(lstatus & BD_LFLAG(TXBD_READY)))
25540a4b5a24SKevin Hao 			break;
25550a4b5a24SKevin Hao 
2556a7312d58SClaudiu Manoil 		lstatus &= ~BD_LFLAG(TXBD_READY);
2557a7312d58SClaudiu Manoil 		txbdp->lstatus = cpu_to_be32(lstatus);
2558a7312d58SClaudiu Manoil 		bufaddr = be32_to_cpu(txbdp->bufPtr);
2559a7312d58SClaudiu Manoil 		dma_unmap_page(priv->dev, bufaddr, be16_to_cpu(txbdp->length),
25600a4b5a24SKevin Hao 			       DMA_TO_DEVICE);
25610a4b5a24SKevin Hao 		txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
25620a4b5a24SKevin Hao 	}
25630a4b5a24SKevin Hao 	gfar_wmb();
25640a4b5a24SKevin Hao 	dev_kfree_skb_any(skb);
25650a4b5a24SKevin Hao 	return NETDEV_TX_OK;
2566ec21e2ecSJeff Kirsher }
2567ec21e2ecSJeff Kirsher 
2568ec21e2ecSJeff Kirsher /* Stops the kernel queue, and halts the controller */
2569ec21e2ecSJeff Kirsher static int gfar_close(struct net_device *dev)
2570ec21e2ecSJeff Kirsher {
2571ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2572ec21e2ecSJeff Kirsher 
2573ec21e2ecSJeff Kirsher 	cancel_work_sync(&priv->reset_task);
2574ec21e2ecSJeff Kirsher 	stop_gfar(dev);
2575ec21e2ecSJeff Kirsher 
2576ec21e2ecSJeff Kirsher 	/* Disconnect from the PHY */
2577ec21e2ecSJeff Kirsher 	phy_disconnect(priv->phydev);
2578ec21e2ecSJeff Kirsher 	priv->phydev = NULL;
2579ec21e2ecSJeff Kirsher 
258080ec396cSClaudiu Manoil 	gfar_free_irq(priv);
258180ec396cSClaudiu Manoil 
2582ec21e2ecSJeff Kirsher 	return 0;
2583ec21e2ecSJeff Kirsher }
2584ec21e2ecSJeff Kirsher 
2585ec21e2ecSJeff Kirsher /* Changes the mac address if the controller is not running. */
2586ec21e2ecSJeff Kirsher static int gfar_set_mac_address(struct net_device *dev)
2587ec21e2ecSJeff Kirsher {
2588ec21e2ecSJeff Kirsher 	gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
2589ec21e2ecSJeff Kirsher 
2590ec21e2ecSJeff Kirsher 	return 0;
2591ec21e2ecSJeff Kirsher }
2592ec21e2ecSJeff Kirsher 
2593ec21e2ecSJeff Kirsher static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2594ec21e2ecSJeff Kirsher {
2595ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2596ec21e2ecSJeff Kirsher 	int frame_size = new_mtu + ETH_HLEN;
2597ec21e2ecSJeff Kirsher 
259875354148SClaudiu Manoil 	if ((frame_size < 64) || (frame_size > GFAR_JUMBO_FRAME_SIZE)) {
2599ec21e2ecSJeff Kirsher 		netif_err(priv, drv, dev, "Invalid MTU setting\n");
2600ec21e2ecSJeff Kirsher 		return -EINVAL;
2601ec21e2ecSJeff Kirsher 	}
2602ec21e2ecSJeff Kirsher 
26030851133bSClaudiu Manoil 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
26040851133bSClaudiu Manoil 		cpu_relax();
26050851133bSClaudiu Manoil 
260688302648SClaudiu Manoil 	if (dev->flags & IFF_UP)
2607ec21e2ecSJeff Kirsher 		stop_gfar(dev);
2608ec21e2ecSJeff Kirsher 
2609ec21e2ecSJeff Kirsher 	dev->mtu = new_mtu;
2610ec21e2ecSJeff Kirsher 
261188302648SClaudiu Manoil 	if (dev->flags & IFF_UP)
2612ec21e2ecSJeff Kirsher 		startup_gfar(dev);
2613ec21e2ecSJeff Kirsher 
26140851133bSClaudiu Manoil 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
26150851133bSClaudiu Manoil 
2616ec21e2ecSJeff Kirsher 	return 0;
2617ec21e2ecSJeff Kirsher }
2618ec21e2ecSJeff Kirsher 
26190851133bSClaudiu Manoil void reset_gfar(struct net_device *ndev)
26200851133bSClaudiu Manoil {
26210851133bSClaudiu Manoil 	struct gfar_private *priv = netdev_priv(ndev);
26220851133bSClaudiu Manoil 
26230851133bSClaudiu Manoil 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
26240851133bSClaudiu Manoil 		cpu_relax();
26250851133bSClaudiu Manoil 
26260851133bSClaudiu Manoil 	stop_gfar(ndev);
26270851133bSClaudiu Manoil 	startup_gfar(ndev);
26280851133bSClaudiu Manoil 
26290851133bSClaudiu Manoil 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
26300851133bSClaudiu Manoil }
26310851133bSClaudiu Manoil 
2632ec21e2ecSJeff Kirsher /* gfar_reset_task gets scheduled when a packet has not been
2633ec21e2ecSJeff Kirsher  * transmitted after a set amount of time.
2634ec21e2ecSJeff Kirsher  * For now, assume that clearing out all the structures, and
2635ec21e2ecSJeff Kirsher  * starting over will fix the problem.
2636ec21e2ecSJeff Kirsher  */
2637ec21e2ecSJeff Kirsher static void gfar_reset_task(struct work_struct *work)
2638ec21e2ecSJeff Kirsher {
2639ec21e2ecSJeff Kirsher 	struct gfar_private *priv = container_of(work, struct gfar_private,
2640ec21e2ecSJeff Kirsher 						 reset_task);
26410851133bSClaudiu Manoil 	reset_gfar(priv->ndev);
2642ec21e2ecSJeff Kirsher }
2643ec21e2ecSJeff Kirsher 
2644ec21e2ecSJeff Kirsher static void gfar_timeout(struct net_device *dev)
2645ec21e2ecSJeff Kirsher {
2646ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2647ec21e2ecSJeff Kirsher 
2648ec21e2ecSJeff Kirsher 	dev->stats.tx_errors++;
2649ec21e2ecSJeff Kirsher 	schedule_work(&priv->reset_task);
2650ec21e2ecSJeff Kirsher }
2651ec21e2ecSJeff Kirsher 
2652ec21e2ecSJeff Kirsher /* Interrupt Handler for Transmit complete */
2653c233cf40SClaudiu Manoil static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
2654ec21e2ecSJeff Kirsher {
2655ec21e2ecSJeff Kirsher 	struct net_device *dev = tx_queue->dev;
2656d8a0f1b0SPaul Gortmaker 	struct netdev_queue *txq;
2657ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
2658ec21e2ecSJeff Kirsher 	struct txbd8 *bdp, *next = NULL;
2659ec21e2ecSJeff Kirsher 	struct txbd8 *lbdp = NULL;
2660ec21e2ecSJeff Kirsher 	struct txbd8 *base = tx_queue->tx_bd_base;
2661ec21e2ecSJeff Kirsher 	struct sk_buff *skb;
2662ec21e2ecSJeff Kirsher 	int skb_dirtytx;
2663ec21e2ecSJeff Kirsher 	int tx_ring_size = tx_queue->tx_ring_size;
2664ec21e2ecSJeff Kirsher 	int frags = 0, nr_txbds = 0;
2665ec21e2ecSJeff Kirsher 	int i;
2666ec21e2ecSJeff Kirsher 	int howmany = 0;
2667d8a0f1b0SPaul Gortmaker 	int tqi = tx_queue->qindex;
2668d8a0f1b0SPaul Gortmaker 	unsigned int bytes_sent = 0;
2669ec21e2ecSJeff Kirsher 	u32 lstatus;
2670ec21e2ecSJeff Kirsher 	size_t buflen;
2671ec21e2ecSJeff Kirsher 
2672d8a0f1b0SPaul Gortmaker 	txq = netdev_get_tx_queue(dev, tqi);
2673ec21e2ecSJeff Kirsher 	bdp = tx_queue->dirty_tx;
2674ec21e2ecSJeff Kirsher 	skb_dirtytx = tx_queue->skb_dirtytx;
2675ec21e2ecSJeff Kirsher 
2676ec21e2ecSJeff Kirsher 	while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
2677ec21e2ecSJeff Kirsher 
2678ec21e2ecSJeff Kirsher 		frags = skb_shinfo(skb)->nr_frags;
2679ec21e2ecSJeff Kirsher 
26800977f817SJan Ceuleers 		/* When time stamping, one additional TxBD must be freed.
2681ec21e2ecSJeff Kirsher 		 * Also, we need to dma_unmap_single() the TxPAL.
2682ec21e2ecSJeff Kirsher 		 */
2683ec21e2ecSJeff Kirsher 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
2684ec21e2ecSJeff Kirsher 			nr_txbds = frags + 2;
2685ec21e2ecSJeff Kirsher 		else
2686ec21e2ecSJeff Kirsher 			nr_txbds = frags + 1;
2687ec21e2ecSJeff Kirsher 
2688ec21e2ecSJeff Kirsher 		lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
2689ec21e2ecSJeff Kirsher 
2690a7312d58SClaudiu Manoil 		lstatus = be32_to_cpu(lbdp->lstatus);
2691ec21e2ecSJeff Kirsher 
2692ec21e2ecSJeff Kirsher 		/* Only clean completed frames */
2693ec21e2ecSJeff Kirsher 		if ((lstatus & BD_LFLAG(TXBD_READY)) &&
2694ec21e2ecSJeff Kirsher 		    (lstatus & BD_LENGTH_MASK))
2695ec21e2ecSJeff Kirsher 			break;
2696ec21e2ecSJeff Kirsher 
2697ec21e2ecSJeff Kirsher 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
2698ec21e2ecSJeff Kirsher 			next = next_txbd(bdp, base, tx_ring_size);
2699a7312d58SClaudiu Manoil 			buflen = be16_to_cpu(next->length) +
2700a7312d58SClaudiu Manoil 				 GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2701ec21e2ecSJeff Kirsher 		} else
2702a7312d58SClaudiu Manoil 			buflen = be16_to_cpu(bdp->length);
2703ec21e2ecSJeff Kirsher 
2704a7312d58SClaudiu Manoil 		dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr),
2705ec21e2ecSJeff Kirsher 				 buflen, DMA_TO_DEVICE);
2706ec21e2ecSJeff Kirsher 
2707ec21e2ecSJeff Kirsher 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
2708ec21e2ecSJeff Kirsher 			struct skb_shared_hwtstamps shhwtstamps;
2709b4b67f26SScott Wood 			u64 *ns = (u64 *)(((uintptr_t)skb->data + 0x10) &
2710b4b67f26SScott Wood 					  ~0x7UL);
2711bc4598bcSJan Ceuleers 
2712ec21e2ecSJeff Kirsher 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2713ec21e2ecSJeff Kirsher 			shhwtstamps.hwtstamp = ns_to_ktime(*ns);
27149c4886e5SManfred Rudigier 			skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
2715ec21e2ecSJeff Kirsher 			skb_tstamp_tx(skb, &shhwtstamps);
2716a7312d58SClaudiu Manoil 			gfar_clear_txbd_status(bdp);
2717ec21e2ecSJeff Kirsher 			bdp = next;
2718ec21e2ecSJeff Kirsher 		}
2719ec21e2ecSJeff Kirsher 
2720a7312d58SClaudiu Manoil 		gfar_clear_txbd_status(bdp);
2721ec21e2ecSJeff Kirsher 		bdp = next_txbd(bdp, base, tx_ring_size);
2722ec21e2ecSJeff Kirsher 
2723ec21e2ecSJeff Kirsher 		for (i = 0; i < frags; i++) {
2724a7312d58SClaudiu Manoil 			dma_unmap_page(priv->dev, be32_to_cpu(bdp->bufPtr),
2725a7312d58SClaudiu Manoil 				       be16_to_cpu(bdp->length),
2726a7312d58SClaudiu Manoil 				       DMA_TO_DEVICE);
2727a7312d58SClaudiu Manoil 			gfar_clear_txbd_status(bdp);
2728ec21e2ecSJeff Kirsher 			bdp = next_txbd(bdp, base, tx_ring_size);
2729ec21e2ecSJeff Kirsher 		}
2730ec21e2ecSJeff Kirsher 
273150ad076bSClaudiu Manoil 		bytes_sent += GFAR_CB(skb)->bytes_sent;
2732d8a0f1b0SPaul Gortmaker 
2733ec21e2ecSJeff Kirsher 		dev_kfree_skb_any(skb);
2734ec21e2ecSJeff Kirsher 
2735ec21e2ecSJeff Kirsher 		tx_queue->tx_skbuff[skb_dirtytx] = NULL;
2736ec21e2ecSJeff Kirsher 
2737ec21e2ecSJeff Kirsher 		skb_dirtytx = (skb_dirtytx + 1) &
2738ec21e2ecSJeff Kirsher 			      TX_RING_MOD_MASK(tx_ring_size);
2739ec21e2ecSJeff Kirsher 
2740ec21e2ecSJeff Kirsher 		howmany++;
2741bc602280SClaudiu Manoil 		spin_lock(&tx_queue->txlock);
2742ec21e2ecSJeff Kirsher 		tx_queue->num_txbdfree += nr_txbds;
2743bc602280SClaudiu Manoil 		spin_unlock(&tx_queue->txlock);
2744ec21e2ecSJeff Kirsher 	}
2745ec21e2ecSJeff Kirsher 
2746ec21e2ecSJeff Kirsher 	/* If we freed a buffer, we can restart transmission, if necessary */
27470851133bSClaudiu Manoil 	if (tx_queue->num_txbdfree &&
27480851133bSClaudiu Manoil 	    netif_tx_queue_stopped(txq) &&
27490851133bSClaudiu Manoil 	    !(test_bit(GFAR_DOWN, &priv->state)))
27500851133bSClaudiu Manoil 		netif_wake_subqueue(priv->ndev, tqi);
2751ec21e2ecSJeff Kirsher 
2752ec21e2ecSJeff Kirsher 	/* Update dirty indicators */
2753ec21e2ecSJeff Kirsher 	tx_queue->skb_dirtytx = skb_dirtytx;
2754ec21e2ecSJeff Kirsher 	tx_queue->dirty_tx = bdp;
2755ec21e2ecSJeff Kirsher 
2756d8a0f1b0SPaul Gortmaker 	netdev_tx_completed_queue(txq, howmany, bytes_sent);
2757ec21e2ecSJeff Kirsher }
2758ec21e2ecSJeff Kirsher 
275975354148SClaudiu Manoil static bool gfar_new_page(struct gfar_priv_rx_q *rxq, struct gfar_rx_buff *rxb)
2760ec21e2ecSJeff Kirsher {
276175354148SClaudiu Manoil 	struct page *page;
276276f31e8bSClaudiu Manoil 	dma_addr_t addr;
2763ec21e2ecSJeff Kirsher 
276475354148SClaudiu Manoil 	page = dev_alloc_page();
276575354148SClaudiu Manoil 	if (unlikely(!page))
276675354148SClaudiu Manoil 		return false;
2767ec21e2ecSJeff Kirsher 
276875354148SClaudiu Manoil 	addr = dma_map_page(rxq->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
276975354148SClaudiu Manoil 	if (unlikely(dma_mapping_error(rxq->dev, addr))) {
277075354148SClaudiu Manoil 		__free_page(page);
2771ec21e2ecSJeff Kirsher 
277275354148SClaudiu Manoil 		return false;
27730a4b5a24SKevin Hao 	}
27740a4b5a24SKevin Hao 
277575354148SClaudiu Manoil 	rxb->dma = addr;
277675354148SClaudiu Manoil 	rxb->page = page;
277775354148SClaudiu Manoil 	rxb->page_offset = 0;
277875354148SClaudiu Manoil 
277975354148SClaudiu Manoil 	return true;
2780ec21e2ecSJeff Kirsher }
2781ec21e2ecSJeff Kirsher 
278276f31e8bSClaudiu Manoil static void gfar_rx_alloc_err(struct gfar_priv_rx_q *rx_queue)
278376f31e8bSClaudiu Manoil {
2784f23223f1SClaudiu Manoil 	struct gfar_private *priv = netdev_priv(rx_queue->ndev);
278576f31e8bSClaudiu Manoil 	struct gfar_extra_stats *estats = &priv->extra_stats;
278676f31e8bSClaudiu Manoil 
2787f23223f1SClaudiu Manoil 	netdev_err(rx_queue->ndev, "Can't alloc RX buffers\n");
278876f31e8bSClaudiu Manoil 	atomic64_inc(&estats->rx_alloc_err);
278976f31e8bSClaudiu Manoil }
279076f31e8bSClaudiu Manoil 
279176f31e8bSClaudiu Manoil static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue,
279276f31e8bSClaudiu Manoil 				int alloc_cnt)
279376f31e8bSClaudiu Manoil {
279475354148SClaudiu Manoil 	struct rxbd8 *bdp;
279575354148SClaudiu Manoil 	struct gfar_rx_buff *rxb;
279676f31e8bSClaudiu Manoil 	int i;
279776f31e8bSClaudiu Manoil 
279876f31e8bSClaudiu Manoil 	i = rx_queue->next_to_use;
279976f31e8bSClaudiu Manoil 	bdp = &rx_queue->rx_bd_base[i];
280075354148SClaudiu Manoil 	rxb = &rx_queue->rx_buff[i];
280176f31e8bSClaudiu Manoil 
280276f31e8bSClaudiu Manoil 	while (alloc_cnt--) {
280375354148SClaudiu Manoil 		/* try reuse page */
280475354148SClaudiu Manoil 		if (unlikely(!rxb->page)) {
280575354148SClaudiu Manoil 			if (unlikely(!gfar_new_page(rx_queue, rxb))) {
280676f31e8bSClaudiu Manoil 				gfar_rx_alloc_err(rx_queue);
280776f31e8bSClaudiu Manoil 				break;
280876f31e8bSClaudiu Manoil 			}
280976f31e8bSClaudiu Manoil 		}
281076f31e8bSClaudiu Manoil 
281176f31e8bSClaudiu Manoil 		/* Setup the new RxBD */
281275354148SClaudiu Manoil 		gfar_init_rxbdp(rx_queue, bdp,
281375354148SClaudiu Manoil 				rxb->dma + rxb->page_offset + RXBUF_ALIGNMENT);
281476f31e8bSClaudiu Manoil 
281576f31e8bSClaudiu Manoil 		/* Update to the next pointer */
281675354148SClaudiu Manoil 		bdp++;
281775354148SClaudiu Manoil 		rxb++;
281876f31e8bSClaudiu Manoil 
281975354148SClaudiu Manoil 		if (unlikely(++i == rx_queue->rx_ring_size)) {
282076f31e8bSClaudiu Manoil 			i = 0;
282175354148SClaudiu Manoil 			bdp = rx_queue->rx_bd_base;
282275354148SClaudiu Manoil 			rxb = rx_queue->rx_buff;
282375354148SClaudiu Manoil 		}
282476f31e8bSClaudiu Manoil 	}
282576f31e8bSClaudiu Manoil 
282676f31e8bSClaudiu Manoil 	rx_queue->next_to_use = i;
282775354148SClaudiu Manoil 	rx_queue->next_to_alloc = i;
282876f31e8bSClaudiu Manoil }
282976f31e8bSClaudiu Manoil 
2830f23223f1SClaudiu Manoil static void count_errors(u32 lstatus, struct net_device *ndev)
2831ec21e2ecSJeff Kirsher {
2832f23223f1SClaudiu Manoil 	struct gfar_private *priv = netdev_priv(ndev);
2833f23223f1SClaudiu Manoil 	struct net_device_stats *stats = &ndev->stats;
2834ec21e2ecSJeff Kirsher 	struct gfar_extra_stats *estats = &priv->extra_stats;
2835ec21e2ecSJeff Kirsher 
28360977f817SJan Ceuleers 	/* If the packet was truncated, none of the other errors matter */
2837f966082eSClaudiu Manoil 	if (lstatus & BD_LFLAG(RXBD_TRUNCATED)) {
2838ec21e2ecSJeff Kirsher 		stats->rx_length_errors++;
2839ec21e2ecSJeff Kirsher 
2840212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_trunc);
2841ec21e2ecSJeff Kirsher 
2842ec21e2ecSJeff Kirsher 		return;
2843ec21e2ecSJeff Kirsher 	}
2844ec21e2ecSJeff Kirsher 	/* Count the errors, if there were any */
2845f966082eSClaudiu Manoil 	if (lstatus & BD_LFLAG(RXBD_LARGE | RXBD_SHORT)) {
2846ec21e2ecSJeff Kirsher 		stats->rx_length_errors++;
2847ec21e2ecSJeff Kirsher 
2848f966082eSClaudiu Manoil 		if (lstatus & BD_LFLAG(RXBD_LARGE))
2849212079dfSPaul Gortmaker 			atomic64_inc(&estats->rx_large);
2850ec21e2ecSJeff Kirsher 		else
2851212079dfSPaul Gortmaker 			atomic64_inc(&estats->rx_short);
2852ec21e2ecSJeff Kirsher 	}
2853f966082eSClaudiu Manoil 	if (lstatus & BD_LFLAG(RXBD_NONOCTET)) {
2854ec21e2ecSJeff Kirsher 		stats->rx_frame_errors++;
2855212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_nonoctet);
2856ec21e2ecSJeff Kirsher 	}
2857f966082eSClaudiu Manoil 	if (lstatus & BD_LFLAG(RXBD_CRCERR)) {
2858212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_crcerr);
2859ec21e2ecSJeff Kirsher 		stats->rx_crc_errors++;
2860ec21e2ecSJeff Kirsher 	}
2861f966082eSClaudiu Manoil 	if (lstatus & BD_LFLAG(RXBD_OVERRUN)) {
2862212079dfSPaul Gortmaker 		atomic64_inc(&estats->rx_overrun);
2863f966082eSClaudiu Manoil 		stats->rx_over_errors++;
2864ec21e2ecSJeff Kirsher 	}
2865ec21e2ecSJeff Kirsher }
2866ec21e2ecSJeff Kirsher 
2867ec21e2ecSJeff Kirsher irqreturn_t gfar_receive(int irq, void *grp_id)
2868ec21e2ecSJeff Kirsher {
2869aeb12c5eSClaudiu Manoil 	struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2870aeb12c5eSClaudiu Manoil 	unsigned long flags;
28713e905b80SClaudiu Manoil 	u32 imask, ievent;
28723e905b80SClaudiu Manoil 
28733e905b80SClaudiu Manoil 	ievent = gfar_read(&grp->regs->ievent);
28743e905b80SClaudiu Manoil 
28753e905b80SClaudiu Manoil 	if (unlikely(ievent & IEVENT_FGPI)) {
28763e905b80SClaudiu Manoil 		gfar_write(&grp->regs->ievent, IEVENT_FGPI);
28773e905b80SClaudiu Manoil 		return IRQ_HANDLED;
28783e905b80SClaudiu Manoil 	}
2879aeb12c5eSClaudiu Manoil 
2880aeb12c5eSClaudiu Manoil 	if (likely(napi_schedule_prep(&grp->napi_rx))) {
2881aeb12c5eSClaudiu Manoil 		spin_lock_irqsave(&grp->grplock, flags);
2882aeb12c5eSClaudiu Manoil 		imask = gfar_read(&grp->regs->imask);
2883aeb12c5eSClaudiu Manoil 		imask &= IMASK_RX_DISABLED;
2884aeb12c5eSClaudiu Manoil 		gfar_write(&grp->regs->imask, imask);
2885aeb12c5eSClaudiu Manoil 		spin_unlock_irqrestore(&grp->grplock, flags);
2886aeb12c5eSClaudiu Manoil 		__napi_schedule(&grp->napi_rx);
2887aeb12c5eSClaudiu Manoil 	} else {
2888aeb12c5eSClaudiu Manoil 		/* Clear IEVENT, so interrupts aren't called again
2889aeb12c5eSClaudiu Manoil 		 * because of the packets that have already arrived.
2890aeb12c5eSClaudiu Manoil 		 */
2891aeb12c5eSClaudiu Manoil 		gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
2892aeb12c5eSClaudiu Manoil 	}
2893aeb12c5eSClaudiu Manoil 
2894aeb12c5eSClaudiu Manoil 	return IRQ_HANDLED;
2895aeb12c5eSClaudiu Manoil }
2896aeb12c5eSClaudiu Manoil 
2897aeb12c5eSClaudiu Manoil /* Interrupt Handler for Transmit complete */
2898aeb12c5eSClaudiu Manoil static irqreturn_t gfar_transmit(int irq, void *grp_id)
2899aeb12c5eSClaudiu Manoil {
2900aeb12c5eSClaudiu Manoil 	struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2901aeb12c5eSClaudiu Manoil 	unsigned long flags;
2902aeb12c5eSClaudiu Manoil 	u32 imask;
2903aeb12c5eSClaudiu Manoil 
2904aeb12c5eSClaudiu Manoil 	if (likely(napi_schedule_prep(&grp->napi_tx))) {
2905aeb12c5eSClaudiu Manoil 		spin_lock_irqsave(&grp->grplock, flags);
2906aeb12c5eSClaudiu Manoil 		imask = gfar_read(&grp->regs->imask);
2907aeb12c5eSClaudiu Manoil 		imask &= IMASK_TX_DISABLED;
2908aeb12c5eSClaudiu Manoil 		gfar_write(&grp->regs->imask, imask);
2909aeb12c5eSClaudiu Manoil 		spin_unlock_irqrestore(&grp->grplock, flags);
2910aeb12c5eSClaudiu Manoil 		__napi_schedule(&grp->napi_tx);
2911aeb12c5eSClaudiu Manoil 	} else {
2912aeb12c5eSClaudiu Manoil 		/* Clear IEVENT, so interrupts aren't called again
2913aeb12c5eSClaudiu Manoil 		 * because of the packets that have already arrived.
2914aeb12c5eSClaudiu Manoil 		 */
2915aeb12c5eSClaudiu Manoil 		gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
2916aeb12c5eSClaudiu Manoil 	}
2917aeb12c5eSClaudiu Manoil 
2918ec21e2ecSJeff Kirsher 	return IRQ_HANDLED;
2919ec21e2ecSJeff Kirsher }
2920ec21e2ecSJeff Kirsher 
292175354148SClaudiu Manoil static bool gfar_add_rx_frag(struct gfar_rx_buff *rxb, u32 lstatus,
292275354148SClaudiu Manoil 			     struct sk_buff *skb, bool first)
292375354148SClaudiu Manoil {
292475354148SClaudiu Manoil 	unsigned int size = lstatus & BD_LENGTH_MASK;
292575354148SClaudiu Manoil 	struct page *page = rxb->page;
292675354148SClaudiu Manoil 
292775354148SClaudiu Manoil 	/* Remove the FCS from the packet length */
292875354148SClaudiu Manoil 	if (likely(lstatus & BD_LFLAG(RXBD_LAST)))
292975354148SClaudiu Manoil 		size -= ETH_FCS_LEN;
293075354148SClaudiu Manoil 
293175354148SClaudiu Manoil 	if (likely(first))
293275354148SClaudiu Manoil 		skb_put(skb, size);
293375354148SClaudiu Manoil 	else
293475354148SClaudiu Manoil 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
293575354148SClaudiu Manoil 				rxb->page_offset + RXBUF_ALIGNMENT,
293675354148SClaudiu Manoil 				size, GFAR_RXB_TRUESIZE);
293775354148SClaudiu Manoil 
293875354148SClaudiu Manoil 	/* try reuse page */
293975354148SClaudiu Manoil 	if (unlikely(page_count(page) != 1))
294075354148SClaudiu Manoil 		return false;
294175354148SClaudiu Manoil 
294275354148SClaudiu Manoil 	/* change offset to the other half */
294375354148SClaudiu Manoil 	rxb->page_offset ^= GFAR_RXB_TRUESIZE;
294475354148SClaudiu Manoil 
294575354148SClaudiu Manoil 	atomic_inc(&page->_count);
294675354148SClaudiu Manoil 
294775354148SClaudiu Manoil 	return true;
294875354148SClaudiu Manoil }
294975354148SClaudiu Manoil 
295075354148SClaudiu Manoil static void gfar_reuse_rx_page(struct gfar_priv_rx_q *rxq,
295175354148SClaudiu Manoil 			       struct gfar_rx_buff *old_rxb)
295275354148SClaudiu Manoil {
295375354148SClaudiu Manoil 	struct gfar_rx_buff *new_rxb;
295475354148SClaudiu Manoil 	u16 nta = rxq->next_to_alloc;
295575354148SClaudiu Manoil 
295675354148SClaudiu Manoil 	new_rxb = &rxq->rx_buff[nta];
295775354148SClaudiu Manoil 
295875354148SClaudiu Manoil 	/* find next buf that can reuse a page */
295975354148SClaudiu Manoil 	nta++;
296075354148SClaudiu Manoil 	rxq->next_to_alloc = (nta < rxq->rx_ring_size) ? nta : 0;
296175354148SClaudiu Manoil 
296275354148SClaudiu Manoil 	/* copy page reference */
296375354148SClaudiu Manoil 	*new_rxb = *old_rxb;
296475354148SClaudiu Manoil 
296575354148SClaudiu Manoil 	/* sync for use by the device */
296675354148SClaudiu Manoil 	dma_sync_single_range_for_device(rxq->dev, old_rxb->dma,
296775354148SClaudiu Manoil 					 old_rxb->page_offset,
296875354148SClaudiu Manoil 					 GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE);
296975354148SClaudiu Manoil }
297075354148SClaudiu Manoil 
297175354148SClaudiu Manoil static struct sk_buff *gfar_get_next_rxbuff(struct gfar_priv_rx_q *rx_queue,
297275354148SClaudiu Manoil 					    u32 lstatus, struct sk_buff *skb)
297375354148SClaudiu Manoil {
297475354148SClaudiu Manoil 	struct gfar_rx_buff *rxb = &rx_queue->rx_buff[rx_queue->next_to_clean];
297575354148SClaudiu Manoil 	struct page *page = rxb->page;
297675354148SClaudiu Manoil 	bool first = false;
297775354148SClaudiu Manoil 
297875354148SClaudiu Manoil 	if (likely(!skb)) {
297975354148SClaudiu Manoil 		void *buff_addr = page_address(page) + rxb->page_offset;
298075354148SClaudiu Manoil 
298175354148SClaudiu Manoil 		skb = build_skb(buff_addr, GFAR_SKBFRAG_SIZE);
298275354148SClaudiu Manoil 		if (unlikely(!skb)) {
298375354148SClaudiu Manoil 			gfar_rx_alloc_err(rx_queue);
298475354148SClaudiu Manoil 			return NULL;
298575354148SClaudiu Manoil 		}
298675354148SClaudiu Manoil 		skb_reserve(skb, RXBUF_ALIGNMENT);
298775354148SClaudiu Manoil 		first = true;
298875354148SClaudiu Manoil 	}
298975354148SClaudiu Manoil 
299075354148SClaudiu Manoil 	dma_sync_single_range_for_cpu(rx_queue->dev, rxb->dma, rxb->page_offset,
299175354148SClaudiu Manoil 				      GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE);
299275354148SClaudiu Manoil 
299375354148SClaudiu Manoil 	if (gfar_add_rx_frag(rxb, lstatus, skb, first)) {
299475354148SClaudiu Manoil 		/* reuse the free half of the page */
299575354148SClaudiu Manoil 		gfar_reuse_rx_page(rx_queue, rxb);
299675354148SClaudiu Manoil 	} else {
299775354148SClaudiu Manoil 		/* page cannot be reused, unmap it */
299875354148SClaudiu Manoil 		dma_unmap_page(rx_queue->dev, rxb->dma,
299975354148SClaudiu Manoil 			       PAGE_SIZE, DMA_FROM_DEVICE);
300075354148SClaudiu Manoil 	}
300175354148SClaudiu Manoil 
300275354148SClaudiu Manoil 	/* clear rxb content */
300375354148SClaudiu Manoil 	rxb->page = NULL;
300475354148SClaudiu Manoil 
300575354148SClaudiu Manoil 	return skb;
300675354148SClaudiu Manoil }
300775354148SClaudiu Manoil 
3008ec21e2ecSJeff Kirsher static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
3009ec21e2ecSJeff Kirsher {
3010ec21e2ecSJeff Kirsher 	/* If valid headers were found, and valid sums
3011ec21e2ecSJeff Kirsher 	 * were verified, then we tell the kernel that no
30120977f817SJan Ceuleers 	 * checksumming is necessary.  Otherwise, it is [FIXME]
30130977f817SJan Ceuleers 	 */
301426eb9374SClaudiu Manoil 	if ((be16_to_cpu(fcb->flags) & RXFCB_CSUM_MASK) ==
301526eb9374SClaudiu Manoil 	    (RXFCB_CIP | RXFCB_CTU))
3016ec21e2ecSJeff Kirsher 		skb->ip_summed = CHECKSUM_UNNECESSARY;
3017ec21e2ecSJeff Kirsher 	else
3018ec21e2ecSJeff Kirsher 		skb_checksum_none_assert(skb);
3019ec21e2ecSJeff Kirsher }
3020ec21e2ecSJeff Kirsher 
30210977f817SJan Ceuleers /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
3022f23223f1SClaudiu Manoil static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb)
3023ec21e2ecSJeff Kirsher {
3024f23223f1SClaudiu Manoil 	struct gfar_private *priv = netdev_priv(ndev);
3025ec21e2ecSJeff Kirsher 	struct rxfcb *fcb = NULL;
3026ec21e2ecSJeff Kirsher 
3027ec21e2ecSJeff Kirsher 	/* fcb is at the beginning if exists */
3028ec21e2ecSJeff Kirsher 	fcb = (struct rxfcb *)skb->data;
3029ec21e2ecSJeff Kirsher 
30300977f817SJan Ceuleers 	/* Remove the FCB from the skb
30310977f817SJan Ceuleers 	 * Remove the padded bytes, if there are any
30320977f817SJan Ceuleers 	 */
3033f23223f1SClaudiu Manoil 	if (priv->uses_rxfcb)
303476f31e8bSClaudiu Manoil 		skb_pull(skb, GMAC_FCB_LEN);
3035ec21e2ecSJeff Kirsher 
3036ec21e2ecSJeff Kirsher 	/* Get receive timestamp from the skb */
3037ec21e2ecSJeff Kirsher 	if (priv->hwts_rx_en) {
3038ec21e2ecSJeff Kirsher 		struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
3039ec21e2ecSJeff Kirsher 		u64 *ns = (u64 *) skb->data;
3040bc4598bcSJan Ceuleers 
3041ec21e2ecSJeff Kirsher 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
3042ec21e2ecSJeff Kirsher 		shhwtstamps->hwtstamp = ns_to_ktime(*ns);
3043ec21e2ecSJeff Kirsher 	}
3044ec21e2ecSJeff Kirsher 
3045ec21e2ecSJeff Kirsher 	if (priv->padding)
3046ec21e2ecSJeff Kirsher 		skb_pull(skb, priv->padding);
3047ec21e2ecSJeff Kirsher 
3048f23223f1SClaudiu Manoil 	if (ndev->features & NETIF_F_RXCSUM)
3049ec21e2ecSJeff Kirsher 		gfar_rx_checksum(skb, fcb);
3050ec21e2ecSJeff Kirsher 
3051ec21e2ecSJeff Kirsher 	/* Tell the skb what kind of packet this is */
3052f23223f1SClaudiu Manoil 	skb->protocol = eth_type_trans(skb, ndev);
3053ec21e2ecSJeff Kirsher 
3054f646968fSPatrick McHardy 	/* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
3055823dcd25SDavid S. Miller 	 * Even if vlan rx accel is disabled, on some chips
3056823dcd25SDavid S. Miller 	 * RXFCB_VLN is pseudo randomly set.
3057823dcd25SDavid S. Miller 	 */
3058f23223f1SClaudiu Manoil 	if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX &&
305926eb9374SClaudiu Manoil 	    be16_to_cpu(fcb->flags) & RXFCB_VLN)
306026eb9374SClaudiu Manoil 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
306126eb9374SClaudiu Manoil 				       be16_to_cpu(fcb->vlctl));
3062ec21e2ecSJeff Kirsher }
3063ec21e2ecSJeff Kirsher 
3064ec21e2ecSJeff Kirsher /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
3065ec21e2ecSJeff Kirsher  * until the budget/quota has been reached. Returns the number
3066ec21e2ecSJeff Kirsher  * of frames handled
3067ec21e2ecSJeff Kirsher  */
3068ec21e2ecSJeff Kirsher int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
3069ec21e2ecSJeff Kirsher {
3070f23223f1SClaudiu Manoil 	struct net_device *ndev = rx_queue->ndev;
3071f23223f1SClaudiu Manoil 	struct gfar_private *priv = netdev_priv(ndev);
307275354148SClaudiu Manoil 	struct rxbd8 *bdp;
307375354148SClaudiu Manoil 	int i, howmany = 0;
307475354148SClaudiu Manoil 	struct sk_buff *skb = rx_queue->skb;
307575354148SClaudiu Manoil 	int cleaned_cnt = gfar_rxbd_unused(rx_queue);
307675354148SClaudiu Manoil 	unsigned int total_bytes = 0, total_pkts = 0;
3077ec21e2ecSJeff Kirsher 
3078ec21e2ecSJeff Kirsher 	/* Get the first full descriptor */
307976f31e8bSClaudiu Manoil 	i = rx_queue->next_to_clean;
3080ec21e2ecSJeff Kirsher 
308176f31e8bSClaudiu Manoil 	while (rx_work_limit--) {
3082f966082eSClaudiu Manoil 		u32 lstatus;
3083ec21e2ecSJeff Kirsher 
308476f31e8bSClaudiu Manoil 		if (cleaned_cnt >= GFAR_RX_BUFF_ALLOC) {
308576f31e8bSClaudiu Manoil 			gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
308676f31e8bSClaudiu Manoil 			cleaned_cnt = 0;
308776f31e8bSClaudiu Manoil 		}
3088bc4598bcSJan Ceuleers 
308976f31e8bSClaudiu Manoil 		bdp = &rx_queue->rx_bd_base[i];
3090f966082eSClaudiu Manoil 		lstatus = be32_to_cpu(bdp->lstatus);
3091f966082eSClaudiu Manoil 		if (lstatus & BD_LFLAG(RXBD_EMPTY))
309276f31e8bSClaudiu Manoil 			break;
309376f31e8bSClaudiu Manoil 
309476f31e8bSClaudiu Manoil 		/* order rx buffer descriptor reads */
3095ec21e2ecSJeff Kirsher 		rmb();
3096ec21e2ecSJeff Kirsher 
309776f31e8bSClaudiu Manoil 		/* fetch next to clean buffer from the ring */
309875354148SClaudiu Manoil 		skb = gfar_get_next_rxbuff(rx_queue, lstatus, skb);
309975354148SClaudiu Manoil 		if (unlikely(!skb))
310075354148SClaudiu Manoil 			break;
3101ec21e2ecSJeff Kirsher 
310275354148SClaudiu Manoil 		cleaned_cnt++;
310375354148SClaudiu Manoil 		howmany++;
3104ec21e2ecSJeff Kirsher 
310575354148SClaudiu Manoil 		if (unlikely(++i == rx_queue->rx_ring_size))
310675354148SClaudiu Manoil 			i = 0;
3107ec21e2ecSJeff Kirsher 
310875354148SClaudiu Manoil 		rx_queue->next_to_clean = i;
310975354148SClaudiu Manoil 
311075354148SClaudiu Manoil 		/* fetch next buffer if not the last in frame */
311175354148SClaudiu Manoil 		if (!(lstatus & BD_LFLAG(RXBD_LAST)))
311275354148SClaudiu Manoil 			continue;
311375354148SClaudiu Manoil 
311475354148SClaudiu Manoil 		if (unlikely(lstatus & BD_LFLAG(RXBD_ERR))) {
3115f23223f1SClaudiu Manoil 			count_errors(lstatus, ndev);
3116ec21e2ecSJeff Kirsher 
311776f31e8bSClaudiu Manoil 			/* discard faulty buffer */
3118acb600deSEric Dumazet 			dev_kfree_skb(skb);
311975354148SClaudiu Manoil 			skb = NULL;
312075354148SClaudiu Manoil 			rx_queue->stats.rx_dropped++;
312175354148SClaudiu Manoil 			continue;
312275354148SClaudiu Manoil 		}
312376f31e8bSClaudiu Manoil 
3124ec21e2ecSJeff Kirsher 		/* Increment the number of packets */
312575354148SClaudiu Manoil 		total_pkts++;
312675354148SClaudiu Manoil 		total_bytes += skb->len;
3127ec21e2ecSJeff Kirsher 
3128ec21e2ecSJeff Kirsher 		skb_record_rx_queue(skb, rx_queue->qindex);
312975354148SClaudiu Manoil 
3130f23223f1SClaudiu Manoil 		gfar_process_frame(ndev, skb);
3131f23223f1SClaudiu Manoil 
3132f23223f1SClaudiu Manoil 		/* Send the packet up the stack */
3133f23223f1SClaudiu Manoil 		napi_gro_receive(&rx_queue->grp->napi_rx, skb);
3134ec21e2ecSJeff Kirsher 
313575354148SClaudiu Manoil 		skb = NULL;
3136ec21e2ecSJeff Kirsher 	}
3137ec21e2ecSJeff Kirsher 
313875354148SClaudiu Manoil 	/* Store incomplete frames for completion */
313975354148SClaudiu Manoil 	rx_queue->skb = skb;
3140ec21e2ecSJeff Kirsher 
314175354148SClaudiu Manoil 	rx_queue->stats.rx_packets += total_pkts;
314275354148SClaudiu Manoil 	rx_queue->stats.rx_bytes += total_bytes;
314376f31e8bSClaudiu Manoil 
314476f31e8bSClaudiu Manoil 	if (cleaned_cnt)
314576f31e8bSClaudiu Manoil 		gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
314676f31e8bSClaudiu Manoil 
314776f31e8bSClaudiu Manoil 	/* Update Last Free RxBD pointer for LFC */
314876f31e8bSClaudiu Manoil 	if (unlikely(priv->tx_actual_en)) {
3149b4b67f26SScott Wood 		u32 bdp_dma = gfar_rxbd_dma_lastfree(rx_queue);
3150b4b67f26SScott Wood 
3151b4b67f26SScott Wood 		gfar_write(rx_queue->rfbptr, bdp_dma);
315276f31e8bSClaudiu Manoil 	}
3153ec21e2ecSJeff Kirsher 
3154ec21e2ecSJeff Kirsher 	return howmany;
3155ec21e2ecSJeff Kirsher }
3156ec21e2ecSJeff Kirsher 
3157aeb12c5eSClaudiu Manoil static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
31585eaedf31SClaudiu Manoil {
31595eaedf31SClaudiu Manoil 	struct gfar_priv_grp *gfargrp =
3160aeb12c5eSClaudiu Manoil 		container_of(napi, struct gfar_priv_grp, napi_rx);
31615eaedf31SClaudiu Manoil 	struct gfar __iomem *regs = gfargrp->regs;
316271ff9e3dSClaudiu Manoil 	struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
31635eaedf31SClaudiu Manoil 	int work_done = 0;
31645eaedf31SClaudiu Manoil 
31655eaedf31SClaudiu Manoil 	/* Clear IEVENT, so interrupts aren't called again
31665eaedf31SClaudiu Manoil 	 * because of the packets that have already arrived
31675eaedf31SClaudiu Manoil 	 */
3168aeb12c5eSClaudiu Manoil 	gfar_write(&regs->ievent, IEVENT_RX_MASK);
31695eaedf31SClaudiu Manoil 
31705eaedf31SClaudiu Manoil 	work_done = gfar_clean_rx_ring(rx_queue, budget);
31715eaedf31SClaudiu Manoil 
31725eaedf31SClaudiu Manoil 	if (work_done < budget) {
3173aeb12c5eSClaudiu Manoil 		u32 imask;
31745eaedf31SClaudiu Manoil 		napi_complete(napi);
31755eaedf31SClaudiu Manoil 		/* Clear the halt bit in RSTAT */
31765eaedf31SClaudiu Manoil 		gfar_write(&regs->rstat, gfargrp->rstat);
31775eaedf31SClaudiu Manoil 
3178aeb12c5eSClaudiu Manoil 		spin_lock_irq(&gfargrp->grplock);
3179aeb12c5eSClaudiu Manoil 		imask = gfar_read(&regs->imask);
3180aeb12c5eSClaudiu Manoil 		imask |= IMASK_RX_DEFAULT;
3181aeb12c5eSClaudiu Manoil 		gfar_write(&regs->imask, imask);
3182aeb12c5eSClaudiu Manoil 		spin_unlock_irq(&gfargrp->grplock);
31835eaedf31SClaudiu Manoil 	}
31845eaedf31SClaudiu Manoil 
31855eaedf31SClaudiu Manoil 	return work_done;
31865eaedf31SClaudiu Manoil }
31875eaedf31SClaudiu Manoil 
3188aeb12c5eSClaudiu Manoil static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
3189ec21e2ecSJeff Kirsher {
3190bc4598bcSJan Ceuleers 	struct gfar_priv_grp *gfargrp =
3191aeb12c5eSClaudiu Manoil 		container_of(napi, struct gfar_priv_grp, napi_tx);
3192aeb12c5eSClaudiu Manoil 	struct gfar __iomem *regs = gfargrp->regs;
319371ff9e3dSClaudiu Manoil 	struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
3194aeb12c5eSClaudiu Manoil 	u32 imask;
3195aeb12c5eSClaudiu Manoil 
3196aeb12c5eSClaudiu Manoil 	/* Clear IEVENT, so interrupts aren't called again
3197aeb12c5eSClaudiu Manoil 	 * because of the packets that have already arrived
3198aeb12c5eSClaudiu Manoil 	 */
3199aeb12c5eSClaudiu Manoil 	gfar_write(&regs->ievent, IEVENT_TX_MASK);
3200aeb12c5eSClaudiu Manoil 
3201aeb12c5eSClaudiu Manoil 	/* run Tx cleanup to completion */
3202aeb12c5eSClaudiu Manoil 	if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
3203aeb12c5eSClaudiu Manoil 		gfar_clean_tx_ring(tx_queue);
3204aeb12c5eSClaudiu Manoil 
3205aeb12c5eSClaudiu Manoil 	napi_complete(napi);
3206aeb12c5eSClaudiu Manoil 
3207aeb12c5eSClaudiu Manoil 	spin_lock_irq(&gfargrp->grplock);
3208aeb12c5eSClaudiu Manoil 	imask = gfar_read(&regs->imask);
3209aeb12c5eSClaudiu Manoil 	imask |= IMASK_TX_DEFAULT;
3210aeb12c5eSClaudiu Manoil 	gfar_write(&regs->imask, imask);
3211aeb12c5eSClaudiu Manoil 	spin_unlock_irq(&gfargrp->grplock);
3212aeb12c5eSClaudiu Manoil 
3213aeb12c5eSClaudiu Manoil 	return 0;
3214aeb12c5eSClaudiu Manoil }
3215aeb12c5eSClaudiu Manoil 
3216aeb12c5eSClaudiu Manoil static int gfar_poll_rx(struct napi_struct *napi, int budget)
3217aeb12c5eSClaudiu Manoil {
3218aeb12c5eSClaudiu Manoil 	struct gfar_priv_grp *gfargrp =
3219aeb12c5eSClaudiu Manoil 		container_of(napi, struct gfar_priv_grp, napi_rx);
3220ec21e2ecSJeff Kirsher 	struct gfar_private *priv = gfargrp->priv;
3221ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = gfargrp->regs;
3222ec21e2ecSJeff Kirsher 	struct gfar_priv_rx_q *rx_queue = NULL;
3223c233cf40SClaudiu Manoil 	int work_done = 0, work_done_per_q = 0;
322439c0a0d5SClaudiu Manoil 	int i, budget_per_q = 0;
32256be5ed3fSClaudiu Manoil 	unsigned long rstat_rxf;
32266be5ed3fSClaudiu Manoil 	int num_act_queues;
3227ec21e2ecSJeff Kirsher 
3228ec21e2ecSJeff Kirsher 	/* Clear IEVENT, so interrupts aren't called again
32290977f817SJan Ceuleers 	 * because of the packets that have already arrived
32300977f817SJan Ceuleers 	 */
3231aeb12c5eSClaudiu Manoil 	gfar_write(&regs->ievent, IEVENT_RX_MASK);
3232ec21e2ecSJeff Kirsher 
32336be5ed3fSClaudiu Manoil 	rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
32346be5ed3fSClaudiu Manoil 
32356be5ed3fSClaudiu Manoil 	num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
32366be5ed3fSClaudiu Manoil 	if (num_act_queues)
32376be5ed3fSClaudiu Manoil 		budget_per_q = budget/num_act_queues;
32386be5ed3fSClaudiu Manoil 
3239ec21e2ecSJeff Kirsher 	for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
32406be5ed3fSClaudiu Manoil 		/* skip queue if not active */
32416be5ed3fSClaudiu Manoil 		if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
3242ec21e2ecSJeff Kirsher 			continue;
3243ec21e2ecSJeff Kirsher 
3244c233cf40SClaudiu Manoil 		rx_queue = priv->rx_queue[i];
3245c233cf40SClaudiu Manoil 		work_done_per_q =
3246c233cf40SClaudiu Manoil 			gfar_clean_rx_ring(rx_queue, budget_per_q);
3247c233cf40SClaudiu Manoil 		work_done += work_done_per_q;
3248c233cf40SClaudiu Manoil 
3249c233cf40SClaudiu Manoil 		/* finished processing this queue */
3250c233cf40SClaudiu Manoil 		if (work_done_per_q < budget_per_q) {
32516be5ed3fSClaudiu Manoil 			/* clear active queue hw indication */
32526be5ed3fSClaudiu Manoil 			gfar_write(&regs->rstat,
32536be5ed3fSClaudiu Manoil 				   RSTAT_CLEAR_RXF0 >> i);
32546be5ed3fSClaudiu Manoil 			num_act_queues--;
32556be5ed3fSClaudiu Manoil 
32566be5ed3fSClaudiu Manoil 			if (!num_act_queues)
3257c233cf40SClaudiu Manoil 				break;
3258ec21e2ecSJeff Kirsher 		}
3259ec21e2ecSJeff Kirsher 	}
3260ec21e2ecSJeff Kirsher 
3261aeb12c5eSClaudiu Manoil 	if (!num_act_queues) {
3262aeb12c5eSClaudiu Manoil 		u32 imask;
3263ec21e2ecSJeff Kirsher 		napi_complete(napi);
3264ec21e2ecSJeff Kirsher 
3265ec21e2ecSJeff Kirsher 		/* Clear the halt bit in RSTAT */
3266ec21e2ecSJeff Kirsher 		gfar_write(&regs->rstat, gfargrp->rstat);
3267ec21e2ecSJeff Kirsher 
3268aeb12c5eSClaudiu Manoil 		spin_lock_irq(&gfargrp->grplock);
3269aeb12c5eSClaudiu Manoil 		imask = gfar_read(&regs->imask);
3270aeb12c5eSClaudiu Manoil 		imask |= IMASK_RX_DEFAULT;
3271aeb12c5eSClaudiu Manoil 		gfar_write(&regs->imask, imask);
3272aeb12c5eSClaudiu Manoil 		spin_unlock_irq(&gfargrp->grplock);
3273ec21e2ecSJeff Kirsher 	}
3274ec21e2ecSJeff Kirsher 
3275c233cf40SClaudiu Manoil 	return work_done;
3276ec21e2ecSJeff Kirsher }
3277ec21e2ecSJeff Kirsher 
3278aeb12c5eSClaudiu Manoil static int gfar_poll_tx(struct napi_struct *napi, int budget)
3279aeb12c5eSClaudiu Manoil {
3280aeb12c5eSClaudiu Manoil 	struct gfar_priv_grp *gfargrp =
3281aeb12c5eSClaudiu Manoil 		container_of(napi, struct gfar_priv_grp, napi_tx);
3282aeb12c5eSClaudiu Manoil 	struct gfar_private *priv = gfargrp->priv;
3283aeb12c5eSClaudiu Manoil 	struct gfar __iomem *regs = gfargrp->regs;
3284aeb12c5eSClaudiu Manoil 	struct gfar_priv_tx_q *tx_queue = NULL;
3285aeb12c5eSClaudiu Manoil 	int has_tx_work = 0;
3286aeb12c5eSClaudiu Manoil 	int i;
3287aeb12c5eSClaudiu Manoil 
3288aeb12c5eSClaudiu Manoil 	/* Clear IEVENT, so interrupts aren't called again
3289aeb12c5eSClaudiu Manoil 	 * because of the packets that have already arrived
3290aeb12c5eSClaudiu Manoil 	 */
3291aeb12c5eSClaudiu Manoil 	gfar_write(&regs->ievent, IEVENT_TX_MASK);
3292aeb12c5eSClaudiu Manoil 
3293aeb12c5eSClaudiu Manoil 	for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
3294aeb12c5eSClaudiu Manoil 		tx_queue = priv->tx_queue[i];
3295aeb12c5eSClaudiu Manoil 		/* run Tx cleanup to completion */
3296aeb12c5eSClaudiu Manoil 		if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
3297aeb12c5eSClaudiu Manoil 			gfar_clean_tx_ring(tx_queue);
3298aeb12c5eSClaudiu Manoil 			has_tx_work = 1;
3299aeb12c5eSClaudiu Manoil 		}
3300aeb12c5eSClaudiu Manoil 	}
3301aeb12c5eSClaudiu Manoil 
3302aeb12c5eSClaudiu Manoil 	if (!has_tx_work) {
3303aeb12c5eSClaudiu Manoil 		u32 imask;
3304aeb12c5eSClaudiu Manoil 		napi_complete(napi);
3305aeb12c5eSClaudiu Manoil 
3306aeb12c5eSClaudiu Manoil 		spin_lock_irq(&gfargrp->grplock);
3307aeb12c5eSClaudiu Manoil 		imask = gfar_read(&regs->imask);
3308aeb12c5eSClaudiu Manoil 		imask |= IMASK_TX_DEFAULT;
3309aeb12c5eSClaudiu Manoil 		gfar_write(&regs->imask, imask);
3310aeb12c5eSClaudiu Manoil 		spin_unlock_irq(&gfargrp->grplock);
3311aeb12c5eSClaudiu Manoil 	}
3312aeb12c5eSClaudiu Manoil 
3313aeb12c5eSClaudiu Manoil 	return 0;
3314aeb12c5eSClaudiu Manoil }
3315aeb12c5eSClaudiu Manoil 
3316aeb12c5eSClaudiu Manoil 
3317ec21e2ecSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
33180977f817SJan Ceuleers /* Polling 'interrupt' - used by things like netconsole to send skbs
3319ec21e2ecSJeff Kirsher  * without having to re-enable interrupts. It's not called while
3320ec21e2ecSJeff Kirsher  * the interrupt routine is executing.
3321ec21e2ecSJeff Kirsher  */
3322ec21e2ecSJeff Kirsher static void gfar_netpoll(struct net_device *dev)
3323ec21e2ecSJeff Kirsher {
3324ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
33253a2e16c8SJan Ceuleers 	int i;
3326ec21e2ecSJeff Kirsher 
3327ec21e2ecSJeff Kirsher 	/* If the device has multiple interrupts, run tx/rx */
3328ec21e2ecSJeff Kirsher 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
3329ec21e2ecSJeff Kirsher 		for (i = 0; i < priv->num_grps; i++) {
333062ed839dSPaul Gortmaker 			struct gfar_priv_grp *grp = &priv->gfargrp[i];
333162ed839dSPaul Gortmaker 
333262ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, TX)->irq);
333362ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, RX)->irq);
333462ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, ER)->irq);
333562ed839dSPaul Gortmaker 			gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
333662ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, ER)->irq);
333762ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, RX)->irq);
333862ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, TX)->irq);
3339ec21e2ecSJeff Kirsher 		}
3340ec21e2ecSJeff Kirsher 	} else {
3341ec21e2ecSJeff Kirsher 		for (i = 0; i < priv->num_grps; i++) {
334262ed839dSPaul Gortmaker 			struct gfar_priv_grp *grp = &priv->gfargrp[i];
334362ed839dSPaul Gortmaker 
334462ed839dSPaul Gortmaker 			disable_irq(gfar_irq(grp, TX)->irq);
334562ed839dSPaul Gortmaker 			gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
334662ed839dSPaul Gortmaker 			enable_irq(gfar_irq(grp, TX)->irq);
3347ec21e2ecSJeff Kirsher 		}
3348ec21e2ecSJeff Kirsher 	}
3349ec21e2ecSJeff Kirsher }
3350ec21e2ecSJeff Kirsher #endif
3351ec21e2ecSJeff Kirsher 
3352ec21e2ecSJeff Kirsher /* The interrupt handler for devices with one interrupt */
3353ec21e2ecSJeff Kirsher static irqreturn_t gfar_interrupt(int irq, void *grp_id)
3354ec21e2ecSJeff Kirsher {
3355ec21e2ecSJeff Kirsher 	struct gfar_priv_grp *gfargrp = grp_id;
3356ec21e2ecSJeff Kirsher 
3357ec21e2ecSJeff Kirsher 	/* Save ievent for future reference */
3358ec21e2ecSJeff Kirsher 	u32 events = gfar_read(&gfargrp->regs->ievent);
3359ec21e2ecSJeff Kirsher 
3360ec21e2ecSJeff Kirsher 	/* Check for reception */
3361ec21e2ecSJeff Kirsher 	if (events & IEVENT_RX_MASK)
3362ec21e2ecSJeff Kirsher 		gfar_receive(irq, grp_id);
3363ec21e2ecSJeff Kirsher 
3364ec21e2ecSJeff Kirsher 	/* Check for transmit completion */
3365ec21e2ecSJeff Kirsher 	if (events & IEVENT_TX_MASK)
3366ec21e2ecSJeff Kirsher 		gfar_transmit(irq, grp_id);
3367ec21e2ecSJeff Kirsher 
3368ec21e2ecSJeff Kirsher 	/* Check for errors */
3369ec21e2ecSJeff Kirsher 	if (events & IEVENT_ERR_MASK)
3370ec21e2ecSJeff Kirsher 		gfar_error(irq, grp_id);
3371ec21e2ecSJeff Kirsher 
3372ec21e2ecSJeff Kirsher 	return IRQ_HANDLED;
3373ec21e2ecSJeff Kirsher }
3374ec21e2ecSJeff Kirsher 
3375ec21e2ecSJeff Kirsher /* Called every time the controller might need to be made
3376ec21e2ecSJeff Kirsher  * aware of new link state.  The PHY code conveys this
3377ec21e2ecSJeff Kirsher  * information through variables in the phydev structure, and this
3378ec21e2ecSJeff Kirsher  * function converts those variables into the appropriate
3379ec21e2ecSJeff Kirsher  * register values, and can bring down the device if needed.
3380ec21e2ecSJeff Kirsher  */
3381ec21e2ecSJeff Kirsher static void adjust_link(struct net_device *dev)
3382ec21e2ecSJeff Kirsher {
3383ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
3384ec21e2ecSJeff Kirsher 	struct phy_device *phydev = priv->phydev;
3385ec21e2ecSJeff Kirsher 
33866ce29b0eSClaudiu Manoil 	if (unlikely(phydev->link != priv->oldlink ||
33870ae93b2cSGuenter Roeck 		     (phydev->link && (phydev->duplex != priv->oldduplex ||
33880ae93b2cSGuenter Roeck 				       phydev->speed != priv->oldspeed))))
33896ce29b0eSClaudiu Manoil 		gfar_update_link_state(priv);
3390ec21e2ecSJeff Kirsher }
3391ec21e2ecSJeff Kirsher 
3392ec21e2ecSJeff Kirsher /* Update the hash table based on the current list of multicast
3393ec21e2ecSJeff Kirsher  * addresses we subscribe to.  Also, change the promiscuity of
3394ec21e2ecSJeff Kirsher  * the device based on the flags (this function is called
33950977f817SJan Ceuleers  * whenever dev->flags is changed
33960977f817SJan Ceuleers  */
3397ec21e2ecSJeff Kirsher static void gfar_set_multi(struct net_device *dev)
3398ec21e2ecSJeff Kirsher {
3399ec21e2ecSJeff Kirsher 	struct netdev_hw_addr *ha;
3400ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
3401ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3402ec21e2ecSJeff Kirsher 	u32 tempval;
3403ec21e2ecSJeff Kirsher 
3404ec21e2ecSJeff Kirsher 	if (dev->flags & IFF_PROMISC) {
3405ec21e2ecSJeff Kirsher 		/* Set RCTRL to PROM */
3406ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->rctrl);
3407ec21e2ecSJeff Kirsher 		tempval |= RCTRL_PROM;
3408ec21e2ecSJeff Kirsher 		gfar_write(&regs->rctrl, tempval);
3409ec21e2ecSJeff Kirsher 	} else {
3410ec21e2ecSJeff Kirsher 		/* Set RCTRL to not PROM */
3411ec21e2ecSJeff Kirsher 		tempval = gfar_read(&regs->rctrl);
3412ec21e2ecSJeff Kirsher 		tempval &= ~(RCTRL_PROM);
3413ec21e2ecSJeff Kirsher 		gfar_write(&regs->rctrl, tempval);
3414ec21e2ecSJeff Kirsher 	}
3415ec21e2ecSJeff Kirsher 
3416ec21e2ecSJeff Kirsher 	if (dev->flags & IFF_ALLMULTI) {
3417ec21e2ecSJeff Kirsher 		/* Set the hash to rx all multicast frames */
3418ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr0, 0xffffffff);
3419ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr1, 0xffffffff);
3420ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr2, 0xffffffff);
3421ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr3, 0xffffffff);
3422ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr4, 0xffffffff);
3423ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr5, 0xffffffff);
3424ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr6, 0xffffffff);
3425ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr7, 0xffffffff);
3426ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr0, 0xffffffff);
3427ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr1, 0xffffffff);
3428ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr2, 0xffffffff);
3429ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr3, 0xffffffff);
3430ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr4, 0xffffffff);
3431ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr5, 0xffffffff);
3432ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr6, 0xffffffff);
3433ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr7, 0xffffffff);
3434ec21e2ecSJeff Kirsher 	} else {
3435ec21e2ecSJeff Kirsher 		int em_num;
3436ec21e2ecSJeff Kirsher 		int idx;
3437ec21e2ecSJeff Kirsher 
3438ec21e2ecSJeff Kirsher 		/* zero out the hash */
3439ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr0, 0x0);
3440ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr1, 0x0);
3441ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr2, 0x0);
3442ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr3, 0x0);
3443ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr4, 0x0);
3444ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr5, 0x0);
3445ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr6, 0x0);
3446ec21e2ecSJeff Kirsher 		gfar_write(&regs->igaddr7, 0x0);
3447ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr0, 0x0);
3448ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr1, 0x0);
3449ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr2, 0x0);
3450ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr3, 0x0);
3451ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr4, 0x0);
3452ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr5, 0x0);
3453ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr6, 0x0);
3454ec21e2ecSJeff Kirsher 		gfar_write(&regs->gaddr7, 0x0);
3455ec21e2ecSJeff Kirsher 
3456ec21e2ecSJeff Kirsher 		/* If we have extended hash tables, we need to
3457ec21e2ecSJeff Kirsher 		 * clear the exact match registers to prepare for
34580977f817SJan Ceuleers 		 * setting them
34590977f817SJan Ceuleers 		 */
3460ec21e2ecSJeff Kirsher 		if (priv->extended_hash) {
3461ec21e2ecSJeff Kirsher 			em_num = GFAR_EM_NUM + 1;
3462ec21e2ecSJeff Kirsher 			gfar_clear_exact_match(dev);
3463ec21e2ecSJeff Kirsher 			idx = 1;
3464ec21e2ecSJeff Kirsher 		} else {
3465ec21e2ecSJeff Kirsher 			idx = 0;
3466ec21e2ecSJeff Kirsher 			em_num = 0;
3467ec21e2ecSJeff Kirsher 		}
3468ec21e2ecSJeff Kirsher 
3469ec21e2ecSJeff Kirsher 		if (netdev_mc_empty(dev))
3470ec21e2ecSJeff Kirsher 			return;
3471ec21e2ecSJeff Kirsher 
3472ec21e2ecSJeff Kirsher 		/* Parse the list, and set the appropriate bits */
3473ec21e2ecSJeff Kirsher 		netdev_for_each_mc_addr(ha, dev) {
3474ec21e2ecSJeff Kirsher 			if (idx < em_num) {
3475ec21e2ecSJeff Kirsher 				gfar_set_mac_for_addr(dev, idx, ha->addr);
3476ec21e2ecSJeff Kirsher 				idx++;
3477ec21e2ecSJeff Kirsher 			} else
3478ec21e2ecSJeff Kirsher 				gfar_set_hash_for_addr(dev, ha->addr);
3479ec21e2ecSJeff Kirsher 		}
3480ec21e2ecSJeff Kirsher 	}
3481ec21e2ecSJeff Kirsher }
3482ec21e2ecSJeff Kirsher 
3483ec21e2ecSJeff Kirsher 
3484ec21e2ecSJeff Kirsher /* Clears each of the exact match registers to zero, so they
34850977f817SJan Ceuleers  * don't interfere with normal reception
34860977f817SJan Ceuleers  */
3487ec21e2ecSJeff Kirsher static void gfar_clear_exact_match(struct net_device *dev)
3488ec21e2ecSJeff Kirsher {
3489ec21e2ecSJeff Kirsher 	int idx;
34906a3c910cSJoe Perches 	static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
3491ec21e2ecSJeff Kirsher 
3492ec21e2ecSJeff Kirsher 	for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
3493ec21e2ecSJeff Kirsher 		gfar_set_mac_for_addr(dev, idx, zero_arr);
3494ec21e2ecSJeff Kirsher }
3495ec21e2ecSJeff Kirsher 
3496ec21e2ecSJeff Kirsher /* Set the appropriate hash bit for the given addr */
3497ec21e2ecSJeff Kirsher /* The algorithm works like so:
3498ec21e2ecSJeff Kirsher  * 1) Take the Destination Address (ie the multicast address), and
3499ec21e2ecSJeff Kirsher  * do a CRC on it (little endian), and reverse the bits of the
3500ec21e2ecSJeff Kirsher  * result.
3501ec21e2ecSJeff Kirsher  * 2) Use the 8 most significant bits as a hash into a 256-entry
3502ec21e2ecSJeff Kirsher  * table.  The table is controlled through 8 32-bit registers:
3503ec21e2ecSJeff Kirsher  * gaddr0-7.  gaddr0's MSB is entry 0, and gaddr7's LSB is
3504ec21e2ecSJeff Kirsher  * gaddr7.  This means that the 3 most significant bits in the
3505ec21e2ecSJeff Kirsher  * hash index which gaddr register to use, and the 5 other bits
3506ec21e2ecSJeff Kirsher  * indicate which bit (assuming an IBM numbering scheme, which
3507ec21e2ecSJeff Kirsher  * for PowerPC (tm) is usually the case) in the register holds
35080977f817SJan Ceuleers  * the entry.
35090977f817SJan Ceuleers  */
3510ec21e2ecSJeff Kirsher static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3511ec21e2ecSJeff Kirsher {
3512ec21e2ecSJeff Kirsher 	u32 tempval;
3513ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
35146a3c910cSJoe Perches 	u32 result = ether_crc(ETH_ALEN, addr);
3515ec21e2ecSJeff Kirsher 	int width = priv->hash_width;
3516ec21e2ecSJeff Kirsher 	u8 whichbit = (result >> (32 - width)) & 0x1f;
3517ec21e2ecSJeff Kirsher 	u8 whichreg = result >> (32 - width + 5);
3518ec21e2ecSJeff Kirsher 	u32 value = (1 << (31-whichbit));
3519ec21e2ecSJeff Kirsher 
3520ec21e2ecSJeff Kirsher 	tempval = gfar_read(priv->hash_regs[whichreg]);
3521ec21e2ecSJeff Kirsher 	tempval |= value;
3522ec21e2ecSJeff Kirsher 	gfar_write(priv->hash_regs[whichreg], tempval);
3523ec21e2ecSJeff Kirsher }
3524ec21e2ecSJeff Kirsher 
3525ec21e2ecSJeff Kirsher 
3526ec21e2ecSJeff Kirsher /* There are multiple MAC Address register pairs on some controllers
3527ec21e2ecSJeff Kirsher  * This function sets the numth pair to a given address
3528ec21e2ecSJeff Kirsher  */
3529ec21e2ecSJeff Kirsher static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3530ec21e2ecSJeff Kirsher 				  const u8 *addr)
3531ec21e2ecSJeff Kirsher {
3532ec21e2ecSJeff Kirsher 	struct gfar_private *priv = netdev_priv(dev);
3533ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3534ec21e2ecSJeff Kirsher 	u32 tempval;
3535ec21e2ecSJeff Kirsher 	u32 __iomem *macptr = &regs->macstnaddr1;
3536ec21e2ecSJeff Kirsher 
3537ec21e2ecSJeff Kirsher 	macptr += num*2;
3538ec21e2ecSJeff Kirsher 
353983bfc3c4SClaudiu Manoil 	/* For a station address of 0x12345678ABCD in transmission
354083bfc3c4SClaudiu Manoil 	 * order (BE), MACnADDR1 is set to 0xCDAB7856 and
354183bfc3c4SClaudiu Manoil 	 * MACnADDR2 is set to 0x34120000.
35420977f817SJan Ceuleers 	 */
354383bfc3c4SClaudiu Manoil 	tempval = (addr[5] << 24) | (addr[4] << 16) |
354483bfc3c4SClaudiu Manoil 		  (addr[3] << 8)  |  addr[2];
3545ec21e2ecSJeff Kirsher 
354683bfc3c4SClaudiu Manoil 	gfar_write(macptr, tempval);
3547ec21e2ecSJeff Kirsher 
354883bfc3c4SClaudiu Manoil 	tempval = (addr[1] << 24) | (addr[0] << 16);
3549ec21e2ecSJeff Kirsher 
3550ec21e2ecSJeff Kirsher 	gfar_write(macptr+1, tempval);
3551ec21e2ecSJeff Kirsher }
3552ec21e2ecSJeff Kirsher 
3553ec21e2ecSJeff Kirsher /* GFAR error interrupt handler */
3554ec21e2ecSJeff Kirsher static irqreturn_t gfar_error(int irq, void *grp_id)
3555ec21e2ecSJeff Kirsher {
3556ec21e2ecSJeff Kirsher 	struct gfar_priv_grp *gfargrp = grp_id;
3557ec21e2ecSJeff Kirsher 	struct gfar __iomem *regs = gfargrp->regs;
3558ec21e2ecSJeff Kirsher 	struct gfar_private *priv= gfargrp->priv;
3559ec21e2ecSJeff Kirsher 	struct net_device *dev = priv->ndev;
3560ec21e2ecSJeff Kirsher 
3561ec21e2ecSJeff Kirsher 	/* Save ievent for future reference */
3562ec21e2ecSJeff Kirsher 	u32 events = gfar_read(&regs->ievent);
3563ec21e2ecSJeff Kirsher 
3564ec21e2ecSJeff Kirsher 	/* Clear IEVENT */
3565ec21e2ecSJeff Kirsher 	gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
3566ec21e2ecSJeff Kirsher 
3567ec21e2ecSJeff Kirsher 	/* Magic Packet is not an error. */
3568ec21e2ecSJeff Kirsher 	if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
3569ec21e2ecSJeff Kirsher 	    (events & IEVENT_MAG))
3570ec21e2ecSJeff Kirsher 		events &= ~IEVENT_MAG;
3571ec21e2ecSJeff Kirsher 
3572ec21e2ecSJeff Kirsher 	/* Hmm... */
3573ec21e2ecSJeff Kirsher 	if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
3574bc4598bcSJan Ceuleers 		netdev_dbg(dev,
3575bc4598bcSJan Ceuleers 			   "error interrupt (ievent=0x%08x imask=0x%08x)\n",
3576ec21e2ecSJeff Kirsher 			   events, gfar_read(&regs->imask));
3577ec21e2ecSJeff Kirsher 
3578ec21e2ecSJeff Kirsher 	/* Update the error counters */
3579ec21e2ecSJeff Kirsher 	if (events & IEVENT_TXE) {
3580ec21e2ecSJeff Kirsher 		dev->stats.tx_errors++;
3581ec21e2ecSJeff Kirsher 
3582ec21e2ecSJeff Kirsher 		if (events & IEVENT_LC)
3583ec21e2ecSJeff Kirsher 			dev->stats.tx_window_errors++;
3584ec21e2ecSJeff Kirsher 		if (events & IEVENT_CRL)
3585ec21e2ecSJeff Kirsher 			dev->stats.tx_aborted_errors++;
3586ec21e2ecSJeff Kirsher 		if (events & IEVENT_XFUN) {
3587ec21e2ecSJeff Kirsher 			netif_dbg(priv, tx_err, dev,
3588ec21e2ecSJeff Kirsher 				  "TX FIFO underrun, packet dropped\n");
3589ec21e2ecSJeff Kirsher 			dev->stats.tx_dropped++;
3590212079dfSPaul Gortmaker 			atomic64_inc(&priv->extra_stats.tx_underrun);
3591ec21e2ecSJeff Kirsher 
3592bc602280SClaudiu Manoil 			schedule_work(&priv->reset_task);
3593ec21e2ecSJeff Kirsher 		}
3594ec21e2ecSJeff Kirsher 		netif_dbg(priv, tx_err, dev, "Transmit Error\n");
3595ec21e2ecSJeff Kirsher 	}
3596ec21e2ecSJeff Kirsher 	if (events & IEVENT_BSY) {
35971de65a5eSClaudiu Manoil 		dev->stats.rx_over_errors++;
3598212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.rx_bsy);
3599ec21e2ecSJeff Kirsher 
3600ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3601ec21e2ecSJeff Kirsher 			  gfar_read(&regs->rstat));
3602ec21e2ecSJeff Kirsher 	}
3603ec21e2ecSJeff Kirsher 	if (events & IEVENT_BABR) {
3604ec21e2ecSJeff Kirsher 		dev->stats.rx_errors++;
3605212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.rx_babr);
3606ec21e2ecSJeff Kirsher 
3607ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_err, dev, "babbling RX error\n");
3608ec21e2ecSJeff Kirsher 	}
3609ec21e2ecSJeff Kirsher 	if (events & IEVENT_EBERR) {
3610212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.eberr);
3611ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_err, dev, "bus error\n");
3612ec21e2ecSJeff Kirsher 	}
3613ec21e2ecSJeff Kirsher 	if (events & IEVENT_RXC)
3614ec21e2ecSJeff Kirsher 		netif_dbg(priv, rx_status, dev, "control frame\n");
3615ec21e2ecSJeff Kirsher 
3616ec21e2ecSJeff Kirsher 	if (events & IEVENT_BABT) {
3617212079dfSPaul Gortmaker 		atomic64_inc(&priv->extra_stats.tx_babt);
3618ec21e2ecSJeff Kirsher 		netif_dbg(priv, tx_err, dev, "babbling TX error\n");
3619ec21e2ecSJeff Kirsher 	}
3620ec21e2ecSJeff Kirsher 	return IRQ_HANDLED;
3621ec21e2ecSJeff Kirsher }
3622ec21e2ecSJeff Kirsher 
36236ce29b0eSClaudiu Manoil static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
36246ce29b0eSClaudiu Manoil {
36256ce29b0eSClaudiu Manoil 	struct phy_device *phydev = priv->phydev;
36266ce29b0eSClaudiu Manoil 	u32 val = 0;
36276ce29b0eSClaudiu Manoil 
36286ce29b0eSClaudiu Manoil 	if (!phydev->duplex)
36296ce29b0eSClaudiu Manoil 		return val;
36306ce29b0eSClaudiu Manoil 
36316ce29b0eSClaudiu Manoil 	if (!priv->pause_aneg_en) {
36326ce29b0eSClaudiu Manoil 		if (priv->tx_pause_en)
36336ce29b0eSClaudiu Manoil 			val |= MACCFG1_TX_FLOW;
36346ce29b0eSClaudiu Manoil 		if (priv->rx_pause_en)
36356ce29b0eSClaudiu Manoil 			val |= MACCFG1_RX_FLOW;
36366ce29b0eSClaudiu Manoil 	} else {
36376ce29b0eSClaudiu Manoil 		u16 lcl_adv, rmt_adv;
36386ce29b0eSClaudiu Manoil 		u8 flowctrl;
36396ce29b0eSClaudiu Manoil 		/* get link partner capabilities */
36406ce29b0eSClaudiu Manoil 		rmt_adv = 0;
36416ce29b0eSClaudiu Manoil 		if (phydev->pause)
36426ce29b0eSClaudiu Manoil 			rmt_adv = LPA_PAUSE_CAP;
36436ce29b0eSClaudiu Manoil 		if (phydev->asym_pause)
36446ce29b0eSClaudiu Manoil 			rmt_adv |= LPA_PAUSE_ASYM;
36456ce29b0eSClaudiu Manoil 
364643ef8d29SPavaluca Matei-B46610 		lcl_adv = 0;
364743ef8d29SPavaluca Matei-B46610 		if (phydev->advertising & ADVERTISED_Pause)
364843ef8d29SPavaluca Matei-B46610 			lcl_adv |= ADVERTISE_PAUSE_CAP;
364943ef8d29SPavaluca Matei-B46610 		if (phydev->advertising & ADVERTISED_Asym_Pause)
365043ef8d29SPavaluca Matei-B46610 			lcl_adv |= ADVERTISE_PAUSE_ASYM;
36516ce29b0eSClaudiu Manoil 
36526ce29b0eSClaudiu Manoil 		flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
36536ce29b0eSClaudiu Manoil 		if (flowctrl & FLOW_CTRL_TX)
36546ce29b0eSClaudiu Manoil 			val |= MACCFG1_TX_FLOW;
36556ce29b0eSClaudiu Manoil 		if (flowctrl & FLOW_CTRL_RX)
36566ce29b0eSClaudiu Manoil 			val |= MACCFG1_RX_FLOW;
36576ce29b0eSClaudiu Manoil 	}
36586ce29b0eSClaudiu Manoil 
36596ce29b0eSClaudiu Manoil 	return val;
36606ce29b0eSClaudiu Manoil }
36616ce29b0eSClaudiu Manoil 
36626ce29b0eSClaudiu Manoil static noinline void gfar_update_link_state(struct gfar_private *priv)
36636ce29b0eSClaudiu Manoil {
36646ce29b0eSClaudiu Manoil 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
36656ce29b0eSClaudiu Manoil 	struct phy_device *phydev = priv->phydev;
366645b679c9SMatei Pavaluca 	struct gfar_priv_rx_q *rx_queue = NULL;
366745b679c9SMatei Pavaluca 	int i;
36686ce29b0eSClaudiu Manoil 
36696ce29b0eSClaudiu Manoil 	if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
36706ce29b0eSClaudiu Manoil 		return;
36716ce29b0eSClaudiu Manoil 
36726ce29b0eSClaudiu Manoil 	if (phydev->link) {
36736ce29b0eSClaudiu Manoil 		u32 tempval1 = gfar_read(&regs->maccfg1);
36746ce29b0eSClaudiu Manoil 		u32 tempval = gfar_read(&regs->maccfg2);
36756ce29b0eSClaudiu Manoil 		u32 ecntrl = gfar_read(&regs->ecntrl);
367645b679c9SMatei Pavaluca 		u32 tx_flow_oldval = (tempval & MACCFG1_TX_FLOW);
36776ce29b0eSClaudiu Manoil 
36786ce29b0eSClaudiu Manoil 		if (phydev->duplex != priv->oldduplex) {
36796ce29b0eSClaudiu Manoil 			if (!(phydev->duplex))
36806ce29b0eSClaudiu Manoil 				tempval &= ~(MACCFG2_FULL_DUPLEX);
36816ce29b0eSClaudiu Manoil 			else
36826ce29b0eSClaudiu Manoil 				tempval |= MACCFG2_FULL_DUPLEX;
36836ce29b0eSClaudiu Manoil 
36846ce29b0eSClaudiu Manoil 			priv->oldduplex = phydev->duplex;
36856ce29b0eSClaudiu Manoil 		}
36866ce29b0eSClaudiu Manoil 
36876ce29b0eSClaudiu Manoil 		if (phydev->speed != priv->oldspeed) {
36886ce29b0eSClaudiu Manoil 			switch (phydev->speed) {
36896ce29b0eSClaudiu Manoil 			case 1000:
36906ce29b0eSClaudiu Manoil 				tempval =
36916ce29b0eSClaudiu Manoil 				    ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
36926ce29b0eSClaudiu Manoil 
36936ce29b0eSClaudiu Manoil 				ecntrl &= ~(ECNTRL_R100);
36946ce29b0eSClaudiu Manoil 				break;
36956ce29b0eSClaudiu Manoil 			case 100:
36966ce29b0eSClaudiu Manoil 			case 10:
36976ce29b0eSClaudiu Manoil 				tempval =
36986ce29b0eSClaudiu Manoil 				    ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
36996ce29b0eSClaudiu Manoil 
37006ce29b0eSClaudiu Manoil 				/* Reduced mode distinguishes
37016ce29b0eSClaudiu Manoil 				 * between 10 and 100
37026ce29b0eSClaudiu Manoil 				 */
37036ce29b0eSClaudiu Manoil 				if (phydev->speed == SPEED_100)
37046ce29b0eSClaudiu Manoil 					ecntrl |= ECNTRL_R100;
37056ce29b0eSClaudiu Manoil 				else
37066ce29b0eSClaudiu Manoil 					ecntrl &= ~(ECNTRL_R100);
37076ce29b0eSClaudiu Manoil 				break;
37086ce29b0eSClaudiu Manoil 			default:
37096ce29b0eSClaudiu Manoil 				netif_warn(priv, link, priv->ndev,
37106ce29b0eSClaudiu Manoil 					   "Ack!  Speed (%d) is not 10/100/1000!\n",
37116ce29b0eSClaudiu Manoil 					   phydev->speed);
37126ce29b0eSClaudiu Manoil 				break;
37136ce29b0eSClaudiu Manoil 			}
37146ce29b0eSClaudiu Manoil 
37156ce29b0eSClaudiu Manoil 			priv->oldspeed = phydev->speed;
37166ce29b0eSClaudiu Manoil 		}
37176ce29b0eSClaudiu Manoil 
37186ce29b0eSClaudiu Manoil 		tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
37196ce29b0eSClaudiu Manoil 		tempval1 |= gfar_get_flowctrl_cfg(priv);
37206ce29b0eSClaudiu Manoil 
372145b679c9SMatei Pavaluca 		/* Turn last free buffer recording on */
372245b679c9SMatei Pavaluca 		if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) {
372345b679c9SMatei Pavaluca 			for (i = 0; i < priv->num_rx_queues; i++) {
3724b4b67f26SScott Wood 				u32 bdp_dma;
3725b4b67f26SScott Wood 
372645b679c9SMatei Pavaluca 				rx_queue = priv->rx_queue[i];
3727b4b67f26SScott Wood 				bdp_dma = gfar_rxbd_dma_lastfree(rx_queue);
3728b4b67f26SScott Wood 				gfar_write(rx_queue->rfbptr, bdp_dma);
372945b679c9SMatei Pavaluca 			}
373045b679c9SMatei Pavaluca 
373145b679c9SMatei Pavaluca 			priv->tx_actual_en = 1;
373245b679c9SMatei Pavaluca 		}
373345b679c9SMatei Pavaluca 
373445b679c9SMatei Pavaluca 		if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval))
373545b679c9SMatei Pavaluca 			priv->tx_actual_en = 0;
373645b679c9SMatei Pavaluca 
37376ce29b0eSClaudiu Manoil 		gfar_write(&regs->maccfg1, tempval1);
37386ce29b0eSClaudiu Manoil 		gfar_write(&regs->maccfg2, tempval);
37396ce29b0eSClaudiu Manoil 		gfar_write(&regs->ecntrl, ecntrl);
37406ce29b0eSClaudiu Manoil 
37416ce29b0eSClaudiu Manoil 		if (!priv->oldlink)
37426ce29b0eSClaudiu Manoil 			priv->oldlink = 1;
37436ce29b0eSClaudiu Manoil 
37446ce29b0eSClaudiu Manoil 	} else if (priv->oldlink) {
37456ce29b0eSClaudiu Manoil 		priv->oldlink = 0;
37466ce29b0eSClaudiu Manoil 		priv->oldspeed = 0;
37476ce29b0eSClaudiu Manoil 		priv->oldduplex = -1;
37486ce29b0eSClaudiu Manoil 	}
37496ce29b0eSClaudiu Manoil 
37506ce29b0eSClaudiu Manoil 	if (netif_msg_link(priv))
37516ce29b0eSClaudiu Manoil 		phy_print_status(phydev);
37526ce29b0eSClaudiu Manoil }
37536ce29b0eSClaudiu Manoil 
375494e5a2a8SFabian Frederick static const struct of_device_id gfar_match[] =
3755ec21e2ecSJeff Kirsher {
3756ec21e2ecSJeff Kirsher 	{
3757ec21e2ecSJeff Kirsher 		.type = "network",
3758ec21e2ecSJeff Kirsher 		.compatible = "gianfar",
3759ec21e2ecSJeff Kirsher 	},
3760ec21e2ecSJeff Kirsher 	{
3761ec21e2ecSJeff Kirsher 		.compatible = "fsl,etsec2",
3762ec21e2ecSJeff Kirsher 	},
3763ec21e2ecSJeff Kirsher 	{},
3764ec21e2ecSJeff Kirsher };
3765ec21e2ecSJeff Kirsher MODULE_DEVICE_TABLE(of, gfar_match);
3766ec21e2ecSJeff Kirsher 
3767ec21e2ecSJeff Kirsher /* Structure for a device driver */
3768ec21e2ecSJeff Kirsher static struct platform_driver gfar_driver = {
3769ec21e2ecSJeff Kirsher 	.driver = {
3770ec21e2ecSJeff Kirsher 		.name = "fsl-gianfar",
3771ec21e2ecSJeff Kirsher 		.pm = GFAR_PM_OPS,
3772ec21e2ecSJeff Kirsher 		.of_match_table = gfar_match,
3773ec21e2ecSJeff Kirsher 	},
3774ec21e2ecSJeff Kirsher 	.probe = gfar_probe,
3775ec21e2ecSJeff Kirsher 	.remove = gfar_remove,
3776ec21e2ecSJeff Kirsher };
3777ec21e2ecSJeff Kirsher 
3778db62f684SAxel Lin module_platform_driver(gfar_driver);
3779