gianfar_ethtool.c 39.9 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
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/mm.h>

#include <asm/io.h>
#include <asm/irq.h>
35
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
36 37 38 39
#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>
44 45
#include <linux/of_platform.h>
#include <linux/fsl/ptp_qoriq.h>
L
Linus Torvalds 已提交
46 47 48

#include "gianfar.h"

49 50
#define GFAR_MAX_COAL_USECS 0xffff
#define GFAR_MAX_COAL_FRAMES 0xff
51
static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
52
			    u64 *buf);
53
static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
54 55 56 57 58 59 60 61 62 63
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 已提交
64

65
static const char stat_gstrings[][ETH_GSTRING_LEN] = {
66 67
	/* extra stats */
	"rx-allocation-errors",
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78 79
	"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",
	"tx-timeout-errors",
80
	/* rmon stats */
L
Linus Torvalds 已提交
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 126
	"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",
};

127 128 129 130 131
/* 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);
132

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

L
Linus Torvalds 已提交
140 141 142 143
/* 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
 */
144 145
static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
			    u64 *buf)
L
Linus Torvalds 已提交
146 147 148
{
	int i;
	struct gfar_private *priv = netdev_priv(dev);
149
	struct gfar __iomem *regs = priv->gfargrp[0].regs;
150
	atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
L
Linus Torvalds 已提交
151

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

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

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

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

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

/* Fills in the drvinfo structure with some basic info */
179 180
static void gfar_gdrvinfo(struct net_device *dev,
			  struct ethtool_drvinfo *drvinfo)
L
Linus Torvalds 已提交
181
{
182 183 184 185 186
	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 已提交
187 188 189
}

/* Return the length of the register structure */
190
static int gfar_reglen(struct net_device *dev)
L
Linus Torvalds 已提交
191 192 193 194 195
{
	return sizeof (struct gfar);
}

/* Return a dump of the GFAR register space */
196 197
static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
			  void *regbuf)
L
Linus Torvalds 已提交
198 199 200
{
	int i;
	struct gfar_private *priv = netdev_priv(dev);
201
	u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
L
Linus Torvalds 已提交
202 203 204
	u32 *buf = (u32 *) regbuf;

	for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
205
		buf[i] = gfar_read(&theregs[i]);
L
Linus Torvalds 已提交
206 207 208 209
}

/* Convert microseconds to ethernet clock ticks, which changes
 * depending on what speed the controller is running at */
210 211
static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
				     unsigned int usecs)
L
Linus Torvalds 已提交
212
{
213 214
	struct net_device *ndev = priv->ndev;
	struct phy_device *phydev = ndev->phydev;
L
Linus Torvalds 已提交
215 216 217
	unsigned int count;

	/* The timer is different, depending on the interface speed */
218
	switch (phydev->speed) {
219
	case SPEED_1000:
L
Linus Torvalds 已提交
220 221
		count = GFAR_GBIT_TIME;
		break;
222
	case SPEED_100:
L
Linus Torvalds 已提交
223 224
		count = GFAR_100_TIME;
		break;
225
	case SPEED_10:
L
Linus Torvalds 已提交
226 227 228 229 230 231 232
	default:
		count = GFAR_10_TIME;
		break;
	}

	/* Make sure we return a number greater than 0
	 * if usecs > 0 */
233
	return DIV_ROUND_UP(usecs * 1000, count);
L
Linus Torvalds 已提交
234 235 236
}

/* Convert ethernet clock ticks to microseconds */
237 238
static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
				     unsigned int ticks)
L
Linus Torvalds 已提交
239
{
240 241
	struct net_device *ndev = priv->ndev;
	struct phy_device *phydev = ndev->phydev;
L
Linus Torvalds 已提交
242 243 244
	unsigned int count;

	/* The timer is different, depending on the interface speed */
245
	switch (phydev->speed) {
246
	case SPEED_1000:
L
Linus Torvalds 已提交
247 248
		count = GFAR_GBIT_TIME;
		break;
249
	case SPEED_100:
L
Linus Torvalds 已提交
250 251
		count = GFAR_100_TIME;
		break;
252
	case SPEED_10:
L
Linus Torvalds 已提交
253 254 255 256 257 258 259
	default:
		count = GFAR_10_TIME;
		break;
	}

	/* Make sure we return a number greater than 0 */
	/* if ticks is > 0 */
260
	return (ticks * count) / 1000;
L
Linus Torvalds 已提交
261 262 263 264
}

/* Get the coalescing parameters, and put them in the cvals
 * structure.  */
265 266
static int gfar_gcoalesce(struct net_device *dev,
			  struct ethtool_coalesce *cvals)
L
Linus Torvalds 已提交
267 268
{
	struct gfar_private *priv = netdev_priv(dev);
269 270
	struct gfar_priv_rx_q *rx_queue = NULL;
	struct gfar_priv_tx_q *tx_queue = NULL;
271 272 273 274
	unsigned long rxtime;
	unsigned long rxcount;
	unsigned long txtime;
	unsigned long txcount;
275

276
	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
277
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
278

279
	if (!dev->phydev)
280 281
		return -ENODEV;

282 283
	rx_queue = priv->rx_queue[0];
	tx_queue = priv->tx_queue[0];
284 285 286 287 288

	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);
289 290
	cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
	cvals->rx_max_coalesced_frames = rxcount;
L
Linus Torvalds 已提交
291

292 293
	cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
	cvals->tx_max_coalesced_frames = txcount;
L
Linus Torvalds 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

	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
 */
331 332
static int gfar_scoalesce(struct net_device *dev,
			  struct ethtool_coalesce *cvals)
L
Linus Torvalds 已提交
333 334
{
	struct gfar_private *priv = netdev_priv(dev);
335
	int i, err = 0;
L
Linus Torvalds 已提交
336

337
	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
338 339
		return -EOPNOTSUPP;

340
	if (!dev->phydev)
341 342 343 344
		return -ENODEV;

	/* Check the bounds of the values */
	if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
345 346
		netdev_info(dev, "Coalescing is limited to %d microseconds\n",
			    GFAR_MAX_COAL_USECS);
347 348 349 350
		return -EINVAL;
	}

	if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
351 352
		netdev_info(dev, "Coalescing is limited to %d frames\n",
			    GFAR_MAX_COAL_FRAMES);
353 354 355
		return -EINVAL;
	}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	/* Check the bounds of the values */
	if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
		netdev_info(dev, "Coalescing is limited to %d microseconds\n",
			    GFAR_MAX_COAL_USECS);
		return -EINVAL;
	}

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

	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
		cpu_relax();

	/* Set up rx coalescing */
	if ((cvals->rx_coalesce_usecs == 0) ||
	    (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;
	}

382 383 384 385 386
	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 已提交
387 388 389

	/* Set up tx coalescing */
	if ((cvals->tx_coalesce_usecs == 0) ||
390 391 392 393 394 395 396
	    (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 已提交
397

398 399 400 401 402
	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 已提交
403

404 405 406 407 408 409 410 411
	if (dev->flags & IFF_UP) {
		stop_gfar(dev);
		err = startup_gfar(dev);
	} else {
		gfar_mac_reset(priv);
	}

	clear_bit_unlock(GFAR_RESETTING, &priv->state);
L
Linus Torvalds 已提交
412

413
	return err;
L
Linus Torvalds 已提交
414 415 416 417 418
}

/* 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 */
419 420
static void gfar_gringparam(struct net_device *dev,
			    struct ethtool_ringparam *rvals)
L
Linus Torvalds 已提交
421 422
{
	struct gfar_private *priv = netdev_priv(dev);
423 424 425
	struct gfar_priv_tx_q *tx_queue = NULL;
	struct gfar_priv_rx_q *rx_queue = NULL;

426 427
	tx_queue = priv->tx_queue[0];
	rx_queue = priv->rx_queue[0];
L
Linus Torvalds 已提交
428 429 430 431 432 433 434 435 436

	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.
	 */
437 438 439 440
	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 已提交
441 442 443
}

/* Change the current ring parameters, stopping the controller if
444
 * necessary so that we don't mess things up while we're in motion.
445 446 447
 */
static int gfar_sringparam(struct net_device *dev,
			   struct ethtool_ringparam *rvals)
L
Linus Torvalds 已提交
448 449
{
	struct gfar_private *priv = netdev_priv(dev);
450
	int err = 0, i;
L
Linus Torvalds 已提交
451 452 453 454 455

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

	if (!is_power_of_2(rvals->rx_pending)) {
456
		netdev_err(dev, "Ring sizes must be a power of 2\n");
L
Linus Torvalds 已提交
457 458 459 460 461 462 463
		return -EINVAL;
	}

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

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

468 469 470
	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
		cpu_relax();

471
	if (dev->flags & IFF_UP)
472
		stop_gfar(dev);
L
Linus Torvalds 已提交
473

474 475
	/* Change the sizes */
	for (i = 0; i < priv->num_rx_queues; i++)
476
		priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
477 478

	for (i = 0; i < priv->num_tx_queues; i++)
479
		priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
L
Linus Torvalds 已提交
480

481
	/* Rebuild the rings with the new size */
482
	if (dev->flags & IFF_UP)
483
		err = startup_gfar(dev);
484 485 486

	clear_bit_unlock(GFAR_RESETTING, &priv->state);

487 488
	return err;
}
L
Linus Torvalds 已提交
489

490 491 492 493 494 495 496 497 498 499 500 501 502 503
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);
504
	struct phy_device *phydev = dev->phydev;
505 506
	struct gfar __iomem *regs = priv->gfargrp[0].regs;

507 508 509
	if (!phydev)
		return -ENODEV;

510
	if (!phy_validate_pause(phydev, epause))
511 512 513
		return -EINVAL;

	priv->rx_pause_en = priv->tx_pause_en = 0;
514
	phy_set_asym_pause(phydev, epause->rx_pause, epause->tx_pause);
515 516 517 518 519
	if (epause->rx_pause) {
		priv->rx_pause_en = 1;

		if (epause->tx_pause) {
			priv->tx_pause_en = 1;
520
		}
521 522
	} else if (epause->tx_pause) {
		priv->tx_pause_en = 1;
523
	}
524 525 526 527 528 529

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

530 531 532 533 534 535 536 537 538
	if (!epause->autoneg) {
		u32 tempval = gfar_read(&regs->maccfg1);

		tempval &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);

		priv->tx_actual_en = 0;
		if (priv->tx_pause_en) {
			priv->tx_actual_en = 1;
			tempval |= MACCFG1_TX_FLOW;
539
		}
540 541 542 543

		if (priv->rx_pause_en)
			tempval |= MACCFG1_RX_FLOW;
		gfar_write(&regs->maccfg1, tempval);
544 545 546 547 548
	}

	return 0;
}

549
int gfar_set_features(struct net_device *dev, netdev_features_t features)
550
{
551
	netdev_features_t changed = dev->features ^ features;
552
	struct gfar_private *priv = netdev_priv(dev);
553
	int err = 0;
L
Linus Torvalds 已提交
554

555 556
	if (!(changed & (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
			 NETIF_F_RXCSUM)))
557
		return 0;
558

559 560 561
	while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
		cpu_relax();

562 563
	dev->features = features;

564 565
	if (dev->flags & IFF_UP) {
		/* Now we take down the rings to rebuild them */
L
Linus Torvalds 已提交
566 567
		stop_gfar(dev);
		err = startup_gfar(dev);
568 569
	} else {
		gfar_mac_reset(priv);
570
	}
571 572 573

	clear_bit_unlock(GFAR_RESETTING, &priv->state);

L
Linus Torvalds 已提交
574 575 576
	return err;
}

577
static uint32_t gfar_get_msglevel(struct net_device *dev)
578
{
579
	struct gfar_private *priv = netdev_priv(dev);
580

581
	return priv->msg_enable;
582 583
}

584
static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
585
{
586
	struct gfar_private *priv = netdev_priv(dev);
587

588 589 590
	priv->msg_enable = data;
}

591 592 593 594 595
#ifdef CONFIG_PM
static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct gfar_private *priv = netdev_priv(dev);

596 597 598 599 600 601 602 603 604 605 606 607 608 609
	wol->supported = 0;
	wol->wolopts = 0;

	if (priv->wol_supported & GFAR_WOL_MAGIC)
		wol->supported |= WAKE_MAGIC;

	if (priv->wol_supported & GFAR_WOL_FILER_UCAST)
		wol->supported |= WAKE_UCAST;

	if (priv->wol_opts & GFAR_WOL_MAGIC)
		wol->wolopts |= WAKE_MAGIC;

	if (priv->wol_opts & GFAR_WOL_FILER_UCAST)
		wol->wolopts |= WAKE_UCAST;
610 611 612 613 614
}

static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct gfar_private *priv = netdev_priv(dev);
615 616
	u16 wol_opts = 0;
	int err;
617

618
	if (!priv->wol_supported && wol->wolopts)
619 620
		return -EINVAL;

621
	if (wol->wolopts & ~(WAKE_MAGIC | WAKE_UCAST))
622 623
		return -EINVAL;

624 625 626 627 628 629 630 631 632 633 634 635 636
	if (wol->wolopts & WAKE_MAGIC) {
		wol_opts |= GFAR_WOL_MAGIC;
	} else {
		if (wol->wolopts & WAKE_UCAST)
			wol_opts |= GFAR_WOL_FILER_UCAST;
	}

	wol_opts &= priv->wol_supported;
	priv->wol_opts = 0;

	err = device_set_wakeup_enable(priv->dev, wol_opts);
	if (err)
		return err;
637

638
	priv->wol_opts = wol_opts;
639 640 641 642

	return 0;
}
#endif
643

644 645 646 647 648
static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
{
	u32 fcr = 0x0, fpr = FPR_FILER_MASK;

	if (ethflow & RXH_L2DA) {
649
		fcr = RQFCR_PID_DAH | RQFCR_CMP_NOMATCH |
650
		      RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
651 652
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
653 654 655
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
		priv->cur_filer_idx = priv->cur_filer_idx - 1;

656
		fcr = RQFCR_PID_DAL | RQFCR_CMP_NOMATCH |
657
		      RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
658 659
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
660 661 662 663 664 665
		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 |
666
		      RQFCR_AND | RQFCR_HASHTBL_0;
667
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
W
Wu Jiajun-B06378 已提交
668 669
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
670 671 672 673 674
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}

	if (ethflow & RXH_IP_SRC) {
		fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
675
		      RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
676 677
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
678 679 680 681 682 683
		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 |
684
		      RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
685 686
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
687 688 689 690 691 692
		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 |
693
		      RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
694 695
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
696 697 698 699 700 701
		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 |
702
		      RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
703 704
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
705 706 707 708 709 710
		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 |
711
		      RQFCR_AND | RQFCR_HASHTBL_0;
W
Wu Jiajun-B06378 已提交
712 713
		priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
		priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
714 715 716 717 718
		gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}
}

719 720
static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
				       u64 class)
721 722
{
	unsigned int cmp_rqfpr;
723 724
	unsigned int *local_rqfpr;
	unsigned int *local_rqfcr;
725 726
	int i = 0x0, k = 0x0;
	int j = MAX_FILER_IDX, l = 0x0;
727 728
	int ret = 1;

729 730 731 732
	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);
733 734 735 736
	if (!local_rqfpr || !local_rqfcr) {
		ret = 0;
		goto err;
	}
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

	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:
752 753
		netdev_err(priv->ndev,
			   "Right now this class is not supported\n");
754 755
		ret = 0;
		goto err;
756 757 758
	}

	for (i = 0; i < MAX_FILER_IDX + 1; i++) {
W
Wu Jiajun-B06378 已提交
759 760
		local_rqfpr[j] = priv->ftp_rqfpr[i];
		local_rqfcr[j] = priv->ftp_rqfcr[i];
761
		j--;
762 763 764
		if ((priv->ftp_rqfcr[i] ==
		     (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
		    (priv->ftp_rqfpr[i] == cmp_rqfpr))
765 766 767 768
			break;
	}

	if (i == MAX_FILER_IDX + 1) {
769 770
		netdev_err(priv->ndev,
			   "No parse rule found, can't create hash rules\n");
771 772
		ret = 0;
		goto err;
773 774 775 776 777 778
	}

	/* 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 已提交
779
		if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
780
		    !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
W
Wu Jiajun-B06378 已提交
781
			priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
782
					     RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
W
Wu Jiajun-B06378 已提交
783 784
			priv->ftp_rqfpr[l] = FPR_FILER_MASK;
			gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
785
					 priv->ftp_rqfpr[l]);
786 787 788
			break;
		}

W
Wu Jiajun-B06378 已提交
789 790
		if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
			(priv->ftp_rqfcr[l] & RQFCR_AND))
791 792
			continue;
		else {
W
Wu Jiajun-B06378 已提交
793 794
			local_rqfpr[j] = priv->ftp_rqfpr[l];
			local_rqfcr[j] = priv->ftp_rqfcr[l];
795 796 797 798 799 800 801 802 803 804 805
			j--;
		}
	}

	priv->cur_filer_idx = l - 1;

	/* 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 已提交
806 807
		priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
		priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
808
		gfar_write_filer(priv, priv->cur_filer_idx,
809
				 local_rqfcr[k], local_rqfpr[k]);
810 811 812 813 814
		if (!priv->cur_filer_idx)
			break;
		priv->cur_filer_idx = priv->cur_filer_idx - 1;
	}

815 816 817 818
err:
	kfree(local_rqfcr);
	kfree(local_rqfpr);
	return ret;
819 820
}

821 822
static int gfar_set_hash_opts(struct gfar_private *priv,
			      struct ethtool_rxnfc *cmd)
823 824 825
{
	/* write the filer rules here */
	if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
826
		return -EINVAL;
827 828 829 830

	return 0;
}

S
Sebastian Poehn 已提交
831 832
static int gfar_check_filer_hardware(struct gfar_private *priv)
{
833
	struct gfar __iomem *regs = priv->gfargrp[0].regs;
S
Sebastian Poehn 已提交
834 835 836 837 838 839 840 841 842 843 844
	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,
845
				    "Receive Queue Filtering enabled\n");
S
Sebastian Poehn 已提交
846 847
		} else {
			netdev_warn(priv->ndev,
848
				    "Receive Queue Filtering disabled\n");
S
Sebastian Poehn 已提交
849 850 851 852 853 854 855 856 857
			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,
858
				    "Receive Queue Filtering enabled\n");
S
Sebastian Poehn 已提交
859 860
		} else {
			netdev_warn(priv->ndev,
861
				    "Receive Queue Filtering disabled\n");
S
Sebastian Poehn 已提交
862 863 864 865 866
			return -EOPNOTSUPP;
		}
	}

	/* Sets the properties for arbitrary filer rule
867 868
	 * to the first 4 Layer 4 Bytes
	 */
869
	gfar_write(&regs->rbifx, 0xC0C1C2C3);
S
Sebastian Poehn 已提交
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
	return 0;
}

/* 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);
885 886
	tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
				   RQFCR_AND;
S
Sebastian Poehn 已提交
887 888 889 890 891
	tab->fe[tab->index].prop = value;
	tab->index++;
}

static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
892
				       struct filer_table *tab)
S
Sebastian Poehn 已提交
893 894 895 896 897 898 899
{
	gfar_set_mask(mask, tab);
	tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
	tab->fe[tab->index].prop = value;
	tab->index++;
}

900
/* For setting a tuple of value and mask of type flag
S
Sebastian Poehn 已提交
901 902 903 904 905 906 907 908 909 910 911 912 913 914
 * 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,
915
			       struct filer_table *tab)
S
Sebastian Poehn 已提交
916 917
{
	switch (flag) {
S
Sebastian Poehn 已提交
918
		/* 3bit */
S
Sebastian Poehn 已提交
919 920 921 922 923 924 925 926 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 963 964 965 966 967 968 969 970 971 972
	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,
973 974
			      struct ethtool_tcpip4_spec *mask,
			      struct filer_table *tab)
S
Sebastian Poehn 已提交
975
{
976 977 978 979 980 981 982 983 984 985 986 987
	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 已提交
988 989 990 991 992
	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,
993 994
			     struct ethtool_usrip4_spec *mask,
			     struct filer_table *tab)
S
Sebastian Poehn 已提交
995
{
996 997 998 999 1000 1001
	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 已提交
1002 1003
	gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
	gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
1004 1005 1006
	gfar_set_attribute(be32_to_cpu(value->l4_4_bytes),
			   be32_to_cpu(mask->l4_4_bytes),
			   RQFCR_PID_ARB, tab);
S
Sebastian Poehn 已提交
1007 1008 1009 1010 1011

}

/* Translates value and mask for ETHER spec */
static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
1012
			   struct filer_table *tab)
S
Sebastian Poehn 已提交
1013 1014 1015
{
	u32 upper_temp_mask = 0;
	u32 lower_temp_mask = 0;
1016

S
Sebastian Poehn 已提交
1017 1018 1019 1020 1021 1022
	/* 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 {
1023 1024 1025 1026 1027 1028
			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 已提交
1029 1030
		}
		/* Upper 24bit */
1031 1032 1033 1034
		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 已提交
1035
		/* And the same for the lower part */
1036 1037 1038 1039
		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 已提交
1040 1041 1042 1043
	}
	/* Destination address */
	if (!is_broadcast_ether_addr(mask->h_dest)) {
		/* Special for destination is limited broadcast */
1044 1045
		if ((is_broadcast_ether_addr(value->h_dest) &&
		    is_zero_ether_addr(mask->h_dest))) {
S
Sebastian Poehn 已提交
1046 1047 1048 1049 1050 1051
			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 {
1052 1053 1054 1055 1056 1057
				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 已提交
1058 1059 1060
			}

			/* Upper 24bit */
1061 1062 1063 1064
			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 已提交
1065
			/* And the same for the lower part */
1066 1067 1068 1069
			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 已提交
1070 1071 1072
		}
	}

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
	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 已提交
1108 1109 1110 1111
}

/* Convert a rule to binary filter format of gianfar */
static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
1112
				 struct filer_table *tab)
S
Sebastian Poehn 已提交
1113 1114 1115 1116 1117 1118 1119 1120
{
	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 */
1121 1122
	if ((rule->flow_type & FLOW_EXT) &&
	    (rule->m_ext.vlan_tci != cpu_to_be16(0xFFFF))) {
S
Sebastian Poehn 已提交
1123
		if (!rule->m_ext.vlan_tci)
1124
			rule->m_ext.vlan_tci = cpu_to_be16(0xFFFF);
S
Sebastian Poehn 已提交
1125 1126 1127 1128 1129

		vlan = RQFPR_VLN;
		vlan_mask = RQFPR_VLN;

		/* Separate the fields */
1130 1131 1132 1133 1134 1135
		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 已提交
1136

1137 1138 1139
		if (cfi_mask) {
			if (cfi)
				vlan |= RQFPR_CFI;
S
Sebastian Poehn 已提交
1140 1141 1142 1143 1144 1145 1146
			vlan_mask |= RQFPR_CFI;
		}
	}

	switch (rule->flow_type & ~FLOW_EXT) {
	case TCP_V4_FLOW:
		gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
1147
				    RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
S
Sebastian Poehn 已提交
1148
		gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
1149
				  &rule->m_u.tcp_ip4_spec, tab);
S
Sebastian Poehn 已提交
1150 1151 1152
		break;
	case UDP_V4_FLOW:
		gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
1153
				    RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
S
Sebastian Poehn 已提交
1154
		gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
1155
				  &rule->m_u.udp_ip4_spec, tab);
S
Sebastian Poehn 已提交
1156 1157 1158
		break;
	case SCTP_V4_FLOW:
		gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1159
				    tab);
S
Sebastian Poehn 已提交
1160
		gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
1161 1162 1163
		gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
				  (struct ethtool_tcpip4_spec *)&rule->m_u,
				  tab);
S
Sebastian Poehn 已提交
1164 1165 1166
		break;
	case IP_USER_FLOW:
		gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1167
				    tab);
S
Sebastian Poehn 已提交
1168
		gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
1169 1170
				 (struct ethtool_usrip4_spec *) &rule->m_u,
				 tab);
S
Sebastian Poehn 已提交
1171 1172 1173 1174 1175
		break;
	case ETHER_FLOW:
		if (vlan)
			gfar_set_parse_bits(vlan, vlan_mask, tab);
		gfar_set_ether((struct ethhdr *) &rule->h_u,
1176
			       (struct ethhdr *) &rule->m_u, tab);
S
Sebastian Poehn 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
		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;
	}

1211 1212 1213
	/* In rare cases the cache can be full while there is
	 * free space in hw
	 */
S
Sebastian Poehn 已提交
1214 1215 1216 1217 1218 1219 1220 1221
	if (tab->index > MAX_FILER_CACHE_IDX - 1)
		return -EBUSY;

	return 0;
}

/* Write the bit-pattern from software's buffer to hardware registers */
static int gfar_write_filer_table(struct gfar_private *priv,
1222
				  struct filer_table *tab)
S
Sebastian Poehn 已提交
1223 1224 1225 1226 1227 1228
{
	u32 i = 0;
	if (tab->index > MAX_FILER_IDX - 1)
		return -EBUSY;

	/* Fill regular entries */
1229
	for (; i < MAX_FILER_IDX && (tab->fe[i].ctrl | tab->fe[i].prop); i++)
S
Sebastian Poehn 已提交
1230 1231
		gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
	/* Fill the rest with fall-troughs */
1232
	for (; i < MAX_FILER_IDX; i++)
S
Sebastian Poehn 已提交
1233 1234
		gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
	/* Last entry must be default accept
1235 1236
	 * because that's what people expect
	 */
S
Sebastian Poehn 已提交
1237 1238 1239 1240 1241 1242
	gfar_write_filer(priv, i, 0x20, 0x0);

	return 0;
}

static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
1243
				 struct gfar_private *priv)
S
Sebastian Poehn 已提交
1244 1245 1246 1247 1248
{

	if (flow->flow_type & FLOW_EXT)	{
		if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
			netdev_warn(priv->ndev,
1249
				    "User-specific data not supported!\n");
S
Sebastian Poehn 已提交
1250 1251
		if (~flow->m_ext.vlan_etype)
			netdev_warn(priv->ndev,
1252
				    "VLAN-etype not supported!\n");
S
Sebastian Poehn 已提交
1253 1254 1255 1256
	}
	if (flow->flow_type == IP_USER_FLOW)
		if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
			netdev_warn(priv->ndev,
1257
				    "IP-Version differing from IPv4 not supported!\n");
S
Sebastian Poehn 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

	return 0;
}

static int gfar_process_filer_changes(struct gfar_private *priv)
{
	struct ethtool_flow_spec_container *j;
	struct filer_table *tab;
	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
1274 1275
	 * filer tables binary format
	 */
S
Sebastian Poehn 已提交
1276 1277 1278
	list_for_each_entry(j, &priv->rx_list.list, list) {
		ret = gfar_convert_to_filer(&j->fs, tab);
		if (ret == -EBUSY) {
1279 1280
			netdev_err(priv->ndev,
				   "Rule not added: No free space!\n");
S
Sebastian Poehn 已提交
1281 1282 1283
			goto end;
		}
		if (ret == -1) {
1284 1285
			netdev_err(priv->ndev,
				   "Rule not added: Unsupported Flow-type!\n");
S
Sebastian Poehn 已提交
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
			goto end;
		}
	}

	/* 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;
	}

1297 1298
end:
	kfree(tab);
S
Sebastian Poehn 已提交
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
	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;

1309 1310 1311 1312
	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 已提交
1313 1314 1315
}

static int gfar_add_cls(struct gfar_private *priv,
1316
			struct ethtool_rx_flow_spec *flow)
S
Sebastian Poehn 已提交
1317 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 1343 1344
{
	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,
1345 1346
					   "Rule not added: ID %d not free!\n",
					   flow->location);
S
Sebastian Poehn 已提交
1347 1348 1349 1350 1351 1352 1353 1354
				ret = -EBUSY;
				goto clean_mem;
			}
		}
		list_add_tail(&temp->list, &priv->rx_list.list);
	}

process:
1355
	priv->rx_list.count++;
S
Sebastian Poehn 已提交
1356 1357 1358 1359 1360 1361
	ret = gfar_process_filer_changes(priv);
	if (ret)
		goto clean_list;
	return ret;

clean_list:
1362
	priv->rx_list.count--;
S
Sebastian Poehn 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
	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,
1409
			    struct ethtool_rxnfc *cmd, u32 *rule_locs)
S
Sebastian Poehn 已提交
1410 1411 1412 1413 1414
{
	struct ethtool_flow_spec_container *comp;
	u32 i = 0;

	list_for_each_entry(comp, &priv->rx_list.list, list) {
1415 1416 1417 1418
		if (i == cmd->rule_cnt)
			return -EMSGSIZE;
		rule_locs[i] = comp->fs.location;
		i++;
S
Sebastian Poehn 已提交
1419 1420 1421
	}

	cmd->data = MAX_FILER_IDX;
1422
	cmd->rule_cnt = i;
S
Sebastian Poehn 已提交
1423 1424 1425 1426

	return 0;
}

1427 1428 1429 1430 1431
static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
{
	struct gfar_private *priv = netdev_priv(dev);
	int ret = 0;

1432 1433 1434
	if (test_bit(GFAR_RESETTING, &priv->state))
		return -EBUSY;

S
Sebastian Poehn 已提交
1435 1436 1437
	mutex_lock(&priv->rx_queue_access);

	switch (cmd->cmd) {
1438 1439 1440
	case ETHTOOL_SRXFH:
		ret = gfar_set_hash_opts(priv, cmd);
		break;
S
Sebastian Poehn 已提交
1441
	case ETHTOOL_SRXCLSRLINS:
1442 1443 1444
		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 已提交
1445 1446 1447 1448 1449 1450 1451 1452
			ret = -EINVAL;
			break;
		}
		ret = gfar_add_cls(priv, &cmd->fs);
		break;
	case ETHTOOL_SRXCLSRLDEL:
		ret = gfar_del_cls(priv, cmd->fs.location);
		break;
1453 1454 1455 1456
	default:
		ret = -EINVAL;
	}

S
Sebastian Poehn 已提交
1457 1458 1459 1460 1461 1462
	mutex_unlock(&priv->rx_queue_access);

	return ret;
}

static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1463
			u32 *rule_locs)
S
Sebastian Poehn 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
{
	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:
1479
		ret = gfar_get_cls_all(priv, cmd, rule_locs);
S
Sebastian Poehn 已提交
1480 1481 1482 1483 1484 1485
		break;
	default:
		ret = -EINVAL;
		break;
	}

1486 1487 1488
	return ret;
}

1489 1490 1491 1492
static int gfar_get_ts_info(struct net_device *dev,
			    struct ethtool_ts_info *info)
{
	struct gfar_private *priv = netdev_priv(dev);
1493 1494
	struct platform_device *ptp_dev;
	struct device_node *ptp_node;
1495
	struct ptp_qoriq *ptp = NULL;
1496 1497

	info->phc_index = -1;
1498 1499

	if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
1500 1501
		info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
					SOF_TIMESTAMPING_SOFTWARE;
1502 1503
		return 0;
	}
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514

	ptp_node = of_find_compatible_node(NULL, NULL, "fsl,etsec-ptp");
	if (ptp_node) {
		ptp_dev = of_find_device_by_node(ptp_node);
		if (ptp_dev)
			ptp = platform_get_drvdata(ptp_dev);
	}

	if (ptp)
		info->phc_index = ptp->phc_index;

1515 1516 1517 1518 1519 1520 1521
	info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
				SOF_TIMESTAMPING_RX_HARDWARE |
				SOF_TIMESTAMPING_RAW_HARDWARE;
	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
			 (1 << HWTSTAMP_TX_ON);
	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
			   (1 << HWTSTAMP_FILTER_ALL);
1522 1523 1524
	return 0;
}

1525
const struct ethtool_ops gfar_ethtool_ops = {
L
Linus Torvalds 已提交
1526 1527 1528 1529 1530 1531 1532 1533
	.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,
1534 1535
	.get_pauseparam = gfar_gpauseparam,
	.set_pauseparam = gfar_spauseparam,
L
Linus Torvalds 已提交
1536
	.get_strings = gfar_gstrings,
1537
	.get_sset_count = gfar_sset_count,
L
Linus Torvalds 已提交
1538
	.get_ethtool_stats = gfar_fill_stats,
1539 1540
	.get_msglevel = gfar_get_msglevel,
	.set_msglevel = gfar_set_msglevel,
1541 1542 1543 1544
#ifdef CONFIG_PM
	.get_wol = gfar_get_wol,
	.set_wol = gfar_set_wol,
#endif
1545
	.set_rxnfc = gfar_set_nfc,
S
Sebastian Poehn 已提交
1546
	.get_rxnfc = gfar_get_nfc,
1547
	.get_ts_info = gfar_get_ts_info,
1548 1549
	.get_link_ksettings = phy_ethtool_get_link_ksettings,
	.set_link_ksettings = phy_ethtool_set_link_ksettings,
L
Linus Torvalds 已提交
1550
};