ath5k: fix rx descriptor debugging
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath5k / debug.c
1 /*
2  * Copyright (c) 2007-2008 Bruno Randolf <bruno@thinktube.com>
3  *
4  *  This file is free software: you may copy, redistribute and/or modify it
5  *  under the terms of the GNU General Public License as published by the
6  *  Free Software Foundation, either version 2 of the License, or (at your
7  *  option) any later version.
8  *
9  *  This file is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  *
18  * This file incorporates work covered by the following copyright and
19  * permission notice:
20  *
21  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
22  * Copyright (c) 2004-2005 Atheros Communications, Inc.
23  * Copyright (c) 2006 Devicescape Software, Inc.
24  * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
25  * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
26  *
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  * 1. Redistributions of source code must retain the above copyright
33  *    notice, this list of conditions and the following disclaimer,
34  *    without modification.
35  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
36  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
37  *    redistribution must be conditioned upon including a substantially
38  *    similar Disclaimer requirement for further binary redistribution.
39  * 3. Neither the names of the above-listed copyright holders nor the names
40  *    of any contributors may be used to endorse or promote products derived
41  *    from this software without specific prior written permission.
42  *
43  * Alternatively, this software may be distributed under the terms of the
44  * GNU General Public License ("GPL") version 2 as published by the Free
45  * Software Foundation.
46  *
47  * NO WARRANTY
48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
51  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
52  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
53  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
56  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58  * THE POSSIBILITY OF SUCH DAMAGES.
59  */
60
61 #include "base.h"
62 #include "debug.h"
63
64 static unsigned int ath5k_debug;
65 module_param_named(debug, ath5k_debug, uint, 0);
66
67
68 #ifdef CONFIG_ATH5K_DEBUG
69
70 #include <linux/seq_file.h>
71 #include "reg.h"
72 #include "ani.h"
73
74 static struct dentry *ath5k_global_debugfs;
75
76 static int ath5k_debugfs_open(struct inode *inode, struct file *file)
77 {
78         file->private_data = inode->i_private;
79         return 0;
80 }
81
82
83 /* debugfs: registers */
84
85 struct reg {
86         const char *name;
87         int addr;
88 };
89
90 #define REG_STRUCT_INIT(r) { #r, r }
91
92 /* just a few random registers, might want to add more */
93 static const struct reg regs[] = {
94         REG_STRUCT_INIT(AR5K_CR),
95         REG_STRUCT_INIT(AR5K_RXDP),
96         REG_STRUCT_INIT(AR5K_CFG),
97         REG_STRUCT_INIT(AR5K_IER),
98         REG_STRUCT_INIT(AR5K_BCR),
99         REG_STRUCT_INIT(AR5K_RTSD0),
100         REG_STRUCT_INIT(AR5K_RTSD1),
101         REG_STRUCT_INIT(AR5K_TXCFG),
102         REG_STRUCT_INIT(AR5K_RXCFG),
103         REG_STRUCT_INIT(AR5K_RXJLA),
104         REG_STRUCT_INIT(AR5K_MIBC),
105         REG_STRUCT_INIT(AR5K_TOPS),
106         REG_STRUCT_INIT(AR5K_RXNOFRM),
107         REG_STRUCT_INIT(AR5K_TXNOFRM),
108         REG_STRUCT_INIT(AR5K_RPGTO),
109         REG_STRUCT_INIT(AR5K_RFCNT),
110         REG_STRUCT_INIT(AR5K_MISC),
111         REG_STRUCT_INIT(AR5K_QCUDCU_CLKGT),
112         REG_STRUCT_INIT(AR5K_ISR),
113         REG_STRUCT_INIT(AR5K_PISR),
114         REG_STRUCT_INIT(AR5K_SISR0),
115         REG_STRUCT_INIT(AR5K_SISR1),
116         REG_STRUCT_INIT(AR5K_SISR2),
117         REG_STRUCT_INIT(AR5K_SISR3),
118         REG_STRUCT_INIT(AR5K_SISR4),
119         REG_STRUCT_INIT(AR5K_IMR),
120         REG_STRUCT_INIT(AR5K_PIMR),
121         REG_STRUCT_INIT(AR5K_SIMR0),
122         REG_STRUCT_INIT(AR5K_SIMR1),
123         REG_STRUCT_INIT(AR5K_SIMR2),
124         REG_STRUCT_INIT(AR5K_SIMR3),
125         REG_STRUCT_INIT(AR5K_SIMR4),
126         REG_STRUCT_INIT(AR5K_DCM_ADDR),
127         REG_STRUCT_INIT(AR5K_DCCFG),
128         REG_STRUCT_INIT(AR5K_CCFG),
129         REG_STRUCT_INIT(AR5K_CPC0),
130         REG_STRUCT_INIT(AR5K_CPC1),
131         REG_STRUCT_INIT(AR5K_CPC2),
132         REG_STRUCT_INIT(AR5K_CPC3),
133         REG_STRUCT_INIT(AR5K_CPCOVF),
134         REG_STRUCT_INIT(AR5K_RESET_CTL),
135         REG_STRUCT_INIT(AR5K_SLEEP_CTL),
136         REG_STRUCT_INIT(AR5K_INTPEND),
137         REG_STRUCT_INIT(AR5K_SFR),
138         REG_STRUCT_INIT(AR5K_PCICFG),
139         REG_STRUCT_INIT(AR5K_GPIOCR),
140         REG_STRUCT_INIT(AR5K_GPIODO),
141         REG_STRUCT_INIT(AR5K_SREV),
142 };
143
144 static void *reg_start(struct seq_file *seq, loff_t *pos)
145 {
146         return *pos < ARRAY_SIZE(regs) ? (void *)&regs[*pos] : NULL;
147 }
148
149 static void reg_stop(struct seq_file *seq, void *p)
150 {
151         /* nothing to do */
152 }
153
154 static void *reg_next(struct seq_file *seq, void *p, loff_t *pos)
155 {
156         ++*pos;
157         return *pos < ARRAY_SIZE(regs) ? (void *)&regs[*pos] : NULL;
158 }
159
160 static int reg_show(struct seq_file *seq, void *p)
161 {
162         struct ath5k_softc *sc = seq->private;
163         struct reg *r = p;
164         seq_printf(seq, "%-25s0x%08x\n", r->name,
165                 ath5k_hw_reg_read(sc->ah, r->addr));
166         return 0;
167 }
168
169 static const struct seq_operations register_seq_ops = {
170         .start = reg_start,
171         .next  = reg_next,
172         .stop  = reg_stop,
173         .show  = reg_show
174 };
175
176 static int open_file_registers(struct inode *inode, struct file *file)
177 {
178         struct seq_file *s;
179         int res;
180         res = seq_open(file, &register_seq_ops);
181         if (res == 0) {
182                 s = file->private_data;
183                 s->private = inode->i_private;
184         }
185         return res;
186 }
187
188 static const struct file_operations fops_registers = {
189         .open = open_file_registers,
190         .read    = seq_read,
191         .llseek  = seq_lseek,
192         .release = seq_release,
193         .owner = THIS_MODULE,
194 };
195
196
197 /* debugfs: beacons */
198
199 static ssize_t read_file_beacon(struct file *file, char __user *user_buf,
200                                    size_t count, loff_t *ppos)
201 {
202         struct ath5k_softc *sc = file->private_data;
203         struct ath5k_hw *ah = sc->ah;
204         char buf[500];
205         unsigned int len = 0;
206         unsigned int v;
207         u64 tsf;
208
209         v = ath5k_hw_reg_read(sc->ah, AR5K_BEACON);
210         len += snprintf(buf+len, sizeof(buf)-len,
211                 "%-24s0x%08x\tintval: %d\tTIM: 0x%x\n",
212                 "AR5K_BEACON", v, v & AR5K_BEACON_PERIOD,
213                 (v & AR5K_BEACON_TIM) >> AR5K_BEACON_TIM_S);
214
215         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\n",
216                 "AR5K_LAST_TSTP", ath5k_hw_reg_read(sc->ah, AR5K_LAST_TSTP));
217
218         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\n\n",
219                 "AR5K_BEACON_CNT", ath5k_hw_reg_read(sc->ah, AR5K_BEACON_CNT));
220
221         v = ath5k_hw_reg_read(sc->ah, AR5K_TIMER0);
222         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\tTU: %08x\n",
223                 "AR5K_TIMER0 (TBTT)", v, v);
224
225         v = ath5k_hw_reg_read(sc->ah, AR5K_TIMER1);
226         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\tTU: %08x\n",
227                 "AR5K_TIMER1 (DMA)", v, v >> 3);
228
229         v = ath5k_hw_reg_read(sc->ah, AR5K_TIMER2);
230         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\tTU: %08x\n",
231                 "AR5K_TIMER2 (SWBA)", v, v >> 3);
232
233         v = ath5k_hw_reg_read(sc->ah, AR5K_TIMER3);
234         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\tTU: %08x\n",
235                 "AR5K_TIMER3 (ATIM)", v, v);
236
237         tsf = ath5k_hw_get_tsf64(sc->ah);
238         len += snprintf(buf+len, sizeof(buf)-len,
239                 "TSF\t\t0x%016llx\tTU: %08x\n",
240                 (unsigned long long)tsf, TSF_TO_TU(tsf));
241
242         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
243 }
244
245 static ssize_t write_file_beacon(struct file *file,
246                                  const char __user *userbuf,
247                                  size_t count, loff_t *ppos)
248 {
249         struct ath5k_softc *sc = file->private_data;
250         struct ath5k_hw *ah = sc->ah;
251         char buf[20];
252
253         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
254                 return -EFAULT;
255
256         if (strncmp(buf, "disable", 7) == 0) {
257                 AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE);
258                 printk(KERN_INFO "debugfs disable beacons\n");
259         } else if (strncmp(buf, "enable", 6) == 0) {
260                 AR5K_REG_ENABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE);
261                 printk(KERN_INFO "debugfs enable beacons\n");
262         }
263         return count;
264 }
265
266 static const struct file_operations fops_beacon = {
267         .read = read_file_beacon,
268         .write = write_file_beacon,
269         .open = ath5k_debugfs_open,
270         .owner = THIS_MODULE,
271 };
272
273
274 /* debugfs: reset */
275
276 static ssize_t write_file_reset(struct file *file,
277                                  const char __user *userbuf,
278                                  size_t count, loff_t *ppos)
279 {
280         struct ath5k_softc *sc = file->private_data;
281         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "debug file triggered reset\n");
282         tasklet_schedule(&sc->restq);
283         return count;
284 }
285
286 static const struct file_operations fops_reset = {
287         .write = write_file_reset,
288         .open = ath5k_debugfs_open,
289         .owner = THIS_MODULE,
290 };
291
292
293 /* debugfs: debug level */
294
295 static const struct {
296         enum ath5k_debug_level level;
297         const char *name;
298         const char *desc;
299 } dbg_info[] = {
300         { ATH5K_DEBUG_RESET,    "reset",        "reset and initialization" },
301         { ATH5K_DEBUG_INTR,     "intr",         "interrupt handling" },
302         { ATH5K_DEBUG_MODE,     "mode",         "mode init/setup" },
303         { ATH5K_DEBUG_XMIT,     "xmit",         "basic xmit operation" },
304         { ATH5K_DEBUG_BEACON,   "beacon",       "beacon handling" },
305         { ATH5K_DEBUG_CALIBRATE, "calib",       "periodic calibration" },
306         { ATH5K_DEBUG_TXPOWER,  "txpower",      "transmit power setting" },
307         { ATH5K_DEBUG_LED,      "led",          "LED management" },
308         { ATH5K_DEBUG_DUMP_RX,  "dumprx",       "print received skb content" },
309         { ATH5K_DEBUG_DUMP_TX,  "dumptx",       "print transmit skb content" },
310         { ATH5K_DEBUG_DUMPBANDS, "dumpbands",   "dump bands" },
311         { ATH5K_DEBUG_ANI,      "ani",          "adaptive noise immunity" },
312         { ATH5K_DEBUG_ANY,      "all",          "show all debug levels" },
313 };
314
315 static ssize_t read_file_debug(struct file *file, char __user *user_buf,
316                                    size_t count, loff_t *ppos)
317 {
318         struct ath5k_softc *sc = file->private_data;
319         char buf[700];
320         unsigned int len = 0;
321         unsigned int i;
322
323         len += snprintf(buf+len, sizeof(buf)-len,
324                 "DEBUG LEVEL: 0x%08x\n\n", sc->debug.level);
325
326         for (i = 0; i < ARRAY_SIZE(dbg_info) - 1; i++) {
327                 len += snprintf(buf+len, sizeof(buf)-len,
328                         "%10s %c 0x%08x - %s\n", dbg_info[i].name,
329                         sc->debug.level & dbg_info[i].level ? '+' : ' ',
330                         dbg_info[i].level, dbg_info[i].desc);
331         }
332         len += snprintf(buf+len, sizeof(buf)-len,
333                 "%10s %c 0x%08x - %s\n", dbg_info[i].name,
334                 sc->debug.level == dbg_info[i].level ? '+' : ' ',
335                 dbg_info[i].level, dbg_info[i].desc);
336
337         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
338 }
339
340 static ssize_t write_file_debug(struct file *file,
341                                  const char __user *userbuf,
342                                  size_t count, loff_t *ppos)
343 {
344         struct ath5k_softc *sc = file->private_data;
345         unsigned int i;
346         char buf[20];
347
348         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
349                 return -EFAULT;
350
351         for (i = 0; i < ARRAY_SIZE(dbg_info); i++) {
352                 if (strncmp(buf, dbg_info[i].name,
353                                         strlen(dbg_info[i].name)) == 0) {
354                         sc->debug.level ^= dbg_info[i].level; /* toggle bit */
355                         break;
356                 }
357         }
358         return count;
359 }
360
361 static const struct file_operations fops_debug = {
362         .read = read_file_debug,
363         .write = write_file_debug,
364         .open = ath5k_debugfs_open,
365         .owner = THIS_MODULE,
366 };
367
368
369 /* debugfs: antenna */
370
371 static ssize_t read_file_antenna(struct file *file, char __user *user_buf,
372                                    size_t count, loff_t *ppos)
373 {
374         struct ath5k_softc *sc = file->private_data;
375         char buf[700];
376         unsigned int len = 0;
377         unsigned int i;
378         unsigned int v;
379
380         len += snprintf(buf+len, sizeof(buf)-len, "antenna mode\t%d\n",
381                 sc->ah->ah_ant_mode);
382         len += snprintf(buf+len, sizeof(buf)-len, "default antenna\t%d\n",
383                 sc->ah->ah_def_ant);
384         len += snprintf(buf+len, sizeof(buf)-len, "tx antenna\t%d\n",
385                 sc->ah->ah_tx_ant);
386
387         len += snprintf(buf+len, sizeof(buf)-len, "\nANTENNA\t\tRX\tTX\n");
388         for (i = 1; i < ARRAY_SIZE(sc->stats.antenna_rx); i++) {
389                 len += snprintf(buf+len, sizeof(buf)-len,
390                         "[antenna %d]\t%d\t%d\n",
391                         i, sc->stats.antenna_rx[i], sc->stats.antenna_tx[i]);
392         }
393         len += snprintf(buf+len, sizeof(buf)-len, "[invalid]\t%d\t%d\n",
394                         sc->stats.antenna_rx[0], sc->stats.antenna_tx[0]);
395
396         v = ath5k_hw_reg_read(sc->ah, AR5K_DEFAULT_ANTENNA);
397         len += snprintf(buf+len, sizeof(buf)-len,
398                         "\nAR5K_DEFAULT_ANTENNA\t0x%08x\n", v);
399
400         v = ath5k_hw_reg_read(sc->ah, AR5K_STA_ID1);
401         len += snprintf(buf+len, sizeof(buf)-len,
402                 "AR5K_STA_ID1_DEFAULT_ANTENNA\t%d\n",
403                 (v & AR5K_STA_ID1_DEFAULT_ANTENNA) != 0);
404         len += snprintf(buf+len, sizeof(buf)-len,
405                 "AR5K_STA_ID1_DESC_ANTENNA\t%d\n",
406                 (v & AR5K_STA_ID1_DESC_ANTENNA) != 0);
407         len += snprintf(buf+len, sizeof(buf)-len,
408                 "AR5K_STA_ID1_RTS_DEF_ANTENNA\t%d\n",
409                 (v & AR5K_STA_ID1_RTS_DEF_ANTENNA) != 0);
410         len += snprintf(buf+len, sizeof(buf)-len,
411                 "AR5K_STA_ID1_SELFGEN_DEF_ANT\t%d\n",
412                 (v & AR5K_STA_ID1_SELFGEN_DEF_ANT) != 0);
413
414         v = ath5k_hw_reg_read(sc->ah, AR5K_PHY_AGCCTL);
415         len += snprintf(buf+len, sizeof(buf)-len,
416                 "\nAR5K_PHY_AGCCTL_OFDM_DIV_DIS\t%d\n",
417                 (v & AR5K_PHY_AGCCTL_OFDM_DIV_DIS) != 0);
418
419         v = ath5k_hw_reg_read(sc->ah, AR5K_PHY_RESTART);
420         len += snprintf(buf+len, sizeof(buf)-len,
421                 "AR5K_PHY_RESTART_DIV_GC\t\t%x\n",
422                 (v & AR5K_PHY_RESTART_DIV_GC) >> AR5K_PHY_RESTART_DIV_GC_S);
423
424         v = ath5k_hw_reg_read(sc->ah, AR5K_PHY_FAST_ANT_DIV);
425         len += snprintf(buf+len, sizeof(buf)-len,
426                 "AR5K_PHY_FAST_ANT_DIV_EN\t%d\n",
427                 (v & AR5K_PHY_FAST_ANT_DIV_EN) != 0);
428
429         v = ath5k_hw_reg_read(sc->ah, AR5K_PHY_ANT_SWITCH_TABLE_0);
430         len += snprintf(buf+len, sizeof(buf)-len,
431                         "\nAR5K_PHY_ANT_SWITCH_TABLE_0\t0x%08x\n", v);
432         v = ath5k_hw_reg_read(sc->ah, AR5K_PHY_ANT_SWITCH_TABLE_1);
433         len += snprintf(buf+len, sizeof(buf)-len,
434                         "AR5K_PHY_ANT_SWITCH_TABLE_1\t0x%08x\n", v);
435
436         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
437 }
438
439 static ssize_t write_file_antenna(struct file *file,
440                                  const char __user *userbuf,
441                                  size_t count, loff_t *ppos)
442 {
443         struct ath5k_softc *sc = file->private_data;
444         unsigned int i;
445         char buf[20];
446
447         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
448                 return -EFAULT;
449
450         if (strncmp(buf, "diversity", 9) == 0) {
451                 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_DEFAULT);
452                 printk(KERN_INFO "ath5k debug: enable diversity\n");
453         } else if (strncmp(buf, "fixed-a", 7) == 0) {
454                 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_A);
455                 printk(KERN_INFO "ath5k debugfs: fixed antenna A\n");
456         } else if (strncmp(buf, "fixed-b", 7) == 0) {
457                 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_B);
458                 printk(KERN_INFO "ath5k debug: fixed antenna B\n");
459         } else if (strncmp(buf, "clear", 5) == 0) {
460                 for (i = 0; i < ARRAY_SIZE(sc->stats.antenna_rx); i++) {
461                         sc->stats.antenna_rx[i] = 0;
462                         sc->stats.antenna_tx[i] = 0;
463                 }
464                 printk(KERN_INFO "ath5k debug: cleared antenna stats\n");
465         }
466         return count;
467 }
468
469 static const struct file_operations fops_antenna = {
470         .read = read_file_antenna,
471         .write = write_file_antenna,
472         .open = ath5k_debugfs_open,
473         .owner = THIS_MODULE,
474 };
475
476
477 /* debugfs: frameerrors */
478
479 static ssize_t read_file_frameerrors(struct file *file, char __user *user_buf,
480                                    size_t count, loff_t *ppos)
481 {
482         struct ath5k_softc *sc = file->private_data;
483         struct ath5k_statistics *st = &sc->stats;
484         char buf[700];
485         unsigned int len = 0;
486         int i;
487
488         len += snprintf(buf+len, sizeof(buf)-len,
489                         "RX\n---------------------\n");
490         len += snprintf(buf+len, sizeof(buf)-len, "CRC\t%d\t(%d%%)\n",
491                         st->rxerr_crc,
492                         st->rx_all_count > 0 ?
493                                 st->rxerr_crc*100/st->rx_all_count : 0);
494         len += snprintf(buf+len, sizeof(buf)-len, "PHY\t%d\t(%d%%)\n",
495                         st->rxerr_phy,
496                         st->rx_all_count > 0 ?
497                                 st->rxerr_phy*100/st->rx_all_count : 0);
498         for (i = 0; i < 32; i++) {
499                 if (st->rxerr_phy_code[i])
500                         len += snprintf(buf+len, sizeof(buf)-len,
501                                 " phy_err[%d]\t%d\n",
502                                 i, st->rxerr_phy_code[i]);
503         }
504
505         len += snprintf(buf+len, sizeof(buf)-len, "FIFO\t%d\t(%d%%)\n",
506                         st->rxerr_fifo,
507                         st->rx_all_count > 0 ?
508                                 st->rxerr_fifo*100/st->rx_all_count : 0);
509         len += snprintf(buf+len, sizeof(buf)-len, "decrypt\t%d\t(%d%%)\n",
510                         st->rxerr_decrypt,
511                         st->rx_all_count > 0 ?
512                                 st->rxerr_decrypt*100/st->rx_all_count : 0);
513         len += snprintf(buf+len, sizeof(buf)-len, "MIC\t%d\t(%d%%)\n",
514                         st->rxerr_mic,
515                         st->rx_all_count > 0 ?
516                                 st->rxerr_mic*100/st->rx_all_count : 0);
517         len += snprintf(buf+len, sizeof(buf)-len, "process\t%d\t(%d%%)\n",
518                         st->rxerr_proc,
519                         st->rx_all_count > 0 ?
520                                 st->rxerr_proc*100/st->rx_all_count : 0);
521         len += snprintf(buf+len, sizeof(buf)-len, "jumbo\t%d\t(%d%%)\n",
522                         st->rxerr_jumbo,
523                         st->rx_all_count > 0 ?
524                                 st->rxerr_jumbo*100/st->rx_all_count : 0);
525         len += snprintf(buf+len, sizeof(buf)-len, "[RX all\t%d]\n",
526                         st->rx_all_count);
527
528         len += snprintf(buf+len, sizeof(buf)-len,
529                         "\nTX\n---------------------\n");
530         len += snprintf(buf+len, sizeof(buf)-len, "retry\t%d\t(%d%%)\n",
531                         st->txerr_retry,
532                         st->tx_all_count > 0 ?
533                                 st->txerr_retry*100/st->tx_all_count : 0);
534         len += snprintf(buf+len, sizeof(buf)-len, "FIFO\t%d\t(%d%%)\n",
535                         st->txerr_fifo,
536                         st->tx_all_count > 0 ?
537                                 st->txerr_fifo*100/st->tx_all_count : 0);
538         len += snprintf(buf+len, sizeof(buf)-len, "filter\t%d\t(%d%%)\n",
539                         st->txerr_filt,
540                         st->tx_all_count > 0 ?
541                                 st->txerr_filt*100/st->tx_all_count : 0);
542         len += snprintf(buf+len, sizeof(buf)-len, "[TX all\t%d]\n",
543                         st->tx_all_count);
544
545         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
546 }
547
548 static ssize_t write_file_frameerrors(struct file *file,
549                                  const char __user *userbuf,
550                                  size_t count, loff_t *ppos)
551 {
552         struct ath5k_softc *sc = file->private_data;
553         struct ath5k_statistics *st = &sc->stats;
554         char buf[20];
555
556         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
557                 return -EFAULT;
558
559         if (strncmp(buf, "clear", 5) == 0) {
560                 st->rxerr_crc = 0;
561                 st->rxerr_phy = 0;
562                 st->rxerr_fifo = 0;
563                 st->rxerr_decrypt = 0;
564                 st->rxerr_mic = 0;
565                 st->rxerr_proc = 0;
566                 st->rxerr_jumbo = 0;
567                 st->rx_all_count = 0;
568                 st->txerr_retry = 0;
569                 st->txerr_fifo = 0;
570                 st->txerr_filt = 0;
571                 st->tx_all_count = 0;
572                 printk(KERN_INFO "ath5k debug: cleared frameerrors stats\n");
573         }
574         return count;
575 }
576
577 static const struct file_operations fops_frameerrors = {
578         .read = read_file_frameerrors,
579         .write = write_file_frameerrors,
580         .open = ath5k_debugfs_open,
581         .owner = THIS_MODULE,
582 };
583
584
585 /* debugfs: ani */
586
587 static ssize_t read_file_ani(struct file *file, char __user *user_buf,
588                                    size_t count, loff_t *ppos)
589 {
590         struct ath5k_softc *sc = file->private_data;
591         struct ath5k_statistics *st = &sc->stats;
592         struct ath5k_ani_state *as = &sc->ani_state;
593
594         char buf[700];
595         unsigned int len = 0;
596
597         len += snprintf(buf+len, sizeof(buf)-len,
598                         "HW has PHY error counters:\t%s\n",
599                         sc->ah->ah_capabilities.cap_has_phyerr_counters ?
600                         "yes" : "no");
601         len += snprintf(buf+len, sizeof(buf)-len,
602                         "HW max spur immunity level:\t%d\n",
603                         as->max_spur_level);
604         len += snprintf(buf+len, sizeof(buf)-len,
605                 "\nANI state\n--------------------------------------------\n");
606         len += snprintf(buf+len, sizeof(buf)-len, "operating mode:\t\t\t");
607         switch (as->ani_mode) {
608         case ATH5K_ANI_MODE_OFF:
609                 len += snprintf(buf+len, sizeof(buf)-len, "OFF\n");
610                 break;
611         case ATH5K_ANI_MODE_MANUAL_LOW:
612                 len += snprintf(buf+len, sizeof(buf)-len,
613                         "MANUAL LOW\n");
614                 break;
615         case ATH5K_ANI_MODE_MANUAL_HIGH:
616                 len += snprintf(buf+len, sizeof(buf)-len,
617                         "MANUAL HIGH\n");
618                 break;
619         case ATH5K_ANI_MODE_AUTO:
620                 len += snprintf(buf+len, sizeof(buf)-len, "AUTO\n");
621                 break;
622         default:
623                 len += snprintf(buf+len, sizeof(buf)-len,
624                         "??? (not good)\n");
625                 break;
626         }
627         len += snprintf(buf+len, sizeof(buf)-len,
628                         "noise immunity level:\t\t%d\n",
629                         as->noise_imm_level);
630         len += snprintf(buf+len, sizeof(buf)-len,
631                         "spur immunity level:\t\t%d\n",
632                         as->spur_level);
633         len += snprintf(buf+len, sizeof(buf)-len, "firstep level:\t\t\t%d\n",
634                         as->firstep_level);
635         len += snprintf(buf+len, sizeof(buf)-len,
636                         "OFDM weak signal detection:\t%s\n",
637                         as->ofdm_weak_sig ? "on" : "off");
638         len += snprintf(buf+len, sizeof(buf)-len,
639                         "CCK weak signal detection:\t%s\n",
640                         as->cck_weak_sig ? "on" : "off");
641
642         len += snprintf(buf+len, sizeof(buf)-len,
643                         "\nMIB INTERRUPTS:\t\t%u\n",
644                         st->mib_intr);
645         len += snprintf(buf+len, sizeof(buf)-len,
646                         "beacon RSSI average:\t%d\n",
647                         sc->ah->ah_beacon_rssi_avg.avg);
648         len += snprintf(buf+len, sizeof(buf)-len, "profcnt tx\t\t%u\t(%d%%)\n",
649                         as->pfc_tx,
650                         as->pfc_cycles > 0 ?
651                         as->pfc_tx*100/as->pfc_cycles : 0);
652         len += snprintf(buf+len, sizeof(buf)-len, "profcnt rx\t\t%u\t(%d%%)\n",
653                         as->pfc_rx,
654                         as->pfc_cycles > 0 ?
655                         as->pfc_rx*100/as->pfc_cycles : 0);
656         len += snprintf(buf+len, sizeof(buf)-len, "profcnt busy\t\t%u\t(%d%%)\n",
657                         as->pfc_busy,
658                         as->pfc_cycles > 0 ?
659                         as->pfc_busy*100/as->pfc_cycles : 0);
660         len += snprintf(buf+len, sizeof(buf)-len, "profcnt cycles\t\t%u\n",
661                         as->pfc_cycles);
662         len += snprintf(buf+len, sizeof(buf)-len,
663                         "listen time\t\t%d\tlast: %d\n",
664                         as->listen_time, as->last_listen);
665         len += snprintf(buf+len, sizeof(buf)-len,
666                         "OFDM errors\t\t%u\tlast: %u\tsum: %u\n",
667                         as->ofdm_errors, as->last_ofdm_errors,
668                         as->sum_ofdm_errors);
669         len += snprintf(buf+len, sizeof(buf)-len,
670                         "CCK errors\t\t%u\tlast: %u\tsum: %u\n",
671                         as->cck_errors, as->last_cck_errors,
672                         as->sum_cck_errors);
673         len += snprintf(buf+len, sizeof(buf)-len,
674                         "AR5K_PHYERR_CNT1\t%x\t(=%d)\n",
675                         ath5k_hw_reg_read(sc->ah, AR5K_PHYERR_CNT1),
676                         ATH5K_ANI_OFDM_TRIG_HIGH - (ATH5K_PHYERR_CNT_MAX -
677                         ath5k_hw_reg_read(sc->ah, AR5K_PHYERR_CNT1)));
678         len += snprintf(buf+len, sizeof(buf)-len,
679                         "AR5K_PHYERR_CNT2\t%x\t(=%d)\n",
680                         ath5k_hw_reg_read(sc->ah, AR5K_PHYERR_CNT2),
681                         ATH5K_ANI_CCK_TRIG_HIGH - (ATH5K_PHYERR_CNT_MAX -
682                         ath5k_hw_reg_read(sc->ah, AR5K_PHYERR_CNT2)));
683
684         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
685 }
686
687 static ssize_t write_file_ani(struct file *file,
688                                  const char __user *userbuf,
689                                  size_t count, loff_t *ppos)
690 {
691         struct ath5k_softc *sc = file->private_data;
692         char buf[20];
693
694         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
695                 return -EFAULT;
696
697         if (strncmp(buf, "sens-low", 8) == 0) {
698                 ath5k_ani_init(sc->ah, ATH5K_ANI_MODE_MANUAL_HIGH);
699         } else if (strncmp(buf, "sens-high", 9) == 0) {
700                 ath5k_ani_init(sc->ah, ATH5K_ANI_MODE_MANUAL_LOW);
701         } else if (strncmp(buf, "ani-off", 7) == 0) {
702                 ath5k_ani_init(sc->ah, ATH5K_ANI_MODE_OFF);
703         } else if (strncmp(buf, "ani-on", 6) == 0) {
704                 ath5k_ani_init(sc->ah, ATH5K_ANI_MODE_AUTO);
705         } else if (strncmp(buf, "noise-low", 9) == 0) {
706                 ath5k_ani_set_noise_immunity_level(sc->ah, 0);
707         } else if (strncmp(buf, "noise-high", 10) == 0) {
708                 ath5k_ani_set_noise_immunity_level(sc->ah,
709                                                    ATH5K_ANI_MAX_NOISE_IMM_LVL);
710         } else if (strncmp(buf, "spur-low", 8) == 0) {
711                 ath5k_ani_set_spur_immunity_level(sc->ah, 0);
712         } else if (strncmp(buf, "spur-high", 9) == 0) {
713                 ath5k_ani_set_spur_immunity_level(sc->ah,
714                                                   sc->ani_state.max_spur_level);
715         } else if (strncmp(buf, "fir-low", 7) == 0) {
716                 ath5k_ani_set_firstep_level(sc->ah, 0);
717         } else if (strncmp(buf, "fir-high", 8) == 0) {
718                 ath5k_ani_set_firstep_level(sc->ah, ATH5K_ANI_MAX_FIRSTEP_LVL);
719         } else if (strncmp(buf, "ofdm-off", 8) == 0) {
720                 ath5k_ani_set_ofdm_weak_signal_detection(sc->ah, false);
721         } else if (strncmp(buf, "ofdm-on", 7) == 0) {
722                 ath5k_ani_set_ofdm_weak_signal_detection(sc->ah, true);
723         } else if (strncmp(buf, "cck-off", 7) == 0) {
724                 ath5k_ani_set_cck_weak_signal_detection(sc->ah, false);
725         } else if (strncmp(buf, "cck-on", 6) == 0) {
726                 ath5k_ani_set_cck_weak_signal_detection(sc->ah, true);
727         }
728         return count;
729 }
730
731 static const struct file_operations fops_ani = {
732         .read = read_file_ani,
733         .write = write_file_ani,
734         .open = ath5k_debugfs_open,
735         .owner = THIS_MODULE,
736 };
737
738
739 /* debugfs: queues etc */
740
741 static ssize_t read_file_queue(struct file *file, char __user *user_buf,
742                                    size_t count, loff_t *ppos)
743 {
744         struct ath5k_softc *sc = file->private_data;
745         char buf[700];
746         unsigned int len = 0;
747
748         struct ath5k_txq *txq;
749         struct ath5k_buf *bf, *bf0;
750         int i, n = 0;
751
752         len += snprintf(buf+len, sizeof(buf)-len,
753                         "available txbuffers: %d\n", sc->txbuf_len);
754
755         for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
756                 txq = &sc->txqs[i];
757
758                 len += snprintf(buf+len, sizeof(buf)-len,
759                         "%02d: %ssetup\n", i, txq->setup ? "" : "not ");
760
761                 if (!txq->setup)
762                         continue;
763
764                 list_for_each_entry_safe(bf, bf0, &txq->q, list)
765                         n++;
766                 len += snprintf(buf+len, sizeof(buf)-len, "  len: %d\n", n);
767         }
768
769         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
770 }
771
772 static ssize_t write_file_queue(struct file *file,
773                                  const char __user *userbuf,
774                                  size_t count, loff_t *ppos)
775 {
776         struct ath5k_softc *sc = file->private_data;
777         char buf[20];
778
779         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
780                 return -EFAULT;
781
782         if (strncmp(buf, "start", 5) == 0)
783                 ieee80211_wake_queues(sc->hw);
784         else if (strncmp(buf, "stop", 4) == 0)
785                 ieee80211_stop_queues(sc->hw);
786
787         return count;
788 }
789
790
791 static const struct file_operations fops_queue = {
792         .read = read_file_queue,
793         .write = write_file_queue,
794         .open = ath5k_debugfs_open,
795         .owner = THIS_MODULE,
796 };
797
798
799 /* init */
800
801 void
802 ath5k_debug_init(void)
803 {
804         ath5k_global_debugfs = debugfs_create_dir("ath5k", NULL);
805 }
806
807 void
808 ath5k_debug_init_device(struct ath5k_softc *sc)
809 {
810         sc->debug.level = ath5k_debug;
811
812         sc->debug.debugfs_phydir = debugfs_create_dir(wiphy_name(sc->hw->wiphy),
813                                 ath5k_global_debugfs);
814
815         sc->debug.debugfs_debug = debugfs_create_file("debug",
816                                 S_IWUSR | S_IRUSR,
817                                 sc->debug.debugfs_phydir, sc, &fops_debug);
818
819         sc->debug.debugfs_registers = debugfs_create_file("registers", S_IRUSR,
820                                 sc->debug.debugfs_phydir, sc, &fops_registers);
821
822         sc->debug.debugfs_beacon = debugfs_create_file("beacon",
823                                 S_IWUSR | S_IRUSR,
824                                 sc->debug.debugfs_phydir, sc, &fops_beacon);
825
826         sc->debug.debugfs_reset = debugfs_create_file("reset", S_IWUSR,
827                                 sc->debug.debugfs_phydir, sc, &fops_reset);
828
829         sc->debug.debugfs_antenna = debugfs_create_file("antenna",
830                                 S_IWUSR | S_IRUSR,
831                                 sc->debug.debugfs_phydir, sc, &fops_antenna);
832
833         sc->debug.debugfs_frameerrors = debugfs_create_file("frameerrors",
834                                 S_IWUSR | S_IRUSR,
835                                 sc->debug.debugfs_phydir, sc,
836                                 &fops_frameerrors);
837
838         sc->debug.debugfs_ani = debugfs_create_file("ani",
839                                 S_IWUSR | S_IRUSR,
840                                 sc->debug.debugfs_phydir, sc,
841                                 &fops_ani);
842
843         sc->debug.debugfs_queue = debugfs_create_file("queue",
844                                 S_IWUSR | S_IRUSR,
845                                 sc->debug.debugfs_phydir, sc,
846                                 &fops_queue);
847 }
848
849 void
850 ath5k_debug_finish(void)
851 {
852         debugfs_remove(ath5k_global_debugfs);
853 }
854
855 void
856 ath5k_debug_finish_device(struct ath5k_softc *sc)
857 {
858         debugfs_remove(sc->debug.debugfs_debug);
859         debugfs_remove(sc->debug.debugfs_registers);
860         debugfs_remove(sc->debug.debugfs_beacon);
861         debugfs_remove(sc->debug.debugfs_reset);
862         debugfs_remove(sc->debug.debugfs_antenna);
863         debugfs_remove(sc->debug.debugfs_frameerrors);
864         debugfs_remove(sc->debug.debugfs_ani);
865         debugfs_remove(sc->debug.debugfs_queue);
866         debugfs_remove(sc->debug.debugfs_phydir);
867 }
868
869
870 /* functions used in other places */
871
872 void
873 ath5k_debug_dump_bands(struct ath5k_softc *sc)
874 {
875         unsigned int b, i;
876
877         if (likely(!(sc->debug.level & ATH5K_DEBUG_DUMPBANDS)))
878                 return;
879
880         BUG_ON(!sc->sbands);
881
882         for (b = 0; b < IEEE80211_NUM_BANDS; b++) {
883                 struct ieee80211_supported_band *band = &sc->sbands[b];
884                 char bname[6];
885                 switch (band->band) {
886                 case IEEE80211_BAND_2GHZ:
887                         strcpy(bname, "2 GHz");
888                         break;
889                 case IEEE80211_BAND_5GHZ:
890                         strcpy(bname, "5 GHz");
891                         break;
892                 default:
893                         printk(KERN_DEBUG "Band not supported: %d\n",
894                                 band->band);
895                         return;
896                 }
897                 printk(KERN_DEBUG "Band %s: channels %d, rates %d\n", bname,
898                                 band->n_channels, band->n_bitrates);
899                 printk(KERN_DEBUG " channels:\n");
900                 for (i = 0; i < band->n_channels; i++)
901                         printk(KERN_DEBUG "  %3d %d %.4x %.4x\n",
902                                         ieee80211_frequency_to_channel(
903                                                 band->channels[i].center_freq),
904                                         band->channels[i].center_freq,
905                                         band->channels[i].hw_value,
906                                         band->channels[i].flags);
907                 printk(KERN_DEBUG " rates:\n");
908                 for (i = 0; i < band->n_bitrates; i++)
909                         printk(KERN_DEBUG "  %4d %.4x %.4x %.4x\n",
910                                         band->bitrates[i].bitrate,
911                                         band->bitrates[i].hw_value,
912                                         band->bitrates[i].flags,
913                                         band->bitrates[i].hw_value_short);
914         }
915 }
916
917 static inline void
918 ath5k_debug_printrxbuf(struct ath5k_buf *bf, int done,
919                        struct ath5k_rx_status *rs)
920 {
921         struct ath5k_desc *ds = bf->desc;
922         struct ath5k_hw_all_rx_desc *rd = &ds->ud.ds_rx;
923
924         printk(KERN_DEBUG "R (%p %llx) %08x %08x %08x %08x %08x %08x %c\n",
925                 ds, (unsigned long long)bf->daddr,
926                 ds->ds_link, ds->ds_data,
927                 rd->rx_ctl.rx_control_0, rd->rx_ctl.rx_control_1,
928                 rd->u.rx_stat.rx_status_0, rd->u.rx_stat.rx_status_1,
929                 !done ? ' ' : (rs->rs_status == 0) ? '*' : '!');
930 }
931
932 void
933 ath5k_debug_printrxbuffs(struct ath5k_softc *sc, struct ath5k_hw *ah)
934 {
935         struct ath5k_desc *ds;
936         struct ath5k_buf *bf;
937         struct ath5k_rx_status rs = {};
938         int status;
939
940         if (likely(!(sc->debug.level & ATH5K_DEBUG_RESET)))
941                 return;
942
943         printk(KERN_DEBUG "rxdp %x, rxlink %p\n",
944                 ath5k_hw_get_rxdp(ah), sc->rxlink);
945
946         spin_lock_bh(&sc->rxbuflock);
947         list_for_each_entry(bf, &sc->rxbuf, list) {
948                 ds = bf->desc;
949                 status = ah->ah_proc_rx_desc(ah, ds, &rs);
950                 if (!status)
951                         ath5k_debug_printrxbuf(bf, status == 0, &rs);
952         }
953         spin_unlock_bh(&sc->rxbuflock);
954 }
955
956 void
957 ath5k_debug_dump_skb(struct ath5k_softc *sc,
958                         struct sk_buff *skb, const char *prefix, int tx)
959 {
960         char buf[16];
961
962         if (likely(!((tx && (sc->debug.level & ATH5K_DEBUG_DUMP_TX)) ||
963                      (!tx && (sc->debug.level & ATH5K_DEBUG_DUMP_RX)))))
964                 return;
965
966         snprintf(buf, sizeof(buf), "%s %s", wiphy_name(sc->hw->wiphy), prefix);
967
968         print_hex_dump_bytes(buf, DUMP_PREFIX_NONE, skb->data,
969                 min(200U, skb->len));
970
971         printk(KERN_DEBUG "\n");
972 }
973
974 void
975 ath5k_debug_printtxbuf(struct ath5k_softc *sc, struct ath5k_buf *bf)
976 {
977         struct ath5k_desc *ds = bf->desc;
978         struct ath5k_hw_5212_tx_desc *td = &ds->ud.ds_tx5212;
979         struct ath5k_tx_status ts = {};
980         int done;
981
982         if (likely(!(sc->debug.level & ATH5K_DEBUG_RESET)))
983                 return;
984
985         done = sc->ah->ah_proc_tx_desc(sc->ah, bf->desc, &ts);
986
987         printk(KERN_DEBUG "T (%p %llx) %08x %08x %08x %08x %08x %08x %08x "
988                 "%08x %c\n", ds, (unsigned long long)bf->daddr, ds->ds_link,
989                 ds->ds_data, td->tx_ctl.tx_control_0, td->tx_ctl.tx_control_1,
990                 td->tx_ctl.tx_control_2, td->tx_ctl.tx_control_3,
991                 td->tx_stat.tx_status_0, td->tx_stat.tx_status_1,
992                 done ? ' ' : (ts.ts_status == 0) ? '*' : '!');
993 }
994
995 #endif /* ifdef CONFIG_ATH5K_DEBUG */