New: mac80211 stack from the wireless-dev tree
[openwrt.git] / package / mac80211 / src / mac80211 / debugfs.c
1 /*
2  * mac80211 debugfs for wireless PHYs
3  *
4  * Copyright 2007       Johannes Berg <johannes@sipsolutions.net>
5  *
6  * GPLv2
7  *
8  */
9
10 #include <linux/debugfs.h>
11 #include <linux/rtnetlink.h>
12 #include "ieee80211_i.h"
13 #include "ieee80211_rate.h"
14 #include "debugfs.h"
15
16 static inline int rtnl_lock_local(struct ieee80211_local *local)
17 {
18         rtnl_lock();
19         if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED)) {
20                 rtnl_unlock();
21                 return -ENODEV;
22         }
23         return 0;
24 }
25
26 int mac80211_open_file_generic(struct inode *inode, struct file *file)
27 {
28         file->private_data = inode->i_private;
29         return 0;
30 }
31
32 static const char *ieee80211_mode_str(int mode)
33 {
34         switch (mode) {
35         case MODE_IEEE80211A:
36                 return "IEEE 802.11a";
37         case MODE_IEEE80211B:
38                 return "IEEE 802.11b";
39         case MODE_IEEE80211G:
40                 return "IEEE 802.11g";
41         case MODE_ATHEROS_TURBO:
42                 return "Atheros Turbo (5 GHz)";
43         default:
44                 return "UNKNOWN";
45         }
46 }
47
48 static ssize_t modes_read(struct file *file, char __user *userbuf,
49                           size_t count, loff_t *ppos)
50 {
51         struct ieee80211_local *local = file->private_data;
52         struct ieee80211_hw_mode *mode;
53         char buf[150], *p = buf;
54
55         /* FIXME: locking! */
56         list_for_each_entry(mode, &local->modes_list, list) {
57                 p += scnprintf(p, sizeof(buf)+buf-p,
58                                "%s\n", ieee80211_mode_str(mode->mode));
59         }
60
61         return simple_read_from_buffer(userbuf, count, ppos, buf, p-buf);
62 }
63
64 static const struct file_operations modes_ops = {
65         .read = modes_read,
66         .open = mac80211_open_file_generic,
67 };
68
69 #define DEBUGFS_READ(name, buflen, fmt, value...)                       \
70 static ssize_t name## _read(struct file *file, char __user *userbuf,    \
71                             size_t count, loff_t *ppos)                 \
72 {                                                                       \
73         struct ieee80211_local *local = file->private_data;             \
74         char buf[buflen];                                               \
75         int res;                                                        \
76                                                                         \
77         res = scnprintf(buf, buflen, fmt "\n", ##value);                \
78         return simple_read_from_buffer(userbuf, count, ppos, buf, res); \
79 }                                                                       \
80
81 #define DEBUGFS_READONLY_FILE(name, buflen, fmt, value...)              \
82 DEBUGFS_READ(name, buflen, fmt, ## value)                               \
83 static const struct file_operations name## _ops = {                     \
84         .read = name## _read,                                           \
85         .open = mac80211_open_file_generic,                             \
86 };
87
88 #define DEBUGFS_ADD_MODE(name, mode)                                    \
89         local->debugfs.name = debugfs_create_file(#name, mode, phyd,    \
90                                                   local, &name## _ops);
91
92 #define DEBUGFS_ADD(name)       DEBUGFS_ADD_MODE(name, 0444)
93
94 #define DEBUGFS_DEL(name)                                               \
95         debugfs_remove(local->debugfs.name);                            \
96         local->debugfs.name = NULL;
97
98
99 DEBUGFS_READONLY_FILE(channel, 20, "%d",
100                       local->hw.conf.channel);
101 DEBUGFS_READONLY_FILE(frequency, 20, "%d",
102                       local->hw.conf.freq);
103 DEBUGFS_READONLY_FILE(radar_detect, 20, "%d",
104                       local->hw.conf.radar_detect);
105 DEBUGFS_READONLY_FILE(antenna_sel_tx, 20, "%d",
106                       local->hw.conf.antenna_sel_tx);
107 DEBUGFS_READONLY_FILE(antenna_sel_rx, 20, "%d",
108                       local->hw.conf.antenna_sel_rx);
109 DEBUGFS_READONLY_FILE(bridge_packets, 20, "%d",
110                       local->bridge_packets);
111 DEBUGFS_READONLY_FILE(key_tx_rx_threshold, 20, "%d",
112                       local->key_tx_rx_threshold);
113 DEBUGFS_READONLY_FILE(rts_threshold, 20, "%d",
114                       local->rts_threshold);
115 DEBUGFS_READONLY_FILE(fragmentation_threshold, 20, "%d",
116                       local->fragmentation_threshold);
117 DEBUGFS_READONLY_FILE(short_retry_limit, 20, "%d",
118                       local->short_retry_limit);
119 DEBUGFS_READONLY_FILE(long_retry_limit, 20, "%d",
120                       local->long_retry_limit);
121 DEBUGFS_READONLY_FILE(total_ps_buffered, 20, "%d",
122                       local->total_ps_buffered);
123 DEBUGFS_READONLY_FILE(mode, 20, "%s",
124                       ieee80211_mode_str(local->hw.conf.phymode));
125 DEBUGFS_READONLY_FILE(wep_iv, 20, "%#06x",
126                       local->wep_iv & 0xffffff);
127 DEBUGFS_READONLY_FILE(tx_power_reduction, 20, "%d.%d dBm",
128                       local->hw.conf.tx_power_reduction / 10,
129                       local->hw.conf.tx_power_reduction & 10);
130
131 DEBUGFS_READ(rate_ctrl_alg, 100, "%s",
132              local->rate_ctrl ? local->rate_ctrl->ops->name : "<unset>");
133
134 static ssize_t rate_ctrl_alg_write(struct file *file, const char __user *userbuf,
135                                    size_t count, loff_t *ppos)
136 {
137         struct ieee80211_local *local = file->private_data;
138         char buf[64];
139         ssize_t buf_size;
140         int res;
141
142         buf_size = min(count, ARRAY_SIZE(buf) - 1);
143         if (copy_from_user(buf, userbuf, buf_size))
144                 return -EFAULT;
145         buf[buf_size] = '\0';
146         res = rtnl_lock_local(local);
147         if (res)
148                 return res;
149         res = ieee80211_init_rate_ctrl_alg(local, buf);
150         rtnl_unlock();
151         return res < 0 ? res : buf_size;
152 }
153
154 static const struct file_operations rate_ctrl_alg_ops = {
155         .read = rate_ctrl_alg_read,
156         .write = rate_ctrl_alg_write,
157         .open = mac80211_open_file_generic,
158 };
159
160 /* statistics stuff */
161
162 #define DEBUGFS_STATS_FILE(name, buflen, fmt, value...)                 \
163         DEBUGFS_READONLY_FILE(stats_ ##name, buflen, fmt, ##value)
164
165 static ssize_t format_devstat_counter(struct ieee80211_local *local,
166         char __user *userbuf,
167         size_t count, loff_t *ppos,
168         int (*printvalue)(struct ieee80211_low_level_stats *stats, char *buf,
169                           int buflen))
170 {
171         struct ieee80211_low_level_stats stats;
172         char buf[20];
173         int res;
174
175         if (!local->ops->get_stats)
176                 return -EOPNOTSUPP;
177
178         res = rtnl_lock_local(local);
179         if (res)
180                 return res;
181
182         res = local->ops->get_stats(local_to_hw(local), &stats);
183         rtnl_unlock();
184         if (!res)
185                 res = printvalue(&stats, buf, sizeof(buf));
186         return simple_read_from_buffer(userbuf, count, ppos, buf, res);
187 }
188
189 #define DEBUGFS_DEVSTATS_FILE(name)                                     \
190 static int print_devstats_##name(struct ieee80211_low_level_stats *stats,\
191                                  char *buf, int buflen)                 \
192 {                                                                       \
193         return scnprintf(buf, buflen, "%u\n", stats->name);             \
194 }                                                                       \
195 static ssize_t stats_ ##name## _read(struct file *file,                 \
196                                      char __user *userbuf,              \
197                                      size_t count, loff_t *ppos)        \
198 {                                                                       \
199         return format_devstat_counter(file->private_data,               \
200                                       userbuf,                          \
201                                       count,                            \
202                                       ppos,                             \
203                                       print_devstats_##name);           \
204 }                                                                       \
205                                                                         \
206 static const struct file_operations stats_ ##name## _ops = {            \
207         .read = stats_ ##name## _read,                                  \
208         .open = mac80211_open_file_generic,                             \
209 };
210
211 #define DEBUGFS_STATS_ADD(name)                                         \
212         local->debugfs.stats.name = debugfs_create_file(#name, 0444, statsd,\
213                 local, &stats_ ##name## _ops);
214
215 #define DEBUGFS_STATS_DEL(name)                                         \
216         debugfs_remove(local->debugfs.stats.name);                      \
217         local->debugfs.stats.name = NULL;
218
219 DEBUGFS_STATS_FILE(transmitted_fragment_count, 20, "%u",
220                    local->dot11TransmittedFragmentCount);
221 DEBUGFS_STATS_FILE(multicast_transmitted_frame_count, 20, "%u",
222                    local->dot11MulticastTransmittedFrameCount);
223 DEBUGFS_STATS_FILE(failed_count, 20, "%u",
224                    local->dot11FailedCount);
225 DEBUGFS_STATS_FILE(retry_count, 20, "%u",
226                    local->dot11RetryCount);
227 DEBUGFS_STATS_FILE(multiple_retry_count, 20, "%u",
228                    local->dot11MultipleRetryCount);
229 DEBUGFS_STATS_FILE(frame_duplicate_count, 20, "%u",
230                    local->dot11FrameDuplicateCount);
231 DEBUGFS_STATS_FILE(received_fragment_count, 20, "%u",
232                    local->dot11ReceivedFragmentCount);
233 DEBUGFS_STATS_FILE(multicast_received_frame_count, 20, "%u",
234                    local->dot11MulticastReceivedFrameCount);
235 DEBUGFS_STATS_FILE(transmitted_frame_count, 20, "%u",
236                    local->dot11TransmittedFrameCount);
237 DEBUGFS_STATS_FILE(wep_undecryptable_count, 20, "%u",
238                    local->dot11WEPUndecryptableCount);
239 #ifdef CONFIG_MAC80211_DEBUG_COUNTERS
240 DEBUGFS_STATS_FILE(tx_handlers_drop, 20, "%u",
241                    local->tx_handlers_drop);
242 DEBUGFS_STATS_FILE(tx_handlers_queued, 20, "%u",
243                    local->tx_handlers_queued);
244 DEBUGFS_STATS_FILE(tx_handlers_drop_unencrypted, 20, "%u",
245                    local->tx_handlers_drop_unencrypted);
246 DEBUGFS_STATS_FILE(tx_handlers_drop_fragment, 20, "%u",
247                    local->tx_handlers_drop_fragment);
248 DEBUGFS_STATS_FILE(tx_handlers_drop_wep, 20, "%u",
249                    local->tx_handlers_drop_wep);
250 DEBUGFS_STATS_FILE(tx_handlers_drop_not_assoc, 20, "%u",
251                    local->tx_handlers_drop_not_assoc);
252 DEBUGFS_STATS_FILE(tx_handlers_drop_unauth_port, 20, "%u",
253                    local->tx_handlers_drop_unauth_port);
254 DEBUGFS_STATS_FILE(rx_handlers_drop, 20, "%u",
255                    local->rx_handlers_drop);
256 DEBUGFS_STATS_FILE(rx_handlers_queued, 20, "%u",
257                    local->rx_handlers_queued);
258 DEBUGFS_STATS_FILE(rx_handlers_drop_nullfunc, 20, "%u",
259                    local->rx_handlers_drop_nullfunc);
260 DEBUGFS_STATS_FILE(rx_handlers_drop_defrag, 20, "%u",
261                    local->rx_handlers_drop_defrag);
262 DEBUGFS_STATS_FILE(rx_handlers_drop_short, 20, "%u",
263                    local->rx_handlers_drop_short);
264 DEBUGFS_STATS_FILE(rx_handlers_drop_passive_scan, 20, "%u",
265                    local->rx_handlers_drop_passive_scan);
266 DEBUGFS_STATS_FILE(tx_expand_skb_head, 20, "%u",
267                    local->tx_expand_skb_head);
268 DEBUGFS_STATS_FILE(tx_expand_skb_head_cloned, 20, "%u",
269                    local->tx_expand_skb_head_cloned);
270 DEBUGFS_STATS_FILE(rx_expand_skb_head, 20, "%u",
271                    local->rx_expand_skb_head);
272 DEBUGFS_STATS_FILE(rx_expand_skb_head2, 20, "%u",
273                    local->rx_expand_skb_head2);
274 DEBUGFS_STATS_FILE(rx_handlers_fragments, 20, "%u",
275                    local->rx_handlers_fragments);
276 DEBUGFS_STATS_FILE(tx_status_drop, 20, "%u",
277                    local->tx_status_drop);
278
279 static ssize_t stats_wme_rx_queue_read(struct file *file,
280                                        char __user *userbuf,
281                                        size_t count, loff_t *ppos)
282 {
283         struct ieee80211_local *local = file->private_data;
284         char buf[NUM_RX_DATA_QUEUES*15], *p = buf;
285         int i;
286
287         for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
288                 p += scnprintf(p, sizeof(buf)+buf-p,
289                                "%u\n", local->wme_rx_queue[i]);
290
291         return simple_read_from_buffer(userbuf, count, ppos, buf, p-buf);
292 }
293
294 static const struct file_operations stats_wme_rx_queue_ops = {
295         .read = stats_wme_rx_queue_read,
296         .open = mac80211_open_file_generic,
297 };
298
299 static ssize_t stats_wme_tx_queue_read(struct file *file,
300                                        char __user *userbuf,
301                                        size_t count, loff_t *ppos)
302 {
303         struct ieee80211_local *local = file->private_data;
304         char buf[NUM_TX_DATA_QUEUES*15], *p = buf;
305         int i;
306
307         for (i = 0; i < NUM_TX_DATA_QUEUES; i++)
308                 p += scnprintf(p, sizeof(buf)+buf-p,
309                                "%u\n", local->wme_tx_queue[i]);
310
311         return simple_read_from_buffer(userbuf, count, ppos, buf, p-buf);
312 }
313
314 static const struct file_operations stats_wme_tx_queue_ops = {
315         .read = stats_wme_tx_queue_read,
316         .open = mac80211_open_file_generic,
317 };
318 #endif
319
320 DEBUGFS_DEVSTATS_FILE(dot11ACKFailureCount);
321 DEBUGFS_DEVSTATS_FILE(dot11RTSFailureCount);
322 DEBUGFS_DEVSTATS_FILE(dot11FCSErrorCount);
323 DEBUGFS_DEVSTATS_FILE(dot11RTSSuccessCount);
324
325
326 void debugfs_hw_add(struct ieee80211_local *local)
327 {
328         struct dentry *phyd = local->hw.wiphy->debugfsdir;
329         struct dentry *statsd;
330
331         if (!phyd)
332                 return;
333
334         local->debugfs.stations = debugfs_create_dir("stations", phyd);
335         local->debugfs.keys = debugfs_create_dir("keys", phyd);
336
337         DEBUGFS_ADD(channel);
338         DEBUGFS_ADD(frequency);
339         DEBUGFS_ADD(radar_detect);
340         DEBUGFS_ADD(antenna_sel_tx);
341         DEBUGFS_ADD(antenna_sel_rx);
342         DEBUGFS_ADD(bridge_packets);
343         DEBUGFS_ADD(key_tx_rx_threshold);
344         DEBUGFS_ADD(rts_threshold);
345         DEBUGFS_ADD(fragmentation_threshold);
346         DEBUGFS_ADD(short_retry_limit);
347         DEBUGFS_ADD(long_retry_limit);
348         DEBUGFS_ADD(total_ps_buffered);
349         DEBUGFS_ADD(mode);
350         DEBUGFS_ADD(wep_iv);
351         DEBUGFS_ADD(tx_power_reduction);
352         DEBUGFS_ADD_MODE(rate_ctrl_alg, 0644);
353         DEBUGFS_ADD(modes);
354
355         statsd = debugfs_create_dir("statistics", phyd);
356         local->debugfs.statistics = statsd;
357
358         /* if the dir failed, don't put all the other things into the root! */
359         if (!statsd)
360                 return;
361
362         DEBUGFS_STATS_ADD(transmitted_fragment_count);
363         DEBUGFS_STATS_ADD(multicast_transmitted_frame_count);
364         DEBUGFS_STATS_ADD(failed_count);
365         DEBUGFS_STATS_ADD(retry_count);
366         DEBUGFS_STATS_ADD(multiple_retry_count);
367         DEBUGFS_STATS_ADD(frame_duplicate_count);
368         DEBUGFS_STATS_ADD(received_fragment_count);
369         DEBUGFS_STATS_ADD(multicast_received_frame_count);
370         DEBUGFS_STATS_ADD(transmitted_frame_count);
371         DEBUGFS_STATS_ADD(wep_undecryptable_count);
372 #ifdef CONFIG_MAC80211_DEBUG_COUNTERS
373         DEBUGFS_STATS_ADD(tx_handlers_drop);
374         DEBUGFS_STATS_ADD(tx_handlers_queued);
375         DEBUGFS_STATS_ADD(tx_handlers_drop_unencrypted);
376         DEBUGFS_STATS_ADD(tx_handlers_drop_fragment);
377         DEBUGFS_STATS_ADD(tx_handlers_drop_wep);
378         DEBUGFS_STATS_ADD(tx_handlers_drop_not_assoc);
379         DEBUGFS_STATS_ADD(tx_handlers_drop_unauth_port);
380         DEBUGFS_STATS_ADD(rx_handlers_drop);
381         DEBUGFS_STATS_ADD(rx_handlers_queued);
382         DEBUGFS_STATS_ADD(rx_handlers_drop_nullfunc);
383         DEBUGFS_STATS_ADD(rx_handlers_drop_defrag);
384         DEBUGFS_STATS_ADD(rx_handlers_drop_short);
385         DEBUGFS_STATS_ADD(rx_handlers_drop_passive_scan);
386         DEBUGFS_STATS_ADD(tx_expand_skb_head);
387         DEBUGFS_STATS_ADD(tx_expand_skb_head_cloned);
388         DEBUGFS_STATS_ADD(rx_expand_skb_head);
389         DEBUGFS_STATS_ADD(rx_expand_skb_head2);
390         DEBUGFS_STATS_ADD(rx_handlers_fragments);
391         DEBUGFS_STATS_ADD(tx_status_drop);
392         DEBUGFS_STATS_ADD(wme_tx_queue);
393         DEBUGFS_STATS_ADD(wme_rx_queue);
394 #endif
395         DEBUGFS_STATS_ADD(dot11ACKFailureCount);
396         DEBUGFS_STATS_ADD(dot11RTSFailureCount);
397         DEBUGFS_STATS_ADD(dot11FCSErrorCount);
398         DEBUGFS_STATS_ADD(dot11RTSSuccessCount);
399 }
400
401 void debugfs_hw_del(struct ieee80211_local *local)
402 {
403         DEBUGFS_DEL(channel);
404         DEBUGFS_DEL(frequency);
405         DEBUGFS_DEL(radar_detect);
406         DEBUGFS_DEL(antenna_sel_tx);
407         DEBUGFS_DEL(antenna_sel_rx);
408         DEBUGFS_DEL(bridge_packets);
409         DEBUGFS_DEL(key_tx_rx_threshold);
410         DEBUGFS_DEL(rts_threshold);
411         DEBUGFS_DEL(fragmentation_threshold);
412         DEBUGFS_DEL(short_retry_limit);
413         DEBUGFS_DEL(long_retry_limit);
414         DEBUGFS_DEL(total_ps_buffered);
415         DEBUGFS_DEL(mode);
416         DEBUGFS_DEL(wep_iv);
417         DEBUGFS_DEL(tx_power_reduction);
418         DEBUGFS_DEL(rate_ctrl_alg);
419         DEBUGFS_DEL(modes);
420
421         DEBUGFS_STATS_DEL(transmitted_fragment_count);
422         DEBUGFS_STATS_DEL(multicast_transmitted_frame_count);
423         DEBUGFS_STATS_DEL(failed_count);
424         DEBUGFS_STATS_DEL(retry_count);
425         DEBUGFS_STATS_DEL(multiple_retry_count);
426         DEBUGFS_STATS_DEL(frame_duplicate_count);
427         DEBUGFS_STATS_DEL(received_fragment_count);
428         DEBUGFS_STATS_DEL(multicast_received_frame_count);
429         DEBUGFS_STATS_DEL(transmitted_frame_count);
430         DEBUGFS_STATS_DEL(wep_undecryptable_count);
431         DEBUGFS_STATS_DEL(num_scans);
432 #ifdef CONFIG_MAC80211_DEBUG_COUNTERS
433         DEBUGFS_STATS_DEL(tx_handlers_drop);
434         DEBUGFS_STATS_DEL(tx_handlers_queued);
435         DEBUGFS_STATS_DEL(tx_handlers_drop_unencrypted);
436         DEBUGFS_STATS_DEL(tx_handlers_drop_fragment);
437         DEBUGFS_STATS_DEL(tx_handlers_drop_wep);
438         DEBUGFS_STATS_DEL(tx_handlers_drop_not_assoc);
439         DEBUGFS_STATS_DEL(tx_handlers_drop_unauth_port);
440         DEBUGFS_STATS_DEL(rx_handlers_drop);
441         DEBUGFS_STATS_DEL(rx_handlers_queued);
442         DEBUGFS_STATS_DEL(rx_handlers_drop_nullfunc);
443         DEBUGFS_STATS_DEL(rx_handlers_drop_defrag);
444         DEBUGFS_STATS_DEL(rx_handlers_drop_short);
445         DEBUGFS_STATS_DEL(rx_handlers_drop_passive_scan);
446         DEBUGFS_STATS_DEL(tx_expand_skb_head);
447         DEBUGFS_STATS_DEL(tx_expand_skb_head_cloned);
448         DEBUGFS_STATS_DEL(rx_expand_skb_head);
449         DEBUGFS_STATS_DEL(rx_expand_skb_head2);
450         DEBUGFS_STATS_DEL(rx_handlers_fragments);
451         DEBUGFS_STATS_DEL(tx_status_drop);
452         DEBUGFS_STATS_DEL(wme_tx_queue);
453         DEBUGFS_STATS_DEL(wme_rx_queue);
454 #endif
455         DEBUGFS_STATS_DEL(dot11ACKFailureCount);
456         DEBUGFS_STATS_DEL(dot11RTSFailureCount);
457         DEBUGFS_STATS_DEL(dot11FCSErrorCount);
458         DEBUGFS_STATS_DEL(dot11RTSSuccessCount);
459
460         debugfs_remove(local->debugfs.statistics);
461         local->debugfs.statistics = NULL;
462         debugfs_remove(local->debugfs.stations);
463         local->debugfs.stations = NULL;
464         debugfs_remove(local->debugfs.keys);
465         local->debugfs.keys = NULL;
466 }