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