blob: d3d7ede27ef14854f11b2f7c4a9648d34574dbeb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Gortmaker3396c782012-01-27 13:36:01 +00002 * drivers/net/ethernet/freescale/gianfar_ethtool.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Gianfar Ethernet Driver
5 * Ethtool support for Gianfar Enet
6 * Based on e1000 ethtool support
7 *
8 * Author: Andy Fleming
Kumar Gala4c8d3d92005-11-13 16:06:30 -08009 * Maintainer: Kumar Gala
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000010 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +000012 * Copyright 2003-2006, 2008-2009, 2011 Freescale Semiconductor, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014 * This software may be used and distributed according to
15 * the terms of the GNU Public License, Version 2, incorporated herein
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * by reference.
17 */
18
Joe Perches59deab22011-06-14 08:57:47 +000019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/string.h>
23#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
David S. Miller65a85a82012-04-06 00:35:34 -040029#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/skbuff.h>
31#include <linux/spinlock.h>
32#include <linux/mm.h>
33
34#include <asm/io.h>
35#include <asm/irq.h>
36#include <asm/uaccess.h>
37#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/crc32.h>
39#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/ethtool.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040041#include <linux/mii.h>
42#include <linux/phy.h>
Sebastian Poehn4aa3a712011-06-20 13:57:59 -070043#include <linux/sort.h>
Sebastian Poehn380b1532011-07-07 04:30:29 -070044#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "gianfar.h"
47
Kumar Gala0bbaf062005-06-20 10:54:21 -050048extern void gfar_start(struct net_device *dev);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +000049extern int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue,
50 int rx_work_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Andy Flemingbb40dcb2005-09-23 22:54:21 -040052#define GFAR_MAX_COAL_USECS 0xffff
53#define GFAR_MAX_COAL_FRAMES 0xff
Kumar Gala0bbaf062005-06-20 10:54:21 -050054static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +000055 u64 *buf);
Kumar Gala0bbaf062005-06-20 10:54:21 -050056static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +000057static int gfar_gcoalesce(struct net_device *dev,
58 struct ethtool_coalesce *cvals);
59static int gfar_scoalesce(struct net_device *dev,
60 struct ethtool_coalesce *cvals);
61static void gfar_gringparam(struct net_device *dev,
62 struct ethtool_ringparam *rvals);
63static int gfar_sringparam(struct net_device *dev,
64 struct ethtool_ringparam *rvals);
65static void gfar_gdrvinfo(struct net_device *dev,
66 struct ethtool_drvinfo *drvinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Paul Gortmaker30f7e312012-01-08 13:21:57 -050068static const char stat_gstrings[][ETH_GSTRING_LEN] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 "rx-large-frame-errors",
70 "rx-short-frame-errors",
71 "rx-non-octet-errors",
72 "rx-crc-errors",
73 "rx-overrun-errors",
74 "rx-busy-errors",
75 "rx-babbling-errors",
76 "rx-truncated-frames",
77 "ethernet-bus-error",
78 "tx-babbling-errors",
79 "tx-underrun-errors",
80 "rx-skb-missing-errors",
81 "tx-timeout-errors",
82 "tx-rx-64-frames",
83 "tx-rx-65-127-frames",
84 "tx-rx-128-255-frames",
85 "tx-rx-256-511-frames",
86 "tx-rx-512-1023-frames",
87 "tx-rx-1024-1518-frames",
88 "tx-rx-1519-1522-good-vlan",
89 "rx-bytes",
90 "rx-packets",
91 "rx-fcs-errors",
92 "receive-multicast-packet",
93 "receive-broadcast-packet",
94 "rx-control-frame-packets",
95 "rx-pause-frame-packets",
96 "rx-unknown-op-code",
97 "rx-alignment-error",
98 "rx-frame-length-error",
99 "rx-code-error",
100 "rx-carrier-sense-error",
101 "rx-undersize-packets",
102 "rx-oversize-packets",
103 "rx-fragmented-frames",
104 "rx-jabber-frames",
105 "rx-dropped-frames",
106 "tx-byte-counter",
107 "tx-packets",
108 "tx-multicast-packets",
109 "tx-broadcast-packets",
110 "tx-pause-control-frames",
111 "tx-deferral-packets",
112 "tx-excessive-deferral-packets",
113 "tx-single-collision-packets",
114 "tx-multiple-collision-packets",
115 "tx-late-collision-packets",
116 "tx-excessive-collision-packets",
117 "tx-total-collision",
118 "reserved",
119 "tx-dropped-frames",
120 "tx-jabber-frames",
121 "tx-fcs-errors",
122 "tx-control-frames",
123 "tx-oversize-frames",
124 "tx-undersize-frames",
125 "tx-fragmented-frames",
126};
127
Kumar Gala0bbaf062005-06-20 10:54:21 -0500128/* Fill in a buffer with the strings which correspond to the
129 * stats */
130static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
131{
132 struct gfar_private *priv = netdev_priv(dev);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600133
Andy Flemingb31a1d82008-12-16 15:29:15 -0800134 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500135 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
136 else
137 memcpy(buf, stat_gstrings,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000138 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/* Fill in an array of 64-bit statistics from various sources.
142 * This array will be appended to the end of the ethtool_stats
143 * structure, and returned to user space
144 */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000145static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
146 u64 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 int i;
149 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000150 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Paul Gortmaker212079d2013-02-12 15:38:19 -0500151 atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Paul Gortmaker68719782013-02-12 15:28:35 -0500153 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
Paul Gortmaker212079d2013-02-12 15:38:19 -0500154 buf[i] = atomic64_read(&extra[i]);
Paul Gortmaker68719782013-02-12 15:28:35 -0500155
Andy Flemingb31a1d82008-12-16 15:29:15 -0800156 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
Sandeep Gopalpetf4983702009-11-02 07:03:09 +0000157 u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Paul Gortmaker68719782013-02-12 15:28:35 -0500159 for (; i < GFAR_STATS_LEN; i++, rmon++)
160 buf[i] = (u64) gfar_read(rmon);
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700164static int gfar_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700168 switch (sset) {
169 case ETH_SS_STATS:
Andy Flemingb31a1d82008-12-16 15:29:15 -0800170 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700171 return GFAR_STATS_LEN;
172 else
173 return GFAR_EXTRA_STATS_LEN;
174 default:
175 return -EOPNOTSUPP;
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* Fills in the drvinfo structure with some basic info */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000180static void gfar_gdrvinfo(struct net_device *dev,
181 struct ethtool_drvinfo *drvinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Jiri Pirko7826d432013-01-06 00:44:26 +0000183 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
184 strlcpy(drvinfo->version, gfar_driver_version,
185 sizeof(drvinfo->version));
186 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
187 strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 drvinfo->regdump_len = 0;
189 drvinfo->eedump_len = 0;
190}
191
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400192
193static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
194{
195 struct gfar_private *priv = netdev_priv(dev);
196 struct phy_device *phydev = priv->phydev;
197
198 if (NULL == phydev)
199 return -ENODEV;
200
201 return phy_ethtool_sset(phydev, cmd);
202}
203
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/* Return the current settings in the ethtool_cmd structure */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500206static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400209 struct phy_device *phydev = priv->phydev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000210 struct gfar_priv_rx_q *rx_queue = NULL;
211 struct gfar_priv_tx_q *tx_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400213 if (NULL == phydev)
214 return -ENODEV;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000215 tx_queue = priv->tx_queue[0];
216 rx_queue = priv->rx_queue[0];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400217
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000218 /* etsec-1.7 and older versions have only one txic
219 * and rxic regs although they support multiple queues */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000220 cmd->maxtxpkt = get_icft_value(tx_queue->txic);
221 cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400223 return phy_ethtool_gset(phydev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/* Return the length of the register structure */
Kumar Gala0bbaf062005-06-20 10:54:21 -0500227static int gfar_reglen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 return sizeof (struct gfar);
230}
231
232/* Return a dump of the GFAR register space */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000233static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
234 void *regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 int i;
237 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000238 u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 u32 *buf = (u32 *) regbuf;
240
241 for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
Kumar Galacc8c6e32006-02-01 15:18:03 -0600242 buf[i] = gfar_read(&theregs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/* Convert microseconds to ethernet clock ticks, which changes
246 * depending on what speed the controller is running at */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000247static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
248 unsigned int usecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 unsigned int count;
251
252 /* The timer is different, depending on the interface speed */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400253 switch (priv->phydev->speed) {
254 case SPEED_1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 count = GFAR_GBIT_TIME;
256 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400257 case SPEED_100:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 count = GFAR_100_TIME;
259 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400260 case SPEED_10:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 default:
262 count = GFAR_10_TIME;
263 break;
264 }
265
266 /* Make sure we return a number greater than 0
267 * if usecs > 0 */
Eric Dumazet807540b2010-09-23 05:40:09 +0000268 return (usecs * 1000 + count - 1) / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/* Convert ethernet clock ticks to microseconds */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000272static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
273 unsigned int ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 unsigned int count;
276
277 /* The timer is different, depending on the interface speed */
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400278 switch (priv->phydev->speed) {
279 case SPEED_1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 count = GFAR_GBIT_TIME;
281 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400282 case SPEED_100:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 count = GFAR_100_TIME;
284 break;
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400285 case SPEED_10:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 default:
287 count = GFAR_10_TIME;
288 break;
289 }
290
291 /* Make sure we return a number greater than 0 */
292 /* if ticks is > 0 */
Eric Dumazet807540b2010-09-23 05:40:09 +0000293 return (ticks * count) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296/* Get the coalescing parameters, and put them in the cvals
297 * structure. */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000298static int gfar_gcoalesce(struct net_device *dev,
299 struct ethtool_coalesce *cvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000302 struct gfar_priv_rx_q *rx_queue = NULL;
303 struct gfar_priv_tx_q *tx_queue = NULL;
Dai Harukib46a8452008-12-16 15:29:52 -0800304 unsigned long rxtime;
305 unsigned long rxcount;
306 unsigned long txtime;
307 unsigned long txcount;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400308
Andy Flemingb31a1d82008-12-16 15:29:15 -0800309 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
Kumar Gala0bbaf062005-06-20 10:54:21 -0500310 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400312 if (NULL == priv->phydev)
313 return -ENODEV;
314
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000315 rx_queue = priv->rx_queue[0];
316 tx_queue = priv->tx_queue[0];
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000317
318 rxtime = get_ictt_value(rx_queue->rxic);
319 rxcount = get_icft_value(rx_queue->rxic);
320 txtime = get_ictt_value(tx_queue->txic);
321 txcount = get_icft_value(tx_queue->txic);
Dai Harukib46a8452008-12-16 15:29:52 -0800322 cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
323 cvals->rx_max_coalesced_frames = rxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Dai Harukib46a8452008-12-16 15:29:52 -0800325 cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
326 cvals->tx_max_coalesced_frames = txcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 cvals->use_adaptive_rx_coalesce = 0;
329 cvals->use_adaptive_tx_coalesce = 0;
330
331 cvals->pkt_rate_low = 0;
332 cvals->rx_coalesce_usecs_low = 0;
333 cvals->rx_max_coalesced_frames_low = 0;
334 cvals->tx_coalesce_usecs_low = 0;
335 cvals->tx_max_coalesced_frames_low = 0;
336
337 /* When the packet rate is below pkt_rate_high but above
338 * pkt_rate_low (both measured in packets per second) the
339 * normal {rx,tx}_* coalescing parameters are used.
340 */
341
342 /* When the packet rate is (measured in packets per second)
343 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
344 * used.
345 */
346 cvals->pkt_rate_high = 0;
347 cvals->rx_coalesce_usecs_high = 0;
348 cvals->rx_max_coalesced_frames_high = 0;
349 cvals->tx_coalesce_usecs_high = 0;
350 cvals->tx_max_coalesced_frames_high = 0;
351
352 /* How often to do adaptive coalescing packet rate sampling,
353 * measured in seconds. Must not be zero.
354 */
355 cvals->rate_sample_interval = 0;
356
357 return 0;
358}
359
360/* Change the coalescing values.
361 * Both cvals->*_usecs and cvals->*_frames have to be > 0
362 * in order for coalescing to be active
363 */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000364static int gfar_scoalesce(struct net_device *dev,
365 struct ethtool_coalesce *cvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000368 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Andy Flemingb31a1d82008-12-16 15:29:15 -0800370 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
Kumar Gala0bbaf062005-06-20 10:54:21 -0500371 return -EOPNOTSUPP;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* Set up rx coalescing */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000374 /* As of now, we will enable/disable coalescing for all
375 * queues together in case of eTSEC2, this will be modified
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000376 * along with the ethtool interface
377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if ((cvals->rx_coalesce_usecs == 0) ||
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000379 (cvals->rx_max_coalesced_frames == 0)) {
380 for (i = 0; i < priv->num_rx_queues; i++)
381 priv->rx_queue[i]->rxcoalescing = 0;
382 } else {
383 for (i = 0; i < priv->num_rx_queues; i++)
384 priv->rx_queue[i]->rxcoalescing = 1;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400387 if (NULL == priv->phydev)
388 return -ENODEV;
389
390 /* Check the bounds of the values */
391 if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
Joe Perches375d6a12013-04-13 19:03:18 +0000392 netdev_info(dev, "Coalescing is limited to %d microseconds\n",
393 GFAR_MAX_COAL_USECS);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400394 return -EINVAL;
395 }
396
397 if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
Joe Perches375d6a12013-04-13 19:03:18 +0000398 netdev_info(dev, "Coalescing is limited to %d frames\n",
399 GFAR_MAX_COAL_FRAMES);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400400 return -EINVAL;
401 }
402
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000403 for (i = 0; i < priv->num_rx_queues; i++) {
404 priv->rx_queue[i]->rxic = mk_ic_value(
405 cvals->rx_max_coalesced_frames,
406 gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* Set up tx coalescing */
410 if ((cvals->tx_coalesce_usecs == 0) ||
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000411 (cvals->tx_max_coalesced_frames == 0)) {
412 for (i = 0; i < priv->num_tx_queues; i++)
413 priv->tx_queue[i]->txcoalescing = 0;
414 } else {
415 for (i = 0; i < priv->num_tx_queues; i++)
416 priv->tx_queue[i]->txcoalescing = 1;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400419 /* Check the bounds of the values */
420 if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
Joe Perches375d6a12013-04-13 19:03:18 +0000421 netdev_info(dev, "Coalescing is limited to %d microseconds\n",
422 GFAR_MAX_COAL_USECS);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400423 return -EINVAL;
424 }
425
426 if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
Joe Perches375d6a12013-04-13 19:03:18 +0000427 netdev_info(dev, "Coalescing is limited to %d frames\n",
428 GFAR_MAX_COAL_FRAMES);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400429 return -EINVAL;
430 }
431
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000432 for (i = 0; i < priv->num_tx_queues; i++) {
433 priv->tx_queue[i]->txic = mk_ic_value(
434 cvals->tx_max_coalesced_frames,
435 gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Claudiu Manoil800c6442013-03-19 07:40:05 +0000438 gfar_configure_coalescing_all(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 return 0;
441}
442
443/* Fills in rvals with the current ring parameters. Currently,
444 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
445 * jumbo are ignored by the driver */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000446static void gfar_gringparam(struct net_device *dev,
447 struct ethtool_ringparam *rvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000450 struct gfar_priv_tx_q *tx_queue = NULL;
451 struct gfar_priv_rx_q *rx_queue = NULL;
452
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000453 tx_queue = priv->tx_queue[0];
454 rx_queue = priv->rx_queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
457 rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
458 rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
459 rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
460
461 /* Values changeable by the user. The valid values are
462 * in the range 1 to the "*_max_pending" counterpart above.
463 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000464 rvals->rx_pending = rx_queue->rx_ring_size;
465 rvals->rx_mini_pending = rx_queue->rx_ring_size;
466 rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
467 rvals->tx_pending = tx_queue->tx_ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470/* Change the current ring parameters, stopping the controller if
471 * necessary so that we don't mess things up while we're in
472 * motion. We wait for the ring to be clean before reallocating
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000473 * the rings.
474 */
475static int gfar_sringparam(struct net_device *dev,
476 struct ethtool_ringparam *rvals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000479 int err = 0, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
482 return -EINVAL;
483
484 if (!is_power_of_2(rvals->rx_pending)) {
Joe Perches59deab22011-06-14 08:57:47 +0000485 netdev_err(dev, "Ring sizes must be a power of 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EINVAL;
487 }
488
489 if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
490 return -EINVAL;
491
492 if (!is_power_of_2(rvals->tx_pending)) {
Joe Perches59deab22011-06-14 08:57:47 +0000493 netdev_err(dev, "Ring sizes must be a power of 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -EINVAL;
495 }
496
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (dev->flags & IFF_UP) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500499 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Kumar Gala0bbaf062005-06-20 10:54:21 -0500501 /* Halt TX and RX, and process the frames which
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000502 * have already been received
503 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000504 local_irq_save(flags);
505 lock_tx_qs(priv);
506 lock_rx_qs(priv);
Andy Flemingfef61082006-04-20 16:44:29 -0500507
Kumar Gala0bbaf062005-06-20 10:54:21 -0500508 gfar_halt(dev);
Andy Flemingfef61082006-04-20 16:44:29 -0500509
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000510 unlock_rx_qs(priv);
511 unlock_tx_qs(priv);
512 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000514 for (i = 0; i < priv->num_rx_queues; i++)
515 gfar_clean_rx_ring(priv->rx_queue[i],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000516 priv->rx_queue[i]->rx_ring_size);
Dai Haruki12dea572008-12-16 15:30:20 -0800517
Kumar Gala0bbaf062005-06-20 10:54:21 -0500518 /* Now we take down the rings to rebuild them */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 stop_gfar(dev);
520 }
521
Kumar Gala0bbaf062005-06-20 10:54:21 -0500522 /* Change the size */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000523 for (i = 0; i < priv->num_rx_queues; i++) {
524 priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
525 priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000526 priv->tx_queue[i]->num_txbdfree =
527 priv->tx_queue[i]->tx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Kumar Gala0bbaf062005-06-20 10:54:21 -0500530 /* Rebuild the rings with the new size */
Dai Haruki12dea572008-12-16 15:30:20 -0800531 if (dev->flags & IFF_UP) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500532 err = startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000533 netif_tx_wake_all_queues(dev);
Dai Haruki12dea572008-12-16 15:30:20 -0800534 }
Kumar Gala0bbaf062005-06-20 10:54:21 -0500535 return err;
536}
537
Claudiu Manoil23402bd2013-08-12 13:53:26 +0300538static void gfar_gpauseparam(struct net_device *dev,
539 struct ethtool_pauseparam *epause)
540{
541 struct gfar_private *priv = netdev_priv(dev);
542
543 epause->autoneg = !!priv->pause_aneg_en;
544 epause->rx_pause = !!priv->rx_pause_en;
545 epause->tx_pause = !!priv->tx_pause_en;
546}
547
548static int gfar_spauseparam(struct net_device *dev,
549 struct ethtool_pauseparam *epause)
550{
551 struct gfar_private *priv = netdev_priv(dev);
552 struct phy_device *phydev = priv->phydev;
553 struct gfar __iomem *regs = priv->gfargrp[0].regs;
554 u32 oldadv, newadv;
555
556 if (!(phydev->supported & SUPPORTED_Pause) ||
557 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
558 (epause->rx_pause != epause->tx_pause)))
559 return -EINVAL;
560
561 priv->rx_pause_en = priv->tx_pause_en = 0;
562 if (epause->rx_pause) {
563 priv->rx_pause_en = 1;
564
565 if (epause->tx_pause) {
566 priv->tx_pause_en = 1;
567 /* FLOW_CTRL_RX & TX */
568 newadv = ADVERTISED_Pause;
569 } else /* FLOW_CTLR_RX */
570 newadv = ADVERTISED_Pause | ADVERTISED_Asym_Pause;
571 } else if (epause->tx_pause) {
572 priv->tx_pause_en = 1;
573 /* FLOW_CTLR_TX */
574 newadv = ADVERTISED_Asym_Pause;
575 } else
576 newadv = 0;
577
578 if (epause->autoneg)
579 priv->pause_aneg_en = 1;
580 else
581 priv->pause_aneg_en = 0;
582
583 oldadv = phydev->advertising &
584 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
585 if (oldadv != newadv) {
586 phydev->advertising &=
587 ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
588 phydev->advertising |= newadv;
589 if (phydev->autoneg)
590 /* inform link partner of our
591 * new flow ctrl settings
592 */
593 return phy_start_aneg(phydev);
594
595 if (!epause->autoneg) {
596 u32 tempval;
597 tempval = gfar_read(&regs->maccfg1);
598 tempval &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
599 if (priv->tx_pause_en)
600 tempval |= MACCFG1_TX_FLOW;
601 if (priv->rx_pause_en)
602 tempval |= MACCFG1_RX_FLOW;
603 gfar_write(&regs->maccfg1, tempval);
604 }
605 }
606
607 return 0;
608}
609
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000610int gfar_set_features(struct net_device *dev, netdev_features_t features)
Kumar Gala0bbaf062005-06-20 10:54:21 -0500611{
612 struct gfar_private *priv = netdev_priv(dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500613 unsigned long flags;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000614 int err = 0, i = 0;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000615 netdev_features_t changed = dev->features ^ features;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500616
Patrick McHardyf6469682013-04-19 02:04:27 +0000617 if (changed & (NETIF_F_HW_VLAN_CTAG_TX|NETIF_F_HW_VLAN_CTAG_RX))
Jiri Pirko87c288c2011-07-20 04:54:19 +0000618 gfar_vlan_mode(dev, features);
619
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000620 if (!(changed & NETIF_F_RXCSUM))
621 return 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000622
Kumar Gala0bbaf062005-06-20 10:54:21 -0500623 if (dev->flags & IFF_UP) {
Kumar Gala0bbaf062005-06-20 10:54:21 -0500624 /* Halt TX and RX, and process the frames which
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000625 * have already been received
626 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000627 local_irq_save(flags);
628 lock_tx_qs(priv);
629 lock_rx_qs(priv);
Andy Flemingfef61082006-04-20 16:44:29 -0500630
Kumar Gala0bbaf062005-06-20 10:54:21 -0500631 gfar_halt(dev);
Andy Flemingfef61082006-04-20 16:44:29 -0500632
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000633 unlock_tx_qs(priv);
634 unlock_rx_qs(priv);
Dan Carpenter97564032010-10-13 09:19:55 +0000635 local_irq_restore(flags);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500636
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000637 for (i = 0; i < priv->num_rx_queues; i++)
638 gfar_clean_rx_ring(priv->rx_queue[i],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000639 priv->rx_queue[i]->rx_ring_size);
Dai Haruki12dea572008-12-16 15:30:20 -0800640
Kumar Gala0bbaf062005-06-20 10:54:21 -0500641 /* Now we take down the rings to rebuild them */
642 stop_gfar(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -0500643
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000644 dev->features = features;
Kumar Gala0bbaf062005-06-20 10:54:21 -0500645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 err = startup_gfar(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000647 netif_tx_wake_all_queues(dev);
Dai Haruki12dea572008-12-16 15:30:20 -0800648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return err;
650}
651
Kumar Gala0bbaf062005-06-20 10:54:21 -0500652static uint32_t gfar_get_msglevel(struct net_device *dev)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400653{
Kumar Gala0bbaf062005-06-20 10:54:21 -0500654 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000655
Kumar Gala0bbaf062005-06-20 10:54:21 -0500656 return priv->msg_enable;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400657}
658
Kumar Gala0bbaf062005-06-20 10:54:21 -0500659static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400660{
Kumar Gala0bbaf062005-06-20 10:54:21 -0500661 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000662
Kumar Gala0bbaf062005-06-20 10:54:21 -0500663 priv->msg_enable = data;
664}
665
Scott Woodd87eb122008-07-11 18:04:45 -0500666#ifdef CONFIG_PM
667static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
668{
669 struct gfar_private *priv = netdev_priv(dev);
670
Andy Flemingb31a1d82008-12-16 15:29:15 -0800671 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
Scott Woodd87eb122008-07-11 18:04:45 -0500672 wol->supported = WAKE_MAGIC;
673 wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
674 } else {
675 wol->supported = wol->wolopts = 0;
676 }
677}
678
679static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
680{
681 struct gfar_private *priv = netdev_priv(dev);
682 unsigned long flags;
683
Andy Flemingb31a1d82008-12-16 15:29:15 -0800684 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -0500685 wol->wolopts != 0)
686 return -EINVAL;
687
688 if (wol->wolopts & ~WAKE_MAGIC)
689 return -EINVAL;
690
Rafael J. Wysocki6c4f1992010-11-09 11:54:19 +0000691 device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);
692
Scott Woodd87eb122008-07-11 18:04:45 -0500693 spin_lock_irqsave(&priv->bflock, flags);
Rafael J. Wysocki6c4f1992010-11-09 11:54:19 +0000694 priv->wol_en = !!device_may_wakeup(&dev->dev);
Scott Woodd87eb122008-07-11 18:04:45 -0500695 spin_unlock_irqrestore(&priv->bflock, flags);
696
697 return 0;
698}
699#endif
Kumar Gala0bbaf062005-06-20 10:54:21 -0500700
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000701static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
702{
703 u32 fcr = 0x0, fpr = FPR_FILER_MASK;
704
705 if (ethflow & RXH_L2DA) {
706 fcr = RQFCR_PID_DAH |RQFCR_CMP_NOMATCH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000707 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000708 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
709 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000710 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
711 priv->cur_filer_idx = priv->cur_filer_idx - 1;
712
713 fcr = RQFCR_PID_DAL | RQFCR_AND | RQFCR_CMP_NOMATCH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000714 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000715 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
716 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000717 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
718 priv->cur_filer_idx = priv->cur_filer_idx - 1;
719 }
720
721 if (ethflow & RXH_VLAN) {
722 fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000723 RQFCR_AND | RQFCR_HASHTBL_0;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000724 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000725 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
726 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000727 priv->cur_filer_idx = priv->cur_filer_idx - 1;
728 }
729
730 if (ethflow & RXH_IP_SRC) {
731 fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000732 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000733 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
734 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000735 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
736 priv->cur_filer_idx = priv->cur_filer_idx - 1;
737 }
738
739 if (ethflow & (RXH_IP_DST)) {
740 fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000741 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000742 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
743 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000744 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
745 priv->cur_filer_idx = priv->cur_filer_idx - 1;
746 }
747
748 if (ethflow & RXH_L3_PROTO) {
749 fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000750 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000751 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
752 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000753 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
754 priv->cur_filer_idx = priv->cur_filer_idx - 1;
755 }
756
757 if (ethflow & RXH_L4_B_0_1) {
758 fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000759 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000760 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
761 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000762 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
763 priv->cur_filer_idx = priv->cur_filer_idx - 1;
764 }
765
766 if (ethflow & RXH_L4_B_2_3) {
767 fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000768 RQFCR_AND | RQFCR_HASHTBL_0;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000769 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
770 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000771 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
772 priv->cur_filer_idx = priv->cur_filer_idx - 1;
773 }
774}
775
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000776static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
777 u64 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000778{
779 unsigned int last_rule_idx = priv->cur_filer_idx;
780 unsigned int cmp_rqfpr;
Wang Shaoyan588dc912011-08-11 17:07:25 +0000781 unsigned int *local_rqfpr;
782 unsigned int *local_rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000783 int i = 0x0, k = 0x0;
784 int j = MAX_FILER_IDX, l = 0x0;
Wang Shaoyan588dc912011-08-11 17:07:25 +0000785 int ret = 1;
786
Joe Perchesb2adaca2013-02-03 17:43:58 +0000787 local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
788 GFP_KERNEL);
789 local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
790 GFP_KERNEL);
Wang Shaoyan588dc912011-08-11 17:07:25 +0000791 if (!local_rqfpr || !local_rqfcr) {
Wang Shaoyan588dc912011-08-11 17:07:25 +0000792 ret = 0;
793 goto err;
794 }
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000795
796 switch (class) {
797 case TCP_V4_FLOW:
798 cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
799 break;
800 case UDP_V4_FLOW:
801 cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
802 break;
803 case TCP_V6_FLOW:
804 cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
805 break;
806 case UDP_V6_FLOW:
807 cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
808 break;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000809 default:
Joe Perches375d6a12013-04-13 19:03:18 +0000810 netdev_err(priv->ndev,
811 "Right now this class is not supported\n");
Wang Shaoyan588dc912011-08-11 17:07:25 +0000812 ret = 0;
813 goto err;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000814 }
815
816 for (i = 0; i < MAX_FILER_IDX + 1; i++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000817 local_rqfpr[j] = priv->ftp_rqfpr[i];
818 local_rqfcr[j] = priv->ftp_rqfcr[i];
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000819 j--;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000820 if ((priv->ftp_rqfcr[i] ==
821 (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
822 (priv->ftp_rqfpr[i] == cmp_rqfpr))
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000823 break;
824 }
825
826 if (i == MAX_FILER_IDX + 1) {
Joe Perches375d6a12013-04-13 19:03:18 +0000827 netdev_err(priv->ndev,
828 "No parse rule found, can't create hash rules\n");
Wang Shaoyan588dc912011-08-11 17:07:25 +0000829 ret = 0;
830 goto err;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000831 }
832
833 /* If a match was found, then it begins the starting of a cluster rule
834 * if it was already programmed, we need to overwrite these rules
835 */
836 for (l = i+1; l < MAX_FILER_IDX; l++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000837 if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000838 !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000839 priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000840 RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000841 priv->ftp_rqfpr[l] = FPR_FILER_MASK;
842 gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000843 priv->ftp_rqfpr[l]);
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000844 break;
845 }
846
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000847 if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
848 (priv->ftp_rqfcr[l] & RQFCR_AND))
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000849 continue;
850 else {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000851 local_rqfpr[j] = priv->ftp_rqfpr[l];
852 local_rqfcr[j] = priv->ftp_rqfcr[l];
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000853 j--;
854 }
855 }
856
857 priv->cur_filer_idx = l - 1;
858 last_rule_idx = l;
859
860 /* hash rules */
861 ethflow_to_filer_rules(priv, ethflow);
862
863 /* Write back the popped out rules again */
864 for (k = j+1; k < MAX_FILER_IDX; k++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000865 priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
866 priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000867 gfar_write_filer(priv, priv->cur_filer_idx,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000868 local_rqfcr[k], local_rqfpr[k]);
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000869 if (!priv->cur_filer_idx)
870 break;
871 priv->cur_filer_idx = priv->cur_filer_idx - 1;
872 }
873
Wang Shaoyan588dc912011-08-11 17:07:25 +0000874err:
875 kfree(local_rqfcr);
876 kfree(local_rqfpr);
877 return ret;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000878}
879
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000880static int gfar_set_hash_opts(struct gfar_private *priv,
881 struct ethtool_rxnfc *cmd)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000882{
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000883 /* write the filer rules here */
884 if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
Ben Hutchingsbde35282011-04-08 13:45:11 +0000885 return -EINVAL;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000886
887 return 0;
888}
889
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700890static int gfar_check_filer_hardware(struct gfar_private *priv)
891{
892 struct gfar __iomem *regs = NULL;
893 u32 i;
894
895 regs = priv->gfargrp[0].regs;
896
897 /* Check if we are in FIFO mode */
898 i = gfar_read(&regs->ecntrl);
899 i &= ECNTRL_FIFM;
900 if (i == ECNTRL_FIFM) {
901 netdev_notice(priv->ndev, "Interface in FIFO mode\n");
902 i = gfar_read(&regs->rctrl);
903 i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
904 if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
905 netdev_info(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000906 "Receive Queue Filtering enabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700907 } else {
908 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000909 "Receive Queue Filtering disabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700910 return -EOPNOTSUPP;
911 }
912 }
913 /* Or in standard mode */
914 else {
915 i = gfar_read(&regs->rctrl);
916 i &= RCTRL_PRSDEP_MASK;
917 if (i == RCTRL_PRSDEP_MASK) {
918 netdev_info(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000919 "Receive Queue Filtering enabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700920 } else {
921 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000922 "Receive Queue Filtering disabled\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700923 return -EOPNOTSUPP;
924 }
925 }
926
927 /* Sets the properties for arbitrary filer rule
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000928 * to the first 4 Layer 4 Bytes
929 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700930 regs->rbifx = 0xC0C1C2C3;
931 return 0;
932}
933
934static int gfar_comp_asc(const void *a, const void *b)
935{
936 return memcmp(a, b, 4);
937}
938
939static int gfar_comp_desc(const void *a, const void *b)
940{
941 return -memcmp(a, b, 4);
942}
943
944static void gfar_swap(void *a, void *b, int size)
945{
946 u32 *_a = a;
947 u32 *_b = b;
948
949 swap(_a[0], _b[0]);
950 swap(_a[1], _b[1]);
951 swap(_a[2], _b[2]);
952 swap(_a[3], _b[3]);
953}
954
955/* Write a mask to filer cache */
956static void gfar_set_mask(u32 mask, struct filer_table *tab)
957{
958 tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
959 tab->fe[tab->index].prop = mask;
960 tab->index++;
961}
962
963/* Sets parse bits (e.g. IP or TCP) */
964static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
965{
966 gfar_set_mask(mask, tab);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000967 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
968 RQFCR_AND;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700969 tab->fe[tab->index].prop = value;
970 tab->index++;
971}
972
973static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000974 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700975{
976 gfar_set_mask(mask, tab);
977 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
978 tab->fe[tab->index].prop = value;
979 tab->index++;
980}
981
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000982/* For setting a tuple of value and mask of type flag
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700983 * Example:
984 * IP-Src = 10.0.0.0/255.0.0.0
985 * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
986 *
987 * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
988 * For a don't care mask it gives us a 0
989 *
990 * The check if don't care and the mask adjustment if mask=0 is done for VLAN
991 * and MAC stuff on an upper level (due to missing information on this level).
992 * For these guys we can discard them if they are value=0 and mask=0.
993 *
994 * Further the all masks are one-padded for better hardware efficiency.
995 */
996static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +0000997 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700998{
999 switch (flag) {
Sebastian Poehn380b1532011-07-07 04:30:29 -07001000 /* 3bit */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001001 case RQFCR_PID_PRI:
1002 if (!(value | mask))
1003 return;
1004 mask |= RQFCR_PID_PRI_MASK;
1005 break;
1006 /* 8bit */
1007 case RQFCR_PID_L4P:
1008 case RQFCR_PID_TOS:
1009 if (!~(mask | RQFCR_PID_L4P_MASK))
1010 return;
1011 if (!mask)
1012 mask = ~0;
1013 else
1014 mask |= RQFCR_PID_L4P_MASK;
1015 break;
1016 /* 12bit */
1017 case RQFCR_PID_VID:
1018 if (!(value | mask))
1019 return;
1020 mask |= RQFCR_PID_VID_MASK;
1021 break;
1022 /* 16bit */
1023 case RQFCR_PID_DPT:
1024 case RQFCR_PID_SPT:
1025 case RQFCR_PID_ETY:
1026 if (!~(mask | RQFCR_PID_PORT_MASK))
1027 return;
1028 if (!mask)
1029 mask = ~0;
1030 else
1031 mask |= RQFCR_PID_PORT_MASK;
1032 break;
1033 /* 24bit */
1034 case RQFCR_PID_DAH:
1035 case RQFCR_PID_DAL:
1036 case RQFCR_PID_SAH:
1037 case RQFCR_PID_SAL:
1038 if (!(value | mask))
1039 return;
1040 mask |= RQFCR_PID_MAC_MASK;
1041 break;
1042 /* for all real 32bit masks */
1043 default:
1044 if (!~mask)
1045 return;
1046 if (!mask)
1047 mask = ~0;
1048 break;
1049 }
1050 gfar_set_general_attribute(value, mask, flag, tab);
1051}
1052
1053/* Translates value and mask for UDP, TCP or SCTP */
1054static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001055 struct ethtool_tcpip4_spec *mask,
1056 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001057{
1058 gfar_set_attribute(value->ip4src, mask->ip4src, RQFCR_PID_SIA, tab);
1059 gfar_set_attribute(value->ip4dst, mask->ip4dst, RQFCR_PID_DIA, tab);
1060 gfar_set_attribute(value->pdst, mask->pdst, RQFCR_PID_DPT, tab);
1061 gfar_set_attribute(value->psrc, mask->psrc, RQFCR_PID_SPT, tab);
1062 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1063}
1064
1065/* Translates value and mask for RAW-IP4 */
1066static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001067 struct ethtool_usrip4_spec *mask,
1068 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001069{
1070 gfar_set_attribute(value->ip4src, mask->ip4src, RQFCR_PID_SIA, tab);
1071 gfar_set_attribute(value->ip4dst, mask->ip4dst, RQFCR_PID_DIA, tab);
1072 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1073 gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
1074 gfar_set_attribute(value->l4_4_bytes, mask->l4_4_bytes, RQFCR_PID_ARB,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001075 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001076
1077}
1078
1079/* Translates value and mask for ETHER spec */
1080static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001081 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001082{
1083 u32 upper_temp_mask = 0;
1084 u32 lower_temp_mask = 0;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001085
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001086 /* Source address */
1087 if (!is_broadcast_ether_addr(mask->h_source)) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001088 if (is_zero_ether_addr(mask->h_source)) {
1089 upper_temp_mask = 0xFFFFFFFF;
1090 lower_temp_mask = 0xFFFFFFFF;
1091 } else {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001092 upper_temp_mask = mask->h_source[0] << 16 |
1093 mask->h_source[1] << 8 |
1094 mask->h_source[2];
1095 lower_temp_mask = mask->h_source[3] << 16 |
1096 mask->h_source[4] << 8 |
1097 mask->h_source[5];
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001098 }
1099 /* Upper 24bit */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001100 gfar_set_attribute(value->h_source[0] << 16 |
1101 value->h_source[1] << 8 |
1102 value->h_source[2],
1103 upper_temp_mask, RQFCR_PID_SAH, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001104 /* And the same for the lower part */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001105 gfar_set_attribute(value->h_source[3] << 16 |
1106 value->h_source[4] << 8 |
1107 value->h_source[5],
1108 lower_temp_mask, RQFCR_PID_SAL, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001109 }
1110 /* Destination address */
1111 if (!is_broadcast_ether_addr(mask->h_dest)) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001112 /* Special for destination is limited broadcast */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001113 if ((is_broadcast_ether_addr(value->h_dest) &&
1114 is_zero_ether_addr(mask->h_dest))) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001115 gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
1116 } else {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001117 if (is_zero_ether_addr(mask->h_dest)) {
1118 upper_temp_mask = 0xFFFFFFFF;
1119 lower_temp_mask = 0xFFFFFFFF;
1120 } else {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001121 upper_temp_mask = mask->h_dest[0] << 16 |
1122 mask->h_dest[1] << 8 |
1123 mask->h_dest[2];
1124 lower_temp_mask = mask->h_dest[3] << 16 |
1125 mask->h_dest[4] << 8 |
1126 mask->h_dest[5];
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001127 }
1128
1129 /* Upper 24bit */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001130 gfar_set_attribute(value->h_dest[0] << 16 |
1131 value->h_dest[1] << 8 |
1132 value->h_dest[2],
1133 upper_temp_mask, RQFCR_PID_DAH, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001134 /* And the same for the lower part */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001135 gfar_set_attribute(value->h_dest[3] << 16 |
1136 value->h_dest[4] << 8 |
1137 value->h_dest[5],
1138 lower_temp_mask, RQFCR_PID_DAL, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001139 }
1140 }
1141
1142 gfar_set_attribute(value->h_proto, mask->h_proto, RQFCR_PID_ETY, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001143}
1144
1145/* Convert a rule to binary filter format of gianfar */
1146static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001147 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001148{
1149 u32 vlan = 0, vlan_mask = 0;
1150 u32 id = 0, id_mask = 0;
1151 u32 cfi = 0, cfi_mask = 0;
1152 u32 prio = 0, prio_mask = 0;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001153 u32 old_index = tab->index;
1154
1155 /* Check if vlan is wanted */
1156 if ((rule->flow_type & FLOW_EXT) && (rule->m_ext.vlan_tci != 0xFFFF)) {
1157 if (!rule->m_ext.vlan_tci)
1158 rule->m_ext.vlan_tci = 0xFFFF;
1159
1160 vlan = RQFPR_VLN;
1161 vlan_mask = RQFPR_VLN;
1162
1163 /* Separate the fields */
Sebastian Poehn380b1532011-07-07 04:30:29 -07001164 id = rule->h_ext.vlan_tci & VLAN_VID_MASK;
1165 id_mask = rule->m_ext.vlan_tci & VLAN_VID_MASK;
1166 cfi = rule->h_ext.vlan_tci & VLAN_CFI_MASK;
1167 cfi_mask = rule->m_ext.vlan_tci & VLAN_CFI_MASK;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001168 prio = (rule->h_ext.vlan_tci & VLAN_PRIO_MASK) >>
1169 VLAN_PRIO_SHIFT;
1170 prio_mask = (rule->m_ext.vlan_tci & VLAN_PRIO_MASK) >>
1171 VLAN_PRIO_SHIFT;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001172
Sebastian Poehn380b1532011-07-07 04:30:29 -07001173 if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001174 vlan |= RQFPR_CFI;
1175 vlan_mask |= RQFPR_CFI;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001176 } else if (cfi != VLAN_TAG_PRESENT &&
1177 cfi_mask == VLAN_TAG_PRESENT) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001178 vlan_mask |= RQFPR_CFI;
1179 }
1180 }
1181
1182 switch (rule->flow_type & ~FLOW_EXT) {
1183 case TCP_V4_FLOW:
1184 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001185 RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001186 gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001187 &rule->m_u.tcp_ip4_spec, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001188 break;
1189 case UDP_V4_FLOW:
1190 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001191 RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001192 gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001193 &rule->m_u.udp_ip4_spec, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001194 break;
1195 case SCTP_V4_FLOW:
1196 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001197 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001198 gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001199 gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
1200 (struct ethtool_tcpip4_spec *)&rule->m_u,
1201 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001202 break;
1203 case IP_USER_FLOW:
1204 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001205 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001206 gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001207 (struct ethtool_usrip4_spec *) &rule->m_u,
1208 tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001209 break;
1210 case ETHER_FLOW:
1211 if (vlan)
1212 gfar_set_parse_bits(vlan, vlan_mask, tab);
1213 gfar_set_ether((struct ethhdr *) &rule->h_u,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001214 (struct ethhdr *) &rule->m_u, tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001215 break;
1216 default:
1217 return -1;
1218 }
1219
1220 /* Set the vlan attributes in the end */
1221 if (vlan) {
1222 gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
1223 gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
1224 }
1225
1226 /* If there has been nothing written till now, it must be a default */
1227 if (tab->index == old_index) {
1228 gfar_set_mask(0xFFFFFFFF, tab);
1229 tab->fe[tab->index].ctrl = 0x20;
1230 tab->fe[tab->index].prop = 0x0;
1231 tab->index++;
1232 }
1233
1234 /* Remove last AND */
1235 tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
1236
1237 /* Specify which queue to use or to drop */
1238 if (rule->ring_cookie == RX_CLS_FLOW_DISC)
1239 tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
1240 else
1241 tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
1242
1243 /* Only big enough entries can be clustered */
1244 if (tab->index > (old_index + 2)) {
1245 tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
1246 tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
1247 }
1248
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001249 /* In rare cases the cache can be full while there is
1250 * free space in hw
1251 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001252 if (tab->index > MAX_FILER_CACHE_IDX - 1)
1253 return -EBUSY;
1254
1255 return 0;
1256}
1257
1258/* Copy size filer entries */
1259static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001260 struct gfar_filer_entry src[0], s32 size)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001261{
1262 while (size > 0) {
1263 size--;
1264 dst[size].ctrl = src[size].ctrl;
1265 dst[size].prop = src[size].prop;
1266 }
1267}
1268
1269/* Delete the contents of the filer-table between start and end
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001270 * and collapse them
1271 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001272static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
1273{
1274 int length;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001275
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001276 if (end > MAX_FILER_CACHE_IDX || end < begin)
1277 return -EINVAL;
1278
1279 end++;
1280 length = end - begin;
1281
1282 /* Copy */
1283 while (end < tab->index) {
1284 tab->fe[begin].ctrl = tab->fe[end].ctrl;
1285 tab->fe[begin++].prop = tab->fe[end++].prop;
1286
1287 }
1288 /* Fill up with don't cares */
1289 while (begin < tab->index) {
1290 tab->fe[begin].ctrl = 0x60;
1291 tab->fe[begin].prop = 0xFFFFFFFF;
1292 begin++;
1293 }
1294
1295 tab->index -= length;
1296 return 0;
1297}
1298
1299/* Make space on the wanted location */
1300static int gfar_expand_filer_entries(u32 begin, u32 length,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001301 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001302{
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001303 if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX ||
1304 begin > MAX_FILER_CACHE_IDX)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001305 return -EINVAL;
1306
1307 gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001308 tab->index - length + 1);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001309
1310 tab->index += length;
1311 return 0;
1312}
1313
1314static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
1315{
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001316 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
1317 start++) {
1318 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
1319 (RQFCR_AND | RQFCR_CLE))
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001320 return start;
1321 }
1322 return -1;
1323}
1324
1325static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
1326{
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001327 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
1328 start++) {
1329 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
1330 (RQFCR_CLE))
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001331 return start;
1332 }
1333 return -1;
1334}
1335
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001336/* Uses hardwares clustering option to reduce
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001337 * the number of filer table entries
1338 */
1339static void gfar_cluster_filer(struct filer_table *tab)
1340{
1341 s32 i = -1, j, iend, jend;
1342
1343 while ((i = gfar_get_next_cluster_start(++i, tab)) != -1) {
1344 j = i;
1345 while ((j = gfar_get_next_cluster_start(++j, tab)) != -1) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001346 /* The cluster entries self and the previous one
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001347 * (a mask) must be identical!
1348 */
1349 if (tab->fe[i].ctrl != tab->fe[j].ctrl)
1350 break;
1351 if (tab->fe[i].prop != tab->fe[j].prop)
1352 break;
1353 if (tab->fe[i - 1].ctrl != tab->fe[j - 1].ctrl)
1354 break;
1355 if (tab->fe[i - 1].prop != tab->fe[j - 1].prop)
1356 break;
1357 iend = gfar_get_next_cluster_end(i, tab);
1358 jend = gfar_get_next_cluster_end(j, tab);
1359 if (jend == -1 || iend == -1)
1360 break;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001361
1362 /* First we make some free space, where our cluster
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001363 * element should be. Then we copy it there and finally
1364 * delete in from its old location.
1365 */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001366 if (gfar_expand_filer_entries(iend, (jend - j), tab) ==
1367 -EINVAL)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001368 break;
1369
1370 gfar_copy_filer_entries(&(tab->fe[iend + 1]),
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001371 &(tab->fe[jend + 1]), jend - j);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001372
1373 if (gfar_trim_filer_entries(jend - 1,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001374 jend + (jend - j),
1375 tab) == -EINVAL)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001376 return;
1377
1378 /* Mask out cluster bit */
1379 tab->fe[iend].ctrl &= ~(RQFCR_CLE);
1380 }
1381 }
1382}
1383
Sebastian Poehn380b1532011-07-07 04:30:29 -07001384/* Swaps the masked bits of a1<>a2 and b1<>b2 */
1385static void gfar_swap_bits(struct gfar_filer_entry *a1,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001386 struct gfar_filer_entry *a2,
1387 struct gfar_filer_entry *b1,
1388 struct gfar_filer_entry *b2, u32 mask)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001389{
1390 u32 temp[4];
Sebastian Poehn380b1532011-07-07 04:30:29 -07001391 temp[0] = a1->ctrl & mask;
1392 temp[1] = a2->ctrl & mask;
1393 temp[2] = b1->ctrl & mask;
1394 temp[3] = b2->ctrl & mask;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001395
Sebastian Poehn380b1532011-07-07 04:30:29 -07001396 a1->ctrl &= ~mask;
1397 a2->ctrl &= ~mask;
1398 b1->ctrl &= ~mask;
1399 b2->ctrl &= ~mask;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001400
1401 a1->ctrl |= temp[1];
1402 a2->ctrl |= temp[0];
1403 b1->ctrl |= temp[3];
1404 b2->ctrl |= temp[2];
1405}
1406
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001407/* Generate a list consisting of masks values with their start and
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001408 * end of validity and block as indicator for parts belonging
1409 * together (glued by ANDs) in mask_table
1410 */
1411static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001412 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001413{
1414 u32 i, and_index = 0, block_index = 1;
1415
1416 for (i = 0; i < tab->index; i++) {
1417
1418 /* LSByte of control = 0 sets a mask */
1419 if (!(tab->fe[i].ctrl & 0xF)) {
1420 mask_table[and_index].mask = tab->fe[i].prop;
1421 mask_table[and_index].start = i;
1422 mask_table[and_index].block = block_index;
1423 if (and_index >= 1)
1424 mask_table[and_index - 1].end = i - 1;
1425 and_index++;
1426 }
Sebastian Poehn380b1532011-07-07 04:30:29 -07001427 /* cluster starts and ends will be separated because they should
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001428 * hold their position
1429 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001430 if (tab->fe[i].ctrl & RQFCR_CLE)
1431 block_index++;
1432 /* A not set AND indicates the end of a depended block */
1433 if (!(tab->fe[i].ctrl & RQFCR_AND))
1434 block_index++;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001435 }
1436
1437 mask_table[and_index - 1].end = i - 1;
1438
1439 return and_index;
1440}
1441
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001442/* Sorts the entries of mask_table by the values of the masks.
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001443 * Important: The 0xFF80 flags of the first and last entry of a
1444 * block must hold their position (which queue, CLusterEnable, ReJEct,
1445 * AND)
1446 */
1447static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001448 struct filer_table *temp_table, u32 and_index)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001449{
1450 /* Pointer to compare function (_asc or _desc) */
1451 int (*gfar_comp)(const void *, const void *);
1452
1453 u32 i, size = 0, start = 0, prev = 1;
1454 u32 old_first, old_last, new_first, new_last;
1455
1456 gfar_comp = &gfar_comp_desc;
1457
1458 for (i = 0; i < and_index; i++) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001459 if (prev != mask_table[i].block) {
1460 old_first = mask_table[start].start + 1;
1461 old_last = mask_table[i - 1].end;
1462 sort(mask_table + start, size,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001463 sizeof(struct gfar_mask_entry),
1464 gfar_comp, &gfar_swap);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001465
1466 /* Toggle order for every block. This makes the
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001467 * thing more efficient!
1468 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001469 if (gfar_comp == gfar_comp_desc)
1470 gfar_comp = &gfar_comp_asc;
1471 else
1472 gfar_comp = &gfar_comp_desc;
1473
1474 new_first = mask_table[start].start + 1;
1475 new_last = mask_table[i - 1].end;
1476
Sebastian Poehn380b1532011-07-07 04:30:29 -07001477 gfar_swap_bits(&temp_table->fe[new_first],
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001478 &temp_table->fe[old_first],
1479 &temp_table->fe[new_last],
1480 &temp_table->fe[old_last],
1481 RQFCR_QUEUE | RQFCR_CLE |
1482 RQFCR_RJE | RQFCR_AND);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001483
1484 start = i;
1485 size = 0;
1486 }
1487 size++;
1488 prev = mask_table[i].block;
1489 }
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001490}
1491
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001492/* Reduces the number of masks needed in the filer table to save entries
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001493 * This is done by sorting the masks of a depended block. A depended block is
1494 * identified by gluing ANDs or CLE. The sorting order toggles after every
1495 * block. Of course entries in scope of a mask must change their location with
1496 * it.
1497 */
1498static int gfar_optimize_filer_masks(struct filer_table *tab)
1499{
1500 struct filer_table *temp_table;
1501 struct gfar_mask_entry *mask_table;
1502
1503 u32 and_index = 0, previous_mask = 0, i = 0, j = 0, size = 0;
1504 s32 ret = 0;
1505
1506 /* We need a copy of the filer table because
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001507 * we want to change its order
1508 */
Thomas Meyerb8ffdbd2011-11-17 13:05:35 +00001509 temp_table = kmemdup(tab, sizeof(*temp_table), GFP_KERNEL);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001510 if (temp_table == NULL)
1511 return -ENOMEM;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001512
1513 mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001514 sizeof(struct gfar_mask_entry), GFP_KERNEL);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001515
1516 if (mask_table == NULL) {
1517 ret = -ENOMEM;
1518 goto end;
1519 }
1520
1521 and_index = gfar_generate_mask_table(mask_table, tab);
1522
1523 gfar_sort_mask_table(mask_table, temp_table, and_index);
1524
1525 /* Now we can copy the data from our duplicated filer table to
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001526 * the real one in the order the mask table says
1527 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001528 for (i = 0; i < and_index; i++) {
1529 size = mask_table[i].end - mask_table[i].start + 1;
1530 gfar_copy_filer_entries(&(tab->fe[j]),
1531 &(temp_table->fe[mask_table[i].start]), size);
1532 j += size;
1533 }
1534
1535 /* And finally we just have to check for duplicated masks and drop the
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001536 * second ones
1537 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001538 for (i = 0; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1539 if (tab->fe[i].ctrl == 0x80) {
1540 previous_mask = i++;
1541 break;
1542 }
1543 }
1544 for (; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1545 if (tab->fe[i].ctrl == 0x80) {
1546 if (tab->fe[i].prop == tab->fe[previous_mask].prop) {
1547 /* Two identical ones found!
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001548 * So drop the second one!
1549 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001550 gfar_trim_filer_entries(i, i, tab);
1551 } else
1552 /* Not identical! */
1553 previous_mask = i;
1554 }
1555 }
1556
1557 kfree(mask_table);
1558end: kfree(temp_table);
1559 return ret;
1560}
1561
1562/* Write the bit-pattern from software's buffer to hardware registers */
1563static int gfar_write_filer_table(struct gfar_private *priv,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001564 struct filer_table *tab)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001565{
1566 u32 i = 0;
1567 if (tab->index > MAX_FILER_IDX - 1)
1568 return -EBUSY;
1569
1570 /* Avoid inconsistent filer table to be processed */
1571 lock_rx_qs(priv);
1572
1573 /* Fill regular entries */
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001574 for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].ctrl);
1575 i++)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001576 gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
1577 /* Fill the rest with fall-troughs */
1578 for (; i < MAX_FILER_IDX - 1; i++)
1579 gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
1580 /* Last entry must be default accept
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001581 * because that's what people expect
1582 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001583 gfar_write_filer(priv, i, 0x20, 0x0);
1584
1585 unlock_rx_qs(priv);
1586
1587 return 0;
1588}
1589
1590static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001591 struct gfar_private *priv)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001592{
1593
1594 if (flow->flow_type & FLOW_EXT) {
1595 if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
1596 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001597 "User-specific data not supported!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001598 if (~flow->m_ext.vlan_etype)
1599 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001600 "VLAN-etype not supported!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001601 }
1602 if (flow->flow_type == IP_USER_FLOW)
1603 if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
1604 netdev_warn(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001605 "IP-Version differing from IPv4 not supported!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001606
1607 return 0;
1608}
1609
1610static int gfar_process_filer_changes(struct gfar_private *priv)
1611{
1612 struct ethtool_flow_spec_container *j;
1613 struct filer_table *tab;
1614 s32 i = 0;
1615 s32 ret = 0;
1616
1617 /* So index is set to zero, too! */
1618 tab = kzalloc(sizeof(*tab), GFP_KERNEL);
1619 if (tab == NULL)
1620 return -ENOMEM;
1621
1622 /* Now convert the existing filer data from flow_spec into
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001623 * filer tables binary format
1624 */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001625 list_for_each_entry(j, &priv->rx_list.list, list) {
1626 ret = gfar_convert_to_filer(&j->fs, tab);
1627 if (ret == -EBUSY) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001628 netdev_err(priv->ndev,
1629 "Rule not added: No free space!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001630 goto end;
1631 }
1632 if (ret == -1) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001633 netdev_err(priv->ndev,
1634 "Rule not added: Unsupported Flow-type!\n");
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001635 goto end;
1636 }
1637 }
1638
1639 i = tab->index;
1640
1641 /* Optimizations to save entries */
1642 gfar_cluster_filer(tab);
1643 gfar_optimize_filer_masks(tab);
1644
Joe Perches375d6a12013-04-13 19:03:18 +00001645 pr_debug("\tSummary:\n"
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001646 "\tData on hardware: %d\n"
1647 "\tCompression rate: %d%%\n",
1648 tab->index, 100 - (100 * tab->index) / i);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001649
1650 /* Write everything to hardware */
1651 ret = gfar_write_filer_table(priv, tab);
1652 if (ret == -EBUSY) {
1653 netdev_err(priv->ndev, "Rule not added: No free space!\n");
1654 goto end;
1655 }
1656
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001657end:
1658 kfree(tab);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001659 return ret;
1660}
1661
1662static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
1663{
1664 u32 i = 0;
1665
1666 for (i = 0; i < sizeof(flow->m_u); i++)
1667 flow->m_u.hdata[i] ^= 0xFF;
1668
1669 flow->m_ext.vlan_etype ^= 0xFFFF;
1670 flow->m_ext.vlan_tci ^= 0xFFFF;
1671 flow->m_ext.data[0] ^= ~0;
1672 flow->m_ext.data[1] ^= ~0;
1673}
1674
1675static int gfar_add_cls(struct gfar_private *priv,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001676 struct ethtool_rx_flow_spec *flow)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001677{
1678 struct ethtool_flow_spec_container *temp, *comp;
1679 int ret = 0;
1680
1681 temp = kmalloc(sizeof(*temp), GFP_KERNEL);
1682 if (temp == NULL)
1683 return -ENOMEM;
1684 memcpy(&temp->fs, flow, sizeof(temp->fs));
1685
1686 gfar_invert_masks(&temp->fs);
1687 ret = gfar_check_capability(&temp->fs, priv);
1688 if (ret)
1689 goto clean_mem;
1690 /* Link in the new element at the right @location */
1691 if (list_empty(&priv->rx_list.list)) {
1692 ret = gfar_check_filer_hardware(priv);
1693 if (ret != 0)
1694 goto clean_mem;
1695 list_add(&temp->list, &priv->rx_list.list);
1696 goto process;
1697 } else {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001698 list_for_each_entry(comp, &priv->rx_list.list, list) {
1699 if (comp->fs.location > flow->location) {
1700 list_add_tail(&temp->list, &comp->list);
1701 goto process;
1702 }
1703 if (comp->fs.location == flow->location) {
1704 netdev_err(priv->ndev,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001705 "Rule not added: ID %d not free!\n",
1706 flow->location);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001707 ret = -EBUSY;
1708 goto clean_mem;
1709 }
1710 }
1711 list_add_tail(&temp->list, &priv->rx_list.list);
1712 }
1713
1714process:
1715 ret = gfar_process_filer_changes(priv);
1716 if (ret)
1717 goto clean_list;
1718 priv->rx_list.count++;
1719 return ret;
1720
1721clean_list:
1722 list_del(&temp->list);
1723clean_mem:
1724 kfree(temp);
1725 return ret;
1726}
1727
1728static int gfar_del_cls(struct gfar_private *priv, u32 loc)
1729{
1730 struct ethtool_flow_spec_container *comp;
1731 u32 ret = -EINVAL;
1732
1733 if (list_empty(&priv->rx_list.list))
1734 return ret;
1735
1736 list_for_each_entry(comp, &priv->rx_list.list, list) {
1737 if (comp->fs.location == loc) {
1738 list_del(&comp->list);
1739 kfree(comp);
1740 priv->rx_list.count--;
1741 gfar_process_filer_changes(priv);
1742 ret = 0;
1743 break;
1744 }
1745 }
1746
1747 return ret;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001748}
1749
1750static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
1751{
1752 struct ethtool_flow_spec_container *comp;
1753 u32 ret = -EINVAL;
1754
1755 list_for_each_entry(comp, &priv->rx_list.list, list) {
1756 if (comp->fs.location == cmd->fs.location) {
1757 memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
1758 gfar_invert_masks(&cmd->fs);
1759 ret = 0;
1760 break;
1761 }
1762 }
1763
1764 return ret;
1765}
1766
1767static int gfar_get_cls_all(struct gfar_private *priv,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001768 struct ethtool_rxnfc *cmd, u32 *rule_locs)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001769{
1770 struct ethtool_flow_spec_container *comp;
1771 u32 i = 0;
1772
1773 list_for_each_entry(comp, &priv->rx_list.list, list) {
David S. Miller8decf862011-09-22 03:23:13 -04001774 if (i == cmd->rule_cnt)
1775 return -EMSGSIZE;
1776 rule_locs[i] = comp->fs.location;
1777 i++;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001778 }
1779
1780 cmd->data = MAX_FILER_IDX;
Ben Hutchings473e64e2011-09-06 13:52:47 +00001781 cmd->rule_cnt = i;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001782
1783 return 0;
1784}
1785
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001786static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1787{
1788 struct gfar_private *priv = netdev_priv(dev);
1789 int ret = 0;
1790
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001791 mutex_lock(&priv->rx_queue_access);
1792
1793 switch (cmd->cmd) {
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001794 case ETHTOOL_SRXFH:
1795 ret = gfar_set_hash_opts(priv, cmd);
1796 break;
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001797 case ETHTOOL_SRXCLSRLINS:
Ben Hutchings3a73e492012-01-03 11:59:30 +00001798 if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
1799 cmd->fs.ring_cookie >= priv->num_rx_queues) ||
1800 cmd->fs.location >= MAX_FILER_IDX) {
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001801 ret = -EINVAL;
1802 break;
1803 }
1804 ret = gfar_add_cls(priv, &cmd->fs);
1805 break;
1806 case ETHTOOL_SRXCLSRLDEL:
1807 ret = gfar_del_cls(priv, cmd->fs.location);
1808 break;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001809 default:
1810 ret = -EINVAL;
1811 }
1812
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001813 mutex_unlock(&priv->rx_queue_access);
1814
1815 return ret;
1816}
1817
1818static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001819 u32 *rule_locs)
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001820{
1821 struct gfar_private *priv = netdev_priv(dev);
1822 int ret = 0;
1823
1824 switch (cmd->cmd) {
1825 case ETHTOOL_GRXRINGS:
1826 cmd->data = priv->num_rx_queues;
1827 break;
1828 case ETHTOOL_GRXCLSRLCNT:
1829 cmd->rule_cnt = priv->rx_list.count;
1830 break;
1831 case ETHTOOL_GRXCLSRULE:
1832 ret = gfar_get_cls(priv, cmd);
1833 break;
1834 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings815c7db2011-09-06 13:49:12 +00001835 ret = gfar_get_cls_all(priv, cmd, rule_locs);
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001836 break;
1837 default:
1838 ret = -EINVAL;
1839 break;
1840 }
1841
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001842 return ret;
1843}
1844
Richard Cochran66636282012-04-03 22:59:19 +00001845int gfar_phc_index = -1;
Richard Cochran28889b72012-09-20 19:11:12 +00001846EXPORT_SYMBOL(gfar_phc_index);
Richard Cochran66636282012-04-03 22:59:19 +00001847
1848static int gfar_get_ts_info(struct net_device *dev,
1849 struct ethtool_ts_info *info)
1850{
1851 struct gfar_private *priv = netdev_priv(dev);
1852
1853 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001854 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
1855 SOF_TIMESTAMPING_SOFTWARE;
Richard Cochran66636282012-04-03 22:59:19 +00001856 info->phc_index = -1;
1857 return 0;
1858 }
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001859 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
1860 SOF_TIMESTAMPING_RX_HARDWARE |
1861 SOF_TIMESTAMPING_RAW_HARDWARE;
Richard Cochran66636282012-04-03 22:59:19 +00001862 info->phc_index = gfar_phc_index;
Jan Ceuleerscbfc60712012-06-05 03:42:15 +00001863 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1864 (1 << HWTSTAMP_TX_ON);
1865 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1866 (1 << HWTSTAMP_FILTER_ALL);
Richard Cochran66636282012-04-03 22:59:19 +00001867 return 0;
1868}
1869
Jeff Garzik7282d492006-09-13 14:30:00 -04001870const struct ethtool_ops gfar_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 .get_settings = gfar_gsettings,
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001872 .set_settings = gfar_ssettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 .get_drvinfo = gfar_gdrvinfo,
1874 .get_regs_len = gfar_reglen,
1875 .get_regs = gfar_get_regs,
1876 .get_link = ethtool_op_get_link,
1877 .get_coalesce = gfar_gcoalesce,
1878 .set_coalesce = gfar_scoalesce,
1879 .get_ringparam = gfar_gringparam,
1880 .set_ringparam = gfar_sringparam,
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001881 .get_pauseparam = gfar_gpauseparam,
1882 .set_pauseparam = gfar_spauseparam,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 .get_strings = gfar_gstrings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001884 .get_sset_count = gfar_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 .get_ethtool_stats = gfar_fill_stats,
Kumar Gala0bbaf062005-06-20 10:54:21 -05001886 .get_msglevel = gfar_get_msglevel,
1887 .set_msglevel = gfar_set_msglevel,
Scott Woodd87eb122008-07-11 18:04:45 -05001888#ifdef CONFIG_PM
1889 .get_wol = gfar_get_wol,
1890 .set_wol = gfar_set_wol,
1891#endif
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001892 .set_rxnfc = gfar_set_nfc,
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001893 .get_rxnfc = gfar_get_nfc,
Richard Cochran66636282012-04-03 22:59:19 +00001894 .get_ts_info = gfar_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895};