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/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.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 <asm/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 
45 #include "gianfar.h"
46 
47 extern void gfar_start(struct net_device *dev);
48 extern int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
49 
50 #define GFAR_MAX_COAL_USECS 0xffff
51 #define GFAR_MAX_COAL_FRAMES 0xff
52 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
53 		     u64 * buf);
54 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
55 static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
56 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
57 static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
58 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
59 static void gfar_gdrvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo);
60 
61 static const char stat_gstrings[][ETH_GSTRING_LEN] = {
62 	"rx-dropped-by-kernel",
63 	"rx-large-frame-errors",
64 	"rx-short-frame-errors",
65 	"rx-non-octet-errors",
66 	"rx-crc-errors",
67 	"rx-overrun-errors",
68 	"rx-busy-errors",
69 	"rx-babbling-errors",
70 	"rx-truncated-frames",
71 	"ethernet-bus-error",
72 	"tx-babbling-errors",
73 	"tx-underrun-errors",
74 	"rx-skb-missing-errors",
75 	"tx-timeout-errors",
76 	"tx-rx-64-frames",
77 	"tx-rx-65-127-frames",
78 	"tx-rx-128-255-frames",
79 	"tx-rx-256-511-frames",
80 	"tx-rx-512-1023-frames",
81 	"tx-rx-1024-1518-frames",
82 	"tx-rx-1519-1522-good-vlan",
83 	"rx-bytes",
84 	"rx-packets",
85 	"rx-fcs-errors",
86 	"receive-multicast-packet",
87 	"receive-broadcast-packet",
88 	"rx-control-frame-packets",
89 	"rx-pause-frame-packets",
90 	"rx-unknown-op-code",
91 	"rx-alignment-error",
92 	"rx-frame-length-error",
93 	"rx-code-error",
94 	"rx-carrier-sense-error",
95 	"rx-undersize-packets",
96 	"rx-oversize-packets",
97 	"rx-fragmented-frames",
98 	"rx-jabber-frames",
99 	"rx-dropped-frames",
100 	"tx-byte-counter",
101 	"tx-packets",
102 	"tx-multicast-packets",
103 	"tx-broadcast-packets",
104 	"tx-pause-control-frames",
105 	"tx-deferral-packets",
106 	"tx-excessive-deferral-packets",
107 	"tx-single-collision-packets",
108 	"tx-multiple-collision-packets",
109 	"tx-late-collision-packets",
110 	"tx-excessive-collision-packets",
111 	"tx-total-collision",
112 	"reserved",
113 	"tx-dropped-frames",
114 	"tx-jabber-frames",
115 	"tx-fcs-errors",
116 	"tx-control-frames",
117 	"tx-oversize-frames",
118 	"tx-undersize-frames",
119 	"tx-fragmented-frames",
120 };
121 
122 /* Fill in a buffer with the strings which correspond to the
123  * stats */
124 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
125 {
126 	struct gfar_private *priv = netdev_priv(dev);
127 
128 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
129 		memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
130 	else
131 		memcpy(buf, stat_gstrings,
132 				GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
133 }
134 
135 /* Fill in an array of 64-bit statistics from various sources.
136  * This array will be appended to the end of the ethtool_stats
137  * structure, and returned to user space
138  */
139 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy, u64 * buf)
140 {
141 	int i;
142 	struct gfar_private *priv = netdev_priv(dev);
143 	struct gfar __iomem *regs = priv->gfargrp[0].regs;
144 	u64 *extra = (u64 *) & priv->extra_stats;
145 
146 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
147 		u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
148 		struct gfar_stats *stats = (struct gfar_stats *) buf;
149 
150 		for (i = 0; i < GFAR_RMON_LEN; i++)
151 			stats->rmon[i] = (u64) gfar_read(&rmon[i]);
152 
153 		for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
154 			stats->extra[i] = extra[i];
155 	} else
156 		for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
157 			buf[i] = extra[i];
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, struct
177 	      ethtool_drvinfo *drvinfo)
178 {
179 	strncpy(drvinfo->driver, DRV_NAME, GFAR_INFOSTR_LEN);
180 	strncpy(drvinfo->version, gfar_driver_version, GFAR_INFOSTR_LEN);
181 	strncpy(drvinfo->fw_version, "N/A", GFAR_INFOSTR_LEN);
182 	strncpy(drvinfo->bus_info, "N/A", GFAR_INFOSTR_LEN);
183 	drvinfo->regdump_len = 0;
184 	drvinfo->eedump_len = 0;
185 }
186 
187 
188 static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
189 {
190 	struct gfar_private *priv = netdev_priv(dev);
191 	struct phy_device *phydev = priv->phydev;
192 
193 	if (NULL == phydev)
194 		return -ENODEV;
195 
196 	return phy_ethtool_sset(phydev, cmd);
197 }
198 
199 
200 /* Return the current settings in the ethtool_cmd structure */
201 static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
202 {
203 	struct gfar_private *priv = netdev_priv(dev);
204 	struct phy_device *phydev = priv->phydev;
205 	struct gfar_priv_rx_q *rx_queue = NULL;
206 	struct gfar_priv_tx_q *tx_queue = NULL;
207 
208 	if (NULL == phydev)
209 		return -ENODEV;
210 	tx_queue = priv->tx_queue[0];
211 	rx_queue = priv->rx_queue[0];
212 
213 	/* etsec-1.7 and older versions have only one txic
214 	 * and rxic regs although they support multiple queues */
215 	cmd->maxtxpkt = get_icft_value(tx_queue->txic);
216 	cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
217 
218 	return phy_ethtool_gset(phydev, cmd);
219 }
220 
221 /* Return the length of the register structure */
222 static int gfar_reglen(struct net_device *dev)
223 {
224 	return sizeof (struct gfar);
225 }
226 
227 /* Return a dump of the GFAR register space */
228 static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
229 {
230 	int i;
231 	struct gfar_private *priv = netdev_priv(dev);
232 	u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
233 	u32 *buf = (u32 *) regbuf;
234 
235 	for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
236 		buf[i] = gfar_read(&theregs[i]);
237 }
238 
239 /* Convert microseconds to ethernet clock ticks, which changes
240  * depending on what speed the controller is running at */
241 static unsigned int gfar_usecs2ticks(struct gfar_private *priv, unsigned int usecs)
242 {
243 	unsigned int count;
244 
245 	/* The timer is different, depending on the interface speed */
246 	switch (priv->phydev->speed) {
247 	case SPEED_1000:
248 		count = GFAR_GBIT_TIME;
249 		break;
250 	case SPEED_100:
251 		count = GFAR_100_TIME;
252 		break;
253 	case SPEED_10:
254 	default:
255 		count = GFAR_10_TIME;
256 		break;
257 	}
258 
259 	/* Make sure we return a number greater than 0
260 	 * if usecs > 0 */
261 	return (usecs * 1000 + count - 1) / count;
262 }
263 
264 /* Convert ethernet clock ticks to microseconds */
265 static unsigned int gfar_ticks2usecs(struct gfar_private *priv, unsigned int ticks)
266 {
267 	unsigned int count;
268 
269 	/* The timer is different, depending on the interface speed */
270 	switch (priv->phydev->speed) {
271 	case SPEED_1000:
272 		count = GFAR_GBIT_TIME;
273 		break;
274 	case SPEED_100:
275 		count = GFAR_100_TIME;
276 		break;
277 	case SPEED_10:
278 	default:
279 		count = GFAR_10_TIME;
280 		break;
281 	}
282 
283 	/* Make sure we return a number greater than 0 */
284 	/* if ticks is > 0 */
285 	return (ticks * count) / 1000;
286 }
287 
288 /* Get the coalescing parameters, and put them in the cvals
289  * structure.  */
290 static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
291 {
292 	struct gfar_private *priv = netdev_priv(dev);
293 	struct gfar_priv_rx_q *rx_queue = NULL;
294 	struct gfar_priv_tx_q *tx_queue = NULL;
295 	unsigned long rxtime;
296 	unsigned long rxcount;
297 	unsigned long txtime;
298 	unsigned long txcount;
299 
300 	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
301 		return -EOPNOTSUPP;
302 
303 	if (NULL == priv->phydev)
304 		return -ENODEV;
305 
306 	rx_queue = priv->rx_queue[0];
307 	tx_queue = priv->tx_queue[0];
308 
309 	rxtime  = get_ictt_value(rx_queue->rxic);
310 	rxcount = get_icft_value(rx_queue->rxic);
311 	txtime  = get_ictt_value(tx_queue->txic);
312 	txcount = get_icft_value(tx_queue->txic);
313 	cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
314 	cvals->rx_max_coalesced_frames = rxcount;
315 
316 	cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
317 	cvals->tx_max_coalesced_frames = txcount;
318 
319 	cvals->use_adaptive_rx_coalesce = 0;
320 	cvals->use_adaptive_tx_coalesce = 0;
321 
322 	cvals->pkt_rate_low = 0;
323 	cvals->rx_coalesce_usecs_low = 0;
324 	cvals->rx_max_coalesced_frames_low = 0;
325 	cvals->tx_coalesce_usecs_low = 0;
326 	cvals->tx_max_coalesced_frames_low = 0;
327 
328 	/* When the packet rate is below pkt_rate_high but above
329 	 * pkt_rate_low (both measured in packets per second) the
330 	 * normal {rx,tx}_* coalescing parameters are used.
331 	 */
332 
333 	/* When the packet rate is (measured in packets per second)
334 	 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
335 	 * used.
336 	 */
337 	cvals->pkt_rate_high = 0;
338 	cvals->rx_coalesce_usecs_high = 0;
339 	cvals->rx_max_coalesced_frames_high = 0;
340 	cvals->tx_coalesce_usecs_high = 0;
341 	cvals->tx_max_coalesced_frames_high = 0;
342 
343 	/* How often to do adaptive coalescing packet rate sampling,
344 	 * measured in seconds.  Must not be zero.
345 	 */
346 	cvals->rate_sample_interval = 0;
347 
348 	return 0;
349 }
350 
351 /* Change the coalescing values.
352  * Both cvals->*_usecs and cvals->*_frames have to be > 0
353  * in order for coalescing to be active
354  */
355 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
356 {
357 	struct gfar_private *priv = netdev_priv(dev);
358 	int i = 0;
359 
360 	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
361 		return -EOPNOTSUPP;
362 
363 	/* Set up rx coalescing */
364 	/* As of now, we will enable/disable coalescing for all
365 	 * queues together in case of eTSEC2, this will be modified
366 	 * along with the ethtool interface */
367 	if ((cvals->rx_coalesce_usecs == 0) ||
368 	    (cvals->rx_max_coalesced_frames == 0)) {
369 		for (i = 0; i < priv->num_rx_queues; i++)
370 			priv->rx_queue[i]->rxcoalescing = 0;
371 	} else {
372 		for (i = 0; i < priv->num_rx_queues; i++)
373 			priv->rx_queue[i]->rxcoalescing = 1;
374 	}
375 
376 	if (NULL == priv->phydev)
377 		return -ENODEV;
378 
379 	/* Check the bounds of the values */
380 	if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
381 		pr_info("Coalescing is limited to %d microseconds\n",
382 			GFAR_MAX_COAL_USECS);
383 		return -EINVAL;
384 	}
385 
386 	if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
387 		pr_info("Coalescing is limited to %d frames\n",
388 			GFAR_MAX_COAL_FRAMES);
389 		return -EINVAL;
390 	}
391 
392 	for (i = 0; i < priv->num_rx_queues; i++) {
393 		priv->rx_queue[i]->rxic = mk_ic_value(
394 			cvals->rx_max_coalesced_frames,
395 			gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
396 	}
397 
398 	/* Set up tx coalescing */
399 	if ((cvals->tx_coalesce_usecs == 0) ||
400 	    (cvals->tx_max_coalesced_frames == 0)) {
401 		for (i = 0; i < priv->num_tx_queues; i++)
402 			priv->tx_queue[i]->txcoalescing = 0;
403 	} else {
404 		for (i = 0; i < priv->num_tx_queues; i++)
405 			priv->tx_queue[i]->txcoalescing = 1;
406 	}
407 
408 	/* Check the bounds of the values */
409 	if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
410 		pr_info("Coalescing is limited to %d microseconds\n",
411 			GFAR_MAX_COAL_USECS);
412 		return -EINVAL;
413 	}
414 
415 	if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
416 		pr_info("Coalescing is limited to %d frames\n",
417 			GFAR_MAX_COAL_FRAMES);
418 		return -EINVAL;
419 	}
420 
421 	for (i = 0; i < priv->num_tx_queues; i++) {
422 		priv->tx_queue[i]->txic = mk_ic_value(
423 			cvals->tx_max_coalesced_frames,
424 			gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
425 	}
426 
427 	gfar_configure_coalescing(priv, 0xFF, 0xFF);
428 
429 	return 0;
430 }
431 
432 /* Fills in rvals with the current ring parameters.  Currently,
433  * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
434  * jumbo are ignored by the driver */
435 static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
436 {
437 	struct gfar_private *priv = netdev_priv(dev);
438 	struct gfar_priv_tx_q *tx_queue = NULL;
439 	struct gfar_priv_rx_q *rx_queue = NULL;
440 
441 	tx_queue = priv->tx_queue[0];
442 	rx_queue = priv->rx_queue[0];
443 
444 	rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
445 	rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
446 	rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
447 	rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
448 
449 	/* Values changeable by the user.  The valid values are
450 	 * in the range 1 to the "*_max_pending" counterpart above.
451 	 */
452 	rvals->rx_pending = rx_queue->rx_ring_size;
453 	rvals->rx_mini_pending = rx_queue->rx_ring_size;
454 	rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
455 	rvals->tx_pending = tx_queue->tx_ring_size;
456 }
457 
458 /* Change the current ring parameters, stopping the controller if
459  * necessary so that we don't mess things up while we're in
460  * motion.  We wait for the ring to be clean before reallocating
461  * the rings. */
462 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
463 {
464 	struct gfar_private *priv = netdev_priv(dev);
465 	int err = 0, i = 0;
466 
467 	if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
468 		return -EINVAL;
469 
470 	if (!is_power_of_2(rvals->rx_pending)) {
471 		netdev_err(dev, "Ring sizes must be a power of 2\n");
472 		return -EINVAL;
473 	}
474 
475 	if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
476 		return -EINVAL;
477 
478 	if (!is_power_of_2(rvals->tx_pending)) {
479 		netdev_err(dev, "Ring sizes must be a power of 2\n");
480 		return -EINVAL;
481 	}
482 
483 
484 	if (dev->flags & IFF_UP) {
485 		unsigned long flags;
486 
487 		/* Halt TX and RX, and process the frames which
488 		 * have already been received */
489 		local_irq_save(flags);
490 		lock_tx_qs(priv);
491 		lock_rx_qs(priv);
492 
493 		gfar_halt(dev);
494 
495 		unlock_rx_qs(priv);
496 		unlock_tx_qs(priv);
497 		local_irq_restore(flags);
498 
499 		for (i = 0; i < priv->num_rx_queues; i++)
500 			gfar_clean_rx_ring(priv->rx_queue[i],
501 					priv->rx_queue[i]->rx_ring_size);
502 
503 		/* Now we take down the rings to rebuild them */
504 		stop_gfar(dev);
505 	}
506 
507 	/* Change the size */
508 	for (i = 0; i < priv->num_rx_queues; i++) {
509 		priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
510 		priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
511 		priv->tx_queue[i]->num_txbdfree = priv->tx_queue[i]->tx_ring_size;
512 	}
513 
514 	/* Rebuild the rings with the new size */
515 	if (dev->flags & IFF_UP) {
516 		err = startup_gfar(dev);
517 		netif_tx_wake_all_queues(dev);
518 	}
519 	return err;
520 }
521 
522 int gfar_set_features(struct net_device *dev, netdev_features_t features)
523 {
524 	struct gfar_private *priv = netdev_priv(dev);
525 	unsigned long flags;
526 	int err = 0, i = 0;
527 	netdev_features_t changed = dev->features ^ features;
528 
529 	if (changed & (NETIF_F_HW_VLAN_TX|NETIF_F_HW_VLAN_RX))
530 		gfar_vlan_mode(dev, features);
531 
532 	if (!(changed & NETIF_F_RXCSUM))
533 		return 0;
534 
535 	if (dev->flags & IFF_UP) {
536 		/* Halt TX and RX, and process the frames which
537 		 * have already been received */
538 		local_irq_save(flags);
539 		lock_tx_qs(priv);
540 		lock_rx_qs(priv);
541 
542 		gfar_halt(dev);
543 
544 		unlock_tx_qs(priv);
545 		unlock_rx_qs(priv);
546 		local_irq_restore(flags);
547 
548 		for (i = 0; i < priv->num_rx_queues; i++)
549 			gfar_clean_rx_ring(priv->rx_queue[i],
550 					priv->rx_queue[i]->rx_ring_size);
551 
552 		/* Now we take down the rings to rebuild them */
553 		stop_gfar(dev);
554 
555 		dev->features = features;
556 
557 		err = startup_gfar(dev);
558 		netif_tx_wake_all_queues(dev);
559 	}
560 	return err;
561 }
562 
563 static uint32_t gfar_get_msglevel(struct net_device *dev)
564 {
565 	struct gfar_private *priv = netdev_priv(dev);
566 	return priv->msg_enable;
567 }
568 
569 static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
570 {
571 	struct gfar_private *priv = netdev_priv(dev);
572 	priv->msg_enable = data;
573 }
574 
575 #ifdef CONFIG_PM
576 static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
577 {
578 	struct gfar_private *priv = netdev_priv(dev);
579 
580 	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
581 		wol->supported = WAKE_MAGIC;
582 		wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
583 	} else {
584 		wol->supported = wol->wolopts = 0;
585 	}
586 }
587 
588 static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
589 {
590 	struct gfar_private *priv = netdev_priv(dev);
591 	unsigned long flags;
592 
593 	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
594 	    wol->wolopts != 0)
595 		return -EINVAL;
596 
597 	if (wol->wolopts & ~WAKE_MAGIC)
598 		return -EINVAL;
599 
600 	device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);
601 
602 	spin_lock_irqsave(&priv->bflock, flags);
603 	priv->wol_en =  !!device_may_wakeup(&dev->dev);
604 	spin_unlock_irqrestore(&priv->bflock, flags);
605 
606 	return 0;
607 }
608 #endif
609 
610 static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
611 {
612 	u32 fcr = 0x0, fpr = FPR_FILER_MASK;
613 
614 	if (ethflow & RXH_L2DA) {
615 		fcr = RQFCR_PID_DAH |RQFCR_CMP_NOMATCH |
616 			RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
617 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
618 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
619 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
620 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
621 
622 		fcr = RQFCR_PID_DAL | RQFCR_AND | RQFCR_CMP_NOMATCH |
623 				RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
624 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
625 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
626 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
627 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
628 	}
629 
630 	if (ethflow & RXH_VLAN) {
631 		fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
632 				RQFCR_AND | RQFCR_HASHTBL_0;
633 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
634 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
635 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
636 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
637 	}
638 
639 	if (ethflow & RXH_IP_SRC) {
640 		fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
641 			RQFCR_AND | RQFCR_HASHTBL_0;
642 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
643 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
644 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
645 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
646 	}
647 
648 	if (ethflow & (RXH_IP_DST)) {
649 		fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
650 			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 
657 	if (ethflow & RXH_L3_PROTO) {
658 		fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
659 			RQFCR_AND | RQFCR_HASHTBL_0;
660 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
661 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
662 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
663 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
664 	}
665 
666 	if (ethflow & RXH_L4_B_0_1) {
667 		fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
668 			RQFCR_AND | RQFCR_HASHTBL_0;
669 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
670 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
671 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
672 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
673 	}
674 
675 	if (ethflow & RXH_L4_B_2_3) {
676 		fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
677 			RQFCR_AND | RQFCR_HASHTBL_0;
678 		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
679 		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
680 		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
681 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
682 	}
683 }
684 
685 static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow, u64 class)
686 {
687 	unsigned int last_rule_idx = priv->cur_filer_idx;
688 	unsigned int cmp_rqfpr;
689 	unsigned int *local_rqfpr;
690 	unsigned int *local_rqfcr;
691 	int i = 0x0, k = 0x0;
692 	int j = MAX_FILER_IDX, l = 0x0;
693 	int ret = 1;
694 
695 	local_rqfpr = kmalloc(sizeof(unsigned int) * (MAX_FILER_IDX + 1),
696 		GFP_KERNEL);
697 	local_rqfcr = kmalloc(sizeof(unsigned int) * (MAX_FILER_IDX + 1),
698 		GFP_KERNEL);
699 	if (!local_rqfpr || !local_rqfcr) {
700 		pr_err("Out of memory\n");
701 		ret = 0;
702 		goto err;
703 	}
704 
705 	switch (class) {
706 	case TCP_V4_FLOW:
707 		cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
708 		break;
709 	case UDP_V4_FLOW:
710 		cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
711 		break;
712 	case TCP_V6_FLOW:
713 		cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
714 		break;
715 	case UDP_V6_FLOW:
716 		cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
717 		break;
718 	default:
719 		pr_err("Right now this class is not supported\n");
720 		ret = 0;
721 		goto err;
722 	}
723 
724 	for (i = 0; i < MAX_FILER_IDX + 1; i++) {
725 		local_rqfpr[j] = priv->ftp_rqfpr[i];
726 		local_rqfcr[j] = priv->ftp_rqfcr[i];
727 		j--;
728 		if ((priv->ftp_rqfcr[i] == (RQFCR_PID_PARSE |
729 			RQFCR_CLE |RQFCR_AND)) &&
730 			(priv->ftp_rqfpr[i] == cmp_rqfpr))
731 			break;
732 	}
733 
734 	if (i == MAX_FILER_IDX + 1) {
735 		pr_err("No parse rule found, can't create hash rules\n");
736 		ret = 0;
737 		goto err;
738 	}
739 
740 	/* If a match was found, then it begins the starting of a cluster rule
741 	 * if it was already programmed, we need to overwrite these rules
742 	 */
743 	for (l = i+1; l < MAX_FILER_IDX; l++) {
744 		if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
745 			!(priv->ftp_rqfcr[l] & RQFCR_AND)) {
746 			priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
747 				RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
748 			priv->ftp_rqfpr[l] = FPR_FILER_MASK;
749 			gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
750 				priv->ftp_rqfpr[l]);
751 			break;
752 		}
753 
754 		if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
755 			(priv->ftp_rqfcr[l] & RQFCR_AND))
756 			continue;
757 		else {
758 			local_rqfpr[j] = priv->ftp_rqfpr[l];
759 			local_rqfcr[j] = priv->ftp_rqfcr[l];
760 			j--;
761 		}
762 	}
763 
764 	priv->cur_filer_idx = l - 1;
765 	last_rule_idx = l;
766 
767 	/* hash rules */
768 	ethflow_to_filer_rules(priv, ethflow);
769 
770 	/* Write back the popped out rules again */
771 	for (k = j+1; k < MAX_FILER_IDX; k++) {
772 		priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
773 		priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
774 		gfar_write_filer(priv, priv->cur_filer_idx,
775 				local_rqfcr[k], local_rqfpr[k]);
776 		if (!priv->cur_filer_idx)
777 			break;
778 		priv->cur_filer_idx = priv->cur_filer_idx - 1;
779 	}
780 
781 err:
782 	kfree(local_rqfcr);
783 	kfree(local_rqfpr);
784 	return ret;
785 }
786 
787 static int gfar_set_hash_opts(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
788 {
789 	/* write the filer rules here */
790 	if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
791 		return -EINVAL;
792 
793 	return 0;
794 }
795 
796 static int gfar_check_filer_hardware(struct gfar_private *priv)
797 {
798 	struct gfar __iomem *regs = NULL;
799 	u32 i;
800 
801 	regs = priv->gfargrp[0].regs;
802 
803 	/* Check if we are in FIFO mode */
804 	i = gfar_read(&regs->ecntrl);
805 	i &= ECNTRL_FIFM;
806 	if (i == ECNTRL_FIFM) {
807 		netdev_notice(priv->ndev, "Interface in FIFO mode\n");
808 		i = gfar_read(&regs->rctrl);
809 		i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
810 		if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
811 			netdev_info(priv->ndev,
812 					"Receive Queue Filtering enabled\n");
813 		} else {
814 			netdev_warn(priv->ndev,
815 					"Receive Queue Filtering disabled\n");
816 			return -EOPNOTSUPP;
817 		}
818 	}
819 	/* Or in standard mode */
820 	else {
821 		i = gfar_read(&regs->rctrl);
822 		i &= RCTRL_PRSDEP_MASK;
823 		if (i == RCTRL_PRSDEP_MASK) {
824 			netdev_info(priv->ndev,
825 					"Receive Queue Filtering enabled\n");
826 		} else {
827 			netdev_warn(priv->ndev,
828 					"Receive Queue Filtering disabled\n");
829 			return -EOPNOTSUPP;
830 		}
831 	}
832 
833 	/* Sets the properties for arbitrary filer rule
834 	 * to the first 4 Layer 4 Bytes */
835 	regs->rbifx = 0xC0C1C2C3;
836 	return 0;
837 }
838 
839 static int gfar_comp_asc(const void *a, const void *b)
840 {
841 	return memcmp(a, b, 4);
842 }
843 
844 static int gfar_comp_desc(const void *a, const void *b)
845 {
846 	return -memcmp(a, b, 4);
847 }
848 
849 static void gfar_swap(void *a, void *b, int size)
850 {
851 	u32 *_a = a;
852 	u32 *_b = b;
853 
854 	swap(_a[0], _b[0]);
855 	swap(_a[1], _b[1]);
856 	swap(_a[2], _b[2]);
857 	swap(_a[3], _b[3]);
858 }
859 
860 /* Write a mask to filer cache */
861 static void gfar_set_mask(u32 mask, struct filer_table *tab)
862 {
863 	tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
864 	tab->fe[tab->index].prop = mask;
865 	tab->index++;
866 }
867 
868 /* Sets parse bits (e.g. IP or TCP) */
869 static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
870 {
871 	gfar_set_mask(mask, tab);
872 	tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE
873 			| RQFCR_AND;
874 	tab->fe[tab->index].prop = value;
875 	tab->index++;
876 }
877 
878 static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
879 		struct filer_table *tab)
880 {
881 	gfar_set_mask(mask, tab);
882 	tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
883 	tab->fe[tab->index].prop = value;
884 	tab->index++;
885 }
886 
887 /*
888  * For setting a tuple of value and mask of type flag
889  * Example:
890  * IP-Src = 10.0.0.0/255.0.0.0
891  * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
892  *
893  * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
894  * For a don't care mask it gives us a 0
895  *
896  * The check if don't care and the mask adjustment if mask=0 is done for VLAN
897  * and MAC stuff on an upper level (due to missing information on this level).
898  * For these guys we can discard them if they are value=0 and mask=0.
899  *
900  * Further the all masks are one-padded for better hardware efficiency.
901  */
902 static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
903 		struct filer_table *tab)
904 {
905 	switch (flag) {
906 		/* 3bit */
907 	case RQFCR_PID_PRI:
908 		if (!(value | mask))
909 			return;
910 		mask |= RQFCR_PID_PRI_MASK;
911 		break;
912 		/* 8bit */
913 	case RQFCR_PID_L4P:
914 	case RQFCR_PID_TOS:
915 		if (!~(mask | RQFCR_PID_L4P_MASK))
916 			return;
917 		if (!mask)
918 			mask = ~0;
919 		else
920 			mask |= RQFCR_PID_L4P_MASK;
921 		break;
922 		/* 12bit */
923 	case RQFCR_PID_VID:
924 		if (!(value | mask))
925 			return;
926 		mask |= RQFCR_PID_VID_MASK;
927 		break;
928 		/* 16bit */
929 	case RQFCR_PID_DPT:
930 	case RQFCR_PID_SPT:
931 	case RQFCR_PID_ETY:
932 		if (!~(mask | RQFCR_PID_PORT_MASK))
933 			return;
934 		if (!mask)
935 			mask = ~0;
936 		else
937 			mask |= RQFCR_PID_PORT_MASK;
938 		break;
939 		/* 24bit */
940 	case RQFCR_PID_DAH:
941 	case RQFCR_PID_DAL:
942 	case RQFCR_PID_SAH:
943 	case RQFCR_PID_SAL:
944 		if (!(value | mask))
945 			return;
946 		mask |= RQFCR_PID_MAC_MASK;
947 		break;
948 		/* for all real 32bit masks */
949 	default:
950 		if (!~mask)
951 			return;
952 		if (!mask)
953 			mask = ~0;
954 		break;
955 	}
956 	gfar_set_general_attribute(value, mask, flag, tab);
957 }
958 
959 /* Translates value and mask for UDP, TCP or SCTP */
960 static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
961 		struct ethtool_tcpip4_spec *mask, struct filer_table *tab)
962 {
963 	gfar_set_attribute(value->ip4src, mask->ip4src, RQFCR_PID_SIA, tab);
964 	gfar_set_attribute(value->ip4dst, mask->ip4dst, RQFCR_PID_DIA, tab);
965 	gfar_set_attribute(value->pdst, mask->pdst, RQFCR_PID_DPT, tab);
966 	gfar_set_attribute(value->psrc, mask->psrc, RQFCR_PID_SPT, tab);
967 	gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
968 }
969 
970 /* Translates value and mask for RAW-IP4 */
971 static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
972 		struct ethtool_usrip4_spec *mask, struct filer_table *tab)
973 {
974 	gfar_set_attribute(value->ip4src, mask->ip4src, RQFCR_PID_SIA, tab);
975 	gfar_set_attribute(value->ip4dst, mask->ip4dst, RQFCR_PID_DIA, tab);
976 	gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
977 	gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
978 	gfar_set_attribute(value->l4_4_bytes, mask->l4_4_bytes, RQFCR_PID_ARB,
979 			tab);
980 
981 }
982 
983 /* Translates value and mask for ETHER spec */
984 static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
985 		struct filer_table *tab)
986 {
987 	u32 upper_temp_mask = 0;
988 	u32 lower_temp_mask = 0;
989 	/* Source address */
990 	if (!is_broadcast_ether_addr(mask->h_source)) {
991 
992 		if (is_zero_ether_addr(mask->h_source)) {
993 			upper_temp_mask = 0xFFFFFFFF;
994 			lower_temp_mask = 0xFFFFFFFF;
995 		} else {
996 			upper_temp_mask = mask->h_source[0] << 16
997 					| mask->h_source[1] << 8
998 					| mask->h_source[2];
999 			lower_temp_mask = mask->h_source[3] << 16
1000 					| mask->h_source[4] << 8
1001 					| mask->h_source[5];
1002 		}
1003 		/* Upper 24bit */
1004 		gfar_set_attribute(
1005 				value->h_source[0] << 16 | value->h_source[1]
1006 						<< 8 | value->h_source[2],
1007 				upper_temp_mask, RQFCR_PID_SAH, tab);
1008 		/* And the same for the lower part */
1009 		gfar_set_attribute(
1010 				value->h_source[3] << 16 | value->h_source[4]
1011 						<< 8 | value->h_source[5],
1012 				lower_temp_mask, RQFCR_PID_SAL, tab);
1013 	}
1014 	/* Destination address */
1015 	if (!is_broadcast_ether_addr(mask->h_dest)) {
1016 
1017 		/* Special for destination is limited broadcast */
1018 		if ((is_broadcast_ether_addr(value->h_dest)
1019 				&& is_zero_ether_addr(mask->h_dest))) {
1020 			gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
1021 		} else {
1022 
1023 			if (is_zero_ether_addr(mask->h_dest)) {
1024 				upper_temp_mask = 0xFFFFFFFF;
1025 				lower_temp_mask = 0xFFFFFFFF;
1026 			} else {
1027 				upper_temp_mask = mask->h_dest[0] << 16
1028 						| mask->h_dest[1] << 8
1029 						| mask->h_dest[2];
1030 				lower_temp_mask = mask->h_dest[3] << 16
1031 						| mask->h_dest[4] << 8
1032 						| mask->h_dest[5];
1033 			}
1034 
1035 			/* Upper 24bit */
1036 			gfar_set_attribute(
1037 					value->h_dest[0] << 16
1038 							| value->h_dest[1] << 8
1039 							| value->h_dest[2],
1040 					upper_temp_mask, RQFCR_PID_DAH, tab);
1041 			/* And the same for the lower part */
1042 			gfar_set_attribute(
1043 					value->h_dest[3] << 16
1044 							| value->h_dest[4] << 8
1045 							| value->h_dest[5],
1046 					lower_temp_mask, RQFCR_PID_DAL, tab);
1047 		}
1048 	}
1049 
1050 	gfar_set_attribute(value->h_proto, mask->h_proto, RQFCR_PID_ETY, tab);
1051 
1052 }
1053 
1054 /* Convert a rule to binary filter format of gianfar */
1055 static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
1056 		struct filer_table *tab)
1057 {
1058 	u32 vlan = 0, vlan_mask = 0;
1059 	u32 id = 0, id_mask = 0;
1060 	u32 cfi = 0, cfi_mask = 0;
1061 	u32 prio = 0, prio_mask = 0;
1062 
1063 	u32 old_index = tab->index;
1064 
1065 	/* Check if vlan is wanted */
1066 	if ((rule->flow_type & FLOW_EXT) && (rule->m_ext.vlan_tci != 0xFFFF)) {
1067 		if (!rule->m_ext.vlan_tci)
1068 			rule->m_ext.vlan_tci = 0xFFFF;
1069 
1070 		vlan = RQFPR_VLN;
1071 		vlan_mask = RQFPR_VLN;
1072 
1073 		/* Separate the fields */
1074 		id = rule->h_ext.vlan_tci & VLAN_VID_MASK;
1075 		id_mask = rule->m_ext.vlan_tci & VLAN_VID_MASK;
1076 		cfi = rule->h_ext.vlan_tci & VLAN_CFI_MASK;
1077 		cfi_mask = rule->m_ext.vlan_tci & VLAN_CFI_MASK;
1078 		prio = (rule->h_ext.vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1079 		prio_mask = (rule->m_ext.vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1080 
1081 		if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
1082 			vlan |= RQFPR_CFI;
1083 			vlan_mask |= RQFPR_CFI;
1084 		} else if (cfi != VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
1085 			vlan_mask |= RQFPR_CFI;
1086 		}
1087 	}
1088 
1089 	switch (rule->flow_type & ~FLOW_EXT) {
1090 	case TCP_V4_FLOW:
1091 		gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
1092 				RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
1093 		gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
1094 				&rule->m_u.tcp_ip4_spec, tab);
1095 		break;
1096 	case UDP_V4_FLOW:
1097 		gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
1098 				RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
1099 		gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
1100 				&rule->m_u.udp_ip4_spec, tab);
1101 		break;
1102 	case SCTP_V4_FLOW:
1103 		gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1104 				tab);
1105 		gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
1106 		gfar_set_basic_ip((struct ethtool_tcpip4_spec *) &rule->h_u,
1107 				(struct ethtool_tcpip4_spec *) &rule->m_u, tab);
1108 		break;
1109 	case IP_USER_FLOW:
1110 		gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1111 				tab);
1112 		gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
1113 				(struct ethtool_usrip4_spec *) &rule->m_u, tab);
1114 		break;
1115 	case ETHER_FLOW:
1116 		if (vlan)
1117 			gfar_set_parse_bits(vlan, vlan_mask, tab);
1118 		gfar_set_ether((struct ethhdr *) &rule->h_u,
1119 				(struct ethhdr *) &rule->m_u, tab);
1120 		break;
1121 	default:
1122 		return -1;
1123 	}
1124 
1125 	/* Set the vlan attributes in the end */
1126 	if (vlan) {
1127 		gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
1128 		gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
1129 	}
1130 
1131 	/* If there has been nothing written till now, it must be a default */
1132 	if (tab->index == old_index) {
1133 		gfar_set_mask(0xFFFFFFFF, tab);
1134 		tab->fe[tab->index].ctrl = 0x20;
1135 		tab->fe[tab->index].prop = 0x0;
1136 		tab->index++;
1137 	}
1138 
1139 	/* Remove last AND */
1140 	tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
1141 
1142 	/* Specify which queue to use or to drop */
1143 	if (rule->ring_cookie == RX_CLS_FLOW_DISC)
1144 		tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
1145 	else
1146 		tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
1147 
1148 	/* Only big enough entries can be clustered */
1149 	if (tab->index > (old_index + 2)) {
1150 		tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
1151 		tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
1152 	}
1153 
1154 	/* In rare cases the cache can be full while there is free space in hw */
1155 	if (tab->index > MAX_FILER_CACHE_IDX - 1)
1156 		return -EBUSY;
1157 
1158 	return 0;
1159 }
1160 
1161 /* Copy size filer entries */
1162 static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
1163 		struct gfar_filer_entry src[0], s32 size)
1164 {
1165 	while (size > 0) {
1166 		size--;
1167 		dst[size].ctrl = src[size].ctrl;
1168 		dst[size].prop = src[size].prop;
1169 	}
1170 }
1171 
1172 /* Delete the contents of the filer-table between start and end
1173  * and collapse them */
1174 static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
1175 {
1176 	int length;
1177 	if (end > MAX_FILER_CACHE_IDX || end < begin)
1178 		return -EINVAL;
1179 
1180 	end++;
1181 	length = end - begin;
1182 
1183 	/* Copy */
1184 	while (end < tab->index) {
1185 		tab->fe[begin].ctrl = tab->fe[end].ctrl;
1186 		tab->fe[begin++].prop = tab->fe[end++].prop;
1187 
1188 	}
1189 	/* Fill up with don't cares */
1190 	while (begin < tab->index) {
1191 		tab->fe[begin].ctrl = 0x60;
1192 		tab->fe[begin].prop = 0xFFFFFFFF;
1193 		begin++;
1194 	}
1195 
1196 	tab->index -= length;
1197 	return 0;
1198 }
1199 
1200 /* Make space on the wanted location */
1201 static int gfar_expand_filer_entries(u32 begin, u32 length,
1202 		struct filer_table *tab)
1203 {
1204 	if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX || begin
1205 			> MAX_FILER_CACHE_IDX)
1206 		return -EINVAL;
1207 
1208 	gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
1209 			tab->index - length + 1);
1210 
1211 	tab->index += length;
1212 	return 0;
1213 }
1214 
1215 static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
1216 {
1217 	for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1); start++) {
1218 		if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE))
1219 				== (RQFCR_AND | RQFCR_CLE))
1220 			return start;
1221 	}
1222 	return -1;
1223 }
1224 
1225 static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
1226 {
1227 	for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1); start++) {
1228 		if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE))
1229 				== (RQFCR_CLE))
1230 			return start;
1231 	}
1232 	return -1;
1233 }
1234 
1235 /*
1236  * Uses hardwares clustering option to reduce
1237  * the number of filer table entries
1238  */
1239 static void gfar_cluster_filer(struct filer_table *tab)
1240 {
1241 	s32 i = -1, j, iend, jend;
1242 
1243 	while ((i = gfar_get_next_cluster_start(++i, tab)) != -1) {
1244 		j = i;
1245 		while ((j = gfar_get_next_cluster_start(++j, tab)) != -1) {
1246 			/*
1247 			 * The cluster entries self and the previous one
1248 			 * (a mask) must be identical!
1249 			 */
1250 			if (tab->fe[i].ctrl != tab->fe[j].ctrl)
1251 				break;
1252 			if (tab->fe[i].prop != tab->fe[j].prop)
1253 				break;
1254 			if (tab->fe[i - 1].ctrl != tab->fe[j - 1].ctrl)
1255 				break;
1256 			if (tab->fe[i - 1].prop != tab->fe[j - 1].prop)
1257 				break;
1258 			iend = gfar_get_next_cluster_end(i, tab);
1259 			jend = gfar_get_next_cluster_end(j, tab);
1260 			if (jend == -1 || iend == -1)
1261 				break;
1262 			/*
1263 			 * First we make some free space, where our cluster
1264 			 * element should be. Then we copy it there and finally
1265 			 * delete in from its old location.
1266 			 */
1267 
1268 			if (gfar_expand_filer_entries(iend, (jend - j), tab)
1269 					== -EINVAL)
1270 				break;
1271 
1272 			gfar_copy_filer_entries(&(tab->fe[iend + 1]),
1273 					&(tab->fe[jend + 1]), jend - j);
1274 
1275 			if (gfar_trim_filer_entries(jend - 1,
1276 					jend + (jend - j), tab) == -EINVAL)
1277 				return;
1278 
1279 			/* Mask out cluster bit */
1280 			tab->fe[iend].ctrl &= ~(RQFCR_CLE);
1281 		}
1282 	}
1283 }
1284 
1285 /* Swaps the masked bits of a1<>a2 and b1<>b2 */
1286 static void gfar_swap_bits(struct gfar_filer_entry *a1,
1287 		struct gfar_filer_entry *a2, struct gfar_filer_entry *b1,
1288 		struct gfar_filer_entry *b2, u32 mask)
1289 {
1290 	u32 temp[4];
1291 	temp[0] = a1->ctrl & mask;
1292 	temp[1] = a2->ctrl & mask;
1293 	temp[2] = b1->ctrl & mask;
1294 	temp[3] = b2->ctrl & mask;
1295 
1296 	a1->ctrl &= ~mask;
1297 	a2->ctrl &= ~mask;
1298 	b1->ctrl &= ~mask;
1299 	b2->ctrl &= ~mask;
1300 
1301 	a1->ctrl |= temp[1];
1302 	a2->ctrl |= temp[0];
1303 	b1->ctrl |= temp[3];
1304 	b2->ctrl |= temp[2];
1305 }
1306 
1307 /*
1308  * Generate a list consisting of masks values with their start and
1309  * end of validity and block as indicator for parts belonging
1310  * together (glued by ANDs) in mask_table
1311  */
1312 static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
1313 		struct filer_table *tab)
1314 {
1315 	u32 i, and_index = 0, block_index = 1;
1316 
1317 	for (i = 0; i < tab->index; i++) {
1318 
1319 		/* LSByte of control = 0 sets a mask */
1320 		if (!(tab->fe[i].ctrl & 0xF)) {
1321 			mask_table[and_index].mask = tab->fe[i].prop;
1322 			mask_table[and_index].start = i;
1323 			mask_table[and_index].block = block_index;
1324 			if (and_index >= 1)
1325 				mask_table[and_index - 1].end = i - 1;
1326 			and_index++;
1327 		}
1328 		/* cluster starts and ends will be separated because they should
1329 		 * hold their position */
1330 		if (tab->fe[i].ctrl & RQFCR_CLE)
1331 			block_index++;
1332 		/* A not set AND indicates the end of a depended block */
1333 		if (!(tab->fe[i].ctrl & RQFCR_AND))
1334 			block_index++;
1335 
1336 	}
1337 
1338 	mask_table[and_index - 1].end = i - 1;
1339 
1340 	return and_index;
1341 }
1342 
1343 /*
1344  * Sorts the entries of mask_table by the values of the masks.
1345  * Important: The 0xFF80 flags of the first and last entry of a
1346  * block must hold their position (which queue, CLusterEnable, ReJEct,
1347  * AND)
1348  */
1349 static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
1350 		struct filer_table *temp_table, u32 and_index)
1351 {
1352 	/* Pointer to compare function (_asc or _desc) */
1353 	int (*gfar_comp)(const void *, const void *);
1354 
1355 	u32 i, size = 0, start = 0, prev = 1;
1356 	u32 old_first, old_last, new_first, new_last;
1357 
1358 	gfar_comp = &gfar_comp_desc;
1359 
1360 	for (i = 0; i < and_index; i++) {
1361 
1362 		if (prev != mask_table[i].block) {
1363 			old_first = mask_table[start].start + 1;
1364 			old_last = mask_table[i - 1].end;
1365 			sort(mask_table + start, size,
1366 					sizeof(struct gfar_mask_entry),
1367 					gfar_comp, &gfar_swap);
1368 
1369 			/* Toggle order for every block. This makes the
1370 			 * thing more efficient! */
1371 			if (gfar_comp == gfar_comp_desc)
1372 				gfar_comp = &gfar_comp_asc;
1373 			else
1374 				gfar_comp = &gfar_comp_desc;
1375 
1376 			new_first = mask_table[start].start + 1;
1377 			new_last = mask_table[i - 1].end;
1378 
1379 			gfar_swap_bits(&temp_table->fe[new_first],
1380 					&temp_table->fe[old_first],
1381 					&temp_table->fe[new_last],
1382 					&temp_table->fe[old_last],
1383 					RQFCR_QUEUE | RQFCR_CLE |
1384 						RQFCR_RJE | RQFCR_AND
1385 					);
1386 
1387 			start = i;
1388 			size = 0;
1389 		}
1390 		size++;
1391 		prev = mask_table[i].block;
1392 	}
1393 
1394 }
1395 
1396 /*
1397  * Reduces the number of masks needed in the filer table to save entries
1398  * This is done by sorting the masks of a depended block. A depended block is
1399  * identified by gluing ANDs or CLE. The sorting order toggles after every
1400  * block. Of course entries in scope of a mask must change their location with
1401  * it.
1402  */
1403 static int gfar_optimize_filer_masks(struct filer_table *tab)
1404 {
1405 	struct filer_table *temp_table;
1406 	struct gfar_mask_entry *mask_table;
1407 
1408 	u32 and_index = 0, previous_mask = 0, i = 0, j = 0, size = 0;
1409 	s32 ret = 0;
1410 
1411 	/* We need a copy of the filer table because
1412 	 * we want to change its order */
1413 	temp_table = kmemdup(tab, sizeof(*temp_table), GFP_KERNEL);
1414 	if (temp_table == NULL)
1415 		return -ENOMEM;
1416 
1417 	mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
1418 			sizeof(struct gfar_mask_entry), GFP_KERNEL);
1419 
1420 	if (mask_table == NULL) {
1421 		ret = -ENOMEM;
1422 		goto end;
1423 	}
1424 
1425 	and_index = gfar_generate_mask_table(mask_table, tab);
1426 
1427 	gfar_sort_mask_table(mask_table, temp_table, and_index);
1428 
1429 	/* Now we can copy the data from our duplicated filer table to
1430 	 * the real one in the order the mask table says */
1431 	for (i = 0; i < and_index; i++) {
1432 		size = mask_table[i].end - mask_table[i].start + 1;
1433 		gfar_copy_filer_entries(&(tab->fe[j]),
1434 				&(temp_table->fe[mask_table[i].start]), size);
1435 		j += size;
1436 	}
1437 
1438 	/* And finally we just have to check for duplicated masks and drop the
1439 	 * second ones */
1440 	for (i = 0; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1441 		if (tab->fe[i].ctrl == 0x80) {
1442 			previous_mask = i++;
1443 			break;
1444 		}
1445 	}
1446 	for (; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1447 		if (tab->fe[i].ctrl == 0x80) {
1448 			if (tab->fe[i].prop == tab->fe[previous_mask].prop) {
1449 				/* Two identical ones found!
1450 				 * So drop the second one! */
1451 				gfar_trim_filer_entries(i, i, tab);
1452 			} else
1453 				/* Not identical! */
1454 				previous_mask = i;
1455 		}
1456 	}
1457 
1458 	kfree(mask_table);
1459 end:	kfree(temp_table);
1460 	return ret;
1461 }
1462 
1463 /* Write the bit-pattern from software's buffer to hardware registers */
1464 static int gfar_write_filer_table(struct gfar_private *priv,
1465 		struct filer_table *tab)
1466 {
1467 	u32 i = 0;
1468 	if (tab->index > MAX_FILER_IDX - 1)
1469 		return -EBUSY;
1470 
1471 	/* Avoid inconsistent filer table to be processed */
1472 	lock_rx_qs(priv);
1473 
1474 	/* Fill regular entries */
1475 	for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].ctrl); i++)
1476 		gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
1477 	/* Fill the rest with fall-troughs */
1478 	for (; i < MAX_FILER_IDX - 1; i++)
1479 		gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
1480 	/* Last entry must be default accept
1481 	 * because that's what people expect */
1482 	gfar_write_filer(priv, i, 0x20, 0x0);
1483 
1484 	unlock_rx_qs(priv);
1485 
1486 	return 0;
1487 }
1488 
1489 static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
1490 		struct gfar_private *priv)
1491 {
1492 
1493 	if (flow->flow_type & FLOW_EXT)	{
1494 		if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
1495 			netdev_warn(priv->ndev,
1496 					"User-specific data not supported!\n");
1497 		if (~flow->m_ext.vlan_etype)
1498 			netdev_warn(priv->ndev,
1499 					"VLAN-etype not supported!\n");
1500 	}
1501 	if (flow->flow_type == IP_USER_FLOW)
1502 		if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
1503 			netdev_warn(priv->ndev,
1504 					"IP-Version differing from IPv4 not supported!\n");
1505 
1506 	return 0;
1507 }
1508 
1509 static int gfar_process_filer_changes(struct gfar_private *priv)
1510 {
1511 	struct ethtool_flow_spec_container *j;
1512 	struct filer_table *tab;
1513 	s32 i = 0;
1514 	s32 ret = 0;
1515 
1516 	/* So index is set to zero, too! */
1517 	tab = kzalloc(sizeof(*tab), GFP_KERNEL);
1518 	if (tab == NULL)
1519 		return -ENOMEM;
1520 
1521 	/* Now convert the existing filer data from flow_spec into
1522 	 * filer tables binary format */
1523 	list_for_each_entry(j, &priv->rx_list.list, list) {
1524 		ret = gfar_convert_to_filer(&j->fs, tab);
1525 		if (ret == -EBUSY) {
1526 			netdev_err(priv->ndev, "Rule not added: No free space!\n");
1527 			goto end;
1528 		}
1529 		if (ret == -1) {
1530 			netdev_err(priv->ndev, "Rule not added: Unsupported Flow-type!\n");
1531 			goto end;
1532 		}
1533 	}
1534 
1535 	i = tab->index;
1536 
1537 	/* Optimizations to save entries */
1538 	gfar_cluster_filer(tab);
1539 	gfar_optimize_filer_masks(tab);
1540 
1541 	pr_debug("\n\tSummary:\n"
1542 		"\tData on hardware: %d\n"
1543 		"\tCompression rate: %d%%\n",
1544 		tab->index, 100 - (100 * tab->index) / i);
1545 
1546 	/* Write everything to hardware */
1547 	ret = gfar_write_filer_table(priv, tab);
1548 	if (ret == -EBUSY) {
1549 		netdev_err(priv->ndev, "Rule not added: No free space!\n");
1550 		goto end;
1551 	}
1552 
1553 end:	kfree(tab);
1554 	return ret;
1555 }
1556 
1557 static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
1558 {
1559 	u32 i = 0;
1560 
1561 	for (i = 0; i < sizeof(flow->m_u); i++)
1562 		flow->m_u.hdata[i] ^= 0xFF;
1563 
1564 	flow->m_ext.vlan_etype ^= 0xFFFF;
1565 	flow->m_ext.vlan_tci ^= 0xFFFF;
1566 	flow->m_ext.data[0] ^= ~0;
1567 	flow->m_ext.data[1] ^= ~0;
1568 }
1569 
1570 static int gfar_add_cls(struct gfar_private *priv,
1571 		struct ethtool_rx_flow_spec *flow)
1572 {
1573 	struct ethtool_flow_spec_container *temp, *comp;
1574 	int ret = 0;
1575 
1576 	temp = kmalloc(sizeof(*temp), GFP_KERNEL);
1577 	if (temp == NULL)
1578 		return -ENOMEM;
1579 	memcpy(&temp->fs, flow, sizeof(temp->fs));
1580 
1581 	gfar_invert_masks(&temp->fs);
1582 	ret = gfar_check_capability(&temp->fs, priv);
1583 	if (ret)
1584 		goto clean_mem;
1585 	/* Link in the new element at the right @location */
1586 	if (list_empty(&priv->rx_list.list)) {
1587 		ret = gfar_check_filer_hardware(priv);
1588 		if (ret != 0)
1589 			goto clean_mem;
1590 		list_add(&temp->list, &priv->rx_list.list);
1591 		goto process;
1592 	} else {
1593 
1594 		list_for_each_entry(comp, &priv->rx_list.list, list) {
1595 			if (comp->fs.location > flow->location) {
1596 				list_add_tail(&temp->list, &comp->list);
1597 				goto process;
1598 			}
1599 			if (comp->fs.location == flow->location) {
1600 				netdev_err(priv->ndev,
1601 						"Rule not added: ID %d not free!\n",
1602 					flow->location);
1603 				ret = -EBUSY;
1604 				goto clean_mem;
1605 			}
1606 		}
1607 		list_add_tail(&temp->list, &priv->rx_list.list);
1608 	}
1609 
1610 process:
1611 	ret = gfar_process_filer_changes(priv);
1612 	if (ret)
1613 		goto clean_list;
1614 	priv->rx_list.count++;
1615 	return ret;
1616 
1617 clean_list:
1618 	list_del(&temp->list);
1619 clean_mem:
1620 	kfree(temp);
1621 	return ret;
1622 }
1623 
1624 static int gfar_del_cls(struct gfar_private *priv, u32 loc)
1625 {
1626 	struct ethtool_flow_spec_container *comp;
1627 	u32 ret = -EINVAL;
1628 
1629 	if (list_empty(&priv->rx_list.list))
1630 		return ret;
1631 
1632 	list_for_each_entry(comp, &priv->rx_list.list, list) {
1633 		if (comp->fs.location == loc) {
1634 			list_del(&comp->list);
1635 			kfree(comp);
1636 			priv->rx_list.count--;
1637 			gfar_process_filer_changes(priv);
1638 			ret = 0;
1639 			break;
1640 		}
1641 	}
1642 
1643 	return ret;
1644 
1645 }
1646 
1647 static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
1648 {
1649 	struct ethtool_flow_spec_container *comp;
1650 	u32 ret = -EINVAL;
1651 
1652 	list_for_each_entry(comp, &priv->rx_list.list, list) {
1653 		if (comp->fs.location == cmd->fs.location) {
1654 			memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
1655 			gfar_invert_masks(&cmd->fs);
1656 			ret = 0;
1657 			break;
1658 		}
1659 	}
1660 
1661 	return ret;
1662 }
1663 
1664 static int gfar_get_cls_all(struct gfar_private *priv,
1665 		struct ethtool_rxnfc *cmd, u32 *rule_locs)
1666 {
1667 	struct ethtool_flow_spec_container *comp;
1668 	u32 i = 0;
1669 
1670 	list_for_each_entry(comp, &priv->rx_list.list, list) {
1671 		if (i == cmd->rule_cnt)
1672 			return -EMSGSIZE;
1673 		rule_locs[i] = comp->fs.location;
1674 		i++;
1675 	}
1676 
1677 	cmd->data = MAX_FILER_IDX;
1678 	cmd->rule_cnt = i;
1679 
1680 	return 0;
1681 }
1682 
1683 static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1684 {
1685 	struct gfar_private *priv = netdev_priv(dev);
1686 	int ret = 0;
1687 
1688 	mutex_lock(&priv->rx_queue_access);
1689 
1690 	switch (cmd->cmd) {
1691 	case ETHTOOL_SRXFH:
1692 		ret = gfar_set_hash_opts(priv, cmd);
1693 		break;
1694 	case ETHTOOL_SRXCLSRLINS:
1695 		if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
1696 		     cmd->fs.ring_cookie >= priv->num_rx_queues) ||
1697 		    cmd->fs.location >= MAX_FILER_IDX) {
1698 			ret = -EINVAL;
1699 			break;
1700 		}
1701 		ret = gfar_add_cls(priv, &cmd->fs);
1702 		break;
1703 	case ETHTOOL_SRXCLSRLDEL:
1704 		ret = gfar_del_cls(priv, cmd->fs.location);
1705 		break;
1706 	default:
1707 		ret = -EINVAL;
1708 	}
1709 
1710 	mutex_unlock(&priv->rx_queue_access);
1711 
1712 	return ret;
1713 }
1714 
1715 static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1716 		u32 *rule_locs)
1717 {
1718 	struct gfar_private *priv = netdev_priv(dev);
1719 	int ret = 0;
1720 
1721 	switch (cmd->cmd) {
1722 	case ETHTOOL_GRXRINGS:
1723 		cmd->data = priv->num_rx_queues;
1724 		break;
1725 	case ETHTOOL_GRXCLSRLCNT:
1726 		cmd->rule_cnt = priv->rx_list.count;
1727 		break;
1728 	case ETHTOOL_GRXCLSRULE:
1729 		ret = gfar_get_cls(priv, cmd);
1730 		break;
1731 	case ETHTOOL_GRXCLSRLALL:
1732 		ret = gfar_get_cls_all(priv, cmd, rule_locs);
1733 		break;
1734 	default:
1735 		ret = -EINVAL;
1736 		break;
1737 	}
1738 
1739 	return ret;
1740 }
1741 
1742 const struct ethtool_ops gfar_ethtool_ops = {
1743 	.get_settings = gfar_gsettings,
1744 	.set_settings = gfar_ssettings,
1745 	.get_drvinfo = gfar_gdrvinfo,
1746 	.get_regs_len = gfar_reglen,
1747 	.get_regs = gfar_get_regs,
1748 	.get_link = ethtool_op_get_link,
1749 	.get_coalesce = gfar_gcoalesce,
1750 	.set_coalesce = gfar_scoalesce,
1751 	.get_ringparam = gfar_gringparam,
1752 	.set_ringparam = gfar_sringparam,
1753 	.get_strings = gfar_gstrings,
1754 	.get_sset_count = gfar_sset_count,
1755 	.get_ethtool_stats = gfar_fill_stats,
1756 	.get_msglevel = gfar_get_msglevel,
1757 	.set_msglevel = gfar_set_msglevel,
1758 #ifdef CONFIG_PM
1759 	.get_wol = gfar_get_wol,
1760 	.set_wol = gfar_set_wol,
1761 #endif
1762 	.set_rxnfc = gfar_set_nfc,
1763 	.get_rxnfc = gfar_get_nfc,
1764 };
1765