gianfar: Add Multiple group Support
[linux-2.6/x86.git] / drivers / net / gianfar_ethtool.c
blob562f6c20f59110e4960b0d627cf2217cf7d4ee7f
1 /*
2 * drivers/net/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 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 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
30 #include <linux/mm.h>
32 #include <asm/io.h>
33 #include <asm/irq.h>
34 #include <asm/uaccess.h>
35 #include <linux/module.h>
36 #include <linux/crc32.h>
37 #include <asm/types.h>
38 #include <linux/ethtool.h>
39 #include <linux/mii.h>
40 #include <linux/phy.h>
42 #include "gianfar.h"
44 extern void gfar_start(struct net_device *dev);
45 extern int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
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,
50 u64 * buf);
51 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
52 static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
53 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
54 static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
55 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
56 static void gfar_gdrvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo);
58 static char stat_gstrings[][ETH_GSTRING_LEN] = {
59 "rx-dropped-by-kernel",
60 "rx-large-frame-errors",
61 "rx-short-frame-errors",
62 "rx-non-octet-errors",
63 "rx-crc-errors",
64 "rx-overrun-errors",
65 "rx-busy-errors",
66 "rx-babbling-errors",
67 "rx-truncated-frames",
68 "ethernet-bus-error",
69 "tx-babbling-errors",
70 "tx-underrun-errors",
71 "rx-skb-missing-errors",
72 "tx-timeout-errors",
73 "tx-rx-64-frames",
74 "tx-rx-65-127-frames",
75 "tx-rx-128-255-frames",
76 "tx-rx-256-511-frames",
77 "tx-rx-512-1023-frames",
78 "tx-rx-1024-1518-frames",
79 "tx-rx-1519-1522-good-vlan",
80 "rx-bytes",
81 "rx-packets",
82 "rx-fcs-errors",
83 "receive-multicast-packet",
84 "receive-broadcast-packet",
85 "rx-control-frame-packets",
86 "rx-pause-frame-packets",
87 "rx-unknown-op-code",
88 "rx-alignment-error",
89 "rx-frame-length-error",
90 "rx-code-error",
91 "rx-carrier-sense-error",
92 "rx-undersize-packets",
93 "rx-oversize-packets",
94 "rx-fragmented-frames",
95 "rx-jabber-frames",
96 "rx-dropped-frames",
97 "tx-byte-counter",
98 "tx-packets",
99 "tx-multicast-packets",
100 "tx-broadcast-packets",
101 "tx-pause-control-frames",
102 "tx-deferral-packets",
103 "tx-excessive-deferral-packets",
104 "tx-single-collision-packets",
105 "tx-multiple-collision-packets",
106 "tx-late-collision-packets",
107 "tx-excessive-collision-packets",
108 "tx-total-collision",
109 "reserved",
110 "tx-dropped-frames",
111 "tx-jabber-frames",
112 "tx-fcs-errors",
113 "tx-control-frames",
114 "tx-oversize-frames",
115 "tx-undersize-frames",
116 "tx-fragmented-frames",
119 /* Fill in a buffer with the strings which correspond to the
120 * stats */
121 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
123 struct gfar_private *priv = netdev_priv(dev);
125 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
126 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
127 else
128 memcpy(buf, stat_gstrings,
129 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
132 /* Fill in an array of 64-bit statistics from various sources.
133 * This array will be appended to the end of the ethtool_stats
134 * structure, and returned to user space
136 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy, u64 * buf)
138 int i;
139 struct gfar_private *priv = netdev_priv(dev);
140 struct gfar __iomem *regs = priv->gfargrp[0].regs;
141 u64 *extra = (u64 *) & priv->extra_stats;
143 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
144 u32 __iomem *rmon = (u32 __iomem *) &regs->rmon;
145 struct gfar_stats *stats = (struct gfar_stats *) buf;
147 for (i = 0; i < GFAR_RMON_LEN; i++)
148 stats->rmon[i] = (u64) gfar_read(&rmon[i]);
150 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
151 stats->extra[i] = extra[i];
152 } else
153 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
154 buf[i] = extra[i];
157 static int gfar_sset_count(struct net_device *dev, int sset)
159 struct gfar_private *priv = netdev_priv(dev);
161 switch (sset) {
162 case ETH_SS_STATS:
163 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
164 return GFAR_STATS_LEN;
165 else
166 return GFAR_EXTRA_STATS_LEN;
167 default:
168 return -EOPNOTSUPP;
172 /* Fills in the drvinfo structure with some basic info */
173 static void gfar_gdrvinfo(struct net_device *dev, struct
174 ethtool_drvinfo *drvinfo)
176 strncpy(drvinfo->driver, DRV_NAME, GFAR_INFOSTR_LEN);
177 strncpy(drvinfo->version, gfar_driver_version, GFAR_INFOSTR_LEN);
178 strncpy(drvinfo->fw_version, "N/A", GFAR_INFOSTR_LEN);
179 strncpy(drvinfo->bus_info, "N/A", GFAR_INFOSTR_LEN);
180 drvinfo->regdump_len = 0;
181 drvinfo->eedump_len = 0;
185 static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
187 struct gfar_private *priv = netdev_priv(dev);
188 struct phy_device *phydev = priv->phydev;
190 if (NULL == phydev)
191 return -ENODEV;
193 return phy_ethtool_sset(phydev, cmd);
197 /* Return the current settings in the ethtool_cmd structure */
198 static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
200 struct gfar_private *priv = netdev_priv(dev);
201 struct phy_device *phydev = priv->phydev;
202 struct gfar_priv_rx_q *rx_queue = NULL;
203 struct gfar_priv_tx_q *tx_queue = NULL;
205 if (NULL == phydev)
206 return -ENODEV;
207 tx_queue = priv->tx_queue[0];
208 rx_queue = priv->rx_queue[0];
210 /* etsec-1.7 and older versions have only one txic
211 * and rxic regs although they support multiple queues */
212 cmd->maxtxpkt = get_icft_value(tx_queue->txic);
213 cmd->maxrxpkt = get_icft_value(rx_queue->rxic);
215 return phy_ethtool_gset(phydev, cmd);
218 /* Return the length of the register structure */
219 static int gfar_reglen(struct net_device *dev)
221 return sizeof (struct gfar);
224 /* Return a dump of the GFAR register space */
225 static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
227 int i;
228 struct gfar_private *priv = netdev_priv(dev);
229 u32 __iomem *theregs = (u32 __iomem *) priv->gfargrp[0].regs;
230 u32 *buf = (u32 *) regbuf;
232 for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
233 buf[i] = gfar_read(&theregs[i]);
236 /* Convert microseconds to ethernet clock ticks, which changes
237 * depending on what speed the controller is running at */
238 static unsigned int gfar_usecs2ticks(struct gfar_private *priv, unsigned int usecs)
240 unsigned int count;
242 /* The timer is different, depending on the interface speed */
243 switch (priv->phydev->speed) {
244 case SPEED_1000:
245 count = GFAR_GBIT_TIME;
246 break;
247 case SPEED_100:
248 count = GFAR_100_TIME;
249 break;
250 case SPEED_10:
251 default:
252 count = GFAR_10_TIME;
253 break;
256 /* Make sure we return a number greater than 0
257 * if usecs > 0 */
258 return ((usecs * 1000 + count - 1) / count);
261 /* Convert ethernet clock ticks to microseconds */
262 static unsigned int gfar_ticks2usecs(struct gfar_private *priv, unsigned int ticks)
264 unsigned int count;
266 /* The timer is different, depending on the interface speed */
267 switch (priv->phydev->speed) {
268 case SPEED_1000:
269 count = GFAR_GBIT_TIME;
270 break;
271 case SPEED_100:
272 count = GFAR_100_TIME;
273 break;
274 case SPEED_10:
275 default:
276 count = GFAR_10_TIME;
277 break;
280 /* Make sure we return a number greater than 0 */
281 /* if ticks is > 0 */
282 return ((ticks * count) / 1000);
285 /* Get the coalescing parameters, and put them in the cvals
286 * structure. */
287 static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
289 struct gfar_private *priv = netdev_priv(dev);
290 struct gfar_priv_rx_q *rx_queue = NULL;
291 struct gfar_priv_tx_q *tx_queue = NULL;
292 unsigned long rxtime;
293 unsigned long rxcount;
294 unsigned long txtime;
295 unsigned long txcount;
297 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
298 return -EOPNOTSUPP;
300 if (NULL == priv->phydev)
301 return -ENODEV;
303 rx_queue = priv->rx_queue[0];
304 tx_queue = priv->tx_queue[0];
306 rxtime = get_ictt_value(rx_queue->rxic);
307 rxcount = get_icft_value(rx_queue->rxic);
308 txtime = get_ictt_value(tx_queue->txic);
309 txcount = get_icft_value(tx_queue->txic);
310 cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
311 cvals->rx_max_coalesced_frames = rxcount;
313 cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
314 cvals->tx_max_coalesced_frames = txcount;
316 cvals->use_adaptive_rx_coalesce = 0;
317 cvals->use_adaptive_tx_coalesce = 0;
319 cvals->pkt_rate_low = 0;
320 cvals->rx_coalesce_usecs_low = 0;
321 cvals->rx_max_coalesced_frames_low = 0;
322 cvals->tx_coalesce_usecs_low = 0;
323 cvals->tx_max_coalesced_frames_low = 0;
325 /* When the packet rate is below pkt_rate_high but above
326 * pkt_rate_low (both measured in packets per second) the
327 * normal {rx,tx}_* coalescing parameters are used.
330 /* When the packet rate is (measured in packets per second)
331 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
332 * used.
334 cvals->pkt_rate_high = 0;
335 cvals->rx_coalesce_usecs_high = 0;
336 cvals->rx_max_coalesced_frames_high = 0;
337 cvals->tx_coalesce_usecs_high = 0;
338 cvals->tx_max_coalesced_frames_high = 0;
340 /* How often to do adaptive coalescing packet rate sampling,
341 * measured in seconds. Must not be zero.
343 cvals->rate_sample_interval = 0;
345 return 0;
348 /* Change the coalescing values.
349 * Both cvals->*_usecs and cvals->*_frames have to be > 0
350 * in order for coalescing to be active
352 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
354 struct gfar_private *priv = netdev_priv(dev);
355 int i = 0;
357 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
358 return -EOPNOTSUPP;
360 /* Set up rx coalescing */
361 /* As of now, we will enable/disable coalescing for all
362 * queues together in case of eTSEC2, this will be modified
363 * along with the ethtool interface */
364 if ((cvals->rx_coalesce_usecs == 0) ||
365 (cvals->rx_max_coalesced_frames == 0)) {
366 for (i = 0; i < priv->num_rx_queues; i++)
367 priv->rx_queue[i]->rxcoalescing = 0;
368 } else {
369 for (i = 0; i < priv->num_rx_queues; i++)
370 priv->rx_queue[i]->rxcoalescing = 1;
373 if (NULL == priv->phydev)
374 return -ENODEV;
376 /* Check the bounds of the values */
377 if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
378 pr_info("Coalescing is limited to %d microseconds\n",
379 GFAR_MAX_COAL_USECS);
380 return -EINVAL;
383 if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
384 pr_info("Coalescing is limited to %d frames\n",
385 GFAR_MAX_COAL_FRAMES);
386 return -EINVAL;
389 for (i = 0; i < priv->num_rx_queues; i++) {
390 priv->rx_queue[i]->rxic = mk_ic_value(
391 cvals->rx_max_coalesced_frames,
392 gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs));
395 /* Set up tx coalescing */
396 if ((cvals->tx_coalesce_usecs == 0) ||
397 (cvals->tx_max_coalesced_frames == 0)) {
398 for (i = 0; i < priv->num_tx_queues; i++)
399 priv->tx_queue[i]->txcoalescing = 0;
400 } else {
401 for (i = 0; i < priv->num_tx_queues; i++)
402 priv->tx_queue[i]->txcoalescing = 1;
405 /* Check the bounds of the values */
406 if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
407 pr_info("Coalescing is limited to %d microseconds\n",
408 GFAR_MAX_COAL_USECS);
409 return -EINVAL;
412 if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
413 pr_info("Coalescing is limited to %d frames\n",
414 GFAR_MAX_COAL_FRAMES);
415 return -EINVAL;
418 for (i = 0; i < priv->num_tx_queues; i++) {
419 priv->tx_queue[i]->txic = mk_ic_value(
420 cvals->tx_max_coalesced_frames,
421 gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs));
424 gfar_configure_coalescing(priv, 0xFF, 0xFF);
426 return 0;
429 /* Fills in rvals with the current ring parameters. Currently,
430 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
431 * jumbo are ignored by the driver */
432 static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
434 struct gfar_private *priv = netdev_priv(dev);
435 struct gfar_priv_tx_q *tx_queue = NULL;
436 struct gfar_priv_rx_q *rx_queue = NULL;
438 tx_queue = priv->tx_queue[0];
439 rx_queue = priv->rx_queue[0];
441 rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
442 rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
443 rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
444 rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
446 /* Values changeable by the user. The valid values are
447 * in the range 1 to the "*_max_pending" counterpart above.
449 rvals->rx_pending = rx_queue->rx_ring_size;
450 rvals->rx_mini_pending = rx_queue->rx_ring_size;
451 rvals->rx_jumbo_pending = rx_queue->rx_ring_size;
452 rvals->tx_pending = tx_queue->tx_ring_size;
455 /* Change the current ring parameters, stopping the controller if
456 * necessary so that we don't mess things up while we're in
457 * motion. We wait for the ring to be clean before reallocating
458 * the rings. */
459 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
461 struct gfar_private *priv = netdev_priv(dev);
462 int err = 0, i = 0;
464 if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
465 return -EINVAL;
467 if (!is_power_of_2(rvals->rx_pending)) {
468 printk("%s: Ring sizes must be a power of 2\n",
469 dev->name);
470 return -EINVAL;
473 if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
474 return -EINVAL;
476 if (!is_power_of_2(rvals->tx_pending)) {
477 printk("%s: Ring sizes must be a power of 2\n",
478 dev->name);
479 return -EINVAL;
483 if (dev->flags & IFF_UP) {
484 unsigned long flags;
486 /* Halt TX and RX, and process the frames which
487 * have already been received */
488 local_irq_save(flags);
489 lock_tx_qs(priv);
490 lock_rx_qs(priv);
492 gfar_halt(dev);
494 unlock_rx_qs(priv);
495 unlock_tx_qs(priv);
496 local_irq_restore(flags);
498 for (i = 0; i < priv->num_rx_queues; i++)
499 gfar_clean_rx_ring(priv->rx_queue[i],
500 priv->rx_queue[i]->rx_ring_size);
502 /* Now we take down the rings to rebuild them */
503 stop_gfar(dev);
506 /* Change the size */
507 for (i = 0; i < priv->num_rx_queues; i++) {
508 priv->rx_queue[i]->rx_ring_size = rvals->rx_pending;
509 priv->tx_queue[i]->tx_ring_size = rvals->tx_pending;
510 priv->tx_queue[i]->num_txbdfree = priv->tx_queue[i]->tx_ring_size;
513 /* Rebuild the rings with the new size */
514 if (dev->flags & IFF_UP) {
515 err = startup_gfar(dev);
516 netif_tx_wake_all_queues(dev);
518 return err;
521 static int gfar_set_rx_csum(struct net_device *dev, uint32_t data)
523 struct gfar_private *priv = netdev_priv(dev);
524 unsigned long flags;
525 int err = 0, i = 0;
527 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
528 return -EOPNOTSUPP;
531 if (dev->flags & IFF_UP) {
532 /* Halt TX and RX, and process the frames which
533 * have already been received */
534 local_irq_save(flags);
535 lock_tx_qs(priv);
536 lock_rx_qs(priv);
538 gfar_halt(dev);
540 unlock_tx_qs(priv);
541 unlock_rx_qs(priv);
542 local_irq_save(flags);
544 for (i = 0; i < priv->num_rx_queues; i++)
545 gfar_clean_rx_ring(priv->rx_queue[i],
546 priv->rx_queue[i]->rx_ring_size);
548 /* Now we take down the rings to rebuild them */
549 stop_gfar(dev);
552 spin_lock_irqsave(&priv->bflock, flags);
553 priv->rx_csum_enable = data;
554 spin_unlock_irqrestore(&priv->bflock, flags);
556 if (dev->flags & IFF_UP) {
557 err = startup_gfar(dev);
558 netif_tx_wake_all_queues(dev);
560 return err;
563 static uint32_t gfar_get_rx_csum(struct net_device *dev)
565 struct gfar_private *priv = netdev_priv(dev);
567 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
568 return 0;
570 return priv->rx_csum_enable;
573 static int gfar_set_tx_csum(struct net_device *dev, uint32_t data)
575 struct gfar_private *priv = netdev_priv(dev);
577 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
578 return -EOPNOTSUPP;
580 netif_tx_lock_bh(dev);
582 if (data)
583 dev->features |= NETIF_F_IP_CSUM;
584 else
585 dev->features &= ~NETIF_F_IP_CSUM;
587 netif_tx_unlock_bh(dev);
589 return 0;
592 static uint32_t gfar_get_tx_csum(struct net_device *dev)
594 struct gfar_private *priv = netdev_priv(dev);
596 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
597 return 0;
599 return (dev->features & NETIF_F_IP_CSUM) != 0;
602 static uint32_t gfar_get_msglevel(struct net_device *dev)
604 struct gfar_private *priv = netdev_priv(dev);
605 return priv->msg_enable;
608 static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
610 struct gfar_private *priv = netdev_priv(dev);
611 priv->msg_enable = data;
614 #ifdef CONFIG_PM
615 static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
617 struct gfar_private *priv = netdev_priv(dev);
619 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
620 wol->supported = WAKE_MAGIC;
621 wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
622 } else {
623 wol->supported = wol->wolopts = 0;
627 static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
629 struct gfar_private *priv = netdev_priv(dev);
630 unsigned long flags;
632 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
633 wol->wolopts != 0)
634 return -EINVAL;
636 if (wol->wolopts & ~WAKE_MAGIC)
637 return -EINVAL;
639 spin_lock_irqsave(&priv->bflock, flags);
640 priv->wol_en = wol->wolopts & WAKE_MAGIC ? 1 : 0;
641 device_set_wakeup_enable(&dev->dev, priv->wol_en);
642 spin_unlock_irqrestore(&priv->bflock, flags);
644 return 0;
646 #endif
648 const struct ethtool_ops gfar_ethtool_ops = {
649 .get_settings = gfar_gsettings,
650 .set_settings = gfar_ssettings,
651 .get_drvinfo = gfar_gdrvinfo,
652 .get_regs_len = gfar_reglen,
653 .get_regs = gfar_get_regs,
654 .get_link = ethtool_op_get_link,
655 .get_coalesce = gfar_gcoalesce,
656 .set_coalesce = gfar_scoalesce,
657 .get_ringparam = gfar_gringparam,
658 .set_ringparam = gfar_sringparam,
659 .get_strings = gfar_gstrings,
660 .get_sset_count = gfar_sset_count,
661 .get_ethtool_stats = gfar_fill_stats,
662 .get_rx_csum = gfar_get_rx_csum,
663 .get_tx_csum = gfar_get_tx_csum,
664 .set_rx_csum = gfar_set_rx_csum,
665 .set_tx_csum = gfar_set_tx_csum,
666 .set_sg = ethtool_op_set_sg,
667 .get_msglevel = gfar_get_msglevel,
668 .set_msglevel = gfar_set_msglevel,
669 #ifdef CONFIG_PM
670 .get_wol = gfar_get_wol,
671 .set_wol = gfar_set_wol,
672 #endif