xgene_enet_ethtool.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* Applied Micro X-Gene SoC Ethernet Driver
  2. *
  3. * Copyright (c) 2014, Applied Micro Circuits Corporation
  4. * Authors: Iyappan Subramanian <isubramanian@apm.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  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 <linux/ethtool.h>
  20. #include "xgene_enet_main.h"
  21. struct xgene_gstrings_stats {
  22. char name[ETH_GSTRING_LEN];
  23. int offset;
  24. };
  25. #define XGENE_STAT(m) { #m, offsetof(struct xgene_enet_pdata, stats.m) }
  26. static const struct xgene_gstrings_stats gstrings_stats[] = {
  27. XGENE_STAT(rx_packets),
  28. XGENE_STAT(tx_packets),
  29. XGENE_STAT(rx_bytes),
  30. XGENE_STAT(tx_bytes),
  31. XGENE_STAT(rx_errors),
  32. XGENE_STAT(tx_errors),
  33. XGENE_STAT(rx_length_errors),
  34. XGENE_STAT(rx_crc_errors),
  35. XGENE_STAT(rx_frame_errors),
  36. XGENE_STAT(rx_fifo_errors)
  37. };
  38. #define XGENE_STATS_LEN ARRAY_SIZE(gstrings_stats)
  39. static void xgene_get_drvinfo(struct net_device *ndev,
  40. struct ethtool_drvinfo *info)
  41. {
  42. struct xgene_enet_pdata *pdata = netdev_priv(ndev);
  43. struct platform_device *pdev = pdata->pdev;
  44. strcpy(info->driver, "xgene_enet");
  45. strcpy(info->version, XGENE_DRV_VERSION);
  46. snprintf(info->fw_version, ETHTOOL_FWVERS_LEN, "N/A");
  47. sprintf(info->bus_info, "%s", pdev->name);
  48. }
  49. static int xgene_get_link_ksettings(struct net_device *ndev,
  50. struct ethtool_link_ksettings *cmd)
  51. {
  52. struct xgene_enet_pdata *pdata = netdev_priv(ndev);
  53. struct phy_device *phydev = ndev->phydev;
  54. u32 supported;
  55. if (pdata->phy_mode == PHY_INTERFACE_MODE_RGMII) {
  56. if (phydev == NULL)
  57. return -ENODEV;
  58. return phy_ethtool_ksettings_get(phydev, cmd);
  59. } else if (pdata->phy_mode == PHY_INTERFACE_MODE_SGMII) {
  60. if (pdata->mdio_driver) {
  61. if (!phydev)
  62. return -ENODEV;
  63. return phy_ethtool_ksettings_get(phydev, cmd);
  64. }
  65. supported = SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
  66. SUPPORTED_MII;
  67. ethtool_convert_legacy_u32_to_link_mode(
  68. cmd->link_modes.supported,
  69. supported);
  70. ethtool_convert_legacy_u32_to_link_mode(
  71. cmd->link_modes.advertising,
  72. supported);
  73. cmd->base.speed = SPEED_1000;
  74. cmd->base.duplex = DUPLEX_FULL;
  75. cmd->base.port = PORT_MII;
  76. cmd->base.autoneg = AUTONEG_ENABLE;
  77. } else {
  78. supported = SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE;
  79. ethtool_convert_legacy_u32_to_link_mode(
  80. cmd->link_modes.supported,
  81. supported);
  82. ethtool_convert_legacy_u32_to_link_mode(
  83. cmd->link_modes.advertising,
  84. supported);
  85. cmd->base.speed = SPEED_10000;
  86. cmd->base.duplex = DUPLEX_FULL;
  87. cmd->base.port = PORT_FIBRE;
  88. cmd->base.autoneg = AUTONEG_DISABLE;
  89. }
  90. return 0;
  91. }
  92. static int xgene_set_link_ksettings(struct net_device *ndev,
  93. const struct ethtool_link_ksettings *cmd)
  94. {
  95. struct xgene_enet_pdata *pdata = netdev_priv(ndev);
  96. struct phy_device *phydev = ndev->phydev;
  97. if (pdata->phy_mode == PHY_INTERFACE_MODE_RGMII) {
  98. if (!phydev)
  99. return -ENODEV;
  100. return phy_ethtool_ksettings_set(phydev, cmd);
  101. }
  102. if (pdata->phy_mode == PHY_INTERFACE_MODE_SGMII) {
  103. if (pdata->mdio_driver) {
  104. if (!phydev)
  105. return -ENODEV;
  106. return phy_ethtool_ksettings_set(phydev, cmd);
  107. }
  108. }
  109. return -EINVAL;
  110. }
  111. static void xgene_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  112. {
  113. int i;
  114. u8 *p = data;
  115. if (stringset != ETH_SS_STATS)
  116. return;
  117. for (i = 0; i < XGENE_STATS_LEN; i++) {
  118. memcpy(p, gstrings_stats[i].name, ETH_GSTRING_LEN);
  119. p += ETH_GSTRING_LEN;
  120. }
  121. }
  122. static int xgene_get_sset_count(struct net_device *ndev, int sset)
  123. {
  124. if (sset != ETH_SS_STATS)
  125. return -EINVAL;
  126. return XGENE_STATS_LEN;
  127. }
  128. static void xgene_get_ethtool_stats(struct net_device *ndev,
  129. struct ethtool_stats *dummy,
  130. u64 *data)
  131. {
  132. void *pdata = netdev_priv(ndev);
  133. int i;
  134. for (i = 0; i < XGENE_STATS_LEN; i++)
  135. *data++ = *(u64 *)(pdata + gstrings_stats[i].offset);
  136. }
  137. static void xgene_get_pauseparam(struct net_device *ndev,
  138. struct ethtool_pauseparam *pp)
  139. {
  140. struct xgene_enet_pdata *pdata = netdev_priv(ndev);
  141. pp->autoneg = pdata->pause_autoneg;
  142. pp->tx_pause = pdata->tx_pause;
  143. pp->rx_pause = pdata->rx_pause;
  144. }
  145. static int xgene_set_pauseparam(struct net_device *ndev,
  146. struct ethtool_pauseparam *pp)
  147. {
  148. struct xgene_enet_pdata *pdata = netdev_priv(ndev);
  149. struct phy_device *phydev = ndev->phydev;
  150. u32 oldadv, newadv;
  151. if (pdata->phy_mode == PHY_INTERFACE_MODE_RGMII ||
  152. pdata->phy_mode == PHY_INTERFACE_MODE_SGMII) {
  153. if (!phydev)
  154. return -EINVAL;
  155. if (!(phydev->supported & SUPPORTED_Pause) ||
  156. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  157. pp->rx_pause != pp->tx_pause))
  158. return -EINVAL;
  159. pdata->pause_autoneg = pp->autoneg;
  160. pdata->tx_pause = pp->tx_pause;
  161. pdata->rx_pause = pp->rx_pause;
  162. oldadv = phydev->advertising;
  163. newadv = oldadv & ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  164. if (pp->rx_pause)
  165. newadv |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
  166. if (pp->tx_pause)
  167. newadv ^= ADVERTISED_Asym_Pause;
  168. if (oldadv ^ newadv) {
  169. phydev->advertising = newadv;
  170. if (phydev->autoneg)
  171. return phy_start_aneg(phydev);
  172. if (!pp->autoneg) {
  173. pdata->mac_ops->flowctl_tx(pdata,
  174. pdata->tx_pause);
  175. pdata->mac_ops->flowctl_rx(pdata,
  176. pdata->rx_pause);
  177. }
  178. }
  179. } else {
  180. if (pp->autoneg)
  181. return -EINVAL;
  182. pdata->tx_pause = pp->tx_pause;
  183. pdata->rx_pause = pp->rx_pause;
  184. pdata->mac_ops->flowctl_tx(pdata, pdata->tx_pause);
  185. pdata->mac_ops->flowctl_rx(pdata, pdata->rx_pause);
  186. }
  187. return 0;
  188. }
  189. static const struct ethtool_ops xgene_ethtool_ops = {
  190. .get_drvinfo = xgene_get_drvinfo,
  191. .get_link = ethtool_op_get_link,
  192. .get_strings = xgene_get_strings,
  193. .get_sset_count = xgene_get_sset_count,
  194. .get_ethtool_stats = xgene_get_ethtool_stats,
  195. .get_link_ksettings = xgene_get_link_ksettings,
  196. .set_link_ksettings = xgene_set_link_ksettings,
  197. .get_pauseparam = xgene_get_pauseparam,
  198. .set_pauseparam = xgene_set_pauseparam
  199. };
  200. void xgene_enet_set_ethtool_ops(struct net_device *ndev)
  201. {
  202. ndev->ethtool_ops = &xgene_ethtool_ops;
  203. }