gianfar_ethtool.c 50.0 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  drivers/net/ethernet/freescale/gianfar_ethtool.c
L
Linus Torvalds 已提交
3 4 5 6 7 8
 *
 *  Gianfar Ethernet Driver
 *  Ethtool support for Gianfar Enet
 *  Based on e1000 ethtool support
 *
 *  Author: Andy Fleming
9
 *  Maintainer: Kumar Gala
10
 *  Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
L
Linus Torvalds 已提交
11
 *
W
Wu Jiajun-B06378 已提交
12
 *  Copyright 2003-2006, 2008-2009, 2011 Freescale Semiconductor, Inc.
L
Linus Torvalds 已提交
13
 *
14 15
 *  This software may be used and distributed according to
 *  the terms of the GNU Public License, Version 2, incorporated herein
L
Linus Torvalds 已提交
16 17 18
 *  by reference.
 */

19 20
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
21 22 23 24 25 26 27
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
28
#include <linux/net_tstamp.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/mm.h>

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <linux/module.h>
#include <linux/crc32.h>
#include <asm/types.h>
#include <linux/ethtool.h>
40 41
#include <linux/mii.h>
#include <linux/phy.h>
S
Sebastian Poehn 已提交
42
#include <linux/sort.h>
S
Sebastian Poehn 已提交
43
#include <linux/if_vlan.h>
L
Linus Torvalds 已提交
44 45 46

#include "gianfar.h"

47 48
extern int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue,
			      int rx_work_limit);
L
Linus Torvalds 已提交
49

50 51
#define GFAR_MAX_COAL_USECS 0xffff
#define GFAR_MAX_COAL_FRAMES 0xff
52
static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
53
			    u64 *buf);
54
static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
55 56 57 58 59 60 61 62 63 64
static int gfar_gcoalesce(struct net_device *dev,
			  struct ethtool_coalesce *cvals);
static int gfar_scoalesce(struct net_device *dev,
			  struct ethtool_coalesce *cvals);
static void gfar_gringparam(struct net_device *dev,
			    struct ethtool_ringparam *rvals);
static int gfar_sringparam(struct net_device *dev,
			   struct ethtool_ringparam *rvals);
static void gfar_gdrvinfo(struct net_device *dev,
			  struct ethtool_drvinfo *drvinfo);
L
Linus Torvalds 已提交
65

66
static const char stat_gstrings[][ETH_GSTRING_LEN] = {
L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	"rx-large-frame-errors",
	"rx-short-frame-errors",
	"rx-non-octet-errors",
	"rx-crc-errors",
	"rx-overrun-errors",
	"rx-busy-errors",
	"rx-babbling-errors",
	"rx-truncated-frames",
	"ethernet-bus-error",
	"tx-babbling-errors",
	"tx-underrun-errors",
	"rx-skb-missing-errors",
	"tx-timeout-errors",
	"tx-rx-64-frames",
	"tx-rx-65-127-frames",
	"tx-rx-128-255-frames",
	"tx-rx-256-511-frames",
	"tx-rx-512-1023-frames",
	"tx-rx-1024-1518-frames",
	"tx-rx-1519-1522-good-vlan",
	"rx-bytes",
	"rx-packets",
	"rx-fcs-errors",
	"receive-multicast-packet",
	"receive-broadcast-packet",
	"rx-control-frame-packets",
	"rx-pause-frame-packets",
	"rx-unknown-op-code",
	"rx-alignment-error",
	"rx-frame-length-error",
	"rx-code-error",
	"rx-carrier-sense-error",
	"rx-undersize-packets",
	"rx-oversize-packets",
	"rx-fragmented-frames",
	"rx-jabber-frames",
	"rx-dropped-frames",
	"tx-byte-counter",
	"tx-packets",
	"tx-multicast-packets",
	"tx-broadcast-packets",
	"tx-pause-control-frames",
	"tx-deferral-packets",
	"tx-excessive-deferral-packets",
	"tx-single-collision-packets",
	"tx-multiple-collision-packets",
	"tx-late-collision-packets",
	"tx-excessive-collision-packets",
	"tx-total-collision",
	"reserved",
	"tx-dropped-frames",
	"tx-jabber-frames",
	"tx-fcs-errors",
	"tx-control-frames",
	"tx-oversize-frames",
	"tx-undersize-frames",
	"tx-fragmented-frames",
};

126 127 128 129 130
/* Fill in a buffer with the strings which correspond to the
 * stats */
static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
{
	struct gfar_private *priv = netdev_priv(dev);
131

132
	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
133 134 135
		memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
	else
		memcpy(buf, stat_gstrings,
136
		       GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
137 138
}

L
Linus Torvalds 已提交
139 140 141 142
/* Fill in an array of 64-bit statistics from various sources.
 * This array will be appended to the end of the ethtool_stats
 * structure, and returned to user space
 */
143 144
static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
			    u64 *buf)
L
Linus Torvalds 已提交
145 146 147
{
	int i;
	struct gfar_private *priv = netdev_priv(dev);
148
	struct gfar __iomem *regs = priv->gfargrp[0].regs;
149
	atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
L
Linus Torvalds 已提交
150

151
	for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
152
		buf[i] = atomic64_read(&extra[i]);
153

154
	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
155
		u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
L
Linus Torvalds 已提交
156

157 158 159
		for (; i < GFAR_STATS_LEN; i++, rmon++)
			buf[i] = (u64) gfar_read(rmon);
	}
L
Linus Torvalds 已提交
160 161
}

162
static int gfar_sset_count(struct net_device *dev, int sset)
L
Linus Torvalds 已提交
163 164 165
{
	struct gfar_private *priv = netdev_priv(dev);

166 167
	switch (sset) {
	case ETH_SS_STATS:
168
		if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
169 170 171 172 173 174
			return GFAR_STATS_LEN;
		else
			return GFAR_EXTRA_STATS_LEN;
	default:
		return -EOPNOTSUPP;
	}
L
Linus Torvalds 已提交
175 176 177
}

/* Fills in the drvinfo structure with some basic info */
178 179
static void gfar_gdrvinfo(struct net_device *dev,
			  struct ethtool_drvinfo *drvinfo)
L
Linus Torvalds 已提交
180
{
181 182 183 184 185
	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
	strlcpy(drvinfo->version, gfar_driver_version,
		sizeof(drvinfo->version));
	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
	strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
L
Linus Torvalds 已提交
186 187 188 189
	drvinfo->regdump_len = 0;
	drvinfo->eedump_len = 0;
}

190 191 192 193 194 195 196 197 198 199 200 201 202

static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct gfar_private *priv = netdev_priv(dev);
	struct phy_device *phydev = priv->phydev;

	if (NULL == phydev)
		return -ENODEV;

	return phy_ethtool_sset(phydev, cmd);
}


L
Linus Torvalds 已提交
203
/* Return the current settings in the ethtool_cmd structure */
204
static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
L
Linus Torvalds 已提交
205 206
{
	struct gfar_private *priv = netdev_priv(dev);
207
	struct phy_device *phydev = priv->phydev;
208 209
	struct gfar_priv_rx_q *rx_queue = NULL;
	struct gfar_priv_tx_q *tx_queue = NULL;
210 211 212

	if (NULL == phydev)
		return -ENODEV;
213 214
	tx_queue = priv->tx_queue[0];
	rx_queue = priv->rx_queue[0];
215

216 217
	/* etsec-1.7 and older versions have only one txic
	 * and rxic regs although they support multiple queues */
218 219
	cmd->maxtxpkt = get_icft_value(tx_queue->txic);
	cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
L
Linus Torvalds 已提交
220

221
	return phy_ethtool_gset(phydev, cmd);
L
Linus Torvalds 已提交
222 223 224
}

/* Return the length of the register structure */
225
static int gfar_reglen(struct net_device *dev)
L
Linus Torvalds 已提交
226 227 228 229 230
{
	return sizeof (struct gfar);
}

/* Return a dump of the GFAR register space */
231 232
static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
			  void *regbuf)
L
Linus Torvalds 已提交
233 234 235
{
	int i;
	struct gfar_private *priv = netdev_priv(dev);
236
	u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
L
Linus Torvalds 已提交
237 238 239
	u32 *buf = (u32 *) regbuf;

	for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
240
		buf[i] = gfar_read(&theregs[i]);
L
Linus Torvalds 已提交
241 242 243 244
}

/* Convert microseconds to ethernet clock ticks, which changes
 * depending on what speed the controller is running at */
245 246
static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
				     unsigned int usecs)
L
Linus Torvalds 已提交
247 248 249 250
{
	unsigned int count;

	/* The timer is different, depending on the interface speed */
251 252
	switch (priv->phydev->speed) {
	case SPEED_1000:
L
Linus Torvalds 已提交
253 254
		count = GFAR_GBIT_TIME;
		break;
255
	case SPEED_100:
L
Linus Torvalds 已提交
256 257
		count = GFAR_100_TIME;
		break;
258
	case SPEED_10:
L
Linus Torvalds 已提交
259 260 261 262 263 264 265
	default:
		count = GFAR_10_TIME;
		break;
	}

	/* Make sure we return a number greater than 0
	 * if usecs > 0 */
266
	return (usecs * 1000 + count - 1) / count;
L
Linus Torvalds 已提交
267 268 269
}

/* Convert ethernet clock ticks to microseconds */
270 271
static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
				     unsigned int ticks)
L
Linus Torvalds 已提交
272 273 274 275
{
	unsigned int count;

	/* The timer is different, depending on the interface speed */
276 277
	switch (priv->phydev->speed) {
	case SPEED_1000:
L
Linus Torvalds 已提交
278 279
		count = GFAR_GBIT_TIME;
		break;
280
	case SPEED_100:
L
Linus Torvalds 已提交
281 282
		count = GFAR_100_TIME;
		break;
283
	case SPEED_10:
L
Linus Torvalds 已提交
284 285 286 287 288 289 290
	default:
		count = GFAR_10_TIME;
		break;
	}

	/* Make sure we return a number greater than 0 */
	/* if ticks is > 0 */
291
	return (ticks * count) / 1000;
L
Linus Torvalds 已提交
292 293 294 295
}

/* Get the coalescing parameters, and put them in the cvals
 * structure.  */
296 297
static int gfar_gcoalesce(struct net_device *dev,
			  struct ethtool_coalesce *cvals)
L
Linus Torvalds 已提交
298 299
{
	struct gfar_private *priv = netdev_priv(dev);
300 301
	struct gfar_priv_rx_q *rx_queue = NULL;
	struct gfar_priv_tx_q *tx_queue = NULL;
302 303 304 305
	unsigned long rxtime;
	unsigned long rxcount;
	unsigned long txtime;
	unsigned long txcount;
306

307
	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
308
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
309

310 311 312
	if (NULL == priv->phydev)
		return -ENODEV;

313 314
	rx_queue = priv->rx_queue[0];
	tx_queue = priv->tx_queue[0];
315 316 317 318 319

	rxtime  = get_ictt_value(rx_queue->rxic);
	rxcount = get_icft_value(rx_queue->rxic);
	txtime  = get_ictt_value(tx_queue->txic);
	txcount = get_icft_value(tx_queue->txic);
320 321
	cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
	cvals->rx_max_coalesced_frames = rxcount;
L
Linus Torvalds 已提交
322

323 324
	cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
	cvals->tx_max_coalesced_frames = txcount;
L
Linus Torvalds 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

	cvals->use_adaptive_rx_coalesce = 0;
	cvals->use_adaptive_tx_coalesce = 0;

	cvals->pkt_rate_low = 0;
	cvals->rx_coalesce_usecs_low = 0;
	cvals->rx_max_coalesced_frames_low = 0;
	cvals->tx_coalesce_usecs_low = 0;
	cvals->tx_max_coalesced_frames_low = 0;

	/* When the packet rate is below pkt_rate_high but above
	 * pkt_rate_low (both measured in packets per second) the
	 * normal {rx,tx}_* coalescing parameters are used.
	 */

	/* When the packet rate is (measured in packets per second)
	 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
	 * used.
	 */
	cvals->pkt_rate_high = 0;
	cvals->rx_coalesce_usecs_high = 0;
	cvals->rx_max_coalesced_frames_high = 0;
	cvals->tx_coalesce_usecs_high = 0;
	cvals->tx_max_coalesced_frames_high = 0;

	/* How often to do adaptive coalescing packet rate sampling,
	 * measured in seconds.  Must not be zero.
	 */
	cvals->rate_sample_interval = 0;

	return 0;
}

/* Change the coalescing values.
 * Both cvals->*_usecs and cvals->*_frames have to be > 0
 * in order for coalescing to be active
 */
362 363
static int gfar_scoalesce(struct net_device *dev,
			  struct ethtool_coalesce *cvals)
L
Linus Torvalds 已提交
364 365
{
	struct gfar_private *priv = netdev_priv(dev);
366
	int i = 0;
L
Linus Torvalds 已提交
367

368
	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
369 370
		return -EOPNOTSUPP;

L
Linus Torvalds 已提交
371
	/* Set up rx coalescing */
372 373
	/* As of now, we will enable/disable coalescing for all
	 * queues together in case of eTSEC2, this will be modified
374 375
	 * along with the ethtool interface
	 */
L
Linus Torvalds 已提交
376
	if ((cvals->rx_coalesce_usecs == 0) ||
377 378 379 380 381 382 383
	    (cvals->rx_max_coalesced_frames == 0)) {
		for (i = 0; i < priv->num_rx_queues; i++)
			priv->rx_queue[i]->rxcoalescing = 0;
	} else {
		for (i = 0; i < priv->num_rx_queues; i++)
			priv->rx_queue[i]->rxcoalescing = 1;
	}
L
Linus Torvalds 已提交
384

385 386 387 388 389
	if (NULL == priv->phydev)
		return -ENODEV;

	/* Check the bounds of the values */
	if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
390 391
		netdev_info(dev, "Coalescing is limited to %d microseconds\n",
			    GFAR_MAX_COAL_USECS);
392 393 394 395
		return -EINVAL;
	}

	if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
396 397
		netdev_info(dev, "Coalescing is limited to %d frames\n",
			    GFAR_MAX_COAL_FRAMES);
398 399 400
		return -EINVAL;
	}

401 402 403 404 405
	for (i = 0; i < priv->num_rx_queues; i++) {
		priv->rx_queue[i]->rxic = mk_ic_value(
			cvals->rx_max_coalesced_frames,
			gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
	}
L
Linus Torvalds 已提交
406 407 408

	/* Set up tx coalescing */
	if ((cvals->tx_coalesce_usecs == 0) ||
409 410 411 412 413 414 415
	    (cvals->tx_max_coalesced_frames == 0)) {
		for (i = 0; i < priv->num_tx_queues; i++)
			priv->tx_queue[i]->txcoalescing = 0;
	} else {
		for (i = 0; i < priv->num_tx_queues; i++)
			priv->tx_queue[i]->txcoalescing = 1;
	}
L
Linus Torvalds 已提交
416

417 418
	/* Check the bounds of the values */
	if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
419 420
		netdev_info(dev, "Coalescing is limited to %d microseconds\n",
			    GFAR_MAX_COAL_USECS);
421 422 423 424
		return -EINVAL;
	}

	if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
425 426
		netdev_info(dev, "Coalescing is limited to %d frames\n",
			    GFAR_MAX_COAL_FRAMES);
427 428 429
		return -EINVAL;
	}

430 431 432 433 434
	for (i = 0; i < priv->num_tx_queues; i++) {
		priv->tx_queue[i]->txic = mk_ic_value(
			cvals->tx_max_coalesced_frames,
			gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
	}
L
Linus Torvalds 已提交
435

436
	gfar_configure_coalescing_all(priv);
L
Linus Torvalds 已提交
437 438 439 440 441 442 443

	return 0;
}

/* Fills in rvals with the current ring parameters.  Currently,
 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
 * jumbo are ignored by the driver */
444 445
static void gfar_gringparam(struct net_device *dev,
			    struct ethtool_ringparam *rvals)
L
Linus Torvalds 已提交
446 447
{
	struct gfar_private *priv = netdev_priv(dev);
448 449 450
	struct gfar_priv_tx_q *tx_queue = NULL;
	struct gfar_priv_rx_q *rx_queue = NULL;

451 452
	tx_queue = priv->tx_queue[0];
	rx_queue = priv->rx_queue[0];
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460 461

	rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
	rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
	rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
	rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;

	/* Values changeable by the user.  The valid values are
	 * in the range 1 to the "*_max_pending" counterpart above.
	 */
462 463 464 465
	rvals->rx_pending = rx_queue->rx_ring_size;
	rvals->rx_mini_pending = rx_queue->rx_ring_size;
	rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
	rvals->tx_pending = tx_queue->tx_ring_size;
L
Linus Torvalds 已提交
466 467 468 469 470
}

/* Change the current ring parameters, stopping the controller if
 * necessary so that we don't mess things up while we're in
 * motion.  We wait for the ring to be clean before reallocating
471 472 473 474
 * the rings.
 */
static int gfar_sringparam(struct net_device *dev,
			   struct ethtool_ringparam *rvals)
L
Linus Torvalds 已提交
475 476
{
	struct gfar_private *priv = netdev_priv(dev);
477
	int err = 0, i = 0;
L
Linus Torvalds 已提交
478 479 480 481 482

	if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
		return -EINVAL;

	if (!is_power_of_2(rvals->rx_pending)) {
483
		netdev_err(dev, "Ring sizes must be a power of 2\n");
L
Linus Torvalds 已提交
484 485 486 487 488 489 490
		return -EINVAL;
	}

	if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
		return -EINVAL;

	if (!is_power_of_2(rvals->tx_pending)) {
491
		netdev_err(dev, "Ring sizes must be a power of 2\n");
L
Linus Torvalds 已提交
492 493 494
		return -EINVAL;
	}

495

496 497
	if (dev->flags & IFF_UP) {
		unsigned long flags;
L
Linus Torvalds 已提交
498

499
		/* Halt TX and RX, and process the frames which
500 501
		 * have already been received
		 */
502 503 504
		local_irq_save(flags);
		lock_tx_qs(priv);
		lock_rx_qs(priv);
A
Andy Fleming 已提交
505

506
		gfar_halt(priv);
A
Andy Fleming 已提交
507

508 509 510
		unlock_rx_qs(priv);
		unlock_tx_qs(priv);
		local_irq_restore(flags);
L
Linus Torvalds 已提交
511

512 513
		for (i = 0; i < priv->num_rx_queues; i++)
			gfar_clean_rx_ring(priv->rx_queue[i],
514
					   priv->rx_queue[i]->rx_ring_size);
515

516 517 518
		/* Now we take down the rings to rebuild them */
		stop_gfar(dev);
	}
L
Linus Torvalds 已提交
519

520
	/* Change the size */
521 522 523
	for (i = 0; i < priv->num_rx_queues; i++) {
		priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
		priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
524 525
		priv->tx_queue[i]->num_txbdfree =
			priv->tx_queue[i]->tx_ring_size;
526
	}
L
Linus Torvalds 已提交
527

528
	/* Rebuild the rings with the new size */
529
	if (dev->flags & IFF_UP) {
530
		err = startup_gfar(dev);
531
		netif_tx_wake_all_queues(dev);
532
	}
533 534
	return err;
}
L
Linus Torvalds 已提交
535

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
static void gfar_gpauseparam(struct net_device *dev,
			     struct ethtool_pauseparam *epause)
{
	struct gfar_private *priv = netdev_priv(dev);

	epause->autoneg = !!priv->pause_aneg_en;
	epause->rx_pause = !!priv->rx_pause_en;
	epause->tx_pause = !!priv->tx_pause_en;
}

static int gfar_spauseparam(struct net_device *dev,
			    struct ethtool_pauseparam *epause)
{
	struct gfar_private *priv = netdev_priv(dev);
	struct phy_device *phydev = priv->phydev;
	struct gfar __iomem *regs = priv->gfargrp[0].regs;
	u32 oldadv, newadv;

	if (!(phydev->supported & SUPPORTED_Pause) ||
	    (!(phydev->supported & SUPPORTED_Asym_Pause) &&
	     (epause->rx_pause != epause->tx_pause)))
		return -EINVAL;

	priv->rx_pause_en = priv->tx_pause_en = 0;
	if (epause->rx_pause) {
		priv->rx_pause_en = 1;

		if (epause->tx_pause) {
			priv->tx_pause_en = 1;
			/* FLOW_CTRL_RX & TX */
			newadv = ADVERTISED_Pause;
		} else  /* FLOW_CTLR_RX */
			newadv = ADVERTISED_Pause | ADVERTISED_Asym_Pause;
	} else if (epause->tx_pause) {
		priv->tx_pause_en = 1;
		/* FLOW_CTLR_TX */
		newadv = ADVERTISED_Asym_Pause;
	} else
		newadv = 0;

	if (epause->autoneg)
		priv->pause_aneg_en = 1;
	else
		priv->pause_aneg_en = 0;

	oldadv = phydev->advertising &
		(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
	if (oldadv != newadv) {
		phydev->advertising &=
			~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
		phydev->advertising |= newadv;
		if (phydev->autoneg)
			/* inform link partner of our
			 * new flow ctrl settings
			 */
			return phy_start_aneg(phydev);

		if (!epause->autoneg) {
			u32 tempval;
			tempval = gfar_read(&regs->maccfg1);
			tempval &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
			if (priv->tx_pause_en)
				tempval |= MACCFG1_TX_FLOW;
			if (priv->rx_pause_en)
				tempval |= MACCFG1_RX_FLOW;
			gfar_write(&regs->maccfg1, tempval);
		}
	}

	return 0;
}

608
int gfar_set_features(struct net_device *dev, netdev_features_t features)
609 610
{
	struct gfar_private *priv = netdev_priv(dev);
611
	unsigned long flags;
612
	int err = 0, i = 0;
613
	netdev_features_t changed = dev->features ^ features;
L
Linus Torvalds 已提交
614

615
	if (changed & (NETIF_F_HW_VLAN_CTAG_TX|NETIF_F_HW_VLAN_CTAG_RX))
J
Jiri Pirko 已提交
616 617
		gfar_vlan_mode(dev, features);

618 619
	if (!(changed & NETIF_F_RXCSUM))
		return 0;
620

621 622
	if (dev->flags & IFF_UP) {
		/* Halt TX and RX, and process the frames which
623 624
		 * have already been received
		 */
625 626 627
		local_irq_save(flags);
		lock_tx_qs(priv);
		lock_rx_qs(priv);
A
Andy Fleming 已提交
628

629
		gfar_halt(priv);
A
Andy Fleming 已提交
630

631 632
		unlock_tx_qs(priv);
		unlock_rx_qs(priv);
D
Dan Carpenter 已提交
633
		local_irq_restore(flags);
634

635 636
		for (i = 0; i < priv->num_rx_queues; i++)
			gfar_clean_rx_ring(priv->rx_queue[i],
637
					   priv->rx_queue[i]->rx_ring_size);
638

639
		/* Now we take down the rings to rebuild them */
L
Linus Torvalds 已提交
640 641
		stop_gfar(dev);

642
		dev->features = features;
L
Linus Torvalds 已提交
643 644

		err = startup_gfar(dev);
645
		netif_tx_wake_all_queues(dev);
646
	}
L
Linus Torvalds 已提交
647 648 649
	return err;
}

650
static uint32_t gfar_get_msglevel(struct net_device *dev)
651
{
652
	struct gfar_private *priv = netdev_priv(dev);
653

654
	return priv->msg_enable;
655 656
}

657
static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
658
{
659
	struct gfar_private *priv = netdev_priv(dev);
660

661 662 663
	priv->msg_enable = data;
}

664 665 666 667 668
#ifdef CONFIG_PM
static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct gfar_private *priv = netdev_priv(dev);

669
	if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
670 671 672 673 674 675 676 677 678 679 680 681
		wol->supported = WAKE_MAGIC;
		wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
	} else {
		wol->supported = wol->wolopts = 0;
	}
}

static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct gfar_private *priv = netdev_priv(dev);
	unsigned long flags;

682
	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
683 684 685 686 687 688
	    wol->wolopts != 0)
		return -EINVAL;

	if (wol->wolopts & ~WAKE_MAGIC)
		return -EINVAL;

689 690
	device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);

691
	spin_lock_irqsave(&priv->bflock, flags);
692
	priv->wol_en =  !!device_may_wakeup(&dev->dev);
693 694 695 696 697
	spin_unlock_irqrestore(&priv->bflock, flags);

	return 0;
}
#endif
698

699 700 701 702 703 704
static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
{
	u32 fcr = 0x0, fpr = FPR_FILER_MASK;

	if (ethflow & RXH_L2DA) {
		fcr = RQFCR_PID_DAH |RQFCR_CMP_NOMATCH |
705
		      RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
706 707
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
708 709 710 711
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
		priv->cur_filer_idx = priv->cur_filer_idx - 1;

		fcr = RQFCR_PID_DAL | RQFCR_AND | RQFCR_CMP_NOMATCH |
712
		      RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
713 714
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
715 716 717 718 719 720
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}

	if (ethflow & RXH_VLAN) {
		fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
721
		      RQFCR_AND | RQFCR_HASHTBL_0;
722
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
W
Wu Jiajun-B06378 已提交
723 724
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
725 726 727 728 729
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}

	if (ethflow & RXH_IP_SRC) {
		fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
730
		      RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
731 732
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
733 734 735 736 737 738
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}

	if (ethflow & (RXH_IP_DST)) {
		fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
739
		      RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
740 741
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
742 743 744 745 746 747
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}

	if (ethflow & RXH_L3_PROTO) {
		fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
748
		      RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
749 750
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
751 752 753 754 755 756
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}

	if (ethflow & RXH_L4_B_0_1) {
		fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
757
		      RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
758 759
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
760 761 762 763 764 765
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}

	if (ethflow & RXH_L4_B_2_3) {
		fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
766
		      RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
767 768
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
769 770 771 772 773
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}
}

774 775
static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
				       u64 class)
776 777 778
{
	unsigned int last_rule_idx = priv->cur_filer_idx;
	unsigned int cmp_rqfpr;
779 780
	unsigned int *local_rqfpr;
	unsigned int *local_rqfcr;
781 782
	int i = 0x0, k = 0x0;
	int j = MAX_FILER_IDX, l = 0x0;
783 784
	int ret = 1;

785 786 787 788
	local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
				    GFP_KERNEL);
	local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
				    GFP_KERNEL);
789 790 791 792
	if (!local_rqfpr || !local_rqfcr) {
		ret = 0;
		goto err;
	}
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

	switch (class) {
	case TCP_V4_FLOW:
		cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
		break;
	case UDP_V4_FLOW:
		cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
		break;
	case TCP_V6_FLOW:
		cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
		break;
	case UDP_V6_FLOW:
		cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
		break;
	default:
808 809
		netdev_err(priv->ndev,
			   "Right now this class is not supported\n");
810 811
		ret = 0;
		goto err;
812 813 814
	}

	for (i = 0; i < MAX_FILER_IDX + 1; i++) {
W
Wu Jiajun-B06378 已提交
815 816
		local_rqfpr[j] = priv->ftp_rqfpr[i];
		local_rqfcr[j] = priv->ftp_rqfcr[i];
817
		j--;
818 819 820
		if ((priv->ftp_rqfcr[i] ==
		     (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
		    (priv->ftp_rqfpr[i] == cmp_rqfpr))
821 822 823 824
			break;
	}

	if (i == MAX_FILER_IDX + 1) {
825 826
		netdev_err(priv->ndev,
			   "No parse rule found, can't create hash rules\n");
827 828
		ret = 0;
		goto err;
829 830 831 832 833 834
	}

	/* If a match was found, then it begins the starting of a cluster rule
	 * if it was already programmed, we need to overwrite these rules
	 */
	for (l = i+1; l < MAX_FILER_IDX; l++) {
W
Wu Jiajun-B06378 已提交
835
		if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
836
		    !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
W
Wu Jiajun-B06378 已提交
837
			priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
838
					     RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
W
Wu Jiajun-B06378 已提交
839 840
			priv->ftp_rqfpr[l] = FPR_FILER_MASK;
			gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
841
					 priv->ftp_rqfpr[l]);
842 843 844
			break;
		}

W
Wu Jiajun-B06378 已提交
845 846
		if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
			(priv->ftp_rqfcr[l] & RQFCR_AND))
847 848
			continue;
		else {
W
Wu Jiajun-B06378 已提交
849 850
			local_rqfpr[j] = priv->ftp_rqfpr[l];
			local_rqfcr[j] = priv->ftp_rqfcr[l];
851 852 853 854 855 856 857 858 859 860 861 862
			j--;
		}
	}

	priv->cur_filer_idx = l - 1;
	last_rule_idx = l;

	/* hash rules */
	ethflow_to_filer_rules(priv, ethflow);

	/* Write back the popped out rules again */
	for (k = j+1; k < MAX_FILER_IDX; k++) {
W
Wu Jiajun-B06378 已提交
863 864
		priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
		priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
865
		gfar_write_filer(priv, priv->cur_filer_idx,
866
				 local_rqfcr[k], local_rqfpr[k]);
867 868 869 870 871
		if (!priv->cur_filer_idx)
			break;
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}

872 873 874 875
err:
	kfree(local_rqfcr);
	kfree(local_rqfpr);
	return ret;
876 877
}

878 879
static int gfar_set_hash_opts(struct gfar_private *priv,
			      struct ethtool_rxnfc *cmd)
880 881 882
{
	/* write the filer rules here */
	if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
883
		return -EINVAL;
884 885 886 887

	return 0;
}

S
Sebastian Poehn 已提交
888 889
static int gfar_check_filer_hardware(struct gfar_private *priv)
{
890
	struct gfar __iomem *regs = priv->gfargrp[0].regs;
S
Sebastian Poehn 已提交
891 892 893 894 895 896 897 898 899 900 901
	u32 i;

	/* Check if we are in FIFO mode */
	i = gfar_read(&regs->ecntrl);
	i &= ECNTRL_FIFM;
	if (i == ECNTRL_FIFM) {
		netdev_notice(priv->ndev, "Interface in FIFO mode\n");
		i = gfar_read(&regs->rctrl);
		i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
		if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
			netdev_info(priv->ndev,
902
				    "Receive Queue Filtering enabled\n");
S
Sebastian Poehn 已提交
903 904
		} else {
			netdev_warn(priv->ndev,
905
				    "Receive Queue Filtering disabled\n");
S
Sebastian Poehn 已提交
906 907 908 909 910 911 912 913 914
			return -EOPNOTSUPP;
		}
	}
	/* Or in standard mode */
	else {
		i = gfar_read(&regs->rctrl);
		i &= RCTRL_PRSDEP_MASK;
		if (i == RCTRL_PRSDEP_MASK) {
			netdev_info(priv->ndev,
915
				    "Receive Queue Filtering enabled\n");
S
Sebastian Poehn 已提交
916 917
		} else {
			netdev_warn(priv->ndev,
918
				    "Receive Queue Filtering disabled\n");
S
Sebastian Poehn 已提交
919 920 921 922 923
			return -EOPNOTSUPP;
		}
	}

	/* Sets the properties for arbitrary filer rule
924 925
	 * to the first 4 Layer 4 Bytes
	 */
926
	gfar_write(&regs->rbifx, 0xC0C1C2C3);
S
Sebastian Poehn 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
	return 0;
}

static int gfar_comp_asc(const void *a, const void *b)
{
	return memcmp(a, b, 4);
}

static int gfar_comp_desc(const void *a, const void *b)
{
	return -memcmp(a, b, 4);
}

static void gfar_swap(void *a, void *b, int size)
{
	u32 *_a = a;
	u32 *_b = b;

	swap(_a[0], _b[0]);
	swap(_a[1], _b[1]);
	swap(_a[2], _b[2]);
	swap(_a[3], _b[3]);
}

/* Write a mask to filer cache */
static void gfar_set_mask(u32 mask, struct filer_table *tab)
{
	tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
	tab->fe[tab->index].prop = mask;
	tab->index++;
}

/* Sets parse bits (e.g. IP or TCP) */
static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
{
	gfar_set_mask(mask, tab);
963 964
	tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
				   RQFCR_AND;
S
Sebastian Poehn 已提交
965 966 967 968 969
	tab->fe[tab->index].prop = value;
	tab->index++;
}

static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
970
				       struct filer_table *tab)
S
Sebastian Poehn 已提交
971 972 973 974 975 976 977
{
	gfar_set_mask(mask, tab);
	tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
	tab->fe[tab->index].prop = value;
	tab->index++;
}

978
/* For setting a tuple of value and mask of type flag
S
Sebastian Poehn 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992
 * Example:
 * IP-Src = 10.0.0.0/255.0.0.0
 * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
 *
 * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
 * For a don't care mask it gives us a 0
 *
 * The check if don't care and the mask adjustment if mask=0 is done for VLAN
 * and MAC stuff on an upper level (due to missing information on this level).
 * For these guys we can discard them if they are value=0 and mask=0.
 *
 * Further the all masks are one-padded for better hardware efficiency.
 */
static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
993
			       struct filer_table *tab)
S
Sebastian Poehn 已提交
994 995
{
	switch (flag) {
S
Sebastian Poehn 已提交
996
		/* 3bit */
S
Sebastian Poehn 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	case RQFCR_PID_PRI:
		if (!(value | mask))
			return;
		mask |= RQFCR_PID_PRI_MASK;
		break;
		/* 8bit */
	case RQFCR_PID_L4P:
	case RQFCR_PID_TOS:
		if (!~(mask | RQFCR_PID_L4P_MASK))
			return;
		if (!mask)
			mask = ~0;
		else
			mask |= RQFCR_PID_L4P_MASK;
		break;
		/* 12bit */
	case RQFCR_PID_VID:
		if (!(value | mask))
			return;
		mask |= RQFCR_PID_VID_MASK;
		break;
		/* 16bit */
	case RQFCR_PID_DPT:
	case RQFCR_PID_SPT:
	case RQFCR_PID_ETY:
		if (!~(mask | RQFCR_PID_PORT_MASK))
			return;
		if (!mask)
			mask = ~0;
		else
			mask |= RQFCR_PID_PORT_MASK;
		break;
		/* 24bit */
	case RQFCR_PID_DAH:
	case RQFCR_PID_DAL:
	case RQFCR_PID_SAH:
	case RQFCR_PID_SAL:
		if (!(value | mask))
			return;
		mask |= RQFCR_PID_MAC_MASK;
		break;
		/* for all real 32bit masks */
	default:
		if (!~mask)
			return;
		if (!mask)
			mask = ~0;
		break;
	}
	gfar_set_general_attribute(value, mask, flag, tab);
}

/* Translates value and mask for UDP, TCP or SCTP */
static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
1051 1052
			      struct ethtool_tcpip4_spec *mask,
			      struct filer_table *tab)
S
Sebastian Poehn 已提交
1053
{
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	gfar_set_attribute(be32_to_cpu(value->ip4src),
			   be32_to_cpu(mask->ip4src),
			   RQFCR_PID_SIA, tab);
	gfar_set_attribute(be32_to_cpu(value->ip4dst),
			   be32_to_cpu(mask->ip4dst),
			   RQFCR_PID_DIA, tab);
	gfar_set_attribute(be16_to_cpu(value->pdst),
			   be16_to_cpu(mask->pdst),
			   RQFCR_PID_DPT, tab);
	gfar_set_attribute(be16_to_cpu(value->psrc),
			   be16_to_cpu(mask->psrc),
			   RQFCR_PID_SPT, tab);
S
Sebastian Poehn 已提交
1066 1067 1068 1069 1070
	gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
}

/* Translates value and mask for RAW-IP4 */
static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
1071 1072
			     struct ethtool_usrip4_spec *mask,
			     struct filer_table *tab)
S
Sebastian Poehn 已提交
1073
{
1074 1075 1076 1077 1078 1079
	gfar_set_attribute(be32_to_cpu(value->ip4src),
			   be32_to_cpu(mask->ip4src),
			   RQFCR_PID_SIA, tab);
	gfar_set_attribute(be32_to_cpu(value->ip4dst),
			   be32_to_cpu(mask->ip4dst),
			   RQFCR_PID_DIA, tab);
S
Sebastian Poehn 已提交
1080 1081
	gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
	gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
1082 1083 1084
	gfar_set_attribute(be32_to_cpu(value->l4_4_bytes),
			   be32_to_cpu(mask->l4_4_bytes),
			   RQFCR_PID_ARB, tab);
S
Sebastian Poehn 已提交
1085 1086 1087 1088 1089

}

/* Translates value and mask for ETHER spec */
static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
1090
			   struct filer_table *tab)
S
Sebastian Poehn 已提交
1091 1092 1093
{
	u32 upper_temp_mask = 0;
	u32 lower_temp_mask = 0;
1094

S
Sebastian Poehn 已提交
1095 1096 1097 1098 1099 1100
	/* Source address */
	if (!is_broadcast_ether_addr(mask->h_source)) {
		if (is_zero_ether_addr(mask->h_source)) {
			upper_temp_mask = 0xFFFFFFFF;
			lower_temp_mask = 0xFFFFFFFF;
		} else {
1101 1102 1103 1104 1105 1106
			upper_temp_mask = mask->h_source[0] << 16 |
					  mask->h_source[1] << 8  |
					  mask->h_source[2];
			lower_temp_mask = mask->h_source[3] << 16 |
					  mask->h_source[4] << 8  |
					  mask->h_source[5];
S
Sebastian Poehn 已提交
1107 1108
		}
		/* Upper 24bit */
1109 1110 1111 1112
		gfar_set_attribute(value->h_source[0] << 16 |
				   value->h_source[1] << 8  |
				   value->h_source[2],
				   upper_temp_mask, RQFCR_PID_SAH, tab);
S
Sebastian Poehn 已提交
1113
		/* And the same for the lower part */
1114 1115 1116 1117
		gfar_set_attribute(value->h_source[3] << 16 |
				   value->h_source[4] << 8  |
				   value->h_source[5],
				   lower_temp_mask, RQFCR_PID_SAL, tab);
S
Sebastian Poehn 已提交
1118 1119 1120 1121
	}
	/* Destination address */
	if (!is_broadcast_ether_addr(mask->h_dest)) {
		/* Special for destination is limited broadcast */
1122 1123
		if ((is_broadcast_ether_addr(value->h_dest) &&
		    is_zero_ether_addr(mask->h_dest))) {
S
Sebastian Poehn 已提交
1124 1125 1126 1127 1128 1129
			gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
		} else {
			if (is_zero_ether_addr(mask->h_dest)) {
				upper_temp_mask = 0xFFFFFFFF;
				lower_temp_mask = 0xFFFFFFFF;
			} else {
1130 1131 1132 1133 1134 1135
				upper_temp_mask = mask->h_dest[0] << 16 |
						  mask->h_dest[1] << 8  |
						  mask->h_dest[2];
				lower_temp_mask = mask->h_dest[3] << 16 |
						  mask->h_dest[4] << 8  |
						  mask->h_dest[5];
S
Sebastian Poehn 已提交
1136 1137 1138
			}

			/* Upper 24bit */
1139 1140 1141 1142
			gfar_set_attribute(value->h_dest[0] << 16 |
					   value->h_dest[1] << 8  |
					   value->h_dest[2],
					   upper_temp_mask, RQFCR_PID_DAH, tab);
S
Sebastian Poehn 已提交
1143
			/* And the same for the lower part */
1144 1145 1146 1147
			gfar_set_attribute(value->h_dest[3] << 16 |
					   value->h_dest[4] << 8  |
					   value->h_dest[5],
					   lower_temp_mask, RQFCR_PID_DAL, tab);
S
Sebastian Poehn 已提交
1148 1149 1150
		}
	}

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	gfar_set_attribute(be16_to_cpu(value->h_proto),
			   be16_to_cpu(mask->h_proto),
			   RQFCR_PID_ETY, tab);
}

static inline u32 vlan_tci_vid(struct ethtool_rx_flow_spec *rule)
{
	return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_VID_MASK;
}

static inline u32 vlan_tci_vidm(struct ethtool_rx_flow_spec *rule)
{
	return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_VID_MASK;
}

static inline u32 vlan_tci_cfi(struct ethtool_rx_flow_spec *rule)
{
	return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_CFI_MASK;
}

static inline u32 vlan_tci_cfim(struct ethtool_rx_flow_spec *rule)
{
	return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_CFI_MASK;
}

static inline u32 vlan_tci_prio(struct ethtool_rx_flow_spec *rule)
{
	return (be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_PRIO_MASK) >>
		VLAN_PRIO_SHIFT;
}

static inline u32 vlan_tci_priom(struct ethtool_rx_flow_spec *rule)
{
	return (be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_PRIO_MASK) >>
		VLAN_PRIO_SHIFT;
S
Sebastian Poehn 已提交
1186 1187 1188 1189
}

/* Convert a rule to binary filter format of gianfar */
static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
1190
				 struct filer_table *tab)
S
Sebastian Poehn 已提交
1191 1192 1193 1194 1195 1196 1197 1198
{
	u32 vlan = 0, vlan_mask = 0;
	u32 id = 0, id_mask = 0;
	u32 cfi = 0, cfi_mask = 0;
	u32 prio = 0, prio_mask = 0;
	u32 old_index = tab->index;

	/* Check if vlan is wanted */
1199 1200
	if ((rule->flow_type & FLOW_EXT) &&
	    (rule->m_ext.vlan_tci != cpu_to_be16(0xFFFF))) {
S
Sebastian Poehn 已提交
1201
		if (!rule->m_ext.vlan_tci)
1202
			rule->m_ext.vlan_tci = cpu_to_be16(0xFFFF);
S
Sebastian Poehn 已提交
1203 1204 1205 1206 1207

		vlan = RQFPR_VLN;
		vlan_mask = RQFPR_VLN;

		/* Separate the fields */
1208 1209 1210 1211 1212 1213
		id = vlan_tci_vid(rule);
		id_mask = vlan_tci_vidm(rule);
		cfi = vlan_tci_cfi(rule);
		cfi_mask = vlan_tci_cfim(rule);
		prio = vlan_tci_prio(rule);
		prio_mask = vlan_tci_priom(rule);
S
Sebastian Poehn 已提交
1214 1215

		if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
S
Sebastian Poehn 已提交
1216 1217
			vlan |= RQFPR_CFI;
			vlan_mask |= RQFPR_CFI;
1218 1219
		} else if (cfi != VLAN_TAG_PRESENT &&
			   cfi_mask == VLAN_TAG_PRESENT) {
S
Sebastian Poehn 已提交
1220 1221 1222 1223 1224 1225 1226
			vlan_mask |= RQFPR_CFI;
		}
	}

	switch (rule->flow_type & ~FLOW_EXT) {
	case TCP_V4_FLOW:
		gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
1227
				    RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
S
Sebastian Poehn 已提交
1228
		gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
1229
				  &rule->m_u.tcp_ip4_spec, tab);
S
Sebastian Poehn 已提交
1230 1231 1232
		break;
	case UDP_V4_FLOW:
		gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
1233
				    RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
S
Sebastian Poehn 已提交
1234
		gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
1235
				  &rule->m_u.udp_ip4_spec, tab);
S
Sebastian Poehn 已提交
1236 1237 1238
		break;
	case SCTP_V4_FLOW:
		gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1239
				    tab);
S
Sebastian Poehn 已提交
1240
		gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
1241 1242 1243
		gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
				  (struct ethtool_tcpip4_spec *)&rule->m_u,
				  tab);
S
Sebastian Poehn 已提交
1244 1245 1246
		break;
	case IP_USER_FLOW:
		gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1247
				    tab);
S
Sebastian Poehn 已提交
1248
		gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
1249 1250
				 (struct ethtool_usrip4_spec *) &rule->m_u,
				 tab);
S
Sebastian Poehn 已提交
1251 1252 1253 1254 1255
		break;
	case ETHER_FLOW:
		if (vlan)
			gfar_set_parse_bits(vlan, vlan_mask, tab);
		gfar_set_ether((struct ethhdr *) &rule->h_u,
1256
			       (struct ethhdr *) &rule->m_u, tab);
S
Sebastian Poehn 已提交
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
		break;
	default:
		return -1;
	}

	/* Set the vlan attributes in the end */
	if (vlan) {
		gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
		gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
	}

	/* If there has been nothing written till now, it must be a default */
	if (tab->index == old_index) {
		gfar_set_mask(0xFFFFFFFF, tab);
		tab->fe[tab->index].ctrl = 0x20;
		tab->fe[tab->index].prop = 0x0;
		tab->index++;
	}

	/* Remove last AND */
	tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);

	/* Specify which queue to use or to drop */
	if (rule->ring_cookie == RX_CLS_FLOW_DISC)
		tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
	else
		tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);

	/* Only big enough entries can be clustered */
	if (tab->index > (old_index + 2)) {
		tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
		tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
	}

1291 1292 1293
	/* In rare cases the cache can be full while there is
	 * free space in hw
	 */
S
Sebastian Poehn 已提交
1294 1295 1296 1297 1298 1299 1300 1301
	if (tab->index > MAX_FILER_CACHE_IDX - 1)
		return -EBUSY;

	return 0;
}

/* Copy size filer entries */
static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
1302
				    struct gfar_filer_entry src[0], s32 size)
S
Sebastian Poehn 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311
{
	while (size > 0) {
		size--;
		dst[size].ctrl = src[size].ctrl;
		dst[size].prop = src[size].prop;
	}
}

/* Delete the contents of the filer-table between start and end
1312 1313
 * and collapse them
 */
S
Sebastian Poehn 已提交
1314 1315 1316
static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
{
	int length;
1317

S
Sebastian Poehn 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
	if (end > MAX_FILER_CACHE_IDX || end < begin)
		return -EINVAL;

	end++;
	length = end - begin;

	/* Copy */
	while (end < tab->index) {
		tab->fe[begin].ctrl = tab->fe[end].ctrl;
		tab->fe[begin++].prop = tab->fe[end++].prop;

	}
	/* Fill up with don't cares */
	while (begin < tab->index) {
		tab->fe[begin].ctrl = 0x60;
		tab->fe[begin].prop = 0xFFFFFFFF;
		begin++;
	}

	tab->index -= length;
	return 0;
}

/* Make space on the wanted location */
static int gfar_expand_filer_entries(u32 begin, u32 length,
1343
				     struct filer_table *tab)
S
Sebastian Poehn 已提交
1344
{
1345 1346
	if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX ||
	    begin > MAX_FILER_CACHE_IDX)
S
Sebastian Poehn 已提交
1347 1348 1349
		return -EINVAL;

	gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
1350
				tab->index - length + 1);
S
Sebastian Poehn 已提交
1351 1352 1353 1354 1355 1356 1357

	tab->index += length;
	return 0;
}

static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
{
1358 1359 1360 1361
	for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
	     start++) {
		if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
		    (RQFCR_AND | RQFCR_CLE))
S
Sebastian Poehn 已提交
1362 1363 1364 1365 1366 1367 1368
			return start;
	}
	return -1;
}

static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
{
1369 1370 1371 1372
	for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
	     start++) {
		if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
		    (RQFCR_CLE))
S
Sebastian Poehn 已提交
1373 1374 1375 1376 1377
			return start;
	}
	return -1;
}

1378
/* Uses hardwares clustering option to reduce
S
Sebastian Poehn 已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387
 * the number of filer table entries
 */
static void gfar_cluster_filer(struct filer_table *tab)
{
	s32 i = -1, j, iend, jend;

	while ((i = gfar_get_next_cluster_start(++i, tab)) != -1) {
		j = i;
		while ((j = gfar_get_next_cluster_start(++j, tab)) != -1) {
1388
			/* The cluster entries self and the previous one
S
Sebastian Poehn 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
			 * (a mask) must be identical!
			 */
			if (tab->fe[i].ctrl != tab->fe[j].ctrl)
				break;
			if (tab->fe[i].prop != tab->fe[j].prop)
				break;
			if (tab->fe[i - 1].ctrl != tab->fe[j - 1].ctrl)
				break;
			if (tab->fe[i - 1].prop != tab->fe[j - 1].prop)
				break;
			iend = gfar_get_next_cluster_end(i, tab);
			jend = gfar_get_next_cluster_end(j, tab);
			if (jend == -1 || iend == -1)
				break;
1403 1404

			/* First we make some free space, where our cluster
S
Sebastian Poehn 已提交
1405 1406 1407
			 * element should be. Then we copy it there and finally
			 * delete in from its old location.
			 */
1408 1409
			if (gfar_expand_filer_entries(iend, (jend - j), tab) ==
			    -EINVAL)
S
Sebastian Poehn 已提交
1410 1411 1412
				break;

			gfar_copy_filer_entries(&(tab->fe[iend + 1]),
1413
						&(tab->fe[jend + 1]), jend - j);
S
Sebastian Poehn 已提交
1414 1415

			if (gfar_trim_filer_entries(jend - 1,
1416 1417
						    jend + (jend - j),
						    tab) == -EINVAL)
S
Sebastian Poehn 已提交
1418 1419 1420 1421 1422 1423 1424 1425
				return;

			/* Mask out cluster bit */
			tab->fe[iend].ctrl &= ~(RQFCR_CLE);
		}
	}
}

S
Sebastian Poehn 已提交
1426 1427
/* Swaps the masked bits of a1<>a2 and b1<>b2 */
static void gfar_swap_bits(struct gfar_filer_entry *a1,
1428 1429 1430
			   struct gfar_filer_entry *a2,
			   struct gfar_filer_entry *b1,
			   struct gfar_filer_entry *b2, u32 mask)
S
Sebastian Poehn 已提交
1431 1432
{
	u32 temp[4];
S
Sebastian Poehn 已提交
1433 1434 1435 1436
	temp[0] = a1->ctrl & mask;
	temp[1] = a2->ctrl & mask;
	temp[2] = b1->ctrl & mask;
	temp[3] = b2->ctrl & mask;
S
Sebastian Poehn 已提交
1437

S
Sebastian Poehn 已提交
1438 1439 1440 1441
	a1->ctrl &= ~mask;
	a2->ctrl &= ~mask;
	b1->ctrl &= ~mask;
	b2->ctrl &= ~mask;
S
Sebastian Poehn 已提交
1442 1443 1444 1445 1446 1447 1448

	a1->ctrl |= temp[1];
	a2->ctrl |= temp[0];
	b1->ctrl |= temp[3];
	b2->ctrl |= temp[2];
}

1449
/* Generate a list consisting of masks values with their start and
S
Sebastian Poehn 已提交
1450 1451 1452 1453
 * end of validity and block as indicator for parts belonging
 * together (glued by ANDs) in mask_table
 */
static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
1454
				    struct filer_table *tab)
S
Sebastian Poehn 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
{
	u32 i, and_index = 0, block_index = 1;

	for (i = 0; i < tab->index; i++) {

		/* LSByte of control = 0 sets a mask */
		if (!(tab->fe[i].ctrl & 0xF)) {
			mask_table[and_index].mask = tab->fe[i].prop;
			mask_table[and_index].start = i;
			mask_table[and_index].block = block_index;
			if (and_index >= 1)
				mask_table[and_index - 1].end = i - 1;
			and_index++;
		}
S
Sebastian Poehn 已提交
1469
		/* cluster starts and ends will be separated because they should
1470 1471
		 * hold their position
		 */
S
Sebastian Poehn 已提交
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
		if (tab->fe[i].ctrl & RQFCR_CLE)
			block_index++;
		/* A not set AND indicates the end of a depended block */
		if (!(tab->fe[i].ctrl & RQFCR_AND))
			block_index++;
	}

	mask_table[and_index - 1].end = i - 1;

	return and_index;
}

1484
/* Sorts the entries of mask_table by the values of the masks.
S
Sebastian Poehn 已提交
1485 1486 1487 1488 1489
 * Important: The 0xFF80 flags of the first and last entry of a
 * block must hold their position (which queue, CLusterEnable, ReJEct,
 * AND)
 */
static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
1490
				 struct filer_table *temp_table, u32 and_index)
S
Sebastian Poehn 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
{
	/* Pointer to compare function (_asc or _desc) */
	int (*gfar_comp)(const void *, const void *);

	u32 i, size = 0, start = 0, prev = 1;
	u32 old_first, old_last, new_first, new_last;

	gfar_comp = &gfar_comp_desc;

	for (i = 0; i < and_index; i++) {
		if (prev != mask_table[i].block) {
			old_first = mask_table[start].start + 1;
			old_last = mask_table[i - 1].end;
			sort(mask_table + start, size,
1505 1506
			     sizeof(struct gfar_mask_entry),
			     gfar_comp, &gfar_swap);
S
Sebastian Poehn 已提交
1507 1508

			/* Toggle order for every block. This makes the
1509 1510
			 * thing more efficient!
			 */
S
Sebastian Poehn 已提交
1511 1512 1513 1514 1515 1516 1517 1518
			if (gfar_comp == gfar_comp_desc)
				gfar_comp = &gfar_comp_asc;
			else
				gfar_comp = &gfar_comp_desc;

			new_first = mask_table[start].start + 1;
			new_last = mask_table[i - 1].end;

S
Sebastian Poehn 已提交
1519
			gfar_swap_bits(&temp_table->fe[new_first],
1520 1521 1522 1523 1524
				       &temp_table->fe[old_first],
				       &temp_table->fe[new_last],
				       &temp_table->fe[old_last],
				       RQFCR_QUEUE | RQFCR_CLE |
				       RQFCR_RJE | RQFCR_AND);
S
Sebastian Poehn 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533

			start = i;
			size = 0;
		}
		size++;
		prev = mask_table[i].block;
	}
}

1534
/* Reduces the number of masks needed in the filer table to save entries
S
Sebastian Poehn 已提交
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
 * This is done by sorting the masks of a depended block. A depended block is
 * identified by gluing ANDs or CLE. The sorting order toggles after every
 * block. Of course entries in scope of a mask must change their location with
 * it.
 */
static int gfar_optimize_filer_masks(struct filer_table *tab)
{
	struct filer_table *temp_table;
	struct gfar_mask_entry *mask_table;

	u32 and_index = 0, previous_mask = 0, i = 0, j = 0, size = 0;
	s32 ret = 0;

	/* We need a copy of the filer table because
1549 1550
	 * we want to change its order
	 */
1551
	temp_table = kmemdup(tab, sizeof(*temp_table), GFP_KERNEL);
S
Sebastian Poehn 已提交
1552 1553 1554 1555
	if (temp_table == NULL)
		return -ENOMEM;

	mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
1556
			     sizeof(struct gfar_mask_entry), GFP_KERNEL);
S
Sebastian Poehn 已提交
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567

	if (mask_table == NULL) {
		ret = -ENOMEM;
		goto end;
	}

	and_index = gfar_generate_mask_table(mask_table, tab);

	gfar_sort_mask_table(mask_table, temp_table, and_index);

	/* Now we can copy the data from our duplicated filer table to
1568 1569
	 * the real one in the order the mask table says
	 */
S
Sebastian Poehn 已提交
1570 1571 1572 1573 1574 1575 1576 1577
	for (i = 0; i < and_index; i++) {
		size = mask_table[i].end - mask_table[i].start + 1;
		gfar_copy_filer_entries(&(tab->fe[j]),
				&(temp_table->fe[mask_table[i].start]), size);
		j += size;
	}

	/* And finally we just have to check for duplicated masks and drop the
1578 1579
	 * second ones
	 */
S
Sebastian Poehn 已提交
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
	for (i = 0; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
		if (tab->fe[i].ctrl == 0x80) {
			previous_mask = i++;
			break;
		}
	}
	for (; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
		if (tab->fe[i].ctrl == 0x80) {
			if (tab->fe[i].prop == tab->fe[previous_mask].prop) {
				/* Two identical ones found!
1590 1591
				 * So drop the second one!
				 */
S
Sebastian Poehn 已提交
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
				gfar_trim_filer_entries(i, i, tab);
			} else
				/* Not identical! */
				previous_mask = i;
		}
	}

	kfree(mask_table);
end:	kfree(temp_table);
	return ret;
}

/* Write the bit-pattern from software's buffer to hardware registers */
static int gfar_write_filer_table(struct gfar_private *priv,
1606
				  struct filer_table *tab)
S
Sebastian Poehn 已提交
1607 1608 1609 1610 1611 1612 1613 1614 1615
{
	u32 i = 0;
	if (tab->index > MAX_FILER_IDX - 1)
		return -EBUSY;

	/* Avoid inconsistent filer table to be processed */
	lock_rx_qs(priv);

	/* Fill regular entries */
1616 1617
	for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].ctrl);
	     i++)
S
Sebastian Poehn 已提交
1618 1619 1620 1621 1622
		gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
	/* Fill the rest with fall-troughs */
	for (; i < MAX_FILER_IDX - 1; i++)
		gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
	/* Last entry must be default accept
1623 1624
	 * because that's what people expect
	 */
S
Sebastian Poehn 已提交
1625 1626 1627 1628 1629 1630 1631 1632
	gfar_write_filer(priv, i, 0x20, 0x0);

	unlock_rx_qs(priv);

	return 0;
}

static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
1633
				 struct gfar_private *priv)
S
Sebastian Poehn 已提交
1634 1635 1636 1637 1638
{

	if (flow->flow_type & FLOW_EXT)	{
		if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
			netdev_warn(priv->ndev,
1639
				    "User-specific data not supported!\n");
S
Sebastian Poehn 已提交
1640 1641
		if (~flow->m_ext.vlan_etype)
			netdev_warn(priv->ndev,
1642
				    "VLAN-etype not supported!\n");
S
Sebastian Poehn 已提交
1643 1644 1645 1646
	}
	if (flow->flow_type == IP_USER_FLOW)
		if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
			netdev_warn(priv->ndev,
1647
				    "IP-Version differing from IPv4 not supported!\n");
S
Sebastian Poehn 已提交
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664

	return 0;
}

static int gfar_process_filer_changes(struct gfar_private *priv)
{
	struct ethtool_flow_spec_container *j;
	struct filer_table *tab;
	s32 i = 0;
	s32 ret = 0;

	/* So index is set to zero, too! */
	tab = kzalloc(sizeof(*tab), GFP_KERNEL);
	if (tab == NULL)
		return -ENOMEM;

	/* Now convert the existing filer data from flow_spec into
1665 1666
	 * filer tables binary format
	 */
S
Sebastian Poehn 已提交
1667 1668 1669
	list_for_each_entry(j, &priv->rx_list.list, list) {
		ret = gfar_convert_to_filer(&j->fs, tab);
		if (ret == -EBUSY) {
1670 1671
			netdev_err(priv->ndev,
				   "Rule not added: No free space!\n");
S
Sebastian Poehn 已提交
1672 1673 1674
			goto end;
		}
		if (ret == -1) {
1675 1676
			netdev_err(priv->ndev,
				   "Rule not added: Unsupported Flow-type!\n");
S
Sebastian Poehn 已提交
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
			goto end;
		}
	}

	i = tab->index;

	/* Optimizations to save entries */
	gfar_cluster_filer(tab);
	gfar_optimize_filer_masks(tab);

1687
	pr_debug("\tSummary:\n"
1688 1689 1690
		 "\tData on hardware: %d\n"
		 "\tCompression rate: %d%%\n",
		 tab->index, 100 - (100 * tab->index) / i);
S
Sebastian Poehn 已提交
1691 1692 1693 1694 1695 1696 1697 1698

	/* Write everything to hardware */
	ret = gfar_write_filer_table(priv, tab);
	if (ret == -EBUSY) {
		netdev_err(priv->ndev, "Rule not added: No free space!\n");
		goto end;
	}

1699 1700
end:
	kfree(tab);
S
Sebastian Poehn 已提交
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
	return ret;
}

static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
{
	u32 i = 0;

	for (i = 0; i < sizeof(flow->m_u); i++)
		flow->m_u.hdata[i] ^= 0xFF;

1711 1712 1713 1714
	flow->m_ext.vlan_etype ^= cpu_to_be16(0xFFFF);
	flow->m_ext.vlan_tci ^= cpu_to_be16(0xFFFF);
	flow->m_ext.data[0] ^= cpu_to_be32(~0);
	flow->m_ext.data[1] ^= cpu_to_be32(~0);
S
Sebastian Poehn 已提交
1715 1716 1717
}

static int gfar_add_cls(struct gfar_private *priv,
1718
			struct ethtool_rx_flow_spec *flow)
S
Sebastian Poehn 已提交
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
{
	struct ethtool_flow_spec_container *temp, *comp;
	int ret = 0;

	temp = kmalloc(sizeof(*temp), GFP_KERNEL);
	if (temp == NULL)
		return -ENOMEM;
	memcpy(&temp->fs, flow, sizeof(temp->fs));

	gfar_invert_masks(&temp->fs);
	ret = gfar_check_capability(&temp->fs, priv);
	if (ret)
		goto clean_mem;
	/* Link in the new element at the right @location */
	if (list_empty(&priv->rx_list.list)) {
		ret = gfar_check_filer_hardware(priv);
		if (ret != 0)
			goto clean_mem;
		list_add(&temp->list, &priv->rx_list.list);
		goto process;
	} else {
		list_for_each_entry(comp, &priv->rx_list.list, list) {
			if (comp->fs.location > flow->location) {
				list_add_tail(&temp->list, &comp->list);
				goto process;
			}
			if (comp->fs.location == flow->location) {
				netdev_err(priv->ndev,
1747 1748
					   "Rule not added: ID %d not free!\n",
					   flow->location);
S
Sebastian Poehn 已提交
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
				ret = -EBUSY;
				goto clean_mem;
			}
		}
		list_add_tail(&temp->list, &priv->rx_list.list);
	}

process:
	ret = gfar_process_filer_changes(priv);
	if (ret)
		goto clean_list;
	priv->rx_list.count++;
	return ret;

clean_list:
	list_del(&temp->list);
clean_mem:
	kfree(temp);
	return ret;
}

static int gfar_del_cls(struct gfar_private *priv, u32 loc)
{
	struct ethtool_flow_spec_container *comp;
	u32 ret = -EINVAL;

	if (list_empty(&priv->rx_list.list))
		return ret;

	list_for_each_entry(comp, &priv->rx_list.list, list) {
		if (comp->fs.location == loc) {
			list_del(&comp->list);
			kfree(comp);
			priv->rx_list.count--;
			gfar_process_filer_changes(priv);
			ret = 0;
			break;
		}
	}

	return ret;
}

static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
{
	struct ethtool_flow_spec_container *comp;
	u32 ret = -EINVAL;

	list_for_each_entry(comp, &priv->rx_list.list, list) {
		if (comp->fs.location == cmd->fs.location) {
			memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
			gfar_invert_masks(&cmd->fs);
			ret = 0;
			break;
		}
	}

	return ret;
}

static int gfar_get_cls_all(struct gfar_private *priv,
1810
			    struct ethtool_rxnfc *cmd, u32 *rule_locs)
S
Sebastian Poehn 已提交
1811 1812 1813 1814 1815
{
	struct ethtool_flow_spec_container *comp;
	u32 i = 0;

	list_for_each_entry(comp, &priv->rx_list.list, list) {
1816 1817 1818 1819
		if (i == cmd->rule_cnt)
			return -EMSGSIZE;
		rule_locs[i] = comp->fs.location;
		i++;
S
Sebastian Poehn 已提交
1820 1821 1822
	}

	cmd->data = MAX_FILER_IDX;
1823
	cmd->rule_cnt = i;
S
Sebastian Poehn 已提交
1824 1825 1826 1827

	return 0;
}

1828 1829 1830 1831 1832
static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
{
	struct gfar_private *priv = netdev_priv(dev);
	int ret = 0;

S
Sebastian Poehn 已提交
1833 1834 1835
	mutex_lock(&priv->rx_queue_access);

	switch (cmd->cmd) {
1836 1837 1838
	case ETHTOOL_SRXFH:
		ret = gfar_set_hash_opts(priv, cmd);
		break;
S
Sebastian Poehn 已提交
1839
	case ETHTOOL_SRXCLSRLINS:
1840 1841 1842
		if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
		     cmd->fs.ring_cookie >= priv->num_rx_queues) ||
		    cmd->fs.location >= MAX_FILER_IDX) {
S
Sebastian Poehn 已提交
1843 1844 1845 1846 1847 1848 1849 1850
			ret = -EINVAL;
			break;
		}
		ret = gfar_add_cls(priv, &cmd->fs);
		break;
	case ETHTOOL_SRXCLSRLDEL:
		ret = gfar_del_cls(priv, cmd->fs.location);
		break;
1851 1852 1853 1854
	default:
		ret = -EINVAL;
	}

S
Sebastian Poehn 已提交
1855 1856 1857 1858 1859 1860
	mutex_unlock(&priv->rx_queue_access);

	return ret;
}

static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1861
			u32 *rule_locs)
S
Sebastian Poehn 已提交
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
{
	struct gfar_private *priv = netdev_priv(dev);
	int ret = 0;

	switch (cmd->cmd) {
	case ETHTOOL_GRXRINGS:
		cmd->data = priv->num_rx_queues;
		break;
	case ETHTOOL_GRXCLSRLCNT:
		cmd->rule_cnt = priv->rx_list.count;
		break;
	case ETHTOOL_GRXCLSRULE:
		ret = gfar_get_cls(priv, cmd);
		break;
	case ETHTOOL_GRXCLSRLALL:
1877
		ret = gfar_get_cls_all(priv, cmd, rule_locs);
S
Sebastian Poehn 已提交
1878 1879 1880 1881 1882 1883
		break;
	default:
		ret = -EINVAL;
		break;
	}

1884 1885 1886
	return ret;
}

1887
int gfar_phc_index = -1;
1888
EXPORT_SYMBOL(gfar_phc_index);
1889 1890 1891 1892 1893 1894 1895

static int gfar_get_ts_info(struct net_device *dev,
			    struct ethtool_ts_info *info)
{
	struct gfar_private *priv = netdev_priv(dev);

	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
1896 1897
		info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
					SOF_TIMESTAMPING_SOFTWARE;
1898 1899 1900
		info->phc_index = -1;
		return 0;
	}
1901 1902 1903
	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
				SOF_TIMESTAMPING_RX_HARDWARE |
				SOF_TIMESTAMPING_RAW_HARDWARE;
1904
	info->phc_index = gfar_phc_index;
1905 1906 1907 1908
	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
			 (1 << HWTSTAMP_TX_ON);
	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
			   (1 << HWTSTAMP_FILTER_ALL);
1909 1910 1911
	return 0;
}

1912
const struct ethtool_ops gfar_ethtool_ops = {
L
Linus Torvalds 已提交
1913
	.get_settings = gfar_gsettings,
1914
	.set_settings = gfar_ssettings,
L
Linus Torvalds 已提交
1915 1916 1917 1918 1919 1920 1921 1922
	.get_drvinfo = gfar_gdrvinfo,
	.get_regs_len = gfar_reglen,
	.get_regs = gfar_get_regs,
	.get_link = ethtool_op_get_link,
	.get_coalesce = gfar_gcoalesce,
	.set_coalesce = gfar_scoalesce,
	.get_ringparam = gfar_gringparam,
	.set_ringparam = gfar_sringparam,
1923 1924
	.get_pauseparam = gfar_gpauseparam,
	.set_pauseparam = gfar_spauseparam,
L
Linus Torvalds 已提交
1925
	.get_strings = gfar_gstrings,
1926
	.get_sset_count = gfar_sset_count,
L
Linus Torvalds 已提交
1927
	.get_ethtool_stats = gfar_fill_stats,
1928 1929
	.get_msglevel = gfar_get_msglevel,
	.set_msglevel = gfar_set_msglevel,
1930 1931 1932 1933
#ifdef CONFIG_PM
	.get_wol = gfar_get_wol,
	.set_wol = gfar_set_wol,
#endif
1934
	.set_rxnfc = gfar_set_nfc,
S
Sebastian Poehn 已提交
1935
	.get_rxnfc = gfar_get_nfc,
1936
	.get_ts_info = gfar_get_ts_info,
L
Linus Torvalds 已提交
1937
};