blob: 012fa4e79ffabb4afde6aa40748226517d6b7290 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
David S. Miller65a85a82012-04-06 00:35:34 -040028#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/crc32.h>
38#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/ethtool.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040040#include <linux/mii.h>
41#include <linux/phy.h>
Sebastian Poehn4aa3a712011-06-20 13:57:59 -070042#include <linux/sort.h>
Sebastian Poehn380b1532011-07-07 04:30:29 -070043#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include "gianfar.h"
46
Andy Flemingbb40dcb2005-09-23 22:54:21 -040047#define GFAR_MAX_COAL_USECS 0xffff
48#define GFAR_MAX_COAL_FRAMES 0xff
Kumar Gala0bbaf062005-06-20 10:54:21 -050049static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +000050 u64 *buf);
Kumar Gala0bbaf062005-06-20 10:54:21 -050051static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +000052static int gfar_gcoalesce(struct net_device *dev,
53 struct ethtool_coalesce *cvals);
54static int gfar_scoalesce(struct net_device *dev,
55 struct ethtool_coalesce *cvals);
56static void gfar_gringparam(struct net_device *dev,
57 struct ethtool_ringparam *rvals);
58static int gfar_sringparam(struct net_device *dev,
59 struct ethtool_ringparam *rvals);
60static void gfar_gdrvinfo(struct net_device *dev,
61 struct ethtool_drvinfo *drvinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Paul Gortmaker30f7e312012-01-08 13:21:57 -050063static const char stat_gstrings[][ETH_GSTRING_LEN] = {
Claudiu Manoil76f31e82015-07-13 16:22:03 +030064 /* extra stats */
65 "rx-allocation-errors",
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 "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",
Claudiu Manoil76f31e82015-07-13 16:22:03 +030079 /* rmon stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 "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
Kumar Gala0bbaf062005-06-20 10:54:21 -0500126/* Fill in a buffer with the strings which correspond to the
127 * stats */
128static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
129{
130 struct gfar_private *priv = netdev_priv(dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600131
Andy Flemingb31a1d82008-12-16 15:29:15 -0800132 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500133 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
134 else
135 memcpy(buf, stat_gstrings,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000136 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/* 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 */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000143static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
144 u64 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int i;
147 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000148 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Paul Gortmaker212079d2013-02-12 15:38:19 -0500149 atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Paul Gortmaker68719782013-02-12 15:28:35 -0500151 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
Paul Gortmaker212079d2013-02-12 15:38:19 -0500152 buf[i] = atomic64_read(&extra[i]);
Paul Gortmaker68719782013-02-12 15:28:35 -0500153
Andy Flemingb31a1d82008-12-16 15:29:15 -0800154 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000155 u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Paul Gortmaker68719782013-02-12 15:28:35 -0500157 for (; i < GFAR_STATS_LEN; i++, rmon++)
158 buf[i] = (u64) gfar_read(rmon);
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700162static int gfar_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700166 switch (sset) {
167 case ETH_SS_STATS:
Andy Flemingb31a1d82008-12-16 15:29:15 -0800168 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700169 return GFAR_STATS_LEN;
170 else
171 return GFAR_EXTRA_STATS_LEN;
172 default:
173 return -EOPNOTSUPP;
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/* Fills in the drvinfo structure with some basic info */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000178static void gfar_gdrvinfo(struct net_device *dev,
179 struct ethtool_drvinfo *drvinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Jiri Pirko7826d432013-01-06 00:44:26 +0000181 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 drvinfo->regdump_len = 0;
187 drvinfo->eedump_len = 0;
188}
189
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400190
191static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* Return the current settings in the ethtool_cmd structure */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500204static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400207 struct phy_device *phydev = priv->phydev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000208 struct gfar_priv_rx_q *rx_queue = NULL;
209 struct gfar_priv_tx_q *tx_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400211 if (NULL == phydev)
212 return -ENODEV;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000213 tx_queue = priv->tx_queue[0];
214 rx_queue = priv->rx_queue[0];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400215
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000216 /* etsec-1.7 and older versions have only one txic
217 * and rxic regs although they support multiple queues */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000218 cmd->maxtxpkt = get_icft_value(tx_queue->txic);
219 cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400221 return phy_ethtool_gset(phydev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/* Return the length of the register structure */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500225static int gfar_reglen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 return sizeof (struct gfar);
228}
229
230/* Return a dump of the GFAR register space */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000231static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
232 void *regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 int i;
235 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000236 u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 u32 *buf = (u32 *) regbuf;
238
239 for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
Kumar Galacc8c6e32006-02-01 15:18:03 -0600240 buf[i] = gfar_read(&theregs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/* Convert microseconds to ethernet clock ticks, which changes
244 * depending on what speed the controller is running at */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000245static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
246 unsigned int usecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 unsigned int count;
249
250 /* The timer is different, depending on the interface speed */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400251 switch (priv->phydev->speed) {
252 case SPEED_1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 count = GFAR_GBIT_TIME;
254 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400255 case SPEED_100:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 count = GFAR_100_TIME;
257 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400258 case SPEED_10:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 default:
260 count = GFAR_10_TIME;
261 break;
262 }
263
264 /* Make sure we return a number greater than 0
265 * if usecs > 0 */
Eric Dumazet807540b2010-09-23 05:40:09 +0000266 return (usecs * 1000 + count - 1) / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269/* Convert ethernet clock ticks to microseconds */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000270static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
271 unsigned int ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 unsigned int count;
274
275 /* The timer is different, depending on the interface speed */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400276 switch (priv->phydev->speed) {
277 case SPEED_1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 count = GFAR_GBIT_TIME;
279 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400280 case SPEED_100:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 count = GFAR_100_TIME;
282 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400283 case SPEED_10:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 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 */
Eric Dumazet807540b2010-09-23 05:40:09 +0000291 return (ticks * count) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/* Get the coalescing parameters, and put them in the cvals
295 * structure. */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000296static int gfar_gcoalesce(struct net_device *dev,
297 struct ethtool_coalesce *cvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000300 struct gfar_priv_rx_q *rx_queue = NULL;
301 struct gfar_priv_tx_q *tx_queue = NULL;
Dai Harukib46a8452008-12-16 15:29:52 -0800302 unsigned long rxtime;
303 unsigned long rxcount;
304 unsigned long txtime;
305 unsigned long txcount;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400306
Andy Flemingb31a1d82008-12-16 15:29:15 -0800307 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
Kumar Gala0bbaf062005-06-20 10:54:21 -0500308 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400310 if (NULL == priv->phydev)
311 return -ENODEV;
312
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000313 rx_queue = priv->rx_queue[0];
314 tx_queue = priv->tx_queue[0];
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000315
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);
Dai Harukib46a8452008-12-16 15:29:52 -0800320 cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
321 cvals->rx_max_coalesced_frames = rxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Dai Harukib46a8452008-12-16 15:29:52 -0800323 cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
324 cvals->tx_max_coalesced_frames = txcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
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 */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000362static int gfar_scoalesce(struct net_device *dev,
363 struct ethtool_coalesce *cvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct gfar_private *priv = netdev_priv(dev);
Claudiu Manoilf19015b2014-02-24 12:13:46 +0200366 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Andy Flemingb31a1d82008-12-16 15:29:15 -0800368 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
Kumar Gala0bbaf062005-06-20 10:54:21 -0500369 return -EOPNOTSUPP;
370
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400371 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) {
Joe Perches375d6a12013-04-13 19:03:18 +0000376 netdev_info(dev, "Coalescing is limited to %d microseconds\n",
377 GFAR_MAX_COAL_USECS);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400378 return -EINVAL;
379 }
380
381 if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
Joe Perches375d6a12013-04-13 19:03:18 +0000382 netdev_info(dev, "Coalescing is limited to %d frames\n",
383 GFAR_MAX_COAL_FRAMES);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400384 return -EINVAL;
385 }
386
Claudiu Manoilf19015b2014-02-24 12:13:46 +0200387 /* 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
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000413 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /* Set up tx coalescing */
420 if ((cvals->tx_coalesce_usecs == 0) ||
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000421 (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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000429 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Claudiu Manoilf19015b2014-02-24 12:13:46 +0200435 if (dev->flags & IFF_UP) {
436 stop_gfar(dev);
437 err = startup_gfar(dev);
438 } else {
439 gfar_mac_reset(priv);
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Claudiu Manoilf19015b2014-02-24 12:13:46 +0200442 clear_bit_unlock(GFAR_RESETTING, &priv->state);
443
444 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
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 */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000450static void gfar_gringparam(struct net_device *dev,
451 struct ethtool_ringparam *rvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000454 struct gfar_priv_tx_q *tx_queue = NULL;
455 struct gfar_priv_rx_q *rx_queue = NULL;
456
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000457 tx_queue = priv->tx_queue[0];
458 rx_queue = priv->rx_queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
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 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000468 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474/* Change the current ring parameters, stopping the controller if
Claudiu Manoil7cca3362014-02-17 12:53:19 +0200475 * necessary so that we don't mess things up while we're in motion.
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000476 */
477static int gfar_sringparam(struct net_device *dev,
478 struct ethtool_ringparam *rvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct gfar_private *priv = netdev_priv(dev);
Claudiu Manoil7cca3362014-02-17 12:53:19 +0200481 int err = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
484 return -EINVAL;
485
486 if (!is_power_of_2(rvals->rx_pending)) {
Joe Perches59deab22011-06-14 08:57:47 +0000487 netdev_err(dev, "Ring sizes must be a power of 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 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)) {
Joe Perches59deab22011-06-14 08:57:47 +0000495 netdev_err(dev, "Ring sizes must be a power of 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return -EINVAL;
497 }
498
Claudiu Manoil08511332014-02-24 12:13:45 +0200499 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
500 cpu_relax();
501
Claudiu Manoil7cca3362014-02-17 12:53:19 +0200502 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 stop_gfar(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Claudiu Manoil7cca3362014-02-17 12:53:19 +0200505 /* Change the sizes */
506 for (i = 0; i < priv->num_rx_queues; i++)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000507 priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
Claudiu Manoil7cca3362014-02-17 12:53:19 +0200508
509 for (i = 0; i < priv->num_tx_queues; i++)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000510 priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Kumar Gala0bbaf062005-06-20 10:54:21 -0500512 /* Rebuild the rings with the new size */
Claudiu Manoil08511332014-02-24 12:13:45 +0200513 if (dev->flags & IFF_UP)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500514 err = startup_gfar(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +0200515
516 clear_bit_unlock(GFAR_RESETTING, &priv->state);
517
Kumar Gala0bbaf062005-06-20 10:54:21 -0500518 return err;
519}
520
Claudiu Manoil23402bd2013-08-12 13:53:26 +0300521static 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
531static 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
Claudiu Manoil98a46d42014-04-23 16:38:47 +0300539 if (!phydev)
540 return -ENODEV;
541
Claudiu Manoil23402bd2013-08-12 13:53:26 +0300542 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);
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200585
586 priv->tx_actual_en = 0;
587 if (priv->tx_pause_en) {
588 priv->tx_actual_en = 1;
Claudiu Manoil23402bd2013-08-12 13:53:26 +0300589 tempval |= MACCFG1_TX_FLOW;
Matei Pavaluca45b679c92014-10-27 10:42:44 +0200590 }
591
Claudiu Manoil23402bd2013-08-12 13:53:26 +0300592 if (priv->rx_pause_en)
593 tempval |= MACCFG1_RX_FLOW;
594 gfar_write(&regs->maccfg1, tempval);
595 }
596 }
597
598 return 0;
599}
600
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000601int gfar_set_features(struct net_device *dev, netdev_features_t features)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500602{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000603 netdev_features_t changed = dev->features ^ features;
Claudiu Manoil08511332014-02-24 12:13:45 +0200604 struct gfar_private *priv = netdev_priv(dev);
Claudiu Manoil7cca3362014-02-17 12:53:19 +0200605 int err = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500606
Claudiu Manoil88302642014-02-24 12:13:43 +0200607 if (!(changed & (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
608 NETIF_F_RXCSUM)))
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000609 return 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000610
Claudiu Manoil08511332014-02-24 12:13:45 +0200611 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
612 cpu_relax();
613
Claudiu Manoil88302642014-02-24 12:13:43 +0200614 dev->features = features;
615
Kumar Gala0bbaf062005-06-20 10:54:21 -0500616 if (dev->flags & IFF_UP) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500617 /* Now we take down the rings to rebuild them */
618 stop_gfar(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 err = startup_gfar(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +0200620 } else {
621 gfar_mac_reset(priv);
Dai Haruki12dea572008-12-16 15:30:20 -0800622 }
Claudiu Manoil08511332014-02-24 12:13:45 +0200623
624 clear_bit_unlock(GFAR_RESETTING, &priv->state);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return err;
627}
628
Kumar Gala0bbaf062005-06-20 10:54:21 -0500629static uint32_t gfar_get_msglevel(struct net_device *dev)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400630{
Kumar Gala0bbaf062005-06-20 10:54:21 -0500631 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000632
Kumar Gala0bbaf062005-06-20 10:54:21 -0500633 return priv->msg_enable;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400634}
635
Kumar Gala0bbaf062005-06-20 10:54:21 -0500636static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400637{
Kumar Gala0bbaf062005-06-20 10:54:21 -0500638 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000639
Kumar Gala0bbaf062005-06-20 10:54:21 -0500640 priv->msg_enable = data;
641}
642
Scott Woodd87eb122008-07-11 18:04:45 -0500643#ifdef CONFIG_PM
644static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
645{
646 struct gfar_private *priv = netdev_priv(dev);
647
Andy Flemingb31a1d82008-12-16 15:29:15 -0800648 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
Scott Woodd87eb122008-07-11 18:04:45 -0500649 wol->supported = WAKE_MAGIC;
650 wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
651 } else {
652 wol->supported = wol->wolopts = 0;
653 }
654}
655
656static 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
Andy Flemingb31a1d82008-12-16 15:29:15 -0800661 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -0500662 wol->wolopts != 0)
663 return -EINVAL;
664
665 if (wol->wolopts & ~WAKE_MAGIC)
666 return -EINVAL;
667
Rafael J. Wysocki6c4f1992010-11-09 11:54:19 +0000668 device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);
669
Scott Woodd87eb122008-07-11 18:04:45 -0500670 spin_lock_irqsave(&priv->bflock, flags);
Rafael J. Wysocki6c4f1992010-11-09 11:54:19 +0000671 priv->wol_en = !!device_may_wakeup(&dev->dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500672 spin_unlock_irqrestore(&priv->bflock, flags);
673
674 return 0;
675}
676#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500677
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000678static 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 |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000684 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000685 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
686 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000687 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 |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000691 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000692 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
693 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000694 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 |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000700 RQFCR_AND | RQFCR_HASHTBL_0;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000701 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000702 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
703 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000704 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 |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000709 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000710 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
711 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000712 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 |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000718 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000719 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
720 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000721 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 |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000727 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000728 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
729 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000730 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 |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000736 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000737 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
738 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000739 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 |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000745 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000746 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
747 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000748 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
749 priv->cur_filer_idx = priv->cur_filer_idx - 1;
750 }
751}
752
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000753static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
754 u64 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000755{
756 unsigned int last_rule_idx = priv->cur_filer_idx;
757 unsigned int cmp_rqfpr;
Wang Shaoyan588dc912011-08-11 17:07:25 +0000758 unsigned int *local_rqfpr;
759 unsigned int *local_rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000760 int i = 0x0, k = 0x0;
761 int j = MAX_FILER_IDX, l = 0x0;
Wang Shaoyan588dc912011-08-11 17:07:25 +0000762 int ret = 1;
763
Joe Perchesb2adaca2013-02-03 17:43:58 +0000764 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);
Wang Shaoyan588dc912011-08-11 17:07:25 +0000768 if (!local_rqfpr || !local_rqfcr) {
Wang Shaoyan588dc912011-08-11 17:07:25 +0000769 ret = 0;
770 goto err;
771 }
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000772
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;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000786 default:
Joe Perches375d6a12013-04-13 19:03:18 +0000787 netdev_err(priv->ndev,
788 "Right now this class is not supported\n");
Wang Shaoyan588dc912011-08-11 17:07:25 +0000789 ret = 0;
790 goto err;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000791 }
792
793 for (i = 0; i < MAX_FILER_IDX + 1; i++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000794 local_rqfpr[j] = priv->ftp_rqfpr[i];
795 local_rqfcr[j] = priv->ftp_rqfcr[i];
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000796 j--;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000797 if ((priv->ftp_rqfcr[i] ==
798 (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
799 (priv->ftp_rqfpr[i] == cmp_rqfpr))
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000800 break;
801 }
802
803 if (i == MAX_FILER_IDX + 1) {
Joe Perches375d6a12013-04-13 19:03:18 +0000804 netdev_err(priv->ndev,
805 "No parse rule found, can't create hash rules\n");
Wang Shaoyan588dc912011-08-11 17:07:25 +0000806 ret = 0;
807 goto err;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000808 }
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++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000814 if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000815 !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000816 priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000817 RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000818 priv->ftp_rqfpr[l] = FPR_FILER_MASK;
819 gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000820 priv->ftp_rqfpr[l]);
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000821 break;
822 }
823
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000824 if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
825 (priv->ftp_rqfcr[l] & RQFCR_AND))
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000826 continue;
827 else {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000828 local_rqfpr[j] = priv->ftp_rqfpr[l];
829 local_rqfcr[j] = priv->ftp_rqfcr[l];
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000830 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++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000842 priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
843 priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000844 gfar_write_filer(priv, priv->cur_filer_idx,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000845 local_rqfcr[k], local_rqfpr[k]);
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000846 if (!priv->cur_filer_idx)
847 break;
848 priv->cur_filer_idx = priv->cur_filer_idx - 1;
849 }
850
Wang Shaoyan588dc912011-08-11 17:07:25 +0000851err:
852 kfree(local_rqfcr);
853 kfree(local_rqfpr);
854 return ret;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000855}
856
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000857static int gfar_set_hash_opts(struct gfar_private *priv,
858 struct ethtool_rxnfc *cmd)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000859{
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000860 /* write the filer rules here */
861 if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
Ben Hutchingsbde35282011-04-08 13:45:11 +0000862 return -EINVAL;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000863
864 return 0;
865}
866
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700867static int gfar_check_filer_hardware(struct gfar_private *priv)
868{
Claudiu Manoil42851e82014-01-14 15:35:00 +0200869 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700870 u32 i;
871
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700872 /* 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,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000881 "Receive Queue Filtering enabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700882 } else {
883 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000884 "Receive Queue Filtering disabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700885 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,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000894 "Receive Queue Filtering enabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700895 } else {
896 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000897 "Receive Queue Filtering disabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700898 return -EOPNOTSUPP;
899 }
900 }
901
902 /* Sets the properties for arbitrary filer rule
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000903 * to the first 4 Layer 4 Bytes
904 */
Claudiu Manoil42851e82014-01-14 15:35:00 +0200905 gfar_write(&regs->rbifx, 0xC0C1C2C3);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700906 return 0;
907}
908
909static int gfar_comp_asc(const void *a, const void *b)
910{
911 return memcmp(a, b, 4);
912}
913
914static int gfar_comp_desc(const void *a, const void *b)
915{
916 return -memcmp(a, b, 4);
917}
918
919static 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 */
931static 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) */
939static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
940{
941 gfar_set_mask(mask, tab);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000942 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
943 RQFCR_AND;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700944 tab->fe[tab->index].prop = value;
945 tab->index++;
946}
947
948static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000949 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700950{
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
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000957/* For setting a tuple of value and mask of type flag
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700958 * 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 */
971static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000972 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700973{
974 switch (flag) {
Sebastian Poehn380b1532011-07-07 04:30:29 -0700975 /* 3bit */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700976 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 */
1029static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001030 struct ethtool_tcpip4_spec *mask,
1031 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001032{
Claudiu Manoil42851e82014-01-14 15:35:00 +02001033 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);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001045 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1046}
1047
1048/* Translates value and mask for RAW-IP4 */
1049static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001050 struct ethtool_usrip4_spec *mask,
1051 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001052{
Claudiu Manoil42851e82014-01-14 15:35:00 +02001053 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);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001059 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1060 gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
Claudiu Manoil42851e82014-01-14 15:35:00 +02001061 gfar_set_attribute(be32_to_cpu(value->l4_4_bytes),
1062 be32_to_cpu(mask->l4_4_bytes),
1063 RQFCR_PID_ARB, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001064
1065}
1066
1067/* Translates value and mask for ETHER spec */
1068static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001069 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001070{
1071 u32 upper_temp_mask = 0;
1072 u32 lower_temp_mask = 0;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001073
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001074 /* Source address */
1075 if (!is_broadcast_ether_addr(mask->h_source)) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001076 if (is_zero_ether_addr(mask->h_source)) {
1077 upper_temp_mask = 0xFFFFFFFF;
1078 lower_temp_mask = 0xFFFFFFFF;
1079 } else {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001080 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];
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001086 }
1087 /* Upper 24bit */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001088 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);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001092 /* And the same for the lower part */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001093 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);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001097 }
1098 /* Destination address */
1099 if (!is_broadcast_ether_addr(mask->h_dest)) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001100 /* Special for destination is limited broadcast */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001101 if ((is_broadcast_ether_addr(value->h_dest) &&
1102 is_zero_ether_addr(mask->h_dest))) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001103 gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
1104 } else {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001105 if (is_zero_ether_addr(mask->h_dest)) {
1106 upper_temp_mask = 0xFFFFFFFF;
1107 lower_temp_mask = 0xFFFFFFFF;
1108 } else {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001109 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];
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001115 }
1116
1117 /* Upper 24bit */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001118 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);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001122 /* And the same for the lower part */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001123 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);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001127 }
1128 }
1129
Claudiu Manoil42851e82014-01-14 15:35:00 +02001130 gfar_set_attribute(be16_to_cpu(value->h_proto),
1131 be16_to_cpu(mask->h_proto),
1132 RQFCR_PID_ETY, tab);
1133}
1134
1135static 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
1140static 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
1145static 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
1150static 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
1155static 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
1161static 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;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001165}
1166
1167/* Convert a rule to binary filter format of gianfar */
1168static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001169 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001170{
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;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001175 u32 old_index = tab->index;
1176
1177 /* Check if vlan is wanted */
Claudiu Manoil42851e82014-01-14 15:35:00 +02001178 if ((rule->flow_type & FLOW_EXT) &&
1179 (rule->m_ext.vlan_tci != cpu_to_be16(0xFFFF))) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001180 if (!rule->m_ext.vlan_tci)
Claudiu Manoil42851e82014-01-14 15:35:00 +02001181 rule->m_ext.vlan_tci = cpu_to_be16(0xFFFF);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001182
1183 vlan = RQFPR_VLN;
1184 vlan_mask = RQFPR_VLN;
1185
1186 /* Separate the fields */
Claudiu Manoil42851e82014-01-14 15:35:00 +02001187 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);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001193
Sebastian Poehn380b1532011-07-07 04:30:29 -07001194 if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001195 vlan |= RQFPR_CFI;
1196 vlan_mask |= RQFPR_CFI;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001197 } else if (cfi != VLAN_TAG_PRESENT &&
1198 cfi_mask == VLAN_TAG_PRESENT) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001199 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,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001206 RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001207 gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001208 &rule->m_u.tcp_ip4_spec, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001209 break;
1210 case UDP_V4_FLOW:
1211 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001212 RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001213 gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001214 &rule->m_u.udp_ip4_spec, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001215 break;
1216 case SCTP_V4_FLOW:
1217 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001218 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001219 gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001220 gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
1221 (struct ethtool_tcpip4_spec *)&rule->m_u,
1222 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001223 break;
1224 case IP_USER_FLOW:
1225 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001226 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001227 gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001228 (struct ethtool_usrip4_spec *) &rule->m_u,
1229 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001230 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,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001235 (struct ethhdr *) &rule->m_u, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001236 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
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001270 /* In rare cases the cache can be full while there is
1271 * free space in hw
1272 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001273 if (tab->index > MAX_FILER_CACHE_IDX - 1)
1274 return -EBUSY;
1275
1276 return 0;
1277}
1278
1279/* Copy size filer entries */
1280static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001281 struct gfar_filer_entry src[0], s32 size)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001282{
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
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001291 * and collapse them
1292 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001293static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
1294{
1295 int length;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001296
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001297 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 */
1321static int gfar_expand_filer_entries(u32 begin, u32 length,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001322 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001323{
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001324 if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX ||
1325 begin > MAX_FILER_CACHE_IDX)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001326 return -EINVAL;
1327
1328 gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001329 tab->index - length + 1);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001330
1331 tab->index += length;
1332 return 0;
1333}
1334
1335static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
1336{
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001337 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))
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001341 return start;
1342 }
1343 return -1;
1344}
1345
1346static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
1347{
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001348 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))
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001352 return start;
1353 }
1354 return -1;
1355}
1356
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001357/* Uses hardwares clustering option to reduce
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001358 * the number of filer table entries
1359 */
1360static 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) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001367 /* The cluster entries self and the previous one
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001368 * (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;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001382
1383 /* First we make some free space, where our cluster
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001384 * element should be. Then we copy it there and finally
1385 * delete in from its old location.
1386 */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001387 if (gfar_expand_filer_entries(iend, (jend - j), tab) ==
1388 -EINVAL)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001389 break;
1390
1391 gfar_copy_filer_entries(&(tab->fe[iend + 1]),
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001392 &(tab->fe[jend + 1]), jend - j);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001393
1394 if (gfar_trim_filer_entries(jend - 1,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001395 jend + (jend - j),
1396 tab) == -EINVAL)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001397 return;
1398
1399 /* Mask out cluster bit */
1400 tab->fe[iend].ctrl &= ~(RQFCR_CLE);
1401 }
1402 }
1403}
1404
Sebastian Poehn380b1532011-07-07 04:30:29 -07001405/* Swaps the masked bits of a1<>a2 and b1<>b2 */
1406static void gfar_swap_bits(struct gfar_filer_entry *a1,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001407 struct gfar_filer_entry *a2,
1408 struct gfar_filer_entry *b1,
1409 struct gfar_filer_entry *b2, u32 mask)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001410{
1411 u32 temp[4];
Sebastian Poehn380b1532011-07-07 04:30:29 -07001412 temp[0] = a1->ctrl & mask;
1413 temp[1] = a2->ctrl & mask;
1414 temp[2] = b1->ctrl & mask;
1415 temp[3] = b2->ctrl & mask;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001416
Sebastian Poehn380b1532011-07-07 04:30:29 -07001417 a1->ctrl &= ~mask;
1418 a2->ctrl &= ~mask;
1419 b1->ctrl &= ~mask;
1420 b2->ctrl &= ~mask;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001421
1422 a1->ctrl |= temp[1];
1423 a2->ctrl |= temp[0];
1424 b1->ctrl |= temp[3];
1425 b2->ctrl |= temp[2];
1426}
1427
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001428/* Generate a list consisting of masks values with their start and
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001429 * end of validity and block as indicator for parts belonging
1430 * together (glued by ANDs) in mask_table
1431 */
1432static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001433 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001434{
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 }
Sebastian Poehn380b1532011-07-07 04:30:29 -07001448 /* cluster starts and ends will be separated because they should
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001449 * hold their position
1450 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001451 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++;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001456 }
1457
1458 mask_table[and_index - 1].end = i - 1;
1459
1460 return and_index;
1461}
1462
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001463/* Sorts the entries of mask_table by the values of the masks.
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001464 * 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 */
1468static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001469 struct filer_table *temp_table, u32 and_index)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001470{
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++) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001480 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,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001484 sizeof(struct gfar_mask_entry),
1485 gfar_comp, &gfar_swap);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001486
1487 /* Toggle order for every block. This makes the
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001488 * thing more efficient!
1489 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001490 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
Sebastian Poehn380b1532011-07-07 04:30:29 -07001498 gfar_swap_bits(&temp_table->fe[new_first],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001499 &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);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001504
1505 start = i;
1506 size = 0;
1507 }
1508 size++;
1509 prev = mask_table[i].block;
1510 }
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001511}
1512
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001513/* Reduces the number of masks needed in the filer table to save entries
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001514 * 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 */
1519static 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
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001528 * we want to change its order
1529 */
Thomas Meyerb8ffdbd2011-11-17 13:05:35 +00001530 temp_table = kmemdup(tab, sizeof(*temp_table), GFP_KERNEL);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001531 if (temp_table == NULL)
1532 return -ENOMEM;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001533
1534 mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001535 sizeof(struct gfar_mask_entry), GFP_KERNEL);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001536
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
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001547 * the real one in the order the mask table says
1548 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001549 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
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001557 * second ones
1558 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001559 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!
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001569 * So drop the second one!
1570 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001571 gfar_trim_filer_entries(i, i, tab);
1572 } else
1573 /* Not identical! */
1574 previous_mask = i;
1575 }
1576 }
1577
1578 kfree(mask_table);
1579end: kfree(temp_table);
1580 return ret;
1581}
1582
1583/* Write the bit-pattern from software's buffer to hardware registers */
1584static int gfar_write_filer_table(struct gfar_private *priv,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001585 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001586{
1587 u32 i = 0;
1588 if (tab->index > MAX_FILER_IDX - 1)
1589 return -EBUSY;
1590
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001591 /* Fill regular entries */
Sanjeev Sharma75300ad2015-02-03 13:02:02 +05301592 for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].prop);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001593 i++)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001594 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
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001599 * because that's what people expect
1600 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001601 gfar_write_filer(priv, i, 0x20, 0x0);
1602
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001603 return 0;
1604}
1605
1606static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001607 struct gfar_private *priv)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001608{
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,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001613 "User-specific data not supported!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001614 if (~flow->m_ext.vlan_etype)
1615 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001616 "VLAN-etype not supported!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001617 }
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,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001621 "IP-Version differing from IPv4 not supported!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001622
1623 return 0;
1624}
1625
1626static 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
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001639 * filer tables binary format
1640 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001641 list_for_each_entry(j, &priv->rx_list.list, list) {
1642 ret = gfar_convert_to_filer(&j->fs, tab);
1643 if (ret == -EBUSY) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001644 netdev_err(priv->ndev,
1645 "Rule not added: No free space!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001646 goto end;
1647 }
1648 if (ret == -1) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001649 netdev_err(priv->ndev,
1650 "Rule not added: Unsupported Flow-type!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001651 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
Joe Perches375d6a12013-04-13 19:03:18 +00001661 pr_debug("\tSummary:\n"
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001662 "\tData on hardware: %d\n"
1663 "\tCompression rate: %d%%\n",
1664 tab->index, 100 - (100 * tab->index) / i);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001665
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
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001673end:
1674 kfree(tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001675 return ret;
1676}
1677
1678static 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
Claudiu Manoil42851e82014-01-14 15:35:00 +02001685 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);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001689}
1690
1691static int gfar_add_cls(struct gfar_private *priv,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001692 struct ethtool_rx_flow_spec *flow)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001693{
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 {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001714 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,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001721 "Rule not added: ID %d not free!\n",
1722 flow->location);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001723 ret = -EBUSY;
1724 goto clean_mem;
1725 }
1726 }
1727 list_add_tail(&temp->list, &priv->rx_list.list);
1728 }
1729
1730process:
1731 ret = gfar_process_filer_changes(priv);
1732 if (ret)
1733 goto clean_list;
1734 priv->rx_list.count++;
1735 return ret;
1736
1737clean_list:
1738 list_del(&temp->list);
1739clean_mem:
1740 kfree(temp);
1741 return ret;
1742}
1743
1744static 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;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001764}
1765
1766static 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
1783static int gfar_get_cls_all(struct gfar_private *priv,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001784 struct ethtool_rxnfc *cmd, u32 *rule_locs)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001785{
1786 struct ethtool_flow_spec_container *comp;
1787 u32 i = 0;
1788
1789 list_for_each_entry(comp, &priv->rx_list.list, list) {
David S. Miller8decf862011-09-22 03:23:13 -04001790 if (i == cmd->rule_cnt)
1791 return -EMSGSIZE;
1792 rule_locs[i] = comp->fs.location;
1793 i++;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001794 }
1795
1796 cmd->data = MAX_FILER_IDX;
Ben Hutchings473e64e2011-09-06 13:52:47 +00001797 cmd->rule_cnt = i;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001798
1799 return 0;
1800}
1801
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001802static 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
Claudiu Manoil08511332014-02-24 12:13:45 +02001807 if (test_bit(GFAR_RESETTING, &priv->state))
1808 return -EBUSY;
1809
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001810 mutex_lock(&priv->rx_queue_access);
1811
1812 switch (cmd->cmd) {
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001813 case ETHTOOL_SRXFH:
1814 ret = gfar_set_hash_opts(priv, cmd);
1815 break;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001816 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a73e492012-01-03 11:59:30 +00001817 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) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001820 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;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001828 default:
1829 ret = -EINVAL;
1830 }
1831
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001832 mutex_unlock(&priv->rx_queue_access);
1833
1834 return ret;
1835}
1836
1837static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001838 u32 *rule_locs)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001839{
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:
Ben Hutchings815c7db2011-09-06 13:49:12 +00001854 ret = gfar_get_cls_all(priv, cmd, rule_locs);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001855 break;
1856 default:
1857 ret = -EINVAL;
1858 break;
1859 }
1860
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001861 return ret;
1862}
1863
Richard Cochran66636282012-04-03 22:59:19 +00001864int gfar_phc_index = -1;
Richard Cochran28889b72012-09-20 19:11:12 +00001865EXPORT_SYMBOL(gfar_phc_index);
Richard Cochran66636282012-04-03 22:59:19 +00001866
1867static 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)) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001873 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
1874 SOF_TIMESTAMPING_SOFTWARE;
Richard Cochran66636282012-04-03 22:59:19 +00001875 info->phc_index = -1;
1876 return 0;
1877 }
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001878 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
1879 SOF_TIMESTAMPING_RX_HARDWARE |
1880 SOF_TIMESTAMPING_RAW_HARDWARE;
Richard Cochran66636282012-04-03 22:59:19 +00001881 info->phc_index = gfar_phc_index;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001882 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);
Richard Cochran66636282012-04-03 22:59:19 +00001886 return 0;
1887}
1888
Jeff Garzik7282d492006-09-13 14:30:00 -04001889const struct ethtool_ops gfar_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 .get_settings = gfar_gsettings,
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001891 .set_settings = gfar_ssettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 .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,
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001900 .get_pauseparam = gfar_gpauseparam,
1901 .set_pauseparam = gfar_spauseparam,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 .get_strings = gfar_gstrings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001903 .get_sset_count = gfar_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 .get_ethtool_stats = gfar_fill_stats,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001905 .get_msglevel = gfar_get_msglevel,
1906 .set_msglevel = gfar_set_msglevel,
Scott Woodd87eb122008-07-11 18:04:45 -05001907#ifdef CONFIG_PM
1908 .get_wol = gfar_get_wol,
1909 .set_wol = gfar_set_wol,
1910#endif
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001911 .set_rxnfc = gfar_set_nfc,
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001912 .get_rxnfc = gfar_get_nfc,
Richard Cochran66636282012-04-03 22:59:19 +00001913 .get_ts_info = gfar_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914};