[POWERPC] Add "is_power_of_2" checking to log2.h.
[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
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/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
30 #include <linux/mm.h>
31
32 #include <asm/io.h>
33 #include <asm/irq.h>
34 #include <asm/uaccess.h>
35 #include <linux/module.h>
36 #include <linux/crc32.h>
37 #include <asm/types.h>
38 #include <asm/uaccess.h>
39 #include <linux/ethtool.h>
40 #include <linux/mii.h>
41 #include <linux/phy.h>
42
43 #include "gianfar.h"
44
45 extern void gfar_start(struct net_device *dev);
46 extern int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
47
48 #define GFAR_MAX_COAL_USECS 0xffff
49 #define GFAR_MAX_COAL_FRAMES 0xff
50 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
51                      u64 * buf);
52 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf);
53 static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
54 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals);
55 static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
56 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals);
57 static void gfar_gdrvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo);
58
59 static char stat_gstrings[][ETH_GSTRING_LEN] = {
60         "rx-dropped-by-kernel",
61         "rx-large-frame-errors",
62         "rx-short-frame-errors",
63         "rx-non-octet-errors",
64         "rx-crc-errors",
65         "rx-overrun-errors",
66         "rx-busy-errors",
67         "rx-babbling-errors",
68         "rx-truncated-frames",
69         "ethernet-bus-error",
70         "tx-babbling-errors",
71         "tx-underrun-errors",
72         "rx-skb-missing-errors",
73         "tx-timeout-errors",
74         "tx-rx-64-frames",
75         "tx-rx-65-127-frames",
76         "tx-rx-128-255-frames",
77         "tx-rx-256-511-frames",
78         "tx-rx-512-1023-frames",
79         "tx-rx-1024-1518-frames",
80         "tx-rx-1519-1522-good-vlan",
81         "rx-bytes",
82         "rx-packets",
83         "rx-fcs-errors",
84         "receive-multicast-packet",
85         "receive-broadcast-packet",
86         "rx-control-frame-packets",
87         "rx-pause-frame-packets",
88         "rx-unknown-op-code",
89         "rx-alignment-error",
90         "rx-frame-length-error",
91         "rx-code-error",
92         "rx-carrier-sense-error",
93         "rx-undersize-packets",
94         "rx-oversize-packets",
95         "rx-fragmented-frames",
96         "rx-jabber-frames",
97         "rx-dropped-frames",
98         "tx-byte-counter",
99         "tx-packets",
100         "tx-multicast-packets",
101         "tx-broadcast-packets",
102         "tx-pause-control-frames",
103         "tx-deferral-packets",
104         "tx-excessive-deferral-packets",
105         "tx-single-collision-packets",
106         "tx-multiple-collision-packets",
107         "tx-late-collision-packets",
108         "tx-excessive-collision-packets",
109         "tx-total-collision",
110         "reserved",
111         "tx-dropped-frames",
112         "tx-jabber-frames",
113         "tx-fcs-errors",
114         "tx-control-frames",
115         "tx-oversize-frames",
116         "tx-undersize-frames",
117         "tx-fragmented-frames",
118 };
119
120 /* Fill in a buffer with the strings which correspond to the
121  * stats */
122 static void gfar_gstrings(struct net_device *dev, u32 stringset, u8 * buf)
123 {
124         struct gfar_private *priv = netdev_priv(dev);
125
126         if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
127                 memcpy(buf, stat_gstrings, GFAR_STATS_LEN * ETH_GSTRING_LEN);
128         else
129                 memcpy(buf, stat_gstrings,
130                                 GFAR_EXTRA_STATS_LEN * ETH_GSTRING_LEN);
131 }
132
133 /* Fill in an array of 64-bit statistics from various sources.
134  * This array will be appended to the end of the ethtool_stats
135  * structure, and returned to user space
136  */
137 static void gfar_fill_stats(struct net_device *dev, struct ethtool_stats *dummy, u64 * buf)
138 {
139         int i;
140         struct gfar_private *priv = netdev_priv(dev);
141         u64 *extra = (u64 *) & priv->extra_stats;
142
143         if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
144                 u32 __iomem *rmon = (u32 __iomem *) & priv->regs->rmon;
145                 struct gfar_stats *stats = (struct gfar_stats *) buf;
146
147                 for (i = 0; i < GFAR_RMON_LEN; i++)
148                         stats->rmon[i] = (u64) gfar_read(&rmon[i]);
149
150                 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
151                         stats->extra[i] = extra[i];
152         } else
153                 for (i = 0; i < GFAR_EXTRA_STATS_LEN; i++)
154                         buf[i] = extra[i];
155 }
156
157 /* Returns the number of stats (and their corresponding strings) */
158 static int gfar_stats_count(struct net_device *dev)
159 {
160         struct gfar_private *priv = netdev_priv(dev);
161
162         if (priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_RMON)
163                 return GFAR_STATS_LEN;
164         else
165                 return GFAR_EXTRA_STATS_LEN;
166 }
167
168 /* Fills in the drvinfo structure with some basic info */
169 static void gfar_gdrvinfo(struct net_device *dev, struct
170               ethtool_drvinfo *drvinfo)
171 {
172         strncpy(drvinfo->driver, DRV_NAME, GFAR_INFOSTR_LEN);
173         strncpy(drvinfo->version, gfar_driver_version, GFAR_INFOSTR_LEN);
174         strncpy(drvinfo->fw_version, "N/A", GFAR_INFOSTR_LEN);
175         strncpy(drvinfo->bus_info, "N/A", GFAR_INFOSTR_LEN);
176         drvinfo->n_stats = GFAR_STATS_LEN;
177         drvinfo->testinfo_len = 0;
178         drvinfo->regdump_len = 0;
179         drvinfo->eedump_len = 0;
180 }
181
182
183 static int gfar_ssettings(struct net_device *dev, struct ethtool_cmd *cmd)
184 {
185         struct gfar_private *priv = netdev_priv(dev);
186         struct phy_device *phydev = priv->phydev;
187
188         if (NULL == phydev)
189                 return -ENODEV;
190
191         return phy_ethtool_sset(phydev, cmd);
192 }
193
194
195 /* Return the current settings in the ethtool_cmd structure */
196 static int gfar_gsettings(struct net_device *dev, struct ethtool_cmd *cmd)
197 {
198         struct gfar_private *priv = netdev_priv(dev);
199         struct phy_device *phydev = priv->phydev;
200
201         if (NULL == phydev)
202                 return -ENODEV;
203
204         cmd->maxtxpkt = priv->txcount;
205         cmd->maxrxpkt = priv->rxcount;
206
207         return phy_ethtool_gset(phydev, cmd);
208 }
209
210 /* Return the length of the register structure */
211 static int gfar_reglen(struct net_device *dev)
212 {
213         return sizeof (struct gfar);
214 }
215
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)
218 {
219         int i;
220         struct gfar_private *priv = netdev_priv(dev);
221         u32 __iomem *theregs = (u32 __iomem *) priv->regs;
222         u32 *buf = (u32 *) regbuf;
223
224         for (i = 0; i < sizeof (struct gfar) / sizeof (u32); i++)
225                 buf[i] = gfar_read(&theregs[i]);
226 }
227
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)
231 {
232         unsigned int count;
233
234         /* The timer is different, depending on the interface speed */
235         switch (priv->phydev->speed) {
236         case SPEED_1000:
237                 count = GFAR_GBIT_TIME;
238                 break;
239         case SPEED_100:
240                 count = GFAR_100_TIME;
241                 break;
242         case SPEED_10:
243         default:
244                 count = GFAR_10_TIME;
245                 break;
246         }
247
248         /* Make sure we return a number greater than 0
249          * if usecs > 0 */
250         return ((usecs * 1000 + count - 1) / count);
251 }
252
253 /* Convert ethernet clock ticks to microseconds */
254 static unsigned int gfar_ticks2usecs(struct gfar_private *priv, unsigned int ticks)
255 {
256         unsigned int count;
257
258         /* The timer is different, depending on the interface speed */
259         switch (priv->phydev->speed) {
260         case SPEED_1000:
261                 count = GFAR_GBIT_TIME;
262                 break;
263         case SPEED_100:
264                 count = GFAR_100_TIME;
265                 break;
266         case SPEED_10:
267         default:
268                 count = GFAR_10_TIME;
269                 break;
270         }
271
272         /* Make sure we return a number greater than 0 */
273         /* if ticks is > 0 */
274         return ((ticks * count) / 1000);
275 }
276
277 /* Get the coalescing parameters, and put them in the cvals
278  * structure.  */
279 static int gfar_gcoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
280 {
281         struct gfar_private *priv = netdev_priv(dev);
282
283         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
284                 return -EOPNOTSUPP;
285
286         if (NULL == priv->phydev)
287                 return -ENODEV;
288
289         cvals->rx_coalesce_usecs = gfar_ticks2usecs(priv, priv->rxtime);
290         cvals->rx_max_coalesced_frames = priv->rxcount;
291
292         cvals->tx_coalesce_usecs = gfar_ticks2usecs(priv, priv->txtime);
293         cvals->tx_max_coalesced_frames = priv->txcount;
294
295         cvals->use_adaptive_rx_coalesce = 0;
296         cvals->use_adaptive_tx_coalesce = 0;
297
298         cvals->pkt_rate_low = 0;
299         cvals->rx_coalesce_usecs_low = 0;
300         cvals->rx_max_coalesced_frames_low = 0;
301         cvals->tx_coalesce_usecs_low = 0;
302         cvals->tx_max_coalesced_frames_low = 0;
303
304         /* When the packet rate is below pkt_rate_high but above
305          * pkt_rate_low (both measured in packets per second) the
306          * normal {rx,tx}_* coalescing parameters are used.
307          */
308
309         /* When the packet rate is (measured in packets per second)
310          * is above pkt_rate_high, the {rx,tx}_*_high parameters are
311          * used.
312          */
313         cvals->pkt_rate_high = 0;
314         cvals->rx_coalesce_usecs_high = 0;
315         cvals->rx_max_coalesced_frames_high = 0;
316         cvals->tx_coalesce_usecs_high = 0;
317         cvals->tx_max_coalesced_frames_high = 0;
318
319         /* How often to do adaptive coalescing packet rate sampling,
320          * measured in seconds.  Must not be zero.
321          */
322         cvals->rate_sample_interval = 0;
323
324         return 0;
325 }
326
327 /* Change the coalescing values.
328  * Both cvals->*_usecs and cvals->*_frames have to be > 0
329  * in order for coalescing to be active
330  */
331 static int gfar_scoalesce(struct net_device *dev, struct ethtool_coalesce *cvals)
332 {
333         struct gfar_private *priv = netdev_priv(dev);
334
335         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_COALESCE))
336                 return -EOPNOTSUPP;
337
338         /* Set up rx coalescing */
339         if ((cvals->rx_coalesce_usecs == 0) ||
340             (cvals->rx_max_coalesced_frames == 0))
341                 priv->rxcoalescing = 0;
342         else
343                 priv->rxcoalescing = 1;
344
345         if (NULL == priv->phydev)
346                 return -ENODEV;
347
348         /* Check the bounds of the values */
349         if (cvals->rx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
350                 pr_info("Coalescing is limited to %d microseconds\n",
351                                 GFAR_MAX_COAL_USECS);
352                 return -EINVAL;
353         }
354
355         if (cvals->rx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
356                 pr_info("Coalescing is limited to %d frames\n",
357                                 GFAR_MAX_COAL_FRAMES);
358                 return -EINVAL;
359         }
360
361         priv->rxtime = gfar_usecs2ticks(priv, cvals->rx_coalesce_usecs);
362         priv->rxcount = cvals->rx_max_coalesced_frames;
363
364         /* Set up tx coalescing */
365         if ((cvals->tx_coalesce_usecs == 0) ||
366             (cvals->tx_max_coalesced_frames == 0))
367                 priv->txcoalescing = 0;
368         else
369                 priv->txcoalescing = 1;
370
371         /* Check the bounds of the values */
372         if (cvals->tx_coalesce_usecs > GFAR_MAX_COAL_USECS) {
373                 pr_info("Coalescing is limited to %d microseconds\n",
374                                 GFAR_MAX_COAL_USECS);
375                 return -EINVAL;
376         }
377
378         if (cvals->tx_max_coalesced_frames > GFAR_MAX_COAL_FRAMES) {
379                 pr_info("Coalescing is limited to %d frames\n",
380                                 GFAR_MAX_COAL_FRAMES);
381                 return -EINVAL;
382         }
383
384         priv->txtime = gfar_usecs2ticks(priv, cvals->tx_coalesce_usecs);
385         priv->txcount = cvals->tx_max_coalesced_frames;
386
387         if (priv->rxcoalescing)
388                 gfar_write(&priv->regs->rxic,
389                            mk_ic_value(priv->rxcount, priv->rxtime));
390         else
391                 gfar_write(&priv->regs->rxic, 0);
392
393         if (priv->txcoalescing)
394                 gfar_write(&priv->regs->txic,
395                            mk_ic_value(priv->txcount, priv->txtime));
396         else
397                 gfar_write(&priv->regs->txic, 0);
398
399         return 0;
400 }
401
402 /* Fills in rvals with the current ring parameters.  Currently,
403  * rx, rx_mini, and rx_jumbo rings are the same size, as mini and
404  * jumbo are ignored by the driver */
405 static void gfar_gringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
406 {
407         struct gfar_private *priv = netdev_priv(dev);
408
409         rvals->rx_max_pending = GFAR_RX_MAX_RING_SIZE;
410         rvals->rx_mini_max_pending = GFAR_RX_MAX_RING_SIZE;
411         rvals->rx_jumbo_max_pending = GFAR_RX_MAX_RING_SIZE;
412         rvals->tx_max_pending = GFAR_TX_MAX_RING_SIZE;
413
414         /* Values changeable by the user.  The valid values are
415          * in the range 1 to the "*_max_pending" counterpart above.
416          */
417         rvals->rx_pending = priv->rx_ring_size;
418         rvals->rx_mini_pending = priv->rx_ring_size;
419         rvals->rx_jumbo_pending = priv->rx_ring_size;
420         rvals->tx_pending = priv->tx_ring_size;
421 }
422
423 /* Change the current ring parameters, stopping the controller if
424  * necessary so that we don't mess things up while we're in
425  * motion.  We wait for the ring to be clean before reallocating
426  * the rings. */
427 static int gfar_sringparam(struct net_device *dev, struct ethtool_ringparam *rvals)
428 {
429         struct gfar_private *priv = netdev_priv(dev);
430         int err = 0;
431
432         if (rvals->rx_pending > GFAR_RX_MAX_RING_SIZE)
433                 return -EINVAL;
434
435         if (!is_power_of_2(rvals->rx_pending)) {
436                 printk("%s: Ring sizes must be a power of 2\n",
437                                 dev->name);
438                 return -EINVAL;
439         }
440
441         if (rvals->tx_pending > GFAR_TX_MAX_RING_SIZE)
442                 return -EINVAL;
443
444         if (!is_power_of_2(rvals->tx_pending)) {
445                 printk("%s: Ring sizes must be a power of 2\n",
446                                 dev->name);
447                 return -EINVAL;
448         }
449
450         if (dev->flags & IFF_UP) {
451                 unsigned long flags;
452
453                 /* Halt TX and RX, and process the frames which
454                  * have already been received */
455                 spin_lock_irqsave(&priv->txlock, flags);
456                 spin_lock(&priv->rxlock);
457
458                 gfar_halt(dev);
459                 gfar_clean_rx_ring(dev, priv->rx_ring_size);
460
461                 spin_unlock(&priv->rxlock);
462                 spin_unlock_irqrestore(&priv->txlock, 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->txlock, flags);
493                 spin_lock(&priv->rxlock);
494
495                 gfar_halt(dev);
496                 gfar_clean_rx_ring(dev, priv->rx_ring_size);
497
498                 spin_unlock(&priv->rxlock);
499                 spin_unlock_irqrestore(&priv->txlock, flags);
500
501                 /* Now we take down the rings to rebuild them */
502                 stop_gfar(dev);
503         }
504
505         priv->rx_csum_enable = data;
506
507         if (dev->flags & IFF_UP)
508                 err = startup_gfar(dev);
509
510         return err;
511 }
512
513 static uint32_t gfar_get_rx_csum(struct net_device *dev)
514 {
515         struct gfar_private *priv = netdev_priv(dev);
516
517         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
518                 return 0;
519
520         return priv->rx_csum_enable;
521 }
522
523 static int gfar_set_tx_csum(struct net_device *dev, uint32_t data)
524 {
525         unsigned long flags;
526         struct gfar_private *priv = netdev_priv(dev);
527
528         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
529                 return -EOPNOTSUPP;
530
531         spin_lock_irqsave(&priv->txlock, flags);
532         gfar_halt(dev);
533
534         if (data)
535                 dev->features |= NETIF_F_IP_CSUM;
536         else
537                 dev->features &= ~NETIF_F_IP_CSUM;
538
539         gfar_start(dev);
540         spin_unlock_irqrestore(&priv->txlock, flags);
541
542         return 0;
543 }
544
545 static uint32_t gfar_get_tx_csum(struct net_device *dev)
546 {
547         struct gfar_private *priv = netdev_priv(dev);
548
549         if (!(priv->einfo->device_flags & FSL_GIANFAR_DEV_HAS_CSUM))
550                 return 0;
551
552         return (dev->features & NETIF_F_IP_CSUM) != 0;
553 }
554
555 static uint32_t gfar_get_msglevel(struct net_device *dev)
556 {
557         struct gfar_private *priv = netdev_priv(dev);
558         return priv->msg_enable;
559 }
560
561 static void gfar_set_msglevel(struct net_device *dev, uint32_t data)
562 {
563         struct gfar_private *priv = netdev_priv(dev);
564         priv->msg_enable = data;
565 }
566
567
568 const struct ethtool_ops gfar_ethtool_ops = {
569         .get_settings = gfar_gsettings,
570         .set_settings = gfar_ssettings,
571         .get_drvinfo = gfar_gdrvinfo,
572         .get_regs_len = gfar_reglen,
573         .get_regs = gfar_get_regs,
574         .get_link = ethtool_op_get_link,
575         .get_coalesce = gfar_gcoalesce,
576         .set_coalesce = gfar_scoalesce,
577         .get_ringparam = gfar_gringparam,
578         .set_ringparam = gfar_sringparam,
579         .get_strings = gfar_gstrings,
580         .get_stats_count = gfar_stats_count,
581         .get_ethtool_stats = gfar_fill_stats,
582         .get_rx_csum = gfar_get_rx_csum,
583         .get_tx_csum = gfar_get_tx_csum,
584         .set_rx_csum = gfar_set_rx_csum,
585         .set_tx_csum = gfar_set_tx_csum,
586         .get_msglevel = gfar_get_msglevel,
587         .set_msglevel = gfar_set_msglevel,
588 };