1 // SPDX-License-Identifier: GPL-2.0-only
3 * mac80211 ethtool hooks for cfg80211
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
10 #include <linux/types.h>
11 #include <net/cfg80211.h>
12 #include "ieee80211_i.h"
14 #include "driver-ops.h"
16 static int ieee80211_set_ringparam(struct net_device *dev,
17 struct ethtool_ringparam *rp,
18 struct kernel_ethtool_ringparam *kernel_rp,
19 struct netlink_ext_ack *extack)
21 struct ieee80211_local *local = wiphy_priv(dev->ieee80211_ptr->wiphy);
24 if (rp->rx_mini_pending != 0 || rp->rx_jumbo_pending != 0)
27 wiphy_lock(local->hw.wiphy);
28 ret = drv_set_ringparam(local, rp->tx_pending, rp->rx_pending);
29 wiphy_unlock(local->hw.wiphy);
34 static void ieee80211_get_ringparam(struct net_device *dev,
35 struct ethtool_ringparam *rp,
36 struct kernel_ethtool_ringparam *kernel_rp,
37 struct netlink_ext_ack *extack)
39 struct ieee80211_local *local = wiphy_priv(dev->ieee80211_ptr->wiphy);
41 memset(rp, 0, sizeof(*rp));
43 wiphy_lock(local->hw.wiphy);
44 drv_get_ringparam(local, &rp->tx_pending, &rp->tx_max_pending,
45 &rp->rx_pending, &rp->rx_max_pending);
46 wiphy_unlock(local->hw.wiphy);
49 static const char ieee80211_gstrings_sta_stats[][ETH_GSTRING_LEN] = {
50 "rx_packets", "rx_bytes",
51 "rx_duplicates", "rx_fragments", "rx_dropped",
52 "tx_packets", "tx_bytes",
53 "tx_filtered", "tx_retry_failed", "tx_retries",
54 "sta_state", "txrate", "rxrate", "signal",
55 "channel", "noise", "ch_time", "ch_time_busy",
56 "ch_time_ext_busy", "ch_time_rx", "ch_time_tx"
58 #define STA_STATS_LEN ARRAY_SIZE(ieee80211_gstrings_sta_stats)
60 static int ieee80211_get_sset_count(struct net_device *dev, int sset)
62 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
65 if (sset == ETH_SS_STATS)
68 rv += drv_get_et_sset_count(sdata, sset);
75 static void ieee80211_get_stats(struct net_device *dev,
76 struct ethtool_stats *stats,
79 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
80 struct ieee80211_chanctx_conf *chanctx_conf;
81 struct ieee80211_channel *channel;
83 struct ieee80211_local *local = sdata->local;
84 struct station_info sinfo;
85 struct survey_info survey;
87 #define STA_STATS_SURVEY_LEN 7
89 memset(data, 0, sizeof(u64) * STA_STATS_LEN);
91 #define ADD_STA_STATS(sta) \
93 data[i++] += sinfo.rx_packets; \
94 data[i++] += sinfo.rx_bytes; \
95 data[i++] += (sta)->rx_stats.num_duplicates; \
96 data[i++] += (sta)->rx_stats.fragments; \
97 data[i++] += sinfo.rx_dropped_misc; \
99 data[i++] += sinfo.tx_packets; \
100 data[i++] += sinfo.tx_bytes; \
101 data[i++] += (sta)->status_stats.filtered; \
102 data[i++] += sinfo.tx_failed; \
103 data[i++] += sinfo.tx_retries; \
106 /* For Managed stations, find the single station based on BSSID
107 * and use that. For interface types, iterate through all available
108 * stations and add stats for any station that is assigned to this
112 wiphy_lock(local->hw.wiphy);
114 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
115 sta = sta_info_get_bss(sdata, sdata->deflink.u.mgd.bssid);
117 if (!(sta && !WARN_ON(sta->sdata->dev != dev)))
120 memset(&sinfo, 0, sizeof(sinfo));
121 sta_set_sinfo(sta, &sinfo, false);
124 ADD_STA_STATS(&sta->deflink);
126 data[i++] = sta->sta_state;
129 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))
130 data[i] = 100000ULL *
131 cfg80211_calculate_bitrate(&sinfo.txrate);
133 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))
134 data[i] = 100000ULL *
135 cfg80211_calculate_bitrate(&sinfo.rxrate);
138 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))
139 data[i] = (u8)sinfo.signal_avg;
142 list_for_each_entry(sta, &local->sta_list, list) {
143 /* Make sure this station belongs to the proper dev */
144 if (sta->sdata->dev != dev)
147 memset(&sinfo, 0, sizeof(sinfo));
148 sta_set_sinfo(sta, &sinfo, false);
150 ADD_STA_STATS(&sta->deflink);
155 i = STA_STATS_LEN - STA_STATS_SURVEY_LEN;
156 /* Get survey stats for current channel */
160 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
162 channel = chanctx_conf->def.chan;
171 if (drv_get_survey(local, q, &survey) != 0) {
176 } while (channel != survey.channel);
180 data[i++] = survey.channel->center_freq;
183 if (survey.filled & SURVEY_INFO_NOISE_DBM)
184 data[i++] = (u8)survey.noise;
187 if (survey.filled & SURVEY_INFO_TIME)
188 data[i++] = survey.time;
191 if (survey.filled & SURVEY_INFO_TIME_BUSY)
192 data[i++] = survey.time_busy;
195 if (survey.filled & SURVEY_INFO_TIME_EXT_BUSY)
196 data[i++] = survey.time_ext_busy;
199 if (survey.filled & SURVEY_INFO_TIME_RX)
200 data[i++] = survey.time_rx;
203 if (survey.filled & SURVEY_INFO_TIME_TX)
204 data[i++] = survey.time_tx;
208 if (WARN_ON(i != STA_STATS_LEN)) {
209 wiphy_unlock(local->hw.wiphy);
213 drv_get_et_stats(sdata, stats, &(data[STA_STATS_LEN]));
214 wiphy_unlock(local->hw.wiphy);
217 static void ieee80211_get_strings(struct net_device *dev, u32 sset, u8 *data)
219 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
220 int sz_sta_stats = 0;
222 if (sset == ETH_SS_STATS) {
223 sz_sta_stats = sizeof(ieee80211_gstrings_sta_stats);
224 memcpy(data, ieee80211_gstrings_sta_stats, sz_sta_stats);
226 drv_get_et_strings(sdata, sset, &(data[sz_sta_stats]));
229 static int ieee80211_get_regs_len(struct net_device *dev)
234 static void ieee80211_get_regs(struct net_device *dev,
235 struct ethtool_regs *regs,
238 struct wireless_dev *wdev = dev->ieee80211_ptr;
240 regs->version = wdev->wiphy->hw_version;
244 const struct ethtool_ops ieee80211_ethtool_ops = {
245 .get_drvinfo = cfg80211_get_drvinfo,
246 .get_regs_len = ieee80211_get_regs_len,
247 .get_regs = ieee80211_get_regs,
248 .get_link = ethtool_op_get_link,
249 .get_ringparam = ieee80211_get_ringparam,
250 .set_ringparam = ieee80211_set_ringparam,
251 .get_strings = ieee80211_get_strings,
252 .get_ethtool_stats = ieee80211_get_stats,
253 .get_sset_count = ieee80211_get_sset_count,