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