ethtool.c 6.4 KB

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