Merge tag 'tty-3.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath9k / debug.c
1 /*
2  * Copyright (c) 2008-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/export.h>
20 #include <asm/unaligned.h>
21
22 #include "ath9k.h"
23
24 #define REG_WRITE_D(_ah, _reg, _val) \
25         ath9k_hw_common(_ah)->ops->write((_ah), (_val), (_reg))
26 #define REG_READ_D(_ah, _reg) \
27         ath9k_hw_common(_ah)->ops->read((_ah), (_reg))
28
29 void ath9k_debug_sync_cause(struct ath_softc *sc, u32 sync_cause)
30 {
31         if (sync_cause)
32                 sc->debug.stats.istats.sync_cause_all++;
33         if (sync_cause & AR_INTR_SYNC_RTC_IRQ)
34                 sc->debug.stats.istats.sync_rtc_irq++;
35         if (sync_cause & AR_INTR_SYNC_MAC_IRQ)
36                 sc->debug.stats.istats.sync_mac_irq++;
37         if (sync_cause & AR_INTR_SYNC_EEPROM_ILLEGAL_ACCESS)
38                 sc->debug.stats.istats.eeprom_illegal_access++;
39         if (sync_cause & AR_INTR_SYNC_APB_TIMEOUT)
40                 sc->debug.stats.istats.apb_timeout++;
41         if (sync_cause & AR_INTR_SYNC_PCI_MODE_CONFLICT)
42                 sc->debug.stats.istats.pci_mode_conflict++;
43         if (sync_cause & AR_INTR_SYNC_HOST1_FATAL)
44                 sc->debug.stats.istats.host1_fatal++;
45         if (sync_cause & AR_INTR_SYNC_HOST1_PERR)
46                 sc->debug.stats.istats.host1_perr++;
47         if (sync_cause & AR_INTR_SYNC_TRCV_FIFO_PERR)
48                 sc->debug.stats.istats.trcv_fifo_perr++;
49         if (sync_cause & AR_INTR_SYNC_RADM_CPL_EP)
50                 sc->debug.stats.istats.radm_cpl_ep++;
51         if (sync_cause & AR_INTR_SYNC_RADM_CPL_DLLP_ABORT)
52                 sc->debug.stats.istats.radm_cpl_dllp_abort++;
53         if (sync_cause & AR_INTR_SYNC_RADM_CPL_TLP_ABORT)
54                 sc->debug.stats.istats.radm_cpl_tlp_abort++;
55         if (sync_cause & AR_INTR_SYNC_RADM_CPL_ECRC_ERR)
56                 sc->debug.stats.istats.radm_cpl_ecrc_err++;
57         if (sync_cause & AR_INTR_SYNC_RADM_CPL_TIMEOUT)
58                 sc->debug.stats.istats.radm_cpl_timeout++;
59         if (sync_cause & AR_INTR_SYNC_LOCAL_TIMEOUT)
60                 sc->debug.stats.istats.local_timeout++;
61         if (sync_cause & AR_INTR_SYNC_PM_ACCESS)
62                 sc->debug.stats.istats.pm_access++;
63         if (sync_cause & AR_INTR_SYNC_MAC_AWAKE)
64                 sc->debug.stats.istats.mac_awake++;
65         if (sync_cause & AR_INTR_SYNC_MAC_ASLEEP)
66                 sc->debug.stats.istats.mac_asleep++;
67         if (sync_cause & AR_INTR_SYNC_MAC_SLEEP_ACCESS)
68                 sc->debug.stats.istats.mac_sleep_access++;
69 }
70
71 static ssize_t ath9k_debugfs_read_buf(struct file *file, char __user *user_buf,
72                                       size_t count, loff_t *ppos)
73 {
74         u8 *buf = file->private_data;
75         return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
76 }
77
78 static int ath9k_debugfs_release_buf(struct inode *inode, struct file *file)
79 {
80         vfree(file->private_data);
81         return 0;
82 }
83
84 #ifdef CONFIG_ATH_DEBUG
85
86 static ssize_t read_file_debug(struct file *file, char __user *user_buf,
87                              size_t count, loff_t *ppos)
88 {
89         struct ath_softc *sc = file->private_data;
90         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
91         char buf[32];
92         unsigned int len;
93
94         len = sprintf(buf, "0x%08x\n", common->debug_mask);
95         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
96 }
97
98 static ssize_t write_file_debug(struct file *file, const char __user *user_buf,
99                              size_t count, loff_t *ppos)
100 {
101         struct ath_softc *sc = file->private_data;
102         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
103         unsigned long mask;
104         char buf[32];
105         ssize_t len;
106
107         len = min(count, sizeof(buf) - 1);
108         if (copy_from_user(buf, user_buf, len))
109                 return -EFAULT;
110
111         buf[len] = '\0';
112         if (kstrtoul(buf, 0, &mask))
113                 return -EINVAL;
114
115         common->debug_mask = mask;
116         return count;
117 }
118
119 static const struct file_operations fops_debug = {
120         .read = read_file_debug,
121         .write = write_file_debug,
122         .open = simple_open,
123         .owner = THIS_MODULE,
124         .llseek = default_llseek,
125 };
126
127 #endif
128
129 #define DMA_BUF_LEN 1024
130
131
132 static ssize_t read_file_ani(struct file *file, char __user *user_buf,
133                              size_t count, loff_t *ppos)
134 {
135         struct ath_softc *sc = file->private_data;
136         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
137         struct ath_hw *ah = sc->sc_ah;
138         unsigned int len = 0;
139         const unsigned int size = 1024;
140         ssize_t retval = 0;
141         char *buf;
142         int i;
143         struct {
144                 const char *name;
145                 unsigned int val;
146         } ani_info[] = {
147                 { "ANI RESET", ah->stats.ast_ani_reset },
148                 { "OFDM LEVEL", ah->ani.ofdmNoiseImmunityLevel },
149                 { "CCK LEVEL", ah->ani.cckNoiseImmunityLevel },
150                 { "SPUR UP", ah->stats.ast_ani_spurup },
151                 { "SPUR DOWN", ah->stats.ast_ani_spurup },
152                 { "OFDM WS-DET ON", ah->stats.ast_ani_ofdmon },
153                 { "OFDM WS-DET OFF", ah->stats.ast_ani_ofdmoff },
154                 { "MRC-CCK ON", ah->stats.ast_ani_ccklow },
155                 { "MRC-CCK OFF", ah->stats.ast_ani_cckhigh },
156                 { "FIR-STEP UP", ah->stats.ast_ani_stepup },
157                 { "FIR-STEP DOWN", ah->stats.ast_ani_stepdown },
158                 { "INV LISTENTIME", ah->stats.ast_ani_lneg_or_lzero },
159                 { "OFDM ERRORS", ah->stats.ast_ani_ofdmerrs },
160                 { "CCK ERRORS", ah->stats.ast_ani_cckerrs },
161         };
162
163         buf = kzalloc(size, GFP_KERNEL);
164         if (buf == NULL)
165                 return -ENOMEM;
166
167         len += scnprintf(buf + len, size - len, "%15s: %s\n", "ANI",
168                          common->disable_ani ? "DISABLED" : "ENABLED");
169
170         if (common->disable_ani)
171                 goto exit;
172
173         for (i = 0; i < ARRAY_SIZE(ani_info); i++)
174                 len += scnprintf(buf + len, size - len, "%15s: %u\n",
175                                  ani_info[i].name, ani_info[i].val);
176
177 exit:
178         if (len > size)
179                 len = size;
180
181         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
182         kfree(buf);
183
184         return retval;
185 }
186
187 static ssize_t write_file_ani(struct file *file,
188                               const char __user *user_buf,
189                               size_t count, loff_t *ppos)
190 {
191         struct ath_softc *sc = file->private_data;
192         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
193         unsigned long ani;
194         char buf[32];
195         ssize_t len;
196
197         len = min(count, sizeof(buf) - 1);
198         if (copy_from_user(buf, user_buf, len))
199                 return -EFAULT;
200
201         buf[len] = '\0';
202         if (kstrtoul(buf, 0, &ani))
203                 return -EINVAL;
204
205         if (ani > 1)
206                 return -EINVAL;
207
208         common->disable_ani = !ani;
209
210         if (common->disable_ani) {
211                 clear_bit(ATH_OP_ANI_RUN, &common->op_flags);
212                 ath_stop_ani(sc);
213         } else {
214                 ath_check_ani(sc);
215         }
216
217         return count;
218 }
219
220 static const struct file_operations fops_ani = {
221         .read = read_file_ani,
222         .write = write_file_ani,
223         .open = simple_open,
224         .owner = THIS_MODULE,
225         .llseek = default_llseek,
226 };
227
228 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
229
230 static ssize_t read_file_bt_ant_diversity(struct file *file,
231                                           char __user *user_buf,
232                                           size_t count, loff_t *ppos)
233 {
234         struct ath_softc *sc = file->private_data;
235         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
236         char buf[32];
237         unsigned int len;
238
239         len = sprintf(buf, "%d\n", common->bt_ant_diversity);
240         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
241 }
242
243 static ssize_t write_file_bt_ant_diversity(struct file *file,
244                                            const char __user *user_buf,
245                                            size_t count, loff_t *ppos)
246 {
247         struct ath_softc *sc = file->private_data;
248         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
249         struct ath9k_hw_capabilities *pCap = &sc->sc_ah->caps;
250         unsigned long bt_ant_diversity;
251         char buf[32];
252         ssize_t len;
253
254         len = min(count, sizeof(buf) - 1);
255         if (copy_from_user(buf, user_buf, len))
256                 return -EFAULT;
257
258         if (!(pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV))
259                 goto exit;
260
261         buf[len] = '\0';
262         if (kstrtoul(buf, 0, &bt_ant_diversity))
263                 return -EINVAL;
264
265         common->bt_ant_diversity = !!bt_ant_diversity;
266         ath9k_ps_wakeup(sc);
267         ath9k_hw_set_bt_ant_diversity(sc->sc_ah, common->bt_ant_diversity);
268         ath_dbg(common, CONFIG, "Enable WLAN/BT RX Antenna diversity: %d\n",
269                 common->bt_ant_diversity);
270         ath9k_ps_restore(sc);
271 exit:
272         return count;
273 }
274
275 static const struct file_operations fops_bt_ant_diversity = {
276         .read = read_file_bt_ant_diversity,
277         .write = write_file_bt_ant_diversity,
278         .open = simple_open,
279         .owner = THIS_MODULE,
280         .llseek = default_llseek,
281 };
282
283 #endif
284
285 void ath9k_debug_stat_ant(struct ath_softc *sc,
286                           struct ath_hw_antcomb_conf *div_ant_conf,
287                           int main_rssi_avg, int alt_rssi_avg)
288 {
289         struct ath_antenna_stats *as_main = &sc->debug.stats.ant_stats[ANT_MAIN];
290         struct ath_antenna_stats *as_alt = &sc->debug.stats.ant_stats[ANT_ALT];
291
292         as_main->lna_attempt_cnt[div_ant_conf->main_lna_conf]++;
293         as_alt->lna_attempt_cnt[div_ant_conf->alt_lna_conf]++;
294
295         as_main->rssi_avg = main_rssi_avg;
296         as_alt->rssi_avg = alt_rssi_avg;
297 }
298
299 static ssize_t read_file_antenna_diversity(struct file *file,
300                                            char __user *user_buf,
301                                            size_t count, loff_t *ppos)
302 {
303         struct ath_softc *sc = file->private_data;
304         struct ath_hw *ah = sc->sc_ah;
305         struct ath9k_hw_capabilities *pCap = &ah->caps;
306         struct ath_antenna_stats *as_main = &sc->debug.stats.ant_stats[ANT_MAIN];
307         struct ath_antenna_stats *as_alt = &sc->debug.stats.ant_stats[ANT_ALT];
308         struct ath_hw_antcomb_conf div_ant_conf;
309         unsigned int len = 0;
310         const unsigned int size = 1024;
311         ssize_t retval = 0;
312         char *buf;
313         static const char *lna_conf_str[4] = {
314                 "LNA1_MINUS_LNA2", "LNA2", "LNA1", "LNA1_PLUS_LNA2"
315         };
316
317         buf = kzalloc(size, GFP_KERNEL);
318         if (buf == NULL)
319                 return -ENOMEM;
320
321         if (!(pCap->hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)) {
322                 len += scnprintf(buf + len, size - len, "%s\n",
323                                  "Antenna Diversity Combining is disabled");
324                 goto exit;
325         }
326
327         ath9k_ps_wakeup(sc);
328         ath9k_hw_antdiv_comb_conf_get(ah, &div_ant_conf);
329         len += scnprintf(buf + len, size - len, "Current MAIN config : %s\n",
330                          lna_conf_str[div_ant_conf.main_lna_conf]);
331         len += scnprintf(buf + len, size - len, "Current ALT config  : %s\n",
332                          lna_conf_str[div_ant_conf.alt_lna_conf]);
333         len += scnprintf(buf + len, size - len, "Average MAIN RSSI   : %d\n",
334                          as_main->rssi_avg);
335         len += scnprintf(buf + len, size - len, "Average ALT RSSI    : %d\n\n",
336                          as_alt->rssi_avg);
337         ath9k_ps_restore(sc);
338
339         len += scnprintf(buf + len, size - len, "Packet Receive Cnt:\n");
340         len += scnprintf(buf + len, size - len, "-------------------\n");
341
342         len += scnprintf(buf + len, size - len, "%30s%15s\n",
343                          "MAIN", "ALT");
344         len += scnprintf(buf + len, size - len, "%-14s:%15d%15d\n",
345                          "TOTAL COUNT",
346                          as_main->recv_cnt,
347                          as_alt->recv_cnt);
348         len += scnprintf(buf + len, size - len, "%-14s:%15d%15d\n",
349                          "LNA1",
350                          as_main->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1],
351                          as_alt->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1]);
352         len += scnprintf(buf + len, size - len, "%-14s:%15d%15d\n",
353                          "LNA2",
354                          as_main->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA2],
355                          as_alt->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA2]);
356         len += scnprintf(buf + len, size - len, "%-14s:%15d%15d\n",
357                          "LNA1 + LNA2",
358                          as_main->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2],
359                          as_alt->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2]);
360         len += scnprintf(buf + len, size - len, "%-14s:%15d%15d\n",
361                          "LNA1 - LNA2",
362                          as_main->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2],
363                          as_alt->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2]);
364
365         len += scnprintf(buf + len, size - len, "\nLNA Config Attempts:\n");
366         len += scnprintf(buf + len, size - len, "--------------------\n");
367
368         len += scnprintf(buf + len, size - len, "%30s%15s\n",
369                          "MAIN", "ALT");
370         len += scnprintf(buf + len, size - len, "%-14s:%15d%15d\n",
371                          "LNA1",
372                          as_main->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1],
373                          as_alt->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1]);
374         len += scnprintf(buf + len, size - len, "%-14s:%15d%15d\n",
375                          "LNA2",
376                          as_main->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA2],
377                          as_alt->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA2]);
378         len += scnprintf(buf + len, size - len, "%-14s:%15d%15d\n",
379                          "LNA1 + LNA2",
380                          as_main->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2],
381                          as_alt->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2]);
382         len += scnprintf(buf + len, size - len, "%-14s:%15d%15d\n",
383                          "LNA1 - LNA2",
384                          as_main->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2],
385                          as_alt->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2]);
386
387 exit:
388         if (len > size)
389                 len = size;
390
391         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
392         kfree(buf);
393
394         return retval;
395 }
396
397 static const struct file_operations fops_antenna_diversity = {
398         .read = read_file_antenna_diversity,
399         .open = simple_open,
400         .owner = THIS_MODULE,
401         .llseek = default_llseek,
402 };
403
404 static ssize_t read_file_dma(struct file *file, char __user *user_buf,
405                              size_t count, loff_t *ppos)
406 {
407         struct ath_softc *sc = file->private_data;
408         struct ath_hw *ah = sc->sc_ah;
409         char *buf;
410         int retval;
411         unsigned int len = 0;
412         u32 val[ATH9K_NUM_DMA_DEBUG_REGS];
413         int i, qcuOffset = 0, dcuOffset = 0;
414         u32 *qcuBase = &val[0], *dcuBase = &val[4];
415
416         buf = kmalloc(DMA_BUF_LEN, GFP_KERNEL);
417         if (!buf)
418                 return -ENOMEM;
419
420         ath9k_ps_wakeup(sc);
421
422         REG_WRITE_D(ah, AR_MACMISC,
423                   ((AR_MACMISC_DMA_OBS_LINE_8 << AR_MACMISC_DMA_OBS_S) |
424                    (AR_MACMISC_MISC_OBS_BUS_1 <<
425                     AR_MACMISC_MISC_OBS_BUS_MSB_S)));
426
427         len += scnprintf(buf + len, DMA_BUF_LEN - len,
428                          "Raw DMA Debug values:\n");
429
430         for (i = 0; i < ATH9K_NUM_DMA_DEBUG_REGS; i++) {
431                 if (i % 4 == 0)
432                         len += scnprintf(buf + len, DMA_BUF_LEN - len, "\n");
433
434                 val[i] = REG_READ_D(ah, AR_DMADBG_0 + (i * sizeof(u32)));
435                 len += scnprintf(buf + len, DMA_BUF_LEN - len, "%d: %08x ",
436                                  i, val[i]);
437         }
438
439         len += scnprintf(buf + len, DMA_BUF_LEN - len, "\n\n");
440         len += scnprintf(buf + len, DMA_BUF_LEN - len,
441                          "Num QCU: chain_st fsp_ok fsp_st DCU: chain_st\n");
442
443         for (i = 0; i < ATH9K_NUM_QUEUES; i++, qcuOffset += 4, dcuOffset += 5) {
444                 if (i == 8) {
445                         qcuOffset = 0;
446                         qcuBase++;
447                 }
448
449                 if (i == 6) {
450                         dcuOffset = 0;
451                         dcuBase++;
452                 }
453
454                 len += scnprintf(buf + len, DMA_BUF_LEN - len,
455                          "%2d          %2x      %1x     %2x           %2x\n",
456                          i, (*qcuBase & (0x7 << qcuOffset)) >> qcuOffset,
457                          (*qcuBase & (0x8 << qcuOffset)) >> (qcuOffset + 3),
458                          (val[2] & (0x7 << (i * 3))) >> (i * 3),
459                          (*dcuBase & (0x1f << dcuOffset)) >> dcuOffset);
460         }
461
462         len += scnprintf(buf + len, DMA_BUF_LEN - len, "\n");
463
464         len += scnprintf(buf + len, DMA_BUF_LEN - len,
465                 "qcu_stitch state:   %2x    qcu_fetch state:        %2x\n",
466                 (val[3] & 0x003c0000) >> 18, (val[3] & 0x03c00000) >> 22);
467         len += scnprintf(buf + len, DMA_BUF_LEN - len,
468                 "qcu_complete state: %2x    dcu_complete state:     %2x\n",
469                 (val[3] & 0x1c000000) >> 26, (val[6] & 0x3));
470         len += scnprintf(buf + len, DMA_BUF_LEN - len,
471                 "dcu_arb state:      %2x    dcu_fp state:           %2x\n",
472                 (val[5] & 0x06000000) >> 25, (val[5] & 0x38000000) >> 27);
473         len += scnprintf(buf + len, DMA_BUF_LEN - len,
474                 "chan_idle_dur:     %3d    chan_idle_dur_valid:     %1d\n",
475                 (val[6] & 0x000003fc) >> 2, (val[6] & 0x00000400) >> 10);
476         len += scnprintf(buf + len, DMA_BUF_LEN - len,
477                 "txfifo_valid_0:      %1d    txfifo_valid_1:          %1d\n",
478                 (val[6] & 0x00000800) >> 11, (val[6] & 0x00001000) >> 12);
479         len += scnprintf(buf + len, DMA_BUF_LEN - len,
480                 "txfifo_dcu_num_0:   %2d    txfifo_dcu_num_1:       %2d\n",
481                 (val[6] & 0x0001e000) >> 13, (val[6] & 0x001e0000) >> 17);
482
483         len += scnprintf(buf + len, DMA_BUF_LEN - len, "pcu observe: 0x%x\n",
484                          REG_READ_D(ah, AR_OBS_BUS_1));
485         len += scnprintf(buf + len, DMA_BUF_LEN - len,
486                          "AR_CR: 0x%x\n", REG_READ_D(ah, AR_CR));
487
488         ath9k_ps_restore(sc);
489
490         if (len > DMA_BUF_LEN)
491                 len = DMA_BUF_LEN;
492
493         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
494         kfree(buf);
495         return retval;
496 }
497
498 static const struct file_operations fops_dma = {
499         .read = read_file_dma,
500         .open = simple_open,
501         .owner = THIS_MODULE,
502         .llseek = default_llseek,
503 };
504
505
506 void ath_debug_stat_interrupt(struct ath_softc *sc, enum ath9k_int status)
507 {
508         if (status)
509                 sc->debug.stats.istats.total++;
510         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
511                 if (status & ATH9K_INT_RXLP)
512                         sc->debug.stats.istats.rxlp++;
513                 if (status & ATH9K_INT_RXHP)
514                         sc->debug.stats.istats.rxhp++;
515                 if (status & ATH9K_INT_BB_WATCHDOG)
516                         sc->debug.stats.istats.bb_watchdog++;
517         } else {
518                 if (status & ATH9K_INT_RX)
519                         sc->debug.stats.istats.rxok++;
520         }
521         if (status & ATH9K_INT_RXEOL)
522                 sc->debug.stats.istats.rxeol++;
523         if (status & ATH9K_INT_RXORN)
524                 sc->debug.stats.istats.rxorn++;
525         if (status & ATH9K_INT_TX)
526                 sc->debug.stats.istats.txok++;
527         if (status & ATH9K_INT_TXURN)
528                 sc->debug.stats.istats.txurn++;
529         if (status & ATH9K_INT_RXPHY)
530                 sc->debug.stats.istats.rxphyerr++;
531         if (status & ATH9K_INT_RXKCM)
532                 sc->debug.stats.istats.rx_keycache_miss++;
533         if (status & ATH9K_INT_SWBA)
534                 sc->debug.stats.istats.swba++;
535         if (status & ATH9K_INT_BMISS)
536                 sc->debug.stats.istats.bmiss++;
537         if (status & ATH9K_INT_BNR)
538                 sc->debug.stats.istats.bnr++;
539         if (status & ATH9K_INT_CST)
540                 sc->debug.stats.istats.cst++;
541         if (status & ATH9K_INT_GTT)
542                 sc->debug.stats.istats.gtt++;
543         if (status & ATH9K_INT_TIM)
544                 sc->debug.stats.istats.tim++;
545         if (status & ATH9K_INT_CABEND)
546                 sc->debug.stats.istats.cabend++;
547         if (status & ATH9K_INT_DTIMSYNC)
548                 sc->debug.stats.istats.dtimsync++;
549         if (status & ATH9K_INT_DTIM)
550                 sc->debug.stats.istats.dtim++;
551         if (status & ATH9K_INT_TSFOOR)
552                 sc->debug.stats.istats.tsfoor++;
553         if (status & ATH9K_INT_MCI)
554                 sc->debug.stats.istats.mci++;
555         if (status & ATH9K_INT_GENTIMER)
556                 sc->debug.stats.istats.gen_timer++;
557 }
558
559 static ssize_t read_file_interrupt(struct file *file, char __user *user_buf,
560                                    size_t count, loff_t *ppos)
561 {
562         struct ath_softc *sc = file->private_data;
563         unsigned int len = 0;
564         int rv;
565         int mxlen = 4000;
566         char *buf = kmalloc(mxlen, GFP_KERNEL);
567         if (!buf)
568                 return -ENOMEM;
569
570 #define PR_IS(a, s)                                             \
571         do {                                                    \
572                 len += scnprintf(buf + len, mxlen - len,        \
573                                  "%21s: %10u\n", a,             \
574                                  sc->debug.stats.istats.s);     \
575         } while (0)
576
577         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
578                 PR_IS("RXLP", rxlp);
579                 PR_IS("RXHP", rxhp);
580                 PR_IS("WATHDOG", bb_watchdog);
581         } else {
582                 PR_IS("RX", rxok);
583         }
584         PR_IS("RXEOL", rxeol);
585         PR_IS("RXORN", rxorn);
586         PR_IS("TX", txok);
587         PR_IS("TXURN", txurn);
588         PR_IS("MIB", mib);
589         PR_IS("RXPHY", rxphyerr);
590         PR_IS("RXKCM", rx_keycache_miss);
591         PR_IS("SWBA", swba);
592         PR_IS("BMISS", bmiss);
593         PR_IS("BNR", bnr);
594         PR_IS("CST", cst);
595         PR_IS("GTT", gtt);
596         PR_IS("TIM", tim);
597         PR_IS("CABEND", cabend);
598         PR_IS("DTIMSYNC", dtimsync);
599         PR_IS("DTIM", dtim);
600         PR_IS("TSFOOR", tsfoor);
601         PR_IS("MCI", mci);
602         PR_IS("GENTIMER", gen_timer);
603         PR_IS("TOTAL", total);
604
605         len += scnprintf(buf + len, mxlen - len,
606                          "SYNC_CAUSE stats:\n");
607
608         PR_IS("Sync-All", sync_cause_all);
609         PR_IS("RTC-IRQ", sync_rtc_irq);
610         PR_IS("MAC-IRQ", sync_mac_irq);
611         PR_IS("EEPROM-Illegal-Access", eeprom_illegal_access);
612         PR_IS("APB-Timeout", apb_timeout);
613         PR_IS("PCI-Mode-Conflict", pci_mode_conflict);
614         PR_IS("HOST1-Fatal", host1_fatal);
615         PR_IS("HOST1-Perr", host1_perr);
616         PR_IS("TRCV-FIFO-Perr", trcv_fifo_perr);
617         PR_IS("RADM-CPL-EP", radm_cpl_ep);
618         PR_IS("RADM-CPL-DLLP-Abort", radm_cpl_dllp_abort);
619         PR_IS("RADM-CPL-TLP-Abort", radm_cpl_tlp_abort);
620         PR_IS("RADM-CPL-ECRC-Err", radm_cpl_ecrc_err);
621         PR_IS("RADM-CPL-Timeout", radm_cpl_timeout);
622         PR_IS("Local-Bus-Timeout", local_timeout);
623         PR_IS("PM-Access", pm_access);
624         PR_IS("MAC-Awake", mac_awake);
625         PR_IS("MAC-Asleep", mac_asleep);
626         PR_IS("MAC-Sleep-Access", mac_sleep_access);
627
628         if (len > mxlen)
629                 len = mxlen;
630
631         rv = simple_read_from_buffer(user_buf, count, ppos, buf, len);
632         kfree(buf);
633         return rv;
634 }
635
636 static const struct file_operations fops_interrupt = {
637         .read = read_file_interrupt,
638         .open = simple_open,
639         .owner = THIS_MODULE,
640         .llseek = default_llseek,
641 };
642
643 static ssize_t read_file_xmit(struct file *file, char __user *user_buf,
644                               size_t count, loff_t *ppos)
645 {
646         struct ath_softc *sc = file->private_data;
647         char *buf;
648         unsigned int len = 0, size = 2048;
649         ssize_t retval = 0;
650
651         buf = kzalloc(size, GFP_KERNEL);
652         if (buf == NULL)
653                 return -ENOMEM;
654
655         len += sprintf(buf, "%30s %10s%10s%10s\n\n",
656                        "BE", "BK", "VI", "VO");
657
658         PR("MPDUs Queued:    ", queued);
659         PR("MPDUs Completed: ", completed);
660         PR("MPDUs XRetried:  ", xretries);
661         PR("Aggregates:      ", a_aggr);
662         PR("AMPDUs Queued HW:", a_queued_hw);
663         PR("AMPDUs Queued SW:", a_queued_sw);
664         PR("AMPDUs Completed:", a_completed);
665         PR("AMPDUs Retried:  ", a_retries);
666         PR("AMPDUs XRetried: ", a_xretries);
667         PR("TXERR Filtered:  ", txerr_filtered);
668         PR("FIFO Underrun:   ", fifo_underrun);
669         PR("TXOP Exceeded:   ", xtxop);
670         PR("TXTIMER Expiry:  ", timer_exp);
671         PR("DESC CFG Error:  ", desc_cfg_err);
672         PR("DATA Underrun:   ", data_underrun);
673         PR("DELIM Underrun:  ", delim_underrun);
674         PR("TX-Pkts-All:     ", tx_pkts_all);
675         PR("TX-Bytes-All:    ", tx_bytes_all);
676         PR("HW-put-tx-buf:   ", puttxbuf);
677         PR("HW-tx-start:     ", txstart);
678         PR("HW-tx-proc-desc: ", txprocdesc);
679         PR("TX-Failed:       ", txfailed);
680
681         if (len > size)
682                 len = size;
683
684         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
685         kfree(buf);
686
687         return retval;
688 }
689
690 static ssize_t print_queue(struct ath_softc *sc, struct ath_txq *txq,
691                            char *buf, ssize_t size)
692 {
693         ssize_t len = 0;
694
695         ath_txq_lock(sc, txq);
696
697         len += scnprintf(buf + len, size - len, "%s: %d ",
698                          "qnum", txq->axq_qnum);
699         len += scnprintf(buf + len, size - len, "%s: %2d ",
700                          "qdepth", txq->axq_depth);
701         len += scnprintf(buf + len, size - len, "%s: %2d ",
702                          "ampdu-depth", txq->axq_ampdu_depth);
703         len += scnprintf(buf + len, size - len, "%s: %3d ",
704                          "pending", txq->pending_frames);
705         len += scnprintf(buf + len, size - len, "%s: %d\n",
706                          "stopped", txq->stopped);
707
708         ath_txq_unlock(sc, txq);
709         return len;
710 }
711
712 static ssize_t read_file_queues(struct file *file, char __user *user_buf,
713                                 size_t count, loff_t *ppos)
714 {
715         struct ath_softc *sc = file->private_data;
716         struct ath_txq *txq;
717         char *buf;
718         unsigned int len = 0;
719         const unsigned int size = 1024;
720         ssize_t retval = 0;
721         int i;
722         static const char *qname[4] = {
723                 "VO", "VI", "BE", "BK"
724         };
725
726         buf = kzalloc(size, GFP_KERNEL);
727         if (buf == NULL)
728                 return -ENOMEM;
729
730         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
731                 txq = sc->tx.txq_map[i];
732                 len += scnprintf(buf + len, size - len, "(%s):  ", qname[i]);
733                 len += print_queue(sc, txq, buf + len, size - len);
734         }
735
736         len += scnprintf(buf + len, size - len, "(CAB): ");
737         len += print_queue(sc, sc->beacon.cabq, buf + len, size - len);
738
739         if (len > size)
740                 len = size;
741
742         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
743         kfree(buf);
744
745         return retval;
746 }
747
748 static ssize_t read_file_misc(struct file *file, char __user *user_buf,
749                               size_t count, loff_t *ppos)
750 {
751         struct ath_softc *sc = file->private_data;
752         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
753         struct ath9k_vif_iter_data iter_data;
754         struct ath_chanctx *ctx;
755         char buf[512];
756         unsigned int len = 0;
757         ssize_t retval = 0;
758         unsigned int reg;
759         u32 rxfilter, i;
760
761         len += scnprintf(buf + len, sizeof(buf) - len,
762                          "BSSID: %pM\n", common->curbssid);
763         len += scnprintf(buf + len, sizeof(buf) - len,
764                          "BSSID-MASK: %pM\n", common->bssidmask);
765         len += scnprintf(buf + len, sizeof(buf) - len,
766                          "OPMODE: %s\n",
767                          ath_opmode_to_string(sc->sc_ah->opmode));
768
769         ath9k_ps_wakeup(sc);
770         rxfilter = ath9k_hw_getrxfilter(sc->sc_ah);
771         ath9k_ps_restore(sc);
772
773         len += scnprintf(buf + len, sizeof(buf) - len,
774                          "RXFILTER: 0x%x", rxfilter);
775
776         if (rxfilter & ATH9K_RX_FILTER_UCAST)
777                 len += scnprintf(buf + len, sizeof(buf) - len, " UCAST");
778         if (rxfilter & ATH9K_RX_FILTER_MCAST)
779                 len += scnprintf(buf + len, sizeof(buf) - len, " MCAST");
780         if (rxfilter & ATH9K_RX_FILTER_BCAST)
781                 len += scnprintf(buf + len, sizeof(buf) - len, " BCAST");
782         if (rxfilter & ATH9K_RX_FILTER_CONTROL)
783                 len += scnprintf(buf + len, sizeof(buf) - len, " CONTROL");
784         if (rxfilter & ATH9K_RX_FILTER_BEACON)
785                 len += scnprintf(buf + len, sizeof(buf) - len, " BEACON");
786         if (rxfilter & ATH9K_RX_FILTER_PROM)
787                 len += scnprintf(buf + len, sizeof(buf) - len, " PROM");
788         if (rxfilter & ATH9K_RX_FILTER_PROBEREQ)
789                 len += scnprintf(buf + len, sizeof(buf) - len, " PROBEREQ");
790         if (rxfilter & ATH9K_RX_FILTER_PHYERR)
791                 len += scnprintf(buf + len, sizeof(buf) - len, " PHYERR");
792         if (rxfilter & ATH9K_RX_FILTER_MYBEACON)
793                 len += scnprintf(buf + len, sizeof(buf) - len, " MYBEACON");
794         if (rxfilter & ATH9K_RX_FILTER_COMP_BAR)
795                 len += scnprintf(buf + len, sizeof(buf) - len, " COMP_BAR");
796         if (rxfilter & ATH9K_RX_FILTER_PSPOLL)
797                 len += scnprintf(buf + len, sizeof(buf) - len, " PSPOLL");
798         if (rxfilter & ATH9K_RX_FILTER_PHYRADAR)
799                 len += scnprintf(buf + len, sizeof(buf) - len, " PHYRADAR");
800         if (rxfilter & ATH9K_RX_FILTER_MCAST_BCAST_ALL)
801                 len += scnprintf(buf + len, sizeof(buf) - len, " MCAST_BCAST_ALL");
802         if (rxfilter & ATH9K_RX_FILTER_CONTROL_WRAPPER)
803                 len += scnprintf(buf + len, sizeof(buf) - len, " CONTROL_WRAPPER");
804
805         len += scnprintf(buf + len, sizeof(buf) - len, "\n");
806
807         reg = sc->sc_ah->imask;
808
809         len += scnprintf(buf + len, sizeof(buf) - len,
810                          "INTERRUPT-MASK: 0x%x", reg);
811
812         if (reg & ATH9K_INT_SWBA)
813                 len += scnprintf(buf + len, sizeof(buf) - len, " SWBA");
814         if (reg & ATH9K_INT_BMISS)
815                 len += scnprintf(buf + len, sizeof(buf) - len, " BMISS");
816         if (reg & ATH9K_INT_CST)
817                 len += scnprintf(buf + len, sizeof(buf) - len, " CST");
818         if (reg & ATH9K_INT_RX)
819                 len += scnprintf(buf + len, sizeof(buf) - len, " RX");
820         if (reg & ATH9K_INT_RXHP)
821                 len += scnprintf(buf + len, sizeof(buf) - len, " RXHP");
822         if (reg & ATH9K_INT_RXLP)
823                 len += scnprintf(buf + len, sizeof(buf) - len, " RXLP");
824         if (reg & ATH9K_INT_BB_WATCHDOG)
825                 len += scnprintf(buf + len, sizeof(buf) - len, " BB_WATCHDOG");
826
827         len += scnprintf(buf + len, sizeof(buf) - len, "\n");
828
829         i = 0;
830         ath_for_each_chanctx(sc, ctx) {
831                 if (list_empty(&ctx->vifs))
832                         continue;
833                 ath9k_calculate_iter_data(sc, ctx, &iter_data);
834
835                 len += scnprintf(buf + len, sizeof(buf) - len,
836                         "VIFS: CTX %i(%i) AP: %i STA: %i MESH: %i WDS: %i",
837                         i++, (int)(ctx->assigned), iter_data.naps,
838                         iter_data.nstations,
839                         iter_data.nmeshes, iter_data.nwds);
840                 len += scnprintf(buf + len, sizeof(buf) - len,
841                         " ADHOC: %i TOTAL: %hi BEACON-VIF: %hi\n",
842                         iter_data.nadhocs, sc->cur_chan->nvifs, sc->nbcnvifs);
843         }
844
845         if (len > sizeof(buf))
846                 len = sizeof(buf);
847
848         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
849         return retval;
850 }
851
852 static ssize_t read_file_reset(struct file *file, char __user *user_buf,
853                                size_t count, loff_t *ppos)
854 {
855         struct ath_softc *sc = file->private_data;
856         static const char * const reset_cause[__RESET_TYPE_MAX] = {
857                 [RESET_TYPE_BB_HANG] = "Baseband Hang",
858                 [RESET_TYPE_BB_WATCHDOG] = "Baseband Watchdog",
859                 [RESET_TYPE_FATAL_INT] = "Fatal HW Error",
860                 [RESET_TYPE_TX_ERROR] = "TX HW error",
861                 [RESET_TYPE_TX_GTT] = "Transmit timeout",
862                 [RESET_TYPE_TX_HANG] = "TX Path Hang",
863                 [RESET_TYPE_PLL_HANG] = "PLL RX Hang",
864                 [RESET_TYPE_MAC_HANG] = "MAC Hang",
865                 [RESET_TYPE_BEACON_STUCK] = "Stuck Beacon",
866                 [RESET_TYPE_MCI] = "MCI Reset",
867                 [RESET_TYPE_CALIBRATION] = "Calibration error",
868         };
869         char buf[512];
870         unsigned int len = 0;
871         int i;
872
873         for (i = 0; i < ARRAY_SIZE(reset_cause); i++) {
874                 if (!reset_cause[i])
875                     continue;
876
877                 len += scnprintf(buf + len, sizeof(buf) - len,
878                                  "%17s: %2d\n", reset_cause[i],
879                                  sc->debug.stats.reset[i]);
880         }
881
882         if (len > sizeof(buf))
883                 len = sizeof(buf);
884
885         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
886 }
887
888 void ath_debug_stat_tx(struct ath_softc *sc, struct ath_buf *bf,
889                        struct ath_tx_status *ts, struct ath_txq *txq,
890                        unsigned int flags)
891 {
892         int qnum = txq->axq_qnum;
893
894         TX_STAT_INC(qnum, tx_pkts_all);
895         sc->debug.stats.txstats[qnum].tx_bytes_all += bf->bf_mpdu->len;
896
897         if (bf_isampdu(bf)) {
898                 if (flags & ATH_TX_ERROR)
899                         TX_STAT_INC(qnum, a_xretries);
900                 else
901                         TX_STAT_INC(qnum, a_completed);
902         } else {
903                 if (ts->ts_status & ATH9K_TXERR_XRETRY)
904                         TX_STAT_INC(qnum, xretries);
905                 else
906                         TX_STAT_INC(qnum, completed);
907         }
908
909         if (ts->ts_status & ATH9K_TXERR_FILT)
910                 TX_STAT_INC(qnum, txerr_filtered);
911         if (ts->ts_status & ATH9K_TXERR_FIFO)
912                 TX_STAT_INC(qnum, fifo_underrun);
913         if (ts->ts_status & ATH9K_TXERR_XTXOP)
914                 TX_STAT_INC(qnum, xtxop);
915         if (ts->ts_status & ATH9K_TXERR_TIMER_EXPIRED)
916                 TX_STAT_INC(qnum, timer_exp);
917         if (ts->ts_flags & ATH9K_TX_DESC_CFG_ERR)
918                 TX_STAT_INC(qnum, desc_cfg_err);
919         if (ts->ts_flags & ATH9K_TX_DATA_UNDERRUN)
920                 TX_STAT_INC(qnum, data_underrun);
921         if (ts->ts_flags & ATH9K_TX_DELIM_UNDERRUN)
922                 TX_STAT_INC(qnum, delim_underrun);
923 }
924
925 static const struct file_operations fops_xmit = {
926         .read = read_file_xmit,
927         .open = simple_open,
928         .owner = THIS_MODULE,
929         .llseek = default_llseek,
930 };
931
932 static const struct file_operations fops_queues = {
933         .read = read_file_queues,
934         .open = simple_open,
935         .owner = THIS_MODULE,
936         .llseek = default_llseek,
937 };
938
939 static const struct file_operations fops_misc = {
940         .read = read_file_misc,
941         .open = simple_open,
942         .owner = THIS_MODULE,
943         .llseek = default_llseek,
944 };
945
946 static const struct file_operations fops_reset = {
947         .read = read_file_reset,
948         .open = simple_open,
949         .owner = THIS_MODULE,
950         .llseek = default_llseek,
951 };
952
953 void ath_debug_stat_rx(struct ath_softc *sc, struct ath_rx_status *rs)
954 {
955         ath9k_cmn_debug_stat_rx(&sc->debug.stats.rxstats, rs);
956 }
957
958 static ssize_t read_file_regidx(struct file *file, char __user *user_buf,
959                                 size_t count, loff_t *ppos)
960 {
961         struct ath_softc *sc = file->private_data;
962         char buf[32];
963         unsigned int len;
964
965         len = sprintf(buf, "0x%08x\n", sc->debug.regidx);
966         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
967 }
968
969 static ssize_t write_file_regidx(struct file *file, const char __user *user_buf,
970                              size_t count, loff_t *ppos)
971 {
972         struct ath_softc *sc = file->private_data;
973         unsigned long regidx;
974         char buf[32];
975         ssize_t len;
976
977         len = min(count, sizeof(buf) - 1);
978         if (copy_from_user(buf, user_buf, len))
979                 return -EFAULT;
980
981         buf[len] = '\0';
982         if (kstrtoul(buf, 0, &regidx))
983                 return -EINVAL;
984
985         sc->debug.regidx = regidx;
986         return count;
987 }
988
989 static const struct file_operations fops_regidx = {
990         .read = read_file_regidx,
991         .write = write_file_regidx,
992         .open = simple_open,
993         .owner = THIS_MODULE,
994         .llseek = default_llseek,
995 };
996
997 static ssize_t read_file_regval(struct file *file, char __user *user_buf,
998                              size_t count, loff_t *ppos)
999 {
1000         struct ath_softc *sc = file->private_data;
1001         struct ath_hw *ah = sc->sc_ah;
1002         char buf[32];
1003         unsigned int len;
1004         u32 regval;
1005
1006         ath9k_ps_wakeup(sc);
1007         regval = REG_READ_D(ah, sc->debug.regidx);
1008         ath9k_ps_restore(sc);
1009         len = sprintf(buf, "0x%08x\n", regval);
1010         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1011 }
1012
1013 static ssize_t write_file_regval(struct file *file, const char __user *user_buf,
1014                              size_t count, loff_t *ppos)
1015 {
1016         struct ath_softc *sc = file->private_data;
1017         struct ath_hw *ah = sc->sc_ah;
1018         unsigned long regval;
1019         char buf[32];
1020         ssize_t len;
1021
1022         len = min(count, sizeof(buf) - 1);
1023         if (copy_from_user(buf, user_buf, len))
1024                 return -EFAULT;
1025
1026         buf[len] = '\0';
1027         if (kstrtoul(buf, 0, &regval))
1028                 return -EINVAL;
1029
1030         ath9k_ps_wakeup(sc);
1031         REG_WRITE_D(ah, sc->debug.regidx, regval);
1032         ath9k_ps_restore(sc);
1033         return count;
1034 }
1035
1036 static const struct file_operations fops_regval = {
1037         .read = read_file_regval,
1038         .write = write_file_regval,
1039         .open = simple_open,
1040         .owner = THIS_MODULE,
1041         .llseek = default_llseek,
1042 };
1043
1044 #define REGDUMP_LINE_SIZE       20
1045
1046 static int open_file_regdump(struct inode *inode, struct file *file)
1047 {
1048         struct ath_softc *sc = inode->i_private;
1049         unsigned int len = 0;
1050         u8 *buf;
1051         int i;
1052         unsigned long num_regs, regdump_len, max_reg_offset;
1053
1054         max_reg_offset = AR_SREV_9300_20_OR_LATER(sc->sc_ah) ? 0x16bd4 : 0xb500;
1055         num_regs = max_reg_offset / 4 + 1;
1056         regdump_len = num_regs * REGDUMP_LINE_SIZE + 1;
1057         buf = vmalloc(regdump_len);
1058         if (!buf)
1059                 return -ENOMEM;
1060
1061         ath9k_ps_wakeup(sc);
1062         for (i = 0; i < num_regs; i++)
1063                 len += scnprintf(buf + len, regdump_len - len,
1064                         "0x%06x 0x%08x\n", i << 2, REG_READ(sc->sc_ah, i << 2));
1065         ath9k_ps_restore(sc);
1066
1067         file->private_data = buf;
1068
1069         return 0;
1070 }
1071
1072 static const struct file_operations fops_regdump = {
1073         .open = open_file_regdump,
1074         .read = ath9k_debugfs_read_buf,
1075         .release = ath9k_debugfs_release_buf,
1076         .owner = THIS_MODULE,
1077         .llseek = default_llseek,/* read accesses f_pos */
1078 };
1079
1080 static ssize_t read_file_dump_nfcal(struct file *file, char __user *user_buf,
1081                                     size_t count, loff_t *ppos)
1082 {
1083         struct ath_softc *sc = file->private_data;
1084         struct ath_hw *ah = sc->sc_ah;
1085         struct ath9k_nfcal_hist *h = sc->cur_chan->caldata.nfCalHist;
1086         struct ath_common *common = ath9k_hw_common(ah);
1087         struct ieee80211_conf *conf = &common->hw->conf;
1088         u32 len = 0, size = 1500;
1089         u32 i, j;
1090         ssize_t retval = 0;
1091         char *buf;
1092         u8 chainmask = (ah->rxchainmask << 3) | ah->rxchainmask;
1093         u8 nread;
1094
1095         buf = kzalloc(size, GFP_KERNEL);
1096         if (!buf)
1097                 return -ENOMEM;
1098
1099         len += scnprintf(buf + len, size - len,
1100                          "Channel Noise Floor : %d\n", ah->noise);
1101         len += scnprintf(buf + len, size - len,
1102                          "Chain | privNF | # Readings | NF Readings\n");
1103         for (i = 0; i < NUM_NF_READINGS; i++) {
1104                 if (!(chainmask & (1 << i)) ||
1105                     ((i >= AR5416_MAX_CHAINS) && !conf_is_ht40(conf)))
1106                         continue;
1107
1108                 nread = AR_PHY_CCA_FILTERWINDOW_LENGTH - h[i].invalidNFcount;
1109                 len += scnprintf(buf + len, size - len, " %d\t %d\t %d\t\t",
1110                                  i, h[i].privNF, nread);
1111                 for (j = 0; j < nread; j++)
1112                         len += scnprintf(buf + len, size - len,
1113                                          " %d", h[i].nfCalBuffer[j]);
1114                 len += scnprintf(buf + len, size - len, "\n");
1115         }
1116
1117         if (len > size)
1118                 len = size;
1119
1120         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
1121         kfree(buf);
1122
1123         return retval;
1124 }
1125
1126 static const struct file_operations fops_dump_nfcal = {
1127         .read = read_file_dump_nfcal,
1128         .open = simple_open,
1129         .owner = THIS_MODULE,
1130         .llseek = default_llseek,
1131 };
1132
1133 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
1134 static ssize_t read_file_btcoex(struct file *file, char __user *user_buf,
1135                                 size_t count, loff_t *ppos)
1136 {
1137         struct ath_softc *sc = file->private_data;
1138         u32 len = 0, size = 1500;
1139         char *buf;
1140         size_t retval;
1141
1142         buf = kzalloc(size, GFP_KERNEL);
1143         if (buf == NULL)
1144                 return -ENOMEM;
1145
1146         if (!sc->sc_ah->common.btcoex_enabled) {
1147                 len = scnprintf(buf, size, "%s\n",
1148                                 "BTCOEX is disabled");
1149                 goto exit;
1150         }
1151
1152         len = ath9k_dump_btcoex(sc, buf, size);
1153 exit:
1154         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
1155         kfree(buf);
1156
1157         return retval;
1158 }
1159
1160 static const struct file_operations fops_btcoex = {
1161         .read = read_file_btcoex,
1162         .open = simple_open,
1163         .owner = THIS_MODULE,
1164         .llseek = default_llseek,
1165 };
1166 #endif
1167
1168 #ifdef CONFIG_ATH9K_DYNACK
1169 static ssize_t read_file_ackto(struct file *file, char __user *user_buf,
1170                                size_t count, loff_t *ppos)
1171 {
1172         struct ath_softc *sc = file->private_data;
1173         struct ath_hw *ah = sc->sc_ah;
1174         char buf[32];
1175         unsigned int len;
1176
1177         len = sprintf(buf, "%u %c\n", ah->dynack.ackto,
1178                       (ah->dynack.enabled) ? 'A' : 'S');
1179
1180         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1181 }
1182
1183 static const struct file_operations fops_ackto = {
1184         .read = read_file_ackto,
1185         .open = simple_open,
1186         .owner = THIS_MODULE,
1187         .llseek = default_llseek,
1188 };
1189 #endif
1190
1191 /* Ethtool support for get-stats */
1192
1193 #define AMKSTR(nm) #nm "_BE", #nm "_BK", #nm "_VI", #nm "_VO"
1194 static const char ath9k_gstrings_stats[][ETH_GSTRING_LEN] = {
1195         "tx_pkts_nic",
1196         "tx_bytes_nic",
1197         "rx_pkts_nic",
1198         "rx_bytes_nic",
1199         AMKSTR(d_tx_pkts),
1200         AMKSTR(d_tx_bytes),
1201         AMKSTR(d_tx_mpdus_queued),
1202         AMKSTR(d_tx_mpdus_completed),
1203         AMKSTR(d_tx_mpdu_xretries),
1204         AMKSTR(d_tx_aggregates),
1205         AMKSTR(d_tx_ampdus_queued_hw),
1206         AMKSTR(d_tx_ampdus_queued_sw),
1207         AMKSTR(d_tx_ampdus_completed),
1208         AMKSTR(d_tx_ampdu_retries),
1209         AMKSTR(d_tx_ampdu_xretries),
1210         AMKSTR(d_tx_fifo_underrun),
1211         AMKSTR(d_tx_op_exceeded),
1212         AMKSTR(d_tx_timer_expiry),
1213         AMKSTR(d_tx_desc_cfg_err),
1214         AMKSTR(d_tx_data_underrun),
1215         AMKSTR(d_tx_delim_underrun),
1216         "d_rx_crc_err",
1217         "d_rx_decrypt_crc_err",
1218         "d_rx_phy_err",
1219         "d_rx_mic_err",
1220         "d_rx_pre_delim_crc_err",
1221         "d_rx_post_delim_crc_err",
1222         "d_rx_decrypt_busy_err",
1223
1224         "d_rx_phyerr_radar",
1225         "d_rx_phyerr_ofdm_timing",
1226         "d_rx_phyerr_cck_timing",
1227
1228 };
1229 #define ATH9K_SSTATS_LEN ARRAY_SIZE(ath9k_gstrings_stats)
1230
1231 void ath9k_get_et_strings(struct ieee80211_hw *hw,
1232                           struct ieee80211_vif *vif,
1233                           u32 sset, u8 *data)
1234 {
1235         if (sset == ETH_SS_STATS)
1236                 memcpy(data, *ath9k_gstrings_stats,
1237                        sizeof(ath9k_gstrings_stats));
1238 }
1239
1240 int ath9k_get_et_sset_count(struct ieee80211_hw *hw,
1241                             struct ieee80211_vif *vif, int sset)
1242 {
1243         if (sset == ETH_SS_STATS)
1244                 return ATH9K_SSTATS_LEN;
1245         return 0;
1246 }
1247
1248 #define AWDATA(elem)                                                    \
1249         do {                                                            \
1250                 data[i++] = sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_BE)].elem; \
1251                 data[i++] = sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_BK)].elem; \
1252                 data[i++] = sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_VI)].elem; \
1253                 data[i++] = sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_VO)].elem; \
1254         } while (0)
1255
1256 #define AWDATA_RX(elem)                                         \
1257         do {                                                    \
1258                 data[i++] = sc->debug.stats.rxstats.elem;       \
1259         } while (0)
1260
1261 void ath9k_get_et_stats(struct ieee80211_hw *hw,
1262                         struct ieee80211_vif *vif,
1263                         struct ethtool_stats *stats, u64 *data)
1264 {
1265         struct ath_softc *sc = hw->priv;
1266         int i = 0;
1267
1268         data[i++] = (sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_BE)].tx_pkts_all +
1269                      sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_BK)].tx_pkts_all +
1270                      sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_VI)].tx_pkts_all +
1271                      sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_VO)].tx_pkts_all);
1272         data[i++] = (sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_BE)].tx_bytes_all +
1273                      sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_BK)].tx_bytes_all +
1274                      sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_VI)].tx_bytes_all +
1275                      sc->debug.stats.txstats[PR_QNUM(IEEE80211_AC_VO)].tx_bytes_all);
1276         AWDATA_RX(rx_pkts_all);
1277         AWDATA_RX(rx_bytes_all);
1278
1279         AWDATA(tx_pkts_all);
1280         AWDATA(tx_bytes_all);
1281         AWDATA(queued);
1282         AWDATA(completed);
1283         AWDATA(xretries);
1284         AWDATA(a_aggr);
1285         AWDATA(a_queued_hw);
1286         AWDATA(a_queued_sw);
1287         AWDATA(a_completed);
1288         AWDATA(a_retries);
1289         AWDATA(a_xretries);
1290         AWDATA(fifo_underrun);
1291         AWDATA(xtxop);
1292         AWDATA(timer_exp);
1293         AWDATA(desc_cfg_err);
1294         AWDATA(data_underrun);
1295         AWDATA(delim_underrun);
1296
1297         AWDATA_RX(crc_err);
1298         AWDATA_RX(decrypt_crc_err);
1299         AWDATA_RX(phy_err);
1300         AWDATA_RX(mic_err);
1301         AWDATA_RX(pre_delim_crc_err);
1302         AWDATA_RX(post_delim_crc_err);
1303         AWDATA_RX(decrypt_busy_err);
1304
1305         AWDATA_RX(phy_err_stats[ATH9K_PHYERR_RADAR]);
1306         AWDATA_RX(phy_err_stats[ATH9K_PHYERR_OFDM_TIMING]);
1307         AWDATA_RX(phy_err_stats[ATH9K_PHYERR_CCK_TIMING]);
1308
1309         WARN_ON(i != ATH9K_SSTATS_LEN);
1310 }
1311
1312 void ath9k_deinit_debug(struct ath_softc *sc)
1313 {
1314         ath9k_cmn_spectral_deinit_debug(&sc->spec_priv);
1315 }
1316
1317 int ath9k_init_debug(struct ath_hw *ah)
1318 {
1319         struct ath_common *common = ath9k_hw_common(ah);
1320         struct ath_softc *sc = (struct ath_softc *) common->priv;
1321
1322         sc->debug.debugfs_phy = debugfs_create_dir("ath9k",
1323                                                    sc->hw->wiphy->debugfsdir);
1324         if (!sc->debug.debugfs_phy)
1325                 return -ENOMEM;
1326
1327 #ifdef CONFIG_ATH_DEBUG
1328         debugfs_create_file("debug", S_IRUSR | S_IWUSR, sc->debug.debugfs_phy,
1329                             sc, &fops_debug);
1330 #endif
1331
1332         ath9k_dfs_init_debug(sc);
1333         ath9k_tx99_init_debug(sc);
1334         ath9k_cmn_spectral_init_debug(&sc->spec_priv, sc->debug.debugfs_phy);
1335
1336         debugfs_create_file("dma", S_IRUSR, sc->debug.debugfs_phy, sc,
1337                             &fops_dma);
1338         debugfs_create_file("interrupt", S_IRUSR, sc->debug.debugfs_phy, sc,
1339                             &fops_interrupt);
1340         debugfs_create_file("xmit", S_IRUSR, sc->debug.debugfs_phy, sc,
1341                             &fops_xmit);
1342         debugfs_create_file("queues", S_IRUSR, sc->debug.debugfs_phy, sc,
1343                             &fops_queues);
1344         debugfs_create_u32("qlen_bk", S_IRUSR | S_IWUSR, sc->debug.debugfs_phy,
1345                            &sc->tx.txq_max_pending[IEEE80211_AC_BK]);
1346         debugfs_create_u32("qlen_be", S_IRUSR | S_IWUSR, sc->debug.debugfs_phy,
1347                            &sc->tx.txq_max_pending[IEEE80211_AC_BE]);
1348         debugfs_create_u32("qlen_vi", S_IRUSR | S_IWUSR, sc->debug.debugfs_phy,
1349                            &sc->tx.txq_max_pending[IEEE80211_AC_VI]);
1350         debugfs_create_u32("qlen_vo", S_IRUSR | S_IWUSR, sc->debug.debugfs_phy,
1351                            &sc->tx.txq_max_pending[IEEE80211_AC_VO]);
1352         debugfs_create_file("misc", S_IRUSR, sc->debug.debugfs_phy, sc,
1353                             &fops_misc);
1354         debugfs_create_file("reset", S_IRUSR, sc->debug.debugfs_phy, sc,
1355                             &fops_reset);
1356
1357         ath9k_cmn_debug_recv(sc->debug.debugfs_phy, &sc->debug.stats.rxstats);
1358         ath9k_cmn_debug_phy_err(sc->debug.debugfs_phy, &sc->debug.stats.rxstats);
1359
1360         debugfs_create_u8("rx_chainmask", S_IRUSR, sc->debug.debugfs_phy,
1361                           &ah->rxchainmask);
1362         debugfs_create_u8("tx_chainmask", S_IRUSR, sc->debug.debugfs_phy,
1363                           &ah->txchainmask);
1364         debugfs_create_file("ani", S_IRUSR | S_IWUSR,
1365                             sc->debug.debugfs_phy, sc, &fops_ani);
1366         debugfs_create_bool("paprd", S_IRUSR | S_IWUSR, sc->debug.debugfs_phy,
1367                             &sc->sc_ah->config.enable_paprd);
1368         debugfs_create_file("regidx", S_IRUSR | S_IWUSR, sc->debug.debugfs_phy,
1369                             sc, &fops_regidx);
1370         debugfs_create_file("regval", S_IRUSR | S_IWUSR, sc->debug.debugfs_phy,
1371                             sc, &fops_regval);
1372         debugfs_create_bool("ignore_extcca", S_IRUSR | S_IWUSR,
1373                             sc->debug.debugfs_phy,
1374                             &ah->config.cwm_ignore_extcca);
1375         debugfs_create_file("regdump", S_IRUSR, sc->debug.debugfs_phy, sc,
1376                             &fops_regdump);
1377         debugfs_create_file("dump_nfcal", S_IRUSR, sc->debug.debugfs_phy, sc,
1378                             &fops_dump_nfcal);
1379
1380         ath9k_cmn_debug_base_eeprom(sc->debug.debugfs_phy, sc->sc_ah);
1381         ath9k_cmn_debug_modal_eeprom(sc->debug.debugfs_phy, sc->sc_ah);
1382
1383         debugfs_create_u32("gpio_mask", S_IRUSR | S_IWUSR,
1384                            sc->debug.debugfs_phy, &sc->sc_ah->gpio_mask);
1385         debugfs_create_u32("gpio_val", S_IRUSR | S_IWUSR,
1386                            sc->debug.debugfs_phy, &sc->sc_ah->gpio_val);
1387         debugfs_create_file("antenna_diversity", S_IRUSR,
1388                             sc->debug.debugfs_phy, sc, &fops_antenna_diversity);
1389 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
1390         debugfs_create_file("bt_ant_diversity", S_IRUSR | S_IWUSR,
1391                             sc->debug.debugfs_phy, sc, &fops_bt_ant_diversity);
1392         debugfs_create_file("btcoex", S_IRUSR, sc->debug.debugfs_phy, sc,
1393                             &fops_btcoex);
1394 #endif
1395
1396 #ifdef CONFIG_ATH9K_DYNACK
1397         debugfs_create_file("ack_to", S_IRUSR | S_IWUSR, sc->debug.debugfs_phy,
1398                             sc, &fops_ackto);
1399 #endif
1400
1401         return 0;
1402 }