wx_ethtool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2015 - 2023 Beijing WangXun Technology Co., Ltd. */
  3. #include <linux/pci.h>
  4. #include <linux/phy.h>
  5. #include <linux/ethtool.h>
  6. #include "wx_type.h"
  7. #include "wx_ethtool.h"
  8. #include "wx_hw.h"
  9. #include "wx_lib.h"
  10. struct wx_stats {
  11. char stat_string[ETH_GSTRING_LEN];
  12. size_t sizeof_stat;
  13. off_t stat_offset;
  14. };
  15. #define WX_STAT(str, m) { \
  16. .stat_string = str, \
  17. .sizeof_stat = sizeof(((struct wx *)0)->m), \
  18. .stat_offset = offsetof(struct wx, m) }
  19. static const struct wx_stats wx_gstrings_stats[] = {
  20. WX_STAT("rx_dma_pkts", stats.gprc),
  21. WX_STAT("tx_dma_pkts", stats.gptc),
  22. WX_STAT("rx_dma_bytes", stats.gorc),
  23. WX_STAT("tx_dma_bytes", stats.gotc),
  24. WX_STAT("rx_total_pkts", stats.tpr),
  25. WX_STAT("tx_total_pkts", stats.tpt),
  26. WX_STAT("rx_long_length_count", stats.roc),
  27. WX_STAT("rx_short_length_count", stats.ruc),
  28. WX_STAT("os2bmc_rx_by_bmc", stats.o2bgptc),
  29. WX_STAT("os2bmc_tx_by_bmc", stats.b2ospc),
  30. WX_STAT("os2bmc_tx_by_host", stats.o2bspc),
  31. WX_STAT("os2bmc_rx_by_host", stats.b2ogprc),
  32. WX_STAT("rx_no_dma_resources", stats.rdmdrop),
  33. WX_STAT("tx_busy", tx_busy),
  34. WX_STAT("non_eop_descs", non_eop_descs),
  35. WX_STAT("tx_restart_queue", restart_queue),
  36. WX_STAT("rx_csum_offload_good_count", hw_csum_rx_good),
  37. WX_STAT("rx_csum_offload_errors", hw_csum_rx_error),
  38. WX_STAT("alloc_rx_buff_failed", alloc_rx_buff_failed),
  39. };
  40. static const struct wx_stats wx_gstrings_fdir_stats[] = {
  41. WX_STAT("fdir_match", stats.fdirmatch),
  42. WX_STAT("fdir_miss", stats.fdirmiss),
  43. };
  44. /* drivers allocates num_tx_queues and num_rx_queues symmetrically so
  45. * we set the num_rx_queues to evaluate to num_tx_queues. This is
  46. * used because we do not have a good way to get the max number of
  47. * rx queues with CONFIG_RPS disabled.
  48. */
  49. #define WX_NUM_RX_QUEUES netdev->num_tx_queues
  50. #define WX_NUM_TX_QUEUES netdev->num_tx_queues
  51. #define WX_QUEUE_STATS_LEN ( \
  52. (WX_NUM_TX_QUEUES + WX_NUM_RX_QUEUES) * \
  53. (sizeof(struct wx_queue_stats) / sizeof(u64)))
  54. #define WX_GLOBAL_STATS_LEN ARRAY_SIZE(wx_gstrings_stats)
  55. #define WX_FDIR_STATS_LEN ARRAY_SIZE(wx_gstrings_fdir_stats)
  56. #define WX_STATS_LEN (WX_GLOBAL_STATS_LEN + WX_QUEUE_STATS_LEN)
  57. int wx_get_sset_count(struct net_device *netdev, int sset)
  58. {
  59. struct wx *wx = netdev_priv(netdev);
  60. switch (sset) {
  61. case ETH_SS_STATS:
  62. return (wx->mac.type == wx_mac_sp) ?
  63. WX_STATS_LEN + WX_FDIR_STATS_LEN : WX_STATS_LEN;
  64. default:
  65. return -EOPNOTSUPP;
  66. }
  67. }
  68. EXPORT_SYMBOL(wx_get_sset_count);
  69. void wx_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  70. {
  71. struct wx *wx = netdev_priv(netdev);
  72. u8 *p = data;
  73. int i;
  74. switch (stringset) {
  75. case ETH_SS_STATS:
  76. for (i = 0; i < WX_GLOBAL_STATS_LEN; i++)
  77. ethtool_puts(&p, wx_gstrings_stats[i].stat_string);
  78. if (wx->mac.type == wx_mac_sp) {
  79. for (i = 0; i < WX_FDIR_STATS_LEN; i++)
  80. ethtool_puts(&p, wx_gstrings_fdir_stats[i].stat_string);
  81. }
  82. for (i = 0; i < netdev->num_tx_queues; i++) {
  83. ethtool_sprintf(&p, "tx_queue_%u_packets", i);
  84. ethtool_sprintf(&p, "tx_queue_%u_bytes", i);
  85. }
  86. for (i = 0; i < WX_NUM_RX_QUEUES; i++) {
  87. ethtool_sprintf(&p, "rx_queue_%u_packets", i);
  88. ethtool_sprintf(&p, "rx_queue_%u_bytes", i);
  89. }
  90. break;
  91. }
  92. }
  93. EXPORT_SYMBOL(wx_get_strings);
  94. void wx_get_ethtool_stats(struct net_device *netdev,
  95. struct ethtool_stats *stats, u64 *data)
  96. {
  97. struct wx *wx = netdev_priv(netdev);
  98. struct wx_ring *ring;
  99. unsigned int start;
  100. int i, j, k;
  101. char *p;
  102. wx_update_stats(wx);
  103. for (i = 0; i < WX_GLOBAL_STATS_LEN; i++) {
  104. p = (char *)wx + wx_gstrings_stats[i].stat_offset;
  105. data[i] = (wx_gstrings_stats[i].sizeof_stat ==
  106. sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
  107. }
  108. if (wx->mac.type == wx_mac_sp) {
  109. for (k = 0; k < WX_FDIR_STATS_LEN; k++) {
  110. p = (char *)wx + wx_gstrings_fdir_stats[k].stat_offset;
  111. data[i++] = *(u64 *)p;
  112. }
  113. }
  114. for (j = 0; j < netdev->num_tx_queues; j++) {
  115. ring = wx->tx_ring[j];
  116. if (!ring) {
  117. data[i++] = 0;
  118. data[i++] = 0;
  119. continue;
  120. }
  121. do {
  122. start = u64_stats_fetch_begin(&ring->syncp);
  123. data[i] = ring->stats.packets;
  124. data[i + 1] = ring->stats.bytes;
  125. } while (u64_stats_fetch_retry(&ring->syncp, start));
  126. i += 2;
  127. }
  128. for (j = 0; j < WX_NUM_RX_QUEUES; j++) {
  129. ring = wx->rx_ring[j];
  130. if (!ring) {
  131. data[i++] = 0;
  132. data[i++] = 0;
  133. continue;
  134. }
  135. do {
  136. start = u64_stats_fetch_begin(&ring->syncp);
  137. data[i] = ring->stats.packets;
  138. data[i + 1] = ring->stats.bytes;
  139. } while (u64_stats_fetch_retry(&ring->syncp, start));
  140. i += 2;
  141. }
  142. }
  143. EXPORT_SYMBOL(wx_get_ethtool_stats);
  144. void wx_get_mac_stats(struct net_device *netdev,
  145. struct ethtool_eth_mac_stats *mac_stats)
  146. {
  147. struct wx *wx = netdev_priv(netdev);
  148. struct wx_hw_stats *hwstats;
  149. wx_update_stats(wx);
  150. hwstats = &wx->stats;
  151. mac_stats->MulticastFramesXmittedOK = hwstats->mptc;
  152. mac_stats->BroadcastFramesXmittedOK = hwstats->bptc;
  153. mac_stats->MulticastFramesReceivedOK = hwstats->mprc;
  154. mac_stats->BroadcastFramesReceivedOK = hwstats->bprc;
  155. }
  156. EXPORT_SYMBOL(wx_get_mac_stats);
  157. void wx_get_pause_stats(struct net_device *netdev,
  158. struct ethtool_pause_stats *stats)
  159. {
  160. struct wx *wx = netdev_priv(netdev);
  161. struct wx_hw_stats *hwstats;
  162. wx_update_stats(wx);
  163. hwstats = &wx->stats;
  164. stats->tx_pause_frames = hwstats->lxontxc + hwstats->lxofftxc;
  165. stats->rx_pause_frames = hwstats->lxonoffrxc;
  166. }
  167. EXPORT_SYMBOL(wx_get_pause_stats);
  168. void wx_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
  169. {
  170. unsigned int stats_len = WX_STATS_LEN;
  171. struct wx *wx = netdev_priv(netdev);
  172. if (wx->mac.type == wx_mac_sp)
  173. stats_len += WX_FDIR_STATS_LEN;
  174. strscpy(info->driver, wx->driver_name, sizeof(info->driver));
  175. strscpy(info->fw_version, wx->eeprom_id, sizeof(info->fw_version));
  176. strscpy(info->bus_info, pci_name(wx->pdev), sizeof(info->bus_info));
  177. if (wx->num_tx_queues <= WX_NUM_TX_QUEUES) {
  178. info->n_stats = stats_len -
  179. (WX_NUM_TX_QUEUES - wx->num_tx_queues) *
  180. (sizeof(struct wx_queue_stats) / sizeof(u64)) * 2;
  181. } else {
  182. info->n_stats = stats_len;
  183. }
  184. }
  185. EXPORT_SYMBOL(wx_get_drvinfo);
  186. int wx_nway_reset(struct net_device *netdev)
  187. {
  188. struct wx *wx = netdev_priv(netdev);
  189. return phylink_ethtool_nway_reset(wx->phylink);
  190. }
  191. EXPORT_SYMBOL(wx_nway_reset);
  192. int wx_get_link_ksettings(struct net_device *netdev,
  193. struct ethtool_link_ksettings *cmd)
  194. {
  195. struct wx *wx = netdev_priv(netdev);
  196. return phylink_ethtool_ksettings_get(wx->phylink, cmd);
  197. }
  198. EXPORT_SYMBOL(wx_get_link_ksettings);
  199. int wx_set_link_ksettings(struct net_device *netdev,
  200. const struct ethtool_link_ksettings *cmd)
  201. {
  202. struct wx *wx = netdev_priv(netdev);
  203. return phylink_ethtool_ksettings_set(wx->phylink, cmd);
  204. }
  205. EXPORT_SYMBOL(wx_set_link_ksettings);
  206. void wx_get_pauseparam(struct net_device *netdev,
  207. struct ethtool_pauseparam *pause)
  208. {
  209. struct wx *wx = netdev_priv(netdev);
  210. phylink_ethtool_get_pauseparam(wx->phylink, pause);
  211. }
  212. EXPORT_SYMBOL(wx_get_pauseparam);
  213. int wx_set_pauseparam(struct net_device *netdev,
  214. struct ethtool_pauseparam *pause)
  215. {
  216. struct wx *wx = netdev_priv(netdev);
  217. return phylink_ethtool_set_pauseparam(wx->phylink, pause);
  218. }
  219. EXPORT_SYMBOL(wx_set_pauseparam);
  220. void wx_get_ringparam(struct net_device *netdev,
  221. struct ethtool_ringparam *ring,
  222. struct kernel_ethtool_ringparam *kernel_ring,
  223. struct netlink_ext_ack *extack)
  224. {
  225. struct wx *wx = netdev_priv(netdev);
  226. ring->rx_max_pending = WX_MAX_RXD;
  227. ring->tx_max_pending = WX_MAX_TXD;
  228. ring->rx_mini_max_pending = 0;
  229. ring->rx_jumbo_max_pending = 0;
  230. ring->rx_pending = wx->rx_ring_count;
  231. ring->tx_pending = wx->tx_ring_count;
  232. ring->rx_mini_pending = 0;
  233. ring->rx_jumbo_pending = 0;
  234. }
  235. EXPORT_SYMBOL(wx_get_ringparam);
  236. int wx_get_coalesce(struct net_device *netdev,
  237. struct ethtool_coalesce *ec,
  238. struct kernel_ethtool_coalesce *kernel_coal,
  239. struct netlink_ext_ack *extack)
  240. {
  241. struct wx *wx = netdev_priv(netdev);
  242. ec->tx_max_coalesced_frames_irq = wx->tx_work_limit;
  243. /* only valid if in constant ITR mode */
  244. if (wx->rx_itr_setting <= 1)
  245. ec->rx_coalesce_usecs = wx->rx_itr_setting;
  246. else
  247. ec->rx_coalesce_usecs = wx->rx_itr_setting >> 2;
  248. /* if in mixed tx/rx queues per vector mode, report only rx settings */
  249. if (wx->q_vector[0]->tx.count && wx->q_vector[0]->rx.count)
  250. return 0;
  251. /* only valid if in constant ITR mode */
  252. if (wx->tx_itr_setting <= 1)
  253. ec->tx_coalesce_usecs = wx->tx_itr_setting;
  254. else
  255. ec->tx_coalesce_usecs = wx->tx_itr_setting >> 2;
  256. return 0;
  257. }
  258. EXPORT_SYMBOL(wx_get_coalesce);
  259. int wx_set_coalesce(struct net_device *netdev,
  260. struct ethtool_coalesce *ec,
  261. struct kernel_ethtool_coalesce *kernel_coal,
  262. struct netlink_ext_ack *extack)
  263. {
  264. struct wx *wx = netdev_priv(netdev);
  265. u16 tx_itr_param, rx_itr_param;
  266. struct wx_q_vector *q_vector;
  267. u16 max_eitr;
  268. int i;
  269. if (wx->q_vector[0]->tx.count && wx->q_vector[0]->rx.count) {
  270. /* reject Tx specific changes in case of mixed RxTx vectors */
  271. if (ec->tx_coalesce_usecs)
  272. return -EOPNOTSUPP;
  273. }
  274. if (ec->tx_max_coalesced_frames_irq)
  275. wx->tx_work_limit = ec->tx_max_coalesced_frames_irq;
  276. if (wx->mac.type == wx_mac_sp)
  277. max_eitr = WX_SP_MAX_EITR;
  278. else
  279. max_eitr = WX_EM_MAX_EITR;
  280. if ((ec->rx_coalesce_usecs > (max_eitr >> 2)) ||
  281. (ec->tx_coalesce_usecs > (max_eitr >> 2)))
  282. return -EINVAL;
  283. if (ec->rx_coalesce_usecs > 1)
  284. wx->rx_itr_setting = ec->rx_coalesce_usecs << 2;
  285. else
  286. wx->rx_itr_setting = ec->rx_coalesce_usecs;
  287. if (wx->rx_itr_setting == 1)
  288. rx_itr_param = WX_20K_ITR;
  289. else
  290. rx_itr_param = wx->rx_itr_setting;
  291. if (ec->tx_coalesce_usecs > 1)
  292. wx->tx_itr_setting = ec->tx_coalesce_usecs << 2;
  293. else
  294. wx->tx_itr_setting = ec->tx_coalesce_usecs;
  295. if (wx->tx_itr_setting == 1) {
  296. if (wx->mac.type == wx_mac_sp)
  297. tx_itr_param = WX_12K_ITR;
  298. else
  299. tx_itr_param = WX_20K_ITR;
  300. } else {
  301. tx_itr_param = wx->tx_itr_setting;
  302. }
  303. /* mixed Rx/Tx */
  304. if (wx->q_vector[0]->tx.count && wx->q_vector[0]->rx.count)
  305. wx->tx_itr_setting = wx->rx_itr_setting;
  306. for (i = 0; i < wx->num_q_vectors; i++) {
  307. q_vector = wx->q_vector[i];
  308. if (q_vector->tx.count && !q_vector->rx.count)
  309. /* tx only */
  310. q_vector->itr = tx_itr_param;
  311. else
  312. /* rx only or mixed */
  313. q_vector->itr = rx_itr_param;
  314. wx_write_eitr(q_vector);
  315. }
  316. return 0;
  317. }
  318. EXPORT_SYMBOL(wx_set_coalesce);
  319. static unsigned int wx_max_channels(struct wx *wx)
  320. {
  321. unsigned int max_combined;
  322. if (!wx->msix_q_entries) {
  323. /* We only support one q_vector without MSI-X */
  324. max_combined = 1;
  325. } else {
  326. /* support up to max allowed queues with RSS */
  327. if (wx->mac.type == wx_mac_sp)
  328. max_combined = 63;
  329. else
  330. max_combined = 8;
  331. }
  332. return max_combined;
  333. }
  334. void wx_get_channels(struct net_device *dev,
  335. struct ethtool_channels *ch)
  336. {
  337. struct wx *wx = netdev_priv(dev);
  338. /* report maximum channels */
  339. ch->max_combined = wx_max_channels(wx);
  340. /* report info for other vector */
  341. if (wx->msix_q_entries) {
  342. ch->max_other = 1;
  343. ch->other_count = 1;
  344. }
  345. /* record RSS queues */
  346. ch->combined_count = wx->ring_feature[RING_F_RSS].indices;
  347. if (test_bit(WX_FLAG_FDIR_CAPABLE, wx->flags))
  348. ch->combined_count = wx->ring_feature[RING_F_FDIR].indices;
  349. }
  350. EXPORT_SYMBOL(wx_get_channels);
  351. int wx_set_channels(struct net_device *dev,
  352. struct ethtool_channels *ch)
  353. {
  354. unsigned int count = ch->combined_count;
  355. struct wx *wx = netdev_priv(dev);
  356. /* verify other_count has not changed */
  357. if (ch->other_count != 1)
  358. return -EINVAL;
  359. /* verify the number of channels does not exceed hardware limits */
  360. if (count > wx_max_channels(wx))
  361. return -EINVAL;
  362. if (test_bit(WX_FLAG_FDIR_CAPABLE, wx->flags))
  363. wx->ring_feature[RING_F_FDIR].limit = count;
  364. wx->ring_feature[RING_F_RSS].limit = count;
  365. return 0;
  366. }
  367. EXPORT_SYMBOL(wx_set_channels);
  368. u32 wx_get_msglevel(struct net_device *netdev)
  369. {
  370. struct wx *wx = netdev_priv(netdev);
  371. return wx->msg_enable;
  372. }
  373. EXPORT_SYMBOL(wx_get_msglevel);
  374. void wx_set_msglevel(struct net_device *netdev, u32 data)
  375. {
  376. struct wx *wx = netdev_priv(netdev);
  377. wx->msg_enable = data;
  378. }
  379. EXPORT_SYMBOL(wx_set_msglevel);