ethtool.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. /* ethtool support for ixgbevf */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include <linux/types.h>
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/pci.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/ethtool.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/if_vlan.h>
  13. #include <linux/uaccess.h>
  14. #include "ixgbevf.h"
  15. enum {NETDEV_STATS, IXGBEVF_STATS};
  16. struct ixgbe_stats {
  17. char stat_string[ETH_GSTRING_LEN];
  18. int type;
  19. int sizeof_stat;
  20. int stat_offset;
  21. };
  22. #define IXGBEVF_STAT(_name, _stat) { \
  23. .stat_string = _name, \
  24. .type = IXGBEVF_STATS, \
  25. .sizeof_stat = sizeof_field(struct ixgbevf_adapter, _stat), \
  26. .stat_offset = offsetof(struct ixgbevf_adapter, _stat) \
  27. }
  28. #define IXGBEVF_NETDEV_STAT(_net_stat) { \
  29. .stat_string = #_net_stat, \
  30. .type = NETDEV_STATS, \
  31. .sizeof_stat = sizeof_field(struct net_device_stats, _net_stat), \
  32. .stat_offset = offsetof(struct net_device_stats, _net_stat) \
  33. }
  34. static struct ixgbe_stats ixgbevf_gstrings_stats[] = {
  35. IXGBEVF_NETDEV_STAT(rx_packets),
  36. IXGBEVF_NETDEV_STAT(tx_packets),
  37. IXGBEVF_NETDEV_STAT(rx_bytes),
  38. IXGBEVF_NETDEV_STAT(tx_bytes),
  39. IXGBEVF_STAT("tx_busy", tx_busy),
  40. IXGBEVF_STAT("tx_restart_queue", restart_queue),
  41. IXGBEVF_STAT("tx_timeout_count", tx_timeout_count),
  42. IXGBEVF_NETDEV_STAT(multicast),
  43. IXGBEVF_STAT("rx_csum_offload_errors", hw_csum_rx_error),
  44. IXGBEVF_STAT("alloc_rx_page", alloc_rx_page),
  45. IXGBEVF_STAT("alloc_rx_page_failed", alloc_rx_page_failed),
  46. IXGBEVF_STAT("alloc_rx_buff_failed", alloc_rx_buff_failed),
  47. IXGBEVF_STAT("tx_ipsec", tx_ipsec),
  48. IXGBEVF_STAT("rx_ipsec", rx_ipsec),
  49. };
  50. #define IXGBEVF_QUEUE_STATS_LEN ( \
  51. (((struct ixgbevf_adapter *)netdev_priv(netdev))->num_tx_queues + \
  52. ((struct ixgbevf_adapter *)netdev_priv(netdev))->num_xdp_queues + \
  53. ((struct ixgbevf_adapter *)netdev_priv(netdev))->num_rx_queues) * \
  54. (sizeof(struct ixgbevf_stats) / sizeof(u64)))
  55. #define IXGBEVF_GLOBAL_STATS_LEN ARRAY_SIZE(ixgbevf_gstrings_stats)
  56. #define IXGBEVF_STATS_LEN (IXGBEVF_GLOBAL_STATS_LEN + IXGBEVF_QUEUE_STATS_LEN)
  57. static const char ixgbe_gstrings_test[][ETH_GSTRING_LEN] = {
  58. "Register test (offline)",
  59. "Link test (on/offline)"
  60. };
  61. #define IXGBEVF_TEST_LEN (sizeof(ixgbe_gstrings_test) / ETH_GSTRING_LEN)
  62. static const char ixgbevf_priv_flags_strings[][ETH_GSTRING_LEN] = {
  63. #define IXGBEVF_PRIV_FLAGS_LEGACY_RX BIT(0)
  64. "legacy-rx",
  65. };
  66. #define IXGBEVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(ixgbevf_priv_flags_strings)
  67. static int ixgbevf_get_link_ksettings(struct net_device *netdev,
  68. struct ethtool_link_ksettings *cmd)
  69. {
  70. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  71. ethtool_link_ksettings_zero_link_mode(cmd, supported);
  72. ethtool_link_ksettings_add_link_mode(cmd, supported, 10000baseT_Full);
  73. cmd->base.autoneg = AUTONEG_DISABLE;
  74. cmd->base.port = -1;
  75. if (adapter->link_up) {
  76. __u32 speed = SPEED_10000;
  77. switch (adapter->link_speed) {
  78. case IXGBE_LINK_SPEED_10GB_FULL:
  79. speed = SPEED_10000;
  80. break;
  81. case IXGBE_LINK_SPEED_1GB_FULL:
  82. speed = SPEED_1000;
  83. break;
  84. case IXGBE_LINK_SPEED_100_FULL:
  85. speed = SPEED_100;
  86. break;
  87. }
  88. cmd->base.speed = speed;
  89. cmd->base.duplex = DUPLEX_FULL;
  90. } else {
  91. cmd->base.speed = SPEED_UNKNOWN;
  92. cmd->base.duplex = DUPLEX_UNKNOWN;
  93. }
  94. return 0;
  95. }
  96. static u32 ixgbevf_get_msglevel(struct net_device *netdev)
  97. {
  98. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  99. return adapter->msg_enable;
  100. }
  101. static void ixgbevf_set_msglevel(struct net_device *netdev, u32 data)
  102. {
  103. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  104. adapter->msg_enable = data;
  105. }
  106. static int ixgbevf_get_regs_len(struct net_device *netdev)
  107. {
  108. #define IXGBE_REGS_LEN 45
  109. return IXGBE_REGS_LEN * sizeof(u32);
  110. }
  111. static void ixgbevf_get_regs(struct net_device *netdev,
  112. struct ethtool_regs *regs,
  113. void *p)
  114. {
  115. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  116. struct ixgbe_hw *hw = &adapter->hw;
  117. u32 *regs_buff = p;
  118. u32 regs_len = ixgbevf_get_regs_len(netdev);
  119. u8 i;
  120. memset(p, 0, regs_len);
  121. /* generate a number suitable for ethtool's register version */
  122. regs->version = (1u << 24) | (hw->revision_id << 16) | hw->device_id;
  123. /* General Registers */
  124. regs_buff[0] = IXGBE_READ_REG(hw, IXGBE_VFCTRL);
  125. regs_buff[1] = IXGBE_READ_REG(hw, IXGBE_VFSTATUS);
  126. regs_buff[2] = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
  127. regs_buff[3] = IXGBE_READ_REG(hw, IXGBE_VFRXMEMWRAP);
  128. regs_buff[4] = IXGBE_READ_REG(hw, IXGBE_VFFRTIMER);
  129. /* Interrupt */
  130. /* don't read EICR because it can clear interrupt causes, instead
  131. * read EICS which is a shadow but doesn't clear EICR
  132. */
  133. regs_buff[5] = IXGBE_READ_REG(hw, IXGBE_VTEICS);
  134. regs_buff[6] = IXGBE_READ_REG(hw, IXGBE_VTEICS);
  135. regs_buff[7] = IXGBE_READ_REG(hw, IXGBE_VTEIMS);
  136. regs_buff[8] = IXGBE_READ_REG(hw, IXGBE_VTEIMC);
  137. regs_buff[9] = IXGBE_READ_REG(hw, IXGBE_VTEIAC);
  138. regs_buff[10] = IXGBE_READ_REG(hw, IXGBE_VTEIAM);
  139. regs_buff[11] = IXGBE_READ_REG(hw, IXGBE_VTEITR(0));
  140. regs_buff[12] = IXGBE_READ_REG(hw, IXGBE_VTIVAR(0));
  141. regs_buff[13] = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
  142. /* Receive DMA */
  143. for (i = 0; i < 2; i++)
  144. regs_buff[14 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDBAL(i));
  145. for (i = 0; i < 2; i++)
  146. regs_buff[16 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDBAH(i));
  147. for (i = 0; i < 2; i++)
  148. regs_buff[18 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDLEN(i));
  149. for (i = 0; i < 2; i++)
  150. regs_buff[20 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDH(i));
  151. for (i = 0; i < 2; i++)
  152. regs_buff[22 + i] = IXGBE_READ_REG(hw, IXGBE_VFRDT(i));
  153. for (i = 0; i < 2; i++)
  154. regs_buff[24 + i] = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(i));
  155. for (i = 0; i < 2; i++)
  156. regs_buff[26 + i] = IXGBE_READ_REG(hw, IXGBE_VFSRRCTL(i));
  157. /* Receive */
  158. regs_buff[28] = IXGBE_READ_REG(hw, IXGBE_VFPSRTYPE);
  159. /* Transmit */
  160. for (i = 0; i < 2; i++)
  161. regs_buff[29 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDBAL(i));
  162. for (i = 0; i < 2; i++)
  163. regs_buff[31 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDBAH(i));
  164. for (i = 0; i < 2; i++)
  165. regs_buff[33 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDLEN(i));
  166. for (i = 0; i < 2; i++)
  167. regs_buff[35 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDH(i));
  168. for (i = 0; i < 2; i++)
  169. regs_buff[37 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDT(i));
  170. for (i = 0; i < 2; i++)
  171. regs_buff[39 + i] = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(i));
  172. for (i = 0; i < 2; i++)
  173. regs_buff[41 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDWBAL(i));
  174. for (i = 0; i < 2; i++)
  175. regs_buff[43 + i] = IXGBE_READ_REG(hw, IXGBE_VFTDWBAH(i));
  176. }
  177. static void ixgbevf_get_drvinfo(struct net_device *netdev,
  178. struct ethtool_drvinfo *drvinfo)
  179. {
  180. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  181. strscpy(drvinfo->driver, ixgbevf_driver_name, sizeof(drvinfo->driver));
  182. strscpy(drvinfo->bus_info, pci_name(adapter->pdev),
  183. sizeof(drvinfo->bus_info));
  184. drvinfo->n_priv_flags = IXGBEVF_PRIV_FLAGS_STR_LEN;
  185. }
  186. static void ixgbevf_get_ringparam(struct net_device *netdev,
  187. struct ethtool_ringparam *ring,
  188. struct kernel_ethtool_ringparam *kernel_ring,
  189. struct netlink_ext_ack *extack)
  190. {
  191. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  192. ring->rx_max_pending = IXGBEVF_MAX_RXD;
  193. ring->tx_max_pending = IXGBEVF_MAX_TXD;
  194. ring->rx_pending = adapter->rx_ring_count;
  195. ring->tx_pending = adapter->tx_ring_count;
  196. }
  197. static int ixgbevf_set_ringparam(struct net_device *netdev,
  198. struct ethtool_ringparam *ring,
  199. struct kernel_ethtool_ringparam *kernel_ring,
  200. struct netlink_ext_ack *extack)
  201. {
  202. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  203. struct ixgbevf_ring *tx_ring = NULL, *rx_ring = NULL;
  204. u32 new_rx_count, new_tx_count;
  205. int i, j, err = 0;
  206. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  207. return -EINVAL;
  208. new_tx_count = max_t(u32, ring->tx_pending, IXGBEVF_MIN_TXD);
  209. new_tx_count = min_t(u32, new_tx_count, IXGBEVF_MAX_TXD);
  210. new_tx_count = ALIGN(new_tx_count, IXGBE_REQ_TX_DESCRIPTOR_MULTIPLE);
  211. new_rx_count = max_t(u32, ring->rx_pending, IXGBEVF_MIN_RXD);
  212. new_rx_count = min_t(u32, new_rx_count, IXGBEVF_MAX_RXD);
  213. new_rx_count = ALIGN(new_rx_count, IXGBE_REQ_RX_DESCRIPTOR_MULTIPLE);
  214. /* if nothing to do return success */
  215. if ((new_tx_count == adapter->tx_ring_count) &&
  216. (new_rx_count == adapter->rx_ring_count))
  217. return 0;
  218. while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
  219. usleep_range(1000, 2000);
  220. if (!netif_running(adapter->netdev)) {
  221. for (i = 0; i < adapter->num_tx_queues; i++)
  222. adapter->tx_ring[i]->count = new_tx_count;
  223. for (i = 0; i < adapter->num_xdp_queues; i++)
  224. adapter->xdp_ring[i]->count = new_tx_count;
  225. for (i = 0; i < adapter->num_rx_queues; i++)
  226. adapter->rx_ring[i]->count = new_rx_count;
  227. adapter->tx_ring_count = new_tx_count;
  228. adapter->xdp_ring_count = new_tx_count;
  229. adapter->rx_ring_count = new_rx_count;
  230. goto clear_reset;
  231. }
  232. if (new_tx_count != adapter->tx_ring_count) {
  233. tx_ring = vmalloc_array(adapter->num_tx_queues +
  234. adapter->num_xdp_queues,
  235. sizeof(*tx_ring));
  236. if (!tx_ring) {
  237. err = -ENOMEM;
  238. goto clear_reset;
  239. }
  240. for (i = 0; i < adapter->num_tx_queues; i++) {
  241. /* clone ring and setup updated count */
  242. tx_ring[i] = *adapter->tx_ring[i];
  243. tx_ring[i].count = new_tx_count;
  244. err = ixgbevf_setup_tx_resources(&tx_ring[i]);
  245. if (err) {
  246. while (i) {
  247. i--;
  248. ixgbevf_free_tx_resources(&tx_ring[i]);
  249. }
  250. vfree(tx_ring);
  251. tx_ring = NULL;
  252. goto clear_reset;
  253. }
  254. }
  255. for (j = 0; j < adapter->num_xdp_queues; i++, j++) {
  256. /* clone ring and setup updated count */
  257. tx_ring[i] = *adapter->xdp_ring[j];
  258. tx_ring[i].count = new_tx_count;
  259. err = ixgbevf_setup_tx_resources(&tx_ring[i]);
  260. if (err) {
  261. while (i) {
  262. i--;
  263. ixgbevf_free_tx_resources(&tx_ring[i]);
  264. }
  265. vfree(tx_ring);
  266. tx_ring = NULL;
  267. goto clear_reset;
  268. }
  269. }
  270. }
  271. if (new_rx_count != adapter->rx_ring_count) {
  272. rx_ring = vmalloc(array_size(sizeof(*rx_ring),
  273. adapter->num_rx_queues));
  274. if (!rx_ring) {
  275. err = -ENOMEM;
  276. goto clear_reset;
  277. }
  278. for (i = 0; i < adapter->num_rx_queues; i++) {
  279. /* clone ring and setup updated count */
  280. rx_ring[i] = *adapter->rx_ring[i];
  281. /* Clear copied XDP RX-queue info */
  282. memset(&rx_ring[i].xdp_rxq, 0,
  283. sizeof(rx_ring[i].xdp_rxq));
  284. rx_ring[i].count = new_rx_count;
  285. err = ixgbevf_setup_rx_resources(adapter, &rx_ring[i]);
  286. if (err) {
  287. while (i) {
  288. i--;
  289. ixgbevf_free_rx_resources(&rx_ring[i]);
  290. }
  291. vfree(rx_ring);
  292. rx_ring = NULL;
  293. goto clear_reset;
  294. }
  295. }
  296. }
  297. /* bring interface down to prepare for update */
  298. ixgbevf_down(adapter);
  299. /* Tx */
  300. if (tx_ring) {
  301. for (i = 0; i < adapter->num_tx_queues; i++) {
  302. ixgbevf_free_tx_resources(adapter->tx_ring[i]);
  303. *adapter->tx_ring[i] = tx_ring[i];
  304. }
  305. adapter->tx_ring_count = new_tx_count;
  306. for (j = 0; j < adapter->num_xdp_queues; i++, j++) {
  307. ixgbevf_free_tx_resources(adapter->xdp_ring[j]);
  308. *adapter->xdp_ring[j] = tx_ring[i];
  309. }
  310. adapter->xdp_ring_count = new_tx_count;
  311. vfree(tx_ring);
  312. tx_ring = NULL;
  313. }
  314. /* Rx */
  315. if (rx_ring) {
  316. for (i = 0; i < adapter->num_rx_queues; i++) {
  317. ixgbevf_free_rx_resources(adapter->rx_ring[i]);
  318. *adapter->rx_ring[i] = rx_ring[i];
  319. }
  320. adapter->rx_ring_count = new_rx_count;
  321. vfree(rx_ring);
  322. rx_ring = NULL;
  323. }
  324. /* restore interface using new values */
  325. ixgbevf_up(adapter);
  326. clear_reset:
  327. /* free Tx resources if Rx error is encountered */
  328. if (tx_ring) {
  329. for (i = 0;
  330. i < adapter->num_tx_queues + adapter->num_xdp_queues; i++)
  331. ixgbevf_free_tx_resources(&tx_ring[i]);
  332. vfree(tx_ring);
  333. }
  334. clear_bit(__IXGBEVF_RESETTING, &adapter->state);
  335. return err;
  336. }
  337. static int ixgbevf_get_sset_count(struct net_device *netdev, int stringset)
  338. {
  339. switch (stringset) {
  340. case ETH_SS_TEST:
  341. return IXGBEVF_TEST_LEN;
  342. case ETH_SS_STATS:
  343. return IXGBEVF_STATS_LEN;
  344. case ETH_SS_PRIV_FLAGS:
  345. return IXGBEVF_PRIV_FLAGS_STR_LEN;
  346. default:
  347. return -EINVAL;
  348. }
  349. }
  350. static void ixgbevf_get_ethtool_stats(struct net_device *netdev,
  351. struct ethtool_stats *stats, u64 *data)
  352. {
  353. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  354. struct rtnl_link_stats64 temp;
  355. const struct rtnl_link_stats64 *net_stats;
  356. unsigned int start;
  357. struct ixgbevf_ring *ring;
  358. int i, j;
  359. char *p;
  360. ixgbevf_update_stats(adapter);
  361. net_stats = dev_get_stats(netdev, &temp);
  362. for (i = 0; i < IXGBEVF_GLOBAL_STATS_LEN; i++) {
  363. switch (ixgbevf_gstrings_stats[i].type) {
  364. case NETDEV_STATS:
  365. p = (char *)net_stats +
  366. ixgbevf_gstrings_stats[i].stat_offset;
  367. break;
  368. case IXGBEVF_STATS:
  369. p = (char *)adapter +
  370. ixgbevf_gstrings_stats[i].stat_offset;
  371. break;
  372. default:
  373. data[i] = 0;
  374. continue;
  375. }
  376. data[i] = (ixgbevf_gstrings_stats[i].sizeof_stat ==
  377. sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
  378. }
  379. /* populate Tx queue data */
  380. for (j = 0; j < adapter->num_tx_queues; j++) {
  381. ring = adapter->tx_ring[j];
  382. if (!ring) {
  383. data[i++] = 0;
  384. data[i++] = 0;
  385. continue;
  386. }
  387. do {
  388. start = u64_stats_fetch_begin(&ring->syncp);
  389. data[i] = ring->stats.packets;
  390. data[i + 1] = ring->stats.bytes;
  391. } while (u64_stats_fetch_retry(&ring->syncp, start));
  392. i += 2;
  393. }
  394. /* populate XDP queue data */
  395. for (j = 0; j < adapter->num_xdp_queues; j++) {
  396. ring = adapter->xdp_ring[j];
  397. if (!ring) {
  398. data[i++] = 0;
  399. data[i++] = 0;
  400. continue;
  401. }
  402. do {
  403. start = u64_stats_fetch_begin(&ring->syncp);
  404. data[i] = ring->stats.packets;
  405. data[i + 1] = ring->stats.bytes;
  406. } while (u64_stats_fetch_retry(&ring->syncp, start));
  407. i += 2;
  408. }
  409. /* populate Rx queue data */
  410. for (j = 0; j < adapter->num_rx_queues; j++) {
  411. ring = adapter->rx_ring[j];
  412. if (!ring) {
  413. data[i++] = 0;
  414. data[i++] = 0;
  415. continue;
  416. }
  417. do {
  418. start = u64_stats_fetch_begin(&ring->syncp);
  419. data[i] = ring->stats.packets;
  420. data[i + 1] = ring->stats.bytes;
  421. } while (u64_stats_fetch_retry(&ring->syncp, start));
  422. i += 2;
  423. }
  424. }
  425. static void ixgbevf_get_strings(struct net_device *netdev, u32 stringset,
  426. u8 *data)
  427. {
  428. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  429. char *p = (char *)data;
  430. int i;
  431. switch (stringset) {
  432. case ETH_SS_TEST:
  433. memcpy(data, *ixgbe_gstrings_test,
  434. IXGBEVF_TEST_LEN * ETH_GSTRING_LEN);
  435. break;
  436. case ETH_SS_STATS:
  437. for (i = 0; i < IXGBEVF_GLOBAL_STATS_LEN; i++) {
  438. memcpy(p, ixgbevf_gstrings_stats[i].stat_string,
  439. ETH_GSTRING_LEN);
  440. p += ETH_GSTRING_LEN;
  441. }
  442. for (i = 0; i < adapter->num_tx_queues; i++) {
  443. sprintf(p, "tx_queue_%u_packets", i);
  444. p += ETH_GSTRING_LEN;
  445. sprintf(p, "tx_queue_%u_bytes", i);
  446. p += ETH_GSTRING_LEN;
  447. }
  448. for (i = 0; i < adapter->num_xdp_queues; i++) {
  449. sprintf(p, "xdp_queue_%u_packets", i);
  450. p += ETH_GSTRING_LEN;
  451. sprintf(p, "xdp_queue_%u_bytes", i);
  452. p += ETH_GSTRING_LEN;
  453. }
  454. for (i = 0; i < adapter->num_rx_queues; i++) {
  455. sprintf(p, "rx_queue_%u_packets", i);
  456. p += ETH_GSTRING_LEN;
  457. sprintf(p, "rx_queue_%u_bytes", i);
  458. p += ETH_GSTRING_LEN;
  459. }
  460. break;
  461. case ETH_SS_PRIV_FLAGS:
  462. memcpy(data, ixgbevf_priv_flags_strings,
  463. IXGBEVF_PRIV_FLAGS_STR_LEN * ETH_GSTRING_LEN);
  464. break;
  465. }
  466. }
  467. static int ixgbevf_link_test(struct ixgbevf_adapter *adapter, u64 *data)
  468. {
  469. struct ixgbe_hw *hw = &adapter->hw;
  470. bool link_up;
  471. u32 link_speed = 0;
  472. *data = 0;
  473. hw->mac.ops.check_link(hw, &link_speed, &link_up, true);
  474. if (!link_up)
  475. *data = 1;
  476. return *data;
  477. }
  478. /* ethtool register test data */
  479. struct ixgbevf_reg_test {
  480. u16 reg;
  481. u8 array_len;
  482. u8 test_type;
  483. u32 mask;
  484. u32 write;
  485. };
  486. /* In the hardware, registers are laid out either singly, in arrays
  487. * spaced 0x40 bytes apart, or in contiguous tables. We assume
  488. * most tests take place on arrays or single registers (handled
  489. * as a single-element array) and special-case the tables.
  490. * Table tests are always pattern tests.
  491. *
  492. * We also make provision for some required setup steps by specifying
  493. * registers to be written without any read-back testing.
  494. */
  495. #define PATTERN_TEST 1
  496. #define SET_READ_TEST 2
  497. #define WRITE_NO_TEST 3
  498. #define TABLE32_TEST 4
  499. #define TABLE64_TEST_LO 5
  500. #define TABLE64_TEST_HI 6
  501. /* default VF register test */
  502. static const struct ixgbevf_reg_test reg_test_vf[] = {
  503. { IXGBE_VFRDBAL(0), 2, PATTERN_TEST, 0xFFFFFF80, 0xFFFFFF80 },
  504. { IXGBE_VFRDBAH(0), 2, PATTERN_TEST, 0xFFFFFFFF, 0xFFFFFFFF },
  505. { IXGBE_VFRDLEN(0), 2, PATTERN_TEST, 0x000FFF80, 0x000FFFFF },
  506. { IXGBE_VFRXDCTL(0), 2, WRITE_NO_TEST, 0, IXGBE_RXDCTL_ENABLE },
  507. { IXGBE_VFRDT(0), 2, PATTERN_TEST, 0x0000FFFF, 0x0000FFFF },
  508. { IXGBE_VFRXDCTL(0), 2, WRITE_NO_TEST, 0, 0 },
  509. { IXGBE_VFTDBAL(0), 2, PATTERN_TEST, 0xFFFFFF80, 0xFFFFFFFF },
  510. { IXGBE_VFTDBAH(0), 2, PATTERN_TEST, 0xFFFFFFFF, 0xFFFFFFFF },
  511. { IXGBE_VFTDLEN(0), 2, PATTERN_TEST, 0x000FFF80, 0x000FFF80 },
  512. { .reg = 0 }
  513. };
  514. static const u32 register_test_patterns[] = {
  515. 0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF
  516. };
  517. static bool reg_pattern_test(struct ixgbevf_adapter *adapter, u64 *data,
  518. int reg, u32 mask, u32 write)
  519. {
  520. u32 pat, val, before;
  521. if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
  522. *data = 1;
  523. return true;
  524. }
  525. for (pat = 0; pat < ARRAY_SIZE(register_test_patterns); pat++) {
  526. before = ixgbevf_read_reg(&adapter->hw, reg);
  527. ixgbe_write_reg(&adapter->hw, reg,
  528. register_test_patterns[pat] & write);
  529. val = ixgbevf_read_reg(&adapter->hw, reg);
  530. if (val != (register_test_patterns[pat] & write & mask)) {
  531. hw_dbg(&adapter->hw,
  532. "pattern test reg %04X failed: got 0x%08X expected 0x%08X\n",
  533. reg, val,
  534. register_test_patterns[pat] & write & mask);
  535. *data = reg;
  536. ixgbe_write_reg(&adapter->hw, reg, before);
  537. return true;
  538. }
  539. ixgbe_write_reg(&adapter->hw, reg, before);
  540. }
  541. return false;
  542. }
  543. static bool reg_set_and_check(struct ixgbevf_adapter *adapter, u64 *data,
  544. int reg, u32 mask, u32 write)
  545. {
  546. u32 val, before;
  547. if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
  548. *data = 1;
  549. return true;
  550. }
  551. before = ixgbevf_read_reg(&adapter->hw, reg);
  552. ixgbe_write_reg(&adapter->hw, reg, write & mask);
  553. val = ixgbevf_read_reg(&adapter->hw, reg);
  554. if ((write & mask) != (val & mask)) {
  555. pr_err("set/check reg %04X test failed: got 0x%08X expected 0x%08X\n",
  556. reg, (val & mask), write & mask);
  557. *data = reg;
  558. ixgbe_write_reg(&adapter->hw, reg, before);
  559. return true;
  560. }
  561. ixgbe_write_reg(&adapter->hw, reg, before);
  562. return false;
  563. }
  564. static int ixgbevf_reg_test(struct ixgbevf_adapter *adapter, u64 *data)
  565. {
  566. const struct ixgbevf_reg_test *test;
  567. u32 i;
  568. if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
  569. dev_err(&adapter->pdev->dev,
  570. "Adapter removed - register test blocked\n");
  571. *data = 1;
  572. return 1;
  573. }
  574. test = reg_test_vf;
  575. /* Perform the register test, looping through the test table
  576. * until we either fail or reach the null entry.
  577. */
  578. while (test->reg) {
  579. for (i = 0; i < test->array_len; i++) {
  580. bool b = false;
  581. switch (test->test_type) {
  582. case PATTERN_TEST:
  583. b = reg_pattern_test(adapter, data,
  584. test->reg + (i * 0x40),
  585. test->mask,
  586. test->write);
  587. break;
  588. case SET_READ_TEST:
  589. b = reg_set_and_check(adapter, data,
  590. test->reg + (i * 0x40),
  591. test->mask,
  592. test->write);
  593. break;
  594. case WRITE_NO_TEST:
  595. ixgbe_write_reg(&adapter->hw,
  596. test->reg + (i * 0x40),
  597. test->write);
  598. break;
  599. case TABLE32_TEST:
  600. b = reg_pattern_test(adapter, data,
  601. test->reg + (i * 4),
  602. test->mask,
  603. test->write);
  604. break;
  605. case TABLE64_TEST_LO:
  606. b = reg_pattern_test(adapter, data,
  607. test->reg + (i * 8),
  608. test->mask,
  609. test->write);
  610. break;
  611. case TABLE64_TEST_HI:
  612. b = reg_pattern_test(adapter, data,
  613. test->reg + 4 + (i * 8),
  614. test->mask,
  615. test->write);
  616. break;
  617. }
  618. if (b)
  619. return 1;
  620. }
  621. test++;
  622. }
  623. *data = 0;
  624. return *data;
  625. }
  626. static void ixgbevf_diag_test(struct net_device *netdev,
  627. struct ethtool_test *eth_test, u64 *data)
  628. {
  629. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  630. bool if_running = netif_running(netdev);
  631. if (IXGBE_REMOVED(adapter->hw.hw_addr)) {
  632. dev_err(&adapter->pdev->dev,
  633. "Adapter removed - test blocked\n");
  634. data[0] = 1;
  635. data[1] = 1;
  636. eth_test->flags |= ETH_TEST_FL_FAILED;
  637. return;
  638. }
  639. set_bit(__IXGBEVF_TESTING, &adapter->state);
  640. if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
  641. /* Offline tests */
  642. hw_dbg(&adapter->hw, "offline testing starting\n");
  643. /* Link test performed before hardware reset so autoneg doesn't
  644. * interfere with test result
  645. */
  646. if (ixgbevf_link_test(adapter, &data[1]))
  647. eth_test->flags |= ETH_TEST_FL_FAILED;
  648. if (if_running)
  649. /* indicate we're in test mode */
  650. ixgbevf_close(netdev);
  651. else
  652. ixgbevf_reset(adapter);
  653. hw_dbg(&adapter->hw, "register testing starting\n");
  654. if (ixgbevf_reg_test(adapter, &data[0]))
  655. eth_test->flags |= ETH_TEST_FL_FAILED;
  656. ixgbevf_reset(adapter);
  657. clear_bit(__IXGBEVF_TESTING, &adapter->state);
  658. if (if_running)
  659. ixgbevf_open(netdev);
  660. } else {
  661. hw_dbg(&adapter->hw, "online testing starting\n");
  662. /* Online tests */
  663. if (ixgbevf_link_test(adapter, &data[1]))
  664. eth_test->flags |= ETH_TEST_FL_FAILED;
  665. /* Online tests aren't run; pass by default */
  666. data[0] = 0;
  667. clear_bit(__IXGBEVF_TESTING, &adapter->state);
  668. }
  669. msleep_interruptible(4 * 1000);
  670. }
  671. static int ixgbevf_nway_reset(struct net_device *netdev)
  672. {
  673. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  674. if (netif_running(netdev))
  675. ixgbevf_reinit_locked(adapter);
  676. return 0;
  677. }
  678. static int ixgbevf_get_coalesce(struct net_device *netdev,
  679. struct ethtool_coalesce *ec,
  680. struct kernel_ethtool_coalesce *kernel_coal,
  681. struct netlink_ext_ack *extack)
  682. {
  683. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  684. /* only valid if in constant ITR mode */
  685. if (adapter->rx_itr_setting <= 1)
  686. ec->rx_coalesce_usecs = adapter->rx_itr_setting;
  687. else
  688. ec->rx_coalesce_usecs = adapter->rx_itr_setting >> 2;
  689. /* if in mixed Tx/Rx queues per vector mode, report only Rx settings */
  690. if (adapter->q_vector[0]->tx.count && adapter->q_vector[0]->rx.count)
  691. return 0;
  692. /* only valid if in constant ITR mode */
  693. if (adapter->tx_itr_setting <= 1)
  694. ec->tx_coalesce_usecs = adapter->tx_itr_setting;
  695. else
  696. ec->tx_coalesce_usecs = adapter->tx_itr_setting >> 2;
  697. return 0;
  698. }
  699. static int ixgbevf_set_coalesce(struct net_device *netdev,
  700. struct ethtool_coalesce *ec,
  701. struct kernel_ethtool_coalesce *kernel_coal,
  702. struct netlink_ext_ack *extack)
  703. {
  704. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  705. struct ixgbevf_q_vector *q_vector;
  706. int num_vectors, i;
  707. u16 tx_itr_param, rx_itr_param;
  708. /* don't accept Tx specific changes if we've got mixed RxTx vectors */
  709. if (adapter->q_vector[0]->tx.count &&
  710. adapter->q_vector[0]->rx.count && ec->tx_coalesce_usecs)
  711. return -EINVAL;
  712. if ((ec->rx_coalesce_usecs > (IXGBE_MAX_EITR >> 2)) ||
  713. (ec->tx_coalesce_usecs > (IXGBE_MAX_EITR >> 2)))
  714. return -EINVAL;
  715. if (ec->rx_coalesce_usecs > 1)
  716. adapter->rx_itr_setting = ec->rx_coalesce_usecs << 2;
  717. else
  718. adapter->rx_itr_setting = ec->rx_coalesce_usecs;
  719. if (adapter->rx_itr_setting == 1)
  720. rx_itr_param = IXGBE_20K_ITR;
  721. else
  722. rx_itr_param = adapter->rx_itr_setting;
  723. if (ec->tx_coalesce_usecs > 1)
  724. adapter->tx_itr_setting = ec->tx_coalesce_usecs << 2;
  725. else
  726. adapter->tx_itr_setting = ec->tx_coalesce_usecs;
  727. if (adapter->tx_itr_setting == 1)
  728. tx_itr_param = IXGBE_12K_ITR;
  729. else
  730. tx_itr_param = adapter->tx_itr_setting;
  731. num_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  732. for (i = 0; i < num_vectors; i++) {
  733. q_vector = adapter->q_vector[i];
  734. if (q_vector->tx.count && !q_vector->rx.count)
  735. /* Tx only */
  736. q_vector->itr = tx_itr_param;
  737. else
  738. /* Rx only or mixed */
  739. q_vector->itr = rx_itr_param;
  740. ixgbevf_write_eitr(q_vector);
  741. }
  742. return 0;
  743. }
  744. static u32 ixgbevf_get_rx_ring_count(struct net_device *dev)
  745. {
  746. struct ixgbevf_adapter *adapter = netdev_priv(dev);
  747. return adapter->num_rx_queues;
  748. }
  749. static u32 ixgbevf_get_rxfh_indir_size(struct net_device *netdev)
  750. {
  751. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  752. if (adapter->hw.mac.type >= ixgbe_mac_X550_vf)
  753. return IXGBEVF_X550_VFRETA_SIZE;
  754. return IXGBEVF_82599_RETA_SIZE;
  755. }
  756. static u32 ixgbevf_get_rxfh_key_size(struct net_device *netdev)
  757. {
  758. return IXGBEVF_RSS_HASH_KEY_SIZE;
  759. }
  760. static int ixgbevf_get_rxfh(struct net_device *netdev,
  761. struct ethtool_rxfh_param *rxfh)
  762. {
  763. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  764. int err = 0;
  765. rxfh->hfunc = ETH_RSS_HASH_TOP;
  766. if (adapter->hw.mac.type >= ixgbe_mac_X550_vf) {
  767. if (rxfh->key)
  768. memcpy(rxfh->key, adapter->rss_key,
  769. ixgbevf_get_rxfh_key_size(netdev));
  770. if (rxfh->indir) {
  771. int i;
  772. for (i = 0; i < IXGBEVF_X550_VFRETA_SIZE; i++)
  773. rxfh->indir[i] = adapter->rss_indir_tbl[i];
  774. }
  775. } else {
  776. /* If neither indirection table nor hash key was requested
  777. * - just return a success avoiding taking any locks.
  778. */
  779. if (!rxfh->indir && !rxfh->key)
  780. return 0;
  781. spin_lock_bh(&adapter->mbx_lock);
  782. if (rxfh->indir)
  783. err = ixgbevf_get_reta_locked(&adapter->hw,
  784. rxfh->indir,
  785. adapter->num_rx_queues);
  786. if (!err && rxfh->key)
  787. err = ixgbevf_get_rss_key_locked(&adapter->hw,
  788. rxfh->key);
  789. spin_unlock_bh(&adapter->mbx_lock);
  790. }
  791. return err;
  792. }
  793. static u32 ixgbevf_get_priv_flags(struct net_device *netdev)
  794. {
  795. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  796. u32 priv_flags = 0;
  797. if (adapter->flags & IXGBEVF_FLAGS_LEGACY_RX)
  798. priv_flags |= IXGBEVF_PRIV_FLAGS_LEGACY_RX;
  799. return priv_flags;
  800. }
  801. static int ixgbevf_set_priv_flags(struct net_device *netdev, u32 priv_flags)
  802. {
  803. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  804. unsigned int flags = adapter->flags;
  805. flags &= ~IXGBEVF_FLAGS_LEGACY_RX;
  806. if (priv_flags & IXGBEVF_PRIV_FLAGS_LEGACY_RX)
  807. flags |= IXGBEVF_FLAGS_LEGACY_RX;
  808. if (flags != adapter->flags) {
  809. adapter->flags = flags;
  810. /* reset interface to repopulate queues */
  811. if (netif_running(netdev))
  812. ixgbevf_reinit_locked(adapter);
  813. }
  814. return 0;
  815. }
  816. static const struct ethtool_ops ixgbevf_ethtool_ops = {
  817. .supported_coalesce_params = ETHTOOL_COALESCE_USECS,
  818. .get_drvinfo = ixgbevf_get_drvinfo,
  819. .get_regs_len = ixgbevf_get_regs_len,
  820. .get_regs = ixgbevf_get_regs,
  821. .nway_reset = ixgbevf_nway_reset,
  822. .get_link = ethtool_op_get_link,
  823. .get_ringparam = ixgbevf_get_ringparam,
  824. .set_ringparam = ixgbevf_set_ringparam,
  825. .get_msglevel = ixgbevf_get_msglevel,
  826. .set_msglevel = ixgbevf_set_msglevel,
  827. .self_test = ixgbevf_diag_test,
  828. .get_sset_count = ixgbevf_get_sset_count,
  829. .get_strings = ixgbevf_get_strings,
  830. .get_ethtool_stats = ixgbevf_get_ethtool_stats,
  831. .get_coalesce = ixgbevf_get_coalesce,
  832. .set_coalesce = ixgbevf_set_coalesce,
  833. .get_rx_ring_count = ixgbevf_get_rx_ring_count,
  834. .get_rxfh_indir_size = ixgbevf_get_rxfh_indir_size,
  835. .get_rxfh_key_size = ixgbevf_get_rxfh_key_size,
  836. .get_rxfh = ixgbevf_get_rxfh,
  837. .get_link_ksettings = ixgbevf_get_link_ksettings,
  838. .get_priv_flags = ixgbevf_get_priv_flags,
  839. .set_priv_flags = ixgbevf_set_priv_flags,
  840. };
  841. void ixgbevf_set_ethtool_ops(struct net_device *netdev)
  842. {
  843. netdev->ethtool_ops = &ixgbevf_ethtool_ops;
  844. }