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
16static 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(wiphy: dev->ieee80211_ptr->wiphy);
22
23 if (rp->rx_mini_pending != 0 || rp->rx_jumbo_pending != 0)
24 return -EINVAL;
25
26 guard(wiphy)(T: local->hw.wiphy);
27
28 return drv_set_ringparam(local, tx: rp->tx_pending, rx: rp->rx_pending);
29}
30
31static 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(wiphy: dev->ieee80211_ptr->wiphy);
37
38 memset(s: rp, c: 0, n: sizeof(*rp));
39
40 guard(wiphy)(T: local->hw.wiphy);
41
42 drv_get_ringparam(local, tx: &rp->tx_pending, tx_max: &rp->tx_max_pending,
43 rx: &rp->rx_pending, rx_max: &rp->rx_max_pending);
44}
45
46static 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
57static 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
72static 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(s: data, c: 0, n: 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)(T: local->hw.wiphy);
110
111 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
112 sta = sta_info_get_bss(sdata, addr: sdata->deflink.u.mgd.bssid);
113
114 if (!(sta && !WARN_ON(sta->sdata->dev != dev)))
115 goto do_survey;
116
117 memset(s: &sinfo, c: 0, n: sizeof(sinfo));
118 sta_set_sinfo(sta, sinfo: &sinfo, tidstats: 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(rate: &sinfo.txrate);
130 i++;
131 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))
132 data[i] = 100000ULL *
133 cfg80211_calculate_bitrate(rate: &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(s: &sinfo, c: 0, n: sizeof(sinfo));
146 sta_set_sinfo(sta, sinfo: &sinfo, tidstats: false);
147 i = 0;
148 ADD_STA_STATS(&sta->deflink);
149 data[i++] = sdata->tx_handlers_drop;
150 }
151 }
152
153do_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, idx: q, survey: &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: &(data[STA_STATS_LEN]));
215}
216
217static 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(to: data, from: ieee80211_gstrings_sta_stats, len: sz_sta_stats);
225 }
226 drv_get_et_strings(sdata, sset, data: &(data[sz_sta_stats]));
227}
228
229static int ieee80211_get_regs_len(struct net_device *dev)
230{
231 return 0;
232}
233
234static 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
244const 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