blob: 05103362bebef36daa87167ed634964a79a83453 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/net/gianfar_ethtool.c
3 *
4 * Gianfar Ethernet Driver
5 * Ethtool support for Gianfar Enet
6 * Based on e1000 ethtool support
7 *
8 * Author: Andy Fleming
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 *
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000012 * Copyright 2003-2006, 2008-2009 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>
29#include <linux/skbuff.h>
30#include <linux/spinlock.h>
31#include <linux/mm.h>
32
33#include <asm/io.h>
34#include <asm/irq.h>
35#include <asm/uaccess.h>
36#include <linux/module.h>
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include "gianfar.h"
45
Kumar Gala0bbaf062005-06-20 10:54:21 -050046extern void gfar_start(struct net_device *dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000047extern int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Flemingbb40dcb2005-09-23 22:54:21 -040049#define GFAR_MAX_COAL_USECS 0xffff
50#define GFAR_MAX_COAL_FRAMES 0xff
Kumar Gala0bbaf062005-06-20 10:54:21 -050051static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 u64 * buf);
Kumar Gala0bbaf062005-06-20 10:54:21 -050053static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
54static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
55static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
56static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
57static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
58static void gfar_gdrvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60static char stat_gstrings[][ETH_GSTRING_LEN] = {
61 "rx-dropped-by-kernel",
62 "rx-large-frame-errors",
63 "rx-short-frame-errors",
64 "rx-non-octet-errors",
65 "rx-crc-errors",
66 "rx-overrun-errors",
67 "rx-busy-errors",
68 "rx-babbling-errors",
69 "rx-truncated-frames",
70 "ethernet-bus-error",
71 "tx-babbling-errors",
72 "tx-underrun-errors",
73 "rx-skb-missing-errors",
74 "tx-timeout-errors",
75 "tx-rx-64-frames",
76 "tx-rx-65-127-frames",
77 "tx-rx-128-255-frames",
78 "tx-rx-256-511-frames",
79 "tx-rx-512-1023-frames",
80 "tx-rx-1024-1518-frames",
81 "tx-rx-1519-1522-good-vlan",
82 "rx-bytes",
83 "rx-packets",
84 "rx-fcs-errors",
85 "receive-multicast-packet",
86 "receive-broadcast-packet",
87 "rx-control-frame-packets",
88 "rx-pause-frame-packets",
89 "rx-unknown-op-code",
90 "rx-alignment-error",
91 "rx-frame-length-error",
92 "rx-code-error",
93 "rx-carrier-sense-error",
94 "rx-undersize-packets",
95 "rx-oversize-packets",
96 "rx-fragmented-frames",
97 "rx-jabber-frames",
98 "rx-dropped-frames",
99 "tx-byte-counter",
100 "tx-packets",
101 "tx-multicast-packets",
102 "tx-broadcast-packets",
103 "tx-pause-control-frames",
104 "tx-deferral-packets",
105 "tx-excessive-deferral-packets",
106 "tx-single-collision-packets",
107 "tx-multiple-collision-packets",
108 "tx-late-collision-packets",
109 "tx-excessive-collision-packets",
110 "tx-total-collision",
111 "reserved",
112 "tx-dropped-frames",
113 "tx-jabber-frames",
114 "tx-fcs-errors",
115 "tx-control-frames",
116 "tx-oversize-frames",
117 "tx-undersize-frames",
118 "tx-fragmented-frames",
119};
120
Kumar Gala0bbaf062005-06-20 10:54:21 -0500121/* Fill in a buffer with the strings which correspond to the
122 * stats */
123static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
124{
125 struct gfar_private *priv = netdev_priv(dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600126
Andy Flemingb31a1d82008-12-16 15:29:15 -0800127 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500128 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
129 else
130 memcpy(buf, stat_gstrings,
131 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/* Fill in an array of 64-bit statistics from various sources.
135 * This array will be appended to the end of the ethtool_stats
136 * structure, and returned to user space
137 */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500138static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy, u64 * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 int i;
141 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000142 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 u64 *extra = (u64 *) & priv->extra_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Andy Flemingb31a1d82008-12-16 15:29:15 -0800145 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000146 u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500147 struct gfar_stats *stats = (struct gfar_stats *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Kumar Gala0bbaf062005-06-20 10:54:21 -0500149 for (i = 0; i < GFAR_RMON_LEN; i++)
Kumar Galacc8c6e32006-02-01 15:18:03 -0600150 stats->rmon[i] = (u64) gfar_read(&rmon[i]);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500151
152 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
153 stats->extra[i] = extra[i];
154 } else
155 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
156 buf[i] = extra[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700159static int gfar_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700163 switch (sset) {
164 case ETH_SS_STATS:
Andy Flemingb31a1d82008-12-16 15:29:15 -0800165 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700166 return GFAR_STATS_LEN;
167 else
168 return GFAR_EXTRA_STATS_LEN;
169 default:
170 return -EOPNOTSUPP;
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/* Fills in the drvinfo structure with some basic info */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500175static void gfar_gdrvinfo(struct net_device *dev, struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 ethtool_drvinfo *drvinfo)
177{
178 strncpy(drvinfo->driver, DRV_NAME, GFAR_INFOSTR_LEN);
179 strncpy(drvinfo->version, gfar_driver_version, GFAR_INFOSTR_LEN);
180 strncpy(drvinfo->fw_version, "N/A", GFAR_INFOSTR_LEN);
181 strncpy(drvinfo->bus_info, "N/A", GFAR_INFOSTR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 drvinfo->regdump_len = 0;
183 drvinfo->eedump_len = 0;
184}
185
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400186
187static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
188{
189 struct gfar_private *priv = netdev_priv(dev);
190 struct phy_device *phydev = priv->phydev;
191
192 if (NULL == phydev)
193 return -ENODEV;
194
195 return phy_ethtool_sset(phydev, cmd);
196}
197
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* Return the current settings in the ethtool_cmd structure */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500200static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400203 struct phy_device *phydev = priv->phydev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000204 struct gfar_priv_rx_q *rx_queue = NULL;
205 struct gfar_priv_tx_q *tx_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400207 if (NULL == phydev)
208 return -ENODEV;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000209 tx_queue = priv->tx_queue[0];
210 rx_queue = priv->rx_queue[0];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400211
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000212 /* etsec-1.7 and older versions have only one txic
213 * and rxic regs although they support multiple queues */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000214 cmd->maxtxpkt = get_icft_value(tx_queue->txic);
215 cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400217 return phy_ethtool_gset(phydev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220/* Return the length of the register structure */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500221static int gfar_reglen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 return sizeof (struct gfar);
224}
225
226/* Return a dump of the GFAR register space */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500227static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 int i;
230 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000231 u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 u32 *buf = (u32 *) regbuf;
233
234 for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
Kumar Galacc8c6e32006-02-01 15:18:03 -0600235 buf[i] = gfar_read(&theregs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/* Convert microseconds to ethernet clock ticks, which changes
239 * depending on what speed the controller is running at */
240static unsigned int gfar_usecs2ticks(struct gfar_private *priv, unsigned int usecs)
241{
242 unsigned int count;
243
244 /* The timer is different, depending on the interface speed */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400245 switch (priv->phydev->speed) {
246 case SPEED_1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 count = GFAR_GBIT_TIME;
248 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400249 case SPEED_100:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 count = GFAR_100_TIME;
251 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400252 case SPEED_10:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 default:
254 count = GFAR_10_TIME;
255 break;
256 }
257
258 /* Make sure we return a number greater than 0
259 * if usecs > 0 */
Eric Dumazet807540b2010-09-23 05:40:09 +0000260 return (usecs * 1000 + count - 1) / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/* Convert ethernet clock ticks to microseconds */
264static unsigned int gfar_ticks2usecs(struct gfar_private *priv, unsigned int ticks)
265{
266 unsigned int count;
267
268 /* The timer is different, depending on the interface speed */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400269 switch (priv->phydev->speed) {
270 case SPEED_1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 count = GFAR_GBIT_TIME;
272 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400273 case SPEED_100:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 count = GFAR_100_TIME;
275 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400276 case SPEED_10:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 default:
278 count = GFAR_10_TIME;
279 break;
280 }
281
282 /* Make sure we return a number greater than 0 */
283 /* if ticks is > 0 */
Eric Dumazet807540b2010-09-23 05:40:09 +0000284 return (ticks * count) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/* Get the coalescing parameters, and put them in the cvals
288 * structure. */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500289static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000292 struct gfar_priv_rx_q *rx_queue = NULL;
293 struct gfar_priv_tx_q *tx_queue = NULL;
Dai Harukib46a8452008-12-16 15:29:52 -0800294 unsigned long rxtime;
295 unsigned long rxcount;
296 unsigned long txtime;
297 unsigned long txcount;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400298
Andy Flemingb31a1d82008-12-16 15:29:15 -0800299 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
Kumar Gala0bbaf062005-06-20 10:54:21 -0500300 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400302 if (NULL == priv->phydev)
303 return -ENODEV;
304
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000305 rx_queue = priv->rx_queue[0];
306 tx_queue = priv->tx_queue[0];
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000307
308 rxtime = get_ictt_value(rx_queue->rxic);
309 rxcount = get_icft_value(rx_queue->rxic);
310 txtime = get_ictt_value(tx_queue->txic);
311 txcount = get_icft_value(tx_queue->txic);
Dai Harukib46a8452008-12-16 15:29:52 -0800312 cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
313 cvals->rx_max_coalesced_frames = rxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Dai Harukib46a8452008-12-16 15:29:52 -0800315 cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
316 cvals->tx_max_coalesced_frames = txcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 cvals->use_adaptive_rx_coalesce = 0;
319 cvals->use_adaptive_tx_coalesce = 0;
320
321 cvals->pkt_rate_low = 0;
322 cvals->rx_coalesce_usecs_low = 0;
323 cvals->rx_max_coalesced_frames_low = 0;
324 cvals->tx_coalesce_usecs_low = 0;
325 cvals->tx_max_coalesced_frames_low = 0;
326
327 /* When the packet rate is below pkt_rate_high but above
328 * pkt_rate_low (both measured in packets per second) the
329 * normal {rx,tx}_* coalescing parameters are used.
330 */
331
332 /* When the packet rate is (measured in packets per second)
333 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
334 * used.
335 */
336 cvals->pkt_rate_high = 0;
337 cvals->rx_coalesce_usecs_high = 0;
338 cvals->rx_max_coalesced_frames_high = 0;
339 cvals->tx_coalesce_usecs_high = 0;
340 cvals->tx_max_coalesced_frames_high = 0;
341
342 /* How often to do adaptive coalescing packet rate sampling,
343 * measured in seconds. Must not be zero.
344 */
345 cvals->rate_sample_interval = 0;
346
347 return 0;
348}
349
350/* Change the coalescing values.
351 * Both cvals->*_usecs and cvals->*_frames have to be > 0
352 * in order for coalescing to be active
353 */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500354static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000357 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Andy Flemingb31a1d82008-12-16 15:29:15 -0800359 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
Kumar Gala0bbaf062005-06-20 10:54:21 -0500360 return -EOPNOTSUPP;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Set up rx coalescing */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000363 /* As of now, we will enable/disable coalescing for all
364 * queues together in case of eTSEC2, this will be modified
365 * along with the ethtool interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if ((cvals->rx_coalesce_usecs == 0) ||
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000367 (cvals->rx_max_coalesced_frames == 0)) {
368 for (i = 0; i < priv->num_rx_queues; i++)
369 priv->rx_queue[i]->rxcoalescing = 0;
370 } else {
371 for (i = 0; i < priv->num_rx_queues; i++)
372 priv->rx_queue[i]->rxcoalescing = 1;
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400375 if (NULL == priv->phydev)
376 return -ENODEV;
377
378 /* Check the bounds of the values */
379 if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
380 pr_info("Coalescing is limited to %d microseconds\n",
Joe Perches59deab22011-06-14 08:57:47 +0000381 GFAR_MAX_COAL_USECS);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400382 return -EINVAL;
383 }
384
385 if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
386 pr_info("Coalescing is limited to %d frames\n",
Joe Perches59deab22011-06-14 08:57:47 +0000387 GFAR_MAX_COAL_FRAMES);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400388 return -EINVAL;
389 }
390
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000391 for (i = 0; i < priv->num_rx_queues; i++) {
392 priv->rx_queue[i]->rxic = mk_ic_value(
393 cvals->rx_max_coalesced_frames,
394 gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* Set up tx coalescing */
398 if ((cvals->tx_coalesce_usecs == 0) ||
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000399 (cvals->tx_max_coalesced_frames == 0)) {
400 for (i = 0; i < priv->num_tx_queues; i++)
401 priv->tx_queue[i]->txcoalescing = 0;
402 } else {
403 for (i = 0; i < priv->num_tx_queues; i++)
404 priv->tx_queue[i]->txcoalescing = 1;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400407 /* Check the bounds of the values */
408 if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
409 pr_info("Coalescing is limited to %d microseconds\n",
Joe Perches59deab22011-06-14 08:57:47 +0000410 GFAR_MAX_COAL_USECS);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400411 return -EINVAL;
412 }
413
414 if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
415 pr_info("Coalescing is limited to %d frames\n",
Joe Perches59deab22011-06-14 08:57:47 +0000416 GFAR_MAX_COAL_FRAMES);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400417 return -EINVAL;
418 }
419
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000420 for (i = 0; i < priv->num_tx_queues; i++) {
421 priv->tx_queue[i]->txic = mk_ic_value(
422 cvals->tx_max_coalesced_frames,
423 gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000426 gfar_configure_coalescing(priv, 0xFF, 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 return 0;
429}
430
431/* Fills in rvals with the current ring parameters. Currently,
432 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
433 * jumbo are ignored by the driver */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500434static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000437 struct gfar_priv_tx_q *tx_queue = NULL;
438 struct gfar_priv_rx_q *rx_queue = NULL;
439
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000440 tx_queue = priv->tx_queue[0];
441 rx_queue = priv->rx_queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
444 rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
445 rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
446 rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
447
448 /* Values changeable by the user. The valid values are
449 * in the range 1 to the "*_max_pending" counterpart above.
450 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000451 rvals->rx_pending = rx_queue->rx_ring_size;
452 rvals->rx_mini_pending = rx_queue->rx_ring_size;
453 rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
454 rvals->tx_pending = tx_queue->tx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/* Change the current ring parameters, stopping the controller if
458 * necessary so that we don't mess things up while we're in
459 * motion. We wait for the ring to be clean before reallocating
460 * the rings. */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500461static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000464 int err = 0, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
467 return -EINVAL;
468
469 if (!is_power_of_2(rvals->rx_pending)) {
Joe Perches59deab22011-06-14 08:57:47 +0000470 netdev_err(dev, "Ring sizes must be a power of 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -EINVAL;
472 }
473
474 if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
475 return -EINVAL;
476
477 if (!is_power_of_2(rvals->tx_pending)) {
Joe Perches59deab22011-06-14 08:57:47 +0000478 netdev_err(dev, "Ring sizes must be a power of 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return -EINVAL;
480 }
481
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (dev->flags & IFF_UP) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500484 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Kumar Gala0bbaf062005-06-20 10:54:21 -0500486 /* Halt TX and RX, and process the frames which
487 * have already been received */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000488 local_irq_save(flags);
489 lock_tx_qs(priv);
490 lock_rx_qs(priv);
Andy Flemingfef61082006-04-20 16:44:29 -0500491
Kumar Gala0bbaf062005-06-20 10:54:21 -0500492 gfar_halt(dev);
Andy Flemingfef61082006-04-20 16:44:29 -0500493
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000494 unlock_rx_qs(priv);
495 unlock_tx_qs(priv);
496 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000498 for (i = 0; i < priv->num_rx_queues; i++)
499 gfar_clean_rx_ring(priv->rx_queue[i],
500 priv->rx_queue[i]->rx_ring_size);
Dai Haruki12dea572008-12-16 15:30:20 -0800501
Kumar Gala0bbaf062005-06-20 10:54:21 -0500502 /* Now we take down the rings to rebuild them */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 stop_gfar(dev);
504 }
505
Kumar Gala0bbaf062005-06-20 10:54:21 -0500506 /* Change the size */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000507 for (i = 0; i < priv->num_rx_queues; i++) {
508 priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
509 priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
510 priv->tx_queue[i]->num_txbdfree = priv->tx_queue[i]->tx_ring_size;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Kumar Gala0bbaf062005-06-20 10:54:21 -0500513 /* Rebuild the rings with the new size */
Dai Haruki12dea572008-12-16 15:30:20 -0800514 if (dev->flags & IFF_UP) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500515 err = startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000516 netif_tx_wake_all_queues(dev);
Dai Haruki12dea572008-12-16 15:30:20 -0800517 }
Kumar Gala0bbaf062005-06-20 10:54:21 -0500518 return err;
519}
520
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000521int gfar_set_features(struct net_device *dev, u32 features)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500522{
523 struct gfar_private *priv = netdev_priv(dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500524 unsigned long flags;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000525 int err = 0, i = 0;
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000526 u32 changed = dev->features ^ features;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500527
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000528 if (!(changed & NETIF_F_RXCSUM))
529 return 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000530
Kumar Gala0bbaf062005-06-20 10:54:21 -0500531 if (dev->flags & IFF_UP) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500532 /* Halt TX and RX, and process the frames which
533 * have already been received */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000534 local_irq_save(flags);
535 lock_tx_qs(priv);
536 lock_rx_qs(priv);
Andy Flemingfef61082006-04-20 16:44:29 -0500537
Kumar Gala0bbaf062005-06-20 10:54:21 -0500538 gfar_halt(dev);
Andy Flemingfef61082006-04-20 16:44:29 -0500539
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000540 unlock_tx_qs(priv);
541 unlock_rx_qs(priv);
Dan Carpenter97564032010-10-13 09:19:55 +0000542 local_irq_restore(flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500543
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000544 for (i = 0; i < priv->num_rx_queues; i++)
545 gfar_clean_rx_ring(priv->rx_queue[i],
546 priv->rx_queue[i]->rx_ring_size);
Dai Haruki12dea572008-12-16 15:30:20 -0800547
Kumar Gala0bbaf062005-06-20 10:54:21 -0500548 /* Now we take down the rings to rebuild them */
549 stop_gfar(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500550
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000551 dev->features = features;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 err = startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000554 netif_tx_wake_all_queues(dev);
Dai Haruki12dea572008-12-16 15:30:20 -0800555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return err;
557}
558
Kumar Gala0bbaf062005-06-20 10:54:21 -0500559static uint32_t gfar_get_msglevel(struct net_device *dev)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400560{
Kumar Gala0bbaf062005-06-20 10:54:21 -0500561 struct gfar_private *priv = netdev_priv(dev);
562 return priv->msg_enable;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400563}
564
Kumar Gala0bbaf062005-06-20 10:54:21 -0500565static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400566{
Kumar Gala0bbaf062005-06-20 10:54:21 -0500567 struct gfar_private *priv = netdev_priv(dev);
568 priv->msg_enable = data;
569}
570
Scott Woodd87eb122008-07-11 18:04:45 -0500571#ifdef CONFIG_PM
572static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
573{
574 struct gfar_private *priv = netdev_priv(dev);
575
Andy Flemingb31a1d82008-12-16 15:29:15 -0800576 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
Scott Woodd87eb122008-07-11 18:04:45 -0500577 wol->supported = WAKE_MAGIC;
578 wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
579 } else {
580 wol->supported = wol->wolopts = 0;
581 }
582}
583
584static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
585{
586 struct gfar_private *priv = netdev_priv(dev);
587 unsigned long flags;
588
Andy Flemingb31a1d82008-12-16 15:29:15 -0800589 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -0500590 wol->wolopts != 0)
591 return -EINVAL;
592
593 if (wol->wolopts & ~WAKE_MAGIC)
594 return -EINVAL;
595
Rafael J. Wysocki6c4f1992010-11-09 11:54:19 +0000596 device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);
597
Scott Woodd87eb122008-07-11 18:04:45 -0500598 spin_lock_irqsave(&priv->bflock, flags);
Rafael J. Wysocki6c4f1992010-11-09 11:54:19 +0000599 priv->wol_en = !!device_may_wakeup(&dev->dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500600 spin_unlock_irqrestore(&priv->bflock, flags);
601
602 return 0;
603}
604#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500605
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000606static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
607{
608 u32 fcr = 0x0, fpr = FPR_FILER_MASK;
609
610 if (ethflow & RXH_L2DA) {
611 fcr = RQFCR_PID_DAH |RQFCR_CMP_NOMATCH |
612 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
613 ftp_rqfpr[priv->cur_filer_idx] = fpr;
614 ftp_rqfcr[priv->cur_filer_idx] = fcr;
615 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
616 priv->cur_filer_idx = priv->cur_filer_idx - 1;
617
618 fcr = RQFCR_PID_DAL | RQFCR_AND | RQFCR_CMP_NOMATCH |
619 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
620 ftp_rqfpr[priv->cur_filer_idx] = fpr;
621 ftp_rqfcr[priv->cur_filer_idx] = fcr;
622 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
623 priv->cur_filer_idx = priv->cur_filer_idx - 1;
624 }
625
626 if (ethflow & RXH_VLAN) {
627 fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
628 RQFCR_AND | RQFCR_HASHTBL_0;
629 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
630 ftp_rqfpr[priv->cur_filer_idx] = fpr;
631 ftp_rqfcr[priv->cur_filer_idx] = fcr;
632 priv->cur_filer_idx = priv->cur_filer_idx - 1;
633 }
634
635 if (ethflow & RXH_IP_SRC) {
636 fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
637 RQFCR_AND | RQFCR_HASHTBL_0;
638 ftp_rqfpr[priv->cur_filer_idx] = fpr;
639 ftp_rqfcr[priv->cur_filer_idx] = fcr;
640 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
641 priv->cur_filer_idx = priv->cur_filer_idx - 1;
642 }
643
644 if (ethflow & (RXH_IP_DST)) {
645 fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
646 RQFCR_AND | RQFCR_HASHTBL_0;
647 ftp_rqfpr[priv->cur_filer_idx] = fpr;
648 ftp_rqfcr[priv->cur_filer_idx] = fcr;
649 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
650 priv->cur_filer_idx = priv->cur_filer_idx - 1;
651 }
652
653 if (ethflow & RXH_L3_PROTO) {
654 fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
655 RQFCR_AND | RQFCR_HASHTBL_0;
656 ftp_rqfpr[priv->cur_filer_idx] = fpr;
657 ftp_rqfcr[priv->cur_filer_idx] = fcr;
658 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
659 priv->cur_filer_idx = priv->cur_filer_idx - 1;
660 }
661
662 if (ethflow & RXH_L4_B_0_1) {
663 fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
664 RQFCR_AND | RQFCR_HASHTBL_0;
665 ftp_rqfpr[priv->cur_filer_idx] = fpr;
666 ftp_rqfcr[priv->cur_filer_idx] = fcr;
667 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
668 priv->cur_filer_idx = priv->cur_filer_idx - 1;
669 }
670
671 if (ethflow & RXH_L4_B_2_3) {
672 fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
673 RQFCR_AND | RQFCR_HASHTBL_0;
674 ftp_rqfpr[priv->cur_filer_idx] = fpr;
675 ftp_rqfcr[priv->cur_filer_idx] = fcr;
676 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
677 priv->cur_filer_idx = priv->cur_filer_idx - 1;
678 }
679}
680
681static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow, u64 class)
682{
683 unsigned int last_rule_idx = priv->cur_filer_idx;
684 unsigned int cmp_rqfpr;
685 unsigned int local_rqfpr[MAX_FILER_IDX + 1];
686 unsigned int local_rqfcr[MAX_FILER_IDX + 1];
687 int i = 0x0, k = 0x0;
688 int j = MAX_FILER_IDX, l = 0x0;
689
690 switch (class) {
691 case TCP_V4_FLOW:
692 cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
693 break;
694 case UDP_V4_FLOW:
695 cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
696 break;
697 case TCP_V6_FLOW:
698 cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
699 break;
700 case UDP_V6_FLOW:
701 cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
702 break;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000703 default:
Joe Perches59deab22011-06-14 08:57:47 +0000704 pr_err("Right now this class is not supported\n");
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000705 return 0;
706 }
707
708 for (i = 0; i < MAX_FILER_IDX + 1; i++) {
709 local_rqfpr[j] = ftp_rqfpr[i];
710 local_rqfcr[j] = ftp_rqfcr[i];
711 j--;
712 if ((ftp_rqfcr[i] == (RQFCR_PID_PARSE |
713 RQFCR_CLE |RQFCR_AND)) &&
714 (ftp_rqfpr[i] == cmp_rqfpr))
715 break;
716 }
717
718 if (i == MAX_FILER_IDX + 1) {
Joe Perches59deab22011-06-14 08:57:47 +0000719 pr_err("No parse rule found, can't create hash rules\n");
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000720 return 0;
721 }
722
723 /* If a match was found, then it begins the starting of a cluster rule
724 * if it was already programmed, we need to overwrite these rules
725 */
726 for (l = i+1; l < MAX_FILER_IDX; l++) {
727 if ((ftp_rqfcr[l] & RQFCR_CLE) &&
728 !(ftp_rqfcr[l] & RQFCR_AND)) {
729 ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
730 RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
731 ftp_rqfpr[l] = FPR_FILER_MASK;
732 gfar_write_filer(priv, l, ftp_rqfcr[l], ftp_rqfpr[l]);
733 break;
734 }
735
736 if (!(ftp_rqfcr[l] & RQFCR_CLE) && (ftp_rqfcr[l] & RQFCR_AND))
737 continue;
738 else {
739 local_rqfpr[j] = ftp_rqfpr[l];
740 local_rqfcr[j] = ftp_rqfcr[l];
741 j--;
742 }
743 }
744
745 priv->cur_filer_idx = l - 1;
746 last_rule_idx = l;
747
748 /* hash rules */
749 ethflow_to_filer_rules(priv, ethflow);
750
751 /* Write back the popped out rules again */
752 for (k = j+1; k < MAX_FILER_IDX; k++) {
753 ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
754 ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
755 gfar_write_filer(priv, priv->cur_filer_idx,
756 local_rqfcr[k], local_rqfpr[k]);
757 if (!priv->cur_filer_idx)
758 break;
759 priv->cur_filer_idx = priv->cur_filer_idx - 1;
760 }
761
762 return 1;
763}
764
765static int gfar_set_hash_opts(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
766{
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000767 /* write the filer rules here */
768 if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
Ben Hutchingsbde35282011-04-08 13:45:11 +0000769 return -EINVAL;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000770
771 return 0;
772}
773
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700774static int gfar_check_filer_hardware(struct gfar_private *priv)
775{
776 struct gfar __iomem *regs = NULL;
777 u32 i;
778
779 regs = priv->gfargrp[0].regs;
780
781 /* Check if we are in FIFO mode */
782 i = gfar_read(&regs->ecntrl);
783 i &= ECNTRL_FIFM;
784 if (i == ECNTRL_FIFM) {
785 netdev_notice(priv->ndev, "Interface in FIFO mode\n");
786 i = gfar_read(&regs->rctrl);
787 i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
788 if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
789 netdev_info(priv->ndev,
790 "Receive Queue Filtering enabled\n");
791 } else {
792 netdev_warn(priv->ndev,
793 "Receive Queue Filtering disabled\n");
794 return -EOPNOTSUPP;
795 }
796 }
797 /* Or in standard mode */
798 else {
799 i = gfar_read(&regs->rctrl);
800 i &= RCTRL_PRSDEP_MASK;
801 if (i == RCTRL_PRSDEP_MASK) {
802 netdev_info(priv->ndev,
803 "Receive Queue Filtering enabled\n");
804 } else {
805 netdev_warn(priv->ndev,
806 "Receive Queue Filtering disabled\n");
807 return -EOPNOTSUPP;
808 }
809 }
810
811 /* Sets the properties for arbitrary filer rule
812 * to the first 4 Layer 4 Bytes */
813 regs->rbifx = 0xC0C1C2C3;
814 return 0;
815}
816
817static int gfar_comp_asc(const void *a, const void *b)
818{
819 return memcmp(a, b, 4);
820}
821
822static int gfar_comp_desc(const void *a, const void *b)
823{
824 return -memcmp(a, b, 4);
825}
826
827static void gfar_swap(void *a, void *b, int size)
828{
829 u32 *_a = a;
830 u32 *_b = b;
831
832 swap(_a[0], _b[0]);
833 swap(_a[1], _b[1]);
834 swap(_a[2], _b[2]);
835 swap(_a[3], _b[3]);
836}
837
838/* Write a mask to filer cache */
839static void gfar_set_mask(u32 mask, struct filer_table *tab)
840{
841 tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
842 tab->fe[tab->index].prop = mask;
843 tab->index++;
844}
845
846/* Sets parse bits (e.g. IP or TCP) */
847static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
848{
849 gfar_set_mask(mask, tab);
850 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE
851 | RQFCR_AND;
852 tab->fe[tab->index].prop = value;
853 tab->index++;
854}
855
856static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
857 struct filer_table *tab)
858{
859 gfar_set_mask(mask, tab);
860 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
861 tab->fe[tab->index].prop = value;
862 tab->index++;
863}
864
865/*
866 * For setting a tuple of value and mask of type flag
867 * Example:
868 * IP-Src = 10.0.0.0/255.0.0.0
869 * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
870 *
871 * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
872 * For a don't care mask it gives us a 0
873 *
874 * The check if don't care and the mask adjustment if mask=0 is done for VLAN
875 * and MAC stuff on an upper level (due to missing information on this level).
876 * For these guys we can discard them if they are value=0 and mask=0.
877 *
878 * Further the all masks are one-padded for better hardware efficiency.
879 */
880static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
881 struct filer_table *tab)
882{
883 switch (flag) {
884 /* 3bit */
885 case RQFCR_PID_PRI:
886 if (!(value | mask))
887 return;
888 mask |= RQFCR_PID_PRI_MASK;
889 break;
890 /* 8bit */
891 case RQFCR_PID_L4P:
892 case RQFCR_PID_TOS:
893 if (!~(mask | RQFCR_PID_L4P_MASK))
894 return;
895 if (!mask)
896 mask = ~0;
897 else
898 mask |= RQFCR_PID_L4P_MASK;
899 break;
900 /* 12bit */
901 case RQFCR_PID_VID:
902 if (!(value | mask))
903 return;
904 mask |= RQFCR_PID_VID_MASK;
905 break;
906 /* 16bit */
907 case RQFCR_PID_DPT:
908 case RQFCR_PID_SPT:
909 case RQFCR_PID_ETY:
910 if (!~(mask | RQFCR_PID_PORT_MASK))
911 return;
912 if (!mask)
913 mask = ~0;
914 else
915 mask |= RQFCR_PID_PORT_MASK;
916 break;
917 /* 24bit */
918 case RQFCR_PID_DAH:
919 case RQFCR_PID_DAL:
920 case RQFCR_PID_SAH:
921 case RQFCR_PID_SAL:
922 if (!(value | mask))
923 return;
924 mask |= RQFCR_PID_MAC_MASK;
925 break;
926 /* for all real 32bit masks */
927 default:
928 if (!~mask)
929 return;
930 if (!mask)
931 mask = ~0;
932 break;
933 }
934 gfar_set_general_attribute(value, mask, flag, tab);
935}
936
937/* Translates value and mask for UDP, TCP or SCTP */
938static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
939 struct ethtool_tcpip4_spec *mask, struct filer_table *tab)
940{
941 gfar_set_attribute(value->ip4src, mask->ip4src, RQFCR_PID_SIA, tab);
942 gfar_set_attribute(value->ip4dst, mask->ip4dst, RQFCR_PID_DIA, tab);
943 gfar_set_attribute(value->pdst, mask->pdst, RQFCR_PID_DPT, tab);
944 gfar_set_attribute(value->psrc, mask->psrc, RQFCR_PID_SPT, tab);
945 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
946}
947
948/* Translates value and mask for RAW-IP4 */
949static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
950 struct ethtool_usrip4_spec *mask, struct filer_table *tab)
951{
952 gfar_set_attribute(value->ip4src, mask->ip4src, RQFCR_PID_SIA, tab);
953 gfar_set_attribute(value->ip4dst, mask->ip4dst, RQFCR_PID_DIA, tab);
954 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
955 gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
956 gfar_set_attribute(value->l4_4_bytes, mask->l4_4_bytes, RQFCR_PID_ARB,
957 tab);
958
959}
960
961/* Translates value and mask for ETHER spec */
962static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
963 struct filer_table *tab)
964{
965 u32 upper_temp_mask = 0;
966 u32 lower_temp_mask = 0;
967 /* Source address */
968 if (!is_broadcast_ether_addr(mask->h_source)) {
969
970 if (is_zero_ether_addr(mask->h_source)) {
971 upper_temp_mask = 0xFFFFFFFF;
972 lower_temp_mask = 0xFFFFFFFF;
973 } else {
974 upper_temp_mask = mask->h_source[0] << 16
975 | mask->h_source[1] << 8
976 | mask->h_source[2];
977 lower_temp_mask = mask->h_source[3] << 16
978 | mask->h_source[4] << 8
979 | mask->h_source[5];
980 }
981 /* Upper 24bit */
982 gfar_set_attribute(
983 value->h_source[0] << 16 | value->h_source[1]
984 << 8 | value->h_source[2],
985 upper_temp_mask, RQFCR_PID_SAH, tab);
986 /* And the same for the lower part */
987 gfar_set_attribute(
988 value->h_source[3] << 16 | value->h_source[4]
989 << 8 | value->h_source[5],
990 lower_temp_mask, RQFCR_PID_SAL, tab);
991 }
992 /* Destination address */
993 if (!is_broadcast_ether_addr(mask->h_dest)) {
994
995 /* Special for destination is limited broadcast */
996 if ((is_broadcast_ether_addr(value->h_dest)
997 && is_zero_ether_addr(mask->h_dest))) {
998 gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
999 } else {
1000
1001 if (is_zero_ether_addr(mask->h_dest)) {
1002 upper_temp_mask = 0xFFFFFFFF;
1003 lower_temp_mask = 0xFFFFFFFF;
1004 } else {
1005 upper_temp_mask = mask->h_dest[0] << 16
1006 | mask->h_dest[1] << 8
1007 | mask->h_dest[2];
1008 lower_temp_mask = mask->h_dest[3] << 16
1009 | mask->h_dest[4] << 8
1010 | mask->h_dest[5];
1011 }
1012
1013 /* Upper 24bit */
1014 gfar_set_attribute(
1015 value->h_dest[0] << 16
1016 | value->h_dest[1] << 8
1017 | value->h_dest[2],
1018 upper_temp_mask, RQFCR_PID_DAH, tab);
1019 /* And the same for the lower part */
1020 gfar_set_attribute(
1021 value->h_dest[3] << 16
1022 | value->h_dest[4] << 8
1023 | value->h_dest[5],
1024 lower_temp_mask, RQFCR_PID_DAL, tab);
1025 }
1026 }
1027
1028 gfar_set_attribute(value->h_proto, mask->h_proto, RQFCR_PID_ETY, tab);
1029
1030}
1031
1032/* Convert a rule to binary filter format of gianfar */
1033static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
1034 struct filer_table *tab)
1035{
1036 u32 vlan = 0, vlan_mask = 0;
1037 u32 id = 0, id_mask = 0;
1038 u32 cfi = 0, cfi_mask = 0;
1039 u32 prio = 0, prio_mask = 0;
1040
1041 u32 old_index = tab->index;
1042
1043 /* Check if vlan is wanted */
1044 if ((rule->flow_type & FLOW_EXT) && (rule->m_ext.vlan_tci != 0xFFFF)) {
1045 if (!rule->m_ext.vlan_tci)
1046 rule->m_ext.vlan_tci = 0xFFFF;
1047
1048 vlan = RQFPR_VLN;
1049 vlan_mask = RQFPR_VLN;
1050
1051 /* Separate the fields */
1052 id = rule->h_ext.vlan_tci & 0xFFF;
1053 id_mask = rule->m_ext.vlan_tci & 0xFFF;
1054 cfi = (rule->h_ext.vlan_tci >> 12) & 1;
1055 cfi_mask = (rule->m_ext.vlan_tci >> 12) & 1;
1056 prio = (rule->h_ext.vlan_tci >> 13) & 0x7;
1057 prio_mask = (rule->m_ext.vlan_tci >> 13) & 0x7;
1058
1059 if (cfi == 1 && cfi_mask == 1) {
1060 vlan |= RQFPR_CFI;
1061 vlan_mask |= RQFPR_CFI;
1062 } else if (cfi == 0 && cfi_mask == 1) {
1063 vlan_mask |= RQFPR_CFI;
1064 }
1065 }
1066
1067 switch (rule->flow_type & ~FLOW_EXT) {
1068 case TCP_V4_FLOW:
1069 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
1070 RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
1071 gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
1072 &rule->m_u.tcp_ip4_spec, tab);
1073 break;
1074 case UDP_V4_FLOW:
1075 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
1076 RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
1077 gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
1078 &rule->m_u.udp_ip4_spec, tab);
1079 break;
1080 case SCTP_V4_FLOW:
1081 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1082 tab);
1083 gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
1084 gfar_set_basic_ip((struct ethtool_tcpip4_spec *) &rule->h_u,
1085 (struct ethtool_tcpip4_spec *) &rule->m_u, tab);
1086 break;
1087 case IP_USER_FLOW:
1088 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1089 tab);
1090 gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
1091 (struct ethtool_usrip4_spec *) &rule->m_u, tab);
1092 break;
1093 case ETHER_FLOW:
1094 if (vlan)
1095 gfar_set_parse_bits(vlan, vlan_mask, tab);
1096 gfar_set_ether((struct ethhdr *) &rule->h_u,
1097 (struct ethhdr *) &rule->m_u, tab);
1098 break;
1099 default:
1100 return -1;
1101 }
1102
1103 /* Set the vlan attributes in the end */
1104 if (vlan) {
1105 gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
1106 gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
1107 }
1108
1109 /* If there has been nothing written till now, it must be a default */
1110 if (tab->index == old_index) {
1111 gfar_set_mask(0xFFFFFFFF, tab);
1112 tab->fe[tab->index].ctrl = 0x20;
1113 tab->fe[tab->index].prop = 0x0;
1114 tab->index++;
1115 }
1116
1117 /* Remove last AND */
1118 tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
1119
1120 /* Specify which queue to use or to drop */
1121 if (rule->ring_cookie == RX_CLS_FLOW_DISC)
1122 tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
1123 else
1124 tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
1125
1126 /* Only big enough entries can be clustered */
1127 if (tab->index > (old_index + 2)) {
1128 tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
1129 tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
1130 }
1131
1132 /* In rare cases the cache can be full while there is free space in hw */
1133 if (tab->index > MAX_FILER_CACHE_IDX - 1)
1134 return -EBUSY;
1135
1136 return 0;
1137}
1138
1139/* Copy size filer entries */
1140static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
1141 struct gfar_filer_entry src[0], s32 size)
1142{
1143 while (size > 0) {
1144 size--;
1145 dst[size].ctrl = src[size].ctrl;
1146 dst[size].prop = src[size].prop;
1147 }
1148}
1149
1150/* Delete the contents of the filer-table between start and end
1151 * and collapse them */
1152static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
1153{
1154 int length;
1155 if (end > MAX_FILER_CACHE_IDX || end < begin)
1156 return -EINVAL;
1157
1158 end++;
1159 length = end - begin;
1160
1161 /* Copy */
1162 while (end < tab->index) {
1163 tab->fe[begin].ctrl = tab->fe[end].ctrl;
1164 tab->fe[begin++].prop = tab->fe[end++].prop;
1165
1166 }
1167 /* Fill up with don't cares */
1168 while (begin < tab->index) {
1169 tab->fe[begin].ctrl = 0x60;
1170 tab->fe[begin].prop = 0xFFFFFFFF;
1171 begin++;
1172 }
1173
1174 tab->index -= length;
1175 return 0;
1176}
1177
1178/* Make space on the wanted location */
1179static int gfar_expand_filer_entries(u32 begin, u32 length,
1180 struct filer_table *tab)
1181{
1182 if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX || begin
1183 > MAX_FILER_CACHE_IDX)
1184 return -EINVAL;
1185
1186 gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
1187 tab->index - length + 1);
1188
1189 tab->index += length;
1190 return 0;
1191}
1192
1193static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
1194{
1195 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1); start++) {
1196 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE))
1197 == (RQFCR_AND | RQFCR_CLE))
1198 return start;
1199 }
1200 return -1;
1201}
1202
1203static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
1204{
1205 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1); start++) {
1206 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE))
1207 == (RQFCR_CLE))
1208 return start;
1209 }
1210 return -1;
1211}
1212
1213/*
1214 * Uses hardwares clustering option to reduce
1215 * the number of filer table entries
1216 */
1217static void gfar_cluster_filer(struct filer_table *tab)
1218{
1219 s32 i = -1, j, iend, jend;
1220
1221 while ((i = gfar_get_next_cluster_start(++i, tab)) != -1) {
1222 j = i;
1223 while ((j = gfar_get_next_cluster_start(++j, tab)) != -1) {
1224 /*
1225 * The cluster entries self and the previous one
1226 * (a mask) must be identical!
1227 */
1228 if (tab->fe[i].ctrl != tab->fe[j].ctrl)
1229 break;
1230 if (tab->fe[i].prop != tab->fe[j].prop)
1231 break;
1232 if (tab->fe[i - 1].ctrl != tab->fe[j - 1].ctrl)
1233 break;
1234 if (tab->fe[i - 1].prop != tab->fe[j - 1].prop)
1235 break;
1236 iend = gfar_get_next_cluster_end(i, tab);
1237 jend = gfar_get_next_cluster_end(j, tab);
1238 if (jend == -1 || iend == -1)
1239 break;
1240 /*
1241 * First we make some free space, where our cluster
1242 * element should be. Then we copy it there and finally
1243 * delete in from its old location.
1244 */
1245
1246 if (gfar_expand_filer_entries(iend, (jend - j), tab)
1247 == -EINVAL)
1248 break;
1249
1250 gfar_copy_filer_entries(&(tab->fe[iend + 1]),
1251 &(tab->fe[jend + 1]), jend - j);
1252
1253 if (gfar_trim_filer_entries(jend - 1,
1254 jend + (jend - j), tab) == -EINVAL)
1255 return;
1256
1257 /* Mask out cluster bit */
1258 tab->fe[iend].ctrl &= ~(RQFCR_CLE);
1259 }
1260 }
1261}
1262
1263/* Swaps the 0xFF80 masked bits of a1<>a2 and b1<>b2 */
1264static void gfar_swap_ff80_bits(struct gfar_filer_entry *a1,
1265 struct gfar_filer_entry *a2, struct gfar_filer_entry *b1,
1266 struct gfar_filer_entry *b2)
1267{
1268 u32 temp[4];
1269 temp[0] = a1->ctrl & 0xFF80;
1270 temp[1] = a2->ctrl & 0xFF80;
1271 temp[2] = b1->ctrl & 0xFF80;
1272 temp[3] = b2->ctrl & 0xFF80;
1273
1274 a1->ctrl &= ~0xFF80;
1275 a2->ctrl &= ~0xFF80;
1276 b1->ctrl &= ~0xFF80;
1277 b2->ctrl &= ~0xFF80;
1278
1279 a1->ctrl |= temp[1];
1280 a2->ctrl |= temp[0];
1281 b1->ctrl |= temp[3];
1282 b2->ctrl |= temp[2];
1283}
1284
1285/*
1286 * Generate a list consisting of masks values with their start and
1287 * end of validity and block as indicator for parts belonging
1288 * together (glued by ANDs) in mask_table
1289 */
1290static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
1291 struct filer_table *tab)
1292{
1293 u32 i, and_index = 0, block_index = 1;
1294
1295 for (i = 0; i < tab->index; i++) {
1296
1297 /* LSByte of control = 0 sets a mask */
1298 if (!(tab->fe[i].ctrl & 0xF)) {
1299 mask_table[and_index].mask = tab->fe[i].prop;
1300 mask_table[and_index].start = i;
1301 mask_table[and_index].block = block_index;
1302 if (and_index >= 1)
1303 mask_table[and_index - 1].end = i - 1;
1304 and_index++;
1305 }
1306 /* cluster starts will be separated because they should
1307 * hold their position */
1308 if (tab->fe[i].ctrl & RQFCR_CLE)
1309 block_index++;
1310 /* A not set AND indicates the end of a depended block */
1311 if (!(tab->fe[i].ctrl & RQFCR_AND))
1312 block_index++;
1313
1314 }
1315
1316 mask_table[and_index - 1].end = i - 1;
1317
1318 return and_index;
1319}
1320
1321/*
1322 * Sorts the entries of mask_table by the values of the masks.
1323 * Important: The 0xFF80 flags of the first and last entry of a
1324 * block must hold their position (which queue, CLusterEnable, ReJEct,
1325 * AND)
1326 */
1327static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
1328 struct filer_table *temp_table, u32 and_index)
1329{
1330 /* Pointer to compare function (_asc or _desc) */
1331 int (*gfar_comp)(const void *, const void *);
1332
1333 u32 i, size = 0, start = 0, prev = 1;
1334 u32 old_first, old_last, new_first, new_last;
1335
1336 gfar_comp = &gfar_comp_desc;
1337
1338 for (i = 0; i < and_index; i++) {
1339
1340 if (prev != mask_table[i].block) {
1341 old_first = mask_table[start].start + 1;
1342 old_last = mask_table[i - 1].end;
1343 sort(mask_table + start, size,
1344 sizeof(struct gfar_mask_entry),
1345 gfar_comp, &gfar_swap);
1346
1347 /* Toggle order for every block. This makes the
1348 * thing more efficient! */
1349 if (gfar_comp == gfar_comp_desc)
1350 gfar_comp = &gfar_comp_asc;
1351 else
1352 gfar_comp = &gfar_comp_desc;
1353
1354 new_first = mask_table[start].start + 1;
1355 new_last = mask_table[i - 1].end;
1356
1357 gfar_swap_ff80_bits(&temp_table->fe[new_first],
1358 &temp_table->fe[old_first],
1359 &temp_table->fe[new_last],
1360 &temp_table->fe[old_last]);
1361
1362 start = i;
1363 size = 0;
1364 }
1365 size++;
1366 prev = mask_table[i].block;
1367 }
1368
1369}
1370
1371/*
1372 * Reduces the number of masks needed in the filer table to save entries
1373 * This is done by sorting the masks of a depended block. A depended block is
1374 * identified by gluing ANDs or CLE. The sorting order toggles after every
1375 * block. Of course entries in scope of a mask must change their location with
1376 * it.
1377 */
1378static int gfar_optimize_filer_masks(struct filer_table *tab)
1379{
1380 struct filer_table *temp_table;
1381 struct gfar_mask_entry *mask_table;
1382
1383 u32 and_index = 0, previous_mask = 0, i = 0, j = 0, size = 0;
1384 s32 ret = 0;
1385
1386 /* We need a copy of the filer table because
1387 * we want to change its order */
1388 temp_table = kmalloc(sizeof(*temp_table), GFP_KERNEL);
1389 if (temp_table == NULL)
1390 return -ENOMEM;
1391 memcpy(temp_table, tab, sizeof(*temp_table));
1392
1393 mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
1394 sizeof(struct gfar_mask_entry), GFP_KERNEL);
1395
1396 if (mask_table == NULL) {
1397 ret = -ENOMEM;
1398 goto end;
1399 }
1400
1401 and_index = gfar_generate_mask_table(mask_table, tab);
1402
1403 gfar_sort_mask_table(mask_table, temp_table, and_index);
1404
1405 /* Now we can copy the data from our duplicated filer table to
1406 * the real one in the order the mask table says */
1407 for (i = 0; i < and_index; i++) {
1408 size = mask_table[i].end - mask_table[i].start + 1;
1409 gfar_copy_filer_entries(&(tab->fe[j]),
1410 &(temp_table->fe[mask_table[i].start]), size);
1411 j += size;
1412 }
1413
1414 /* And finally we just have to check for duplicated masks and drop the
1415 * second ones */
1416 for (i = 0; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1417 if (tab->fe[i].ctrl == 0x80) {
1418 previous_mask = i++;
1419 break;
1420 }
1421 }
1422 for (; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1423 if (tab->fe[i].ctrl == 0x80) {
1424 if (tab->fe[i].prop == tab->fe[previous_mask].prop) {
1425 /* Two identical ones found!
1426 * So drop the second one! */
1427 gfar_trim_filer_entries(i, i, tab);
1428 } else
1429 /* Not identical! */
1430 previous_mask = i;
1431 }
1432 }
1433
1434 kfree(mask_table);
1435end: kfree(temp_table);
1436 return ret;
1437}
1438
1439/* Write the bit-pattern from software's buffer to hardware registers */
1440static int gfar_write_filer_table(struct gfar_private *priv,
1441 struct filer_table *tab)
1442{
1443 u32 i = 0;
1444 if (tab->index > MAX_FILER_IDX - 1)
1445 return -EBUSY;
1446
1447 /* Avoid inconsistent filer table to be processed */
1448 lock_rx_qs(priv);
1449
1450 /* Fill regular entries */
1451 for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].ctrl); i++)
1452 gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
1453 /* Fill the rest with fall-troughs */
1454 for (; i < MAX_FILER_IDX - 1; i++)
1455 gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
1456 /* Last entry must be default accept
1457 * because that's what people expect */
1458 gfar_write_filer(priv, i, 0x20, 0x0);
1459
1460 unlock_rx_qs(priv);
1461
1462 return 0;
1463}
1464
1465static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
1466 struct gfar_private *priv)
1467{
1468
1469 if (flow->flow_type & FLOW_EXT) {
1470 if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
1471 netdev_warn(priv->ndev,
1472 "User-specific data not supported!\n");
1473 if (~flow->m_ext.vlan_etype)
1474 netdev_warn(priv->ndev,
1475 "VLAN-etype not supported!\n");
1476 }
1477 if (flow->flow_type == IP_USER_FLOW)
1478 if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
1479 netdev_warn(priv->ndev,
1480 "IP-Version differing from IPv4 not supported!\n");
1481
1482 return 0;
1483}
1484
1485static int gfar_process_filer_changes(struct gfar_private *priv)
1486{
1487 struct ethtool_flow_spec_container *j;
1488 struct filer_table *tab;
1489 s32 i = 0;
1490 s32 ret = 0;
1491
1492 /* So index is set to zero, too! */
1493 tab = kzalloc(sizeof(*tab), GFP_KERNEL);
1494 if (tab == NULL)
1495 return -ENOMEM;
1496
1497 /* Now convert the existing filer data from flow_spec into
1498 * filer tables binary format */
1499 list_for_each_entry(j, &priv->rx_list.list, list) {
1500 ret = gfar_convert_to_filer(&j->fs, tab);
1501 if (ret == -EBUSY) {
1502 netdev_err(priv->ndev, "Rule not added: No free space!\n");
1503 goto end;
1504 }
1505 if (ret == -1) {
1506 netdev_err(priv->ndev, "Rule not added: Unsupported Flow-type!\n");
1507 goto end;
1508 }
1509 }
1510
1511 i = tab->index;
1512
1513 /* Optimizations to save entries */
1514 gfar_cluster_filer(tab);
1515 gfar_optimize_filer_masks(tab);
1516
1517 pr_debug("\n\tSummary:\n"
1518 "\tData on hardware: %d\n"
1519 "\tCompression rate: %d%%\n",
1520 tab->index, 100 - (100 * tab->index) / i);
1521
1522 /* Write everything to hardware */
1523 ret = gfar_write_filer_table(priv, tab);
1524 if (ret == -EBUSY) {
1525 netdev_err(priv->ndev, "Rule not added: No free space!\n");
1526 goto end;
1527 }
1528
1529end: kfree(tab);
1530 return ret;
1531}
1532
1533static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
1534{
1535 u32 i = 0;
1536
1537 for (i = 0; i < sizeof(flow->m_u); i++)
1538 flow->m_u.hdata[i] ^= 0xFF;
1539
1540 flow->m_ext.vlan_etype ^= 0xFFFF;
1541 flow->m_ext.vlan_tci ^= 0xFFFF;
1542 flow->m_ext.data[0] ^= ~0;
1543 flow->m_ext.data[1] ^= ~0;
1544}
1545
1546static int gfar_add_cls(struct gfar_private *priv,
1547 struct ethtool_rx_flow_spec *flow)
1548{
1549 struct ethtool_flow_spec_container *temp, *comp;
1550 int ret = 0;
1551
1552 temp = kmalloc(sizeof(*temp), GFP_KERNEL);
1553 if (temp == NULL)
1554 return -ENOMEM;
1555 memcpy(&temp->fs, flow, sizeof(temp->fs));
1556
1557 gfar_invert_masks(&temp->fs);
1558 ret = gfar_check_capability(&temp->fs, priv);
1559 if (ret)
1560 goto clean_mem;
1561 /* Link in the new element at the right @location */
1562 if (list_empty(&priv->rx_list.list)) {
1563 ret = gfar_check_filer_hardware(priv);
1564 if (ret != 0)
1565 goto clean_mem;
1566 list_add(&temp->list, &priv->rx_list.list);
1567 goto process;
1568 } else {
1569
1570 list_for_each_entry(comp, &priv->rx_list.list, list) {
1571 if (comp->fs.location > flow->location) {
1572 list_add_tail(&temp->list, &comp->list);
1573 goto process;
1574 }
1575 if (comp->fs.location == flow->location) {
1576 netdev_err(priv->ndev,
1577 "Rule not added: ID %d not free!\n",
1578 flow->location);
1579 ret = -EBUSY;
1580 goto clean_mem;
1581 }
1582 }
1583 list_add_tail(&temp->list, &priv->rx_list.list);
1584 }
1585
1586process:
1587 ret = gfar_process_filer_changes(priv);
1588 if (ret)
1589 goto clean_list;
1590 priv->rx_list.count++;
1591 return ret;
1592
1593clean_list:
1594 list_del(&temp->list);
1595clean_mem:
1596 kfree(temp);
1597 return ret;
1598}
1599
1600static int gfar_del_cls(struct gfar_private *priv, u32 loc)
1601{
1602 struct ethtool_flow_spec_container *comp;
1603 u32 ret = -EINVAL;
1604
1605 if (list_empty(&priv->rx_list.list))
1606 return ret;
1607
1608 list_for_each_entry(comp, &priv->rx_list.list, list) {
1609 if (comp->fs.location == loc) {
1610 list_del(&comp->list);
1611 kfree(comp);
1612 priv->rx_list.count--;
1613 gfar_process_filer_changes(priv);
1614 ret = 0;
1615 break;
1616 }
1617 }
1618
1619 return ret;
1620
1621}
1622
1623static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
1624{
1625 struct ethtool_flow_spec_container *comp;
1626 u32 ret = -EINVAL;
1627
1628 list_for_each_entry(comp, &priv->rx_list.list, list) {
1629 if (comp->fs.location == cmd->fs.location) {
1630 memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
1631 gfar_invert_masks(&cmd->fs);
1632 ret = 0;
1633 break;
1634 }
1635 }
1636
1637 return ret;
1638}
1639
1640static int gfar_get_cls_all(struct gfar_private *priv,
1641 struct ethtool_rxnfc *cmd, u32 *rule_locs)
1642{
1643 struct ethtool_flow_spec_container *comp;
1644 u32 i = 0;
1645
1646 list_for_each_entry(comp, &priv->rx_list.list, list) {
1647 if (i <= cmd->rule_cnt) {
1648 rule_locs[i] = comp->fs.location;
1649 i++;
1650 }
1651 }
1652
1653 cmd->data = MAX_FILER_IDX;
1654
1655 return 0;
1656}
1657
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001658static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1659{
1660 struct gfar_private *priv = netdev_priv(dev);
1661 int ret = 0;
1662
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001663 mutex_lock(&priv->rx_queue_access);
1664
1665 switch (cmd->cmd) {
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001666 case ETHTOOL_SRXFH:
1667 ret = gfar_set_hash_opts(priv, cmd);
1668 break;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001669 case ETHTOOL_SRXCLSRLINS:
1670 if (cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
1671 cmd->fs.ring_cookie >= priv->num_rx_queues) {
1672 ret = -EINVAL;
1673 break;
1674 }
1675 ret = gfar_add_cls(priv, &cmd->fs);
1676 break;
1677 case ETHTOOL_SRXCLSRLDEL:
1678 ret = gfar_del_cls(priv, cmd->fs.location);
1679 break;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001680 default:
1681 ret = -EINVAL;
1682 }
1683
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001684 mutex_unlock(&priv->rx_queue_access);
1685
1686 return ret;
1687}
1688
1689static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1690 void *rule_locs)
1691{
1692 struct gfar_private *priv = netdev_priv(dev);
1693 int ret = 0;
1694
1695 switch (cmd->cmd) {
1696 case ETHTOOL_GRXRINGS:
1697 cmd->data = priv->num_rx_queues;
1698 break;
1699 case ETHTOOL_GRXCLSRLCNT:
1700 cmd->rule_cnt = priv->rx_list.count;
1701 break;
1702 case ETHTOOL_GRXCLSRULE:
1703 ret = gfar_get_cls(priv, cmd);
1704 break;
1705 case ETHTOOL_GRXCLSRLALL:
1706 ret = gfar_get_cls_all(priv, cmd, (u32 *) rule_locs);
1707 break;
1708 default:
1709 ret = -EINVAL;
1710 break;
1711 }
1712
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001713 return ret;
1714}
1715
Jeff Garzik7282d492006-09-13 14:30:00 -04001716const struct ethtool_ops gfar_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 .get_settings = gfar_gsettings,
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001718 .set_settings = gfar_ssettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 .get_drvinfo = gfar_gdrvinfo,
1720 .get_regs_len = gfar_reglen,
1721 .get_regs = gfar_get_regs,
1722 .get_link = ethtool_op_get_link,
1723 .get_coalesce = gfar_gcoalesce,
1724 .set_coalesce = gfar_scoalesce,
1725 .get_ringparam = gfar_gringparam,
1726 .set_ringparam = gfar_sringparam,
1727 .get_strings = gfar_gstrings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001728 .get_sset_count = gfar_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 .get_ethtool_stats = gfar_fill_stats,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001730 .get_msglevel = gfar_get_msglevel,
1731 .set_msglevel = gfar_set_msglevel,
Scott Woodd87eb122008-07-11 18:04:45 -05001732#ifdef CONFIG_PM
1733 .get_wol = gfar_get_wol,
1734 .set_wol = gfar_set_wol,
1735#endif
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001736 .set_rxnfc = gfar_set_nfc,
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001737 .get_rxnfc = gfar_get_nfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738};