Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pch_gbe_ethtool.c
Go to the documentation of this file.
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, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  */
20 #include "pch_gbe.h"
21 #include "pch_gbe_api.h"
22 
26 struct pch_gbe_stats {
27  char string[ETH_GSTRING_LEN];
28  size_t size;
29  size_t offset;
30 };
31 
32 #define PCH_GBE_STAT(m) \
33 { \
34  .string = #m, \
35  .size = FIELD_SIZEOF(struct pch_gbe_hw_stats, m), \
36  .offset = offsetof(struct pch_gbe_hw_stats, m), \
37 }
38 
42 static const struct pch_gbe_stats pch_gbe_gstrings_stats[] = {
51  PCH_GBE_STAT(multicast),
53  PCH_GBE_STAT(rx_crc_errors),
54  PCH_GBE_STAT(rx_frame_errors),
55  PCH_GBE_STAT(rx_alloc_buff_failed),
56  PCH_GBE_STAT(tx_length_errors),
57  PCH_GBE_STAT(tx_aborted_errors),
58  PCH_GBE_STAT(tx_carrier_errors),
59  PCH_GBE_STAT(tx_timeout_count),
60  PCH_GBE_STAT(tx_restart_count),
61  PCH_GBE_STAT(intr_rx_dsc_empty_count),
62  PCH_GBE_STAT(intr_rx_frame_err_count),
63  PCH_GBE_STAT(intr_rx_fifo_err_count),
64  PCH_GBE_STAT(intr_rx_dma_err_count),
65  PCH_GBE_STAT(intr_tx_fifo_err_count),
66  PCH_GBE_STAT(intr_tx_dma_err_count),
67  PCH_GBE_STAT(intr_tcpip_err_count)
68 };
69 
70 #define PCH_GBE_QUEUE_STATS_LEN 0
71 #define PCH_GBE_GLOBAL_STATS_LEN ARRAY_SIZE(pch_gbe_gstrings_stats)
72 #define PCH_GBE_STATS_LEN (PCH_GBE_GLOBAL_STATS_LEN + PCH_GBE_QUEUE_STATS_LEN)
73 
74 #define PCH_GBE_MAC_REGS_LEN (sizeof(struct pch_gbe_regs) / 4)
75 #define PCH_GBE_REGS_LEN (PCH_GBE_MAC_REGS_LEN + PCH_GBE_PHY_REGS_LEN)
76 
84 static int pch_gbe_get_settings(struct net_device *netdev,
85  struct ethtool_cmd *ecmd)
86 {
87  struct pch_gbe_adapter *adapter = netdev_priv(netdev);
88  int ret;
89 
90  ret = mii_ethtool_gset(&adapter->mii, ecmd);
93 
94  if (!netif_carrier_ok(adapter->netdev))
95  ethtool_cmd_speed_set(ecmd, -1);
96  return ret;
97 }
98 
107 static int pch_gbe_set_settings(struct net_device *netdev,
108  struct ethtool_cmd *ecmd)
109 {
110  struct pch_gbe_adapter *adapter = netdev_priv(netdev);
111  struct pch_gbe_hw *hw = &adapter->hw;
112  u32 speed = ethtool_cmd_speed(ecmd);
113  int ret;
114 
116 
117  /* when set_settings() is called with a ethtool_cmd previously
118  * filled by get_settings() on a down link, speed is -1: */
119  if (speed == UINT_MAX) {
120  speed = SPEED_1000;
121  ecmd->duplex = DUPLEX_FULL;
122  }
123  ret = mii_ethtool_sset(&adapter->mii, ecmd);
124  if (ret) {
125  pr_err("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 
148 static int pch_gbe_get_regs_len(struct net_device *netdev)
149 {
150  return PCH_GBE_REGS_LEN * (int)sizeof(u32);
151 }
152 
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 
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 
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 
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 
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 
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 
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 
275  ring->rx_pending = rxdr->count;
276  ring->tx_pending = txdr->count;
277 }
278 
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 =
322 
323  txdr->count =
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 
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 
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 
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,
452  p += ETH_GSTRING_LEN;
453  }
454  break;
455  }
456 }
457 
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 
510 {
511  SET_ETHTOOL_OPS(netdev, &pch_gbe_ethtool_ops);
512 }