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