altera_tse_ethtool.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Ethtool support for Altera Triple-Speed Ethernet MAC driver
  2. * Copyright (C) 2008-2014 Altera Corporation. All rights reserved
  3. *
  4. * Contributors:
  5. * Dalon Westergreen
  6. * Thomas Chou
  7. * Ian Abbott
  8. * Yuriy Kozlov
  9. * Tobias Klauser
  10. * Andriy Smolskyy
  11. * Roman Bulgakov
  12. * Dmytro Mytarchuk
  13. *
  14. * Original driver contributed by SLS.
  15. * Major updates contributed by GlobalLogic
  16. *
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms and conditions of the GNU General Public License,
  19. * version 2, as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope it will be useful, but WITHOUT
  22. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  23. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  24. * more details.
  25. *
  26. * You should have received a copy of the GNU General Public License along with
  27. * this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. #include <linux/ethtool.h>
  30. #include <linux/kernel.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/phy.h>
  33. #include "altera_tse.h"
  34. #define TSE_STATS_LEN 31
  35. #define TSE_NUM_REGS 128
  36. static char const stat_gstrings[][ETH_GSTRING_LEN] = {
  37. "tx_packets",
  38. "rx_packets",
  39. "rx_crc_errors",
  40. "rx_align_errors",
  41. "tx_bytes",
  42. "rx_bytes",
  43. "tx_pause",
  44. "rx_pause",
  45. "rx_errors",
  46. "tx_errors",
  47. "rx_unicast",
  48. "rx_multicast",
  49. "rx_broadcast",
  50. "tx_discards",
  51. "tx_unicast",
  52. "tx_multicast",
  53. "tx_broadcast",
  54. "ether_drops",
  55. "rx_total_bytes",
  56. "rx_total_packets",
  57. "rx_undersize",
  58. "rx_oversize",
  59. "rx_64_bytes",
  60. "rx_65_127_bytes",
  61. "rx_128_255_bytes",
  62. "rx_256_511_bytes",
  63. "rx_512_1023_bytes",
  64. "rx_1024_1518_bytes",
  65. "rx_gte_1519_bytes",
  66. "rx_jabbers",
  67. "rx_runts",
  68. };
  69. static void tse_get_drvinfo(struct net_device *dev,
  70. struct ethtool_drvinfo *info)
  71. {
  72. struct altera_tse_private *priv = netdev_priv(dev);
  73. u32 rev = ioread32(&priv->mac_dev->megacore_revision);
  74. strcpy(info->driver, "altera_tse");
  75. strcpy(info->version, "v8.0");
  76. snprintf(info->fw_version, ETHTOOL_FWVERS_LEN, "v%d.%d",
  77. rev & 0xFFFF, (rev & 0xFFFF0000) >> 16);
  78. sprintf(info->bus_info, "platform");
  79. }
  80. /* Fill in a buffer with the strings which correspond to the
  81. * stats
  82. */
  83. static void tse_gstrings(struct net_device *dev, u32 stringset, u8 *buf)
  84. {
  85. memcpy(buf, stat_gstrings, TSE_STATS_LEN * ETH_GSTRING_LEN);
  86. }
  87. static void tse_fill_stats(struct net_device *dev, struct ethtool_stats *dummy,
  88. u64 *buf)
  89. {
  90. struct altera_tse_private *priv = netdev_priv(dev);
  91. u64 ext;
  92. buf[0] = csrrd32(priv->mac_dev,
  93. tse_csroffs(frames_transmitted_ok));
  94. buf[1] = csrrd32(priv->mac_dev,
  95. tse_csroffs(frames_received_ok));
  96. buf[2] = csrrd32(priv->mac_dev,
  97. tse_csroffs(frames_check_sequence_errors));
  98. buf[3] = csrrd32(priv->mac_dev,
  99. tse_csroffs(alignment_errors));
  100. /* Extended aOctetsTransmittedOK counter */
  101. ext = (u64) csrrd32(priv->mac_dev,
  102. tse_csroffs(msb_octets_transmitted_ok)) << 32;
  103. ext |= csrrd32(priv->mac_dev,
  104. tse_csroffs(octets_transmitted_ok));
  105. buf[4] = ext;
  106. /* Extended aOctetsReceivedOK counter */
  107. ext = (u64) csrrd32(priv->mac_dev,
  108. tse_csroffs(msb_octets_received_ok)) << 32;
  109. ext |= csrrd32(priv->mac_dev,
  110. tse_csroffs(octets_received_ok));
  111. buf[5] = ext;
  112. buf[6] = csrrd32(priv->mac_dev,
  113. tse_csroffs(tx_pause_mac_ctrl_frames));
  114. buf[7] = csrrd32(priv->mac_dev,
  115. tse_csroffs(rx_pause_mac_ctrl_frames));
  116. buf[8] = csrrd32(priv->mac_dev,
  117. tse_csroffs(if_in_errors));
  118. buf[9] = csrrd32(priv->mac_dev,
  119. tse_csroffs(if_out_errors));
  120. buf[10] = csrrd32(priv->mac_dev,
  121. tse_csroffs(if_in_ucast_pkts));
  122. buf[11] = csrrd32(priv->mac_dev,
  123. tse_csroffs(if_in_multicast_pkts));
  124. buf[12] = csrrd32(priv->mac_dev,
  125. tse_csroffs(if_in_broadcast_pkts));
  126. buf[13] = csrrd32(priv->mac_dev,
  127. tse_csroffs(if_out_discards));
  128. buf[14] = csrrd32(priv->mac_dev,
  129. tse_csroffs(if_out_ucast_pkts));
  130. buf[15] = csrrd32(priv->mac_dev,
  131. tse_csroffs(if_out_multicast_pkts));
  132. buf[16] = csrrd32(priv->mac_dev,
  133. tse_csroffs(if_out_broadcast_pkts));
  134. buf[17] = csrrd32(priv->mac_dev,
  135. tse_csroffs(ether_stats_drop_events));
  136. /* Extended etherStatsOctets counter */
  137. ext = (u64) csrrd32(priv->mac_dev,
  138. tse_csroffs(msb_ether_stats_octets)) << 32;
  139. ext |= csrrd32(priv->mac_dev,
  140. tse_csroffs(ether_stats_octets));
  141. buf[18] = ext;
  142. buf[19] = csrrd32(priv->mac_dev,
  143. tse_csroffs(ether_stats_pkts));
  144. buf[20] = csrrd32(priv->mac_dev,
  145. tse_csroffs(ether_stats_undersize_pkts));
  146. buf[21] = csrrd32(priv->mac_dev,
  147. tse_csroffs(ether_stats_oversize_pkts));
  148. buf[22] = csrrd32(priv->mac_dev,
  149. tse_csroffs(ether_stats_pkts_64_octets));
  150. buf[23] = csrrd32(priv->mac_dev,
  151. tse_csroffs(ether_stats_pkts_65to127_octets));
  152. buf[24] = csrrd32(priv->mac_dev,
  153. tse_csroffs(ether_stats_pkts_128to255_octets));
  154. buf[25] = csrrd32(priv->mac_dev,
  155. tse_csroffs(ether_stats_pkts_256to511_octets));
  156. buf[26] = csrrd32(priv->mac_dev,
  157. tse_csroffs(ether_stats_pkts_512to1023_octets));
  158. buf[27] = csrrd32(priv->mac_dev,
  159. tse_csroffs(ether_stats_pkts_1024to1518_octets));
  160. buf[28] = csrrd32(priv->mac_dev,
  161. tse_csroffs(ether_stats_pkts_1519tox_octets));
  162. buf[29] = csrrd32(priv->mac_dev,
  163. tse_csroffs(ether_stats_jabbers));
  164. buf[30] = csrrd32(priv->mac_dev,
  165. tse_csroffs(ether_stats_fragments));
  166. }
  167. static int tse_sset_count(struct net_device *dev, int sset)
  168. {
  169. switch (sset) {
  170. case ETH_SS_STATS:
  171. return TSE_STATS_LEN;
  172. default:
  173. return -EOPNOTSUPP;
  174. }
  175. }
  176. static u32 tse_get_msglevel(struct net_device *dev)
  177. {
  178. struct altera_tse_private *priv = netdev_priv(dev);
  179. return priv->msg_enable;
  180. }
  181. static void tse_set_msglevel(struct net_device *dev, uint32_t data)
  182. {
  183. struct altera_tse_private *priv = netdev_priv(dev);
  184. priv->msg_enable = data;
  185. }
  186. static int tse_reglen(struct net_device *dev)
  187. {
  188. return TSE_NUM_REGS * sizeof(u32);
  189. }
  190. static void tse_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  191. void *regbuf)
  192. {
  193. int i;
  194. struct altera_tse_private *priv = netdev_priv(dev);
  195. u32 *buf = regbuf;
  196. /* Set version to a known value, so ethtool knows
  197. * how to do any special formatting of this data.
  198. * This version number will need to change if and
  199. * when this register table is changed.
  200. *
  201. * version[31:0] = 1: Dump the first 128 TSE Registers
  202. * Upper bits are all 0 by default
  203. *
  204. * Upper 16-bits will indicate feature presence for
  205. * Ethtool register decoding in future version.
  206. */
  207. regs->version = 1;
  208. for (i = 0; i < TSE_NUM_REGS; i++)
  209. buf[i] = csrrd32(priv->mac_dev, i * 4);
  210. }
  211. static int tse_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  212. {
  213. struct altera_tse_private *priv = netdev_priv(dev);
  214. struct phy_device *phydev = priv->phydev;
  215. if (phydev == NULL)
  216. return -ENODEV;
  217. return phy_ethtool_gset(phydev, cmd);
  218. }
  219. static int tse_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  220. {
  221. struct altera_tse_private *priv = netdev_priv(dev);
  222. struct phy_device *phydev = priv->phydev;
  223. if (phydev == NULL)
  224. return -ENODEV;
  225. return phy_ethtool_sset(phydev, cmd);
  226. }
  227. static const struct ethtool_ops tse_ethtool_ops = {
  228. .get_drvinfo = tse_get_drvinfo,
  229. .get_regs_len = tse_reglen,
  230. .get_regs = tse_get_regs,
  231. .get_link = ethtool_op_get_link,
  232. .get_settings = tse_get_settings,
  233. .set_settings = tse_set_settings,
  234. .get_strings = tse_gstrings,
  235. .get_sset_count = tse_sset_count,
  236. .get_ethtool_stats = tse_fill_stats,
  237. .get_msglevel = tse_get_msglevel,
  238. .set_msglevel = tse_set_msglevel,
  239. };
  240. void altera_tse_set_ethtool_ops(struct net_device *netdev)
  241. {
  242. netdev->ethtool_ops = &tse_ethtool_ops;
  243. }