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