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