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