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