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