xref: /linux/drivers/net/wireless/ath/ath10k/debug.c (revision 0883c2c06fb5bcf5b9e008270827e63c09a88c1e)
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <linux/module.h>
19 #include <linux/debugfs.h>
20 #include <linux/vmalloc.h>
21 #include <linux/utsname.h>
22 #include <linux/crc32.h>
23 #include <linux/firmware.h>
24 
25 #include "core.h"
26 #include "debug.h"
27 #include "hif.h"
28 #include "wmi-ops.h"
29 
30 /* ms */
31 #define ATH10K_DEBUG_HTT_STATS_INTERVAL 1000
32 
33 #define ATH10K_FW_CRASH_DUMP_VERSION 1
34 
35 /**
36  * enum ath10k_fw_crash_dump_type - types of data in the dump file
37  * @ATH10K_FW_CRASH_DUMP_REGDUMP: Register crash dump in binary format
38  */
39 enum ath10k_fw_crash_dump_type {
40 	ATH10K_FW_CRASH_DUMP_REGISTERS = 0,
41 
42 	ATH10K_FW_CRASH_DUMP_MAX,
43 };
44 
45 struct ath10k_tlv_dump_data {
46 	/* see ath10k_fw_crash_dump_type above */
47 	__le32 type;
48 
49 	/* in bytes */
50 	__le32 tlv_len;
51 
52 	/* pad to 32-bit boundaries as needed */
53 	u8 tlv_data[];
54 } __packed;
55 
56 struct ath10k_dump_file_data {
57 	/* dump file information */
58 
59 	/* "ATH10K-FW-DUMP" */
60 	char df_magic[16];
61 
62 	__le32 len;
63 
64 	/* file dump version */
65 	__le32 version;
66 
67 	/* some info we can get from ath10k struct that might help */
68 
69 	u8 uuid[16];
70 
71 	__le32 chip_id;
72 
73 	/* 0 for now, in place for later hardware */
74 	__le32 bus_type;
75 
76 	__le32 target_version;
77 	__le32 fw_version_major;
78 	__le32 fw_version_minor;
79 	__le32 fw_version_release;
80 	__le32 fw_version_build;
81 	__le32 phy_capability;
82 	__le32 hw_min_tx_power;
83 	__le32 hw_max_tx_power;
84 	__le32 ht_cap_info;
85 	__le32 vht_cap_info;
86 	__le32 num_rf_chains;
87 
88 	/* firmware version string */
89 	char fw_ver[ETHTOOL_FWVERS_LEN];
90 
91 	/* Kernel related information */
92 
93 	/* time-of-day stamp */
94 	__le64 tv_sec;
95 
96 	/* time-of-day stamp, nano-seconds */
97 	__le64 tv_nsec;
98 
99 	/* LINUX_VERSION_CODE */
100 	__le32 kernel_ver_code;
101 
102 	/* VERMAGIC_STRING */
103 	char kernel_ver[64];
104 
105 	/* room for growth w/out changing binary format */
106 	u8 unused[128];
107 
108 	/* struct ath10k_tlv_dump_data + more */
109 	u8 data[0];
110 } __packed;
111 
112 void ath10k_info(struct ath10k *ar, const char *fmt, ...)
113 {
114 	struct va_format vaf = {
115 		.fmt = fmt,
116 	};
117 	va_list args;
118 
119 	va_start(args, fmt);
120 	vaf.va = &args;
121 	dev_info(ar->dev, "%pV", &vaf);
122 	trace_ath10k_log_info(ar, &vaf);
123 	va_end(args);
124 }
125 EXPORT_SYMBOL(ath10k_info);
126 
127 void ath10k_debug_print_hwfw_info(struct ath10k *ar)
128 {
129 	const struct firmware *firmware;
130 	char fw_features[128] = {};
131 	u32 crc = 0;
132 
133 	ath10k_core_get_fw_features_str(ar, fw_features, sizeof(fw_features));
134 
135 	ath10k_info(ar, "%s target 0x%08x chip_id 0x%08x sub %04x:%04x",
136 		    ar->hw_params.name,
137 		    ar->target_version,
138 		    ar->chip_id,
139 		    ar->id.subsystem_vendor, ar->id.subsystem_device);
140 
141 	ath10k_info(ar, "kconfig debug %d debugfs %d tracing %d dfs %d testmode %d\n",
142 		    config_enabled(CONFIG_ATH10K_DEBUG),
143 		    config_enabled(CONFIG_ATH10K_DEBUGFS),
144 		    config_enabled(CONFIG_ATH10K_TRACING),
145 		    config_enabled(CONFIG_ATH10K_DFS_CERTIFIED),
146 		    config_enabled(CONFIG_NL80211_TESTMODE));
147 
148 	firmware = ar->normal_mode_fw.fw_file.firmware;
149 	if (firmware)
150 		crc = crc32_le(0, firmware->data, firmware->size);
151 
152 	ath10k_info(ar, "firmware ver %s api %d features %s crc32 %08x\n",
153 		    ar->hw->wiphy->fw_version,
154 		    ar->fw_api,
155 		    fw_features,
156 		    crc);
157 }
158 
159 void ath10k_debug_print_board_info(struct ath10k *ar)
160 {
161 	char boardinfo[100];
162 
163 	if (ar->id.bmi_ids_valid)
164 		scnprintf(boardinfo, sizeof(boardinfo), "%d:%d",
165 			  ar->id.bmi_chip_id, ar->id.bmi_board_id);
166 	else
167 		scnprintf(boardinfo, sizeof(boardinfo), "N/A");
168 
169 	ath10k_info(ar, "board_file api %d bmi_id %s crc32 %08x",
170 		    ar->bd_api,
171 		    boardinfo,
172 		    crc32_le(0, ar->normal_mode_fw.board->data,
173 			     ar->normal_mode_fw.board->size));
174 }
175 
176 void ath10k_debug_print_boot_info(struct ath10k *ar)
177 {
178 	ath10k_info(ar, "htt-ver %d.%d wmi-op %d htt-op %d cal %s max-sta %d raw %d hwcrypto %d\n",
179 		    ar->htt.target_version_major,
180 		    ar->htt.target_version_minor,
181 		    ar->normal_mode_fw.fw_file.wmi_op_version,
182 		    ar->normal_mode_fw.fw_file.htt_op_version,
183 		    ath10k_cal_mode_str(ar->cal_mode),
184 		    ar->max_num_stations,
185 		    test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags),
186 		    !test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags));
187 }
188 
189 void ath10k_print_driver_info(struct ath10k *ar)
190 {
191 	ath10k_debug_print_hwfw_info(ar);
192 	ath10k_debug_print_board_info(ar);
193 	ath10k_debug_print_boot_info(ar);
194 }
195 EXPORT_SYMBOL(ath10k_print_driver_info);
196 
197 void ath10k_err(struct ath10k *ar, const char *fmt, ...)
198 {
199 	struct va_format vaf = {
200 		.fmt = fmt,
201 	};
202 	va_list args;
203 
204 	va_start(args, fmt);
205 	vaf.va = &args;
206 	dev_err(ar->dev, "%pV", &vaf);
207 	trace_ath10k_log_err(ar, &vaf);
208 	va_end(args);
209 }
210 EXPORT_SYMBOL(ath10k_err);
211 
212 void ath10k_warn(struct ath10k *ar, const char *fmt, ...)
213 {
214 	struct va_format vaf = {
215 		.fmt = fmt,
216 	};
217 	va_list args;
218 
219 	va_start(args, fmt);
220 	vaf.va = &args;
221 	dev_warn_ratelimited(ar->dev, "%pV", &vaf);
222 	trace_ath10k_log_warn(ar, &vaf);
223 
224 	va_end(args);
225 }
226 EXPORT_SYMBOL(ath10k_warn);
227 
228 #ifdef CONFIG_ATH10K_DEBUGFS
229 
230 static ssize_t ath10k_read_wmi_services(struct file *file,
231 					char __user *user_buf,
232 					size_t count, loff_t *ppos)
233 {
234 	struct ath10k *ar = file->private_data;
235 	char *buf;
236 	unsigned int len = 0, buf_len = 4096;
237 	const char *name;
238 	ssize_t ret_cnt;
239 	bool enabled;
240 	int i;
241 
242 	buf = kzalloc(buf_len, GFP_KERNEL);
243 	if (!buf)
244 		return -ENOMEM;
245 
246 	mutex_lock(&ar->conf_mutex);
247 
248 	if (len > buf_len)
249 		len = buf_len;
250 
251 	spin_lock_bh(&ar->data_lock);
252 	for (i = 0; i < WMI_SERVICE_MAX; i++) {
253 		enabled = test_bit(i, ar->wmi.svc_map);
254 		name = wmi_service_name(i);
255 
256 		if (!name) {
257 			if (enabled)
258 				len += scnprintf(buf + len, buf_len - len,
259 						 "%-40s %s (bit %d)\n",
260 						 "unknown", "enabled", i);
261 
262 			continue;
263 		}
264 
265 		len += scnprintf(buf + len, buf_len - len,
266 				 "%-40s %s\n",
267 				 name, enabled ? "enabled" : "-");
268 	}
269 	spin_unlock_bh(&ar->data_lock);
270 
271 	ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
272 
273 	mutex_unlock(&ar->conf_mutex);
274 
275 	kfree(buf);
276 	return ret_cnt;
277 }
278 
279 static const struct file_operations fops_wmi_services = {
280 	.read = ath10k_read_wmi_services,
281 	.open = simple_open,
282 	.owner = THIS_MODULE,
283 	.llseek = default_llseek,
284 };
285 
286 static void ath10k_fw_stats_pdevs_free(struct list_head *head)
287 {
288 	struct ath10k_fw_stats_pdev *i, *tmp;
289 
290 	list_for_each_entry_safe(i, tmp, head, list) {
291 		list_del(&i->list);
292 		kfree(i);
293 	}
294 }
295 
296 static void ath10k_fw_stats_vdevs_free(struct list_head *head)
297 {
298 	struct ath10k_fw_stats_vdev *i, *tmp;
299 
300 	list_for_each_entry_safe(i, tmp, head, list) {
301 		list_del(&i->list);
302 		kfree(i);
303 	}
304 }
305 
306 static void ath10k_fw_stats_peers_free(struct list_head *head)
307 {
308 	struct ath10k_fw_stats_peer *i, *tmp;
309 
310 	list_for_each_entry_safe(i, tmp, head, list) {
311 		list_del(&i->list);
312 		kfree(i);
313 	}
314 }
315 
316 static void ath10k_debug_fw_stats_reset(struct ath10k *ar)
317 {
318 	spin_lock_bh(&ar->data_lock);
319 	ar->debug.fw_stats_done = false;
320 	ath10k_fw_stats_pdevs_free(&ar->debug.fw_stats.pdevs);
321 	ath10k_fw_stats_vdevs_free(&ar->debug.fw_stats.vdevs);
322 	ath10k_fw_stats_peers_free(&ar->debug.fw_stats.peers);
323 	spin_unlock_bh(&ar->data_lock);
324 }
325 
326 void ath10k_debug_fw_stats_process(struct ath10k *ar, struct sk_buff *skb)
327 {
328 	struct ath10k_fw_stats stats = {};
329 	bool is_start, is_started, is_end;
330 	size_t num_peers;
331 	size_t num_vdevs;
332 	int ret;
333 
334 	INIT_LIST_HEAD(&stats.pdevs);
335 	INIT_LIST_HEAD(&stats.vdevs);
336 	INIT_LIST_HEAD(&stats.peers);
337 
338 	spin_lock_bh(&ar->data_lock);
339 	ret = ath10k_wmi_pull_fw_stats(ar, skb, &stats);
340 	if (ret) {
341 		ath10k_warn(ar, "failed to pull fw stats: %d\n", ret);
342 		goto free;
343 	}
344 
345 	/* Stat data may exceed htc-wmi buffer limit. In such case firmware
346 	 * splits the stats data and delivers it in a ping-pong fashion of
347 	 * request cmd-update event.
348 	 *
349 	 * However there is no explicit end-of-data. Instead start-of-data is
350 	 * used as an implicit one. This works as follows:
351 	 *  a) discard stat update events until one with pdev stats is
352 	 *     delivered - this skips session started at end of (b)
353 	 *  b) consume stat update events until another one with pdev stats is
354 	 *     delivered which is treated as end-of-data and is itself discarded
355 	 */
356 	if (ath10k_peer_stats_enabled(ar))
357 		ath10k_sta_update_rx_duration(ar, &stats.peers);
358 
359 	if (ar->debug.fw_stats_done) {
360 		if (!ath10k_peer_stats_enabled(ar))
361 			ath10k_warn(ar, "received unsolicited stats update event\n");
362 
363 		goto free;
364 	}
365 
366 	num_peers = ath10k_wmi_fw_stats_num_peers(&ar->debug.fw_stats.peers);
367 	num_vdevs = ath10k_wmi_fw_stats_num_vdevs(&ar->debug.fw_stats.vdevs);
368 	is_start = (list_empty(&ar->debug.fw_stats.pdevs) &&
369 		    !list_empty(&stats.pdevs));
370 	is_end = (!list_empty(&ar->debug.fw_stats.pdevs) &&
371 		  !list_empty(&stats.pdevs));
372 
373 	if (is_start)
374 		list_splice_tail_init(&stats.pdevs, &ar->debug.fw_stats.pdevs);
375 
376 	if (is_end)
377 		ar->debug.fw_stats_done = true;
378 
379 	is_started = !list_empty(&ar->debug.fw_stats.pdevs);
380 
381 	if (is_started && !is_end) {
382 		if (num_peers >= ATH10K_MAX_NUM_PEER_IDS) {
383 			/* Although this is unlikely impose a sane limit to
384 			 * prevent firmware from DoS-ing the host.
385 			 */
386 			ath10k_fw_stats_peers_free(&ar->debug.fw_stats.peers);
387 			ath10k_warn(ar, "dropping fw peer stats\n");
388 			goto free;
389 		}
390 
391 		if (num_vdevs >= BITS_PER_LONG) {
392 			ath10k_fw_stats_vdevs_free(&ar->debug.fw_stats.vdevs);
393 			ath10k_warn(ar, "dropping fw vdev stats\n");
394 			goto free;
395 		}
396 
397 		list_splice_tail_init(&stats.peers, &ar->debug.fw_stats.peers);
398 		list_splice_tail_init(&stats.vdevs, &ar->debug.fw_stats.vdevs);
399 	}
400 
401 	complete(&ar->debug.fw_stats_complete);
402 
403 free:
404 	/* In some cases lists have been spliced and cleared. Free up
405 	 * resources if that is not the case.
406 	 */
407 	ath10k_fw_stats_pdevs_free(&stats.pdevs);
408 	ath10k_fw_stats_vdevs_free(&stats.vdevs);
409 	ath10k_fw_stats_peers_free(&stats.peers);
410 
411 	spin_unlock_bh(&ar->data_lock);
412 }
413 
414 static int ath10k_debug_fw_stats_request(struct ath10k *ar)
415 {
416 	unsigned long timeout, time_left;
417 	int ret;
418 
419 	lockdep_assert_held(&ar->conf_mutex);
420 
421 	timeout = jiffies + msecs_to_jiffies(1 * HZ);
422 
423 	ath10k_debug_fw_stats_reset(ar);
424 
425 	for (;;) {
426 		if (time_after(jiffies, timeout))
427 			return -ETIMEDOUT;
428 
429 		reinit_completion(&ar->debug.fw_stats_complete);
430 
431 		ret = ath10k_wmi_request_stats(ar, ar->fw_stats_req_mask);
432 		if (ret) {
433 			ath10k_warn(ar, "could not request stats (%d)\n", ret);
434 			return ret;
435 		}
436 
437 		time_left =
438 		wait_for_completion_timeout(&ar->debug.fw_stats_complete,
439 					    1 * HZ);
440 		if (!time_left)
441 			return -ETIMEDOUT;
442 
443 		spin_lock_bh(&ar->data_lock);
444 		if (ar->debug.fw_stats_done) {
445 			spin_unlock_bh(&ar->data_lock);
446 			break;
447 		}
448 		spin_unlock_bh(&ar->data_lock);
449 	}
450 
451 	return 0;
452 }
453 
454 static int ath10k_fw_stats_open(struct inode *inode, struct file *file)
455 {
456 	struct ath10k *ar = inode->i_private;
457 	void *buf = NULL;
458 	int ret;
459 
460 	mutex_lock(&ar->conf_mutex);
461 
462 	if (ar->state != ATH10K_STATE_ON) {
463 		ret = -ENETDOWN;
464 		goto err_unlock;
465 	}
466 
467 	buf = vmalloc(ATH10K_FW_STATS_BUF_SIZE);
468 	if (!buf) {
469 		ret = -ENOMEM;
470 		goto err_unlock;
471 	}
472 
473 	ret = ath10k_debug_fw_stats_request(ar);
474 	if (ret) {
475 		ath10k_warn(ar, "failed to request fw stats: %d\n", ret);
476 		goto err_free;
477 	}
478 
479 	ret = ath10k_wmi_fw_stats_fill(ar, &ar->debug.fw_stats, buf);
480 	if (ret) {
481 		ath10k_warn(ar, "failed to fill fw stats: %d\n", ret);
482 		goto err_free;
483 	}
484 
485 	file->private_data = buf;
486 
487 	mutex_unlock(&ar->conf_mutex);
488 	return 0;
489 
490 err_free:
491 	vfree(buf);
492 
493 err_unlock:
494 	mutex_unlock(&ar->conf_mutex);
495 	return ret;
496 }
497 
498 static int ath10k_fw_stats_release(struct inode *inode, struct file *file)
499 {
500 	vfree(file->private_data);
501 
502 	return 0;
503 }
504 
505 static ssize_t ath10k_fw_stats_read(struct file *file, char __user *user_buf,
506 				    size_t count, loff_t *ppos)
507 {
508 	const char *buf = file->private_data;
509 	unsigned int len = strlen(buf);
510 
511 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
512 }
513 
514 static const struct file_operations fops_fw_stats = {
515 	.open = ath10k_fw_stats_open,
516 	.release = ath10k_fw_stats_release,
517 	.read = ath10k_fw_stats_read,
518 	.owner = THIS_MODULE,
519 	.llseek = default_llseek,
520 };
521 
522 static ssize_t ath10k_debug_fw_reset_stats_read(struct file *file,
523 						char __user *user_buf,
524 						size_t count, loff_t *ppos)
525 {
526 	struct ath10k *ar = file->private_data;
527 	int ret, len, buf_len;
528 	char *buf;
529 
530 	buf_len = 500;
531 	buf = kmalloc(buf_len, GFP_KERNEL);
532 	if (!buf)
533 		return -ENOMEM;
534 
535 	spin_lock_bh(&ar->data_lock);
536 
537 	len = 0;
538 	len += scnprintf(buf + len, buf_len - len,
539 			 "fw_crash_counter\t\t%d\n", ar->stats.fw_crash_counter);
540 	len += scnprintf(buf + len, buf_len - len,
541 			 "fw_warm_reset_counter\t\t%d\n",
542 			 ar->stats.fw_warm_reset_counter);
543 	len += scnprintf(buf + len, buf_len - len,
544 			 "fw_cold_reset_counter\t\t%d\n",
545 			 ar->stats.fw_cold_reset_counter);
546 
547 	spin_unlock_bh(&ar->data_lock);
548 
549 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
550 
551 	kfree(buf);
552 
553 	return ret;
554 }
555 
556 static const struct file_operations fops_fw_reset_stats = {
557 	.open = simple_open,
558 	.read = ath10k_debug_fw_reset_stats_read,
559 	.owner = THIS_MODULE,
560 	.llseek = default_llseek,
561 };
562 
563 /* This is a clean assert crash in firmware. */
564 static int ath10k_debug_fw_assert(struct ath10k *ar)
565 {
566 	struct wmi_vdev_install_key_cmd *cmd;
567 	struct sk_buff *skb;
568 
569 	skb = ath10k_wmi_alloc_skb(ar, sizeof(*cmd) + 16);
570 	if (!skb)
571 		return -ENOMEM;
572 
573 	cmd = (struct wmi_vdev_install_key_cmd *)skb->data;
574 	memset(cmd, 0, sizeof(*cmd));
575 
576 	/* big enough number so that firmware asserts */
577 	cmd->vdev_id = __cpu_to_le32(0x7ffe);
578 
579 	return ath10k_wmi_cmd_send(ar, skb,
580 				   ar->wmi.cmd->vdev_install_key_cmdid);
581 }
582 
583 static ssize_t ath10k_read_simulate_fw_crash(struct file *file,
584 					     char __user *user_buf,
585 					     size_t count, loff_t *ppos)
586 {
587 	const char buf[] =
588 		"To simulate firmware crash write one of the keywords to this file:\n"
589 		"`soft` - this will send WMI_FORCE_FW_HANG_ASSERT to firmware if FW supports that command.\n"
590 		"`hard` - this will send to firmware command with illegal parameters causing firmware crash.\n"
591 		"`assert` - this will send special illegal parameter to firmware to cause assert failure and crash.\n"
592 		"`hw-restart` - this will simply queue hw restart without fw/hw actually crashing.\n";
593 
594 	return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
595 }
596 
597 /* Simulate firmware crash:
598  * 'soft': Call wmi command causing firmware hang. This firmware hang is
599  * recoverable by warm firmware reset.
600  * 'hard': Force firmware crash by setting any vdev parameter for not allowed
601  * vdev id. This is hard firmware crash because it is recoverable only by cold
602  * firmware reset.
603  */
604 static ssize_t ath10k_write_simulate_fw_crash(struct file *file,
605 					      const char __user *user_buf,
606 					      size_t count, loff_t *ppos)
607 {
608 	struct ath10k *ar = file->private_data;
609 	char buf[32];
610 	int ret;
611 
612 	mutex_lock(&ar->conf_mutex);
613 
614 	simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
615 
616 	/* make sure that buf is null terminated */
617 	buf[sizeof(buf) - 1] = 0;
618 
619 	if (ar->state != ATH10K_STATE_ON &&
620 	    ar->state != ATH10K_STATE_RESTARTED) {
621 		ret = -ENETDOWN;
622 		goto exit;
623 	}
624 
625 	/* drop the possible '\n' from the end */
626 	if (buf[count - 1] == '\n') {
627 		buf[count - 1] = 0;
628 		count--;
629 	}
630 
631 	if (!strcmp(buf, "soft")) {
632 		ath10k_info(ar, "simulating soft firmware crash\n");
633 		ret = ath10k_wmi_force_fw_hang(ar, WMI_FORCE_FW_HANG_ASSERT, 0);
634 	} else if (!strcmp(buf, "hard")) {
635 		ath10k_info(ar, "simulating hard firmware crash\n");
636 		/* 0x7fff is vdev id, and it is always out of range for all
637 		 * firmware variants in order to force a firmware crash.
638 		 */
639 		ret = ath10k_wmi_vdev_set_param(ar, 0x7fff,
640 						ar->wmi.vdev_param->rts_threshold,
641 						0);
642 	} else if (!strcmp(buf, "assert")) {
643 		ath10k_info(ar, "simulating firmware assert crash\n");
644 		ret = ath10k_debug_fw_assert(ar);
645 	} else if (!strcmp(buf, "hw-restart")) {
646 		ath10k_info(ar, "user requested hw restart\n");
647 		queue_work(ar->workqueue, &ar->restart_work);
648 		ret = 0;
649 	} else {
650 		ret = -EINVAL;
651 		goto exit;
652 	}
653 
654 	if (ret) {
655 		ath10k_warn(ar, "failed to simulate firmware crash: %d\n", ret);
656 		goto exit;
657 	}
658 
659 	ret = count;
660 
661 exit:
662 	mutex_unlock(&ar->conf_mutex);
663 	return ret;
664 }
665 
666 static const struct file_operations fops_simulate_fw_crash = {
667 	.read = ath10k_read_simulate_fw_crash,
668 	.write = ath10k_write_simulate_fw_crash,
669 	.open = simple_open,
670 	.owner = THIS_MODULE,
671 	.llseek = default_llseek,
672 };
673 
674 static ssize_t ath10k_read_chip_id(struct file *file, char __user *user_buf,
675 				   size_t count, loff_t *ppos)
676 {
677 	struct ath10k *ar = file->private_data;
678 	unsigned int len;
679 	char buf[50];
680 
681 	len = scnprintf(buf, sizeof(buf), "0x%08x\n", ar->chip_id);
682 
683 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
684 }
685 
686 static const struct file_operations fops_chip_id = {
687 	.read = ath10k_read_chip_id,
688 	.open = simple_open,
689 	.owner = THIS_MODULE,
690 	.llseek = default_llseek,
691 };
692 
693 struct ath10k_fw_crash_data *
694 ath10k_debug_get_new_fw_crash_data(struct ath10k *ar)
695 {
696 	struct ath10k_fw_crash_data *crash_data = ar->debug.fw_crash_data;
697 
698 	lockdep_assert_held(&ar->data_lock);
699 
700 	crash_data->crashed_since_read = true;
701 	uuid_le_gen(&crash_data->uuid);
702 	getnstimeofday(&crash_data->timestamp);
703 
704 	return crash_data;
705 }
706 EXPORT_SYMBOL(ath10k_debug_get_new_fw_crash_data);
707 
708 static struct ath10k_dump_file_data *ath10k_build_dump_file(struct ath10k *ar)
709 {
710 	struct ath10k_fw_crash_data *crash_data = ar->debug.fw_crash_data;
711 	struct ath10k_dump_file_data *dump_data;
712 	struct ath10k_tlv_dump_data *dump_tlv;
713 	int hdr_len = sizeof(*dump_data);
714 	unsigned int len, sofar = 0;
715 	unsigned char *buf;
716 
717 	len = hdr_len;
718 	len += sizeof(*dump_tlv) + sizeof(crash_data->registers);
719 
720 	sofar += hdr_len;
721 
722 	/* This is going to get big when we start dumping FW RAM and such,
723 	 * so go ahead and use vmalloc.
724 	 */
725 	buf = vzalloc(len);
726 	if (!buf)
727 		return NULL;
728 
729 	spin_lock_bh(&ar->data_lock);
730 
731 	if (!crash_data->crashed_since_read) {
732 		spin_unlock_bh(&ar->data_lock);
733 		vfree(buf);
734 		return NULL;
735 	}
736 
737 	dump_data = (struct ath10k_dump_file_data *)(buf);
738 	strlcpy(dump_data->df_magic, "ATH10K-FW-DUMP",
739 		sizeof(dump_data->df_magic));
740 	dump_data->len = cpu_to_le32(len);
741 
742 	dump_data->version = cpu_to_le32(ATH10K_FW_CRASH_DUMP_VERSION);
743 
744 	memcpy(dump_data->uuid, &crash_data->uuid, sizeof(dump_data->uuid));
745 	dump_data->chip_id = cpu_to_le32(ar->chip_id);
746 	dump_data->bus_type = cpu_to_le32(0);
747 	dump_data->target_version = cpu_to_le32(ar->target_version);
748 	dump_data->fw_version_major = cpu_to_le32(ar->fw_version_major);
749 	dump_data->fw_version_minor = cpu_to_le32(ar->fw_version_minor);
750 	dump_data->fw_version_release = cpu_to_le32(ar->fw_version_release);
751 	dump_data->fw_version_build = cpu_to_le32(ar->fw_version_build);
752 	dump_data->phy_capability = cpu_to_le32(ar->phy_capability);
753 	dump_data->hw_min_tx_power = cpu_to_le32(ar->hw_min_tx_power);
754 	dump_data->hw_max_tx_power = cpu_to_le32(ar->hw_max_tx_power);
755 	dump_data->ht_cap_info = cpu_to_le32(ar->ht_cap_info);
756 	dump_data->vht_cap_info = cpu_to_le32(ar->vht_cap_info);
757 	dump_data->num_rf_chains = cpu_to_le32(ar->num_rf_chains);
758 
759 	strlcpy(dump_data->fw_ver, ar->hw->wiphy->fw_version,
760 		sizeof(dump_data->fw_ver));
761 
762 	dump_data->kernel_ver_code = 0;
763 	strlcpy(dump_data->kernel_ver, init_utsname()->release,
764 		sizeof(dump_data->kernel_ver));
765 
766 	dump_data->tv_sec = cpu_to_le64(crash_data->timestamp.tv_sec);
767 	dump_data->tv_nsec = cpu_to_le64(crash_data->timestamp.tv_nsec);
768 
769 	/* Gather crash-dump */
770 	dump_tlv = (struct ath10k_tlv_dump_data *)(buf + sofar);
771 	dump_tlv->type = cpu_to_le32(ATH10K_FW_CRASH_DUMP_REGISTERS);
772 	dump_tlv->tlv_len = cpu_to_le32(sizeof(crash_data->registers));
773 	memcpy(dump_tlv->tlv_data, &crash_data->registers,
774 	       sizeof(crash_data->registers));
775 	sofar += sizeof(*dump_tlv) + sizeof(crash_data->registers);
776 
777 	ar->debug.fw_crash_data->crashed_since_read = false;
778 
779 	spin_unlock_bh(&ar->data_lock);
780 
781 	return dump_data;
782 }
783 
784 static int ath10k_fw_crash_dump_open(struct inode *inode, struct file *file)
785 {
786 	struct ath10k *ar = inode->i_private;
787 	struct ath10k_dump_file_data *dump;
788 
789 	dump = ath10k_build_dump_file(ar);
790 	if (!dump)
791 		return -ENODATA;
792 
793 	file->private_data = dump;
794 
795 	return 0;
796 }
797 
798 static ssize_t ath10k_fw_crash_dump_read(struct file *file,
799 					 char __user *user_buf,
800 					 size_t count, loff_t *ppos)
801 {
802 	struct ath10k_dump_file_data *dump_file = file->private_data;
803 
804 	return simple_read_from_buffer(user_buf, count, ppos,
805 				       dump_file,
806 				       le32_to_cpu(dump_file->len));
807 }
808 
809 static int ath10k_fw_crash_dump_release(struct inode *inode,
810 					struct file *file)
811 {
812 	vfree(file->private_data);
813 
814 	return 0;
815 }
816 
817 static const struct file_operations fops_fw_crash_dump = {
818 	.open = ath10k_fw_crash_dump_open,
819 	.read = ath10k_fw_crash_dump_read,
820 	.release = ath10k_fw_crash_dump_release,
821 	.owner = THIS_MODULE,
822 	.llseek = default_llseek,
823 };
824 
825 static ssize_t ath10k_reg_addr_read(struct file *file,
826 				    char __user *user_buf,
827 				    size_t count, loff_t *ppos)
828 {
829 	struct ath10k *ar = file->private_data;
830 	u8 buf[32];
831 	unsigned int len = 0;
832 	u32 reg_addr;
833 
834 	mutex_lock(&ar->conf_mutex);
835 	reg_addr = ar->debug.reg_addr;
836 	mutex_unlock(&ar->conf_mutex);
837 
838 	len += scnprintf(buf + len, sizeof(buf) - len, "0x%x\n", reg_addr);
839 
840 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
841 }
842 
843 static ssize_t ath10k_reg_addr_write(struct file *file,
844 				     const char __user *user_buf,
845 				     size_t count, loff_t *ppos)
846 {
847 	struct ath10k *ar = file->private_data;
848 	u32 reg_addr;
849 	int ret;
850 
851 	ret = kstrtou32_from_user(user_buf, count, 0, &reg_addr);
852 	if (ret)
853 		return ret;
854 
855 	if (!IS_ALIGNED(reg_addr, 4))
856 		return -EFAULT;
857 
858 	mutex_lock(&ar->conf_mutex);
859 	ar->debug.reg_addr = reg_addr;
860 	mutex_unlock(&ar->conf_mutex);
861 
862 	return count;
863 }
864 
865 static const struct file_operations fops_reg_addr = {
866 	.read = ath10k_reg_addr_read,
867 	.write = ath10k_reg_addr_write,
868 	.open = simple_open,
869 	.owner = THIS_MODULE,
870 	.llseek = default_llseek,
871 };
872 
873 static ssize_t ath10k_reg_value_read(struct file *file,
874 				     char __user *user_buf,
875 				     size_t count, loff_t *ppos)
876 {
877 	struct ath10k *ar = file->private_data;
878 	u8 buf[48];
879 	unsigned int len;
880 	u32 reg_addr, reg_val;
881 	int ret;
882 
883 	mutex_lock(&ar->conf_mutex);
884 
885 	if (ar->state != ATH10K_STATE_ON &&
886 	    ar->state != ATH10K_STATE_UTF) {
887 		ret = -ENETDOWN;
888 		goto exit;
889 	}
890 
891 	reg_addr = ar->debug.reg_addr;
892 
893 	reg_val = ath10k_hif_read32(ar, reg_addr);
894 	len = scnprintf(buf, sizeof(buf), "0x%08x:0x%08x\n", reg_addr, reg_val);
895 
896 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
897 
898 exit:
899 	mutex_unlock(&ar->conf_mutex);
900 
901 	return ret;
902 }
903 
904 static ssize_t ath10k_reg_value_write(struct file *file,
905 				      const char __user *user_buf,
906 				      size_t count, loff_t *ppos)
907 {
908 	struct ath10k *ar = file->private_data;
909 	u32 reg_addr, reg_val;
910 	int ret;
911 
912 	mutex_lock(&ar->conf_mutex);
913 
914 	if (ar->state != ATH10K_STATE_ON &&
915 	    ar->state != ATH10K_STATE_UTF) {
916 		ret = -ENETDOWN;
917 		goto exit;
918 	}
919 
920 	reg_addr = ar->debug.reg_addr;
921 
922 	ret = kstrtou32_from_user(user_buf, count, 0, &reg_val);
923 	if (ret)
924 		goto exit;
925 
926 	ath10k_hif_write32(ar, reg_addr, reg_val);
927 
928 	ret = count;
929 
930 exit:
931 	mutex_unlock(&ar->conf_mutex);
932 
933 	return ret;
934 }
935 
936 static const struct file_operations fops_reg_value = {
937 	.read = ath10k_reg_value_read,
938 	.write = ath10k_reg_value_write,
939 	.open = simple_open,
940 	.owner = THIS_MODULE,
941 	.llseek = default_llseek,
942 };
943 
944 static ssize_t ath10k_mem_value_read(struct file *file,
945 				     char __user *user_buf,
946 				     size_t count, loff_t *ppos)
947 {
948 	struct ath10k *ar = file->private_data;
949 	u8 *buf;
950 	int ret;
951 
952 	if (*ppos < 0)
953 		return -EINVAL;
954 
955 	if (!count)
956 		return 0;
957 
958 	mutex_lock(&ar->conf_mutex);
959 
960 	buf = vmalloc(count);
961 	if (!buf) {
962 		ret = -ENOMEM;
963 		goto exit;
964 	}
965 
966 	if (ar->state != ATH10K_STATE_ON &&
967 	    ar->state != ATH10K_STATE_UTF) {
968 		ret = -ENETDOWN;
969 		goto exit;
970 	}
971 
972 	ret = ath10k_hif_diag_read(ar, *ppos, buf, count);
973 	if (ret) {
974 		ath10k_warn(ar, "failed to read address 0x%08x via diagnose window fnrom debugfs: %d\n",
975 			    (u32)(*ppos), ret);
976 		goto exit;
977 	}
978 
979 	ret = copy_to_user(user_buf, buf, count);
980 	if (ret) {
981 		ret = -EFAULT;
982 		goto exit;
983 	}
984 
985 	count -= ret;
986 	*ppos += count;
987 	ret = count;
988 
989 exit:
990 	vfree(buf);
991 	mutex_unlock(&ar->conf_mutex);
992 
993 	return ret;
994 }
995 
996 static ssize_t ath10k_mem_value_write(struct file *file,
997 				      const char __user *user_buf,
998 				      size_t count, loff_t *ppos)
999 {
1000 	struct ath10k *ar = file->private_data;
1001 	u8 *buf;
1002 	int ret;
1003 
1004 	if (*ppos < 0)
1005 		return -EINVAL;
1006 
1007 	if (!count)
1008 		return 0;
1009 
1010 	mutex_lock(&ar->conf_mutex);
1011 
1012 	buf = vmalloc(count);
1013 	if (!buf) {
1014 		ret = -ENOMEM;
1015 		goto exit;
1016 	}
1017 
1018 	if (ar->state != ATH10K_STATE_ON &&
1019 	    ar->state != ATH10K_STATE_UTF) {
1020 		ret = -ENETDOWN;
1021 		goto exit;
1022 	}
1023 
1024 	ret = copy_from_user(buf, user_buf, count);
1025 	if (ret) {
1026 		ret = -EFAULT;
1027 		goto exit;
1028 	}
1029 
1030 	ret = ath10k_hif_diag_write(ar, *ppos, buf, count);
1031 	if (ret) {
1032 		ath10k_warn(ar, "failed to write address 0x%08x via diagnose window from debugfs: %d\n",
1033 			    (u32)(*ppos), ret);
1034 		goto exit;
1035 	}
1036 
1037 	*ppos += count;
1038 	ret = count;
1039 
1040 exit:
1041 	vfree(buf);
1042 	mutex_unlock(&ar->conf_mutex);
1043 
1044 	return ret;
1045 }
1046 
1047 static const struct file_operations fops_mem_value = {
1048 	.read = ath10k_mem_value_read,
1049 	.write = ath10k_mem_value_write,
1050 	.open = simple_open,
1051 	.owner = THIS_MODULE,
1052 	.llseek = default_llseek,
1053 };
1054 
1055 static int ath10k_debug_htt_stats_req(struct ath10k *ar)
1056 {
1057 	u64 cookie;
1058 	int ret;
1059 
1060 	lockdep_assert_held(&ar->conf_mutex);
1061 
1062 	if (ar->debug.htt_stats_mask == 0)
1063 		/* htt stats are disabled */
1064 		return 0;
1065 
1066 	if (ar->state != ATH10K_STATE_ON)
1067 		return 0;
1068 
1069 	cookie = get_jiffies_64();
1070 
1071 	ret = ath10k_htt_h2t_stats_req(&ar->htt, ar->debug.htt_stats_mask,
1072 				       cookie);
1073 	if (ret) {
1074 		ath10k_warn(ar, "failed to send htt stats request: %d\n", ret);
1075 		return ret;
1076 	}
1077 
1078 	queue_delayed_work(ar->workqueue, &ar->debug.htt_stats_dwork,
1079 			   msecs_to_jiffies(ATH10K_DEBUG_HTT_STATS_INTERVAL));
1080 
1081 	return 0;
1082 }
1083 
1084 static void ath10k_debug_htt_stats_dwork(struct work_struct *work)
1085 {
1086 	struct ath10k *ar = container_of(work, struct ath10k,
1087 					 debug.htt_stats_dwork.work);
1088 
1089 	mutex_lock(&ar->conf_mutex);
1090 
1091 	ath10k_debug_htt_stats_req(ar);
1092 
1093 	mutex_unlock(&ar->conf_mutex);
1094 }
1095 
1096 static ssize_t ath10k_read_htt_stats_mask(struct file *file,
1097 					  char __user *user_buf,
1098 					  size_t count, loff_t *ppos)
1099 {
1100 	struct ath10k *ar = file->private_data;
1101 	char buf[32];
1102 	unsigned int len;
1103 
1104 	len = scnprintf(buf, sizeof(buf), "%lu\n", ar->debug.htt_stats_mask);
1105 
1106 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1107 }
1108 
1109 static ssize_t ath10k_write_htt_stats_mask(struct file *file,
1110 					   const char __user *user_buf,
1111 					   size_t count, loff_t *ppos)
1112 {
1113 	struct ath10k *ar = file->private_data;
1114 	unsigned long mask;
1115 	int ret;
1116 
1117 	ret = kstrtoul_from_user(user_buf, count, 0, &mask);
1118 	if (ret)
1119 		return ret;
1120 
1121 	/* max 8 bit masks (for now) */
1122 	if (mask > 0xff)
1123 		return -E2BIG;
1124 
1125 	mutex_lock(&ar->conf_mutex);
1126 
1127 	ar->debug.htt_stats_mask = mask;
1128 
1129 	ret = ath10k_debug_htt_stats_req(ar);
1130 	if (ret)
1131 		goto out;
1132 
1133 	ret = count;
1134 
1135 out:
1136 	mutex_unlock(&ar->conf_mutex);
1137 
1138 	return ret;
1139 }
1140 
1141 static const struct file_operations fops_htt_stats_mask = {
1142 	.read = ath10k_read_htt_stats_mask,
1143 	.write = ath10k_write_htt_stats_mask,
1144 	.open = simple_open,
1145 	.owner = THIS_MODULE,
1146 	.llseek = default_llseek,
1147 };
1148 
1149 static ssize_t ath10k_read_htt_max_amsdu_ampdu(struct file *file,
1150 					       char __user *user_buf,
1151 					       size_t count, loff_t *ppos)
1152 {
1153 	struct ath10k *ar = file->private_data;
1154 	char buf[64];
1155 	u8 amsdu, ampdu;
1156 	unsigned int len;
1157 
1158 	mutex_lock(&ar->conf_mutex);
1159 
1160 	amsdu = ar->htt.max_num_amsdu;
1161 	ampdu = ar->htt.max_num_ampdu;
1162 	mutex_unlock(&ar->conf_mutex);
1163 
1164 	len = scnprintf(buf, sizeof(buf), "%u %u\n", amsdu, ampdu);
1165 
1166 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1167 }
1168 
1169 static ssize_t ath10k_write_htt_max_amsdu_ampdu(struct file *file,
1170 						const char __user *user_buf,
1171 						size_t count, loff_t *ppos)
1172 {
1173 	struct ath10k *ar = file->private_data;
1174 	int res;
1175 	char buf[64];
1176 	unsigned int amsdu, ampdu;
1177 
1178 	simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
1179 
1180 	/* make sure that buf is null terminated */
1181 	buf[sizeof(buf) - 1] = 0;
1182 
1183 	res = sscanf(buf, "%u %u", &amsdu, &ampdu);
1184 
1185 	if (res != 2)
1186 		return -EINVAL;
1187 
1188 	mutex_lock(&ar->conf_mutex);
1189 
1190 	res = ath10k_htt_h2t_aggr_cfg_msg(&ar->htt, ampdu, amsdu);
1191 	if (res)
1192 		goto out;
1193 
1194 	res = count;
1195 	ar->htt.max_num_amsdu = amsdu;
1196 	ar->htt.max_num_ampdu = ampdu;
1197 
1198 out:
1199 	mutex_unlock(&ar->conf_mutex);
1200 	return res;
1201 }
1202 
1203 static const struct file_operations fops_htt_max_amsdu_ampdu = {
1204 	.read = ath10k_read_htt_max_amsdu_ampdu,
1205 	.write = ath10k_write_htt_max_amsdu_ampdu,
1206 	.open = simple_open,
1207 	.owner = THIS_MODULE,
1208 	.llseek = default_llseek,
1209 };
1210 
1211 static ssize_t ath10k_read_fw_dbglog(struct file *file,
1212 				     char __user *user_buf,
1213 				     size_t count, loff_t *ppos)
1214 {
1215 	struct ath10k *ar = file->private_data;
1216 	unsigned int len;
1217 	char buf[64];
1218 
1219 	len = scnprintf(buf, sizeof(buf), "0x%08x %u\n",
1220 			ar->debug.fw_dbglog_mask, ar->debug.fw_dbglog_level);
1221 
1222 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1223 }
1224 
1225 static ssize_t ath10k_write_fw_dbglog(struct file *file,
1226 				      const char __user *user_buf,
1227 				      size_t count, loff_t *ppos)
1228 {
1229 	struct ath10k *ar = file->private_data;
1230 	int ret;
1231 	char buf[64];
1232 	unsigned int log_level, mask;
1233 
1234 	simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
1235 
1236 	/* make sure that buf is null terminated */
1237 	buf[sizeof(buf) - 1] = 0;
1238 
1239 	ret = sscanf(buf, "%x %u", &mask, &log_level);
1240 
1241 	if (!ret)
1242 		return -EINVAL;
1243 
1244 	if (ret == 1)
1245 		/* default if user did not specify */
1246 		log_level = ATH10K_DBGLOG_LEVEL_WARN;
1247 
1248 	mutex_lock(&ar->conf_mutex);
1249 
1250 	ar->debug.fw_dbglog_mask = mask;
1251 	ar->debug.fw_dbglog_level = log_level;
1252 
1253 	if (ar->state == ATH10K_STATE_ON) {
1254 		ret = ath10k_wmi_dbglog_cfg(ar, ar->debug.fw_dbglog_mask,
1255 					    ar->debug.fw_dbglog_level);
1256 		if (ret) {
1257 			ath10k_warn(ar, "dbglog cfg failed from debugfs: %d\n",
1258 				    ret);
1259 			goto exit;
1260 		}
1261 	}
1262 
1263 	ret = count;
1264 
1265 exit:
1266 	mutex_unlock(&ar->conf_mutex);
1267 
1268 	return ret;
1269 }
1270 
1271 /* TODO:  Would be nice to always support ethtool stats, would need to
1272  * move the stats storage out of ath10k_debug, or always have ath10k_debug
1273  * struct available..
1274  */
1275 
1276 /* This generally cooresponds to the debugfs fw_stats file */
1277 static const char ath10k_gstrings_stats[][ETH_GSTRING_LEN] = {
1278 	"tx_pkts_nic",
1279 	"tx_bytes_nic",
1280 	"rx_pkts_nic",
1281 	"rx_bytes_nic",
1282 	"d_noise_floor",
1283 	"d_cycle_count",
1284 	"d_phy_error",
1285 	"d_rts_bad",
1286 	"d_rts_good",
1287 	"d_tx_power", /* in .5 dbM I think */
1288 	"d_rx_crc_err", /* fcs_bad */
1289 	"d_no_beacon",
1290 	"d_tx_mpdus_queued",
1291 	"d_tx_msdu_queued",
1292 	"d_tx_msdu_dropped",
1293 	"d_local_enqued",
1294 	"d_local_freed",
1295 	"d_tx_ppdu_hw_queued",
1296 	"d_tx_ppdu_reaped",
1297 	"d_tx_fifo_underrun",
1298 	"d_tx_ppdu_abort",
1299 	"d_tx_mpdu_requed",
1300 	"d_tx_excessive_retries",
1301 	"d_tx_hw_rate",
1302 	"d_tx_dropped_sw_retries",
1303 	"d_tx_illegal_rate",
1304 	"d_tx_continuous_xretries",
1305 	"d_tx_timeout",
1306 	"d_tx_mpdu_txop_limit",
1307 	"d_pdev_resets",
1308 	"d_rx_mid_ppdu_route_change",
1309 	"d_rx_status",
1310 	"d_rx_extra_frags_ring0",
1311 	"d_rx_extra_frags_ring1",
1312 	"d_rx_extra_frags_ring2",
1313 	"d_rx_extra_frags_ring3",
1314 	"d_rx_msdu_htt",
1315 	"d_rx_mpdu_htt",
1316 	"d_rx_msdu_stack",
1317 	"d_rx_mpdu_stack",
1318 	"d_rx_phy_err",
1319 	"d_rx_phy_err_drops",
1320 	"d_rx_mpdu_errors", /* FCS, MIC, ENC */
1321 	"d_fw_crash_count",
1322 	"d_fw_warm_reset_count",
1323 	"d_fw_cold_reset_count",
1324 };
1325 
1326 #define ATH10K_SSTATS_LEN ARRAY_SIZE(ath10k_gstrings_stats)
1327 
1328 void ath10k_debug_get_et_strings(struct ieee80211_hw *hw,
1329 				 struct ieee80211_vif *vif,
1330 				 u32 sset, u8 *data)
1331 {
1332 	if (sset == ETH_SS_STATS)
1333 		memcpy(data, *ath10k_gstrings_stats,
1334 		       sizeof(ath10k_gstrings_stats));
1335 }
1336 
1337 int ath10k_debug_get_et_sset_count(struct ieee80211_hw *hw,
1338 				   struct ieee80211_vif *vif, int sset)
1339 {
1340 	if (sset == ETH_SS_STATS)
1341 		return ATH10K_SSTATS_LEN;
1342 
1343 	return 0;
1344 }
1345 
1346 void ath10k_debug_get_et_stats(struct ieee80211_hw *hw,
1347 			       struct ieee80211_vif *vif,
1348 			       struct ethtool_stats *stats, u64 *data)
1349 {
1350 	struct ath10k *ar = hw->priv;
1351 	static const struct ath10k_fw_stats_pdev zero_stats = {};
1352 	const struct ath10k_fw_stats_pdev *pdev_stats;
1353 	int i = 0, ret;
1354 
1355 	mutex_lock(&ar->conf_mutex);
1356 
1357 	if (ar->state == ATH10K_STATE_ON) {
1358 		ret = ath10k_debug_fw_stats_request(ar);
1359 		if (ret) {
1360 			/* just print a warning and try to use older results */
1361 			ath10k_warn(ar,
1362 				    "failed to get fw stats for ethtool: %d\n",
1363 				    ret);
1364 		}
1365 	}
1366 
1367 	pdev_stats = list_first_entry_or_null(&ar->debug.fw_stats.pdevs,
1368 					      struct ath10k_fw_stats_pdev,
1369 					      list);
1370 	if (!pdev_stats) {
1371 		/* no results available so just return zeroes */
1372 		pdev_stats = &zero_stats;
1373 	}
1374 
1375 	spin_lock_bh(&ar->data_lock);
1376 
1377 	data[i++] = pdev_stats->hw_reaped; /* ppdu reaped */
1378 	data[i++] = 0; /* tx bytes */
1379 	data[i++] = pdev_stats->htt_mpdus;
1380 	data[i++] = 0; /* rx bytes */
1381 	data[i++] = pdev_stats->ch_noise_floor;
1382 	data[i++] = pdev_stats->cycle_count;
1383 	data[i++] = pdev_stats->phy_err_count;
1384 	data[i++] = pdev_stats->rts_bad;
1385 	data[i++] = pdev_stats->rts_good;
1386 	data[i++] = pdev_stats->chan_tx_power;
1387 	data[i++] = pdev_stats->fcs_bad;
1388 	data[i++] = pdev_stats->no_beacons;
1389 	data[i++] = pdev_stats->mpdu_enqued;
1390 	data[i++] = pdev_stats->msdu_enqued;
1391 	data[i++] = pdev_stats->wmm_drop;
1392 	data[i++] = pdev_stats->local_enqued;
1393 	data[i++] = pdev_stats->local_freed;
1394 	data[i++] = pdev_stats->hw_queued;
1395 	data[i++] = pdev_stats->hw_reaped;
1396 	data[i++] = pdev_stats->underrun;
1397 	data[i++] = pdev_stats->tx_abort;
1398 	data[i++] = pdev_stats->mpdus_requed;
1399 	data[i++] = pdev_stats->tx_ko;
1400 	data[i++] = pdev_stats->data_rc;
1401 	data[i++] = pdev_stats->sw_retry_failure;
1402 	data[i++] = pdev_stats->illgl_rate_phy_err;
1403 	data[i++] = pdev_stats->pdev_cont_xretry;
1404 	data[i++] = pdev_stats->pdev_tx_timeout;
1405 	data[i++] = pdev_stats->txop_ovf;
1406 	data[i++] = pdev_stats->pdev_resets;
1407 	data[i++] = pdev_stats->mid_ppdu_route_change;
1408 	data[i++] = pdev_stats->status_rcvd;
1409 	data[i++] = pdev_stats->r0_frags;
1410 	data[i++] = pdev_stats->r1_frags;
1411 	data[i++] = pdev_stats->r2_frags;
1412 	data[i++] = pdev_stats->r3_frags;
1413 	data[i++] = pdev_stats->htt_msdus;
1414 	data[i++] = pdev_stats->htt_mpdus;
1415 	data[i++] = pdev_stats->loc_msdus;
1416 	data[i++] = pdev_stats->loc_mpdus;
1417 	data[i++] = pdev_stats->phy_errs;
1418 	data[i++] = pdev_stats->phy_err_drop;
1419 	data[i++] = pdev_stats->mpdu_errs;
1420 	data[i++] = ar->stats.fw_crash_counter;
1421 	data[i++] = ar->stats.fw_warm_reset_counter;
1422 	data[i++] = ar->stats.fw_cold_reset_counter;
1423 
1424 	spin_unlock_bh(&ar->data_lock);
1425 
1426 	mutex_unlock(&ar->conf_mutex);
1427 
1428 	WARN_ON(i != ATH10K_SSTATS_LEN);
1429 }
1430 
1431 static const struct file_operations fops_fw_dbglog = {
1432 	.read = ath10k_read_fw_dbglog,
1433 	.write = ath10k_write_fw_dbglog,
1434 	.open = simple_open,
1435 	.owner = THIS_MODULE,
1436 	.llseek = default_llseek,
1437 };
1438 
1439 static int ath10k_debug_cal_data_open(struct inode *inode, struct file *file)
1440 {
1441 	struct ath10k *ar = inode->i_private;
1442 	void *buf;
1443 	u32 hi_addr;
1444 	__le32 addr;
1445 	int ret;
1446 
1447 	mutex_lock(&ar->conf_mutex);
1448 
1449 	if (ar->state != ATH10K_STATE_ON &&
1450 	    ar->state != ATH10K_STATE_UTF) {
1451 		ret = -ENETDOWN;
1452 		goto err;
1453 	}
1454 
1455 	buf = vmalloc(ar->hw_params.cal_data_len);
1456 	if (!buf) {
1457 		ret = -ENOMEM;
1458 		goto err;
1459 	}
1460 
1461 	hi_addr = host_interest_item_address(HI_ITEM(hi_board_data));
1462 
1463 	ret = ath10k_hif_diag_read(ar, hi_addr, &addr, sizeof(addr));
1464 	if (ret) {
1465 		ath10k_warn(ar, "failed to read hi_board_data address: %d\n", ret);
1466 		goto err_vfree;
1467 	}
1468 
1469 	ret = ath10k_hif_diag_read(ar, le32_to_cpu(addr), buf,
1470 				   ar->hw_params.cal_data_len);
1471 	if (ret) {
1472 		ath10k_warn(ar, "failed to read calibration data: %d\n", ret);
1473 		goto err_vfree;
1474 	}
1475 
1476 	file->private_data = buf;
1477 
1478 	mutex_unlock(&ar->conf_mutex);
1479 
1480 	return 0;
1481 
1482 err_vfree:
1483 	vfree(buf);
1484 
1485 err:
1486 	mutex_unlock(&ar->conf_mutex);
1487 
1488 	return ret;
1489 }
1490 
1491 static ssize_t ath10k_debug_cal_data_read(struct file *file,
1492 					  char __user *user_buf,
1493 					  size_t count, loff_t *ppos)
1494 {
1495 	struct ath10k *ar = file->private_data;
1496 	void *buf = file->private_data;
1497 
1498 	return simple_read_from_buffer(user_buf, count, ppos,
1499 				       buf, ar->hw_params.cal_data_len);
1500 }
1501 
1502 static int ath10k_debug_cal_data_release(struct inode *inode,
1503 					 struct file *file)
1504 {
1505 	vfree(file->private_data);
1506 
1507 	return 0;
1508 }
1509 
1510 static ssize_t ath10k_write_ani_enable(struct file *file,
1511 				       const char __user *user_buf,
1512 				       size_t count, loff_t *ppos)
1513 {
1514 	struct ath10k *ar = file->private_data;
1515 	int ret;
1516 	u8 enable;
1517 
1518 	if (kstrtou8_from_user(user_buf, count, 0, &enable))
1519 		return -EINVAL;
1520 
1521 	mutex_lock(&ar->conf_mutex);
1522 
1523 	if (ar->ani_enabled == enable) {
1524 		ret = count;
1525 		goto exit;
1526 	}
1527 
1528 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->ani_enable,
1529 					enable);
1530 	if (ret) {
1531 		ath10k_warn(ar, "ani_enable failed from debugfs: %d\n", ret);
1532 		goto exit;
1533 	}
1534 	ar->ani_enabled = enable;
1535 
1536 	ret = count;
1537 
1538 exit:
1539 	mutex_unlock(&ar->conf_mutex);
1540 
1541 	return ret;
1542 }
1543 
1544 static ssize_t ath10k_read_ani_enable(struct file *file, char __user *user_buf,
1545 				      size_t count, loff_t *ppos)
1546 {
1547 	struct ath10k *ar = file->private_data;
1548 	int len = 0;
1549 	char buf[32];
1550 
1551 	len = scnprintf(buf, sizeof(buf) - len, "%d\n",
1552 			ar->ani_enabled);
1553 
1554 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1555 }
1556 
1557 static const struct file_operations fops_ani_enable = {
1558 	.read = ath10k_read_ani_enable,
1559 	.write = ath10k_write_ani_enable,
1560 	.open = simple_open,
1561 	.owner = THIS_MODULE,
1562 	.llseek = default_llseek,
1563 };
1564 
1565 static const struct file_operations fops_cal_data = {
1566 	.open = ath10k_debug_cal_data_open,
1567 	.read = ath10k_debug_cal_data_read,
1568 	.release = ath10k_debug_cal_data_release,
1569 	.owner = THIS_MODULE,
1570 	.llseek = default_llseek,
1571 };
1572 
1573 static ssize_t ath10k_read_nf_cal_period(struct file *file,
1574 					 char __user *user_buf,
1575 					 size_t count, loff_t *ppos)
1576 {
1577 	struct ath10k *ar = file->private_data;
1578 	unsigned int len;
1579 	char buf[32];
1580 
1581 	len = scnprintf(buf, sizeof(buf), "%d\n",
1582 			ar->debug.nf_cal_period);
1583 
1584 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1585 }
1586 
1587 static ssize_t ath10k_write_nf_cal_period(struct file *file,
1588 					  const char __user *user_buf,
1589 					  size_t count, loff_t *ppos)
1590 {
1591 	struct ath10k *ar = file->private_data;
1592 	unsigned long period;
1593 	int ret;
1594 
1595 	ret = kstrtoul_from_user(user_buf, count, 0, &period);
1596 	if (ret)
1597 		return ret;
1598 
1599 	if (period > WMI_PDEV_PARAM_CAL_PERIOD_MAX)
1600 		return -EINVAL;
1601 
1602 	/* there's no way to switch back to the firmware default */
1603 	if (period == 0)
1604 		return -EINVAL;
1605 
1606 	mutex_lock(&ar->conf_mutex);
1607 
1608 	ar->debug.nf_cal_period = period;
1609 
1610 	if (ar->state != ATH10K_STATE_ON) {
1611 		/* firmware is not running, nothing else to do */
1612 		ret = count;
1613 		goto exit;
1614 	}
1615 
1616 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->cal_period,
1617 					ar->debug.nf_cal_period);
1618 	if (ret) {
1619 		ath10k_warn(ar, "cal period cfg failed from debugfs: %d\n",
1620 			    ret);
1621 		goto exit;
1622 	}
1623 
1624 	ret = count;
1625 
1626 exit:
1627 	mutex_unlock(&ar->conf_mutex);
1628 
1629 	return ret;
1630 }
1631 
1632 static const struct file_operations fops_nf_cal_period = {
1633 	.read = ath10k_read_nf_cal_period,
1634 	.write = ath10k_write_nf_cal_period,
1635 	.open = simple_open,
1636 	.owner = THIS_MODULE,
1637 	.llseek = default_llseek,
1638 };
1639 
1640 #define ATH10K_TPC_CONFIG_BUF_SIZE	(1024 * 1024)
1641 
1642 static int ath10k_debug_tpc_stats_request(struct ath10k *ar)
1643 {
1644 	int ret;
1645 	unsigned long time_left;
1646 
1647 	lockdep_assert_held(&ar->conf_mutex);
1648 
1649 	reinit_completion(&ar->debug.tpc_complete);
1650 
1651 	ret = ath10k_wmi_pdev_get_tpc_config(ar, WMI_TPC_CONFIG_PARAM);
1652 	if (ret) {
1653 		ath10k_warn(ar, "failed to request tpc config: %d\n", ret);
1654 		return ret;
1655 	}
1656 
1657 	time_left = wait_for_completion_timeout(&ar->debug.tpc_complete,
1658 						1 * HZ);
1659 	if (time_left == 0)
1660 		return -ETIMEDOUT;
1661 
1662 	return 0;
1663 }
1664 
1665 void ath10k_debug_tpc_stats_process(struct ath10k *ar,
1666 				    struct ath10k_tpc_stats *tpc_stats)
1667 {
1668 	spin_lock_bh(&ar->data_lock);
1669 
1670 	kfree(ar->debug.tpc_stats);
1671 	ar->debug.tpc_stats = tpc_stats;
1672 	complete(&ar->debug.tpc_complete);
1673 
1674 	spin_unlock_bh(&ar->data_lock);
1675 }
1676 
1677 static void ath10k_tpc_stats_print(struct ath10k_tpc_stats *tpc_stats,
1678 				   unsigned int j, char *buf, unsigned int *len)
1679 {
1680 	unsigned int i, buf_len;
1681 	static const char table_str[][5] = { "CDD",
1682 					     "STBC",
1683 					     "TXBF" };
1684 	static const char pream_str[][6] = { "CCK",
1685 					     "OFDM",
1686 					     "HT20",
1687 					     "HT40",
1688 					     "VHT20",
1689 					     "VHT40",
1690 					     "VHT80",
1691 					     "HTCUP" };
1692 
1693 	buf_len = ATH10K_TPC_CONFIG_BUF_SIZE;
1694 	*len += scnprintf(buf + *len, buf_len - *len,
1695 			  "********************************\n");
1696 	*len += scnprintf(buf + *len, buf_len - *len,
1697 			  "******************* %s POWER TABLE ****************\n",
1698 			  table_str[j]);
1699 	*len += scnprintf(buf + *len, buf_len - *len,
1700 			  "********************************\n");
1701 	*len += scnprintf(buf + *len, buf_len - *len,
1702 			  "No.  Preamble Rate_code tpc_value1 tpc_value2 tpc_value3\n");
1703 
1704 	for (i = 0; i < tpc_stats->rate_max; i++) {
1705 		*len += scnprintf(buf + *len, buf_len - *len,
1706 				  "%8d %s 0x%2x %s\n", i,
1707 				  pream_str[tpc_stats->tpc_table[j].pream_idx[i]],
1708 				  tpc_stats->tpc_table[j].rate_code[i],
1709 				  tpc_stats->tpc_table[j].tpc_value[i]);
1710 	}
1711 
1712 	*len += scnprintf(buf + *len, buf_len - *len,
1713 			  "***********************************\n");
1714 }
1715 
1716 static void ath10k_tpc_stats_fill(struct ath10k *ar,
1717 				  struct ath10k_tpc_stats *tpc_stats,
1718 				  char *buf)
1719 {
1720 	unsigned int len, j, buf_len;
1721 
1722 	len = 0;
1723 	buf_len = ATH10K_TPC_CONFIG_BUF_SIZE;
1724 
1725 	spin_lock_bh(&ar->data_lock);
1726 
1727 	if (!tpc_stats) {
1728 		ath10k_warn(ar, "failed to get tpc stats\n");
1729 		goto unlock;
1730 	}
1731 
1732 	len += scnprintf(buf + len, buf_len - len, "\n");
1733 	len += scnprintf(buf + len, buf_len - len,
1734 			 "*************************************\n");
1735 	len += scnprintf(buf + len, buf_len - len,
1736 			 "TPC config for channel %4d mode %d\n",
1737 			 tpc_stats->chan_freq,
1738 			 tpc_stats->phy_mode);
1739 	len += scnprintf(buf + len, buf_len - len,
1740 			 "*************************************\n");
1741 	len += scnprintf(buf + len, buf_len - len,
1742 			 "CTL		=  0x%2x Reg. Domain		= %2d\n",
1743 			 tpc_stats->ctl,
1744 			 tpc_stats->reg_domain);
1745 	len += scnprintf(buf + len, buf_len - len,
1746 			 "Antenna Gain	= %2d Reg. Max Antenna Gain	=  %2d\n",
1747 			 tpc_stats->twice_antenna_gain,
1748 			 tpc_stats->twice_antenna_reduction);
1749 	len += scnprintf(buf + len, buf_len - len,
1750 			 "Power Limit	= %2d Reg. Max Power		= %2d\n",
1751 			 tpc_stats->power_limit,
1752 			 tpc_stats->twice_max_rd_power / 2);
1753 	len += scnprintf(buf + len, buf_len - len,
1754 			 "Num tx chains	= %2d Num supported rates	= %2d\n",
1755 			 tpc_stats->num_tx_chain,
1756 			 tpc_stats->rate_max);
1757 
1758 	for (j = 0; j < tpc_stats->num_tx_chain ; j++) {
1759 		switch (j) {
1760 		case WMI_TPC_TABLE_TYPE_CDD:
1761 			if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) {
1762 				len += scnprintf(buf + len, buf_len - len,
1763 						 "CDD not supported\n");
1764 				break;
1765 			}
1766 
1767 			ath10k_tpc_stats_print(tpc_stats, j, buf, &len);
1768 			break;
1769 		case WMI_TPC_TABLE_TYPE_STBC:
1770 			if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) {
1771 				len += scnprintf(buf + len, buf_len - len,
1772 						 "STBC not supported\n");
1773 				break;
1774 			}
1775 
1776 			ath10k_tpc_stats_print(tpc_stats, j, buf, &len);
1777 			break;
1778 		case WMI_TPC_TABLE_TYPE_TXBF:
1779 			if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) {
1780 				len += scnprintf(buf + len, buf_len - len,
1781 						 "TXBF not supported\n***************************\n");
1782 				break;
1783 			}
1784 
1785 			ath10k_tpc_stats_print(tpc_stats, j, buf, &len);
1786 			break;
1787 		default:
1788 			len += scnprintf(buf + len, buf_len - len,
1789 					 "Invalid Type\n");
1790 			break;
1791 		}
1792 	}
1793 
1794 unlock:
1795 	spin_unlock_bh(&ar->data_lock);
1796 
1797 	if (len >= buf_len)
1798 		buf[len - 1] = 0;
1799 	else
1800 		buf[len] = 0;
1801 }
1802 
1803 static int ath10k_tpc_stats_open(struct inode *inode, struct file *file)
1804 {
1805 	struct ath10k *ar = inode->i_private;
1806 	void *buf = NULL;
1807 	int ret;
1808 
1809 	mutex_lock(&ar->conf_mutex);
1810 
1811 	if (ar->state != ATH10K_STATE_ON) {
1812 		ret = -ENETDOWN;
1813 		goto err_unlock;
1814 	}
1815 
1816 	buf = vmalloc(ATH10K_TPC_CONFIG_BUF_SIZE);
1817 	if (!buf) {
1818 		ret = -ENOMEM;
1819 		goto err_unlock;
1820 	}
1821 
1822 	ret = ath10k_debug_tpc_stats_request(ar);
1823 	if (ret) {
1824 		ath10k_warn(ar, "failed to request tpc config stats: %d\n",
1825 			    ret);
1826 		goto err_free;
1827 	}
1828 
1829 	ath10k_tpc_stats_fill(ar, ar->debug.tpc_stats, buf);
1830 	file->private_data = buf;
1831 
1832 	mutex_unlock(&ar->conf_mutex);
1833 	return 0;
1834 
1835 err_free:
1836 	vfree(buf);
1837 
1838 err_unlock:
1839 	mutex_unlock(&ar->conf_mutex);
1840 	return ret;
1841 }
1842 
1843 static int ath10k_tpc_stats_release(struct inode *inode, struct file *file)
1844 {
1845 	vfree(file->private_data);
1846 
1847 	return 0;
1848 }
1849 
1850 static ssize_t ath10k_tpc_stats_read(struct file *file, char __user *user_buf,
1851 				     size_t count, loff_t *ppos)
1852 {
1853 	const char *buf = file->private_data;
1854 	unsigned int len = strlen(buf);
1855 
1856 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1857 }
1858 
1859 static const struct file_operations fops_tpc_stats = {
1860 	.open = ath10k_tpc_stats_open,
1861 	.release = ath10k_tpc_stats_release,
1862 	.read = ath10k_tpc_stats_read,
1863 	.owner = THIS_MODULE,
1864 	.llseek = default_llseek,
1865 };
1866 
1867 int ath10k_debug_start(struct ath10k *ar)
1868 {
1869 	int ret;
1870 
1871 	lockdep_assert_held(&ar->conf_mutex);
1872 
1873 	ret = ath10k_debug_htt_stats_req(ar);
1874 	if (ret)
1875 		/* continue normally anyway, this isn't serious */
1876 		ath10k_warn(ar, "failed to start htt stats workqueue: %d\n",
1877 			    ret);
1878 
1879 	if (ar->debug.fw_dbglog_mask) {
1880 		ret = ath10k_wmi_dbglog_cfg(ar, ar->debug.fw_dbglog_mask,
1881 					    ATH10K_DBGLOG_LEVEL_WARN);
1882 		if (ret)
1883 			/* not serious */
1884 			ath10k_warn(ar, "failed to enable dbglog during start: %d",
1885 				    ret);
1886 	}
1887 
1888 	if (ar->debug.pktlog_filter) {
1889 		ret = ath10k_wmi_pdev_pktlog_enable(ar,
1890 						    ar->debug.pktlog_filter);
1891 		if (ret)
1892 			/* not serious */
1893 			ath10k_warn(ar,
1894 				    "failed to enable pktlog filter %x: %d\n",
1895 				    ar->debug.pktlog_filter, ret);
1896 	} else {
1897 		ret = ath10k_wmi_pdev_pktlog_disable(ar);
1898 		if (ret)
1899 			/* not serious */
1900 			ath10k_warn(ar, "failed to disable pktlog: %d\n", ret);
1901 	}
1902 
1903 	if (ar->debug.nf_cal_period) {
1904 		ret = ath10k_wmi_pdev_set_param(ar,
1905 						ar->wmi.pdev_param->cal_period,
1906 						ar->debug.nf_cal_period);
1907 		if (ret)
1908 			/* not serious */
1909 			ath10k_warn(ar, "cal period cfg failed from debug start: %d\n",
1910 				    ret);
1911 	}
1912 
1913 	return ret;
1914 }
1915 
1916 void ath10k_debug_stop(struct ath10k *ar)
1917 {
1918 	lockdep_assert_held(&ar->conf_mutex);
1919 
1920 	/* Must not use _sync to avoid deadlock, we do that in
1921 	 * ath10k_debug_destroy(). The check for htt_stats_mask is to avoid
1922 	 * warning from del_timer(). */
1923 	if (ar->debug.htt_stats_mask != 0)
1924 		cancel_delayed_work(&ar->debug.htt_stats_dwork);
1925 
1926 	ath10k_wmi_pdev_pktlog_disable(ar);
1927 }
1928 
1929 static ssize_t ath10k_write_simulate_radar(struct file *file,
1930 					   const char __user *user_buf,
1931 					   size_t count, loff_t *ppos)
1932 {
1933 	struct ath10k *ar = file->private_data;
1934 
1935 	ieee80211_radar_detected(ar->hw);
1936 
1937 	return count;
1938 }
1939 
1940 static const struct file_operations fops_simulate_radar = {
1941 	.write = ath10k_write_simulate_radar,
1942 	.open = simple_open,
1943 	.owner = THIS_MODULE,
1944 	.llseek = default_llseek,
1945 };
1946 
1947 #define ATH10K_DFS_STAT(s, p) (\
1948 	len += scnprintf(buf + len, size - len, "%-28s : %10u\n", s, \
1949 			 ar->debug.dfs_stats.p))
1950 
1951 #define ATH10K_DFS_POOL_STAT(s, p) (\
1952 	len += scnprintf(buf + len, size - len, "%-28s : %10u\n", s, \
1953 			 ar->debug.dfs_pool_stats.p))
1954 
1955 static ssize_t ath10k_read_dfs_stats(struct file *file, char __user *user_buf,
1956 				     size_t count, loff_t *ppos)
1957 {
1958 	int retval = 0, len = 0;
1959 	const int size = 8000;
1960 	struct ath10k *ar = file->private_data;
1961 	char *buf;
1962 
1963 	buf = kzalloc(size, GFP_KERNEL);
1964 	if (buf == NULL)
1965 		return -ENOMEM;
1966 
1967 	if (!ar->dfs_detector) {
1968 		len += scnprintf(buf + len, size - len, "DFS not enabled\n");
1969 		goto exit;
1970 	}
1971 
1972 	ar->debug.dfs_pool_stats =
1973 			ar->dfs_detector->get_stats(ar->dfs_detector);
1974 
1975 	len += scnprintf(buf + len, size - len, "Pulse detector statistics:\n");
1976 
1977 	ATH10K_DFS_STAT("reported phy errors", phy_errors);
1978 	ATH10K_DFS_STAT("pulse events reported", pulses_total);
1979 	ATH10K_DFS_STAT("DFS pulses detected", pulses_detected);
1980 	ATH10K_DFS_STAT("DFS pulses discarded", pulses_discarded);
1981 	ATH10K_DFS_STAT("Radars detected", radar_detected);
1982 
1983 	len += scnprintf(buf + len, size - len, "Global Pool statistics:\n");
1984 	ATH10K_DFS_POOL_STAT("Pool references", pool_reference);
1985 	ATH10K_DFS_POOL_STAT("Pulses allocated", pulse_allocated);
1986 	ATH10K_DFS_POOL_STAT("Pulses alloc error", pulse_alloc_error);
1987 	ATH10K_DFS_POOL_STAT("Pulses in use", pulse_used);
1988 	ATH10K_DFS_POOL_STAT("Seqs. allocated", pseq_allocated);
1989 	ATH10K_DFS_POOL_STAT("Seqs. alloc error", pseq_alloc_error);
1990 	ATH10K_DFS_POOL_STAT("Seqs. in use", pseq_used);
1991 
1992 exit:
1993 	if (len > size)
1994 		len = size;
1995 
1996 	retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
1997 	kfree(buf);
1998 
1999 	return retval;
2000 }
2001 
2002 static const struct file_operations fops_dfs_stats = {
2003 	.read = ath10k_read_dfs_stats,
2004 	.open = simple_open,
2005 	.owner = THIS_MODULE,
2006 	.llseek = default_llseek,
2007 };
2008 
2009 static ssize_t ath10k_write_pktlog_filter(struct file *file,
2010 					  const char __user *ubuf,
2011 					  size_t count, loff_t *ppos)
2012 {
2013 	struct ath10k *ar = file->private_data;
2014 	u32 filter;
2015 	int ret;
2016 
2017 	if (kstrtouint_from_user(ubuf, count, 0, &filter))
2018 		return -EINVAL;
2019 
2020 	mutex_lock(&ar->conf_mutex);
2021 
2022 	if (ar->state != ATH10K_STATE_ON) {
2023 		ar->debug.pktlog_filter = filter;
2024 		ret = count;
2025 		goto out;
2026 	}
2027 
2028 	if (filter == ar->debug.pktlog_filter) {
2029 		ret = count;
2030 		goto out;
2031 	}
2032 
2033 	if (filter) {
2034 		ret = ath10k_wmi_pdev_pktlog_enable(ar, filter);
2035 		if (ret) {
2036 			ath10k_warn(ar, "failed to enable pktlog filter %x: %d\n",
2037 				    ar->debug.pktlog_filter, ret);
2038 			goto out;
2039 		}
2040 	} else {
2041 		ret = ath10k_wmi_pdev_pktlog_disable(ar);
2042 		if (ret) {
2043 			ath10k_warn(ar, "failed to disable pktlog: %d\n", ret);
2044 			goto out;
2045 		}
2046 	}
2047 
2048 	ar->debug.pktlog_filter = filter;
2049 	ret = count;
2050 
2051 out:
2052 	mutex_unlock(&ar->conf_mutex);
2053 	return ret;
2054 }
2055 
2056 static ssize_t ath10k_read_pktlog_filter(struct file *file, char __user *ubuf,
2057 					 size_t count, loff_t *ppos)
2058 {
2059 	char buf[32];
2060 	struct ath10k *ar = file->private_data;
2061 	int len = 0;
2062 
2063 	mutex_lock(&ar->conf_mutex);
2064 	len = scnprintf(buf, sizeof(buf) - len, "%08x\n",
2065 			ar->debug.pktlog_filter);
2066 	mutex_unlock(&ar->conf_mutex);
2067 
2068 	return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2069 }
2070 
2071 static const struct file_operations fops_pktlog_filter = {
2072 	.read = ath10k_read_pktlog_filter,
2073 	.write = ath10k_write_pktlog_filter,
2074 	.open = simple_open
2075 };
2076 
2077 static ssize_t ath10k_write_quiet_period(struct file *file,
2078 					 const char __user *ubuf,
2079 					 size_t count, loff_t *ppos)
2080 {
2081 	struct ath10k *ar = file->private_data;
2082 	u32 period;
2083 
2084 	if (kstrtouint_from_user(ubuf, count, 0, &period))
2085 		return -EINVAL;
2086 
2087 	if (period < ATH10K_QUIET_PERIOD_MIN) {
2088 		ath10k_warn(ar, "Quiet period %u can not be lesser than 25ms\n",
2089 			    period);
2090 		return -EINVAL;
2091 	}
2092 	mutex_lock(&ar->conf_mutex);
2093 	ar->thermal.quiet_period = period;
2094 	ath10k_thermal_set_throttling(ar);
2095 	mutex_unlock(&ar->conf_mutex);
2096 
2097 	return count;
2098 }
2099 
2100 static ssize_t ath10k_read_quiet_period(struct file *file, char __user *ubuf,
2101 					size_t count, loff_t *ppos)
2102 {
2103 	char buf[32];
2104 	struct ath10k *ar = file->private_data;
2105 	int len = 0;
2106 
2107 	mutex_lock(&ar->conf_mutex);
2108 	len = scnprintf(buf, sizeof(buf) - len, "%d\n",
2109 			ar->thermal.quiet_period);
2110 	mutex_unlock(&ar->conf_mutex);
2111 
2112 	return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2113 }
2114 
2115 static const struct file_operations fops_quiet_period = {
2116 	.read = ath10k_read_quiet_period,
2117 	.write = ath10k_write_quiet_period,
2118 	.open = simple_open
2119 };
2120 
2121 static ssize_t ath10k_write_btcoex(struct file *file,
2122 				   const char __user *ubuf,
2123 				   size_t count, loff_t *ppos)
2124 {
2125 	struct ath10k *ar = file->private_data;
2126 	char buf[32];
2127 	size_t buf_size;
2128 	int ret;
2129 	bool val;
2130 
2131 	buf_size = min(count, (sizeof(buf) - 1));
2132 	if (copy_from_user(buf, ubuf, buf_size))
2133 		return -EFAULT;
2134 
2135 	buf[buf_size] = '\0';
2136 
2137 	if (strtobool(buf, &val) != 0)
2138 		return -EINVAL;
2139 
2140 	mutex_lock(&ar->conf_mutex);
2141 
2142 	if (ar->state != ATH10K_STATE_ON &&
2143 	    ar->state != ATH10K_STATE_RESTARTED) {
2144 		ret = -ENETDOWN;
2145 		goto exit;
2146 	}
2147 
2148 	if (!(test_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags) ^ val)) {
2149 		ret = count;
2150 		goto exit;
2151 	}
2152 
2153 	if (val)
2154 		set_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
2155 	else
2156 		clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
2157 
2158 	ath10k_info(ar, "restarting firmware due to btcoex change");
2159 
2160 	queue_work(ar->workqueue, &ar->restart_work);
2161 	ret = count;
2162 
2163 exit:
2164 	mutex_unlock(&ar->conf_mutex);
2165 
2166 	return ret;
2167 }
2168 
2169 static ssize_t ath10k_read_btcoex(struct file *file, char __user *ubuf,
2170 				  size_t count, loff_t *ppos)
2171 {
2172 	char buf[32];
2173 	struct ath10k *ar = file->private_data;
2174 	int len = 0;
2175 
2176 	mutex_lock(&ar->conf_mutex);
2177 	len = scnprintf(buf, sizeof(buf) - len, "%d\n",
2178 			test_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags));
2179 	mutex_unlock(&ar->conf_mutex);
2180 
2181 	return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2182 }
2183 
2184 static const struct file_operations fops_btcoex = {
2185 	.read = ath10k_read_btcoex,
2186 	.write = ath10k_write_btcoex,
2187 	.open = simple_open
2188 };
2189 
2190 static ssize_t ath10k_write_peer_stats(struct file *file,
2191 				       const char __user *ubuf,
2192 				       size_t count, loff_t *ppos)
2193 {
2194 	struct ath10k *ar = file->private_data;
2195 	char buf[32];
2196 	size_t buf_size;
2197 	int ret;
2198 	bool val;
2199 
2200 	buf_size = min(count, (sizeof(buf) - 1));
2201 	if (copy_from_user(buf, ubuf, buf_size))
2202 		return -EFAULT;
2203 
2204 	buf[buf_size] = '\0';
2205 
2206 	if (strtobool(buf, &val) != 0)
2207 		return -EINVAL;
2208 
2209 	mutex_lock(&ar->conf_mutex);
2210 
2211 	if (ar->state != ATH10K_STATE_ON &&
2212 	    ar->state != ATH10K_STATE_RESTARTED) {
2213 		ret = -ENETDOWN;
2214 		goto exit;
2215 	}
2216 
2217 	if (!(test_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags) ^ val)) {
2218 		ret = count;
2219 		goto exit;
2220 	}
2221 
2222 	if (val)
2223 		set_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags);
2224 	else
2225 		clear_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags);
2226 
2227 	ath10k_info(ar, "restarting firmware due to Peer stats change");
2228 
2229 	queue_work(ar->workqueue, &ar->restart_work);
2230 	ret = count;
2231 
2232 exit:
2233 	mutex_unlock(&ar->conf_mutex);
2234 	return ret;
2235 }
2236 
2237 static ssize_t ath10k_read_peer_stats(struct file *file, char __user *ubuf,
2238 				      size_t count, loff_t *ppos)
2239 
2240 {
2241 	char buf[32];
2242 	struct ath10k *ar = file->private_data;
2243 	int len = 0;
2244 
2245 	mutex_lock(&ar->conf_mutex);
2246 	len = scnprintf(buf, sizeof(buf) - len, "%d\n",
2247 			test_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags));
2248 	mutex_unlock(&ar->conf_mutex);
2249 
2250 	return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2251 }
2252 
2253 static const struct file_operations fops_peer_stats = {
2254 	.read = ath10k_read_peer_stats,
2255 	.write = ath10k_write_peer_stats,
2256 	.open = simple_open
2257 };
2258 
2259 static ssize_t ath10k_debug_fw_checksums_read(struct file *file,
2260 					      char __user *user_buf,
2261 					      size_t count, loff_t *ppos)
2262 {
2263 	struct ath10k *ar = file->private_data;
2264 	unsigned int len = 0, buf_len = 4096;
2265 	ssize_t ret_cnt;
2266 	char *buf;
2267 
2268 	buf = kzalloc(buf_len, GFP_KERNEL);
2269 	if (!buf)
2270 		return -ENOMEM;
2271 
2272 	mutex_lock(&ar->conf_mutex);
2273 
2274 	len += scnprintf(buf + len, buf_len - len,
2275 			 "firmware-N.bin\t\t%08x\n",
2276 			 crc32_le(0, ar->normal_mode_fw.fw_file.firmware->data,
2277 				  ar->normal_mode_fw.fw_file.firmware->size));
2278 	len += scnprintf(buf + len, buf_len - len,
2279 			 "athwlan\t\t\t%08x\n",
2280 			 crc32_le(0, ar->normal_mode_fw.fw_file.firmware_data,
2281 				  ar->normal_mode_fw.fw_file.firmware_len));
2282 	len += scnprintf(buf + len, buf_len - len,
2283 			 "otp\t\t\t%08x\n",
2284 			 crc32_le(0, ar->normal_mode_fw.fw_file.otp_data,
2285 				  ar->normal_mode_fw.fw_file.otp_len));
2286 	len += scnprintf(buf + len, buf_len - len,
2287 			 "codeswap\t\t%08x\n",
2288 			 crc32_le(0, ar->normal_mode_fw.fw_file.codeswap_data,
2289 				  ar->normal_mode_fw.fw_file.codeswap_len));
2290 	len += scnprintf(buf + len, buf_len - len,
2291 			 "board-N.bin\t\t%08x\n",
2292 			 crc32_le(0, ar->normal_mode_fw.board->data,
2293 				  ar->normal_mode_fw.board->size));
2294 	len += scnprintf(buf + len, buf_len - len,
2295 			 "board\t\t\t%08x\n",
2296 			 crc32_le(0, ar->normal_mode_fw.board_data,
2297 				  ar->normal_mode_fw.board_len));
2298 
2299 	ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
2300 
2301 	mutex_unlock(&ar->conf_mutex);
2302 
2303 	kfree(buf);
2304 	return ret_cnt;
2305 }
2306 
2307 static const struct file_operations fops_fw_checksums = {
2308 	.read = ath10k_debug_fw_checksums_read,
2309 	.open = simple_open,
2310 	.owner = THIS_MODULE,
2311 	.llseek = default_llseek,
2312 };
2313 
2314 int ath10k_debug_create(struct ath10k *ar)
2315 {
2316 	ar->debug.fw_crash_data = vzalloc(sizeof(*ar->debug.fw_crash_data));
2317 	if (!ar->debug.fw_crash_data)
2318 		return -ENOMEM;
2319 
2320 	INIT_LIST_HEAD(&ar->debug.fw_stats.pdevs);
2321 	INIT_LIST_HEAD(&ar->debug.fw_stats.vdevs);
2322 	INIT_LIST_HEAD(&ar->debug.fw_stats.peers);
2323 
2324 	return 0;
2325 }
2326 
2327 void ath10k_debug_destroy(struct ath10k *ar)
2328 {
2329 	vfree(ar->debug.fw_crash_data);
2330 	ar->debug.fw_crash_data = NULL;
2331 
2332 	ath10k_debug_fw_stats_reset(ar);
2333 
2334 	kfree(ar->debug.tpc_stats);
2335 }
2336 
2337 int ath10k_debug_register(struct ath10k *ar)
2338 {
2339 	ar->debug.debugfs_phy = debugfs_create_dir("ath10k",
2340 						   ar->hw->wiphy->debugfsdir);
2341 	if (IS_ERR_OR_NULL(ar->debug.debugfs_phy)) {
2342 		if (IS_ERR(ar->debug.debugfs_phy))
2343 			return PTR_ERR(ar->debug.debugfs_phy);
2344 
2345 		return -ENOMEM;
2346 	}
2347 
2348 	INIT_DELAYED_WORK(&ar->debug.htt_stats_dwork,
2349 			  ath10k_debug_htt_stats_dwork);
2350 
2351 	init_completion(&ar->debug.tpc_complete);
2352 	init_completion(&ar->debug.fw_stats_complete);
2353 
2354 	debugfs_create_file("fw_stats", S_IRUSR, ar->debug.debugfs_phy, ar,
2355 			    &fops_fw_stats);
2356 
2357 	debugfs_create_file("fw_reset_stats", S_IRUSR, ar->debug.debugfs_phy,
2358 			    ar, &fops_fw_reset_stats);
2359 
2360 	debugfs_create_file("wmi_services", S_IRUSR, ar->debug.debugfs_phy, ar,
2361 			    &fops_wmi_services);
2362 
2363 	debugfs_create_file("simulate_fw_crash", S_IRUSR | S_IWUSR,
2364 			    ar->debug.debugfs_phy, ar, &fops_simulate_fw_crash);
2365 
2366 	debugfs_create_file("fw_crash_dump", S_IRUSR, ar->debug.debugfs_phy,
2367 			    ar, &fops_fw_crash_dump);
2368 
2369 	debugfs_create_file("reg_addr", S_IRUSR | S_IWUSR,
2370 			    ar->debug.debugfs_phy, ar, &fops_reg_addr);
2371 
2372 	debugfs_create_file("reg_value", S_IRUSR | S_IWUSR,
2373 			    ar->debug.debugfs_phy, ar, &fops_reg_value);
2374 
2375 	debugfs_create_file("mem_value", S_IRUSR | S_IWUSR,
2376 			    ar->debug.debugfs_phy, ar, &fops_mem_value);
2377 
2378 	debugfs_create_file("chip_id", S_IRUSR, ar->debug.debugfs_phy,
2379 			    ar, &fops_chip_id);
2380 
2381 	debugfs_create_file("htt_stats_mask", S_IRUSR | S_IWUSR,
2382 			    ar->debug.debugfs_phy, ar, &fops_htt_stats_mask);
2383 
2384 	debugfs_create_file("htt_max_amsdu_ampdu", S_IRUSR | S_IWUSR,
2385 			    ar->debug.debugfs_phy, ar,
2386 			    &fops_htt_max_amsdu_ampdu);
2387 
2388 	debugfs_create_file("fw_dbglog", S_IRUSR | S_IWUSR,
2389 			    ar->debug.debugfs_phy, ar, &fops_fw_dbglog);
2390 
2391 	debugfs_create_file("cal_data", S_IRUSR, ar->debug.debugfs_phy,
2392 			    ar, &fops_cal_data);
2393 
2394 	debugfs_create_file("ani_enable", S_IRUSR | S_IWUSR,
2395 			    ar->debug.debugfs_phy, ar, &fops_ani_enable);
2396 
2397 	debugfs_create_file("nf_cal_period", S_IRUSR | S_IWUSR,
2398 			    ar->debug.debugfs_phy, ar, &fops_nf_cal_period);
2399 
2400 	if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
2401 		debugfs_create_file("dfs_simulate_radar", S_IWUSR,
2402 				    ar->debug.debugfs_phy, ar,
2403 				    &fops_simulate_radar);
2404 
2405 		debugfs_create_bool("dfs_block_radar_events", S_IWUSR,
2406 				    ar->debug.debugfs_phy,
2407 				    &ar->dfs_block_radar_events);
2408 
2409 		debugfs_create_file("dfs_stats", S_IRUSR,
2410 				    ar->debug.debugfs_phy, ar,
2411 				    &fops_dfs_stats);
2412 	}
2413 
2414 	debugfs_create_file("pktlog_filter", S_IRUGO | S_IWUSR,
2415 			    ar->debug.debugfs_phy, ar, &fops_pktlog_filter);
2416 
2417 	debugfs_create_file("quiet_period", S_IRUGO | S_IWUSR,
2418 			    ar->debug.debugfs_phy, ar, &fops_quiet_period);
2419 
2420 	debugfs_create_file("tpc_stats", S_IRUSR,
2421 			    ar->debug.debugfs_phy, ar, &fops_tpc_stats);
2422 
2423 	if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map))
2424 		debugfs_create_file("btcoex", S_IRUGO | S_IWUSR,
2425 				    ar->debug.debugfs_phy, ar, &fops_btcoex);
2426 
2427 	if (test_bit(WMI_SERVICE_PEER_STATS, ar->wmi.svc_map))
2428 		debugfs_create_file("peer_stats", S_IRUGO | S_IWUSR,
2429 				    ar->debug.debugfs_phy, ar,
2430 				    &fops_peer_stats);
2431 
2432 	debugfs_create_file("fw_checksums", S_IRUSR,
2433 			    ar->debug.debugfs_phy, ar, &fops_fw_checksums);
2434 
2435 	return 0;
2436 }
2437 
2438 void ath10k_debug_unregister(struct ath10k *ar)
2439 {
2440 	cancel_delayed_work_sync(&ar->debug.htt_stats_dwork);
2441 }
2442 
2443 #endif /* CONFIG_ATH10K_DEBUGFS */
2444 
2445 #ifdef CONFIG_ATH10K_DEBUG
2446 void ath10k_dbg(struct ath10k *ar, enum ath10k_debug_mask mask,
2447 		const char *fmt, ...)
2448 {
2449 	struct va_format vaf;
2450 	va_list args;
2451 
2452 	va_start(args, fmt);
2453 
2454 	vaf.fmt = fmt;
2455 	vaf.va = &args;
2456 
2457 	if (ath10k_debug_mask & mask)
2458 		dev_printk(KERN_DEBUG, ar->dev, "%pV", &vaf);
2459 
2460 	trace_ath10k_log_dbg(ar, mask, &vaf);
2461 
2462 	va_end(args);
2463 }
2464 EXPORT_SYMBOL(ath10k_dbg);
2465 
2466 void ath10k_dbg_dump(struct ath10k *ar,
2467 		     enum ath10k_debug_mask mask,
2468 		     const char *msg, const char *prefix,
2469 		     const void *buf, size_t len)
2470 {
2471 	char linebuf[256];
2472 	unsigned int linebuflen;
2473 	const void *ptr;
2474 
2475 	if (ath10k_debug_mask & mask) {
2476 		if (msg)
2477 			ath10k_dbg(ar, mask, "%s\n", msg);
2478 
2479 		for (ptr = buf; (ptr - buf) < len; ptr += 16) {
2480 			linebuflen = 0;
2481 			linebuflen += scnprintf(linebuf + linebuflen,
2482 						sizeof(linebuf) - linebuflen,
2483 						"%s%08x: ",
2484 						(prefix ? prefix : ""),
2485 						(unsigned int)(ptr - buf));
2486 			hex_dump_to_buffer(ptr, len - (ptr - buf), 16, 1,
2487 					   linebuf + linebuflen,
2488 					   sizeof(linebuf) - linebuflen, true);
2489 			dev_printk(KERN_DEBUG, ar->dev, "%s\n", linebuf);
2490 		}
2491 	}
2492 
2493 	/* tracing code doesn't like null strings :/ */
2494 	trace_ath10k_log_dbg_dump(ar, msg ? msg : "", prefix ? prefix : "",
2495 				  buf, len);
2496 }
2497 EXPORT_SYMBOL(ath10k_dbg_dump);
2498 
2499 #endif /* CONFIG_ATH10K_DEBUG */
2500