PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / net / ethernet / freescale / gianfar_ethtool.c
blob63d234419cc1febaab1a9f143a607c19790289a6
1 /*
2 * drivers/net/ethernet/freescale/gianfar_ethtool.c
4 * Gianfar Ethernet Driver
5 * Ethtool support for Gianfar Enet
6 * Based on e1000 ethtool support
8 * Author: Andy Fleming
9 * Maintainer: Kumar Gala
10 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
12 * Copyright 2003-2006, 2008-2009, 2011 Freescale Semiconductor, Inc.
14 * This software may be used and distributed according to
15 * the terms of the GNU Public License, Version 2, incorporated herein
16 * by reference.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/net_tstamp.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mm.h>
33 #include <asm/io.h>
34 #include <asm/irq.h>
35 #include <asm/uaccess.h>
36 #include <linux/module.h>
37 #include <linux/crc32.h>
38 #include <asm/types.h>
39 #include <linux/ethtool.h>
40 #include <linux/mii.h>
41 #include <linux/phy.h>
42 #include <linux/sort.h>
43 #include <linux/if_vlan.h>
45 #include "gianfar.h"
47 extern void gfar_start(struct net_device *dev);
48 extern int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue,
49 int rx_work_limit);
51 #define GFAR_MAX_COAL_USECS 0xffff
52 #define GFAR_MAX_COAL_FRAMES 0xff
53 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
54 u64 *buf);
55 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
56 static int gfar_gcoalesce(struct net_device *dev,
57 struct ethtool_coalesce *cvals);
58 static int gfar_scoalesce(struct net_device *dev,
59 struct ethtool_coalesce *cvals);
60 static void gfar_gringparam(struct net_device *dev,
61 struct ethtool_ringparam *rvals);
62 static int gfar_sringparam(struct net_device *dev,
63 struct ethtool_ringparam *rvals);
64 static void gfar_gdrvinfo(struct net_device *dev,
65 struct ethtool_drvinfo *drvinfo);
67 static const char stat_gstrings[][ETH_GSTRING_LEN] = {
68 "rx-large-frame-errors",
69 "rx-short-frame-errors",
70 "rx-non-octet-errors",
71 "rx-crc-errors",
72 "rx-overrun-errors",
73 "rx-busy-errors",
74 "rx-babbling-errors",
75 "rx-truncated-frames",
76 "ethernet-bus-error",
77 "tx-babbling-errors",
78 "tx-underrun-errors",
79 "rx-skb-missing-errors",
80 "tx-timeout-errors",
81 "tx-rx-64-frames",
82 "tx-rx-65-127-frames",
83 "tx-rx-128-255-frames",
84 "tx-rx-256-511-frames",
85 "tx-rx-512-1023-frames",
86 "tx-rx-1024-1518-frames",
87 "tx-rx-1519-1522-good-vlan",
88 "rx-bytes",
89 "rx-packets",
90 "rx-fcs-errors",
91 "receive-multicast-packet",
92 "receive-broadcast-packet",
93 "rx-control-frame-packets",
94 "rx-pause-frame-packets",
95 "rx-unknown-op-code",
96 "rx-alignment-error",
97 "rx-frame-length-error",
98 "rx-code-error",
99 "rx-carrier-sense-error",
100 "rx-undersize-packets",
101 "rx-oversize-packets",
102 "rx-fragmented-frames",
103 "rx-jabber-frames",
104 "rx-dropped-frames",
105 "tx-byte-counter",
106 "tx-packets",
107 "tx-multicast-packets",
108 "tx-broadcast-packets",
109 "tx-pause-control-frames",
110 "tx-deferral-packets",
111 "tx-excessive-deferral-packets",
112 "tx-single-collision-packets",
113 "tx-multiple-collision-packets",
114 "tx-late-collision-packets",
115 "tx-excessive-collision-packets",
116 "tx-total-collision",
117 "reserved",
118 "tx-dropped-frames",
119 "tx-jabber-frames",
120 "tx-fcs-errors",
121 "tx-control-frames",
122 "tx-oversize-frames",
123 "tx-undersize-frames",
124 "tx-fragmented-frames",
127 /* Fill in a buffer with the strings which correspond to the
128 * stats */
129 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
131 struct gfar_private *priv = netdev_priv(dev);
133 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
134 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
135 else
136 memcpy(buf, stat_gstrings,
137 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
140 /* Fill in an array of 64-bit statistics from various sources.
141 * This array will be appended to the end of the ethtool_stats
142 * structure, and returned to user space
144 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
145 u64 *buf)
147 int i;
148 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;
152 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
153 buf[i] = atomic64_read(&extra[i]);
155 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
156 u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
158 for (; i < GFAR_STATS_LEN; i++, rmon++)
159 buf[i] = (u64) gfar_read(rmon);
163 static int gfar_sset_count(struct net_device *dev, int sset)
165 struct gfar_private *priv = netdev_priv(dev);
167 switch (sset) {
168 case ETH_SS_STATS:
169 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
170 return GFAR_STATS_LEN;
171 else
172 return GFAR_EXTRA_STATS_LEN;
173 default:
174 return -EOPNOTSUPP;
178 /* Fills in the drvinfo structure with some basic info */
179 static void gfar_gdrvinfo(struct net_device *dev,
180 struct ethtool_drvinfo *drvinfo)
182 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
183 strlcpy(drvinfo->version, gfar_driver_version,
184 sizeof(drvinfo->version));
185 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
186 strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
187 drvinfo->regdump_len = 0;
188 drvinfo->eedump_len = 0;
192 static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
194 struct gfar_private *priv = netdev_priv(dev);
195 struct phy_device *phydev = priv->phydev;
197 if (NULL == phydev)
198 return -ENODEV;
200 return phy_ethtool_sset(phydev, cmd);
204 /* Return the current settings in the ethtool_cmd structure */
205 static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
207 struct gfar_private *priv = netdev_priv(dev);
208 struct phy_device *phydev = priv->phydev;
209 struct gfar_priv_rx_q *rx_queue = NULL;
210 struct gfar_priv_tx_q *tx_queue = NULL;
212 if (NULL == phydev)
213 return -ENODEV;
214 tx_queue = priv->tx_queue[0];
215 rx_queue = priv->rx_queue[0];
217 /* etsec-1.7 and older versions have only one txic
218 * and rxic regs although they support multiple queues */
219 cmd->maxtxpkt = get_icft_value(tx_queue->txic);
220 cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
222 return phy_ethtool_gset(phydev, cmd);
225 /* Return the length of the register structure */
226 static int gfar_reglen(struct net_device *dev)
228 return sizeof (struct gfar);
231 /* Return a dump of the GFAR register space */
232 static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
233 void *regbuf)
235 int i;
236 struct gfar_private *priv = netdev_priv(dev);
237 u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
238 u32 *buf = (u32 *) regbuf;
240 for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
241 buf[i] = gfar_read(&theregs[i]);
244 /* Convert microseconds to ethernet clock ticks, which changes
245 * depending on what speed the controller is running at */
246 static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
247 unsigned int usecs)
249 unsigned int count;
251 /* The timer is different, depending on the interface speed */
252 switch (priv->phydev->speed) {
253 case SPEED_1000:
254 count = GFAR_GBIT_TIME;
255 break;
256 case SPEED_100:
257 count = GFAR_100_TIME;
258 break;
259 case SPEED_10:
260 default:
261 count = GFAR_10_TIME;
262 break;
265 /* Make sure we return a number greater than 0
266 * if usecs > 0 */
267 return (usecs * 1000 + count - 1) / count;
270 /* Convert ethernet clock ticks to microseconds */
271 static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
272 unsigned int ticks)
274 unsigned int count;
276 /* The timer is different, depending on the interface speed */
277 switch (priv->phydev->speed) {
278 case SPEED_1000:
279 count = GFAR_GBIT_TIME;
280 break;
281 case SPEED_100:
282 count = GFAR_100_TIME;
283 break;
284 case SPEED_10:
285 default:
286 count = GFAR_10_TIME;
287 break;
290 /* Make sure we return a number greater than 0 */
291 /* if ticks is > 0 */
292 return (ticks * count) / 1000;
295 /* Get the coalescing parameters, and put them in the cvals
296 * structure. */
297 static int gfar_gcoalesce(struct net_device *dev,
298 struct ethtool_coalesce *cvals)
300 struct gfar_private *priv = netdev_priv(dev);
301 struct gfar_priv_rx_q *rx_queue = NULL;
302 struct gfar_priv_tx_q *tx_queue = NULL;
303 unsigned long rxtime;
304 unsigned long rxcount;
305 unsigned long txtime;
306 unsigned long txcount;
308 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
309 return -EOPNOTSUPP;
311 if (NULL == priv->phydev)
312 return -ENODEV;
314 rx_queue = priv->rx_queue[0];
315 tx_queue = priv->tx_queue[0];
317 rxtime = get_ictt_value(rx_queue->rxic);
318 rxcount = get_icft_value(rx_queue->rxic);
319 txtime = get_ictt_value(tx_queue->txic);
320 txcount = get_icft_value(tx_queue->txic);
321 cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
322 cvals->rx_max_coalesced_frames = rxcount;
324 cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
325 cvals->tx_max_coalesced_frames = txcount;
327 cvals->use_adaptive_rx_coalesce = 0;
328 cvals->use_adaptive_tx_coalesce = 0;
330 cvals->pkt_rate_low = 0;
331 cvals->rx_coalesce_usecs_low = 0;
332 cvals->rx_max_coalesced_frames_low = 0;
333 cvals->tx_coalesce_usecs_low = 0;
334 cvals->tx_max_coalesced_frames_low = 0;
336 /* When the packet rate is below pkt_rate_high but above
337 * pkt_rate_low (both measured in packets per second) the
338 * normal {rx,tx}_* coalescing parameters are used.
341 /* When the packet rate is (measured in packets per second)
342 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
343 * used.
345 cvals->pkt_rate_high = 0;
346 cvals->rx_coalesce_usecs_high = 0;
347 cvals->rx_max_coalesced_frames_high = 0;
348 cvals->tx_coalesce_usecs_high = 0;
349 cvals->tx_max_coalesced_frames_high = 0;
351 /* How often to do adaptive coalescing packet rate sampling,
352 * measured in seconds. Must not be zero.
354 cvals->rate_sample_interval = 0;
356 return 0;
359 /* Change the coalescing values.
360 * Both cvals->*_usecs and cvals->*_frames have to be > 0
361 * in order for coalescing to be active
363 static int gfar_scoalesce(struct net_device *dev,
364 struct ethtool_coalesce *cvals)
366 struct gfar_private *priv = netdev_priv(dev);
367 int i = 0;
369 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
370 return -EOPNOTSUPP;
372 /* Set up rx coalescing */
373 /* As of now, we will enable/disable coalescing for all
374 * queues together in case of eTSEC2, this will be modified
375 * along with the ethtool interface
377 if ((cvals->rx_coalesce_usecs == 0) ||
378 (cvals->rx_max_coalesced_frames == 0)) {
379 for (i = 0; i < priv->num_rx_queues; i++)
380 priv->rx_queue[i]->rxcoalescing = 0;
381 } else {
382 for (i = 0; i < priv->num_rx_queues; i++)
383 priv->rx_queue[i]->rxcoalescing = 1;
386 if (NULL == priv->phydev)
387 return -ENODEV;
389 /* Check the bounds of the values */
390 if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
391 netdev_info(dev, "Coalescing is limited to %d microseconds\n",
392 GFAR_MAX_COAL_USECS);
393 return -EINVAL;
396 if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
397 netdev_info(dev, "Coalescing is limited to %d frames\n",
398 GFAR_MAX_COAL_FRAMES);
399 return -EINVAL;
402 for (i = 0; i < priv->num_rx_queues; i++) {
403 priv->rx_queue[i]->rxic = mk_ic_value(
404 cvals->rx_max_coalesced_frames,
405 gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
408 /* Set up tx coalescing */
409 if ((cvals->tx_coalesce_usecs == 0) ||
410 (cvals->tx_max_coalesced_frames == 0)) {
411 for (i = 0; i < priv->num_tx_queues; i++)
412 priv->tx_queue[i]->txcoalescing = 0;
413 } else {
414 for (i = 0; i < priv->num_tx_queues; i++)
415 priv->tx_queue[i]->txcoalescing = 1;
418 /* Check the bounds of the values */
419 if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
420 netdev_info(dev, "Coalescing is limited to %d microseconds\n",
421 GFAR_MAX_COAL_USECS);
422 return -EINVAL;
425 if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
426 netdev_info(dev, "Coalescing is limited to %d frames\n",
427 GFAR_MAX_COAL_FRAMES);
428 return -EINVAL;
431 for (i = 0; i < priv->num_tx_queues; i++) {
432 priv->tx_queue[i]->txic = mk_ic_value(
433 cvals->tx_max_coalesced_frames,
434 gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
437 gfar_configure_coalescing_all(priv);
439 return 0;
442 /* Fills in rvals with the current ring parameters. Currently,
443 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
444 * jumbo are ignored by the driver */
445 static void gfar_gringparam(struct net_device *dev,
446 struct ethtool_ringparam *rvals)
448 struct gfar_private *priv = netdev_priv(dev);
449 struct gfar_priv_tx_q *tx_queue = NULL;
450 struct gfar_priv_rx_q *rx_queue = NULL;
452 tx_queue = priv->tx_queue[0];
453 rx_queue = priv->rx_queue[0];
455 rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
456 rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
457 rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
458 rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
460 /* Values changeable by the user. The valid values are
461 * in the range 1 to the "*_max_pending" counterpart above.
463 rvals->rx_pending = rx_queue->rx_ring_size;
464 rvals->rx_mini_pending = rx_queue->rx_ring_size;
465 rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
466 rvals->tx_pending = tx_queue->tx_ring_size;
469 /* Change the current ring parameters, stopping the controller if
470 * necessary so that we don't mess things up while we're in
471 * motion. We wait for the ring to be clean before reallocating
472 * the rings.
474 static int gfar_sringparam(struct net_device *dev,
475 struct ethtool_ringparam *rvals)
477 struct gfar_private *priv = netdev_priv(dev);
478 int err = 0, i = 0;
480 if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
481 return -EINVAL;
483 if (!is_power_of_2(rvals->rx_pending)) {
484 netdev_err(dev, "Ring sizes must be a power of 2\n");
485 return -EINVAL;
488 if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
489 return -EINVAL;
491 if (!is_power_of_2(rvals->tx_pending)) {
492 netdev_err(dev, "Ring sizes must be a power of 2\n");
493 return -EINVAL;
497 if (dev->flags & IFF_UP) {
498 unsigned long flags;
500 /* Halt TX and RX, and process the frames which
501 * have already been received
503 local_irq_save(flags);
504 lock_tx_qs(priv);
505 lock_rx_qs(priv);
507 gfar_halt(dev);
509 unlock_rx_qs(priv);
510 unlock_tx_qs(priv);
511 local_irq_restore(flags);
513 for (i = 0; i < priv->num_rx_queues; i++)
514 gfar_clean_rx_ring(priv->rx_queue[i],
515 priv->rx_queue[i]->rx_ring_size);
517 /* Now we take down the rings to rebuild them */
518 stop_gfar(dev);
521 /* Change the size */
522 for (i = 0; i < priv->num_rx_queues; i++) {
523 priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
524 priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
525 priv->tx_queue[i]->num_txbdfree =
526 priv->tx_queue[i]->tx_ring_size;
529 /* Rebuild the rings with the new size */
530 if (dev->flags & IFF_UP) {
531 err = startup_gfar(dev);
532 netif_tx_wake_all_queues(dev);
534 return err;
537 static void gfar_gpauseparam(struct net_device *dev,
538 struct ethtool_pauseparam *epause)
540 struct gfar_private *priv = netdev_priv(dev);
542 epause->autoneg = !!priv->pause_aneg_en;
543 epause->rx_pause = !!priv->rx_pause_en;
544 epause->tx_pause = !!priv->tx_pause_en;
547 static int gfar_spauseparam(struct net_device *dev,
548 struct ethtool_pauseparam *epause)
550 struct gfar_private *priv = netdev_priv(dev);
551 struct phy_device *phydev = priv->phydev;
552 struct gfar __iomem *regs = priv->gfargrp[0].regs;
553 u32 oldadv, newadv;
555 if (!(phydev->supported & SUPPORTED_Pause) ||
556 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
557 (epause->rx_pause != epause->tx_pause)))
558 return -EINVAL;
560 priv->rx_pause_en = priv->tx_pause_en = 0;
561 if (epause->rx_pause) {
562 priv->rx_pause_en = 1;
564 if (epause->tx_pause) {
565 priv->tx_pause_en = 1;
566 /* FLOW_CTRL_RX & TX */
567 newadv = ADVERTISED_Pause;
568 } else /* FLOW_CTLR_RX */
569 newadv = ADVERTISED_Pause | ADVERTISED_Asym_Pause;
570 } else if (epause->tx_pause) {
571 priv->tx_pause_en = 1;
572 /* FLOW_CTLR_TX */
573 newadv = ADVERTISED_Asym_Pause;
574 } else
575 newadv = 0;
577 if (epause->autoneg)
578 priv->pause_aneg_en = 1;
579 else
580 priv->pause_aneg_en = 0;
582 oldadv = phydev->advertising &
583 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
584 if (oldadv != newadv) {
585 phydev->advertising &=
586 ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
587 phydev->advertising |= newadv;
588 if (phydev->autoneg)
589 /* inform link partner of our
590 * new flow ctrl settings
592 return phy_start_aneg(phydev);
594 if (!epause->autoneg) {
595 u32 tempval;
596 tempval = gfar_read(&regs->maccfg1);
597 tempval &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
598 if (priv->tx_pause_en)
599 tempval |= MACCFG1_TX_FLOW;
600 if (priv->rx_pause_en)
601 tempval |= MACCFG1_RX_FLOW;
602 gfar_write(&regs->maccfg1, tempval);
606 return 0;
609 int gfar_set_features(struct net_device *dev, netdev_features_t features)
611 struct gfar_private *priv = netdev_priv(dev);
612 unsigned long flags;
613 int err = 0, i = 0;
614 netdev_features_t changed = dev->features ^ features;
616 if (changed & (NETIF_F_HW_VLAN_CTAG_TX|NETIF_F_HW_VLAN_CTAG_RX))
617 gfar_vlan_mode(dev, features);
619 if (!(changed & NETIF_F_RXCSUM))
620 return 0;
622 if (dev->flags & IFF_UP) {
623 /* Halt TX and RX, and process the frames which
624 * have already been received
626 local_irq_save(flags);
627 lock_tx_qs(priv);
628 lock_rx_qs(priv);
630 gfar_halt(dev);
632 unlock_tx_qs(priv);
633 unlock_rx_qs(priv);
634 local_irq_restore(flags);
636 for (i = 0; i < priv->num_rx_queues; i++)
637 gfar_clean_rx_ring(priv->rx_queue[i],
638 priv->rx_queue[i]->rx_ring_size);
640 /* Now we take down the rings to rebuild them */
641 stop_gfar(dev);
643 dev->features = features;
645 err = startup_gfar(dev);
646 netif_tx_wake_all_queues(dev);
648 return err;
651 static uint32_t gfar_get_msglevel(struct net_device *dev)
653 struct gfar_private *priv = netdev_priv(dev);
655 return priv->msg_enable;
658 static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
660 struct gfar_private *priv = netdev_priv(dev);
662 priv->msg_enable = data;
665 #ifdef CONFIG_PM
666 static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
668 struct gfar_private *priv = netdev_priv(dev);
670 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
671 wol->supported = WAKE_MAGIC;
672 wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
673 } else {
674 wol->supported = wol->wolopts = 0;
678 static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
680 struct gfar_private *priv = netdev_priv(dev);
681 unsigned long flags;
683 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
684 wol->wolopts != 0)
685 return -EINVAL;
687 if (wol->wolopts & ~WAKE_MAGIC)
688 return -EINVAL;
690 device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);
692 spin_lock_irqsave(&priv->bflock, flags);
693 priv->wol_en = !!device_may_wakeup(&dev->dev);
694 spin_unlock_irqrestore(&priv->bflock, flags);
696 return 0;
698 #endif
700 static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
702 u32 fcr = 0x0, fpr = FPR_FILER_MASK;
704 if (ethflow & RXH_L2DA) {
705 fcr = RQFCR_PID_DAH |RQFCR_CMP_NOMATCH |
706 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
707 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
708 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
709 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
710 priv->cur_filer_idx = priv->cur_filer_idx - 1;
712 fcr = RQFCR_PID_DAL | RQFCR_AND | RQFCR_CMP_NOMATCH |
713 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
714 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
715 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
716 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
717 priv->cur_filer_idx = priv->cur_filer_idx - 1;
720 if (ethflow & RXH_VLAN) {
721 fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
722 RQFCR_AND | RQFCR_HASHTBL_0;
723 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
724 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
725 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
726 priv->cur_filer_idx = priv->cur_filer_idx - 1;
729 if (ethflow & RXH_IP_SRC) {
730 fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
731 RQFCR_AND | RQFCR_HASHTBL_0;
732 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
733 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
734 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
735 priv->cur_filer_idx = priv->cur_filer_idx - 1;
738 if (ethflow & (RXH_IP_DST)) {
739 fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
740 RQFCR_AND | RQFCR_HASHTBL_0;
741 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
742 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
743 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
744 priv->cur_filer_idx = priv->cur_filer_idx - 1;
747 if (ethflow & RXH_L3_PROTO) {
748 fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
749 RQFCR_AND | RQFCR_HASHTBL_0;
750 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
751 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
752 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
753 priv->cur_filer_idx = priv->cur_filer_idx - 1;
756 if (ethflow & RXH_L4_B_0_1) {
757 fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
758 RQFCR_AND | RQFCR_HASHTBL_0;
759 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
760 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
761 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
762 priv->cur_filer_idx = priv->cur_filer_idx - 1;
765 if (ethflow & RXH_L4_B_2_3) {
766 fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
767 RQFCR_AND | RQFCR_HASHTBL_0;
768 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
769 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
770 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
771 priv->cur_filer_idx = priv->cur_filer_idx - 1;
775 static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
776 u64 class)
778 unsigned int last_rule_idx = priv->cur_filer_idx;
779 unsigned int cmp_rqfpr;
780 unsigned int *local_rqfpr;
781 unsigned int *local_rqfcr;
782 int i = 0x0, k = 0x0;
783 int j = MAX_FILER_IDX, l = 0x0;
784 int ret = 1;
786 local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
787 GFP_KERNEL);
788 local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
789 GFP_KERNEL);
790 if (!local_rqfpr || !local_rqfcr) {
791 ret = 0;
792 goto err;
795 switch (class) {
796 case TCP_V4_FLOW:
797 cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
798 break;
799 case UDP_V4_FLOW:
800 cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
801 break;
802 case TCP_V6_FLOW:
803 cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
804 break;
805 case UDP_V6_FLOW:
806 cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
807 break;
808 default:
809 netdev_err(priv->ndev,
810 "Right now this class is not supported\n");
811 ret = 0;
812 goto err;
815 for (i = 0; i < MAX_FILER_IDX + 1; i++) {
816 local_rqfpr[j] = priv->ftp_rqfpr[i];
817 local_rqfcr[j] = priv->ftp_rqfcr[i];
818 j--;
819 if ((priv->ftp_rqfcr[i] ==
820 (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
821 (priv->ftp_rqfpr[i] == cmp_rqfpr))
822 break;
825 if (i == MAX_FILER_IDX + 1) {
826 netdev_err(priv->ndev,
827 "No parse rule found, can't create hash rules\n");
828 ret = 0;
829 goto err;
832 /* If a match was found, then it begins the starting of a cluster rule
833 * if it was already programmed, we need to overwrite these rules
835 for (l = i+1; l < MAX_FILER_IDX; l++) {
836 if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
837 !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
838 priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
839 RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
840 priv->ftp_rqfpr[l] = FPR_FILER_MASK;
841 gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
842 priv->ftp_rqfpr[l]);
843 break;
846 if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
847 (priv->ftp_rqfcr[l] & RQFCR_AND))
848 continue;
849 else {
850 local_rqfpr[j] = priv->ftp_rqfpr[l];
851 local_rqfcr[j] = priv->ftp_rqfcr[l];
852 j--;
856 priv->cur_filer_idx = l - 1;
857 last_rule_idx = l;
859 /* hash rules */
860 ethflow_to_filer_rules(priv, ethflow);
862 /* Write back the popped out rules again */
863 for (k = j+1; k < MAX_FILER_IDX; k++) {
864 priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
865 priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
866 gfar_write_filer(priv, priv->cur_filer_idx,
867 local_rqfcr[k], local_rqfpr[k]);
868 if (!priv->cur_filer_idx)
869 break;
870 priv->cur_filer_idx = priv->cur_filer_idx - 1;
873 err:
874 kfree(local_rqfcr);
875 kfree(local_rqfpr);
876 return ret;
879 static int gfar_set_hash_opts(struct gfar_private *priv,
880 struct ethtool_rxnfc *cmd)
882 /* write the filer rules here */
883 if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
884 return -EINVAL;
886 return 0;
889 static int gfar_check_filer_hardware(struct gfar_private *priv)
891 struct gfar __iomem *regs = priv->gfargrp[0].regs;
892 u32 i;
894 /* Check if we are in FIFO mode */
895 i = gfar_read(&regs->ecntrl);
896 i &= ECNTRL_FIFM;
897 if (i == ECNTRL_FIFM) {
898 netdev_notice(priv->ndev, "Interface in FIFO mode\n");
899 i = gfar_read(&regs->rctrl);
900 i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
901 if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
902 netdev_info(priv->ndev,
903 "Receive Queue Filtering enabled\n");
904 } else {
905 netdev_warn(priv->ndev,
906 "Receive Queue Filtering disabled\n");
907 return -EOPNOTSUPP;
910 /* Or in standard mode */
911 else {
912 i = gfar_read(&regs->rctrl);
913 i &= RCTRL_PRSDEP_MASK;
914 if (i == RCTRL_PRSDEP_MASK) {
915 netdev_info(priv->ndev,
916 "Receive Queue Filtering enabled\n");
917 } else {
918 netdev_warn(priv->ndev,
919 "Receive Queue Filtering disabled\n");
920 return -EOPNOTSUPP;
924 /* Sets the properties for arbitrary filer rule
925 * to the first 4 Layer 4 Bytes
927 gfar_write(&regs->rbifx, 0xC0C1C2C3);
928 return 0;
931 static int gfar_comp_asc(const void *a, const void *b)
933 return memcmp(a, b, 4);
936 static int gfar_comp_desc(const void *a, const void *b)
938 return -memcmp(a, b, 4);
941 static void gfar_swap(void *a, void *b, int size)
943 u32 *_a = a;
944 u32 *_b = b;
946 swap(_a[0], _b[0]);
947 swap(_a[1], _b[1]);
948 swap(_a[2], _b[2]);
949 swap(_a[3], _b[3]);
952 /* Write a mask to filer cache */
953 static void gfar_set_mask(u32 mask, struct filer_table *tab)
955 tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
956 tab->fe[tab->index].prop = mask;
957 tab->index++;
960 /* Sets parse bits (e.g. IP or TCP) */
961 static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
963 gfar_set_mask(mask, tab);
964 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
965 RQFCR_AND;
966 tab->fe[tab->index].prop = value;
967 tab->index++;
970 static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
971 struct filer_table *tab)
973 gfar_set_mask(mask, tab);
974 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
975 tab->fe[tab->index].prop = value;
976 tab->index++;
979 /* For setting a tuple of value and mask of type flag
980 * Example:
981 * IP-Src = 10.0.0.0/255.0.0.0
982 * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
984 * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
985 * For a don't care mask it gives us a 0
987 * The check if don't care and the mask adjustment if mask=0 is done for VLAN
988 * and MAC stuff on an upper level (due to missing information on this level).
989 * For these guys we can discard them if they are value=0 and mask=0.
991 * Further the all masks are one-padded for better hardware efficiency.
993 static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
994 struct filer_table *tab)
996 switch (flag) {
997 /* 3bit */
998 case RQFCR_PID_PRI:
999 if (!(value | mask))
1000 return;
1001 mask |= RQFCR_PID_PRI_MASK;
1002 break;
1003 /* 8bit */
1004 case RQFCR_PID_L4P:
1005 case RQFCR_PID_TOS:
1006 if (!~(mask | RQFCR_PID_L4P_MASK))
1007 return;
1008 if (!mask)
1009 mask = ~0;
1010 else
1011 mask |= RQFCR_PID_L4P_MASK;
1012 break;
1013 /* 12bit */
1014 case RQFCR_PID_VID:
1015 if (!(value | mask))
1016 return;
1017 mask |= RQFCR_PID_VID_MASK;
1018 break;
1019 /* 16bit */
1020 case RQFCR_PID_DPT:
1021 case RQFCR_PID_SPT:
1022 case RQFCR_PID_ETY:
1023 if (!~(mask | RQFCR_PID_PORT_MASK))
1024 return;
1025 if (!mask)
1026 mask = ~0;
1027 else
1028 mask |= RQFCR_PID_PORT_MASK;
1029 break;
1030 /* 24bit */
1031 case RQFCR_PID_DAH:
1032 case RQFCR_PID_DAL:
1033 case RQFCR_PID_SAH:
1034 case RQFCR_PID_SAL:
1035 if (!(value | mask))
1036 return;
1037 mask |= RQFCR_PID_MAC_MASK;
1038 break;
1039 /* for all real 32bit masks */
1040 default:
1041 if (!~mask)
1042 return;
1043 if (!mask)
1044 mask = ~0;
1045 break;
1047 gfar_set_general_attribute(value, mask, flag, tab);
1050 /* Translates value and mask for UDP, TCP or SCTP */
1051 static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
1052 struct ethtool_tcpip4_spec *mask,
1053 struct filer_table *tab)
1055 gfar_set_attribute(be32_to_cpu(value->ip4src),
1056 be32_to_cpu(mask->ip4src),
1057 RQFCR_PID_SIA, tab);
1058 gfar_set_attribute(be32_to_cpu(value->ip4dst),
1059 be32_to_cpu(mask->ip4dst),
1060 RQFCR_PID_DIA, tab);
1061 gfar_set_attribute(be16_to_cpu(value->pdst),
1062 be16_to_cpu(mask->pdst),
1063 RQFCR_PID_DPT, tab);
1064 gfar_set_attribute(be16_to_cpu(value->psrc),
1065 be16_to_cpu(mask->psrc),
1066 RQFCR_PID_SPT, tab);
1067 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1070 /* Translates value and mask for RAW-IP4 */
1071 static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
1072 struct ethtool_usrip4_spec *mask,
1073 struct filer_table *tab)
1075 gfar_set_attribute(be32_to_cpu(value->ip4src),
1076 be32_to_cpu(mask->ip4src),
1077 RQFCR_PID_SIA, tab);
1078 gfar_set_attribute(be32_to_cpu(value->ip4dst),
1079 be32_to_cpu(mask->ip4dst),
1080 RQFCR_PID_DIA, tab);
1081 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1082 gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
1083 gfar_set_attribute(be32_to_cpu(value->l4_4_bytes),
1084 be32_to_cpu(mask->l4_4_bytes),
1085 RQFCR_PID_ARB, tab);
1089 /* Translates value and mask for ETHER spec */
1090 static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
1091 struct filer_table *tab)
1093 u32 upper_temp_mask = 0;
1094 u32 lower_temp_mask = 0;
1096 /* Source address */
1097 if (!is_broadcast_ether_addr(mask->h_source)) {
1098 if (is_zero_ether_addr(mask->h_source)) {
1099 upper_temp_mask = 0xFFFFFFFF;
1100 lower_temp_mask = 0xFFFFFFFF;
1101 } else {
1102 upper_temp_mask = mask->h_source[0] << 16 |
1103 mask->h_source[1] << 8 |
1104 mask->h_source[2];
1105 lower_temp_mask = mask->h_source[3] << 16 |
1106 mask->h_source[4] << 8 |
1107 mask->h_source[5];
1109 /* Upper 24bit */
1110 gfar_set_attribute(value->h_source[0] << 16 |
1111 value->h_source[1] << 8 |
1112 value->h_source[2],
1113 upper_temp_mask, RQFCR_PID_SAH, tab);
1114 /* And the same for the lower part */
1115 gfar_set_attribute(value->h_source[3] << 16 |
1116 value->h_source[4] << 8 |
1117 value->h_source[5],
1118 lower_temp_mask, RQFCR_PID_SAL, tab);
1120 /* Destination address */
1121 if (!is_broadcast_ether_addr(mask->h_dest)) {
1122 /* Special for destination is limited broadcast */
1123 if ((is_broadcast_ether_addr(value->h_dest) &&
1124 is_zero_ether_addr(mask->h_dest))) {
1125 gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
1126 } else {
1127 if (is_zero_ether_addr(mask->h_dest)) {
1128 upper_temp_mask = 0xFFFFFFFF;
1129 lower_temp_mask = 0xFFFFFFFF;
1130 } else {
1131 upper_temp_mask = mask->h_dest[0] << 16 |
1132 mask->h_dest[1] << 8 |
1133 mask->h_dest[2];
1134 lower_temp_mask = mask->h_dest[3] << 16 |
1135 mask->h_dest[4] << 8 |
1136 mask->h_dest[5];
1139 /* Upper 24bit */
1140 gfar_set_attribute(value->h_dest[0] << 16 |
1141 value->h_dest[1] << 8 |
1142 value->h_dest[2],
1143 upper_temp_mask, RQFCR_PID_DAH, tab);
1144 /* And the same for the lower part */
1145 gfar_set_attribute(value->h_dest[3] << 16 |
1146 value->h_dest[4] << 8 |
1147 value->h_dest[5],
1148 lower_temp_mask, RQFCR_PID_DAL, tab);
1152 gfar_set_attribute(be16_to_cpu(value->h_proto),
1153 be16_to_cpu(mask->h_proto),
1154 RQFCR_PID_ETY, tab);
1157 static inline u32 vlan_tci_vid(struct ethtool_rx_flow_spec *rule)
1159 return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_VID_MASK;
1162 static inline u32 vlan_tci_vidm(struct ethtool_rx_flow_spec *rule)
1164 return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_VID_MASK;
1167 static inline u32 vlan_tci_cfi(struct ethtool_rx_flow_spec *rule)
1169 return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_CFI_MASK;
1172 static inline u32 vlan_tci_cfim(struct ethtool_rx_flow_spec *rule)
1174 return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_CFI_MASK;
1177 static inline u32 vlan_tci_prio(struct ethtool_rx_flow_spec *rule)
1179 return (be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_PRIO_MASK) >>
1180 VLAN_PRIO_SHIFT;
1183 static inline u32 vlan_tci_priom(struct ethtool_rx_flow_spec *rule)
1185 return (be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_PRIO_MASK) >>
1186 VLAN_PRIO_SHIFT;
1189 /* Convert a rule to binary filter format of gianfar */
1190 static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
1191 struct filer_table *tab)
1193 u32 vlan = 0, vlan_mask = 0;
1194 u32 id = 0, id_mask = 0;
1195 u32 cfi = 0, cfi_mask = 0;
1196 u32 prio = 0, prio_mask = 0;
1197 u32 old_index = tab->index;
1199 /* Check if vlan is wanted */
1200 if ((rule->flow_type & FLOW_EXT) &&
1201 (rule->m_ext.vlan_tci != cpu_to_be16(0xFFFF))) {
1202 if (!rule->m_ext.vlan_tci)
1203 rule->m_ext.vlan_tci = cpu_to_be16(0xFFFF);
1205 vlan = RQFPR_VLN;
1206 vlan_mask = RQFPR_VLN;
1208 /* Separate the fields */
1209 id = vlan_tci_vid(rule);
1210 id_mask = vlan_tci_vidm(rule);
1211 cfi = vlan_tci_cfi(rule);
1212 cfi_mask = vlan_tci_cfim(rule);
1213 prio = vlan_tci_prio(rule);
1214 prio_mask = vlan_tci_priom(rule);
1216 if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
1217 vlan |= RQFPR_CFI;
1218 vlan_mask |= RQFPR_CFI;
1219 } else if (cfi != VLAN_TAG_PRESENT &&
1220 cfi_mask == VLAN_TAG_PRESENT) {
1221 vlan_mask |= RQFPR_CFI;
1225 switch (rule->flow_type & ~FLOW_EXT) {
1226 case TCP_V4_FLOW:
1227 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
1228 RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
1229 gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
1230 &rule->m_u.tcp_ip4_spec, tab);
1231 break;
1232 case UDP_V4_FLOW:
1233 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
1234 RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
1235 gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
1236 &rule->m_u.udp_ip4_spec, tab);
1237 break;
1238 case SCTP_V4_FLOW:
1239 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1240 tab);
1241 gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
1242 gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
1243 (struct ethtool_tcpip4_spec *)&rule->m_u,
1244 tab);
1245 break;
1246 case IP_USER_FLOW:
1247 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1248 tab);
1249 gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
1250 (struct ethtool_usrip4_spec *) &rule->m_u,
1251 tab);
1252 break;
1253 case ETHER_FLOW:
1254 if (vlan)
1255 gfar_set_parse_bits(vlan, vlan_mask, tab);
1256 gfar_set_ether((struct ethhdr *) &rule->h_u,
1257 (struct ethhdr *) &rule->m_u, tab);
1258 break;
1259 default:
1260 return -1;
1263 /* Set the vlan attributes in the end */
1264 if (vlan) {
1265 gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
1266 gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
1269 /* If there has been nothing written till now, it must be a default */
1270 if (tab->index == old_index) {
1271 gfar_set_mask(0xFFFFFFFF, tab);
1272 tab->fe[tab->index].ctrl = 0x20;
1273 tab->fe[tab->index].prop = 0x0;
1274 tab->index++;
1277 /* Remove last AND */
1278 tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
1280 /* Specify which queue to use or to drop */
1281 if (rule->ring_cookie == RX_CLS_FLOW_DISC)
1282 tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
1283 else
1284 tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
1286 /* Only big enough entries can be clustered */
1287 if (tab->index > (old_index + 2)) {
1288 tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
1289 tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
1292 /* In rare cases the cache can be full while there is
1293 * free space in hw
1295 if (tab->index > MAX_FILER_CACHE_IDX - 1)
1296 return -EBUSY;
1298 return 0;
1301 /* Copy size filer entries */
1302 static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
1303 struct gfar_filer_entry src[0], s32 size)
1305 while (size > 0) {
1306 size--;
1307 dst[size].ctrl = src[size].ctrl;
1308 dst[size].prop = src[size].prop;
1312 /* Delete the contents of the filer-table between start and end
1313 * and collapse them
1315 static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
1317 int length;
1319 if (end > MAX_FILER_CACHE_IDX || end < begin)
1320 return -EINVAL;
1322 end++;
1323 length = end - begin;
1325 /* Copy */
1326 while (end < tab->index) {
1327 tab->fe[begin].ctrl = tab->fe[end].ctrl;
1328 tab->fe[begin++].prop = tab->fe[end++].prop;
1331 /* Fill up with don't cares */
1332 while (begin < tab->index) {
1333 tab->fe[begin].ctrl = 0x60;
1334 tab->fe[begin].prop = 0xFFFFFFFF;
1335 begin++;
1338 tab->index -= length;
1339 return 0;
1342 /* Make space on the wanted location */
1343 static int gfar_expand_filer_entries(u32 begin, u32 length,
1344 struct filer_table *tab)
1346 if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX ||
1347 begin > MAX_FILER_CACHE_IDX)
1348 return -EINVAL;
1350 gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
1351 tab->index - length + 1);
1353 tab->index += length;
1354 return 0;
1357 static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
1359 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
1360 start++) {
1361 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
1362 (RQFCR_AND | RQFCR_CLE))
1363 return start;
1365 return -1;
1368 static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
1370 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
1371 start++) {
1372 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
1373 (RQFCR_CLE))
1374 return start;
1376 return -1;
1379 /* Uses hardwares clustering option to reduce
1380 * the number of filer table entries
1382 static void gfar_cluster_filer(struct filer_table *tab)
1384 s32 i = -1, j, iend, jend;
1386 while ((i = gfar_get_next_cluster_start(++i, tab)) != -1) {
1387 j = i;
1388 while ((j = gfar_get_next_cluster_start(++j, tab)) != -1) {
1389 /* The cluster entries self and the previous one
1390 * (a mask) must be identical!
1392 if (tab->fe[i].ctrl != tab->fe[j].ctrl)
1393 break;
1394 if (tab->fe[i].prop != tab->fe[j].prop)
1395 break;
1396 if (tab->fe[i - 1].ctrl != tab->fe[j - 1].ctrl)
1397 break;
1398 if (tab->fe[i - 1].prop != tab->fe[j - 1].prop)
1399 break;
1400 iend = gfar_get_next_cluster_end(i, tab);
1401 jend = gfar_get_next_cluster_end(j, tab);
1402 if (jend == -1 || iend == -1)
1403 break;
1405 /* First we make some free space, where our cluster
1406 * element should be. Then we copy it there and finally
1407 * delete in from its old location.
1409 if (gfar_expand_filer_entries(iend, (jend - j), tab) ==
1410 -EINVAL)
1411 break;
1413 gfar_copy_filer_entries(&(tab->fe[iend + 1]),
1414 &(tab->fe[jend + 1]), jend - j);
1416 if (gfar_trim_filer_entries(jend - 1,
1417 jend + (jend - j),
1418 tab) == -EINVAL)
1419 return;
1421 /* Mask out cluster bit */
1422 tab->fe[iend].ctrl &= ~(RQFCR_CLE);
1427 /* Swaps the masked bits of a1<>a2 and b1<>b2 */
1428 static void gfar_swap_bits(struct gfar_filer_entry *a1,
1429 struct gfar_filer_entry *a2,
1430 struct gfar_filer_entry *b1,
1431 struct gfar_filer_entry *b2, u32 mask)
1433 u32 temp[4];
1434 temp[0] = a1->ctrl & mask;
1435 temp[1] = a2->ctrl & mask;
1436 temp[2] = b1->ctrl & mask;
1437 temp[3] = b2->ctrl & mask;
1439 a1->ctrl &= ~mask;
1440 a2->ctrl &= ~mask;
1441 b1->ctrl &= ~mask;
1442 b2->ctrl &= ~mask;
1444 a1->ctrl |= temp[1];
1445 a2->ctrl |= temp[0];
1446 b1->ctrl |= temp[3];
1447 b2->ctrl |= temp[2];
1450 /* Generate a list consisting of masks values with their start and
1451 * end of validity and block as indicator for parts belonging
1452 * together (glued by ANDs) in mask_table
1454 static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
1455 struct filer_table *tab)
1457 u32 i, and_index = 0, block_index = 1;
1459 for (i = 0; i < tab->index; i++) {
1461 /* LSByte of control = 0 sets a mask */
1462 if (!(tab->fe[i].ctrl & 0xF)) {
1463 mask_table[and_index].mask = tab->fe[i].prop;
1464 mask_table[and_index].start = i;
1465 mask_table[and_index].block = block_index;
1466 if (and_index >= 1)
1467 mask_table[and_index - 1].end = i - 1;
1468 and_index++;
1470 /* cluster starts and ends will be separated because they should
1471 * hold their position
1473 if (tab->fe[i].ctrl & RQFCR_CLE)
1474 block_index++;
1475 /* A not set AND indicates the end of a depended block */
1476 if (!(tab->fe[i].ctrl & RQFCR_AND))
1477 block_index++;
1480 mask_table[and_index - 1].end = i - 1;
1482 return and_index;
1485 /* Sorts the entries of mask_table by the values of the masks.
1486 * Important: The 0xFF80 flags of the first and last entry of a
1487 * block must hold their position (which queue, CLusterEnable, ReJEct,
1488 * AND)
1490 static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
1491 struct filer_table *temp_table, u32 and_index)
1493 /* Pointer to compare function (_asc or _desc) */
1494 int (*gfar_comp)(const void *, const void *);
1496 u32 i, size = 0, start = 0, prev = 1;
1497 u32 old_first, old_last, new_first, new_last;
1499 gfar_comp = &gfar_comp_desc;
1501 for (i = 0; i < and_index; i++) {
1502 if (prev != mask_table[i].block) {
1503 old_first = mask_table[start].start + 1;
1504 old_last = mask_table[i - 1].end;
1505 sort(mask_table + start, size,
1506 sizeof(struct gfar_mask_entry),
1507 gfar_comp, &gfar_swap);
1509 /* Toggle order for every block. This makes the
1510 * thing more efficient!
1512 if (gfar_comp == gfar_comp_desc)
1513 gfar_comp = &gfar_comp_asc;
1514 else
1515 gfar_comp = &gfar_comp_desc;
1517 new_first = mask_table[start].start + 1;
1518 new_last = mask_table[i - 1].end;
1520 gfar_swap_bits(&temp_table->fe[new_first],
1521 &temp_table->fe[old_first],
1522 &temp_table->fe[new_last],
1523 &temp_table->fe[old_last],
1524 RQFCR_QUEUE | RQFCR_CLE |
1525 RQFCR_RJE | RQFCR_AND);
1527 start = i;
1528 size = 0;
1530 size++;
1531 prev = mask_table[i].block;
1535 /* Reduces the number of masks needed in the filer table to save entries
1536 * This is done by sorting the masks of a depended block. A depended block is
1537 * identified by gluing ANDs or CLE. The sorting order toggles after every
1538 * block. Of course entries in scope of a mask must change their location with
1539 * it.
1541 static int gfar_optimize_filer_masks(struct filer_table *tab)
1543 struct filer_table *temp_table;
1544 struct gfar_mask_entry *mask_table;
1546 u32 and_index = 0, previous_mask = 0, i = 0, j = 0, size = 0;
1547 s32 ret = 0;
1549 /* We need a copy of the filer table because
1550 * we want to change its order
1552 temp_table = kmemdup(tab, sizeof(*temp_table), GFP_KERNEL);
1553 if (temp_table == NULL)
1554 return -ENOMEM;
1556 mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
1557 sizeof(struct gfar_mask_entry), GFP_KERNEL);
1559 if (mask_table == NULL) {
1560 ret = -ENOMEM;
1561 goto end;
1564 and_index = gfar_generate_mask_table(mask_table, tab);
1566 gfar_sort_mask_table(mask_table, temp_table, and_index);
1568 /* Now we can copy the data from our duplicated filer table to
1569 * the real one in the order the mask table says
1571 for (i = 0; i < and_index; i++) {
1572 size = mask_table[i].end - mask_table[i].start + 1;
1573 gfar_copy_filer_entries(&(tab->fe[j]),
1574 &(temp_table->fe[mask_table[i].start]), size);
1575 j += size;
1578 /* And finally we just have to check for duplicated masks and drop the
1579 * second ones
1581 for (i = 0; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1582 if (tab->fe[i].ctrl == 0x80) {
1583 previous_mask = i++;
1584 break;
1587 for (; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1588 if (tab->fe[i].ctrl == 0x80) {
1589 if (tab->fe[i].prop == tab->fe[previous_mask].prop) {
1590 /* Two identical ones found!
1591 * So drop the second one!
1593 gfar_trim_filer_entries(i, i, tab);
1594 } else
1595 /* Not identical! */
1596 previous_mask = i;
1600 kfree(mask_table);
1601 end: kfree(temp_table);
1602 return ret;
1605 /* Write the bit-pattern from software's buffer to hardware registers */
1606 static int gfar_write_filer_table(struct gfar_private *priv,
1607 struct filer_table *tab)
1609 u32 i = 0;
1610 if (tab->index > MAX_FILER_IDX - 1)
1611 return -EBUSY;
1613 /* Avoid inconsistent filer table to be processed */
1614 lock_rx_qs(priv);
1616 /* Fill regular entries */
1617 for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].ctrl);
1618 i++)
1619 gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
1620 /* Fill the rest with fall-troughs */
1621 for (; i < MAX_FILER_IDX - 1; i++)
1622 gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
1623 /* Last entry must be default accept
1624 * because that's what people expect
1626 gfar_write_filer(priv, i, 0x20, 0x0);
1628 unlock_rx_qs(priv);
1630 return 0;
1633 static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
1634 struct gfar_private *priv)
1637 if (flow->flow_type & FLOW_EXT) {
1638 if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
1639 netdev_warn(priv->ndev,
1640 "User-specific data not supported!\n");
1641 if (~flow->m_ext.vlan_etype)
1642 netdev_warn(priv->ndev,
1643 "VLAN-etype not supported!\n");
1645 if (flow->flow_type == IP_USER_FLOW)
1646 if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
1647 netdev_warn(priv->ndev,
1648 "IP-Version differing from IPv4 not supported!\n");
1650 return 0;
1653 static int gfar_process_filer_changes(struct gfar_private *priv)
1655 struct ethtool_flow_spec_container *j;
1656 struct filer_table *tab;
1657 s32 i = 0;
1658 s32 ret = 0;
1660 /* So index is set to zero, too! */
1661 tab = kzalloc(sizeof(*tab), GFP_KERNEL);
1662 if (tab == NULL)
1663 return -ENOMEM;
1665 /* Now convert the existing filer data from flow_spec into
1666 * filer tables binary format
1668 list_for_each_entry(j, &priv->rx_list.list, list) {
1669 ret = gfar_convert_to_filer(&j->fs, tab);
1670 if (ret == -EBUSY) {
1671 netdev_err(priv->ndev,
1672 "Rule not added: No free space!\n");
1673 goto end;
1675 if (ret == -1) {
1676 netdev_err(priv->ndev,
1677 "Rule not added: Unsupported Flow-type!\n");
1678 goto end;
1682 i = tab->index;
1684 /* Optimizations to save entries */
1685 gfar_cluster_filer(tab);
1686 gfar_optimize_filer_masks(tab);
1688 pr_debug("\tSummary:\n"
1689 "\tData on hardware: %d\n"
1690 "\tCompression rate: %d%%\n",
1691 tab->index, 100 - (100 * tab->index) / i);
1693 /* Write everything to hardware */
1694 ret = gfar_write_filer_table(priv, tab);
1695 if (ret == -EBUSY) {
1696 netdev_err(priv->ndev, "Rule not added: No free space!\n");
1697 goto end;
1700 end:
1701 kfree(tab);
1702 return ret;
1705 static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
1707 u32 i = 0;
1709 for (i = 0; i < sizeof(flow->m_u); i++)
1710 flow->m_u.hdata[i] ^= 0xFF;
1712 flow->m_ext.vlan_etype ^= cpu_to_be16(0xFFFF);
1713 flow->m_ext.vlan_tci ^= cpu_to_be16(0xFFFF);
1714 flow->m_ext.data[0] ^= cpu_to_be32(~0);
1715 flow->m_ext.data[1] ^= cpu_to_be32(~0);
1718 static int gfar_add_cls(struct gfar_private *priv,
1719 struct ethtool_rx_flow_spec *flow)
1721 struct ethtool_flow_spec_container *temp, *comp;
1722 int ret = 0;
1724 temp = kmalloc(sizeof(*temp), GFP_KERNEL);
1725 if (temp == NULL)
1726 return -ENOMEM;
1727 memcpy(&temp->fs, flow, sizeof(temp->fs));
1729 gfar_invert_masks(&temp->fs);
1730 ret = gfar_check_capability(&temp->fs, priv);
1731 if (ret)
1732 goto clean_mem;
1733 /* Link in the new element at the right @location */
1734 if (list_empty(&priv->rx_list.list)) {
1735 ret = gfar_check_filer_hardware(priv);
1736 if (ret != 0)
1737 goto clean_mem;
1738 list_add(&temp->list, &priv->rx_list.list);
1739 goto process;
1740 } else {
1741 list_for_each_entry(comp, &priv->rx_list.list, list) {
1742 if (comp->fs.location > flow->location) {
1743 list_add_tail(&temp->list, &comp->list);
1744 goto process;
1746 if (comp->fs.location == flow->location) {
1747 netdev_err(priv->ndev,
1748 "Rule not added: ID %d not free!\n",
1749 flow->location);
1750 ret = -EBUSY;
1751 goto clean_mem;
1754 list_add_tail(&temp->list, &priv->rx_list.list);
1757 process:
1758 ret = gfar_process_filer_changes(priv);
1759 if (ret)
1760 goto clean_list;
1761 priv->rx_list.count++;
1762 return ret;
1764 clean_list:
1765 list_del(&temp->list);
1766 clean_mem:
1767 kfree(temp);
1768 return ret;
1771 static int gfar_del_cls(struct gfar_private *priv, u32 loc)
1773 struct ethtool_flow_spec_container *comp;
1774 u32 ret = -EINVAL;
1776 if (list_empty(&priv->rx_list.list))
1777 return ret;
1779 list_for_each_entry(comp, &priv->rx_list.list, list) {
1780 if (comp->fs.location == loc) {
1781 list_del(&comp->list);
1782 kfree(comp);
1783 priv->rx_list.count--;
1784 gfar_process_filer_changes(priv);
1785 ret = 0;
1786 break;
1790 return ret;
1793 static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
1795 struct ethtool_flow_spec_container *comp;
1796 u32 ret = -EINVAL;
1798 list_for_each_entry(comp, &priv->rx_list.list, list) {
1799 if (comp->fs.location == cmd->fs.location) {
1800 memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
1801 gfar_invert_masks(&cmd->fs);
1802 ret = 0;
1803 break;
1807 return ret;
1810 static int gfar_get_cls_all(struct gfar_private *priv,
1811 struct ethtool_rxnfc *cmd, u32 *rule_locs)
1813 struct ethtool_flow_spec_container *comp;
1814 u32 i = 0;
1816 list_for_each_entry(comp, &priv->rx_list.list, list) {
1817 if (i == cmd->rule_cnt)
1818 return -EMSGSIZE;
1819 rule_locs[i] = comp->fs.location;
1820 i++;
1823 cmd->data = MAX_FILER_IDX;
1824 cmd->rule_cnt = i;
1826 return 0;
1829 static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1831 struct gfar_private *priv = netdev_priv(dev);
1832 int ret = 0;
1834 mutex_lock(&priv->rx_queue_access);
1836 switch (cmd->cmd) {
1837 case ETHTOOL_SRXFH:
1838 ret = gfar_set_hash_opts(priv, cmd);
1839 break;
1840 case ETHTOOL_SRXCLSRLINS:
1841 if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
1842 cmd->fs.ring_cookie >= priv->num_rx_queues) ||
1843 cmd->fs.location >= MAX_FILER_IDX) {
1844 ret = -EINVAL;
1845 break;
1847 ret = gfar_add_cls(priv, &cmd->fs);
1848 break;
1849 case ETHTOOL_SRXCLSRLDEL:
1850 ret = gfar_del_cls(priv, cmd->fs.location);
1851 break;
1852 default:
1853 ret = -EINVAL;
1856 mutex_unlock(&priv->rx_queue_access);
1858 return ret;
1861 static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1862 u32 *rule_locs)
1864 struct gfar_private *priv = netdev_priv(dev);
1865 int ret = 0;
1867 switch (cmd->cmd) {
1868 case ETHTOOL_GRXRINGS:
1869 cmd->data = priv->num_rx_queues;
1870 break;
1871 case ETHTOOL_GRXCLSRLCNT:
1872 cmd->rule_cnt = priv->rx_list.count;
1873 break;
1874 case ETHTOOL_GRXCLSRULE:
1875 ret = gfar_get_cls(priv, cmd);
1876 break;
1877 case ETHTOOL_GRXCLSRLALL:
1878 ret = gfar_get_cls_all(priv, cmd, rule_locs);
1879 break;
1880 default:
1881 ret = -EINVAL;
1882 break;
1885 return ret;
1888 int gfar_phc_index = -1;
1889 EXPORT_SYMBOL(gfar_phc_index);
1891 static int gfar_get_ts_info(struct net_device *dev,
1892 struct ethtool_ts_info *info)
1894 struct gfar_private *priv = netdev_priv(dev);
1896 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
1897 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
1898 SOF_TIMESTAMPING_SOFTWARE;
1899 info->phc_index = -1;
1900 return 0;
1902 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
1903 SOF_TIMESTAMPING_RX_HARDWARE |
1904 SOF_TIMESTAMPING_RAW_HARDWARE;
1905 info->phc_index = gfar_phc_index;
1906 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1907 (1 << HWTSTAMP_TX_ON);
1908 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1909 (1 << HWTSTAMP_FILTER_ALL);
1910 return 0;
1913 const struct ethtool_ops gfar_ethtool_ops = {
1914 .get_settings = gfar_gsettings,
1915 .set_settings = gfar_ssettings,
1916 .get_drvinfo = gfar_gdrvinfo,
1917 .get_regs_len = gfar_reglen,
1918 .get_regs = gfar_get_regs,
1919 .get_link = ethtool_op_get_link,
1920 .get_coalesce = gfar_gcoalesce,
1921 .set_coalesce = gfar_scoalesce,
1922 .get_ringparam = gfar_gringparam,
1923 .set_ringparam = gfar_sringparam,
1924 .get_pauseparam = gfar_gpauseparam,
1925 .set_pauseparam = gfar_spauseparam,
1926 .get_strings = gfar_gstrings,
1927 .get_sset_count = gfar_sset_count,
1928 .get_ethtool_stats = gfar_fill_stats,
1929 .get_msglevel = gfar_get_msglevel,
1930 .set_msglevel = gfar_set_msglevel,
1931 #ifdef CONFIG_PM
1932 .get_wol = gfar_get_wol,
1933 .set_wol = gfar_set_wol,
1934 #endif
1935 .set_rxnfc = gfar_set_nfc,
1936 .get_rxnfc = gfar_get_nfc,
1937 .get_ts_info = gfar_get_ts_info,