tsnep_ethtool.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2021 Gerhard Engleder <[email protected]> */
  3. #include "tsnep.h"
  4. static const char tsnep_stats_strings[][ETH_GSTRING_LEN] = {
  5. "rx_packets",
  6. "rx_bytes",
  7. "rx_dropped",
  8. "rx_multicast",
  9. "rx_phy_errors",
  10. "rx_forwarded_phy_errors",
  11. "rx_invalid_frame_errors",
  12. "tx_packets",
  13. "tx_bytes",
  14. "tx_dropped",
  15. };
  16. struct tsnep_stats {
  17. u64 rx_packets;
  18. u64 rx_bytes;
  19. u64 rx_dropped;
  20. u64 rx_multicast;
  21. u64 rx_phy_errors;
  22. u64 rx_forwarded_phy_errors;
  23. u64 rx_invalid_frame_errors;
  24. u64 tx_packets;
  25. u64 tx_bytes;
  26. u64 tx_dropped;
  27. };
  28. #define TSNEP_STATS_COUNT (sizeof(struct tsnep_stats) / sizeof(u64))
  29. static const char tsnep_rx_queue_stats_strings[][ETH_GSTRING_LEN] = {
  30. "rx_%d_packets",
  31. "rx_%d_bytes",
  32. "rx_%d_dropped",
  33. "rx_%d_multicast",
  34. "rx_%d_no_descriptor_errors",
  35. "rx_%d_buffer_too_small_errors",
  36. "rx_%d_fifo_overflow_errors",
  37. "rx_%d_invalid_frame_errors",
  38. };
  39. struct tsnep_rx_queue_stats {
  40. u64 rx_packets;
  41. u64 rx_bytes;
  42. u64 rx_dropped;
  43. u64 rx_multicast;
  44. u64 rx_no_descriptor_errors;
  45. u64 rx_buffer_too_small_errors;
  46. u64 rx_fifo_overflow_errors;
  47. u64 rx_invalid_frame_errors;
  48. };
  49. #define TSNEP_RX_QUEUE_STATS_COUNT (sizeof(struct tsnep_rx_queue_stats) / \
  50. sizeof(u64))
  51. static const char tsnep_tx_queue_stats_strings[][ETH_GSTRING_LEN] = {
  52. "tx_%d_packets",
  53. "tx_%d_bytes",
  54. "tx_%d_dropped",
  55. };
  56. struct tsnep_tx_queue_stats {
  57. u64 tx_packets;
  58. u64 tx_bytes;
  59. u64 tx_dropped;
  60. };
  61. #define TSNEP_TX_QUEUE_STATS_COUNT (sizeof(struct tsnep_tx_queue_stats) / \
  62. sizeof(u64))
  63. static void tsnep_ethtool_get_drvinfo(struct net_device *netdev,
  64. struct ethtool_drvinfo *drvinfo)
  65. {
  66. struct tsnep_adapter *adapter = netdev_priv(netdev);
  67. strscpy(drvinfo->driver, TSNEP, sizeof(drvinfo->driver));
  68. strscpy(drvinfo->bus_info, dev_name(&adapter->pdev->dev),
  69. sizeof(drvinfo->bus_info));
  70. }
  71. static int tsnep_ethtool_get_regs_len(struct net_device *netdev)
  72. {
  73. struct tsnep_adapter *adapter = netdev_priv(netdev);
  74. int len;
  75. int num_additional_queues;
  76. len = TSNEP_MAC_SIZE;
  77. /* first queue pair is within TSNEP_MAC_SIZE, only queues additional to
  78. * the first queue pair extend the register length by TSNEP_QUEUE_SIZE
  79. */
  80. num_additional_queues =
  81. max(adapter->num_tx_queues, adapter->num_rx_queues) - 1;
  82. len += TSNEP_QUEUE_SIZE * num_additional_queues;
  83. return len;
  84. }
  85. static void tsnep_ethtool_get_regs(struct net_device *netdev,
  86. struct ethtool_regs *regs,
  87. void *p)
  88. {
  89. struct tsnep_adapter *adapter = netdev_priv(netdev);
  90. regs->version = 1;
  91. memcpy_fromio(p, adapter->addr, regs->len);
  92. }
  93. static u32 tsnep_ethtool_get_msglevel(struct net_device *netdev)
  94. {
  95. struct tsnep_adapter *adapter = netdev_priv(netdev);
  96. return adapter->msg_enable;
  97. }
  98. static void tsnep_ethtool_set_msglevel(struct net_device *netdev, u32 data)
  99. {
  100. struct tsnep_adapter *adapter = netdev_priv(netdev);
  101. adapter->msg_enable = data;
  102. }
  103. static void tsnep_ethtool_get_strings(struct net_device *netdev, u32 stringset,
  104. u8 *data)
  105. {
  106. struct tsnep_adapter *adapter = netdev_priv(netdev);
  107. int rx_count = adapter->num_rx_queues;
  108. int tx_count = adapter->num_tx_queues;
  109. int i, j;
  110. switch (stringset) {
  111. case ETH_SS_STATS:
  112. memcpy(data, tsnep_stats_strings, sizeof(tsnep_stats_strings));
  113. data += sizeof(tsnep_stats_strings);
  114. for (i = 0; i < rx_count; i++) {
  115. for (j = 0; j < TSNEP_RX_QUEUE_STATS_COUNT; j++) {
  116. snprintf(data, ETH_GSTRING_LEN,
  117. tsnep_rx_queue_stats_strings[j], i);
  118. data += ETH_GSTRING_LEN;
  119. }
  120. }
  121. for (i = 0; i < tx_count; i++) {
  122. for (j = 0; j < TSNEP_TX_QUEUE_STATS_COUNT; j++) {
  123. snprintf(data, ETH_GSTRING_LEN,
  124. tsnep_tx_queue_stats_strings[j], i);
  125. data += ETH_GSTRING_LEN;
  126. }
  127. }
  128. break;
  129. case ETH_SS_TEST:
  130. tsnep_ethtool_get_test_strings(data);
  131. break;
  132. }
  133. }
  134. static void tsnep_ethtool_get_ethtool_stats(struct net_device *netdev,
  135. struct ethtool_stats *stats,
  136. u64 *data)
  137. {
  138. struct tsnep_adapter *adapter = netdev_priv(netdev);
  139. int rx_count = adapter->num_rx_queues;
  140. int tx_count = adapter->num_tx_queues;
  141. struct tsnep_stats tsnep_stats;
  142. struct tsnep_rx_queue_stats tsnep_rx_queue_stats;
  143. struct tsnep_tx_queue_stats tsnep_tx_queue_stats;
  144. u32 reg;
  145. int i;
  146. memset(&tsnep_stats, 0, sizeof(tsnep_stats));
  147. for (i = 0; i < adapter->num_rx_queues; i++) {
  148. tsnep_stats.rx_packets += adapter->rx[i].packets;
  149. tsnep_stats.rx_bytes += adapter->rx[i].bytes;
  150. tsnep_stats.rx_dropped += adapter->rx[i].dropped;
  151. tsnep_stats.rx_multicast += adapter->rx[i].multicast;
  152. }
  153. reg = ioread32(adapter->addr + ECM_STAT);
  154. tsnep_stats.rx_phy_errors =
  155. (reg & ECM_STAT_RX_ERR_MASK) >> ECM_STAT_RX_ERR_SHIFT;
  156. tsnep_stats.rx_forwarded_phy_errors =
  157. (reg & ECM_STAT_FWD_RX_ERR_MASK) >> ECM_STAT_FWD_RX_ERR_SHIFT;
  158. tsnep_stats.rx_invalid_frame_errors =
  159. (reg & ECM_STAT_INV_FRM_MASK) >> ECM_STAT_INV_FRM_SHIFT;
  160. for (i = 0; i < adapter->num_tx_queues; i++) {
  161. tsnep_stats.tx_packets += adapter->tx[i].packets;
  162. tsnep_stats.tx_bytes += adapter->tx[i].bytes;
  163. tsnep_stats.tx_dropped += adapter->tx[i].dropped;
  164. }
  165. memcpy(data, &tsnep_stats, sizeof(tsnep_stats));
  166. data += TSNEP_STATS_COUNT;
  167. for (i = 0; i < rx_count; i++) {
  168. memset(&tsnep_rx_queue_stats, 0, sizeof(tsnep_rx_queue_stats));
  169. tsnep_rx_queue_stats.rx_packets = adapter->rx[i].packets;
  170. tsnep_rx_queue_stats.rx_bytes = adapter->rx[i].bytes;
  171. tsnep_rx_queue_stats.rx_dropped = adapter->rx[i].dropped;
  172. tsnep_rx_queue_stats.rx_multicast = adapter->rx[i].multicast;
  173. reg = ioread32(adapter->addr + TSNEP_QUEUE(i) +
  174. TSNEP_RX_STATISTIC);
  175. tsnep_rx_queue_stats.rx_no_descriptor_errors =
  176. (reg & TSNEP_RX_STATISTIC_NO_DESC_MASK) >>
  177. TSNEP_RX_STATISTIC_NO_DESC_SHIFT;
  178. tsnep_rx_queue_stats.rx_buffer_too_small_errors =
  179. (reg & TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_MASK) >>
  180. TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_SHIFT;
  181. tsnep_rx_queue_stats.rx_fifo_overflow_errors =
  182. (reg & TSNEP_RX_STATISTIC_FIFO_OVERFLOW_MASK) >>
  183. TSNEP_RX_STATISTIC_FIFO_OVERFLOW_SHIFT;
  184. tsnep_rx_queue_stats.rx_invalid_frame_errors =
  185. (reg & TSNEP_RX_STATISTIC_INVALID_FRAME_MASK) >>
  186. TSNEP_RX_STATISTIC_INVALID_FRAME_SHIFT;
  187. memcpy(data, &tsnep_rx_queue_stats,
  188. sizeof(tsnep_rx_queue_stats));
  189. data += TSNEP_RX_QUEUE_STATS_COUNT;
  190. }
  191. for (i = 0; i < tx_count; i++) {
  192. memset(&tsnep_tx_queue_stats, 0, sizeof(tsnep_tx_queue_stats));
  193. tsnep_tx_queue_stats.tx_packets += adapter->tx[i].packets;
  194. tsnep_tx_queue_stats.tx_bytes += adapter->tx[i].bytes;
  195. tsnep_tx_queue_stats.tx_dropped += adapter->tx[i].dropped;
  196. memcpy(data, &tsnep_tx_queue_stats,
  197. sizeof(tsnep_tx_queue_stats));
  198. data += TSNEP_TX_QUEUE_STATS_COUNT;
  199. }
  200. }
  201. static int tsnep_ethtool_get_sset_count(struct net_device *netdev, int sset)
  202. {
  203. struct tsnep_adapter *adapter = netdev_priv(netdev);
  204. int rx_count;
  205. int tx_count;
  206. switch (sset) {
  207. case ETH_SS_STATS:
  208. rx_count = adapter->num_rx_queues;
  209. tx_count = adapter->num_tx_queues;
  210. return TSNEP_STATS_COUNT +
  211. TSNEP_RX_QUEUE_STATS_COUNT * rx_count +
  212. TSNEP_TX_QUEUE_STATS_COUNT * tx_count;
  213. case ETH_SS_TEST:
  214. return tsnep_ethtool_get_test_count();
  215. default:
  216. return -EOPNOTSUPP;
  217. }
  218. }
  219. static int tsnep_ethtool_get_rxnfc(struct net_device *dev,
  220. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  221. {
  222. struct tsnep_adapter *adapter = netdev_priv(dev);
  223. switch (cmd->cmd) {
  224. case ETHTOOL_GRXRINGS:
  225. cmd->data = adapter->num_rx_queues;
  226. return 0;
  227. case ETHTOOL_GRXCLSRLCNT:
  228. cmd->rule_cnt = adapter->rxnfc_count;
  229. cmd->data = adapter->rxnfc_max;
  230. cmd->data |= RX_CLS_LOC_SPECIAL;
  231. return 0;
  232. case ETHTOOL_GRXCLSRULE:
  233. return tsnep_rxnfc_get_rule(adapter, cmd);
  234. case ETHTOOL_GRXCLSRLALL:
  235. return tsnep_rxnfc_get_all(adapter, cmd, rule_locs);
  236. default:
  237. return -EOPNOTSUPP;
  238. }
  239. }
  240. static int tsnep_ethtool_set_rxnfc(struct net_device *dev,
  241. struct ethtool_rxnfc *cmd)
  242. {
  243. struct tsnep_adapter *adapter = netdev_priv(dev);
  244. switch (cmd->cmd) {
  245. case ETHTOOL_SRXCLSRLINS:
  246. return tsnep_rxnfc_add_rule(adapter, cmd);
  247. case ETHTOOL_SRXCLSRLDEL:
  248. return tsnep_rxnfc_del_rule(adapter, cmd);
  249. default:
  250. return -EOPNOTSUPP;
  251. }
  252. }
  253. static int tsnep_ethtool_get_ts_info(struct net_device *dev,
  254. struct ethtool_ts_info *info)
  255. {
  256. struct tsnep_adapter *adapter = netdev_priv(dev);
  257. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  258. SOF_TIMESTAMPING_RX_SOFTWARE |
  259. SOF_TIMESTAMPING_SOFTWARE |
  260. SOF_TIMESTAMPING_TX_HARDWARE |
  261. SOF_TIMESTAMPING_RX_HARDWARE |
  262. SOF_TIMESTAMPING_RAW_HARDWARE;
  263. if (adapter->ptp_clock)
  264. info->phc_index = ptp_clock_index(adapter->ptp_clock);
  265. else
  266. info->phc_index = -1;
  267. info->tx_types = BIT(HWTSTAMP_TX_OFF) |
  268. BIT(HWTSTAMP_TX_ON);
  269. info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
  270. BIT(HWTSTAMP_FILTER_ALL);
  271. return 0;
  272. }
  273. const struct ethtool_ops tsnep_ethtool_ops = {
  274. .get_drvinfo = tsnep_ethtool_get_drvinfo,
  275. .get_regs_len = tsnep_ethtool_get_regs_len,
  276. .get_regs = tsnep_ethtool_get_regs,
  277. .get_msglevel = tsnep_ethtool_get_msglevel,
  278. .set_msglevel = tsnep_ethtool_set_msglevel,
  279. .nway_reset = phy_ethtool_nway_reset,
  280. .get_link = ethtool_op_get_link,
  281. .self_test = tsnep_ethtool_self_test,
  282. .get_strings = tsnep_ethtool_get_strings,
  283. .get_ethtool_stats = tsnep_ethtool_get_ethtool_stats,
  284. .get_sset_count = tsnep_ethtool_get_sset_count,
  285. .get_rxnfc = tsnep_ethtool_get_rxnfc,
  286. .set_rxnfc = tsnep_ethtool_set_rxnfc,
  287. .get_ts_info = tsnep_ethtool_get_ts_info,
  288. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  289. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  290. };