xref: /openbmc/linux/drivers/net/ethernet/freescale/gianfar.c (revision 71ff9e3df7e1c5d3293af6b595309124e8c97412)
1 /* drivers/net/ethernet/freescale/gianfar.c
2  *
3  * Gianfar Ethernet Driver
4  * This driver is designed for the non-CPM ethernet controllers
5  * on the 85xx and 83xx family of integrated processors
6  * Based on 8260_io/fcc_enet.c
7  *
8  * Author: Andy Fleming
9  * Maintainer: Kumar Gala
10  * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
11  *
12  * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc.
13  * Copyright 2007 MontaVista Software, Inc.
14  *
15  * This program is free software; you can redistribute  it and/or modify it
16  * under  the terms of  the GNU General  Public License as published by the
17  * Free Software Foundation;  either version 2 of the  License, or (at your
18  * option) any later version.
19  *
20  *  Gianfar:  AKA Lambda Draconis, "Dragon"
21  *  RA 11 31 24.2
22  *  Dec +69 19 52
23  *  V 3.84
24  *  B-V +1.62
25  *
26  *  Theory of operation
27  *
28  *  The driver is initialized through of_device. Configuration information
29  *  is therefore conveyed through an OF-style device tree.
30  *
31  *  The Gianfar Ethernet Controller uses a ring of buffer
32  *  descriptors.  The beginning is indicated by a register
33  *  pointing to the physical address of the start of the ring.
34  *  The end is determined by a "wrap" bit being set in the
35  *  last descriptor of the ring.
36  *
37  *  When a packet is received, the RXF bit in the
38  *  IEVENT register is set, triggering an interrupt when the
39  *  corresponding bit in the IMASK register is also set (if
40  *  interrupt coalescing is active, then the interrupt may not
41  *  happen immediately, but will wait until either a set number
42  *  of frames or amount of time have passed).  In NAPI, the
43  *  interrupt handler will signal there is work to be done, and
44  *  exit. This method will start at the last known empty
45  *  descriptor, and process every subsequent descriptor until there
46  *  are none left with data (NAPI will stop after a set number of
47  *  packets to give time to other tasks, but will eventually
48  *  process all the packets).  The data arrives inside a
49  *  pre-allocated skb, and so after the skb is passed up to the
50  *  stack, a new skb must be allocated, and the address field in
51  *  the buffer descriptor must be updated to indicate this new
52  *  skb.
53  *
54  *  When the kernel requests that a packet be transmitted, the
55  *  driver starts where it left off last time, and points the
56  *  descriptor at the buffer which was passed in.  The driver
57  *  then informs the DMA engine that there are packets ready to
58  *  be transmitted.  Once the controller is finished transmitting
59  *  the packet, an interrupt may be triggered (under the same
60  *  conditions as for reception, but depending on the TXF bit).
61  *  The driver then cleans up the buffer.
62  */
63 
64 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
65 #define DEBUG
66 
67 #include <linux/kernel.h>
68 #include <linux/string.h>
69 #include <linux/errno.h>
70 #include <linux/unistd.h>
71 #include <linux/slab.h>
72 #include <linux/interrupt.h>
73 #include <linux/delay.h>
74 #include <linux/netdevice.h>
75 #include <linux/etherdevice.h>
76 #include <linux/skbuff.h>
77 #include <linux/if_vlan.h>
78 #include <linux/spinlock.h>
79 #include <linux/mm.h>
80 #include <linux/of_address.h>
81 #include <linux/of_irq.h>
82 #include <linux/of_mdio.h>
83 #include <linux/of_platform.h>
84 #include <linux/ip.h>
85 #include <linux/tcp.h>
86 #include <linux/udp.h>
87 #include <linux/in.h>
88 #include <linux/net_tstamp.h>
89 
90 #include <asm/io.h>
91 #include <asm/reg.h>
92 #include <asm/mpc85xx.h>
93 #include <asm/irq.h>
94 #include <asm/uaccess.h>
95 #include <linux/module.h>
96 #include <linux/dma-mapping.h>
97 #include <linux/crc32.h>
98 #include <linux/mii.h>
99 #include <linux/phy.h>
100 #include <linux/phy_fixed.h>
101 #include <linux/of.h>
102 #include <linux/of_net.h>
103 
104 #include "gianfar.h"
105 
106 #define TX_TIMEOUT      (1*HZ)
107 
108 const char gfar_driver_version[] = "1.3";
109 
110 static int gfar_enet_open(struct net_device *dev);
111 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
112 static void gfar_reset_task(struct work_struct *work);
113 static void gfar_timeout(struct net_device *dev);
114 static int gfar_close(struct net_device *dev);
115 struct sk_buff *gfar_new_skb(struct net_device *dev);
116 static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
117 			   struct sk_buff *skb);
118 static int gfar_set_mac_address(struct net_device *dev);
119 static int gfar_change_mtu(struct net_device *dev, int new_mtu);
120 static irqreturn_t gfar_error(int irq, void *dev_id);
121 static irqreturn_t gfar_transmit(int irq, void *dev_id);
122 static irqreturn_t gfar_interrupt(int irq, void *dev_id);
123 static void adjust_link(struct net_device *dev);
124 static int init_phy(struct net_device *dev);
125 static int gfar_probe(struct platform_device *ofdev);
126 static int gfar_remove(struct platform_device *ofdev);
127 static void free_skb_resources(struct gfar_private *priv);
128 static void gfar_set_multi(struct net_device *dev);
129 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
130 static void gfar_configure_serdes(struct net_device *dev);
131 static int gfar_poll_rx(struct napi_struct *napi, int budget);
132 static int gfar_poll_tx(struct napi_struct *napi, int budget);
133 static int gfar_poll_rx_sq(struct napi_struct *napi, int budget);
134 static int gfar_poll_tx_sq(struct napi_struct *napi, int budget);
135 #ifdef CONFIG_NET_POLL_CONTROLLER
136 static void gfar_netpoll(struct net_device *dev);
137 #endif
138 int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
139 static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
140 static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
141 			       int amount_pull, struct napi_struct *napi);
142 static void gfar_halt_nodisable(struct gfar_private *priv);
143 static void gfar_clear_exact_match(struct net_device *dev);
144 static void gfar_set_mac_for_addr(struct net_device *dev, int num,
145 				  const u8 *addr);
146 static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
147 
148 MODULE_AUTHOR("Freescale Semiconductor, Inc");
149 MODULE_DESCRIPTION("Gianfar Ethernet Driver");
150 MODULE_LICENSE("GPL");
151 
152 static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
153 			    dma_addr_t buf)
154 {
155 	u32 lstatus;
156 
157 	bdp->bufPtr = buf;
158 
159 	lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
160 	if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
161 		lstatus |= BD_LFLAG(RXBD_WRAP);
162 
163 	eieio();
164 
165 	bdp->lstatus = lstatus;
166 }
167 
168 static int gfar_init_bds(struct net_device *ndev)
169 {
170 	struct gfar_private *priv = netdev_priv(ndev);
171 	struct gfar_priv_tx_q *tx_queue = NULL;
172 	struct gfar_priv_rx_q *rx_queue = NULL;
173 	struct txbd8 *txbdp;
174 	struct rxbd8 *rxbdp;
175 	int i, j;
176 
177 	for (i = 0; i < priv->num_tx_queues; i++) {
178 		tx_queue = priv->tx_queue[i];
179 		/* Initialize some variables in our dev structure */
180 		tx_queue->num_txbdfree = tx_queue->tx_ring_size;
181 		tx_queue->dirty_tx = tx_queue->tx_bd_base;
182 		tx_queue->cur_tx = tx_queue->tx_bd_base;
183 		tx_queue->skb_curtx = 0;
184 		tx_queue->skb_dirtytx = 0;
185 
186 		/* Initialize Transmit Descriptor Ring */
187 		txbdp = tx_queue->tx_bd_base;
188 		for (j = 0; j < tx_queue->tx_ring_size; j++) {
189 			txbdp->lstatus = 0;
190 			txbdp->bufPtr = 0;
191 			txbdp++;
192 		}
193 
194 		/* Set the last descriptor in the ring to indicate wrap */
195 		txbdp--;
196 		txbdp->status |= TXBD_WRAP;
197 	}
198 
199 	for (i = 0; i < priv->num_rx_queues; i++) {
200 		rx_queue = priv->rx_queue[i];
201 		rx_queue->cur_rx = rx_queue->rx_bd_base;
202 		rx_queue->skb_currx = 0;
203 		rxbdp = rx_queue->rx_bd_base;
204 
205 		for (j = 0; j < rx_queue->rx_ring_size; j++) {
206 			struct sk_buff *skb = rx_queue->rx_skbuff[j];
207 
208 			if (skb) {
209 				gfar_init_rxbdp(rx_queue, rxbdp,
210 						rxbdp->bufPtr);
211 			} else {
212 				skb = gfar_new_skb(ndev);
213 				if (!skb) {
214 					netdev_err(ndev, "Can't allocate RX buffers\n");
215 					return -ENOMEM;
216 				}
217 				rx_queue->rx_skbuff[j] = skb;
218 
219 				gfar_new_rxbdp(rx_queue, rxbdp, skb);
220 			}
221 
222 			rxbdp++;
223 		}
224 
225 	}
226 
227 	return 0;
228 }
229 
230 static int gfar_alloc_skb_resources(struct net_device *ndev)
231 {
232 	void *vaddr;
233 	dma_addr_t addr;
234 	int i, j, k;
235 	struct gfar_private *priv = netdev_priv(ndev);
236 	struct device *dev = priv->dev;
237 	struct gfar_priv_tx_q *tx_queue = NULL;
238 	struct gfar_priv_rx_q *rx_queue = NULL;
239 
240 	priv->total_tx_ring_size = 0;
241 	for (i = 0; i < priv->num_tx_queues; i++)
242 		priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
243 
244 	priv->total_rx_ring_size = 0;
245 	for (i = 0; i < priv->num_rx_queues; i++)
246 		priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
247 
248 	/* Allocate memory for the buffer descriptors */
249 	vaddr = dma_alloc_coherent(dev,
250 				   (priv->total_tx_ring_size *
251 				    sizeof(struct txbd8)) +
252 				   (priv->total_rx_ring_size *
253 				    sizeof(struct rxbd8)),
254 				   &addr, GFP_KERNEL);
255 	if (!vaddr)
256 		return -ENOMEM;
257 
258 	for (i = 0; i < priv->num_tx_queues; i++) {
259 		tx_queue = priv->tx_queue[i];
260 		tx_queue->tx_bd_base = vaddr;
261 		tx_queue->tx_bd_dma_base = addr;
262 		tx_queue->dev = ndev;
263 		/* enet DMA only understands physical addresses */
264 		addr  += sizeof(struct txbd8) * tx_queue->tx_ring_size;
265 		vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
266 	}
267 
268 	/* Start the rx descriptor ring where the tx ring leaves off */
269 	for (i = 0; i < priv->num_rx_queues; i++) {
270 		rx_queue = priv->rx_queue[i];
271 		rx_queue->rx_bd_base = vaddr;
272 		rx_queue->rx_bd_dma_base = addr;
273 		rx_queue->dev = ndev;
274 		addr  += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
275 		vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
276 	}
277 
278 	/* Setup the skbuff rings */
279 	for (i = 0; i < priv->num_tx_queues; i++) {
280 		tx_queue = priv->tx_queue[i];
281 		tx_queue->tx_skbuff =
282 			kmalloc_array(tx_queue->tx_ring_size,
283 				      sizeof(*tx_queue->tx_skbuff),
284 				      GFP_KERNEL);
285 		if (!tx_queue->tx_skbuff)
286 			goto cleanup;
287 
288 		for (k = 0; k < tx_queue->tx_ring_size; k++)
289 			tx_queue->tx_skbuff[k] = NULL;
290 	}
291 
292 	for (i = 0; i < priv->num_rx_queues; i++) {
293 		rx_queue = priv->rx_queue[i];
294 		rx_queue->rx_skbuff =
295 			kmalloc_array(rx_queue->rx_ring_size,
296 				      sizeof(*rx_queue->rx_skbuff),
297 				      GFP_KERNEL);
298 		if (!rx_queue->rx_skbuff)
299 			goto cleanup;
300 
301 		for (j = 0; j < rx_queue->rx_ring_size; j++)
302 			rx_queue->rx_skbuff[j] = NULL;
303 	}
304 
305 	if (gfar_init_bds(ndev))
306 		goto cleanup;
307 
308 	return 0;
309 
310 cleanup:
311 	free_skb_resources(priv);
312 	return -ENOMEM;
313 }
314 
315 static void gfar_init_tx_rx_base(struct gfar_private *priv)
316 {
317 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
318 	u32 __iomem *baddr;
319 	int i;
320 
321 	baddr = &regs->tbase0;
322 	for (i = 0; i < priv->num_tx_queues; i++) {
323 		gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
324 		baddr += 2;
325 	}
326 
327 	baddr = &regs->rbase0;
328 	for (i = 0; i < priv->num_rx_queues; i++) {
329 		gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
330 		baddr += 2;
331 	}
332 }
333 
334 static void gfar_rx_buff_size_config(struct gfar_private *priv)
335 {
336 	int frame_size = priv->ndev->mtu + ETH_HLEN;
337 
338 	/* set this when rx hw offload (TOE) functions are being used */
339 	priv->uses_rxfcb = 0;
340 
341 	if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
342 		priv->uses_rxfcb = 1;
343 
344 	if (priv->hwts_rx_en)
345 		priv->uses_rxfcb = 1;
346 
347 	if (priv->uses_rxfcb)
348 		frame_size += GMAC_FCB_LEN;
349 
350 	frame_size += priv->padding;
351 
352 	frame_size = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
353 		     INCREMENTAL_BUFFER_SIZE;
354 
355 	priv->rx_buffer_size = frame_size;
356 }
357 
358 static void gfar_mac_rx_config(struct gfar_private *priv)
359 {
360 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
361 	u32 rctrl = 0;
362 
363 	if (priv->rx_filer_enable) {
364 		rctrl |= RCTRL_FILREN;
365 		/* Program the RIR0 reg with the required distribution */
366 		if (priv->poll_mode == GFAR_SQ_POLLING)
367 			gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
368 		else /* GFAR_MQ_POLLING */
369 			gfar_write(&regs->rir0, DEFAULT_8RXQ_RIR0);
370 	}
371 
372 	/* Restore PROMISC mode */
373 	if (priv->ndev->flags & IFF_PROMISC)
374 		rctrl |= RCTRL_PROM;
375 
376 	if (priv->ndev->features & NETIF_F_RXCSUM)
377 		rctrl |= RCTRL_CHECKSUMMING;
378 
379 	if (priv->extended_hash)
380 		rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
381 
382 	if (priv->padding) {
383 		rctrl &= ~RCTRL_PAL_MASK;
384 		rctrl |= RCTRL_PADDING(priv->padding);
385 	}
386 
387 	/* Enable HW time stamping if requested from user space */
388 	if (priv->hwts_rx_en)
389 		rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
390 
391 	if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
392 		rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
393 
394 	/* Init rctrl based on our settings */
395 	gfar_write(&regs->rctrl, rctrl);
396 }
397 
398 static void gfar_mac_tx_config(struct gfar_private *priv)
399 {
400 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
401 	u32 tctrl = 0;
402 
403 	if (priv->ndev->features & NETIF_F_IP_CSUM)
404 		tctrl |= TCTRL_INIT_CSUM;
405 
406 	if (priv->prio_sched_en)
407 		tctrl |= TCTRL_TXSCHED_PRIO;
408 	else {
409 		tctrl |= TCTRL_TXSCHED_WRRS;
410 		gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
411 		gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
412 	}
413 
414 	if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
415 		tctrl |= TCTRL_VLINS;
416 
417 	gfar_write(&regs->tctrl, tctrl);
418 }
419 
420 static void gfar_configure_coalescing(struct gfar_private *priv,
421 			       unsigned long tx_mask, unsigned long rx_mask)
422 {
423 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
424 	u32 __iomem *baddr;
425 
426 	if (priv->mode == MQ_MG_MODE) {
427 		int i = 0;
428 
429 		baddr = &regs->txic0;
430 		for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
431 			gfar_write(baddr + i, 0);
432 			if (likely(priv->tx_queue[i]->txcoalescing))
433 				gfar_write(baddr + i, priv->tx_queue[i]->txic);
434 		}
435 
436 		baddr = &regs->rxic0;
437 		for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
438 			gfar_write(baddr + i, 0);
439 			if (likely(priv->rx_queue[i]->rxcoalescing))
440 				gfar_write(baddr + i, priv->rx_queue[i]->rxic);
441 		}
442 	} else {
443 		/* Backward compatible case -- even if we enable
444 		 * multiple queues, there's only single reg to program
445 		 */
446 		gfar_write(&regs->txic, 0);
447 		if (likely(priv->tx_queue[0]->txcoalescing))
448 			gfar_write(&regs->txic, priv->tx_queue[0]->txic);
449 
450 		gfar_write(&regs->rxic, 0);
451 		if (unlikely(priv->rx_queue[0]->rxcoalescing))
452 			gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
453 	}
454 }
455 
456 void gfar_configure_coalescing_all(struct gfar_private *priv)
457 {
458 	gfar_configure_coalescing(priv, 0xFF, 0xFF);
459 }
460 
461 static struct net_device_stats *gfar_get_stats(struct net_device *dev)
462 {
463 	struct gfar_private *priv = netdev_priv(dev);
464 	unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
465 	unsigned long tx_packets = 0, tx_bytes = 0;
466 	int i;
467 
468 	for (i = 0; i < priv->num_rx_queues; i++) {
469 		rx_packets += priv->rx_queue[i]->stats.rx_packets;
470 		rx_bytes   += priv->rx_queue[i]->stats.rx_bytes;
471 		rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
472 	}
473 
474 	dev->stats.rx_packets = rx_packets;
475 	dev->stats.rx_bytes   = rx_bytes;
476 	dev->stats.rx_dropped = rx_dropped;
477 
478 	for (i = 0; i < priv->num_tx_queues; i++) {
479 		tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
480 		tx_packets += priv->tx_queue[i]->stats.tx_packets;
481 	}
482 
483 	dev->stats.tx_bytes   = tx_bytes;
484 	dev->stats.tx_packets = tx_packets;
485 
486 	return &dev->stats;
487 }
488 
489 static const struct net_device_ops gfar_netdev_ops = {
490 	.ndo_open = gfar_enet_open,
491 	.ndo_start_xmit = gfar_start_xmit,
492 	.ndo_stop = gfar_close,
493 	.ndo_change_mtu = gfar_change_mtu,
494 	.ndo_set_features = gfar_set_features,
495 	.ndo_set_rx_mode = gfar_set_multi,
496 	.ndo_tx_timeout = gfar_timeout,
497 	.ndo_do_ioctl = gfar_ioctl,
498 	.ndo_get_stats = gfar_get_stats,
499 	.ndo_set_mac_address = eth_mac_addr,
500 	.ndo_validate_addr = eth_validate_addr,
501 #ifdef CONFIG_NET_POLL_CONTROLLER
502 	.ndo_poll_controller = gfar_netpoll,
503 #endif
504 };
505 
506 static void gfar_ints_disable(struct gfar_private *priv)
507 {
508 	int i;
509 	for (i = 0; i < priv->num_grps; i++) {
510 		struct gfar __iomem *regs = priv->gfargrp[i].regs;
511 		/* Clear IEVENT */
512 		gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
513 
514 		/* Initialize IMASK */
515 		gfar_write(&regs->imask, IMASK_INIT_CLEAR);
516 	}
517 }
518 
519 static void gfar_ints_enable(struct gfar_private *priv)
520 {
521 	int i;
522 	for (i = 0; i < priv->num_grps; i++) {
523 		struct gfar __iomem *regs = priv->gfargrp[i].regs;
524 		/* Unmask the interrupts we look for */
525 		gfar_write(&regs->imask, IMASK_DEFAULT);
526 	}
527 }
528 
529 void lock_tx_qs(struct gfar_private *priv)
530 {
531 	int i;
532 
533 	for (i = 0; i < priv->num_tx_queues; i++)
534 		spin_lock(&priv->tx_queue[i]->txlock);
535 }
536 
537 void unlock_tx_qs(struct gfar_private *priv)
538 {
539 	int i;
540 
541 	for (i = 0; i < priv->num_tx_queues; i++)
542 		spin_unlock(&priv->tx_queue[i]->txlock);
543 }
544 
545 static int gfar_alloc_tx_queues(struct gfar_private *priv)
546 {
547 	int i;
548 
549 	for (i = 0; i < priv->num_tx_queues; i++) {
550 		priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
551 					    GFP_KERNEL);
552 		if (!priv->tx_queue[i])
553 			return -ENOMEM;
554 
555 		priv->tx_queue[i]->tx_skbuff = NULL;
556 		priv->tx_queue[i]->qindex = i;
557 		priv->tx_queue[i]->dev = priv->ndev;
558 		spin_lock_init(&(priv->tx_queue[i]->txlock));
559 	}
560 	return 0;
561 }
562 
563 static int gfar_alloc_rx_queues(struct gfar_private *priv)
564 {
565 	int i;
566 
567 	for (i = 0; i < priv->num_rx_queues; i++) {
568 		priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
569 					    GFP_KERNEL);
570 		if (!priv->rx_queue[i])
571 			return -ENOMEM;
572 
573 		priv->rx_queue[i]->rx_skbuff = NULL;
574 		priv->rx_queue[i]->qindex = i;
575 		priv->rx_queue[i]->dev = priv->ndev;
576 	}
577 	return 0;
578 }
579 
580 static void gfar_free_tx_queues(struct gfar_private *priv)
581 {
582 	int i;
583 
584 	for (i = 0; i < priv->num_tx_queues; i++)
585 		kfree(priv->tx_queue[i]);
586 }
587 
588 static void gfar_free_rx_queues(struct gfar_private *priv)
589 {
590 	int i;
591 
592 	for (i = 0; i < priv->num_rx_queues; i++)
593 		kfree(priv->rx_queue[i]);
594 }
595 
596 static void unmap_group_regs(struct gfar_private *priv)
597 {
598 	int i;
599 
600 	for (i = 0; i < MAXGROUPS; i++)
601 		if (priv->gfargrp[i].regs)
602 			iounmap(priv->gfargrp[i].regs);
603 }
604 
605 static void free_gfar_dev(struct gfar_private *priv)
606 {
607 	int i, j;
608 
609 	for (i = 0; i < priv->num_grps; i++)
610 		for (j = 0; j < GFAR_NUM_IRQS; j++) {
611 			kfree(priv->gfargrp[i].irqinfo[j]);
612 			priv->gfargrp[i].irqinfo[j] = NULL;
613 		}
614 
615 	free_netdev(priv->ndev);
616 }
617 
618 static void disable_napi(struct gfar_private *priv)
619 {
620 	int i;
621 
622 	for (i = 0; i < priv->num_grps; i++) {
623 		napi_disable(&priv->gfargrp[i].napi_rx);
624 		napi_disable(&priv->gfargrp[i].napi_tx);
625 	}
626 }
627 
628 static void enable_napi(struct gfar_private *priv)
629 {
630 	int i;
631 
632 	for (i = 0; i < priv->num_grps; i++) {
633 		napi_enable(&priv->gfargrp[i].napi_rx);
634 		napi_enable(&priv->gfargrp[i].napi_tx);
635 	}
636 }
637 
638 static int gfar_parse_group(struct device_node *np,
639 			    struct gfar_private *priv, const char *model)
640 {
641 	struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
642 	int i;
643 
644 	for (i = 0; i < GFAR_NUM_IRQS; i++) {
645 		grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
646 					  GFP_KERNEL);
647 		if (!grp->irqinfo[i])
648 			return -ENOMEM;
649 	}
650 
651 	grp->regs = of_iomap(np, 0);
652 	if (!grp->regs)
653 		return -ENOMEM;
654 
655 	gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
656 
657 	/* If we aren't the FEC we have multiple interrupts */
658 	if (model && strcasecmp(model, "FEC")) {
659 		gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
660 		gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
661 		if (gfar_irq(grp, TX)->irq == NO_IRQ ||
662 		    gfar_irq(grp, RX)->irq == NO_IRQ ||
663 		    gfar_irq(grp, ER)->irq == NO_IRQ)
664 			return -EINVAL;
665 	}
666 
667 	grp->priv = priv;
668 	spin_lock_init(&grp->grplock);
669 	if (priv->mode == MQ_MG_MODE) {
670 		u32 *rxq_mask, *txq_mask;
671 		rxq_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL);
672 		txq_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL);
673 
674 		if (priv->poll_mode == GFAR_SQ_POLLING) {
675 			/* One Q per interrupt group: Q0 to G0, Q1 to G1 */
676 			grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
677 			grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
678 		} else { /* GFAR_MQ_POLLING */
679 			grp->rx_bit_map = rxq_mask ?
680 			*rxq_mask : (DEFAULT_MAPPING >> priv->num_grps);
681 			grp->tx_bit_map = txq_mask ?
682 			*txq_mask : (DEFAULT_MAPPING >> priv->num_grps);
683 		}
684 	} else {
685 		grp->rx_bit_map = 0xFF;
686 		grp->tx_bit_map = 0xFF;
687 	}
688 
689 	/* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
690 	 * right to left, so we need to revert the 8 bits to get the q index
691 	 */
692 	grp->rx_bit_map = bitrev8(grp->rx_bit_map);
693 	grp->tx_bit_map = bitrev8(grp->tx_bit_map);
694 
695 	/* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
696 	 * also assign queues to groups
697 	 */
698 	for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
699 		if (!grp->rx_queue)
700 			grp->rx_queue = priv->rx_queue[i];
701 		grp->num_rx_queues++;
702 		grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
703 		priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
704 		priv->rx_queue[i]->grp = grp;
705 	}
706 
707 	for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
708 		if (!grp->tx_queue)
709 			grp->tx_queue = priv->tx_queue[i];
710 		grp->num_tx_queues++;
711 		grp->tstat |= (TSTAT_CLEAR_THALT >> i);
712 		priv->tqueue |= (TQUEUE_EN0 >> i);
713 		priv->tx_queue[i]->grp = grp;
714 	}
715 
716 	priv->num_grps++;
717 
718 	return 0;
719 }
720 
721 static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
722 {
723 	const char *model;
724 	const char *ctype;
725 	const void *mac_addr;
726 	int err = 0, i;
727 	struct net_device *dev = NULL;
728 	struct gfar_private *priv = NULL;
729 	struct device_node *np = ofdev->dev.of_node;
730 	struct device_node *child = NULL;
731 	const u32 *stash;
732 	const u32 *stash_len;
733 	const u32 *stash_idx;
734 	unsigned int num_tx_qs, num_rx_qs;
735 	u32 *tx_queues, *rx_queues;
736 
737 	if (!np || !of_device_is_available(np))
738 		return -ENODEV;
739 
740 	/* parse the num of HW tx and rx queues */
741 	tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
742 	rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
743 
744 	if (priv->mode == SQ_SG_MODE) {
745 		num_tx_qs = 1;
746 		num_rx_qs = 1;
747 	} else { /* MQ_MG_MODE */
748 		if (priv->poll_mode == GFAR_SQ_POLLING) {
749 			num_tx_qs = 2; /* one q per int group */
750 			num_rx_qs = 2; /* one q per int group */
751 		} else { /* GFAR_MQ_POLLING */
752 			num_tx_qs = tx_queues ? *tx_queues : 1;
753 			num_rx_qs = rx_queues ? *rx_queues : 1;
754 		}
755 	}
756 
757 	if (num_tx_qs > MAX_TX_QS) {
758 		pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
759 		       num_tx_qs, MAX_TX_QS);
760 		pr_err("Cannot do alloc_etherdev, aborting\n");
761 		return -EINVAL;
762 	}
763 
764 	if (num_rx_qs > MAX_RX_QS) {
765 		pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
766 		       num_rx_qs, MAX_RX_QS);
767 		pr_err("Cannot do alloc_etherdev, aborting\n");
768 		return -EINVAL;
769 	}
770 
771 	*pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
772 	dev = *pdev;
773 	if (NULL == dev)
774 		return -ENOMEM;
775 
776 	priv = netdev_priv(dev);
777 	priv->ndev = dev;
778 
779 	priv->num_tx_queues = num_tx_qs;
780 	netif_set_real_num_rx_queues(dev, num_rx_qs);
781 	priv->num_rx_queues = num_rx_qs;
782 
783 	err = gfar_alloc_tx_queues(priv);
784 	if (err)
785 		goto tx_alloc_failed;
786 
787 	err = gfar_alloc_rx_queues(priv);
788 	if (err)
789 		goto rx_alloc_failed;
790 
791 	/* Init Rx queue filer rule set linked list */
792 	INIT_LIST_HEAD(&priv->rx_list.list);
793 	priv->rx_list.count = 0;
794 	mutex_init(&priv->rx_queue_access);
795 
796 	model = of_get_property(np, "model", NULL);
797 
798 	for (i = 0; i < MAXGROUPS; i++)
799 		priv->gfargrp[i].regs = NULL;
800 
801 	/* Parse and initialize group specific information */
802 	if (of_device_is_compatible(np, "fsl,etsec2")) {
803 		priv->mode = MQ_MG_MODE;
804 		priv->poll_mode = GFAR_SQ_POLLING;
805 		for_each_child_of_node(np, child) {
806 			err = gfar_parse_group(child, priv, model);
807 			if (err)
808 				goto err_grp_init;
809 		}
810 	} else {
811 		priv->mode = SQ_SG_MODE;
812 		priv->poll_mode = GFAR_SQ_POLLING;
813 		err = gfar_parse_group(np, priv, model);
814 		if (err)
815 			goto err_grp_init;
816 	}
817 
818 	stash = of_get_property(np, "bd-stash", NULL);
819 
820 	if (stash) {
821 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
822 		priv->bd_stash_en = 1;
823 	}
824 
825 	stash_len = of_get_property(np, "rx-stash-len", NULL);
826 
827 	if (stash_len)
828 		priv->rx_stash_size = *stash_len;
829 
830 	stash_idx = of_get_property(np, "rx-stash-idx", NULL);
831 
832 	if (stash_idx)
833 		priv->rx_stash_index = *stash_idx;
834 
835 	if (stash_len || stash_idx)
836 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
837 
838 	mac_addr = of_get_mac_address(np);
839 
840 	if (mac_addr)
841 		memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
842 
843 	if (model && !strcasecmp(model, "TSEC"))
844 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
845 				     FSL_GIANFAR_DEV_HAS_COALESCE |
846 				     FSL_GIANFAR_DEV_HAS_RMON |
847 				     FSL_GIANFAR_DEV_HAS_MULTI_INTR;
848 
849 	if (model && !strcasecmp(model, "eTSEC"))
850 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
851 				     FSL_GIANFAR_DEV_HAS_COALESCE |
852 				     FSL_GIANFAR_DEV_HAS_RMON |
853 				     FSL_GIANFAR_DEV_HAS_MULTI_INTR |
854 				     FSL_GIANFAR_DEV_HAS_CSUM |
855 				     FSL_GIANFAR_DEV_HAS_VLAN |
856 				     FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
857 				     FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
858 				     FSL_GIANFAR_DEV_HAS_TIMER;
859 
860 	ctype = of_get_property(np, "phy-connection-type", NULL);
861 
862 	/* We only care about rgmii-id.  The rest are autodetected */
863 	if (ctype && !strcmp(ctype, "rgmii-id"))
864 		priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
865 	else
866 		priv->interface = PHY_INTERFACE_MODE_MII;
867 
868 	if (of_get_property(np, "fsl,magic-packet", NULL))
869 		priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
870 
871 	priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
872 
873 	/* Find the TBI PHY.  If it's not there, we don't support SGMII */
874 	priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
875 
876 	return 0;
877 
878 err_grp_init:
879 	unmap_group_regs(priv);
880 rx_alloc_failed:
881 	gfar_free_rx_queues(priv);
882 tx_alloc_failed:
883 	gfar_free_tx_queues(priv);
884 	free_gfar_dev(priv);
885 	return err;
886 }
887 
888 static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
889 {
890 	struct hwtstamp_config config;
891 	struct gfar_private *priv = netdev_priv(netdev);
892 
893 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
894 		return -EFAULT;
895 
896 	/* reserved for future extensions */
897 	if (config.flags)
898 		return -EINVAL;
899 
900 	switch (config.tx_type) {
901 	case HWTSTAMP_TX_OFF:
902 		priv->hwts_tx_en = 0;
903 		break;
904 	case HWTSTAMP_TX_ON:
905 		if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
906 			return -ERANGE;
907 		priv->hwts_tx_en = 1;
908 		break;
909 	default:
910 		return -ERANGE;
911 	}
912 
913 	switch (config.rx_filter) {
914 	case HWTSTAMP_FILTER_NONE:
915 		if (priv->hwts_rx_en) {
916 			priv->hwts_rx_en = 0;
917 			reset_gfar(netdev);
918 		}
919 		break;
920 	default:
921 		if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
922 			return -ERANGE;
923 		if (!priv->hwts_rx_en) {
924 			priv->hwts_rx_en = 1;
925 			reset_gfar(netdev);
926 		}
927 		config.rx_filter = HWTSTAMP_FILTER_ALL;
928 		break;
929 	}
930 
931 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
932 		-EFAULT : 0;
933 }
934 
935 static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
936 {
937 	struct hwtstamp_config config;
938 	struct gfar_private *priv = netdev_priv(netdev);
939 
940 	config.flags = 0;
941 	config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
942 	config.rx_filter = (priv->hwts_rx_en ?
943 			    HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
944 
945 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
946 		-EFAULT : 0;
947 }
948 
949 static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
950 {
951 	struct gfar_private *priv = netdev_priv(dev);
952 
953 	if (!netif_running(dev))
954 		return -EINVAL;
955 
956 	if (cmd == SIOCSHWTSTAMP)
957 		return gfar_hwtstamp_set(dev, rq);
958 	if (cmd == SIOCGHWTSTAMP)
959 		return gfar_hwtstamp_get(dev, rq);
960 
961 	if (!priv->phydev)
962 		return -ENODEV;
963 
964 	return phy_mii_ioctl(priv->phydev, rq, cmd);
965 }
966 
967 static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
968 				   u32 class)
969 {
970 	u32 rqfpr = FPR_FILER_MASK;
971 	u32 rqfcr = 0x0;
972 
973 	rqfar--;
974 	rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
975 	priv->ftp_rqfpr[rqfar] = rqfpr;
976 	priv->ftp_rqfcr[rqfar] = rqfcr;
977 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
978 
979 	rqfar--;
980 	rqfcr = RQFCR_CMP_NOMATCH;
981 	priv->ftp_rqfpr[rqfar] = rqfpr;
982 	priv->ftp_rqfcr[rqfar] = rqfcr;
983 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
984 
985 	rqfar--;
986 	rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
987 	rqfpr = class;
988 	priv->ftp_rqfcr[rqfar] = rqfcr;
989 	priv->ftp_rqfpr[rqfar] = rqfpr;
990 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
991 
992 	rqfar--;
993 	rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
994 	rqfpr = class;
995 	priv->ftp_rqfcr[rqfar] = rqfcr;
996 	priv->ftp_rqfpr[rqfar] = rqfpr;
997 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
998 
999 	return rqfar;
1000 }
1001 
1002 static void gfar_init_filer_table(struct gfar_private *priv)
1003 {
1004 	int i = 0x0;
1005 	u32 rqfar = MAX_FILER_IDX;
1006 	u32 rqfcr = 0x0;
1007 	u32 rqfpr = FPR_FILER_MASK;
1008 
1009 	/* Default rule */
1010 	rqfcr = RQFCR_CMP_MATCH;
1011 	priv->ftp_rqfcr[rqfar] = rqfcr;
1012 	priv->ftp_rqfpr[rqfar] = rqfpr;
1013 	gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1014 
1015 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
1016 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
1017 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
1018 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
1019 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
1020 	rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
1021 
1022 	/* cur_filer_idx indicated the first non-masked rule */
1023 	priv->cur_filer_idx = rqfar;
1024 
1025 	/* Rest are masked rules */
1026 	rqfcr = RQFCR_CMP_NOMATCH;
1027 	for (i = 0; i < rqfar; i++) {
1028 		priv->ftp_rqfcr[i] = rqfcr;
1029 		priv->ftp_rqfpr[i] = rqfpr;
1030 		gfar_write_filer(priv, i, rqfcr, rqfpr);
1031 	}
1032 }
1033 
1034 static void __gfar_detect_errata_83xx(struct gfar_private *priv)
1035 {
1036 	unsigned int pvr = mfspr(SPRN_PVR);
1037 	unsigned int svr = mfspr(SPRN_SVR);
1038 	unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
1039 	unsigned int rev = svr & 0xffff;
1040 
1041 	/* MPC8313 Rev 2.0 and higher; All MPC837x */
1042 	if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
1043 	    (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
1044 		priv->errata |= GFAR_ERRATA_74;
1045 
1046 	/* MPC8313 and MPC837x all rev */
1047 	if ((pvr == 0x80850010 && mod == 0x80b0) ||
1048 	    (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
1049 		priv->errata |= GFAR_ERRATA_76;
1050 
1051 	/* MPC8313 Rev < 2.0 */
1052 	if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
1053 		priv->errata |= GFAR_ERRATA_12;
1054 }
1055 
1056 static void __gfar_detect_errata_85xx(struct gfar_private *priv)
1057 {
1058 	unsigned int svr = mfspr(SPRN_SVR);
1059 
1060 	if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
1061 		priv->errata |= GFAR_ERRATA_12;
1062 	if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
1063 	    ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
1064 		priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
1065 }
1066 
1067 static void gfar_detect_errata(struct gfar_private *priv)
1068 {
1069 	struct device *dev = &priv->ofdev->dev;
1070 
1071 	/* no plans to fix */
1072 	priv->errata |= GFAR_ERRATA_A002;
1073 
1074 	if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
1075 		__gfar_detect_errata_85xx(priv);
1076 	else /* non-mpc85xx parts, i.e. e300 core based */
1077 		__gfar_detect_errata_83xx(priv);
1078 
1079 	if (priv->errata)
1080 		dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1081 			 priv->errata);
1082 }
1083 
1084 void gfar_mac_reset(struct gfar_private *priv)
1085 {
1086 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1087 	u32 tempval;
1088 
1089 	/* Reset MAC layer */
1090 	gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
1091 
1092 	/* We need to delay at least 3 TX clocks */
1093 	udelay(3);
1094 
1095 	/* the soft reset bit is not self-resetting, so we need to
1096 	 * clear it before resuming normal operation
1097 	 */
1098 	gfar_write(&regs->maccfg1, 0);
1099 
1100 	udelay(3);
1101 
1102 	/* Compute rx_buff_size based on config flags */
1103 	gfar_rx_buff_size_config(priv);
1104 
1105 	/* Initialize the max receive frame/buffer lengths */
1106 	gfar_write(&regs->maxfrm, priv->rx_buffer_size);
1107 	gfar_write(&regs->mrblr, priv->rx_buffer_size);
1108 
1109 	/* Initialize the Minimum Frame Length Register */
1110 	gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1111 
1112 	/* Initialize MACCFG2. */
1113 	tempval = MACCFG2_INIT_SETTINGS;
1114 
1115 	/* If the mtu is larger than the max size for standard
1116 	 * ethernet frames (ie, a jumbo frame), then set maccfg2
1117 	 * to allow huge frames, and to check the length
1118 	 */
1119 	if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
1120 	    gfar_has_errata(priv, GFAR_ERRATA_74))
1121 		tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
1122 
1123 	gfar_write(&regs->maccfg2, tempval);
1124 
1125 	/* Clear mac addr hash registers */
1126 	gfar_write(&regs->igaddr0, 0);
1127 	gfar_write(&regs->igaddr1, 0);
1128 	gfar_write(&regs->igaddr2, 0);
1129 	gfar_write(&regs->igaddr3, 0);
1130 	gfar_write(&regs->igaddr4, 0);
1131 	gfar_write(&regs->igaddr5, 0);
1132 	gfar_write(&regs->igaddr6, 0);
1133 	gfar_write(&regs->igaddr7, 0);
1134 
1135 	gfar_write(&regs->gaddr0, 0);
1136 	gfar_write(&regs->gaddr1, 0);
1137 	gfar_write(&regs->gaddr2, 0);
1138 	gfar_write(&regs->gaddr3, 0);
1139 	gfar_write(&regs->gaddr4, 0);
1140 	gfar_write(&regs->gaddr5, 0);
1141 	gfar_write(&regs->gaddr6, 0);
1142 	gfar_write(&regs->gaddr7, 0);
1143 
1144 	if (priv->extended_hash)
1145 		gfar_clear_exact_match(priv->ndev);
1146 
1147 	gfar_mac_rx_config(priv);
1148 
1149 	gfar_mac_tx_config(priv);
1150 
1151 	gfar_set_mac_address(priv->ndev);
1152 
1153 	gfar_set_multi(priv->ndev);
1154 
1155 	/* clear ievent and imask before configuring coalescing */
1156 	gfar_ints_disable(priv);
1157 
1158 	/* Configure the coalescing support */
1159 	gfar_configure_coalescing_all(priv);
1160 }
1161 
1162 static void gfar_hw_init(struct gfar_private *priv)
1163 {
1164 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1165 	u32 attrs;
1166 
1167 	/* Stop the DMA engine now, in case it was running before
1168 	 * (The firmware could have used it, and left it running).
1169 	 */
1170 	gfar_halt(priv);
1171 
1172 	gfar_mac_reset(priv);
1173 
1174 	/* Zero out the rmon mib registers if it has them */
1175 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1176 		memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
1177 
1178 		/* Mask off the CAM interrupts */
1179 		gfar_write(&regs->rmon.cam1, 0xffffffff);
1180 		gfar_write(&regs->rmon.cam2, 0xffffffff);
1181 	}
1182 
1183 	/* Initialize ECNTRL */
1184 	gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
1185 
1186 	/* Set the extraction length and index */
1187 	attrs = ATTRELI_EL(priv->rx_stash_size) |
1188 		ATTRELI_EI(priv->rx_stash_index);
1189 
1190 	gfar_write(&regs->attreli, attrs);
1191 
1192 	/* Start with defaults, and add stashing
1193 	 * depending on driver parameters
1194 	 */
1195 	attrs = ATTR_INIT_SETTINGS;
1196 
1197 	if (priv->bd_stash_en)
1198 		attrs |= ATTR_BDSTASH;
1199 
1200 	if (priv->rx_stash_size != 0)
1201 		attrs |= ATTR_BUFSTASH;
1202 
1203 	gfar_write(&regs->attr, attrs);
1204 
1205 	/* FIFO configs */
1206 	gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
1207 	gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
1208 	gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
1209 
1210 	/* Program the interrupt steering regs, only for MG devices */
1211 	if (priv->num_grps > 1)
1212 		gfar_write_isrg(priv);
1213 }
1214 
1215 static void __init gfar_init_addr_hash_table(struct gfar_private *priv)
1216 {
1217 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1218 
1219 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
1220 		priv->extended_hash = 1;
1221 		priv->hash_width = 9;
1222 
1223 		priv->hash_regs[0] = &regs->igaddr0;
1224 		priv->hash_regs[1] = &regs->igaddr1;
1225 		priv->hash_regs[2] = &regs->igaddr2;
1226 		priv->hash_regs[3] = &regs->igaddr3;
1227 		priv->hash_regs[4] = &regs->igaddr4;
1228 		priv->hash_regs[5] = &regs->igaddr5;
1229 		priv->hash_regs[6] = &regs->igaddr6;
1230 		priv->hash_regs[7] = &regs->igaddr7;
1231 		priv->hash_regs[8] = &regs->gaddr0;
1232 		priv->hash_regs[9] = &regs->gaddr1;
1233 		priv->hash_regs[10] = &regs->gaddr2;
1234 		priv->hash_regs[11] = &regs->gaddr3;
1235 		priv->hash_regs[12] = &regs->gaddr4;
1236 		priv->hash_regs[13] = &regs->gaddr5;
1237 		priv->hash_regs[14] = &regs->gaddr6;
1238 		priv->hash_regs[15] = &regs->gaddr7;
1239 
1240 	} else {
1241 		priv->extended_hash = 0;
1242 		priv->hash_width = 8;
1243 
1244 		priv->hash_regs[0] = &regs->gaddr0;
1245 		priv->hash_regs[1] = &regs->gaddr1;
1246 		priv->hash_regs[2] = &regs->gaddr2;
1247 		priv->hash_regs[3] = &regs->gaddr3;
1248 		priv->hash_regs[4] = &regs->gaddr4;
1249 		priv->hash_regs[5] = &regs->gaddr5;
1250 		priv->hash_regs[6] = &regs->gaddr6;
1251 		priv->hash_regs[7] = &regs->gaddr7;
1252 	}
1253 }
1254 
1255 /* Set up the ethernet device structure, private data,
1256  * and anything else we need before we start
1257  */
1258 static int gfar_probe(struct platform_device *ofdev)
1259 {
1260 	struct net_device *dev = NULL;
1261 	struct gfar_private *priv = NULL;
1262 	int err = 0, i;
1263 
1264 	err = gfar_of_init(ofdev, &dev);
1265 
1266 	if (err)
1267 		return err;
1268 
1269 	priv = netdev_priv(dev);
1270 	priv->ndev = dev;
1271 	priv->ofdev = ofdev;
1272 	priv->dev = &ofdev->dev;
1273 	SET_NETDEV_DEV(dev, &ofdev->dev);
1274 
1275 	spin_lock_init(&priv->bflock);
1276 	INIT_WORK(&priv->reset_task, gfar_reset_task);
1277 
1278 	platform_set_drvdata(ofdev, priv);
1279 
1280 	gfar_detect_errata(priv);
1281 
1282 	/* Set the dev->base_addr to the gfar reg region */
1283 	dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
1284 
1285 	/* Fill in the dev structure */
1286 	dev->watchdog_timeo = TX_TIMEOUT;
1287 	dev->mtu = 1500;
1288 	dev->netdev_ops = &gfar_netdev_ops;
1289 	dev->ethtool_ops = &gfar_ethtool_ops;
1290 
1291 	/* Register for napi ...We are registering NAPI for each grp */
1292 	for (i = 0; i < priv->num_grps; i++) {
1293 		if (priv->poll_mode == GFAR_SQ_POLLING) {
1294 			netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1295 				       gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
1296 			netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1297 				       gfar_poll_tx_sq, 2);
1298 		} else {
1299 			netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1300 				       gfar_poll_rx, GFAR_DEV_WEIGHT);
1301 			netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1302 				       gfar_poll_tx, 2);
1303 		}
1304 	}
1305 
1306 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
1307 		dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
1308 				   NETIF_F_RXCSUM;
1309 		dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
1310 				 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
1311 	}
1312 
1313 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
1314 		dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
1315 				    NETIF_F_HW_VLAN_CTAG_RX;
1316 		dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
1317 	}
1318 
1319 	gfar_init_addr_hash_table(priv);
1320 
1321 	/* Insert receive time stamps into padding alignment bytes */
1322 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1323 		priv->padding = 8;
1324 
1325 	if (dev->features & NETIF_F_IP_CSUM ||
1326 	    priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1327 		dev->needed_headroom = GMAC_FCB_LEN;
1328 
1329 	priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
1330 
1331 	/* Initializing some of the rx/tx queue level parameters */
1332 	for (i = 0; i < priv->num_tx_queues; i++) {
1333 		priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1334 		priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1335 		priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1336 		priv->tx_queue[i]->txic = DEFAULT_TXIC;
1337 	}
1338 
1339 	for (i = 0; i < priv->num_rx_queues; i++) {
1340 		priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1341 		priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1342 		priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1343 	}
1344 
1345 	/* always enable rx filer */
1346 	priv->rx_filer_enable = 1;
1347 	/* Enable most messages by default */
1348 	priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1349 	/* use pritority h/w tx queue scheduling for single queue devices */
1350 	if (priv->num_tx_queues == 1)
1351 		priv->prio_sched_en = 1;
1352 
1353 	set_bit(GFAR_DOWN, &priv->state);
1354 
1355 	gfar_hw_init(priv);
1356 
1357 	err = register_netdev(dev);
1358 
1359 	if (err) {
1360 		pr_err("%s: Cannot register net device, aborting\n", dev->name);
1361 		goto register_fail;
1362 	}
1363 
1364 	/* Carrier starts down, phylib will bring it up */
1365 	netif_carrier_off(dev);
1366 
1367 	device_init_wakeup(&dev->dev,
1368 			   priv->device_flags &
1369 			   FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1370 
1371 	/* fill out IRQ number and name fields */
1372 	for (i = 0; i < priv->num_grps; i++) {
1373 		struct gfar_priv_grp *grp = &priv->gfargrp[i];
1374 		if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1375 			sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
1376 				dev->name, "_g", '0' + i, "_tx");
1377 			sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
1378 				dev->name, "_g", '0' + i, "_rx");
1379 			sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
1380 				dev->name, "_g", '0' + i, "_er");
1381 		} else
1382 			strcpy(gfar_irq(grp, TX)->name, dev->name);
1383 	}
1384 
1385 	/* Initialize the filer table */
1386 	gfar_init_filer_table(priv);
1387 
1388 	/* Print out the device info */
1389 	netdev_info(dev, "mac: %pM\n", dev->dev_addr);
1390 
1391 	/* Even more device info helps when determining which kernel
1392 	 * provided which set of benchmarks.
1393 	 */
1394 	netdev_info(dev, "Running with NAPI enabled\n");
1395 	for (i = 0; i < priv->num_rx_queues; i++)
1396 		netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1397 			    i, priv->rx_queue[i]->rx_ring_size);
1398 	for (i = 0; i < priv->num_tx_queues; i++)
1399 		netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1400 			    i, priv->tx_queue[i]->tx_ring_size);
1401 
1402 	return 0;
1403 
1404 register_fail:
1405 	unmap_group_regs(priv);
1406 	gfar_free_rx_queues(priv);
1407 	gfar_free_tx_queues(priv);
1408 	if (priv->phy_node)
1409 		of_node_put(priv->phy_node);
1410 	if (priv->tbi_node)
1411 		of_node_put(priv->tbi_node);
1412 	free_gfar_dev(priv);
1413 	return err;
1414 }
1415 
1416 static int gfar_remove(struct platform_device *ofdev)
1417 {
1418 	struct gfar_private *priv = platform_get_drvdata(ofdev);
1419 
1420 	if (priv->phy_node)
1421 		of_node_put(priv->phy_node);
1422 	if (priv->tbi_node)
1423 		of_node_put(priv->tbi_node);
1424 
1425 	unregister_netdev(priv->ndev);
1426 	unmap_group_regs(priv);
1427 	gfar_free_rx_queues(priv);
1428 	gfar_free_tx_queues(priv);
1429 	free_gfar_dev(priv);
1430 
1431 	return 0;
1432 }
1433 
1434 #ifdef CONFIG_PM
1435 
1436 static int gfar_suspend(struct device *dev)
1437 {
1438 	struct gfar_private *priv = dev_get_drvdata(dev);
1439 	struct net_device *ndev = priv->ndev;
1440 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1441 	unsigned long flags;
1442 	u32 tempval;
1443 
1444 	int magic_packet = priv->wol_en &&
1445 			   (priv->device_flags &
1446 			    FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1447 
1448 	netif_device_detach(ndev);
1449 
1450 	if (netif_running(ndev)) {
1451 
1452 		local_irq_save(flags);
1453 		lock_tx_qs(priv);
1454 
1455 		gfar_halt_nodisable(priv);
1456 
1457 		/* Disable Tx, and Rx if wake-on-LAN is disabled. */
1458 		tempval = gfar_read(&regs->maccfg1);
1459 
1460 		tempval &= ~MACCFG1_TX_EN;
1461 
1462 		if (!magic_packet)
1463 			tempval &= ~MACCFG1_RX_EN;
1464 
1465 		gfar_write(&regs->maccfg1, tempval);
1466 
1467 		unlock_tx_qs(priv);
1468 		local_irq_restore(flags);
1469 
1470 		disable_napi(priv);
1471 
1472 		if (magic_packet) {
1473 			/* Enable interrupt on Magic Packet */
1474 			gfar_write(&regs->imask, IMASK_MAG);
1475 
1476 			/* Enable Magic Packet mode */
1477 			tempval = gfar_read(&regs->maccfg2);
1478 			tempval |= MACCFG2_MPEN;
1479 			gfar_write(&regs->maccfg2, tempval);
1480 		} else {
1481 			phy_stop(priv->phydev);
1482 		}
1483 	}
1484 
1485 	return 0;
1486 }
1487 
1488 static int gfar_resume(struct device *dev)
1489 {
1490 	struct gfar_private *priv = dev_get_drvdata(dev);
1491 	struct net_device *ndev = priv->ndev;
1492 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1493 	unsigned long flags;
1494 	u32 tempval;
1495 	int magic_packet = priv->wol_en &&
1496 			   (priv->device_flags &
1497 			    FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
1498 
1499 	if (!netif_running(ndev)) {
1500 		netif_device_attach(ndev);
1501 		return 0;
1502 	}
1503 
1504 	if (!magic_packet && priv->phydev)
1505 		phy_start(priv->phydev);
1506 
1507 	/* Disable Magic Packet mode, in case something
1508 	 * else woke us up.
1509 	 */
1510 	local_irq_save(flags);
1511 	lock_tx_qs(priv);
1512 
1513 	tempval = gfar_read(&regs->maccfg2);
1514 	tempval &= ~MACCFG2_MPEN;
1515 	gfar_write(&regs->maccfg2, tempval);
1516 
1517 	gfar_start(priv);
1518 
1519 	unlock_tx_qs(priv);
1520 	local_irq_restore(flags);
1521 
1522 	netif_device_attach(ndev);
1523 
1524 	enable_napi(priv);
1525 
1526 	return 0;
1527 }
1528 
1529 static int gfar_restore(struct device *dev)
1530 {
1531 	struct gfar_private *priv = dev_get_drvdata(dev);
1532 	struct net_device *ndev = priv->ndev;
1533 
1534 	if (!netif_running(ndev)) {
1535 		netif_device_attach(ndev);
1536 
1537 		return 0;
1538 	}
1539 
1540 	if (gfar_init_bds(ndev)) {
1541 		free_skb_resources(priv);
1542 		return -ENOMEM;
1543 	}
1544 
1545 	gfar_mac_reset(priv);
1546 
1547 	gfar_init_tx_rx_base(priv);
1548 
1549 	gfar_start(priv);
1550 
1551 	priv->oldlink = 0;
1552 	priv->oldspeed = 0;
1553 	priv->oldduplex = -1;
1554 
1555 	if (priv->phydev)
1556 		phy_start(priv->phydev);
1557 
1558 	netif_device_attach(ndev);
1559 	enable_napi(priv);
1560 
1561 	return 0;
1562 }
1563 
1564 static struct dev_pm_ops gfar_pm_ops = {
1565 	.suspend = gfar_suspend,
1566 	.resume = gfar_resume,
1567 	.freeze = gfar_suspend,
1568 	.thaw = gfar_resume,
1569 	.restore = gfar_restore,
1570 };
1571 
1572 #define GFAR_PM_OPS (&gfar_pm_ops)
1573 
1574 #else
1575 
1576 #define GFAR_PM_OPS NULL
1577 
1578 #endif
1579 
1580 /* Reads the controller's registers to determine what interface
1581  * connects it to the PHY.
1582  */
1583 static phy_interface_t gfar_get_interface(struct net_device *dev)
1584 {
1585 	struct gfar_private *priv = netdev_priv(dev);
1586 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1587 	u32 ecntrl;
1588 
1589 	ecntrl = gfar_read(&regs->ecntrl);
1590 
1591 	if (ecntrl & ECNTRL_SGMII_MODE)
1592 		return PHY_INTERFACE_MODE_SGMII;
1593 
1594 	if (ecntrl & ECNTRL_TBI_MODE) {
1595 		if (ecntrl & ECNTRL_REDUCED_MODE)
1596 			return PHY_INTERFACE_MODE_RTBI;
1597 		else
1598 			return PHY_INTERFACE_MODE_TBI;
1599 	}
1600 
1601 	if (ecntrl & ECNTRL_REDUCED_MODE) {
1602 		if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
1603 			return PHY_INTERFACE_MODE_RMII;
1604 		}
1605 		else {
1606 			phy_interface_t interface = priv->interface;
1607 
1608 			/* This isn't autodetected right now, so it must
1609 			 * be set by the device tree or platform code.
1610 			 */
1611 			if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1612 				return PHY_INTERFACE_MODE_RGMII_ID;
1613 
1614 			return PHY_INTERFACE_MODE_RGMII;
1615 		}
1616 	}
1617 
1618 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
1619 		return PHY_INTERFACE_MODE_GMII;
1620 
1621 	return PHY_INTERFACE_MODE_MII;
1622 }
1623 
1624 
1625 /* Initializes driver's PHY state, and attaches to the PHY.
1626  * Returns 0 on success.
1627  */
1628 static int init_phy(struct net_device *dev)
1629 {
1630 	struct gfar_private *priv = netdev_priv(dev);
1631 	uint gigabit_support =
1632 		priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
1633 		GFAR_SUPPORTED_GBIT : 0;
1634 	phy_interface_t interface;
1635 
1636 	priv->oldlink = 0;
1637 	priv->oldspeed = 0;
1638 	priv->oldduplex = -1;
1639 
1640 	interface = gfar_get_interface(dev);
1641 
1642 	priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1643 				      interface);
1644 	if (!priv->phydev)
1645 		priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1646 							 interface);
1647 	if (!priv->phydev) {
1648 		dev_err(&dev->dev, "could not attach to PHY\n");
1649 		return -ENODEV;
1650 	}
1651 
1652 	if (interface == PHY_INTERFACE_MODE_SGMII)
1653 		gfar_configure_serdes(dev);
1654 
1655 	/* Remove any features not supported by the controller */
1656 	priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1657 	priv->phydev->advertising = priv->phydev->supported;
1658 
1659 	return 0;
1660 }
1661 
1662 /* Initialize TBI PHY interface for communicating with the
1663  * SERDES lynx PHY on the chip.  We communicate with this PHY
1664  * through the MDIO bus on each controller, treating it as a
1665  * "normal" PHY at the address found in the TBIPA register.  We assume
1666  * that the TBIPA register is valid.  Either the MDIO bus code will set
1667  * it to a value that doesn't conflict with other PHYs on the bus, or the
1668  * value doesn't matter, as there are no other PHYs on the bus.
1669  */
1670 static void gfar_configure_serdes(struct net_device *dev)
1671 {
1672 	struct gfar_private *priv = netdev_priv(dev);
1673 	struct phy_device *tbiphy;
1674 
1675 	if (!priv->tbi_node) {
1676 		dev_warn(&dev->dev, "error: SGMII mode requires that the "
1677 				    "device tree specify a tbi-handle\n");
1678 		return;
1679 	}
1680 
1681 	tbiphy = of_phy_find_device(priv->tbi_node);
1682 	if (!tbiphy) {
1683 		dev_err(&dev->dev, "error: Could not get TBI device\n");
1684 		return;
1685 	}
1686 
1687 	/* If the link is already up, we must already be ok, and don't need to
1688 	 * configure and reset the TBI<->SerDes link.  Maybe U-Boot configured
1689 	 * everything for us?  Resetting it takes the link down and requires
1690 	 * several seconds for it to come back.
1691 	 */
1692 	if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
1693 		return;
1694 
1695 	/* Single clk mode, mii mode off(for serdes communication) */
1696 	phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
1697 
1698 	phy_write(tbiphy, MII_ADVERTISE,
1699 		  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1700 		  ADVERTISE_1000XPSE_ASYM);
1701 
1702 	phy_write(tbiphy, MII_BMCR,
1703 		  BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1704 		  BMCR_SPEED1000);
1705 }
1706 
1707 static int __gfar_is_rx_idle(struct gfar_private *priv)
1708 {
1709 	u32 res;
1710 
1711 	/* Normaly TSEC should not hang on GRS commands, so we should
1712 	 * actually wait for IEVENT_GRSC flag.
1713 	 */
1714 	if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
1715 		return 0;
1716 
1717 	/* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
1718 	 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1719 	 * and the Rx can be safely reset.
1720 	 */
1721 	res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1722 	res &= 0x7f807f80;
1723 	if ((res & 0xffff) == (res >> 16))
1724 		return 1;
1725 
1726 	return 0;
1727 }
1728 
1729 /* Halt the receive and transmit queues */
1730 static void gfar_halt_nodisable(struct gfar_private *priv)
1731 {
1732 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1733 	u32 tempval;
1734 
1735 	gfar_ints_disable(priv);
1736 
1737 	/* Stop the DMA, and wait for it to stop */
1738 	tempval = gfar_read(&regs->dmactrl);
1739 	if ((tempval & (DMACTRL_GRS | DMACTRL_GTS)) !=
1740 	    (DMACTRL_GRS | DMACTRL_GTS)) {
1741 		int ret;
1742 
1743 		tempval |= (DMACTRL_GRS | DMACTRL_GTS);
1744 		gfar_write(&regs->dmactrl, tempval);
1745 
1746 		do {
1747 			ret = spin_event_timeout(((gfar_read(&regs->ievent) &
1748 				 (IEVENT_GRSC | IEVENT_GTSC)) ==
1749 				 (IEVENT_GRSC | IEVENT_GTSC)), 1000000, 0);
1750 			if (!ret && !(gfar_read(&regs->ievent) & IEVENT_GRSC))
1751 				ret = __gfar_is_rx_idle(priv);
1752 		} while (!ret);
1753 	}
1754 }
1755 
1756 /* Halt the receive and transmit queues */
1757 void gfar_halt(struct gfar_private *priv)
1758 {
1759 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1760 	u32 tempval;
1761 
1762 	/* Dissable the Rx/Tx hw queues */
1763 	gfar_write(&regs->rqueue, 0);
1764 	gfar_write(&regs->tqueue, 0);
1765 
1766 	mdelay(10);
1767 
1768 	gfar_halt_nodisable(priv);
1769 
1770 	/* Disable Rx/Tx DMA */
1771 	tempval = gfar_read(&regs->maccfg1);
1772 	tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1773 	gfar_write(&regs->maccfg1, tempval);
1774 }
1775 
1776 void stop_gfar(struct net_device *dev)
1777 {
1778 	struct gfar_private *priv = netdev_priv(dev);
1779 
1780 	netif_tx_stop_all_queues(dev);
1781 
1782 	smp_mb__before_clear_bit();
1783 	set_bit(GFAR_DOWN, &priv->state);
1784 	smp_mb__after_clear_bit();
1785 
1786 	disable_napi(priv);
1787 
1788 	/* disable ints and gracefully shut down Rx/Tx DMA */
1789 	gfar_halt(priv);
1790 
1791 	phy_stop(priv->phydev);
1792 
1793 	free_skb_resources(priv);
1794 }
1795 
1796 static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
1797 {
1798 	struct txbd8 *txbdp;
1799 	struct gfar_private *priv = netdev_priv(tx_queue->dev);
1800 	int i, j;
1801 
1802 	txbdp = tx_queue->tx_bd_base;
1803 
1804 	for (i = 0; i < tx_queue->tx_ring_size; i++) {
1805 		if (!tx_queue->tx_skbuff[i])
1806 			continue;
1807 
1808 		dma_unmap_single(priv->dev, txbdp->bufPtr,
1809 				 txbdp->length, DMA_TO_DEVICE);
1810 		txbdp->lstatus = 0;
1811 		for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
1812 		     j++) {
1813 			txbdp++;
1814 			dma_unmap_page(priv->dev, txbdp->bufPtr,
1815 				       txbdp->length, DMA_TO_DEVICE);
1816 		}
1817 		txbdp++;
1818 		dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1819 		tx_queue->tx_skbuff[i] = NULL;
1820 	}
1821 	kfree(tx_queue->tx_skbuff);
1822 	tx_queue->tx_skbuff = NULL;
1823 }
1824 
1825 static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1826 {
1827 	struct rxbd8 *rxbdp;
1828 	struct gfar_private *priv = netdev_priv(rx_queue->dev);
1829 	int i;
1830 
1831 	rxbdp = rx_queue->rx_bd_base;
1832 
1833 	for (i = 0; i < rx_queue->rx_ring_size; i++) {
1834 		if (rx_queue->rx_skbuff[i]) {
1835 			dma_unmap_single(priv->dev, rxbdp->bufPtr,
1836 					 priv->rx_buffer_size,
1837 					 DMA_FROM_DEVICE);
1838 			dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1839 			rx_queue->rx_skbuff[i] = NULL;
1840 		}
1841 		rxbdp->lstatus = 0;
1842 		rxbdp->bufPtr = 0;
1843 		rxbdp++;
1844 	}
1845 	kfree(rx_queue->rx_skbuff);
1846 	rx_queue->rx_skbuff = NULL;
1847 }
1848 
1849 /* If there are any tx skbs or rx skbs still around, free them.
1850  * Then free tx_skbuff and rx_skbuff
1851  */
1852 static void free_skb_resources(struct gfar_private *priv)
1853 {
1854 	struct gfar_priv_tx_q *tx_queue = NULL;
1855 	struct gfar_priv_rx_q *rx_queue = NULL;
1856 	int i;
1857 
1858 	/* Go through all the buffer descriptors and free their data buffers */
1859 	for (i = 0; i < priv->num_tx_queues; i++) {
1860 		struct netdev_queue *txq;
1861 
1862 		tx_queue = priv->tx_queue[i];
1863 		txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
1864 		if (tx_queue->tx_skbuff)
1865 			free_skb_tx_queue(tx_queue);
1866 		netdev_tx_reset_queue(txq);
1867 	}
1868 
1869 	for (i = 0; i < priv->num_rx_queues; i++) {
1870 		rx_queue = priv->rx_queue[i];
1871 		if (rx_queue->rx_skbuff)
1872 			free_skb_rx_queue(rx_queue);
1873 	}
1874 
1875 	dma_free_coherent(priv->dev,
1876 			  sizeof(struct txbd8) * priv->total_tx_ring_size +
1877 			  sizeof(struct rxbd8) * priv->total_rx_ring_size,
1878 			  priv->tx_queue[0]->tx_bd_base,
1879 			  priv->tx_queue[0]->tx_bd_dma_base);
1880 }
1881 
1882 void gfar_start(struct gfar_private *priv)
1883 {
1884 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1885 	u32 tempval;
1886 	int i = 0;
1887 
1888 	/* Enable Rx/Tx hw queues */
1889 	gfar_write(&regs->rqueue, priv->rqueue);
1890 	gfar_write(&regs->tqueue, priv->tqueue);
1891 
1892 	/* Initialize DMACTRL to have WWR and WOP */
1893 	tempval = gfar_read(&regs->dmactrl);
1894 	tempval |= DMACTRL_INIT_SETTINGS;
1895 	gfar_write(&regs->dmactrl, tempval);
1896 
1897 	/* Make sure we aren't stopped */
1898 	tempval = gfar_read(&regs->dmactrl);
1899 	tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
1900 	gfar_write(&regs->dmactrl, tempval);
1901 
1902 	for (i = 0; i < priv->num_grps; i++) {
1903 		regs = priv->gfargrp[i].regs;
1904 		/* Clear THLT/RHLT, so that the DMA starts polling now */
1905 		gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1906 		gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
1907 	}
1908 
1909 	/* Enable Rx/Tx DMA */
1910 	tempval = gfar_read(&regs->maccfg1);
1911 	tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1912 	gfar_write(&regs->maccfg1, tempval);
1913 
1914 	gfar_ints_enable(priv);
1915 
1916 	priv->ndev->trans_start = jiffies; /* prevent tx timeout */
1917 }
1918 
1919 static void free_grp_irqs(struct gfar_priv_grp *grp)
1920 {
1921 	free_irq(gfar_irq(grp, TX)->irq, grp);
1922 	free_irq(gfar_irq(grp, RX)->irq, grp);
1923 	free_irq(gfar_irq(grp, ER)->irq, grp);
1924 }
1925 
1926 static int register_grp_irqs(struct gfar_priv_grp *grp)
1927 {
1928 	struct gfar_private *priv = grp->priv;
1929 	struct net_device *dev = priv->ndev;
1930 	int err;
1931 
1932 	/* If the device has multiple interrupts, register for
1933 	 * them.  Otherwise, only register for the one
1934 	 */
1935 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1936 		/* Install our interrupt handlers for Error,
1937 		 * Transmit, and Receive
1938 		 */
1939 		err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
1940 				  gfar_irq(grp, ER)->name, grp);
1941 		if (err < 0) {
1942 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1943 				  gfar_irq(grp, ER)->irq);
1944 
1945 			goto err_irq_fail;
1946 		}
1947 		err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
1948 				  gfar_irq(grp, TX)->name, grp);
1949 		if (err < 0) {
1950 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1951 				  gfar_irq(grp, TX)->irq);
1952 			goto tx_irq_fail;
1953 		}
1954 		err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
1955 				  gfar_irq(grp, RX)->name, grp);
1956 		if (err < 0) {
1957 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1958 				  gfar_irq(grp, RX)->irq);
1959 			goto rx_irq_fail;
1960 		}
1961 	} else {
1962 		err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
1963 				  gfar_irq(grp, TX)->name, grp);
1964 		if (err < 0) {
1965 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
1966 				  gfar_irq(grp, TX)->irq);
1967 			goto err_irq_fail;
1968 		}
1969 	}
1970 
1971 	return 0;
1972 
1973 rx_irq_fail:
1974 	free_irq(gfar_irq(grp, TX)->irq, grp);
1975 tx_irq_fail:
1976 	free_irq(gfar_irq(grp, ER)->irq, grp);
1977 err_irq_fail:
1978 	return err;
1979 
1980 }
1981 
1982 static void gfar_free_irq(struct gfar_private *priv)
1983 {
1984 	int i;
1985 
1986 	/* Free the IRQs */
1987 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1988 		for (i = 0; i < priv->num_grps; i++)
1989 			free_grp_irqs(&priv->gfargrp[i]);
1990 	} else {
1991 		for (i = 0; i < priv->num_grps; i++)
1992 			free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
1993 				 &priv->gfargrp[i]);
1994 	}
1995 }
1996 
1997 static int gfar_request_irq(struct gfar_private *priv)
1998 {
1999 	int err, i, j;
2000 
2001 	for (i = 0; i < priv->num_grps; i++) {
2002 		err = register_grp_irqs(&priv->gfargrp[i]);
2003 		if (err) {
2004 			for (j = 0; j < i; j++)
2005 				free_grp_irqs(&priv->gfargrp[j]);
2006 			return err;
2007 		}
2008 	}
2009 
2010 	return 0;
2011 }
2012 
2013 /* Bring the controller up and running */
2014 int startup_gfar(struct net_device *ndev)
2015 {
2016 	struct gfar_private *priv = netdev_priv(ndev);
2017 	int err;
2018 
2019 	gfar_mac_reset(priv);
2020 
2021 	err = gfar_alloc_skb_resources(ndev);
2022 	if (err)
2023 		return err;
2024 
2025 	gfar_init_tx_rx_base(priv);
2026 
2027 	smp_mb__before_clear_bit();
2028 	clear_bit(GFAR_DOWN, &priv->state);
2029 	smp_mb__after_clear_bit();
2030 
2031 	/* Start Rx/Tx DMA and enable the interrupts */
2032 	gfar_start(priv);
2033 
2034 	phy_start(priv->phydev);
2035 
2036 	enable_napi(priv);
2037 
2038 	netif_tx_wake_all_queues(ndev);
2039 
2040 	return 0;
2041 }
2042 
2043 /* Called when something needs to use the ethernet device
2044  * Returns 0 for success.
2045  */
2046 static int gfar_enet_open(struct net_device *dev)
2047 {
2048 	struct gfar_private *priv = netdev_priv(dev);
2049 	int err;
2050 
2051 	err = init_phy(dev);
2052 	if (err)
2053 		return err;
2054 
2055 	err = gfar_request_irq(priv);
2056 	if (err)
2057 		return err;
2058 
2059 	err = startup_gfar(dev);
2060 	if (err)
2061 		return err;
2062 
2063 	device_set_wakeup_enable(&dev->dev, priv->wol_en);
2064 
2065 	return err;
2066 }
2067 
2068 static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
2069 {
2070 	struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
2071 
2072 	memset(fcb, 0, GMAC_FCB_LEN);
2073 
2074 	return fcb;
2075 }
2076 
2077 static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
2078 				    int fcb_length)
2079 {
2080 	/* If we're here, it's a IP packet with a TCP or UDP
2081 	 * payload.  We set it to checksum, using a pseudo-header
2082 	 * we provide
2083 	 */
2084 	u8 flags = TXFCB_DEFAULT;
2085 
2086 	/* Tell the controller what the protocol is
2087 	 * And provide the already calculated phcs
2088 	 */
2089 	if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
2090 		flags |= TXFCB_UDP;
2091 		fcb->phcs = udp_hdr(skb)->check;
2092 	} else
2093 		fcb->phcs = tcp_hdr(skb)->check;
2094 
2095 	/* l3os is the distance between the start of the
2096 	 * frame (skb->data) and the start of the IP hdr.
2097 	 * l4os is the distance between the start of the
2098 	 * l3 hdr and the l4 hdr
2099 	 */
2100 	fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
2101 	fcb->l4os = skb_network_header_len(skb);
2102 
2103 	fcb->flags = flags;
2104 }
2105 
2106 void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
2107 {
2108 	fcb->flags |= TXFCB_VLN;
2109 	fcb->vlctl = vlan_tx_tag_get(skb);
2110 }
2111 
2112 static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
2113 				      struct txbd8 *base, int ring_size)
2114 {
2115 	struct txbd8 *new_bd = bdp + stride;
2116 
2117 	return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2118 }
2119 
2120 static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
2121 				      int ring_size)
2122 {
2123 	return skip_txbd(bdp, 1, base, ring_size);
2124 }
2125 
2126 /* eTSEC12: csum generation not supported for some fcb offsets */
2127 static inline bool gfar_csum_errata_12(struct gfar_private *priv,
2128 				       unsigned long fcb_addr)
2129 {
2130 	return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
2131 	       (fcb_addr % 0x20) > 0x18);
2132 }
2133 
2134 /* eTSEC76: csum generation for frames larger than 2500 may
2135  * cause excess delays before start of transmission
2136  */
2137 static inline bool gfar_csum_errata_76(struct gfar_private *priv,
2138 				       unsigned int len)
2139 {
2140 	return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
2141 	       (len > 2500));
2142 }
2143 
2144 /* This is called by the kernel when a frame is ready for transmission.
2145  * It is pointed to by the dev->hard_start_xmit function pointer
2146  */
2147 static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2148 {
2149 	struct gfar_private *priv = netdev_priv(dev);
2150 	struct gfar_priv_tx_q *tx_queue = NULL;
2151 	struct netdev_queue *txq;
2152 	struct gfar __iomem *regs = NULL;
2153 	struct txfcb *fcb = NULL;
2154 	struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
2155 	u32 lstatus;
2156 	int i, rq = 0;
2157 	int do_tstamp, do_csum, do_vlan;
2158 	u32 bufaddr;
2159 	unsigned long flags;
2160 	unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
2161 
2162 	rq = skb->queue_mapping;
2163 	tx_queue = priv->tx_queue[rq];
2164 	txq = netdev_get_tx_queue(dev, rq);
2165 	base = tx_queue->tx_bd_base;
2166 	regs = tx_queue->grp->regs;
2167 
2168 	do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
2169 	do_vlan = vlan_tx_tag_present(skb);
2170 	do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2171 		    priv->hwts_tx_en;
2172 
2173 	if (do_csum || do_vlan)
2174 		fcb_len = GMAC_FCB_LEN;
2175 
2176 	/* check if time stamp should be generated */
2177 	if (unlikely(do_tstamp))
2178 		fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2179 
2180 	/* make space for additional header when fcb is needed */
2181 	if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
2182 		struct sk_buff *skb_new;
2183 
2184 		skb_new = skb_realloc_headroom(skb, fcb_len);
2185 		if (!skb_new) {
2186 			dev->stats.tx_errors++;
2187 			kfree_skb(skb);
2188 			return NETDEV_TX_OK;
2189 		}
2190 
2191 		if (skb->sk)
2192 			skb_set_owner_w(skb_new, skb->sk);
2193 		consume_skb(skb);
2194 		skb = skb_new;
2195 	}
2196 
2197 	/* total number of fragments in the SKB */
2198 	nr_frags = skb_shinfo(skb)->nr_frags;
2199 
2200 	/* calculate the required number of TxBDs for this skb */
2201 	if (unlikely(do_tstamp))
2202 		nr_txbds = nr_frags + 2;
2203 	else
2204 		nr_txbds = nr_frags + 1;
2205 
2206 	/* check if there is space to queue this packet */
2207 	if (nr_txbds > tx_queue->num_txbdfree) {
2208 		/* no space, stop the queue */
2209 		netif_tx_stop_queue(txq);
2210 		dev->stats.tx_fifo_errors++;
2211 		return NETDEV_TX_BUSY;
2212 	}
2213 
2214 	/* Update transmit stats */
2215 	bytes_sent = skb->len;
2216 	tx_queue->stats.tx_bytes += bytes_sent;
2217 	/* keep Tx bytes on wire for BQL accounting */
2218 	GFAR_CB(skb)->bytes_sent = bytes_sent;
2219 	tx_queue->stats.tx_packets++;
2220 
2221 	txbdp = txbdp_start = tx_queue->cur_tx;
2222 	lstatus = txbdp->lstatus;
2223 
2224 	/* Time stamp insertion requires one additional TxBD */
2225 	if (unlikely(do_tstamp))
2226 		txbdp_tstamp = txbdp = next_txbd(txbdp, base,
2227 						 tx_queue->tx_ring_size);
2228 
2229 	if (nr_frags == 0) {
2230 		if (unlikely(do_tstamp))
2231 			txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
2232 							  TXBD_INTERRUPT);
2233 		else
2234 			lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2235 	} else {
2236 		/* Place the fragment addresses and lengths into the TxBDs */
2237 		for (i = 0; i < nr_frags; i++) {
2238 			unsigned int frag_len;
2239 			/* Point at the next BD, wrapping as needed */
2240 			txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2241 
2242 			frag_len = skb_shinfo(skb)->frags[i].size;
2243 
2244 			lstatus = txbdp->lstatus | frag_len |
2245 				  BD_LFLAG(TXBD_READY);
2246 
2247 			/* Handle the last BD specially */
2248 			if (i == nr_frags - 1)
2249 				lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2250 
2251 			bufaddr = skb_frag_dma_map(priv->dev,
2252 						   &skb_shinfo(skb)->frags[i],
2253 						   0,
2254 						   frag_len,
2255 						   DMA_TO_DEVICE);
2256 
2257 			/* set the TxBD length and buffer pointer */
2258 			txbdp->bufPtr = bufaddr;
2259 			txbdp->lstatus = lstatus;
2260 		}
2261 
2262 		lstatus = txbdp_start->lstatus;
2263 	}
2264 
2265 	/* Add TxPAL between FCB and frame if required */
2266 	if (unlikely(do_tstamp)) {
2267 		skb_push(skb, GMAC_TXPAL_LEN);
2268 		memset(skb->data, 0, GMAC_TXPAL_LEN);
2269 	}
2270 
2271 	/* Add TxFCB if required */
2272 	if (fcb_len) {
2273 		fcb = gfar_add_fcb(skb);
2274 		lstatus |= BD_LFLAG(TXBD_TOE);
2275 	}
2276 
2277 	/* Set up checksumming */
2278 	if (do_csum) {
2279 		gfar_tx_checksum(skb, fcb, fcb_len);
2280 
2281 		if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
2282 		    unlikely(gfar_csum_errata_76(priv, skb->len))) {
2283 			__skb_pull(skb, GMAC_FCB_LEN);
2284 			skb_checksum_help(skb);
2285 			if (do_vlan || do_tstamp) {
2286 				/* put back a new fcb for vlan/tstamp TOE */
2287 				fcb = gfar_add_fcb(skb);
2288 			} else {
2289 				/* Tx TOE not used */
2290 				lstatus &= ~(BD_LFLAG(TXBD_TOE));
2291 				fcb = NULL;
2292 			}
2293 		}
2294 	}
2295 
2296 	if (do_vlan)
2297 		gfar_tx_vlan(skb, fcb);
2298 
2299 	/* Setup tx hardware time stamping if requested */
2300 	if (unlikely(do_tstamp)) {
2301 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2302 		fcb->ptp = 1;
2303 	}
2304 
2305 	txbdp_start->bufPtr = dma_map_single(priv->dev, skb->data,
2306 					     skb_headlen(skb), DMA_TO_DEVICE);
2307 
2308 	/* If time stamping is requested one additional TxBD must be set up. The
2309 	 * first TxBD points to the FCB and must have a data length of
2310 	 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2311 	 * the full frame length.
2312 	 */
2313 	if (unlikely(do_tstamp)) {
2314 		txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_len;
2315 		txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
2316 					 (skb_headlen(skb) - fcb_len);
2317 		lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2318 	} else {
2319 		lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2320 	}
2321 
2322 	netdev_tx_sent_queue(txq, bytes_sent);
2323 
2324 	/* We can work in parallel with gfar_clean_tx_ring(), except
2325 	 * when modifying num_txbdfree. Note that we didn't grab the lock
2326 	 * when we were reading the num_txbdfree and checking for available
2327 	 * space, that's because outside of this function it can only grow,
2328 	 * and once we've got needed space, it cannot suddenly disappear.
2329 	 *
2330 	 * The lock also protects us from gfar_error(), which can modify
2331 	 * regs->tstat and thus retrigger the transfers, which is why we
2332 	 * also must grab the lock before setting ready bit for the first
2333 	 * to be transmitted BD.
2334 	 */
2335 	spin_lock_irqsave(&tx_queue->txlock, flags);
2336 
2337 	/* The powerpc-specific eieio() is used, as wmb() has too strong
2338 	 * semantics (it requires synchronization between cacheable and
2339 	 * uncacheable mappings, which eieio doesn't provide and which we
2340 	 * don't need), thus requiring a more expensive sync instruction.  At
2341 	 * some point, the set of architecture-independent barrier functions
2342 	 * should be expanded to include weaker barriers.
2343 	 */
2344 	eieio();
2345 
2346 	txbdp_start->lstatus = lstatus;
2347 
2348 	eieio(); /* force lstatus write before tx_skbuff */
2349 
2350 	tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2351 
2352 	/* Update the current skb pointer to the next entry we will use
2353 	 * (wrapping if necessary)
2354 	 */
2355 	tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
2356 			      TX_RING_MOD_MASK(tx_queue->tx_ring_size);
2357 
2358 	tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
2359 
2360 	/* reduce TxBD free count */
2361 	tx_queue->num_txbdfree -= (nr_txbds);
2362 
2363 	/* If the next BD still needs to be cleaned up, then the bds
2364 	 * are full.  We need to tell the kernel to stop sending us stuff.
2365 	 */
2366 	if (!tx_queue->num_txbdfree) {
2367 		netif_tx_stop_queue(txq);
2368 
2369 		dev->stats.tx_fifo_errors++;
2370 	}
2371 
2372 	/* Tell the DMA to go go go */
2373 	gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
2374 
2375 	/* Unlock priv */
2376 	spin_unlock_irqrestore(&tx_queue->txlock, flags);
2377 
2378 	return NETDEV_TX_OK;
2379 }
2380 
2381 /* Stops the kernel queue, and halts the controller */
2382 static int gfar_close(struct net_device *dev)
2383 {
2384 	struct gfar_private *priv = netdev_priv(dev);
2385 
2386 	cancel_work_sync(&priv->reset_task);
2387 	stop_gfar(dev);
2388 
2389 	/* Disconnect from the PHY */
2390 	phy_disconnect(priv->phydev);
2391 	priv->phydev = NULL;
2392 
2393 	gfar_free_irq(priv);
2394 
2395 	return 0;
2396 }
2397 
2398 /* Changes the mac address if the controller is not running. */
2399 static int gfar_set_mac_address(struct net_device *dev)
2400 {
2401 	gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
2402 
2403 	return 0;
2404 }
2405 
2406 static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2407 {
2408 	struct gfar_private *priv = netdev_priv(dev);
2409 	int frame_size = new_mtu + ETH_HLEN;
2410 
2411 	if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
2412 		netif_err(priv, drv, dev, "Invalid MTU setting\n");
2413 		return -EINVAL;
2414 	}
2415 
2416 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2417 		cpu_relax();
2418 
2419 	if (dev->flags & IFF_UP)
2420 		stop_gfar(dev);
2421 
2422 	dev->mtu = new_mtu;
2423 
2424 	if (dev->flags & IFF_UP)
2425 		startup_gfar(dev);
2426 
2427 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
2428 
2429 	return 0;
2430 }
2431 
2432 void reset_gfar(struct net_device *ndev)
2433 {
2434 	struct gfar_private *priv = netdev_priv(ndev);
2435 
2436 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2437 		cpu_relax();
2438 
2439 	stop_gfar(ndev);
2440 	startup_gfar(ndev);
2441 
2442 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
2443 }
2444 
2445 /* gfar_reset_task gets scheduled when a packet has not been
2446  * transmitted after a set amount of time.
2447  * For now, assume that clearing out all the structures, and
2448  * starting over will fix the problem.
2449  */
2450 static void gfar_reset_task(struct work_struct *work)
2451 {
2452 	struct gfar_private *priv = container_of(work, struct gfar_private,
2453 						 reset_task);
2454 	reset_gfar(priv->ndev);
2455 }
2456 
2457 static void gfar_timeout(struct net_device *dev)
2458 {
2459 	struct gfar_private *priv = netdev_priv(dev);
2460 
2461 	dev->stats.tx_errors++;
2462 	schedule_work(&priv->reset_task);
2463 }
2464 
2465 static void gfar_align_skb(struct sk_buff *skb)
2466 {
2467 	/* We need the data buffer to be aligned properly.  We will reserve
2468 	 * as many bytes as needed to align the data properly
2469 	 */
2470 	skb_reserve(skb, RXBUF_ALIGNMENT -
2471 		    (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
2472 }
2473 
2474 /* Interrupt Handler for Transmit complete */
2475 static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
2476 {
2477 	struct net_device *dev = tx_queue->dev;
2478 	struct netdev_queue *txq;
2479 	struct gfar_private *priv = netdev_priv(dev);
2480 	struct txbd8 *bdp, *next = NULL;
2481 	struct txbd8 *lbdp = NULL;
2482 	struct txbd8 *base = tx_queue->tx_bd_base;
2483 	struct sk_buff *skb;
2484 	int skb_dirtytx;
2485 	int tx_ring_size = tx_queue->tx_ring_size;
2486 	int frags = 0, nr_txbds = 0;
2487 	int i;
2488 	int howmany = 0;
2489 	int tqi = tx_queue->qindex;
2490 	unsigned int bytes_sent = 0;
2491 	u32 lstatus;
2492 	size_t buflen;
2493 
2494 	txq = netdev_get_tx_queue(dev, tqi);
2495 	bdp = tx_queue->dirty_tx;
2496 	skb_dirtytx = tx_queue->skb_dirtytx;
2497 
2498 	while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
2499 		unsigned long flags;
2500 
2501 		frags = skb_shinfo(skb)->nr_frags;
2502 
2503 		/* When time stamping, one additional TxBD must be freed.
2504 		 * Also, we need to dma_unmap_single() the TxPAL.
2505 		 */
2506 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
2507 			nr_txbds = frags + 2;
2508 		else
2509 			nr_txbds = frags + 1;
2510 
2511 		lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
2512 
2513 		lstatus = lbdp->lstatus;
2514 
2515 		/* Only clean completed frames */
2516 		if ((lstatus & BD_LFLAG(TXBD_READY)) &&
2517 		    (lstatus & BD_LENGTH_MASK))
2518 			break;
2519 
2520 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
2521 			next = next_txbd(bdp, base, tx_ring_size);
2522 			buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2523 		} else
2524 			buflen = bdp->length;
2525 
2526 		dma_unmap_single(priv->dev, bdp->bufPtr,
2527 				 buflen, DMA_TO_DEVICE);
2528 
2529 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
2530 			struct skb_shared_hwtstamps shhwtstamps;
2531 			u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
2532 
2533 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2534 			shhwtstamps.hwtstamp = ns_to_ktime(*ns);
2535 			skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
2536 			skb_tstamp_tx(skb, &shhwtstamps);
2537 			bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2538 			bdp = next;
2539 		}
2540 
2541 		bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2542 		bdp = next_txbd(bdp, base, tx_ring_size);
2543 
2544 		for (i = 0; i < frags; i++) {
2545 			dma_unmap_page(priv->dev, bdp->bufPtr,
2546 				       bdp->length, DMA_TO_DEVICE);
2547 			bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2548 			bdp = next_txbd(bdp, base, tx_ring_size);
2549 		}
2550 
2551 		bytes_sent += GFAR_CB(skb)->bytes_sent;
2552 
2553 		dev_kfree_skb_any(skb);
2554 
2555 		tx_queue->tx_skbuff[skb_dirtytx] = NULL;
2556 
2557 		skb_dirtytx = (skb_dirtytx + 1) &
2558 			      TX_RING_MOD_MASK(tx_ring_size);
2559 
2560 		howmany++;
2561 		spin_lock_irqsave(&tx_queue->txlock, flags);
2562 		tx_queue->num_txbdfree += nr_txbds;
2563 		spin_unlock_irqrestore(&tx_queue->txlock, flags);
2564 	}
2565 
2566 	/* If we freed a buffer, we can restart transmission, if necessary */
2567 	if (tx_queue->num_txbdfree &&
2568 	    netif_tx_queue_stopped(txq) &&
2569 	    !(test_bit(GFAR_DOWN, &priv->state)))
2570 		netif_wake_subqueue(priv->ndev, tqi);
2571 
2572 	/* Update dirty indicators */
2573 	tx_queue->skb_dirtytx = skb_dirtytx;
2574 	tx_queue->dirty_tx = bdp;
2575 
2576 	netdev_tx_completed_queue(txq, howmany, bytes_sent);
2577 }
2578 
2579 static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
2580 			   struct sk_buff *skb)
2581 {
2582 	struct net_device *dev = rx_queue->dev;
2583 	struct gfar_private *priv = netdev_priv(dev);
2584 	dma_addr_t buf;
2585 
2586 	buf = dma_map_single(priv->dev, skb->data,
2587 			     priv->rx_buffer_size, DMA_FROM_DEVICE);
2588 	gfar_init_rxbdp(rx_queue, bdp, buf);
2589 }
2590 
2591 static struct sk_buff *gfar_alloc_skb(struct net_device *dev)
2592 {
2593 	struct gfar_private *priv = netdev_priv(dev);
2594 	struct sk_buff *skb;
2595 
2596 	skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
2597 	if (!skb)
2598 		return NULL;
2599 
2600 	gfar_align_skb(skb);
2601 
2602 	return skb;
2603 }
2604 
2605 struct sk_buff *gfar_new_skb(struct net_device *dev)
2606 {
2607 	return gfar_alloc_skb(dev);
2608 }
2609 
2610 static inline void count_errors(unsigned short status, struct net_device *dev)
2611 {
2612 	struct gfar_private *priv = netdev_priv(dev);
2613 	struct net_device_stats *stats = &dev->stats;
2614 	struct gfar_extra_stats *estats = &priv->extra_stats;
2615 
2616 	/* If the packet was truncated, none of the other errors matter */
2617 	if (status & RXBD_TRUNCATED) {
2618 		stats->rx_length_errors++;
2619 
2620 		atomic64_inc(&estats->rx_trunc);
2621 
2622 		return;
2623 	}
2624 	/* Count the errors, if there were any */
2625 	if (status & (RXBD_LARGE | RXBD_SHORT)) {
2626 		stats->rx_length_errors++;
2627 
2628 		if (status & RXBD_LARGE)
2629 			atomic64_inc(&estats->rx_large);
2630 		else
2631 			atomic64_inc(&estats->rx_short);
2632 	}
2633 	if (status & RXBD_NONOCTET) {
2634 		stats->rx_frame_errors++;
2635 		atomic64_inc(&estats->rx_nonoctet);
2636 	}
2637 	if (status & RXBD_CRCERR) {
2638 		atomic64_inc(&estats->rx_crcerr);
2639 		stats->rx_crc_errors++;
2640 	}
2641 	if (status & RXBD_OVERRUN) {
2642 		atomic64_inc(&estats->rx_overrun);
2643 		stats->rx_crc_errors++;
2644 	}
2645 }
2646 
2647 irqreturn_t gfar_receive(int irq, void *grp_id)
2648 {
2649 	struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2650 	unsigned long flags;
2651 	u32 imask;
2652 
2653 	if (likely(napi_schedule_prep(&grp->napi_rx))) {
2654 		spin_lock_irqsave(&grp->grplock, flags);
2655 		imask = gfar_read(&grp->regs->imask);
2656 		imask &= IMASK_RX_DISABLED;
2657 		gfar_write(&grp->regs->imask, imask);
2658 		spin_unlock_irqrestore(&grp->grplock, flags);
2659 		__napi_schedule(&grp->napi_rx);
2660 	} else {
2661 		/* Clear IEVENT, so interrupts aren't called again
2662 		 * because of the packets that have already arrived.
2663 		 */
2664 		gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
2665 	}
2666 
2667 	return IRQ_HANDLED;
2668 }
2669 
2670 /* Interrupt Handler for Transmit complete */
2671 static irqreturn_t gfar_transmit(int irq, void *grp_id)
2672 {
2673 	struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2674 	unsigned long flags;
2675 	u32 imask;
2676 
2677 	if (likely(napi_schedule_prep(&grp->napi_tx))) {
2678 		spin_lock_irqsave(&grp->grplock, flags);
2679 		imask = gfar_read(&grp->regs->imask);
2680 		imask &= IMASK_TX_DISABLED;
2681 		gfar_write(&grp->regs->imask, imask);
2682 		spin_unlock_irqrestore(&grp->grplock, flags);
2683 		__napi_schedule(&grp->napi_tx);
2684 	} else {
2685 		/* Clear IEVENT, so interrupts aren't called again
2686 		 * because of the packets that have already arrived.
2687 		 */
2688 		gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
2689 	}
2690 
2691 	return IRQ_HANDLED;
2692 }
2693 
2694 static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2695 {
2696 	/* If valid headers were found, and valid sums
2697 	 * were verified, then we tell the kernel that no
2698 	 * checksumming is necessary.  Otherwise, it is [FIXME]
2699 	 */
2700 	if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
2701 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2702 	else
2703 		skb_checksum_none_assert(skb);
2704 }
2705 
2706 
2707 /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
2708 static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
2709 			       int amount_pull, struct napi_struct *napi)
2710 {
2711 	struct gfar_private *priv = netdev_priv(dev);
2712 	struct rxfcb *fcb = NULL;
2713 
2714 	/* fcb is at the beginning if exists */
2715 	fcb = (struct rxfcb *)skb->data;
2716 
2717 	/* Remove the FCB from the skb
2718 	 * Remove the padded bytes, if there are any
2719 	 */
2720 	if (amount_pull) {
2721 		skb_record_rx_queue(skb, fcb->rq);
2722 		skb_pull(skb, amount_pull);
2723 	}
2724 
2725 	/* Get receive timestamp from the skb */
2726 	if (priv->hwts_rx_en) {
2727 		struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2728 		u64 *ns = (u64 *) skb->data;
2729 
2730 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2731 		shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2732 	}
2733 
2734 	if (priv->padding)
2735 		skb_pull(skb, priv->padding);
2736 
2737 	if (dev->features & NETIF_F_RXCSUM)
2738 		gfar_rx_checksum(skb, fcb);
2739 
2740 	/* Tell the skb what kind of packet this is */
2741 	skb->protocol = eth_type_trans(skb, dev);
2742 
2743 	/* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
2744 	 * Even if vlan rx accel is disabled, on some chips
2745 	 * RXFCB_VLN is pseudo randomly set.
2746 	 */
2747 	if (dev->features & NETIF_F_HW_VLAN_CTAG_RX &&
2748 	    fcb->flags & RXFCB_VLN)
2749 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), fcb->vlctl);
2750 
2751 	/* Send the packet up the stack */
2752 	napi_gro_receive(napi, skb);
2753 
2754 }
2755 
2756 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
2757  * until the budget/quota has been reached. Returns the number
2758  * of frames handled
2759  */
2760 int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
2761 {
2762 	struct net_device *dev = rx_queue->dev;
2763 	struct rxbd8 *bdp, *base;
2764 	struct sk_buff *skb;
2765 	int pkt_len;
2766 	int amount_pull;
2767 	int howmany = 0;
2768 	struct gfar_private *priv = netdev_priv(dev);
2769 
2770 	/* Get the first full descriptor */
2771 	bdp = rx_queue->cur_rx;
2772 	base = rx_queue->rx_bd_base;
2773 
2774 	amount_pull = priv->uses_rxfcb ? GMAC_FCB_LEN : 0;
2775 
2776 	while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
2777 		struct sk_buff *newskb;
2778 
2779 		rmb();
2780 
2781 		/* Add another skb for the future */
2782 		newskb = gfar_new_skb(dev);
2783 
2784 		skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
2785 
2786 		dma_unmap_single(priv->dev, bdp->bufPtr,
2787 				 priv->rx_buffer_size, DMA_FROM_DEVICE);
2788 
2789 		if (unlikely(!(bdp->status & RXBD_ERR) &&
2790 			     bdp->length > priv->rx_buffer_size))
2791 			bdp->status = RXBD_LARGE;
2792 
2793 		/* We drop the frame if we failed to allocate a new buffer */
2794 		if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
2795 			     bdp->status & RXBD_ERR)) {
2796 			count_errors(bdp->status, dev);
2797 
2798 			if (unlikely(!newskb))
2799 				newskb = skb;
2800 			else if (skb)
2801 				dev_kfree_skb(skb);
2802 		} else {
2803 			/* Increment the number of packets */
2804 			rx_queue->stats.rx_packets++;
2805 			howmany++;
2806 
2807 			if (likely(skb)) {
2808 				pkt_len = bdp->length - ETH_FCS_LEN;
2809 				/* Remove the FCS from the packet length */
2810 				skb_put(skb, pkt_len);
2811 				rx_queue->stats.rx_bytes += pkt_len;
2812 				skb_record_rx_queue(skb, rx_queue->qindex);
2813 				gfar_process_frame(dev, skb, amount_pull,
2814 						   &rx_queue->grp->napi_rx);
2815 
2816 			} else {
2817 				netif_warn(priv, rx_err, dev, "Missing skb!\n");
2818 				rx_queue->stats.rx_dropped++;
2819 				atomic64_inc(&priv->extra_stats.rx_skbmissing);
2820 			}
2821 
2822 		}
2823 
2824 		rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
2825 
2826 		/* Setup the new bdp */
2827 		gfar_new_rxbdp(rx_queue, bdp, newskb);
2828 
2829 		/* Update to the next pointer */
2830 		bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
2831 
2832 		/* update to point at the next skb */
2833 		rx_queue->skb_currx = (rx_queue->skb_currx + 1) &
2834 				      RX_RING_MOD_MASK(rx_queue->rx_ring_size);
2835 	}
2836 
2837 	/* Update the current rxbd pointer to be the next one */
2838 	rx_queue->cur_rx = bdp;
2839 
2840 	return howmany;
2841 }
2842 
2843 static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
2844 {
2845 	struct gfar_priv_grp *gfargrp =
2846 		container_of(napi, struct gfar_priv_grp, napi_rx);
2847 	struct gfar __iomem *regs = gfargrp->regs;
2848 	struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
2849 	int work_done = 0;
2850 
2851 	/* Clear IEVENT, so interrupts aren't called again
2852 	 * because of the packets that have already arrived
2853 	 */
2854 	gfar_write(&regs->ievent, IEVENT_RX_MASK);
2855 
2856 	work_done = gfar_clean_rx_ring(rx_queue, budget);
2857 
2858 	if (work_done < budget) {
2859 		u32 imask;
2860 		napi_complete(napi);
2861 		/* Clear the halt bit in RSTAT */
2862 		gfar_write(&regs->rstat, gfargrp->rstat);
2863 
2864 		spin_lock_irq(&gfargrp->grplock);
2865 		imask = gfar_read(&regs->imask);
2866 		imask |= IMASK_RX_DEFAULT;
2867 		gfar_write(&regs->imask, imask);
2868 		spin_unlock_irq(&gfargrp->grplock);
2869 	}
2870 
2871 	return work_done;
2872 }
2873 
2874 static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
2875 {
2876 	struct gfar_priv_grp *gfargrp =
2877 		container_of(napi, struct gfar_priv_grp, napi_tx);
2878 	struct gfar __iomem *regs = gfargrp->regs;
2879 	struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
2880 	u32 imask;
2881 
2882 	/* Clear IEVENT, so interrupts aren't called again
2883 	 * because of the packets that have already arrived
2884 	 */
2885 	gfar_write(&regs->ievent, IEVENT_TX_MASK);
2886 
2887 	/* run Tx cleanup to completion */
2888 	if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
2889 		gfar_clean_tx_ring(tx_queue);
2890 
2891 	napi_complete(napi);
2892 
2893 	spin_lock_irq(&gfargrp->grplock);
2894 	imask = gfar_read(&regs->imask);
2895 	imask |= IMASK_TX_DEFAULT;
2896 	gfar_write(&regs->imask, imask);
2897 	spin_unlock_irq(&gfargrp->grplock);
2898 
2899 	return 0;
2900 }
2901 
2902 static int gfar_poll_rx(struct napi_struct *napi, int budget)
2903 {
2904 	struct gfar_priv_grp *gfargrp =
2905 		container_of(napi, struct gfar_priv_grp, napi_rx);
2906 	struct gfar_private *priv = gfargrp->priv;
2907 	struct gfar __iomem *regs = gfargrp->regs;
2908 	struct gfar_priv_rx_q *rx_queue = NULL;
2909 	int work_done = 0, work_done_per_q = 0;
2910 	int i, budget_per_q = 0;
2911 	unsigned long rstat_rxf;
2912 	int num_act_queues;
2913 
2914 	/* Clear IEVENT, so interrupts aren't called again
2915 	 * because of the packets that have already arrived
2916 	 */
2917 	gfar_write(&regs->ievent, IEVENT_RX_MASK);
2918 
2919 	rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
2920 
2921 	num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
2922 	if (num_act_queues)
2923 		budget_per_q = budget/num_act_queues;
2924 
2925 	for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
2926 		/* skip queue if not active */
2927 		if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
2928 			continue;
2929 
2930 		rx_queue = priv->rx_queue[i];
2931 		work_done_per_q =
2932 			gfar_clean_rx_ring(rx_queue, budget_per_q);
2933 		work_done += work_done_per_q;
2934 
2935 		/* finished processing this queue */
2936 		if (work_done_per_q < budget_per_q) {
2937 			/* clear active queue hw indication */
2938 			gfar_write(&regs->rstat,
2939 				   RSTAT_CLEAR_RXF0 >> i);
2940 			num_act_queues--;
2941 
2942 			if (!num_act_queues)
2943 				break;
2944 		}
2945 	}
2946 
2947 	if (!num_act_queues) {
2948 		u32 imask;
2949 		napi_complete(napi);
2950 
2951 		/* Clear the halt bit in RSTAT */
2952 		gfar_write(&regs->rstat, gfargrp->rstat);
2953 
2954 		spin_lock_irq(&gfargrp->grplock);
2955 		imask = gfar_read(&regs->imask);
2956 		imask |= IMASK_RX_DEFAULT;
2957 		gfar_write(&regs->imask, imask);
2958 		spin_unlock_irq(&gfargrp->grplock);
2959 	}
2960 
2961 	return work_done;
2962 }
2963 
2964 static int gfar_poll_tx(struct napi_struct *napi, int budget)
2965 {
2966 	struct gfar_priv_grp *gfargrp =
2967 		container_of(napi, struct gfar_priv_grp, napi_tx);
2968 	struct gfar_private *priv = gfargrp->priv;
2969 	struct gfar __iomem *regs = gfargrp->regs;
2970 	struct gfar_priv_tx_q *tx_queue = NULL;
2971 	int has_tx_work = 0;
2972 	int i;
2973 
2974 	/* Clear IEVENT, so interrupts aren't called again
2975 	 * because of the packets that have already arrived
2976 	 */
2977 	gfar_write(&regs->ievent, IEVENT_TX_MASK);
2978 
2979 	for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
2980 		tx_queue = priv->tx_queue[i];
2981 		/* run Tx cleanup to completion */
2982 		if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
2983 			gfar_clean_tx_ring(tx_queue);
2984 			has_tx_work = 1;
2985 		}
2986 	}
2987 
2988 	if (!has_tx_work) {
2989 		u32 imask;
2990 		napi_complete(napi);
2991 
2992 		spin_lock_irq(&gfargrp->grplock);
2993 		imask = gfar_read(&regs->imask);
2994 		imask |= IMASK_TX_DEFAULT;
2995 		gfar_write(&regs->imask, imask);
2996 		spin_unlock_irq(&gfargrp->grplock);
2997 	}
2998 
2999 	return 0;
3000 }
3001 
3002 
3003 #ifdef CONFIG_NET_POLL_CONTROLLER
3004 /* Polling 'interrupt' - used by things like netconsole to send skbs
3005  * without having to re-enable interrupts. It's not called while
3006  * the interrupt routine is executing.
3007  */
3008 static void gfar_netpoll(struct net_device *dev)
3009 {
3010 	struct gfar_private *priv = netdev_priv(dev);
3011 	int i;
3012 
3013 	/* If the device has multiple interrupts, run tx/rx */
3014 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
3015 		for (i = 0; i < priv->num_grps; i++) {
3016 			struct gfar_priv_grp *grp = &priv->gfargrp[i];
3017 
3018 			disable_irq(gfar_irq(grp, TX)->irq);
3019 			disable_irq(gfar_irq(grp, RX)->irq);
3020 			disable_irq(gfar_irq(grp, ER)->irq);
3021 			gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3022 			enable_irq(gfar_irq(grp, ER)->irq);
3023 			enable_irq(gfar_irq(grp, RX)->irq);
3024 			enable_irq(gfar_irq(grp, TX)->irq);
3025 		}
3026 	} else {
3027 		for (i = 0; i < priv->num_grps; i++) {
3028 			struct gfar_priv_grp *grp = &priv->gfargrp[i];
3029 
3030 			disable_irq(gfar_irq(grp, TX)->irq);
3031 			gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3032 			enable_irq(gfar_irq(grp, TX)->irq);
3033 		}
3034 	}
3035 }
3036 #endif
3037 
3038 /* The interrupt handler for devices with one interrupt */
3039 static irqreturn_t gfar_interrupt(int irq, void *grp_id)
3040 {
3041 	struct gfar_priv_grp *gfargrp = grp_id;
3042 
3043 	/* Save ievent for future reference */
3044 	u32 events = gfar_read(&gfargrp->regs->ievent);
3045 
3046 	/* Check for reception */
3047 	if (events & IEVENT_RX_MASK)
3048 		gfar_receive(irq, grp_id);
3049 
3050 	/* Check for transmit completion */
3051 	if (events & IEVENT_TX_MASK)
3052 		gfar_transmit(irq, grp_id);
3053 
3054 	/* Check for errors */
3055 	if (events & IEVENT_ERR_MASK)
3056 		gfar_error(irq, grp_id);
3057 
3058 	return IRQ_HANDLED;
3059 }
3060 
3061 static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
3062 {
3063 	struct phy_device *phydev = priv->phydev;
3064 	u32 val = 0;
3065 
3066 	if (!phydev->duplex)
3067 		return val;
3068 
3069 	if (!priv->pause_aneg_en) {
3070 		if (priv->tx_pause_en)
3071 			val |= MACCFG1_TX_FLOW;
3072 		if (priv->rx_pause_en)
3073 			val |= MACCFG1_RX_FLOW;
3074 	} else {
3075 		u16 lcl_adv, rmt_adv;
3076 		u8 flowctrl;
3077 		/* get link partner capabilities */
3078 		rmt_adv = 0;
3079 		if (phydev->pause)
3080 			rmt_adv = LPA_PAUSE_CAP;
3081 		if (phydev->asym_pause)
3082 			rmt_adv |= LPA_PAUSE_ASYM;
3083 
3084 		lcl_adv = mii_advertise_flowctrl(phydev->advertising);
3085 
3086 		flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
3087 		if (flowctrl & FLOW_CTRL_TX)
3088 			val |= MACCFG1_TX_FLOW;
3089 		if (flowctrl & FLOW_CTRL_RX)
3090 			val |= MACCFG1_RX_FLOW;
3091 	}
3092 
3093 	return val;
3094 }
3095 
3096 /* Called every time the controller might need to be made
3097  * aware of new link state.  The PHY code conveys this
3098  * information through variables in the phydev structure, and this
3099  * function converts those variables into the appropriate
3100  * register values, and can bring down the device if needed.
3101  */
3102 static void adjust_link(struct net_device *dev)
3103 {
3104 	struct gfar_private *priv = netdev_priv(dev);
3105 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3106 	struct phy_device *phydev = priv->phydev;
3107 	int new_state = 0;
3108 
3109 	if (test_bit(GFAR_RESETTING, &priv->state))
3110 		return;
3111 
3112 	if (phydev->link) {
3113 		u32 tempval1 = gfar_read(&regs->maccfg1);
3114 		u32 tempval = gfar_read(&regs->maccfg2);
3115 		u32 ecntrl = gfar_read(&regs->ecntrl);
3116 
3117 		/* Now we make sure that we can be in full duplex mode.
3118 		 * If not, we operate in half-duplex mode.
3119 		 */
3120 		if (phydev->duplex != priv->oldduplex) {
3121 			new_state = 1;
3122 			if (!(phydev->duplex))
3123 				tempval &= ~(MACCFG2_FULL_DUPLEX);
3124 			else
3125 				tempval |= MACCFG2_FULL_DUPLEX;
3126 
3127 			priv->oldduplex = phydev->duplex;
3128 		}
3129 
3130 		if (phydev->speed != priv->oldspeed) {
3131 			new_state = 1;
3132 			switch (phydev->speed) {
3133 			case 1000:
3134 				tempval =
3135 				    ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
3136 
3137 				ecntrl &= ~(ECNTRL_R100);
3138 				break;
3139 			case 100:
3140 			case 10:
3141 				tempval =
3142 				    ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
3143 
3144 				/* Reduced mode distinguishes
3145 				 * between 10 and 100
3146 				 */
3147 				if (phydev->speed == SPEED_100)
3148 					ecntrl |= ECNTRL_R100;
3149 				else
3150 					ecntrl &= ~(ECNTRL_R100);
3151 				break;
3152 			default:
3153 				netif_warn(priv, link, dev,
3154 					   "Ack!  Speed (%d) is not 10/100/1000!\n",
3155 					   phydev->speed);
3156 				break;
3157 			}
3158 
3159 			priv->oldspeed = phydev->speed;
3160 		}
3161 
3162 		tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
3163 		tempval1 |= gfar_get_flowctrl_cfg(priv);
3164 
3165 		gfar_write(&regs->maccfg1, tempval1);
3166 		gfar_write(&regs->maccfg2, tempval);
3167 		gfar_write(&regs->ecntrl, ecntrl);
3168 
3169 		if (!priv->oldlink) {
3170 			new_state = 1;
3171 			priv->oldlink = 1;
3172 		}
3173 	} else if (priv->oldlink) {
3174 		new_state = 1;
3175 		priv->oldlink = 0;
3176 		priv->oldspeed = 0;
3177 		priv->oldduplex = -1;
3178 	}
3179 
3180 	if (new_state && netif_msg_link(priv))
3181 		phy_print_status(phydev);
3182 }
3183 
3184 /* Update the hash table based on the current list of multicast
3185  * addresses we subscribe to.  Also, change the promiscuity of
3186  * the device based on the flags (this function is called
3187  * whenever dev->flags is changed
3188  */
3189 static void gfar_set_multi(struct net_device *dev)
3190 {
3191 	struct netdev_hw_addr *ha;
3192 	struct gfar_private *priv = netdev_priv(dev);
3193 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3194 	u32 tempval;
3195 
3196 	if (dev->flags & IFF_PROMISC) {
3197 		/* Set RCTRL to PROM */
3198 		tempval = gfar_read(&regs->rctrl);
3199 		tempval |= RCTRL_PROM;
3200 		gfar_write(&regs->rctrl, tempval);
3201 	} else {
3202 		/* Set RCTRL to not PROM */
3203 		tempval = gfar_read(&regs->rctrl);
3204 		tempval &= ~(RCTRL_PROM);
3205 		gfar_write(&regs->rctrl, tempval);
3206 	}
3207 
3208 	if (dev->flags & IFF_ALLMULTI) {
3209 		/* Set the hash to rx all multicast frames */
3210 		gfar_write(&regs->igaddr0, 0xffffffff);
3211 		gfar_write(&regs->igaddr1, 0xffffffff);
3212 		gfar_write(&regs->igaddr2, 0xffffffff);
3213 		gfar_write(&regs->igaddr3, 0xffffffff);
3214 		gfar_write(&regs->igaddr4, 0xffffffff);
3215 		gfar_write(&regs->igaddr5, 0xffffffff);
3216 		gfar_write(&regs->igaddr6, 0xffffffff);
3217 		gfar_write(&regs->igaddr7, 0xffffffff);
3218 		gfar_write(&regs->gaddr0, 0xffffffff);
3219 		gfar_write(&regs->gaddr1, 0xffffffff);
3220 		gfar_write(&regs->gaddr2, 0xffffffff);
3221 		gfar_write(&regs->gaddr3, 0xffffffff);
3222 		gfar_write(&regs->gaddr4, 0xffffffff);
3223 		gfar_write(&regs->gaddr5, 0xffffffff);
3224 		gfar_write(&regs->gaddr6, 0xffffffff);
3225 		gfar_write(&regs->gaddr7, 0xffffffff);
3226 	} else {
3227 		int em_num;
3228 		int idx;
3229 
3230 		/* zero out the hash */
3231 		gfar_write(&regs->igaddr0, 0x0);
3232 		gfar_write(&regs->igaddr1, 0x0);
3233 		gfar_write(&regs->igaddr2, 0x0);
3234 		gfar_write(&regs->igaddr3, 0x0);
3235 		gfar_write(&regs->igaddr4, 0x0);
3236 		gfar_write(&regs->igaddr5, 0x0);
3237 		gfar_write(&regs->igaddr6, 0x0);
3238 		gfar_write(&regs->igaddr7, 0x0);
3239 		gfar_write(&regs->gaddr0, 0x0);
3240 		gfar_write(&regs->gaddr1, 0x0);
3241 		gfar_write(&regs->gaddr2, 0x0);
3242 		gfar_write(&regs->gaddr3, 0x0);
3243 		gfar_write(&regs->gaddr4, 0x0);
3244 		gfar_write(&regs->gaddr5, 0x0);
3245 		gfar_write(&regs->gaddr6, 0x0);
3246 		gfar_write(&regs->gaddr7, 0x0);
3247 
3248 		/* If we have extended hash tables, we need to
3249 		 * clear the exact match registers to prepare for
3250 		 * setting them
3251 		 */
3252 		if (priv->extended_hash) {
3253 			em_num = GFAR_EM_NUM + 1;
3254 			gfar_clear_exact_match(dev);
3255 			idx = 1;
3256 		} else {
3257 			idx = 0;
3258 			em_num = 0;
3259 		}
3260 
3261 		if (netdev_mc_empty(dev))
3262 			return;
3263 
3264 		/* Parse the list, and set the appropriate bits */
3265 		netdev_for_each_mc_addr(ha, dev) {
3266 			if (idx < em_num) {
3267 				gfar_set_mac_for_addr(dev, idx, ha->addr);
3268 				idx++;
3269 			} else
3270 				gfar_set_hash_for_addr(dev, ha->addr);
3271 		}
3272 	}
3273 }
3274 
3275 
3276 /* Clears each of the exact match registers to zero, so they
3277  * don't interfere with normal reception
3278  */
3279 static void gfar_clear_exact_match(struct net_device *dev)
3280 {
3281 	int idx;
3282 	static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
3283 
3284 	for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
3285 		gfar_set_mac_for_addr(dev, idx, zero_arr);
3286 }
3287 
3288 /* Set the appropriate hash bit for the given addr */
3289 /* The algorithm works like so:
3290  * 1) Take the Destination Address (ie the multicast address), and
3291  * do a CRC on it (little endian), and reverse the bits of the
3292  * result.
3293  * 2) Use the 8 most significant bits as a hash into a 256-entry
3294  * table.  The table is controlled through 8 32-bit registers:
3295  * gaddr0-7.  gaddr0's MSB is entry 0, and gaddr7's LSB is
3296  * gaddr7.  This means that the 3 most significant bits in the
3297  * hash index which gaddr register to use, and the 5 other bits
3298  * indicate which bit (assuming an IBM numbering scheme, which
3299  * for PowerPC (tm) is usually the case) in the register holds
3300  * the entry.
3301  */
3302 static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3303 {
3304 	u32 tempval;
3305 	struct gfar_private *priv = netdev_priv(dev);
3306 	u32 result = ether_crc(ETH_ALEN, addr);
3307 	int width = priv->hash_width;
3308 	u8 whichbit = (result >> (32 - width)) & 0x1f;
3309 	u8 whichreg = result >> (32 - width + 5);
3310 	u32 value = (1 << (31-whichbit));
3311 
3312 	tempval = gfar_read(priv->hash_regs[whichreg]);
3313 	tempval |= value;
3314 	gfar_write(priv->hash_regs[whichreg], tempval);
3315 }
3316 
3317 
3318 /* There are multiple MAC Address register pairs on some controllers
3319  * This function sets the numth pair to a given address
3320  */
3321 static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3322 				  const u8 *addr)
3323 {
3324 	struct gfar_private *priv = netdev_priv(dev);
3325 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3326 	int idx;
3327 	char tmpbuf[ETH_ALEN];
3328 	u32 tempval;
3329 	u32 __iomem *macptr = &regs->macstnaddr1;
3330 
3331 	macptr += num*2;
3332 
3333 	/* Now copy it into the mac registers backwards, cuz
3334 	 * little endian is silly
3335 	 */
3336 	for (idx = 0; idx < ETH_ALEN; idx++)
3337 		tmpbuf[ETH_ALEN - 1 - idx] = addr[idx];
3338 
3339 	gfar_write(macptr, *((u32 *) (tmpbuf)));
3340 
3341 	tempval = *((u32 *) (tmpbuf + 4));
3342 
3343 	gfar_write(macptr+1, tempval);
3344 }
3345 
3346 /* GFAR error interrupt handler */
3347 static irqreturn_t gfar_error(int irq, void *grp_id)
3348 {
3349 	struct gfar_priv_grp *gfargrp = grp_id;
3350 	struct gfar __iomem *regs = gfargrp->regs;
3351 	struct gfar_private *priv= gfargrp->priv;
3352 	struct net_device *dev = priv->ndev;
3353 
3354 	/* Save ievent for future reference */
3355 	u32 events = gfar_read(&regs->ievent);
3356 
3357 	/* Clear IEVENT */
3358 	gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
3359 
3360 	/* Magic Packet is not an error. */
3361 	if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
3362 	    (events & IEVENT_MAG))
3363 		events &= ~IEVENT_MAG;
3364 
3365 	/* Hmm... */
3366 	if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
3367 		netdev_dbg(dev,
3368 			   "error interrupt (ievent=0x%08x imask=0x%08x)\n",
3369 			   events, gfar_read(&regs->imask));
3370 
3371 	/* Update the error counters */
3372 	if (events & IEVENT_TXE) {
3373 		dev->stats.tx_errors++;
3374 
3375 		if (events & IEVENT_LC)
3376 			dev->stats.tx_window_errors++;
3377 		if (events & IEVENT_CRL)
3378 			dev->stats.tx_aborted_errors++;
3379 		if (events & IEVENT_XFUN) {
3380 			unsigned long flags;
3381 
3382 			netif_dbg(priv, tx_err, dev,
3383 				  "TX FIFO underrun, packet dropped\n");
3384 			dev->stats.tx_dropped++;
3385 			atomic64_inc(&priv->extra_stats.tx_underrun);
3386 
3387 			local_irq_save(flags);
3388 			lock_tx_qs(priv);
3389 
3390 			/* Reactivate the Tx Queues */
3391 			gfar_write(&regs->tstat, gfargrp->tstat);
3392 
3393 			unlock_tx_qs(priv);
3394 			local_irq_restore(flags);
3395 		}
3396 		netif_dbg(priv, tx_err, dev, "Transmit Error\n");
3397 	}
3398 	if (events & IEVENT_BSY) {
3399 		dev->stats.rx_errors++;
3400 		atomic64_inc(&priv->extra_stats.rx_bsy);
3401 
3402 		gfar_receive(irq, grp_id);
3403 
3404 		netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3405 			  gfar_read(&regs->rstat));
3406 	}
3407 	if (events & IEVENT_BABR) {
3408 		dev->stats.rx_errors++;
3409 		atomic64_inc(&priv->extra_stats.rx_babr);
3410 
3411 		netif_dbg(priv, rx_err, dev, "babbling RX error\n");
3412 	}
3413 	if (events & IEVENT_EBERR) {
3414 		atomic64_inc(&priv->extra_stats.eberr);
3415 		netif_dbg(priv, rx_err, dev, "bus error\n");
3416 	}
3417 	if (events & IEVENT_RXC)
3418 		netif_dbg(priv, rx_status, dev, "control frame\n");
3419 
3420 	if (events & IEVENT_BABT) {
3421 		atomic64_inc(&priv->extra_stats.tx_babt);
3422 		netif_dbg(priv, tx_err, dev, "babbling TX error\n");
3423 	}
3424 	return IRQ_HANDLED;
3425 }
3426 
3427 static struct of_device_id gfar_match[] =
3428 {
3429 	{
3430 		.type = "network",
3431 		.compatible = "gianfar",
3432 	},
3433 	{
3434 		.compatible = "fsl,etsec2",
3435 	},
3436 	{},
3437 };
3438 MODULE_DEVICE_TABLE(of, gfar_match);
3439 
3440 /* Structure for a device driver */
3441 static struct platform_driver gfar_driver = {
3442 	.driver = {
3443 		.name = "fsl-gianfar",
3444 		.owner = THIS_MODULE,
3445 		.pm = GFAR_PM_OPS,
3446 		.of_match_table = gfar_match,
3447 	},
3448 	.probe = gfar_probe,
3449 	.remove = gfar_remove,
3450 };
3451 
3452 module_platform_driver(gfar_driver);
3453