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
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/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/interrupt.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/spinlock.h>
33 #include <asm/uaccess.h>
34 #include <linux/module.h>
35 #include <linux/crc32.h>
36 #include <asm/types.h>
37 #include <linux/ethtool.h>
38 #include <linux/mii.h>
39 #include <linux/phy.h>
43 extern void gfar_start(struct net_device *dev);
44 extern int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
46 #define GFAR_MAX_COAL_USECS 0xffff
47 #define GFAR_MAX_COAL_FRAMES 0xff
48 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
50 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
51 static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
52 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
53 static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
54 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
55 static void gfar_gdrvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo);
57 static char stat_gstrings[][ETH_GSTRING_LEN] = {
58 "rx-dropped-by-kernel",
59 "rx-large-frame-errors",
60 "rx-short-frame-errors",
61 "rx-non-octet-errors",
66 "rx-truncated-frames",
70 "rx-skb-missing-errors",
73 "tx-rx-65-127-frames",
74 "tx-rx-128-255-frames",
75 "tx-rx-256-511-frames",
76 "tx-rx-512-1023-frames",
77 "tx-rx-1024-1518-frames",
78 "tx-rx-1519-1522-good-vlan",
82 "receive-multicast-packet",
83 "receive-broadcast-packet",
84 "rx-control-frame-packets",
85 "rx-pause-frame-packets",
88 "rx-frame-length-error",
90 "rx-carrier-sense-error",
91 "rx-undersize-packets",
92 "rx-oversize-packets",
93 "rx-fragmented-frames",
98 "tx-multicast-packets",
99 "tx-broadcast-packets",
100 "tx-pause-control-frames",
101 "tx-deferral-packets",
102 "tx-excessive-deferral-packets",
103 "tx-single-collision-packets",
104 "tx-multiple-collision-packets",
105 "tx-late-collision-packets",
106 "tx-excessive-collision-packets",
107 "tx-total-collision",
113 "tx-oversize-frames",
114 "tx-undersize-frames",
115 "tx-fragmented-frames",
118 /* Fill in a buffer with the strings which correspond to the
120 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
122 struct gfar_private *priv = netdev_priv(dev);
124 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
125 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
127 memcpy(buf, stat_gstrings,
128 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
131 /* Fill in an array of 64-bit statistics from various sources.
132 * This array will be appended to the end of the ethtool_stats
133 * structure, and returned to user space
135 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy, u64 * buf)
138 struct gfar_private *priv = netdev_priv(dev);
139 u64 *extra = (u64 *) & priv->extra_stats;
141 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
142 u32 __iomem *rmon = (u32 __iomem *) & priv->regs->rmon;
143 struct gfar_stats *stats = (struct gfar_stats *) buf;
145 for (i = 0; i < GFAR_RMON_LEN; i++)
146 stats->rmon[i] = (u64) gfar_read(&rmon[i]);
148 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
149 stats->extra[i] = extra[i];
151 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
155 static int gfar_sset_count(struct net_device *dev, int sset)
157 struct gfar_private *priv = netdev_priv(dev);
161 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
162 return GFAR_STATS_LEN;
164 return GFAR_EXTRA_STATS_LEN;
170 /* Fills in the drvinfo structure with some basic info */
171 static void gfar_gdrvinfo(struct net_device *dev, struct
172 ethtool_drvinfo *drvinfo)
174 strncpy(drvinfo->driver, DRV_NAME, GFAR_INFOSTR_LEN);
175 strncpy(drvinfo->version, gfar_driver_version, GFAR_INFOSTR_LEN);
176 strncpy(drvinfo->fw_version, "N/A", GFAR_INFOSTR_LEN);
177 strncpy(drvinfo->bus_info, "N/A", GFAR_INFOSTR_LEN);
178 drvinfo->regdump_len = 0;
179 drvinfo->eedump_len = 0;
183 static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
185 struct gfar_private *priv = netdev_priv(dev);
186 struct phy_device *phydev = priv->phydev;
191 return phy_ethtool_sset(phydev, cmd);
195 /* Return the current settings in the ethtool_cmd structure */
196 static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
198 struct gfar_private *priv = netdev_priv(dev);
199 struct phy_device *phydev = priv->phydev;
204 cmd->maxtxpkt = get_icft_value(priv->txic);
205 cmd->maxrxpkt = get_icft_value(priv->rxic);
207 return phy_ethtool_gset(phydev, cmd);
210 /* Return the length of the register structure */
211 static int gfar_reglen(struct net_device *dev)
213 return sizeof (struct gfar);
216 /* Return a dump of the GFAR register space */
217 static void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
220 struct gfar_private *priv = netdev_priv(dev);
221 u32 __iomem *theregs = (u32 __iomem *) priv->regs;
222 u32 *buf = (u32 *) regbuf;
224 for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
225 buf[i] = gfar_read(&theregs[i]);
228 /* Convert microseconds to ethernet clock ticks, which changes
229 * depending on what speed the controller is running at */
230 static unsigned int gfar_usecs2ticks(struct gfar_private *priv, unsigned int usecs)
234 /* The timer is different, depending on the interface speed */
235 switch (priv->phydev->speed) {
237 count = GFAR_GBIT_TIME;
240 count = GFAR_100_TIME;
244 count = GFAR_10_TIME;
248 /* Make sure we return a number greater than 0
250 return ((usecs * 1000 + count - 1) / count);
253 /* Convert ethernet clock ticks to microseconds */
254 static unsigned int gfar_ticks2usecs(struct gfar_private *priv, unsigned int ticks)
258 /* The timer is different, depending on the interface speed */
259 switch (priv->phydev->speed) {
261 count = GFAR_GBIT_TIME;
264 count = GFAR_100_TIME;
268 count = GFAR_10_TIME;
272 /* Make sure we return a number greater than 0 */
273 /* if ticks is > 0 */
274 return ((ticks * count) / 1000);
277 /* Get the coalescing parameters, and put them in the cvals
279 static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
281 struct gfar_private *priv = netdev_priv(dev);
282 unsigned long rxtime;
283 unsigned long rxcount;
284 unsigned long txtime;
285 unsigned long txcount;
287 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
290 if (NULL == priv->phydev)
293 rxtime = get_ictt_value(priv->rxic);
294 rxcount = get_icft_value(priv->rxic);
295 txtime = get_ictt_value(priv->txic);
296 txcount = get_icft_value(priv->txic);;
297 cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, rxtime);
298 cvals->rx_max_coalesced_frames = rxcount;
300 cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, txtime);
301 cvals->tx_max_coalesced_frames = txcount;
303 cvals->use_adaptive_rx_coalesce = 0;
304 cvals->use_adaptive_tx_coalesce = 0;
306 cvals->pkt_rate_low = 0;
307 cvals->rx_coalesce_usecs_low = 0;
308 cvals->rx_max_coalesced_frames_low = 0;
309 cvals->tx_coalesce_usecs_low = 0;
310 cvals->tx_max_coalesced_frames_low = 0;
312 /* When the packet rate is below pkt_rate_high but above
313 * pkt_rate_low (both measured in packets per second) the
314 * normal {rx,tx}_* coalescing parameters are used.
317 /* When the packet rate is (measured in packets per second)
318 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
321 cvals->pkt_rate_high = 0;
322 cvals->rx_coalesce_usecs_high = 0;
323 cvals->rx_max_coalesced_frames_high = 0;
324 cvals->tx_coalesce_usecs_high = 0;
325 cvals->tx_max_coalesced_frames_high = 0;
327 /* How often to do adaptive coalescing packet rate sampling,
328 * measured in seconds. Must not be zero.
330 cvals->rate_sample_interval = 0;
335 /* Change the coalescing values.
336 * Both cvals->*_usecs and cvals->*_frames have to be > 0
337 * in order for coalescing to be active
339 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
341 struct gfar_private *priv = netdev_priv(dev);
343 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
346 /* Set up rx coalescing */
347 if ((cvals->rx_coalesce_usecs == 0) ||
348 (cvals->rx_max_coalesced_frames == 0))
349 priv->rxcoalescing = 0;
351 priv->rxcoalescing = 1;
353 if (NULL == priv->phydev)
356 /* Check the bounds of the values */
357 if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
358 pr_info("Coalescing is limited to %d microseconds\n",
359 GFAR_MAX_COAL_USECS);
363 if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
364 pr_info("Coalescing is limited to %d frames\n",
365 GFAR_MAX_COAL_FRAMES);
369 priv->rxic = mk_ic_value(
370 gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs),
371 cvals->rx_max_coalesced_frames);
373 /* Set up tx coalescing */
374 if ((cvals->tx_coalesce_usecs == 0) ||
375 (cvals->tx_max_coalesced_frames == 0))
376 priv->txcoalescing = 0;
378 priv->txcoalescing = 1;
380 /* Check the bounds of the values */
381 if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
382 pr_info("Coalescing is limited to %d microseconds\n",
383 GFAR_MAX_COAL_USECS);
387 if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
388 pr_info("Coalescing is limited to %d frames\n",
389 GFAR_MAX_COAL_FRAMES);
393 priv->txic = mk_ic_value(
394 gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs),
395 cvals->tx_max_coalesced_frames);
397 gfar_write(&priv->regs->rxic, 0);
398 if (priv->rxcoalescing)
399 gfar_write(&priv->regs->rxic, priv->rxic);
401 gfar_write(&priv->regs->txic, 0);
402 if (priv->txcoalescing)
403 gfar_write(&priv->regs->txic, priv->txic);
408 /* Fills in rvals with the current ring parameters. Currently,
409 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
410 * jumbo are ignored by the driver */
411 static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
413 struct gfar_private *priv = netdev_priv(dev);
415 rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
416 rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
417 rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
418 rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
420 /* Values changeable by the user. The valid values are
421 * in the range 1 to the "*_max_pending" counterpart above.
423 rvals->rx_pending = priv->rx_ring_size;
424 rvals->rx_mini_pending = priv->rx_ring_size;
425 rvals->rx_jumbo_pending = priv->rx_ring_size;
426 rvals->tx_pending = priv->tx_ring_size;
429 /* Change the current ring parameters, stopping the controller if
430 * necessary so that we don't mess things up while we're in
431 * motion. We wait for the ring to be clean before reallocating
433 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
435 struct gfar_private *priv = netdev_priv(dev);
438 if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
441 if (!is_power_of_2(rvals->rx_pending)) {
442 printk("%s: Ring sizes must be a power of 2\n",
447 if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
450 if (!is_power_of_2(rvals->tx_pending)) {
451 printk("%s: Ring sizes must be a power of 2\n",
456 if (dev->flags & IFF_UP) {
459 /* Halt TX and RX, and process the frames which
460 * have already been received */
461 spin_lock_irqsave(&priv->txlock, flags);
462 spin_lock(&priv->rxlock);
466 spin_unlock(&priv->rxlock);
467 spin_unlock_irqrestore(&priv->txlock, flags);
469 gfar_clean_rx_ring(dev, priv->rx_ring_size);
471 /* Now we take down the rings to rebuild them */
475 /* Change the size */
476 priv->rx_ring_size = rvals->rx_pending;
477 priv->tx_ring_size = rvals->tx_pending;
478 priv->num_txbdfree = priv->tx_ring_size;
480 /* Rebuild the rings with the new size */
481 if (dev->flags & IFF_UP) {
482 err = startup_gfar(dev);
483 netif_wake_queue(dev);
488 static int gfar_set_rx_csum(struct net_device *dev, uint32_t data)
490 struct gfar_private *priv = netdev_priv(dev);
494 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
497 if (dev->flags & IFF_UP) {
498 /* Halt TX and RX, and process the frames which
499 * have already been received */
500 spin_lock_irqsave(&priv->txlock, flags);
501 spin_lock(&priv->rxlock);
505 spin_unlock(&priv->rxlock);
506 spin_unlock_irqrestore(&priv->txlock, flags);
508 gfar_clean_rx_ring(dev, priv->rx_ring_size);
510 /* Now we take down the rings to rebuild them */
514 spin_lock_irqsave(&priv->bflock, flags);
515 priv->rx_csum_enable = data;
516 spin_unlock_irqrestore(&priv->bflock, flags);
518 if (dev->flags & IFF_UP) {
519 err = startup_gfar(dev);
520 netif_wake_queue(dev);
525 static uint32_t gfar_get_rx_csum(struct net_device *dev)
527 struct gfar_private *priv = netdev_priv(dev);
529 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
532 return priv->rx_csum_enable;
535 static int gfar_set_tx_csum(struct net_device *dev, uint32_t data)
537 struct gfar_private *priv = netdev_priv(dev);
539 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
542 netif_tx_lock_bh(dev);
545 dev->features |= NETIF_F_IP_CSUM;
547 dev->features &= ~NETIF_F_IP_CSUM;
549 netif_tx_unlock_bh(dev);
554 static uint32_t gfar_get_tx_csum(struct net_device *dev)
556 struct gfar_private *priv = netdev_priv(dev);
558 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
561 return (dev->features & NETIF_F_IP_CSUM) != 0;
564 static uint32_t gfar_get_msglevel(struct net_device *dev)
566 struct gfar_private *priv = netdev_priv(dev);
567 return priv->msg_enable;
570 static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
572 struct gfar_private *priv = netdev_priv(dev);
573 priv->msg_enable = data;
577 static void gfar_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
579 struct gfar_private *priv = netdev_priv(dev);
581 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) {
582 wol->supported = WAKE_MAGIC;
583 wol->wolopts = priv->wol_en ? WAKE_MAGIC : 0;
585 wol->supported = wol->wolopts = 0;
589 static int gfar_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
591 struct gfar_private *priv = netdev_priv(dev);
594 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
598 if (wol->wolopts & ~WAKE_MAGIC)
601 spin_lock_irqsave(&priv->bflock, flags);
602 priv->wol_en = wol->wolopts & WAKE_MAGIC ? 1 : 0;
603 device_set_wakeup_enable(&dev->dev, priv->wol_en);
604 spin_unlock_irqrestore(&priv->bflock, flags);
610 const struct ethtool_ops gfar_ethtool_ops = {
611 .get_settings = gfar_gsettings,
612 .set_settings = gfar_ssettings,
613 .get_drvinfo = gfar_gdrvinfo,
614 .get_regs_len = gfar_reglen,
615 .get_regs = gfar_get_regs,
616 .get_link = ethtool_op_get_link,
617 .get_coalesce = gfar_gcoalesce,
618 .set_coalesce = gfar_scoalesce,
619 .get_ringparam = gfar_gringparam,
620 .set_ringparam = gfar_sringparam,
621 .get_strings = gfar_gstrings,
622 .get_sset_count = gfar_sset_count,
623 .get_ethtool_stats = gfar_fill_stats,
624 .get_rx_csum = gfar_get_rx_csum,
625 .get_tx_csum = gfar_get_tx_csum,
626 .set_rx_csum = gfar_set_rx_csum,
627 .set_tx_csum = gfar_set_tx_csum,
628 .set_sg = ethtool_op_set_sg,
629 .get_msglevel = gfar_get_msglevel,
630 .set_msglevel = gfar_set_msglevel,
632 .get_wol = gfar_get_wol,
633 .set_wol = gfar_set_wol,