2 * drivers/net/ethernet/freescale/gianfar_ethtool.c
4 * Gianfar Ethernet Driver
5 * Ethtool support for Gianfar Enet
6 * Based on e1000 ethtool support
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
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>
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>
47 #define GFAR_MAX_COAL_USECS 0xffff
48 #define GFAR_MAX_COAL_FRAMES 0xff
49 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
51 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
52 static int gfar_gcoalesce(struct net_device *dev,
53 struct ethtool_coalesce *cvals);
54 static int gfar_scoalesce(struct net_device *dev,
55 struct ethtool_coalesce *cvals);
56 static void gfar_gringparam(struct net_device *dev,
57 struct ethtool_ringparam *rvals);
58 static int gfar_sringparam(struct net_device *dev,
59 struct ethtool_ringparam *rvals);
60 static void gfar_gdrvinfo(struct net_device *dev,
61 struct ethtool_drvinfo *drvinfo);
63 static const char stat_gstrings[][ETH_GSTRING_LEN] = {
65 "rx-allocation-errors",
66 "rx-large-frame-errors",
67 "rx-short-frame-errors",
68 "rx-non-octet-errors",
73 "rx-truncated-frames",
77 "rx-skb-missing-errors",
81 "tx-rx-65-127-frames",
82 "tx-rx-128-255-frames",
83 "tx-rx-256-511-frames",
84 "tx-rx-512-1023-frames",
85 "tx-rx-1024-1518-frames",
86 "tx-rx-1519-1522-good-vlan",
90 "receive-multicast-packet",
91 "receive-broadcast-packet",
92 "rx-control-frame-packets",
93 "rx-pause-frame-packets",
96 "rx-frame-length-error",
98 "rx-carrier-sense-error",
99 "rx-undersize-packets",
100 "rx-oversize-packets",
101 "rx-fragmented-frames",
106 "tx-multicast-packets",
107 "tx-broadcast-packets",
108 "tx-pause-control-frames",
109 "tx-deferral-packets",
110 "tx-excessive-deferral-packets",
111 "tx-single-collision-packets",
112 "tx-multiple-collision-packets",
113 "tx-late-collision-packets",
114 "tx-excessive-collision-packets",
115 "tx-total-collision",
121 "tx-oversize-frames",
122 "tx-undersize-frames",
123 "tx-fragmented-frames",
126 /* Fill in a buffer with the strings which correspond to the
128 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
130 struct gfar_private *priv = netdev_priv(dev);
132 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
133 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
135 memcpy(buf, stat_gstrings,
136 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
139 /* Fill in an array of 64-bit statistics from various sources.
140 * This array will be appended to the end of the ethtool_stats
141 * structure, and returned to user space
143 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
147 struct gfar_private *priv = netdev_priv(dev);
148 struct gfar __iomem *regs = priv->gfargrp[0].regs;
149 atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
151 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
152 buf[i] = atomic64_read(&extra[i]);
154 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
155 u32 __iomem *rmon = (u32 __iomem *) ®s->rmon;
157 for (; i < GFAR_STATS_LEN; i++, rmon++)
158 buf[i] = (u64) gfar_read(rmon);
162 static int gfar_sset_count(struct net_device *dev, int sset)
164 struct gfar_private *priv = netdev_priv(dev);
168 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
169 return GFAR_STATS_LEN;
171 return GFAR_EXTRA_STATS_LEN;
177 /* Fills in the drvinfo structure with some basic info */
178 static void gfar_gdrvinfo(struct net_device *dev,
179 struct ethtool_drvinfo *drvinfo)
181 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
182 strlcpy(drvinfo->version, gfar_driver_version,
183 sizeof(drvinfo->version));
184 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
185 strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
186 drvinfo->regdump_len = 0;
187 drvinfo->eedump_len = 0;
191 static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
193 struct gfar_private *priv = netdev_priv(dev);
194 struct phy_device *phydev = priv->phydev;
199 return phy_ethtool_sset(phydev, cmd);
203 /* Return the current settings in the ethtool_cmd structure */
204 static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
206 struct gfar_private *priv = netdev_priv(dev);
207 struct phy_device *phydev = priv->phydev;
208 struct gfar_priv_rx_q *rx_queue = NULL;
209 struct gfar_priv_tx_q *tx_queue = NULL;
213 tx_queue = priv->tx_queue[0];
214 rx_queue = priv->rx_queue[0];
216 /* etsec-1.7 and older versions have only one txic
217 * and rxic regs although they support multiple queues */
218 cmd->maxtxpkt = get_icft_value(tx_queue->txic);
219 cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
221 return phy_ethtool_gset(phydev, cmd);
224 /* Return the length of the register structure */
225 static int gfar_reglen(struct net_device *dev)
227 return sizeof (struct gfar);
230 /* Return a dump of the GFAR register space */
231 static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
235 struct gfar_private *priv = netdev_priv(dev);
236 u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
237 u32 *buf = (u32 *) regbuf;
239 for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
240 buf[i] = gfar_read(&theregs[i]);
243 /* Convert microseconds to ethernet clock ticks, which changes
244 * depending on what speed the controller is running at */
245 static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
250 /* The timer is different, depending on the interface speed */
251 switch (priv->phydev->speed) {
253 count = GFAR_GBIT_TIME;
256 count = GFAR_100_TIME;
260 count = GFAR_10_TIME;
264 /* Make sure we return a number greater than 0
266 return (usecs * 1000 + count - 1) / count;
269 /* Convert ethernet clock ticks to microseconds */
270 static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
275 /* The timer is different, depending on the interface speed */
276 switch (priv->phydev->speed) {
278 count = GFAR_GBIT_TIME;
281 count = GFAR_100_TIME;
285 count = GFAR_10_TIME;
289 /* Make sure we return a number greater than 0 */
290 /* if ticks is > 0 */
291 return (ticks * count) / 1000;
294 /* Get the coalescing parameters, and put them in the cvals
296 static int gfar_gcoalesce(struct net_device *dev,
297 struct ethtool_coalesce *cvals)
299 struct gfar_private *priv = netdev_priv(dev);
300 struct gfar_priv_rx_q *rx_queue = NULL;
301 struct gfar_priv_tx_q *tx_queue = NULL;
302 unsigned long rxtime;
303 unsigned long rxcount;
304 unsigned long txtime;
305 unsigned long txcount;
307 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
310 if (NULL == priv->phydev)
313 rx_queue = priv->rx_queue[0];
314 tx_queue = priv->tx_queue[0];
316 rxtime = get_ictt_value(rx_queue->rxic);
317 rxcount = get_icft_value(rx_queue->rxic);
318 txtime = get_ictt_value(tx_queue->txic);
319 txcount = get_icft_value(tx_queue->txic);
320 cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
321 cvals->rx_max_coalesced_frames = rxcount;
323 cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
324 cvals->tx_max_coalesced_frames = txcount;
326 cvals->use_adaptive_rx_coalesce = 0;
327 cvals->use_adaptive_tx_coalesce = 0;
329 cvals->pkt_rate_low = 0;
330 cvals->rx_coalesce_usecs_low = 0;
331 cvals->rx_max_coalesced_frames_low = 0;
332 cvals->tx_coalesce_usecs_low = 0;
333 cvals->tx_max_coalesced_frames_low = 0;
335 /* When the packet rate is below pkt_rate_high but above
336 * pkt_rate_low (both measured in packets per second) the
337 * normal {rx,tx}_* coalescing parameters are used.
340 /* When the packet rate is (measured in packets per second)
341 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
344 cvals->pkt_rate_high = 0;
345 cvals->rx_coalesce_usecs_high = 0;
346 cvals->rx_max_coalesced_frames_high = 0;
347 cvals->tx_coalesce_usecs_high = 0;
348 cvals->tx_max_coalesced_frames_high = 0;
350 /* How often to do adaptive coalescing packet rate sampling,
351 * measured in seconds. Must not be zero.
353 cvals->rate_sample_interval = 0;
358 /* Change the coalescing values.
359 * Both cvals->*_usecs and cvals->*_frames have to be > 0
360 * in order for coalescing to be active
362 static int gfar_scoalesce(struct net_device *dev,
363 struct ethtool_coalesce *cvals)
365 struct gfar_private *priv = netdev_priv(dev);
368 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
371 if (NULL == priv->phydev)
374 /* Check the bounds of the values */
375 if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
376 netdev_info(dev, "Coalescing is limited to %d microseconds\n",
377 GFAR_MAX_COAL_USECS);
381 if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
382 netdev_info(dev, "Coalescing is limited to %d frames\n",
383 GFAR_MAX_COAL_FRAMES);
387 /* Check the bounds of the values */
388 if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
389 netdev_info(dev, "Coalescing is limited to %d microseconds\n",
390 GFAR_MAX_COAL_USECS);
394 if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
395 netdev_info(dev, "Coalescing is limited to %d frames\n",
396 GFAR_MAX_COAL_FRAMES);
400 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
403 /* Set up rx coalescing */
404 if ((cvals->rx_coalesce_usecs == 0) ||
405 (cvals->rx_max_coalesced_frames == 0)) {
406 for (i = 0; i < priv->num_rx_queues; i++)
407 priv->rx_queue[i]->rxcoalescing = 0;
409 for (i = 0; i < priv->num_rx_queues; i++)
410 priv->rx_queue[i]->rxcoalescing = 1;
413 for (i = 0; i < priv->num_rx_queues; i++) {
414 priv->rx_queue[i]->rxic = mk_ic_value(
415 cvals->rx_max_coalesced_frames,
416 gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
419 /* Set up tx coalescing */
420 if ((cvals->tx_coalesce_usecs == 0) ||
421 (cvals->tx_max_coalesced_frames == 0)) {
422 for (i = 0; i < priv->num_tx_queues; i++)
423 priv->tx_queue[i]->txcoalescing = 0;
425 for (i = 0; i < priv->num_tx_queues; i++)
426 priv->tx_queue[i]->txcoalescing = 1;
429 for (i = 0; i < priv->num_tx_queues; i++) {
430 priv->tx_queue[i]->txic = mk_ic_value(
431 cvals->tx_max_coalesced_frames,
432 gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
435 if (dev->flags & IFF_UP) {
437 err = startup_gfar(dev);
439 gfar_mac_reset(priv);
442 clear_bit_unlock(GFAR_RESETTING, &priv->state);
447 /* Fills in rvals with the current ring parameters. Currently,
448 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
449 * jumbo are ignored by the driver */
450 static void gfar_gringparam(struct net_device *dev,
451 struct ethtool_ringparam *rvals)
453 struct gfar_private *priv = netdev_priv(dev);
454 struct gfar_priv_tx_q *tx_queue = NULL;
455 struct gfar_priv_rx_q *rx_queue = NULL;
457 tx_queue = priv->tx_queue[0];
458 rx_queue = priv->rx_queue[0];
460 rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
461 rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
462 rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
463 rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
465 /* Values changeable by the user. The valid values are
466 * in the range 1 to the "*_max_pending" counterpart above.
468 rvals->rx_pending = rx_queue->rx_ring_size;
469 rvals->rx_mini_pending = rx_queue->rx_ring_size;
470 rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
471 rvals->tx_pending = tx_queue->tx_ring_size;
474 /* Change the current ring parameters, stopping the controller if
475 * necessary so that we don't mess things up while we're in motion.
477 static int gfar_sringparam(struct net_device *dev,
478 struct ethtool_ringparam *rvals)
480 struct gfar_private *priv = netdev_priv(dev);
483 if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
486 if (!is_power_of_2(rvals->rx_pending)) {
487 netdev_err(dev, "Ring sizes must be a power of 2\n");
491 if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
494 if (!is_power_of_2(rvals->tx_pending)) {
495 netdev_err(dev, "Ring sizes must be a power of 2\n");
499 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
502 if (dev->flags & IFF_UP)
505 /* Change the sizes */
506 for (i = 0; i < priv->num_rx_queues; i++)
507 priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
509 for (i = 0; i < priv->num_tx_queues; i++)
510 priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
512 /* Rebuild the rings with the new size */
513 if (dev->flags & IFF_UP)
514 err = startup_gfar(dev);
516 clear_bit_unlock(GFAR_RESETTING, &priv->state);
521 static void gfar_gpauseparam(struct net_device *dev,
522 struct ethtool_pauseparam *epause)
524 struct gfar_private *priv = netdev_priv(dev);
526 epause->autoneg = !!priv->pause_aneg_en;
527 epause->rx_pause = !!priv->rx_pause_en;
528 epause->tx_pause = !!priv->tx_pause_en;
531 static int gfar_spauseparam(struct net_device *dev,
532 struct ethtool_pauseparam *epause)
534 struct gfar_private *priv = netdev_priv(dev);
535 struct phy_device *phydev = priv->phydev;
536 struct gfar __iomem *regs = priv->gfargrp[0].regs;
542 if (!(phydev->supported & SUPPORTED_Pause) ||
543 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
544 (epause->rx_pause != epause->tx_pause)))
547 priv->rx_pause_en = priv->tx_pause_en = 0;
548 if (epause->rx_pause) {
549 priv->rx_pause_en = 1;
551 if (epause->tx_pause) {
552 priv->tx_pause_en = 1;
553 /* FLOW_CTRL_RX & TX */
554 newadv = ADVERTISED_Pause;
555 } else /* FLOW_CTLR_RX */
556 newadv = ADVERTISED_Pause | ADVERTISED_Asym_Pause;
557 } else if (epause->tx_pause) {
558 priv->tx_pause_en = 1;
560 newadv = ADVERTISED_Asym_Pause;
565 priv->pause_aneg_en = 1;
567 priv->pause_aneg_en = 0;
569 oldadv = phydev->advertising &
570 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
571 if (oldadv != newadv) {
572 phydev->advertising &=
573 ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
574 phydev->advertising |= newadv;
576 /* inform link partner of our
577 * new flow ctrl settings
579 return phy_start_aneg(phydev);
581 if (!epause->autoneg) {
583 tempval = gfar_read(®s->maccfg1);
584 tempval &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
586 priv->tx_actual_en = 0;
587 if (priv->tx_pause_en) {
588 priv->tx_actual_en = 1;
589 tempval |= MACCFG1_TX_FLOW;
592 if (priv->rx_pause_en)
593 tempval |= MACCFG1_RX_FLOW;
594 gfar_write(®s->maccfg1, tempval);
601 int gfar_set_features(struct net_device *dev, netdev_features_t features)
603 netdev_features_t changed = dev->features ^ features;
604 struct gfar_private *priv = netdev_priv(dev);
607 if (!(changed & (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
611 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
614 dev->features = features;
616 if (dev->flags & IFF_UP) {
617 /* Now we take down the rings to rebuild them */
619 err = startup_gfar(dev);
621 gfar_mac_reset(priv);
624 clear_bit_unlock(GFAR_RESETTING, &priv->state);
629 static uint32_t gfar_get_msglevel(struct net_device *dev)
631 struct gfar_private *priv = netdev_priv(dev);
633 return priv->msg_enable;
636 static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
638 struct gfar_private *priv = netdev_priv(dev);
640 priv->msg_enable = data;
644 static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
646 struct gfar_private *priv = netdev_priv(dev);
648 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
649 wol->supported = WAKE_MAGIC;
650 wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
652 wol->supported = wol->wolopts = 0;
656 static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
658 struct gfar_private *priv = netdev_priv(dev);
661 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
665 if (wol->wolopts & ~WAKE_MAGIC)
668 device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);
670 spin_lock_irqsave(&priv->bflock, flags);
671 priv->wol_en = !!device_may_wakeup(&dev->dev);
672 spin_unlock_irqrestore(&priv->bflock, flags);
678 static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
680 u32 fcr = 0x0, fpr = FPR_FILER_MASK;
682 if (ethflow & RXH_L2DA) {
683 fcr = RQFCR_PID_DAH |RQFCR_CMP_NOMATCH |
684 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
685 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
686 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
687 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
688 priv->cur_filer_idx = priv->cur_filer_idx - 1;
690 fcr = RQFCR_PID_DAL | RQFCR_AND | RQFCR_CMP_NOMATCH |
691 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
692 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
693 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
694 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
695 priv->cur_filer_idx = priv->cur_filer_idx - 1;
698 if (ethflow & RXH_VLAN) {
699 fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
700 RQFCR_AND | RQFCR_HASHTBL_0;
701 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
702 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
703 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
704 priv->cur_filer_idx = priv->cur_filer_idx - 1;
707 if (ethflow & RXH_IP_SRC) {
708 fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
709 RQFCR_AND | RQFCR_HASHTBL_0;
710 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
711 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
712 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
713 priv->cur_filer_idx = priv->cur_filer_idx - 1;
716 if (ethflow & (RXH_IP_DST)) {
717 fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
718 RQFCR_AND | RQFCR_HASHTBL_0;
719 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
720 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
721 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
722 priv->cur_filer_idx = priv->cur_filer_idx - 1;
725 if (ethflow & RXH_L3_PROTO) {
726 fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
727 RQFCR_AND | RQFCR_HASHTBL_0;
728 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
729 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
730 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
731 priv->cur_filer_idx = priv->cur_filer_idx - 1;
734 if (ethflow & RXH_L4_B_0_1) {
735 fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
736 RQFCR_AND | RQFCR_HASHTBL_0;
737 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
738 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
739 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
740 priv->cur_filer_idx = priv->cur_filer_idx - 1;
743 if (ethflow & RXH_L4_B_2_3) {
744 fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
745 RQFCR_AND | RQFCR_HASHTBL_0;
746 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
747 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
748 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
749 priv->cur_filer_idx = priv->cur_filer_idx - 1;
753 static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
756 unsigned int last_rule_idx = priv->cur_filer_idx;
757 unsigned int cmp_rqfpr;
758 unsigned int *local_rqfpr;
759 unsigned int *local_rqfcr;
760 int i = 0x0, k = 0x0;
761 int j = MAX_FILER_IDX, l = 0x0;
764 local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
766 local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
768 if (!local_rqfpr || !local_rqfcr) {
775 cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
778 cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
781 cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
784 cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
787 netdev_err(priv->ndev,
788 "Right now this class is not supported\n");
793 for (i = 0; i < MAX_FILER_IDX + 1; i++) {
794 local_rqfpr[j] = priv->ftp_rqfpr[i];
795 local_rqfcr[j] = priv->ftp_rqfcr[i];
797 if ((priv->ftp_rqfcr[i] ==
798 (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
799 (priv->ftp_rqfpr[i] == cmp_rqfpr))
803 if (i == MAX_FILER_IDX + 1) {
804 netdev_err(priv->ndev,
805 "No parse rule found, can't create hash rules\n");
810 /* If a match was found, then it begins the starting of a cluster rule
811 * if it was already programmed, we need to overwrite these rules
813 for (l = i+1; l < MAX_FILER_IDX; l++) {
814 if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
815 !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
816 priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
817 RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
818 priv->ftp_rqfpr[l] = FPR_FILER_MASK;
819 gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
824 if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
825 (priv->ftp_rqfcr[l] & RQFCR_AND))
828 local_rqfpr[j] = priv->ftp_rqfpr[l];
829 local_rqfcr[j] = priv->ftp_rqfcr[l];
834 priv->cur_filer_idx = l - 1;
838 ethflow_to_filer_rules(priv, ethflow);
840 /* Write back the popped out rules again */
841 for (k = j+1; k < MAX_FILER_IDX; k++) {
842 priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
843 priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
844 gfar_write_filer(priv, priv->cur_filer_idx,
845 local_rqfcr[k], local_rqfpr[k]);
846 if (!priv->cur_filer_idx)
848 priv->cur_filer_idx = priv->cur_filer_idx - 1;
857 static int gfar_set_hash_opts(struct gfar_private *priv,
858 struct ethtool_rxnfc *cmd)
860 /* write the filer rules here */
861 if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
867 static int gfar_check_filer_hardware(struct gfar_private *priv)
869 struct gfar __iomem *regs = priv->gfargrp[0].regs;
872 /* Check if we are in FIFO mode */
873 i = gfar_read(®s->ecntrl);
875 if (i == ECNTRL_FIFM) {
876 netdev_notice(priv->ndev, "Interface in FIFO mode\n");
877 i = gfar_read(®s->rctrl);
878 i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
879 if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
880 netdev_info(priv->ndev,
881 "Receive Queue Filtering enabled\n");
883 netdev_warn(priv->ndev,
884 "Receive Queue Filtering disabled\n");
888 /* Or in standard mode */
890 i = gfar_read(®s->rctrl);
891 i &= RCTRL_PRSDEP_MASK;
892 if (i == RCTRL_PRSDEP_MASK) {
893 netdev_info(priv->ndev,
894 "Receive Queue Filtering enabled\n");
896 netdev_warn(priv->ndev,
897 "Receive Queue Filtering disabled\n");
902 /* Sets the properties for arbitrary filer rule
903 * to the first 4 Layer 4 Bytes
905 gfar_write(®s->rbifx, 0xC0C1C2C3);
909 static int gfar_comp_asc(const void *a, const void *b)
911 return memcmp(a, b, 4);
914 static int gfar_comp_desc(const void *a, const void *b)
916 return -memcmp(a, b, 4);
919 static void gfar_swap(void *a, void *b, int size)
930 /* Write a mask to filer cache */
931 static void gfar_set_mask(u32 mask, struct filer_table *tab)
933 tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
934 tab->fe[tab->index].prop = mask;
938 /* Sets parse bits (e.g. IP or TCP) */
939 static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
941 gfar_set_mask(mask, tab);
942 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
944 tab->fe[tab->index].prop = value;
948 static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
949 struct filer_table *tab)
951 gfar_set_mask(mask, tab);
952 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
953 tab->fe[tab->index].prop = value;
957 /* For setting a tuple of value and mask of type flag
959 * IP-Src = 10.0.0.0/255.0.0.0
960 * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
962 * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
963 * For a don't care mask it gives us a 0
965 * The check if don't care and the mask adjustment if mask=0 is done for VLAN
966 * and MAC stuff on an upper level (due to missing information on this level).
967 * For these guys we can discard them if they are value=0 and mask=0.
969 * Further the all masks are one-padded for better hardware efficiency.
971 static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
972 struct filer_table *tab)
979 mask |= RQFCR_PID_PRI_MASK;
984 if (!~(mask | RQFCR_PID_L4P_MASK))
989 mask |= RQFCR_PID_L4P_MASK;
995 mask |= RQFCR_PID_VID_MASK;
1001 if (!~(mask | RQFCR_PID_PORT_MASK))
1006 mask |= RQFCR_PID_PORT_MASK;
1013 if (!(value | mask))
1015 mask |= RQFCR_PID_MAC_MASK;
1017 /* for all real 32bit masks */
1025 gfar_set_general_attribute(value, mask, flag, tab);
1028 /* Translates value and mask for UDP, TCP or SCTP */
1029 static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
1030 struct ethtool_tcpip4_spec *mask,
1031 struct filer_table *tab)
1033 gfar_set_attribute(be32_to_cpu(value->ip4src),
1034 be32_to_cpu(mask->ip4src),
1035 RQFCR_PID_SIA, tab);
1036 gfar_set_attribute(be32_to_cpu(value->ip4dst),
1037 be32_to_cpu(mask->ip4dst),
1038 RQFCR_PID_DIA, tab);
1039 gfar_set_attribute(be16_to_cpu(value->pdst),
1040 be16_to_cpu(mask->pdst),
1041 RQFCR_PID_DPT, tab);
1042 gfar_set_attribute(be16_to_cpu(value->psrc),
1043 be16_to_cpu(mask->psrc),
1044 RQFCR_PID_SPT, tab);
1045 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1048 /* Translates value and mask for RAW-IP4 */
1049 static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
1050 struct ethtool_usrip4_spec *mask,
1051 struct filer_table *tab)
1053 gfar_set_attribute(be32_to_cpu(value->ip4src),
1054 be32_to_cpu(mask->ip4src),
1055 RQFCR_PID_SIA, tab);
1056 gfar_set_attribute(be32_to_cpu(value->ip4dst),
1057 be32_to_cpu(mask->ip4dst),
1058 RQFCR_PID_DIA, tab);
1059 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1060 gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
1061 gfar_set_attribute(be32_to_cpu(value->l4_4_bytes),
1062 be32_to_cpu(mask->l4_4_bytes),
1063 RQFCR_PID_ARB, tab);
1067 /* Translates value and mask for ETHER spec */
1068 static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
1069 struct filer_table *tab)
1071 u32 upper_temp_mask = 0;
1072 u32 lower_temp_mask = 0;
1074 /* Source address */
1075 if (!is_broadcast_ether_addr(mask->h_source)) {
1076 if (is_zero_ether_addr(mask->h_source)) {
1077 upper_temp_mask = 0xFFFFFFFF;
1078 lower_temp_mask = 0xFFFFFFFF;
1080 upper_temp_mask = mask->h_source[0] << 16 |
1081 mask->h_source[1] << 8 |
1083 lower_temp_mask = mask->h_source[3] << 16 |
1084 mask->h_source[4] << 8 |
1088 gfar_set_attribute(value->h_source[0] << 16 |
1089 value->h_source[1] << 8 |
1091 upper_temp_mask, RQFCR_PID_SAH, tab);
1092 /* And the same for the lower part */
1093 gfar_set_attribute(value->h_source[3] << 16 |
1094 value->h_source[4] << 8 |
1096 lower_temp_mask, RQFCR_PID_SAL, tab);
1098 /* Destination address */
1099 if (!is_broadcast_ether_addr(mask->h_dest)) {
1100 /* Special for destination is limited broadcast */
1101 if ((is_broadcast_ether_addr(value->h_dest) &&
1102 is_zero_ether_addr(mask->h_dest))) {
1103 gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
1105 if (is_zero_ether_addr(mask->h_dest)) {
1106 upper_temp_mask = 0xFFFFFFFF;
1107 lower_temp_mask = 0xFFFFFFFF;
1109 upper_temp_mask = mask->h_dest[0] << 16 |
1110 mask->h_dest[1] << 8 |
1112 lower_temp_mask = mask->h_dest[3] << 16 |
1113 mask->h_dest[4] << 8 |
1118 gfar_set_attribute(value->h_dest[0] << 16 |
1119 value->h_dest[1] << 8 |
1121 upper_temp_mask, RQFCR_PID_DAH, tab);
1122 /* And the same for the lower part */
1123 gfar_set_attribute(value->h_dest[3] << 16 |
1124 value->h_dest[4] << 8 |
1126 lower_temp_mask, RQFCR_PID_DAL, tab);
1130 gfar_set_attribute(be16_to_cpu(value->h_proto),
1131 be16_to_cpu(mask->h_proto),
1132 RQFCR_PID_ETY, tab);
1135 static inline u32 vlan_tci_vid(struct ethtool_rx_flow_spec *rule)
1137 return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_VID_MASK;
1140 static inline u32 vlan_tci_vidm(struct ethtool_rx_flow_spec *rule)
1142 return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_VID_MASK;
1145 static inline u32 vlan_tci_cfi(struct ethtool_rx_flow_spec *rule)
1147 return be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_CFI_MASK;
1150 static inline u32 vlan_tci_cfim(struct ethtool_rx_flow_spec *rule)
1152 return be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_CFI_MASK;
1155 static inline u32 vlan_tci_prio(struct ethtool_rx_flow_spec *rule)
1157 return (be16_to_cpu(rule->h_ext.vlan_tci) & VLAN_PRIO_MASK) >>
1161 static inline u32 vlan_tci_priom(struct ethtool_rx_flow_spec *rule)
1163 return (be16_to_cpu(rule->m_ext.vlan_tci) & VLAN_PRIO_MASK) >>
1167 /* Convert a rule to binary filter format of gianfar */
1168 static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
1169 struct filer_table *tab)
1171 u32 vlan = 0, vlan_mask = 0;
1172 u32 id = 0, id_mask = 0;
1173 u32 cfi = 0, cfi_mask = 0;
1174 u32 prio = 0, prio_mask = 0;
1175 u32 old_index = tab->index;
1177 /* Check if vlan is wanted */
1178 if ((rule->flow_type & FLOW_EXT) &&
1179 (rule->m_ext.vlan_tci != cpu_to_be16(0xFFFF))) {
1180 if (!rule->m_ext.vlan_tci)
1181 rule->m_ext.vlan_tci = cpu_to_be16(0xFFFF);
1184 vlan_mask = RQFPR_VLN;
1186 /* Separate the fields */
1187 id = vlan_tci_vid(rule);
1188 id_mask = vlan_tci_vidm(rule);
1189 cfi = vlan_tci_cfi(rule);
1190 cfi_mask = vlan_tci_cfim(rule);
1191 prio = vlan_tci_prio(rule);
1192 prio_mask = vlan_tci_priom(rule);
1194 if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
1196 vlan_mask |= RQFPR_CFI;
1197 } else if (cfi != VLAN_TAG_PRESENT &&
1198 cfi_mask == VLAN_TAG_PRESENT) {
1199 vlan_mask |= RQFPR_CFI;
1203 switch (rule->flow_type & ~FLOW_EXT) {
1205 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
1206 RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
1207 gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
1208 &rule->m_u.tcp_ip4_spec, tab);
1211 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
1212 RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
1213 gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
1214 &rule->m_u.udp_ip4_spec, tab);
1217 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1219 gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
1220 gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
1221 (struct ethtool_tcpip4_spec *)&rule->m_u,
1225 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1227 gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
1228 (struct ethtool_usrip4_spec *) &rule->m_u,
1233 gfar_set_parse_bits(vlan, vlan_mask, tab);
1234 gfar_set_ether((struct ethhdr *) &rule->h_u,
1235 (struct ethhdr *) &rule->m_u, tab);
1241 /* Set the vlan attributes in the end */
1243 gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
1244 gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
1247 /* If there has been nothing written till now, it must be a default */
1248 if (tab->index == old_index) {
1249 gfar_set_mask(0xFFFFFFFF, tab);
1250 tab->fe[tab->index].ctrl = 0x20;
1251 tab->fe[tab->index].prop = 0x0;
1255 /* Remove last AND */
1256 tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
1258 /* Specify which queue to use or to drop */
1259 if (rule->ring_cookie == RX_CLS_FLOW_DISC)
1260 tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
1262 tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
1264 /* Only big enough entries can be clustered */
1265 if (tab->index > (old_index + 2)) {
1266 tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
1267 tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
1270 /* In rare cases the cache can be full while there is
1273 if (tab->index > MAX_FILER_CACHE_IDX - 1)
1279 /* Copy size filer entries */
1280 static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
1281 struct gfar_filer_entry src[0], s32 size)
1285 dst[size].ctrl = src[size].ctrl;
1286 dst[size].prop = src[size].prop;
1290 /* Delete the contents of the filer-table between start and end
1293 static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
1297 if (end > MAX_FILER_CACHE_IDX || end < begin)
1301 length = end - begin;
1304 while (end < tab->index) {
1305 tab->fe[begin].ctrl = tab->fe[end].ctrl;
1306 tab->fe[begin++].prop = tab->fe[end++].prop;
1309 /* Fill up with don't cares */
1310 while (begin < tab->index) {
1311 tab->fe[begin].ctrl = 0x60;
1312 tab->fe[begin].prop = 0xFFFFFFFF;
1316 tab->index -= length;
1320 /* Make space on the wanted location */
1321 static int gfar_expand_filer_entries(u32 begin, u32 length,
1322 struct filer_table *tab)
1324 if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX ||
1325 begin > MAX_FILER_CACHE_IDX)
1328 gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
1329 tab->index - length + 1);
1331 tab->index += length;
1335 static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
1337 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
1339 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
1340 (RQFCR_AND | RQFCR_CLE))
1346 static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
1348 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
1350 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
1357 /* Uses hardwares clustering option to reduce
1358 * the number of filer table entries
1360 static void gfar_cluster_filer(struct filer_table *tab)
1362 s32 i = -1, j, iend, jend;
1364 while ((i = gfar_get_next_cluster_start(++i, tab)) != -1) {
1366 while ((j = gfar_get_next_cluster_start(++j, tab)) != -1) {
1367 /* The cluster entries self and the previous one
1368 * (a mask) must be identical!
1370 if (tab->fe[i].ctrl != tab->fe[j].ctrl)
1372 if (tab->fe[i].prop != tab->fe[j].prop)
1374 if (tab->fe[i - 1].ctrl != tab->fe[j - 1].ctrl)
1376 if (tab->fe[i - 1].prop != tab->fe[j - 1].prop)
1378 iend = gfar_get_next_cluster_end(i, tab);
1379 jend = gfar_get_next_cluster_end(j, tab);
1380 if (jend == -1 || iend == -1)
1383 /* First we make some free space, where our cluster
1384 * element should be. Then we copy it there and finally
1385 * delete in from its old location.
1387 if (gfar_expand_filer_entries(iend, (jend - j), tab) ==
1391 gfar_copy_filer_entries(&(tab->fe[iend + 1]),
1392 &(tab->fe[jend + 1]), jend - j);
1394 if (gfar_trim_filer_entries(jend - 1,
1399 /* Mask out cluster bit */
1400 tab->fe[iend].ctrl &= ~(RQFCR_CLE);
1405 /* Swaps the masked bits of a1<>a2 and b1<>b2 */
1406 static void gfar_swap_bits(struct gfar_filer_entry *a1,
1407 struct gfar_filer_entry *a2,
1408 struct gfar_filer_entry *b1,
1409 struct gfar_filer_entry *b2, u32 mask)
1412 temp[0] = a1->ctrl & mask;
1413 temp[1] = a2->ctrl & mask;
1414 temp[2] = b1->ctrl & mask;
1415 temp[3] = b2->ctrl & mask;
1422 a1->ctrl |= temp[1];
1423 a2->ctrl |= temp[0];
1424 b1->ctrl |= temp[3];
1425 b2->ctrl |= temp[2];
1428 /* Generate a list consisting of masks values with their start and
1429 * end of validity and block as indicator for parts belonging
1430 * together (glued by ANDs) in mask_table
1432 static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
1433 struct filer_table *tab)
1435 u32 i, and_index = 0, block_index = 1;
1437 for (i = 0; i < tab->index; i++) {
1439 /* LSByte of control = 0 sets a mask */
1440 if (!(tab->fe[i].ctrl & 0xF)) {
1441 mask_table[and_index].mask = tab->fe[i].prop;
1442 mask_table[and_index].start = i;
1443 mask_table[and_index].block = block_index;
1445 mask_table[and_index - 1].end = i - 1;
1448 /* cluster starts and ends will be separated because they should
1449 * hold their position
1451 if (tab->fe[i].ctrl & RQFCR_CLE)
1453 /* A not set AND indicates the end of a depended block */
1454 if (!(tab->fe[i].ctrl & RQFCR_AND))
1458 mask_table[and_index - 1].end = i - 1;
1463 /* Sorts the entries of mask_table by the values of the masks.
1464 * Important: The 0xFF80 flags of the first and last entry of a
1465 * block must hold their position (which queue, CLusterEnable, ReJEct,
1468 static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
1469 struct filer_table *temp_table, u32 and_index)
1471 /* Pointer to compare function (_asc or _desc) */
1472 int (*gfar_comp)(const void *, const void *);
1474 u32 i, size = 0, start = 0, prev = 1;
1475 u32 old_first, old_last, new_first, new_last;
1477 gfar_comp = &gfar_comp_desc;
1479 for (i = 0; i < and_index; i++) {
1480 if (prev != mask_table[i].block) {
1481 old_first = mask_table[start].start + 1;
1482 old_last = mask_table[i - 1].end;
1483 sort(mask_table + start, size,
1484 sizeof(struct gfar_mask_entry),
1485 gfar_comp, &gfar_swap);
1487 /* Toggle order for every block. This makes the
1488 * thing more efficient!
1490 if (gfar_comp == gfar_comp_desc)
1491 gfar_comp = &gfar_comp_asc;
1493 gfar_comp = &gfar_comp_desc;
1495 new_first = mask_table[start].start + 1;
1496 new_last = mask_table[i - 1].end;
1498 gfar_swap_bits(&temp_table->fe[new_first],
1499 &temp_table->fe[old_first],
1500 &temp_table->fe[new_last],
1501 &temp_table->fe[old_last],
1502 RQFCR_QUEUE | RQFCR_CLE |
1503 RQFCR_RJE | RQFCR_AND);
1509 prev = mask_table[i].block;
1513 /* Reduces the number of masks needed in the filer table to save entries
1514 * This is done by sorting the masks of a depended block. A depended block is
1515 * identified by gluing ANDs or CLE. The sorting order toggles after every
1516 * block. Of course entries in scope of a mask must change their location with
1519 static int gfar_optimize_filer_masks(struct filer_table *tab)
1521 struct filer_table *temp_table;
1522 struct gfar_mask_entry *mask_table;
1524 u32 and_index = 0, previous_mask = 0, i = 0, j = 0, size = 0;
1527 /* We need a copy of the filer table because
1528 * we want to change its order
1530 temp_table = kmemdup(tab, sizeof(*temp_table), GFP_KERNEL);
1531 if (temp_table == NULL)
1534 mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
1535 sizeof(struct gfar_mask_entry), GFP_KERNEL);
1537 if (mask_table == NULL) {
1542 and_index = gfar_generate_mask_table(mask_table, tab);
1544 gfar_sort_mask_table(mask_table, temp_table, and_index);
1546 /* Now we can copy the data from our duplicated filer table to
1547 * the real one in the order the mask table says
1549 for (i = 0; i < and_index; i++) {
1550 size = mask_table[i].end - mask_table[i].start + 1;
1551 gfar_copy_filer_entries(&(tab->fe[j]),
1552 &(temp_table->fe[mask_table[i].start]), size);
1556 /* And finally we just have to check for duplicated masks and drop the
1559 for (i = 0; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1560 if (tab->fe[i].ctrl == 0x80) {
1561 previous_mask = i++;
1565 for (; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1566 if (tab->fe[i].ctrl == 0x80) {
1567 if (tab->fe[i].prop == tab->fe[previous_mask].prop) {
1568 /* Two identical ones found!
1569 * So drop the second one!
1571 gfar_trim_filer_entries(i, i, tab);
1573 /* Not identical! */
1579 end: kfree(temp_table);
1583 /* Write the bit-pattern from software's buffer to hardware registers */
1584 static int gfar_write_filer_table(struct gfar_private *priv,
1585 struct filer_table *tab)
1588 if (tab->index > MAX_FILER_IDX - 1)
1591 /* Fill regular entries */
1592 for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].prop);
1594 gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
1595 /* Fill the rest with fall-troughs */
1596 for (; i < MAX_FILER_IDX - 1; i++)
1597 gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
1598 /* Last entry must be default accept
1599 * because that's what people expect
1601 gfar_write_filer(priv, i, 0x20, 0x0);
1606 static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
1607 struct gfar_private *priv)
1610 if (flow->flow_type & FLOW_EXT) {
1611 if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
1612 netdev_warn(priv->ndev,
1613 "User-specific data not supported!\n");
1614 if (~flow->m_ext.vlan_etype)
1615 netdev_warn(priv->ndev,
1616 "VLAN-etype not supported!\n");
1618 if (flow->flow_type == IP_USER_FLOW)
1619 if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
1620 netdev_warn(priv->ndev,
1621 "IP-Version differing from IPv4 not supported!\n");
1626 static int gfar_process_filer_changes(struct gfar_private *priv)
1628 struct ethtool_flow_spec_container *j;
1629 struct filer_table *tab;
1633 /* So index is set to zero, too! */
1634 tab = kzalloc(sizeof(*tab), GFP_KERNEL);
1638 /* Now convert the existing filer data from flow_spec into
1639 * filer tables binary format
1641 list_for_each_entry(j, &priv->rx_list.list, list) {
1642 ret = gfar_convert_to_filer(&j->fs, tab);
1643 if (ret == -EBUSY) {
1644 netdev_err(priv->ndev,
1645 "Rule not added: No free space!\n");
1649 netdev_err(priv->ndev,
1650 "Rule not added: Unsupported Flow-type!\n");
1657 /* Optimizations to save entries */
1658 gfar_cluster_filer(tab);
1659 gfar_optimize_filer_masks(tab);
1661 pr_debug("\tSummary:\n"
1662 "\tData on hardware: %d\n"
1663 "\tCompression rate: %d%%\n",
1664 tab->index, 100 - (100 * tab->index) / i);
1666 /* Write everything to hardware */
1667 ret = gfar_write_filer_table(priv, tab);
1668 if (ret == -EBUSY) {
1669 netdev_err(priv->ndev, "Rule not added: No free space!\n");
1678 static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
1682 for (i = 0; i < sizeof(flow->m_u); i++)
1683 flow->m_u.hdata[i] ^= 0xFF;
1685 flow->m_ext.vlan_etype ^= cpu_to_be16(0xFFFF);
1686 flow->m_ext.vlan_tci ^= cpu_to_be16(0xFFFF);
1687 flow->m_ext.data[0] ^= cpu_to_be32(~0);
1688 flow->m_ext.data[1] ^= cpu_to_be32(~0);
1691 static int gfar_add_cls(struct gfar_private *priv,
1692 struct ethtool_rx_flow_spec *flow)
1694 struct ethtool_flow_spec_container *temp, *comp;
1697 temp = kmalloc(sizeof(*temp), GFP_KERNEL);
1700 memcpy(&temp->fs, flow, sizeof(temp->fs));
1702 gfar_invert_masks(&temp->fs);
1703 ret = gfar_check_capability(&temp->fs, priv);
1706 /* Link in the new element at the right @location */
1707 if (list_empty(&priv->rx_list.list)) {
1708 ret = gfar_check_filer_hardware(priv);
1711 list_add(&temp->list, &priv->rx_list.list);
1714 list_for_each_entry(comp, &priv->rx_list.list, list) {
1715 if (comp->fs.location > flow->location) {
1716 list_add_tail(&temp->list, &comp->list);
1719 if (comp->fs.location == flow->location) {
1720 netdev_err(priv->ndev,
1721 "Rule not added: ID %d not free!\n",
1727 list_add_tail(&temp->list, &priv->rx_list.list);
1731 ret = gfar_process_filer_changes(priv);
1734 priv->rx_list.count++;
1738 list_del(&temp->list);
1744 static int gfar_del_cls(struct gfar_private *priv, u32 loc)
1746 struct ethtool_flow_spec_container *comp;
1749 if (list_empty(&priv->rx_list.list))
1752 list_for_each_entry(comp, &priv->rx_list.list, list) {
1753 if (comp->fs.location == loc) {
1754 list_del(&comp->list);
1756 priv->rx_list.count--;
1757 gfar_process_filer_changes(priv);
1766 static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
1768 struct ethtool_flow_spec_container *comp;
1771 list_for_each_entry(comp, &priv->rx_list.list, list) {
1772 if (comp->fs.location == cmd->fs.location) {
1773 memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
1774 gfar_invert_masks(&cmd->fs);
1783 static int gfar_get_cls_all(struct gfar_private *priv,
1784 struct ethtool_rxnfc *cmd, u32 *rule_locs)
1786 struct ethtool_flow_spec_container *comp;
1789 list_for_each_entry(comp, &priv->rx_list.list, list) {
1790 if (i == cmd->rule_cnt)
1792 rule_locs[i] = comp->fs.location;
1796 cmd->data = MAX_FILER_IDX;
1802 static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1804 struct gfar_private *priv = netdev_priv(dev);
1807 if (test_bit(GFAR_RESETTING, &priv->state))
1810 mutex_lock(&priv->rx_queue_access);
1814 ret = gfar_set_hash_opts(priv, cmd);
1816 case ETHTOOL_SRXCLSRLINS:
1817 if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
1818 cmd->fs.ring_cookie >= priv->num_rx_queues) ||
1819 cmd->fs.location >= MAX_FILER_IDX) {
1823 ret = gfar_add_cls(priv, &cmd->fs);
1825 case ETHTOOL_SRXCLSRLDEL:
1826 ret = gfar_del_cls(priv, cmd->fs.location);
1832 mutex_unlock(&priv->rx_queue_access);
1837 static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1840 struct gfar_private *priv = netdev_priv(dev);
1844 case ETHTOOL_GRXRINGS:
1845 cmd->data = priv->num_rx_queues;
1847 case ETHTOOL_GRXCLSRLCNT:
1848 cmd->rule_cnt = priv->rx_list.count;
1850 case ETHTOOL_GRXCLSRULE:
1851 ret = gfar_get_cls(priv, cmd);
1853 case ETHTOOL_GRXCLSRLALL:
1854 ret = gfar_get_cls_all(priv, cmd, rule_locs);
1864 int gfar_phc_index = -1;
1865 EXPORT_SYMBOL(gfar_phc_index);
1867 static int gfar_get_ts_info(struct net_device *dev,
1868 struct ethtool_ts_info *info)
1870 struct gfar_private *priv = netdev_priv(dev);
1872 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
1873 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
1874 SOF_TIMESTAMPING_SOFTWARE;
1875 info->phc_index = -1;
1878 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
1879 SOF_TIMESTAMPING_RX_HARDWARE |
1880 SOF_TIMESTAMPING_RAW_HARDWARE;
1881 info->phc_index = gfar_phc_index;
1882 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1883 (1 << HWTSTAMP_TX_ON);
1884 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1885 (1 << HWTSTAMP_FILTER_ALL);
1889 const struct ethtool_ops gfar_ethtool_ops = {
1890 .get_settings = gfar_gsettings,
1891 .set_settings = gfar_ssettings,
1892 .get_drvinfo = gfar_gdrvinfo,
1893 .get_regs_len = gfar_reglen,
1894 .get_regs = gfar_get_regs,
1895 .get_link = ethtool_op_get_link,
1896 .get_coalesce = gfar_gcoalesce,
1897 .set_coalesce = gfar_scoalesce,
1898 .get_ringparam = gfar_gringparam,
1899 .set_ringparam = gfar_sringparam,
1900 .get_pauseparam = gfar_gpauseparam,
1901 .set_pauseparam = gfar_spauseparam,
1902 .get_strings = gfar_gstrings,
1903 .get_sset_count = gfar_sset_count,
1904 .get_ethtool_stats = gfar_fill_stats,
1905 .get_msglevel = gfar_get_msglevel,
1906 .set_msglevel = gfar_set_msglevel,
1908 .get_wol = gfar_get_wol,
1909 .set_wol = gfar_set_wol,
1911 .set_rxnfc = gfar_set_nfc,
1912 .get_rxnfc = gfar_get_nfc,
1913 .get_ts_info = gfar_get_ts_info,