xref: /linux/drivers/net/ethernet/freescale/gianfar.c (revision 87320be9f0d24fce67631b7eef919f0b79c3e45c)
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 
gfar_init_rxbdp(struct gfar_priv_rx_q * rx_queue,struct rxbd8 * bdp,dma_addr_t buf)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 
gfar_init_tx_rx_base(struct gfar_private * priv)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 
gfar_init_rqprm(struct gfar_private * priv)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 
gfar_rx_offload_en(struct gfar_private * priv)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 
gfar_mac_rx_config(struct gfar_private * priv)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 
gfar_mac_tx_config(struct gfar_private * priv)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 
gfar_configure_coalescing(struct gfar_private * priv,unsigned long tx_mask,unsigned long rx_mask)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 
gfar_configure_coalescing_all(struct gfar_private * priv)273 static void gfar_configure_coalescing_all(struct gfar_private *priv)
274 {
275 	gfar_configure_coalescing(priv, 0xFF, 0xFF);
276 }
277 
gfar_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)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  */
gfar_set_hash_for_addr(struct net_device * dev,u8 * addr)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  */
gfar_set_mac_for_addr(struct net_device * dev,int num,const u8 * addr)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 
gfar_set_mac_addr(struct net_device * dev,void * p)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 
gfar_ints_disable(struct gfar_private * priv)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 
gfar_ints_enable(struct gfar_private * priv)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 
gfar_alloc_tx_queues(struct gfar_private * priv)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 
gfar_alloc_rx_queues(struct gfar_private * priv)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 
gfar_free_tx_queues(struct gfar_private * priv)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 
gfar_free_rx_queues(struct gfar_private * priv)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 
unmap_group_regs(struct gfar_private * priv)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 
free_gfar_dev(struct gfar_private * priv)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 
disable_napi(struct gfar_private * priv)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 
enable_napi(struct gfar_private * priv)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 
gfar_parse_group(struct device_node * np,struct gfar_private * priv,const char * model)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  */
gfar_get_interface(struct net_device * dev)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 
gfar_of_init(struct platform_device * ofdev,struct net_device ** pdev)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 
cluster_entry_per_class(struct gfar_private * priv,u32 rqfar,u32 class)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 
gfar_init_filer_table(struct gfar_private * priv)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
__gfar_detect_errata_83xx(struct gfar_private * priv)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 
__gfar_detect_errata_85xx(struct gfar_private * priv)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 
gfar_detect_errata(struct gfar_private * priv)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 
gfar_init_addr_hash_table(struct gfar_private * priv)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 
__gfar_is_rx_idle(struct gfar_private * priv)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 */
gfar_halt_nodisable(struct gfar_private * priv)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 */
gfar_halt(struct gfar_private * priv)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 
free_skb_tx_queue(struct gfar_priv_tx_q * tx_queue)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 
free_skb_rx_queue(struct gfar_priv_rx_q * rx_queue)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 
1096 	for (i = 0; i < rx_queue->rx_ring_size; i++) {
1097 		struct	gfar_rx_buff *rxb = &rx_queue->rx_buff[i];
1098 
1099 		rxbdp->lstatus = 0;
1100 		rxbdp->bufPtr = 0;
1101 		rxbdp++;
1102 
1103 		if (!rxb->page)
1104 			continue;
1105 
1106 		dma_unmap_page(rx_queue->dev, rxb->dma,
1107 			       PAGE_SIZE, DMA_FROM_DEVICE);
1108 		__free_page(rxb->page);
1109 
1110 		rxb->page = NULL;
1111 	}
1112 
1113 	kfree(rx_queue->rx_buff);
1114 	rx_queue->rx_buff = NULL;
1115 }
1116 
1117 /* If there are any tx skbs or rx skbs still around, free them.
1118  * Then free tx_skbuff and rx_skbuff
1119  */
free_skb_resources(struct gfar_private * priv)1120 static void free_skb_resources(struct gfar_private *priv)
1121 {
1122 	struct gfar_priv_tx_q *tx_queue = NULL;
1123 	struct gfar_priv_rx_q *rx_queue = NULL;
1124 	int i;
1125 
1126 	/* Go through all the buffer descriptors and free their data buffers */
1127 	for (i = 0; i < priv->num_tx_queues; i++) {
1128 		struct netdev_queue *txq;
1129 
1130 		tx_queue = priv->tx_queue[i];
1131 		txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
1132 		if (tx_queue->tx_skbuff)
1133 			free_skb_tx_queue(tx_queue);
1134 		netdev_tx_reset_queue(txq);
1135 	}
1136 
1137 	for (i = 0; i < priv->num_rx_queues; i++) {
1138 		rx_queue = priv->rx_queue[i];
1139 		if (rx_queue->rx_buff)
1140 			free_skb_rx_queue(rx_queue);
1141 	}
1142 
1143 	dma_free_coherent(priv->dev,
1144 			  sizeof(struct txbd8) * priv->total_tx_ring_size +
1145 			  sizeof(struct rxbd8) * priv->total_rx_ring_size,
1146 			  priv->tx_queue[0]->tx_bd_base,
1147 			  priv->tx_queue[0]->tx_bd_dma_base);
1148 }
1149 
stop_gfar(struct net_device * dev)1150 void stop_gfar(struct net_device *dev)
1151 {
1152 	struct gfar_private *priv = netdev_priv(dev);
1153 
1154 	netif_tx_stop_all_queues(dev);
1155 
1156 	smp_mb__before_atomic();
1157 	set_bit(GFAR_DOWN, &priv->state);
1158 	smp_mb__after_atomic();
1159 
1160 	disable_napi(priv);
1161 
1162 	/* disable ints and gracefully shut down Rx/Tx DMA */
1163 	gfar_halt(priv);
1164 
1165 	phy_stop(dev->phydev);
1166 
1167 	free_skb_resources(priv);
1168 }
1169 
gfar_start(struct gfar_private * priv)1170 static void gfar_start(struct gfar_private *priv)
1171 {
1172 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1173 	u32 tempval;
1174 	int i = 0;
1175 
1176 	/* Enable Rx/Tx hw queues */
1177 	gfar_write(&regs->rqueue, priv->rqueue);
1178 	gfar_write(&regs->tqueue, priv->tqueue);
1179 
1180 	/* Initialize DMACTRL to have WWR and WOP */
1181 	tempval = gfar_read(&regs->dmactrl);
1182 	tempval |= DMACTRL_INIT_SETTINGS;
1183 	gfar_write(&regs->dmactrl, tempval);
1184 
1185 	/* Make sure we aren't stopped */
1186 	tempval = gfar_read(&regs->dmactrl);
1187 	tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
1188 	gfar_write(&regs->dmactrl, tempval);
1189 
1190 	for (i = 0; i < priv->num_grps; i++) {
1191 		regs = priv->gfargrp[i].regs;
1192 		/* Clear THLT/RHLT, so that the DMA starts polling now */
1193 		gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1194 		gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
1195 	}
1196 
1197 	/* Enable Rx/Tx DMA */
1198 	tempval = gfar_read(&regs->maccfg1);
1199 	tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1200 	gfar_write(&regs->maccfg1, tempval);
1201 
1202 	gfar_ints_enable(priv);
1203 
1204 	netif_trans_update(priv->ndev); /* prevent tx timeout */
1205 }
1206 
gfar_new_page(struct gfar_priv_rx_q * rxq,struct gfar_rx_buff * rxb)1207 static bool gfar_new_page(struct gfar_priv_rx_q *rxq, struct gfar_rx_buff *rxb)
1208 {
1209 	struct page *page;
1210 	dma_addr_t addr;
1211 
1212 	page = dev_alloc_page();
1213 	if (unlikely(!page))
1214 		return false;
1215 
1216 	addr = dma_map_page(rxq->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1217 	if (unlikely(dma_mapping_error(rxq->dev, addr))) {
1218 		__free_page(page);
1219 
1220 		return false;
1221 	}
1222 
1223 	rxb->dma = addr;
1224 	rxb->page = page;
1225 	rxb->page_offset = 0;
1226 
1227 	return true;
1228 }
1229 
gfar_rx_alloc_err(struct gfar_priv_rx_q * rx_queue)1230 static void gfar_rx_alloc_err(struct gfar_priv_rx_q *rx_queue)
1231 {
1232 	struct gfar_private *priv = netdev_priv(rx_queue->ndev);
1233 	struct gfar_extra_stats *estats = &priv->extra_stats;
1234 
1235 	netdev_err(rx_queue->ndev, "Can't alloc RX buffers\n");
1236 	atomic64_inc(&estats->rx_alloc_err);
1237 }
1238 
gfar_alloc_rx_buffs(struct gfar_priv_rx_q * rx_queue,int alloc_cnt)1239 static void gfar_alloc_rx_buffs(struct gfar_priv_rx_q *rx_queue,
1240 				int alloc_cnt)
1241 {
1242 	struct rxbd8 *bdp;
1243 	struct gfar_rx_buff *rxb;
1244 	int i;
1245 
1246 	i = rx_queue->next_to_use;
1247 	bdp = &rx_queue->rx_bd_base[i];
1248 	rxb = &rx_queue->rx_buff[i];
1249 
1250 	while (alloc_cnt--) {
1251 		/* try reuse page */
1252 		if (unlikely(!rxb->page)) {
1253 			if (unlikely(!gfar_new_page(rx_queue, rxb))) {
1254 				gfar_rx_alloc_err(rx_queue);
1255 				break;
1256 			}
1257 		}
1258 
1259 		/* Setup the new RxBD */
1260 		gfar_init_rxbdp(rx_queue, bdp,
1261 				rxb->dma + rxb->page_offset + RXBUF_ALIGNMENT);
1262 
1263 		/* Update to the next pointer */
1264 		bdp++;
1265 		rxb++;
1266 
1267 		if (unlikely(++i == rx_queue->rx_ring_size)) {
1268 			i = 0;
1269 			bdp = rx_queue->rx_bd_base;
1270 			rxb = rx_queue->rx_buff;
1271 		}
1272 	}
1273 
1274 	rx_queue->next_to_use = i;
1275 	rx_queue->next_to_alloc = i;
1276 }
1277 
gfar_init_bds(struct net_device * ndev)1278 static void gfar_init_bds(struct net_device *ndev)
1279 {
1280 	struct gfar_private *priv = netdev_priv(ndev);
1281 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1282 	struct gfar_priv_tx_q *tx_queue = NULL;
1283 	struct gfar_priv_rx_q *rx_queue = NULL;
1284 	struct txbd8 *txbdp;
1285 	u32 __iomem *rfbptr;
1286 	int i, j;
1287 
1288 	for (i = 0; i < priv->num_tx_queues; i++) {
1289 		tx_queue = priv->tx_queue[i];
1290 		/* Initialize some variables in our dev structure */
1291 		tx_queue->num_txbdfree = tx_queue->tx_ring_size;
1292 		tx_queue->dirty_tx = tx_queue->tx_bd_base;
1293 		tx_queue->cur_tx = tx_queue->tx_bd_base;
1294 		tx_queue->skb_curtx = 0;
1295 		tx_queue->skb_dirtytx = 0;
1296 
1297 		/* Initialize Transmit Descriptor Ring */
1298 		txbdp = tx_queue->tx_bd_base;
1299 		for (j = 0; j < tx_queue->tx_ring_size; j++) {
1300 			txbdp->lstatus = 0;
1301 			txbdp->bufPtr = 0;
1302 			txbdp++;
1303 		}
1304 
1305 		/* Set the last descriptor in the ring to indicate wrap */
1306 		txbdp--;
1307 		txbdp->status = cpu_to_be16(be16_to_cpu(txbdp->status) |
1308 					    TXBD_WRAP);
1309 	}
1310 
1311 	rfbptr = &regs->rfbptr0;
1312 	for (i = 0; i < priv->num_rx_queues; i++) {
1313 		rx_queue = priv->rx_queue[i];
1314 
1315 		rx_queue->next_to_clean = 0;
1316 		rx_queue->next_to_use = 0;
1317 		rx_queue->next_to_alloc = 0;
1318 
1319 		/* make sure next_to_clean != next_to_use after this
1320 		 * by leaving at least 1 unused descriptor
1321 		 */
1322 		gfar_alloc_rx_buffs(rx_queue, gfar_rxbd_unused(rx_queue));
1323 
1324 		rx_queue->rfbptr = rfbptr;
1325 		rfbptr += 2;
1326 	}
1327 }
1328 
gfar_alloc_skb_resources(struct net_device * ndev)1329 static int gfar_alloc_skb_resources(struct net_device *ndev)
1330 {
1331 	void *vaddr;
1332 	dma_addr_t addr;
1333 	int i, j;
1334 	struct gfar_private *priv = netdev_priv(ndev);
1335 	struct device *dev = priv->dev;
1336 	struct gfar_priv_tx_q *tx_queue = NULL;
1337 	struct gfar_priv_rx_q *rx_queue = NULL;
1338 
1339 	priv->total_tx_ring_size = 0;
1340 	for (i = 0; i < priv->num_tx_queues; i++)
1341 		priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
1342 
1343 	priv->total_rx_ring_size = 0;
1344 	for (i = 0; i < priv->num_rx_queues; i++)
1345 		priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
1346 
1347 	/* Allocate memory for the buffer descriptors */
1348 	vaddr = dma_alloc_coherent(dev,
1349 				   (priv->total_tx_ring_size *
1350 				    sizeof(struct txbd8)) +
1351 				   (priv->total_rx_ring_size *
1352 				    sizeof(struct rxbd8)),
1353 				   &addr, GFP_KERNEL);
1354 	if (!vaddr)
1355 		return -ENOMEM;
1356 
1357 	for (i = 0; i < priv->num_tx_queues; i++) {
1358 		tx_queue = priv->tx_queue[i];
1359 		tx_queue->tx_bd_base = vaddr;
1360 		tx_queue->tx_bd_dma_base = addr;
1361 		tx_queue->dev = ndev;
1362 		/* enet DMA only understands physical addresses */
1363 		addr  += sizeof(struct txbd8) * tx_queue->tx_ring_size;
1364 		vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
1365 	}
1366 
1367 	/* Start the rx descriptor ring where the tx ring leaves off */
1368 	for (i = 0; i < priv->num_rx_queues; i++) {
1369 		rx_queue = priv->rx_queue[i];
1370 		rx_queue->rx_bd_base = vaddr;
1371 		rx_queue->rx_bd_dma_base = addr;
1372 		rx_queue->ndev = ndev;
1373 		rx_queue->dev = dev;
1374 		addr  += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
1375 		vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
1376 	}
1377 
1378 	/* Setup the skbuff rings */
1379 	for (i = 0; i < priv->num_tx_queues; i++) {
1380 		tx_queue = priv->tx_queue[i];
1381 		tx_queue->tx_skbuff =
1382 			kmalloc_objs(*tx_queue->tx_skbuff,
1383 				     tx_queue->tx_ring_size);
1384 		if (!tx_queue->tx_skbuff)
1385 			goto cleanup;
1386 
1387 		for (j = 0; j < tx_queue->tx_ring_size; j++)
1388 			tx_queue->tx_skbuff[j] = NULL;
1389 	}
1390 
1391 	for (i = 0; i < priv->num_rx_queues; i++) {
1392 		rx_queue = priv->rx_queue[i];
1393 		rx_queue->rx_buff = kzalloc_objs(*rx_queue->rx_buff,
1394 						 rx_queue->rx_ring_size);
1395 		if (!rx_queue->rx_buff)
1396 			goto cleanup;
1397 	}
1398 
1399 	gfar_init_bds(ndev);
1400 
1401 	return 0;
1402 
1403 cleanup:
1404 	free_skb_resources(priv);
1405 	return -ENOMEM;
1406 }
1407 
1408 /* Bring the controller up and running */
startup_gfar(struct net_device * ndev)1409 int startup_gfar(struct net_device *ndev)
1410 {
1411 	struct gfar_private *priv = netdev_priv(ndev);
1412 	int err;
1413 
1414 	gfar_mac_reset(priv);
1415 
1416 	err = gfar_alloc_skb_resources(ndev);
1417 	if (err)
1418 		return err;
1419 
1420 	gfar_init_tx_rx_base(priv);
1421 
1422 	smp_mb__before_atomic();
1423 	clear_bit(GFAR_DOWN, &priv->state);
1424 	smp_mb__after_atomic();
1425 
1426 	/* Start Rx/Tx DMA and enable the interrupts */
1427 	gfar_start(priv);
1428 
1429 	/* force link state update after mac reset */
1430 	priv->oldlink = 0;
1431 	priv->oldspeed = 0;
1432 	priv->oldduplex = -1;
1433 
1434 	phy_start(ndev->phydev);
1435 
1436 	enable_napi(priv);
1437 
1438 	netif_tx_wake_all_queues(ndev);
1439 
1440 	return 0;
1441 }
1442 
gfar_get_flowctrl_cfg(struct gfar_private * priv)1443 static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
1444 {
1445 	struct net_device *ndev = priv->ndev;
1446 	struct phy_device *phydev = ndev->phydev;
1447 	u32 val = 0;
1448 
1449 	if (!phydev->duplex)
1450 		return val;
1451 
1452 	if (!priv->pause_aneg_en) {
1453 		if (priv->tx_pause_en)
1454 			val |= MACCFG1_TX_FLOW;
1455 		if (priv->rx_pause_en)
1456 			val |= MACCFG1_RX_FLOW;
1457 	} else {
1458 		u16 lcl_adv, rmt_adv;
1459 		u8 flowctrl;
1460 		/* get link partner capabilities */
1461 		rmt_adv = 0;
1462 		if (phydev->pause)
1463 			rmt_adv = LPA_PAUSE_CAP;
1464 		if (phydev->asym_pause)
1465 			rmt_adv |= LPA_PAUSE_ASYM;
1466 
1467 		lcl_adv = linkmode_adv_to_lcl_adv_t(phydev->advertising);
1468 		flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
1469 		if (flowctrl & FLOW_CTRL_TX)
1470 			val |= MACCFG1_TX_FLOW;
1471 		if (flowctrl & FLOW_CTRL_RX)
1472 			val |= MACCFG1_RX_FLOW;
1473 	}
1474 
1475 	return val;
1476 }
1477 
gfar_update_link_state(struct gfar_private * priv)1478 static noinline void gfar_update_link_state(struct gfar_private *priv)
1479 {
1480 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
1481 	struct net_device *ndev = priv->ndev;
1482 	struct phy_device *phydev = ndev->phydev;
1483 	struct gfar_priv_rx_q *rx_queue = NULL;
1484 	int i;
1485 
1486 	if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
1487 		return;
1488 
1489 	if (phydev->link) {
1490 		u32 tempval1 = gfar_read(&regs->maccfg1);
1491 		u32 tempval = gfar_read(&regs->maccfg2);
1492 		u32 ecntrl = gfar_read(&regs->ecntrl);
1493 		u32 tx_flow_oldval = (tempval1 & MACCFG1_TX_FLOW);
1494 
1495 		if (phydev->duplex != priv->oldduplex) {
1496 			if (!(phydev->duplex))
1497 				tempval &= ~(MACCFG2_FULL_DUPLEX);
1498 			else
1499 				tempval |= MACCFG2_FULL_DUPLEX;
1500 
1501 			priv->oldduplex = phydev->duplex;
1502 		}
1503 
1504 		if (phydev->speed != priv->oldspeed) {
1505 			switch (phydev->speed) {
1506 			case 1000:
1507 				tempval =
1508 				    ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
1509 
1510 				ecntrl &= ~(ECNTRL_R100);
1511 				break;
1512 			case 100:
1513 			case 10:
1514 				tempval =
1515 				    ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
1516 
1517 				/* Reduced mode distinguishes
1518 				 * between 10 and 100
1519 				 */
1520 				if (phydev->speed == SPEED_100)
1521 					ecntrl |= ECNTRL_R100;
1522 				else
1523 					ecntrl &= ~(ECNTRL_R100);
1524 				break;
1525 			default:
1526 				netif_warn(priv, link, priv->ndev,
1527 					   "Ack!  Speed (%d) is not 10/100/1000!\n",
1528 					   phydev->speed);
1529 				break;
1530 			}
1531 
1532 			priv->oldspeed = phydev->speed;
1533 		}
1534 
1535 		tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
1536 		tempval1 |= gfar_get_flowctrl_cfg(priv);
1537 
1538 		/* Turn last free buffer recording on */
1539 		if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) {
1540 			for (i = 0; i < priv->num_rx_queues; i++) {
1541 				u32 bdp_dma;
1542 
1543 				rx_queue = priv->rx_queue[i];
1544 				bdp_dma = gfar_rxbd_dma_lastfree(rx_queue);
1545 				gfar_write(rx_queue->rfbptr, bdp_dma);
1546 			}
1547 
1548 			priv->tx_actual_en = 1;
1549 		}
1550 
1551 		if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval))
1552 			priv->tx_actual_en = 0;
1553 
1554 		gfar_write(&regs->maccfg1, tempval1);
1555 		gfar_write(&regs->maccfg2, tempval);
1556 		gfar_write(&regs->ecntrl, ecntrl);
1557 
1558 		if (!priv->oldlink)
1559 			priv->oldlink = 1;
1560 
1561 	} else if (priv->oldlink) {
1562 		priv->oldlink = 0;
1563 		priv->oldspeed = 0;
1564 		priv->oldduplex = -1;
1565 	}
1566 
1567 	if (netif_msg_link(priv))
1568 		phy_print_status(phydev);
1569 }
1570 
1571 /* Called every time the controller might need to be made
1572  * aware of new link state.  The PHY code conveys this
1573  * information through variables in the phydev structure, and this
1574  * function converts those variables into the appropriate
1575  * register values, and can bring down the device if needed.
1576  */
adjust_link(struct net_device * dev)1577 static void adjust_link(struct net_device *dev)
1578 {
1579 	struct gfar_private *priv = netdev_priv(dev);
1580 	struct phy_device *phydev = dev->phydev;
1581 
1582 	if (unlikely(phydev->link != priv->oldlink ||
1583 		     (phydev->link && (phydev->duplex != priv->oldduplex ||
1584 				       phydev->speed != priv->oldspeed))))
1585 		gfar_update_link_state(priv);
1586 }
1587 
1588 /* Initialize TBI PHY interface for communicating with the
1589  * SERDES lynx PHY on the chip.  We communicate with this PHY
1590  * through the MDIO bus on each controller, treating it as a
1591  * "normal" PHY at the address found in the TBIPA register.  We assume
1592  * that the TBIPA register is valid.  Either the MDIO bus code will set
1593  * it to a value that doesn't conflict with other PHYs on the bus, or the
1594  * value doesn't matter, as there are no other PHYs on the bus.
1595  */
gfar_configure_serdes(struct net_device * dev)1596 static void gfar_configure_serdes(struct net_device *dev)
1597 {
1598 	struct gfar_private *priv = netdev_priv(dev);
1599 	struct phy_device *tbiphy;
1600 
1601 	if (!priv->tbi_node) {
1602 		dev_warn(&dev->dev, "error: SGMII mode requires that the "
1603 				    "device tree specify a tbi-handle\n");
1604 		return;
1605 	}
1606 
1607 	tbiphy = of_phy_find_device(priv->tbi_node);
1608 	if (!tbiphy) {
1609 		dev_err(&dev->dev, "error: Could not get TBI device\n");
1610 		return;
1611 	}
1612 
1613 	/* If the link is already up, we must already be ok, and don't need to
1614 	 * configure and reset the TBI<->SerDes link.  Maybe U-Boot configured
1615 	 * everything for us?  Resetting it takes the link down and requires
1616 	 * several seconds for it to come back.
1617 	 */
1618 	if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS) {
1619 		put_device(&tbiphy->mdio.dev);
1620 		return;
1621 	}
1622 
1623 	/* Single clk mode, mii mode off(for serdes communication) */
1624 	phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
1625 
1626 	phy_write(tbiphy, MII_ADVERTISE,
1627 		  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1628 		  ADVERTISE_1000XPSE_ASYM);
1629 
1630 	phy_write(tbiphy, MII_BMCR,
1631 		  BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1632 		  BMCR_SPEED1000);
1633 
1634 	put_device(&tbiphy->mdio.dev);
1635 }
1636 
1637 /* Initializes driver's PHY state, and attaches to the PHY.
1638  * Returns 0 on success.
1639  */
init_phy(struct net_device * dev)1640 static int init_phy(struct net_device *dev)
1641 {
1642 	struct gfar_private *priv = netdev_priv(dev);
1643 	phy_interface_t interface = priv->interface;
1644 	struct phy_device *phydev;
1645 	struct ethtool_keee edata;
1646 
1647 	priv->oldlink = 0;
1648 	priv->oldspeed = 0;
1649 	priv->oldduplex = -1;
1650 
1651 	phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1652 				interface);
1653 	if (!phydev) {
1654 		dev_err(&dev->dev, "could not attach to PHY\n");
1655 		return -ENODEV;
1656 	}
1657 
1658 	if (interface == PHY_INTERFACE_MODE_SGMII)
1659 		gfar_configure_serdes(dev);
1660 
1661 	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT))
1662 		phy_set_max_speed(phydev, SPEED_100);
1663 
1664 	/* Add support for flow control */
1665 	phy_support_asym_pause(phydev);
1666 
1667 	/* disable EEE autoneg, EEE not supported by eTSEC */
1668 	memset(&edata, 0, sizeof(struct ethtool_keee));
1669 	phy_ethtool_set_eee(phydev, &edata);
1670 
1671 	return 0;
1672 }
1673 
gfar_add_fcb(struct sk_buff * skb)1674 static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
1675 {
1676 	struct txfcb *fcb = skb_push(skb, GMAC_FCB_LEN);
1677 
1678 	memset(fcb, 0, GMAC_FCB_LEN);
1679 
1680 	return fcb;
1681 }
1682 
gfar_tx_checksum(struct sk_buff * skb,struct txfcb * fcb,int fcb_length)1683 static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
1684 				    int fcb_length)
1685 {
1686 	/* If we're here, it's a IP packet with a TCP or UDP
1687 	 * payload.  We set it to checksum, using a pseudo-header
1688 	 * we provide
1689 	 */
1690 	u8 flags = TXFCB_DEFAULT;
1691 
1692 	/* Tell the controller what the protocol is
1693 	 * And provide the already calculated phcs
1694 	 */
1695 	if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
1696 		flags |= TXFCB_UDP;
1697 		fcb->phcs = (__force __be16)(udp_hdr(skb)->check);
1698 	} else
1699 		fcb->phcs = (__force __be16)(tcp_hdr(skb)->check);
1700 
1701 	/* l3os is the distance between the start of the
1702 	 * frame (skb->data) and the start of the IP hdr.
1703 	 * l4os is the distance between the start of the
1704 	 * l3 hdr and the l4 hdr
1705 	 */
1706 	fcb->l3os = (u8)(skb_network_offset(skb) - fcb_length);
1707 	fcb->l4os = skb_network_header_len(skb);
1708 
1709 	fcb->flags = flags;
1710 }
1711 
gfar_tx_vlan(struct sk_buff * skb,struct txfcb * fcb)1712 static inline void gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
1713 {
1714 	fcb->flags |= TXFCB_VLN;
1715 	fcb->vlctl = cpu_to_be16(skb_vlan_tag_get(skb));
1716 }
1717 
skip_txbd(struct txbd8 * bdp,int stride,struct txbd8 * base,int ring_size)1718 static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
1719 				      struct txbd8 *base, int ring_size)
1720 {
1721 	struct txbd8 *new_bd = bdp + stride;
1722 
1723 	return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
1724 }
1725 
next_txbd(struct txbd8 * bdp,struct txbd8 * base,int ring_size)1726 static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
1727 				      int ring_size)
1728 {
1729 	return skip_txbd(bdp, 1, base, ring_size);
1730 }
1731 
1732 /* eTSEC12: csum generation not supported for some fcb offsets */
gfar_csum_errata_12(struct gfar_private * priv,unsigned long fcb_addr)1733 static inline bool gfar_csum_errata_12(struct gfar_private *priv,
1734 				       unsigned long fcb_addr)
1735 {
1736 	return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
1737 	       (fcb_addr % 0x20) > 0x18);
1738 }
1739 
1740 /* eTSEC76: csum generation for frames larger than 2500 may
1741  * cause excess delays before start of transmission
1742  */
gfar_csum_errata_76(struct gfar_private * priv,unsigned int len)1743 static inline bool gfar_csum_errata_76(struct gfar_private *priv,
1744 				       unsigned int len)
1745 {
1746 	return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
1747 	       (len > 2500));
1748 }
1749 
1750 /* This is called by the kernel when a frame is ready for transmission.
1751  * It is pointed to by the dev->hard_start_xmit function pointer
1752  */
gfar_start_xmit(struct sk_buff * skb,struct net_device * dev)1753 static netdev_tx_t gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
1754 {
1755 	struct gfar_private *priv = netdev_priv(dev);
1756 	struct gfar_priv_tx_q *tx_queue = NULL;
1757 	struct netdev_queue *txq;
1758 	struct gfar __iomem *regs = NULL;
1759 	struct txfcb *fcb = NULL;
1760 	struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
1761 	u32 lstatus;
1762 	skb_frag_t *frag;
1763 	int i, rq = 0;
1764 	int do_tstamp, do_csum, do_vlan;
1765 	u32 bufaddr;
1766 	unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
1767 
1768 	rq = skb->queue_mapping;
1769 	tx_queue = priv->tx_queue[rq];
1770 	txq = netdev_get_tx_queue(dev, rq);
1771 	base = tx_queue->tx_bd_base;
1772 	regs = tx_queue->grp->regs;
1773 
1774 	do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
1775 	do_vlan = skb_vlan_tag_present(skb);
1776 	do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1777 		    priv->hwts_tx_en;
1778 
1779 	if (do_csum || do_vlan)
1780 		fcb_len = GMAC_FCB_LEN;
1781 
1782 	/* check if time stamp should be generated */
1783 	if (unlikely(do_tstamp))
1784 		fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
1785 
1786 	/* make space for additional header when fcb is needed */
1787 	if (fcb_len) {
1788 		if (unlikely(skb_cow_head(skb, fcb_len))) {
1789 			dev->stats.tx_errors++;
1790 			dev_kfree_skb_any(skb);
1791 			return NETDEV_TX_OK;
1792 		}
1793 	}
1794 
1795 	/* total number of fragments in the SKB */
1796 	nr_frags = skb_shinfo(skb)->nr_frags;
1797 
1798 	/* calculate the required number of TxBDs for this skb */
1799 	if (unlikely(do_tstamp))
1800 		nr_txbds = nr_frags + 2;
1801 	else
1802 		nr_txbds = nr_frags + 1;
1803 
1804 	/* check if there is space to queue this packet */
1805 	if (nr_txbds > tx_queue->num_txbdfree) {
1806 		/* no space, stop the queue */
1807 		netif_tx_stop_queue(txq);
1808 		dev->stats.tx_fifo_errors++;
1809 		return NETDEV_TX_BUSY;
1810 	}
1811 
1812 	/* Update transmit stats */
1813 	bytes_sent = skb->len;
1814 	tx_queue->stats.tx_bytes += bytes_sent;
1815 	/* keep Tx bytes on wire for BQL accounting */
1816 	GFAR_CB(skb)->bytes_sent = bytes_sent;
1817 	tx_queue->stats.tx_packets++;
1818 
1819 	txbdp = txbdp_start = tx_queue->cur_tx;
1820 	lstatus = be32_to_cpu(txbdp->lstatus);
1821 
1822 	/* Add TxPAL between FCB and frame if required */
1823 	if (unlikely(do_tstamp)) {
1824 		skb_push(skb, GMAC_TXPAL_LEN);
1825 		memset(skb->data, 0, GMAC_TXPAL_LEN);
1826 	}
1827 
1828 	/* Add TxFCB if required */
1829 	if (fcb_len) {
1830 		fcb = gfar_add_fcb(skb);
1831 		lstatus |= BD_LFLAG(TXBD_TOE);
1832 	}
1833 
1834 	/* Set up checksumming */
1835 	if (do_csum) {
1836 		gfar_tx_checksum(skb, fcb, fcb_len);
1837 
1838 		if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
1839 		    unlikely(gfar_csum_errata_76(priv, skb->len))) {
1840 			__skb_pull(skb, GMAC_FCB_LEN);
1841 			skb_checksum_help(skb);
1842 			if (do_vlan || do_tstamp) {
1843 				/* put back a new fcb for vlan/tstamp TOE */
1844 				fcb = gfar_add_fcb(skb);
1845 			} else {
1846 				/* Tx TOE not used */
1847 				lstatus &= ~(BD_LFLAG(TXBD_TOE));
1848 				fcb = NULL;
1849 			}
1850 		}
1851 	}
1852 
1853 	if (do_vlan)
1854 		gfar_tx_vlan(skb, fcb);
1855 
1856 	bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb),
1857 				 DMA_TO_DEVICE);
1858 	if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
1859 		goto dma_map_err;
1860 
1861 	txbdp_start->bufPtr = cpu_to_be32(bufaddr);
1862 
1863 	/* Time stamp insertion requires one additional TxBD */
1864 	if (unlikely(do_tstamp))
1865 		txbdp_tstamp = txbdp = next_txbd(txbdp, base,
1866 						 tx_queue->tx_ring_size);
1867 
1868 	if (likely(!nr_frags)) {
1869 		if (likely(!do_tstamp))
1870 			lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1871 	} else {
1872 		u32 lstatus_start = lstatus;
1873 
1874 		/* Place the fragment addresses and lengths into the TxBDs */
1875 		frag = &skb_shinfo(skb)->frags[0];
1876 		for (i = 0; i < nr_frags; i++, frag++) {
1877 			unsigned int size;
1878 
1879 			/* Point at the next BD, wrapping as needed */
1880 			txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
1881 
1882 			size = skb_frag_size(frag);
1883 
1884 			lstatus = be32_to_cpu(txbdp->lstatus) | size |
1885 				  BD_LFLAG(TXBD_READY);
1886 
1887 			/* Handle the last BD specially */
1888 			if (i == nr_frags - 1)
1889 				lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1890 
1891 			bufaddr = skb_frag_dma_map(priv->dev, frag, 0,
1892 						   size, DMA_TO_DEVICE);
1893 			if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
1894 				goto dma_map_err;
1895 
1896 			/* set the TxBD length and buffer pointer */
1897 			txbdp->bufPtr = cpu_to_be32(bufaddr);
1898 			txbdp->lstatus = cpu_to_be32(lstatus);
1899 		}
1900 
1901 		lstatus = lstatus_start;
1902 	}
1903 
1904 	/* If time stamping is requested one additional TxBD must be set up. The
1905 	 * first TxBD points to the FCB and must have a data length of
1906 	 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
1907 	 * the full frame length.
1908 	 */
1909 	if (unlikely(do_tstamp)) {
1910 		u32 lstatus_ts = be32_to_cpu(txbdp_tstamp->lstatus);
1911 
1912 		bufaddr = be32_to_cpu(txbdp_start->bufPtr);
1913 		bufaddr += fcb_len;
1914 
1915 		lstatus_ts |= BD_LFLAG(TXBD_READY) |
1916 			      (skb_headlen(skb) - fcb_len);
1917 		if (!nr_frags)
1918 			lstatus_ts |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
1919 
1920 		txbdp_tstamp->bufPtr = cpu_to_be32(bufaddr);
1921 		txbdp_tstamp->lstatus = cpu_to_be32(lstatus_ts);
1922 		lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
1923 
1924 		/* Setup tx hardware time stamping */
1925 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1926 		fcb->ptp = 1;
1927 	} else {
1928 		lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
1929 	}
1930 
1931 	skb_tx_timestamp(skb);
1932 	netdev_tx_sent_queue(txq, bytes_sent);
1933 
1934 	gfar_wmb();
1935 
1936 	txbdp_start->lstatus = cpu_to_be32(lstatus);
1937 
1938 	gfar_wmb(); /* force lstatus write before tx_skbuff */
1939 
1940 	tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
1941 
1942 	/* Update the current skb pointer to the next entry we will use
1943 	 * (wrapping if necessary)
1944 	 */
1945 	tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
1946 			      TX_RING_MOD_MASK(tx_queue->tx_ring_size);
1947 
1948 	tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
1949 
1950 	/* We can work in parallel with gfar_clean_tx_ring(), except
1951 	 * when modifying num_txbdfree. Note that we didn't grab the lock
1952 	 * when we were reading the num_txbdfree and checking for available
1953 	 * space, that's because outside of this function it can only grow.
1954 	 */
1955 	spin_lock_bh(&tx_queue->txlock);
1956 	/* reduce TxBD free count */
1957 	tx_queue->num_txbdfree -= (nr_txbds);
1958 	spin_unlock_bh(&tx_queue->txlock);
1959 
1960 	/* If the next BD still needs to be cleaned up, then the bds
1961 	 * are full.  We need to tell the kernel to stop sending us stuff.
1962 	 */
1963 	if (!tx_queue->num_txbdfree) {
1964 		netif_tx_stop_queue(txq);
1965 
1966 		dev->stats.tx_fifo_errors++;
1967 	}
1968 
1969 	/* Tell the DMA to go go go */
1970 	gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
1971 
1972 	return NETDEV_TX_OK;
1973 
1974 dma_map_err:
1975 	txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size);
1976 	if (do_tstamp)
1977 		txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
1978 	for (i = 0; i < nr_frags; i++) {
1979 		lstatus = be32_to_cpu(txbdp->lstatus);
1980 		if (!(lstatus & BD_LFLAG(TXBD_READY)))
1981 			break;
1982 
1983 		lstatus &= ~BD_LFLAG(TXBD_READY);
1984 		txbdp->lstatus = cpu_to_be32(lstatus);
1985 		bufaddr = be32_to_cpu(txbdp->bufPtr);
1986 		dma_unmap_page(priv->dev, bufaddr, be16_to_cpu(txbdp->length),
1987 			       DMA_TO_DEVICE);
1988 		txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
1989 	}
1990 	gfar_wmb();
1991 	dev_kfree_skb_any(skb);
1992 	return NETDEV_TX_OK;
1993 }
1994 
1995 /* Changes the mac address if the controller is not running. */
gfar_set_mac_address(struct net_device * dev)1996 static int gfar_set_mac_address(struct net_device *dev)
1997 {
1998 	gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
1999 
2000 	return 0;
2001 }
2002 
gfar_change_mtu(struct net_device * dev,int new_mtu)2003 static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2004 {
2005 	struct gfar_private *priv = netdev_priv(dev);
2006 
2007 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2008 		cpu_relax();
2009 
2010 	if (dev->flags & IFF_UP)
2011 		stop_gfar(dev);
2012 
2013 	WRITE_ONCE(dev->mtu, new_mtu);
2014 
2015 	if (dev->flags & IFF_UP)
2016 		startup_gfar(dev);
2017 
2018 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
2019 
2020 	return 0;
2021 }
2022 
reset_gfar(struct net_device * ndev)2023 static void reset_gfar(struct net_device *ndev)
2024 {
2025 	struct gfar_private *priv = netdev_priv(ndev);
2026 
2027 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2028 		cpu_relax();
2029 
2030 	stop_gfar(ndev);
2031 	startup_gfar(ndev);
2032 
2033 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
2034 }
2035 
2036 /* gfar_reset_task gets scheduled when a packet has not been
2037  * transmitted after a set amount of time.
2038  * For now, assume that clearing out all the structures, and
2039  * starting over will fix the problem.
2040  */
gfar_reset_task(struct work_struct * work)2041 static void gfar_reset_task(struct work_struct *work)
2042 {
2043 	struct gfar_private *priv = container_of(work, struct gfar_private,
2044 						 reset_task);
2045 	reset_gfar(priv->ndev);
2046 }
2047 
gfar_timeout(struct net_device * dev,unsigned int txqueue)2048 static void gfar_timeout(struct net_device *dev, unsigned int txqueue)
2049 {
2050 	struct gfar_private *priv = netdev_priv(dev);
2051 
2052 	dev->stats.tx_errors++;
2053 	schedule_work(&priv->reset_task);
2054 }
2055 
gfar_hwtstamp_set(struct net_device * netdev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)2056 static int gfar_hwtstamp_set(struct net_device *netdev,
2057 			     struct kernel_hwtstamp_config *config,
2058 			     struct netlink_ext_ack *extack)
2059 {
2060 	struct gfar_private *priv = netdev_priv(netdev);
2061 
2062 	switch (config->tx_type) {
2063 	case HWTSTAMP_TX_OFF:
2064 		priv->hwts_tx_en = 0;
2065 		break;
2066 	case HWTSTAMP_TX_ON:
2067 		if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
2068 			return -ERANGE;
2069 		priv->hwts_tx_en = 1;
2070 		break;
2071 	default:
2072 		return -ERANGE;
2073 	}
2074 
2075 	switch (config->rx_filter) {
2076 	case HWTSTAMP_FILTER_NONE:
2077 		if (priv->hwts_rx_en) {
2078 			priv->hwts_rx_en = 0;
2079 			reset_gfar(netdev);
2080 		}
2081 		break;
2082 	default:
2083 		if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
2084 			return -ERANGE;
2085 		if (!priv->hwts_rx_en) {
2086 			priv->hwts_rx_en = 1;
2087 			reset_gfar(netdev);
2088 		}
2089 		config->rx_filter = HWTSTAMP_FILTER_ALL;
2090 		break;
2091 	}
2092 
2093 	return 0;
2094 }
2095 
gfar_hwtstamp_get(struct net_device * netdev,struct kernel_hwtstamp_config * config)2096 static int gfar_hwtstamp_get(struct net_device *netdev,
2097 			     struct kernel_hwtstamp_config *config)
2098 {
2099 	struct gfar_private *priv = netdev_priv(netdev);
2100 
2101 	config->tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
2102 	config->rx_filter = priv->hwts_rx_en ? HWTSTAMP_FILTER_ALL :
2103 			    HWTSTAMP_FILTER_NONE;
2104 
2105 	return 0;
2106 }
2107 
2108 /* Interrupt Handler for Transmit complete */
gfar_clean_tx_ring(struct gfar_priv_tx_q * tx_queue)2109 static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
2110 {
2111 	struct net_device *dev = tx_queue->dev;
2112 	struct netdev_queue *txq;
2113 	struct gfar_private *priv = netdev_priv(dev);
2114 	struct txbd8 *bdp, *next = NULL;
2115 	struct txbd8 *lbdp = NULL;
2116 	struct txbd8 *base = tx_queue->tx_bd_base;
2117 	struct sk_buff *skb;
2118 	int skb_dirtytx;
2119 	int tx_ring_size = tx_queue->tx_ring_size;
2120 	int frags = 0, nr_txbds = 0;
2121 	int i;
2122 	int howmany = 0;
2123 	int tqi = tx_queue->qindex;
2124 	unsigned int bytes_sent = 0;
2125 	u32 lstatus;
2126 	size_t buflen;
2127 
2128 	txq = netdev_get_tx_queue(dev, tqi);
2129 	bdp = tx_queue->dirty_tx;
2130 	skb_dirtytx = tx_queue->skb_dirtytx;
2131 
2132 	while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
2133 		bool do_tstamp;
2134 
2135 		do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2136 			    priv->hwts_tx_en;
2137 
2138 		frags = skb_shinfo(skb)->nr_frags;
2139 
2140 		/* When time stamping, one additional TxBD must be freed.
2141 		 * Also, we need to dma_unmap_single() the TxPAL.
2142 		 */
2143 		if (unlikely(do_tstamp))
2144 			nr_txbds = frags + 2;
2145 		else
2146 			nr_txbds = frags + 1;
2147 
2148 		lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
2149 
2150 		lstatus = be32_to_cpu(lbdp->lstatus);
2151 
2152 		/* Only clean completed frames */
2153 		if ((lstatus & BD_LFLAG(TXBD_READY)) &&
2154 		    (lstatus & BD_LENGTH_MASK))
2155 			break;
2156 
2157 		if (unlikely(do_tstamp)) {
2158 			next = next_txbd(bdp, base, tx_ring_size);
2159 			buflen = be16_to_cpu(next->length) +
2160 				 GMAC_FCB_LEN + GMAC_TXPAL_LEN;
2161 		} else
2162 			buflen = be16_to_cpu(bdp->length);
2163 
2164 		dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr),
2165 				 buflen, DMA_TO_DEVICE);
2166 
2167 		if (unlikely(do_tstamp)) {
2168 			struct skb_shared_hwtstamps shhwtstamps;
2169 			__be64 *ns;
2170 
2171 			ns = (__be64 *)(((uintptr_t)skb->data + 0x10) & ~0x7UL);
2172 
2173 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2174 			shhwtstamps.hwtstamp = ns_to_ktime(be64_to_cpu(*ns));
2175 			skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
2176 			skb_tstamp_tx(skb, &shhwtstamps);
2177 			gfar_clear_txbd_status(bdp);
2178 			bdp = next;
2179 		}
2180 
2181 		gfar_clear_txbd_status(bdp);
2182 		bdp = next_txbd(bdp, base, tx_ring_size);
2183 
2184 		for (i = 0; i < frags; i++) {
2185 			dma_unmap_page(priv->dev, be32_to_cpu(bdp->bufPtr),
2186 				       be16_to_cpu(bdp->length),
2187 				       DMA_TO_DEVICE);
2188 			gfar_clear_txbd_status(bdp);
2189 			bdp = next_txbd(bdp, base, tx_ring_size);
2190 		}
2191 
2192 		bytes_sent += GFAR_CB(skb)->bytes_sent;
2193 
2194 		dev_kfree_skb_any(skb);
2195 
2196 		tx_queue->tx_skbuff[skb_dirtytx] = NULL;
2197 
2198 		skb_dirtytx = (skb_dirtytx + 1) &
2199 			      TX_RING_MOD_MASK(tx_ring_size);
2200 
2201 		howmany++;
2202 		spin_lock(&tx_queue->txlock);
2203 		tx_queue->num_txbdfree += nr_txbds;
2204 		spin_unlock(&tx_queue->txlock);
2205 	}
2206 
2207 	/* If we freed a buffer, we can restart transmission, if necessary */
2208 	if (tx_queue->num_txbdfree &&
2209 	    netif_tx_queue_stopped(txq) &&
2210 	    !(test_bit(GFAR_DOWN, &priv->state)))
2211 		netif_wake_subqueue(priv->ndev, tqi);
2212 
2213 	/* Update dirty indicators */
2214 	tx_queue->skb_dirtytx = skb_dirtytx;
2215 	tx_queue->dirty_tx = bdp;
2216 
2217 	netdev_tx_completed_queue(txq, howmany, bytes_sent);
2218 }
2219 
count_errors(u32 lstatus,struct net_device * ndev)2220 static void count_errors(u32 lstatus, struct net_device *ndev)
2221 {
2222 	struct gfar_private *priv = netdev_priv(ndev);
2223 	struct net_device_stats *stats = &ndev->stats;
2224 	struct gfar_extra_stats *estats = &priv->extra_stats;
2225 
2226 	/* If the packet was truncated, none of the other errors matter */
2227 	if (lstatus & BD_LFLAG(RXBD_TRUNCATED)) {
2228 		stats->rx_length_errors++;
2229 
2230 		atomic64_inc(&estats->rx_trunc);
2231 
2232 		return;
2233 	}
2234 	/* Count the errors, if there were any */
2235 	if (lstatus & BD_LFLAG(RXBD_LARGE | RXBD_SHORT)) {
2236 		stats->rx_length_errors++;
2237 
2238 		if (lstatus & BD_LFLAG(RXBD_LARGE))
2239 			atomic64_inc(&estats->rx_large);
2240 		else
2241 			atomic64_inc(&estats->rx_short);
2242 	}
2243 	if (lstatus & BD_LFLAG(RXBD_NONOCTET)) {
2244 		stats->rx_frame_errors++;
2245 		atomic64_inc(&estats->rx_nonoctet);
2246 	}
2247 	if (lstatus & BD_LFLAG(RXBD_CRCERR)) {
2248 		atomic64_inc(&estats->rx_crcerr);
2249 		stats->rx_crc_errors++;
2250 	}
2251 	if (lstatus & BD_LFLAG(RXBD_OVERRUN)) {
2252 		atomic64_inc(&estats->rx_overrun);
2253 		stats->rx_over_errors++;
2254 	}
2255 }
2256 
gfar_receive(int irq,void * grp_id)2257 static irqreturn_t gfar_receive(int irq, void *grp_id)
2258 {
2259 	struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2260 	unsigned long flags;
2261 	u32 imask, ievent;
2262 
2263 	ievent = gfar_read(&grp->regs->ievent);
2264 
2265 	if (unlikely(ievent & IEVENT_FGPI)) {
2266 		gfar_write(&grp->regs->ievent, IEVENT_FGPI);
2267 		return IRQ_HANDLED;
2268 	}
2269 
2270 	if (likely(napi_schedule_prep(&grp->napi_rx))) {
2271 		spin_lock_irqsave(&grp->grplock, flags);
2272 		imask = gfar_read(&grp->regs->imask);
2273 		imask &= IMASK_RX_DISABLED | grp->priv->rmon_overflow.imask;
2274 		gfar_write(&grp->regs->imask, imask);
2275 		spin_unlock_irqrestore(&grp->grplock, flags);
2276 		__napi_schedule(&grp->napi_rx);
2277 	} else {
2278 		/* Clear IEVENT, so interrupts aren't called again
2279 		 * because of the packets that have already arrived.
2280 		 */
2281 		gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
2282 	}
2283 
2284 	return IRQ_HANDLED;
2285 }
2286 
2287 /* Interrupt Handler for Transmit complete */
gfar_transmit(int irq,void * grp_id)2288 static irqreturn_t gfar_transmit(int irq, void *grp_id)
2289 {
2290 	struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2291 	unsigned long flags;
2292 	u32 imask;
2293 
2294 	if (likely(napi_schedule_prep(&grp->napi_tx))) {
2295 		spin_lock_irqsave(&grp->grplock, flags);
2296 		imask = gfar_read(&grp->regs->imask);
2297 		imask &= IMASK_TX_DISABLED | grp->priv->rmon_overflow.imask;
2298 		gfar_write(&grp->regs->imask, imask);
2299 		spin_unlock_irqrestore(&grp->grplock, flags);
2300 		__napi_schedule(&grp->napi_tx);
2301 	} else {
2302 		/* Clear IEVENT, so interrupts aren't called again
2303 		 * because of the packets that have already arrived.
2304 		 */
2305 		gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
2306 	}
2307 
2308 	return IRQ_HANDLED;
2309 }
2310 
gfar_add_rx_frag(struct gfar_rx_buff * rxb,u32 lstatus,struct sk_buff * skb,bool first)2311 static bool gfar_add_rx_frag(struct gfar_rx_buff *rxb, u32 lstatus,
2312 			     struct sk_buff *skb, bool first)
2313 {
2314 	int size = lstatus & BD_LENGTH_MASK;
2315 	struct page *page = rxb->page;
2316 
2317 	if (likely(first)) {
2318 		skb_put(skb, size);
2319 	} else {
2320 		/* the last fragments' length contains the full frame length */
2321 		if (lstatus & BD_LFLAG(RXBD_LAST))
2322 			size -= skb->len;
2323 
2324 		WARN(size < 0, "gianfar: rx fragment size underflow");
2325 		if (size < 0)
2326 			return false;
2327 
2328 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
2329 				rxb->page_offset + RXBUF_ALIGNMENT,
2330 				size, GFAR_RXB_TRUESIZE);
2331 	}
2332 
2333 	/* try reuse page */
2334 	if (unlikely(page_count(page) != 1 || page_is_pfmemalloc(page)))
2335 		return false;
2336 
2337 	/* change offset to the other half */
2338 	rxb->page_offset ^= GFAR_RXB_TRUESIZE;
2339 
2340 	page_ref_inc(page);
2341 
2342 	return true;
2343 }
2344 
gfar_reuse_rx_page(struct gfar_priv_rx_q * rxq,struct gfar_rx_buff * old_rxb)2345 static void gfar_reuse_rx_page(struct gfar_priv_rx_q *rxq,
2346 			       struct gfar_rx_buff *old_rxb)
2347 {
2348 	struct gfar_rx_buff *new_rxb;
2349 	u16 nta = rxq->next_to_alloc;
2350 
2351 	new_rxb = &rxq->rx_buff[nta];
2352 
2353 	/* find next buf that can reuse a page */
2354 	nta++;
2355 	rxq->next_to_alloc = (nta < rxq->rx_ring_size) ? nta : 0;
2356 
2357 	/* copy page reference */
2358 	*new_rxb = *old_rxb;
2359 
2360 	/* sync for use by the device */
2361 	dma_sync_single_range_for_device(rxq->dev, old_rxb->dma,
2362 					 old_rxb->page_offset,
2363 					 GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE);
2364 }
2365 
gfar_get_next_rxbuff(struct gfar_priv_rx_q * rx_queue,u32 lstatus,struct sk_buff * skb)2366 static struct sk_buff *gfar_get_next_rxbuff(struct gfar_priv_rx_q *rx_queue,
2367 					    u32 lstatus, struct sk_buff *skb)
2368 {
2369 	struct gfar_rx_buff *rxb = &rx_queue->rx_buff[rx_queue->next_to_clean];
2370 	struct page *page = rxb->page;
2371 	bool first = false;
2372 
2373 	if (likely(!skb)) {
2374 		void *buff_addr = page_address(page) + rxb->page_offset;
2375 
2376 		skb = build_skb(buff_addr, GFAR_SKBFRAG_SIZE);
2377 		if (unlikely(!skb)) {
2378 			gfar_rx_alloc_err(rx_queue);
2379 			return NULL;
2380 		}
2381 		skb_reserve(skb, RXBUF_ALIGNMENT);
2382 		first = true;
2383 	}
2384 
2385 	dma_sync_single_range_for_cpu(rx_queue->dev, rxb->dma, rxb->page_offset,
2386 				      GFAR_RXB_TRUESIZE, DMA_FROM_DEVICE);
2387 
2388 	if (gfar_add_rx_frag(rxb, lstatus, skb, first)) {
2389 		/* reuse the free half of the page */
2390 		gfar_reuse_rx_page(rx_queue, rxb);
2391 	} else {
2392 		/* page cannot be reused, unmap it */
2393 		dma_unmap_page(rx_queue->dev, rxb->dma,
2394 			       PAGE_SIZE, DMA_FROM_DEVICE);
2395 	}
2396 
2397 	/* clear rxb content */
2398 	rxb->page = NULL;
2399 
2400 	return skb;
2401 }
2402 
gfar_rx_checksum(struct sk_buff * skb,struct rxfcb * fcb)2403 static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2404 {
2405 	/* If valid headers were found, and valid sums
2406 	 * were verified, then we tell the kernel that no
2407 	 * checksumming is necessary.  Otherwise, it is [FIXME]
2408 	 */
2409 	if ((be16_to_cpu(fcb->flags) & RXFCB_CSUM_MASK) ==
2410 	    (RXFCB_CIP | RXFCB_CTU))
2411 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2412 	else
2413 		skb_checksum_none_assert(skb);
2414 }
2415 
2416 /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
gfar_process_frame(struct net_device * ndev,struct sk_buff * skb)2417 static void gfar_process_frame(struct net_device *ndev, struct sk_buff *skb)
2418 {
2419 	struct gfar_private *priv = netdev_priv(ndev);
2420 	struct rxfcb *fcb = NULL;
2421 
2422 	/* fcb is at the beginning if exists */
2423 	fcb = (struct rxfcb *)skb->data;
2424 
2425 	/* Remove the FCB from the skb
2426 	 * Remove the padded bytes, if there are any
2427 	 */
2428 	if (priv->uses_rxfcb)
2429 		skb_pull(skb, GMAC_FCB_LEN);
2430 
2431 	/* Get receive timestamp from the skb */
2432 	if (priv->hwts_rx_en) {
2433 		struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2434 		__be64 *ns = (__be64 *)skb->data;
2435 
2436 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2437 		shhwtstamps->hwtstamp = ns_to_ktime(be64_to_cpu(*ns));
2438 	}
2439 
2440 	if (priv->padding)
2441 		skb_pull(skb, priv->padding);
2442 
2443 	/* Trim off the FCS */
2444 	pskb_trim(skb, skb->len - ETH_FCS_LEN);
2445 
2446 	if (ndev->features & NETIF_F_RXCSUM)
2447 		gfar_rx_checksum(skb, fcb);
2448 
2449 	/* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
2450 	 * Even if vlan rx accel is disabled, on some chips
2451 	 * RXFCB_VLN is pseudo randomly set.
2452 	 */
2453 	if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX &&
2454 	    be16_to_cpu(fcb->flags) & RXFCB_VLN)
2455 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2456 				       be16_to_cpu(fcb->vlctl));
2457 }
2458 
2459 /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
2460  * until the budget/quota has been reached. Returns the number
2461  * of frames handled
2462  */
gfar_clean_rx_ring(struct gfar_priv_rx_q * rx_queue,int rx_work_limit)2463 static int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue,
2464 			      int rx_work_limit)
2465 {
2466 	struct net_device *ndev = rx_queue->ndev;
2467 	struct gfar_private *priv = netdev_priv(ndev);
2468 	struct rxbd8 *bdp;
2469 	int i, howmany = 0;
2470 	struct sk_buff *skb = rx_queue->skb;
2471 	int cleaned_cnt = gfar_rxbd_unused(rx_queue);
2472 	unsigned int total_bytes = 0, total_pkts = 0;
2473 
2474 	/* Get the first full descriptor */
2475 	i = rx_queue->next_to_clean;
2476 
2477 	while (rx_work_limit--) {
2478 		u32 lstatus;
2479 
2480 		if (cleaned_cnt >= GFAR_RX_BUFF_ALLOC) {
2481 			gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
2482 			cleaned_cnt = 0;
2483 		}
2484 
2485 		bdp = &rx_queue->rx_bd_base[i];
2486 		lstatus = be32_to_cpu(bdp->lstatus);
2487 		if (lstatus & BD_LFLAG(RXBD_EMPTY))
2488 			break;
2489 
2490 		/* lost RXBD_LAST descriptor due to overrun */
2491 		if (skb &&
2492 		    (lstatus & BD_LFLAG(RXBD_FIRST))) {
2493 			/* discard faulty buffer */
2494 			dev_kfree_skb(skb);
2495 			skb = NULL;
2496 			rx_queue->stats.rx_dropped++;
2497 
2498 			/* can continue normally */
2499 		}
2500 
2501 		/* order rx buffer descriptor reads */
2502 		rmb();
2503 
2504 		/* fetch next to clean buffer from the ring */
2505 		skb = gfar_get_next_rxbuff(rx_queue, lstatus, skb);
2506 		if (unlikely(!skb))
2507 			break;
2508 
2509 		cleaned_cnt++;
2510 		howmany++;
2511 
2512 		if (unlikely(++i == rx_queue->rx_ring_size))
2513 			i = 0;
2514 
2515 		rx_queue->next_to_clean = i;
2516 
2517 		/* fetch next buffer if not the last in frame */
2518 		if (!(lstatus & BD_LFLAG(RXBD_LAST)))
2519 			continue;
2520 
2521 		if (unlikely(lstatus & BD_LFLAG(RXBD_ERR))) {
2522 			count_errors(lstatus, ndev);
2523 
2524 			/* discard faulty buffer */
2525 			dev_kfree_skb(skb);
2526 			skb = NULL;
2527 			rx_queue->stats.rx_dropped++;
2528 			continue;
2529 		}
2530 
2531 		gfar_process_frame(ndev, skb);
2532 
2533 		/* Increment the number of packets */
2534 		total_pkts++;
2535 		total_bytes += skb->len;
2536 
2537 		skb_record_rx_queue(skb, rx_queue->qindex);
2538 
2539 		skb->protocol = eth_type_trans(skb, ndev);
2540 
2541 		/* Send the packet up the stack */
2542 		napi_gro_receive(&rx_queue->grp->napi_rx, skb);
2543 
2544 		skb = NULL;
2545 	}
2546 
2547 	/* Store incomplete frames for completion */
2548 	rx_queue->skb = skb;
2549 
2550 	rx_queue->stats.rx_packets += total_pkts;
2551 	rx_queue->stats.rx_bytes += total_bytes;
2552 
2553 	if (cleaned_cnt)
2554 		gfar_alloc_rx_buffs(rx_queue, cleaned_cnt);
2555 
2556 	/* Update Last Free RxBD pointer for LFC */
2557 	if (unlikely(priv->tx_actual_en)) {
2558 		u32 bdp_dma = gfar_rxbd_dma_lastfree(rx_queue);
2559 
2560 		gfar_write(rx_queue->rfbptr, bdp_dma);
2561 	}
2562 
2563 	return howmany;
2564 }
2565 
gfar_poll_rx_sq(struct napi_struct * napi,int budget)2566 static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
2567 {
2568 	struct gfar_priv_grp *gfargrp =
2569 		container_of(napi, struct gfar_priv_grp, napi_rx);
2570 	struct gfar __iomem *regs = gfargrp->regs;
2571 	struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
2572 	int work_done = 0;
2573 
2574 	/* Clear IEVENT, so interrupts aren't called again
2575 	 * because of the packets that have already arrived
2576 	 */
2577 	gfar_write(&regs->ievent, IEVENT_RX_MASK);
2578 
2579 	work_done = gfar_clean_rx_ring(rx_queue, budget);
2580 
2581 	if (work_done < budget) {
2582 		u32 imask;
2583 		napi_complete_done(napi, work_done);
2584 		/* Clear the halt bit in RSTAT */
2585 		gfar_write(&regs->rstat, gfargrp->rstat);
2586 
2587 		spin_lock_irq(&gfargrp->grplock);
2588 		imask = gfar_read(&regs->imask);
2589 		imask |= IMASK_RX_DEFAULT;
2590 		gfar_write(&regs->imask, imask);
2591 		spin_unlock_irq(&gfargrp->grplock);
2592 	}
2593 
2594 	return work_done;
2595 }
2596 
gfar_poll_tx_sq(struct napi_struct * napi,int budget)2597 static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
2598 {
2599 	struct gfar_priv_grp *gfargrp =
2600 		container_of(napi, struct gfar_priv_grp, napi_tx);
2601 	struct gfar __iomem *regs = gfargrp->regs;
2602 	struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
2603 	u32 imask;
2604 
2605 	/* Clear IEVENT, so interrupts aren't called again
2606 	 * because of the packets that have already arrived
2607 	 */
2608 	gfar_write(&regs->ievent, IEVENT_TX_MASK);
2609 
2610 	/* run Tx cleanup to completion */
2611 	if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
2612 		gfar_clean_tx_ring(tx_queue);
2613 
2614 	napi_complete(napi);
2615 
2616 	spin_lock_irq(&gfargrp->grplock);
2617 	imask = gfar_read(&regs->imask);
2618 	imask |= IMASK_TX_DEFAULT;
2619 	gfar_write(&regs->imask, imask);
2620 	spin_unlock_irq(&gfargrp->grplock);
2621 
2622 	return 0;
2623 }
2624 
2625 /* GFAR error interrupt handler */
gfar_error(int irq,void * grp_id)2626 static irqreturn_t gfar_error(int irq, void *grp_id)
2627 {
2628 	struct gfar_priv_grp *gfargrp = grp_id;
2629 	struct gfar __iomem *regs = gfargrp->regs;
2630 	struct gfar_private *priv= gfargrp->priv;
2631 	struct net_device *dev = priv->ndev;
2632 
2633 	/* Save ievent for future reference */
2634 	u32 events = gfar_read(&regs->ievent);
2635 
2636 	/* Clear IEVENT */
2637 	gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
2638 
2639 	/* Magic Packet is not an error. */
2640 	if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
2641 	    (events & IEVENT_MAG))
2642 		events &= ~IEVENT_MAG;
2643 
2644 	/* Hmm... */
2645 	if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
2646 		netdev_dbg(dev,
2647 			   "error interrupt (ievent=0x%08x imask=0x%08x)\n",
2648 			   events, gfar_read(&regs->imask));
2649 
2650 	/* Update the error counters */
2651 	if (events & IEVENT_TXE) {
2652 		dev->stats.tx_errors++;
2653 
2654 		if (events & IEVENT_LC)
2655 			dev->stats.tx_window_errors++;
2656 		if (events & IEVENT_CRL)
2657 			dev->stats.tx_aborted_errors++;
2658 		if (events & IEVENT_XFUN) {
2659 			netif_dbg(priv, tx_err, dev,
2660 				  "TX FIFO underrun, packet dropped\n");
2661 			dev->stats.tx_dropped++;
2662 			atomic64_inc(&priv->extra_stats.tx_underrun);
2663 
2664 			schedule_work(&priv->reset_task);
2665 		}
2666 		netif_dbg(priv, tx_err, dev, "Transmit Error\n");
2667 	}
2668 	if (events & IEVENT_MSRO) {
2669 		struct rmon_mib __iomem *rmon = &regs->rmon;
2670 		u32 car;
2671 
2672 		spin_lock(&priv->rmon_overflow.lock);
2673 		car = gfar_read(&rmon->car1) & CAR1_C1RDR;
2674 		if (car) {
2675 			priv->rmon_overflow.rdrp++;
2676 			gfar_write(&rmon->car1, car);
2677 		}
2678 		spin_unlock(&priv->rmon_overflow.lock);
2679 	}
2680 	if (events & IEVENT_BSY) {
2681 		dev->stats.rx_over_errors++;
2682 		atomic64_inc(&priv->extra_stats.rx_bsy);
2683 
2684 		netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
2685 			  gfar_read(&regs->rstat));
2686 	}
2687 	if (events & IEVENT_BABR) {
2688 		dev->stats.rx_errors++;
2689 		atomic64_inc(&priv->extra_stats.rx_babr);
2690 
2691 		netif_dbg(priv, rx_err, dev, "babbling RX error\n");
2692 	}
2693 	if (events & IEVENT_EBERR) {
2694 		atomic64_inc(&priv->extra_stats.eberr);
2695 		netif_dbg(priv, rx_err, dev, "bus error\n");
2696 	}
2697 	if (events & IEVENT_RXC)
2698 		netif_dbg(priv, rx_status, dev, "control frame\n");
2699 
2700 	if (events & IEVENT_BABT) {
2701 		atomic64_inc(&priv->extra_stats.tx_babt);
2702 		netif_dbg(priv, tx_err, dev, "babbling TX error\n");
2703 	}
2704 	return IRQ_HANDLED;
2705 }
2706 
2707 /* The interrupt handler for devices with one interrupt */
gfar_interrupt(int irq,void * grp_id)2708 static irqreturn_t gfar_interrupt(int irq, void *grp_id)
2709 {
2710 	struct gfar_priv_grp *gfargrp = grp_id;
2711 
2712 	/* Save ievent for future reference */
2713 	u32 events = gfar_read(&gfargrp->regs->ievent);
2714 
2715 	/* Check for reception */
2716 	if (events & IEVENT_RX_MASK)
2717 		gfar_receive(irq, grp_id);
2718 
2719 	/* Check for transmit completion */
2720 	if (events & IEVENT_TX_MASK)
2721 		gfar_transmit(irq, grp_id);
2722 
2723 	/* Check for errors */
2724 	if (events & IEVENT_ERR_MASK)
2725 		gfar_error(irq, grp_id);
2726 
2727 	return IRQ_HANDLED;
2728 }
2729 
2730 #ifdef CONFIG_NET_POLL_CONTROLLER
2731 /* Polling 'interrupt' - used by things like netconsole to send skbs
2732  * without having to re-enable interrupts. It's not called while
2733  * the interrupt routine is executing.
2734  */
gfar_netpoll(struct net_device * dev)2735 static void gfar_netpoll(struct net_device *dev)
2736 {
2737 	struct gfar_private *priv = netdev_priv(dev);
2738 	int i;
2739 
2740 	/* If the device has multiple interrupts, run tx/rx */
2741 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2742 		for (i = 0; i < priv->num_grps; i++) {
2743 			struct gfar_priv_grp *grp = &priv->gfargrp[i];
2744 
2745 			disable_irq(gfar_irq(grp, TX)->irq);
2746 			disable_irq(gfar_irq(grp, RX)->irq);
2747 			disable_irq(gfar_irq(grp, ER)->irq);
2748 			gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
2749 			enable_irq(gfar_irq(grp, ER)->irq);
2750 			enable_irq(gfar_irq(grp, RX)->irq);
2751 			enable_irq(gfar_irq(grp, TX)->irq);
2752 		}
2753 	} else {
2754 		for (i = 0; i < priv->num_grps; i++) {
2755 			struct gfar_priv_grp *grp = &priv->gfargrp[i];
2756 
2757 			disable_irq(gfar_irq(grp, TX)->irq);
2758 			gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
2759 			enable_irq(gfar_irq(grp, TX)->irq);
2760 		}
2761 	}
2762 }
2763 #endif
2764 
free_grp_irqs(struct gfar_priv_grp * grp)2765 static void free_grp_irqs(struct gfar_priv_grp *grp)
2766 {
2767 	free_irq(gfar_irq(grp, TX)->irq, grp);
2768 	free_irq(gfar_irq(grp, RX)->irq, grp);
2769 	free_irq(gfar_irq(grp, ER)->irq, grp);
2770 }
2771 
register_grp_irqs(struct gfar_priv_grp * grp)2772 static int register_grp_irqs(struct gfar_priv_grp *grp)
2773 {
2774 	struct gfar_private *priv = grp->priv;
2775 	struct net_device *dev = priv->ndev;
2776 	int err;
2777 
2778 	/* If the device has multiple interrupts, register for
2779 	 * them.  Otherwise, only register for the one
2780 	 */
2781 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2782 		/* Install our interrupt handlers for Error,
2783 		 * Transmit, and Receive
2784 		 */
2785 		err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
2786 				  gfar_irq(grp, ER)->name, grp);
2787 		if (err < 0) {
2788 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
2789 				  gfar_irq(grp, ER)->irq);
2790 
2791 			goto err_irq_fail;
2792 		}
2793 		enable_irq_wake(gfar_irq(grp, ER)->irq);
2794 
2795 		err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
2796 				  gfar_irq(grp, TX)->name, grp);
2797 		if (err < 0) {
2798 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
2799 				  gfar_irq(grp, TX)->irq);
2800 			goto tx_irq_fail;
2801 		}
2802 		err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
2803 				  gfar_irq(grp, RX)->name, grp);
2804 		if (err < 0) {
2805 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
2806 				  gfar_irq(grp, RX)->irq);
2807 			goto rx_irq_fail;
2808 		}
2809 		enable_irq_wake(gfar_irq(grp, RX)->irq);
2810 
2811 	} else {
2812 		err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
2813 				  gfar_irq(grp, TX)->name, grp);
2814 		if (err < 0) {
2815 			netif_err(priv, intr, dev, "Can't get IRQ %d\n",
2816 				  gfar_irq(grp, TX)->irq);
2817 			goto err_irq_fail;
2818 		}
2819 		enable_irq_wake(gfar_irq(grp, TX)->irq);
2820 	}
2821 
2822 	return 0;
2823 
2824 rx_irq_fail:
2825 	free_irq(gfar_irq(grp, TX)->irq, grp);
2826 tx_irq_fail:
2827 	free_irq(gfar_irq(grp, ER)->irq, grp);
2828 err_irq_fail:
2829 	return err;
2830 
2831 }
2832 
gfar_free_irq(struct gfar_private * priv)2833 static void gfar_free_irq(struct gfar_private *priv)
2834 {
2835 	int i;
2836 
2837 	/* Free the IRQs */
2838 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
2839 		for (i = 0; i < priv->num_grps; i++)
2840 			free_grp_irqs(&priv->gfargrp[i]);
2841 	} else {
2842 		for (i = 0; i < priv->num_grps; i++)
2843 			free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
2844 				 &priv->gfargrp[i]);
2845 	}
2846 }
2847 
gfar_request_irq(struct gfar_private * priv)2848 static int gfar_request_irq(struct gfar_private *priv)
2849 {
2850 	int err, i, j;
2851 
2852 	for (i = 0; i < priv->num_grps; i++) {
2853 		err = register_grp_irqs(&priv->gfargrp[i]);
2854 		if (err) {
2855 			for (j = 0; j < i; j++)
2856 				free_grp_irqs(&priv->gfargrp[j]);
2857 			return err;
2858 		}
2859 	}
2860 
2861 	return 0;
2862 }
2863 
2864 /* Called when something needs to use the ethernet device
2865  * Returns 0 for success.
2866  */
gfar_enet_open(struct net_device * dev)2867 static int gfar_enet_open(struct net_device *dev)
2868 {
2869 	struct gfar_private *priv = netdev_priv(dev);
2870 	int err;
2871 
2872 	err = init_phy(dev);
2873 	if (err)
2874 		return err;
2875 
2876 	err = gfar_request_irq(priv);
2877 	if (err)
2878 		return err;
2879 
2880 	err = startup_gfar(dev);
2881 	if (err)
2882 		return err;
2883 
2884 	return err;
2885 }
2886 
2887 /* Stops the kernel queue, and halts the controller */
gfar_close(struct net_device * dev)2888 static int gfar_close(struct net_device *dev)
2889 {
2890 	struct gfar_private *priv = netdev_priv(dev);
2891 
2892 	cancel_work_sync(&priv->reset_task);
2893 	stop_gfar(dev);
2894 
2895 	/* Disconnect from the PHY */
2896 	phy_disconnect(dev->phydev);
2897 
2898 	gfar_free_irq(priv);
2899 
2900 	return 0;
2901 }
2902 
2903 /* Clears each of the exact match registers to zero, so they
2904  * don't interfere with normal reception
2905  */
gfar_clear_exact_match(struct net_device * dev)2906 static void gfar_clear_exact_match(struct net_device *dev)
2907 {
2908 	int idx;
2909 	static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
2910 
2911 	for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
2912 		gfar_set_mac_for_addr(dev, idx, zero_arr);
2913 }
2914 
2915 /* Update the hash table based on the current list of multicast
2916  * addresses we subscribe to.  Also, change the promiscuity of
2917  * the device based on the flags (this function is called
2918  * whenever dev->flags is changed
2919  */
gfar_set_multi(struct net_device * dev)2920 static void gfar_set_multi(struct net_device *dev)
2921 {
2922 	struct netdev_hw_addr *ha;
2923 	struct gfar_private *priv = netdev_priv(dev);
2924 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
2925 	u32 tempval;
2926 
2927 	if (dev->flags & IFF_PROMISC) {
2928 		/* Set RCTRL to PROM */
2929 		tempval = gfar_read(&regs->rctrl);
2930 		tempval |= RCTRL_PROM;
2931 		gfar_write(&regs->rctrl, tempval);
2932 	} else {
2933 		/* Set RCTRL to not PROM */
2934 		tempval = gfar_read(&regs->rctrl);
2935 		tempval &= ~(RCTRL_PROM);
2936 		gfar_write(&regs->rctrl, tempval);
2937 	}
2938 
2939 	if (dev->flags & IFF_ALLMULTI) {
2940 		/* Set the hash to rx all multicast frames */
2941 		gfar_write(&regs->igaddr0, 0xffffffff);
2942 		gfar_write(&regs->igaddr1, 0xffffffff);
2943 		gfar_write(&regs->igaddr2, 0xffffffff);
2944 		gfar_write(&regs->igaddr3, 0xffffffff);
2945 		gfar_write(&regs->igaddr4, 0xffffffff);
2946 		gfar_write(&regs->igaddr5, 0xffffffff);
2947 		gfar_write(&regs->igaddr6, 0xffffffff);
2948 		gfar_write(&regs->igaddr7, 0xffffffff);
2949 		gfar_write(&regs->gaddr0, 0xffffffff);
2950 		gfar_write(&regs->gaddr1, 0xffffffff);
2951 		gfar_write(&regs->gaddr2, 0xffffffff);
2952 		gfar_write(&regs->gaddr3, 0xffffffff);
2953 		gfar_write(&regs->gaddr4, 0xffffffff);
2954 		gfar_write(&regs->gaddr5, 0xffffffff);
2955 		gfar_write(&regs->gaddr6, 0xffffffff);
2956 		gfar_write(&regs->gaddr7, 0xffffffff);
2957 	} else {
2958 		int em_num;
2959 		int idx;
2960 
2961 		/* zero out the hash */
2962 		gfar_write(&regs->igaddr0, 0x0);
2963 		gfar_write(&regs->igaddr1, 0x0);
2964 		gfar_write(&regs->igaddr2, 0x0);
2965 		gfar_write(&regs->igaddr3, 0x0);
2966 		gfar_write(&regs->igaddr4, 0x0);
2967 		gfar_write(&regs->igaddr5, 0x0);
2968 		gfar_write(&regs->igaddr6, 0x0);
2969 		gfar_write(&regs->igaddr7, 0x0);
2970 		gfar_write(&regs->gaddr0, 0x0);
2971 		gfar_write(&regs->gaddr1, 0x0);
2972 		gfar_write(&regs->gaddr2, 0x0);
2973 		gfar_write(&regs->gaddr3, 0x0);
2974 		gfar_write(&regs->gaddr4, 0x0);
2975 		gfar_write(&regs->gaddr5, 0x0);
2976 		gfar_write(&regs->gaddr6, 0x0);
2977 		gfar_write(&regs->gaddr7, 0x0);
2978 
2979 		/* If we have extended hash tables, we need to
2980 		 * clear the exact match registers to prepare for
2981 		 * setting them
2982 		 */
2983 		if (priv->extended_hash) {
2984 			em_num = GFAR_EM_NUM + 1;
2985 			gfar_clear_exact_match(dev);
2986 			idx = 1;
2987 		} else {
2988 			idx = 0;
2989 			em_num = 0;
2990 		}
2991 
2992 		if (netdev_mc_empty(dev))
2993 			return;
2994 
2995 		/* Parse the list, and set the appropriate bits */
2996 		netdev_for_each_mc_addr(ha, dev) {
2997 			if (idx < em_num) {
2998 				gfar_set_mac_for_addr(dev, idx, ha->addr);
2999 				idx++;
3000 			} else
3001 				gfar_set_hash_for_addr(dev, ha->addr);
3002 		}
3003 	}
3004 }
3005 
gfar_mac_reset(struct gfar_private * priv)3006 void gfar_mac_reset(struct gfar_private *priv)
3007 {
3008 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3009 	u32 tempval;
3010 
3011 	/* Reset MAC layer */
3012 	gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
3013 
3014 	/* We need to delay at least 3 TX clocks */
3015 	udelay(3);
3016 
3017 	/* the soft reset bit is not self-resetting, so we need to
3018 	 * clear it before resuming normal operation
3019 	 */
3020 	gfar_write(&regs->maccfg1, 0);
3021 
3022 	udelay(3);
3023 
3024 	gfar_rx_offload_en(priv);
3025 
3026 	/* Initialize the max receive frame/buffer lengths */
3027 	gfar_write(&regs->maxfrm, GFAR_JUMBO_FRAME_SIZE);
3028 	gfar_write(&regs->mrblr, GFAR_RXB_SIZE);
3029 
3030 	/* Initialize the Minimum Frame Length Register */
3031 	gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
3032 
3033 	/* Initialize MACCFG2. */
3034 	tempval = MACCFG2_INIT_SETTINGS;
3035 
3036 	/* eTSEC74 erratum: Rx frames of length MAXFRM or MAXFRM-1
3037 	 * are marked as truncated.  Avoid this by MACCFG2[Huge Frame]=1,
3038 	 * and by checking RxBD[LG] and discarding larger than MAXFRM.
3039 	 */
3040 	if (gfar_has_errata(priv, GFAR_ERRATA_74))
3041 		tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
3042 
3043 	gfar_write(&regs->maccfg2, tempval);
3044 
3045 	/* Clear mac addr hash registers */
3046 	gfar_write(&regs->igaddr0, 0);
3047 	gfar_write(&regs->igaddr1, 0);
3048 	gfar_write(&regs->igaddr2, 0);
3049 	gfar_write(&regs->igaddr3, 0);
3050 	gfar_write(&regs->igaddr4, 0);
3051 	gfar_write(&regs->igaddr5, 0);
3052 	gfar_write(&regs->igaddr6, 0);
3053 	gfar_write(&regs->igaddr7, 0);
3054 
3055 	gfar_write(&regs->gaddr0, 0);
3056 	gfar_write(&regs->gaddr1, 0);
3057 	gfar_write(&regs->gaddr2, 0);
3058 	gfar_write(&regs->gaddr3, 0);
3059 	gfar_write(&regs->gaddr4, 0);
3060 	gfar_write(&regs->gaddr5, 0);
3061 	gfar_write(&regs->gaddr6, 0);
3062 	gfar_write(&regs->gaddr7, 0);
3063 
3064 	if (priv->extended_hash)
3065 		gfar_clear_exact_match(priv->ndev);
3066 
3067 	gfar_mac_rx_config(priv);
3068 
3069 	gfar_mac_tx_config(priv);
3070 
3071 	gfar_set_mac_address(priv->ndev);
3072 
3073 	gfar_set_multi(priv->ndev);
3074 
3075 	/* clear ievent and imask before configuring coalescing */
3076 	gfar_ints_disable(priv);
3077 
3078 	/* Configure the coalescing support */
3079 	gfar_configure_coalescing_all(priv);
3080 }
3081 
gfar_hw_init(struct gfar_private * priv)3082 static void gfar_hw_init(struct gfar_private *priv)
3083 {
3084 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3085 	u32 attrs;
3086 
3087 	/* Stop the DMA engine now, in case it was running before
3088 	 * (The firmware could have used it, and left it running).
3089 	 */
3090 	gfar_halt(priv);
3091 
3092 	gfar_mac_reset(priv);
3093 
3094 	/* Zero out the rmon mib registers if it has them */
3095 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
3096 		memset_io(&regs->rmon, 0, offsetof(struct rmon_mib, car1));
3097 
3098 		/* Mask off the CAM interrupts */
3099 		gfar_write(&regs->rmon.cam1, 0xffffffff);
3100 		gfar_write(&regs->rmon.cam2, 0xffffffff);
3101 		/* Clear the CAR registers (w1c style) */
3102 		gfar_write(&regs->rmon.car1, 0xffffffff);
3103 		gfar_write(&regs->rmon.car2, 0xffffffff);
3104 	}
3105 
3106 	/* Initialize ECNTRL */
3107 	gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
3108 
3109 	/* Set the extraction length and index */
3110 	attrs = ATTRELI_EL(priv->rx_stash_size) |
3111 		ATTRELI_EI(priv->rx_stash_index);
3112 
3113 	gfar_write(&regs->attreli, attrs);
3114 
3115 	/* Start with defaults, and add stashing
3116 	 * depending on driver parameters
3117 	 */
3118 	attrs = ATTR_INIT_SETTINGS;
3119 
3120 	if (priv->bd_stash_en)
3121 		attrs |= ATTR_BDSTASH;
3122 
3123 	if (priv->rx_stash_size != 0)
3124 		attrs |= ATTR_BUFSTASH;
3125 
3126 	gfar_write(&regs->attr, attrs);
3127 
3128 	/* FIFO configs */
3129 	gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
3130 	gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
3131 	gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
3132 
3133 	/* Program the interrupt steering regs, only for MG devices */
3134 	if (priv->num_grps > 1)
3135 		gfar_write_isrg(priv);
3136 }
3137 
3138 static const struct net_device_ops gfar_netdev_ops = {
3139 	.ndo_open = gfar_enet_open,
3140 	.ndo_start_xmit = gfar_start_xmit,
3141 	.ndo_stop = gfar_close,
3142 	.ndo_change_mtu = gfar_change_mtu,
3143 	.ndo_set_features = gfar_set_features,
3144 	.ndo_set_rx_mode = gfar_set_multi,
3145 	.ndo_tx_timeout = gfar_timeout,
3146 	.ndo_eth_ioctl = phy_do_ioctl_running,
3147 	.ndo_get_stats64 = gfar_get_stats64,
3148 	.ndo_change_carrier = fixed_phy_change_carrier,
3149 	.ndo_set_mac_address = gfar_set_mac_addr,
3150 	.ndo_validate_addr = eth_validate_addr,
3151 #ifdef CONFIG_NET_POLL_CONTROLLER
3152 	.ndo_poll_controller = gfar_netpoll,
3153 #endif
3154 	.ndo_hwtstamp_get = gfar_hwtstamp_get,
3155 	.ndo_hwtstamp_set = gfar_hwtstamp_set,
3156 };
3157 
3158 /* Set up the ethernet device structure, private data,
3159  * and anything else we need before we start
3160  */
gfar_probe(struct platform_device * ofdev)3161 static int gfar_probe(struct platform_device *ofdev)
3162 {
3163 	struct device_node *np = ofdev->dev.of_node;
3164 	struct net_device *dev = NULL;
3165 	struct gfar_private *priv = NULL;
3166 	int err = 0, i;
3167 
3168 	err = gfar_of_init(ofdev, &dev);
3169 
3170 	if (err)
3171 		return err;
3172 
3173 	priv = netdev_priv(dev);
3174 	priv->ndev = dev;
3175 	priv->ofdev = ofdev;
3176 	priv->dev = &ofdev->dev;
3177 	SET_NETDEV_DEV(dev, &ofdev->dev);
3178 
3179 	INIT_WORK(&priv->reset_task, gfar_reset_task);
3180 
3181 	platform_set_drvdata(ofdev, priv);
3182 
3183 	gfar_detect_errata(priv);
3184 
3185 	/* Set the dev->base_addr to the gfar reg region */
3186 	dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
3187 
3188 	/* Fill in the dev structure */
3189 	dev->watchdog_timeo = TX_TIMEOUT;
3190 	/* MTU range: 50 - 9586 */
3191 	dev->mtu = 1500;
3192 	dev->min_mtu = 50;
3193 	dev->max_mtu = GFAR_JUMBO_FRAME_SIZE - ETH_HLEN;
3194 	dev->netdev_ops = &gfar_netdev_ops;
3195 	dev->ethtool_ops = &gfar_ethtool_ops;
3196 
3197 	/* Register for napi ...We are registering NAPI for each grp */
3198 	for (i = 0; i < priv->num_grps; i++) {
3199 		netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
3200 			       gfar_poll_rx_sq);
3201 		netif_napi_add_tx_weight(dev, &priv->gfargrp[i].napi_tx,
3202 					 gfar_poll_tx_sq, 2);
3203 	}
3204 
3205 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
3206 		dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
3207 				   NETIF_F_RXCSUM;
3208 		dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
3209 				 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
3210 	}
3211 
3212 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
3213 		dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
3214 				    NETIF_F_HW_VLAN_CTAG_RX;
3215 		dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3216 	}
3217 
3218 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3219 
3220 	gfar_init_addr_hash_table(priv);
3221 
3222 	/* Insert receive time stamps into padding alignment bytes, and
3223 	 * plus 2 bytes padding to ensure the cpu alignment.
3224 	 */
3225 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
3226 		priv->padding = 8 + DEFAULT_PADDING;
3227 
3228 	if (dev->features & NETIF_F_IP_CSUM ||
3229 	    priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
3230 		dev->needed_headroom = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
3231 
3232 	/* Initializing some of the rx/tx queue level parameters */
3233 	for (i = 0; i < priv->num_tx_queues; i++) {
3234 		priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
3235 		priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
3236 		priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
3237 		priv->tx_queue[i]->txic = DEFAULT_TXIC;
3238 	}
3239 
3240 	for (i = 0; i < priv->num_rx_queues; i++) {
3241 		priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
3242 		priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
3243 		priv->rx_queue[i]->rxic = DEFAULT_RXIC;
3244 	}
3245 
3246 	/* Always enable rx filer if available */
3247 	priv->rx_filer_enable =
3248 	    (priv->device_flags & FSL_GIANFAR_DEV_HAS_RX_FILER) ? 1 : 0;
3249 	/* Enable most messages by default */
3250 	priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
3251 	/* use pritority h/w tx queue scheduling for single queue devices */
3252 	if (priv->num_tx_queues == 1)
3253 		priv->prio_sched_en = 1;
3254 
3255 	set_bit(GFAR_DOWN, &priv->state);
3256 
3257 	gfar_hw_init(priv);
3258 
3259 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
3260 		struct rmon_mib __iomem *rmon = &priv->gfargrp[0].regs->rmon;
3261 
3262 		spin_lock_init(&priv->rmon_overflow.lock);
3263 		priv->rmon_overflow.imask = IMASK_MSRO;
3264 		gfar_write(&rmon->cam1, gfar_read(&rmon->cam1) & ~CAM1_M1RDR);
3265 	}
3266 
3267 	/* Carrier starts down, phylib will bring it up */
3268 	netif_carrier_off(dev);
3269 
3270 	err = register_netdev(dev);
3271 
3272 	if (err) {
3273 		pr_err("%s: Cannot register net device, aborting\n", dev->name);
3274 		goto register_fail;
3275 	}
3276 
3277 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET)
3278 		priv->wol_supported |= GFAR_WOL_MAGIC;
3279 
3280 	if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_WAKE_ON_FILER) &&
3281 	    priv->rx_filer_enable)
3282 		priv->wol_supported |= GFAR_WOL_FILER_UCAST;
3283 
3284 	device_set_wakeup_capable(&ofdev->dev, priv->wol_supported);
3285 
3286 	/* fill out IRQ number and name fields */
3287 	for (i = 0; i < priv->num_grps; i++) {
3288 		struct gfar_priv_grp *grp = &priv->gfargrp[i];
3289 		if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
3290 			sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
3291 				dev->name, "_g", '0' + i, "_tx");
3292 			sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
3293 				dev->name, "_g", '0' + i, "_rx");
3294 			sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
3295 				dev->name, "_g", '0' + i, "_er");
3296 		} else
3297 			strcpy(gfar_irq(grp, TX)->name, dev->name);
3298 	}
3299 
3300 	/* Initialize the filer table */
3301 	gfar_init_filer_table(priv);
3302 
3303 	/* Print out the device info */
3304 	netdev_info(dev, "mac: %pM\n", dev->dev_addr);
3305 
3306 	/* Even more device info helps when determining which kernel
3307 	 * provided which set of benchmarks.
3308 	 */
3309 	netdev_info(dev, "Running with NAPI enabled\n");
3310 	for (i = 0; i < priv->num_rx_queues; i++)
3311 		netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
3312 			    i, priv->rx_queue[i]->rx_ring_size);
3313 	for (i = 0; i < priv->num_tx_queues; i++)
3314 		netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
3315 			    i, priv->tx_queue[i]->tx_ring_size);
3316 
3317 	return 0;
3318 
3319 register_fail:
3320 	if (of_phy_is_fixed_link(np))
3321 		of_phy_deregister_fixed_link(np);
3322 	unmap_group_regs(priv);
3323 	gfar_free_rx_queues(priv);
3324 	gfar_free_tx_queues(priv);
3325 	of_node_put(priv->phy_node);
3326 	of_node_put(priv->tbi_node);
3327 	free_gfar_dev(priv);
3328 	return err;
3329 }
3330 
gfar_remove(struct platform_device * ofdev)3331 static void gfar_remove(struct platform_device *ofdev)
3332 {
3333 	struct gfar_private *priv = platform_get_drvdata(ofdev);
3334 	struct device_node *np = ofdev->dev.of_node;
3335 
3336 	of_node_put(priv->phy_node);
3337 	of_node_put(priv->tbi_node);
3338 
3339 	unregister_netdev(priv->ndev);
3340 
3341 	if (of_phy_is_fixed_link(np))
3342 		of_phy_deregister_fixed_link(np);
3343 
3344 	unmap_group_regs(priv);
3345 	gfar_free_rx_queues(priv);
3346 	gfar_free_tx_queues(priv);
3347 	free_gfar_dev(priv);
3348 }
3349 
3350 #ifdef CONFIG_PM
3351 
__gfar_filer_disable(struct gfar_private * priv)3352 static void __gfar_filer_disable(struct gfar_private *priv)
3353 {
3354 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3355 	u32 temp;
3356 
3357 	temp = gfar_read(&regs->rctrl);
3358 	temp &= ~(RCTRL_FILREN | RCTRL_PRSDEP_INIT);
3359 	gfar_write(&regs->rctrl, temp);
3360 }
3361 
__gfar_filer_enable(struct gfar_private * priv)3362 static void __gfar_filer_enable(struct gfar_private *priv)
3363 {
3364 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3365 	u32 temp;
3366 
3367 	temp = gfar_read(&regs->rctrl);
3368 	temp |= RCTRL_FILREN | RCTRL_PRSDEP_INIT;
3369 	gfar_write(&regs->rctrl, temp);
3370 }
3371 
3372 /* Filer rules implementing wol capabilities */
gfar_filer_config_wol(struct gfar_private * priv)3373 static void gfar_filer_config_wol(struct gfar_private *priv)
3374 {
3375 	unsigned int i;
3376 	u32 rqfcr;
3377 
3378 	__gfar_filer_disable(priv);
3379 
3380 	/* clear the filer table, reject any packet by default */
3381 	rqfcr = RQFCR_RJE | RQFCR_CMP_MATCH;
3382 	for (i = 0; i <= MAX_FILER_IDX; i++)
3383 		gfar_write_filer(priv, i, rqfcr, 0);
3384 
3385 	i = 0;
3386 	if (priv->wol_opts & GFAR_WOL_FILER_UCAST) {
3387 		/* unicast packet, accept it */
3388 		struct net_device *ndev = priv->ndev;
3389 		/* get the default rx queue index */
3390 		u8 qindex = (u8)priv->gfargrp[0].rx_queue->qindex;
3391 		u32 dest_mac_addr = (ndev->dev_addr[0] << 16) |
3392 				    (ndev->dev_addr[1] << 8) |
3393 				     ndev->dev_addr[2];
3394 
3395 		rqfcr = (qindex << 10) | RQFCR_AND |
3396 			RQFCR_CMP_EXACT | RQFCR_PID_DAH;
3397 
3398 		gfar_write_filer(priv, i++, rqfcr, dest_mac_addr);
3399 
3400 		dest_mac_addr = (ndev->dev_addr[3] << 16) |
3401 				(ndev->dev_addr[4] << 8) |
3402 				 ndev->dev_addr[5];
3403 		rqfcr = (qindex << 10) | RQFCR_GPI |
3404 			RQFCR_CMP_EXACT | RQFCR_PID_DAL;
3405 		gfar_write_filer(priv, i++, rqfcr, dest_mac_addr);
3406 	}
3407 
3408 	__gfar_filer_enable(priv);
3409 }
3410 
gfar_filer_restore_table(struct gfar_private * priv)3411 static void gfar_filer_restore_table(struct gfar_private *priv)
3412 {
3413 	u32 rqfcr, rqfpr;
3414 	unsigned int i;
3415 
3416 	__gfar_filer_disable(priv);
3417 
3418 	for (i = 0; i <= MAX_FILER_IDX; i++) {
3419 		rqfcr = priv->ftp_rqfcr[i];
3420 		rqfpr = priv->ftp_rqfpr[i];
3421 		gfar_write_filer(priv, i, rqfcr, rqfpr);
3422 	}
3423 
3424 	__gfar_filer_enable(priv);
3425 }
3426 
3427 /* gfar_start() for Rx only and with the FGPI filer interrupt enabled */
gfar_start_wol_filer(struct gfar_private * priv)3428 static void gfar_start_wol_filer(struct gfar_private *priv)
3429 {
3430 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3431 	u32 tempval;
3432 	int i = 0;
3433 
3434 	/* Enable Rx hw queues */
3435 	gfar_write(&regs->rqueue, priv->rqueue);
3436 
3437 	/* Initialize DMACTRL to have WWR and WOP */
3438 	tempval = gfar_read(&regs->dmactrl);
3439 	tempval |= DMACTRL_INIT_SETTINGS;
3440 	gfar_write(&regs->dmactrl, tempval);
3441 
3442 	/* Make sure we aren't stopped */
3443 	tempval = gfar_read(&regs->dmactrl);
3444 	tempval &= ~DMACTRL_GRS;
3445 	gfar_write(&regs->dmactrl, tempval);
3446 
3447 	for (i = 0; i < priv->num_grps; i++) {
3448 		regs = priv->gfargrp[i].regs;
3449 		/* Clear RHLT, so that the DMA starts polling now */
3450 		gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
3451 		/* enable the Filer General Purpose Interrupt */
3452 		gfar_write(&regs->imask, IMASK_FGPI);
3453 	}
3454 
3455 	/* Enable Rx DMA */
3456 	tempval = gfar_read(&regs->maccfg1);
3457 	tempval |= MACCFG1_RX_EN;
3458 	gfar_write(&regs->maccfg1, tempval);
3459 }
3460 
gfar_suspend(struct device * dev)3461 static int gfar_suspend(struct device *dev)
3462 {
3463 	struct gfar_private *priv = dev_get_drvdata(dev);
3464 	struct net_device *ndev = priv->ndev;
3465 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3466 	u32 tempval;
3467 	u16 wol = priv->wol_opts;
3468 
3469 	if (!netif_running(ndev))
3470 		return 0;
3471 
3472 	disable_napi(priv);
3473 	netif_tx_lock(ndev);
3474 	netif_device_detach(ndev);
3475 	netif_tx_unlock(ndev);
3476 
3477 	gfar_halt(priv);
3478 
3479 	if (wol & GFAR_WOL_MAGIC) {
3480 		/* Enable interrupt on Magic Packet */
3481 		gfar_write(&regs->imask, IMASK_MAG);
3482 
3483 		/* Enable Magic Packet mode */
3484 		tempval = gfar_read(&regs->maccfg2);
3485 		tempval |= MACCFG2_MPEN;
3486 		gfar_write(&regs->maccfg2, tempval);
3487 
3488 		/* re-enable the Rx block */
3489 		tempval = gfar_read(&regs->maccfg1);
3490 		tempval |= MACCFG1_RX_EN;
3491 		gfar_write(&regs->maccfg1, tempval);
3492 
3493 	} else if (wol & GFAR_WOL_FILER_UCAST) {
3494 		gfar_filer_config_wol(priv);
3495 		gfar_start_wol_filer(priv);
3496 
3497 	} else {
3498 		phy_stop(ndev->phydev);
3499 	}
3500 
3501 	return 0;
3502 }
3503 
gfar_resume(struct device * dev)3504 static int gfar_resume(struct device *dev)
3505 {
3506 	struct gfar_private *priv = dev_get_drvdata(dev);
3507 	struct net_device *ndev = priv->ndev;
3508 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
3509 	u32 tempval;
3510 	u16 wol = priv->wol_opts;
3511 
3512 	if (!netif_running(ndev))
3513 		return 0;
3514 
3515 	if (wol & GFAR_WOL_MAGIC) {
3516 		/* Disable Magic Packet mode */
3517 		tempval = gfar_read(&regs->maccfg2);
3518 		tempval &= ~MACCFG2_MPEN;
3519 		gfar_write(&regs->maccfg2, tempval);
3520 
3521 	} else if (wol & GFAR_WOL_FILER_UCAST) {
3522 		/* need to stop rx only, tx is already down */
3523 		gfar_halt(priv);
3524 		gfar_filer_restore_table(priv);
3525 
3526 	} else {
3527 		phy_start(ndev->phydev);
3528 	}
3529 
3530 	gfar_start(priv);
3531 
3532 	netif_device_attach(ndev);
3533 	enable_napi(priv);
3534 
3535 	return 0;
3536 }
3537 
gfar_restore(struct device * dev)3538 static int gfar_restore(struct device *dev)
3539 {
3540 	struct gfar_private *priv = dev_get_drvdata(dev);
3541 	struct net_device *ndev = priv->ndev;
3542 
3543 	if (!netif_running(ndev)) {
3544 		netif_device_attach(ndev);
3545 
3546 		return 0;
3547 	}
3548 
3549 	gfar_init_bds(ndev);
3550 
3551 	gfar_mac_reset(priv);
3552 
3553 	gfar_init_tx_rx_base(priv);
3554 
3555 	gfar_start(priv);
3556 
3557 	priv->oldlink = 0;
3558 	priv->oldspeed = 0;
3559 	priv->oldduplex = -1;
3560 
3561 	if (ndev->phydev)
3562 		phy_start(ndev->phydev);
3563 
3564 	netif_device_attach(ndev);
3565 	enable_napi(priv);
3566 
3567 	return 0;
3568 }
3569 
3570 static const struct dev_pm_ops gfar_pm_ops = {
3571 	.suspend = gfar_suspend,
3572 	.resume = gfar_resume,
3573 	.freeze = gfar_suspend,
3574 	.thaw = gfar_resume,
3575 	.restore = gfar_restore,
3576 };
3577 
3578 #define GFAR_PM_OPS (&gfar_pm_ops)
3579 
3580 #else
3581 
3582 #define GFAR_PM_OPS NULL
3583 
3584 #endif
3585 
3586 static const struct of_device_id gfar_match[] =
3587 {
3588 	{
3589 		.type = "network",
3590 		.compatible = "gianfar",
3591 	},
3592 	{
3593 		.compatible = "fsl,etsec2",
3594 	},
3595 	{},
3596 };
3597 MODULE_DEVICE_TABLE(of, gfar_match);
3598 
3599 /* Structure for a device driver */
3600 static struct platform_driver gfar_driver = {
3601 	.driver = {
3602 		.name = "fsl-gianfar",
3603 		.pm = GFAR_PM_OPS,
3604 		.of_match_table = gfar_match,
3605 	},
3606 	.probe = gfar_probe,
3607 	.remove = gfar_remove,
3608 };
3609 
3610 module_platform_driver(gfar_driver);
3611