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