1 /*
2  *  drivers/net/ethernet/freescale/gianfar_ethtool.c
3  *
4  *  Gianfar Ethernet Driver
5  *  Ethtool support for Gianfar Enet
6  *  Based on e1000 ethtool support
7  *
8  *  Author: Andy Fleming
9  *  Maintainer: Kumar Gala
10  *  Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
11  *
12  *  Copyright 2003-2006, 2008-2009, 2011 Freescale Semiconductor, Inc.
13  *
14  *  This software may be used and distributed according to
15  *  the terms of the GNU Public License, Version 2, incorporated herein
16  *  by reference.
17  */
18 
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/net_tstamp.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mm.h>
32 
33 #include <asm/io.h>
34 #include <asm/irq.h>
35 #include <linux/uaccess.h>
36 #include <linux/module.h>
37 #include <linux/crc32.h>
38 #include <asm/types.h>
39 #include <linux/ethtool.h>
40 #include <linux/mii.h>
41 #include <linux/phy.h>
42 #include <linux/sort.h>
43 #include <linux/if_vlan.h>
44 #include <linux/of_platform.h>
45 #include <linux/fsl/ptp_qoriq.h>
46 
47 #include "gianfar.h"
48 
49 #define GFAR_MAX_COAL_USECS 0xffff
50 #define GFAR_MAX_COAL_FRAMES 0xff
51 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
52 			    u64 *buf);
53 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
54 static int gfar_gcoalesce(struct net_device *dev,
55 			  struct ethtool_coalesce *cvals);
56 static int gfar_scoalesce(struct net_device *dev,
57 			  struct ethtool_coalesce *cvals);
58 static void gfar_gringparam(struct net_device *dev,
59 			    struct ethtool_ringparam *rvals);
60 static int gfar_sringparam(struct net_device *dev,
61 			   struct ethtool_ringparam *rvals);
62 static void gfar_gdrvinfo(struct net_device *dev,
63 			  struct ethtool_drvinfo *drvinfo);
64 
65 static const char stat_gstrings[][ETH_GSTRING_LEN] = {
66 	/* extra stats */
67 	"rx-allocation-errors",
68 	"rx-large-frame-errors",
69 	"rx-short-frame-errors",
70 	"rx-non-octet-errors",
71 	"rx-crc-errors",
72 	"rx-overrun-errors",
73 	"rx-busy-errors",
74 	"rx-babbling-errors",
75 	"rx-truncated-frames",
76 	"ethernet-bus-error",
77 	"tx-babbling-errors",
78 	"tx-underrun-errors",
79 	"tx-timeout-errors",
80 	/* rmon stats */
81 	"tx-rx-64-frames",
82 	"tx-rx-65-127-frames",
83 	"tx-rx-128-255-frames",
84 	"tx-rx-256-511-frames",
85 	"tx-rx-512-1023-frames",
86 	"tx-rx-1024-1518-frames",
87 	"tx-rx-1519-1522-good-vlan",
88 	"rx-bytes",
89 	"rx-packets",
90 	"rx-fcs-errors",
91 	"receive-multicast-packet",
92 	"receive-broadcast-packet",
93 	"rx-control-frame-packets",
94 	"rx-pause-frame-packets",
95 	"rx-unknown-op-code",
96 	"rx-alignment-error",
97 	"rx-frame-length-error",
98 	"rx-code-error",
99 	"rx-carrier-sense-error",
100 	"rx-undersize-packets",
101 	"rx-oversize-packets",
102 	"rx-fragmented-frames",
103 	"rx-jabber-frames",
104 	"rx-dropped-frames",
105 	"tx-byte-counter",
106 	"tx-packets",
107 	"tx-multicast-packets",
108 	"tx-broadcast-packets",
109 	"tx-pause-control-frames",
110 	"tx-deferral-packets",
111 	"tx-excessive-deferral-packets",
112 	"tx-single-collision-packets",
113 	"tx-multiple-collision-packets",
114 	"tx-late-collision-packets",
115 	"tx-excessive-collision-packets",
116 	"tx-total-collision",
117 	"reserved",
118 	"tx-dropped-frames",
119 	"tx-jabber-frames",
120 	"tx-fcs-errors",
121 	"tx-control-frames",
122 	"tx-oversize-frames",
123 	"tx-undersize-frames",
124 	"tx-fragmented-frames",
125 };
126 
127 /* Fill in a buffer with the strings which correspond to the
128  * stats */
129 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
130 {
131 	struct gfar_private *priv = netdev_priv(dev);
132 
133 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
134 		memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
135 	else
136 		memcpy(buf, stat_gstrings,
137 		       GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
138 }
139 
140 /* Fill in an array of 64-bit statistics from various sources.
141  * This array will be appended to the end of the ethtool_stats
142  * structure, and returned to user space
143  */
144 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
145 			    u64 *buf)
146 {
147 	int i;
148 	struct gfar_private *priv = netdev_priv(dev);
149 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
150 	atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
151 
152 	for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
153 		buf[i] = atomic64_read(&extra[i]);
154 
155 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
156 		u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
157 
158 		for (; i < GFAR_STATS_LEN; i++, rmon++)
159 			buf[i] = (u64) gfar_read(rmon);
160 	}
161 }
162 
163 static int gfar_sset_count(struct net_device *dev, int sset)
164 {
165 	struct gfar_private *priv = netdev_priv(dev);
166 
167 	switch (sset) {
168 	case ETH_SS_STATS:
169 		if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
170 			return GFAR_STATS_LEN;
171 		else
172 			return GFAR_EXTRA_STATS_LEN;
173 	default:
174 		return -EOPNOTSUPP;
175 	}
176 }
177 
178 /* Fills in the drvinfo structure with some basic info */
179 static void gfar_gdrvinfo(struct net_device *dev,
180 			  struct ethtool_drvinfo *drvinfo)
181 {
182 	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
183 	strlcpy(drvinfo->version, gfar_driver_version,
184 		sizeof(drvinfo->version));
185 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
186 	strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
187 }
188 
189 /* Return the length of the register structure */
190 static int gfar_reglen(struct net_device *dev)
191 {
192 	return sizeof (struct gfar);
193 }
194 
195 /* Return a dump of the GFAR register space */
196 static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
197 			  void *regbuf)
198 {
199 	int i;
200 	struct gfar_private *priv = netdev_priv(dev);
201 	u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
202 	u32 *buf = (u32 *) regbuf;
203 
204 	for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
205 		buf[i] = gfar_read(&theregs[i]);
206 }
207 
208 /* Convert microseconds to ethernet clock ticks, which changes
209  * depending on what speed the controller is running at */
210 static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
211 				     unsigned int usecs)
212 {
213 	struct net_device *ndev = priv->ndev;
214 	struct phy_device *phydev = ndev->phydev;
215 	unsigned int count;
216 
217 	/* The timer is different, depending on the interface speed */
218 	switch (phydev->speed) {
219 	case SPEED_1000:
220 		count = GFAR_GBIT_TIME;
221 		break;
222 	case SPEED_100:
223 		count = GFAR_100_TIME;
224 		break;
225 	case SPEED_10:
226 	default:
227 		count = GFAR_10_TIME;
228 		break;
229 	}
230 
231 	/* Make sure we return a number greater than 0
232 	 * if usecs > 0 */
233 	return DIV_ROUND_UP(usecs * 1000, count);
234 }
235 
236 /* Convert ethernet clock ticks to microseconds */
237 static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
238 				     unsigned int ticks)
239 {
240 	struct net_device *ndev = priv->ndev;
241 	struct phy_device *phydev = ndev->phydev;
242 	unsigned int count;
243 
244 	/* The timer is different, depending on the interface speed */
245 	switch (phydev->speed) {
246 	case SPEED_1000:
247 		count = GFAR_GBIT_TIME;
248 		break;
249 	case SPEED_100:
250 		count = GFAR_100_TIME;
251 		break;
252 	case SPEED_10:
253 	default:
254 		count = GFAR_10_TIME;
255 		break;
256 	}
257 
258 	/* Make sure we return a number greater than 0 */
259 	/* if ticks is > 0 */
260 	return (ticks * count) / 1000;
261 }
262 
263 /* Get the coalescing parameters, and put them in the cvals
264  * structure.  */
265 static int gfar_gcoalesce(struct net_device *dev,
266 			  struct ethtool_coalesce *cvals)
267 {
268 	struct gfar_private *priv = netdev_priv(dev);
269 	struct gfar_priv_rx_q *rx_queue = NULL;
270 	struct gfar_priv_tx_q *tx_queue = NULL;
271 	unsigned long rxtime;
272 	unsigned long rxcount;
273 	unsigned long txtime;
274 	unsigned long txcount;
275 
276 	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
277 		return -EOPNOTSUPP;
278 
279 	if (!dev->phydev)
280 		return -ENODEV;
281 
282 	rx_queue = priv->rx_queue[0];
283 	tx_queue = priv->tx_queue[0];
284 
285 	rxtime  = get_ictt_value(rx_queue->rxic);
286 	rxcount = get_icft_value(rx_queue->rxic);
287 	txtime  = get_ictt_value(tx_queue->txic);
288 	txcount = get_icft_value(tx_queue->txic);
289 	cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
290 	cvals->rx_max_coalesced_frames = rxcount;
291 
292 	cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
293 	cvals->tx_max_coalesced_frames = txcount;
294 
295 	cvals->use_adaptive_rx_coalesce = 0;
296 	cvals->use_adaptive_tx_coalesce = 0;
297 
298 	cvals->pkt_rate_low = 0;
299 	cvals->rx_coalesce_usecs_low = 0;
300 	cvals->rx_max_coalesced_frames_low = 0;
301 	cvals->tx_coalesce_usecs_low = 0;
302 	cvals->tx_max_coalesced_frames_low = 0;
303 
304 	/* When the packet rate is below pkt_rate_high but above
305 	 * pkt_rate_low (both measured in packets per second) the
306 	 * normal {rx,tx}_* coalescing parameters are used.
307 	 */
308 
309 	/* When the packet rate is (measured in packets per second)
310 	 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
311 	 * used.
312 	 */
313 	cvals->pkt_rate_high = 0;
314 	cvals->rx_coalesce_usecs_high = 0;
315 	cvals->rx_max_coalesced_frames_high = 0;
316 	cvals->tx_coalesce_usecs_high = 0;
317 	cvals->tx_max_coalesced_frames_high = 0;
318 
319 	/* How often to do adaptive coalescing packet rate sampling,
320 	 * measured in seconds.  Must not be zero.
321 	 */
322 	cvals->rate_sample_interval = 0;
323 
324 	return 0;
325 }
326 
327 /* Change the coalescing values.
328  * Both cvals->*_usecs and cvals->*_frames have to be > 0
329  * in order for coalescing to be active
330  */
331 static int gfar_scoalesce(struct net_device *dev,
332 			  struct ethtool_coalesce *cvals)
333 {
334 	struct gfar_private *priv = netdev_priv(dev);
335 	int i, err = 0;
336 
337 	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
338 		return -EOPNOTSUPP;
339 
340 	if (!dev->phydev)
341 		return -ENODEV;
342 
343 	/* Check the bounds of the values */
344 	if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
345 		netdev_info(dev, "Coalescing is limited to %d microseconds\n",
346 			    GFAR_MAX_COAL_USECS);
347 		return -EINVAL;
348 	}
349 
350 	if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
351 		netdev_info(dev, "Coalescing is limited to %d frames\n",
352 			    GFAR_MAX_COAL_FRAMES);
353 		return -EINVAL;
354 	}
355 
356 	/* Check the bounds of the values */
357 	if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
358 		netdev_info(dev, "Coalescing is limited to %d microseconds\n",
359 			    GFAR_MAX_COAL_USECS);
360 		return -EINVAL;
361 	}
362 
363 	if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
364 		netdev_info(dev, "Coalescing is limited to %d frames\n",
365 			    GFAR_MAX_COAL_FRAMES);
366 		return -EINVAL;
367 	}
368 
369 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
370 		cpu_relax();
371 
372 	/* Set up rx coalescing */
373 	if ((cvals->rx_coalesce_usecs == 0) ||
374 	    (cvals->rx_max_coalesced_frames == 0)) {
375 		for (i = 0; i < priv->num_rx_queues; i++)
376 			priv->rx_queue[i]->rxcoalescing = 0;
377 	} else {
378 		for (i = 0; i < priv->num_rx_queues; i++)
379 			priv->rx_queue[i]->rxcoalescing = 1;
380 	}
381 
382 	for (i = 0; i < priv->num_rx_queues; i++) {
383 		priv->rx_queue[i]->rxic = mk_ic_value(
384 			cvals->rx_max_coalesced_frames,
385 			gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
386 	}
387 
388 	/* Set up tx coalescing */
389 	if ((cvals->tx_coalesce_usecs == 0) ||
390 	    (cvals->tx_max_coalesced_frames == 0)) {
391 		for (i = 0; i < priv->num_tx_queues; i++)
392 			priv->tx_queue[i]->txcoalescing = 0;
393 	} else {
394 		for (i = 0; i < priv->num_tx_queues; i++)
395 			priv->tx_queue[i]->txcoalescing = 1;
396 	}
397 
398 	for (i = 0; i < priv->num_tx_queues; i++) {
399 		priv->tx_queue[i]->txic = mk_ic_value(
400 			cvals->tx_max_coalesced_frames,
401 			gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
402 	}
403 
404 	if (dev->flags & IFF_UP) {
405 		stop_gfar(dev);
406 		err = startup_gfar(dev);
407 	} else {
408 		gfar_mac_reset(priv);
409 	}
410 
411 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
412 
413 	return err;
414 }
415 
416 /* Fills in rvals with the current ring parameters.  Currently,
417  * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
418  * jumbo are ignored by the driver */
419 static void gfar_gringparam(struct net_device *dev,
420 			    struct ethtool_ringparam *rvals)
421 {
422 	struct gfar_private *priv = netdev_priv(dev);
423 	struct gfar_priv_tx_q *tx_queue = NULL;
424 	struct gfar_priv_rx_q *rx_queue = NULL;
425 
426 	tx_queue = priv->tx_queue[0];
427 	rx_queue = priv->rx_queue[0];
428 
429 	rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
430 	rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
431 	rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
432 	rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
433 
434 	/* Values changeable by the user.  The valid values are
435 	 * in the range 1 to the "*_max_pending" counterpart above.
436 	 */
437 	rvals->rx_pending = rx_queue->rx_ring_size;
438 	rvals->rx_mini_pending = rx_queue->rx_ring_size;
439 	rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
440 	rvals->tx_pending = tx_queue->tx_ring_size;
441 }
442 
443 /* Change the current ring parameters, stopping the controller if
444  * necessary so that we don't mess things up while we're in motion.
445  */
446 static int gfar_sringparam(struct net_device *dev,
447 			   struct ethtool_ringparam *rvals)
448 {
449 	struct gfar_private *priv = netdev_priv(dev);
450 	int err = 0, i;
451 
452 	if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
453 		return -EINVAL;
454 
455 	if (!is_power_of_2(rvals->rx_pending)) {
456 		netdev_err(dev, "Ring sizes must be a power of 2\n");
457 		return -EINVAL;
458 	}
459 
460 	if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
461 		return -EINVAL;
462 
463 	if (!is_power_of_2(rvals->tx_pending)) {
464 		netdev_err(dev, "Ring sizes must be a power of 2\n");
465 		return -EINVAL;
466 	}
467 
468 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
469 		cpu_relax();
470 
471 	if (dev->flags & IFF_UP)
472 		stop_gfar(dev);
473 
474 	/* Change the sizes */
475 	for (i = 0; i < priv->num_rx_queues; i++)
476 		priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
477 
478 	for (i = 0; i < priv->num_tx_queues; i++)
479 		priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
480 
481 	/* Rebuild the rings with the new size */
482 	if (dev->flags & IFF_UP)
483 		err = startup_gfar(dev);
484 
485 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
486 
487 	return err;
488 }
489 
490 static void gfar_gpauseparam(struct net_device *dev,
491 			     struct ethtool_pauseparam *epause)
492 {
493 	struct gfar_private *priv = netdev_priv(dev);
494 
495 	epause->autoneg = !!priv->pause_aneg_en;
496 	epause->rx_pause = !!priv->rx_pause_en;
497 	epause->tx_pause = !!priv->tx_pause_en;
498 }
499 
500 static int gfar_spauseparam(struct net_device *dev,
501 			    struct ethtool_pauseparam *epause)
502 {
503 	struct gfar_private *priv = netdev_priv(dev);
504 	struct phy_device *phydev = dev->phydev;
505 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
506 
507 	if (!phydev)
508 		return -ENODEV;
509 
510 	if (!phy_validate_pause(phydev, epause))
511 		return -EINVAL;
512 
513 	priv->rx_pause_en = priv->tx_pause_en = 0;
514 	phy_set_asym_pause(phydev, epause->rx_pause, epause->tx_pause);
515 	if (epause->rx_pause) {
516 		priv->rx_pause_en = 1;
517 
518 		if (epause->tx_pause) {
519 			priv->tx_pause_en = 1;
520 		}
521 	} else if (epause->tx_pause) {
522 		priv->tx_pause_en = 1;
523 	}
524 
525 	if (epause->autoneg)
526 		priv->pause_aneg_en = 1;
527 	else
528 		priv->pause_aneg_en = 0;
529 
530 	if (!epause->autoneg) {
531 		u32 tempval = gfar_read(&regs->maccfg1);
532 
533 		tempval &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
534 
535 		priv->tx_actual_en = 0;
536 		if (priv->tx_pause_en) {
537 			priv->tx_actual_en = 1;
538 			tempval |= MACCFG1_TX_FLOW;
539 		}
540 
541 		if (priv->rx_pause_en)
542 			tempval |= MACCFG1_RX_FLOW;
543 		gfar_write(&regs->maccfg1, tempval);
544 	}
545 
546 	return 0;
547 }
548 
549 int gfar_set_features(struct net_device *dev, netdev_features_t features)
550 {
551 	netdev_features_t changed = dev->features ^ features;
552 	struct gfar_private *priv = netdev_priv(dev);
553 	int err = 0;
554 
555 	if (!(changed & (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
556 			 NETIF_F_RXCSUM)))
557 		return 0;
558 
559 	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
560 		cpu_relax();
561 
562 	dev->features = features;
563 
564 	if (dev->flags & IFF_UP) {
565 		/* Now we take down the rings to rebuild them */
566 		stop_gfar(dev);
567 		err = startup_gfar(dev);
568 	} else {
569 		gfar_mac_reset(priv);
570 	}
571 
572 	clear_bit_unlock(GFAR_RESETTING, &priv->state);
573 
574 	return err;
575 }
576 
577 static uint32_t gfar_get_msglevel(struct net_device *dev)
578 {
579 	struct gfar_private *priv = netdev_priv(dev);
580 
581 	return priv->msg_enable;
582 }
583 
584 static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
585 {
586 	struct gfar_private *priv = netdev_priv(dev);
587 
588 	priv->msg_enable = data;
589 }
590 
591 #ifdef CONFIG_PM
592 static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
593 {
594 	struct gfar_private *priv = netdev_priv(dev);
595 
596 	wol->supported = 0;
597 	wol->wolopts = 0;
598 
599 	if (priv->wol_supported & GFAR_WOL_MAGIC)
600 		wol->supported |= WAKE_MAGIC;
601 
602 	if (priv->wol_supported & GFAR_WOL_FILER_UCAST)
603 		wol->supported |= WAKE_UCAST;
604 
605 	if (priv->wol_opts & GFAR_WOL_MAGIC)
606 		wol->wolopts |= WAKE_MAGIC;
607 
608 	if (priv->wol_opts & GFAR_WOL_FILER_UCAST)
609 		wol->wolopts |= WAKE_UCAST;
610 }
611 
612 static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
613 {
614 	struct gfar_private *priv = netdev_priv(dev);
615 	u16 wol_opts = 0;
616 	int err;
617 
618 	if (!priv->wol_supported && wol->wolopts)
619 		return -EINVAL;
620 
621 	if (wol->wolopts & ~(WAKE_MAGIC | WAKE_UCAST))
622 		return -EINVAL;
623 
624 	if (wol->wolopts & WAKE_MAGIC) {
625 		wol_opts |= GFAR_WOL_MAGIC;
626 	} else {
627 		if (wol->wolopts & WAKE_UCAST)
628 			wol_opts |= GFAR_WOL_FILER_UCAST;
629 	}
630 
631 	wol_opts &= priv->wol_supported;
632 	priv->wol_opts = 0;
633 
634 	err = device_set_wakeup_enable(priv->dev, wol_opts);
635 	if (err)
636 		return err;
637 
638 	priv->wol_opts = wol_opts;
639 
640 	return 0;
641 }
642 #endif
643 
644 static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
645 {
646 	u32 fcr = 0x0, fpr = FPR_FILER_MASK;
647 
648 	if (ethflow & RXH_L2DA) {
649 		fcr = RQFCR_PID_DAH | RQFCR_CMP_NOMATCH |
650 		      RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
651 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
652 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
653 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
654 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
655 
656 		fcr = RQFCR_PID_DAL | RQFCR_CMP_NOMATCH |
657 		      RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
658 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
659 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
660 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
661 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
662 	}
663 
664 	if (ethflow & RXH_VLAN) {
665 		fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
666 		      RQFCR_AND | RQFCR_HASHTBL_0;
667 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
668 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
669 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
670 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
671 	}
672 
673 	if (ethflow & RXH_IP_SRC) {
674 		fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
675 		      RQFCR_AND | RQFCR_HASHTBL_0;
676 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
677 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
678 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
679 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
680 	}
681 
682 	if (ethflow & (RXH_IP_DST)) {
683 		fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
684 		      RQFCR_AND | RQFCR_HASHTBL_0;
685 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
686 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
687 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
688 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
689 	}
690 
691 	if (ethflow & RXH_L3_PROTO) {
692 		fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
693 		      RQFCR_AND | RQFCR_HASHTBL_0;
694 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
695 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
696 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
697 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
698 	}
699 
700 	if (ethflow & RXH_L4_B_0_1) {
701 		fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
702 		      RQFCR_AND | RQFCR_HASHTBL_0;
703 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
704 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
705 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
706 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
707 	}
708 
709 	if (ethflow & RXH_L4_B_2_3) {
710 		fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
711 		      RQFCR_AND | RQFCR_HASHTBL_0;
712 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
713 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
714 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
715 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
716 	}
717 }
718 
719 static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
720 				       u64 class)
721 {
722 	unsigned int cmp_rqfpr;
723 	unsigned int *local_rqfpr;
724 	unsigned int *local_rqfcr;
725 	int i = 0x0, k = 0x0;
726 	int j = MAX_FILER_IDX, l = 0x0;
727 	int ret = 1;
728 
729 	local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
730 				    GFP_KERNEL);
731 	local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
732 				    GFP_KERNEL);
733 	if (!local_rqfpr || !local_rqfcr) {
734 		ret = 0;
735 		goto err;
736 	}
737 
738 	switch (class) {
739 	case TCP_V4_FLOW:
740 		cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
741 		break;
742 	case UDP_V4_FLOW:
743 		cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
744 		break;
745 	case TCP_V6_FLOW:
746 		cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
747 		break;
748 	case UDP_V6_FLOW:
749 		cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
750 		break;
751 	default:
752 		netdev_err(priv->ndev,
753 			   "Right now this class is not supported\n");
754 		ret = 0;
755 		goto err;
756 	}
757 
758 	for (i = 0; i < MAX_FILER_IDX + 1; i++) {
759 		local_rqfpr[j] = priv->ftp_rqfpr[i];
760 		local_rqfcr[j] = priv->ftp_rqfcr[i];
761 		j--;
762 		if ((priv->ftp_rqfcr[i] ==
763 		     (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
764 		    (priv->ftp_rqfpr[i] == cmp_rqfpr))
765 			break;
766 	}
767 
768 	if (i == MAX_FILER_IDX + 1) {
769 		netdev_err(priv->ndev,
770 			   "No parse rule found, can't create hash rules\n");
771 		ret = 0;
772 		goto err;
773 	}
774 
775 	/* If a match was found, then it begins the starting of a cluster rule
776 	 * if it was already programmed, we need to overwrite these rules
777 	 */
778 	for (l = i+1; l < MAX_FILER_IDX; l++) {
779 		if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
780 		    !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
781 			priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
782 					     RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
783 			priv->ftp_rqfpr[l] = FPR_FILER_MASK;
784 			gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
785 					 priv->ftp_rqfpr[l]);
786 			break;
787 		}
788 
789 		if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
790 			(priv->ftp_rqfcr[l] & RQFCR_AND))
791 			continue;
792 		else {
793 			local_rqfpr[j] = priv->ftp_rqfpr[l];
794 			local_rqfcr[j] = priv->ftp_rqfcr[l];
795 			j--;
796 		}
797 	}
798 
799 	priv->cur_filer_idx = l - 1;
800 
801 	/* hash rules */
802 	ethflow_to_filer_rules(priv, ethflow);
803 
804 	/* Write back the popped out rules again */
805 	for (k = j+1; k < MAX_FILER_IDX; k++) {
806 		priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
807 		priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
808 		gfar_write_filer(priv, priv->cur_filer_idx,
809 				 local_rqfcr[k], local_rqfpr[k]);
810 		if (!priv->cur_filer_idx)
811 			break;
812 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
813 	}
814 
815 err:
816 	kfree(local_rqfcr);
817 	kfree(local_rqfpr);
818 	return ret;
819 }
820 
821 static int gfar_set_hash_opts(struct gfar_private *priv,
822 			      struct ethtool_rxnfc *cmd)
823 {
824 	/* write the filer rules here */
825 	if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
826 		return -EINVAL;
827 
828 	return 0;
829 }
830 
831 static int gfar_check_filer_hardware(struct gfar_private *priv)
832 {
833 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
834 	u32 i;
835 
836 	/* Check if we are in FIFO mode */
837 	i = gfar_read(&regs->ecntrl);
838 	i &= ECNTRL_FIFM;
839 	if (i == ECNTRL_FIFM) {
840 		netdev_notice(priv->ndev, "Interface in FIFO mode\n");
841 		i = gfar_read(&regs->rctrl);
842 		i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
843 		if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
844 			netdev_info(priv->ndev,
845 				    "Receive Queue Filtering enabled\n");
846 		} else {
847 			netdev_warn(priv->ndev,
848 				    "Receive Queue Filtering disabled\n");
849 			return -EOPNOTSUPP;
850 		}
851 	}
852 	/* Or in standard mode */
853 	else {
854 		i = gfar_read(&regs->rctrl);
855 		i &= RCTRL_PRSDEP_MASK;
856 		if (i == RCTRL_PRSDEP_MASK) {
857 			netdev_info(priv->ndev,
858 				    "Receive Queue Filtering enabled\n");
859 		} else {
860 			netdev_warn(priv->ndev,
861 				    "Receive Queue Filtering disabled\n");
862 			return -EOPNOTSUPP;
863 		}
864 	}
865 
866 	/* Sets the properties for arbitrary filer rule
867 	 * to the first 4 Layer 4 Bytes
868 	 */
869 	gfar_write(&regs->rbifx, 0xC0C1C2C3);
870 	return 0;
871 }
872 
873 /* Write a mask to filer cache */
874 static void gfar_set_mask(u32 mask, struct filer_table *tab)
875 {
876 	tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
877 	tab->fe[tab->index].prop = mask;
878 	tab->index++;
879 }
880 
881 /* Sets parse bits (e.g. IP or TCP) */
882 static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
883 {
884 	gfar_set_mask(mask, tab);
885 	tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
886 				   RQFCR_AND;
887 	tab->fe[tab->index].prop = value;
888 	tab->index++;
889 }
890 
891 static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
892 				       struct filer_table *tab)
893 {
894 	gfar_set_mask(mask, tab);
895 	tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
896 	tab->fe[tab->index].prop = value;
897 	tab->index++;
898 }
899 
900 /* For setting a tuple of value and mask of type flag
901  * Example:
902  * IP-Src = 10.0.0.0/255.0.0.0
903  * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
904  *
905  * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
906  * For a don't care mask it gives us a 0
907  *
908  * The check if don't care and the mask adjustment if mask=0 is done for VLAN
909  * and MAC stuff on an upper level (due to missing information on this level).
910  * For these guys we can discard them if they are value=0 and mask=0.
911  *
912  * Further the all masks are one-padded for better hardware efficiency.
913  */
914 static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
915 			       struct filer_table *tab)
916 {
917 	switch (flag) {
918 		/* 3bit */
919 	case RQFCR_PID_PRI:
920 		if (!(value | mask))
921 			return;
922 		mask |= RQFCR_PID_PRI_MASK;
923 		break;
924 		/* 8bit */
925 	case RQFCR_PID_L4P:
926 	case RQFCR_PID_TOS:
927 		if (!~(mask | RQFCR_PID_L4P_MASK))
928 			return;
929 		if (!mask)
930 			mask = ~0;
931 		else
932 			mask |= RQFCR_PID_L4P_MASK;
933 		break;
934 		/* 12bit */
935 	case RQFCR_PID_VID:
936 		if (!(value | mask))
937 			return;
938 		mask |= RQFCR_PID_VID_MASK;
939 		break;
940 		/* 16bit */
941 	case RQFCR_PID_DPT:
942 	case RQFCR_PID_SPT:
943 	case RQFCR_PID_ETY:
944 		if (!~(mask | RQFCR_PID_PORT_MASK))
945 			return;
946 		if (!mask)
947 			mask = ~0;
948 		else
949 			mask |= RQFCR_PID_PORT_MASK;
950 		break;
951 		/* 24bit */
952 	case RQFCR_PID_DAH:
953 	case RQFCR_PID_DAL:
954 	case RQFCR_PID_SAH:
955 	case RQFCR_PID_SAL:
956 		if (!(value | mask))
957 			return;
958 		mask |= RQFCR_PID_MAC_MASK;
959 		break;
960 		/* for all real 32bit masks */
961 	default:
962 		if (!~mask)
963 			return;
964 		if (!mask)
965 			mask = ~0;
966 		break;
967 	}
968 	gfar_set_general_attribute(value, mask, flag, tab);
969 }
970 
971 /* Translates value and mask for UDP, TCP or SCTP */
972 static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
973 			      struct ethtool_tcpip4_spec *mask,
974 			      struct filer_table *tab)
975 {
976 	gfar_set_attribute(be32_to_cpu(value->ip4src),
977 			   be32_to_cpu(mask->ip4src),
978 			   RQFCR_PID_SIA, tab);
979 	gfar_set_attribute(be32_to_cpu(value->ip4dst),
980 			   be32_to_cpu(mask->ip4dst),
981 			   RQFCR_PID_DIA, tab);
982 	gfar_set_attribute(be16_to_cpu(value->pdst),
983 			   be16_to_cpu(mask->pdst),
984 			   RQFCR_PID_DPT, tab);
985 	gfar_set_attribute(be16_to_cpu(value->psrc),
986 			   be16_to_cpu(mask->psrc),
987 			   RQFCR_PID_SPT, tab);
988 	gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
989 }
990 
991 /* Translates value and mask for RAW-IP4 */
992 static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
993 			     struct ethtool_usrip4_spec *mask,
994 			     struct filer_table *tab)
995 {
996 	gfar_set_attribute(be32_to_cpu(value->ip4src),
997 			   be32_to_cpu(mask->ip4src),
998 			   RQFCR_PID_SIA, tab);
999 	gfar_set_attribute(be32_to_cpu(value->ip4dst),
1000 			   be32_to_cpu(mask->ip4dst),
1001 			   RQFCR_PID_DIA, tab);
1002 	gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1003 	gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
1004 	gfar_set_attribute(be32_to_cpu(value->l4_4_bytes),
1005 			   be32_to_cpu(mask->l4_4_bytes),
1006 			   RQFCR_PID_ARB, tab);
1007 
1008 }
1009 
1010 /* Translates value and mask for ETHER spec */
1011 static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
1012 			   struct filer_table *tab)
1013 {
1014 	u32 upper_temp_mask = 0;
1015 	u32 lower_temp_mask = 0;
1016 
1017 	/* Source address */
1018 	if (!is_broadcast_ether_addr(mask->h_source)) {
1019 		if (is_zero_ether_addr(mask->h_source)) {
1020 			upper_temp_mask = 0xFFFFFFFF;
1021 			lower_temp_mask = 0xFFFFFFFF;
1022 		} else {
1023 			upper_temp_mask = mask->h_source[0] << 16 |
1024 					  mask->h_source[1] << 8  |
1025 					  mask->h_source[2];
1026 			lower_temp_mask = mask->h_source[3] << 16 |
1027 					  mask->h_source[4] << 8  |
1028 					  mask->h_source[5];
1029 		}
1030 		/* Upper 24bit */
1031 		gfar_set_attribute(value->h_source[0] << 16 |
1032 				   value->h_source[1] << 8  |
1033 				   value->h_source[2],
1034 				   upper_temp_mask, RQFCR_PID_SAH, tab);
1035 		/* And the same for the lower part */
1036 		gfar_set_attribute(value->h_source[3] << 16 |
1037 				   value->h_source[4] << 8  |
1038 				   value->h_source[5],
1039 				   lower_temp_mask, RQFCR_PID_SAL, tab);
1040 	}
1041 	/* Destination address */
1042 	if (!is_broadcast_ether_addr(mask->h_dest)) {
1043 		/* Special for destination is limited broadcast */
1044 		if ((is_broadcast_ether_addr(value->h_dest) &&
1045 		    is_zero_ether_addr(mask->h_dest))) {
1046 			gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
1047 		} else {
1048 			if (is_zero_ether_addr(mask->h_dest)) {
1049 				upper_temp_mask = 0xFFFFFFFF;
1050 				lower_temp_mask = 0xFFFFFFFF;
1051 			} else {
1052 				upper_temp_mask = mask->h_dest[0] << 16 |
1053 						  mask->h_dest[1] << 8  |
1054 						  mask->h_dest[2];
1055 				lower_temp_mask = mask->h_dest[3] << 16 |
1056 						  mask->h_dest[4] << 8  |
1057 						  mask->h_dest[5];
1058 			}
1059 
1060 			/* Upper 24bit */
1061 			gfar_set_attribute(value->h_dest[0] << 16 |
1062 					   value->h_dest[1] << 8  |
1063 					   value->h_dest[2],
1064 					   upper_temp_mask, RQFCR_PID_DAH, tab);
1065 			/* And the same for the lower part */
1066 			gfar_set_attribute(value->h_dest[3] << 16 |
1067 					   value->h_dest[4] << 8  |
1068 					   value->h_dest[5],
1069 					   lower_temp_mask, RQFCR_PID_DAL, tab);
1070 		}
1071 	}
1072 
1073 	gfar_set_attribute(be16_to_cpu(value->h_proto),
1074 			   be16_to_cpu(mask->h_proto),
1075 			   RQFCR_PID_ETY, tab);
1076 }
1077 
1078 static inline u32 vlan_tci_vid(struct ethtool_rx_flow_spec *rule)
1079 {
1080 	return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_VID_MASK;
1081 }
1082 
1083 static inline u32 vlan_tci_vidm(struct ethtool_rx_flow_spec *rule)
1084 {
1085 	return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_VID_MASK;
1086 }
1087 
1088 static inline u32 vlan_tci_cfi(struct ethtool_rx_flow_spec *rule)
1089 {
1090 	return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_CFI_MASK;
1091 }
1092 
1093 static inline u32 vlan_tci_cfim(struct ethtool_rx_flow_spec *rule)
1094 {
1095 	return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_CFI_MASK;
1096 }
1097 
1098 static inline u32 vlan_tci_prio(struct ethtool_rx_flow_spec *rule)
1099 {
1100 	return (be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_PRIO_MASK) >>
1101 		VLAN_PRIO_SHIFT;
1102 }
1103 
1104 static inline u32 vlan_tci_priom(struct ethtool_rx_flow_spec *rule)
1105 {
1106 	return (be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_PRIO_MASK) >>
1107 		VLAN_PRIO_SHIFT;
1108 }
1109 
1110 /* Convert a rule to binary filter format of gianfar */
1111 static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
1112 				 struct filer_table *tab)
1113 {
1114 	u32 vlan = 0, vlan_mask = 0;
1115 	u32 id = 0, id_mask = 0;
1116 	u32 cfi = 0, cfi_mask = 0;
1117 	u32 prio = 0, prio_mask = 0;
1118 	u32 old_index = tab->index;
1119 
1120 	/* Check if vlan is wanted */
1121 	if ((rule->flow_type & FLOW_EXT) &&
1122 	    (rule->m_ext.vlan_tci != cpu_to_be16(0xFFFF))) {
1123 		if (!rule->m_ext.vlan_tci)
1124 			rule->m_ext.vlan_tci = cpu_to_be16(0xFFFF);
1125 
1126 		vlan = RQFPR_VLN;
1127 		vlan_mask = RQFPR_VLN;
1128 
1129 		/* Separate the fields */
1130 		id = vlan_tci_vid(rule);
1131 		id_mask = vlan_tci_vidm(rule);
1132 		cfi = vlan_tci_cfi(rule);
1133 		cfi_mask = vlan_tci_cfim(rule);
1134 		prio = vlan_tci_prio(rule);
1135 		prio_mask = vlan_tci_priom(rule);
1136 
1137 		if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
1138 			vlan |= RQFPR_CFI;
1139 			vlan_mask |= RQFPR_CFI;
1140 		} else if (cfi != VLAN_TAG_PRESENT &&
1141 			   cfi_mask == VLAN_TAG_PRESENT) {
1142 			vlan_mask |= RQFPR_CFI;
1143 		}
1144 	}
1145 
1146 	switch (rule->flow_type & ~FLOW_EXT) {
1147 	case TCP_V4_FLOW:
1148 		gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
1149 				    RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
1150 		gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
1151 				  &rule->m_u.tcp_ip4_spec, tab);
1152 		break;
1153 	case UDP_V4_FLOW:
1154 		gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
1155 				    RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
1156 		gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
1157 				  &rule->m_u.udp_ip4_spec, tab);
1158 		break;
1159 	case SCTP_V4_FLOW:
1160 		gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1161 				    tab);
1162 		gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
1163 		gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
1164 				  (struct ethtool_tcpip4_spec *)&rule->m_u,
1165 				  tab);
1166 		break;
1167 	case IP_USER_FLOW:
1168 		gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1169 				    tab);
1170 		gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
1171 				 (struct ethtool_usrip4_spec *) &rule->m_u,
1172 				 tab);
1173 		break;
1174 	case ETHER_FLOW:
1175 		if (vlan)
1176 			gfar_set_parse_bits(vlan, vlan_mask, tab);
1177 		gfar_set_ether((struct ethhdr *) &rule->h_u,
1178 			       (struct ethhdr *) &rule->m_u, tab);
1179 		break;
1180 	default:
1181 		return -1;
1182 	}
1183 
1184 	/* Set the vlan attributes in the end */
1185 	if (vlan) {
1186 		gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
1187 		gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
1188 	}
1189 
1190 	/* If there has been nothing written till now, it must be a default */
1191 	if (tab->index == old_index) {
1192 		gfar_set_mask(0xFFFFFFFF, tab);
1193 		tab->fe[tab->index].ctrl = 0x20;
1194 		tab->fe[tab->index].prop = 0x0;
1195 		tab->index++;
1196 	}
1197 
1198 	/* Remove last AND */
1199 	tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
1200 
1201 	/* Specify which queue to use or to drop */
1202 	if (rule->ring_cookie == RX_CLS_FLOW_DISC)
1203 		tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
1204 	else
1205 		tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
1206 
1207 	/* Only big enough entries can be clustered */
1208 	if (tab->index > (old_index + 2)) {
1209 		tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
1210 		tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
1211 	}
1212 
1213 	/* In rare cases the cache can be full while there is
1214 	 * free space in hw
1215 	 */
1216 	if (tab->index > MAX_FILER_CACHE_IDX - 1)
1217 		return -EBUSY;
1218 
1219 	return 0;
1220 }
1221 
1222 /* Write the bit-pattern from software's buffer to hardware registers */
1223 static int gfar_write_filer_table(struct gfar_private *priv,
1224 				  struct filer_table *tab)
1225 {
1226 	u32 i = 0;
1227 	if (tab->index > MAX_FILER_IDX - 1)
1228 		return -EBUSY;
1229 
1230 	/* Fill regular entries */
1231 	for (; i < MAX_FILER_IDX && (tab->fe[i].ctrl | tab->fe[i].prop); i++)
1232 		gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
1233 	/* Fill the rest with fall-troughs */
1234 	for (; i < MAX_FILER_IDX; i++)
1235 		gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
1236 	/* Last entry must be default accept
1237 	 * because that's what people expect
1238 	 */
1239 	gfar_write_filer(priv, i, 0x20, 0x0);
1240 
1241 	return 0;
1242 }
1243 
1244 static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
1245 				 struct gfar_private *priv)
1246 {
1247 
1248 	if (flow->flow_type & FLOW_EXT)	{
1249 		if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
1250 			netdev_warn(priv->ndev,
1251 				    "User-specific data not supported!\n");
1252 		if (~flow->m_ext.vlan_etype)
1253 			netdev_warn(priv->ndev,
1254 				    "VLAN-etype not supported!\n");
1255 	}
1256 	if (flow->flow_type == IP_USER_FLOW)
1257 		if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
1258 			netdev_warn(priv->ndev,
1259 				    "IP-Version differing from IPv4 not supported!\n");
1260 
1261 	return 0;
1262 }
1263 
1264 static int gfar_process_filer_changes(struct gfar_private *priv)
1265 {
1266 	struct ethtool_flow_spec_container *j;
1267 	struct filer_table *tab;
1268 	s32 ret = 0;
1269 
1270 	/* So index is set to zero, too! */
1271 	tab = kzalloc(sizeof(*tab), GFP_KERNEL);
1272 	if (tab == NULL)
1273 		return -ENOMEM;
1274 
1275 	/* Now convert the existing filer data from flow_spec into
1276 	 * filer tables binary format
1277 	 */
1278 	list_for_each_entry(j, &priv->rx_list.list, list) {
1279 		ret = gfar_convert_to_filer(&j->fs, tab);
1280 		if (ret == -EBUSY) {
1281 			netdev_err(priv->ndev,
1282 				   "Rule not added: No free space!\n");
1283 			goto end;
1284 		}
1285 		if (ret == -1) {
1286 			netdev_err(priv->ndev,
1287 				   "Rule not added: Unsupported Flow-type!\n");
1288 			goto end;
1289 		}
1290 	}
1291 
1292 	/* Write everything to hardware */
1293 	ret = gfar_write_filer_table(priv, tab);
1294 	if (ret == -EBUSY) {
1295 		netdev_err(priv->ndev, "Rule not added: No free space!\n");
1296 		goto end;
1297 	}
1298 
1299 end:
1300 	kfree(tab);
1301 	return ret;
1302 }
1303 
1304 static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
1305 {
1306 	u32 i = 0;
1307 
1308 	for (i = 0; i < sizeof(flow->m_u); i++)
1309 		flow->m_u.hdata[i] ^= 0xFF;
1310 
1311 	flow->m_ext.vlan_etype ^= cpu_to_be16(0xFFFF);
1312 	flow->m_ext.vlan_tci ^= cpu_to_be16(0xFFFF);
1313 	flow->m_ext.data[0] ^= cpu_to_be32(~0);
1314 	flow->m_ext.data[1] ^= cpu_to_be32(~0);
1315 }
1316 
1317 static int gfar_add_cls(struct gfar_private *priv,
1318 			struct ethtool_rx_flow_spec *flow)
1319 {
1320 	struct ethtool_flow_spec_container *temp, *comp;
1321 	int ret = 0;
1322 
1323 	temp = kmalloc(sizeof(*temp), GFP_KERNEL);
1324 	if (temp == NULL)
1325 		return -ENOMEM;
1326 	memcpy(&temp->fs, flow, sizeof(temp->fs));
1327 
1328 	gfar_invert_masks(&temp->fs);
1329 	ret = gfar_check_capability(&temp->fs, priv);
1330 	if (ret)
1331 		goto clean_mem;
1332 	/* Link in the new element at the right @location */
1333 	if (list_empty(&priv->rx_list.list)) {
1334 		ret = gfar_check_filer_hardware(priv);
1335 		if (ret != 0)
1336 			goto clean_mem;
1337 		list_add(&temp->list, &priv->rx_list.list);
1338 		goto process;
1339 	} else {
1340 		list_for_each_entry(comp, &priv->rx_list.list, list) {
1341 			if (comp->fs.location > flow->location) {
1342 				list_add_tail(&temp->list, &comp->list);
1343 				goto process;
1344 			}
1345 			if (comp->fs.location == flow->location) {
1346 				netdev_err(priv->ndev,
1347 					   "Rule not added: ID %d not free!\n",
1348 					   flow->location);
1349 				ret = -EBUSY;
1350 				goto clean_mem;
1351 			}
1352 		}
1353 		list_add_tail(&temp->list, &priv->rx_list.list);
1354 	}
1355 
1356 process:
1357 	priv->rx_list.count++;
1358 	ret = gfar_process_filer_changes(priv);
1359 	if (ret)
1360 		goto clean_list;
1361 	return ret;
1362 
1363 clean_list:
1364 	priv->rx_list.count--;
1365 	list_del(&temp->list);
1366 clean_mem:
1367 	kfree(temp);
1368 	return ret;
1369 }
1370 
1371 static int gfar_del_cls(struct gfar_private *priv, u32 loc)
1372 {
1373 	struct ethtool_flow_spec_container *comp;
1374 	u32 ret = -EINVAL;
1375 
1376 	if (list_empty(&priv->rx_list.list))
1377 		return ret;
1378 
1379 	list_for_each_entry(comp, &priv->rx_list.list, list) {
1380 		if (comp->fs.location == loc) {
1381 			list_del(&comp->list);
1382 			kfree(comp);
1383 			priv->rx_list.count--;
1384 			gfar_process_filer_changes(priv);
1385 			ret = 0;
1386 			break;
1387 		}
1388 	}
1389 
1390 	return ret;
1391 }
1392 
1393 static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
1394 {
1395 	struct ethtool_flow_spec_container *comp;
1396 	u32 ret = -EINVAL;
1397 
1398 	list_for_each_entry(comp, &priv->rx_list.list, list) {
1399 		if (comp->fs.location == cmd->fs.location) {
1400 			memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
1401 			gfar_invert_masks(&cmd->fs);
1402 			ret = 0;
1403 			break;
1404 		}
1405 	}
1406 
1407 	return ret;
1408 }
1409 
1410 static int gfar_get_cls_all(struct gfar_private *priv,
1411 			    struct ethtool_rxnfc *cmd, u32 *rule_locs)
1412 {
1413 	struct ethtool_flow_spec_container *comp;
1414 	u32 i = 0;
1415 
1416 	list_for_each_entry(comp, &priv->rx_list.list, list) {
1417 		if (i == cmd->rule_cnt)
1418 			return -EMSGSIZE;
1419 		rule_locs[i] = comp->fs.location;
1420 		i++;
1421 	}
1422 
1423 	cmd->data = MAX_FILER_IDX;
1424 	cmd->rule_cnt = i;
1425 
1426 	return 0;
1427 }
1428 
1429 static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1430 {
1431 	struct gfar_private *priv = netdev_priv(dev);
1432 	int ret = 0;
1433 
1434 	if (test_bit(GFAR_RESETTING, &priv->state))
1435 		return -EBUSY;
1436 
1437 	mutex_lock(&priv->rx_queue_access);
1438 
1439 	switch (cmd->cmd) {
1440 	case ETHTOOL_SRXFH:
1441 		ret = gfar_set_hash_opts(priv, cmd);
1442 		break;
1443 	case ETHTOOL_SRXCLSRLINS:
1444 		if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
1445 		     cmd->fs.ring_cookie >= priv->num_rx_queues) ||
1446 		    cmd->fs.location >= MAX_FILER_IDX) {
1447 			ret = -EINVAL;
1448 			break;
1449 		}
1450 		ret = gfar_add_cls(priv, &cmd->fs);
1451 		break;
1452 	case ETHTOOL_SRXCLSRLDEL:
1453 		ret = gfar_del_cls(priv, cmd->fs.location);
1454 		break;
1455 	default:
1456 		ret = -EINVAL;
1457 	}
1458 
1459 	mutex_unlock(&priv->rx_queue_access);
1460 
1461 	return ret;
1462 }
1463 
1464 static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1465 			u32 *rule_locs)
1466 {
1467 	struct gfar_private *priv = netdev_priv(dev);
1468 	int ret = 0;
1469 
1470 	switch (cmd->cmd) {
1471 	case ETHTOOL_GRXRINGS:
1472 		cmd->data = priv->num_rx_queues;
1473 		break;
1474 	case ETHTOOL_GRXCLSRLCNT:
1475 		cmd->rule_cnt = priv->rx_list.count;
1476 		break;
1477 	case ETHTOOL_GRXCLSRULE:
1478 		ret = gfar_get_cls(priv, cmd);
1479 		break;
1480 	case ETHTOOL_GRXCLSRLALL:
1481 		ret = gfar_get_cls_all(priv, cmd, rule_locs);
1482 		break;
1483 	default:
1484 		ret = -EINVAL;
1485 		break;
1486 	}
1487 
1488 	return ret;
1489 }
1490 
1491 static int gfar_get_ts_info(struct net_device *dev,
1492 			    struct ethtool_ts_info *info)
1493 {
1494 	struct gfar_private *priv = netdev_priv(dev);
1495 	struct platform_device *ptp_dev;
1496 	struct device_node *ptp_node;
1497 	struct qoriq_ptp *ptp = NULL;
1498 
1499 	info->phc_index = -1;
1500 
1501 	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
1502 		info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
1503 					SOF_TIMESTAMPING_SOFTWARE;
1504 		return 0;
1505 	}
1506 
1507 	ptp_node = of_find_compatible_node(NULL, NULL, "fsl,etsec-ptp");
1508 	if (ptp_node) {
1509 		ptp_dev = of_find_device_by_node(ptp_node);
1510 		if (ptp_dev)
1511 			ptp = platform_get_drvdata(ptp_dev);
1512 	}
1513 
1514 	if (ptp)
1515 		info->phc_index = ptp->phc_index;
1516 
1517 	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
1518 				SOF_TIMESTAMPING_RX_HARDWARE |
1519 				SOF_TIMESTAMPING_RAW_HARDWARE;
1520 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1521 			 (1 << HWTSTAMP_TX_ON);
1522 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1523 			   (1 << HWTSTAMP_FILTER_ALL);
1524 	return 0;
1525 }
1526 
1527 const struct ethtool_ops gfar_ethtool_ops = {
1528 	.get_drvinfo = gfar_gdrvinfo,
1529 	.get_regs_len = gfar_reglen,
1530 	.get_regs = gfar_get_regs,
1531 	.get_link = ethtool_op_get_link,
1532 	.get_coalesce = gfar_gcoalesce,
1533 	.set_coalesce = gfar_scoalesce,
1534 	.get_ringparam = gfar_gringparam,
1535 	.set_ringparam = gfar_sringparam,
1536 	.get_pauseparam = gfar_gpauseparam,
1537 	.set_pauseparam = gfar_spauseparam,
1538 	.get_strings = gfar_gstrings,
1539 	.get_sset_count = gfar_sset_count,
1540 	.get_ethtool_stats = gfar_fill_stats,
1541 	.get_msglevel = gfar_get_msglevel,
1542 	.set_msglevel = gfar_set_msglevel,
1543 #ifdef CONFIG_PM
1544 	.get_wol = gfar_get_wol,
1545 	.set_wol = gfar_set_wol,
1546 #endif
1547 	.set_rxnfc = gfar_set_nfc,
1548 	.get_rxnfc = gfar_get_nfc,
1549 	.get_ts_info = gfar_get_ts_info,
1550 	.get_link_ksettings = phy_ethtool_get_link_ksettings,
1551 	.set_link_ksettings = phy_ethtool_set_link_ksettings,
1552 };
1553