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>
45 #define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
47 extern int startup_gfar(struct net_device
*dev
);
48 extern void stop_gfar(struct net_device
*dev
);
49 extern void gfar_receive(int irq
, void *dev_id
, struct pt_regs
*regs
);
51 void gfar_fill_stats(struct net_device
*dev
, struct ethtool_stats
*dummy
,
53 void gfar_gstrings(struct net_device
*dev
, u32 stringset
, u8
* buf
);
54 int gfar_gcoalesce(struct net_device
*dev
, struct ethtool_coalesce
*cvals
);
55 int gfar_scoalesce(struct net_device
*dev
, struct ethtool_coalesce
*cvals
);
56 void gfar_gringparam(struct net_device
*dev
, struct ethtool_ringparam
*rvals
);
57 int gfar_sringparam(struct net_device
*dev
, struct ethtool_ringparam
*rvals
);
58 void gfar_gdrvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*drvinfo
);
60 static char stat_gstrings
[][ETH_GSTRING_LEN
] = {
61 "rx-dropped-by-kernel",
62 "rx-large-frame-errors",
63 "rx-short-frame-errors",
64 "rx-non-octet-errors",
69 "rx-truncated-frames",
73 "rx-skb-missing-errors",
76 "tx-rx-65-127-frames",
77 "tx-rx-128-255-frames",
78 "tx-rx-256-511-frames",
79 "tx-rx-512-1023-frames",
80 "tx-rx-1024-1518-frames",
81 "tx-rx-1519-1522-good-vlan",
85 "receive-multicast-packet",
86 "receive-broadcast-packet",
87 "rx-control-frame-packets",
88 "rx-pause-frame-packets",
91 "rx-frame-length-error",
93 "rx-carrier-sense-error",
94 "rx-undersize-packets",
95 "rx-oversize-packets",
96 "rx-fragmented-frames",
101 "tx-multicast-packets",
102 "tx-broadcast-packets",
103 "tx-pause-control-frames",
104 "tx-deferral-packets",
105 "tx-excessive-deferral-packets",
106 "tx-single-collision-packets",
107 "tx-multiple-collision-packets",
108 "tx-late-collision-packets",
109 "tx-excessive-collision-packets",
110 "tx-total-collision",
116 "tx-oversize-frames",
117 "tx-undersize-frames",
118 "tx-fragmented-frames",
121 /* Fill in an array of 64-bit statistics from various sources.
122 * This array will be appended to the end of the ethtool_stats
123 * structure, and returned to user space
125 void gfar_fill_stats(struct net_device
*dev
, struct ethtool_stats
*dummy
, u64
* buf
)
128 struct gfar_private
*priv
= netdev_priv(dev
);
129 u32
*rmon
= (u32
*) & priv
->regs
->rmon
;
130 u64
*extra
= (u64
*) & priv
->extra_stats
;
131 struct gfar_stats
*stats
= (struct gfar_stats
*) buf
;
133 for (i
= 0; i
< GFAR_RMON_LEN
; i
++) {
134 stats
->rmon
[i
] = (u64
) (rmon
[i
]);
137 for (i
= 0; i
< GFAR_EXTRA_STATS_LEN
; i
++) {
138 stats
->extra
[i
] = extra
[i
];
142 /* Returns the number of stats (and their corresponding strings) */
143 int gfar_stats_count(struct net_device
*dev
)
145 return GFAR_STATS_LEN
;
148 void gfar_gstrings_normon(struct net_device
*dev
, u32 stringset
, u8
* buf
)
150 memcpy(buf
, stat_gstrings
, GFAR_EXTRA_STATS_LEN
* ETH_GSTRING_LEN
);
153 void gfar_fill_stats_normon(struct net_device
*dev
,
154 struct ethtool_stats
*dummy
, u64
* buf
)
157 struct gfar_private
*priv
= netdev_priv(dev
);
158 u64
*extra
= (u64
*) & priv
->extra_stats
;
160 for (i
= 0; i
< GFAR_EXTRA_STATS_LEN
; i
++) {
166 int gfar_stats_count_normon(struct net_device
*dev
)
168 return GFAR_EXTRA_STATS_LEN
;
170 /* Fills in the drvinfo structure with some basic info */
171 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
->n_stats
= GFAR_STATS_LEN
;
179 drvinfo
->testinfo_len
= 0;
180 drvinfo
->regdump_len
= 0;
181 drvinfo
->eedump_len
= 0;
184 /* Return the current settings in the ethtool_cmd structure */
185 int gfar_gsettings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
187 struct gfar_private
*priv
= netdev_priv(dev
);
188 uint gigabit_support
=
189 priv
->einfo
->flags
& GFAR_HAS_GIGABIT
? SUPPORTED_1000baseT_Full
: 0;
190 uint gigabit_advert
=
191 priv
->einfo
->flags
& GFAR_HAS_GIGABIT
? ADVERTISED_1000baseT_Full
: 0;
193 cmd
->supported
= (SUPPORTED_10baseT_Half
194 | SUPPORTED_100baseT_Half
195 | SUPPORTED_100baseT_Full
196 | gigabit_support
| SUPPORTED_Autoneg
);
198 /* For now, we always advertise everything */
199 cmd
->advertising
= (ADVERTISED_10baseT_Half
200 | ADVERTISED_100baseT_Half
201 | ADVERTISED_100baseT_Full
202 | gigabit_advert
| ADVERTISED_Autoneg
);
204 cmd
->speed
= priv
->mii_info
->speed
;
205 cmd
->duplex
= priv
->mii_info
->duplex
;
206 cmd
->port
= PORT_MII
;
207 cmd
->phy_address
= priv
->mii_info
->mii_id
;
208 cmd
->transceiver
= XCVR_EXTERNAL
;
209 cmd
->autoneg
= AUTONEG_ENABLE
;
210 cmd
->maxtxpkt
= priv
->txcount
;
211 cmd
->maxrxpkt
= priv
->rxcount
;
216 /* Return the length of the register structure */
217 int gfar_reglen(struct net_device
*dev
)
219 return sizeof (struct gfar
);
222 /* Return a dump of the GFAR register space */
223 void gfar_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *regbuf
)
226 struct gfar_private
*priv
= netdev_priv(dev
);
227 u32
*theregs
= (u32
*) priv
->regs
;
228 u32
*buf
= (u32
*) regbuf
;
230 for (i
= 0; i
< sizeof (struct gfar
) / sizeof (u32
); i
++)
234 /* Fill in a buffer with the strings which correspond to the
236 void gfar_gstrings(struct net_device
*dev
, u32 stringset
, u8
* buf
)
238 memcpy(buf
, stat_gstrings
, GFAR_STATS_LEN
* ETH_GSTRING_LEN
);
241 /* Convert microseconds to ethernet clock ticks, which changes
242 * depending on what speed the controller is running at */
243 static unsigned int gfar_usecs2ticks(struct gfar_private
*priv
, unsigned int usecs
)
247 /* The timer is different, depending on the interface speed */
248 switch (priv
->mii_info
->speed
) {
250 count
= GFAR_GBIT_TIME
;
253 count
= GFAR_100_TIME
;
257 count
= GFAR_10_TIME
;
261 /* Make sure we return a number greater than 0
263 return ((usecs
* 1000 + count
- 1) / count
);
266 /* Convert ethernet clock ticks to microseconds */
267 static unsigned int gfar_ticks2usecs(struct gfar_private
*priv
, unsigned int ticks
)
271 /* The timer is different, depending on the interface speed */
272 switch (priv
->mii_info
->speed
) {
274 count
= GFAR_GBIT_TIME
;
277 count
= GFAR_100_TIME
;
281 count
= GFAR_10_TIME
;
285 /* Make sure we return a number greater than 0 */
286 /* if ticks is > 0 */
287 return ((ticks
* count
) / 1000);
290 /* Get the coalescing parameters, and put them in the cvals
292 int gfar_gcoalesce(struct net_device
*dev
, struct ethtool_coalesce
*cvals
)
294 struct gfar_private
*priv
= netdev_priv(dev
);
296 cvals
->rx_coalesce_usecs
= gfar_ticks2usecs(priv
, priv
->rxtime
);
297 cvals
->rx_max_coalesced_frames
= priv
->rxcount
;
299 cvals
->tx_coalesce_usecs
= gfar_ticks2usecs(priv
, priv
->txtime
);
300 cvals
->tx_max_coalesced_frames
= priv
->txcount
;
302 cvals
->use_adaptive_rx_coalesce
= 0;
303 cvals
->use_adaptive_tx_coalesce
= 0;
305 cvals
->pkt_rate_low
= 0;
306 cvals
->rx_coalesce_usecs_low
= 0;
307 cvals
->rx_max_coalesced_frames_low
= 0;
308 cvals
->tx_coalesce_usecs_low
= 0;
309 cvals
->tx_max_coalesced_frames_low
= 0;
311 /* When the packet rate is below pkt_rate_high but above
312 * pkt_rate_low (both measured in packets per second) the
313 * normal {rx,tx}_* coalescing parameters are used.
316 /* When the packet rate is (measured in packets per second)
317 * is above pkt_rate_high, the {rx,tx}_*_high parameters are
320 cvals
->pkt_rate_high
= 0;
321 cvals
->rx_coalesce_usecs_high
= 0;
322 cvals
->rx_max_coalesced_frames_high
= 0;
323 cvals
->tx_coalesce_usecs_high
= 0;
324 cvals
->tx_max_coalesced_frames_high
= 0;
326 /* How often to do adaptive coalescing packet rate sampling,
327 * measured in seconds. Must not be zero.
329 cvals
->rate_sample_interval
= 0;
334 /* Change the coalescing values.
335 * Both cvals->*_usecs and cvals->*_frames have to be > 0
336 * in order for coalescing to be active
338 int gfar_scoalesce(struct net_device
*dev
, struct ethtool_coalesce
*cvals
)
340 struct gfar_private
*priv
= netdev_priv(dev
);
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 priv
->rxtime
= gfar_usecs2ticks(priv
, cvals
->rx_coalesce_usecs
);
350 priv
->rxcount
= cvals
->rx_max_coalesced_frames
;
352 /* Set up tx coalescing */
353 if ((cvals
->tx_coalesce_usecs
== 0) ||
354 (cvals
->tx_max_coalesced_frames
== 0))
355 priv
->txcoalescing
= 0;
357 priv
->txcoalescing
= 1;
359 priv
->txtime
= gfar_usecs2ticks(priv
, cvals
->tx_coalesce_usecs
);
360 priv
->txcount
= cvals
->tx_max_coalesced_frames
;
362 if (priv
->rxcoalescing
)
363 gfar_write(&priv
->regs
->rxic
,
364 mk_ic_value(priv
->rxcount
, priv
->rxtime
));
366 gfar_write(&priv
->regs
->rxic
, 0);
368 if (priv
->txcoalescing
)
369 gfar_write(&priv
->regs
->txic
,
370 mk_ic_value(priv
->txcount
, priv
->txtime
));
372 gfar_write(&priv
->regs
->txic
, 0);
377 /* Fills in rvals with the current ring parameters. Currently,
378 * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
379 * jumbo are ignored by the driver */
380 void gfar_gringparam(struct net_device
*dev
, struct ethtool_ringparam
*rvals
)
382 struct gfar_private
*priv
= netdev_priv(dev
);
384 rvals
->rx_max_pending
= GFAR_RX_MAX_RING_SIZE
;
385 rvals
->rx_mini_max_pending
= GFAR_RX_MAX_RING_SIZE
;
386 rvals
->rx_jumbo_max_pending
= GFAR_RX_MAX_RING_SIZE
;
387 rvals
->tx_max_pending
= GFAR_TX_MAX_RING_SIZE
;
389 /* Values changeable by the user. The valid values are
390 * in the range 1 to the "*_max_pending" counterpart above.
392 rvals
->rx_pending
= priv
->rx_ring_size
;
393 rvals
->rx_mini_pending
= priv
->rx_ring_size
;
394 rvals
->rx_jumbo_pending
= priv
->rx_ring_size
;
395 rvals
->tx_pending
= priv
->tx_ring_size
;
398 /* Change the current ring parameters, stopping the controller if
399 * necessary so that we don't mess things up while we're in
400 * motion. We wait for the ring to be clean before reallocating
402 int gfar_sringparam(struct net_device
*dev
, struct ethtool_ringparam
*rvals
)
405 struct gfar_private
*priv
= netdev_priv(dev
);
408 if (rvals
->rx_pending
> GFAR_RX_MAX_RING_SIZE
)
411 if (!is_power_of_2(rvals
->rx_pending
)) {
412 printk("%s: Ring sizes must be a power of 2\n",
417 if (rvals
->tx_pending
> GFAR_TX_MAX_RING_SIZE
)
420 if (!is_power_of_2(rvals
->tx_pending
)) {
421 printk("%s: Ring sizes must be a power of 2\n",
426 /* Stop the controller so we don't rx any more frames */
427 /* But first, make sure we clear the bits */
428 tempval
= gfar_read(&priv
->regs
->dmactrl
);
429 tempval
&= ~(DMACTRL_GRS
| DMACTRL_GTS
);
430 gfar_write(&priv
->regs
->dmactrl
, tempval
);
432 tempval
= gfar_read(&priv
->regs
->dmactrl
);
433 tempval
|= (DMACTRL_GRS
| DMACTRL_GTS
);
434 gfar_write(&priv
->regs
->dmactrl
, tempval
);
436 while (!(gfar_read(&priv
->regs
->ievent
) & (IEVENT_GRSC
| IEVENT_GTSC
)))
439 /* Note that rx is not clean right now */
442 if (dev
->flags
& IFF_UP
) {
443 /* Tell the driver to process the rest of the frames */
444 gfar_receive(0, (void *) dev
, NULL
);
446 /* Now wait for it to be done */
447 wait_event_interruptible(priv
->rxcleanupq
, priv
->rxclean
);
449 /* Ok, all packets have been handled. Now we bring it down,
450 * change the ring size, and bring it up */
455 priv
->rx_ring_size
= rvals
->rx_pending
;
456 priv
->tx_ring_size
= rvals
->tx_pending
;
458 if (dev
->flags
& IFF_UP
)
459 err
= startup_gfar(dev
);
464 struct ethtool_ops gfar_ethtool_ops
= {
465 .get_settings
= gfar_gsettings
,
466 .get_drvinfo
= gfar_gdrvinfo
,
467 .get_regs_len
= gfar_reglen
,
468 .get_regs
= gfar_get_regs
,
469 .get_link
= ethtool_op_get_link
,
470 .get_coalesce
= gfar_gcoalesce
,
471 .set_coalesce
= gfar_scoalesce
,
472 .get_ringparam
= gfar_gringparam
,
473 .set_ringparam
= gfar_sringparam
,
474 .get_strings
= gfar_gstrings
,
475 .get_stats_count
= gfar_stats_count
,
476 .get_ethtool_stats
= gfar_fill_stats
,
479 struct ethtool_ops gfar_normon_nocoalesce_ethtool_ops
= {
480 .get_settings
= gfar_gsettings
,
481 .get_drvinfo
= gfar_gdrvinfo
,
482 .get_regs_len
= gfar_reglen
,
483 .get_regs
= gfar_get_regs
,
484 .get_link
= ethtool_op_get_link
,
485 .get_ringparam
= gfar_gringparam
,
486 .set_ringparam
= gfar_sringparam
,
487 .get_strings
= gfar_gstrings_normon
,
488 .get_stats_count
= gfar_stats_count_normon
,
489 .get_ethtool_stats
= gfar_fill_stats_normon
,
492 struct ethtool_ops gfar_nocoalesce_ethtool_ops
= {
493 .get_settings
= gfar_gsettings
,
494 .get_drvinfo
= gfar_gdrvinfo
,
495 .get_regs_len
= gfar_reglen
,
496 .get_regs
= gfar_get_regs
,
497 .get_link
= ethtool_op_get_link
,
498 .get_ringparam
= gfar_gringparam
,
499 .set_ringparam
= gfar_sringparam
,
500 .get_strings
= gfar_gstrings
,
501 .get_stats_count
= gfar_stats_count
,
502 .get_ethtool_stats
= gfar_fill_stats
,
505 struct ethtool_ops gfar_normon_ethtool_ops
= {
506 .get_settings
= gfar_gsettings
,
507 .get_drvinfo
= gfar_gdrvinfo
,
508 .get_regs_len
= gfar_reglen
,
509 .get_regs
= gfar_get_regs
,
510 .get_link
= ethtool_op_get_link
,
511 .get_coalesce
= gfar_gcoalesce
,
512 .set_coalesce
= gfar_scoalesce
,
513 .get_ringparam
= gfar_gringparam
,
514 .set_ringparam
= gfar_sringparam
,
515 .get_strings
= gfar_gstrings_normon
,
516 .get_stats_count
= gfar_stats_count_normon
,
517 .get_ethtool_stats
= gfar_fill_stats_normon
,
520 struct ethtool_ops
*gfar_op_array
[] = {
522 &gfar_normon_ethtool_ops
,
523 &gfar_nocoalesce_ethtool_ops
,
524 &gfar_normon_nocoalesce_ethtool_ops