ethtool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2009 - 2018 Intel Corporation. */
  3. /* ethtool support for igbvf */
  4. #include <linux/netdevice.h>
  5. #include <linux/ethtool.h>
  6. #include <linux/pci.h>
  7. #include <linux/vmalloc.h>
  8. #include <linux/delay.h>
  9. #include "igbvf.h"
  10. #include <linux/if_vlan.h>
  11. struct igbvf_stats {
  12. char stat_string[ETH_GSTRING_LEN];
  13. int sizeof_stat;
  14. int stat_offset;
  15. int base_stat_offset;
  16. };
  17. #define IGBVF_STAT(current, base) \
  18. sizeof(((struct igbvf_adapter *)0)->current), \
  19. offsetof(struct igbvf_adapter, current), \
  20. offsetof(struct igbvf_adapter, base)
  21. static const struct igbvf_stats igbvf_gstrings_stats[] = {
  22. { "rx_packets", IGBVF_STAT(stats.gprc, stats.base_gprc) },
  23. { "tx_packets", IGBVF_STAT(stats.gptc, stats.base_gptc) },
  24. { "rx_bytes", IGBVF_STAT(stats.gorc, stats.base_gorc) },
  25. { "tx_bytes", IGBVF_STAT(stats.gotc, stats.base_gotc) },
  26. { "multicast", IGBVF_STAT(stats.mprc, stats.base_mprc) },
  27. { "lbrx_bytes", IGBVF_STAT(stats.gorlbc, stats.base_gorlbc) },
  28. { "lbrx_packets", IGBVF_STAT(stats.gprlbc, stats.base_gprlbc) },
  29. { "tx_restart_queue", IGBVF_STAT(restart_queue, zero_base) },
  30. { "rx_long_byte_count", IGBVF_STAT(stats.gorc, stats.base_gorc) },
  31. { "rx_csum_offload_good", IGBVF_STAT(hw_csum_good, zero_base) },
  32. { "rx_csum_offload_errors", IGBVF_STAT(hw_csum_err, zero_base) },
  33. { "rx_header_split", IGBVF_STAT(rx_hdr_split, zero_base) },
  34. { "alloc_rx_buff_failed", IGBVF_STAT(alloc_rx_buff_failed, zero_base) },
  35. };
  36. #define IGBVF_GLOBAL_STATS_LEN ARRAY_SIZE(igbvf_gstrings_stats)
  37. static const char igbvf_gstrings_test[][ETH_GSTRING_LEN] = {
  38. "Link test (on/offline)"
  39. };
  40. #define IGBVF_TEST_LEN ARRAY_SIZE(igbvf_gstrings_test)
  41. static int igbvf_get_link_ksettings(struct net_device *netdev,
  42. struct ethtool_link_ksettings *cmd)
  43. {
  44. struct igbvf_adapter *adapter = netdev_priv(netdev);
  45. struct e1000_hw *hw = &adapter->hw;
  46. u32 status;
  47. ethtool_link_ksettings_zero_link_mode(cmd, supported);
  48. ethtool_link_ksettings_add_link_mode(cmd, supported, 1000baseT_Full);
  49. ethtool_link_ksettings_zero_link_mode(cmd, advertising);
  50. ethtool_link_ksettings_add_link_mode(cmd, advertising, 1000baseT_Full);
  51. cmd->base.port = -1;
  52. status = er32(STATUS);
  53. if (status & E1000_STATUS_LU) {
  54. if (status & E1000_STATUS_SPEED_1000)
  55. cmd->base.speed = SPEED_1000;
  56. else if (status & E1000_STATUS_SPEED_100)
  57. cmd->base.speed = SPEED_100;
  58. else
  59. cmd->base.speed = SPEED_10;
  60. if (status & E1000_STATUS_FD)
  61. cmd->base.duplex = DUPLEX_FULL;
  62. else
  63. cmd->base.duplex = DUPLEX_HALF;
  64. } else {
  65. cmd->base.speed = SPEED_UNKNOWN;
  66. cmd->base.duplex = DUPLEX_UNKNOWN;
  67. }
  68. cmd->base.autoneg = AUTONEG_DISABLE;
  69. return 0;
  70. }
  71. static int igbvf_set_link_ksettings(struct net_device *netdev,
  72. const struct ethtool_link_ksettings *cmd)
  73. {
  74. return -EOPNOTSUPP;
  75. }
  76. static void igbvf_get_pauseparam(struct net_device *netdev,
  77. struct ethtool_pauseparam *pause)
  78. {
  79. }
  80. static int igbvf_set_pauseparam(struct net_device *netdev,
  81. struct ethtool_pauseparam *pause)
  82. {
  83. return -EOPNOTSUPP;
  84. }
  85. static u32 igbvf_get_msglevel(struct net_device *netdev)
  86. {
  87. struct igbvf_adapter *adapter = netdev_priv(netdev);
  88. return adapter->msg_enable;
  89. }
  90. static void igbvf_set_msglevel(struct net_device *netdev, u32 data)
  91. {
  92. struct igbvf_adapter *adapter = netdev_priv(netdev);
  93. adapter->msg_enable = data;
  94. }
  95. static int igbvf_get_regs_len(struct net_device *netdev)
  96. {
  97. #define IGBVF_REGS_LEN 8
  98. return IGBVF_REGS_LEN * sizeof(u32);
  99. }
  100. static void igbvf_get_regs(struct net_device *netdev,
  101. struct ethtool_regs *regs, void *p)
  102. {
  103. struct igbvf_adapter *adapter = netdev_priv(netdev);
  104. struct e1000_hw *hw = &adapter->hw;
  105. u32 *regs_buff = p;
  106. memset(p, 0, IGBVF_REGS_LEN * sizeof(u32));
  107. regs->version = (1u << 24) |
  108. (adapter->pdev->revision << 16) |
  109. adapter->pdev->device;
  110. regs_buff[0] = er32(CTRL);
  111. regs_buff[1] = er32(STATUS);
  112. regs_buff[2] = er32(RDLEN(0));
  113. regs_buff[3] = er32(RDH(0));
  114. regs_buff[4] = er32(RDT(0));
  115. regs_buff[5] = er32(TDLEN(0));
  116. regs_buff[6] = er32(TDH(0));
  117. regs_buff[7] = er32(TDT(0));
  118. }
  119. static int igbvf_get_eeprom_len(struct net_device *netdev)
  120. {
  121. return 0;
  122. }
  123. static int igbvf_get_eeprom(struct net_device *netdev,
  124. struct ethtool_eeprom *eeprom, u8 *bytes)
  125. {
  126. return -EOPNOTSUPP;
  127. }
  128. static int igbvf_set_eeprom(struct net_device *netdev,
  129. struct ethtool_eeprom *eeprom, u8 *bytes)
  130. {
  131. return -EOPNOTSUPP;
  132. }
  133. static void igbvf_get_drvinfo(struct net_device *netdev,
  134. struct ethtool_drvinfo *drvinfo)
  135. {
  136. struct igbvf_adapter *adapter = netdev_priv(netdev);
  137. strlcpy(drvinfo->driver, igbvf_driver_name, sizeof(drvinfo->driver));
  138. strlcpy(drvinfo->version, igbvf_driver_version,
  139. sizeof(drvinfo->version));
  140. strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
  141. sizeof(drvinfo->bus_info));
  142. }
  143. static void igbvf_get_ringparam(struct net_device *netdev,
  144. struct ethtool_ringparam *ring)
  145. {
  146. struct igbvf_adapter *adapter = netdev_priv(netdev);
  147. struct igbvf_ring *tx_ring = adapter->tx_ring;
  148. struct igbvf_ring *rx_ring = adapter->rx_ring;
  149. ring->rx_max_pending = IGBVF_MAX_RXD;
  150. ring->tx_max_pending = IGBVF_MAX_TXD;
  151. ring->rx_pending = rx_ring->count;
  152. ring->tx_pending = tx_ring->count;
  153. }
  154. static int igbvf_set_ringparam(struct net_device *netdev,
  155. struct ethtool_ringparam *ring)
  156. {
  157. struct igbvf_adapter *adapter = netdev_priv(netdev);
  158. struct igbvf_ring *temp_ring;
  159. int err = 0;
  160. u32 new_rx_count, new_tx_count;
  161. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  162. return -EINVAL;
  163. new_rx_count = max_t(u32, ring->rx_pending, IGBVF_MIN_RXD);
  164. new_rx_count = min_t(u32, new_rx_count, IGBVF_MAX_RXD);
  165. new_rx_count = ALIGN(new_rx_count, REQ_RX_DESCRIPTOR_MULTIPLE);
  166. new_tx_count = max_t(u32, ring->tx_pending, IGBVF_MIN_TXD);
  167. new_tx_count = min_t(u32, new_tx_count, IGBVF_MAX_TXD);
  168. new_tx_count = ALIGN(new_tx_count, REQ_TX_DESCRIPTOR_MULTIPLE);
  169. if ((new_tx_count == adapter->tx_ring->count) &&
  170. (new_rx_count == adapter->rx_ring->count)) {
  171. /* nothing to do */
  172. return 0;
  173. }
  174. while (test_and_set_bit(__IGBVF_RESETTING, &adapter->state))
  175. usleep_range(1000, 2000);
  176. if (!netif_running(adapter->netdev)) {
  177. adapter->tx_ring->count = new_tx_count;
  178. adapter->rx_ring->count = new_rx_count;
  179. goto clear_reset;
  180. }
  181. temp_ring = vmalloc(sizeof(struct igbvf_ring));
  182. if (!temp_ring) {
  183. err = -ENOMEM;
  184. goto clear_reset;
  185. }
  186. igbvf_down(adapter);
  187. /* We can't just free everything and then setup again,
  188. * because the ISRs in MSI-X mode get passed pointers
  189. * to the Tx and Rx ring structs.
  190. */
  191. if (new_tx_count != adapter->tx_ring->count) {
  192. memcpy(temp_ring, adapter->tx_ring, sizeof(struct igbvf_ring));
  193. temp_ring->count = new_tx_count;
  194. err = igbvf_setup_tx_resources(adapter, temp_ring);
  195. if (err)
  196. goto err_setup;
  197. igbvf_free_tx_resources(adapter->tx_ring);
  198. memcpy(adapter->tx_ring, temp_ring, sizeof(struct igbvf_ring));
  199. }
  200. if (new_rx_count != adapter->rx_ring->count) {
  201. memcpy(temp_ring, adapter->rx_ring, sizeof(struct igbvf_ring));
  202. temp_ring->count = new_rx_count;
  203. err = igbvf_setup_rx_resources(adapter, temp_ring);
  204. if (err)
  205. goto err_setup;
  206. igbvf_free_rx_resources(adapter->rx_ring);
  207. memcpy(adapter->rx_ring, temp_ring, sizeof(struct igbvf_ring));
  208. }
  209. err_setup:
  210. igbvf_up(adapter);
  211. vfree(temp_ring);
  212. clear_reset:
  213. clear_bit(__IGBVF_RESETTING, &adapter->state);
  214. return err;
  215. }
  216. static int igbvf_link_test(struct igbvf_adapter *adapter, u64 *data)
  217. {
  218. struct e1000_hw *hw = &adapter->hw;
  219. *data = 0;
  220. spin_lock_bh(&hw->mbx_lock);
  221. hw->mac.ops.check_for_link(hw);
  222. spin_unlock_bh(&hw->mbx_lock);
  223. if (!(er32(STATUS) & E1000_STATUS_LU))
  224. *data = 1;
  225. return *data;
  226. }
  227. static void igbvf_diag_test(struct net_device *netdev,
  228. struct ethtool_test *eth_test, u64 *data)
  229. {
  230. struct igbvf_adapter *adapter = netdev_priv(netdev);
  231. set_bit(__IGBVF_TESTING, &adapter->state);
  232. /* Link test performed before hardware reset so autoneg doesn't
  233. * interfere with test result
  234. */
  235. if (igbvf_link_test(adapter, &data[0]))
  236. eth_test->flags |= ETH_TEST_FL_FAILED;
  237. clear_bit(__IGBVF_TESTING, &adapter->state);
  238. msleep_interruptible(4 * 1000);
  239. }
  240. static void igbvf_get_wol(struct net_device *netdev,
  241. struct ethtool_wolinfo *wol)
  242. {
  243. wol->supported = 0;
  244. wol->wolopts = 0;
  245. }
  246. static int igbvf_set_wol(struct net_device *netdev,
  247. struct ethtool_wolinfo *wol)
  248. {
  249. return -EOPNOTSUPP;
  250. }
  251. static int igbvf_get_coalesce(struct net_device *netdev,
  252. struct ethtool_coalesce *ec)
  253. {
  254. struct igbvf_adapter *adapter = netdev_priv(netdev);
  255. if (adapter->requested_itr <= 3)
  256. ec->rx_coalesce_usecs = adapter->requested_itr;
  257. else
  258. ec->rx_coalesce_usecs = adapter->current_itr >> 2;
  259. return 0;
  260. }
  261. static int igbvf_set_coalesce(struct net_device *netdev,
  262. struct ethtool_coalesce *ec)
  263. {
  264. struct igbvf_adapter *adapter = netdev_priv(netdev);
  265. struct e1000_hw *hw = &adapter->hw;
  266. if ((ec->rx_coalesce_usecs >= IGBVF_MIN_ITR_USECS) &&
  267. (ec->rx_coalesce_usecs <= IGBVF_MAX_ITR_USECS)) {
  268. adapter->current_itr = ec->rx_coalesce_usecs << 2;
  269. adapter->requested_itr = 1000000000 /
  270. (adapter->current_itr * 256);
  271. } else if ((ec->rx_coalesce_usecs == 3) ||
  272. (ec->rx_coalesce_usecs == 2)) {
  273. adapter->current_itr = IGBVF_START_ITR;
  274. adapter->requested_itr = ec->rx_coalesce_usecs;
  275. } else if (ec->rx_coalesce_usecs == 0) {
  276. /* The user's desire is to turn off interrupt throttling
  277. * altogether, but due to HW limitations, we can't do that.
  278. * Instead we set a very small value in EITR, which would
  279. * allow ~967k interrupts per second, but allow the adapter's
  280. * internal clocking to still function properly.
  281. */
  282. adapter->current_itr = 4;
  283. adapter->requested_itr = 1000000000 /
  284. (adapter->current_itr * 256);
  285. } else {
  286. return -EINVAL;
  287. }
  288. writel(adapter->current_itr,
  289. hw->hw_addr + adapter->rx_ring->itr_register);
  290. return 0;
  291. }
  292. static int igbvf_nway_reset(struct net_device *netdev)
  293. {
  294. struct igbvf_adapter *adapter = netdev_priv(netdev);
  295. if (netif_running(netdev))
  296. igbvf_reinit_locked(adapter);
  297. return 0;
  298. }
  299. static void igbvf_get_ethtool_stats(struct net_device *netdev,
  300. struct ethtool_stats *stats,
  301. u64 *data)
  302. {
  303. struct igbvf_adapter *adapter = netdev_priv(netdev);
  304. int i;
  305. igbvf_update_stats(adapter);
  306. for (i = 0; i < IGBVF_GLOBAL_STATS_LEN; i++) {
  307. char *p = (char *)adapter +
  308. igbvf_gstrings_stats[i].stat_offset;
  309. char *b = (char *)adapter +
  310. igbvf_gstrings_stats[i].base_stat_offset;
  311. data[i] = ((igbvf_gstrings_stats[i].sizeof_stat ==
  312. sizeof(u64)) ? (*(u64 *)p - *(u64 *)b) :
  313. (*(u32 *)p - *(u32 *)b));
  314. }
  315. }
  316. static int igbvf_get_sset_count(struct net_device *dev, int stringset)
  317. {
  318. switch (stringset) {
  319. case ETH_SS_TEST:
  320. return IGBVF_TEST_LEN;
  321. case ETH_SS_STATS:
  322. return IGBVF_GLOBAL_STATS_LEN;
  323. default:
  324. return -EINVAL;
  325. }
  326. }
  327. static void igbvf_get_strings(struct net_device *netdev, u32 stringset,
  328. u8 *data)
  329. {
  330. u8 *p = data;
  331. int i;
  332. switch (stringset) {
  333. case ETH_SS_TEST:
  334. memcpy(data, *igbvf_gstrings_test, sizeof(igbvf_gstrings_test));
  335. break;
  336. case ETH_SS_STATS:
  337. for (i = 0; i < IGBVF_GLOBAL_STATS_LEN; i++) {
  338. memcpy(p, igbvf_gstrings_stats[i].stat_string,
  339. ETH_GSTRING_LEN);
  340. p += ETH_GSTRING_LEN;
  341. }
  342. break;
  343. }
  344. }
  345. static const struct ethtool_ops igbvf_ethtool_ops = {
  346. .get_drvinfo = igbvf_get_drvinfo,
  347. .get_regs_len = igbvf_get_regs_len,
  348. .get_regs = igbvf_get_regs,
  349. .get_wol = igbvf_get_wol,
  350. .set_wol = igbvf_set_wol,
  351. .get_msglevel = igbvf_get_msglevel,
  352. .set_msglevel = igbvf_set_msglevel,
  353. .nway_reset = igbvf_nway_reset,
  354. .get_link = ethtool_op_get_link,
  355. .get_eeprom_len = igbvf_get_eeprom_len,
  356. .get_eeprom = igbvf_get_eeprom,
  357. .set_eeprom = igbvf_set_eeprom,
  358. .get_ringparam = igbvf_get_ringparam,
  359. .set_ringparam = igbvf_set_ringparam,
  360. .get_pauseparam = igbvf_get_pauseparam,
  361. .set_pauseparam = igbvf_set_pauseparam,
  362. .self_test = igbvf_diag_test,
  363. .get_sset_count = igbvf_get_sset_count,
  364. .get_strings = igbvf_get_strings,
  365. .get_ethtool_stats = igbvf_get_ethtool_stats,
  366. .get_coalesce = igbvf_get_coalesce,
  367. .set_coalesce = igbvf_set_coalesce,
  368. .get_link_ksettings = igbvf_get_link_ksettings,
  369. .set_link_ksettings = igbvf_set_link_ksettings,
  370. };
  371. void igbvf_set_ethtool_ops(struct net_device *netdev)
  372. {
  373. netdev->ethtool_ops = &igbvf_ethtool_ops;
  374. }