051b225057208bd57f595b2d002aa6cbf0882308
[linux-2.6-block.git] / net / mac80211 / debugfs_sta.c
1 /*
2  * Copyright 2003-2005  Devicescape Software, Inc.
3  * Copyright (c) 2006   Jiri Benc <jbenc@suse.cz>
4  * Copyright 2007       Johannes Berg <johannes@sipsolutions.net>
5  * Copyright 2013-2014  Intel Mobile Communications GmbH
6  * Copyright(c) 2016 Intel Deutschland GmbH
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/debugfs.h>
14 #include <linux/ieee80211.h>
15 #include "ieee80211_i.h"
16 #include "debugfs.h"
17 #include "debugfs_sta.h"
18 #include "sta_info.h"
19 #include "driver-ops.h"
20
21 /* sta attributtes */
22
23 #define STA_READ(name, field, format_string)                            \
24 static ssize_t sta_ ##name## _read(struct file *file,                   \
25                                    char __user *userbuf,                \
26                                    size_t count, loff_t *ppos)          \
27 {                                                                       \
28         struct sta_info *sta = file->private_data;                      \
29         return mac80211_format_buffer(userbuf, count, ppos,             \
30                                       format_string, sta->field);       \
31 }
32 #define STA_READ_D(name, field) STA_READ(name, field, "%d\n")
33
34 #define STA_OPS(name)                                                   \
35 static const struct file_operations sta_ ##name## _ops = {              \
36         .read = sta_##name##_read,                                      \
37         .open = simple_open,                                            \
38         .llseek = generic_file_llseek,                                  \
39 }
40
41 #define STA_OPS_RW(name)                                                \
42 static const struct file_operations sta_ ##name## _ops = {              \
43         .read = sta_##name##_read,                                      \
44         .write = sta_##name##_write,                                    \
45         .open = simple_open,                                            \
46         .llseek = generic_file_llseek,                                  \
47 }
48
49 #define STA_FILE(name, field, format)                                   \
50                 STA_READ_##format(name, field)                          \
51                 STA_OPS(name)
52
53 STA_FILE(aid, sta.aid, D);
54
55 static ssize_t sta_flags_read(struct file *file, char __user *userbuf,
56                               size_t count, loff_t *ppos)
57 {
58         char buf[121];
59         struct sta_info *sta = file->private_data;
60
61 #define TEST(flg) \
62         test_sta_flag(sta, WLAN_STA_##flg) ? #flg "\n" : ""
63
64         int res = scnprintf(buf, sizeof(buf),
65                             "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
66                             TEST(AUTH), TEST(ASSOC), TEST(PS_STA),
67                             TEST(PS_DRIVER), TEST(AUTHORIZED),
68                             TEST(SHORT_PREAMBLE),
69                             sta->sta.wme ? "WME\n" : "",
70                             TEST(WDS), TEST(CLEAR_PS_FILT),
71                             TEST(MFP), TEST(BLOCK_BA), TEST(PSPOLL),
72                             TEST(UAPSD), TEST(SP), TEST(TDLS_PEER),
73                             TEST(TDLS_PEER_AUTH), TEST(TDLS_INITIATOR),
74                             TEST(TDLS_CHAN_SWITCH), TEST(TDLS_OFF_CHANNEL),
75                             TEST(4ADDR_EVENT), TEST(INSERTED),
76                             TEST(RATE_CONTROL), TEST(TOFFSET_KNOWN),
77                             TEST(MPSP_OWNER), TEST(MPSP_RECIPIENT));
78 #undef TEST
79         return simple_read_from_buffer(userbuf, count, ppos, buf, res);
80 }
81 STA_OPS(flags);
82
83 static ssize_t sta_num_ps_buf_frames_read(struct file *file,
84                                           char __user *userbuf,
85                                           size_t count, loff_t *ppos)
86 {
87         struct sta_info *sta = file->private_data;
88         char buf[17*IEEE80211_NUM_ACS], *p = buf;
89         int ac;
90
91         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
92                 p += scnprintf(p, sizeof(buf)+buf-p, "AC%d: %d\n", ac,
93                                skb_queue_len(&sta->ps_tx_buf[ac]) +
94                                skb_queue_len(&sta->tx_filtered[ac]));
95         return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
96 }
97 STA_OPS(num_ps_buf_frames);
98
99 static ssize_t sta_last_seq_ctrl_read(struct file *file, char __user *userbuf,
100                                       size_t count, loff_t *ppos)
101 {
102         char buf[15*IEEE80211_NUM_TIDS], *p = buf;
103         int i;
104         struct sta_info *sta = file->private_data;
105         for (i = 0; i < IEEE80211_NUM_TIDS; i++)
106                 p += scnprintf(p, sizeof(buf)+buf-p, "%x ",
107                                le16_to_cpu(sta->last_seq_ctrl[i]));
108         p += scnprintf(p, sizeof(buf)+buf-p, "\n");
109         return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
110 }
111 STA_OPS(last_seq_ctrl);
112
113 static ssize_t sta_agg_status_read(struct file *file, char __user *userbuf,
114                                         size_t count, loff_t *ppos)
115 {
116         char buf[71 + IEEE80211_NUM_TIDS * 40], *p = buf;
117         int i;
118         struct sta_info *sta = file->private_data;
119         struct tid_ampdu_rx *tid_rx;
120         struct tid_ampdu_tx *tid_tx;
121
122         rcu_read_lock();
123
124         p += scnprintf(p, sizeof(buf) + buf - p, "next dialog_token: %#02x\n",
125                         sta->ampdu_mlme.dialog_token_allocator + 1);
126         p += scnprintf(p, sizeof(buf) + buf - p,
127                        "TID\t\tRX\tDTKN\tSSN\t\tTX\tDTKN\tpending\n");
128
129         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
130                 tid_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[i]);
131                 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[i]);
132
133                 p += scnprintf(p, sizeof(buf) + buf - p, "%02d", i);
134                 p += scnprintf(p, sizeof(buf) + buf - p, "\t\t%x", !!tid_rx);
135                 p += scnprintf(p, sizeof(buf) + buf - p, "\t%#.2x",
136                                 tid_rx ? tid_rx->dialog_token : 0);
137                 p += scnprintf(p, sizeof(buf) + buf - p, "\t%#.3x",
138                                 tid_rx ? tid_rx->ssn : 0);
139
140                 p += scnprintf(p, sizeof(buf) + buf - p, "\t\t%x", !!tid_tx);
141                 p += scnprintf(p, sizeof(buf) + buf - p, "\t%#.2x",
142                                 tid_tx ? tid_tx->dialog_token : 0);
143                 p += scnprintf(p, sizeof(buf) + buf - p, "\t%03d",
144                                 tid_tx ? skb_queue_len(&tid_tx->pending) : 0);
145                 p += scnprintf(p, sizeof(buf) + buf - p, "\n");
146         }
147         rcu_read_unlock();
148
149         return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
150 }
151
152 static ssize_t sta_agg_status_write(struct file *file, const char __user *userbuf,
153                                     size_t count, loff_t *ppos)
154 {
155         char _buf[25] = {}, *buf = _buf;
156         struct sta_info *sta = file->private_data;
157         bool start, tx;
158         unsigned long tid;
159         char *pos;
160         int ret, timeout = 5000;
161
162         if (count > sizeof(_buf))
163                 return -EINVAL;
164
165         if (copy_from_user(buf, userbuf, count))
166                 return -EFAULT;
167
168         buf[sizeof(_buf) - 1] = '\0';
169         pos = buf;
170         buf = strsep(&pos, " ");
171         if (!buf)
172                 return -EINVAL;
173
174         if (!strcmp(buf, "tx"))
175                 tx = true;
176         else if (!strcmp(buf, "rx"))
177                 tx = false;
178         else
179                 return -EINVAL;
180
181         buf = strsep(&pos, " ");
182         if (!buf)
183                 return -EINVAL;
184         if (!strcmp(buf, "start")) {
185                 start = true;
186                 if (!tx)
187                         return -EINVAL;
188         } else if (!strcmp(buf, "stop")) {
189                 start = false;
190         } else {
191                 return -EINVAL;
192         }
193
194         buf = strsep(&pos, " ");
195         if (!buf)
196                 return -EINVAL;
197         if (sscanf(buf, "timeout=%d", &timeout) == 1) {
198                 buf = strsep(&pos, " ");
199                 if (!buf || !tx || !start)
200                         return -EINVAL;
201         }
202
203         ret = kstrtoul(buf, 0, &tid);
204         if (ret || tid >= IEEE80211_NUM_TIDS)
205                 return -EINVAL;
206
207         if (tx) {
208                 if (start)
209                         ret = ieee80211_start_tx_ba_session(&sta->sta, tid,
210                                                             timeout);
211                 else
212                         ret = ieee80211_stop_tx_ba_session(&sta->sta, tid);
213         } else {
214                 __ieee80211_stop_rx_ba_session(sta, tid, WLAN_BACK_RECIPIENT,
215                                                3, true);
216                 ret = 0;
217         }
218
219         return ret ?: count;
220 }
221 STA_OPS_RW(agg_status);
222
223 static ssize_t sta_ht_capa_read(struct file *file, char __user *userbuf,
224                                 size_t count, loff_t *ppos)
225 {
226 #define PRINT_HT_CAP(_cond, _str) \
227         do { \
228         if (_cond) \
229                         p += scnprintf(p, sizeof(buf)+buf-p, "\t" _str "\n"); \
230         } while (0)
231         char buf[512], *p = buf;
232         int i;
233         struct sta_info *sta = file->private_data;
234         struct ieee80211_sta_ht_cap *htc = &sta->sta.ht_cap;
235
236         p += scnprintf(p, sizeof(buf) + buf - p, "ht %ssupported\n",
237                         htc->ht_supported ? "" : "not ");
238         if (htc->ht_supported) {
239                 p += scnprintf(p, sizeof(buf)+buf-p, "cap: %#.4x\n", htc->cap);
240
241                 PRINT_HT_CAP((htc->cap & BIT(0)), "RX LDPC");
242                 PRINT_HT_CAP((htc->cap & BIT(1)), "HT20/HT40");
243                 PRINT_HT_CAP(!(htc->cap & BIT(1)), "HT20");
244
245                 PRINT_HT_CAP(((htc->cap >> 2) & 0x3) == 0, "Static SM Power Save");
246                 PRINT_HT_CAP(((htc->cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
247                 PRINT_HT_CAP(((htc->cap >> 2) & 0x3) == 3, "SM Power Save disabled");
248
249                 PRINT_HT_CAP((htc->cap & BIT(4)), "RX Greenfield");
250                 PRINT_HT_CAP((htc->cap & BIT(5)), "RX HT20 SGI");
251                 PRINT_HT_CAP((htc->cap & BIT(6)), "RX HT40 SGI");
252                 PRINT_HT_CAP((htc->cap & BIT(7)), "TX STBC");
253
254                 PRINT_HT_CAP(((htc->cap >> 8) & 0x3) == 0, "No RX STBC");
255                 PRINT_HT_CAP(((htc->cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
256                 PRINT_HT_CAP(((htc->cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
257                 PRINT_HT_CAP(((htc->cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
258
259                 PRINT_HT_CAP((htc->cap & BIT(10)), "HT Delayed Block Ack");
260
261                 PRINT_HT_CAP(!(htc->cap & BIT(11)), "Max AMSDU length: "
262                              "3839 bytes");
263                 PRINT_HT_CAP((htc->cap & BIT(11)), "Max AMSDU length: "
264                              "7935 bytes");
265
266                 /*
267                  * For beacons and probe response this would mean the BSS
268                  * does or does not allow the usage of DSSS/CCK HT40.
269                  * Otherwise it means the STA does or does not use
270                  * DSSS/CCK HT40.
271                  */
272                 PRINT_HT_CAP((htc->cap & BIT(12)), "DSSS/CCK HT40");
273                 PRINT_HT_CAP(!(htc->cap & BIT(12)), "No DSSS/CCK HT40");
274
275                 /* BIT(13) is reserved */
276
277                 PRINT_HT_CAP((htc->cap & BIT(14)), "40 MHz Intolerant");
278
279                 PRINT_HT_CAP((htc->cap & BIT(15)), "L-SIG TXOP protection");
280
281                 p += scnprintf(p, sizeof(buf)+buf-p, "ampdu factor/density: %d/%d\n",
282                                 htc->ampdu_factor, htc->ampdu_density);
283                 p += scnprintf(p, sizeof(buf)+buf-p, "MCS mask:");
284
285                 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
286                         p += scnprintf(p, sizeof(buf)+buf-p, " %.2x",
287                                         htc->mcs.rx_mask[i]);
288                 p += scnprintf(p, sizeof(buf)+buf-p, "\n");
289
290                 /* If not set this is meaningless */
291                 if (le16_to_cpu(htc->mcs.rx_highest)) {
292                         p += scnprintf(p, sizeof(buf)+buf-p,
293                                        "MCS rx highest: %d Mbps\n",
294                                        le16_to_cpu(htc->mcs.rx_highest));
295                 }
296
297                 p += scnprintf(p, sizeof(buf)+buf-p, "MCS tx params: %x\n",
298                                 htc->mcs.tx_params);
299         }
300
301         return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
302 }
303 STA_OPS(ht_capa);
304
305 static ssize_t sta_vht_capa_read(struct file *file, char __user *userbuf,
306                                  size_t count, loff_t *ppos)
307 {
308         char buf[128], *p = buf;
309         struct sta_info *sta = file->private_data;
310         struct ieee80211_sta_vht_cap *vhtc = &sta->sta.vht_cap;
311
312         p += scnprintf(p, sizeof(buf) + buf - p, "VHT %ssupported\n",
313                         vhtc->vht_supported ? "" : "not ");
314         if (vhtc->vht_supported) {
315                 p += scnprintf(p, sizeof(buf)+buf-p, "cap: %#.8x\n", vhtc->cap);
316
317                 p += scnprintf(p, sizeof(buf)+buf-p, "RX MCS: %.4x\n",
318                                le16_to_cpu(vhtc->vht_mcs.rx_mcs_map));
319                 if (vhtc->vht_mcs.rx_highest)
320                         p += scnprintf(p, sizeof(buf)+buf-p,
321                                        "MCS RX highest: %d Mbps\n",
322                                        le16_to_cpu(vhtc->vht_mcs.rx_highest));
323                 p += scnprintf(p, sizeof(buf)+buf-p, "TX MCS: %.4x\n",
324                                le16_to_cpu(vhtc->vht_mcs.tx_mcs_map));
325                 if (vhtc->vht_mcs.tx_highest)
326                         p += scnprintf(p, sizeof(buf)+buf-p,
327                                        "MCS TX highest: %d Mbps\n",
328                                        le16_to_cpu(vhtc->vht_mcs.tx_highest));
329         }
330
331         return simple_read_from_buffer(userbuf, count, ppos, buf, p - buf);
332 }
333 STA_OPS(vht_capa);
334
335
336 #define DEBUGFS_ADD(name) \
337         debugfs_create_file(#name, 0400, \
338                 sta->debugfs.dir, sta, &sta_ ##name## _ops);
339
340 #define DEBUGFS_ADD_COUNTER(name, field)                                \
341         if (sizeof(sta->field) == sizeof(u32))                          \
342                 debugfs_create_u32(#name, 0400, sta->debugfs.dir,       \
343                         (u32 *) &sta->field);                           \
344         else                                                            \
345                 debugfs_create_u64(#name, 0400, sta->debugfs.dir,       \
346                         (u64 *) &sta->field);
347
348 void ieee80211_sta_debugfs_add(struct sta_info *sta)
349 {
350         struct ieee80211_local *local = sta->local;
351         struct ieee80211_sub_if_data *sdata = sta->sdata;
352         struct dentry *stations_dir = sta->sdata->debugfs.subdir_stations;
353         u8 mac[3*ETH_ALEN];
354
355         if (!stations_dir)
356                 return;
357
358         snprintf(mac, sizeof(mac), "%pM", sta->sta.addr);
359
360         /*
361          * This might fail due to a race condition:
362          * When mac80211 unlinks a station, the debugfs entries
363          * remain, but it is already possible to link a new
364          * station with the same address which triggers adding
365          * it to debugfs; therefore, if the old station isn't
366          * destroyed quickly enough the old station's debugfs
367          * dir might still be around.
368          */
369         sta->debugfs.dir = debugfs_create_dir(mac, stations_dir);
370         if (!sta->debugfs.dir)
371                 return;
372
373         DEBUGFS_ADD(flags);
374         DEBUGFS_ADD(num_ps_buf_frames);
375         DEBUGFS_ADD(last_seq_ctrl);
376         DEBUGFS_ADD(agg_status);
377         DEBUGFS_ADD(ht_capa);
378         DEBUGFS_ADD(vht_capa);
379
380         DEBUGFS_ADD_COUNTER(rx_duplicates, rx_stats.num_duplicates);
381         DEBUGFS_ADD_COUNTER(rx_fragments, rx_stats.fragments);
382         DEBUGFS_ADD_COUNTER(tx_filtered, status_stats.filtered);
383
384         if (sizeof(sta->driver_buffered_tids) == sizeof(u32))
385                 debugfs_create_x32("driver_buffered_tids", 0400,
386                                    sta->debugfs.dir,
387                                    (u32 *)&sta->driver_buffered_tids);
388         else
389                 debugfs_create_x64("driver_buffered_tids", 0400,
390                                    sta->debugfs.dir,
391                                    (u64 *)&sta->driver_buffered_tids);
392
393         drv_sta_add_debugfs(local, sdata, &sta->sta, sta->debugfs.dir);
394 }
395
396 void ieee80211_sta_debugfs_remove(struct sta_info *sta)
397 {
398         struct ieee80211_local *local = sta->local;
399         struct ieee80211_sub_if_data *sdata = sta->sdata;
400
401         drv_sta_remove_debugfs(local, sdata, &sta->sta, sta->debugfs.dir);
402         debugfs_remove_recursive(sta->debugfs.dir);
403         sta->debugfs.dir = NULL;
404 }