[PATCH] declance: Convert to irqreturn_t.
[sfrench/cifs-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 (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 #include <linux/mii.h>
43 #include <linux/phy.h>
44
45 #include "gianfar.h"
46
47 #define is_power_of_2(x)        ((x) != 0 && (((x) & ((x) - 1)) == 0))
48
49 extern void gfar_start(struct net_device *dev);
50 extern int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
51
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,
55                      u64 * buf);
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);
62
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",
68         "rx-crc-errors",
69         "rx-overrun-errors",
70         "rx-busy-errors",
71         "rx-babbling-errors",
72         "rx-truncated-frames",
73         "ethernet-bus-error",
74         "tx-babbling-errors",
75         "tx-underrun-errors",
76         "rx-skb-missing-errors",
77         "tx-timeout-errors",
78         "tx-rx-64-frames",
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",
85         "rx-bytes",
86         "rx-packets",
87         "rx-fcs-errors",
88         "receive-multicast-packet",
89         "receive-broadcast-packet",
90         "rx-control-frame-packets",
91         "rx-pause-frame-packets",
92         "rx-unknown-op-code",
93         "rx-alignment-error",
94         "rx-frame-length-error",
95         "rx-code-error",
96         "rx-carrier-sense-error",
97         "rx-undersize-packets",
98         "rx-oversize-packets",
99         "rx-fragmented-frames",
100         "rx-jabber-frames",
101         "rx-dropped-frames",
102         "tx-byte-counter",
103         "tx-packets",
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",
114         "reserved",
115         "tx-dropped-frames",
116         "tx-jabber-frames",
117         "tx-fcs-errors",
118         "tx-control-frames",
119         "tx-oversize-frames",
120         "tx-undersize-frames",
121         "tx-fragmented-frames",
122 };
123
124 /* Fill in a buffer with the strings which correspond to the
125  * stats */
126 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
127 {
128         struct gfar_private *priv = netdev_priv(dev);
129         
130         if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
131                 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
132         else
133                 memcpy(buf, stat_gstrings,
134                                 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
135 }
136
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
140  */
141 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy, u64 * buf)
142 {
143         int i;
144         struct gfar_private *priv = netdev_priv(dev);
145         u64 *extra = (u64 *) & priv->extra_stats;
146
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;
150
151                 for (i = 0; i < GFAR_RMON_LEN; i++)
152                         stats->rmon[i] = (u64) (rmon[i]);
153
154                 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
155                         stats->extra[i] = extra[i];
156         } else
157                 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
158                         buf[i] = extra[i];
159 }
160
161 /* Returns the number of stats (and their corresponding strings) */
162 static int gfar_stats_count(struct net_device *dev)
163 {
164         struct gfar_private *priv = netdev_priv(dev);
165
166         if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
167                 return GFAR_STATS_LEN;
168         else
169                 return GFAR_EXTRA_STATS_LEN;
170 }
171
172 /* Fills in the drvinfo structure with some basic info */
173 static void gfar_gdrvinfo(struct net_device *dev, struct
174               ethtool_drvinfo *drvinfo)
175 {
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;
184 }
185
186
187 static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
188 {
189         struct gfar_private *priv = netdev_priv(dev);
190         struct phy_device *phydev = priv->phydev;
191
192         if (NULL == phydev)
193                 return -ENODEV;
194
195         return phy_ethtool_sset(phydev, cmd);
196 }
197
198
199 /* Return the current settings in the ethtool_cmd structure */
200 static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
201 {
202         struct gfar_private *priv = netdev_priv(dev);
203         struct phy_device *phydev = priv->phydev;
204
205         if (NULL == phydev)
206                 return -ENODEV;
207         
208         cmd->maxtxpkt = priv->txcount;
209         cmd->maxrxpkt = priv->rxcount;
210
211         return phy_ethtool_gset(phydev, cmd);
212 }
213
214 /* Return the length of the register structure */
215 static int gfar_reglen(struct net_device *dev)
216 {
217         return sizeof (struct gfar);
218 }
219
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)
222 {
223         int i;
224         struct gfar_private *priv = netdev_priv(dev);
225         u32 *theregs = (u32 *) priv->regs;
226         u32 *buf = (u32 *) regbuf;
227
228         for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
229                 buf[i] = theregs[i];
230 }
231
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)
235 {
236         unsigned int count;
237
238         /* The timer is different, depending on the interface speed */
239         switch (priv->phydev->speed) {
240         case SPEED_1000:
241                 count = GFAR_GBIT_TIME;
242                 break;
243         case SPEED_100:
244                 count = GFAR_100_TIME;
245                 break;
246         case SPEED_10:
247         default:
248                 count = GFAR_10_TIME;
249                 break;
250         }
251
252         /* Make sure we return a number greater than 0
253          * if usecs > 0 */
254         return ((usecs * 1000 + count - 1) / count);
255 }
256
257 /* Convert ethernet clock ticks to microseconds */
258 static unsigned int gfar_ticks2usecs(struct gfar_private *priv, unsigned int ticks)
259 {
260         unsigned int count;
261
262         /* The timer is different, depending on the interface speed */
263         switch (priv->phydev->speed) {
264         case SPEED_1000:
265                 count = GFAR_GBIT_TIME;
266                 break;
267         case SPEED_100:
268                 count = GFAR_100_TIME;
269                 break;
270         case SPEED_10:
271         default:
272                 count = GFAR_10_TIME;
273                 break;
274         }
275
276         /* Make sure we return a number greater than 0 */
277         /* if ticks is > 0 */
278         return ((ticks * count) / 1000);
279 }
280
281 /* Get the coalescing parameters, and put them in the cvals
282  * structure.  */
283 static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
284 {
285         struct gfar_private *priv = netdev_priv(dev);
286         
287         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
288                 return -EOPNOTSUPP;
289
290         if (NULL == priv->phydev)
291                 return -ENODEV;
292
293         cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, priv->rxtime);
294         cvals->rx_max_coalesced_frames = priv->rxcount;
295
296         cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, priv->txtime);
297         cvals->tx_max_coalesced_frames = priv->txcount;
298
299         cvals->use_adaptive_rx_coalesce = 0;
300         cvals->use_adaptive_tx_coalesce = 0;
301
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;
307
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.
311          */
312
313         /* When the packet rate is (measured in packets per second)
314          * is above pkt_rate_high, the {rx,tx}_*_high parameters are
315          * used.
316          */
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;
322
323         /* How often to do adaptive coalescing packet rate sampling,
324          * measured in seconds.  Must not be zero.
325          */
326         cvals->rate_sample_interval = 0;
327
328         return 0;
329 }
330
331 /* Change the coalescing values.
332  * Both cvals->*_usecs and cvals->*_frames have to be > 0
333  * in order for coalescing to be active
334  */
335 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
336 {
337         struct gfar_private *priv = netdev_priv(dev);
338
339         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
340                 return -EOPNOTSUPP;
341
342         /* Set up rx coalescing */
343         if ((cvals->rx_coalesce_usecs == 0) ||
344             (cvals->rx_max_coalesced_frames == 0))
345                 priv->rxcoalescing = 0;
346         else
347                 priv->rxcoalescing = 1;
348
349         if (NULL == priv->phydev)
350                 return -ENODEV;
351
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);
356                 return -EINVAL;
357         }
358
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);
362                 return -EINVAL;
363         }
364
365         priv->rxtime = gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs);
366         priv->rxcount = cvals->rx_max_coalesced_frames;
367
368         /* Set up tx coalescing */
369         if ((cvals->tx_coalesce_usecs == 0) ||
370             (cvals->tx_max_coalesced_frames == 0))
371                 priv->txcoalescing = 0;
372         else
373                 priv->txcoalescing = 1;
374
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);
379                 return -EINVAL;
380         }
381
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);
385                 return -EINVAL;
386         }
387
388         priv->txtime = gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs);
389         priv->txcount = cvals->tx_max_coalesced_frames;
390
391         if (priv->rxcoalescing)
392                 gfar_write(&priv->regs->rxic,
393                            mk_ic_value(priv->rxcount, priv->rxtime));
394         else
395                 gfar_write(&priv->regs->rxic, 0);
396
397         if (priv->txcoalescing)
398                 gfar_write(&priv->regs->txic,
399                            mk_ic_value(priv->txcount, priv->txtime));
400         else
401                 gfar_write(&priv->regs->txic, 0);
402
403         return 0;
404 }
405
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)
410 {
411         struct gfar_private *priv = netdev_priv(dev);
412
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;
417
418         /* Values changeable by the user.  The valid values are
419          * in the range 1 to the "*_max_pending" counterpart above.
420          */
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;
425 }
426
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
430  * the rings. */
431 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
432 {
433         struct gfar_private *priv = netdev_priv(dev);
434         int err = 0;
435
436         if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
437                 return -EINVAL;
438
439         if (!is_power_of_2(rvals->rx_pending)) {
440                 printk("%s: Ring sizes must be a power of 2\n",
441                                 dev->name);
442                 return -EINVAL;
443         }
444
445         if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
446                 return -EINVAL;
447
448         if (!is_power_of_2(rvals->tx_pending)) {
449                 printk("%s: Ring sizes must be a power of 2\n",
450                                 dev->name);
451                 return -EINVAL;
452         }
453
454         if (dev->flags & IFF_UP) {
455                 unsigned long flags;
456
457                 /* Halt TX and RX, and process the frames which
458                  * have already been received */
459                 spin_lock_irqsave(&priv->lock, flags);
460                 gfar_halt(dev);
461                 gfar_clean_rx_ring(dev, priv->rx_ring_size);
462                 spin_unlock_irqrestore(&priv->lock, flags);
463
464                 /* Now we take down the rings to rebuild them */
465                 stop_gfar(dev);
466         }
467
468         /* Change the size */
469         priv->rx_ring_size = rvals->rx_pending;
470         priv->tx_ring_size = rvals->tx_pending;
471
472         /* Rebuild the rings with the new size */
473         if (dev->flags & IFF_UP)
474                 err = startup_gfar(dev);
475
476         return err;
477 }
478
479 static int gfar_set_rx_csum(struct net_device *dev, uint32_t data)
480 {
481         struct gfar_private *priv = netdev_priv(dev);
482         int err = 0;
483
484         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
485                 return -EOPNOTSUPP;
486
487         if (dev->flags & IFF_UP) {
488                 unsigned long flags;
489
490                 /* Halt TX and RX, and process the frames which
491                  * have already been received */
492                 spin_lock_irqsave(&priv->lock, flags);
493                 gfar_halt(dev);
494                 gfar_clean_rx_ring(dev, priv->rx_ring_size);
495                 spin_unlock_irqrestore(&priv->lock, flags);
496
497                 /* Now we take down the rings to rebuild them */
498                 stop_gfar(dev);
499         }
500
501         priv->rx_csum_enable = data;
502
503         if (dev->flags & IFF_UP)
504                 err = startup_gfar(dev);
505
506         return err;
507 }
508
509 static uint32_t gfar_get_rx_csum(struct net_device *dev)
510 {
511         struct gfar_private *priv = netdev_priv(dev);
512
513         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
514                 return 0;
515
516         return priv->rx_csum_enable;
517 }
518
519 static int gfar_set_tx_csum(struct net_device *dev, uint32_t data)
520 {
521         unsigned long flags;
522         struct gfar_private *priv = netdev_priv(dev);
523
524         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
525                 return -EOPNOTSUPP;
526
527         spin_lock_irqsave(&priv->lock, flags);
528         gfar_halt(dev);
529
530         if (data)
531                 dev->features |= NETIF_F_IP_CSUM;
532         else
533                 dev->features &= ~NETIF_F_IP_CSUM;
534
535         gfar_start(dev);
536         spin_unlock_irqrestore(&priv->lock, flags);
537
538         return 0;
539 }
540
541 static uint32_t gfar_get_tx_csum(struct net_device *dev)
542 {
543         struct gfar_private *priv = netdev_priv(dev);
544
545         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
546                 return 0;
547
548         return (dev->features & NETIF_F_IP_CSUM) != 0;
549 }
550
551 static uint32_t gfar_get_msglevel(struct net_device *dev)
552 {       
553         struct gfar_private *priv = netdev_priv(dev);
554         return priv->msg_enable;
555 }       
556         
557 static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
558 {       
559         struct gfar_private *priv = netdev_priv(dev);
560         priv->msg_enable = data;
561 }
562
563
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,
584 };