Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / drivers / net / ethernet / oki-semi / pch_gbe / pch_gbe_ethtool.c
1 /*
2  * Copyright (C) 1999 - 2010 Intel Corporation.
3  * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
4  *
5  * This code was derived from the Intel e1000e Linux driver.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "pch_gbe.h"
20 #include "pch_gbe_api.h"
21
22 /**
23  * pch_gbe_stats - Stats item information
24  */
25 struct pch_gbe_stats {
26         char string[ETH_GSTRING_LEN];
27         size_t size;
28         size_t offset;
29 };
30
31 #define PCH_GBE_STAT(m)                                         \
32 {                                                               \
33         .string = #m,                                           \
34         .size = FIELD_SIZEOF(struct pch_gbe_hw_stats, m),       \
35         .offset = offsetof(struct pch_gbe_hw_stats, m),         \
36 }
37
38 /**
39  * pch_gbe_gstrings_stats - ethtool information status name list
40  */
41 static const struct pch_gbe_stats pch_gbe_gstrings_stats[] = {
42         PCH_GBE_STAT(rx_packets),
43         PCH_GBE_STAT(tx_packets),
44         PCH_GBE_STAT(rx_bytes),
45         PCH_GBE_STAT(tx_bytes),
46         PCH_GBE_STAT(rx_errors),
47         PCH_GBE_STAT(tx_errors),
48         PCH_GBE_STAT(rx_dropped),
49         PCH_GBE_STAT(tx_dropped),
50         PCH_GBE_STAT(multicast),
51         PCH_GBE_STAT(collisions),
52         PCH_GBE_STAT(rx_crc_errors),
53         PCH_GBE_STAT(rx_frame_errors),
54         PCH_GBE_STAT(rx_alloc_buff_failed),
55         PCH_GBE_STAT(tx_length_errors),
56         PCH_GBE_STAT(tx_aborted_errors),
57         PCH_GBE_STAT(tx_carrier_errors),
58         PCH_GBE_STAT(tx_timeout_count),
59         PCH_GBE_STAT(tx_restart_count),
60         PCH_GBE_STAT(intr_rx_dsc_empty_count),
61         PCH_GBE_STAT(intr_rx_frame_err_count),
62         PCH_GBE_STAT(intr_rx_fifo_err_count),
63         PCH_GBE_STAT(intr_rx_dma_err_count),
64         PCH_GBE_STAT(intr_tx_fifo_err_count),
65         PCH_GBE_STAT(intr_tx_dma_err_count),
66         PCH_GBE_STAT(intr_tcpip_err_count)
67 };
68
69 #define PCH_GBE_QUEUE_STATS_LEN 0
70 #define PCH_GBE_GLOBAL_STATS_LEN        ARRAY_SIZE(pch_gbe_gstrings_stats)
71 #define PCH_GBE_STATS_LEN (PCH_GBE_GLOBAL_STATS_LEN + PCH_GBE_QUEUE_STATS_LEN)
72
73 #define PCH_GBE_MAC_REGS_LEN    (sizeof(struct pch_gbe_regs) / 4)
74 #define PCH_GBE_REGS_LEN        (PCH_GBE_MAC_REGS_LEN + PCH_GBE_PHY_REGS_LEN)
75 /**
76  * pch_gbe_get_settings - Get device-specific settings
77  * @netdev: Network interface device structure
78  * @ecmd:   Ethtool command
79  * Returns:
80  *      0:                      Successful.
81  *      Negative value:         Failed.
82  */
83 static int pch_gbe_get_settings(struct net_device *netdev,
84                                  struct ethtool_cmd *ecmd)
85 {
86         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
87         int ret;
88
89         ret = mii_ethtool_gset(&adapter->mii, ecmd);
90         ecmd->supported &= ~(SUPPORTED_TP | SUPPORTED_1000baseT_Half);
91         ecmd->advertising &= ~(ADVERTISED_TP | ADVERTISED_1000baseT_Half);
92
93         if (!netif_carrier_ok(adapter->netdev))
94                 ethtool_cmd_speed_set(ecmd, -1);
95         return ret;
96 }
97
98 /**
99  * pch_gbe_set_settings - Set device-specific settings
100  * @netdev: Network interface device structure
101  * @ecmd:   Ethtool command
102  * Returns:
103  *      0:                      Successful.
104  *      Negative value:         Failed.
105  */
106 static int pch_gbe_set_settings(struct net_device *netdev,
107                                  struct ethtool_cmd *ecmd)
108 {
109         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
110         struct pch_gbe_hw *hw = &adapter->hw;
111         u32 speed = ethtool_cmd_speed(ecmd);
112         int ret;
113
114         pch_gbe_hal_write_phy_reg(hw, MII_BMCR, BMCR_RESET);
115
116         /* when set_settings() is called with a ethtool_cmd previously
117          * filled by get_settings() on a down link, speed is -1: */
118         if (speed == UINT_MAX) {
119                 speed = SPEED_1000;
120                 ethtool_cmd_speed_set(ecmd, speed);
121                 ecmd->duplex = DUPLEX_FULL;
122         }
123         ret = mii_ethtool_sset(&adapter->mii, ecmd);
124         if (ret) {
125                 netdev_err(netdev, "Error: mii_ethtool_sset\n");
126                 return ret;
127         }
128         hw->mac.link_speed = speed;
129         hw->mac.link_duplex = ecmd->duplex;
130         hw->phy.autoneg_advertised = ecmd->advertising;
131         hw->mac.autoneg = ecmd->autoneg;
132
133         /* reset the link */
134         if (netif_running(adapter->netdev)) {
135                 pch_gbe_down(adapter);
136                 ret = pch_gbe_up(adapter);
137         } else {
138                 pch_gbe_reset(adapter);
139         }
140         return ret;
141 }
142
143 /**
144  * pch_gbe_get_regs_len - Report the size of device registers
145  * @netdev: Network interface device structure
146  * Returns: the size of device registers.
147  */
148 static int pch_gbe_get_regs_len(struct net_device *netdev)
149 {
150         return PCH_GBE_REGS_LEN * (int)sizeof(u32);
151 }
152
153 /**
154  * pch_gbe_get_drvinfo - Report driver information
155  * @netdev:  Network interface device structure
156  * @drvinfo: Driver information structure
157  */
158 static void pch_gbe_get_drvinfo(struct net_device *netdev,
159                                  struct ethtool_drvinfo *drvinfo)
160 {
161         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
162
163         strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
164         strlcpy(drvinfo->version, pch_driver_version, sizeof(drvinfo->version));
165         strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
166                 sizeof(drvinfo->bus_info));
167         drvinfo->regdump_len = pch_gbe_get_regs_len(netdev);
168 }
169
170 /**
171  * pch_gbe_get_regs - Get device registers
172  * @netdev: Network interface device structure
173  * @regs:   Ethtool register structure
174  * @p:      Buffer pointer of read device register date
175  */
176 static void pch_gbe_get_regs(struct net_device *netdev,
177                                 struct ethtool_regs *regs, void *p)
178 {
179         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
180         struct pch_gbe_hw *hw = &adapter->hw;
181         struct pci_dev *pdev = adapter->pdev;
182         u32 *regs_buff = p;
183         u16 i, tmp;
184
185         regs->version = 0x1000000 | (__u32)pdev->revision << 16 | pdev->device;
186         for (i = 0; i < PCH_GBE_MAC_REGS_LEN; i++)
187                 *regs_buff++ = ioread32(&hw->reg->INT_ST + i);
188         /* PHY register */
189         for (i = 0; i < PCH_GBE_PHY_REGS_LEN; i++) {
190                 pch_gbe_hal_read_phy_reg(&adapter->hw, i, &tmp);
191                 *regs_buff++ = tmp;
192         }
193 }
194
195 /**
196  * pch_gbe_get_wol - Report whether Wake-on-Lan is enabled
197  * @netdev: Network interface device structure
198  * @wol:    Wake-on-Lan information
199  */
200 static void pch_gbe_get_wol(struct net_device *netdev,
201                                 struct ethtool_wolinfo *wol)
202 {
203         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
204
205         wol->supported = WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC;
206         wol->wolopts = 0;
207
208         if ((adapter->wake_up_evt & PCH_GBE_WLC_IND))
209                 wol->wolopts |= WAKE_UCAST;
210         if ((adapter->wake_up_evt & PCH_GBE_WLC_MLT))
211                 wol->wolopts |= WAKE_MCAST;
212         if ((adapter->wake_up_evt & PCH_GBE_WLC_BR))
213                 wol->wolopts |= WAKE_BCAST;
214         if ((adapter->wake_up_evt & PCH_GBE_WLC_MP))
215                 wol->wolopts |= WAKE_MAGIC;
216 }
217
218 /**
219  * pch_gbe_set_wol - Turn Wake-on-Lan on or off
220  * @netdev: Network interface device structure
221  * @wol:    Pointer of wake-on-Lan information straucture
222  * Returns:
223  *      0:                      Successful.
224  *      Negative value:         Failed.
225  */
226 static int pch_gbe_set_wol(struct net_device *netdev,
227                                 struct ethtool_wolinfo *wol)
228 {
229         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
230
231         if ((wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE)))
232                 return -EOPNOTSUPP;
233         /* these settings will always override what we currently have */
234         adapter->wake_up_evt = 0;
235
236         if ((wol->wolopts & WAKE_UCAST))
237                 adapter->wake_up_evt |= PCH_GBE_WLC_IND;
238         if ((wol->wolopts & WAKE_MCAST))
239                 adapter->wake_up_evt |= PCH_GBE_WLC_MLT;
240         if ((wol->wolopts & WAKE_BCAST))
241                 adapter->wake_up_evt |= PCH_GBE_WLC_BR;
242         if ((wol->wolopts & WAKE_MAGIC))
243                 adapter->wake_up_evt |= PCH_GBE_WLC_MP;
244         return 0;
245 }
246
247 /**
248  * pch_gbe_nway_reset - Restart autonegotiation
249  * @netdev: Network interface device structure
250  * Returns:
251  *      0:                      Successful.
252  *      Negative value:         Failed.
253  */
254 static int pch_gbe_nway_reset(struct net_device *netdev)
255 {
256         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
257
258         return mii_nway_restart(&adapter->mii);
259 }
260
261 /**
262  * pch_gbe_get_ringparam - Report ring sizes
263  * @netdev:  Network interface device structure
264  * @ring:    Ring param structure
265  */
266 static void pch_gbe_get_ringparam(struct net_device *netdev,
267                                         struct ethtool_ringparam *ring)
268 {
269         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
270         struct pch_gbe_tx_ring *txdr = adapter->tx_ring;
271         struct pch_gbe_rx_ring *rxdr = adapter->rx_ring;
272
273         ring->rx_max_pending = PCH_GBE_MAX_RXD;
274         ring->tx_max_pending = PCH_GBE_MAX_TXD;
275         ring->rx_pending = rxdr->count;
276         ring->tx_pending = txdr->count;
277 }
278
279 /**
280  * pch_gbe_set_ringparam - Set ring sizes
281  * @netdev:  Network interface device structure
282  * @ring:    Ring param structure
283  * Returns
284  *      0:                      Successful.
285  *      Negative value:         Failed.
286  */
287 static int pch_gbe_set_ringparam(struct net_device *netdev,
288                                         struct ethtool_ringparam *ring)
289 {
290         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
291         struct pch_gbe_tx_ring *txdr, *tx_old;
292         struct pch_gbe_rx_ring *rxdr, *rx_old;
293         int tx_ring_size, rx_ring_size;
294         int err = 0;
295
296         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
297                 return -EINVAL;
298         tx_ring_size = (int)sizeof(struct pch_gbe_tx_ring);
299         rx_ring_size = (int)sizeof(struct pch_gbe_rx_ring);
300
301         if ((netif_running(adapter->netdev)))
302                 pch_gbe_down(adapter);
303         tx_old = adapter->tx_ring;
304         rx_old = adapter->rx_ring;
305
306         txdr = kzalloc(tx_ring_size, GFP_KERNEL);
307         if (!txdr) {
308                 err = -ENOMEM;
309                 goto err_alloc_tx;
310         }
311         rxdr = kzalloc(rx_ring_size, GFP_KERNEL);
312         if (!rxdr) {
313                 err = -ENOMEM;
314                 goto err_alloc_rx;
315         }
316         adapter->tx_ring = txdr;
317         adapter->rx_ring = rxdr;
318
319         rxdr->count =
320                 clamp_val(ring->rx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD);
321         rxdr->count = roundup(rxdr->count, PCH_GBE_RX_DESC_MULTIPLE);
322
323         txdr->count =
324                 clamp_val(ring->tx_pending, PCH_GBE_MIN_RXD, PCH_GBE_MAX_RXD);
325         txdr->count = roundup(txdr->count, PCH_GBE_TX_DESC_MULTIPLE);
326
327         if ((netif_running(adapter->netdev))) {
328                 /* Try to get new resources before deleting old */
329                 err = pch_gbe_setup_rx_resources(adapter, adapter->rx_ring);
330                 if (err)
331                         goto err_setup_rx;
332                 err = pch_gbe_setup_tx_resources(adapter, adapter->tx_ring);
333                 if (err)
334                         goto err_setup_tx;
335                 /* save the new, restore the old in order to free it,
336                  * then restore the new back again */
337 #ifdef RINGFREE
338                 adapter->rx_ring = rx_old;
339                 adapter->tx_ring = tx_old;
340                 pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
341                 pch_gbe_free_tx_resources(adapter, adapter->tx_ring);
342                 kfree(tx_old);
343                 kfree(rx_old);
344                 adapter->rx_ring = rxdr;
345                 adapter->tx_ring = txdr;
346 #else
347                 pch_gbe_free_rx_resources(adapter, rx_old);
348                 pch_gbe_free_tx_resources(adapter, tx_old);
349                 kfree(tx_old);
350                 kfree(rx_old);
351                 adapter->rx_ring = rxdr;
352                 adapter->tx_ring = txdr;
353 #endif
354                 err = pch_gbe_up(adapter);
355         }
356         return err;
357
358 err_setup_tx:
359         pch_gbe_free_rx_resources(adapter, adapter->rx_ring);
360 err_setup_rx:
361         adapter->rx_ring = rx_old;
362         adapter->tx_ring = tx_old;
363         kfree(rxdr);
364 err_alloc_rx:
365         kfree(txdr);
366 err_alloc_tx:
367         if (netif_running(adapter->netdev))
368                 pch_gbe_up(adapter);
369         return err;
370 }
371
372 /**
373  * pch_gbe_get_pauseparam - Report pause parameters
374  * @netdev:  Network interface device structure
375  * @pause:   Pause parameters structure
376  */
377 static void pch_gbe_get_pauseparam(struct net_device *netdev,
378                                        struct ethtool_pauseparam *pause)
379 {
380         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
381         struct pch_gbe_hw *hw = &adapter->hw;
382
383         pause->autoneg =
384             ((hw->mac.fc_autoneg) ? AUTONEG_ENABLE : AUTONEG_DISABLE);
385
386         if (hw->mac.fc == PCH_GBE_FC_RX_PAUSE) {
387                 pause->rx_pause = 1;
388         } else if (hw->mac.fc == PCH_GBE_FC_TX_PAUSE) {
389                 pause->tx_pause = 1;
390         } else if (hw->mac.fc == PCH_GBE_FC_FULL) {
391                 pause->rx_pause = 1;
392                 pause->tx_pause = 1;
393         }
394 }
395
396 /**
397  * pch_gbe_set_pauseparam - Set pause paramters
398  * @netdev:  Network interface device structure
399  * @pause:   Pause parameters structure
400  * Returns:
401  *      0:                      Successful.
402  *      Negative value:         Failed.
403  */
404 static int pch_gbe_set_pauseparam(struct net_device *netdev,
405                                        struct ethtool_pauseparam *pause)
406 {
407         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
408         struct pch_gbe_hw *hw = &adapter->hw;
409         int ret = 0;
410
411         hw->mac.fc_autoneg = pause->autoneg;
412         if ((pause->rx_pause) && (pause->tx_pause))
413                 hw->mac.fc = PCH_GBE_FC_FULL;
414         else if ((pause->rx_pause) && (!pause->tx_pause))
415                 hw->mac.fc = PCH_GBE_FC_RX_PAUSE;
416         else if ((!pause->rx_pause) && (pause->tx_pause))
417                 hw->mac.fc = PCH_GBE_FC_TX_PAUSE;
418         else if ((!pause->rx_pause) && (!pause->tx_pause))
419                 hw->mac.fc = PCH_GBE_FC_NONE;
420
421         if (hw->mac.fc_autoneg == AUTONEG_ENABLE) {
422                 if ((netif_running(adapter->netdev))) {
423                         pch_gbe_down(adapter);
424                         ret = pch_gbe_up(adapter);
425                 } else {
426                         pch_gbe_reset(adapter);
427                 }
428         } else {
429                 ret = pch_gbe_mac_force_mac_fc(hw);
430         }
431         return ret;
432 }
433
434 /**
435  * pch_gbe_get_strings - Return a set of strings that describe the requested
436  *                       objects
437  * @netdev:    Network interface device structure
438  * @stringset: Select the stringset. [ETH_SS_TEST] [ETH_SS_STATS]
439  * @data:      Pointer of read string data.
440  */
441 static void pch_gbe_get_strings(struct net_device *netdev, u32 stringset,
442                                         u8 *data)
443 {
444         u8 *p = data;
445         int i;
446
447         switch (stringset) {
448         case (u32) ETH_SS_STATS:
449                 for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
450                         memcpy(p, pch_gbe_gstrings_stats[i].string,
451                                ETH_GSTRING_LEN);
452                         p += ETH_GSTRING_LEN;
453                 }
454                 break;
455         }
456 }
457
458 /**
459  * pch_gbe_get_ethtool_stats - Return statistics about the device
460  * @netdev: Network interface device structure
461  * @stats:  Ethtool statue structure
462  * @data:   Pointer of read status area
463  */
464 static void pch_gbe_get_ethtool_stats(struct net_device *netdev,
465                                   struct ethtool_stats *stats, u64 *data)
466 {
467         struct pch_gbe_adapter *adapter = netdev_priv(netdev);
468         int i;
469         const struct pch_gbe_stats *gstats = pch_gbe_gstrings_stats;
470         char *hw_stats = (char *)&adapter->stats;
471
472         pch_gbe_update_stats(adapter);
473         for (i = 0; i < PCH_GBE_GLOBAL_STATS_LEN; i++) {
474                 char *p = hw_stats + gstats->offset;
475                 data[i] = gstats->size == sizeof(u64) ? *(u64 *)p:(*(u32 *)p);
476                 gstats++;
477         }
478 }
479
480 static int pch_gbe_get_sset_count(struct net_device *netdev, int sset)
481 {
482         switch (sset) {
483         case ETH_SS_STATS:
484                 return PCH_GBE_STATS_LEN;
485         default:
486                 return -EOPNOTSUPP;
487         }
488 }
489
490 static const struct ethtool_ops pch_gbe_ethtool_ops = {
491         .get_settings = pch_gbe_get_settings,
492         .set_settings = pch_gbe_set_settings,
493         .get_drvinfo = pch_gbe_get_drvinfo,
494         .get_regs_len = pch_gbe_get_regs_len,
495         .get_regs = pch_gbe_get_regs,
496         .get_wol = pch_gbe_get_wol,
497         .set_wol = pch_gbe_set_wol,
498         .nway_reset = pch_gbe_nway_reset,
499         .get_link = ethtool_op_get_link,
500         .get_ringparam = pch_gbe_get_ringparam,
501         .set_ringparam = pch_gbe_set_ringparam,
502         .get_pauseparam = pch_gbe_get_pauseparam,
503         .set_pauseparam = pch_gbe_set_pauseparam,
504         .get_strings = pch_gbe_get_strings,
505         .get_ethtool_stats = pch_gbe_get_ethtool_stats,
506         .get_sset_count = pch_gbe_get_sset_count,
507 };
508
509 void pch_gbe_set_ethtool_ops(struct net_device *netdev)
510 {
511         SET_ETHTOOL_OPS(netdev, &pch_gbe_ethtool_ops);
512 }