qlge: Add support for firmware mailbox commands.
[linux-2.6] / drivers / net / ucc_geth_ethtool.c
1 /*
2  * Copyright (c) 2007 Freescale Semiconductor, Inc. All rights reserved.
3  *
4  * Description: QE UCC Gigabit Ethernet Ethtool API Set
5  *
6  * Author: Li Yang <leoli@freescale.com>
7  *
8  * Limitation:
9  * Can only get/set setttings of the first queue.
10  * Need to re-open the interface manually after changing some paramters.
11  *
12  * This program is free software; you can redistribute  it and/or modify it
13  * under  the terms of  the GNU General  Public License as published by the
14  * Free Software Foundation;  either version 2 of the  License, or (at your
15  * option) any later version.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/stddef.h>
23 #include <linux/interrupt.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/spinlock.h>
28 #include <linux/mm.h>
29 #include <linux/delay.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/fsl_devices.h>
32 #include <linux/ethtool.h>
33 #include <linux/mii.h>
34 #include <linux/phy.h>
35
36 #include <asm/io.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
39 #include <asm/types.h>
40
41 #include "ucc_geth.h"
42
43 static char hw_stat_gstrings[][ETH_GSTRING_LEN] = {
44         "tx-64-frames",
45         "tx-65-127-frames",
46         "tx-128-255-frames",
47         "rx-64-frames",
48         "rx-65-127-frames",
49         "rx-128-255-frames",
50         "tx-bytes-ok",
51         "tx-pause-frames",
52         "tx-multicast-frames",
53         "tx-broadcast-frames",
54         "rx-frames",
55         "rx-bytes-ok",
56         "rx-bytes-all",
57         "rx-multicast-frames",
58         "rx-broadcast-frames",
59         "stats-counter-carry",
60         "stats-counter-mask",
61         "rx-dropped-frames",
62 };
63
64 static char tx_fw_stat_gstrings[][ETH_GSTRING_LEN] = {
65         "tx-single-collision",
66         "tx-multiple-collision",
67         "tx-late-collsion",
68         "tx-aborted-frames",
69         "tx-lost-frames",
70         "tx-carrier-sense-errors",
71         "tx-frames-ok",
72         "tx-excessive-differ-frames",
73         "tx-256-511-frames",
74         "tx-512-1023-frames",
75         "tx-1024-1518-frames",
76         "tx-jumbo-frames",
77 };
78
79 static char rx_fw_stat_gstrings[][ETH_GSTRING_LEN] = {
80         "rx-crc-errors",
81         "rx-alignment-errors",
82         "rx-in-range-length-errors",
83         "rx-out-of-range-length-errors",
84         "rx-too-long-frames",
85         "rx-runt",
86         "rx-very-long-event",
87         "rx-symbol-errors",
88         "rx-busy-drop-frames",
89         "reserved",
90         "reserved",
91         "rx-mismatch-drop-frames",
92         "rx-small-than-64",
93         "rx-256-511-frames",
94         "rx-512-1023-frames",
95         "rx-1024-1518-frames",
96         "rx-jumbo-frames",
97         "rx-mac-error-loss",
98         "rx-pause-frames",
99         "reserved",
100         "rx-vlan-removed",
101         "rx-vlan-replaced",
102         "rx-vlan-inserted",
103         "rx-ip-checksum-errors",
104 };
105
106 #define UEC_HW_STATS_LEN ARRAY_SIZE(hw_stat_gstrings)
107 #define UEC_TX_FW_STATS_LEN ARRAY_SIZE(tx_fw_stat_gstrings)
108 #define UEC_RX_FW_STATS_LEN ARRAY_SIZE(rx_fw_stat_gstrings)
109
110 static int
111 uec_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
112 {
113         struct ucc_geth_private *ugeth = netdev_priv(netdev);
114         struct phy_device *phydev = ugeth->phydev;
115         struct ucc_geth_info *ug_info = ugeth->ug_info;
116
117         if (!phydev)
118                 return -ENODEV;
119
120         ecmd->maxtxpkt = 1;
121         ecmd->maxrxpkt = ug_info->interruptcoalescingmaxvalue[0];
122
123         return phy_ethtool_gset(phydev, ecmd);
124 }
125
126 static int
127 uec_set_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
128 {
129         struct ucc_geth_private *ugeth = netdev_priv(netdev);
130         struct phy_device *phydev = ugeth->phydev;
131
132         if (!phydev)
133                 return -ENODEV;
134
135         return phy_ethtool_sset(phydev, ecmd);
136 }
137
138 static void
139 uec_get_pauseparam(struct net_device *netdev,
140                      struct ethtool_pauseparam *pause)
141 {
142         struct ucc_geth_private *ugeth = netdev_priv(netdev);
143
144         pause->autoneg = ugeth->phydev->autoneg;
145
146         if (ugeth->ug_info->receiveFlowControl)
147                 pause->rx_pause = 1;
148         if (ugeth->ug_info->transmitFlowControl)
149                 pause->tx_pause = 1;
150 }
151
152 static int
153 uec_set_pauseparam(struct net_device *netdev,
154                      struct ethtool_pauseparam *pause)
155 {
156         struct ucc_geth_private *ugeth = netdev_priv(netdev);
157         int ret = 0;
158
159         ugeth->ug_info->receiveFlowControl = pause->rx_pause;
160         ugeth->ug_info->transmitFlowControl = pause->tx_pause;
161
162         if (ugeth->phydev->autoneg) {
163                 if (netif_running(netdev)) {
164                         /* FIXME: automatically restart */
165                         printk(KERN_INFO
166                                 "Please re-open the interface.\n");
167                 }
168         } else {
169                 struct ucc_geth_info *ug_info = ugeth->ug_info;
170
171                 ret = init_flow_control_params(ug_info->aufc,
172                                         ug_info->receiveFlowControl,
173                                         ug_info->transmitFlowControl,
174                                         ug_info->pausePeriod,
175                                         ug_info->extensionField,
176                                         &ugeth->uccf->uf_regs->upsmr,
177                                         &ugeth->ug_regs->uempr,
178                                         &ugeth->ug_regs->maccfg1);
179         }
180
181         return ret;
182 }
183
184 static uint32_t
185 uec_get_msglevel(struct net_device *netdev)
186 {
187         struct ucc_geth_private *ugeth = netdev_priv(netdev);
188         return ugeth->msg_enable;
189 }
190
191 static void
192 uec_set_msglevel(struct net_device *netdev, uint32_t data)
193 {
194         struct ucc_geth_private *ugeth = netdev_priv(netdev);
195         ugeth->msg_enable = data;
196 }
197
198 static int
199 uec_get_regs_len(struct net_device *netdev)
200 {
201         return sizeof(struct ucc_geth);
202 }
203
204 static void
205 uec_get_regs(struct net_device *netdev,
206                struct ethtool_regs *regs, void *p)
207 {
208         int i;
209         struct ucc_geth_private *ugeth = netdev_priv(netdev);
210         u32 __iomem *ug_regs = (u32 __iomem *)ugeth->ug_regs;
211         u32 *buff = p;
212
213         for (i = 0; i < sizeof(struct ucc_geth) / sizeof(u32); i++)
214                 buff[i] = in_be32(&ug_regs[i]);
215 }
216
217 static void
218 uec_get_ringparam(struct net_device *netdev,
219                     struct ethtool_ringparam *ring)
220 {
221         struct ucc_geth_private *ugeth = netdev_priv(netdev);
222         struct ucc_geth_info *ug_info = ugeth->ug_info;
223         int queue = 0;
224
225         ring->rx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
226         ring->rx_mini_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
227         ring->rx_jumbo_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
228         ring->tx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
229
230         ring->rx_pending = ug_info->bdRingLenRx[queue];
231         ring->rx_mini_pending = ug_info->bdRingLenRx[queue];
232         ring->rx_jumbo_pending = ug_info->bdRingLenRx[queue];
233         ring->tx_pending = ug_info->bdRingLenTx[queue];
234 }
235
236 static int
237 uec_set_ringparam(struct net_device *netdev,
238                     struct ethtool_ringparam *ring)
239 {
240         struct ucc_geth_private *ugeth = netdev_priv(netdev);
241         struct ucc_geth_info *ug_info = ugeth->ug_info;
242         int queue = 0, ret = 0;
243
244         if (ring->rx_pending < UCC_GETH_RX_BD_RING_SIZE_MIN) {
245                 printk("%s: RxBD ring size must be no smaller than %d.\n",
246                                 netdev->name, UCC_GETH_RX_BD_RING_SIZE_MIN);
247                 return -EINVAL;
248         }
249         if (ring->rx_pending % UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT) {
250                 printk("%s: RxBD ring size must be multiple of %d.\n",
251                         netdev->name, UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT);
252                 return -EINVAL;
253         }
254         if (ring->tx_pending < UCC_GETH_TX_BD_RING_SIZE_MIN) {
255                 printk("%s: TxBD ring size must be no smaller than %d.\n",
256                                 netdev->name, UCC_GETH_TX_BD_RING_SIZE_MIN);
257                 return -EINVAL;
258         }
259
260         ug_info->bdRingLenRx[queue] = ring->rx_pending;
261         ug_info->bdRingLenTx[queue] = ring->tx_pending;
262
263         if (netif_running(netdev)) {
264                 /* FIXME: restart automatically */
265                 printk(KERN_INFO
266                         "Please re-open the interface.\n");
267         }
268
269         return ret;
270 }
271
272 static int uec_get_sset_count(struct net_device *netdev, int sset)
273 {
274         struct ucc_geth_private *ugeth = netdev_priv(netdev);
275         u32 stats_mode = ugeth->ug_info->statisticsMode;
276         int len = 0;
277
278         switch (sset) {
279         case ETH_SS_STATS:
280                 if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE)
281                         len += UEC_HW_STATS_LEN;
282                 if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX)
283                         len += UEC_TX_FW_STATS_LEN;
284                 if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
285                         len += UEC_RX_FW_STATS_LEN;
286
287                 return len;
288
289         default:
290                 return -EOPNOTSUPP;
291         }
292 }
293
294 static void uec_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
295 {
296         struct ucc_geth_private *ugeth = netdev_priv(netdev);
297         u32 stats_mode = ugeth->ug_info->statisticsMode;
298
299         if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE) {
300                 memcpy(buf, hw_stat_gstrings, UEC_HW_STATS_LEN *
301                                 ETH_GSTRING_LEN);
302                 buf += UEC_HW_STATS_LEN * ETH_GSTRING_LEN;
303         }
304         if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
305                 memcpy(buf, tx_fw_stat_gstrings, UEC_TX_FW_STATS_LEN *
306                                 ETH_GSTRING_LEN);
307                 buf += UEC_TX_FW_STATS_LEN * ETH_GSTRING_LEN;
308         }
309         if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
310                 memcpy(buf, rx_fw_stat_gstrings, UEC_RX_FW_STATS_LEN *
311                                 ETH_GSTRING_LEN);
312 }
313
314 static void uec_get_ethtool_stats(struct net_device *netdev,
315                 struct ethtool_stats *stats, uint64_t *data)
316 {
317         struct ucc_geth_private *ugeth = netdev_priv(netdev);
318         u32 stats_mode = ugeth->ug_info->statisticsMode;
319         u32 __iomem *base;
320         int i, j = 0;
321
322         if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE) {
323                 base = (u32 __iomem *)&ugeth->ug_regs->tx64;
324                 for (i = 0; i < UEC_HW_STATS_LEN; i++)
325                         data[j++] = in_be32(&base[i]);
326         }
327         if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
328                 base = (u32 __iomem *)ugeth->p_tx_fw_statistics_pram;
329                 for (i = 0; i < UEC_TX_FW_STATS_LEN; i++)
330                         data[j++] = base ? in_be32(&base[i]) : 0;
331         }
332         if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX) {
333                 base = (u32 __iomem *)ugeth->p_rx_fw_statistics_pram;
334                 for (i = 0; i < UEC_RX_FW_STATS_LEN; i++)
335                         data[j++] = base ? in_be32(&base[i]) : 0;
336         }
337 }
338
339 static int uec_nway_reset(struct net_device *netdev)
340 {
341         struct ucc_geth_private *ugeth = netdev_priv(netdev);
342
343         return phy_start_aneg(ugeth->phydev);
344 }
345
346 /* Report driver information */
347 static void
348 uec_get_drvinfo(struct net_device *netdev,
349                        struct ethtool_drvinfo *drvinfo)
350 {
351         strncpy(drvinfo->driver, DRV_NAME, 32);
352         strncpy(drvinfo->version, DRV_VERSION, 32);
353         strncpy(drvinfo->fw_version, "N/A", 32);
354         strncpy(drvinfo->bus_info, "QUICC ENGINE", 32);
355         drvinfo->eedump_len = 0;
356         drvinfo->regdump_len = uec_get_regs_len(netdev);
357 }
358
359 static const struct ethtool_ops uec_ethtool_ops = {
360         .get_settings           = uec_get_settings,
361         .set_settings           = uec_set_settings,
362         .get_drvinfo            = uec_get_drvinfo,
363         .get_regs_len           = uec_get_regs_len,
364         .get_regs               = uec_get_regs,
365         .get_msglevel           = uec_get_msglevel,
366         .set_msglevel           = uec_set_msglevel,
367         .nway_reset             = uec_nway_reset,
368         .get_link               = ethtool_op_get_link,
369         .get_ringparam          = uec_get_ringparam,
370         .set_ringparam          = uec_set_ringparam,
371         .get_pauseparam         = uec_get_pauseparam,
372         .set_pauseparam         = uec_set_pauseparam,
373         .set_sg                 = ethtool_op_set_sg,
374         .get_sset_count         = uec_get_sset_count,
375         .get_strings            = uec_get_strings,
376         .get_ethtool_stats      = uec_get_ethtool_stats,
377 };
378
379 void uec_set_ethtool_ops(struct net_device *netdev)
380 {
381         SET_ETHTOOL_OPS(netdev, &uec_ethtool_ops);
382 }