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 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)
19 struct ieee80211_local *local = wiphy_priv(dev->ieee80211_ptr->wiphy);
21 if (rp->rx_mini_pending != 0 || rp->rx_jumbo_pending != 0)
24 return drv_set_ringparam(local, rp->tx_pending, rp->rx_pending);
27 static void ieee80211_get_ringparam(struct net_device *dev,
28 struct ethtool_ringparam *rp)
30 struct ieee80211_local *local = wiphy_priv(dev->ieee80211_ptr->wiphy);
32 memset(rp, 0, sizeof(*rp));
34 drv_get_ringparam(local, &rp->tx_pending, &rp->tx_max_pending,
35 &rp->rx_pending, &rp->rx_max_pending);
38 static const char ieee80211_gstrings_sta_stats[][ETH_GSTRING_LEN] = {
39 "rx_packets", "rx_bytes",
40 "rx_duplicates", "rx_fragments", "rx_dropped",
41 "tx_packets", "tx_bytes",
42 "tx_filtered", "tx_retry_failed", "tx_retries",
43 "sta_state", "txrate", "rxrate", "signal",
44 "channel", "noise", "ch_time", "ch_time_busy",
45 "ch_time_ext_busy", "ch_time_rx", "ch_time_tx"
47 #define STA_STATS_LEN ARRAY_SIZE(ieee80211_gstrings_sta_stats)
49 static int ieee80211_get_sset_count(struct net_device *dev, int sset)
51 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
54 if (sset == ETH_SS_STATS)
57 rv += drv_get_et_sset_count(sdata, sset);
64 static void ieee80211_get_stats(struct net_device *dev,
65 struct ethtool_stats *stats,
68 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
69 struct ieee80211_chanctx_conf *chanctx_conf;
70 struct ieee80211_channel *channel;
72 struct ieee80211_local *local = sdata->local;
73 struct station_info sinfo;
74 struct survey_info survey;
76 #define STA_STATS_SURVEY_LEN 7
78 memset(data, 0, sizeof(u64) * STA_STATS_LEN);
80 #define ADD_STA_STATS(sta) \
82 data[i++] += sta->rx_stats.packets; \
83 data[i++] += sta->rx_stats.bytes; \
84 data[i++] += sta->rx_stats.num_duplicates; \
85 data[i++] += sta->rx_stats.fragments; \
86 data[i++] += sta->rx_stats.dropped; \
88 data[i++] += sinfo.tx_packets; \
89 data[i++] += sinfo.tx_bytes; \
90 data[i++] += sta->status_stats.filtered; \
91 data[i++] += sta->status_stats.retry_failed; \
92 data[i++] += sta->status_stats.retry_count; \
95 /* For Managed stations, find the single station based on BSSID
96 * and use that. For interface types, iterate through all available
97 * stations and add stats for any station that is assigned to this
101 mutex_lock(&local->sta_mtx);
103 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
104 sta = sta_info_get_bss(sdata, sdata->u.mgd.bssid);
106 if (!(sta && !WARN_ON(sta->sdata->dev != dev)))
109 memset(&sinfo, 0, sizeof(sinfo));
110 sta_set_sinfo(sta, &sinfo, false);
115 data[i++] = sta->sta_state;
118 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))
119 data[i] = 100000ULL *
120 cfg80211_calculate_bitrate(&sinfo.txrate);
122 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))
123 data[i] = 100000ULL *
124 cfg80211_calculate_bitrate(&sinfo.rxrate);
127 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))
128 data[i] = (u8)sinfo.signal_avg;
131 list_for_each_entry(sta, &local->sta_list, list) {
132 /* Make sure this station belongs to the proper dev */
133 if (sta->sdata->dev != dev)
136 memset(&sinfo, 0, sizeof(sinfo));
137 sta_set_sinfo(sta, &sinfo, false);
144 i = STA_STATS_LEN - STA_STATS_SURVEY_LEN;
145 /* Get survey stats for current channel */
149 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
151 channel = chanctx_conf->def.chan;
160 if (drv_get_survey(local, q, &survey) != 0) {
165 } while (channel != survey.channel);
169 data[i++] = survey.channel->center_freq;
172 if (survey.filled & SURVEY_INFO_NOISE_DBM)
173 data[i++] = (u8)survey.noise;
176 if (survey.filled & SURVEY_INFO_TIME)
177 data[i++] = survey.time;
180 if (survey.filled & SURVEY_INFO_TIME_BUSY)
181 data[i++] = survey.time_busy;
184 if (survey.filled & SURVEY_INFO_TIME_EXT_BUSY)
185 data[i++] = survey.time_ext_busy;
188 if (survey.filled & SURVEY_INFO_TIME_RX)
189 data[i++] = survey.time_rx;
192 if (survey.filled & SURVEY_INFO_TIME_TX)
193 data[i++] = survey.time_tx;
197 mutex_unlock(&local->sta_mtx);
199 if (WARN_ON(i != STA_STATS_LEN))
202 drv_get_et_stats(sdata, stats, &(data[STA_STATS_LEN]));
205 static void ieee80211_get_strings(struct net_device *dev, u32 sset, u8 *data)
207 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
208 int sz_sta_stats = 0;
210 if (sset == ETH_SS_STATS) {
211 sz_sta_stats = sizeof(ieee80211_gstrings_sta_stats);
212 memcpy(data, ieee80211_gstrings_sta_stats, sz_sta_stats);
214 drv_get_et_strings(sdata, sset, &(data[sz_sta_stats]));
217 static int ieee80211_get_regs_len(struct net_device *dev)
222 static void ieee80211_get_regs(struct net_device *dev,
223 struct ethtool_regs *regs,
226 struct wireless_dev *wdev = dev->ieee80211_ptr;
228 regs->version = wdev->wiphy->hw_version;
232 const struct ethtool_ops ieee80211_ethtool_ops = {
233 .get_drvinfo = cfg80211_get_drvinfo,
234 .get_regs_len = ieee80211_get_regs_len,
235 .get_regs = ieee80211_get_regs,
236 .get_link = ethtool_op_get_link,
237 .get_ringparam = ieee80211_get_ringparam,
238 .set_ringparam = ieee80211_set_ringparam,
239 .get_strings = ieee80211_get_strings,
240 .get_ethtool_stats = ieee80211_get_stats,
241 .get_sset_count = ieee80211_get_sset_count,