xref: /openbmc/linux/drivers/net/wireless/ath/ath9k/debug.c (revision 99c15bf575b18e12c9373304a6a09a78f9c8a017)
1203c4805SLuis R. Rodriguez /*
2203c4805SLuis R. Rodriguez  * Copyright (c) 2008-2009 Atheros Communications Inc.
3203c4805SLuis R. Rodriguez  *
4203c4805SLuis R. Rodriguez  * Permission to use, copy, modify, and/or distribute this software for any
5203c4805SLuis R. Rodriguez  * purpose with or without fee is hereby granted, provided that the above
6203c4805SLuis R. Rodriguez  * copyright notice and this permission notice appear in all copies.
7203c4805SLuis R. Rodriguez  *
8203c4805SLuis R. Rodriguez  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9203c4805SLuis R. Rodriguez  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10203c4805SLuis R. Rodriguez  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11203c4805SLuis R. Rodriguez  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12203c4805SLuis R. Rodriguez  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13203c4805SLuis R. Rodriguez  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14203c4805SLuis R. Rodriguez  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15203c4805SLuis R. Rodriguez  */
16203c4805SLuis R. Rodriguez 
175a0e3ad6STejun Heo #include <linux/slab.h>
18203c4805SLuis R. Rodriguez #include <asm/unaligned.h>
19203c4805SLuis R. Rodriguez 
20203c4805SLuis R. Rodriguez #include "ath9k.h"
21203c4805SLuis R. Rodriguez 
22475a6e4dSLuis R. Rodriguez #define REG_WRITE_D(_ah, _reg, _val) \
23475a6e4dSLuis R. Rodriguez 	ath9k_hw_common(_ah)->ops->write((_ah), (_val), (_reg))
24475a6e4dSLuis R. Rodriguez #define REG_READ_D(_ah, _reg) \
25475a6e4dSLuis R. Rodriguez 	ath9k_hw_common(_ah)->ops->read((_ah), (_reg))
26475a6e4dSLuis R. Rodriguez 
27203c4805SLuis R. Rodriguez static struct dentry *ath9k_debugfs_root;
28203c4805SLuis R. Rodriguez 
29203c4805SLuis R. Rodriguez static int ath9k_debugfs_open(struct inode *inode, struct file *file)
30203c4805SLuis R. Rodriguez {
31203c4805SLuis R. Rodriguez 	file->private_data = inode->i_private;
32203c4805SLuis R. Rodriguez 	return 0;
33203c4805SLuis R. Rodriguez }
34203c4805SLuis R. Rodriguez 
35a830df07SFelix Fietkau #ifdef CONFIG_ATH_DEBUG
36a830df07SFelix Fietkau 
372493928eSJeff Hansen static ssize_t read_file_debug(struct file *file, char __user *user_buf,
382493928eSJeff Hansen 			     size_t count, loff_t *ppos)
392493928eSJeff Hansen {
402493928eSJeff Hansen 	struct ath_softc *sc = file->private_data;
41c46917bbSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
422493928eSJeff Hansen 	char buf[32];
43581f725cSVasanthakumar Thiagarajan 	unsigned int len;
44581f725cSVasanthakumar Thiagarajan 
452b87f3aaSDan Carpenter 	len = sprintf(buf, "0x%08x\n", common->debug_mask);
462493928eSJeff Hansen 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
472493928eSJeff Hansen }
482493928eSJeff Hansen 
492493928eSJeff Hansen static ssize_t write_file_debug(struct file *file, const char __user *user_buf,
502493928eSJeff Hansen 			     size_t count, loff_t *ppos)
512493928eSJeff Hansen {
522493928eSJeff Hansen 	struct ath_softc *sc = file->private_data;
53c46917bbSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
542493928eSJeff Hansen 	unsigned long mask;
552493928eSJeff Hansen 	char buf[32];
56581f725cSVasanthakumar Thiagarajan 	ssize_t len;
57581f725cSVasanthakumar Thiagarajan 
58581f725cSVasanthakumar Thiagarajan 	len = min(count, sizeof(buf) - 1);
59581f725cSVasanthakumar Thiagarajan 	if (copy_from_user(buf, user_buf, len))
6004236066SDan Carpenter 		return -EFAULT;
61581f725cSVasanthakumar Thiagarajan 
62581f725cSVasanthakumar Thiagarajan 	buf[len] = '\0';
63581f725cSVasanthakumar Thiagarajan 	if (strict_strtoul(buf, 0, &mask))
64581f725cSVasanthakumar Thiagarajan 		return -EINVAL;
65581f725cSVasanthakumar Thiagarajan 
66c46917bbSLuis R. Rodriguez 	common->debug_mask = mask;
672493928eSJeff Hansen 	return count;
682493928eSJeff Hansen }
692493928eSJeff Hansen 
702493928eSJeff Hansen static const struct file_operations fops_debug = {
712493928eSJeff Hansen 	.read = read_file_debug,
722493928eSJeff Hansen 	.write = write_file_debug,
732493928eSJeff Hansen 	.open = ath9k_debugfs_open,
742493928eSJeff Hansen 	.owner = THIS_MODULE
752493928eSJeff Hansen };
762493928eSJeff Hansen 
77a830df07SFelix Fietkau #endif
78a830df07SFelix Fietkau 
79991a0987SPavel Roskin #define DMA_BUF_LEN 1024
80991a0987SPavel Roskin 
8115340694SFelix Fietkau static ssize_t read_file_tx_chainmask(struct file *file, char __user *user_buf,
8215340694SFelix Fietkau 			     size_t count, loff_t *ppos)
8315340694SFelix Fietkau {
8415340694SFelix Fietkau 	struct ath_softc *sc = file->private_data;
8515340694SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
8615340694SFelix Fietkau 	char buf[32];
8715340694SFelix Fietkau 	unsigned int len;
8815340694SFelix Fietkau 
892b87f3aaSDan Carpenter 	len = sprintf(buf, "0x%08x\n", common->tx_chainmask);
9015340694SFelix Fietkau 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
9115340694SFelix Fietkau }
9215340694SFelix Fietkau 
9315340694SFelix Fietkau static ssize_t write_file_tx_chainmask(struct file *file, const char __user *user_buf,
9415340694SFelix Fietkau 			     size_t count, loff_t *ppos)
9515340694SFelix Fietkau {
9615340694SFelix Fietkau 	struct ath_softc *sc = file->private_data;
9715340694SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
9815340694SFelix Fietkau 	unsigned long mask;
9915340694SFelix Fietkau 	char buf[32];
10015340694SFelix Fietkau 	ssize_t len;
10115340694SFelix Fietkau 
10215340694SFelix Fietkau 	len = min(count, sizeof(buf) - 1);
10315340694SFelix Fietkau 	if (copy_from_user(buf, user_buf, len))
10404236066SDan Carpenter 		return -EFAULT;
10515340694SFelix Fietkau 
10615340694SFelix Fietkau 	buf[len] = '\0';
10715340694SFelix Fietkau 	if (strict_strtoul(buf, 0, &mask))
10815340694SFelix Fietkau 		return -EINVAL;
10915340694SFelix Fietkau 
11015340694SFelix Fietkau 	common->tx_chainmask = mask;
11115340694SFelix Fietkau 	sc->sc_ah->caps.tx_chainmask = mask;
11215340694SFelix Fietkau 	return count;
11315340694SFelix Fietkau }
11415340694SFelix Fietkau 
11515340694SFelix Fietkau static const struct file_operations fops_tx_chainmask = {
11615340694SFelix Fietkau 	.read = read_file_tx_chainmask,
11715340694SFelix Fietkau 	.write = write_file_tx_chainmask,
11815340694SFelix Fietkau 	.open = ath9k_debugfs_open,
11915340694SFelix Fietkau 	.owner = THIS_MODULE
12015340694SFelix Fietkau };
12115340694SFelix Fietkau 
12215340694SFelix Fietkau 
12315340694SFelix Fietkau static ssize_t read_file_rx_chainmask(struct file *file, char __user *user_buf,
12415340694SFelix Fietkau 			     size_t count, loff_t *ppos)
12515340694SFelix Fietkau {
12615340694SFelix Fietkau 	struct ath_softc *sc = file->private_data;
12715340694SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
12815340694SFelix Fietkau 	char buf[32];
12915340694SFelix Fietkau 	unsigned int len;
13015340694SFelix Fietkau 
1312b87f3aaSDan Carpenter 	len = sprintf(buf, "0x%08x\n", common->rx_chainmask);
13215340694SFelix Fietkau 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
13315340694SFelix Fietkau }
13415340694SFelix Fietkau 
13515340694SFelix Fietkau static ssize_t write_file_rx_chainmask(struct file *file, const char __user *user_buf,
13615340694SFelix Fietkau 			     size_t count, loff_t *ppos)
13715340694SFelix Fietkau {
13815340694SFelix Fietkau 	struct ath_softc *sc = file->private_data;
13915340694SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
14015340694SFelix Fietkau 	unsigned long mask;
14115340694SFelix Fietkau 	char buf[32];
14215340694SFelix Fietkau 	ssize_t len;
14315340694SFelix Fietkau 
14415340694SFelix Fietkau 	len = min(count, sizeof(buf) - 1);
14515340694SFelix Fietkau 	if (copy_from_user(buf, user_buf, len))
14604236066SDan Carpenter 		return -EFAULT;
14715340694SFelix Fietkau 
14815340694SFelix Fietkau 	buf[len] = '\0';
14915340694SFelix Fietkau 	if (strict_strtoul(buf, 0, &mask))
15015340694SFelix Fietkau 		return -EINVAL;
15115340694SFelix Fietkau 
15215340694SFelix Fietkau 	common->rx_chainmask = mask;
15315340694SFelix Fietkau 	sc->sc_ah->caps.rx_chainmask = mask;
15415340694SFelix Fietkau 	return count;
15515340694SFelix Fietkau }
15615340694SFelix Fietkau 
15715340694SFelix Fietkau static const struct file_operations fops_rx_chainmask = {
15815340694SFelix Fietkau 	.read = read_file_rx_chainmask,
15915340694SFelix Fietkau 	.write = write_file_rx_chainmask,
16015340694SFelix Fietkau 	.open = ath9k_debugfs_open,
16115340694SFelix Fietkau 	.owner = THIS_MODULE
16215340694SFelix Fietkau };
16315340694SFelix Fietkau 
16415340694SFelix Fietkau 
165203c4805SLuis R. Rodriguez static ssize_t read_file_dma(struct file *file, char __user *user_buf,
166203c4805SLuis R. Rodriguez 			     size_t count, loff_t *ppos)
167203c4805SLuis R. Rodriguez {
168203c4805SLuis R. Rodriguez 	struct ath_softc *sc = file->private_data;
169203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
170991a0987SPavel Roskin 	char *buf;
171991a0987SPavel Roskin 	int retval;
172203c4805SLuis R. Rodriguez 	unsigned int len = 0;
173203c4805SLuis R. Rodriguez 	u32 val[ATH9K_NUM_DMA_DEBUG_REGS];
174203c4805SLuis R. Rodriguez 	int i, qcuOffset = 0, dcuOffset = 0;
175203c4805SLuis R. Rodriguez 	u32 *qcuBase = &val[0], *dcuBase = &val[4];
176203c4805SLuis R. Rodriguez 
177991a0987SPavel Roskin 	buf = kmalloc(DMA_BUF_LEN, GFP_KERNEL);
178991a0987SPavel Roskin 	if (!buf)
17904236066SDan Carpenter 		return -ENOMEM;
180991a0987SPavel Roskin 
1817cf4a2e7SSujith 	ath9k_ps_wakeup(sc);
1827cf4a2e7SSujith 
183475a6e4dSLuis R. Rodriguez 	REG_WRITE_D(ah, AR_MACMISC,
184203c4805SLuis R. Rodriguez 		  ((AR_MACMISC_DMA_OBS_LINE_8 << AR_MACMISC_DMA_OBS_S) |
185203c4805SLuis R. Rodriguez 		   (AR_MACMISC_MISC_OBS_BUS_1 <<
186203c4805SLuis R. Rodriguez 		    AR_MACMISC_MISC_OBS_BUS_MSB_S)));
187203c4805SLuis R. Rodriguez 
188991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len,
189203c4805SLuis R. Rodriguez 			"Raw DMA Debug values:\n");
190203c4805SLuis R. Rodriguez 
191203c4805SLuis R. Rodriguez 	for (i = 0; i < ATH9K_NUM_DMA_DEBUG_REGS; i++) {
192203c4805SLuis R. Rodriguez 		if (i % 4 == 0)
193991a0987SPavel Roskin 			len += snprintf(buf + len, DMA_BUF_LEN - len, "\n");
194203c4805SLuis R. Rodriguez 
195475a6e4dSLuis R. Rodriguez 		val[i] = REG_READ_D(ah, AR_DMADBG_0 + (i * sizeof(u32)));
196991a0987SPavel Roskin 		len += snprintf(buf + len, DMA_BUF_LEN - len, "%d: %08x ",
197203c4805SLuis R. Rodriguez 				i, val[i]);
198203c4805SLuis R. Rodriguez 	}
199203c4805SLuis R. Rodriguez 
200991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len, "\n\n");
201991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len,
202203c4805SLuis R. Rodriguez 			"Num QCU: chain_st fsp_ok fsp_st DCU: chain_st\n");
203203c4805SLuis R. Rodriguez 
204203c4805SLuis R. Rodriguez 	for (i = 0; i < ATH9K_NUM_QUEUES; i++, qcuOffset += 4, dcuOffset += 5) {
205203c4805SLuis R. Rodriguez 		if (i == 8) {
206203c4805SLuis R. Rodriguez 			qcuOffset = 0;
207203c4805SLuis R. Rodriguez 			qcuBase++;
208203c4805SLuis R. Rodriguez 		}
209203c4805SLuis R. Rodriguez 
210203c4805SLuis R. Rodriguez 		if (i == 6) {
211203c4805SLuis R. Rodriguez 			dcuOffset = 0;
212203c4805SLuis R. Rodriguez 			dcuBase++;
213203c4805SLuis R. Rodriguez 		}
214203c4805SLuis R. Rodriguez 
215991a0987SPavel Roskin 		len += snprintf(buf + len, DMA_BUF_LEN - len,
216203c4805SLuis R. Rodriguez 			"%2d          %2x      %1x     %2x           %2x\n",
217203c4805SLuis R. Rodriguez 			i, (*qcuBase & (0x7 << qcuOffset)) >> qcuOffset,
218203c4805SLuis R. Rodriguez 			(*qcuBase & (0x8 << qcuOffset)) >> (qcuOffset + 3),
219203c4805SLuis R. Rodriguez 			val[2] & (0x7 << (i * 3)) >> (i * 3),
220203c4805SLuis R. Rodriguez 			(*dcuBase & (0x1f << dcuOffset)) >> dcuOffset);
221203c4805SLuis R. Rodriguez 	}
222203c4805SLuis R. Rodriguez 
223991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len, "\n");
224203c4805SLuis R. Rodriguez 
225991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len,
226203c4805SLuis R. Rodriguez 		"qcu_stitch state:   %2x    qcu_fetch state:        %2x\n",
227203c4805SLuis R. Rodriguez 		(val[3] & 0x003c0000) >> 18, (val[3] & 0x03c00000) >> 22);
228991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len,
229203c4805SLuis R. Rodriguez 		"qcu_complete state: %2x    dcu_complete state:     %2x\n",
230203c4805SLuis R. Rodriguez 		(val[3] & 0x1c000000) >> 26, (val[6] & 0x3));
231991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len,
232203c4805SLuis R. Rodriguez 		"dcu_arb state:      %2x    dcu_fp state:           %2x\n",
233203c4805SLuis R. Rodriguez 		(val[5] & 0x06000000) >> 25, (val[5] & 0x38000000) >> 27);
234991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len,
235203c4805SLuis R. Rodriguez 		"chan_idle_dur:     %3d    chan_idle_dur_valid:     %1d\n",
236203c4805SLuis R. Rodriguez 		(val[6] & 0x000003fc) >> 2, (val[6] & 0x00000400) >> 10);
237991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len,
238203c4805SLuis R. Rodriguez 		"txfifo_valid_0:      %1d    txfifo_valid_1:          %1d\n",
239203c4805SLuis R. Rodriguez 		(val[6] & 0x00000800) >> 11, (val[6] & 0x00001000) >> 12);
240991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len,
241203c4805SLuis R. Rodriguez 		"txfifo_dcu_num_0:   %2d    txfifo_dcu_num_1:       %2d\n",
242203c4805SLuis R. Rodriguez 		(val[6] & 0x0001e000) >> 13, (val[6] & 0x001e0000) >> 17);
243203c4805SLuis R. Rodriguez 
244991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len, "pcu observe: 0x%x\n",
245475a6e4dSLuis R. Rodriguez 			REG_READ_D(ah, AR_OBS_BUS_1));
246991a0987SPavel Roskin 	len += snprintf(buf + len, DMA_BUF_LEN - len,
247475a6e4dSLuis R. Rodriguez 			"AR_CR: 0x%x\n", REG_READ_D(ah, AR_CR));
248203c4805SLuis R. Rodriguez 
2497cf4a2e7SSujith 	ath9k_ps_restore(sc);
2507cf4a2e7SSujith 
2512b87f3aaSDan Carpenter 	if (len > DMA_BUF_LEN)
2522b87f3aaSDan Carpenter 		len = DMA_BUF_LEN;
2532b87f3aaSDan Carpenter 
254991a0987SPavel Roskin 	retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
255991a0987SPavel Roskin 	kfree(buf);
256991a0987SPavel Roskin 	return retval;
257203c4805SLuis R. Rodriguez }
258203c4805SLuis R. Rodriguez 
259203c4805SLuis R. Rodriguez static const struct file_operations fops_dma = {
260203c4805SLuis R. Rodriguez 	.read = read_file_dma,
261203c4805SLuis R. Rodriguez 	.open = ath9k_debugfs_open,
262203c4805SLuis R. Rodriguez 	.owner = THIS_MODULE
263203c4805SLuis R. Rodriguez };
264203c4805SLuis R. Rodriguez 
265203c4805SLuis R. Rodriguez 
266203c4805SLuis R. Rodriguez void ath_debug_stat_interrupt(struct ath_softc *sc, enum ath9k_int status)
267203c4805SLuis R. Rodriguez {
268203c4805SLuis R. Rodriguez 	if (status)
269203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.total++;
270a9616f41SLuis R. Rodriguez 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
271a9616f41SLuis R. Rodriguez 		if (status & ATH9K_INT_RXLP)
272a9616f41SLuis R. Rodriguez 			sc->debug.stats.istats.rxlp++;
273a9616f41SLuis R. Rodriguez 		if (status & ATH9K_INT_RXHP)
274a9616f41SLuis R. Rodriguez 			sc->debug.stats.istats.rxhp++;
27508578b8fSLuis R. Rodriguez 		if (status & ATH9K_INT_BB_WATCHDOG)
27608578b8fSLuis R. Rodriguez 			sc->debug.stats.istats.bb_watchdog++;
277a9616f41SLuis R. Rodriguez 	} else {
278203c4805SLuis R. Rodriguez 		if (status & ATH9K_INT_RX)
279203c4805SLuis R. Rodriguez 			sc->debug.stats.istats.rxok++;
280a9616f41SLuis R. Rodriguez 	}
281203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_RXEOL)
282203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.rxeol++;
283203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_RXORN)
284203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.rxorn++;
285203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_TX)
286203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.txok++;
287203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_TXURN)
288203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.txurn++;
289203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_MIB)
290203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.mib++;
291203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_RXPHY)
292203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.rxphyerr++;
293203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_RXKCM)
294203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.rx_keycache_miss++;
295203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_SWBA)
296203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.swba++;
297203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_BMISS)
298203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.bmiss++;
299203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_BNR)
300203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.bnr++;
301203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_CST)
302203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.cst++;
303203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_GTT)
304203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.gtt++;
305203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_TIM)
306203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.tim++;
307203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_CABEND)
308203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.cabend++;
309203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_DTIMSYNC)
310203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.dtimsync++;
311203c4805SLuis R. Rodriguez 	if (status & ATH9K_INT_DTIM)
312203c4805SLuis R. Rodriguez 		sc->debug.stats.istats.dtim++;
313203c4805SLuis R. Rodriguez }
314203c4805SLuis R. Rodriguez 
315203c4805SLuis R. Rodriguez static ssize_t read_file_interrupt(struct file *file, char __user *user_buf,
316203c4805SLuis R. Rodriguez 				   size_t count, loff_t *ppos)
317203c4805SLuis R. Rodriguez {
318203c4805SLuis R. Rodriguez 	struct ath_softc *sc = file->private_data;
319203c4805SLuis R. Rodriguez 	char buf[512];
320203c4805SLuis R. Rodriguez 	unsigned int len = 0;
321203c4805SLuis R. Rodriguez 
322a9616f41SLuis R. Rodriguez 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
323a9616f41SLuis R. Rodriguez 		len += snprintf(buf + len, sizeof(buf) - len,
324a9616f41SLuis R. Rodriguez 			"%8s: %10u\n", "RXLP", sc->debug.stats.istats.rxlp);
325a9616f41SLuis R. Rodriguez 		len += snprintf(buf + len, sizeof(buf) - len,
326a9616f41SLuis R. Rodriguez 			"%8s: %10u\n", "RXHP", sc->debug.stats.istats.rxhp);
32708578b8fSLuis R. Rodriguez 		len += snprintf(buf + len, sizeof(buf) - len,
32808578b8fSLuis R. Rodriguez 			"%8s: %10u\n", "WATCHDOG",
32908578b8fSLuis R. Rodriguez 			sc->debug.stats.istats.bb_watchdog);
330a9616f41SLuis R. Rodriguez 	} else {
331203c4805SLuis R. Rodriguez 		len += snprintf(buf + len, sizeof(buf) - len,
332203c4805SLuis R. Rodriguez 			"%8s: %10u\n", "RX", sc->debug.stats.istats.rxok);
333a9616f41SLuis R. Rodriguez 	}
334203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
335203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "RXEOL", sc->debug.stats.istats.rxeol);
336203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
337203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "RXORN", sc->debug.stats.istats.rxorn);
338203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
339203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "TX", sc->debug.stats.istats.txok);
340203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
341203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "TXURN", sc->debug.stats.istats.txurn);
342203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
343203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "MIB", sc->debug.stats.istats.mib);
344203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
345203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "RXPHY", sc->debug.stats.istats.rxphyerr);
346203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
347203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "RXKCM", sc->debug.stats.istats.rx_keycache_miss);
348203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
349203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "SWBA", sc->debug.stats.istats.swba);
350203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
351203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "BMISS", sc->debug.stats.istats.bmiss);
352203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
353203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "BNR", sc->debug.stats.istats.bnr);
354203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
355203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "CST", sc->debug.stats.istats.cst);
356203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
357203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "GTT", sc->debug.stats.istats.gtt);
358203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
359203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "TIM", sc->debug.stats.istats.tim);
360203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
361203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "CABEND", sc->debug.stats.istats.cabend);
362203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
363203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "DTIMSYNC", sc->debug.stats.istats.dtimsync);
364203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
365203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "DTIM", sc->debug.stats.istats.dtim);
366203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
367203c4805SLuis R. Rodriguez 		"%8s: %10u\n", "TOTAL", sc->debug.stats.istats.total);
368203c4805SLuis R. Rodriguez 
3692b87f3aaSDan Carpenter 	if (len > sizeof(buf))
3702b87f3aaSDan Carpenter 		len = sizeof(buf);
3712b87f3aaSDan Carpenter 
372203c4805SLuis R. Rodriguez 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
373203c4805SLuis R. Rodriguez }
374203c4805SLuis R. Rodriguez 
375203c4805SLuis R. Rodriguez static const struct file_operations fops_interrupt = {
376203c4805SLuis R. Rodriguez 	.read = read_file_interrupt,
377203c4805SLuis R. Rodriguez 	.open = ath9k_debugfs_open,
378203c4805SLuis R. Rodriguez 	.owner = THIS_MODULE
379203c4805SLuis R. Rodriguez };
380203c4805SLuis R. Rodriguez 
381545750d3SFelix Fietkau void ath_debug_stat_rc(struct ath_softc *sc, int final_rate)
382203c4805SLuis R. Rodriguez {
383bedf087aSJeff Hansen 	struct ath_rc_stats *stats;
384203c4805SLuis R. Rodriguez 
385545750d3SFelix Fietkau 	stats = &sc->debug.stats.rcstats[final_rate];
386bedf087aSJeff Hansen 	stats->success++;
387203c4805SLuis R. Rodriguez }
388203c4805SLuis R. Rodriguez 
389203c4805SLuis R. Rodriguez void ath_debug_stat_retries(struct ath_softc *sc, int rix,
390203c4805SLuis R. Rodriguez 			    int xretries, int retries, u8 per)
391203c4805SLuis R. Rodriguez {
392bedf087aSJeff Hansen 	struct ath_rc_stats *stats = &sc->debug.stats.rcstats[rix];
393203c4805SLuis R. Rodriguez 
394bedf087aSJeff Hansen 	stats->xretries += xretries;
395bedf087aSJeff Hansen 	stats->retries += retries;
396bedf087aSJeff Hansen 	stats->per = per;
397203c4805SLuis R. Rodriguez }
398203c4805SLuis R. Rodriguez 
399203c4805SLuis R. Rodriguez static ssize_t read_file_rcstat(struct file *file, char __user *user_buf,
400203c4805SLuis R. Rodriguez 				size_t count, loff_t *ppos)
401203c4805SLuis R. Rodriguez {
402203c4805SLuis R. Rodriguez 	struct ath_softc *sc = file->private_data;
403bedf087aSJeff Hansen 	char *buf;
404bedf087aSJeff Hansen 	unsigned int len = 0, max;
405bedf087aSJeff Hansen 	int i = 0;
406bedf087aSJeff Hansen 	ssize_t retval;
407203c4805SLuis R. Rodriguez 
408203c4805SLuis R. Rodriguez 	if (sc->cur_rate_table == NULL)
409203c4805SLuis R. Rodriguez 		return 0;
410203c4805SLuis R. Rodriguez 
4112b87f3aaSDan Carpenter 	max = 80 + sc->cur_rate_table->rate_cnt * 1024 + 1;
4122b87f3aaSDan Carpenter 	buf = kmalloc(max, GFP_KERNEL);
413bedf087aSJeff Hansen 	if (buf == NULL)
41404236066SDan Carpenter 		return -ENOMEM;
415bedf087aSJeff Hansen 
416c755ad34SLuis R. Rodriguez 	len += sprintf(buf, "%6s %6s %6s "
417c755ad34SLuis R. Rodriguez 		       "%10s %10s %10s %10s\n",
418c755ad34SLuis R. Rodriguez 		       "HT", "MCS", "Rate",
419c755ad34SLuis R. Rodriguez 		       "Success", "Retries", "XRetries", "PER");
420bedf087aSJeff Hansen 
421bedf087aSJeff Hansen 	for (i = 0; i < sc->cur_rate_table->rate_cnt; i++) {
422bedf087aSJeff Hansen 		u32 ratekbps = sc->cur_rate_table->info[i].ratekbps;
423bedf087aSJeff Hansen 		struct ath_rc_stats *stats = &sc->debug.stats.rcstats[i];
424c755ad34SLuis R. Rodriguez 		char mcs[5];
425c755ad34SLuis R. Rodriguez 		char htmode[5];
426c755ad34SLuis R. Rodriguez 		int used_mcs = 0, used_htmode = 0;
427c755ad34SLuis R. Rodriguez 
428c755ad34SLuis R. Rodriguez 		if (WLAN_RC_PHY_HT(sc->cur_rate_table->info[i].phy)) {
429c755ad34SLuis R. Rodriguez 			used_mcs = snprintf(mcs, 5, "%d",
430c755ad34SLuis R. Rodriguez 				sc->cur_rate_table->info[i].ratecode);
431c755ad34SLuis R. Rodriguez 
432c755ad34SLuis R. Rodriguez 			if (WLAN_RC_PHY_40(sc->cur_rate_table->info[i].phy))
433c755ad34SLuis R. Rodriguez 				used_htmode = snprintf(htmode, 5, "HT40");
434c755ad34SLuis R. Rodriguez 			else if (WLAN_RC_PHY_20(sc->cur_rate_table->info[i].phy))
435c755ad34SLuis R. Rodriguez 				used_htmode = snprintf(htmode, 5, "HT20");
436c755ad34SLuis R. Rodriguez 			else
437c755ad34SLuis R. Rodriguez 				used_htmode = snprintf(htmode, 5, "????");
438c755ad34SLuis R. Rodriguez 		}
439c755ad34SLuis R. Rodriguez 
440c755ad34SLuis R. Rodriguez 		mcs[used_mcs] = '\0';
441c755ad34SLuis R. Rodriguez 		htmode[used_htmode] = '\0';
442bedf087aSJeff Hansen 
443bedf087aSJeff Hansen 		len += snprintf(buf + len, max - len,
444c755ad34SLuis R. Rodriguez 			"%6s %6s %3u.%d: "
445c755ad34SLuis R. Rodriguez 			"%10u %10u %10u %10u\n",
446c755ad34SLuis R. Rodriguez 			htmode,
447c755ad34SLuis R. Rodriguez 			mcs,
448c755ad34SLuis R. Rodriguez 			ratekbps / 1000,
449c755ad34SLuis R. Rodriguez 			(ratekbps % 1000) / 100,
450c755ad34SLuis R. Rodriguez 			stats->success,
451c755ad34SLuis R. Rodriguez 			stats->retries,
452c755ad34SLuis R. Rodriguez 			stats->xretries,
453bedf087aSJeff Hansen 			stats->per);
454bedf087aSJeff Hansen 	}
455bedf087aSJeff Hansen 
4562b87f3aaSDan Carpenter 	if (len > max)
4572b87f3aaSDan Carpenter 		len = max;
4582b87f3aaSDan Carpenter 
459bedf087aSJeff Hansen 	retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
460bedf087aSJeff Hansen 	kfree(buf);
461bedf087aSJeff Hansen 	return retval;
462203c4805SLuis R. Rodriguez }
463203c4805SLuis R. Rodriguez 
464203c4805SLuis R. Rodriguez static const struct file_operations fops_rcstat = {
465203c4805SLuis R. Rodriguez 	.read = read_file_rcstat,
466203c4805SLuis R. Rodriguez 	.open = ath9k_debugfs_open,
467203c4805SLuis R. Rodriguez 	.owner = THIS_MODULE
468203c4805SLuis R. Rodriguez };
469203c4805SLuis R. Rodriguez 
470203c4805SLuis R. Rodriguez static const char * ath_wiphy_state_str(enum ath_wiphy_state state)
471203c4805SLuis R. Rodriguez {
472203c4805SLuis R. Rodriguez 	switch (state) {
473203c4805SLuis R. Rodriguez 	case ATH_WIPHY_INACTIVE:
474203c4805SLuis R. Rodriguez 		return "INACTIVE";
475203c4805SLuis R. Rodriguez 	case ATH_WIPHY_ACTIVE:
476203c4805SLuis R. Rodriguez 		return "ACTIVE";
477203c4805SLuis R. Rodriguez 	case ATH_WIPHY_PAUSING:
478203c4805SLuis R. Rodriguez 		return "PAUSING";
479203c4805SLuis R. Rodriguez 	case ATH_WIPHY_PAUSED:
480203c4805SLuis R. Rodriguez 		return "PAUSED";
481203c4805SLuis R. Rodriguez 	case ATH_WIPHY_SCAN:
482203c4805SLuis R. Rodriguez 		return "SCAN";
483203c4805SLuis R. Rodriguez 	}
484203c4805SLuis R. Rodriguez 	return "?";
485203c4805SLuis R. Rodriguez }
486203c4805SLuis R. Rodriguez 
487203c4805SLuis R. Rodriguez static ssize_t read_file_wiphy(struct file *file, char __user *user_buf,
488203c4805SLuis R. Rodriguez 			       size_t count, loff_t *ppos)
489203c4805SLuis R. Rodriguez {
490203c4805SLuis R. Rodriguez 	struct ath_softc *sc = file->private_data;
4918e0167a4SMohammed Shafi Shajakhan 	struct ath_wiphy *aphy = sc->pri_wiphy;
4928e0167a4SMohammed Shafi Shajakhan 	struct ieee80211_channel *chan = aphy->hw->conf.channel;
493203c4805SLuis R. Rodriguez 	char buf[512];
494203c4805SLuis R. Rodriguez 	unsigned int len = 0;
495203c4805SLuis R. Rodriguez 	int i;
496203c4805SLuis R. Rodriguez 	u8 addr[ETH_ALEN];
49739057512SBen Greear 	u32 tmp;
498203c4805SLuis R. Rodriguez 
499203c4805SLuis R. Rodriguez 	len += snprintf(buf + len, sizeof(buf) - len,
500203c4805SLuis R. Rodriguez 			"primary: %s (%s chan=%d ht=%d)\n",
501203c4805SLuis R. Rodriguez 			wiphy_name(sc->pri_wiphy->hw->wiphy),
502203c4805SLuis R. Rodriguez 			ath_wiphy_state_str(sc->pri_wiphy->state),
5038e0167a4SMohammed Shafi Shajakhan 			ieee80211_frequency_to_channel(chan->center_freq),
5048e0167a4SMohammed Shafi Shajakhan 			aphy->chan_is_ht);
50539057512SBen Greear 
50639057512SBen Greear 	put_unaligned_le32(REG_READ_D(sc->sc_ah, AR_STA_ID0), addr);
50739057512SBen Greear 	put_unaligned_le16(REG_READ_D(sc->sc_ah, AR_STA_ID1) & 0xffff, addr + 4);
50839057512SBen Greear 	len += snprintf(buf + len, sizeof(buf) - len,
50939057512SBen Greear 			"addr: %pM\n", addr);
51039057512SBen Greear 	put_unaligned_le32(REG_READ_D(sc->sc_ah, AR_BSSMSKL), addr);
51139057512SBen Greear 	put_unaligned_le16(REG_READ_D(sc->sc_ah, AR_BSSMSKU) & 0xffff, addr + 4);
51239057512SBen Greear 	len += snprintf(buf + len, sizeof(buf) - len,
51339057512SBen Greear 			"addrmask: %pM\n", addr);
51439057512SBen Greear 	tmp = ath9k_hw_getrxfilter(sc->sc_ah);
51539057512SBen Greear 	len += snprintf(buf + len, sizeof(buf) - len,
51639057512SBen Greear 			"rfilt: 0x%x", tmp);
51739057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_UCAST)
51839057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " UCAST");
51939057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_MCAST)
52039057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " MCAST");
52139057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_BCAST)
52239057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " BCAST");
52339057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_CONTROL)
52439057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " CONTROL");
52539057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_BEACON)
52639057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " BEACON");
52739057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_PROM)
52839057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " PROM");
52939057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_PROBEREQ)
53039057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " PROBEREQ");
53139057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_PHYERR)
53239057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " PHYERR");
53339057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_MYBEACON)
53439057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " MYBEACON");
53539057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_COMP_BAR)
53639057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " COMP_BAR");
53739057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_PSPOLL)
53839057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " PSPOLL");
53939057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_PHYRADAR)
54039057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " PHYRADAR");
54139057512SBen Greear 	if (tmp & ATH9K_RX_FILTER_MCAST_BCAST_ALL)
54239057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, " MCAST_BCAST_ALL\n");
54339057512SBen Greear 	else
54439057512SBen Greear 		len += snprintf(buf + len, sizeof(buf) - len, "\n");
54539057512SBen Greear 
54639057512SBen Greear 	/* Put variable-length stuff down here, and check for overflows. */
547203c4805SLuis R. Rodriguez 	for (i = 0; i < sc->num_sec_wiphy; i++) {
548203c4805SLuis R. Rodriguez 		struct ath_wiphy *aphy = sc->sec_wiphy[i];
549203c4805SLuis R. Rodriguez 		if (aphy == NULL)
550203c4805SLuis R. Rodriguez 			continue;
5518e0167a4SMohammed Shafi Shajakhan 		chan = aphy->hw->conf.channel;
552203c4805SLuis R. Rodriguez 		len += snprintf(buf + len, sizeof(buf) - len,
553203c4805SLuis R. Rodriguez 			"secondary: %s (%s chan=%d ht=%d)\n",
554203c4805SLuis R. Rodriguez 			wiphy_name(aphy->hw->wiphy),
555203c4805SLuis R. Rodriguez 			ath_wiphy_state_str(aphy->state),
5568e0167a4SMohammed Shafi Shajakhan 			ieee80211_frequency_to_channel(chan->center_freq),
5578e0167a4SMohammed Shafi Shajakhan 			aphy->chan_is_ht);
558203c4805SLuis R. Rodriguez 	}
5592b87f3aaSDan Carpenter 	if (len > sizeof(buf))
5602b87f3aaSDan Carpenter 		len = sizeof(buf);
5612b87f3aaSDan Carpenter 
562203c4805SLuis R. Rodriguez 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
563203c4805SLuis R. Rodriguez }
564203c4805SLuis R. Rodriguez 
565203c4805SLuis R. Rodriguez static struct ath_wiphy * get_wiphy(struct ath_softc *sc, const char *name)
566203c4805SLuis R. Rodriguez {
567203c4805SLuis R. Rodriguez 	int i;
568203c4805SLuis R. Rodriguez 	if (strcmp(name, wiphy_name(sc->pri_wiphy->hw->wiphy)) == 0)
569203c4805SLuis R. Rodriguez 		return sc->pri_wiphy;
570203c4805SLuis R. Rodriguez 	for (i = 0; i < sc->num_sec_wiphy; i++) {
571203c4805SLuis R. Rodriguez 		struct ath_wiphy *aphy = sc->sec_wiphy[i];
572203c4805SLuis R. Rodriguez 		if (aphy && strcmp(name, wiphy_name(aphy->hw->wiphy)) == 0)
573203c4805SLuis R. Rodriguez 			return aphy;
574203c4805SLuis R. Rodriguez 	}
575203c4805SLuis R. Rodriguez 	return NULL;
576203c4805SLuis R. Rodriguez }
577203c4805SLuis R. Rodriguez 
578203c4805SLuis R. Rodriguez static int del_wiphy(struct ath_softc *sc, const char *name)
579203c4805SLuis R. Rodriguez {
580203c4805SLuis R. Rodriguez 	struct ath_wiphy *aphy = get_wiphy(sc, name);
581203c4805SLuis R. Rodriguez 	if (!aphy)
582203c4805SLuis R. Rodriguez 		return -ENOENT;
583203c4805SLuis R. Rodriguez 	return ath9k_wiphy_del(aphy);
584203c4805SLuis R. Rodriguez }
585203c4805SLuis R. Rodriguez 
586203c4805SLuis R. Rodriguez static int pause_wiphy(struct ath_softc *sc, const char *name)
587203c4805SLuis R. Rodriguez {
588203c4805SLuis R. Rodriguez 	struct ath_wiphy *aphy = get_wiphy(sc, name);
589203c4805SLuis R. Rodriguez 	if (!aphy)
590203c4805SLuis R. Rodriguez 		return -ENOENT;
591203c4805SLuis R. Rodriguez 	return ath9k_wiphy_pause(aphy);
592203c4805SLuis R. Rodriguez }
593203c4805SLuis R. Rodriguez 
594203c4805SLuis R. Rodriguez static int unpause_wiphy(struct ath_softc *sc, const char *name)
595203c4805SLuis R. Rodriguez {
596203c4805SLuis R. Rodriguez 	struct ath_wiphy *aphy = get_wiphy(sc, name);
597203c4805SLuis R. Rodriguez 	if (!aphy)
598203c4805SLuis R. Rodriguez 		return -ENOENT;
599203c4805SLuis R. Rodriguez 	return ath9k_wiphy_unpause(aphy);
600203c4805SLuis R. Rodriguez }
601203c4805SLuis R. Rodriguez 
602203c4805SLuis R. Rodriguez static int select_wiphy(struct ath_softc *sc, const char *name)
603203c4805SLuis R. Rodriguez {
604203c4805SLuis R. Rodriguez 	struct ath_wiphy *aphy = get_wiphy(sc, name);
605203c4805SLuis R. Rodriguez 	if (!aphy)
606203c4805SLuis R. Rodriguez 		return -ENOENT;
607203c4805SLuis R. Rodriguez 	return ath9k_wiphy_select(aphy);
608203c4805SLuis R. Rodriguez }
609203c4805SLuis R. Rodriguez 
610203c4805SLuis R. Rodriguez static int schedule_wiphy(struct ath_softc *sc, const char *msec)
611203c4805SLuis R. Rodriguez {
612203c4805SLuis R. Rodriguez 	ath9k_wiphy_set_scheduler(sc, simple_strtoul(msec, NULL, 0));
613203c4805SLuis R. Rodriguez 	return 0;
614203c4805SLuis R. Rodriguez }
615203c4805SLuis R. Rodriguez 
616203c4805SLuis R. Rodriguez static ssize_t write_file_wiphy(struct file *file, const char __user *user_buf,
617203c4805SLuis R. Rodriguez 				size_t count, loff_t *ppos)
618203c4805SLuis R. Rodriguez {
619203c4805SLuis R. Rodriguez 	struct ath_softc *sc = file->private_data;
620203c4805SLuis R. Rodriguez 	char buf[50];
621203c4805SLuis R. Rodriguez 	size_t len;
622203c4805SLuis R. Rodriguez 
623203c4805SLuis R. Rodriguez 	len = min(count, sizeof(buf) - 1);
624203c4805SLuis R. Rodriguez 	if (copy_from_user(buf, user_buf, len))
625203c4805SLuis R. Rodriguez 		return -EFAULT;
626203c4805SLuis R. Rodriguez 	buf[len] = '\0';
627203c4805SLuis R. Rodriguez 	if (len > 0 && buf[len - 1] == '\n')
628203c4805SLuis R. Rodriguez 		buf[len - 1] = '\0';
629203c4805SLuis R. Rodriguez 
630203c4805SLuis R. Rodriguez 	if (strncmp(buf, "add", 3) == 0) {
631203c4805SLuis R. Rodriguez 		int res = ath9k_wiphy_add(sc);
632203c4805SLuis R. Rodriguez 		if (res < 0)
633203c4805SLuis R. Rodriguez 			return res;
634203c4805SLuis R. Rodriguez 	} else if (strncmp(buf, "del=", 4) == 0) {
635203c4805SLuis R. Rodriguez 		int res = del_wiphy(sc, buf + 4);
636203c4805SLuis R. Rodriguez 		if (res < 0)
637203c4805SLuis R. Rodriguez 			return res;
638203c4805SLuis R. Rodriguez 	} else if (strncmp(buf, "pause=", 6) == 0) {
639203c4805SLuis R. Rodriguez 		int res = pause_wiphy(sc, buf + 6);
640203c4805SLuis R. Rodriguez 		if (res < 0)
641203c4805SLuis R. Rodriguez 			return res;
642203c4805SLuis R. Rodriguez 	} else if (strncmp(buf, "unpause=", 8) == 0) {
643203c4805SLuis R. Rodriguez 		int res = unpause_wiphy(sc, buf + 8);
644203c4805SLuis R. Rodriguez 		if (res < 0)
645203c4805SLuis R. Rodriguez 			return res;
646203c4805SLuis R. Rodriguez 	} else if (strncmp(buf, "select=", 7) == 0) {
647203c4805SLuis R. Rodriguez 		int res = select_wiphy(sc, buf + 7);
648203c4805SLuis R. Rodriguez 		if (res < 0)
649203c4805SLuis R. Rodriguez 			return res;
650203c4805SLuis R. Rodriguez 	} else if (strncmp(buf, "schedule=", 9) == 0) {
651203c4805SLuis R. Rodriguez 		int res = schedule_wiphy(sc, buf + 9);
652203c4805SLuis R. Rodriguez 		if (res < 0)
653203c4805SLuis R. Rodriguez 			return res;
654203c4805SLuis R. Rodriguez 	} else
655203c4805SLuis R. Rodriguez 		return -EOPNOTSUPP;
656203c4805SLuis R. Rodriguez 
657203c4805SLuis R. Rodriguez 	return count;
658203c4805SLuis R. Rodriguez }
659203c4805SLuis R. Rodriguez 
660203c4805SLuis R. Rodriguez static const struct file_operations fops_wiphy = {
661203c4805SLuis R. Rodriguez 	.read = read_file_wiphy,
662203c4805SLuis R. Rodriguez 	.write = write_file_wiphy,
663203c4805SLuis R. Rodriguez 	.open = ath9k_debugfs_open,
664203c4805SLuis R. Rodriguez 	.owner = THIS_MODULE
665203c4805SLuis R. Rodriguez };
666203c4805SLuis R. Rodriguez 
667fec247c0SSujith #define PR(str, elem)							\
668fec247c0SSujith 	do {								\
669fec247c0SSujith 		len += snprintf(buf + len, size - len,			\
670fec247c0SSujith 				"%s%13u%11u%10u%10u\n", str,		\
6711d2231e2SFelix Fietkau 		sc->debug.stats.txstats[sc->tx.hwq_map[WME_AC_BE]].elem, \
6721d2231e2SFelix Fietkau 		sc->debug.stats.txstats[sc->tx.hwq_map[WME_AC_BK]].elem, \
6731d2231e2SFelix Fietkau 		sc->debug.stats.txstats[sc->tx.hwq_map[WME_AC_VI]].elem, \
6741d2231e2SFelix Fietkau 		sc->debug.stats.txstats[sc->tx.hwq_map[WME_AC_VO]].elem); \
675fec247c0SSujith } while(0)
676fec247c0SSujith 
677fec247c0SSujith static ssize_t read_file_xmit(struct file *file, char __user *user_buf,
678fec247c0SSujith 			      size_t count, loff_t *ppos)
679fec247c0SSujith {
680fec247c0SSujith 	struct ath_softc *sc = file->private_data;
681fec247c0SSujith 	char *buf;
682fec247c0SSujith 	unsigned int len = 0, size = 2048;
683fec247c0SSujith 	ssize_t retval = 0;
684fec247c0SSujith 
685fec247c0SSujith 	buf = kzalloc(size, GFP_KERNEL);
686fec247c0SSujith 	if (buf == NULL)
68704236066SDan Carpenter 		return -ENOMEM;
688fec247c0SSujith 
689fec247c0SSujith 	len += sprintf(buf, "%30s %10s%10s%10s\n\n", "BE", "BK", "VI", "VO");
690fec247c0SSujith 
691fec247c0SSujith 	PR("MPDUs Queued:    ", queued);
692fec247c0SSujith 	PR("MPDUs Completed: ", completed);
693fec247c0SSujith 	PR("Aggregates:      ", a_aggr);
694fec247c0SSujith 	PR("AMPDUs Queued:   ", a_queued);
695fec247c0SSujith 	PR("AMPDUs Completed:", a_completed);
696fec247c0SSujith 	PR("AMPDUs Retried:  ", a_retries);
697fec247c0SSujith 	PR("AMPDUs XRetried: ", a_xretries);
698fec247c0SSujith 	PR("FIFO Underrun:   ", fifo_underrun);
699fec247c0SSujith 	PR("TXOP Exceeded:   ", xtxop);
700fec247c0SSujith 	PR("TXTIMER Expiry:  ", timer_exp);
701fec247c0SSujith 	PR("DESC CFG Error:  ", desc_cfg_err);
702fec247c0SSujith 	PR("DATA Underrun:   ", data_underrun);
703fec247c0SSujith 	PR("DELIM Underrun:  ", delim_underrun);
704*99c15bf5SBen Greear 	PR("TX-Pkts-All:     ", tx_pkts_all);
705*99c15bf5SBen Greear 	PR("TX-Bytes-All:    ", tx_bytes_all);
706fec247c0SSujith 
7072b87f3aaSDan Carpenter 	if (len > size)
7082b87f3aaSDan Carpenter 		len = size;
7092b87f3aaSDan Carpenter 
710fec247c0SSujith 	retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
711fec247c0SSujith 	kfree(buf);
712fec247c0SSujith 
713fec247c0SSujith 	return retval;
714fec247c0SSujith }
715fec247c0SSujith 
716fec247c0SSujith void ath_debug_stat_tx(struct ath_softc *sc, struct ath_txq *txq,
717db1a052bSFelix Fietkau 		       struct ath_buf *bf, struct ath_tx_status *ts)
718fec247c0SSujith {
719*99c15bf5SBen Greear 	TX_STAT_INC(txq->axq_qnum, tx_pkts_all);
720*99c15bf5SBen Greear 	sc->debug.stats.txstats[txq->axq_qnum].tx_bytes_all += bf->bf_mpdu->len;
721*99c15bf5SBen Greear 
722fec247c0SSujith 	if (bf_isampdu(bf)) {
723fec247c0SSujith 		if (bf_isxretried(bf))
724fec247c0SSujith 			TX_STAT_INC(txq->axq_qnum, a_xretries);
725fec247c0SSujith 		else
726fec247c0SSujith 			TX_STAT_INC(txq->axq_qnum, a_completed);
727fec247c0SSujith 	} else {
728fec247c0SSujith 		TX_STAT_INC(txq->axq_qnum, completed);
729fec247c0SSujith 	}
730fec247c0SSujith 
731db1a052bSFelix Fietkau 	if (ts->ts_status & ATH9K_TXERR_FIFO)
732fec247c0SSujith 		TX_STAT_INC(txq->axq_qnum, fifo_underrun);
733db1a052bSFelix Fietkau 	if (ts->ts_status & ATH9K_TXERR_XTXOP)
734fec247c0SSujith 		TX_STAT_INC(txq->axq_qnum, xtxop);
735db1a052bSFelix Fietkau 	if (ts->ts_status & ATH9K_TXERR_TIMER_EXPIRED)
736fec247c0SSujith 		TX_STAT_INC(txq->axq_qnum, timer_exp);
737db1a052bSFelix Fietkau 	if (ts->ts_flags & ATH9K_TX_DESC_CFG_ERR)
738fec247c0SSujith 		TX_STAT_INC(txq->axq_qnum, desc_cfg_err);
739db1a052bSFelix Fietkau 	if (ts->ts_flags & ATH9K_TX_DATA_UNDERRUN)
740fec247c0SSujith 		TX_STAT_INC(txq->axq_qnum, data_underrun);
741db1a052bSFelix Fietkau 	if (ts->ts_flags & ATH9K_TX_DELIM_UNDERRUN)
742fec247c0SSujith 		TX_STAT_INC(txq->axq_qnum, delim_underrun);
743fec247c0SSujith }
744fec247c0SSujith 
745fec247c0SSujith static const struct file_operations fops_xmit = {
746fec247c0SSujith 	.read = read_file_xmit,
747fec247c0SSujith 	.open = ath9k_debugfs_open,
748fec247c0SSujith 	.owner = THIS_MODULE
749fec247c0SSujith };
750203c4805SLuis R. Rodriguez 
7511395d3f0SSujith static ssize_t read_file_recv(struct file *file, char __user *user_buf,
7521395d3f0SSujith 			      size_t count, loff_t *ppos)
7531395d3f0SSujith {
7541395d3f0SSujith #define PHY_ERR(s, p) \
7551395d3f0SSujith 	len += snprintf(buf + len, size - len, "%18s : %10u\n", s, \
7561395d3f0SSujith 			sc->debug.stats.rxstats.phy_err_stats[p]);
7571395d3f0SSujith 
7581395d3f0SSujith 	struct ath_softc *sc = file->private_data;
7591395d3f0SSujith 	char *buf;
7601395d3f0SSujith 	unsigned int len = 0, size = 1152;
7611395d3f0SSujith 	ssize_t retval = 0;
7621395d3f0SSujith 
7631395d3f0SSujith 	buf = kzalloc(size, GFP_KERNEL);
7641395d3f0SSujith 	if (buf == NULL)
76504236066SDan Carpenter 		return -ENOMEM;
7661395d3f0SSujith 
7671395d3f0SSujith 	len += snprintf(buf + len, size - len,
7681395d3f0SSujith 			"%18s : %10u\n", "CRC ERR",
7691395d3f0SSujith 			sc->debug.stats.rxstats.crc_err);
7701395d3f0SSujith 	len += snprintf(buf + len, size - len,
7711395d3f0SSujith 			"%18s : %10u\n", "DECRYPT CRC ERR",
7721395d3f0SSujith 			sc->debug.stats.rxstats.decrypt_crc_err);
7731395d3f0SSujith 	len += snprintf(buf + len, size - len,
7741395d3f0SSujith 			"%18s : %10u\n", "PHY ERR",
7751395d3f0SSujith 			sc->debug.stats.rxstats.phy_err);
7761395d3f0SSujith 	len += snprintf(buf + len, size - len,
7771395d3f0SSujith 			"%18s : %10u\n", "MIC ERR",
7781395d3f0SSujith 			sc->debug.stats.rxstats.mic_err);
7791395d3f0SSujith 	len += snprintf(buf + len, size - len,
7801395d3f0SSujith 			"%18s : %10u\n", "PRE-DELIM CRC ERR",
7811395d3f0SSujith 			sc->debug.stats.rxstats.pre_delim_crc_err);
7821395d3f0SSujith 	len += snprintf(buf + len, size - len,
7831395d3f0SSujith 			"%18s : %10u\n", "POST-DELIM CRC ERR",
7841395d3f0SSujith 			sc->debug.stats.rxstats.post_delim_crc_err);
7851395d3f0SSujith 	len += snprintf(buf + len, size - len,
7861395d3f0SSujith 			"%18s : %10u\n", "DECRYPT BUSY ERR",
7871395d3f0SSujith 			sc->debug.stats.rxstats.decrypt_busy_err);
7881395d3f0SSujith 
7891395d3f0SSujith 	PHY_ERR("UNDERRUN", ATH9K_PHYERR_UNDERRUN);
7901395d3f0SSujith 	PHY_ERR("TIMING", ATH9K_PHYERR_TIMING);
7911395d3f0SSujith 	PHY_ERR("PARITY", ATH9K_PHYERR_PARITY);
7921395d3f0SSujith 	PHY_ERR("RATE", ATH9K_PHYERR_RATE);
7931395d3f0SSujith 	PHY_ERR("LENGTH", ATH9K_PHYERR_LENGTH);
7941395d3f0SSujith 	PHY_ERR("RADAR", ATH9K_PHYERR_RADAR);
7951395d3f0SSujith 	PHY_ERR("SERVICE", ATH9K_PHYERR_SERVICE);
7961395d3f0SSujith 	PHY_ERR("TOR", ATH9K_PHYERR_TOR);
7971395d3f0SSujith 	PHY_ERR("OFDM-TIMING", ATH9K_PHYERR_OFDM_TIMING);
7981395d3f0SSujith 	PHY_ERR("OFDM-SIGNAL-PARITY", ATH9K_PHYERR_OFDM_SIGNAL_PARITY);
7991395d3f0SSujith 	PHY_ERR("OFDM-RATE", ATH9K_PHYERR_OFDM_RATE_ILLEGAL);
8001395d3f0SSujith 	PHY_ERR("OFDM-LENGTH", ATH9K_PHYERR_OFDM_LENGTH_ILLEGAL);
8011395d3f0SSujith 	PHY_ERR("OFDM-POWER-DROP", ATH9K_PHYERR_OFDM_POWER_DROP);
8021395d3f0SSujith 	PHY_ERR("OFDM-SERVICE", ATH9K_PHYERR_OFDM_SERVICE);
8031395d3f0SSujith 	PHY_ERR("OFDM-RESTART", ATH9K_PHYERR_OFDM_RESTART);
8041395d3f0SSujith 	PHY_ERR("FALSE-RADAR-EXT", ATH9K_PHYERR_FALSE_RADAR_EXT);
8051395d3f0SSujith 	PHY_ERR("CCK-TIMING", ATH9K_PHYERR_CCK_TIMING);
8061395d3f0SSujith 	PHY_ERR("CCK-HEADER-CRC", ATH9K_PHYERR_CCK_HEADER_CRC);
8071395d3f0SSujith 	PHY_ERR("CCK-RATE", ATH9K_PHYERR_CCK_RATE_ILLEGAL);
8081395d3f0SSujith 	PHY_ERR("CCK-SERVICE", ATH9K_PHYERR_CCK_SERVICE);
8091395d3f0SSujith 	PHY_ERR("CCK-RESTART", ATH9K_PHYERR_CCK_RESTART);
8101395d3f0SSujith 	PHY_ERR("CCK-LENGTH", ATH9K_PHYERR_CCK_LENGTH_ILLEGAL);
8111395d3f0SSujith 	PHY_ERR("CCK-POWER-DROP", ATH9K_PHYERR_CCK_POWER_DROP);
8121395d3f0SSujith 	PHY_ERR("HT-CRC", ATH9K_PHYERR_HT_CRC_ERROR);
8131395d3f0SSujith 	PHY_ERR("HT-LENGTH", ATH9K_PHYERR_HT_LENGTH_ILLEGAL);
8141395d3f0SSujith 	PHY_ERR("HT-RATE", ATH9K_PHYERR_HT_RATE_ILLEGAL);
8151395d3f0SSujith 
816*99c15bf5SBen Greear 	len += snprintf(buf + len, size - len,
817*99c15bf5SBen Greear 			"%18s : %10u\n", "RX-Pkts-All",
818*99c15bf5SBen Greear 			sc->debug.stats.rxstats.rx_pkts_all);
819*99c15bf5SBen Greear 	len += snprintf(buf + len, size - len,
820*99c15bf5SBen Greear 			"%18s : %10u\n", "RX-Bytes-All",
821*99c15bf5SBen Greear 			sc->debug.stats.rxstats.rx_bytes_all);
822*99c15bf5SBen Greear 
8232b87f3aaSDan Carpenter 	if (len > size)
8242b87f3aaSDan Carpenter 		len = size;
8252b87f3aaSDan Carpenter 
8261395d3f0SSujith 	retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
8271395d3f0SSujith 	kfree(buf);
8281395d3f0SSujith 
8291395d3f0SSujith 	return retval;
8301395d3f0SSujith 
8311395d3f0SSujith #undef PHY_ERR
8321395d3f0SSujith }
8331395d3f0SSujith 
8348e6f5aa2SFelix Fietkau void ath_debug_stat_rx(struct ath_softc *sc, struct ath_rx_status *rs)
8351395d3f0SSujith {
8361395d3f0SSujith #define RX_STAT_INC(c) sc->debug.stats.rxstats.c++
8371395d3f0SSujith #define RX_PHY_ERR_INC(c) sc->debug.stats.rxstats.phy_err_stats[c]++
8381395d3f0SSujith 
8391395d3f0SSujith 	u32 phyerr;
8401395d3f0SSujith 
841*99c15bf5SBen Greear 	RX_STAT_INC(rx_pkts_all);
842*99c15bf5SBen Greear 	sc->debug.stats.rxstats.rx_bytes_all += rs->rs_datalen;
843*99c15bf5SBen Greear 
8448e6f5aa2SFelix Fietkau 	if (rs->rs_status & ATH9K_RXERR_CRC)
8451395d3f0SSujith 		RX_STAT_INC(crc_err);
8468e6f5aa2SFelix Fietkau 	if (rs->rs_status & ATH9K_RXERR_DECRYPT)
8471395d3f0SSujith 		RX_STAT_INC(decrypt_crc_err);
8488e6f5aa2SFelix Fietkau 	if (rs->rs_status & ATH9K_RXERR_MIC)
8491395d3f0SSujith 		RX_STAT_INC(mic_err);
8508e6f5aa2SFelix Fietkau 	if (rs->rs_status & ATH9K_RX_DELIM_CRC_PRE)
8511395d3f0SSujith 		RX_STAT_INC(pre_delim_crc_err);
8528e6f5aa2SFelix Fietkau 	if (rs->rs_status & ATH9K_RX_DELIM_CRC_POST)
8531395d3f0SSujith 		RX_STAT_INC(post_delim_crc_err);
8548e6f5aa2SFelix Fietkau 	if (rs->rs_status & ATH9K_RX_DECRYPT_BUSY)
8551395d3f0SSujith 		RX_STAT_INC(decrypt_busy_err);
8561395d3f0SSujith 
8578e6f5aa2SFelix Fietkau 	if (rs->rs_status & ATH9K_RXERR_PHY) {
8581395d3f0SSujith 		RX_STAT_INC(phy_err);
8598e6f5aa2SFelix Fietkau 		phyerr = rs->rs_phyerr & 0x24;
8601395d3f0SSujith 		RX_PHY_ERR_INC(phyerr);
8611395d3f0SSujith 	}
8621395d3f0SSujith 
8631395d3f0SSujith #undef RX_STAT_INC
8641395d3f0SSujith #undef RX_PHY_ERR_INC
8651395d3f0SSujith }
8661395d3f0SSujith 
8671395d3f0SSujith static const struct file_operations fops_recv = {
8681395d3f0SSujith 	.read = read_file_recv,
8691395d3f0SSujith 	.open = ath9k_debugfs_open,
8701395d3f0SSujith 	.owner = THIS_MODULE
8711395d3f0SSujith };
8721395d3f0SSujith 
8739bff0bc4SFelix Fietkau static ssize_t read_file_regidx(struct file *file, char __user *user_buf,
8749bff0bc4SFelix Fietkau                                 size_t count, loff_t *ppos)
8759bff0bc4SFelix Fietkau {
8769bff0bc4SFelix Fietkau 	struct ath_softc *sc = file->private_data;
8779bff0bc4SFelix Fietkau 	char buf[32];
8789bff0bc4SFelix Fietkau 	unsigned int len;
8799bff0bc4SFelix Fietkau 
8802b87f3aaSDan Carpenter 	len = sprintf(buf, "0x%08x\n", sc->debug.regidx);
8819bff0bc4SFelix Fietkau 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
8829bff0bc4SFelix Fietkau }
8839bff0bc4SFelix Fietkau 
8849bff0bc4SFelix Fietkau static ssize_t write_file_regidx(struct file *file, const char __user *user_buf,
8859bff0bc4SFelix Fietkau 			     size_t count, loff_t *ppos)
8869bff0bc4SFelix Fietkau {
8879bff0bc4SFelix Fietkau 	struct ath_softc *sc = file->private_data;
8889bff0bc4SFelix Fietkau 	unsigned long regidx;
8899bff0bc4SFelix Fietkau 	char buf[32];
8909bff0bc4SFelix Fietkau 	ssize_t len;
8919bff0bc4SFelix Fietkau 
8929bff0bc4SFelix Fietkau 	len = min(count, sizeof(buf) - 1);
8939bff0bc4SFelix Fietkau 	if (copy_from_user(buf, user_buf, len))
89404236066SDan Carpenter 		return -EFAULT;
8959bff0bc4SFelix Fietkau 
8969bff0bc4SFelix Fietkau 	buf[len] = '\0';
8979bff0bc4SFelix Fietkau 	if (strict_strtoul(buf, 0, &regidx))
8989bff0bc4SFelix Fietkau 		return -EINVAL;
8999bff0bc4SFelix Fietkau 
9009bff0bc4SFelix Fietkau 	sc->debug.regidx = regidx;
9019bff0bc4SFelix Fietkau 	return count;
9029bff0bc4SFelix Fietkau }
9039bff0bc4SFelix Fietkau 
9049bff0bc4SFelix Fietkau static const struct file_operations fops_regidx = {
9059bff0bc4SFelix Fietkau 	.read = read_file_regidx,
9069bff0bc4SFelix Fietkau 	.write = write_file_regidx,
9079bff0bc4SFelix Fietkau 	.open = ath9k_debugfs_open,
9089bff0bc4SFelix Fietkau 	.owner = THIS_MODULE
9099bff0bc4SFelix Fietkau };
9109bff0bc4SFelix Fietkau 
9119bff0bc4SFelix Fietkau static ssize_t read_file_regval(struct file *file, char __user *user_buf,
9129bff0bc4SFelix Fietkau 			     size_t count, loff_t *ppos)
9139bff0bc4SFelix Fietkau {
9149bff0bc4SFelix Fietkau 	struct ath_softc *sc = file->private_data;
9159bff0bc4SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
9169bff0bc4SFelix Fietkau 	char buf[32];
9179bff0bc4SFelix Fietkau 	unsigned int len;
9189bff0bc4SFelix Fietkau 	u32 regval;
9199bff0bc4SFelix Fietkau 
9209bff0bc4SFelix Fietkau 	regval = REG_READ_D(ah, sc->debug.regidx);
9212b87f3aaSDan Carpenter 	len = sprintf(buf, "0x%08x\n", regval);
9229bff0bc4SFelix Fietkau 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
9239bff0bc4SFelix Fietkau }
9249bff0bc4SFelix Fietkau 
9259bff0bc4SFelix Fietkau static ssize_t write_file_regval(struct file *file, const char __user *user_buf,
9269bff0bc4SFelix Fietkau 			     size_t count, loff_t *ppos)
9279bff0bc4SFelix Fietkau {
9289bff0bc4SFelix Fietkau 	struct ath_softc *sc = file->private_data;
9299bff0bc4SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
9309bff0bc4SFelix Fietkau 	unsigned long regval;
9319bff0bc4SFelix Fietkau 	char buf[32];
9329bff0bc4SFelix Fietkau 	ssize_t len;
9339bff0bc4SFelix Fietkau 
9349bff0bc4SFelix Fietkau 	len = min(count, sizeof(buf) - 1);
9359bff0bc4SFelix Fietkau 	if (copy_from_user(buf, user_buf, len))
93604236066SDan Carpenter 		return -EFAULT;
9379bff0bc4SFelix Fietkau 
9389bff0bc4SFelix Fietkau 	buf[len] = '\0';
9399bff0bc4SFelix Fietkau 	if (strict_strtoul(buf, 0, &regval))
9409bff0bc4SFelix Fietkau 		return -EINVAL;
9419bff0bc4SFelix Fietkau 
9429bff0bc4SFelix Fietkau 	REG_WRITE_D(ah, sc->debug.regidx, regval);
9439bff0bc4SFelix Fietkau 	return count;
9449bff0bc4SFelix Fietkau }
9459bff0bc4SFelix Fietkau 
9469bff0bc4SFelix Fietkau static const struct file_operations fops_regval = {
9479bff0bc4SFelix Fietkau 	.read = read_file_regval,
9489bff0bc4SFelix Fietkau 	.write = write_file_regval,
9499bff0bc4SFelix Fietkau 	.open = ath9k_debugfs_open,
9509bff0bc4SFelix Fietkau 	.owner = THIS_MODULE
9519bff0bc4SFelix Fietkau };
9529bff0bc4SFelix Fietkau 
9534d6b228dSLuis R. Rodriguez int ath9k_init_debug(struct ath_hw *ah)
954203c4805SLuis R. Rodriguez {
955bc974f4aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
956bc974f4aSLuis R. Rodriguez 	struct ath_softc *sc = (struct ath_softc *) common->priv;
9574d6b228dSLuis R. Rodriguez 
958203c4805SLuis R. Rodriguez 	if (!ath9k_debugfs_root)
959203c4805SLuis R. Rodriguez 		return -ENOENT;
960203c4805SLuis R. Rodriguez 
961203c4805SLuis R. Rodriguez 	sc->debug.debugfs_phy = debugfs_create_dir(wiphy_name(sc->hw->wiphy),
962203c4805SLuis R. Rodriguez 						      ath9k_debugfs_root);
963203c4805SLuis R. Rodriguez 	if (!sc->debug.debugfs_phy)
964c8a72c00SFelix Fietkau 		return -ENOMEM;
965203c4805SLuis R. Rodriguez 
966a830df07SFelix Fietkau #ifdef CONFIG_ATH_DEBUG
967c8a72c00SFelix Fietkau 	if (!debugfs_create_file("debug", S_IRUSR | S_IWUSR,
968c8a72c00SFelix Fietkau 			sc->debug.debugfs_phy, sc, &fops_debug))
9692493928eSJeff Hansen 		goto err;
970a830df07SFelix Fietkau #endif
9712493928eSJeff Hansen 
972c8a72c00SFelix Fietkau 	if (!debugfs_create_file("dma", S_IRUSR, sc->debug.debugfs_phy,
973c8a72c00SFelix Fietkau 			sc, &fops_dma))
974203c4805SLuis R. Rodriguez 		goto err;
975203c4805SLuis R. Rodriguez 
976c8a72c00SFelix Fietkau 	if (!debugfs_create_file("interrupt", S_IRUSR, sc->debug.debugfs_phy,
977c8a72c00SFelix Fietkau 			sc, &fops_interrupt))
978203c4805SLuis R. Rodriguez 		goto err;
979203c4805SLuis R. Rodriguez 
980c8a72c00SFelix Fietkau 	if (!debugfs_create_file("rcstat", S_IRUSR, sc->debug.debugfs_phy,
981c8a72c00SFelix Fietkau 			sc, &fops_rcstat))
982203c4805SLuis R. Rodriguez 		goto err;
983203c4805SLuis R. Rodriguez 
984c8a72c00SFelix Fietkau 	if (!debugfs_create_file("wiphy", S_IRUSR | S_IWUSR,
985c8a72c00SFelix Fietkau 			sc->debug.debugfs_phy, sc, &fops_wiphy))
986203c4805SLuis R. Rodriguez 		goto err;
987203c4805SLuis R. Rodriguez 
988c8a72c00SFelix Fietkau 	if (!debugfs_create_file("xmit", S_IRUSR, sc->debug.debugfs_phy,
989c8a72c00SFelix Fietkau 			sc, &fops_xmit))
990fec247c0SSujith 		goto err;
991fec247c0SSujith 
992c8a72c00SFelix Fietkau 	if (!debugfs_create_file("recv", S_IRUSR, sc->debug.debugfs_phy,
993c8a72c00SFelix Fietkau 			sc, &fops_recv))
9941395d3f0SSujith 		goto err;
9951395d3f0SSujith 
99615340694SFelix Fietkau 	if (!debugfs_create_file("rx_chainmask", S_IRUSR | S_IWUSR,
99715340694SFelix Fietkau 			sc->debug.debugfs_phy, sc, &fops_rx_chainmask))
99815340694SFelix Fietkau 		goto err;
99915340694SFelix Fietkau 
100015340694SFelix Fietkau 	if (!debugfs_create_file("tx_chainmask", S_IRUSR | S_IWUSR,
100115340694SFelix Fietkau 			sc->debug.debugfs_phy, sc, &fops_tx_chainmask))
100215340694SFelix Fietkau 		goto err;
100315340694SFelix Fietkau 
10049bff0bc4SFelix Fietkau 	if (!debugfs_create_file("regidx", S_IRUSR | S_IWUSR,
10059bff0bc4SFelix Fietkau 			sc->debug.debugfs_phy, sc, &fops_regidx))
10069bff0bc4SFelix Fietkau 		goto err;
10079bff0bc4SFelix Fietkau 
10089bff0bc4SFelix Fietkau 	if (!debugfs_create_file("regval", S_IRUSR | S_IWUSR,
10099bff0bc4SFelix Fietkau 			sc->debug.debugfs_phy, sc, &fops_regval))
10109bff0bc4SFelix Fietkau 		goto err;
10119bff0bc4SFelix Fietkau 
101241f3e54dSFelix Fietkau 	if (!debugfs_create_bool("ignore_extcca", S_IRUSR | S_IWUSR,
101341f3e54dSFelix Fietkau 			sc->debug.debugfs_phy, &ah->config.cwm_ignore_extcca))
101441f3e54dSFelix Fietkau 		goto err;
101541f3e54dSFelix Fietkau 
10169bff0bc4SFelix Fietkau 	sc->debug.regidx = 0;
1017203c4805SLuis R. Rodriguez 	return 0;
1018203c4805SLuis R. Rodriguez err:
10194d6b228dSLuis R. Rodriguez 	ath9k_exit_debug(ah);
1020203c4805SLuis R. Rodriguez 	return -ENOMEM;
1021203c4805SLuis R. Rodriguez }
1022203c4805SLuis R. Rodriguez 
10234d6b228dSLuis R. Rodriguez void ath9k_exit_debug(struct ath_hw *ah)
1024203c4805SLuis R. Rodriguez {
1025bc974f4aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
1026bc974f4aSLuis R. Rodriguez 	struct ath_softc *sc = (struct ath_softc *) common->priv;
10274d6b228dSLuis R. Rodriguez 
1028c8a72c00SFelix Fietkau 	debugfs_remove_recursive(sc->debug.debugfs_phy);
1029203c4805SLuis R. Rodriguez }
1030203c4805SLuis R. Rodriguez 
1031203c4805SLuis R. Rodriguez int ath9k_debug_create_root(void)
1032203c4805SLuis R. Rodriguez {
1033203c4805SLuis R. Rodriguez 	ath9k_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
1034203c4805SLuis R. Rodriguez 	if (!ath9k_debugfs_root)
1035203c4805SLuis R. Rodriguez 		return -ENOENT;
1036203c4805SLuis R. Rodriguez 
1037203c4805SLuis R. Rodriguez 	return 0;
1038203c4805SLuis R. Rodriguez }
1039203c4805SLuis R. Rodriguez 
1040203c4805SLuis R. Rodriguez void ath9k_debug_remove_root(void)
1041203c4805SLuis R. Rodriguez {
1042203c4805SLuis R. Rodriguez 	debugfs_remove(ath9k_debugfs_root);
1043203c4805SLuis R. Rodriguez 	ath9k_debugfs_root = NULL;
1044203c4805SLuis R. Rodriguez }
1045