emac-ethtool.c 6.4 KB

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