iavf_ethtool.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. /* ethtool support for iavf */
  4. #include "iavf.h"
  5. #include <linux/uaccess.h>
  6. /* ethtool statistics helpers */
  7. /**
  8. * struct iavf_stats - definition for an ethtool statistic
  9. * @stat_string: statistic name to display in ethtool -S output
  10. * @sizeof_stat: the sizeof() the stat, must be no greater than sizeof(u64)
  11. * @stat_offset: offsetof() the stat from a base pointer
  12. *
  13. * This structure defines a statistic to be added to the ethtool stats buffer.
  14. * It defines a statistic as offset from a common base pointer. Stats should
  15. * be defined in constant arrays using the IAVF_STAT macro, with every element
  16. * of the array using the same _type for calculating the sizeof_stat and
  17. * stat_offset.
  18. *
  19. * The @sizeof_stat is expected to be sizeof(u8), sizeof(u16), sizeof(u32) or
  20. * sizeof(u64). Other sizes are not expected and will produce a WARN_ONCE from
  21. * the iavf_add_ethtool_stat() helper function.
  22. *
  23. * The @stat_string is interpreted as a format string, allowing formatted
  24. * values to be inserted while looping over multiple structures for a given
  25. * statistics array. Thus, every statistic string in an array should have the
  26. * same type and number of format specifiers, to be formatted by variadic
  27. * arguments to the iavf_add_stat_string() helper function.
  28. **/
  29. struct iavf_stats {
  30. char stat_string[ETH_GSTRING_LEN];
  31. int sizeof_stat;
  32. int stat_offset;
  33. };
  34. /* Helper macro to define an iavf_stat structure with proper size and type.
  35. * Use this when defining constant statistics arrays. Note that @_type expects
  36. * only a type name and is used multiple times.
  37. */
  38. #define IAVF_STAT(_type, _name, _stat) { \
  39. .stat_string = _name, \
  40. .sizeof_stat = FIELD_SIZEOF(_type, _stat), \
  41. .stat_offset = offsetof(_type, _stat) \
  42. }
  43. /* Helper macro for defining some statistics related to queues */
  44. #define IAVF_QUEUE_STAT(_name, _stat) \
  45. IAVF_STAT(struct iavf_ring, _name, _stat)
  46. /* Stats associated with a Tx or Rx ring */
  47. static const struct iavf_stats iavf_gstrings_queue_stats[] = {
  48. IAVF_QUEUE_STAT("%s-%u.packets", stats.packets),
  49. IAVF_QUEUE_STAT("%s-%u.bytes", stats.bytes),
  50. };
  51. /**
  52. * iavf_add_one_ethtool_stat - copy the stat into the supplied buffer
  53. * @data: location to store the stat value
  54. * @pointer: basis for where to copy from
  55. * @stat: the stat definition
  56. *
  57. * Copies the stat data defined by the pointer and stat structure pair into
  58. * the memory supplied as data. Used to implement iavf_add_ethtool_stats and
  59. * iavf_add_queue_stats. If the pointer is null, data will be zero'd.
  60. */
  61. static void
  62. iavf_add_one_ethtool_stat(u64 *data, void *pointer,
  63. const struct iavf_stats *stat)
  64. {
  65. char *p;
  66. if (!pointer) {
  67. /* ensure that the ethtool data buffer is zero'd for any stats
  68. * which don't have a valid pointer.
  69. */
  70. *data = 0;
  71. return;
  72. }
  73. p = (char *)pointer + stat->stat_offset;
  74. switch (stat->sizeof_stat) {
  75. case sizeof(u64):
  76. *data = *((u64 *)p);
  77. break;
  78. case sizeof(u32):
  79. *data = *((u32 *)p);
  80. break;
  81. case sizeof(u16):
  82. *data = *((u16 *)p);
  83. break;
  84. case sizeof(u8):
  85. *data = *((u8 *)p);
  86. break;
  87. default:
  88. WARN_ONCE(1, "unexpected stat size for %s",
  89. stat->stat_string);
  90. *data = 0;
  91. }
  92. }
  93. /**
  94. * __iavf_add_ethtool_stats - copy stats into the ethtool supplied buffer
  95. * @data: ethtool stats buffer
  96. * @pointer: location to copy stats from
  97. * @stats: array of stats to copy
  98. * @size: the size of the stats definition
  99. *
  100. * Copy the stats defined by the stats array using the pointer as a base into
  101. * the data buffer supplied by ethtool. Updates the data pointer to point to
  102. * the next empty location for successive calls to __iavf_add_ethtool_stats.
  103. * If pointer is null, set the data values to zero and update the pointer to
  104. * skip these stats.
  105. **/
  106. static void
  107. __iavf_add_ethtool_stats(u64 **data, void *pointer,
  108. const struct iavf_stats stats[],
  109. const unsigned int size)
  110. {
  111. unsigned int i;
  112. for (i = 0; i < size; i++)
  113. iavf_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
  114. }
  115. /**
  116. * iavf_add_ethtool_stats - copy stats into ethtool supplied buffer
  117. * @data: ethtool stats buffer
  118. * @pointer: location where stats are stored
  119. * @stats: static const array of stat definitions
  120. *
  121. * Macro to ease the use of __iavf_add_ethtool_stats by taking a static
  122. * constant stats array and passing the ARRAY_SIZE(). This avoids typos by
  123. * ensuring that we pass the size associated with the given stats array.
  124. *
  125. * The parameter @stats is evaluated twice, so parameters with side effects
  126. * should be avoided.
  127. **/
  128. #define iavf_add_ethtool_stats(data, pointer, stats) \
  129. __iavf_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
  130. /**
  131. * iavf_add_queue_stats - copy queue statistics into supplied buffer
  132. * @data: ethtool stats buffer
  133. * @ring: the ring to copy
  134. *
  135. * Queue statistics must be copied while protected by
  136. * u64_stats_fetch_begin_irq, so we can't directly use iavf_add_ethtool_stats.
  137. * Assumes that queue stats are defined in iavf_gstrings_queue_stats. If the
  138. * ring pointer is null, zero out the queue stat values and update the data
  139. * pointer. Otherwise safely copy the stats from the ring into the supplied
  140. * buffer and update the data pointer when finished.
  141. *
  142. * This function expects to be called while under rcu_read_lock().
  143. **/
  144. static void
  145. iavf_add_queue_stats(u64 **data, struct iavf_ring *ring)
  146. {
  147. const unsigned int size = ARRAY_SIZE(iavf_gstrings_queue_stats);
  148. const struct iavf_stats *stats = iavf_gstrings_queue_stats;
  149. unsigned int start;
  150. unsigned int i;
  151. /* To avoid invalid statistics values, ensure that we keep retrying
  152. * the copy until we get a consistent value according to
  153. * u64_stats_fetch_retry_irq. But first, make sure our ring is
  154. * non-null before attempting to access its syncp.
  155. */
  156. do {
  157. start = !ring ? 0 : u64_stats_fetch_begin_irq(&ring->syncp);
  158. for (i = 0; i < size; i++)
  159. iavf_add_one_ethtool_stat(&(*data)[i], ring, &stats[i]);
  160. } while (ring && u64_stats_fetch_retry_irq(&ring->syncp, start));
  161. /* Once we successfully copy the stats in, update the data pointer */
  162. *data += size;
  163. }
  164. /**
  165. * __iavf_add_stat_strings - copy stat strings into ethtool buffer
  166. * @p: ethtool supplied buffer
  167. * @stats: stat definitions array
  168. * @size: size of the stats array
  169. *
  170. * Format and copy the strings described by stats into the buffer pointed at
  171. * by p.
  172. **/
  173. static void __iavf_add_stat_strings(u8 **p, const struct iavf_stats stats[],
  174. const unsigned int size, ...)
  175. {
  176. unsigned int i;
  177. for (i = 0; i < size; i++) {
  178. va_list args;
  179. va_start(args, size);
  180. vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
  181. *p += ETH_GSTRING_LEN;
  182. va_end(args);
  183. }
  184. }
  185. /**
  186. * iavf_add_stat_strings - copy stat strings into ethtool buffer
  187. * @p: ethtool supplied buffer
  188. * @stats: stat definitions array
  189. *
  190. * Format and copy the strings described by the const static stats value into
  191. * the buffer pointed at by p.
  192. *
  193. * The parameter @stats is evaluated twice, so parameters with side effects
  194. * should be avoided. Additionally, stats must be an array such that
  195. * ARRAY_SIZE can be called on it.
  196. **/
  197. #define iavf_add_stat_strings(p, stats, ...) \
  198. __iavf_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
  199. #define VF_STAT(_name, _stat) \
  200. IAVF_STAT(struct iavf_adapter, _name, _stat)
  201. static const struct iavf_stats iavf_gstrings_stats[] = {
  202. VF_STAT("rx_bytes", current_stats.rx_bytes),
  203. VF_STAT("rx_unicast", current_stats.rx_unicast),
  204. VF_STAT("rx_multicast", current_stats.rx_multicast),
  205. VF_STAT("rx_broadcast", current_stats.rx_broadcast),
  206. VF_STAT("rx_discards", current_stats.rx_discards),
  207. VF_STAT("rx_unknown_protocol", current_stats.rx_unknown_protocol),
  208. VF_STAT("tx_bytes", current_stats.tx_bytes),
  209. VF_STAT("tx_unicast", current_stats.tx_unicast),
  210. VF_STAT("tx_multicast", current_stats.tx_multicast),
  211. VF_STAT("tx_broadcast", current_stats.tx_broadcast),
  212. VF_STAT("tx_discards", current_stats.tx_discards),
  213. VF_STAT("tx_errors", current_stats.tx_errors),
  214. };
  215. #define IAVF_STATS_LEN ARRAY_SIZE(iavf_gstrings_stats)
  216. #define IAVF_QUEUE_STATS_LEN ARRAY_SIZE(iavf_gstrings_queue_stats)
  217. /* For now we have one and only one private flag and it is only defined
  218. * when we have support for the SKIP_CPU_SYNC DMA attribute. Instead
  219. * of leaving all this code sitting around empty we will strip it unless
  220. * our one private flag is actually available.
  221. */
  222. struct iavf_priv_flags {
  223. char flag_string[ETH_GSTRING_LEN];
  224. u32 flag;
  225. bool read_only;
  226. };
  227. #define IAVF_PRIV_FLAG(_name, _flag, _read_only) { \
  228. .flag_string = _name, \
  229. .flag = _flag, \
  230. .read_only = _read_only, \
  231. }
  232. static const struct iavf_priv_flags iavf_gstrings_priv_flags[] = {
  233. IAVF_PRIV_FLAG("legacy-rx", IAVF_FLAG_LEGACY_RX, 0),
  234. };
  235. #define IAVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(iavf_gstrings_priv_flags)
  236. /**
  237. * iavf_get_link_ksettings - Get Link Speed and Duplex settings
  238. * @netdev: network interface device structure
  239. * @cmd: ethtool command
  240. *
  241. * Reports speed/duplex settings. Because this is a VF, we don't know what
  242. * kind of link we really have, so we fake it.
  243. **/
  244. static int iavf_get_link_ksettings(struct net_device *netdev,
  245. struct ethtool_link_ksettings *cmd)
  246. {
  247. struct iavf_adapter *adapter = netdev_priv(netdev);
  248. ethtool_link_ksettings_zero_link_mode(cmd, supported);
  249. cmd->base.autoneg = AUTONEG_DISABLE;
  250. cmd->base.port = PORT_NONE;
  251. /* Set speed and duplex */
  252. switch (adapter->link_speed) {
  253. case I40E_LINK_SPEED_40GB:
  254. cmd->base.speed = SPEED_40000;
  255. break;
  256. case I40E_LINK_SPEED_25GB:
  257. #ifdef SPEED_25000
  258. cmd->base.speed = SPEED_25000;
  259. #else
  260. netdev_info(netdev,
  261. "Speed is 25G, display not supported by this version of ethtool.\n");
  262. #endif
  263. break;
  264. case I40E_LINK_SPEED_20GB:
  265. cmd->base.speed = SPEED_20000;
  266. break;
  267. case I40E_LINK_SPEED_10GB:
  268. cmd->base.speed = SPEED_10000;
  269. break;
  270. case I40E_LINK_SPEED_1GB:
  271. cmd->base.speed = SPEED_1000;
  272. break;
  273. case I40E_LINK_SPEED_100MB:
  274. cmd->base.speed = SPEED_100;
  275. break;
  276. default:
  277. break;
  278. }
  279. cmd->base.duplex = DUPLEX_FULL;
  280. return 0;
  281. }
  282. /**
  283. * iavf_get_sset_count - Get length of string set
  284. * @netdev: network interface device structure
  285. * @sset: id of string set
  286. *
  287. * Reports size of various string tables.
  288. **/
  289. static int iavf_get_sset_count(struct net_device *netdev, int sset)
  290. {
  291. if (sset == ETH_SS_STATS)
  292. return IAVF_STATS_LEN +
  293. (IAVF_QUEUE_STATS_LEN * 2 * IAVF_MAX_REQ_QUEUES);
  294. else if (sset == ETH_SS_PRIV_FLAGS)
  295. return IAVF_PRIV_FLAGS_STR_LEN;
  296. else
  297. return -EINVAL;
  298. }
  299. /**
  300. * iavf_get_ethtool_stats - report device statistics
  301. * @netdev: network interface device structure
  302. * @stats: ethtool statistics structure
  303. * @data: pointer to data buffer
  304. *
  305. * All statistics are added to the data buffer as an array of u64.
  306. **/
  307. static void iavf_get_ethtool_stats(struct net_device *netdev,
  308. struct ethtool_stats *stats, u64 *data)
  309. {
  310. struct iavf_adapter *adapter = netdev_priv(netdev);
  311. unsigned int i;
  312. iavf_add_ethtool_stats(&data, adapter, iavf_gstrings_stats);
  313. rcu_read_lock();
  314. for (i = 0; i < IAVF_MAX_REQ_QUEUES; i++) {
  315. struct iavf_ring *ring;
  316. /* Avoid accessing un-allocated queues */
  317. ring = (i < adapter->num_active_queues ?
  318. &adapter->tx_rings[i] : NULL);
  319. iavf_add_queue_stats(&data, ring);
  320. /* Avoid accessing un-allocated queues */
  321. ring = (i < adapter->num_active_queues ?
  322. &adapter->rx_rings[i] : NULL);
  323. iavf_add_queue_stats(&data, ring);
  324. }
  325. rcu_read_unlock();
  326. }
  327. /**
  328. * iavf_get_priv_flag_strings - Get private flag strings
  329. * @netdev: network interface device structure
  330. * @data: buffer for string data
  331. *
  332. * Builds the private flags string table
  333. **/
  334. static void iavf_get_priv_flag_strings(struct net_device *netdev, u8 *data)
  335. {
  336. unsigned int i;
  337. for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
  338. snprintf(data, ETH_GSTRING_LEN, "%s",
  339. iavf_gstrings_priv_flags[i].flag_string);
  340. data += ETH_GSTRING_LEN;
  341. }
  342. }
  343. /**
  344. * iavf_get_stat_strings - Get stat strings
  345. * @netdev: network interface device structure
  346. * @data: buffer for string data
  347. *
  348. * Builds the statistics string table
  349. **/
  350. static void iavf_get_stat_strings(struct net_device *netdev, u8 *data)
  351. {
  352. unsigned int i;
  353. iavf_add_stat_strings(&data, iavf_gstrings_stats);
  354. /* Queues are always allocated in pairs, so we just use num_tx_queues
  355. * for both Tx and Rx queues.
  356. */
  357. for (i = 0; i < netdev->num_tx_queues; i++) {
  358. iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
  359. "tx", i);
  360. iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
  361. "rx", i);
  362. }
  363. }
  364. /**
  365. * iavf_get_strings - Get string set
  366. * @netdev: network interface device structure
  367. * @sset: id of string set
  368. * @data: buffer for string data
  369. *
  370. * Builds string tables for various string sets
  371. **/
  372. static void iavf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
  373. {
  374. switch (sset) {
  375. case ETH_SS_STATS:
  376. iavf_get_stat_strings(netdev, data);
  377. break;
  378. case ETH_SS_PRIV_FLAGS:
  379. iavf_get_priv_flag_strings(netdev, data);
  380. break;
  381. default:
  382. break;
  383. }
  384. }
  385. /**
  386. * iavf_get_priv_flags - report device private flags
  387. * @netdev: network interface device structure
  388. *
  389. * The get string set count and the string set should be matched for each
  390. * flag returned. Add new strings for each flag to the iavf_gstrings_priv_flags
  391. * array.
  392. *
  393. * Returns a u32 bitmap of flags.
  394. **/
  395. static u32 iavf_get_priv_flags(struct net_device *netdev)
  396. {
  397. struct iavf_adapter *adapter = netdev_priv(netdev);
  398. u32 i, ret_flags = 0;
  399. for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
  400. const struct iavf_priv_flags *priv_flags;
  401. priv_flags = &iavf_gstrings_priv_flags[i];
  402. if (priv_flags->flag & adapter->flags)
  403. ret_flags |= BIT(i);
  404. }
  405. return ret_flags;
  406. }
  407. /**
  408. * iavf_set_priv_flags - set private flags
  409. * @netdev: network interface device structure
  410. * @flags: bit flags to be set
  411. **/
  412. static int iavf_set_priv_flags(struct net_device *netdev, u32 flags)
  413. {
  414. struct iavf_adapter *adapter = netdev_priv(netdev);
  415. u32 orig_flags, new_flags, changed_flags;
  416. u32 i;
  417. orig_flags = READ_ONCE(adapter->flags);
  418. new_flags = orig_flags;
  419. for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
  420. const struct iavf_priv_flags *priv_flags;
  421. priv_flags = &iavf_gstrings_priv_flags[i];
  422. if (flags & BIT(i))
  423. new_flags |= priv_flags->flag;
  424. else
  425. new_flags &= ~(priv_flags->flag);
  426. if (priv_flags->read_only &&
  427. ((orig_flags ^ new_flags) & ~BIT(i)))
  428. return -EOPNOTSUPP;
  429. }
  430. /* Before we finalize any flag changes, any checks which we need to
  431. * perform to determine if the new flags will be supported should go
  432. * here...
  433. */
  434. /* Compare and exchange the new flags into place. If we failed, that
  435. * is if cmpxchg returns anything but the old value, this means
  436. * something else must have modified the flags variable since we
  437. * copied it. We'll just punt with an error and log something in the
  438. * message buffer.
  439. */
  440. if (cmpxchg(&adapter->flags, orig_flags, new_flags) != orig_flags) {
  441. dev_warn(&adapter->pdev->dev,
  442. "Unable to update adapter->flags as it was modified by another thread...\n");
  443. return -EAGAIN;
  444. }
  445. changed_flags = orig_flags ^ new_flags;
  446. /* Process any additional changes needed as a result of flag changes.
  447. * The changed_flags value reflects the list of bits that were changed
  448. * in the code above.
  449. */
  450. /* issue a reset to force legacy-rx change to take effect */
  451. if (changed_flags & IAVF_FLAG_LEGACY_RX) {
  452. if (netif_running(netdev)) {
  453. adapter->flags |= IAVF_FLAG_RESET_NEEDED;
  454. schedule_work(&adapter->reset_task);
  455. }
  456. }
  457. return 0;
  458. }
  459. /**
  460. * iavf_get_msglevel - Get debug message level
  461. * @netdev: network interface device structure
  462. *
  463. * Returns current debug message level.
  464. **/
  465. static u32 iavf_get_msglevel(struct net_device *netdev)
  466. {
  467. struct iavf_adapter *adapter = netdev_priv(netdev);
  468. return adapter->msg_enable;
  469. }
  470. /**
  471. * iavf_set_msglevel - Set debug message level
  472. * @netdev: network interface device structure
  473. * @data: message level
  474. *
  475. * Set current debug message level. Higher values cause the driver to
  476. * be noisier.
  477. **/
  478. static void iavf_set_msglevel(struct net_device *netdev, u32 data)
  479. {
  480. struct iavf_adapter *adapter = netdev_priv(netdev);
  481. if (IAVF_DEBUG_USER & data)
  482. adapter->hw.debug_mask = data;
  483. adapter->msg_enable = data;
  484. }
  485. /**
  486. * iavf_get_drvinfo - Get driver info
  487. * @netdev: network interface device structure
  488. * @drvinfo: ethool driver info structure
  489. *
  490. * Returns information about the driver and device for display to the user.
  491. **/
  492. static void iavf_get_drvinfo(struct net_device *netdev,
  493. struct ethtool_drvinfo *drvinfo)
  494. {
  495. struct iavf_adapter *adapter = netdev_priv(netdev);
  496. strlcpy(drvinfo->driver, iavf_driver_name, 32);
  497. strlcpy(drvinfo->version, iavf_driver_version, 32);
  498. strlcpy(drvinfo->fw_version, "N/A", 4);
  499. strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
  500. drvinfo->n_priv_flags = IAVF_PRIV_FLAGS_STR_LEN;
  501. }
  502. /**
  503. * iavf_get_ringparam - Get ring parameters
  504. * @netdev: network interface device structure
  505. * @ring: ethtool ringparam structure
  506. *
  507. * Returns current ring parameters. TX and RX rings are reported separately,
  508. * but the number of rings is not reported.
  509. **/
  510. static void iavf_get_ringparam(struct net_device *netdev,
  511. struct ethtool_ringparam *ring)
  512. {
  513. struct iavf_adapter *adapter = netdev_priv(netdev);
  514. ring->rx_max_pending = IAVF_MAX_RXD;
  515. ring->tx_max_pending = IAVF_MAX_TXD;
  516. ring->rx_pending = adapter->rx_desc_count;
  517. ring->tx_pending = adapter->tx_desc_count;
  518. }
  519. /**
  520. * iavf_set_ringparam - Set ring parameters
  521. * @netdev: network interface device structure
  522. * @ring: ethtool ringparam structure
  523. *
  524. * Sets ring parameters. TX and RX rings are controlled separately, but the
  525. * number of rings is not specified, so all rings get the same settings.
  526. **/
  527. static int iavf_set_ringparam(struct net_device *netdev,
  528. struct ethtool_ringparam *ring)
  529. {
  530. struct iavf_adapter *adapter = netdev_priv(netdev);
  531. u32 new_rx_count, new_tx_count;
  532. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  533. return -EINVAL;
  534. new_tx_count = clamp_t(u32, ring->tx_pending,
  535. IAVF_MIN_TXD,
  536. IAVF_MAX_TXD);
  537. new_tx_count = ALIGN(new_tx_count, IAVF_REQ_DESCRIPTOR_MULTIPLE);
  538. new_rx_count = clamp_t(u32, ring->rx_pending,
  539. IAVF_MIN_RXD,
  540. IAVF_MAX_RXD);
  541. new_rx_count = ALIGN(new_rx_count, IAVF_REQ_DESCRIPTOR_MULTIPLE);
  542. /* if nothing to do return success */
  543. if ((new_tx_count == adapter->tx_desc_count) &&
  544. (new_rx_count == adapter->rx_desc_count))
  545. return 0;
  546. adapter->tx_desc_count = new_tx_count;
  547. adapter->rx_desc_count = new_rx_count;
  548. if (netif_running(netdev)) {
  549. adapter->flags |= IAVF_FLAG_RESET_NEEDED;
  550. schedule_work(&adapter->reset_task);
  551. }
  552. return 0;
  553. }
  554. /**
  555. * __iavf_get_coalesce - get per-queue coalesce settings
  556. * @netdev: the netdev to check
  557. * @ec: ethtool coalesce data structure
  558. * @queue: which queue to pick
  559. *
  560. * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
  561. * are per queue. If queue is <0 then we default to queue 0 as the
  562. * representative value.
  563. **/
  564. static int __iavf_get_coalesce(struct net_device *netdev,
  565. struct ethtool_coalesce *ec, int queue)
  566. {
  567. struct iavf_adapter *adapter = netdev_priv(netdev);
  568. struct iavf_vsi *vsi = &adapter->vsi;
  569. struct iavf_ring *rx_ring, *tx_ring;
  570. ec->tx_max_coalesced_frames = vsi->work_limit;
  571. ec->rx_max_coalesced_frames = vsi->work_limit;
  572. /* Rx and Tx usecs per queue value. If user doesn't specify the
  573. * queue, return queue 0's value to represent.
  574. */
  575. if (queue < 0)
  576. queue = 0;
  577. else if (queue >= adapter->num_active_queues)
  578. return -EINVAL;
  579. rx_ring = &adapter->rx_rings[queue];
  580. tx_ring = &adapter->tx_rings[queue];
  581. if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
  582. ec->use_adaptive_rx_coalesce = 1;
  583. if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
  584. ec->use_adaptive_tx_coalesce = 1;
  585. ec->rx_coalesce_usecs = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
  586. ec->tx_coalesce_usecs = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
  587. return 0;
  588. }
  589. /**
  590. * iavf_get_coalesce - Get interrupt coalescing settings
  591. * @netdev: network interface device structure
  592. * @ec: ethtool coalesce structure
  593. *
  594. * Returns current coalescing settings. This is referred to elsewhere in the
  595. * driver as Interrupt Throttle Rate, as this is how the hardware describes
  596. * this functionality. Note that if per-queue settings have been modified this
  597. * only represents the settings of queue 0.
  598. **/
  599. static int iavf_get_coalesce(struct net_device *netdev,
  600. struct ethtool_coalesce *ec)
  601. {
  602. return __iavf_get_coalesce(netdev, ec, -1);
  603. }
  604. /**
  605. * iavf_get_per_queue_coalesce - get coalesce values for specific queue
  606. * @netdev: netdev to read
  607. * @ec: coalesce settings from ethtool
  608. * @queue: the queue to read
  609. *
  610. * Read specific queue's coalesce settings.
  611. **/
  612. static int iavf_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
  613. struct ethtool_coalesce *ec)
  614. {
  615. return __iavf_get_coalesce(netdev, ec, queue);
  616. }
  617. /**
  618. * iavf_set_itr_per_queue - set ITR values for specific queue
  619. * @adapter: the VF adapter struct to set values for
  620. * @ec: coalesce settings from ethtool
  621. * @queue: the queue to modify
  622. *
  623. * Change the ITR settings for a specific queue.
  624. **/
  625. static void iavf_set_itr_per_queue(struct iavf_adapter *adapter,
  626. struct ethtool_coalesce *ec, int queue)
  627. {
  628. struct iavf_ring *rx_ring = &adapter->rx_rings[queue];
  629. struct iavf_ring *tx_ring = &adapter->tx_rings[queue];
  630. struct iavf_q_vector *q_vector;
  631. rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
  632. tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
  633. rx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
  634. if (!ec->use_adaptive_rx_coalesce)
  635. rx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
  636. tx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
  637. if (!ec->use_adaptive_tx_coalesce)
  638. tx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
  639. q_vector = rx_ring->q_vector;
  640. q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
  641. q_vector = tx_ring->q_vector;
  642. q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
  643. /* The interrupt handler itself will take care of programming
  644. * the Tx and Rx ITR values based on the values we have entered
  645. * into the q_vector, no need to write the values now.
  646. */
  647. }
  648. /**
  649. * __iavf_set_coalesce - set coalesce settings for particular queue
  650. * @netdev: the netdev to change
  651. * @ec: ethtool coalesce settings
  652. * @queue: the queue to change
  653. *
  654. * Sets the coalesce settings for a particular queue.
  655. **/
  656. static int __iavf_set_coalesce(struct net_device *netdev,
  657. struct ethtool_coalesce *ec, int queue)
  658. {
  659. struct iavf_adapter *adapter = netdev_priv(netdev);
  660. struct iavf_vsi *vsi = &adapter->vsi;
  661. int i;
  662. if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
  663. vsi->work_limit = ec->tx_max_coalesced_frames_irq;
  664. if (ec->rx_coalesce_usecs == 0) {
  665. if (ec->use_adaptive_rx_coalesce)
  666. netif_info(adapter, drv, netdev, "rx-usecs=0, need to disable adaptive-rx for a complete disable\n");
  667. } else if ((ec->rx_coalesce_usecs < IAVF_MIN_ITR) ||
  668. (ec->rx_coalesce_usecs > IAVF_MAX_ITR)) {
  669. netif_info(adapter, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
  670. return -EINVAL;
  671. } else if (ec->tx_coalesce_usecs == 0) {
  672. if (ec->use_adaptive_tx_coalesce)
  673. netif_info(adapter, drv, netdev, "tx-usecs=0, need to disable adaptive-tx for a complete disable\n");
  674. } else if ((ec->tx_coalesce_usecs < IAVF_MIN_ITR) ||
  675. (ec->tx_coalesce_usecs > IAVF_MAX_ITR)) {
  676. netif_info(adapter, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
  677. return -EINVAL;
  678. }
  679. /* Rx and Tx usecs has per queue value. If user doesn't specify the
  680. * queue, apply to all queues.
  681. */
  682. if (queue < 0) {
  683. for (i = 0; i < adapter->num_active_queues; i++)
  684. iavf_set_itr_per_queue(adapter, ec, i);
  685. } else if (queue < adapter->num_active_queues) {
  686. iavf_set_itr_per_queue(adapter, ec, queue);
  687. } else {
  688. netif_info(adapter, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
  689. adapter->num_active_queues - 1);
  690. return -EINVAL;
  691. }
  692. return 0;
  693. }
  694. /**
  695. * iavf_set_coalesce - Set interrupt coalescing settings
  696. * @netdev: network interface device structure
  697. * @ec: ethtool coalesce structure
  698. *
  699. * Change current coalescing settings for every queue.
  700. **/
  701. static int iavf_set_coalesce(struct net_device *netdev,
  702. struct ethtool_coalesce *ec)
  703. {
  704. return __iavf_set_coalesce(netdev, ec, -1);
  705. }
  706. /**
  707. * iavf_set_per_queue_coalesce - set specific queue's coalesce settings
  708. * @netdev: the netdev to change
  709. * @ec: ethtool's coalesce settings
  710. * @queue: the queue to modify
  711. *
  712. * Modifies a specific queue's coalesce settings.
  713. */
  714. static int iavf_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
  715. struct ethtool_coalesce *ec)
  716. {
  717. return __iavf_set_coalesce(netdev, ec, queue);
  718. }
  719. /**
  720. * iavf_get_rxnfc - command to get RX flow classification rules
  721. * @netdev: network interface device structure
  722. * @cmd: ethtool rxnfc command
  723. * @rule_locs: pointer to store rule locations
  724. *
  725. * Returns Success if the command is supported.
  726. **/
  727. static int iavf_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
  728. u32 *rule_locs)
  729. {
  730. struct iavf_adapter *adapter = netdev_priv(netdev);
  731. int ret = -EOPNOTSUPP;
  732. switch (cmd->cmd) {
  733. case ETHTOOL_GRXRINGS:
  734. cmd->data = adapter->num_active_queues;
  735. ret = 0;
  736. break;
  737. case ETHTOOL_GRXFH:
  738. netdev_info(netdev,
  739. "RSS hash info is not available to vf, use pf.\n");
  740. break;
  741. default:
  742. break;
  743. }
  744. return ret;
  745. }
  746. /**
  747. * iavf_get_channels: get the number of channels supported by the device
  748. * @netdev: network interface device structure
  749. * @ch: channel information structure
  750. *
  751. * For the purposes of our device, we only use combined channels, i.e. a tx/rx
  752. * queue pair. Report one extra channel to match our "other" MSI-X vector.
  753. **/
  754. static void iavf_get_channels(struct net_device *netdev,
  755. struct ethtool_channels *ch)
  756. {
  757. struct iavf_adapter *adapter = netdev_priv(netdev);
  758. /* Report maximum channels */
  759. ch->max_combined = IAVF_MAX_REQ_QUEUES;
  760. ch->max_other = NONQ_VECS;
  761. ch->other_count = NONQ_VECS;
  762. ch->combined_count = adapter->num_active_queues;
  763. }
  764. /**
  765. * iavf_set_channels: set the new channel count
  766. * @netdev: network interface device structure
  767. * @ch: channel information structure
  768. *
  769. * Negotiate a new number of channels with the PF then do a reset. During
  770. * reset we'll realloc queues and fix the RSS table. Returns 0 on success,
  771. * negative on failure.
  772. **/
  773. static int iavf_set_channels(struct net_device *netdev,
  774. struct ethtool_channels *ch)
  775. {
  776. struct iavf_adapter *adapter = netdev_priv(netdev);
  777. int num_req = ch->combined_count;
  778. if (num_req != adapter->num_active_queues &&
  779. !(adapter->vf_res->vf_cap_flags &
  780. VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)) {
  781. dev_info(&adapter->pdev->dev, "PF is not capable of queue negotiation.\n");
  782. return -EINVAL;
  783. }
  784. if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
  785. adapter->num_tc) {
  786. dev_info(&adapter->pdev->dev, "Cannot set channels since ADq is enabled.\n");
  787. return -EINVAL;
  788. }
  789. /* All of these should have already been checked by ethtool before this
  790. * even gets to us, but just to be sure.
  791. */
  792. if (num_req <= 0 || num_req > IAVF_MAX_REQ_QUEUES)
  793. return -EINVAL;
  794. if (ch->rx_count || ch->tx_count || ch->other_count != NONQ_VECS)
  795. return -EINVAL;
  796. adapter->num_req_queues = num_req;
  797. return iavf_request_queues(adapter, num_req);
  798. }
  799. /**
  800. * iavf_get_rxfh_key_size - get the RSS hash key size
  801. * @netdev: network interface device structure
  802. *
  803. * Returns the table size.
  804. **/
  805. static u32 iavf_get_rxfh_key_size(struct net_device *netdev)
  806. {
  807. struct iavf_adapter *adapter = netdev_priv(netdev);
  808. return adapter->rss_key_size;
  809. }
  810. /**
  811. * iavf_get_rxfh_indir_size - get the rx flow hash indirection table size
  812. * @netdev: network interface device structure
  813. *
  814. * Returns the table size.
  815. **/
  816. static u32 iavf_get_rxfh_indir_size(struct net_device *netdev)
  817. {
  818. struct iavf_adapter *adapter = netdev_priv(netdev);
  819. return adapter->rss_lut_size;
  820. }
  821. /**
  822. * iavf_get_rxfh - get the rx flow hash indirection table
  823. * @netdev: network interface device structure
  824. * @indir: indirection table
  825. * @key: hash key
  826. * @hfunc: hash function in use
  827. *
  828. * Reads the indirection table directly from the hardware. Always returns 0.
  829. **/
  830. static int iavf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
  831. u8 *hfunc)
  832. {
  833. struct iavf_adapter *adapter = netdev_priv(netdev);
  834. u16 i;
  835. if (hfunc)
  836. *hfunc = ETH_RSS_HASH_TOP;
  837. if (!indir)
  838. return 0;
  839. memcpy(key, adapter->rss_key, adapter->rss_key_size);
  840. /* Each 32 bits pointed by 'indir' is stored with a lut entry */
  841. for (i = 0; i < adapter->rss_lut_size; i++)
  842. indir[i] = (u32)adapter->rss_lut[i];
  843. return 0;
  844. }
  845. /**
  846. * iavf_set_rxfh - set the rx flow hash indirection table
  847. * @netdev: network interface device structure
  848. * @indir: indirection table
  849. * @key: hash key
  850. * @hfunc: hash function to use
  851. *
  852. * Returns -EINVAL if the table specifies an inavlid queue id, otherwise
  853. * returns 0 after programming the table.
  854. **/
  855. static int iavf_set_rxfh(struct net_device *netdev, const u32 *indir,
  856. const u8 *key, const u8 hfunc)
  857. {
  858. struct iavf_adapter *adapter = netdev_priv(netdev);
  859. u16 i;
  860. /* We do not allow change in unsupported parameters */
  861. if (key ||
  862. (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
  863. return -EOPNOTSUPP;
  864. if (!indir)
  865. return 0;
  866. if (key)
  867. memcpy(adapter->rss_key, key, adapter->rss_key_size);
  868. /* Each 32 bits pointed by 'indir' is stored with a lut entry */
  869. for (i = 0; i < adapter->rss_lut_size; i++)
  870. adapter->rss_lut[i] = (u8)(indir[i]);
  871. return iavf_config_rss(adapter);
  872. }
  873. static const struct ethtool_ops iavf_ethtool_ops = {
  874. .get_drvinfo = iavf_get_drvinfo,
  875. .get_link = ethtool_op_get_link,
  876. .get_ringparam = iavf_get_ringparam,
  877. .set_ringparam = iavf_set_ringparam,
  878. .get_strings = iavf_get_strings,
  879. .get_ethtool_stats = iavf_get_ethtool_stats,
  880. .get_sset_count = iavf_get_sset_count,
  881. .get_priv_flags = iavf_get_priv_flags,
  882. .set_priv_flags = iavf_set_priv_flags,
  883. .get_msglevel = iavf_get_msglevel,
  884. .set_msglevel = iavf_set_msglevel,
  885. .get_coalesce = iavf_get_coalesce,
  886. .set_coalesce = iavf_set_coalesce,
  887. .get_per_queue_coalesce = iavf_get_per_queue_coalesce,
  888. .set_per_queue_coalesce = iavf_set_per_queue_coalesce,
  889. .get_rxnfc = iavf_get_rxnfc,
  890. .get_rxfh_indir_size = iavf_get_rxfh_indir_size,
  891. .get_rxfh = iavf_get_rxfh,
  892. .set_rxfh = iavf_set_rxfh,
  893. .get_channels = iavf_get_channels,
  894. .set_channels = iavf_set_channels,
  895. .get_rxfh_key_size = iavf_get_rxfh_key_size,
  896. .get_link_ksettings = iavf_get_link_ksettings,
  897. };
  898. /**
  899. * iavf_set_ethtool_ops - Initialize ethtool ops struct
  900. * @netdev: network interface device structure
  901. *
  902. * Sets ethtool ops struct in our netdev so that ethtool can call
  903. * our functions.
  904. **/
  905. void iavf_set_ethtool_ops(struct net_device *netdev)
  906. {
  907. netdev->ethtool_ops = &iavf_ethtool_ops;
  908. }