Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.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 (c) 2003,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-64-frames",
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",
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 = 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;
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 = netdev_priv(dev);
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, 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;
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 = netdev_priv(dev);
188         uint gigabit_support = 
189                 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
190                         SUPPORTED_1000baseT_Full : 0;
191         uint gigabit_advert = 
192                 priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
193                         ADVERTISED_1000baseT_Full: 0;
194
195         cmd->supported = (SUPPORTED_10baseT_Half
196                           | SUPPORTED_100baseT_Half
197                           | SUPPORTED_100baseT_Full
198                           | gigabit_support | SUPPORTED_Autoneg);
199
200         /* For now, we always advertise everything */
201         cmd->advertising = (ADVERTISED_10baseT_Half
202                             | ADVERTISED_100baseT_Half
203                             | ADVERTISED_100baseT_Full
204                             | gigabit_advert | ADVERTISED_Autoneg);
205
206         cmd->speed = priv->mii_info->speed;
207         cmd->duplex = priv->mii_info->duplex;
208         cmd->port = PORT_MII;
209         cmd->phy_address = priv->mii_info->mii_id;
210         cmd->transceiver = XCVR_EXTERNAL;
211         cmd->autoneg = AUTONEG_ENABLE;
212         cmd->maxtxpkt = priv->txcount;
213         cmd->maxrxpkt = priv->rxcount;
214
215         return 0;
216 }
217
218 /* Return the length of the register structure */
219 int gfar_reglen(struct net_device *dev)
220 {
221         return sizeof (struct gfar);
222 }
223
224 /* Return a dump of the GFAR register space */
225 void gfar_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
226 {
227         int i;
228         struct gfar_private *priv = netdev_priv(dev);
229         u32 *theregs = (u32 *) priv->regs;
230         u32 *buf = (u32 *) regbuf;
231
232         for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
233                 buf[i] = theregs[i];
234 }
235
236 /* Fill in a buffer with the strings which correspond to the
237  * stats */
238 void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
239 {
240         memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
241 }
242
243 /* Convert microseconds to ethernet clock ticks, which changes
244  * depending on what speed the controller is running at */
245 static unsigned int gfar_usecs2ticks(struct gfar_private *priv, unsigned int usecs)
246 {
247         unsigned int count;
248
249         /* The timer is different, depending on the interface speed */
250         switch (priv->mii_info->speed) {
251         case 1000:
252                 count = GFAR_GBIT_TIME;
253                 break;
254         case 100:
255                 count = GFAR_100_TIME;
256                 break;
257         case 10:
258         default:
259                 count = GFAR_10_TIME;
260                 break;
261         }
262
263         /* Make sure we return a number greater than 0
264          * if usecs > 0 */
265         return ((usecs * 1000 + count - 1) / count);
266 }
267
268 /* Convert ethernet clock ticks to microseconds */
269 static unsigned int gfar_ticks2usecs(struct gfar_private *priv, unsigned int ticks)
270 {
271         unsigned int count;
272
273         /* The timer is different, depending on the interface speed */
274         switch (priv->mii_info->speed) {
275         case 1000:
276                 count = GFAR_GBIT_TIME;
277                 break;
278         case 100:
279                 count = GFAR_100_TIME;
280                 break;
281         case 10:
282         default:
283                 count = GFAR_10_TIME;
284                 break;
285         }
286
287         /* Make sure we return a number greater than 0 */
288         /* if ticks is > 0 */
289         return ((ticks * count) / 1000);
290 }
291
292 /* Get the coalescing parameters, and put them in the cvals
293  * structure.  */
294 int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
295 {
296         struct gfar_private *priv = netdev_priv(dev);
297
298         cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, priv->rxtime);
299         cvals->rx_max_coalesced_frames = priv->rxcount;
300
301         cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, priv->txtime);
302         cvals->tx_max_coalesced_frames = priv->txcount;
303
304         cvals->use_adaptive_rx_coalesce = 0;
305         cvals->use_adaptive_tx_coalesce = 0;
306
307         cvals->pkt_rate_low = 0;
308         cvals->rx_coalesce_usecs_low = 0;
309         cvals->rx_max_coalesced_frames_low = 0;
310         cvals->tx_coalesce_usecs_low = 0;
311         cvals->tx_max_coalesced_frames_low = 0;
312
313         /* When the packet rate is below pkt_rate_high but above
314          * pkt_rate_low (both measured in packets per second) the
315          * normal {rx,tx}_* coalescing parameters are used.
316          */
317
318         /* When the packet rate is (measured in packets per second)
319          * is above pkt_rate_high, the {rx,tx}_*_high parameters are
320          * used.
321          */
322         cvals->pkt_rate_high = 0;
323         cvals->rx_coalesce_usecs_high = 0;
324         cvals->rx_max_coalesced_frames_high = 0;
325         cvals->tx_coalesce_usecs_high = 0;
326         cvals->tx_max_coalesced_frames_high = 0;
327
328         /* How often to do adaptive coalescing packet rate sampling,
329          * measured in seconds.  Must not be zero.
330          */
331         cvals->rate_sample_interval = 0;
332
333         return 0;
334 }
335
336 /* Change the coalescing values.
337  * Both cvals->*_usecs and cvals->*_frames have to be > 0
338  * in order for coalescing to be active
339  */
340 int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
341 {
342         struct gfar_private *priv = netdev_priv(dev);
343
344         /* Set up rx coalescing */
345         if ((cvals->rx_coalesce_usecs == 0) ||
346             (cvals->rx_max_coalesced_frames == 0))
347                 priv->rxcoalescing = 0;
348         else
349                 priv->rxcoalescing = 1;
350
351         priv->rxtime = gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs);
352         priv->rxcount = cvals->rx_max_coalesced_frames;
353
354         /* Set up tx coalescing */
355         if ((cvals->tx_coalesce_usecs == 0) ||
356             (cvals->tx_max_coalesced_frames == 0))
357                 priv->txcoalescing = 0;
358         else
359                 priv->txcoalescing = 1;
360
361         priv->txtime = gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs);
362         priv->txcount = cvals->tx_max_coalesced_frames;
363
364         if (priv->rxcoalescing)
365                 gfar_write(&priv->regs->rxic,
366                            mk_ic_value(priv->rxcount, priv->rxtime));
367         else
368                 gfar_write(&priv->regs->rxic, 0);
369
370         if (priv->txcoalescing)
371                 gfar_write(&priv->regs->txic,
372                            mk_ic_value(priv->txcount, priv->txtime));
373         else
374                 gfar_write(&priv->regs->txic, 0);
375
376         return 0;
377 }
378
379 /* Fills in rvals with the current ring parameters.  Currently,
380  * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
381  * jumbo are ignored by the driver */
382 void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
383 {
384         struct gfar_private *priv = netdev_priv(dev);
385
386         rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
387         rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
388         rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
389         rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
390
391         /* Values changeable by the user.  The valid values are
392          * in the range 1 to the "*_max_pending" counterpart above.
393          */
394         rvals->rx_pending = priv->rx_ring_size;
395         rvals->rx_mini_pending = priv->rx_ring_size;
396         rvals->rx_jumbo_pending = priv->rx_ring_size;
397         rvals->tx_pending = priv->tx_ring_size;
398 }
399
400 /* Change the current ring parameters, stopping the controller if
401  * necessary so that we don't mess things up while we're in
402  * motion.  We wait for the ring to be clean before reallocating
403  * the rings. */
404 int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
405 {
406         u32 tempval;
407         struct gfar_private *priv = netdev_priv(dev);
408         int err = 0;
409
410         if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
411                 return -EINVAL;
412
413         if (!is_power_of_2(rvals->rx_pending)) {
414                 printk("%s: Ring sizes must be a power of 2\n",
415                                 dev->name);
416                 return -EINVAL;
417         }
418
419         if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
420                 return -EINVAL;
421
422         if (!is_power_of_2(rvals->tx_pending)) {
423                 printk("%s: Ring sizes must be a power of 2\n",
424                                 dev->name);
425                 return -EINVAL;
426         }
427
428         /* Stop the controller so we don't rx any more frames */
429         /* But first, make sure we clear the bits */
430         tempval = gfar_read(&priv->regs->dmactrl);
431         tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
432         gfar_write(&priv->regs->dmactrl, tempval);
433
434         tempval = gfar_read(&priv->regs->dmactrl);
435         tempval |= (DMACTRL_GRS | DMACTRL_GTS);
436         gfar_write(&priv->regs->dmactrl, tempval);
437
438         while (!(gfar_read(&priv->regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC)))
439                 cpu_relax();
440
441         /* Note that rx is not clean right now */
442         priv->rxclean = 0;
443
444         if (dev->flags & IFF_UP) {
445                 /* Tell the driver to process the rest of the frames */
446                 gfar_receive(0, (void *) dev, NULL);
447
448                 /* Now wait for it to be done */
449                 wait_event_interruptible(priv->rxcleanupq, priv->rxclean);
450
451                 /* Ok, all packets have been handled.  Now we bring it down,
452                  * change the ring size, and bring it up */
453
454                 stop_gfar(dev);
455         }
456
457         priv->rx_ring_size = rvals->rx_pending;
458         priv->tx_ring_size = rvals->tx_pending;
459
460         if (dev->flags & IFF_UP)
461                 err = startup_gfar(dev);
462
463         return err;
464 }
465
466 struct ethtool_ops gfar_ethtool_ops = {
467         .get_settings = gfar_gsettings,
468         .get_drvinfo = gfar_gdrvinfo,
469         .get_regs_len = gfar_reglen,
470         .get_regs = gfar_get_regs,
471         .get_link = ethtool_op_get_link,
472         .get_coalesce = gfar_gcoalesce,
473         .set_coalesce = gfar_scoalesce,
474         .get_ringparam = gfar_gringparam,
475         .set_ringparam = gfar_sringparam,
476         .get_strings = gfar_gstrings,
477         .get_stats_count = gfar_stats_count,
478         .get_ethtool_stats = gfar_fill_stats,
479 };
480
481 struct ethtool_ops gfar_normon_nocoalesce_ethtool_ops = {
482         .get_settings = gfar_gsettings,
483         .get_drvinfo = gfar_gdrvinfo,
484         .get_regs_len = gfar_reglen,
485         .get_regs = gfar_get_regs,
486         .get_link = ethtool_op_get_link,
487         .get_ringparam = gfar_gringparam,
488         .set_ringparam = gfar_sringparam,
489         .get_strings = gfar_gstrings_normon,
490         .get_stats_count = gfar_stats_count_normon,
491         .get_ethtool_stats = gfar_fill_stats_normon,
492 };
493
494 struct ethtool_ops gfar_nocoalesce_ethtool_ops = {
495         .get_settings = gfar_gsettings,
496         .get_drvinfo = gfar_gdrvinfo,
497         .get_regs_len = gfar_reglen,
498         .get_regs = gfar_get_regs,
499         .get_link = ethtool_op_get_link,
500         .get_ringparam = gfar_gringparam,
501         .set_ringparam = gfar_sringparam,
502         .get_strings = gfar_gstrings,
503         .get_stats_count = gfar_stats_count,
504         .get_ethtool_stats = gfar_fill_stats,
505 };
506
507 struct ethtool_ops gfar_normon_ethtool_ops = {
508         .get_settings = gfar_gsettings,
509         .get_drvinfo = gfar_gdrvinfo,
510         .get_regs_len = gfar_reglen,
511         .get_regs = gfar_get_regs,
512         .get_link = ethtool_op_get_link,
513         .get_coalesce = gfar_gcoalesce,
514         .set_coalesce = gfar_scoalesce,
515         .get_ringparam = gfar_gringparam,
516         .set_ringparam = gfar_sringparam,
517         .get_strings = gfar_gstrings_normon,
518         .get_stats_count = gfar_stats_count_normon,
519         .get_ethtool_stats = gfar_fill_stats_normon,
520 };
521
522 struct ethtool_ops *gfar_op_array[] = {
523         &gfar_ethtool_ops,
524         &gfar_normon_ethtool_ops,
525         &gfar_nocoalesce_ethtool_ops,
526         &gfar_normon_nocoalesce_ethtool_ops
527 };