2 * drivers/net/gianfar_ethtool.c
4 * Gianfar Ethernet Driver
5 * Ethtool support for Gianfar Enet
6 * Based on e1000 ethtool support
9 * Maintainer: Kumar Gala (kumar.gala@freescale.com)
11 * Copyright (c) 2003,2004 Freescale Semiconductor, Inc.
13 * This software may be used and distributed according to
14 * the terms of the GNU Public License, Version 2, incorporated herein
18 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/slab.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/skbuff.h>
30 #include <linux/spinlock.h>
35 #include <asm/uaccess.h>
36 #include <linux/module.h>
37 #include <linux/version.h>
38 #include <linux/crc32.h>
39 #include <asm/types.h>
40 #include <asm/uaccess.h>
41 #include <linux/ethtool.h>
42 #include <linux/mii.h>
43 #include <linux/phy.h>
47 #define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
49 extern void gfar_start(struct net_device *dev);
50 extern int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
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, struct ethtool_coalesce *cvals);
58 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
59 static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
60 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
61 static void gfar_gdrvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo);
63 static char stat_gstrings[][ETH_GSTRING_LEN] = {
64 "rx-dropped-by-kernel",
65 "rx-large-frame-errors",
66 "rx-short-frame-errors",
67 "rx-non-octet-errors",
72 "rx-truncated-frames",
76 "rx-skb-missing-errors",
79 "tx-rx-65-127-frames",
80 "tx-rx-128-255-frames",
81 "tx-rx-256-511-frames",
82 "tx-rx-512-1023-frames",
83 "tx-rx-1024-1518-frames",
84 "tx-rx-1519-1522-good-vlan",
88 "receive-multicast-packet",
89 "receive-broadcast-packet",
90 "rx-control-frame-packets",
91 "rx-pause-frame-packets",
94 "rx-frame-length-error",
96 "rx-carrier-sense-error",
97 "rx-undersize-packets",
98 "rx-oversize-packets",
99 "rx-fragmented-frames",
104 "tx-multicast-packets",
105 "tx-broadcast-packets",
106 "tx-pause-control-frames",
107 "tx-deferral-packets",
108 "tx-excessive-deferral-packets",
109 "tx-single-collision-packets",
110 "tx-multiple-collision-packets",
111 "tx-late-collision-packets",
112 "tx-excessive-collision-packets",
113 "tx-total-collision",
119 "tx-oversize-frames",
120 "tx-undersize-frames",
121 "tx-fragmented-frames",
124 /* Fill in a buffer with the strings which correspond to the
126 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
128 struct gfar_private *priv = netdev_priv(dev);
130 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
131 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
133 memcpy(buf, stat_gstrings,
134 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
137 /* Fill in an array of 64-bit statistics from various sources.
138 * This array will be appended to the end of the ethtool_stats
139 * structure, and returned to user space
141 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy, u64 * buf)
144 struct gfar_private *priv = netdev_priv(dev);
145 u64 *extra = (u64 *) & priv->extra_stats;
147 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
148 u32 *rmon = (u32 *) & priv->regs->rmon;
149 struct gfar_stats *stats = (struct gfar_stats *) buf;
151 for (i = 0; i < GFAR_RMON_LEN; i++)
152 stats->rmon[i] = (u64) (rmon[i]);
154 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
155 stats->extra[i] = extra[i];
157 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
161 /* Returns the number of stats (and their corresponding strings) */
162 static int gfar_stats_count(struct net_device *dev)
164 struct gfar_private *priv = netdev_priv(dev);
166 if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
167 return GFAR_STATS_LEN;
169 return GFAR_EXTRA_STATS_LEN;
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->n_stats = GFAR_STATS_LEN;
181 drvinfo->testinfo_len = 0;
182 drvinfo->regdump_len = 0;
183 drvinfo->eedump_len = 0;
187 static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
189 struct gfar_private *priv = netdev_priv(dev);
190 struct phy_device *phydev = priv->phydev;
195 return phy_ethtool_sset(phydev, cmd);
199 /* Return the current settings in the ethtool_cmd structure */
200 static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
202 struct gfar_private *priv = netdev_priv(dev);
203 struct phy_device *phydev = priv->phydev;
208 cmd->maxtxpkt = priv->txcount;
209 cmd->maxrxpkt = priv->rxcount;
211 return phy_ethtool_gset(phydev, cmd);
214 /* Return the length of the register structure */
215 static int gfar_reglen(struct net_device *dev)
217 return sizeof (struct gfar);
220 /* Return a dump of the GFAR register space */
221 static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
224 struct gfar_private *priv = netdev_priv(dev);
225 u32 *theregs = (u32 *) priv->regs;
226 u32 *buf = (u32 *) regbuf;
228 for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
232 /* Convert microseconds to ethernet clock ticks, which changes
233 * depending on what speed the controller is running at */
234 static unsigned int gfar_usecs2ticks(struct gfar_private *priv, unsigned int usecs)
238 /* The timer is different, depending on the interface speed */
239 switch (priv->phydev->speed) {
241 count = GFAR_GBIT_TIME;
244 count = GFAR_100_TIME;
248 count = GFAR_10_TIME;
252 /* Make sure we return a number greater than 0
254 return ((usecs * 1000 + count - 1) / count);
257 /* Convert ethernet clock ticks to microseconds */
258 static unsigned int gfar_ticks2usecs(struct gfar_private *priv, unsigned int ticks)
262 /* The timer is different, depending on the interface speed */
263 switch (priv->phydev->speed) {
265 count = GFAR_GBIT_TIME;
268 count = GFAR_100_TIME;
272 count = GFAR_10_TIME;
276 /* Make sure we return a number greater than 0 */
277 /* if ticks is > 0 */
278 return ((ticks * count) / 1000);
281 /* Get the coalescing parameters, and put them in the cvals
283 static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
285 struct gfar_private *priv = netdev_priv(dev);
287 if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
290 if (NULL == priv->phydev)
293 cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, priv->rxtime);
294 cvals->rx_max_coalesced_frames = priv->rxcount;
296 cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, priv->txtime);
297 cvals->tx_max_coalesced_frames = priv->txcount;
299 cvals->use_adaptive_rx_coalesce = 0;
300 cvals->use_adaptive_tx_coalesce = 0;
302 cvals->pkt_rate_low = 0;
303 cvals->rx_coalesce_usecs_low = 0;
304 cvals->rx_max_coalesced_frames_low = 0;
305 cvals->tx_coalesce_usecs_low = 0;
306 cvals->tx_max_coalesced_frames_low = 0;
308 /* When the packet rate is below pkt_rate_high but above
309 * pkt_rate_low (both measured in packets per second) the
310 * normal {rx,tx}_* coalescing parameters are used.
313 /* When the packet rate is (measured in packets per second)
314 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
317 cvals->pkt_rate_high = 0;
318 cvals->rx_coalesce_usecs_high = 0;
319 cvals->rx_max_coalesced_frames_high = 0;
320 cvals->tx_coalesce_usecs_high = 0;
321 cvals->tx_max_coalesced_frames_high = 0;
323 /* How often to do adaptive coalescing packet rate sampling,
324 * measured in seconds. Must not be zero.
326 cvals->rate_sample_interval = 0;
331 /* Change the coalescing values.
332 * Both cvals->*_usecs and cvals->*_frames have to be > 0
333 * in order for coalescing to be active
335 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
337 struct gfar_private *priv = netdev_priv(dev);
339 if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
342 /* Set up rx coalescing */
343 if ((cvals->rx_coalesce_usecs == 0) ||
344 (cvals->rx_max_coalesced_frames == 0))
345 priv->rxcoalescing = 0;
347 priv->rxcoalescing = 1;
349 if (NULL == priv->phydev)
352 /* Check the bounds of the values */
353 if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
354 pr_info("Coalescing is limited to %d microseconds\n",
355 GFAR_MAX_COAL_USECS);
359 if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
360 pr_info("Coalescing is limited to %d frames\n",
361 GFAR_MAX_COAL_FRAMES);
365 priv->rxtime = gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs);
366 priv->rxcount = cvals->rx_max_coalesced_frames;
368 /* Set up tx coalescing */
369 if ((cvals->tx_coalesce_usecs == 0) ||
370 (cvals->tx_max_coalesced_frames == 0))
371 priv->txcoalescing = 0;
373 priv->txcoalescing = 1;
375 /* Check the bounds of the values */
376 if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
377 pr_info("Coalescing is limited to %d microseconds\n",
378 GFAR_MAX_COAL_USECS);
382 if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
383 pr_info("Coalescing is limited to %d frames\n",
384 GFAR_MAX_COAL_FRAMES);
388 priv->txtime = gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs);
389 priv->txcount = cvals->tx_max_coalesced_frames;
391 if (priv->rxcoalescing)
392 gfar_write(&priv->regs->rxic,
393 mk_ic_value(priv->rxcount, priv->rxtime));
395 gfar_write(&priv->regs->rxic, 0);
397 if (priv->txcoalescing)
398 gfar_write(&priv->regs->txic,
399 mk_ic_value(priv->txcount, priv->txtime));
401 gfar_write(&priv->regs->txic, 0);
406 /* Fills in rvals with the current ring parameters. Currently,
407 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
408 * jumbo are ignored by the driver */
409 static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
411 struct gfar_private *priv = netdev_priv(dev);
413 rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
414 rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
415 rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
416 rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
418 /* Values changeable by the user. The valid values are
419 * in the range 1 to the "*_max_pending" counterpart above.
421 rvals->rx_pending = priv->rx_ring_size;
422 rvals->rx_mini_pending = priv->rx_ring_size;
423 rvals->rx_jumbo_pending = priv->rx_ring_size;
424 rvals->tx_pending = priv->tx_ring_size;
427 /* Change the current ring parameters, stopping the controller if
428 * necessary so that we don't mess things up while we're in
429 * motion. We wait for the ring to be clean before reallocating
431 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
433 struct gfar_private *priv = netdev_priv(dev);
436 if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
439 if (!is_power_of_2(rvals->rx_pending)) {
440 printk("%s: Ring sizes must be a power of 2\n",
445 if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
448 if (!is_power_of_2(rvals->tx_pending)) {
449 printk("%s: Ring sizes must be a power of 2\n",
454 if (dev->flags & IFF_UP) {
457 /* Halt TX and RX, and process the frames which
458 * have already been received */
459 spin_lock_irqsave(&priv->lock, flags);
461 gfar_clean_rx_ring(dev, priv->rx_ring_size);
462 spin_unlock_irqrestore(&priv->lock, flags);
464 /* Now we take down the rings to rebuild them */
468 /* Change the size */
469 priv->rx_ring_size = rvals->rx_pending;
470 priv->tx_ring_size = rvals->tx_pending;
472 /* Rebuild the rings with the new size */
473 if (dev->flags & IFF_UP)
474 err = startup_gfar(dev);
479 static int gfar_set_rx_csum(struct net_device *dev, uint32_t data)
481 struct gfar_private *priv = netdev_priv(dev);
484 if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
487 if (dev->flags & IFF_UP) {
490 /* Halt TX and RX, and process the frames which
491 * have already been received */
492 spin_lock_irqsave(&priv->lock, flags);
494 gfar_clean_rx_ring(dev, priv->rx_ring_size);
495 spin_unlock_irqrestore(&priv->lock, flags);
497 /* Now we take down the rings to rebuild them */
501 priv->rx_csum_enable = data;
503 if (dev->flags & IFF_UP)
504 err = startup_gfar(dev);
509 static uint32_t gfar_get_rx_csum(struct net_device *dev)
511 struct gfar_private *priv = netdev_priv(dev);
513 if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
516 return priv->rx_csum_enable;
519 static int gfar_set_tx_csum(struct net_device *dev, uint32_t data)
522 struct gfar_private *priv = netdev_priv(dev);
524 if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
527 spin_lock_irqsave(&priv->lock, flags);
531 dev->features |= NETIF_F_IP_CSUM;
533 dev->features &= ~NETIF_F_IP_CSUM;
536 spin_unlock_irqrestore(&priv->lock, flags);
541 static uint32_t gfar_get_tx_csum(struct net_device *dev)
543 struct gfar_private *priv = netdev_priv(dev);
545 if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
548 return (dev->features & NETIF_F_IP_CSUM) != 0;
551 static uint32_t gfar_get_msglevel(struct net_device *dev)
553 struct gfar_private *priv = netdev_priv(dev);
554 return priv->msg_enable;
557 static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
559 struct gfar_private *priv = netdev_priv(dev);
560 priv->msg_enable = data;
564 struct ethtool_ops gfar_ethtool_ops = {
565 .get_settings = gfar_gsettings,
566 .set_settings = gfar_ssettings,
567 .get_drvinfo = gfar_gdrvinfo,
568 .get_regs_len = gfar_reglen,
569 .get_regs = gfar_get_regs,
570 .get_link = ethtool_op_get_link,
571 .get_coalesce = gfar_gcoalesce,
572 .set_coalesce = gfar_scoalesce,
573 .get_ringparam = gfar_gringparam,
574 .set_ringparam = gfar_sringparam,
575 .get_strings = gfar_gstrings,
576 .get_stats_count = gfar_stats_count,
577 .get_ethtool_stats = gfar_fill_stats,
578 .get_rx_csum = gfar_get_rx_csum,
579 .get_tx_csum = gfar_get_tx_csum,
580 .set_rx_csum = gfar_set_rx_csum,
581 .set_tx_csum = gfar_set_tx_csum,
582 .get_msglevel = gfar_get_msglevel,
583 .set_msglevel = gfar_set_msglevel,