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