tsnep_ethtool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2021 Gerhard Engleder <gerhard@engleder-embedded.com> */
  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_alloc_failed",
  10. "rx_phy_errors",
  11. "rx_forwarded_phy_errors",
  12. "rx_invalid_frame_errors",
  13. "tx_packets",
  14. "tx_bytes",
  15. "tx_dropped",
  16. };
  17. struct tsnep_stats {
  18. u64 rx_packets;
  19. u64 rx_bytes;
  20. u64 rx_dropped;
  21. u64 rx_multicast;
  22. u64 rx_alloc_failed;
  23. u64 rx_phy_errors;
  24. u64 rx_forwarded_phy_errors;
  25. u64 rx_invalid_frame_errors;
  26. u64 tx_packets;
  27. u64 tx_bytes;
  28. u64 tx_dropped;
  29. };
  30. #define TSNEP_STATS_COUNT (sizeof(struct tsnep_stats) / sizeof(u64))
  31. static const char tsnep_rx_queue_stats_strings[][ETH_GSTRING_LEN] = {
  32. "rx_%d_packets",
  33. "rx_%d_bytes",
  34. "rx_%d_dropped",
  35. "rx_%d_multicast",
  36. "rx_%d_alloc_failed",
  37. "rx_%d_no_descriptor_errors",
  38. "rx_%d_buffer_too_small_errors",
  39. "rx_%d_fifo_overflow_errors",
  40. "rx_%d_invalid_frame_errors",
  41. };
  42. struct tsnep_rx_queue_stats {
  43. u64 rx_packets;
  44. u64 rx_bytes;
  45. u64 rx_dropped;
  46. u64 rx_multicast;
  47. u64 rx_alloc_failed;
  48. u64 rx_no_descriptor_errors;
  49. u64 rx_buffer_too_small_errors;
  50. u64 rx_fifo_overflow_errors;
  51. u64 rx_invalid_frame_errors;
  52. };
  53. #define TSNEP_RX_QUEUE_STATS_COUNT (sizeof(struct tsnep_rx_queue_stats) / \
  54. sizeof(u64))
  55. static const char tsnep_tx_queue_stats_strings[][ETH_GSTRING_LEN] = {
  56. "tx_%d_packets",
  57. "tx_%d_bytes",
  58. "tx_%d_dropped",
  59. };
  60. struct tsnep_tx_queue_stats {
  61. u64 tx_packets;
  62. u64 tx_bytes;
  63. u64 tx_dropped;
  64. };
  65. #define TSNEP_TX_QUEUE_STATS_COUNT (sizeof(struct tsnep_tx_queue_stats) / \
  66. sizeof(u64))
  67. static void tsnep_ethtool_get_drvinfo(struct net_device *netdev,
  68. struct ethtool_drvinfo *drvinfo)
  69. {
  70. struct tsnep_adapter *adapter = netdev_priv(netdev);
  71. strscpy(drvinfo->driver, TSNEP, sizeof(drvinfo->driver));
  72. strscpy(drvinfo->bus_info, dev_name(&adapter->pdev->dev),
  73. sizeof(drvinfo->bus_info));
  74. }
  75. static int tsnep_ethtool_get_regs_len(struct net_device *netdev)
  76. {
  77. struct tsnep_adapter *adapter = netdev_priv(netdev);
  78. int len;
  79. int num_additional_queues;
  80. len = TSNEP_MAC_SIZE;
  81. /* first queue pair is within TSNEP_MAC_SIZE, only queues additional to
  82. * the first queue pair extend the register length by TSNEP_QUEUE_SIZE
  83. */
  84. num_additional_queues =
  85. max(adapter->num_tx_queues, adapter->num_rx_queues) - 1;
  86. len += TSNEP_QUEUE_SIZE * num_additional_queues;
  87. return len;
  88. }
  89. static void tsnep_ethtool_get_regs(struct net_device *netdev,
  90. struct ethtool_regs *regs,
  91. void *p)
  92. {
  93. struct tsnep_adapter *adapter = netdev_priv(netdev);
  94. regs->version = 1;
  95. memcpy_fromio(p, adapter->addr, regs->len);
  96. }
  97. static u32 tsnep_ethtool_get_msglevel(struct net_device *netdev)
  98. {
  99. struct tsnep_adapter *adapter = netdev_priv(netdev);
  100. return adapter->msg_enable;
  101. }
  102. static void tsnep_ethtool_set_msglevel(struct net_device *netdev, u32 data)
  103. {
  104. struct tsnep_adapter *adapter = netdev_priv(netdev);
  105. adapter->msg_enable = data;
  106. }
  107. static void tsnep_ethtool_get_strings(struct net_device *netdev, u32 stringset,
  108. u8 *data)
  109. {
  110. struct tsnep_adapter *adapter = netdev_priv(netdev);
  111. int rx_count = adapter->num_rx_queues;
  112. int tx_count = adapter->num_tx_queues;
  113. int i, j;
  114. switch (stringset) {
  115. case ETH_SS_STATS:
  116. memcpy(data, tsnep_stats_strings, sizeof(tsnep_stats_strings));
  117. data += sizeof(tsnep_stats_strings);
  118. for (i = 0; i < rx_count; i++) {
  119. for (j = 0; j < TSNEP_RX_QUEUE_STATS_COUNT; j++) {
  120. snprintf(data, ETH_GSTRING_LEN,
  121. tsnep_rx_queue_stats_strings[j], i);
  122. data += ETH_GSTRING_LEN;
  123. }
  124. }
  125. for (i = 0; i < tx_count; i++) {
  126. for (j = 0; j < TSNEP_TX_QUEUE_STATS_COUNT; j++) {
  127. snprintf(data, ETH_GSTRING_LEN,
  128. tsnep_tx_queue_stats_strings[j], i);
  129. data += ETH_GSTRING_LEN;
  130. }
  131. }
  132. break;
  133. case ETH_SS_TEST:
  134. tsnep_ethtool_get_test_strings(data);
  135. break;
  136. }
  137. }
  138. static void tsnep_ethtool_get_ethtool_stats(struct net_device *netdev,
  139. struct ethtool_stats *stats,
  140. u64 *data)
  141. {
  142. struct tsnep_adapter *adapter = netdev_priv(netdev);
  143. int rx_count = adapter->num_rx_queues;
  144. int tx_count = adapter->num_tx_queues;
  145. struct tsnep_stats tsnep_stats;
  146. struct tsnep_rx_queue_stats tsnep_rx_queue_stats;
  147. struct tsnep_tx_queue_stats tsnep_tx_queue_stats;
  148. u32 reg;
  149. int i;
  150. memset(&tsnep_stats, 0, sizeof(tsnep_stats));
  151. for (i = 0; i < adapter->num_rx_queues; i++) {
  152. tsnep_stats.rx_packets += adapter->rx[i].packets;
  153. tsnep_stats.rx_bytes += adapter->rx[i].bytes;
  154. tsnep_stats.rx_dropped += adapter->rx[i].dropped;
  155. tsnep_stats.rx_multicast += adapter->rx[i].multicast;
  156. tsnep_stats.rx_alloc_failed += adapter->rx[i].alloc_failed;
  157. }
  158. reg = ioread32(adapter->addr + ECM_STAT);
  159. tsnep_stats.rx_phy_errors =
  160. (reg & ECM_STAT_RX_ERR_MASK) >> ECM_STAT_RX_ERR_SHIFT;
  161. tsnep_stats.rx_forwarded_phy_errors =
  162. (reg & ECM_STAT_FWD_RX_ERR_MASK) >> ECM_STAT_FWD_RX_ERR_SHIFT;
  163. tsnep_stats.rx_invalid_frame_errors =
  164. (reg & ECM_STAT_INV_FRM_MASK) >> ECM_STAT_INV_FRM_SHIFT;
  165. for (i = 0; i < adapter->num_tx_queues; i++) {
  166. tsnep_stats.tx_packets += adapter->tx[i].packets;
  167. tsnep_stats.tx_bytes += adapter->tx[i].bytes;
  168. tsnep_stats.tx_dropped += adapter->tx[i].dropped;
  169. }
  170. memcpy(data, &tsnep_stats, sizeof(tsnep_stats));
  171. data += TSNEP_STATS_COUNT;
  172. for (i = 0; i < rx_count; i++) {
  173. memset(&tsnep_rx_queue_stats, 0, sizeof(tsnep_rx_queue_stats));
  174. tsnep_rx_queue_stats.rx_packets = adapter->rx[i].packets;
  175. tsnep_rx_queue_stats.rx_bytes = adapter->rx[i].bytes;
  176. tsnep_rx_queue_stats.rx_dropped = adapter->rx[i].dropped;
  177. tsnep_rx_queue_stats.rx_multicast = adapter->rx[i].multicast;
  178. tsnep_rx_queue_stats.rx_alloc_failed =
  179. adapter->rx[i].alloc_failed;
  180. reg = ioread32(adapter->addr + TSNEP_QUEUE(i) +
  181. TSNEP_RX_STATISTIC);
  182. tsnep_rx_queue_stats.rx_no_descriptor_errors =
  183. (reg & TSNEP_RX_STATISTIC_NO_DESC_MASK) >>
  184. TSNEP_RX_STATISTIC_NO_DESC_SHIFT;
  185. tsnep_rx_queue_stats.rx_buffer_too_small_errors =
  186. (reg & TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_MASK) >>
  187. TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_SHIFT;
  188. tsnep_rx_queue_stats.rx_fifo_overflow_errors =
  189. (reg & TSNEP_RX_STATISTIC_FIFO_OVERFLOW_MASK) >>
  190. TSNEP_RX_STATISTIC_FIFO_OVERFLOW_SHIFT;
  191. tsnep_rx_queue_stats.rx_invalid_frame_errors =
  192. (reg & TSNEP_RX_STATISTIC_INVALID_FRAME_MASK) >>
  193. TSNEP_RX_STATISTIC_INVALID_FRAME_SHIFT;
  194. memcpy(data, &tsnep_rx_queue_stats,
  195. sizeof(tsnep_rx_queue_stats));
  196. data += TSNEP_RX_QUEUE_STATS_COUNT;
  197. }
  198. for (i = 0; i < tx_count; i++) {
  199. memset(&tsnep_tx_queue_stats, 0, sizeof(tsnep_tx_queue_stats));
  200. tsnep_tx_queue_stats.tx_packets += adapter->tx[i].packets;
  201. tsnep_tx_queue_stats.tx_bytes += adapter->tx[i].bytes;
  202. tsnep_tx_queue_stats.tx_dropped += adapter->tx[i].dropped;
  203. memcpy(data, &tsnep_tx_queue_stats,
  204. sizeof(tsnep_tx_queue_stats));
  205. data += TSNEP_TX_QUEUE_STATS_COUNT;
  206. }
  207. }
  208. static int tsnep_ethtool_get_sset_count(struct net_device *netdev, int sset)
  209. {
  210. struct tsnep_adapter *adapter = netdev_priv(netdev);
  211. int rx_count;
  212. int tx_count;
  213. switch (sset) {
  214. case ETH_SS_STATS:
  215. rx_count = adapter->num_rx_queues;
  216. tx_count = adapter->num_tx_queues;
  217. return TSNEP_STATS_COUNT +
  218. TSNEP_RX_QUEUE_STATS_COUNT * rx_count +
  219. TSNEP_TX_QUEUE_STATS_COUNT * tx_count;
  220. case ETH_SS_TEST:
  221. return tsnep_ethtool_get_test_count();
  222. default:
  223. return -EOPNOTSUPP;
  224. }
  225. }
  226. static u32 tsnep_ethtool_get_rx_ring_count(struct net_device *netdev)
  227. {
  228. struct tsnep_adapter *adapter = netdev_priv(netdev);
  229. return adapter->num_rx_queues;
  230. }
  231. static int tsnep_ethtool_get_rxnfc(struct net_device *netdev,
  232. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  233. {
  234. struct tsnep_adapter *adapter = netdev_priv(netdev);
  235. switch (cmd->cmd) {
  236. case ETHTOOL_GRXCLSRLCNT:
  237. cmd->rule_cnt = adapter->rxnfc_count;
  238. cmd->data = adapter->rxnfc_max;
  239. cmd->data |= RX_CLS_LOC_SPECIAL;
  240. return 0;
  241. case ETHTOOL_GRXCLSRULE:
  242. return tsnep_rxnfc_get_rule(adapter, cmd);
  243. case ETHTOOL_GRXCLSRLALL:
  244. return tsnep_rxnfc_get_all(adapter, cmd, rule_locs);
  245. default:
  246. return -EOPNOTSUPP;
  247. }
  248. }
  249. static int tsnep_ethtool_set_rxnfc(struct net_device *netdev,
  250. struct ethtool_rxnfc *cmd)
  251. {
  252. struct tsnep_adapter *adapter = netdev_priv(netdev);
  253. switch (cmd->cmd) {
  254. case ETHTOOL_SRXCLSRLINS:
  255. return tsnep_rxnfc_add_rule(adapter, cmd);
  256. case ETHTOOL_SRXCLSRLDEL:
  257. return tsnep_rxnfc_del_rule(adapter, cmd);
  258. default:
  259. return -EOPNOTSUPP;
  260. }
  261. }
  262. static void tsnep_ethtool_get_channels(struct net_device *netdev,
  263. struct ethtool_channels *ch)
  264. {
  265. struct tsnep_adapter *adapter = netdev_priv(netdev);
  266. ch->max_combined = adapter->num_queues;
  267. ch->combined_count = adapter->num_queues;
  268. }
  269. static int tsnep_ethtool_get_ts_info(struct net_device *netdev,
  270. struct kernel_ethtool_ts_info *info)
  271. {
  272. struct tsnep_adapter *adapter = netdev_priv(netdev);
  273. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  274. SOF_TIMESTAMPING_TX_HARDWARE |
  275. SOF_TIMESTAMPING_RX_HARDWARE |
  276. SOF_TIMESTAMPING_RAW_HARDWARE;
  277. if (adapter->ptp_clock)
  278. info->phc_index = ptp_clock_index(adapter->ptp_clock);
  279. info->tx_types = BIT(HWTSTAMP_TX_OFF) |
  280. BIT(HWTSTAMP_TX_ON);
  281. info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
  282. BIT(HWTSTAMP_FILTER_ALL);
  283. return 0;
  284. }
  285. static struct tsnep_queue *tsnep_get_queue_with_tx(struct tsnep_adapter *adapter,
  286. int index)
  287. {
  288. int i;
  289. for (i = 0; i < adapter->num_queues; i++) {
  290. if (adapter->queue[i].tx) {
  291. if (index == 0)
  292. return &adapter->queue[i];
  293. index--;
  294. }
  295. }
  296. return NULL;
  297. }
  298. static struct tsnep_queue *tsnep_get_queue_with_rx(struct tsnep_adapter *adapter,
  299. int index)
  300. {
  301. int i;
  302. for (i = 0; i < adapter->num_queues; i++) {
  303. if (adapter->queue[i].rx) {
  304. if (index == 0)
  305. return &adapter->queue[i];
  306. index--;
  307. }
  308. }
  309. return NULL;
  310. }
  311. static int tsnep_ethtool_get_coalesce(struct net_device *netdev,
  312. struct ethtool_coalesce *ec,
  313. struct kernel_ethtool_coalesce *kernel_coal,
  314. struct netlink_ext_ack *extack)
  315. {
  316. struct tsnep_adapter *adapter = netdev_priv(netdev);
  317. struct tsnep_queue *queue;
  318. queue = tsnep_get_queue_with_rx(adapter, 0);
  319. if (queue)
  320. ec->rx_coalesce_usecs = tsnep_get_irq_coalesce(queue);
  321. queue = tsnep_get_queue_with_tx(adapter, 0);
  322. if (queue)
  323. ec->tx_coalesce_usecs = tsnep_get_irq_coalesce(queue);
  324. return 0;
  325. }
  326. static int tsnep_ethtool_set_coalesce(struct net_device *netdev,
  327. struct ethtool_coalesce *ec,
  328. struct kernel_ethtool_coalesce *kernel_coal,
  329. struct netlink_ext_ack *extack)
  330. {
  331. struct tsnep_adapter *adapter = netdev_priv(netdev);
  332. int i;
  333. int retval;
  334. for (i = 0; i < adapter->num_queues; i++) {
  335. /* RX coalesce has priority for queues with TX and RX */
  336. if (adapter->queue[i].rx)
  337. retval = tsnep_set_irq_coalesce(&adapter->queue[i],
  338. ec->rx_coalesce_usecs);
  339. else
  340. retval = tsnep_set_irq_coalesce(&adapter->queue[i],
  341. ec->tx_coalesce_usecs);
  342. if (retval != 0)
  343. return retval;
  344. }
  345. return 0;
  346. }
  347. static int tsnep_ethtool_get_per_queue_coalesce(struct net_device *netdev,
  348. u32 queue,
  349. struct ethtool_coalesce *ec)
  350. {
  351. struct tsnep_adapter *adapter = netdev_priv(netdev);
  352. struct tsnep_queue *queue_with_rx;
  353. struct tsnep_queue *queue_with_tx;
  354. if (queue >= max(adapter->num_tx_queues, adapter->num_rx_queues))
  355. return -EINVAL;
  356. queue_with_rx = tsnep_get_queue_with_rx(adapter, queue);
  357. if (queue_with_rx)
  358. ec->rx_coalesce_usecs = tsnep_get_irq_coalesce(queue_with_rx);
  359. queue_with_tx = tsnep_get_queue_with_tx(adapter, queue);
  360. if (queue_with_tx)
  361. ec->tx_coalesce_usecs = tsnep_get_irq_coalesce(queue_with_tx);
  362. return 0;
  363. }
  364. static int tsnep_ethtool_set_per_queue_coalesce(struct net_device *netdev,
  365. u32 queue,
  366. struct ethtool_coalesce *ec)
  367. {
  368. struct tsnep_adapter *adapter = netdev_priv(netdev);
  369. struct tsnep_queue *queue_with_rx;
  370. struct tsnep_queue *queue_with_tx;
  371. int retval;
  372. if (queue >= max(adapter->num_tx_queues, adapter->num_rx_queues))
  373. return -EINVAL;
  374. queue_with_rx = tsnep_get_queue_with_rx(adapter, queue);
  375. if (queue_with_rx) {
  376. retval = tsnep_set_irq_coalesce(queue_with_rx, ec->rx_coalesce_usecs);
  377. if (retval != 0)
  378. return retval;
  379. }
  380. /* RX coalesce has priority for queues with TX and RX */
  381. queue_with_tx = tsnep_get_queue_with_tx(adapter, queue);
  382. if (queue_with_tx && !queue_with_tx->rx) {
  383. retval = tsnep_set_irq_coalesce(queue_with_tx, ec->tx_coalesce_usecs);
  384. if (retval != 0)
  385. return retval;
  386. }
  387. return 0;
  388. }
  389. const struct ethtool_ops tsnep_ethtool_ops = {
  390. .supported_coalesce_params = ETHTOOL_COALESCE_USECS,
  391. .get_drvinfo = tsnep_ethtool_get_drvinfo,
  392. .get_regs_len = tsnep_ethtool_get_regs_len,
  393. .get_regs = tsnep_ethtool_get_regs,
  394. .get_msglevel = tsnep_ethtool_get_msglevel,
  395. .set_msglevel = tsnep_ethtool_set_msglevel,
  396. .nway_reset = phy_ethtool_nway_reset,
  397. .get_link = ethtool_op_get_link,
  398. .self_test = tsnep_ethtool_self_test,
  399. .get_strings = tsnep_ethtool_get_strings,
  400. .get_ethtool_stats = tsnep_ethtool_get_ethtool_stats,
  401. .get_sset_count = tsnep_ethtool_get_sset_count,
  402. .get_rxnfc = tsnep_ethtool_get_rxnfc,
  403. .set_rxnfc = tsnep_ethtool_set_rxnfc,
  404. .get_rx_ring_count = tsnep_ethtool_get_rx_ring_count,
  405. .get_channels = tsnep_ethtool_get_channels,
  406. .get_ts_info = tsnep_ethtool_get_ts_info,
  407. .get_coalesce = tsnep_ethtool_get_coalesce,
  408. .set_coalesce = tsnep_ethtool_set_coalesce,
  409. .get_per_queue_coalesce = tsnep_ethtool_get_per_queue_coalesce,
  410. .set_per_queue_coalesce = tsnep_ethtool_set_per_queue_coalesce,
  411. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  412. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  413. };