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