ralink_ethtool.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; version 2 of the License
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  14. *
  15. * Copyright (C) 2009-2013 Michael Lee <[email protected]>
  16. */
  17. #include "ralink_soc_eth.h"
  18. static const char fe_gdma_str[][ETH_GSTRING_LEN] = {
  19. #define _FE(x...) # x,
  20. FE_STAT_REG_DECLARE
  21. #undef _FE
  22. };
  23. static int fe_get_settings(struct net_device *dev,
  24. struct ethtool_cmd *cmd)
  25. {
  26. struct fe_priv *priv = netdev_priv(dev);
  27. int err;
  28. if (!priv->phy_dev)
  29. goto out_gset;
  30. if (priv->phy_flags == FE_PHY_FLAG_ATTACH) {
  31. err = phy_read_status(priv->phy_dev);
  32. if (err)
  33. goto out_gset;
  34. }
  35. return phy_ethtool_gset(priv->phy_dev, cmd);
  36. out_gset:
  37. return -ENODEV;
  38. }
  39. static int fe_set_settings(struct net_device *dev,
  40. struct ethtool_cmd *cmd)
  41. {
  42. struct fe_priv *priv = netdev_priv(dev);
  43. if (!priv->phy_dev)
  44. goto out_sset;
  45. if (cmd->phy_address != priv->phy_dev->addr) {
  46. if (priv->phy->phy_node[cmd->phy_address]) {
  47. priv->phy_dev = priv->phy->phy[cmd->phy_address];
  48. priv->phy_flags = FE_PHY_FLAG_PORT;
  49. } else if (priv->mii_bus &&
  50. priv->mii_bus->phy_map[cmd->phy_address]) {
  51. priv->phy_dev = priv->mii_bus->phy_map[cmd->phy_address];
  52. priv->phy_flags = FE_PHY_FLAG_ATTACH;
  53. } else
  54. goto out_sset;
  55. }
  56. return phy_ethtool_sset(priv->phy_dev, cmd);
  57. out_sset:
  58. return -ENODEV;
  59. }
  60. static void fe_get_drvinfo (struct net_device *dev,
  61. struct ethtool_drvinfo *info)
  62. {
  63. struct fe_priv *priv = netdev_priv(dev);
  64. struct fe_soc_data *soc = priv->soc;
  65. strlcpy(info->driver, priv->device->driver->name, sizeof(info->driver));
  66. strlcpy(info->version, FE_DRV_VERSION, sizeof(info->version));
  67. strlcpy(info->bus_info, dev_name(priv->device), sizeof(info->bus_info));
  68. if (soc->reg_table[FE_REG_FE_COUNTER_BASE])
  69. info->n_stats = ARRAY_SIZE(fe_gdma_str);
  70. }
  71. static u32 fe_get_msglevel(struct net_device *dev)
  72. {
  73. struct fe_priv *priv = netdev_priv(dev);
  74. return priv->msg_enable;
  75. }
  76. static void fe_set_msglevel(struct net_device *dev, u32 value)
  77. {
  78. struct fe_priv *priv = netdev_priv(dev);
  79. priv->msg_enable = value;
  80. }
  81. static int fe_nway_reset(struct net_device *dev)
  82. {
  83. struct fe_priv *priv = netdev_priv(dev);
  84. if (!priv->phy_dev)
  85. goto out_nway_reset;
  86. return genphy_restart_aneg(priv->phy_dev);
  87. out_nway_reset:
  88. return -EOPNOTSUPP;
  89. }
  90. static u32 fe_get_link(struct net_device *dev)
  91. {
  92. struct fe_priv *priv = netdev_priv(dev);
  93. int err;
  94. if (!priv->phy_dev)
  95. goto out_get_link;
  96. if (priv->phy_flags == FE_PHY_FLAG_ATTACH) {
  97. err = genphy_update_link(priv->phy_dev);
  98. if (err)
  99. goto out_get_link;
  100. }
  101. return priv->phy_dev->link;
  102. out_get_link:
  103. return ethtool_op_get_link(dev);
  104. }
  105. static int fe_set_ringparam(struct net_device *dev,
  106. struct ethtool_ringparam *ring)
  107. {
  108. struct fe_priv *priv = netdev_priv(dev);
  109. if ((ring->tx_pending < 2) ||
  110. (ring->rx_pending < 2) ||
  111. (ring->rx_pending > MAX_DMA_DESC) ||
  112. (ring->tx_pending > MAX_DMA_DESC))
  113. return -EINVAL;
  114. dev->netdev_ops->ndo_stop(dev);
  115. priv->tx_ring_size = BIT(fls(ring->tx_pending) - 1);
  116. priv->rx_ring_size = BIT(fls(ring->rx_pending) - 1);
  117. dev->netdev_ops->ndo_open(dev);
  118. return 0;
  119. }
  120. static void fe_get_ringparam(struct net_device *dev,
  121. struct ethtool_ringparam *ring)
  122. {
  123. struct fe_priv *priv = netdev_priv(dev);
  124. ring->rx_max_pending = MAX_DMA_DESC;
  125. ring->tx_max_pending = MAX_DMA_DESC;
  126. ring->rx_pending = priv->rx_ring_size;
  127. ring->tx_pending = priv->tx_ring_size;
  128. }
  129. static void fe_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  130. {
  131. switch (stringset) {
  132. case ETH_SS_STATS:
  133. memcpy(data, *fe_gdma_str, sizeof(fe_gdma_str));
  134. break;
  135. }
  136. }
  137. static int fe_get_sset_count(struct net_device *dev, int sset)
  138. {
  139. switch (sset) {
  140. case ETH_SS_STATS:
  141. return ARRAY_SIZE(fe_gdma_str);
  142. default:
  143. return -EOPNOTSUPP;
  144. }
  145. }
  146. static void fe_get_ethtool_stats(struct net_device *dev,
  147. struct ethtool_stats *stats, u64 *data)
  148. {
  149. struct fe_priv *priv = netdev_priv(dev);
  150. struct fe_hw_stats *hwstats = priv->hw_stats;
  151. u64 *data_src, *data_dst;
  152. unsigned int start;
  153. int i;
  154. if (netif_running(dev) && netif_device_present(dev)) {
  155. if (spin_trylock(&hwstats->stats_lock)) {
  156. fe_stats_update(priv);
  157. spin_unlock(&hwstats->stats_lock);
  158. }
  159. }
  160. do {
  161. data_src = &hwstats->tx_bytes;
  162. data_dst = data;
  163. start = u64_stats_fetch_begin_irq(&hwstats->syncp);
  164. for (i = 0; i < ARRAY_SIZE(fe_gdma_str); i++)
  165. *data_dst++ = *data_src++;
  166. } while (u64_stats_fetch_retry_irq(&hwstats->syncp, start));
  167. }
  168. static struct ethtool_ops fe_ethtool_ops = {
  169. .get_settings = fe_get_settings,
  170. .set_settings = fe_set_settings,
  171. .get_drvinfo = fe_get_drvinfo,
  172. .get_msglevel = fe_get_msglevel,
  173. .set_msglevel = fe_set_msglevel,
  174. .nway_reset = fe_nway_reset,
  175. .get_link = fe_get_link,
  176. .set_ringparam = fe_set_ringparam,
  177. .get_ringparam = fe_get_ringparam,
  178. };
  179. void fe_set_ethtool_ops(struct net_device *netdev)
  180. {
  181. struct fe_priv *priv = netdev_priv(netdev);
  182. struct fe_soc_data *soc = priv->soc;
  183. if (soc->reg_table[FE_REG_FE_COUNTER_BASE]) {
  184. fe_ethtool_ops.get_strings = fe_get_strings;
  185. fe_ethtool_ops.get_sset_count = fe_get_sset_count;
  186. fe_ethtool_ops.get_ethtool_stats = fe_get_ethtool_stats;
  187. }
  188. netdev->ethtool_ops = &fe_ethtool_ops;
  189. }