]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/wireless/ath/ath9k/debug.c
ath9k: enable the baseband watchdog events for AR9003
[net-next-2.6.git] / drivers / net / wireless / ath / ath9k / debug.c
1 /*
2  * Copyright (c) 2008-2009 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 <asm/unaligned.h>
19
20 #include "ath9k.h"
21
22 #define REG_WRITE_D(_ah, _reg, _val) \
23         ath9k_hw_common(_ah)->ops->write((_ah), (_val), (_reg))
24 #define REG_READ_D(_ah, _reg) \
25         ath9k_hw_common(_ah)->ops->read((_ah), (_reg))
26
27 static struct dentry *ath9k_debugfs_root;
28
29 static int ath9k_debugfs_open(struct inode *inode, struct file *file)
30 {
31         file->private_data = inode->i_private;
32         return 0;
33 }
34
35 #ifdef CONFIG_ATH_DEBUG
36
37 static ssize_t read_file_debug(struct file *file, char __user *user_buf,
38                              size_t count, loff_t *ppos)
39 {
40         struct ath_softc *sc = file->private_data;
41         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
42         char buf[32];
43         unsigned int len;
44
45         len = snprintf(buf, sizeof(buf), "0x%08x\n", common->debug_mask);
46         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
47 }
48
49 static ssize_t write_file_debug(struct file *file, const char __user *user_buf,
50                              size_t count, loff_t *ppos)
51 {
52         struct ath_softc *sc = file->private_data;
53         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
54         unsigned long mask;
55         char buf[32];
56         ssize_t len;
57
58         len = min(count, sizeof(buf) - 1);
59         if (copy_from_user(buf, user_buf, len))
60                 return -EINVAL;
61
62         buf[len] = '\0';
63         if (strict_strtoul(buf, 0, &mask))
64                 return -EINVAL;
65
66         common->debug_mask = mask;
67         return count;
68 }
69
70 static const struct file_operations fops_debug = {
71         .read = read_file_debug,
72         .write = write_file_debug,
73         .open = ath9k_debugfs_open,
74         .owner = THIS_MODULE
75 };
76
77 #endif
78
79 #define DMA_BUF_LEN 1024
80
81 static ssize_t read_file_tx_chainmask(struct file *file, char __user *user_buf,
82                              size_t count, loff_t *ppos)
83 {
84         struct ath_softc *sc = file->private_data;
85         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
86         char buf[32];
87         unsigned int len;
88
89         len = snprintf(buf, sizeof(buf), "0x%08x\n", common->tx_chainmask);
90         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
91 }
92
93 static ssize_t write_file_tx_chainmask(struct file *file, const char __user *user_buf,
94                              size_t count, loff_t *ppos)
95 {
96         struct ath_softc *sc = file->private_data;
97         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
98         unsigned long mask;
99         char buf[32];
100         ssize_t len;
101
102         len = min(count, sizeof(buf) - 1);
103         if (copy_from_user(buf, user_buf, len))
104                 return -EINVAL;
105
106         buf[len] = '\0';
107         if (strict_strtoul(buf, 0, &mask))
108                 return -EINVAL;
109
110         common->tx_chainmask = mask;
111         sc->sc_ah->caps.tx_chainmask = mask;
112         return count;
113 }
114
115 static const struct file_operations fops_tx_chainmask = {
116         .read = read_file_tx_chainmask,
117         .write = write_file_tx_chainmask,
118         .open = ath9k_debugfs_open,
119         .owner = THIS_MODULE
120 };
121
122
123 static ssize_t read_file_rx_chainmask(struct file *file, char __user *user_buf,
124                              size_t count, loff_t *ppos)
125 {
126         struct ath_softc *sc = file->private_data;
127         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
128         char buf[32];
129         unsigned int len;
130
131         len = snprintf(buf, sizeof(buf), "0x%08x\n", common->rx_chainmask);
132         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
133 }
134
135 static ssize_t write_file_rx_chainmask(struct file *file, const char __user *user_buf,
136                              size_t count, loff_t *ppos)
137 {
138         struct ath_softc *sc = file->private_data;
139         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
140         unsigned long mask;
141         char buf[32];
142         ssize_t len;
143
144         len = min(count, sizeof(buf) - 1);
145         if (copy_from_user(buf, user_buf, len))
146                 return -EINVAL;
147
148         buf[len] = '\0';
149         if (strict_strtoul(buf, 0, &mask))
150                 return -EINVAL;
151
152         common->rx_chainmask = mask;
153         sc->sc_ah->caps.rx_chainmask = mask;
154         return count;
155 }
156
157 static const struct file_operations fops_rx_chainmask = {
158         .read = read_file_rx_chainmask,
159         .write = write_file_rx_chainmask,
160         .open = ath9k_debugfs_open,
161         .owner = THIS_MODULE
162 };
163
164
165 static ssize_t read_file_dma(struct file *file, char __user *user_buf,
166                              size_t count, loff_t *ppos)
167 {
168         struct ath_softc *sc = file->private_data;
169         struct ath_hw *ah = sc->sc_ah;
170         char *buf;
171         int retval;
172         unsigned int len = 0;
173         u32 val[ATH9K_NUM_DMA_DEBUG_REGS];
174         int i, qcuOffset = 0, dcuOffset = 0;
175         u32 *qcuBase = &val[0], *dcuBase = &val[4];
176
177         buf = kmalloc(DMA_BUF_LEN, GFP_KERNEL);
178         if (!buf)
179                 return 0;
180
181         ath9k_ps_wakeup(sc);
182
183         REG_WRITE_D(ah, AR_MACMISC,
184                   ((AR_MACMISC_DMA_OBS_LINE_8 << AR_MACMISC_DMA_OBS_S) |
185                    (AR_MACMISC_MISC_OBS_BUS_1 <<
186                     AR_MACMISC_MISC_OBS_BUS_MSB_S)));
187
188         len += snprintf(buf + len, DMA_BUF_LEN - len,
189                         "Raw DMA Debug values:\n");
190
191         for (i = 0; i < ATH9K_NUM_DMA_DEBUG_REGS; i++) {
192                 if (i % 4 == 0)
193                         len += snprintf(buf + len, DMA_BUF_LEN - len, "\n");
194
195                 val[i] = REG_READ_D(ah, AR_DMADBG_0 + (i * sizeof(u32)));
196                 len += snprintf(buf + len, DMA_BUF_LEN - len, "%d: %08x ",
197                                 i, val[i]);
198         }
199
200         len += snprintf(buf + len, DMA_BUF_LEN - len, "\n\n");
201         len += snprintf(buf + len, DMA_BUF_LEN - len,
202                         "Num QCU: chain_st fsp_ok fsp_st DCU: chain_st\n");
203
204         for (i = 0; i < ATH9K_NUM_QUEUES; i++, qcuOffset += 4, dcuOffset += 5) {
205                 if (i == 8) {
206                         qcuOffset = 0;
207                         qcuBase++;
208                 }
209
210                 if (i == 6) {
211                         dcuOffset = 0;
212                         dcuBase++;
213                 }
214
215                 len += snprintf(buf + len, DMA_BUF_LEN - len,
216                         "%2d          %2x      %1x     %2x           %2x\n",
217                         i, (*qcuBase & (0x7 << qcuOffset)) >> qcuOffset,
218                         (*qcuBase & (0x8 << qcuOffset)) >> (qcuOffset + 3),
219                         val[2] & (0x7 << (i * 3)) >> (i * 3),
220                         (*dcuBase & (0x1f << dcuOffset)) >> dcuOffset);
221         }
222
223         len += snprintf(buf + len, DMA_BUF_LEN - len, "\n");
224
225         len += snprintf(buf + len, DMA_BUF_LEN - len,
226                 "qcu_stitch state:   %2x    qcu_fetch state:        %2x\n",
227                 (val[3] & 0x003c0000) >> 18, (val[3] & 0x03c00000) >> 22);
228         len += snprintf(buf + len, DMA_BUF_LEN - len,
229                 "qcu_complete state: %2x    dcu_complete state:     %2x\n",
230                 (val[3] & 0x1c000000) >> 26, (val[6] & 0x3));
231         len += snprintf(buf + len, DMA_BUF_LEN - len,
232                 "dcu_arb state:      %2x    dcu_fp state:           %2x\n",
233                 (val[5] & 0x06000000) >> 25, (val[5] & 0x38000000) >> 27);
234         len += snprintf(buf + len, DMA_BUF_LEN - len,
235                 "chan_idle_dur:     %3d    chan_idle_dur_valid:     %1d\n",
236                 (val[6] & 0x000003fc) >> 2, (val[6] & 0x00000400) >> 10);
237         len += snprintf(buf + len, DMA_BUF_LEN - len,
238                 "txfifo_valid_0:      %1d    txfifo_valid_1:          %1d\n",
239                 (val[6] & 0x00000800) >> 11, (val[6] & 0x00001000) >> 12);
240         len += snprintf(buf + len, DMA_BUF_LEN - len,
241                 "txfifo_dcu_num_0:   %2d    txfifo_dcu_num_1:       %2d\n",
242                 (val[6] & 0x0001e000) >> 13, (val[6] & 0x001e0000) >> 17);
243
244         len += snprintf(buf + len, DMA_BUF_LEN - len, "pcu observe: 0x%x\n",
245                         REG_READ_D(ah, AR_OBS_BUS_1));
246         len += snprintf(buf + len, DMA_BUF_LEN - len,
247                         "AR_CR: 0x%x\n", REG_READ_D(ah, AR_CR));
248
249         ath9k_ps_restore(sc);
250
251         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
252         kfree(buf);
253         return retval;
254 }
255
256 static const struct file_operations fops_dma = {
257         .read = read_file_dma,
258         .open = ath9k_debugfs_open,
259         .owner = THIS_MODULE
260 };
261
262
263 void ath_debug_stat_interrupt(struct ath_softc *sc, enum ath9k_int status)
264 {
265         if (status)
266                 sc->debug.stats.istats.total++;
267         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
268                 if (status & ATH9K_INT_RXLP)
269                         sc->debug.stats.istats.rxlp++;
270                 if (status & ATH9K_INT_RXHP)
271                         sc->debug.stats.istats.rxhp++;
272                 if (status & ATH9K_INT_BB_WATCHDOG)
273                         sc->debug.stats.istats.bb_watchdog++;
274         } else {
275                 if (status & ATH9K_INT_RX)
276                         sc->debug.stats.istats.rxok++;
277         }
278         if (status & ATH9K_INT_RXEOL)
279                 sc->debug.stats.istats.rxeol++;
280         if (status & ATH9K_INT_RXORN)
281                 sc->debug.stats.istats.rxorn++;
282         if (status & ATH9K_INT_TX)
283                 sc->debug.stats.istats.txok++;
284         if (status & ATH9K_INT_TXURN)
285                 sc->debug.stats.istats.txurn++;
286         if (status & ATH9K_INT_MIB)
287                 sc->debug.stats.istats.mib++;
288         if (status & ATH9K_INT_RXPHY)
289                 sc->debug.stats.istats.rxphyerr++;
290         if (status & ATH9K_INT_RXKCM)
291                 sc->debug.stats.istats.rx_keycache_miss++;
292         if (status & ATH9K_INT_SWBA)
293                 sc->debug.stats.istats.swba++;
294         if (status & ATH9K_INT_BMISS)
295                 sc->debug.stats.istats.bmiss++;
296         if (status & ATH9K_INT_BNR)
297                 sc->debug.stats.istats.bnr++;
298         if (status & ATH9K_INT_CST)
299                 sc->debug.stats.istats.cst++;
300         if (status & ATH9K_INT_GTT)
301                 sc->debug.stats.istats.gtt++;
302         if (status & ATH9K_INT_TIM)
303                 sc->debug.stats.istats.tim++;
304         if (status & ATH9K_INT_CABEND)
305                 sc->debug.stats.istats.cabend++;
306         if (status & ATH9K_INT_DTIMSYNC)
307                 sc->debug.stats.istats.dtimsync++;
308         if (status & ATH9K_INT_DTIM)
309                 sc->debug.stats.istats.dtim++;
310 }
311
312 static ssize_t read_file_interrupt(struct file *file, char __user *user_buf,
313                                    size_t count, loff_t *ppos)
314 {
315         struct ath_softc *sc = file->private_data;
316         char buf[512];
317         unsigned int len = 0;
318
319         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
320                 len += snprintf(buf + len, sizeof(buf) - len,
321                         "%8s: %10u\n", "RXLP", sc->debug.stats.istats.rxlp);
322                 len += snprintf(buf + len, sizeof(buf) - len,
323                         "%8s: %10u\n", "RXHP", sc->debug.stats.istats.rxhp);
324                 len += snprintf(buf + len, sizeof(buf) - len,
325                         "%8s: %10u\n", "WATCHDOG",
326                         sc->debug.stats.istats.bb_watchdog);
327         } else {
328                 len += snprintf(buf + len, sizeof(buf) - len,
329                         "%8s: %10u\n", "RX", sc->debug.stats.istats.rxok);
330         }
331         len += snprintf(buf + len, sizeof(buf) - len,
332                 "%8s: %10u\n", "RXEOL", sc->debug.stats.istats.rxeol);
333         len += snprintf(buf + len, sizeof(buf) - len,
334                 "%8s: %10u\n", "RXORN", sc->debug.stats.istats.rxorn);
335         len += snprintf(buf + len, sizeof(buf) - len,
336                 "%8s: %10u\n", "TX", sc->debug.stats.istats.txok);
337         len += snprintf(buf + len, sizeof(buf) - len,
338                 "%8s: %10u\n", "TXURN", sc->debug.stats.istats.txurn);
339         len += snprintf(buf + len, sizeof(buf) - len,
340                 "%8s: %10u\n", "MIB", sc->debug.stats.istats.mib);
341         len += snprintf(buf + len, sizeof(buf) - len,
342                 "%8s: %10u\n", "RXPHY", sc->debug.stats.istats.rxphyerr);
343         len += snprintf(buf + len, sizeof(buf) - len,
344                 "%8s: %10u\n", "RXKCM", sc->debug.stats.istats.rx_keycache_miss);
345         len += snprintf(buf + len, sizeof(buf) - len,
346                 "%8s: %10u\n", "SWBA", sc->debug.stats.istats.swba);
347         len += snprintf(buf + len, sizeof(buf) - len,
348                 "%8s: %10u\n", "BMISS", sc->debug.stats.istats.bmiss);
349         len += snprintf(buf + len, sizeof(buf) - len,
350                 "%8s: %10u\n", "BNR", sc->debug.stats.istats.bnr);
351         len += snprintf(buf + len, sizeof(buf) - len,
352                 "%8s: %10u\n", "CST", sc->debug.stats.istats.cst);
353         len += snprintf(buf + len, sizeof(buf) - len,
354                 "%8s: %10u\n", "GTT", sc->debug.stats.istats.gtt);
355         len += snprintf(buf + len, sizeof(buf) - len,
356                 "%8s: %10u\n", "TIM", sc->debug.stats.istats.tim);
357         len += snprintf(buf + len, sizeof(buf) - len,
358                 "%8s: %10u\n", "CABEND", sc->debug.stats.istats.cabend);
359         len += snprintf(buf + len, sizeof(buf) - len,
360                 "%8s: %10u\n", "DTIMSYNC", sc->debug.stats.istats.dtimsync);
361         len += snprintf(buf + len, sizeof(buf) - len,
362                 "%8s: %10u\n", "DTIM", sc->debug.stats.istats.dtim);
363         len += snprintf(buf + len, sizeof(buf) - len,
364                 "%8s: %10u\n", "TOTAL", sc->debug.stats.istats.total);
365
366         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
367 }
368
369 static const struct file_operations fops_interrupt = {
370         .read = read_file_interrupt,
371         .open = ath9k_debugfs_open,
372         .owner = THIS_MODULE
373 };
374
375 void ath_debug_stat_rc(struct ath_softc *sc, int final_rate)
376 {
377         struct ath_rc_stats *stats;
378
379         stats = &sc->debug.stats.rcstats[final_rate];
380         stats->success++;
381 }
382
383 void ath_debug_stat_retries(struct ath_softc *sc, int rix,
384                             int xretries, int retries, u8 per)
385 {
386         struct ath_rc_stats *stats = &sc->debug.stats.rcstats[rix];
387
388         stats->xretries += xretries;
389         stats->retries += retries;
390         stats->per = per;
391 }
392
393 static ssize_t read_file_rcstat(struct file *file, char __user *user_buf,
394                                 size_t count, loff_t *ppos)
395 {
396         struct ath_softc *sc = file->private_data;
397         char *buf;
398         unsigned int len = 0, max;
399         int i = 0;
400         ssize_t retval;
401
402         if (sc->cur_rate_table == NULL)
403                 return 0;
404
405         max = 80 + sc->cur_rate_table->rate_cnt * 1024;
406         buf = kmalloc(max + 1, GFP_KERNEL);
407         if (buf == NULL)
408                 return 0;
409         buf[max] = 0;
410
411         len += sprintf(buf, "%6s %6s %6s "
412                        "%10s %10s %10s %10s\n",
413                        "HT", "MCS", "Rate",
414                        "Success", "Retries", "XRetries", "PER");
415
416         for (i = 0; i < sc->cur_rate_table->rate_cnt; i++) {
417                 u32 ratekbps = sc->cur_rate_table->info[i].ratekbps;
418                 struct ath_rc_stats *stats = &sc->debug.stats.rcstats[i];
419                 char mcs[5];
420                 char htmode[5];
421                 int used_mcs = 0, used_htmode = 0;
422
423                 if (WLAN_RC_PHY_HT(sc->cur_rate_table->info[i].phy)) {
424                         used_mcs = snprintf(mcs, 5, "%d",
425                                 sc->cur_rate_table->info[i].ratecode);
426
427                         if (WLAN_RC_PHY_40(sc->cur_rate_table->info[i].phy))
428                                 used_htmode = snprintf(htmode, 5, "HT40");
429                         else if (WLAN_RC_PHY_20(sc->cur_rate_table->info[i].phy))
430                                 used_htmode = snprintf(htmode, 5, "HT20");
431                         else
432                                 used_htmode = snprintf(htmode, 5, "????");
433                 }
434
435                 mcs[used_mcs] = '\0';
436                 htmode[used_htmode] = '\0';
437
438                 len += snprintf(buf + len, max - len,
439                         "%6s %6s %3u.%d: "
440                         "%10u %10u %10u %10u\n",
441                         htmode,
442                         mcs,
443                         ratekbps / 1000,
444                         (ratekbps % 1000) / 100,
445                         stats->success,
446                         stats->retries,
447                         stats->xretries,
448                         stats->per);
449         }
450
451         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
452         kfree(buf);
453         return retval;
454 }
455
456 static const struct file_operations fops_rcstat = {
457         .read = read_file_rcstat,
458         .open = ath9k_debugfs_open,
459         .owner = THIS_MODULE
460 };
461
462 static const char * ath_wiphy_state_str(enum ath_wiphy_state state)
463 {
464         switch (state) {
465         case ATH_WIPHY_INACTIVE:
466                 return "INACTIVE";
467         case ATH_WIPHY_ACTIVE:
468                 return "ACTIVE";
469         case ATH_WIPHY_PAUSING:
470                 return "PAUSING";
471         case ATH_WIPHY_PAUSED:
472                 return "PAUSED";
473         case ATH_WIPHY_SCAN:
474                 return "SCAN";
475         }
476         return "?";
477 }
478
479 static ssize_t read_file_wiphy(struct file *file, char __user *user_buf,
480                                size_t count, loff_t *ppos)
481 {
482         struct ath_softc *sc = file->private_data;
483         char buf[512];
484         unsigned int len = 0;
485         int i;
486         u8 addr[ETH_ALEN];
487
488         len += snprintf(buf + len, sizeof(buf) - len,
489                         "primary: %s (%s chan=%d ht=%d)\n",
490                         wiphy_name(sc->pri_wiphy->hw->wiphy),
491                         ath_wiphy_state_str(sc->pri_wiphy->state),
492                         sc->pri_wiphy->chan_idx, sc->pri_wiphy->chan_is_ht);
493         for (i = 0; i < sc->num_sec_wiphy; i++) {
494                 struct ath_wiphy *aphy = sc->sec_wiphy[i];
495                 if (aphy == NULL)
496                         continue;
497                 len += snprintf(buf + len, sizeof(buf) - len,
498                                 "secondary: %s (%s chan=%d ht=%d)\n",
499                                 wiphy_name(aphy->hw->wiphy),
500                                 ath_wiphy_state_str(aphy->state),
501                                 aphy->chan_idx, aphy->chan_is_ht);
502         }
503
504         put_unaligned_le32(REG_READ_D(sc->sc_ah, AR_STA_ID0), addr);
505         put_unaligned_le16(REG_READ_D(sc->sc_ah, AR_STA_ID1) & 0xffff, addr + 4);
506         len += snprintf(buf + len, sizeof(buf) - len,
507                         "addr: %pM\n", addr);
508         put_unaligned_le32(REG_READ_D(sc->sc_ah, AR_BSSMSKL), addr);
509         put_unaligned_le16(REG_READ_D(sc->sc_ah, AR_BSSMSKU) & 0xffff, addr + 4);
510         len += snprintf(buf + len, sizeof(buf) - len,
511                         "addrmask: %pM\n", addr);
512
513         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
514 }
515
516 static struct ath_wiphy * get_wiphy(struct ath_softc *sc, const char *name)
517 {
518         int i;
519         if (strcmp(name, wiphy_name(sc->pri_wiphy->hw->wiphy)) == 0)
520                 return sc->pri_wiphy;
521         for (i = 0; i < sc->num_sec_wiphy; i++) {
522                 struct ath_wiphy *aphy = sc->sec_wiphy[i];
523                 if (aphy && strcmp(name, wiphy_name(aphy->hw->wiphy)) == 0)
524                         return aphy;
525         }
526         return NULL;
527 }
528
529 static int del_wiphy(struct ath_softc *sc, const char *name)
530 {
531         struct ath_wiphy *aphy = get_wiphy(sc, name);
532         if (!aphy)
533                 return -ENOENT;
534         return ath9k_wiphy_del(aphy);
535 }
536
537 static int pause_wiphy(struct ath_softc *sc, const char *name)
538 {
539         struct ath_wiphy *aphy = get_wiphy(sc, name);
540         if (!aphy)
541                 return -ENOENT;
542         return ath9k_wiphy_pause(aphy);
543 }
544
545 static int unpause_wiphy(struct ath_softc *sc, const char *name)
546 {
547         struct ath_wiphy *aphy = get_wiphy(sc, name);
548         if (!aphy)
549                 return -ENOENT;
550         return ath9k_wiphy_unpause(aphy);
551 }
552
553 static int select_wiphy(struct ath_softc *sc, const char *name)
554 {
555         struct ath_wiphy *aphy = get_wiphy(sc, name);
556         if (!aphy)
557                 return -ENOENT;
558         return ath9k_wiphy_select(aphy);
559 }
560
561 static int schedule_wiphy(struct ath_softc *sc, const char *msec)
562 {
563         ath9k_wiphy_set_scheduler(sc, simple_strtoul(msec, NULL, 0));
564         return 0;
565 }
566
567 static ssize_t write_file_wiphy(struct file *file, const char __user *user_buf,
568                                 size_t count, loff_t *ppos)
569 {
570         struct ath_softc *sc = file->private_data;
571         char buf[50];
572         size_t len;
573
574         len = min(count, sizeof(buf) - 1);
575         if (copy_from_user(buf, user_buf, len))
576                 return -EFAULT;
577         buf[len] = '\0';
578         if (len > 0 && buf[len - 1] == '\n')
579                 buf[len - 1] = '\0';
580
581         if (strncmp(buf, "add", 3) == 0) {
582                 int res = ath9k_wiphy_add(sc);
583                 if (res < 0)
584                         return res;
585         } else if (strncmp(buf, "del=", 4) == 0) {
586                 int res = del_wiphy(sc, buf + 4);
587                 if (res < 0)
588                         return res;
589         } else if (strncmp(buf, "pause=", 6) == 0) {
590                 int res = pause_wiphy(sc, buf + 6);
591                 if (res < 0)
592                         return res;
593         } else if (strncmp(buf, "unpause=", 8) == 0) {
594                 int res = unpause_wiphy(sc, buf + 8);
595                 if (res < 0)
596                         return res;
597         } else if (strncmp(buf, "select=", 7) == 0) {
598                 int res = select_wiphy(sc, buf + 7);
599                 if (res < 0)
600                         return res;
601         } else if (strncmp(buf, "schedule=", 9) == 0) {
602                 int res = schedule_wiphy(sc, buf + 9);
603                 if (res < 0)
604                         return res;
605         } else
606                 return -EOPNOTSUPP;
607
608         return count;
609 }
610
611 static const struct file_operations fops_wiphy = {
612         .read = read_file_wiphy,
613         .write = write_file_wiphy,
614         .open = ath9k_debugfs_open,
615         .owner = THIS_MODULE
616 };
617
618 #define PR(str, elem)                                                   \
619         do {                                                            \
620                 len += snprintf(buf + len, size - len,                  \
621                                 "%s%13u%11u%10u%10u\n", str,            \
622                 sc->debug.stats.txstats[sc->tx.hwq_map[ATH9K_WME_AC_BE]].elem, \
623                 sc->debug.stats.txstats[sc->tx.hwq_map[ATH9K_WME_AC_BK]].elem, \
624                 sc->debug.stats.txstats[sc->tx.hwq_map[ATH9K_WME_AC_VI]].elem, \
625                 sc->debug.stats.txstats[sc->tx.hwq_map[ATH9K_WME_AC_VO]].elem); \
626 } while(0)
627
628 static ssize_t read_file_xmit(struct file *file, char __user *user_buf,
629                               size_t count, loff_t *ppos)
630 {
631         struct ath_softc *sc = file->private_data;
632         char *buf;
633         unsigned int len = 0, size = 2048;
634         ssize_t retval = 0;
635
636         buf = kzalloc(size, GFP_KERNEL);
637         if (buf == NULL)
638                 return 0;
639
640         len += sprintf(buf, "%30s %10s%10s%10s\n\n", "BE", "BK", "VI", "VO");
641
642         PR("MPDUs Queued:    ", queued);
643         PR("MPDUs Completed: ", completed);
644         PR("Aggregates:      ", a_aggr);
645         PR("AMPDUs Queued:   ", a_queued);
646         PR("AMPDUs Completed:", a_completed);
647         PR("AMPDUs Retried:  ", a_retries);
648         PR("AMPDUs XRetried: ", a_xretries);
649         PR("FIFO Underrun:   ", fifo_underrun);
650         PR("TXOP Exceeded:   ", xtxop);
651         PR("TXTIMER Expiry:  ", timer_exp);
652         PR("DESC CFG Error:  ", desc_cfg_err);
653         PR("DATA Underrun:   ", data_underrun);
654         PR("DELIM Underrun:  ", delim_underrun);
655
656         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
657         kfree(buf);
658
659         return retval;
660 }
661
662 void ath_debug_stat_tx(struct ath_softc *sc, struct ath_txq *txq,
663                        struct ath_buf *bf, struct ath_tx_status *ts)
664 {
665         if (bf_isampdu(bf)) {
666                 if (bf_isxretried(bf))
667                         TX_STAT_INC(txq->axq_qnum, a_xretries);
668                 else
669                         TX_STAT_INC(txq->axq_qnum, a_completed);
670         } else {
671                 TX_STAT_INC(txq->axq_qnum, completed);
672         }
673
674         if (ts->ts_status & ATH9K_TXERR_FIFO)
675                 TX_STAT_INC(txq->axq_qnum, fifo_underrun);
676         if (ts->ts_status & ATH9K_TXERR_XTXOP)
677                 TX_STAT_INC(txq->axq_qnum, xtxop);
678         if (ts->ts_status & ATH9K_TXERR_TIMER_EXPIRED)
679                 TX_STAT_INC(txq->axq_qnum, timer_exp);
680         if (ts->ts_flags & ATH9K_TX_DESC_CFG_ERR)
681                 TX_STAT_INC(txq->axq_qnum, desc_cfg_err);
682         if (ts->ts_flags & ATH9K_TX_DATA_UNDERRUN)
683                 TX_STAT_INC(txq->axq_qnum, data_underrun);
684         if (ts->ts_flags & ATH9K_TX_DELIM_UNDERRUN)
685                 TX_STAT_INC(txq->axq_qnum, delim_underrun);
686 }
687
688 static const struct file_operations fops_xmit = {
689         .read = read_file_xmit,
690         .open = ath9k_debugfs_open,
691         .owner = THIS_MODULE
692 };
693
694 static ssize_t read_file_recv(struct file *file, char __user *user_buf,
695                               size_t count, loff_t *ppos)
696 {
697 #define PHY_ERR(s, p) \
698         len += snprintf(buf + len, size - len, "%18s : %10u\n", s, \
699                         sc->debug.stats.rxstats.phy_err_stats[p]);
700
701         struct ath_softc *sc = file->private_data;
702         char *buf;
703         unsigned int len = 0, size = 1152;
704         ssize_t retval = 0;
705
706         buf = kzalloc(size, GFP_KERNEL);
707         if (buf == NULL)
708                 return 0;
709
710         len += snprintf(buf + len, size - len,
711                         "%18s : %10u\n", "CRC ERR",
712                         sc->debug.stats.rxstats.crc_err);
713         len += snprintf(buf + len, size - len,
714                         "%18s : %10u\n", "DECRYPT CRC ERR",
715                         sc->debug.stats.rxstats.decrypt_crc_err);
716         len += snprintf(buf + len, size - len,
717                         "%18s : %10u\n", "PHY ERR",
718                         sc->debug.stats.rxstats.phy_err);
719         len += snprintf(buf + len, size - len,
720                         "%18s : %10u\n", "MIC ERR",
721                         sc->debug.stats.rxstats.mic_err);
722         len += snprintf(buf + len, size - len,
723                         "%18s : %10u\n", "PRE-DELIM CRC ERR",
724                         sc->debug.stats.rxstats.pre_delim_crc_err);
725         len += snprintf(buf + len, size - len,
726                         "%18s : %10u\n", "POST-DELIM CRC ERR",
727                         sc->debug.stats.rxstats.post_delim_crc_err);
728         len += snprintf(buf + len, size - len,
729                         "%18s : %10u\n", "DECRYPT BUSY ERR",
730                         sc->debug.stats.rxstats.decrypt_busy_err);
731
732         PHY_ERR("UNDERRUN", ATH9K_PHYERR_UNDERRUN);
733         PHY_ERR("TIMING", ATH9K_PHYERR_TIMING);
734         PHY_ERR("PARITY", ATH9K_PHYERR_PARITY);
735         PHY_ERR("RATE", ATH9K_PHYERR_RATE);
736         PHY_ERR("LENGTH", ATH9K_PHYERR_LENGTH);
737         PHY_ERR("RADAR", ATH9K_PHYERR_RADAR);
738         PHY_ERR("SERVICE", ATH9K_PHYERR_SERVICE);
739         PHY_ERR("TOR", ATH9K_PHYERR_TOR);
740         PHY_ERR("OFDM-TIMING", ATH9K_PHYERR_OFDM_TIMING);
741         PHY_ERR("OFDM-SIGNAL-PARITY", ATH9K_PHYERR_OFDM_SIGNAL_PARITY);
742         PHY_ERR("OFDM-RATE", ATH9K_PHYERR_OFDM_RATE_ILLEGAL);
743         PHY_ERR("OFDM-LENGTH", ATH9K_PHYERR_OFDM_LENGTH_ILLEGAL);
744         PHY_ERR("OFDM-POWER-DROP", ATH9K_PHYERR_OFDM_POWER_DROP);
745         PHY_ERR("OFDM-SERVICE", ATH9K_PHYERR_OFDM_SERVICE);
746         PHY_ERR("OFDM-RESTART", ATH9K_PHYERR_OFDM_RESTART);
747         PHY_ERR("FALSE-RADAR-EXT", ATH9K_PHYERR_FALSE_RADAR_EXT);
748         PHY_ERR("CCK-TIMING", ATH9K_PHYERR_CCK_TIMING);
749         PHY_ERR("CCK-HEADER-CRC", ATH9K_PHYERR_CCK_HEADER_CRC);
750         PHY_ERR("CCK-RATE", ATH9K_PHYERR_CCK_RATE_ILLEGAL);
751         PHY_ERR("CCK-SERVICE", ATH9K_PHYERR_CCK_SERVICE);
752         PHY_ERR("CCK-RESTART", ATH9K_PHYERR_CCK_RESTART);
753         PHY_ERR("CCK-LENGTH", ATH9K_PHYERR_CCK_LENGTH_ILLEGAL);
754         PHY_ERR("CCK-POWER-DROP", ATH9K_PHYERR_CCK_POWER_DROP);
755         PHY_ERR("HT-CRC", ATH9K_PHYERR_HT_CRC_ERROR);
756         PHY_ERR("HT-LENGTH", ATH9K_PHYERR_HT_LENGTH_ILLEGAL);
757         PHY_ERR("HT-RATE", ATH9K_PHYERR_HT_RATE_ILLEGAL);
758
759         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
760         kfree(buf);
761
762         return retval;
763
764 #undef PHY_ERR
765 }
766
767 void ath_debug_stat_rx(struct ath_softc *sc, struct ath_rx_status *rs)
768 {
769 #define RX_STAT_INC(c) sc->debug.stats.rxstats.c++
770 #define RX_PHY_ERR_INC(c) sc->debug.stats.rxstats.phy_err_stats[c]++
771
772         u32 phyerr;
773
774         if (rs->rs_status & ATH9K_RXERR_CRC)
775                 RX_STAT_INC(crc_err);
776         if (rs->rs_status & ATH9K_RXERR_DECRYPT)
777                 RX_STAT_INC(decrypt_crc_err);
778         if (rs->rs_status & ATH9K_RXERR_MIC)
779                 RX_STAT_INC(mic_err);
780         if (rs->rs_status & ATH9K_RX_DELIM_CRC_PRE)
781                 RX_STAT_INC(pre_delim_crc_err);
782         if (rs->rs_status & ATH9K_RX_DELIM_CRC_POST)
783                 RX_STAT_INC(post_delim_crc_err);
784         if (rs->rs_status & ATH9K_RX_DECRYPT_BUSY)
785                 RX_STAT_INC(decrypt_busy_err);
786
787         if (rs->rs_status & ATH9K_RXERR_PHY) {
788                 RX_STAT_INC(phy_err);
789                 phyerr = rs->rs_phyerr & 0x24;
790                 RX_PHY_ERR_INC(phyerr);
791         }
792
793 #undef RX_STAT_INC
794 #undef RX_PHY_ERR_INC
795 }
796
797 static const struct file_operations fops_recv = {
798         .read = read_file_recv,
799         .open = ath9k_debugfs_open,
800         .owner = THIS_MODULE
801 };
802
803 static ssize_t read_file_regidx(struct file *file, char __user *user_buf,
804                                 size_t count, loff_t *ppos)
805 {
806         struct ath_softc *sc = file->private_data;
807         char buf[32];
808         unsigned int len;
809
810         len = snprintf(buf, sizeof(buf), "0x%08x\n", sc->debug.regidx);
811         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
812 }
813
814 static ssize_t write_file_regidx(struct file *file, const char __user *user_buf,
815                              size_t count, loff_t *ppos)
816 {
817         struct ath_softc *sc = file->private_data;
818         unsigned long regidx;
819         char buf[32];
820         ssize_t len;
821
822         len = min(count, sizeof(buf) - 1);
823         if (copy_from_user(buf, user_buf, len))
824                 return -EINVAL;
825
826         buf[len] = '\0';
827         if (strict_strtoul(buf, 0, &regidx))
828                 return -EINVAL;
829
830         sc->debug.regidx = regidx;
831         return count;
832 }
833
834 static const struct file_operations fops_regidx = {
835         .read = read_file_regidx,
836         .write = write_file_regidx,
837         .open = ath9k_debugfs_open,
838         .owner = THIS_MODULE
839 };
840
841 static ssize_t read_file_regval(struct file *file, char __user *user_buf,
842                              size_t count, loff_t *ppos)
843 {
844         struct ath_softc *sc = file->private_data;
845         struct ath_hw *ah = sc->sc_ah;
846         char buf[32];
847         unsigned int len;
848         u32 regval;
849
850         regval = REG_READ_D(ah, sc->debug.regidx);
851         len = snprintf(buf, sizeof(buf), "0x%08x\n", regval);
852         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
853 }
854
855 static ssize_t write_file_regval(struct file *file, const char __user *user_buf,
856                              size_t count, loff_t *ppos)
857 {
858         struct ath_softc *sc = file->private_data;
859         struct ath_hw *ah = sc->sc_ah;
860         unsigned long regval;
861         char buf[32];
862         ssize_t len;
863
864         len = min(count, sizeof(buf) - 1);
865         if (copy_from_user(buf, user_buf, len))
866                 return -EINVAL;
867
868         buf[len] = '\0';
869         if (strict_strtoul(buf, 0, &regval))
870                 return -EINVAL;
871
872         REG_WRITE_D(ah, sc->debug.regidx, regval);
873         return count;
874 }
875
876 static const struct file_operations fops_regval = {
877         .read = read_file_regval,
878         .write = write_file_regval,
879         .open = ath9k_debugfs_open,
880         .owner = THIS_MODULE
881 };
882
883 int ath9k_init_debug(struct ath_hw *ah)
884 {
885         struct ath_common *common = ath9k_hw_common(ah);
886         struct ath_softc *sc = (struct ath_softc *) common->priv;
887
888         if (!ath9k_debugfs_root)
889                 return -ENOENT;
890
891         sc->debug.debugfs_phy = debugfs_create_dir(wiphy_name(sc->hw->wiphy),
892                                                       ath9k_debugfs_root);
893         if (!sc->debug.debugfs_phy)
894                 return -ENOMEM;
895
896 #ifdef CONFIG_ATH_DEBUG
897         if (!debugfs_create_file("debug", S_IRUSR | S_IWUSR,
898                         sc->debug.debugfs_phy, sc, &fops_debug))
899                 goto err;
900 #endif
901
902         if (!debugfs_create_file("dma", S_IRUSR, sc->debug.debugfs_phy,
903                         sc, &fops_dma))
904                 goto err;
905
906         if (!debugfs_create_file("interrupt", S_IRUSR, sc->debug.debugfs_phy,
907                         sc, &fops_interrupt))
908                 goto err;
909
910         if (!debugfs_create_file("rcstat", S_IRUSR, sc->debug.debugfs_phy,
911                         sc, &fops_rcstat))
912                 goto err;
913
914         if (!debugfs_create_file("wiphy", S_IRUSR | S_IWUSR,
915                         sc->debug.debugfs_phy, sc, &fops_wiphy))
916                 goto err;
917
918         if (!debugfs_create_file("xmit", S_IRUSR, sc->debug.debugfs_phy,
919                         sc, &fops_xmit))
920                 goto err;
921
922         if (!debugfs_create_file("recv", S_IRUSR, sc->debug.debugfs_phy,
923                         sc, &fops_recv))
924                 goto err;
925
926         if (!debugfs_create_file("rx_chainmask", S_IRUSR | S_IWUSR,
927                         sc->debug.debugfs_phy, sc, &fops_rx_chainmask))
928                 goto err;
929
930         if (!debugfs_create_file("tx_chainmask", S_IRUSR | S_IWUSR,
931                         sc->debug.debugfs_phy, sc, &fops_tx_chainmask))
932                 goto err;
933
934         if (!debugfs_create_file("regidx", S_IRUSR | S_IWUSR,
935                         sc->debug.debugfs_phy, sc, &fops_regidx))
936                 goto err;
937
938         if (!debugfs_create_file("regval", S_IRUSR | S_IWUSR,
939                         sc->debug.debugfs_phy, sc, &fops_regval))
940                 goto err;
941
942         sc->debug.regidx = 0;
943         return 0;
944 err:
945         ath9k_exit_debug(ah);
946         return -ENOMEM;
947 }
948
949 void ath9k_exit_debug(struct ath_hw *ah)
950 {
951         struct ath_common *common = ath9k_hw_common(ah);
952         struct ath_softc *sc = (struct ath_softc *) common->priv;
953
954         debugfs_remove_recursive(sc->debug.debugfs_phy);
955 }
956
957 int ath9k_debug_create_root(void)
958 {
959         ath9k_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
960         if (!ath9k_debugfs_root)
961                 return -ENOENT;
962
963         return 0;
964 }
965
966 void ath9k_debug_remove_root(void)
967 {
968         debugfs_remove(ath9k_debugfs_root);
969         ath9k_debugfs_root = NULL;
970 }