blob: 8248df760aad419e7a178f0d1926d330d86ec780 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Gortmaker3396c782012-01-27 13:36:01 +00002 * drivers/net/ethernet/freescale/gianfar_ethtool.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Gianfar Ethernet Driver
5 * Ethtool support for Gianfar Enet
6 * Based on e1000 ethtool support
7 *
8 * Author: Andy Fleming
Kumar Gala4c8d3d92005-11-13 16:06:30 -08009 * Maintainer: Kumar Gala
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000010 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +000012 * Copyright 2003-2006, 2008-2009, 2011 Freescale Semiconductor, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014 * This software may be used and distributed according to
15 * the terms of the GNU Public License, Version 2, incorporated herein
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * by reference.
17 */
18
Joe Perches59deab22011-06-14 08:57:47 +000019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/string.h>
23#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
David S. Miller65a85a82012-04-06 00:35:34 -040029#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/crc32.h>
39#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/ethtool.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040041#include <linux/mii.h>
42#include <linux/phy.h>
Sebastian Poehn4aa3a712011-06-20 13:57:59 -070043#include <linux/sort.h>
Sebastian Poehn380b1532011-07-07 04:30:29 -070044#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "gianfar.h"
47
Kumar Gala0bbaf062005-06-20 10:54:21 -050048extern void gfar_start(struct net_device *dev);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +000049extern int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue,
50 int rx_work_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Andy Flemingbb40dcb2005-09-23 22:54:21 -040052#define GFAR_MAX_COAL_USECS 0xffff
53#define GFAR_MAX_COAL_FRAMES 0xff
Kumar Gala0bbaf062005-06-20 10:54:21 -050054static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +000055 u64 *buf);
Kumar Gala0bbaf062005-06-20 10:54:21 -050056static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +000057static int gfar_gcoalesce(struct net_device *dev,
58 struct ethtool_coalesce *cvals);
59static int gfar_scoalesce(struct net_device *dev,
60 struct ethtool_coalesce *cvals);
61static void gfar_gringparam(struct net_device *dev,
62 struct ethtool_ringparam *rvals);
63static int gfar_sringparam(struct net_device *dev,
64 struct ethtool_ringparam *rvals);
65static void gfar_gdrvinfo(struct net_device *dev,
66 struct ethtool_drvinfo *drvinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Paul Gortmaker30f7e312012-01-08 13:21:57 -050068static const char stat_gstrings[][ETH_GSTRING_LEN] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 "rx-dropped-by-kernel",
70 "rx-large-frame-errors",
71 "rx-short-frame-errors",
72 "rx-non-octet-errors",
73 "rx-crc-errors",
74 "rx-overrun-errors",
75 "rx-busy-errors",
76 "rx-babbling-errors",
77 "rx-truncated-frames",
78 "ethernet-bus-error",
79 "tx-babbling-errors",
80 "tx-underrun-errors",
81 "rx-skb-missing-errors",
82 "tx-timeout-errors",
83 "tx-rx-64-frames",
84 "tx-rx-65-127-frames",
85 "tx-rx-128-255-frames",
86 "tx-rx-256-511-frames",
87 "tx-rx-512-1023-frames",
88 "tx-rx-1024-1518-frames",
89 "tx-rx-1519-1522-good-vlan",
90 "rx-bytes",
91 "rx-packets",
92 "rx-fcs-errors",
93 "receive-multicast-packet",
94 "receive-broadcast-packet",
95 "rx-control-frame-packets",
96 "rx-pause-frame-packets",
97 "rx-unknown-op-code",
98 "rx-alignment-error",
99 "rx-frame-length-error",
100 "rx-code-error",
101 "rx-carrier-sense-error",
102 "rx-undersize-packets",
103 "rx-oversize-packets",
104 "rx-fragmented-frames",
105 "rx-jabber-frames",
106 "rx-dropped-frames",
107 "tx-byte-counter",
108 "tx-packets",
109 "tx-multicast-packets",
110 "tx-broadcast-packets",
111 "tx-pause-control-frames",
112 "tx-deferral-packets",
113 "tx-excessive-deferral-packets",
114 "tx-single-collision-packets",
115 "tx-multiple-collision-packets",
116 "tx-late-collision-packets",
117 "tx-excessive-collision-packets",
118 "tx-total-collision",
119 "reserved",
120 "tx-dropped-frames",
121 "tx-jabber-frames",
122 "tx-fcs-errors",
123 "tx-control-frames",
124 "tx-oversize-frames",
125 "tx-undersize-frames",
126 "tx-fragmented-frames",
127};
128
Kumar Gala0bbaf062005-06-20 10:54:21 -0500129/* Fill in a buffer with the strings which correspond to the
130 * stats */
131static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
132{
133 struct gfar_private *priv = netdev_priv(dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600134
Andy Flemingb31a1d82008-12-16 15:29:15 -0800135 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500136 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
137 else
138 memcpy(buf, stat_gstrings,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000139 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* Fill in an array of 64-bit statistics from various sources.
143 * This array will be appended to the end of the ethtool_stats
144 * structure, and returned to user space
145 */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000146static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
147 u64 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 int i;
150 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000151 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Paul Gortmaker212079d2013-02-12 15:38:19 -0500152 atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Paul Gortmaker68719782013-02-12 15:28:35 -0500154 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
Paul Gortmaker212079d2013-02-12 15:38:19 -0500155 buf[i] = atomic64_read(&extra[i]);
Paul Gortmaker68719782013-02-12 15:28:35 -0500156
Andy Flemingb31a1d82008-12-16 15:29:15 -0800157 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000158 u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Paul Gortmaker68719782013-02-12 15:28:35 -0500160 for (; i < GFAR_STATS_LEN; i++, rmon++)
161 buf[i] = (u64) gfar_read(rmon);
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700165static int gfar_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700169 switch (sset) {
170 case ETH_SS_STATS:
Andy Flemingb31a1d82008-12-16 15:29:15 -0800171 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700172 return GFAR_STATS_LEN;
173 else
174 return GFAR_EXTRA_STATS_LEN;
175 default:
176 return -EOPNOTSUPP;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/* Fills in the drvinfo structure with some basic info */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000181static void gfar_gdrvinfo(struct net_device *dev,
182 struct ethtool_drvinfo *drvinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Jiri Pirko7826d432013-01-06 00:44:26 +0000184 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
185 strlcpy(drvinfo->version, gfar_driver_version,
186 sizeof(drvinfo->version));
187 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
188 strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 drvinfo->regdump_len = 0;
190 drvinfo->eedump_len = 0;
191}
192
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400193
194static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
195{
196 struct gfar_private *priv = netdev_priv(dev);
197 struct phy_device *phydev = priv->phydev;
198
199 if (NULL == phydev)
200 return -ENODEV;
201
202 return phy_ethtool_sset(phydev, cmd);
203}
204
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* Return the current settings in the ethtool_cmd structure */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500207static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400210 struct phy_device *phydev = priv->phydev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000211 struct gfar_priv_rx_q *rx_queue = NULL;
212 struct gfar_priv_tx_q *tx_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400214 if (NULL == phydev)
215 return -ENODEV;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000216 tx_queue = priv->tx_queue[0];
217 rx_queue = priv->rx_queue[0];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400218
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000219 /* etsec-1.7 and older versions have only one txic
220 * and rxic regs although they support multiple queues */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000221 cmd->maxtxpkt = get_icft_value(tx_queue->txic);
222 cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400224 return phy_ethtool_gset(phydev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/* Return the length of the register structure */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500228static int gfar_reglen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 return sizeof (struct gfar);
231}
232
233/* Return a dump of the GFAR register space */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000234static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
235 void *regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i;
238 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000239 u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 u32 *buf = (u32 *) regbuf;
241
242 for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
Kumar Galacc8c6e32006-02-01 15:18:03 -0600243 buf[i] = gfar_read(&theregs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/* Convert microseconds to ethernet clock ticks, which changes
247 * depending on what speed the controller is running at */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000248static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
249 unsigned int usecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 unsigned int count;
252
253 /* The timer is different, depending on the interface speed */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400254 switch (priv->phydev->speed) {
255 case SPEED_1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 count = GFAR_GBIT_TIME;
257 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400258 case SPEED_100:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 count = GFAR_100_TIME;
260 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400261 case SPEED_10:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 default:
263 count = GFAR_10_TIME;
264 break;
265 }
266
267 /* Make sure we return a number greater than 0
268 * if usecs > 0 */
Eric Dumazet807540b2010-09-23 05:40:09 +0000269 return (usecs * 1000 + count - 1) / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/* Convert ethernet clock ticks to microseconds */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000273static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
274 unsigned int ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 unsigned int count;
277
278 /* The timer is different, depending on the interface speed */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400279 switch (priv->phydev->speed) {
280 case SPEED_1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 count = GFAR_GBIT_TIME;
282 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400283 case SPEED_100:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 count = GFAR_100_TIME;
285 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400286 case SPEED_10:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 default:
288 count = GFAR_10_TIME;
289 break;
290 }
291
292 /* Make sure we return a number greater than 0 */
293 /* if ticks is > 0 */
Eric Dumazet807540b2010-09-23 05:40:09 +0000294 return (ticks * count) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/* Get the coalescing parameters, and put them in the cvals
298 * structure. */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000299static int gfar_gcoalesce(struct net_device *dev,
300 struct ethtool_coalesce *cvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000303 struct gfar_priv_rx_q *rx_queue = NULL;
304 struct gfar_priv_tx_q *tx_queue = NULL;
Dai Harukib46a8452008-12-16 15:29:52 -0800305 unsigned long rxtime;
306 unsigned long rxcount;
307 unsigned long txtime;
308 unsigned long txcount;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400309
Andy Flemingb31a1d82008-12-16 15:29:15 -0800310 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
Kumar Gala0bbaf062005-06-20 10:54:21 -0500311 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400313 if (NULL == priv->phydev)
314 return -ENODEV;
315
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000316 rx_queue = priv->rx_queue[0];
317 tx_queue = priv->tx_queue[0];
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000318
319 rxtime = get_ictt_value(rx_queue->rxic);
320 rxcount = get_icft_value(rx_queue->rxic);
321 txtime = get_ictt_value(tx_queue->txic);
322 txcount = get_icft_value(tx_queue->txic);
Dai Harukib46a8452008-12-16 15:29:52 -0800323 cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
324 cvals->rx_max_coalesced_frames = rxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Dai Harukib46a8452008-12-16 15:29:52 -0800326 cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
327 cvals->tx_max_coalesced_frames = txcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 cvals->use_adaptive_rx_coalesce = 0;
330 cvals->use_adaptive_tx_coalesce = 0;
331
332 cvals->pkt_rate_low = 0;
333 cvals->rx_coalesce_usecs_low = 0;
334 cvals->rx_max_coalesced_frames_low = 0;
335 cvals->tx_coalesce_usecs_low = 0;
336 cvals->tx_max_coalesced_frames_low = 0;
337
338 /* When the packet rate is below pkt_rate_high but above
339 * pkt_rate_low (both measured in packets per second) the
340 * normal {rx,tx}_* coalescing parameters are used.
341 */
342
343 /* When the packet rate is (measured in packets per second)
344 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
345 * used.
346 */
347 cvals->pkt_rate_high = 0;
348 cvals->rx_coalesce_usecs_high = 0;
349 cvals->rx_max_coalesced_frames_high = 0;
350 cvals->tx_coalesce_usecs_high = 0;
351 cvals->tx_max_coalesced_frames_high = 0;
352
353 /* How often to do adaptive coalescing packet rate sampling,
354 * measured in seconds. Must not be zero.
355 */
356 cvals->rate_sample_interval = 0;
357
358 return 0;
359}
360
361/* Change the coalescing values.
362 * Both cvals->*_usecs and cvals->*_frames have to be > 0
363 * in order for coalescing to be active
364 */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000365static int gfar_scoalesce(struct net_device *dev,
366 struct ethtool_coalesce *cvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000369 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Andy Flemingb31a1d82008-12-16 15:29:15 -0800371 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
Kumar Gala0bbaf062005-06-20 10:54:21 -0500372 return -EOPNOTSUPP;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* Set up rx coalescing */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000375 /* As of now, we will enable/disable coalescing for all
376 * queues together in case of eTSEC2, this will be modified
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000377 * along with the ethtool interface
378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if ((cvals->rx_coalesce_usecs == 0) ||
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000380 (cvals->rx_max_coalesced_frames == 0)) {
381 for (i = 0; i < priv->num_rx_queues; i++)
382 priv->rx_queue[i]->rxcoalescing = 0;
383 } else {
384 for (i = 0; i < priv->num_rx_queues; i++)
385 priv->rx_queue[i]->rxcoalescing = 1;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400388 if (NULL == priv->phydev)
389 return -ENODEV;
390
391 /* Check the bounds of the values */
392 if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
393 pr_info("Coalescing is limited to %d microseconds\n",
Joe Perches59deab22011-06-14 08:57:47 +0000394 GFAR_MAX_COAL_USECS);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400395 return -EINVAL;
396 }
397
398 if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
399 pr_info("Coalescing is limited to %d frames\n",
Joe Perches59deab22011-06-14 08:57:47 +0000400 GFAR_MAX_COAL_FRAMES);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400401 return -EINVAL;
402 }
403
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000404 for (i = 0; i < priv->num_rx_queues; i++) {
405 priv->rx_queue[i]->rxic = mk_ic_value(
406 cvals->rx_max_coalesced_frames,
407 gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* Set up tx coalescing */
411 if ((cvals->tx_coalesce_usecs == 0) ||
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000412 (cvals->tx_max_coalesced_frames == 0)) {
413 for (i = 0; i < priv->num_tx_queues; i++)
414 priv->tx_queue[i]->txcoalescing = 0;
415 } else {
416 for (i = 0; i < priv->num_tx_queues; i++)
417 priv->tx_queue[i]->txcoalescing = 1;
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400420 /* Check the bounds of the values */
421 if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
422 pr_info("Coalescing is limited to %d microseconds\n",
Joe Perches59deab22011-06-14 08:57:47 +0000423 GFAR_MAX_COAL_USECS);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400424 return -EINVAL;
425 }
426
427 if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
428 pr_info("Coalescing is limited to %d frames\n",
Joe Perches59deab22011-06-14 08:57:47 +0000429 GFAR_MAX_COAL_FRAMES);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400430 return -EINVAL;
431 }
432
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000433 for (i = 0; i < priv->num_tx_queues; i++) {
434 priv->tx_queue[i]->txic = mk_ic_value(
435 cvals->tx_max_coalesced_frames,
436 gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Claudiu Manoil800c6442013-03-19 07:40:05 +0000439 gfar_configure_coalescing_all(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 return 0;
442}
443
444/* Fills in rvals with the current ring parameters. Currently,
445 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
446 * jumbo are ignored by the driver */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000447static void gfar_gringparam(struct net_device *dev,
448 struct ethtool_ringparam *rvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000451 struct gfar_priv_tx_q *tx_queue = NULL;
452 struct gfar_priv_rx_q *rx_queue = NULL;
453
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000454 tx_queue = priv->tx_queue[0];
455 rx_queue = priv->rx_queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
458 rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
459 rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
460 rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
461
462 /* Values changeable by the user. The valid values are
463 * in the range 1 to the "*_max_pending" counterpart above.
464 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000465 rvals->rx_pending = rx_queue->rx_ring_size;
466 rvals->rx_mini_pending = rx_queue->rx_ring_size;
467 rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
468 rvals->tx_pending = tx_queue->tx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471/* Change the current ring parameters, stopping the controller if
472 * necessary so that we don't mess things up while we're in
473 * motion. We wait for the ring to be clean before reallocating
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000474 * the rings.
475 */
476static int gfar_sringparam(struct net_device *dev,
477 struct ethtool_ringparam *rvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000480 int err = 0, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
483 return -EINVAL;
484
485 if (!is_power_of_2(rvals->rx_pending)) {
Joe Perches59deab22011-06-14 08:57:47 +0000486 netdev_err(dev, "Ring sizes must be a power of 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -EINVAL;
488 }
489
490 if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
491 return -EINVAL;
492
493 if (!is_power_of_2(rvals->tx_pending)) {
Joe Perches59deab22011-06-14 08:57:47 +0000494 netdev_err(dev, "Ring sizes must be a power of 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -EINVAL;
496 }
497
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (dev->flags & IFF_UP) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500500 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Kumar Gala0bbaf062005-06-20 10:54:21 -0500502 /* Halt TX and RX, and process the frames which
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000503 * have already been received
504 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000505 local_irq_save(flags);
506 lock_tx_qs(priv);
507 lock_rx_qs(priv);
Andy Flemingfef61082006-04-20 16:44:29 -0500508
Kumar Gala0bbaf062005-06-20 10:54:21 -0500509 gfar_halt(dev);
Andy Flemingfef61082006-04-20 16:44:29 -0500510
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000511 unlock_rx_qs(priv);
512 unlock_tx_qs(priv);
513 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000515 for (i = 0; i < priv->num_rx_queues; i++)
516 gfar_clean_rx_ring(priv->rx_queue[i],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000517 priv->rx_queue[i]->rx_ring_size);
Dai Haruki12dea572008-12-16 15:30:20 -0800518
Kumar Gala0bbaf062005-06-20 10:54:21 -0500519 /* Now we take down the rings to rebuild them */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 stop_gfar(dev);
521 }
522
Kumar Gala0bbaf062005-06-20 10:54:21 -0500523 /* Change the size */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000524 for (i = 0; i < priv->num_rx_queues; i++) {
525 priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
526 priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000527 priv->tx_queue[i]->num_txbdfree =
528 priv->tx_queue[i]->tx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Kumar Gala0bbaf062005-06-20 10:54:21 -0500531 /* Rebuild the rings with the new size */
Dai Haruki12dea572008-12-16 15:30:20 -0800532 if (dev->flags & IFF_UP) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500533 err = startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000534 netif_tx_wake_all_queues(dev);
Dai Haruki12dea572008-12-16 15:30:20 -0800535 }
Kumar Gala0bbaf062005-06-20 10:54:21 -0500536 return err;
537}
538
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000539int gfar_set_features(struct net_device *dev, netdev_features_t features)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500540{
541 struct gfar_private *priv = netdev_priv(dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500542 unsigned long flags;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000543 int err = 0, i = 0;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000544 netdev_features_t changed = dev->features ^ features;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500545
Jiri Pirko87c288c2011-07-20 04:54:19 +0000546 if (changed & (NETIF_F_HW_VLAN_TX|NETIF_F_HW_VLAN_RX))
547 gfar_vlan_mode(dev, features);
548
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000549 if (!(changed & NETIF_F_RXCSUM))
550 return 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000551
Kumar Gala0bbaf062005-06-20 10:54:21 -0500552 if (dev->flags & IFF_UP) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500553 /* Halt TX and RX, and process the frames which
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000554 * have already been received
555 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000556 local_irq_save(flags);
557 lock_tx_qs(priv);
558 lock_rx_qs(priv);
Andy Flemingfef61082006-04-20 16:44:29 -0500559
Kumar Gala0bbaf062005-06-20 10:54:21 -0500560 gfar_halt(dev);
Andy Flemingfef61082006-04-20 16:44:29 -0500561
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000562 unlock_tx_qs(priv);
563 unlock_rx_qs(priv);
Dan Carpenter97564032010-10-13 09:19:55 +0000564 local_irq_restore(flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500565
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000566 for (i = 0; i < priv->num_rx_queues; i++)
567 gfar_clean_rx_ring(priv->rx_queue[i],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000568 priv->rx_queue[i]->rx_ring_size);
Dai Haruki12dea572008-12-16 15:30:20 -0800569
Kumar Gala0bbaf062005-06-20 10:54:21 -0500570 /* Now we take down the rings to rebuild them */
571 stop_gfar(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500572
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000573 dev->features = features;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 err = startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000576 netif_tx_wake_all_queues(dev);
Dai Haruki12dea572008-12-16 15:30:20 -0800577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return err;
579}
580
Kumar Gala0bbaf062005-06-20 10:54:21 -0500581static uint32_t gfar_get_msglevel(struct net_device *dev)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400582{
Kumar Gala0bbaf062005-06-20 10:54:21 -0500583 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000584
Kumar Gala0bbaf062005-06-20 10:54:21 -0500585 return priv->msg_enable;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400586}
587
Kumar Gala0bbaf062005-06-20 10:54:21 -0500588static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400589{
Kumar Gala0bbaf062005-06-20 10:54:21 -0500590 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000591
Kumar Gala0bbaf062005-06-20 10:54:21 -0500592 priv->msg_enable = data;
593}
594
Scott Woodd87eb122008-07-11 18:04:45 -0500595#ifdef CONFIG_PM
596static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
597{
598 struct gfar_private *priv = netdev_priv(dev);
599
Andy Flemingb31a1d82008-12-16 15:29:15 -0800600 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
Scott Woodd87eb122008-07-11 18:04:45 -0500601 wol->supported = WAKE_MAGIC;
602 wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
603 } else {
604 wol->supported = wol->wolopts = 0;
605 }
606}
607
608static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
609{
610 struct gfar_private *priv = netdev_priv(dev);
611 unsigned long flags;
612
Andy Flemingb31a1d82008-12-16 15:29:15 -0800613 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -0500614 wol->wolopts != 0)
615 return -EINVAL;
616
617 if (wol->wolopts & ~WAKE_MAGIC)
618 return -EINVAL;
619
Rafael J. Wysocki6c4f1992010-11-09 11:54:19 +0000620 device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);
621
Scott Woodd87eb122008-07-11 18:04:45 -0500622 spin_lock_irqsave(&priv->bflock, flags);
Rafael J. Wysocki6c4f1992010-11-09 11:54:19 +0000623 priv->wol_en = !!device_may_wakeup(&dev->dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500624 spin_unlock_irqrestore(&priv->bflock, flags);
625
626 return 0;
627}
628#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500629
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000630static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
631{
632 u32 fcr = 0x0, fpr = FPR_FILER_MASK;
633
634 if (ethflow & RXH_L2DA) {
635 fcr = RQFCR_PID_DAH |RQFCR_CMP_NOMATCH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000636 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000637 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
638 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000639 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
640 priv->cur_filer_idx = priv->cur_filer_idx - 1;
641
642 fcr = RQFCR_PID_DAL | RQFCR_AND | RQFCR_CMP_NOMATCH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000643 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000644 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
645 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000646 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
647 priv->cur_filer_idx = priv->cur_filer_idx - 1;
648 }
649
650 if (ethflow & RXH_VLAN) {
651 fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000652 RQFCR_AND | RQFCR_HASHTBL_0;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000653 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000654 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
655 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000656 priv->cur_filer_idx = priv->cur_filer_idx - 1;
657 }
658
659 if (ethflow & RXH_IP_SRC) {
660 fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000661 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000662 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
663 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000664 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
665 priv->cur_filer_idx = priv->cur_filer_idx - 1;
666 }
667
668 if (ethflow & (RXH_IP_DST)) {
669 fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000670 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000671 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
672 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000673 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
674 priv->cur_filer_idx = priv->cur_filer_idx - 1;
675 }
676
677 if (ethflow & RXH_L3_PROTO) {
678 fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000679 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000680 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
681 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000682 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
683 priv->cur_filer_idx = priv->cur_filer_idx - 1;
684 }
685
686 if (ethflow & RXH_L4_B_0_1) {
687 fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000688 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000689 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
690 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000691 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
692 priv->cur_filer_idx = priv->cur_filer_idx - 1;
693 }
694
695 if (ethflow & RXH_L4_B_2_3) {
696 fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000697 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000698 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
699 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000700 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
701 priv->cur_filer_idx = priv->cur_filer_idx - 1;
702 }
703}
704
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000705static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
706 u64 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000707{
708 unsigned int last_rule_idx = priv->cur_filer_idx;
709 unsigned int cmp_rqfpr;
Wang Shaoyan588dc912011-08-11 17:07:25 +0000710 unsigned int *local_rqfpr;
711 unsigned int *local_rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000712 int i = 0x0, k = 0x0;
713 int j = MAX_FILER_IDX, l = 0x0;
Wang Shaoyan588dc912011-08-11 17:07:25 +0000714 int ret = 1;
715
Joe Perchesb2adaca2013-02-03 17:43:58 +0000716 local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
717 GFP_KERNEL);
718 local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
719 GFP_KERNEL);
Wang Shaoyan588dc912011-08-11 17:07:25 +0000720 if (!local_rqfpr || !local_rqfcr) {
Wang Shaoyan588dc912011-08-11 17:07:25 +0000721 ret = 0;
722 goto err;
723 }
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000724
725 switch (class) {
726 case TCP_V4_FLOW:
727 cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
728 break;
729 case UDP_V4_FLOW:
730 cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
731 break;
732 case TCP_V6_FLOW:
733 cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
734 break;
735 case UDP_V6_FLOW:
736 cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
737 break;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000738 default:
Joe Perches59deab22011-06-14 08:57:47 +0000739 pr_err("Right now this class is not supported\n");
Wang Shaoyan588dc912011-08-11 17:07:25 +0000740 ret = 0;
741 goto err;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000742 }
743
744 for (i = 0; i < MAX_FILER_IDX + 1; i++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000745 local_rqfpr[j] = priv->ftp_rqfpr[i];
746 local_rqfcr[j] = priv->ftp_rqfcr[i];
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000747 j--;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000748 if ((priv->ftp_rqfcr[i] ==
749 (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
750 (priv->ftp_rqfpr[i] == cmp_rqfpr))
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000751 break;
752 }
753
754 if (i == MAX_FILER_IDX + 1) {
Joe Perches59deab22011-06-14 08:57:47 +0000755 pr_err("No parse rule found, can't create hash rules\n");
Wang Shaoyan588dc912011-08-11 17:07:25 +0000756 ret = 0;
757 goto err;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000758 }
759
760 /* If a match was found, then it begins the starting of a cluster rule
761 * if it was already programmed, we need to overwrite these rules
762 */
763 for (l = i+1; l < MAX_FILER_IDX; l++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000764 if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000765 !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000766 priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000767 RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000768 priv->ftp_rqfpr[l] = FPR_FILER_MASK;
769 gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000770 priv->ftp_rqfpr[l]);
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000771 break;
772 }
773
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000774 if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
775 (priv->ftp_rqfcr[l] & RQFCR_AND))
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000776 continue;
777 else {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000778 local_rqfpr[j] = priv->ftp_rqfpr[l];
779 local_rqfcr[j] = priv->ftp_rqfcr[l];
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000780 j--;
781 }
782 }
783
784 priv->cur_filer_idx = l - 1;
785 last_rule_idx = l;
786
787 /* hash rules */
788 ethflow_to_filer_rules(priv, ethflow);
789
790 /* Write back the popped out rules again */
791 for (k = j+1; k < MAX_FILER_IDX; k++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000792 priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
793 priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000794 gfar_write_filer(priv, priv->cur_filer_idx,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000795 local_rqfcr[k], local_rqfpr[k]);
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000796 if (!priv->cur_filer_idx)
797 break;
798 priv->cur_filer_idx = priv->cur_filer_idx - 1;
799 }
800
Wang Shaoyan588dc912011-08-11 17:07:25 +0000801err:
802 kfree(local_rqfcr);
803 kfree(local_rqfpr);
804 return ret;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000805}
806
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000807static int gfar_set_hash_opts(struct gfar_private *priv,
808 struct ethtool_rxnfc *cmd)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000809{
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000810 /* write the filer rules here */
811 if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
Ben Hutchingsbde35282011-04-08 13:45:11 +0000812 return -EINVAL;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000813
814 return 0;
815}
816
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700817static int gfar_check_filer_hardware(struct gfar_private *priv)
818{
819 struct gfar __iomem *regs = NULL;
820 u32 i;
821
822 regs = priv->gfargrp[0].regs;
823
824 /* Check if we are in FIFO mode */
825 i = gfar_read(&regs->ecntrl);
826 i &= ECNTRL_FIFM;
827 if (i == ECNTRL_FIFM) {
828 netdev_notice(priv->ndev, "Interface in FIFO mode\n");
829 i = gfar_read(&regs->rctrl);
830 i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
831 if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
832 netdev_info(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000833 "Receive Queue Filtering enabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700834 } else {
835 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000836 "Receive Queue Filtering disabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700837 return -EOPNOTSUPP;
838 }
839 }
840 /* Or in standard mode */
841 else {
842 i = gfar_read(&regs->rctrl);
843 i &= RCTRL_PRSDEP_MASK;
844 if (i == RCTRL_PRSDEP_MASK) {
845 netdev_info(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000846 "Receive Queue Filtering enabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700847 } else {
848 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000849 "Receive Queue Filtering disabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700850 return -EOPNOTSUPP;
851 }
852 }
853
854 /* Sets the properties for arbitrary filer rule
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000855 * to the first 4 Layer 4 Bytes
856 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700857 regs->rbifx = 0xC0C1C2C3;
858 return 0;
859}
860
861static int gfar_comp_asc(const void *a, const void *b)
862{
863 return memcmp(a, b, 4);
864}
865
866static int gfar_comp_desc(const void *a, const void *b)
867{
868 return -memcmp(a, b, 4);
869}
870
871static void gfar_swap(void *a, void *b, int size)
872{
873 u32 *_a = a;
874 u32 *_b = b;
875
876 swap(_a[0], _b[0]);
877 swap(_a[1], _b[1]);
878 swap(_a[2], _b[2]);
879 swap(_a[3], _b[3]);
880}
881
882/* Write a mask to filer cache */
883static void gfar_set_mask(u32 mask, struct filer_table *tab)
884{
885 tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
886 tab->fe[tab->index].prop = mask;
887 tab->index++;
888}
889
890/* Sets parse bits (e.g. IP or TCP) */
891static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
892{
893 gfar_set_mask(mask, tab);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000894 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
895 RQFCR_AND;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700896 tab->fe[tab->index].prop = value;
897 tab->index++;
898}
899
900static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000901 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700902{
903 gfar_set_mask(mask, tab);
904 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
905 tab->fe[tab->index].prop = value;
906 tab->index++;
907}
908
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000909/* For setting a tuple of value and mask of type flag
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700910 * Example:
911 * IP-Src = 10.0.0.0/255.0.0.0
912 * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
913 *
914 * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
915 * For a don't care mask it gives us a 0
916 *
917 * The check if don't care and the mask adjustment if mask=0 is done for VLAN
918 * and MAC stuff on an upper level (due to missing information on this level).
919 * For these guys we can discard them if they are value=0 and mask=0.
920 *
921 * Further the all masks are one-padded for better hardware efficiency.
922 */
923static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000924 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700925{
926 switch (flag) {
Sebastian Poehn380b1532011-07-07 04:30:29 -0700927 /* 3bit */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700928 case RQFCR_PID_PRI:
929 if (!(value | mask))
930 return;
931 mask |= RQFCR_PID_PRI_MASK;
932 break;
933 /* 8bit */
934 case RQFCR_PID_L4P:
935 case RQFCR_PID_TOS:
936 if (!~(mask | RQFCR_PID_L4P_MASK))
937 return;
938 if (!mask)
939 mask = ~0;
940 else
941 mask |= RQFCR_PID_L4P_MASK;
942 break;
943 /* 12bit */
944 case RQFCR_PID_VID:
945 if (!(value | mask))
946 return;
947 mask |= RQFCR_PID_VID_MASK;
948 break;
949 /* 16bit */
950 case RQFCR_PID_DPT:
951 case RQFCR_PID_SPT:
952 case RQFCR_PID_ETY:
953 if (!~(mask | RQFCR_PID_PORT_MASK))
954 return;
955 if (!mask)
956 mask = ~0;
957 else
958 mask |= RQFCR_PID_PORT_MASK;
959 break;
960 /* 24bit */
961 case RQFCR_PID_DAH:
962 case RQFCR_PID_DAL:
963 case RQFCR_PID_SAH:
964 case RQFCR_PID_SAL:
965 if (!(value | mask))
966 return;
967 mask |= RQFCR_PID_MAC_MASK;
968 break;
969 /* for all real 32bit masks */
970 default:
971 if (!~mask)
972 return;
973 if (!mask)
974 mask = ~0;
975 break;
976 }
977 gfar_set_general_attribute(value, mask, flag, tab);
978}
979
980/* Translates value and mask for UDP, TCP or SCTP */
981static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000982 struct ethtool_tcpip4_spec *mask,
983 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700984{
985 gfar_set_attribute(value->ip4src, mask->ip4src, RQFCR_PID_SIA, tab);
986 gfar_set_attribute(value->ip4dst, mask->ip4dst, RQFCR_PID_DIA, tab);
987 gfar_set_attribute(value->pdst, mask->pdst, RQFCR_PID_DPT, tab);
988 gfar_set_attribute(value->psrc, mask->psrc, RQFCR_PID_SPT, tab);
989 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
990}
991
992/* Translates value and mask for RAW-IP4 */
993static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000994 struct ethtool_usrip4_spec *mask,
995 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700996{
997 gfar_set_attribute(value->ip4src, mask->ip4src, RQFCR_PID_SIA, tab);
998 gfar_set_attribute(value->ip4dst, mask->ip4dst, RQFCR_PID_DIA, tab);
999 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1000 gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
1001 gfar_set_attribute(value->l4_4_bytes, mask->l4_4_bytes, RQFCR_PID_ARB,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001002 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001003
1004}
1005
1006/* Translates value and mask for ETHER spec */
1007static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001008 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001009{
1010 u32 upper_temp_mask = 0;
1011 u32 lower_temp_mask = 0;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001012
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001013 /* Source address */
1014 if (!is_broadcast_ether_addr(mask->h_source)) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001015 if (is_zero_ether_addr(mask->h_source)) {
1016 upper_temp_mask = 0xFFFFFFFF;
1017 lower_temp_mask = 0xFFFFFFFF;
1018 } else {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001019 upper_temp_mask = mask->h_source[0] << 16 |
1020 mask->h_source[1] << 8 |
1021 mask->h_source[2];
1022 lower_temp_mask = mask->h_source[3] << 16 |
1023 mask->h_source[4] << 8 |
1024 mask->h_source[5];
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001025 }
1026 /* Upper 24bit */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001027 gfar_set_attribute(value->h_source[0] << 16 |
1028 value->h_source[1] << 8 |
1029 value->h_source[2],
1030 upper_temp_mask, RQFCR_PID_SAH, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001031 /* And the same for the lower part */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001032 gfar_set_attribute(value->h_source[3] << 16 |
1033 value->h_source[4] << 8 |
1034 value->h_source[5],
1035 lower_temp_mask, RQFCR_PID_SAL, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001036 }
1037 /* Destination address */
1038 if (!is_broadcast_ether_addr(mask->h_dest)) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001039 /* Special for destination is limited broadcast */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001040 if ((is_broadcast_ether_addr(value->h_dest) &&
1041 is_zero_ether_addr(mask->h_dest))) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001042 gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
1043 } else {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001044 if (is_zero_ether_addr(mask->h_dest)) {
1045 upper_temp_mask = 0xFFFFFFFF;
1046 lower_temp_mask = 0xFFFFFFFF;
1047 } else {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001048 upper_temp_mask = mask->h_dest[0] << 16 |
1049 mask->h_dest[1] << 8 |
1050 mask->h_dest[2];
1051 lower_temp_mask = mask->h_dest[3] << 16 |
1052 mask->h_dest[4] << 8 |
1053 mask->h_dest[5];
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001054 }
1055
1056 /* Upper 24bit */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001057 gfar_set_attribute(value->h_dest[0] << 16 |
1058 value->h_dest[1] << 8 |
1059 value->h_dest[2],
1060 upper_temp_mask, RQFCR_PID_DAH, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001061 /* And the same for the lower part */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001062 gfar_set_attribute(value->h_dest[3] << 16 |
1063 value->h_dest[4] << 8 |
1064 value->h_dest[5],
1065 lower_temp_mask, RQFCR_PID_DAL, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001066 }
1067 }
1068
1069 gfar_set_attribute(value->h_proto, mask->h_proto, RQFCR_PID_ETY, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001070}
1071
1072/* Convert a rule to binary filter format of gianfar */
1073static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001074 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001075{
1076 u32 vlan = 0, vlan_mask = 0;
1077 u32 id = 0, id_mask = 0;
1078 u32 cfi = 0, cfi_mask = 0;
1079 u32 prio = 0, prio_mask = 0;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001080 u32 old_index = tab->index;
1081
1082 /* Check if vlan is wanted */
1083 if ((rule->flow_type & FLOW_EXT) && (rule->m_ext.vlan_tci != 0xFFFF)) {
1084 if (!rule->m_ext.vlan_tci)
1085 rule->m_ext.vlan_tci = 0xFFFF;
1086
1087 vlan = RQFPR_VLN;
1088 vlan_mask = RQFPR_VLN;
1089
1090 /* Separate the fields */
Sebastian Poehn380b1532011-07-07 04:30:29 -07001091 id = rule->h_ext.vlan_tci & VLAN_VID_MASK;
1092 id_mask = rule->m_ext.vlan_tci & VLAN_VID_MASK;
1093 cfi = rule->h_ext.vlan_tci & VLAN_CFI_MASK;
1094 cfi_mask = rule->m_ext.vlan_tci & VLAN_CFI_MASK;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001095 prio = (rule->h_ext.vlan_tci & VLAN_PRIO_MASK) >>
1096 VLAN_PRIO_SHIFT;
1097 prio_mask = (rule->m_ext.vlan_tci & VLAN_PRIO_MASK) >>
1098 VLAN_PRIO_SHIFT;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001099
Sebastian Poehn380b1532011-07-07 04:30:29 -07001100 if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001101 vlan |= RQFPR_CFI;
1102 vlan_mask |= RQFPR_CFI;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001103 } else if (cfi != VLAN_TAG_PRESENT &&
1104 cfi_mask == VLAN_TAG_PRESENT) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001105 vlan_mask |= RQFPR_CFI;
1106 }
1107 }
1108
1109 switch (rule->flow_type & ~FLOW_EXT) {
1110 case TCP_V4_FLOW:
1111 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001112 RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001113 gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001114 &rule->m_u.tcp_ip4_spec, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001115 break;
1116 case UDP_V4_FLOW:
1117 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001118 RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001119 gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001120 &rule->m_u.udp_ip4_spec, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001121 break;
1122 case SCTP_V4_FLOW:
1123 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001124 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001125 gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001126 gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
1127 (struct ethtool_tcpip4_spec *)&rule->m_u,
1128 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001129 break;
1130 case IP_USER_FLOW:
1131 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001132 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001133 gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001134 (struct ethtool_usrip4_spec *) &rule->m_u,
1135 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001136 break;
1137 case ETHER_FLOW:
1138 if (vlan)
1139 gfar_set_parse_bits(vlan, vlan_mask, tab);
1140 gfar_set_ether((struct ethhdr *) &rule->h_u,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001141 (struct ethhdr *) &rule->m_u, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001142 break;
1143 default:
1144 return -1;
1145 }
1146
1147 /* Set the vlan attributes in the end */
1148 if (vlan) {
1149 gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
1150 gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
1151 }
1152
1153 /* If there has been nothing written till now, it must be a default */
1154 if (tab->index == old_index) {
1155 gfar_set_mask(0xFFFFFFFF, tab);
1156 tab->fe[tab->index].ctrl = 0x20;
1157 tab->fe[tab->index].prop = 0x0;
1158 tab->index++;
1159 }
1160
1161 /* Remove last AND */
1162 tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
1163
1164 /* Specify which queue to use or to drop */
1165 if (rule->ring_cookie == RX_CLS_FLOW_DISC)
1166 tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
1167 else
1168 tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
1169
1170 /* Only big enough entries can be clustered */
1171 if (tab->index > (old_index + 2)) {
1172 tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
1173 tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
1174 }
1175
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001176 /* In rare cases the cache can be full while there is
1177 * free space in hw
1178 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001179 if (tab->index > MAX_FILER_CACHE_IDX - 1)
1180 return -EBUSY;
1181
1182 return 0;
1183}
1184
1185/* Copy size filer entries */
1186static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001187 struct gfar_filer_entry src[0], s32 size)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001188{
1189 while (size > 0) {
1190 size--;
1191 dst[size].ctrl = src[size].ctrl;
1192 dst[size].prop = src[size].prop;
1193 }
1194}
1195
1196/* Delete the contents of the filer-table between start and end
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001197 * and collapse them
1198 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001199static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
1200{
1201 int length;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001202
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001203 if (end > MAX_FILER_CACHE_IDX || end < begin)
1204 return -EINVAL;
1205
1206 end++;
1207 length = end - begin;
1208
1209 /* Copy */
1210 while (end < tab->index) {
1211 tab->fe[begin].ctrl = tab->fe[end].ctrl;
1212 tab->fe[begin++].prop = tab->fe[end++].prop;
1213
1214 }
1215 /* Fill up with don't cares */
1216 while (begin < tab->index) {
1217 tab->fe[begin].ctrl = 0x60;
1218 tab->fe[begin].prop = 0xFFFFFFFF;
1219 begin++;
1220 }
1221
1222 tab->index -= length;
1223 return 0;
1224}
1225
1226/* Make space on the wanted location */
1227static int gfar_expand_filer_entries(u32 begin, u32 length,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001228 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001229{
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001230 if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX ||
1231 begin > MAX_FILER_CACHE_IDX)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001232 return -EINVAL;
1233
1234 gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001235 tab->index - length + 1);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001236
1237 tab->index += length;
1238 return 0;
1239}
1240
1241static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
1242{
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001243 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
1244 start++) {
1245 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
1246 (RQFCR_AND | RQFCR_CLE))
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001247 return start;
1248 }
1249 return -1;
1250}
1251
1252static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
1253{
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001254 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
1255 start++) {
1256 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
1257 (RQFCR_CLE))
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001258 return start;
1259 }
1260 return -1;
1261}
1262
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001263/* Uses hardwares clustering option to reduce
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001264 * the number of filer table entries
1265 */
1266static void gfar_cluster_filer(struct filer_table *tab)
1267{
1268 s32 i = -1, j, iend, jend;
1269
1270 while ((i = gfar_get_next_cluster_start(++i, tab)) != -1) {
1271 j = i;
1272 while ((j = gfar_get_next_cluster_start(++j, tab)) != -1) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001273 /* The cluster entries self and the previous one
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001274 * (a mask) must be identical!
1275 */
1276 if (tab->fe[i].ctrl != tab->fe[j].ctrl)
1277 break;
1278 if (tab->fe[i].prop != tab->fe[j].prop)
1279 break;
1280 if (tab->fe[i - 1].ctrl != tab->fe[j - 1].ctrl)
1281 break;
1282 if (tab->fe[i - 1].prop != tab->fe[j - 1].prop)
1283 break;
1284 iend = gfar_get_next_cluster_end(i, tab);
1285 jend = gfar_get_next_cluster_end(j, tab);
1286 if (jend == -1 || iend == -1)
1287 break;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001288
1289 /* First we make some free space, where our cluster
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001290 * element should be. Then we copy it there and finally
1291 * delete in from its old location.
1292 */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001293 if (gfar_expand_filer_entries(iend, (jend - j), tab) ==
1294 -EINVAL)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001295 break;
1296
1297 gfar_copy_filer_entries(&(tab->fe[iend + 1]),
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001298 &(tab->fe[jend + 1]), jend - j);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001299
1300 if (gfar_trim_filer_entries(jend - 1,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001301 jend + (jend - j),
1302 tab) == -EINVAL)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001303 return;
1304
1305 /* Mask out cluster bit */
1306 tab->fe[iend].ctrl &= ~(RQFCR_CLE);
1307 }
1308 }
1309}
1310
Sebastian Poehn380b1532011-07-07 04:30:29 -07001311/* Swaps the masked bits of a1<>a2 and b1<>b2 */
1312static void gfar_swap_bits(struct gfar_filer_entry *a1,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001313 struct gfar_filer_entry *a2,
1314 struct gfar_filer_entry *b1,
1315 struct gfar_filer_entry *b2, u32 mask)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001316{
1317 u32 temp[4];
Sebastian Poehn380b1532011-07-07 04:30:29 -07001318 temp[0] = a1->ctrl & mask;
1319 temp[1] = a2->ctrl & mask;
1320 temp[2] = b1->ctrl & mask;
1321 temp[3] = b2->ctrl & mask;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001322
Sebastian Poehn380b1532011-07-07 04:30:29 -07001323 a1->ctrl &= ~mask;
1324 a2->ctrl &= ~mask;
1325 b1->ctrl &= ~mask;
1326 b2->ctrl &= ~mask;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001327
1328 a1->ctrl |= temp[1];
1329 a2->ctrl |= temp[0];
1330 b1->ctrl |= temp[3];
1331 b2->ctrl |= temp[2];
1332}
1333
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001334/* Generate a list consisting of masks values with their start and
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001335 * end of validity and block as indicator for parts belonging
1336 * together (glued by ANDs) in mask_table
1337 */
1338static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001339 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001340{
1341 u32 i, and_index = 0, block_index = 1;
1342
1343 for (i = 0; i < tab->index; i++) {
1344
1345 /* LSByte of control = 0 sets a mask */
1346 if (!(tab->fe[i].ctrl & 0xF)) {
1347 mask_table[and_index].mask = tab->fe[i].prop;
1348 mask_table[and_index].start = i;
1349 mask_table[and_index].block = block_index;
1350 if (and_index >= 1)
1351 mask_table[and_index - 1].end = i - 1;
1352 and_index++;
1353 }
Sebastian Poehn380b1532011-07-07 04:30:29 -07001354 /* cluster starts and ends will be separated because they should
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001355 * hold their position
1356 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001357 if (tab->fe[i].ctrl & RQFCR_CLE)
1358 block_index++;
1359 /* A not set AND indicates the end of a depended block */
1360 if (!(tab->fe[i].ctrl & RQFCR_AND))
1361 block_index++;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001362 }
1363
1364 mask_table[and_index - 1].end = i - 1;
1365
1366 return and_index;
1367}
1368
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001369/* Sorts the entries of mask_table by the values of the masks.
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001370 * Important: The 0xFF80 flags of the first and last entry of a
1371 * block must hold their position (which queue, CLusterEnable, ReJEct,
1372 * AND)
1373 */
1374static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001375 struct filer_table *temp_table, u32 and_index)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001376{
1377 /* Pointer to compare function (_asc or _desc) */
1378 int (*gfar_comp)(const void *, const void *);
1379
1380 u32 i, size = 0, start = 0, prev = 1;
1381 u32 old_first, old_last, new_first, new_last;
1382
1383 gfar_comp = &gfar_comp_desc;
1384
1385 for (i = 0; i < and_index; i++) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001386 if (prev != mask_table[i].block) {
1387 old_first = mask_table[start].start + 1;
1388 old_last = mask_table[i - 1].end;
1389 sort(mask_table + start, size,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001390 sizeof(struct gfar_mask_entry),
1391 gfar_comp, &gfar_swap);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001392
1393 /* Toggle order for every block. This makes the
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001394 * thing more efficient!
1395 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001396 if (gfar_comp == gfar_comp_desc)
1397 gfar_comp = &gfar_comp_asc;
1398 else
1399 gfar_comp = &gfar_comp_desc;
1400
1401 new_first = mask_table[start].start + 1;
1402 new_last = mask_table[i - 1].end;
1403
Sebastian Poehn380b1532011-07-07 04:30:29 -07001404 gfar_swap_bits(&temp_table->fe[new_first],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001405 &temp_table->fe[old_first],
1406 &temp_table->fe[new_last],
1407 &temp_table->fe[old_last],
1408 RQFCR_QUEUE | RQFCR_CLE |
1409 RQFCR_RJE | RQFCR_AND);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001410
1411 start = i;
1412 size = 0;
1413 }
1414 size++;
1415 prev = mask_table[i].block;
1416 }
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001417}
1418
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001419/* Reduces the number of masks needed in the filer table to save entries
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001420 * This is done by sorting the masks of a depended block. A depended block is
1421 * identified by gluing ANDs or CLE. The sorting order toggles after every
1422 * block. Of course entries in scope of a mask must change their location with
1423 * it.
1424 */
1425static int gfar_optimize_filer_masks(struct filer_table *tab)
1426{
1427 struct filer_table *temp_table;
1428 struct gfar_mask_entry *mask_table;
1429
1430 u32 and_index = 0, previous_mask = 0, i = 0, j = 0, size = 0;
1431 s32 ret = 0;
1432
1433 /* We need a copy of the filer table because
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001434 * we want to change its order
1435 */
Thomas Meyerb8ffdbd2011-11-17 13:05:35 +00001436 temp_table = kmemdup(tab, sizeof(*temp_table), GFP_KERNEL);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001437 if (temp_table == NULL)
1438 return -ENOMEM;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001439
1440 mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001441 sizeof(struct gfar_mask_entry), GFP_KERNEL);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001442
1443 if (mask_table == NULL) {
1444 ret = -ENOMEM;
1445 goto end;
1446 }
1447
1448 and_index = gfar_generate_mask_table(mask_table, tab);
1449
1450 gfar_sort_mask_table(mask_table, temp_table, and_index);
1451
1452 /* Now we can copy the data from our duplicated filer table to
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001453 * the real one in the order the mask table says
1454 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001455 for (i = 0; i < and_index; i++) {
1456 size = mask_table[i].end - mask_table[i].start + 1;
1457 gfar_copy_filer_entries(&(tab->fe[j]),
1458 &(temp_table->fe[mask_table[i].start]), size);
1459 j += size;
1460 }
1461
1462 /* And finally we just have to check for duplicated masks and drop the
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001463 * second ones
1464 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001465 for (i = 0; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1466 if (tab->fe[i].ctrl == 0x80) {
1467 previous_mask = i++;
1468 break;
1469 }
1470 }
1471 for (; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1472 if (tab->fe[i].ctrl == 0x80) {
1473 if (tab->fe[i].prop == tab->fe[previous_mask].prop) {
1474 /* Two identical ones found!
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001475 * So drop the second one!
1476 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001477 gfar_trim_filer_entries(i, i, tab);
1478 } else
1479 /* Not identical! */
1480 previous_mask = i;
1481 }
1482 }
1483
1484 kfree(mask_table);
1485end: kfree(temp_table);
1486 return ret;
1487}
1488
1489/* Write the bit-pattern from software's buffer to hardware registers */
1490static int gfar_write_filer_table(struct gfar_private *priv,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001491 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001492{
1493 u32 i = 0;
1494 if (tab->index > MAX_FILER_IDX - 1)
1495 return -EBUSY;
1496
1497 /* Avoid inconsistent filer table to be processed */
1498 lock_rx_qs(priv);
1499
1500 /* Fill regular entries */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001501 for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].ctrl);
1502 i++)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001503 gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
1504 /* Fill the rest with fall-troughs */
1505 for (; i < MAX_FILER_IDX - 1; i++)
1506 gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
1507 /* Last entry must be default accept
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001508 * because that's what people expect
1509 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001510 gfar_write_filer(priv, i, 0x20, 0x0);
1511
1512 unlock_rx_qs(priv);
1513
1514 return 0;
1515}
1516
1517static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001518 struct gfar_private *priv)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001519{
1520
1521 if (flow->flow_type & FLOW_EXT) {
1522 if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
1523 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001524 "User-specific data not supported!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001525 if (~flow->m_ext.vlan_etype)
1526 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001527 "VLAN-etype not supported!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001528 }
1529 if (flow->flow_type == IP_USER_FLOW)
1530 if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
1531 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001532 "IP-Version differing from IPv4 not supported!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001533
1534 return 0;
1535}
1536
1537static int gfar_process_filer_changes(struct gfar_private *priv)
1538{
1539 struct ethtool_flow_spec_container *j;
1540 struct filer_table *tab;
1541 s32 i = 0;
1542 s32 ret = 0;
1543
1544 /* So index is set to zero, too! */
1545 tab = kzalloc(sizeof(*tab), GFP_KERNEL);
1546 if (tab == NULL)
1547 return -ENOMEM;
1548
1549 /* Now convert the existing filer data from flow_spec into
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001550 * filer tables binary format
1551 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001552 list_for_each_entry(j, &priv->rx_list.list, list) {
1553 ret = gfar_convert_to_filer(&j->fs, tab);
1554 if (ret == -EBUSY) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001555 netdev_err(priv->ndev,
1556 "Rule not added: No free space!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001557 goto end;
1558 }
1559 if (ret == -1) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001560 netdev_err(priv->ndev,
1561 "Rule not added: Unsupported Flow-type!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001562 goto end;
1563 }
1564 }
1565
1566 i = tab->index;
1567
1568 /* Optimizations to save entries */
1569 gfar_cluster_filer(tab);
1570 gfar_optimize_filer_masks(tab);
1571
1572 pr_debug("\n\tSummary:\n"
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001573 "\tData on hardware: %d\n"
1574 "\tCompression rate: %d%%\n",
1575 tab->index, 100 - (100 * tab->index) / i);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001576
1577 /* Write everything to hardware */
1578 ret = gfar_write_filer_table(priv, tab);
1579 if (ret == -EBUSY) {
1580 netdev_err(priv->ndev, "Rule not added: No free space!\n");
1581 goto end;
1582 }
1583
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001584end:
1585 kfree(tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001586 return ret;
1587}
1588
1589static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
1590{
1591 u32 i = 0;
1592
1593 for (i = 0; i < sizeof(flow->m_u); i++)
1594 flow->m_u.hdata[i] ^= 0xFF;
1595
1596 flow->m_ext.vlan_etype ^= 0xFFFF;
1597 flow->m_ext.vlan_tci ^= 0xFFFF;
1598 flow->m_ext.data[0] ^= ~0;
1599 flow->m_ext.data[1] ^= ~0;
1600}
1601
1602static int gfar_add_cls(struct gfar_private *priv,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001603 struct ethtool_rx_flow_spec *flow)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001604{
1605 struct ethtool_flow_spec_container *temp, *comp;
1606 int ret = 0;
1607
1608 temp = kmalloc(sizeof(*temp), GFP_KERNEL);
1609 if (temp == NULL)
1610 return -ENOMEM;
1611 memcpy(&temp->fs, flow, sizeof(temp->fs));
1612
1613 gfar_invert_masks(&temp->fs);
1614 ret = gfar_check_capability(&temp->fs, priv);
1615 if (ret)
1616 goto clean_mem;
1617 /* Link in the new element at the right @location */
1618 if (list_empty(&priv->rx_list.list)) {
1619 ret = gfar_check_filer_hardware(priv);
1620 if (ret != 0)
1621 goto clean_mem;
1622 list_add(&temp->list, &priv->rx_list.list);
1623 goto process;
1624 } else {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001625 list_for_each_entry(comp, &priv->rx_list.list, list) {
1626 if (comp->fs.location > flow->location) {
1627 list_add_tail(&temp->list, &comp->list);
1628 goto process;
1629 }
1630 if (comp->fs.location == flow->location) {
1631 netdev_err(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001632 "Rule not added: ID %d not free!\n",
1633 flow->location);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001634 ret = -EBUSY;
1635 goto clean_mem;
1636 }
1637 }
1638 list_add_tail(&temp->list, &priv->rx_list.list);
1639 }
1640
1641process:
1642 ret = gfar_process_filer_changes(priv);
1643 if (ret)
1644 goto clean_list;
1645 priv->rx_list.count++;
1646 return ret;
1647
1648clean_list:
1649 list_del(&temp->list);
1650clean_mem:
1651 kfree(temp);
1652 return ret;
1653}
1654
1655static int gfar_del_cls(struct gfar_private *priv, u32 loc)
1656{
1657 struct ethtool_flow_spec_container *comp;
1658 u32 ret = -EINVAL;
1659
1660 if (list_empty(&priv->rx_list.list))
1661 return ret;
1662
1663 list_for_each_entry(comp, &priv->rx_list.list, list) {
1664 if (comp->fs.location == loc) {
1665 list_del(&comp->list);
1666 kfree(comp);
1667 priv->rx_list.count--;
1668 gfar_process_filer_changes(priv);
1669 ret = 0;
1670 break;
1671 }
1672 }
1673
1674 return ret;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001675}
1676
1677static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
1678{
1679 struct ethtool_flow_spec_container *comp;
1680 u32 ret = -EINVAL;
1681
1682 list_for_each_entry(comp, &priv->rx_list.list, list) {
1683 if (comp->fs.location == cmd->fs.location) {
1684 memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
1685 gfar_invert_masks(&cmd->fs);
1686 ret = 0;
1687 break;
1688 }
1689 }
1690
1691 return ret;
1692}
1693
1694static int gfar_get_cls_all(struct gfar_private *priv,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001695 struct ethtool_rxnfc *cmd, u32 *rule_locs)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001696{
1697 struct ethtool_flow_spec_container *comp;
1698 u32 i = 0;
1699
1700 list_for_each_entry(comp, &priv->rx_list.list, list) {
David S. Miller8decf862011-09-22 03:23:13 -04001701 if (i == cmd->rule_cnt)
1702 return -EMSGSIZE;
1703 rule_locs[i] = comp->fs.location;
1704 i++;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001705 }
1706
1707 cmd->data = MAX_FILER_IDX;
Ben Hutchings473e64e2011-09-06 13:52:47 +00001708 cmd->rule_cnt = i;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001709
1710 return 0;
1711}
1712
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001713static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1714{
1715 struct gfar_private *priv = netdev_priv(dev);
1716 int ret = 0;
1717
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001718 mutex_lock(&priv->rx_queue_access);
1719
1720 switch (cmd->cmd) {
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001721 case ETHTOOL_SRXFH:
1722 ret = gfar_set_hash_opts(priv, cmd);
1723 break;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001724 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a73e492012-01-03 11:59:30 +00001725 if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
1726 cmd->fs.ring_cookie >= priv->num_rx_queues) ||
1727 cmd->fs.location >= MAX_FILER_IDX) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001728 ret = -EINVAL;
1729 break;
1730 }
1731 ret = gfar_add_cls(priv, &cmd->fs);
1732 break;
1733 case ETHTOOL_SRXCLSRLDEL:
1734 ret = gfar_del_cls(priv, cmd->fs.location);
1735 break;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001736 default:
1737 ret = -EINVAL;
1738 }
1739
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001740 mutex_unlock(&priv->rx_queue_access);
1741
1742 return ret;
1743}
1744
1745static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001746 u32 *rule_locs)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001747{
1748 struct gfar_private *priv = netdev_priv(dev);
1749 int ret = 0;
1750
1751 switch (cmd->cmd) {
1752 case ETHTOOL_GRXRINGS:
1753 cmd->data = priv->num_rx_queues;
1754 break;
1755 case ETHTOOL_GRXCLSRLCNT:
1756 cmd->rule_cnt = priv->rx_list.count;
1757 break;
1758 case ETHTOOL_GRXCLSRULE:
1759 ret = gfar_get_cls(priv, cmd);
1760 break;
1761 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings815c7db2011-09-06 13:49:12 +00001762 ret = gfar_get_cls_all(priv, cmd, rule_locs);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001763 break;
1764 default:
1765 ret = -EINVAL;
1766 break;
1767 }
1768
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001769 return ret;
1770}
1771
Richard Cochran66636282012-04-03 22:59:19 +00001772int gfar_phc_index = -1;
Richard Cochran28889b72012-09-20 19:11:12 +00001773EXPORT_SYMBOL(gfar_phc_index);
Richard Cochran66636282012-04-03 22:59:19 +00001774
1775static int gfar_get_ts_info(struct net_device *dev,
1776 struct ethtool_ts_info *info)
1777{
1778 struct gfar_private *priv = netdev_priv(dev);
1779
1780 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001781 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
1782 SOF_TIMESTAMPING_SOFTWARE;
Richard Cochran66636282012-04-03 22:59:19 +00001783 info->phc_index = -1;
1784 return 0;
1785 }
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001786 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
1787 SOF_TIMESTAMPING_RX_HARDWARE |
1788 SOF_TIMESTAMPING_RAW_HARDWARE;
Richard Cochran66636282012-04-03 22:59:19 +00001789 info->phc_index = gfar_phc_index;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001790 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1791 (1 << HWTSTAMP_TX_ON);
1792 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1793 (1 << HWTSTAMP_FILTER_ALL);
Richard Cochran66636282012-04-03 22:59:19 +00001794 return 0;
1795}
1796
Jeff Garzik7282d492006-09-13 14:30:00 -04001797const struct ethtool_ops gfar_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 .get_settings = gfar_gsettings,
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001799 .set_settings = gfar_ssettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 .get_drvinfo = gfar_gdrvinfo,
1801 .get_regs_len = gfar_reglen,
1802 .get_regs = gfar_get_regs,
1803 .get_link = ethtool_op_get_link,
1804 .get_coalesce = gfar_gcoalesce,
1805 .set_coalesce = gfar_scoalesce,
1806 .get_ringparam = gfar_gringparam,
1807 .set_ringparam = gfar_sringparam,
1808 .get_strings = gfar_gstrings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001809 .get_sset_count = gfar_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 .get_ethtool_stats = gfar_fill_stats,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001811 .get_msglevel = gfar_get_msglevel,
1812 .set_msglevel = gfar_set_msglevel,
Scott Woodd87eb122008-07-11 18:04:45 -05001813#ifdef CONFIG_PM
1814 .get_wol = gfar_get_wol,
1815 .set_wol = gfar_set_wol,
1816#endif
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001817 .set_rxnfc = gfar_set_nfc,
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001818 .get_rxnfc = gfar_get_nfc,
Richard Cochran66636282012-04-03 22:59:19 +00001819 .get_ts_info = gfar_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820};