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