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/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/net_tstamp.h>
30 #include <linux/skbuff.h>
31 #include <linux/spinlock.h>
36 #include <asm/uaccess.h>
37 #include <linux/module.h>
38 #include <linux/crc32.h>
39 #include <asm/types.h>
40 #include <linux/ethtool.h>
41 #include <linux/mii.h>
42 #include <linux/phy.h>
43 #include <linux/sort.h>
44 #include <linux/if_vlan.h>
48 extern void gfar_start(struct net_device *dev);
49 extern int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue,
52 #define GFAR_MAX_COAL_USECS 0xffff
53 #define GFAR_MAX_COAL_FRAMES 0xff
54 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
56 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
57 static int gfar_gcoalesce(struct net_device *dev,
58 struct ethtool_coalesce *cvals);
59 static int gfar_scoalesce(struct net_device *dev,
60 struct ethtool_coalesce *cvals);
61 static void gfar_gringparam(struct net_device *dev,
62 struct ethtool_ringparam *rvals);
63 static int gfar_sringparam(struct net_device *dev,
64 struct ethtool_ringparam *rvals);
65 static void gfar_gdrvinfo(struct net_device *dev,
66 struct ethtool_drvinfo *drvinfo);
68 static const char stat_gstrings[][ETH_GSTRING_LEN] = {
69 "rx-large-frame-errors",
70 "rx-short-frame-errors",
71 "rx-non-octet-errors",
76 "rx-truncated-frames",
80 "rx-skb-missing-errors",
83 "tx-rx-65-127-frames",
84 "tx-rx-128-255-frames",
85 "tx-rx-256-511-frames",
86 "tx-rx-512-1023-frames",
87 "tx-rx-1024-1518-frames",
88 "tx-rx-1519-1522-good-vlan",
92 "receive-multicast-packet",
93 "receive-broadcast-packet",
94 "rx-control-frame-packets",
95 "rx-pause-frame-packets",
98 "rx-frame-length-error",
100 "rx-carrier-sense-error",
101 "rx-undersize-packets",
102 "rx-oversize-packets",
103 "rx-fragmented-frames",
108 "tx-multicast-packets",
109 "tx-broadcast-packets",
110 "tx-pause-control-frames",
111 "tx-deferral-packets",
112 "tx-excessive-deferral-packets",
113 "tx-single-collision-packets",
114 "tx-multiple-collision-packets",
115 "tx-late-collision-packets",
116 "tx-excessive-collision-packets",
117 "tx-total-collision",
123 "tx-oversize-frames",
124 "tx-undersize-frames",
125 "tx-fragmented-frames",
128 /* Fill in a buffer with the strings which correspond to the
130 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
132 struct gfar_private *priv = netdev_priv(dev);
134 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
135 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
137 memcpy(buf, stat_gstrings,
138 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
141 /* Fill in an array of 64-bit statistics from various sources.
142 * This array will be appended to the end of the ethtool_stats
143 * structure, and returned to user space
145 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
149 struct gfar_private *priv = netdev_priv(dev);
150 struct gfar __iomem *regs = priv->gfargrp[0].regs;
151 atomic64_t *extra = (atomic64_t *)&priv->extra_stats;
153 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
154 buf[i] = atomic64_read(&extra[i]);
156 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
157 u32 __iomem *rmon = (u32 __iomem *) ®s->rmon;
159 for (; i < GFAR_STATS_LEN; i++, rmon++)
160 buf[i] = (u64) gfar_read(rmon);
164 static int gfar_sset_count(struct net_device *dev, int sset)
166 struct gfar_private *priv = netdev_priv(dev);
170 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
171 return GFAR_STATS_LEN;
173 return GFAR_EXTRA_STATS_LEN;
179 /* Fills in the drvinfo structure with some basic info */
180 static void gfar_gdrvinfo(struct net_device *dev,
181 struct ethtool_drvinfo *drvinfo)
183 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
184 strlcpy(drvinfo->version, gfar_driver_version,
185 sizeof(drvinfo->version));
186 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
187 strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
188 drvinfo->regdump_len = 0;
189 drvinfo->eedump_len = 0;
193 static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
195 struct gfar_private *priv = netdev_priv(dev);
196 struct phy_device *phydev = priv->phydev;
201 return phy_ethtool_sset(phydev, cmd);
205 /* Return the current settings in the ethtool_cmd structure */
206 static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
208 struct gfar_private *priv = netdev_priv(dev);
209 struct phy_device *phydev = priv->phydev;
210 struct gfar_priv_rx_q *rx_queue = NULL;
211 struct gfar_priv_tx_q *tx_queue = NULL;
215 tx_queue = priv->tx_queue[0];
216 rx_queue = priv->rx_queue[0];
218 /* etsec-1.7 and older versions have only one txic
219 * and rxic regs although they support multiple queues */
220 cmd->maxtxpkt = get_icft_value(tx_queue->txic);
221 cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
223 return phy_ethtool_gset(phydev, cmd);
226 /* Return the length of the register structure */
227 static int gfar_reglen(struct net_device *dev)
229 return sizeof (struct gfar);
232 /* Return a dump of the GFAR register space */
233 static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs,
237 struct gfar_private *priv = netdev_priv(dev);
238 u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
239 u32 *buf = (u32 *) regbuf;
241 for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
242 buf[i] = gfar_read(&theregs[i]);
245 /* Convert microseconds to ethernet clock ticks, which changes
246 * depending on what speed the controller is running at */
247 static unsigned int gfar_usecs2ticks(struct gfar_private *priv,
252 /* The timer is different, depending on the interface speed */
253 switch (priv->phydev->speed) {
255 count = GFAR_GBIT_TIME;
258 count = GFAR_100_TIME;
262 count = GFAR_10_TIME;
266 /* Make sure we return a number greater than 0
268 return (usecs * 1000 + count - 1) / count;
271 /* Convert ethernet clock ticks to microseconds */
272 static unsigned int gfar_ticks2usecs(struct gfar_private *priv,
277 /* The timer is different, depending on the interface speed */
278 switch (priv->phydev->speed) {
280 count = GFAR_GBIT_TIME;
283 count = GFAR_100_TIME;
287 count = GFAR_10_TIME;
291 /* Make sure we return a number greater than 0 */
292 /* if ticks is > 0 */
293 return (ticks * count) / 1000;
296 /* Get the coalescing parameters, and put them in the cvals
298 static int gfar_gcoalesce(struct net_device *dev,
299 struct ethtool_coalesce *cvals)
301 struct gfar_private *priv = netdev_priv(dev);
302 struct gfar_priv_rx_q *rx_queue = NULL;
303 struct gfar_priv_tx_q *tx_queue = NULL;
304 unsigned long rxtime;
305 unsigned long rxcount;
306 unsigned long txtime;
307 unsigned long txcount;
309 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
312 if (NULL == priv->phydev)
315 rx_queue = priv->rx_queue[0];
316 tx_queue = priv->tx_queue[0];
318 rxtime = get_ictt_value(rx_queue->rxic);
319 rxcount = get_icft_value(rx_queue->rxic);
320 txtime = get_ictt_value(tx_queue->txic);
321 txcount = get_icft_value(tx_queue->txic);
322 cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
323 cvals->rx_max_coalesced_frames = rxcount;
325 cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
326 cvals->tx_max_coalesced_frames = txcount;
328 cvals->use_adaptive_rx_coalesce = 0;
329 cvals->use_adaptive_tx_coalesce = 0;
331 cvals->pkt_rate_low = 0;
332 cvals->rx_coalesce_usecs_low = 0;
333 cvals->rx_max_coalesced_frames_low = 0;
334 cvals->tx_coalesce_usecs_low = 0;
335 cvals->tx_max_coalesced_frames_low = 0;
337 /* When the packet rate is below pkt_rate_high but above
338 * pkt_rate_low (both measured in packets per second) the
339 * normal {rx,tx}_* coalescing parameters are used.
342 /* When the packet rate is (measured in packets per second)
343 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
346 cvals->pkt_rate_high = 0;
347 cvals->rx_coalesce_usecs_high = 0;
348 cvals->rx_max_coalesced_frames_high = 0;
349 cvals->tx_coalesce_usecs_high = 0;
350 cvals->tx_max_coalesced_frames_high = 0;
352 /* How often to do adaptive coalescing packet rate sampling,
353 * measured in seconds. Must not be zero.
355 cvals->rate_sample_interval = 0;
360 /* Change the coalescing values.
361 * Both cvals->*_usecs and cvals->*_frames have to be > 0
362 * in order for coalescing to be active
364 static int gfar_scoalesce(struct net_device *dev,
365 struct ethtool_coalesce *cvals)
367 struct gfar_private *priv = netdev_priv(dev);
370 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
373 /* Set up rx coalescing */
374 /* As of now, we will enable/disable coalescing for all
375 * queues together in case of eTSEC2, this will be modified
376 * along with the ethtool interface
378 if ((cvals->rx_coalesce_usecs == 0) ||
379 (cvals->rx_max_coalesced_frames == 0)) {
380 for (i = 0; i < priv->num_rx_queues; i++)
381 priv->rx_queue[i]->rxcoalescing = 0;
383 for (i = 0; i < priv->num_rx_queues; i++)
384 priv->rx_queue[i]->rxcoalescing = 1;
387 if (NULL == priv->phydev)
390 /* Check the bounds of the values */
391 if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
392 netdev_info(dev, "Coalescing is limited to %d microseconds\n",
393 GFAR_MAX_COAL_USECS);
397 if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
398 netdev_info(dev, "Coalescing is limited to %d frames\n",
399 GFAR_MAX_COAL_FRAMES);
403 for (i = 0; i < priv->num_rx_queues; i++) {
404 priv->rx_queue[i]->rxic = mk_ic_value(
405 cvals->rx_max_coalesced_frames,
406 gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
409 /* Set up tx coalescing */
410 if ((cvals->tx_coalesce_usecs == 0) ||
411 (cvals->tx_max_coalesced_frames == 0)) {
412 for (i = 0; i < priv->num_tx_queues; i++)
413 priv->tx_queue[i]->txcoalescing = 0;
415 for (i = 0; i < priv->num_tx_queues; i++)
416 priv->tx_queue[i]->txcoalescing = 1;
419 /* Check the bounds of the values */
420 if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
421 netdev_info(dev, "Coalescing is limited to %d microseconds\n",
422 GFAR_MAX_COAL_USECS);
426 if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
427 netdev_info(dev, "Coalescing is limited to %d frames\n",
428 GFAR_MAX_COAL_FRAMES);
432 for (i = 0; i < priv->num_tx_queues; i++) {
433 priv->tx_queue[i]->txic = mk_ic_value(
434 cvals->tx_max_coalesced_frames,
435 gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
438 gfar_configure_coalescing_all(priv);
443 /* Fills in rvals with the current ring parameters. Currently,
444 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
445 * jumbo are ignored by the driver */
446 static void gfar_gringparam(struct net_device *dev,
447 struct ethtool_ringparam *rvals)
449 struct gfar_private *priv = netdev_priv(dev);
450 struct gfar_priv_tx_q *tx_queue = NULL;
451 struct gfar_priv_rx_q *rx_queue = NULL;
453 tx_queue = priv->tx_queue[0];
454 rx_queue = priv->rx_queue[0];
456 rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
457 rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
458 rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
459 rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
461 /* Values changeable by the user. The valid values are
462 * in the range 1 to the "*_max_pending" counterpart above.
464 rvals->rx_pending = rx_queue->rx_ring_size;
465 rvals->rx_mini_pending = rx_queue->rx_ring_size;
466 rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
467 rvals->tx_pending = tx_queue->tx_ring_size;
470 /* Change the current ring parameters, stopping the controller if
471 * necessary so that we don't mess things up while we're in
472 * motion. We wait for the ring to be clean before reallocating
475 static int gfar_sringparam(struct net_device *dev,
476 struct ethtool_ringparam *rvals)
478 struct gfar_private *priv = netdev_priv(dev);
481 if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
484 if (!is_power_of_2(rvals->rx_pending)) {
485 netdev_err(dev, "Ring sizes must be a power of 2\n");
489 if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
492 if (!is_power_of_2(rvals->tx_pending)) {
493 netdev_err(dev, "Ring sizes must be a power of 2\n");
498 if (dev->flags & IFF_UP) {
501 /* Halt TX and RX, and process the frames which
502 * have already been received
504 local_irq_save(flags);
512 local_irq_restore(flags);
514 for (i = 0; i < priv->num_rx_queues; i++)
515 gfar_clean_rx_ring(priv->rx_queue[i],
516 priv->rx_queue[i]->rx_ring_size);
518 /* Now we take down the rings to rebuild them */
522 /* Change the size */
523 for (i = 0; i < priv->num_rx_queues; i++) {
524 priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
525 priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
526 priv->tx_queue[i]->num_txbdfree =
527 priv->tx_queue[i]->tx_ring_size;
530 /* Rebuild the rings with the new size */
531 if (dev->flags & IFF_UP) {
532 err = startup_gfar(dev);
533 netif_tx_wake_all_queues(dev);
538 static void gfar_gpauseparam(struct net_device *dev,
539 struct ethtool_pauseparam *epause)
541 struct gfar_private *priv = netdev_priv(dev);
543 epause->autoneg = !!priv->pause_aneg_en;
544 epause->rx_pause = !!priv->rx_pause_en;
545 epause->tx_pause = !!priv->tx_pause_en;
548 static int gfar_spauseparam(struct net_device *dev,
549 struct ethtool_pauseparam *epause)
551 struct gfar_private *priv = netdev_priv(dev);
552 struct phy_device *phydev = priv->phydev;
553 struct gfar __iomem *regs = priv->gfargrp[0].regs;
556 if (!(phydev->supported & SUPPORTED_Pause) ||
557 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
558 (epause->rx_pause != epause->tx_pause)))
561 priv->rx_pause_en = priv->tx_pause_en = 0;
562 if (epause->rx_pause) {
563 priv->rx_pause_en = 1;
565 if (epause->tx_pause) {
566 priv->tx_pause_en = 1;
567 /* FLOW_CTRL_RX & TX */
568 newadv = ADVERTISED_Pause;
569 } else /* FLOW_CTLR_RX */
570 newadv = ADVERTISED_Pause | ADVERTISED_Asym_Pause;
571 } else if (epause->tx_pause) {
572 priv->tx_pause_en = 1;
574 newadv = ADVERTISED_Asym_Pause;
579 priv->pause_aneg_en = 1;
581 priv->pause_aneg_en = 0;
583 oldadv = phydev->advertising &
584 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
585 if (oldadv != newadv) {
586 phydev->advertising &=
587 ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
588 phydev->advertising |= newadv;
590 /* inform link partner of our
591 * new flow ctrl settings
593 return phy_start_aneg(phydev);
595 if (!epause->autoneg) {
597 tempval = gfar_read(®s->maccfg1);
598 tempval &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
599 if (priv->tx_pause_en)
600 tempval |= MACCFG1_TX_FLOW;
601 if (priv->rx_pause_en)
602 tempval |= MACCFG1_RX_FLOW;
603 gfar_write(®s->maccfg1, tempval);
610 int gfar_set_features(struct net_device *dev, netdev_features_t features)
612 struct gfar_private *priv = netdev_priv(dev);
615 netdev_features_t changed = dev->features ^ features;
617 if (changed & (NETIF_F_HW_VLAN_CTAG_TX|NETIF_F_HW_VLAN_CTAG_RX))
618 gfar_vlan_mode(dev, features);
620 if (!(changed & NETIF_F_RXCSUM))
623 if (dev->flags & IFF_UP) {
624 /* Halt TX and RX, and process the frames which
625 * have already been received
627 local_irq_save(flags);
635 local_irq_restore(flags);
637 for (i = 0; i < priv->num_rx_queues; i++)
638 gfar_clean_rx_ring(priv->rx_queue[i],
639 priv->rx_queue[i]->rx_ring_size);
641 /* Now we take down the rings to rebuild them */
644 dev->features = features;
646 err = startup_gfar(dev);
647 netif_tx_wake_all_queues(dev);
652 static uint32_t gfar_get_msglevel(struct net_device *dev)
654 struct gfar_private *priv = netdev_priv(dev);
656 return priv->msg_enable;
659 static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
661 struct gfar_private *priv = netdev_priv(dev);
663 priv->msg_enable = data;
667 static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
669 struct gfar_private *priv = netdev_priv(dev);
671 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
672 wol->supported = WAKE_MAGIC;
673 wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
675 wol->supported = wol->wolopts = 0;
679 static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
681 struct gfar_private *priv = netdev_priv(dev);
684 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
688 if (wol->wolopts & ~WAKE_MAGIC)
691 device_set_wakeup_enable(&dev->dev, wol->wolopts & WAKE_MAGIC);
693 spin_lock_irqsave(&priv->bflock, flags);
694 priv->wol_en = !!device_may_wakeup(&dev->dev);
695 spin_unlock_irqrestore(&priv->bflock, flags);
701 static void ethflow_to_filer_rules (struct gfar_private *priv, u64 ethflow)
703 u32 fcr = 0x0, fpr = FPR_FILER_MASK;
705 if (ethflow & RXH_L2DA) {
706 fcr = RQFCR_PID_DAH |RQFCR_CMP_NOMATCH |
707 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
708 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
709 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
710 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
711 priv->cur_filer_idx = priv->cur_filer_idx - 1;
713 fcr = RQFCR_PID_DAL | RQFCR_AND | RQFCR_CMP_NOMATCH |
714 RQFCR_HASH | RQFCR_AND | RQFCR_HASHTBL_0;
715 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
716 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
717 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
718 priv->cur_filer_idx = priv->cur_filer_idx - 1;
721 if (ethflow & RXH_VLAN) {
722 fcr = RQFCR_PID_VID | RQFCR_CMP_NOMATCH | RQFCR_HASH |
723 RQFCR_AND | RQFCR_HASHTBL_0;
724 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
725 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
726 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
727 priv->cur_filer_idx = priv->cur_filer_idx - 1;
730 if (ethflow & RXH_IP_SRC) {
731 fcr = RQFCR_PID_SIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
732 RQFCR_AND | RQFCR_HASHTBL_0;
733 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
734 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
735 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
736 priv->cur_filer_idx = priv->cur_filer_idx - 1;
739 if (ethflow & (RXH_IP_DST)) {
740 fcr = RQFCR_PID_DIA | RQFCR_CMP_NOMATCH | RQFCR_HASH |
741 RQFCR_AND | RQFCR_HASHTBL_0;
742 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
743 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
744 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
745 priv->cur_filer_idx = priv->cur_filer_idx - 1;
748 if (ethflow & RXH_L3_PROTO) {
749 fcr = RQFCR_PID_L4P | RQFCR_CMP_NOMATCH | RQFCR_HASH |
750 RQFCR_AND | RQFCR_HASHTBL_0;
751 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
752 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
753 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
754 priv->cur_filer_idx = priv->cur_filer_idx - 1;
757 if (ethflow & RXH_L4_B_0_1) {
758 fcr = RQFCR_PID_SPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
759 RQFCR_AND | RQFCR_HASHTBL_0;
760 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
761 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
762 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
763 priv->cur_filer_idx = priv->cur_filer_idx - 1;
766 if (ethflow & RXH_L4_B_2_3) {
767 fcr = RQFCR_PID_DPT | RQFCR_CMP_NOMATCH | RQFCR_HASH |
768 RQFCR_AND | RQFCR_HASHTBL_0;
769 priv->ftp_rqfpr[priv->cur_filer_idx] = fpr;
770 priv->ftp_rqfcr[priv->cur_filer_idx] = fcr;
771 gfar_write_filer(priv, priv->cur_filer_idx, fcr, fpr);
772 priv->cur_filer_idx = priv->cur_filer_idx - 1;
776 static int gfar_ethflow_to_filer_table(struct gfar_private *priv, u64 ethflow,
779 unsigned int last_rule_idx = priv->cur_filer_idx;
780 unsigned int cmp_rqfpr;
781 unsigned int *local_rqfpr;
782 unsigned int *local_rqfcr;
783 int i = 0x0, k = 0x0;
784 int j = MAX_FILER_IDX, l = 0x0;
787 local_rqfpr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
789 local_rqfcr = kmalloc_array(MAX_FILER_IDX + 1, sizeof(unsigned int),
791 if (!local_rqfpr || !local_rqfcr) {
798 cmp_rqfpr = RQFPR_IPV4 |RQFPR_TCP;
801 cmp_rqfpr = RQFPR_IPV4 |RQFPR_UDP;
804 cmp_rqfpr = RQFPR_IPV6 |RQFPR_TCP;
807 cmp_rqfpr = RQFPR_IPV6 |RQFPR_UDP;
810 netdev_err(priv->ndev,
811 "Right now this class is not supported\n");
816 for (i = 0; i < MAX_FILER_IDX + 1; i++) {
817 local_rqfpr[j] = priv->ftp_rqfpr[i];
818 local_rqfcr[j] = priv->ftp_rqfcr[i];
820 if ((priv->ftp_rqfcr[i] ==
821 (RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND)) &&
822 (priv->ftp_rqfpr[i] == cmp_rqfpr))
826 if (i == MAX_FILER_IDX + 1) {
827 netdev_err(priv->ndev,
828 "No parse rule found, can't create hash rules\n");
833 /* If a match was found, then it begins the starting of a cluster rule
834 * if it was already programmed, we need to overwrite these rules
836 for (l = i+1; l < MAX_FILER_IDX; l++) {
837 if ((priv->ftp_rqfcr[l] & RQFCR_CLE) &&
838 !(priv->ftp_rqfcr[l] & RQFCR_AND)) {
839 priv->ftp_rqfcr[l] = RQFCR_CLE | RQFCR_CMP_EXACT |
840 RQFCR_HASHTBL_0 | RQFCR_PID_MASK;
841 priv->ftp_rqfpr[l] = FPR_FILER_MASK;
842 gfar_write_filer(priv, l, priv->ftp_rqfcr[l],
847 if (!(priv->ftp_rqfcr[l] & RQFCR_CLE) &&
848 (priv->ftp_rqfcr[l] & RQFCR_AND))
851 local_rqfpr[j] = priv->ftp_rqfpr[l];
852 local_rqfcr[j] = priv->ftp_rqfcr[l];
857 priv->cur_filer_idx = l - 1;
861 ethflow_to_filer_rules(priv, ethflow);
863 /* Write back the popped out rules again */
864 for (k = j+1; k < MAX_FILER_IDX; k++) {
865 priv->ftp_rqfpr[priv->cur_filer_idx] = local_rqfpr[k];
866 priv->ftp_rqfcr[priv->cur_filer_idx] = local_rqfcr[k];
867 gfar_write_filer(priv, priv->cur_filer_idx,
868 local_rqfcr[k], local_rqfpr[k]);
869 if (!priv->cur_filer_idx)
871 priv->cur_filer_idx = priv->cur_filer_idx - 1;
880 static int gfar_set_hash_opts(struct gfar_private *priv,
881 struct ethtool_rxnfc *cmd)
883 /* write the filer rules here */
884 if (!gfar_ethflow_to_filer_table(priv, cmd->data, cmd->flow_type))
890 static int gfar_check_filer_hardware(struct gfar_private *priv)
892 struct gfar __iomem *regs = NULL;
895 regs = priv->gfargrp[0].regs;
897 /* Check if we are in FIFO mode */
898 i = gfar_read(®s->ecntrl);
900 if (i == ECNTRL_FIFM) {
901 netdev_notice(priv->ndev, "Interface in FIFO mode\n");
902 i = gfar_read(®s->rctrl);
903 i &= RCTRL_PRSDEP_MASK | RCTRL_PRSFM;
904 if (i == (RCTRL_PRSDEP_MASK | RCTRL_PRSFM)) {
905 netdev_info(priv->ndev,
906 "Receive Queue Filtering enabled\n");
908 netdev_warn(priv->ndev,
909 "Receive Queue Filtering disabled\n");
913 /* Or in standard mode */
915 i = gfar_read(®s->rctrl);
916 i &= RCTRL_PRSDEP_MASK;
917 if (i == RCTRL_PRSDEP_MASK) {
918 netdev_info(priv->ndev,
919 "Receive Queue Filtering enabled\n");
921 netdev_warn(priv->ndev,
922 "Receive Queue Filtering disabled\n");
927 /* Sets the properties for arbitrary filer rule
928 * to the first 4 Layer 4 Bytes
930 regs->rbifx = 0xC0C1C2C3;
934 static int gfar_comp_asc(const void *a, const void *b)
936 return memcmp(a, b, 4);
939 static int gfar_comp_desc(const void *a, const void *b)
941 return -memcmp(a, b, 4);
944 static void gfar_swap(void *a, void *b, int size)
955 /* Write a mask to filer cache */
956 static void gfar_set_mask(u32 mask, struct filer_table *tab)
958 tab->fe[tab->index].ctrl = RQFCR_AND | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
959 tab->fe[tab->index].prop = mask;
963 /* Sets parse bits (e.g. IP or TCP) */
964 static void gfar_set_parse_bits(u32 value, u32 mask, struct filer_table *tab)
966 gfar_set_mask(mask, tab);
967 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_PID_PARSE |
969 tab->fe[tab->index].prop = value;
973 static void gfar_set_general_attribute(u32 value, u32 mask, u32 flag,
974 struct filer_table *tab)
976 gfar_set_mask(mask, tab);
977 tab->fe[tab->index].ctrl = RQFCR_CMP_EXACT | RQFCR_AND | flag;
978 tab->fe[tab->index].prop = value;
982 /* For setting a tuple of value and mask of type flag
984 * IP-Src = 10.0.0.0/255.0.0.0
985 * value: 0x0A000000 mask: FF000000 flag: RQFPR_IPV4
987 * Ethtool gives us a value=0 and mask=~0 for don't care a tuple
988 * For a don't care mask it gives us a 0
990 * The check if don't care and the mask adjustment if mask=0 is done for VLAN
991 * and MAC stuff on an upper level (due to missing information on this level).
992 * For these guys we can discard them if they are value=0 and mask=0.
994 * Further the all masks are one-padded for better hardware efficiency.
996 static void gfar_set_attribute(u32 value, u32 mask, u32 flag,
997 struct filer_table *tab)
1002 if (!(value | mask))
1004 mask |= RQFCR_PID_PRI_MASK;
1009 if (!~(mask | RQFCR_PID_L4P_MASK))
1014 mask |= RQFCR_PID_L4P_MASK;
1018 if (!(value | mask))
1020 mask |= RQFCR_PID_VID_MASK;
1026 if (!~(mask | RQFCR_PID_PORT_MASK))
1031 mask |= RQFCR_PID_PORT_MASK;
1038 if (!(value | mask))
1040 mask |= RQFCR_PID_MAC_MASK;
1042 /* for all real 32bit masks */
1050 gfar_set_general_attribute(value, mask, flag, tab);
1053 /* Translates value and mask for UDP, TCP or SCTP */
1054 static void gfar_set_basic_ip(struct ethtool_tcpip4_spec *value,
1055 struct ethtool_tcpip4_spec *mask,
1056 struct filer_table *tab)
1058 gfar_set_attribute(value->ip4src, mask->ip4src, RQFCR_PID_SIA, tab);
1059 gfar_set_attribute(value->ip4dst, mask->ip4dst, RQFCR_PID_DIA, tab);
1060 gfar_set_attribute(value->pdst, mask->pdst, RQFCR_PID_DPT, tab);
1061 gfar_set_attribute(value->psrc, mask->psrc, RQFCR_PID_SPT, tab);
1062 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1065 /* Translates value and mask for RAW-IP4 */
1066 static void gfar_set_user_ip(struct ethtool_usrip4_spec *value,
1067 struct ethtool_usrip4_spec *mask,
1068 struct filer_table *tab)
1070 gfar_set_attribute(value->ip4src, mask->ip4src, RQFCR_PID_SIA, tab);
1071 gfar_set_attribute(value->ip4dst, mask->ip4dst, RQFCR_PID_DIA, tab);
1072 gfar_set_attribute(value->tos, mask->tos, RQFCR_PID_TOS, tab);
1073 gfar_set_attribute(value->proto, mask->proto, RQFCR_PID_L4P, tab);
1074 gfar_set_attribute(value->l4_4_bytes, mask->l4_4_bytes, RQFCR_PID_ARB,
1079 /* Translates value and mask for ETHER spec */
1080 static void gfar_set_ether(struct ethhdr *value, struct ethhdr *mask,
1081 struct filer_table *tab)
1083 u32 upper_temp_mask = 0;
1084 u32 lower_temp_mask = 0;
1086 /* Source address */
1087 if (!is_broadcast_ether_addr(mask->h_source)) {
1088 if (is_zero_ether_addr(mask->h_source)) {
1089 upper_temp_mask = 0xFFFFFFFF;
1090 lower_temp_mask = 0xFFFFFFFF;
1092 upper_temp_mask = mask->h_source[0] << 16 |
1093 mask->h_source[1] << 8 |
1095 lower_temp_mask = mask->h_source[3] << 16 |
1096 mask->h_source[4] << 8 |
1100 gfar_set_attribute(value->h_source[0] << 16 |
1101 value->h_source[1] << 8 |
1103 upper_temp_mask, RQFCR_PID_SAH, tab);
1104 /* And the same for the lower part */
1105 gfar_set_attribute(value->h_source[3] << 16 |
1106 value->h_source[4] << 8 |
1108 lower_temp_mask, RQFCR_PID_SAL, tab);
1110 /* Destination address */
1111 if (!is_broadcast_ether_addr(mask->h_dest)) {
1112 /* Special for destination is limited broadcast */
1113 if ((is_broadcast_ether_addr(value->h_dest) &&
1114 is_zero_ether_addr(mask->h_dest))) {
1115 gfar_set_parse_bits(RQFPR_EBC, RQFPR_EBC, tab);
1117 if (is_zero_ether_addr(mask->h_dest)) {
1118 upper_temp_mask = 0xFFFFFFFF;
1119 lower_temp_mask = 0xFFFFFFFF;
1121 upper_temp_mask = mask->h_dest[0] << 16 |
1122 mask->h_dest[1] << 8 |
1124 lower_temp_mask = mask->h_dest[3] << 16 |
1125 mask->h_dest[4] << 8 |
1130 gfar_set_attribute(value->h_dest[0] << 16 |
1131 value->h_dest[1] << 8 |
1133 upper_temp_mask, RQFCR_PID_DAH, tab);
1134 /* And the same for the lower part */
1135 gfar_set_attribute(value->h_dest[3] << 16 |
1136 value->h_dest[4] << 8 |
1138 lower_temp_mask, RQFCR_PID_DAL, tab);
1142 gfar_set_attribute(value->h_proto, mask->h_proto, RQFCR_PID_ETY, tab);
1145 /* Convert a rule to binary filter format of gianfar */
1146 static int gfar_convert_to_filer(struct ethtool_rx_flow_spec *rule,
1147 struct filer_table *tab)
1149 u32 vlan = 0, vlan_mask = 0;
1150 u32 id = 0, id_mask = 0;
1151 u32 cfi = 0, cfi_mask = 0;
1152 u32 prio = 0, prio_mask = 0;
1153 u32 old_index = tab->index;
1155 /* Check if vlan is wanted */
1156 if ((rule->flow_type & FLOW_EXT) && (rule->m_ext.vlan_tci != 0xFFFF)) {
1157 if (!rule->m_ext.vlan_tci)
1158 rule->m_ext.vlan_tci = 0xFFFF;
1161 vlan_mask = RQFPR_VLN;
1163 /* Separate the fields */
1164 id = rule->h_ext.vlan_tci & VLAN_VID_MASK;
1165 id_mask = rule->m_ext.vlan_tci & VLAN_VID_MASK;
1166 cfi = rule->h_ext.vlan_tci & VLAN_CFI_MASK;
1167 cfi_mask = rule->m_ext.vlan_tci & VLAN_CFI_MASK;
1168 prio = (rule->h_ext.vlan_tci & VLAN_PRIO_MASK) >>
1170 prio_mask = (rule->m_ext.vlan_tci & VLAN_PRIO_MASK) >>
1173 if (cfi == VLAN_TAG_PRESENT && cfi_mask == VLAN_TAG_PRESENT) {
1175 vlan_mask |= RQFPR_CFI;
1176 } else if (cfi != VLAN_TAG_PRESENT &&
1177 cfi_mask == VLAN_TAG_PRESENT) {
1178 vlan_mask |= RQFPR_CFI;
1182 switch (rule->flow_type & ~FLOW_EXT) {
1184 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_TCP | vlan,
1185 RQFPR_IPV4 | RQFPR_TCP | vlan_mask, tab);
1186 gfar_set_basic_ip(&rule->h_u.tcp_ip4_spec,
1187 &rule->m_u.tcp_ip4_spec, tab);
1190 gfar_set_parse_bits(RQFPR_IPV4 | RQFPR_UDP | vlan,
1191 RQFPR_IPV4 | RQFPR_UDP | vlan_mask, tab);
1192 gfar_set_basic_ip(&rule->h_u.udp_ip4_spec,
1193 &rule->m_u.udp_ip4_spec, tab);
1196 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1198 gfar_set_attribute(132, 0, RQFCR_PID_L4P, tab);
1199 gfar_set_basic_ip((struct ethtool_tcpip4_spec *)&rule->h_u,
1200 (struct ethtool_tcpip4_spec *)&rule->m_u,
1204 gfar_set_parse_bits(RQFPR_IPV4 | vlan, RQFPR_IPV4 | vlan_mask,
1206 gfar_set_user_ip((struct ethtool_usrip4_spec *) &rule->h_u,
1207 (struct ethtool_usrip4_spec *) &rule->m_u,
1212 gfar_set_parse_bits(vlan, vlan_mask, tab);
1213 gfar_set_ether((struct ethhdr *) &rule->h_u,
1214 (struct ethhdr *) &rule->m_u, tab);
1220 /* Set the vlan attributes in the end */
1222 gfar_set_attribute(id, id_mask, RQFCR_PID_VID, tab);
1223 gfar_set_attribute(prio, prio_mask, RQFCR_PID_PRI, tab);
1226 /* If there has been nothing written till now, it must be a default */
1227 if (tab->index == old_index) {
1228 gfar_set_mask(0xFFFFFFFF, tab);
1229 tab->fe[tab->index].ctrl = 0x20;
1230 tab->fe[tab->index].prop = 0x0;
1234 /* Remove last AND */
1235 tab->fe[tab->index - 1].ctrl &= (~RQFCR_AND);
1237 /* Specify which queue to use or to drop */
1238 if (rule->ring_cookie == RX_CLS_FLOW_DISC)
1239 tab->fe[tab->index - 1].ctrl |= RQFCR_RJE;
1241 tab->fe[tab->index - 1].ctrl |= (rule->ring_cookie << 10);
1243 /* Only big enough entries can be clustered */
1244 if (tab->index > (old_index + 2)) {
1245 tab->fe[old_index + 1].ctrl |= RQFCR_CLE;
1246 tab->fe[tab->index - 1].ctrl |= RQFCR_CLE;
1249 /* In rare cases the cache can be full while there is
1252 if (tab->index > MAX_FILER_CACHE_IDX - 1)
1258 /* Copy size filer entries */
1259 static void gfar_copy_filer_entries(struct gfar_filer_entry dst[0],
1260 struct gfar_filer_entry src[0], s32 size)
1264 dst[size].ctrl = src[size].ctrl;
1265 dst[size].prop = src[size].prop;
1269 /* Delete the contents of the filer-table between start and end
1272 static int gfar_trim_filer_entries(u32 begin, u32 end, struct filer_table *tab)
1276 if (end > MAX_FILER_CACHE_IDX || end < begin)
1280 length = end - begin;
1283 while (end < tab->index) {
1284 tab->fe[begin].ctrl = tab->fe[end].ctrl;
1285 tab->fe[begin++].prop = tab->fe[end++].prop;
1288 /* Fill up with don't cares */
1289 while (begin < tab->index) {
1290 tab->fe[begin].ctrl = 0x60;
1291 tab->fe[begin].prop = 0xFFFFFFFF;
1295 tab->index -= length;
1299 /* Make space on the wanted location */
1300 static int gfar_expand_filer_entries(u32 begin, u32 length,
1301 struct filer_table *tab)
1303 if (length == 0 || length + tab->index > MAX_FILER_CACHE_IDX ||
1304 begin > MAX_FILER_CACHE_IDX)
1307 gfar_copy_filer_entries(&(tab->fe[begin + length]), &(tab->fe[begin]),
1308 tab->index - length + 1);
1310 tab->index += length;
1314 static int gfar_get_next_cluster_start(int start, struct filer_table *tab)
1316 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
1318 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
1319 (RQFCR_AND | RQFCR_CLE))
1325 static int gfar_get_next_cluster_end(int start, struct filer_table *tab)
1327 for (; (start < tab->index) && (start < MAX_FILER_CACHE_IDX - 1);
1329 if ((tab->fe[start].ctrl & (RQFCR_AND | RQFCR_CLE)) ==
1336 /* Uses hardwares clustering option to reduce
1337 * the number of filer table entries
1339 static void gfar_cluster_filer(struct filer_table *tab)
1341 s32 i = -1, j, iend, jend;
1343 while ((i = gfar_get_next_cluster_start(++i, tab)) != -1) {
1345 while ((j = gfar_get_next_cluster_start(++j, tab)) != -1) {
1346 /* The cluster entries self and the previous one
1347 * (a mask) must be identical!
1349 if (tab->fe[i].ctrl != tab->fe[j].ctrl)
1351 if (tab->fe[i].prop != tab->fe[j].prop)
1353 if (tab->fe[i - 1].ctrl != tab->fe[j - 1].ctrl)
1355 if (tab->fe[i - 1].prop != tab->fe[j - 1].prop)
1357 iend = gfar_get_next_cluster_end(i, tab);
1358 jend = gfar_get_next_cluster_end(j, tab);
1359 if (jend == -1 || iend == -1)
1362 /* First we make some free space, where our cluster
1363 * element should be. Then we copy it there and finally
1364 * delete in from its old location.
1366 if (gfar_expand_filer_entries(iend, (jend - j), tab) ==
1370 gfar_copy_filer_entries(&(tab->fe[iend + 1]),
1371 &(tab->fe[jend + 1]), jend - j);
1373 if (gfar_trim_filer_entries(jend - 1,
1378 /* Mask out cluster bit */
1379 tab->fe[iend].ctrl &= ~(RQFCR_CLE);
1384 /* Swaps the masked bits of a1<>a2 and b1<>b2 */
1385 static void gfar_swap_bits(struct gfar_filer_entry *a1,
1386 struct gfar_filer_entry *a2,
1387 struct gfar_filer_entry *b1,
1388 struct gfar_filer_entry *b2, u32 mask)
1391 temp[0] = a1->ctrl & mask;
1392 temp[1] = a2->ctrl & mask;
1393 temp[2] = b1->ctrl & mask;
1394 temp[3] = b2->ctrl & mask;
1401 a1->ctrl |= temp[1];
1402 a2->ctrl |= temp[0];
1403 b1->ctrl |= temp[3];
1404 b2->ctrl |= temp[2];
1407 /* Generate a list consisting of masks values with their start and
1408 * end of validity and block as indicator for parts belonging
1409 * together (glued by ANDs) in mask_table
1411 static u32 gfar_generate_mask_table(struct gfar_mask_entry *mask_table,
1412 struct filer_table *tab)
1414 u32 i, and_index = 0, block_index = 1;
1416 for (i = 0; i < tab->index; i++) {
1418 /* LSByte of control = 0 sets a mask */
1419 if (!(tab->fe[i].ctrl & 0xF)) {
1420 mask_table[and_index].mask = tab->fe[i].prop;
1421 mask_table[and_index].start = i;
1422 mask_table[and_index].block = block_index;
1424 mask_table[and_index - 1].end = i - 1;
1427 /* cluster starts and ends will be separated because they should
1428 * hold their position
1430 if (tab->fe[i].ctrl & RQFCR_CLE)
1432 /* A not set AND indicates the end of a depended block */
1433 if (!(tab->fe[i].ctrl & RQFCR_AND))
1437 mask_table[and_index - 1].end = i - 1;
1442 /* Sorts the entries of mask_table by the values of the masks.
1443 * Important: The 0xFF80 flags of the first and last entry of a
1444 * block must hold their position (which queue, CLusterEnable, ReJEct,
1447 static void gfar_sort_mask_table(struct gfar_mask_entry *mask_table,
1448 struct filer_table *temp_table, u32 and_index)
1450 /* Pointer to compare function (_asc or _desc) */
1451 int (*gfar_comp)(const void *, const void *);
1453 u32 i, size = 0, start = 0, prev = 1;
1454 u32 old_first, old_last, new_first, new_last;
1456 gfar_comp = &gfar_comp_desc;
1458 for (i = 0; i < and_index; i++) {
1459 if (prev != mask_table[i].block) {
1460 old_first = mask_table[start].start + 1;
1461 old_last = mask_table[i - 1].end;
1462 sort(mask_table + start, size,
1463 sizeof(struct gfar_mask_entry),
1464 gfar_comp, &gfar_swap);
1466 /* Toggle order for every block. This makes the
1467 * thing more efficient!
1469 if (gfar_comp == gfar_comp_desc)
1470 gfar_comp = &gfar_comp_asc;
1472 gfar_comp = &gfar_comp_desc;
1474 new_first = mask_table[start].start + 1;
1475 new_last = mask_table[i - 1].end;
1477 gfar_swap_bits(&temp_table->fe[new_first],
1478 &temp_table->fe[old_first],
1479 &temp_table->fe[new_last],
1480 &temp_table->fe[old_last],
1481 RQFCR_QUEUE | RQFCR_CLE |
1482 RQFCR_RJE | RQFCR_AND);
1488 prev = mask_table[i].block;
1492 /* Reduces the number of masks needed in the filer table to save entries
1493 * This is done by sorting the masks of a depended block. A depended block is
1494 * identified by gluing ANDs or CLE. The sorting order toggles after every
1495 * block. Of course entries in scope of a mask must change their location with
1498 static int gfar_optimize_filer_masks(struct filer_table *tab)
1500 struct filer_table *temp_table;
1501 struct gfar_mask_entry *mask_table;
1503 u32 and_index = 0, previous_mask = 0, i = 0, j = 0, size = 0;
1506 /* We need a copy of the filer table because
1507 * we want to change its order
1509 temp_table = kmemdup(tab, sizeof(*temp_table), GFP_KERNEL);
1510 if (temp_table == NULL)
1513 mask_table = kcalloc(MAX_FILER_CACHE_IDX / 2 + 1,
1514 sizeof(struct gfar_mask_entry), GFP_KERNEL);
1516 if (mask_table == NULL) {
1521 and_index = gfar_generate_mask_table(mask_table, tab);
1523 gfar_sort_mask_table(mask_table, temp_table, and_index);
1525 /* Now we can copy the data from our duplicated filer table to
1526 * the real one in the order the mask table says
1528 for (i = 0; i < and_index; i++) {
1529 size = mask_table[i].end - mask_table[i].start + 1;
1530 gfar_copy_filer_entries(&(tab->fe[j]),
1531 &(temp_table->fe[mask_table[i].start]), size);
1535 /* And finally we just have to check for duplicated masks and drop the
1538 for (i = 0; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1539 if (tab->fe[i].ctrl == 0x80) {
1540 previous_mask = i++;
1544 for (; i < tab->index && i < MAX_FILER_CACHE_IDX; i++) {
1545 if (tab->fe[i].ctrl == 0x80) {
1546 if (tab->fe[i].prop == tab->fe[previous_mask].prop) {
1547 /* Two identical ones found!
1548 * So drop the second one!
1550 gfar_trim_filer_entries(i, i, tab);
1552 /* Not identical! */
1558 end: kfree(temp_table);
1562 /* Write the bit-pattern from software's buffer to hardware registers */
1563 static int gfar_write_filer_table(struct gfar_private *priv,
1564 struct filer_table *tab)
1567 if (tab->index > MAX_FILER_IDX - 1)
1570 /* Avoid inconsistent filer table to be processed */
1573 /* Fill regular entries */
1574 for (; i < MAX_FILER_IDX - 1 && (tab->fe[i].ctrl | tab->fe[i].ctrl);
1576 gfar_write_filer(priv, i, tab->fe[i].ctrl, tab->fe[i].prop);
1577 /* Fill the rest with fall-troughs */
1578 for (; i < MAX_FILER_IDX - 1; i++)
1579 gfar_write_filer(priv, i, 0x60, 0xFFFFFFFF);
1580 /* Last entry must be default accept
1581 * because that's what people expect
1583 gfar_write_filer(priv, i, 0x20, 0x0);
1590 static int gfar_check_capability(struct ethtool_rx_flow_spec *flow,
1591 struct gfar_private *priv)
1594 if (flow->flow_type & FLOW_EXT) {
1595 if (~flow->m_ext.data[0] || ~flow->m_ext.data[1])
1596 netdev_warn(priv->ndev,
1597 "User-specific data not supported!\n");
1598 if (~flow->m_ext.vlan_etype)
1599 netdev_warn(priv->ndev,
1600 "VLAN-etype not supported!\n");
1602 if (flow->flow_type == IP_USER_FLOW)
1603 if (flow->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
1604 netdev_warn(priv->ndev,
1605 "IP-Version differing from IPv4 not supported!\n");
1610 static int gfar_process_filer_changes(struct gfar_private *priv)
1612 struct ethtool_flow_spec_container *j;
1613 struct filer_table *tab;
1617 /* So index is set to zero, too! */
1618 tab = kzalloc(sizeof(*tab), GFP_KERNEL);
1622 /* Now convert the existing filer data from flow_spec into
1623 * filer tables binary format
1625 list_for_each_entry(j, &priv->rx_list.list, list) {
1626 ret = gfar_convert_to_filer(&j->fs, tab);
1627 if (ret == -EBUSY) {
1628 netdev_err(priv->ndev,
1629 "Rule not added: No free space!\n");
1633 netdev_err(priv->ndev,
1634 "Rule not added: Unsupported Flow-type!\n");
1641 /* Optimizations to save entries */
1642 gfar_cluster_filer(tab);
1643 gfar_optimize_filer_masks(tab);
1645 pr_debug("\tSummary:\n"
1646 "\tData on hardware: %d\n"
1647 "\tCompression rate: %d%%\n",
1648 tab->index, 100 - (100 * tab->index) / i);
1650 /* Write everything to hardware */
1651 ret = gfar_write_filer_table(priv, tab);
1652 if (ret == -EBUSY) {
1653 netdev_err(priv->ndev, "Rule not added: No free space!\n");
1662 static void gfar_invert_masks(struct ethtool_rx_flow_spec *flow)
1666 for (i = 0; i < sizeof(flow->m_u); i++)
1667 flow->m_u.hdata[i] ^= 0xFF;
1669 flow->m_ext.vlan_etype ^= 0xFFFF;
1670 flow->m_ext.vlan_tci ^= 0xFFFF;
1671 flow->m_ext.data[0] ^= ~0;
1672 flow->m_ext.data[1] ^= ~0;
1675 static int gfar_add_cls(struct gfar_private *priv,
1676 struct ethtool_rx_flow_spec *flow)
1678 struct ethtool_flow_spec_container *temp, *comp;
1681 temp = kmalloc(sizeof(*temp), GFP_KERNEL);
1684 memcpy(&temp->fs, flow, sizeof(temp->fs));
1686 gfar_invert_masks(&temp->fs);
1687 ret = gfar_check_capability(&temp->fs, priv);
1690 /* Link in the new element at the right @location */
1691 if (list_empty(&priv->rx_list.list)) {
1692 ret = gfar_check_filer_hardware(priv);
1695 list_add(&temp->list, &priv->rx_list.list);
1698 list_for_each_entry(comp, &priv->rx_list.list, list) {
1699 if (comp->fs.location > flow->location) {
1700 list_add_tail(&temp->list, &comp->list);
1703 if (comp->fs.location == flow->location) {
1704 netdev_err(priv->ndev,
1705 "Rule not added: ID %d not free!\n",
1711 list_add_tail(&temp->list, &priv->rx_list.list);
1715 ret = gfar_process_filer_changes(priv);
1718 priv->rx_list.count++;
1722 list_del(&temp->list);
1728 static int gfar_del_cls(struct gfar_private *priv, u32 loc)
1730 struct ethtool_flow_spec_container *comp;
1733 if (list_empty(&priv->rx_list.list))
1736 list_for_each_entry(comp, &priv->rx_list.list, list) {
1737 if (comp->fs.location == loc) {
1738 list_del(&comp->list);
1740 priv->rx_list.count--;
1741 gfar_process_filer_changes(priv);
1750 static int gfar_get_cls(struct gfar_private *priv, struct ethtool_rxnfc *cmd)
1752 struct ethtool_flow_spec_container *comp;
1755 list_for_each_entry(comp, &priv->rx_list.list, list) {
1756 if (comp->fs.location == cmd->fs.location) {
1757 memcpy(&cmd->fs, &comp->fs, sizeof(cmd->fs));
1758 gfar_invert_masks(&cmd->fs);
1767 static int gfar_get_cls_all(struct gfar_private *priv,
1768 struct ethtool_rxnfc *cmd, u32 *rule_locs)
1770 struct ethtool_flow_spec_container *comp;
1773 list_for_each_entry(comp, &priv->rx_list.list, list) {
1774 if (i == cmd->rule_cnt)
1776 rule_locs[i] = comp->fs.location;
1780 cmd->data = MAX_FILER_IDX;
1786 static int gfar_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1788 struct gfar_private *priv = netdev_priv(dev);
1791 mutex_lock(&priv->rx_queue_access);
1795 ret = gfar_set_hash_opts(priv, cmd);
1797 case ETHTOOL_SRXCLSRLINS:
1798 if ((cmd->fs.ring_cookie != RX_CLS_FLOW_DISC &&
1799 cmd->fs.ring_cookie >= priv->num_rx_queues) ||
1800 cmd->fs.location >= MAX_FILER_IDX) {
1804 ret = gfar_add_cls(priv, &cmd->fs);
1806 case ETHTOOL_SRXCLSRLDEL:
1807 ret = gfar_del_cls(priv, cmd->fs.location);
1813 mutex_unlock(&priv->rx_queue_access);
1818 static int gfar_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1821 struct gfar_private *priv = netdev_priv(dev);
1825 case ETHTOOL_GRXRINGS:
1826 cmd->data = priv->num_rx_queues;
1828 case ETHTOOL_GRXCLSRLCNT:
1829 cmd->rule_cnt = priv->rx_list.count;
1831 case ETHTOOL_GRXCLSRULE:
1832 ret = gfar_get_cls(priv, cmd);
1834 case ETHTOOL_GRXCLSRLALL:
1835 ret = gfar_get_cls_all(priv, cmd, rule_locs);
1845 int gfar_phc_index = -1;
1846 EXPORT_SYMBOL(gfar_phc_index);
1848 static int gfar_get_ts_info(struct net_device *dev,
1849 struct ethtool_ts_info *info)
1851 struct gfar_private *priv = netdev_priv(dev);
1853 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)) {
1854 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
1855 SOF_TIMESTAMPING_SOFTWARE;
1856 info->phc_index = -1;
1859 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
1860 SOF_TIMESTAMPING_RX_HARDWARE |
1861 SOF_TIMESTAMPING_RAW_HARDWARE;
1862 info->phc_index = gfar_phc_index;
1863 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1864 (1 << HWTSTAMP_TX_ON);
1865 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1866 (1 << HWTSTAMP_FILTER_ALL);
1870 const struct ethtool_ops gfar_ethtool_ops = {
1871 .get_settings = gfar_gsettings,
1872 .set_settings = gfar_ssettings,
1873 .get_drvinfo = gfar_gdrvinfo,
1874 .get_regs_len = gfar_reglen,
1875 .get_regs = gfar_get_regs,
1876 .get_link = ethtool_op_get_link,
1877 .get_coalesce = gfar_gcoalesce,
1878 .set_coalesce = gfar_scoalesce,
1879 .get_ringparam = gfar_gringparam,
1880 .set_ringparam = gfar_sringparam,
1881 .get_pauseparam = gfar_gpauseparam,
1882 .set_pauseparam = gfar_spauseparam,
1883 .get_strings = gfar_gstrings,
1884 .get_sset_count = gfar_sset_count,
1885 .get_ethtool_stats = gfar_fill_stats,
1886 .get_msglevel = gfar_get_msglevel,
1887 .set_msglevel = gfar_set_msglevel,
1889 .get_wol = gfar_get_wol,
1890 .set_wol = gfar_set_wol,
1892 .set_rxnfc = gfar_set_nfc,
1893 .get_rxnfc = gfar_get_nfc,
1894 .get_ts_info = gfar_get_ts_info,