emac-ethtool.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Copyright (c) 2016, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/ethtool.h>
  13. #include <linux/phy.h>
  14. #include "emac.h"
  15. static const char * const emac_ethtool_stat_strings[] = {
  16. "rx_ok",
  17. "rx_bcast",
  18. "rx_mcast",
  19. "rx_pause",
  20. "rx_ctrl",
  21. "rx_fcs_err",
  22. "rx_len_err",
  23. "rx_byte_cnt",
  24. "rx_runt",
  25. "rx_frag",
  26. "rx_sz_64",
  27. "rx_sz_65_127",
  28. "rx_sz_128_255",
  29. "rx_sz_256_511",
  30. "rx_sz_512_1023",
  31. "rx_sz_1024_1518",
  32. "rx_sz_1519_max",
  33. "rx_sz_ov",
  34. "rx_rxf_ov",
  35. "rx_align_err",
  36. "rx_bcast_byte_cnt",
  37. "rx_mcast_byte_cnt",
  38. "rx_err_addr",
  39. "rx_crc_align",
  40. "rx_jabbers",
  41. "tx_ok",
  42. "tx_bcast",
  43. "tx_mcast",
  44. "tx_pause",
  45. "tx_exc_defer",
  46. "tx_ctrl",
  47. "tx_defer",
  48. "tx_byte_cnt",
  49. "tx_sz_64",
  50. "tx_sz_65_127",
  51. "tx_sz_128_255",
  52. "tx_sz_256_511",
  53. "tx_sz_512_1023",
  54. "tx_sz_1024_1518",
  55. "tx_sz_1519_max",
  56. "tx_1_col",
  57. "tx_2_col",
  58. "tx_late_col",
  59. "tx_abort_col",
  60. "tx_underrun",
  61. "tx_rd_eop",
  62. "tx_len_err",
  63. "tx_trunc",
  64. "tx_bcast_byte",
  65. "tx_mcast_byte",
  66. "tx_col",
  67. };
  68. #define EMAC_STATS_LEN ARRAY_SIZE(emac_ethtool_stat_strings)
  69. static u32 emac_get_msglevel(struct net_device *netdev)
  70. {
  71. struct emac_adapter *adpt = netdev_priv(netdev);
  72. return adpt->msg_enable;
  73. }
  74. static void emac_set_msglevel(struct net_device *netdev, u32 data)
  75. {
  76. struct emac_adapter *adpt = netdev_priv(netdev);
  77. adpt->msg_enable = data;
  78. }
  79. static int emac_get_sset_count(struct net_device *netdev, int sset)
  80. {
  81. switch (sset) {
  82. case ETH_SS_STATS:
  83. return EMAC_STATS_LEN;
  84. default:
  85. return -EOPNOTSUPP;
  86. }
  87. }
  88. static void emac_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  89. {
  90. unsigned int i;
  91. switch (stringset) {
  92. case ETH_SS_STATS:
  93. for (i = 0; i < EMAC_STATS_LEN; i++) {
  94. strlcpy(data, emac_ethtool_stat_strings[i],
  95. ETH_GSTRING_LEN);
  96. data += ETH_GSTRING_LEN;
  97. }
  98. break;
  99. }
  100. }
  101. static void emac_get_ethtool_stats(struct net_device *netdev,
  102. struct ethtool_stats *stats,
  103. u64 *data)
  104. {
  105. struct emac_adapter *adpt = netdev_priv(netdev);
  106. spin_lock(&adpt->stats.lock);
  107. emac_update_hw_stats(adpt);
  108. memcpy(data, &adpt->stats, EMAC_STATS_LEN * sizeof(u64));
  109. spin_unlock(&adpt->stats.lock);
  110. }
  111. static int emac_nway_reset(struct net_device *netdev)
  112. {
  113. struct phy_device *phydev = netdev->phydev;
  114. if (!phydev)
  115. return -ENODEV;
  116. return genphy_restart_aneg(phydev);
  117. }
  118. static void emac_get_ringparam(struct net_device *netdev,
  119. struct ethtool_ringparam *ring)
  120. {
  121. struct emac_adapter *adpt = netdev_priv(netdev);
  122. ring->rx_max_pending = EMAC_MAX_RX_DESCS;
  123. ring->tx_max_pending = EMAC_MAX_TX_DESCS;
  124. ring->rx_pending = adpt->rx_desc_cnt;
  125. ring->tx_pending = adpt->tx_desc_cnt;
  126. }
  127. static int emac_set_ringparam(struct net_device *netdev,
  128. struct ethtool_ringparam *ring)
  129. {
  130. struct emac_adapter *adpt = netdev_priv(netdev);
  131. /* We don't have separate queues/rings for small/large frames, so
  132. * reject any attempt to specify those values separately.
  133. */
  134. if (ring->rx_mini_pending || ring->rx_jumbo_pending)
  135. return -EINVAL;
  136. adpt->tx_desc_cnt =
  137. clamp_val(ring->tx_pending, EMAC_MIN_TX_DESCS, EMAC_MAX_TX_DESCS);
  138. adpt->rx_desc_cnt =
  139. clamp_val(ring->rx_pending, EMAC_MIN_RX_DESCS, EMAC_MAX_RX_DESCS);
  140. if (netif_running(netdev))
  141. return emac_reinit_locked(adpt);
  142. return 0;
  143. }
  144. static void emac_get_pauseparam(struct net_device *netdev,
  145. struct ethtool_pauseparam *pause)
  146. {
  147. struct emac_adapter *adpt = netdev_priv(netdev);
  148. pause->autoneg = adpt->automatic ? AUTONEG_ENABLE : AUTONEG_DISABLE;
  149. pause->rx_pause = adpt->rx_flow_control ? 1 : 0;
  150. pause->tx_pause = adpt->tx_flow_control ? 1 : 0;
  151. }
  152. static int emac_set_pauseparam(struct net_device *netdev,
  153. struct ethtool_pauseparam *pause)
  154. {
  155. struct emac_adapter *adpt = netdev_priv(netdev);
  156. adpt->automatic = pause->autoneg == AUTONEG_ENABLE;
  157. adpt->rx_flow_control = pause->rx_pause != 0;
  158. adpt->tx_flow_control = pause->tx_pause != 0;
  159. if (netif_running(netdev))
  160. return emac_reinit_locked(adpt);
  161. return 0;
  162. }
  163. /* Selected registers that might want to track during runtime. */
  164. static const u16 emac_regs[] = {
  165. EMAC_DMA_MAS_CTRL,
  166. EMAC_MAC_CTRL,
  167. EMAC_TXQ_CTRL_0,
  168. EMAC_RXQ_CTRL_0,
  169. EMAC_DMA_CTRL,
  170. EMAC_INT_MASK,
  171. EMAC_AXI_MAST_CTRL,
  172. EMAC_CORE_HW_VERSION,
  173. EMAC_MISC_CTRL,
  174. };
  175. /* Every time emac_regs[] above is changed, increase this version number. */
  176. #define EMAC_REGS_VERSION 0
  177. #define EMAC_MAX_REG_SIZE ARRAY_SIZE(emac_regs)
  178. static void emac_get_regs(struct net_device *netdev,
  179. struct ethtool_regs *regs, void *buff)
  180. {
  181. struct emac_adapter *adpt = netdev_priv(netdev);
  182. u32 *val = buff;
  183. unsigned int i;
  184. regs->version = EMAC_REGS_VERSION;
  185. regs->len = EMAC_MAX_REG_SIZE * sizeof(u32);
  186. for (i = 0; i < EMAC_MAX_REG_SIZE; i++)
  187. val[i] = readl(adpt->base + emac_regs[i]);
  188. }
  189. static int emac_get_regs_len(struct net_device *netdev)
  190. {
  191. return EMAC_MAX_REG_SIZE * sizeof(u32);
  192. }
  193. static const struct ethtool_ops emac_ethtool_ops = {
  194. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  195. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  196. .get_msglevel = emac_get_msglevel,
  197. .set_msglevel = emac_set_msglevel,
  198. .get_sset_count = emac_get_sset_count,
  199. .get_strings = emac_get_strings,
  200. .get_ethtool_stats = emac_get_ethtool_stats,
  201. .get_ringparam = emac_get_ringparam,
  202. .set_ringparam = emac_set_ringparam,
  203. .get_pauseparam = emac_get_pauseparam,
  204. .set_pauseparam = emac_set_pauseparam,
  205. .nway_reset = emac_nway_reset,
  206. .get_link = ethtool_op_get_link,
  207. .get_regs_len = emac_get_regs_len,
  208. .get_regs = emac_get_regs,
  209. };
  210. void emac_set_ethtool_ops(struct net_device *netdev)
  211. {
  212. netdev->ethtool_ops = &emac_ethtool_ops;
  213. }