xgene_enet_ethtool.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
  50. {
  51. struct xgene_enet_pdata *pdata = netdev_priv(ndev);
  52. struct phy_device *phydev = pdata->phy_dev;
  53. if (pdata->phy_mode == PHY_INTERFACE_MODE_RGMII) {
  54. if (phydev == NULL)
  55. return -ENODEV;
  56. return phy_ethtool_gset(phydev, cmd);
  57. } else if (pdata->phy_mode == PHY_INTERFACE_MODE_SGMII) {
  58. cmd->supported = SUPPORTED_1000baseT_Full |
  59. SUPPORTED_Autoneg | SUPPORTED_MII;
  60. cmd->advertising = cmd->supported;
  61. ethtool_cmd_speed_set(cmd, SPEED_1000);
  62. cmd->duplex = DUPLEX_FULL;
  63. cmd->port = PORT_MII;
  64. cmd->transceiver = XCVR_INTERNAL;
  65. cmd->autoneg = AUTONEG_ENABLE;
  66. } else {
  67. cmd->supported = SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE;
  68. cmd->advertising = cmd->supported;
  69. ethtool_cmd_speed_set(cmd, SPEED_10000);
  70. cmd->duplex = DUPLEX_FULL;
  71. cmd->port = PORT_FIBRE;
  72. cmd->transceiver = XCVR_INTERNAL;
  73. cmd->autoneg = AUTONEG_DISABLE;
  74. }
  75. return 0;
  76. }
  77. static int xgene_set_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
  78. {
  79. struct xgene_enet_pdata *pdata = netdev_priv(ndev);
  80. struct phy_device *phydev = pdata->phy_dev;
  81. if (pdata->phy_mode == PHY_INTERFACE_MODE_RGMII) {
  82. if (phydev == NULL)
  83. return -ENODEV;
  84. return phy_ethtool_sset(phydev, cmd);
  85. }
  86. return -EINVAL;
  87. }
  88. static void xgene_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  89. {
  90. int i;
  91. u8 *p = data;
  92. if (stringset != ETH_SS_STATS)
  93. return;
  94. for (i = 0; i < XGENE_STATS_LEN; i++) {
  95. memcpy(p, gstrings_stats[i].name, ETH_GSTRING_LEN);
  96. p += ETH_GSTRING_LEN;
  97. }
  98. }
  99. static int xgene_get_sset_count(struct net_device *ndev, int sset)
  100. {
  101. if (sset != ETH_SS_STATS)
  102. return -EINVAL;
  103. return XGENE_STATS_LEN;
  104. }
  105. static void xgene_get_ethtool_stats(struct net_device *ndev,
  106. struct ethtool_stats *dummy,
  107. u64 *data)
  108. {
  109. void *pdata = netdev_priv(ndev);
  110. int i;
  111. for (i = 0; i < XGENE_STATS_LEN; i++)
  112. *data++ = *(u64 *)(pdata + gstrings_stats[i].offset);
  113. }
  114. static const struct ethtool_ops xgene_ethtool_ops = {
  115. .get_drvinfo = xgene_get_drvinfo,
  116. .get_settings = xgene_get_settings,
  117. .set_settings = xgene_set_settings,
  118. .get_link = ethtool_op_get_link,
  119. .get_strings = xgene_get_strings,
  120. .get_sset_count = xgene_get_sset_count,
  121. .get_ethtool_stats = xgene_get_ethtool_stats
  122. };
  123. void xgene_enet_set_ethtool_ops(struct net_device *ndev)
  124. {
  125. ndev->ethtool_ops = &xgene_ethtool_ops;
  126. }