ethtool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. strscpy(drvinfo->driver, igbvf_driver_name, sizeof(drvinfo->driver));
  138. strscpy(drvinfo->bus_info, pci_name(adapter->pdev),
  139. sizeof(drvinfo->bus_info));
  140. }
  141. static void igbvf_get_ringparam(struct net_device *netdev,
  142. struct ethtool_ringparam *ring,
  143. struct kernel_ethtool_ringparam *kernel_ring,
  144. struct netlink_ext_ack *extack)
  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. struct kernel_ethtool_ringparam *kernel_ring,
  157. struct netlink_ext_ack *extack)
  158. {
  159. struct igbvf_adapter *adapter = netdev_priv(netdev);
  160. struct igbvf_ring *temp_ring;
  161. int err = 0;
  162. u32 new_rx_count, new_tx_count;
  163. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  164. return -EINVAL;
  165. new_rx_count = max_t(u32, ring->rx_pending, IGBVF_MIN_RXD);
  166. new_rx_count = min_t(u32, new_rx_count, IGBVF_MAX_RXD);
  167. new_rx_count = ALIGN(new_rx_count, REQ_RX_DESCRIPTOR_MULTIPLE);
  168. new_tx_count = max_t(u32, ring->tx_pending, IGBVF_MIN_TXD);
  169. new_tx_count = min_t(u32, new_tx_count, IGBVF_MAX_TXD);
  170. new_tx_count = ALIGN(new_tx_count, REQ_TX_DESCRIPTOR_MULTIPLE);
  171. if ((new_tx_count == adapter->tx_ring->count) &&
  172. (new_rx_count == adapter->rx_ring->count)) {
  173. /* nothing to do */
  174. return 0;
  175. }
  176. while (test_and_set_bit(__IGBVF_RESETTING, &adapter->state))
  177. usleep_range(1000, 2000);
  178. if (!netif_running(adapter->netdev)) {
  179. adapter->tx_ring->count = new_tx_count;
  180. adapter->rx_ring->count = new_rx_count;
  181. goto clear_reset;
  182. }
  183. temp_ring = vmalloc(sizeof(struct igbvf_ring));
  184. if (!temp_ring) {
  185. err = -ENOMEM;
  186. goto clear_reset;
  187. }
  188. igbvf_down(adapter);
  189. /* We can't just free everything and then setup again,
  190. * because the ISRs in MSI-X mode get passed pointers
  191. * to the Tx and Rx ring structs.
  192. */
  193. if (new_tx_count != adapter->tx_ring->count) {
  194. memcpy(temp_ring, adapter->tx_ring, sizeof(struct igbvf_ring));
  195. temp_ring->count = new_tx_count;
  196. err = igbvf_setup_tx_resources(adapter, temp_ring);
  197. if (err)
  198. goto err_setup;
  199. igbvf_free_tx_resources(adapter->tx_ring);
  200. memcpy(adapter->tx_ring, temp_ring, sizeof(struct igbvf_ring));
  201. }
  202. if (new_rx_count != adapter->rx_ring->count) {
  203. memcpy(temp_ring, adapter->rx_ring, sizeof(struct igbvf_ring));
  204. temp_ring->count = new_rx_count;
  205. err = igbvf_setup_rx_resources(adapter, temp_ring);
  206. if (err)
  207. goto err_setup;
  208. igbvf_free_rx_resources(adapter->rx_ring);
  209. memcpy(adapter->rx_ring, temp_ring, sizeof(struct igbvf_ring));
  210. }
  211. err_setup:
  212. igbvf_up(adapter);
  213. vfree(temp_ring);
  214. clear_reset:
  215. clear_bit(__IGBVF_RESETTING, &adapter->state);
  216. return err;
  217. }
  218. static int igbvf_link_test(struct igbvf_adapter *adapter, u64 *data)
  219. {
  220. struct e1000_hw *hw = &adapter->hw;
  221. *data = 0;
  222. spin_lock_bh(&hw->mbx_lock);
  223. hw->mac.ops.check_for_link(hw);
  224. spin_unlock_bh(&hw->mbx_lock);
  225. if (!(er32(STATUS) & E1000_STATUS_LU))
  226. *data = 1;
  227. return *data;
  228. }
  229. static void igbvf_diag_test(struct net_device *netdev,
  230. struct ethtool_test *eth_test, u64 *data)
  231. {
  232. struct igbvf_adapter *adapter = netdev_priv(netdev);
  233. set_bit(__IGBVF_TESTING, &adapter->state);
  234. /* Link test performed before hardware reset so autoneg doesn't
  235. * interfere with test result
  236. */
  237. if (igbvf_link_test(adapter, &data[0]))
  238. eth_test->flags |= ETH_TEST_FL_FAILED;
  239. clear_bit(__IGBVF_TESTING, &adapter->state);
  240. msleep_interruptible(4 * 1000);
  241. }
  242. static void igbvf_get_wol(struct net_device *netdev,
  243. struct ethtool_wolinfo *wol)
  244. {
  245. wol->supported = 0;
  246. wol->wolopts = 0;
  247. }
  248. static int igbvf_set_wol(struct net_device *netdev,
  249. struct ethtool_wolinfo *wol)
  250. {
  251. return -EOPNOTSUPP;
  252. }
  253. static int igbvf_get_coalesce(struct net_device *netdev,
  254. struct ethtool_coalesce *ec,
  255. struct kernel_ethtool_coalesce *kernel_coal,
  256. struct netlink_ext_ack *extack)
  257. {
  258. struct igbvf_adapter *adapter = netdev_priv(netdev);
  259. if (adapter->requested_itr <= 3)
  260. ec->rx_coalesce_usecs = adapter->requested_itr;
  261. else
  262. ec->rx_coalesce_usecs = adapter->current_itr >> 2;
  263. return 0;
  264. }
  265. static int igbvf_set_coalesce(struct net_device *netdev,
  266. struct ethtool_coalesce *ec,
  267. struct kernel_ethtool_coalesce *kernel_coal,
  268. struct netlink_ext_ack *extack)
  269. {
  270. struct igbvf_adapter *adapter = netdev_priv(netdev);
  271. struct e1000_hw *hw = &adapter->hw;
  272. if ((ec->rx_coalesce_usecs >= IGBVF_MIN_ITR_USECS) &&
  273. (ec->rx_coalesce_usecs <= IGBVF_MAX_ITR_USECS)) {
  274. adapter->current_itr = ec->rx_coalesce_usecs << 2;
  275. adapter->requested_itr = 1000000000 /
  276. (adapter->current_itr * 256);
  277. } else if ((ec->rx_coalesce_usecs == 3) ||
  278. (ec->rx_coalesce_usecs == 2)) {
  279. adapter->current_itr = IGBVF_START_ITR;
  280. adapter->requested_itr = ec->rx_coalesce_usecs;
  281. } else if (ec->rx_coalesce_usecs == 0) {
  282. /* The user's desire is to turn off interrupt throttling
  283. * altogether, but due to HW limitations, we can't do that.
  284. * Instead we set a very small value in EITR, which would
  285. * allow ~967k interrupts per second, but allow the adapter's
  286. * internal clocking to still function properly.
  287. */
  288. adapter->current_itr = 4;
  289. adapter->requested_itr = 1000000000 /
  290. (adapter->current_itr * 256);
  291. } else {
  292. return -EINVAL;
  293. }
  294. writel(adapter->current_itr,
  295. hw->hw_addr + adapter->rx_ring->itr_register);
  296. return 0;
  297. }
  298. static int igbvf_nway_reset(struct net_device *netdev)
  299. {
  300. struct igbvf_adapter *adapter = netdev_priv(netdev);
  301. if (netif_running(netdev))
  302. igbvf_reinit_locked(adapter);
  303. return 0;
  304. }
  305. static void igbvf_get_ethtool_stats(struct net_device *netdev,
  306. struct ethtool_stats *stats,
  307. u64 *data)
  308. {
  309. struct igbvf_adapter *adapter = netdev_priv(netdev);
  310. int i;
  311. igbvf_update_stats(adapter);
  312. for (i = 0; i < IGBVF_GLOBAL_STATS_LEN; i++) {
  313. char *p = (char *)adapter +
  314. igbvf_gstrings_stats[i].stat_offset;
  315. char *b = (char *)adapter +
  316. igbvf_gstrings_stats[i].base_stat_offset;
  317. data[i] = ((igbvf_gstrings_stats[i].sizeof_stat ==
  318. sizeof(u64)) ? (*(u64 *)p - *(u64 *)b) :
  319. (*(u32 *)p - *(u32 *)b));
  320. }
  321. }
  322. static int igbvf_get_sset_count(struct net_device *dev, int stringset)
  323. {
  324. switch (stringset) {
  325. case ETH_SS_TEST:
  326. return IGBVF_TEST_LEN;
  327. case ETH_SS_STATS:
  328. return IGBVF_GLOBAL_STATS_LEN;
  329. default:
  330. return -EINVAL;
  331. }
  332. }
  333. static void igbvf_get_strings(struct net_device *netdev, u32 stringset,
  334. u8 *data)
  335. {
  336. u8 *p = data;
  337. int i;
  338. switch (stringset) {
  339. case ETH_SS_TEST:
  340. memcpy(data, *igbvf_gstrings_test, sizeof(igbvf_gstrings_test));
  341. break;
  342. case ETH_SS_STATS:
  343. for (i = 0; i < IGBVF_GLOBAL_STATS_LEN; i++) {
  344. memcpy(p, igbvf_gstrings_stats[i].stat_string,
  345. ETH_GSTRING_LEN);
  346. p += ETH_GSTRING_LEN;
  347. }
  348. break;
  349. }
  350. }
  351. static const struct ethtool_ops igbvf_ethtool_ops = {
  352. .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
  353. .get_drvinfo = igbvf_get_drvinfo,
  354. .get_regs_len = igbvf_get_regs_len,
  355. .get_regs = igbvf_get_regs,
  356. .get_wol = igbvf_get_wol,
  357. .set_wol = igbvf_set_wol,
  358. .get_msglevel = igbvf_get_msglevel,
  359. .set_msglevel = igbvf_set_msglevel,
  360. .nway_reset = igbvf_nway_reset,
  361. .get_link = ethtool_op_get_link,
  362. .get_eeprom_len = igbvf_get_eeprom_len,
  363. .get_eeprom = igbvf_get_eeprom,
  364. .set_eeprom = igbvf_set_eeprom,
  365. .get_ringparam = igbvf_get_ringparam,
  366. .set_ringparam = igbvf_set_ringparam,
  367. .get_pauseparam = igbvf_get_pauseparam,
  368. .set_pauseparam = igbvf_set_pauseparam,
  369. .self_test = igbvf_diag_test,
  370. .get_sset_count = igbvf_get_sset_count,
  371. .get_strings = igbvf_get_strings,
  372. .get_ethtool_stats = igbvf_get_ethtool_stats,
  373. .get_coalesce = igbvf_get_coalesce,
  374. .set_coalesce = igbvf_set_coalesce,
  375. .get_link_ksettings = igbvf_get_link_ksettings,
  376. .set_link_ksettings = igbvf_set_link_ksettings,
  377. };
  378. void igbvf_set_ethtool_ops(struct net_device *netdev)
  379. {
  380. netdev->ethtool_ops = &igbvf_ethtool_ops;
  381. }