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