ethtool.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mac80211 ethtool hooks for cfg80211
  4. *
  5. * Copied from cfg.c - originally
  6. * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2014 Intel Corporation (Author: Johannes Berg)
  8. * Copyright (C) 2018, 2022-2023 Intel Corporation
  9. */
  10. #include <linux/types.h>
  11. #include <net/cfg80211.h>
  12. #include "ieee80211_i.h"
  13. #include "sta_info.h"
  14. #include "driver-ops.h"
  15. static int ieee80211_set_ringparam(struct net_device *dev,
  16. struct ethtool_ringparam *rp,
  17. struct kernel_ethtool_ringparam *kernel_rp,
  18. struct netlink_ext_ack *extack)
  19. {
  20. struct ieee80211_local *local = wiphy_priv(dev->ieee80211_ptr->wiphy);
  21. if (rp->rx_mini_pending != 0 || rp->rx_jumbo_pending != 0)
  22. return -EINVAL;
  23. guard(wiphy)(local->hw.wiphy);
  24. return drv_set_ringparam(local, rp->tx_pending, rp->rx_pending);
  25. }
  26. static void ieee80211_get_ringparam(struct net_device *dev,
  27. struct ethtool_ringparam *rp,
  28. struct kernel_ethtool_ringparam *kernel_rp,
  29. struct netlink_ext_ack *extack)
  30. {
  31. struct ieee80211_local *local = wiphy_priv(dev->ieee80211_ptr->wiphy);
  32. memset(rp, 0, sizeof(*rp));
  33. guard(wiphy)(local->hw.wiphy);
  34. drv_get_ringparam(local, &rp->tx_pending, &rp->tx_max_pending,
  35. &rp->rx_pending, &rp->rx_max_pending);
  36. }
  37. static const char ieee80211_gstrings_sta_stats[][ETH_GSTRING_LEN] = {
  38. "rx_packets", "rx_bytes",
  39. "rx_duplicates", "rx_fragments", "rx_dropped",
  40. "tx_packets", "tx_bytes",
  41. "tx_filtered", "tx_retry_failed", "tx_retries",
  42. "tx_handlers_drop", "sta_state", "txrate", "rxrate",
  43. "signal", "channel", "noise", "ch_time", "ch_time_busy",
  44. "ch_time_ext_busy", "ch_time_rx", "ch_time_tx"
  45. };
  46. #define STA_STATS_LEN ARRAY_SIZE(ieee80211_gstrings_sta_stats)
  47. static int ieee80211_get_sset_count(struct net_device *dev, int sset)
  48. {
  49. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  50. int rv = 0;
  51. if (sset == ETH_SS_STATS)
  52. rv += STA_STATS_LEN;
  53. rv += drv_get_et_sset_count(sdata, sset);
  54. if (rv == 0)
  55. return -EOPNOTSUPP;
  56. return rv;
  57. }
  58. static void ieee80211_get_stats(struct net_device *dev,
  59. struct ethtool_stats *stats,
  60. u64 *data)
  61. {
  62. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  63. struct ieee80211_chanctx_conf *chanctx_conf;
  64. struct ieee80211_channel *channel;
  65. struct sta_info *sta;
  66. struct ieee80211_local *local = sdata->local;
  67. struct station_info sinfo;
  68. struct survey_info survey;
  69. int i, q;
  70. #define STA_STATS_SURVEY_LEN 7
  71. memset(data, 0, sizeof(u64) * STA_STATS_LEN);
  72. #define ADD_STA_STATS(sta) \
  73. do { \
  74. data[i++] += sinfo.rx_packets; \
  75. data[i++] += sinfo.rx_bytes; \
  76. data[i++] += (sta)->rx_stats.num_duplicates; \
  77. data[i++] += (sta)->rx_stats.fragments; \
  78. data[i++] += sinfo.rx_dropped_misc; \
  79. \
  80. data[i++] += sinfo.tx_packets; \
  81. data[i++] += sinfo.tx_bytes; \
  82. data[i++] += (sta)->status_stats.filtered; \
  83. data[i++] += sinfo.tx_failed; \
  84. data[i++] += sinfo.tx_retries; \
  85. } while (0)
  86. /* For Managed stations, find the single station based on BSSID
  87. * and use that. For interface types, iterate through all available
  88. * stations and add stats for any station that is assigned to this
  89. * network device.
  90. */
  91. guard(wiphy)(local->hw.wiphy);
  92. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  93. sta = sta_info_get_bss(sdata, sdata->deflink.u.mgd.bssid);
  94. if (!(sta && !WARN_ON(sta->sdata->dev != dev)))
  95. goto do_survey;
  96. memset(&sinfo, 0, sizeof(sinfo));
  97. sta_set_sinfo(sta, &sinfo, false);
  98. i = 0;
  99. ADD_STA_STATS(&sta->deflink);
  100. data[i++] = sdata->tx_handlers_drop;
  101. data[i++] = sta->sta_state;
  102. if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))
  103. data[i] = 100000ULL *
  104. cfg80211_calculate_bitrate(&sinfo.txrate);
  105. i++;
  106. if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))
  107. data[i] = 100000ULL *
  108. cfg80211_calculate_bitrate(&sinfo.rxrate);
  109. i++;
  110. if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))
  111. data[i] = (u8)sinfo.signal_avg;
  112. i++;
  113. } else {
  114. list_for_each_entry(sta, &local->sta_list, list) {
  115. /* Make sure this station belongs to the proper dev */
  116. if (sta->sdata->dev != dev)
  117. continue;
  118. memset(&sinfo, 0, sizeof(sinfo));
  119. sta_set_sinfo(sta, &sinfo, false);
  120. i = 0;
  121. ADD_STA_STATS(&sta->deflink);
  122. data[i++] = sdata->tx_handlers_drop;
  123. }
  124. }
  125. do_survey:
  126. i = STA_STATS_LEN - STA_STATS_SURVEY_LEN;
  127. /* Get survey stats for current channel */
  128. survey.filled = 0;
  129. rcu_read_lock();
  130. chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  131. if (chanctx_conf)
  132. channel = chanctx_conf->def.chan;
  133. else if (local->open_count > 0 &&
  134. local->open_count == local->virt_monitors &&
  135. sdata->vif.type == NL80211_IFTYPE_MONITOR)
  136. channel = local->monitor_chanreq.oper.chan;
  137. else
  138. channel = NULL;
  139. rcu_read_unlock();
  140. if (channel) {
  141. q = 0;
  142. do {
  143. survey.filled = 0;
  144. if (drv_get_survey(local, q, &survey) != 0) {
  145. survey.filled = 0;
  146. break;
  147. }
  148. q++;
  149. } while (channel != survey.channel);
  150. }
  151. if (survey.filled)
  152. data[i++] = survey.channel->center_freq;
  153. else
  154. data[i++] = 0;
  155. if (survey.filled & SURVEY_INFO_NOISE_DBM)
  156. data[i++] = (u8)survey.noise;
  157. else
  158. data[i++] = -1LL;
  159. if (survey.filled & SURVEY_INFO_TIME)
  160. data[i++] = survey.time;
  161. else
  162. data[i++] = -1LL;
  163. if (survey.filled & SURVEY_INFO_TIME_BUSY)
  164. data[i++] = survey.time_busy;
  165. else
  166. data[i++] = -1LL;
  167. if (survey.filled & SURVEY_INFO_TIME_EXT_BUSY)
  168. data[i++] = survey.time_ext_busy;
  169. else
  170. data[i++] = -1LL;
  171. if (survey.filled & SURVEY_INFO_TIME_RX)
  172. data[i++] = survey.time_rx;
  173. else
  174. data[i++] = -1LL;
  175. if (survey.filled & SURVEY_INFO_TIME_TX)
  176. data[i++] = survey.time_tx;
  177. else
  178. data[i++] = -1LL;
  179. if (WARN_ON(i != STA_STATS_LEN))
  180. return;
  181. drv_get_et_stats(sdata, stats, &(data[STA_STATS_LEN]));
  182. }
  183. static void ieee80211_get_strings(struct net_device *dev, u32 sset, u8 *data)
  184. {
  185. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  186. int sz_sta_stats = 0;
  187. if (sset == ETH_SS_STATS) {
  188. sz_sta_stats = sizeof(ieee80211_gstrings_sta_stats);
  189. memcpy(data, ieee80211_gstrings_sta_stats, sz_sta_stats);
  190. }
  191. drv_get_et_strings(sdata, sset, &(data[sz_sta_stats]));
  192. }
  193. static int ieee80211_get_regs_len(struct net_device *dev)
  194. {
  195. return 0;
  196. }
  197. static void ieee80211_get_regs(struct net_device *dev,
  198. struct ethtool_regs *regs,
  199. void *data)
  200. {
  201. struct wireless_dev *wdev = dev->ieee80211_ptr;
  202. regs->version = wdev->wiphy->hw_version;
  203. regs->len = 0;
  204. }
  205. const struct ethtool_ops ieee80211_ethtool_ops = {
  206. .get_drvinfo = cfg80211_get_drvinfo,
  207. .get_regs_len = ieee80211_get_regs_len,
  208. .get_regs = ieee80211_get_regs,
  209. .get_link = ethtool_op_get_link,
  210. .get_ringparam = ieee80211_get_ringparam,
  211. .set_ringparam = ieee80211_set_ringparam,
  212. .get_strings = ieee80211_get_strings,
  213. .get_ethtool_stats = ieee80211_get_stats,
  214. .get_sset_count = ieee80211_get_sset_count,
  215. };