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