This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / net / gianfar_ethtool.c
1 /*
2  * drivers/net/gianfar_ethtool.c
3  *
4  * Gianfar Ethernet Driver
5  * Ethtool support for Gianfar Enet
6  * Based on e1000 ethtool support
7  *
8  * Author: Andy Fleming
9  * Maintainer: Kumar Gala (kumar.gala@freescale.com)
10  *
11  * Copyright 2004 Freescale Semiconductor, Inc
12  *
13  * This software may be used and distributed according to 
14  * the terms of the GNU Public License, Version 2, incorporated herein 
15  * by reference.
16  */
17
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>
31 #include <linux/mm.h>
32
33 #include <asm/io.h>
34 #include <asm/irq.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
43 #include "gianfar.h"
44
45 #define is_power_of_2(x)        ((x) != 0 && (((x) & ((x) - 1)) == 0))
46
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);
50
51 void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
52                      u64 * buf);
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);
59
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",
65         "RX CRC Errors",
66         "RX Overrun Errors",
67         "RX Busy Errors",
68         "RX Babbling Errors",
69         "RX Truncated Frames",
70         "Ethernet Bus Error",
71         "TX Babbling Errors",
72         "TX Underrun Errors",
73         "RX SKB Missing Errors",
74         "TX Timeout Errors",
75         "tx&rx 64B frames",
76         "tx&rx 65-127B frames",
77         "tx&rx 128-255B frames",
78         "tx&rx 256-511B frames",
79         "tx&rx 512-1023B frames",
80         "tx&rx 1024-1518B frames",
81         "tx&rx 1519-1522B Good VLAN",
82         "RX bytes",
83         "RX Packets",
84         "RX FCS Errors",
85         "Receive Multicast Packet",
86         "Receive Broadcast Packet",
87         "RX Control Frame Packets",
88         "RX Pause Frame Packets",
89         "RX Unknown OP Code",
90         "RX Alignment Error",
91         "RX Frame Length Error",
92         "RX Code Error",
93         "RX Carrier Sense Error",
94         "RX Undersize Packets",
95         "RX Oversize Packets",
96         "RX Fragmented Frames",
97         "RX Jabber Frames",
98         "RX Dropped Frames",
99         "TX Byte Counter",
100         "TX Packets",
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",
111         "RESERVED",
112         "TX Dropped Frames",
113         "TX Jabber Frames",
114         "TX FCS Errors",
115         "TX Control Frames",
116         "TX Oversize Frames",
117         "TX Undersize Frames",
118         "TX Fragmented Frames",
119 };
120
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
124  */
125 void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy, u64 * buf)
126 {
127         int i;
128         struct gfar_private *priv = (struct gfar_private *) dev->priv;
129         u32 *rmon = (u32 *) & priv->regs->rmon;
130         u64 *extra = (u64 *) & priv->extra_stats;
131         struct gfar_stats *stats = (struct gfar_stats *) buf;
132
133         for (i = 0; i < GFAR_RMON_LEN; i++) {
134                 stats->rmon[i] = (u64) (rmon[i]);
135         }
136
137         for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++) {
138                 stats->extra[i] = extra[i];
139         }
140 }
141
142 /* Returns the number of stats (and their corresponding strings) */
143 int gfar_stats_count(struct net_device *dev)
144 {
145         return GFAR_STATS_LEN;
146 }
147
148 void gfar_gstrings_normon(struct net_device *dev, u32 stringset, u8 * buf)
149 {
150         memcpy(buf, stat_gstrings, GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
151 }
152
153 void gfar_fill_stats_normon(struct net_device *dev, 
154                 struct ethtool_stats *dummy, u64 * buf)
155 {
156         int i;
157         struct gfar_private *priv = (struct gfar_private *) dev->priv;
158         u64 *extra = (u64 *) & priv->extra_stats;
159
160         for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++) {
161                 buf[i] = extra[i];
162         }
163 }
164
165
166 int gfar_stats_count_normon(struct net_device *dev)
167 {
168         return GFAR_EXTRA_STATS_LEN;
169 }
170 /* Fills in the drvinfo structure with some basic info */
171 void gfar_gdrvinfo(struct net_device *dev, struct
172               ethtool_drvinfo *drvinfo)
173 {
174         strncpy(drvinfo->driver, gfar_driver_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;
182 }
183
184 /* Return the current settings in the ethtool_cmd structure */
185 int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
186 {
187         struct gfar_private *priv = (struct gfar_private *) dev->priv;
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;
192
193         cmd->supported = (SUPPORTED_10baseT_Half
194                           | SUPPORTED_100baseT_Half
195                           | SUPPORTED_100baseT_Full
196                           | gigabit_support | SUPPORTED_Autoneg);
197
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);
203
204         cmd->speed = priv->speed;
205         cmd->duplex = priv->duplexity;
206         cmd->port = PORT_MII;
207         cmd->phy_address = priv->einfo->phyid;
208         cmd->transceiver = XCVR_EXTERNAL;
209         cmd->autoneg = AUTONEG_ENABLE;
210         cmd->maxtxpkt = priv->txcount;
211         cmd->maxrxpkt = priv->rxcount;
212
213         return 0;
214 }
215
216 /* Return the length of the register structure */
217 int gfar_reglen(struct net_device *dev)
218 {
219         return sizeof (struct gfar);
220 }
221
222 /* Return a dump of the GFAR register space */
223 void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
224 {
225         int i;
226         struct gfar_private *priv = (struct gfar_private *) dev->priv;
227         u32 *theregs = (u32 *) priv->regs;
228         u32 *buf = (u32 *) regbuf;
229
230         for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
231                 buf[i] = theregs[i];
232 }
233
234 /* Return the link state 1 is up, 0 is down */
235 u32 gfar_get_link(struct net_device *dev)
236 {
237         struct gfar_private *priv = (struct gfar_private *) dev->priv;
238         return (u32) priv->link;
239 }
240
241 /* Fill in a buffer with the strings which correspond to the
242  * stats */
243 void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
244 {
245         memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
246 }
247
248 /* Convert microseconds to ethernet clock ticks, which changes
249  * depending on what speed the controller is running at */
250 static unsigned int gfar_usecs2ticks(struct gfar_private *priv, unsigned int usecs)
251 {
252         unsigned int count;
253
254         /* The timer is different, depending on the interface speed */
255         switch (priv->speed) {
256         case 1000:
257                 count = GFAR_GBIT_TIME;
258                 break;
259         case 100:
260                 count = GFAR_100_TIME;
261                 break;
262         case 10:
263         default:
264                 count = GFAR_10_TIME;
265                 break;
266         }
267
268         /* Make sure we return a number greater than 0
269          * if usecs > 0 */
270         return ((usecs * 1000 + count - 1) / count);
271 }
272
273 /* Convert ethernet clock ticks to microseconds */
274 static unsigned int gfar_ticks2usecs(struct gfar_private *priv, unsigned int ticks)
275 {
276         unsigned int count;
277
278         /* The timer is different, depending on the interface speed */
279         switch (priv->speed) {
280         case 1000:
281                 count = GFAR_GBIT_TIME;
282                 break;
283         case 100:
284                 count = GFAR_100_TIME;
285                 break;
286         case 10:
287         default:
288                 count = GFAR_10_TIME;
289                 break;
290         }
291
292         /* Make sure we return a number greater than 0 */
293         /* if ticks is > 0 */
294         return ((ticks * count) / 1000);
295 }
296
297 /* Get the coalescing parameters, and put them in the cvals
298  * structure.  */
299 int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
300 {
301         struct gfar_private *priv = (struct gfar_private *) dev->priv;
302
303         cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, priv->rxtime);
304         cvals->rx_max_coalesced_frames = priv->rxcount;
305
306         cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, priv->txtime);
307         cvals->tx_max_coalesced_frames = priv->txcount;
308
309         cvals->use_adaptive_rx_coalesce = 0;
310         cvals->use_adaptive_tx_coalesce = 0;
311
312         cvals->pkt_rate_low = 0;
313         cvals->rx_coalesce_usecs_low = 0;
314         cvals->rx_max_coalesced_frames_low = 0;
315         cvals->tx_coalesce_usecs_low = 0;
316         cvals->tx_max_coalesced_frames_low = 0;
317
318         /* When the packet rate is below pkt_rate_high but above
319          * pkt_rate_low (both measured in packets per second) the
320          * normal {rx,tx}_* coalescing parameters are used.
321          */
322
323         /* When the packet rate is (measured in packets per second)
324          * is above pkt_rate_high, the {rx,tx}_*_high parameters are
325          * used.
326          */
327         cvals->pkt_rate_high = 0;
328         cvals->rx_coalesce_usecs_high = 0;
329         cvals->rx_max_coalesced_frames_high = 0;
330         cvals->tx_coalesce_usecs_high = 0;
331         cvals->tx_max_coalesced_frames_high = 0;
332
333         /* How often to do adaptive coalescing packet rate sampling,
334          * measured in seconds.  Must not be zero.
335          */
336         cvals->rate_sample_interval = 0;
337
338         return 0;
339 }
340
341 /* Change the coalescing values.
342  * Both cvals->*_usecs and cvals->*_frames have to be > 0
343  * in order for coalescing to be active
344  */
345 int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
346 {
347         struct gfar_private *priv = (struct gfar_private *) dev->priv;
348
349         /* Set up rx coalescing */
350         if ((cvals->rx_coalesce_usecs == 0) ||
351             (cvals->rx_max_coalesced_frames == 0))
352                 priv->rxcoalescing = 0;
353         else
354                 priv->rxcoalescing = 1;
355
356         priv->rxtime = gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs);
357         priv->rxcount = cvals->rx_max_coalesced_frames;
358
359         /* Set up tx coalescing */
360         if ((cvals->tx_coalesce_usecs == 0) ||
361             (cvals->tx_max_coalesced_frames == 0))
362                 priv->txcoalescing = 0;
363         else
364                 priv->txcoalescing = 1;
365
366         priv->txtime = gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs);
367         priv->txcount = cvals->tx_max_coalesced_frames;
368
369         if (priv->rxcoalescing)
370                 gfar_write(&priv->regs->rxic,
371                            mk_ic_value(priv->rxcount, priv->rxtime));
372         else
373                 gfar_write(&priv->regs->rxic, 0);
374
375         if (priv->txcoalescing)
376                 gfar_write(&priv->regs->txic,
377                            mk_ic_value(priv->txcount, priv->txtime));
378         else
379                 gfar_write(&priv->regs->txic, 0);
380
381         return 0;
382 }
383
384 /* Fills in rvals with the current ring parameters.  Currently,
385  * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
386  * jumbo are ignored by the driver */
387 void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
388 {
389         struct gfar_private *priv = (struct gfar_private *) dev->priv;
390
391         rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
392         rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
393         rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
394         rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
395
396         /* Values changeable by the user.  The valid values are
397          * in the range 1 to the "*_max_pending" counterpart above.
398          */
399         rvals->rx_pending = priv->rx_ring_size;
400         rvals->rx_mini_pending = priv->rx_ring_size;
401         rvals->rx_jumbo_pending = priv->rx_ring_size;
402         rvals->tx_pending = priv->tx_ring_size;
403 }
404
405 /* Change the current ring parameters, stopping the controller if
406  * necessary so that we don't mess things up while we're in
407  * motion.  We wait for the ring to be clean before reallocating
408  * the rings. */
409 int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
410 {
411         u32 tempval;
412         struct gfar_private *priv = (struct gfar_private *) dev->priv;
413         int err = 0;
414
415         if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
416                 return -EINVAL;
417
418         if (!is_power_of_2(rvals->rx_pending)) {
419                 printk("%s: Ring sizes must be a power of 2\n",
420                                 dev->name);
421                 return -EINVAL;
422         }
423
424         if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
425                 return -EINVAL;
426
427         if (!is_power_of_2(rvals->tx_pending)) {
428                 printk("%s: Ring sizes must be a power of 2\n",
429                                 dev->name);
430                 return -EINVAL;
431         }
432
433         /* Stop the controller so we don't rx any more frames */
434         /* But first, make sure we clear the bits */
435         tempval = gfar_read(&priv->regs->dmactrl);
436         tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
437         gfar_write(&priv->regs->dmactrl, tempval);
438
439         tempval = gfar_read(&priv->regs->dmactrl);
440         tempval |= (DMACTRL_GRS | DMACTRL_GTS);
441         gfar_write(&priv->regs->dmactrl, tempval);
442
443         while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
444                 cpu_relax();
445
446         /* Note that rx is not clean right now */
447         priv->rxclean = 0;
448
449         if (dev->flags & IFF_UP) {
450                 /* Tell the driver to process the rest of the frames */
451                 gfar_receive(0, (void *) dev, NULL);
452
453                 /* Now wait for it to be done */
454                 wait_event_interruptible(priv->rxcleanupq, priv->rxclean);
455
456                 /* Ok, all packets have been handled.  Now we bring it down,
457                  * change the ring size, and bring it up */
458
459                 stop_gfar(dev);
460         }
461
462         priv->rx_ring_size = rvals->rx_pending;
463         priv->tx_ring_size = rvals->tx_pending;
464
465         if (dev->flags & IFF_UP)
466                 err = startup_gfar(dev);
467
468         return err;
469 }
470
471 struct ethtool_ops gfar_ethtool_ops = {
472         .get_settings = gfar_gsettings,
473         .get_drvinfo = gfar_gdrvinfo,
474         .get_regs_len = gfar_reglen,
475         .get_regs = gfar_get_regs,
476         .get_link = gfar_get_link,
477         .get_coalesce = gfar_gcoalesce,
478         .set_coalesce = gfar_scoalesce,
479         .get_ringparam = gfar_gringparam,
480         .set_ringparam = gfar_sringparam,
481         .get_strings = gfar_gstrings,
482         .get_stats_count = gfar_stats_count,
483         .get_ethtool_stats = gfar_fill_stats,
484 };