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