xref: /linux/net/mac80211/ethtool.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
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 	"tx_handlers_drop", "sta_state", "txrate", "rxrate",
52 	"signal", "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++] = sdata->tx_handlers_drop;
124 		data[i++] = sta->sta_state;
125 
126 
127 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))
128 			data[i] = 100000ULL *
129 				cfg80211_calculate_bitrate(&sinfo.txrate);
130 		i++;
131 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))
132 			data[i] = 100000ULL *
133 				cfg80211_calculate_bitrate(&sinfo.rxrate);
134 		i++;
135 
136 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))
137 			data[i] = (u8)sinfo.signal_avg;
138 		i++;
139 	} else {
140 		list_for_each_entry(sta, &local->sta_list, list) {
141 			/* Make sure this station belongs to the proper dev */
142 			if (sta->sdata->dev != dev)
143 				continue;
144 
145 			memset(&sinfo, 0, sizeof(sinfo));
146 			sta_set_sinfo(sta, &sinfo, false);
147 			i = 0;
148 			ADD_STA_STATS(&sta->deflink);
149 			data[i++] = sdata->tx_handlers_drop;
150 		}
151 	}
152 
153 do_survey:
154 	i = STA_STATS_LEN - STA_STATS_SURVEY_LEN;
155 	/* Get survey stats for current channel */
156 	survey.filled = 0;
157 
158 	rcu_read_lock();
159 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
160 	if (chanctx_conf)
161 		channel = chanctx_conf->def.chan;
162 	else if (local->open_count > 0 &&
163 		 local->open_count == local->virt_monitors &&
164 		 sdata->vif.type == NL80211_IFTYPE_MONITOR)
165 		channel = local->monitor_chanreq.oper.chan;
166 	else
167 		channel = NULL;
168 	rcu_read_unlock();
169 
170 	if (channel) {
171 		q = 0;
172 		do {
173 			survey.filled = 0;
174 			if (drv_get_survey(local, q, &survey) != 0) {
175 				survey.filled = 0;
176 				break;
177 			}
178 			q++;
179 		} while (channel != survey.channel);
180 	}
181 
182 	if (survey.filled)
183 		data[i++] = survey.channel->center_freq;
184 	else
185 		data[i++] = 0;
186 	if (survey.filled & SURVEY_INFO_NOISE_DBM)
187 		data[i++] = (u8)survey.noise;
188 	else
189 		data[i++] = -1LL;
190 	if (survey.filled & SURVEY_INFO_TIME)
191 		data[i++] = survey.time;
192 	else
193 		data[i++] = -1LL;
194 	if (survey.filled & SURVEY_INFO_TIME_BUSY)
195 		data[i++] = survey.time_busy;
196 	else
197 		data[i++] = -1LL;
198 	if (survey.filled & SURVEY_INFO_TIME_EXT_BUSY)
199 		data[i++] = survey.time_ext_busy;
200 	else
201 		data[i++] = -1LL;
202 	if (survey.filled & SURVEY_INFO_TIME_RX)
203 		data[i++] = survey.time_rx;
204 	else
205 		data[i++] = -1LL;
206 	if (survey.filled & SURVEY_INFO_TIME_TX)
207 		data[i++] = survey.time_tx;
208 	else
209 		data[i++] = -1LL;
210 
211 	if (WARN_ON(i != STA_STATS_LEN))
212 		return;
213 
214 	drv_get_et_stats(sdata, stats, &(data[STA_STATS_LEN]));
215 }
216 
217 static void ieee80211_get_strings(struct net_device *dev, u32 sset, u8 *data)
218 {
219 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
220 	int sz_sta_stats = 0;
221 
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);
225 	}
226 	drv_get_et_strings(sdata, sset, &(data[sz_sta_stats]));
227 }
228 
229 static int ieee80211_get_regs_len(struct net_device *dev)
230 {
231 	return 0;
232 }
233 
234 static void ieee80211_get_regs(struct net_device *dev,
235 			       struct ethtool_regs *regs,
236 			       void *data)
237 {
238 	struct wireless_dev *wdev = dev->ieee80211_ptr;
239 
240 	regs->version = wdev->wiphy->hw_version;
241 	regs->len = 0;
242 }
243 
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,
254 };
255