blob: b106a06d21cbfe9782498e7791daad2742c90a48 [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
Yu Wang0e9b8df2018-01-30 14:06:08 +02004 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
Kalle Valo5e3dd152013-06-12 20:52:10 +03005 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <linux/module.h>
20#include <linux/debugfs.h>
Ben Greear384914b2014-08-25 08:37:32 +030021#include <linux/vmalloc.h>
Johannes Berg12c27152014-11-21 18:58:49 +020022#include <linux/utsname.h>
Kalle Valo3e580442015-11-25 15:38:34 +020023#include <linux/crc32.h>
24#include <linux/firmware.h>
Kalle Valo5e3dd152013-06-12 20:52:10 +030025
26#include "core.h"
27#include "debug.h"
Kalle Valo7869b4f2014-09-24 14:16:58 +030028#include "hif.h"
Michal Kaziord7579d12014-12-03 10:10:54 +020029#include "wmi-ops.h"
Kalle Valo5e3dd152013-06-12 20:52:10 +030030
Kalle Valoa3d135e2013-09-03 11:44:10 +030031/* ms */
32#define ATH10K_DEBUG_HTT_STATS_INTERVAL 1000
33
Marty Faltesekf67b107d2016-10-10 19:00:04 +030034#define ATH10K_DEBUG_CAL_DATA_LEN 12064
35
Ben Greear384914b2014-08-25 08:37:32 +030036#define ATH10K_FW_CRASH_DUMP_VERSION 1
37
38/**
39 * enum ath10k_fw_crash_dump_type - types of data in the dump file
40 * @ATH10K_FW_CRASH_DUMP_REGDUMP: Register crash dump in binary format
41 */
42enum ath10k_fw_crash_dump_type {
43 ATH10K_FW_CRASH_DUMP_REGISTERS = 0,
44
45 ATH10K_FW_CRASH_DUMP_MAX,
46};
47
48struct ath10k_tlv_dump_data {
49 /* see ath10k_fw_crash_dump_type above */
50 __le32 type;
51
52 /* in bytes */
53 __le32 tlv_len;
54
55 /* pad to 32-bit boundaries as needed */
56 u8 tlv_data[];
57} __packed;
58
59struct ath10k_dump_file_data {
60 /* dump file information */
61
62 /* "ATH10K-FW-DUMP" */
63 char df_magic[16];
64
65 __le32 len;
66
67 /* file dump version */
68 __le32 version;
69
70 /* some info we can get from ath10k struct that might help */
71
72 u8 uuid[16];
73
74 __le32 chip_id;
75
76 /* 0 for now, in place for later hardware */
77 __le32 bus_type;
78
79 __le32 target_version;
80 __le32 fw_version_major;
81 __le32 fw_version_minor;
82 __le32 fw_version_release;
83 __le32 fw_version_build;
84 __le32 phy_capability;
85 __le32 hw_min_tx_power;
86 __le32 hw_max_tx_power;
87 __le32 ht_cap_info;
88 __le32 vht_cap_info;
89 __le32 num_rf_chains;
90
91 /* firmware version string */
92 char fw_ver[ETHTOOL_FWVERS_LEN];
93
94 /* Kernel related information */
95
96 /* time-of-day stamp */
97 __le64 tv_sec;
98
99 /* time-of-day stamp, nano-seconds */
100 __le64 tv_nsec;
101
102 /* LINUX_VERSION_CODE */
103 __le32 kernel_ver_code;
104
105 /* VERMAGIC_STRING */
106 char kernel_ver[64];
107
108 /* room for growth w/out changing binary format */
109 u8 unused[128];
110
111 /* struct ath10k_tlv_dump_data + more */
112 u8 data[0];
113} __packed;
114
Joe Perchesbabcb3e2014-09-22 10:35:34 -0700115void ath10k_info(struct ath10k *ar, const char *fmt, ...)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300116{
117 struct va_format vaf = {
118 .fmt = fmt,
119 };
120 va_list args;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300121
122 va_start(args, fmt);
123 vaf.va = &args;
Joe Perchesbabcb3e2014-09-22 10:35:34 -0700124 dev_info(ar->dev, "%pV", &vaf);
Michal Kaziord35a6c12014-09-02 11:00:21 +0300125 trace_ath10k_log_info(ar, &vaf);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300126 va_end(args);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300127}
128EXPORT_SYMBOL(ath10k_info);
129
Kalle Valo23f591e2015-11-25 15:38:27 +0200130void ath10k_debug_print_hwfw_info(struct ath10k *ar)
Kalle Valo8a0c7972014-08-25 08:37:45 +0300131{
Kalle Valo7ebf7212016-04-20 19:44:51 +0300132 const struct firmware *firmware;
Michal Kazior84e3df62015-08-05 06:55:37 +0200133 char fw_features[128] = {};
Michal Kazior8866c722016-03-17 10:52:08 +0100134 u32 crc = 0;
Michal Kaziorb27bc5a2015-06-15 14:46:40 +0300135
136 ath10k_core_get_fw_features_str(ar, fw_features, sizeof(fw_features));
137
Kalle Valo8605c022015-11-25 15:38:19 +0200138 ath10k_info(ar, "%s target 0x%08x chip_id 0x%08x sub %04x:%04x",
Kalle Valo8a0c7972014-08-25 08:37:45 +0300139 ar->hw_params.name,
140 ar->target_version,
141 ar->chip_id,
Kalle Valo8605c022015-11-25 15:38:19 +0200142 ar->id.subsystem_vendor, ar->id.subsystem_device);
Kalle Valof0de90b2015-11-25 15:38:12 +0200143
Kalle Valof0de90b2015-11-25 15:38:12 +0200144 ath10k_info(ar, "kconfig debug %d debugfs %d tracing %d dfs %d testmode %d\n",
Masahiro Yamada97f26452016-08-03 13:45:50 -0700145 IS_ENABLED(CONFIG_ATH10K_DEBUG),
146 IS_ENABLED(CONFIG_ATH10K_DEBUGFS),
147 IS_ENABLED(CONFIG_ATH10K_TRACING),
148 IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED),
149 IS_ENABLED(CONFIG_NL80211_TESTMODE));
Kalle Valof0de90b2015-11-25 15:38:12 +0200150
Kalle Valo7ebf7212016-04-20 19:44:51 +0300151 firmware = ar->normal_mode_fw.fw_file.firmware;
152 if (firmware)
153 crc = crc32_le(0, firmware->data, firmware->size);
Michal Kazior8866c722016-03-17 10:52:08 +0100154
Kalle Valo3e580442015-11-25 15:38:34 +0200155 ath10k_info(ar, "firmware ver %s api %d features %s crc32 %08x\n",
Kalle Valo23f591e2015-11-25 15:38:27 +0200156 ar->hw->wiphy->fw_version,
157 ar->fw_api,
Kalle Valo3e580442015-11-25 15:38:34 +0200158 fw_features,
Michal Kazior8866c722016-03-17 10:52:08 +0100159 crc);
Kalle Valo23f591e2015-11-25 15:38:27 +0200160}
161
162void ath10k_debug_print_board_info(struct ath10k *ar)
163{
164 char boardinfo[100];
Yu Wang0e9b8df2018-01-30 14:06:08 +0200165 const struct firmware *board;
166 u32 crc;
Kalle Valo23f591e2015-11-25 15:38:27 +0200167
168 if (ar->id.bmi_ids_valid)
169 scnprintf(boardinfo, sizeof(boardinfo), "%d:%d",
170 ar->id.bmi_chip_id, ar->id.bmi_board_id);
171 else
172 scnprintf(boardinfo, sizeof(boardinfo), "N/A");
173
Yu Wang0e9b8df2018-01-30 14:06:08 +0200174 board = ar->normal_mode_fw.board;
175 if (!IS_ERR_OR_NULL(board))
176 crc = crc32_le(0, board->data, board->size);
177 else
178 crc = 0;
179
Kalle Valo3e580442015-11-25 15:38:34 +0200180 ath10k_info(ar, "board_file api %d bmi_id %s crc32 %08x",
Kalle Valo23f591e2015-11-25 15:38:27 +0200181 ar->bd_api,
Kalle Valo3e580442015-11-25 15:38:34 +0200182 boardinfo,
Yu Wang0e9b8df2018-01-30 14:06:08 +0200183 crc);
Kalle Valo23f591e2015-11-25 15:38:27 +0200184}
185
186void ath10k_debug_print_boot_info(struct ath10k *ar)
187{
Kalle Valof0de90b2015-11-25 15:38:12 +0200188 ath10k_info(ar, "htt-ver %d.%d wmi-op %d htt-op %d cal %s max-sta %d raw %d hwcrypto %d\n",
Kalle Valo8a0c7972014-08-25 08:37:45 +0300189 ar->htt.target_version_major,
Michal Kazior34b28b62014-09-23 10:22:52 +0200190 ar->htt.target_version_minor,
Kalle Valobf3c13a2016-04-20 19:45:33 +0300191 ar->normal_mode_fw.fw_file.wmi_op_version,
Kalle Valo77561f92016-04-20 19:45:47 +0300192 ar->normal_mode_fw.fw_file.htt_op_version,
Michal Kaziorcfd10612014-11-25 15:16:05 +0100193 ath10k_cal_mode_str(ar->cal_mode),
Michal Kaziorb27bc5a2015-06-15 14:46:40 +0300194 ar->max_num_stations,
David Liuccec9032015-07-24 20:25:32 +0300195 test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags),
Kalle Valof0de90b2015-11-25 15:38:12 +0200196 !test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags));
Kalle Valo8a0c7972014-08-25 08:37:45 +0300197}
Kalle Valo23f591e2015-11-25 15:38:27 +0200198
199void ath10k_print_driver_info(struct ath10k *ar)
200{
201 ath10k_debug_print_hwfw_info(ar);
202 ath10k_debug_print_board_info(ar);
203 ath10k_debug_print_boot_info(ar);
204}
Kalle Valo8a0c7972014-08-25 08:37:45 +0300205EXPORT_SYMBOL(ath10k_print_driver_info);
206
Joe Perchesbabcb3e2014-09-22 10:35:34 -0700207void ath10k_err(struct ath10k *ar, const char *fmt, ...)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300208{
209 struct va_format vaf = {
210 .fmt = fmt,
211 };
212 va_list args;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300213
214 va_start(args, fmt);
215 vaf.va = &args;
Joe Perchesbabcb3e2014-09-22 10:35:34 -0700216 dev_err(ar->dev, "%pV", &vaf);
Michal Kaziord35a6c12014-09-02 11:00:21 +0300217 trace_ath10k_log_err(ar, &vaf);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300218 va_end(args);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300219}
220EXPORT_SYMBOL(ath10k_err);
221
Joe Perchesbabcb3e2014-09-22 10:35:34 -0700222void ath10k_warn(struct ath10k *ar, const char *fmt, ...)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300223{
224 struct va_format vaf = {
225 .fmt = fmt,
226 };
227 va_list args;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300228
229 va_start(args, fmt);
230 vaf.va = &args;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200231 dev_warn_ratelimited(ar->dev, "%pV", &vaf);
Michal Kaziord35a6c12014-09-02 11:00:21 +0300232 trace_ath10k_log_warn(ar, &vaf);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300233
234 va_end(args);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300235}
236EXPORT_SYMBOL(ath10k_warn);
237
238#ifdef CONFIG_ATH10K_DEBUGFS
239
Kalle Valo5e3dd152013-06-12 20:52:10 +0300240static ssize_t ath10k_read_wmi_services(struct file *file,
241 char __user *user_buf,
242 size_t count, loff_t *ppos)
243{
244 struct ath10k *ar = file->private_data;
245 char *buf;
Michal Kaziorcff990c2014-08-04 09:18:33 +0300246 unsigned int len = 0, buf_len = 4096;
247 const char *name;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300248 ssize_t ret_cnt;
Michal Kaziorcff990c2014-08-04 09:18:33 +0300249 bool enabled;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300250 int i;
251
252 buf = kzalloc(buf_len, GFP_KERNEL);
253 if (!buf)
254 return -ENOMEM;
255
256 mutex_lock(&ar->conf_mutex);
257
258 if (len > buf_len)
259 len = buf_len;
260
Michal Kazioracfe7ec2014-11-27 10:11:17 +0100261 spin_lock_bh(&ar->data_lock);
Michal Kaziorc4f8c832014-09-04 10:18:32 +0200262 for (i = 0; i < WMI_SERVICE_MAX; i++) {
Michal Kazioracfe7ec2014-11-27 10:11:17 +0100263 enabled = test_bit(i, ar->wmi.svc_map);
Michal Kaziorcff990c2014-08-04 09:18:33 +0300264 name = wmi_service_name(i);
265
266 if (!name) {
267 if (enabled)
268 len += scnprintf(buf + len, buf_len - len,
269 "%-40s %s (bit %d)\n",
270 "unknown", "enabled", i);
271
272 continue;
273 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300274
275 len += scnprintf(buf + len, buf_len - len,
Michal Kaziorcff990c2014-08-04 09:18:33 +0300276 "%-40s %s\n",
277 name, enabled ? "enabled" : "-");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300278 }
Michal Kazioracfe7ec2014-11-27 10:11:17 +0100279 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300280
281 ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
282
283 mutex_unlock(&ar->conf_mutex);
284
285 kfree(buf);
286 return ret_cnt;
287}
288
289static const struct file_operations fops_wmi_services = {
290 .read = ath10k_read_wmi_services,
291 .open = simple_open,
292 .owner = THIS_MODULE,
293 .llseek = default_llseek,
294};
295
Mohammed Shafi Shajakhanb4619ea2016-01-13 21:16:31 +0530296static void ath10k_fw_stats_pdevs_free(struct list_head *head)
Michal Kazior53268492014-09-25 12:33:50 +0200297{
298 struct ath10k_fw_stats_pdev *i, *tmp;
299
300 list_for_each_entry_safe(i, tmp, head, list) {
301 list_del(&i->list);
302 kfree(i);
303 }
304}
305
Mohammed Shafi Shajakhanb4619ea2016-01-13 21:16:31 +0530306static void ath10k_fw_stats_vdevs_free(struct list_head *head)
Michal Kazior7b6b1532015-02-15 16:50:40 +0200307{
308 struct ath10k_fw_stats_vdev *i, *tmp;
309
310 list_for_each_entry_safe(i, tmp, head, list) {
311 list_del(&i->list);
312 kfree(i);
313 }
314}
315
Mohammed Shafi Shajakhanb4619ea2016-01-13 21:16:31 +0530316static void ath10k_fw_stats_peers_free(struct list_head *head)
Michal Kazior53268492014-09-25 12:33:50 +0200317{
318 struct ath10k_fw_stats_peer *i, *tmp;
319
320 list_for_each_entry_safe(i, tmp, head, list) {
321 list_del(&i->list);
322 kfree(i);
323 }
324}
325
Mohammed Shafi Shajakhan4a49ae92016-06-30 15:23:47 +0300326static void ath10k_fw_extd_stats_peers_free(struct list_head *head)
327{
328 struct ath10k_fw_extd_stats_peer *i, *tmp;
329
330 list_for_each_entry_safe(i, tmp, head, list) {
331 list_del(&i->list);
332 kfree(i);
333 }
334}
335
Michal Kazior53268492014-09-25 12:33:50 +0200336static void ath10k_debug_fw_stats_reset(struct ath10k *ar)
337{
338 spin_lock_bh(&ar->data_lock);
339 ar->debug.fw_stats_done = false;
Mohammed Shafi Shajakhan4a49ae92016-06-30 15:23:47 +0300340 ar->debug.fw_stats.extended = false;
Mohammed Shafi Shajakhanb4619ea2016-01-13 21:16:31 +0530341 ath10k_fw_stats_pdevs_free(&ar->debug.fw_stats.pdevs);
342 ath10k_fw_stats_vdevs_free(&ar->debug.fw_stats.vdevs);
343 ath10k_fw_stats_peers_free(&ar->debug.fw_stats.peers);
Mohammed Shafi Shajakhan4a49ae92016-06-30 15:23:47 +0300344 ath10k_fw_extd_stats_peers_free(&ar->debug.fw_stats.peers_extd);
Michal Kazior53268492014-09-25 12:33:50 +0200345 spin_unlock_bh(&ar->data_lock);
346}
347
Michal Kazior60ef4012014-09-25 12:33:48 +0200348void ath10k_debug_fw_stats_process(struct ath10k *ar, struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300349{
Michal Kazior53268492014-09-25 12:33:50 +0200350 struct ath10k_fw_stats stats = {};
Mohammed Shafi Shajakhancc61a1b2016-03-16 18:13:32 +0530351 bool is_start, is_started, is_end;
Michal Kazior53268492014-09-25 12:33:50 +0200352 size_t num_peers;
Michal Kazior7b6b1532015-02-15 16:50:40 +0200353 size_t num_vdevs;
Michal Kaziord15fb522014-09-25 12:33:47 +0200354 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300355
Michal Kazior53268492014-09-25 12:33:50 +0200356 INIT_LIST_HEAD(&stats.pdevs);
Michal Kazior7b6b1532015-02-15 16:50:40 +0200357 INIT_LIST_HEAD(&stats.vdevs);
Michal Kazior53268492014-09-25 12:33:50 +0200358 INIT_LIST_HEAD(&stats.peers);
Mohammed Shafi Shajakhan4a49ae92016-06-30 15:23:47 +0300359 INIT_LIST_HEAD(&stats.peers_extd);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300360
Michal Kazior53268492014-09-25 12:33:50 +0200361 spin_lock_bh(&ar->data_lock);
362 ret = ath10k_wmi_pull_fw_stats(ar, skb, &stats);
Michal Kaziord15fb522014-09-25 12:33:47 +0200363 if (ret) {
364 ath10k_warn(ar, "failed to pull fw stats: %d\n", ret);
Raja Mani5db879a2015-07-09 14:19:43 +0530365 goto free;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300366 }
367
Michal Kazior53268492014-09-25 12:33:50 +0200368 /* Stat data may exceed htc-wmi buffer limit. In such case firmware
369 * splits the stats data and delivers it in a ping-pong fashion of
370 * request cmd-update event.
371 *
372 * However there is no explicit end-of-data. Instead start-of-data is
373 * used as an implicit one. This works as follows:
374 * a) discard stat update events until one with pdev stats is
375 * delivered - this skips session started at end of (b)
376 * b) consume stat update events until another one with pdev stats is
377 * delivered which is treated as end-of-data and is itself discarded
378 */
Mohammed Shafi Shajakhancc61a1b2016-03-16 18:13:32 +0530379 if (ath10k_peer_stats_enabled(ar))
Mohammed Shafi Shajakhan4a49ae92016-06-30 15:23:47 +0300380 ath10k_sta_update_rx_duration(ar, &stats);
Mohammed Shafi Shajakhan74135f52016-02-03 21:07:42 +0530381
Mohammed Shafi Shajakhane0b6ce02016-02-03 21:07:43 +0530382 if (ar->debug.fw_stats_done) {
Mohammed Shafi Shajakhancc61a1b2016-03-16 18:13:32 +0530383 if (!ath10k_peer_stats_enabled(ar))
Mohammed Shafi Shajakhane0b6ce02016-02-03 21:07:43 +0530384 ath10k_warn(ar, "received unsolicited stats update event\n");
385
Michal Kazior53268492014-09-25 12:33:50 +0200386 goto free;
387 }
388
Manikanta Pubbisettybc6f9ae2015-10-16 15:54:52 +0300389 num_peers = ath10k_wmi_fw_stats_num_peers(&ar->debug.fw_stats.peers);
390 num_vdevs = ath10k_wmi_fw_stats_num_vdevs(&ar->debug.fw_stats.vdevs);
Michal Kazior53268492014-09-25 12:33:50 +0200391 is_start = (list_empty(&ar->debug.fw_stats.pdevs) &&
392 !list_empty(&stats.pdevs));
393 is_end = (!list_empty(&ar->debug.fw_stats.pdevs) &&
394 !list_empty(&stats.pdevs));
395
396 if (is_start)
397 list_splice_tail_init(&stats.pdevs, &ar->debug.fw_stats.pdevs);
398
399 if (is_end)
400 ar->debug.fw_stats_done = true;
401
402 is_started = !list_empty(&ar->debug.fw_stats.pdevs);
403
404 if (is_started && !is_end) {
405 if (num_peers >= ATH10K_MAX_NUM_PEER_IDS) {
406 /* Although this is unlikely impose a sane limit to
407 * prevent firmware from DoS-ing the host.
408 */
Mohammed Shafi Shajakhand57e7f22016-01-13 21:16:32 +0530409 ath10k_fw_stats_peers_free(&ar->debug.fw_stats.peers);
Michal Kazior53268492014-09-25 12:33:50 +0200410 ath10k_warn(ar, "dropping fw peer stats\n");
411 goto free;
412 }
413
Michal Kazior7b6b1532015-02-15 16:50:40 +0200414 if (num_vdevs >= BITS_PER_LONG) {
Mohammed Shafi Shajakhand57e7f22016-01-13 21:16:32 +0530415 ath10k_fw_stats_vdevs_free(&ar->debug.fw_stats.vdevs);
Michal Kazior7b6b1532015-02-15 16:50:40 +0200416 ath10k_warn(ar, "dropping fw vdev stats\n");
417 goto free;
418 }
419
Michal Kazior53268492014-09-25 12:33:50 +0200420 list_splice_tail_init(&stats.peers, &ar->debug.fw_stats.peers);
Michal Kazior7b6b1532015-02-15 16:50:40 +0200421 list_splice_tail_init(&stats.vdevs, &ar->debug.fw_stats.vdevs);
Mohammed Shafi Shajakhan4a49ae92016-06-30 15:23:47 +0300422 list_splice_tail_init(&stats.peers_extd,
423 &ar->debug.fw_stats.peers_extd);
Michal Kazior53268492014-09-25 12:33:50 +0200424 }
425
Michal Kazior60ef4012014-09-25 12:33:48 +0200426 complete(&ar->debug.fw_stats_complete);
Michal Kaziord15fb522014-09-25 12:33:47 +0200427
Michal Kazior53268492014-09-25 12:33:50 +0200428free:
429 /* In some cases lists have been spliced and cleared. Free up
430 * resources if that is not the case.
431 */
Mohammed Shafi Shajakhanb4619ea2016-01-13 21:16:31 +0530432 ath10k_fw_stats_pdevs_free(&stats.pdevs);
433 ath10k_fw_stats_vdevs_free(&stats.vdevs);
434 ath10k_fw_stats_peers_free(&stats.peers);
Mohammed Shafi Shajakhan4a49ae92016-06-30 15:23:47 +0300435 ath10k_fw_extd_stats_peers_free(&stats.peers_extd);
Michal Kazior53268492014-09-25 12:33:50 +0200436
Michal Kaziord15fb522014-09-25 12:33:47 +0200437 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300438}
439
Michal Kazior53268492014-09-25 12:33:50 +0200440static int ath10k_debug_fw_stats_request(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300441{
Nicholas Mc Guire6e8d5432015-03-30 15:39:20 +0300442 unsigned long timeout, time_left;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300443 int ret;
444
Michal Kaziorfb2e9c02014-09-25 12:33:49 +0200445 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300446
Nicholas Mc Guire6e8d5432015-03-30 15:39:20 +0300447 timeout = jiffies + msecs_to_jiffies(1 * HZ);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300448
Michal Kazior53268492014-09-25 12:33:50 +0200449 ath10k_debug_fw_stats_reset(ar);
450
451 for (;;) {
452 if (time_after(jiffies, timeout))
453 return -ETIMEDOUT;
454
455 reinit_completion(&ar->debug.fw_stats_complete);
456
Yanbo Li6274cd42015-04-01 22:53:21 +0300457 ret = ath10k_wmi_request_stats(ar, ar->fw_stats_req_mask);
Michal Kazior53268492014-09-25 12:33:50 +0200458 if (ret) {
459 ath10k_warn(ar, "could not request stats (%d)\n", ret);
460 return ret;
461 }
462
Nicholas Mc Guire6e8d5432015-03-30 15:39:20 +0300463 time_left =
464 wait_for_completion_timeout(&ar->debug.fw_stats_complete,
465 1 * HZ);
466 if (!time_left)
Michal Kazior53268492014-09-25 12:33:50 +0200467 return -ETIMEDOUT;
468
469 spin_lock_bh(&ar->data_lock);
470 if (ar->debug.fw_stats_done) {
471 spin_unlock_bh(&ar->data_lock);
472 break;
473 }
474 spin_unlock_bh(&ar->data_lock);
475 }
Michal Kaziorfb2e9c02014-09-25 12:33:49 +0200476
477 return 0;
478}
479
Michal Kaziorfb2e9c02014-09-25 12:33:49 +0200480static int ath10k_fw_stats_open(struct inode *inode, struct file *file)
481{
482 struct ath10k *ar = inode->i_private;
483 void *buf = NULL;
484 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300485
Michal Kaziorfb2e9c02014-09-25 12:33:49 +0200486 mutex_lock(&ar->conf_mutex);
487
488 if (ar->state != ATH10K_STATE_ON) {
489 ret = -ENETDOWN;
490 goto err_unlock;
491 }
492
493 buf = vmalloc(ATH10K_FW_STATS_BUF_SIZE);
494 if (!buf) {
495 ret = -ENOMEM;
496 goto err_unlock;
497 }
498
Michal Kazior53268492014-09-25 12:33:50 +0200499 ret = ath10k_debug_fw_stats_request(ar);
Michal Kaziorfb2e9c02014-09-25 12:33:49 +0200500 if (ret) {
501 ath10k_warn(ar, "failed to request fw stats: %d\n", ret);
502 goto err_free;
503 }
504
Manikanta Pubbisettybc6f9ae2015-10-16 15:54:52 +0300505 ret = ath10k_wmi_fw_stats_fill(ar, &ar->debug.fw_stats, buf);
506 if (ret) {
507 ath10k_warn(ar, "failed to fill fw stats: %d\n", ret);
508 goto err_free;
509 }
510
Michal Kaziorfb2e9c02014-09-25 12:33:49 +0200511 file->private_data = buf;
512
Kalle Valo5e3dd152013-06-12 20:52:10 +0300513 mutex_unlock(&ar->conf_mutex);
Michal Kaziorfb2e9c02014-09-25 12:33:49 +0200514 return 0;
515
516err_free:
517 vfree(buf);
518
519err_unlock:
520 mutex_unlock(&ar->conf_mutex);
521 return ret;
522}
523
524static int ath10k_fw_stats_release(struct inode *inode, struct file *file)
525{
526 vfree(file->private_data);
527
528 return 0;
529}
530
531static ssize_t ath10k_fw_stats_read(struct file *file, char __user *user_buf,
532 size_t count, loff_t *ppos)
533{
534 const char *buf = file->private_data;
535 unsigned int len = strlen(buf);
536
537 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300538}
539
540static const struct file_operations fops_fw_stats = {
Michal Kaziorfb2e9c02014-09-25 12:33:49 +0200541 .open = ath10k_fw_stats_open,
542 .release = ath10k_fw_stats_release,
Michal Kazior60ef4012014-09-25 12:33:48 +0200543 .read = ath10k_fw_stats_read,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300544 .owner = THIS_MODULE,
545 .llseek = default_llseek,
546};
547
Ben Greearf51dbe72014-09-29 14:41:46 +0300548static ssize_t ath10k_debug_fw_reset_stats_read(struct file *file,
549 char __user *user_buf,
550 size_t count, loff_t *ppos)
551{
552 struct ath10k *ar = file->private_data;
553 int ret, len, buf_len;
554 char *buf;
555
556 buf_len = 500;
557 buf = kmalloc(buf_len, GFP_KERNEL);
558 if (!buf)
559 return -ENOMEM;
560
561 spin_lock_bh(&ar->data_lock);
562
563 len = 0;
564 len += scnprintf(buf + len, buf_len - len,
565 "fw_crash_counter\t\t%d\n", ar->stats.fw_crash_counter);
566 len += scnprintf(buf + len, buf_len - len,
567 "fw_warm_reset_counter\t\t%d\n",
568 ar->stats.fw_warm_reset_counter);
569 len += scnprintf(buf + len, buf_len - len,
570 "fw_cold_reset_counter\t\t%d\n",
571 ar->stats.fw_cold_reset_counter);
572
573 spin_unlock_bh(&ar->data_lock);
574
575 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
576
577 kfree(buf);
578
579 return ret;
580}
581
582static const struct file_operations fops_fw_reset_stats = {
583 .open = simple_open,
584 .read = ath10k_debug_fw_reset_stats_read,
585 .owner = THIS_MODULE,
586 .llseek = default_llseek,
587};
588
Ben Greeard5aebc72014-09-10 18:59:28 +0300589/* This is a clean assert crash in firmware. */
590static int ath10k_debug_fw_assert(struct ath10k *ar)
591{
592 struct wmi_vdev_install_key_cmd *cmd;
593 struct sk_buff *skb;
594
595 skb = ath10k_wmi_alloc_skb(ar, sizeof(*cmd) + 16);
596 if (!skb)
597 return -ENOMEM;
598
599 cmd = (struct wmi_vdev_install_key_cmd *)skb->data;
600 memset(cmd, 0, sizeof(*cmd));
601
602 /* big enough number so that firmware asserts */
603 cmd->vdev_id = __cpu_to_le32(0x7ffe);
604
605 return ath10k_wmi_cmd_send(ar, skb,
606 ar->wmi.cmd->vdev_install_key_cmdid);
607}
608
Michal Kazior278c4a82013-07-22 14:08:51 +0200609static ssize_t ath10k_read_simulate_fw_crash(struct file *file,
610 char __user *user_buf,
611 size_t count, loff_t *ppos)
612{
Kalle Valo75cb96d2014-09-14 12:50:44 +0300613 const char buf[] =
614 "To simulate firmware crash write one of the keywords to this file:\n"
615 "`soft` - this will send WMI_FORCE_FW_HANG_ASSERT to firmware if FW supports that command.\n"
616 "`hard` - this will send to firmware command with illegal parameters causing firmware crash.\n"
Michal Kazior605cdba2014-10-28 10:34:37 +0100617 "`assert` - this will send special illegal parameter to firmware to cause assert failure and crash.\n"
618 "`hw-restart` - this will simply queue hw restart without fw/hw actually crashing.\n";
Marek Puzyniak8c656992014-03-21 17:46:56 +0200619
Michal Kazior278c4a82013-07-22 14:08:51 +0200620 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
621}
622
Marek Puzyniak8c656992014-03-21 17:46:56 +0200623/* Simulate firmware crash:
624 * 'soft': Call wmi command causing firmware hang. This firmware hang is
625 * recoverable by warm firmware reset.
626 * 'hard': Force firmware crash by setting any vdev parameter for not allowed
627 * vdev id. This is hard firmware crash because it is recoverable only by cold
628 * firmware reset.
629 */
Michal Kazior278c4a82013-07-22 14:08:51 +0200630static ssize_t ath10k_write_simulate_fw_crash(struct file *file,
631 const char __user *user_buf,
632 size_t count, loff_t *ppos)
633{
634 struct ath10k *ar = file->private_data;
Michael Mera975760b2017-04-24 16:11:57 +0900635 char buf[32] = {0};
636 ssize_t rc;
Michal Kazior278c4a82013-07-22 14:08:51 +0200637 int ret;
638
Michael Mera975760b2017-04-24 16:11:57 +0900639 /* filter partial writes and invalid commands */
640 if (*ppos != 0 || count >= sizeof(buf) || count == 0)
641 return -EINVAL;
Marek Puzyniak8c656992014-03-21 17:46:56 +0200642
Michael Mera975760b2017-04-24 16:11:57 +0900643 rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
644 if (rc < 0)
645 return rc;
Michal Kazior278c4a82013-07-22 14:08:51 +0200646
Mohammed Shafi Shajakhanf5e30752016-05-28 11:25:41 +0300647 /* drop the possible '\n' from the end */
Michael Mera975760b2017-04-24 16:11:57 +0900648 if (buf[*ppos - 1] == '\n')
649 buf[*ppos - 1] = '\0';
Mohammed Shafi Shajakhanf5e30752016-05-28 11:25:41 +0300650
651 mutex_lock(&ar->conf_mutex);
652
Michal Kazior278c4a82013-07-22 14:08:51 +0200653 if (ar->state != ATH10K_STATE_ON &&
654 ar->state != ATH10K_STATE_RESTARTED) {
655 ret = -ENETDOWN;
656 goto exit;
657 }
658
Marek Puzyniak8c656992014-03-21 17:46:56 +0200659 if (!strcmp(buf, "soft")) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200660 ath10k_info(ar, "simulating soft firmware crash\n");
Marek Puzyniak8c656992014-03-21 17:46:56 +0200661 ret = ath10k_wmi_force_fw_hang(ar, WMI_FORCE_FW_HANG_ASSERT, 0);
662 } else if (!strcmp(buf, "hard")) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200663 ath10k_info(ar, "simulating hard firmware crash\n");
Ben Greear611b3682014-07-25 11:56:40 +0300664 /* 0x7fff is vdev id, and it is always out of range for all
665 * firmware variants in order to force a firmware crash.
666 */
667 ret = ath10k_wmi_vdev_set_param(ar, 0x7fff,
Kalle Valo5b07e072014-09-14 12:50:06 +0300668 ar->wmi.vdev_param->rts_threshold,
669 0);
Ben Greeard5aebc72014-09-10 18:59:28 +0300670 } else if (!strcmp(buf, "assert")) {
671 ath10k_info(ar, "simulating firmware assert crash\n");
672 ret = ath10k_debug_fw_assert(ar);
Michal Kazior605cdba2014-10-28 10:34:37 +0100673 } else if (!strcmp(buf, "hw-restart")) {
674 ath10k_info(ar, "user requested hw restart\n");
675 queue_work(ar->workqueue, &ar->restart_work);
676 ret = 0;
Marek Puzyniak8c656992014-03-21 17:46:56 +0200677 } else {
678 ret = -EINVAL;
679 goto exit;
680 }
Michal Kazior278c4a82013-07-22 14:08:51 +0200681
Marek Puzyniak8c656992014-03-21 17:46:56 +0200682 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200683 ath10k_warn(ar, "failed to simulate firmware crash: %d\n", ret);
Marek Puzyniak8c656992014-03-21 17:46:56 +0200684 goto exit;
685 }
686
687 ret = count;
Michal Kazior278c4a82013-07-22 14:08:51 +0200688
689exit:
690 mutex_unlock(&ar->conf_mutex);
691 return ret;
692}
693
694static const struct file_operations fops_simulate_fw_crash = {
695 .read = ath10k_read_simulate_fw_crash,
696 .write = ath10k_write_simulate_fw_crash,
697 .open = simple_open,
698 .owner = THIS_MODULE,
699 .llseek = default_llseek,
700};
701
Kalle Valo763b8cd2013-09-01 11:22:21 +0300702static ssize_t ath10k_read_chip_id(struct file *file, char __user *user_buf,
703 size_t count, loff_t *ppos)
704{
705 struct ath10k *ar = file->private_data;
706 unsigned int len;
707 char buf[50];
708
709 len = scnprintf(buf, sizeof(buf), "0x%08x\n", ar->chip_id);
710
711 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
712}
713
714static const struct file_operations fops_chip_id = {
715 .read = ath10k_read_chip_id,
716 .open = simple_open,
717 .owner = THIS_MODULE,
718 .llseek = default_llseek,
719};
720
Ben Greear384914b2014-08-25 08:37:32 +0300721struct ath10k_fw_crash_data *
722ath10k_debug_get_new_fw_crash_data(struct ath10k *ar)
723{
724 struct ath10k_fw_crash_data *crash_data = ar->debug.fw_crash_data;
725
726 lockdep_assert_held(&ar->data_lock);
727
728 crash_data->crashed_since_read = true;
729 uuid_le_gen(&crash_data->uuid);
730 getnstimeofday(&crash_data->timestamp);
731
732 return crash_data;
733}
734EXPORT_SYMBOL(ath10k_debug_get_new_fw_crash_data);
735
736static struct ath10k_dump_file_data *ath10k_build_dump_file(struct ath10k *ar)
737{
738 struct ath10k_fw_crash_data *crash_data = ar->debug.fw_crash_data;
739 struct ath10k_dump_file_data *dump_data;
740 struct ath10k_tlv_dump_data *dump_tlv;
741 int hdr_len = sizeof(*dump_data);
742 unsigned int len, sofar = 0;
743 unsigned char *buf;
744
745 len = hdr_len;
746 len += sizeof(*dump_tlv) + sizeof(crash_data->registers);
747
748 sofar += hdr_len;
749
750 /* This is going to get big when we start dumping FW RAM and such,
751 * so go ahead and use vmalloc.
752 */
753 buf = vzalloc(len);
754 if (!buf)
755 return NULL;
756
757 spin_lock_bh(&ar->data_lock);
758
759 if (!crash_data->crashed_since_read) {
760 spin_unlock_bh(&ar->data_lock);
761 vfree(buf);
762 return NULL;
763 }
764
765 dump_data = (struct ath10k_dump_file_data *)(buf);
766 strlcpy(dump_data->df_magic, "ATH10K-FW-DUMP",
767 sizeof(dump_data->df_magic));
768 dump_data->len = cpu_to_le32(len);
769
770 dump_data->version = cpu_to_le32(ATH10K_FW_CRASH_DUMP_VERSION);
771
772 memcpy(dump_data->uuid, &crash_data->uuid, sizeof(dump_data->uuid));
773 dump_data->chip_id = cpu_to_le32(ar->chip_id);
774 dump_data->bus_type = cpu_to_le32(0);
775 dump_data->target_version = cpu_to_le32(ar->target_version);
776 dump_data->fw_version_major = cpu_to_le32(ar->fw_version_major);
777 dump_data->fw_version_minor = cpu_to_le32(ar->fw_version_minor);
778 dump_data->fw_version_release = cpu_to_le32(ar->fw_version_release);
779 dump_data->fw_version_build = cpu_to_le32(ar->fw_version_build);
780 dump_data->phy_capability = cpu_to_le32(ar->phy_capability);
781 dump_data->hw_min_tx_power = cpu_to_le32(ar->hw_min_tx_power);
782 dump_data->hw_max_tx_power = cpu_to_le32(ar->hw_max_tx_power);
783 dump_data->ht_cap_info = cpu_to_le32(ar->ht_cap_info);
784 dump_data->vht_cap_info = cpu_to_le32(ar->vht_cap_info);
785 dump_data->num_rf_chains = cpu_to_le32(ar->num_rf_chains);
786
787 strlcpy(dump_data->fw_ver, ar->hw->wiphy->fw_version,
788 sizeof(dump_data->fw_ver));
789
Johannes Berg12c27152014-11-21 18:58:49 +0200790 dump_data->kernel_ver_code = 0;
791 strlcpy(dump_data->kernel_ver, init_utsname()->release,
Ben Greear384914b2014-08-25 08:37:32 +0300792 sizeof(dump_data->kernel_ver));
793
794 dump_data->tv_sec = cpu_to_le64(crash_data->timestamp.tv_sec);
795 dump_data->tv_nsec = cpu_to_le64(crash_data->timestamp.tv_nsec);
796
797 /* Gather crash-dump */
798 dump_tlv = (struct ath10k_tlv_dump_data *)(buf + sofar);
799 dump_tlv->type = cpu_to_le32(ATH10K_FW_CRASH_DUMP_REGISTERS);
800 dump_tlv->tlv_len = cpu_to_le32(sizeof(crash_data->registers));
801 memcpy(dump_tlv->tlv_data, &crash_data->registers,
802 sizeof(crash_data->registers));
803 sofar += sizeof(*dump_tlv) + sizeof(crash_data->registers);
804
805 ar->debug.fw_crash_data->crashed_since_read = false;
806
807 spin_unlock_bh(&ar->data_lock);
808
809 return dump_data;
810}
811
812static int ath10k_fw_crash_dump_open(struct inode *inode, struct file *file)
813{
814 struct ath10k *ar = inode->i_private;
815 struct ath10k_dump_file_data *dump;
816
817 dump = ath10k_build_dump_file(ar);
818 if (!dump)
819 return -ENODATA;
820
821 file->private_data = dump;
822
823 return 0;
824}
825
826static ssize_t ath10k_fw_crash_dump_read(struct file *file,
827 char __user *user_buf,
828 size_t count, loff_t *ppos)
829{
830 struct ath10k_dump_file_data *dump_file = file->private_data;
831
832 return simple_read_from_buffer(user_buf, count, ppos,
833 dump_file,
834 le32_to_cpu(dump_file->len));
835}
836
837static int ath10k_fw_crash_dump_release(struct inode *inode,
838 struct file *file)
839{
840 vfree(file->private_data);
841
842 return 0;
843}
844
845static const struct file_operations fops_fw_crash_dump = {
846 .open = ath10k_fw_crash_dump_open,
847 .read = ath10k_fw_crash_dump_read,
848 .release = ath10k_fw_crash_dump_release,
849 .owner = THIS_MODULE,
850 .llseek = default_llseek,
851};
852
Yanbo Li077a3802014-11-25 12:24:33 +0200853static ssize_t ath10k_reg_addr_read(struct file *file,
854 char __user *user_buf,
855 size_t count, loff_t *ppos)
856{
857 struct ath10k *ar = file->private_data;
858 u8 buf[32];
859 unsigned int len = 0;
860 u32 reg_addr;
861
862 mutex_lock(&ar->conf_mutex);
863 reg_addr = ar->debug.reg_addr;
864 mutex_unlock(&ar->conf_mutex);
865
866 len += scnprintf(buf + len, sizeof(buf) - len, "0x%x\n", reg_addr);
867
868 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
869}
870
871static ssize_t ath10k_reg_addr_write(struct file *file,
872 const char __user *user_buf,
873 size_t count, loff_t *ppos)
874{
875 struct ath10k *ar = file->private_data;
876 u32 reg_addr;
877 int ret;
878
879 ret = kstrtou32_from_user(user_buf, count, 0, &reg_addr);
880 if (ret)
881 return ret;
882
883 if (!IS_ALIGNED(reg_addr, 4))
884 return -EFAULT;
885
886 mutex_lock(&ar->conf_mutex);
887 ar->debug.reg_addr = reg_addr;
888 mutex_unlock(&ar->conf_mutex);
889
890 return count;
891}
892
893static const struct file_operations fops_reg_addr = {
894 .read = ath10k_reg_addr_read,
895 .write = ath10k_reg_addr_write,
896 .open = simple_open,
897 .owner = THIS_MODULE,
898 .llseek = default_llseek,
899};
900
901static ssize_t ath10k_reg_value_read(struct file *file,
902 char __user *user_buf,
903 size_t count, loff_t *ppos)
904{
905 struct ath10k *ar = file->private_data;
906 u8 buf[48];
907 unsigned int len;
908 u32 reg_addr, reg_val;
909 int ret;
910
911 mutex_lock(&ar->conf_mutex);
912
913 if (ar->state != ATH10K_STATE_ON &&
914 ar->state != ATH10K_STATE_UTF) {
915 ret = -ENETDOWN;
916 goto exit;
917 }
918
919 reg_addr = ar->debug.reg_addr;
920
921 reg_val = ath10k_hif_read32(ar, reg_addr);
922 len = scnprintf(buf, sizeof(buf), "0x%08x:0x%08x\n", reg_addr, reg_val);
923
924 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
925
926exit:
927 mutex_unlock(&ar->conf_mutex);
928
929 return ret;
930}
931
932static ssize_t ath10k_reg_value_write(struct file *file,
933 const char __user *user_buf,
934 size_t count, loff_t *ppos)
935{
936 struct ath10k *ar = file->private_data;
937 u32 reg_addr, reg_val;
938 int ret;
939
940 mutex_lock(&ar->conf_mutex);
941
942 if (ar->state != ATH10K_STATE_ON &&
943 ar->state != ATH10K_STATE_UTF) {
944 ret = -ENETDOWN;
945 goto exit;
946 }
947
948 reg_addr = ar->debug.reg_addr;
949
950 ret = kstrtou32_from_user(user_buf, count, 0, &reg_val);
951 if (ret)
952 goto exit;
953
954 ath10k_hif_write32(ar, reg_addr, reg_val);
955
956 ret = count;
957
958exit:
959 mutex_unlock(&ar->conf_mutex);
960
961 return ret;
962}
963
964static const struct file_operations fops_reg_value = {
965 .read = ath10k_reg_value_read,
966 .write = ath10k_reg_value_write,
967 .open = simple_open,
968 .owner = THIS_MODULE,
969 .llseek = default_llseek,
970};
971
Yanbo Li9f65ad22014-11-25 12:24:48 +0200972static ssize_t ath10k_mem_value_read(struct file *file,
973 char __user *user_buf,
974 size_t count, loff_t *ppos)
975{
976 struct ath10k *ar = file->private_data;
977 u8 *buf;
978 int ret;
979
980 if (*ppos < 0)
981 return -EINVAL;
982
983 if (!count)
984 return 0;
985
986 mutex_lock(&ar->conf_mutex);
987
988 buf = vmalloc(count);
989 if (!buf) {
990 ret = -ENOMEM;
991 goto exit;
992 }
993
994 if (ar->state != ATH10K_STATE_ON &&
995 ar->state != ATH10K_STATE_UTF) {
996 ret = -ENETDOWN;
997 goto exit;
998 }
999
1000 ret = ath10k_hif_diag_read(ar, *ppos, buf, count);
1001 if (ret) {
1002 ath10k_warn(ar, "failed to read address 0x%08x via diagnose window fnrom debugfs: %d\n",
1003 (u32)(*ppos), ret);
1004 goto exit;
1005 }
1006
1007 ret = copy_to_user(user_buf, buf, count);
1008 if (ret) {
1009 ret = -EFAULT;
1010 goto exit;
1011 }
1012
1013 count -= ret;
1014 *ppos += count;
1015 ret = count;
1016
1017exit:
1018 vfree(buf);
1019 mutex_unlock(&ar->conf_mutex);
1020
1021 return ret;
1022}
1023
1024static ssize_t ath10k_mem_value_write(struct file *file,
1025 const char __user *user_buf,
1026 size_t count, loff_t *ppos)
1027{
1028 struct ath10k *ar = file->private_data;
1029 u8 *buf;
1030 int ret;
1031
1032 if (*ppos < 0)
1033 return -EINVAL;
1034
1035 if (!count)
1036 return 0;
1037
1038 mutex_lock(&ar->conf_mutex);
1039
1040 buf = vmalloc(count);
1041 if (!buf) {
1042 ret = -ENOMEM;
1043 goto exit;
1044 }
1045
1046 if (ar->state != ATH10K_STATE_ON &&
1047 ar->state != ATH10K_STATE_UTF) {
1048 ret = -ENETDOWN;
1049 goto exit;
1050 }
1051
1052 ret = copy_from_user(buf, user_buf, count);
1053 if (ret) {
1054 ret = -EFAULT;
1055 goto exit;
1056 }
1057
1058 ret = ath10k_hif_diag_write(ar, *ppos, buf, count);
1059 if (ret) {
1060 ath10k_warn(ar, "failed to write address 0x%08x via diagnose window from debugfs: %d\n",
1061 (u32)(*ppos), ret);
1062 goto exit;
1063 }
1064
1065 *ppos += count;
1066 ret = count;
1067
1068exit:
1069 vfree(buf);
1070 mutex_unlock(&ar->conf_mutex);
1071
1072 return ret;
1073}
1074
1075static const struct file_operations fops_mem_value = {
1076 .read = ath10k_mem_value_read,
1077 .write = ath10k_mem_value_write,
1078 .open = simple_open,
1079 .owner = THIS_MODULE,
1080 .llseek = default_llseek,
1081};
1082
Kalle Valoa3d135e2013-09-03 11:44:10 +03001083static int ath10k_debug_htt_stats_req(struct ath10k *ar)
1084{
1085 u64 cookie;
1086 int ret;
1087
1088 lockdep_assert_held(&ar->conf_mutex);
1089
1090 if (ar->debug.htt_stats_mask == 0)
1091 /* htt stats are disabled */
1092 return 0;
1093
1094 if (ar->state != ATH10K_STATE_ON)
1095 return 0;
1096
1097 cookie = get_jiffies_64();
1098
1099 ret = ath10k_htt_h2t_stats_req(&ar->htt, ar->debug.htt_stats_mask,
1100 cookie);
1101 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001102 ath10k_warn(ar, "failed to send htt stats request: %d\n", ret);
Kalle Valoa3d135e2013-09-03 11:44:10 +03001103 return ret;
1104 }
1105
1106 queue_delayed_work(ar->workqueue, &ar->debug.htt_stats_dwork,
1107 msecs_to_jiffies(ATH10K_DEBUG_HTT_STATS_INTERVAL));
1108
1109 return 0;
1110}
1111
1112static void ath10k_debug_htt_stats_dwork(struct work_struct *work)
1113{
1114 struct ath10k *ar = container_of(work, struct ath10k,
1115 debug.htt_stats_dwork.work);
1116
1117 mutex_lock(&ar->conf_mutex);
1118
1119 ath10k_debug_htt_stats_req(ar);
1120
1121 mutex_unlock(&ar->conf_mutex);
1122}
1123
1124static ssize_t ath10k_read_htt_stats_mask(struct file *file,
Kalle Valo5b07e072014-09-14 12:50:06 +03001125 char __user *user_buf,
1126 size_t count, loff_t *ppos)
Kalle Valoa3d135e2013-09-03 11:44:10 +03001127{
1128 struct ath10k *ar = file->private_data;
1129 char buf[32];
1130 unsigned int len;
1131
1132 len = scnprintf(buf, sizeof(buf), "%lu\n", ar->debug.htt_stats_mask);
1133
1134 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1135}
1136
1137static ssize_t ath10k_write_htt_stats_mask(struct file *file,
Kalle Valo5b07e072014-09-14 12:50:06 +03001138 const char __user *user_buf,
1139 size_t count, loff_t *ppos)
Kalle Valoa3d135e2013-09-03 11:44:10 +03001140{
1141 struct ath10k *ar = file->private_data;
1142 unsigned long mask;
1143 int ret;
1144
1145 ret = kstrtoul_from_user(user_buf, count, 0, &mask);
1146 if (ret)
1147 return ret;
1148
1149 /* max 8 bit masks (for now) */
1150 if (mask > 0xff)
1151 return -E2BIG;
1152
1153 mutex_lock(&ar->conf_mutex);
1154
1155 ar->debug.htt_stats_mask = mask;
1156
1157 ret = ath10k_debug_htt_stats_req(ar);
1158 if (ret)
1159 goto out;
1160
1161 ret = count;
1162
1163out:
1164 mutex_unlock(&ar->conf_mutex);
1165
1166 return ret;
1167}
1168
1169static const struct file_operations fops_htt_stats_mask = {
1170 .read = ath10k_read_htt_stats_mask,
1171 .write = ath10k_write_htt_stats_mask,
1172 .open = simple_open,
1173 .owner = THIS_MODULE,
1174 .llseek = default_llseek,
1175};
1176
Janusz Dziedzicd3856232014-06-02 21:19:46 +03001177static ssize_t ath10k_read_htt_max_amsdu_ampdu(struct file *file,
1178 char __user *user_buf,
1179 size_t count, loff_t *ppos)
1180{
1181 struct ath10k *ar = file->private_data;
1182 char buf[64];
Mohammed Shafi Shajakhan81ec3c02015-11-30 22:29:39 +05301183 u8 amsdu, ampdu;
Janusz Dziedzicd3856232014-06-02 21:19:46 +03001184 unsigned int len;
1185
1186 mutex_lock(&ar->conf_mutex);
1187
David Liuccec9032015-07-24 20:25:32 +03001188 amsdu = ar->htt.max_num_amsdu;
1189 ampdu = ar->htt.max_num_ampdu;
Janusz Dziedzicd3856232014-06-02 21:19:46 +03001190 mutex_unlock(&ar->conf_mutex);
1191
1192 len = scnprintf(buf, sizeof(buf), "%u %u\n", amsdu, ampdu);
1193
1194 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1195}
1196
1197static ssize_t ath10k_write_htt_max_amsdu_ampdu(struct file *file,
1198 const char __user *user_buf,
1199 size_t count, loff_t *ppos)
1200{
1201 struct ath10k *ar = file->private_data;
1202 int res;
1203 char buf[64];
1204 unsigned int amsdu, ampdu;
1205
1206 simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
1207
1208 /* make sure that buf is null terminated */
1209 buf[sizeof(buf) - 1] = 0;
1210
1211 res = sscanf(buf, "%u %u", &amsdu, &ampdu);
1212
1213 if (res != 2)
1214 return -EINVAL;
1215
1216 mutex_lock(&ar->conf_mutex);
1217
1218 res = ath10k_htt_h2t_aggr_cfg_msg(&ar->htt, ampdu, amsdu);
1219 if (res)
1220 goto out;
1221
1222 res = count;
David Liuccec9032015-07-24 20:25:32 +03001223 ar->htt.max_num_amsdu = amsdu;
1224 ar->htt.max_num_ampdu = ampdu;
Janusz Dziedzicd3856232014-06-02 21:19:46 +03001225
1226out:
1227 mutex_unlock(&ar->conf_mutex);
1228 return res;
1229}
1230
1231static const struct file_operations fops_htt_max_amsdu_ampdu = {
1232 .read = ath10k_read_htt_max_amsdu_ampdu,
1233 .write = ath10k_write_htt_max_amsdu_ampdu,
1234 .open = simple_open,
1235 .owner = THIS_MODULE,
1236 .llseek = default_llseek,
1237};
1238
Kalle Valof118a3e2014-01-03 12:59:31 +02001239static ssize_t ath10k_read_fw_dbglog(struct file *file,
Kalle Valo5b07e072014-09-14 12:50:06 +03001240 char __user *user_buf,
1241 size_t count, loff_t *ppos)
Kalle Valof118a3e2014-01-03 12:59:31 +02001242{
1243 struct ath10k *ar = file->private_data;
1244 unsigned int len;
Maharaja Kennadyrajanafcbc822016-08-23 15:35:36 +05301245 char buf[96];
Kalle Valof118a3e2014-01-03 12:59:31 +02001246
Maharaja Kennadyrajanafcbc822016-08-23 15:35:36 +05301247 len = scnprintf(buf, sizeof(buf), "0x%16llx %u\n",
SenthilKumar Jegadeesan467210a2015-01-29 14:36:52 +05301248 ar->debug.fw_dbglog_mask, ar->debug.fw_dbglog_level);
Kalle Valof118a3e2014-01-03 12:59:31 +02001249
1250 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1251}
1252
1253static ssize_t ath10k_write_fw_dbglog(struct file *file,
1254 const char __user *user_buf,
1255 size_t count, loff_t *ppos)
1256{
1257 struct ath10k *ar = file->private_data;
Kalle Valof118a3e2014-01-03 12:59:31 +02001258 int ret;
Maharaja Kennadyrajanafcbc822016-08-23 15:35:36 +05301259 char buf[96];
1260 unsigned int log_level;
1261 u64 mask;
Kalle Valof118a3e2014-01-03 12:59:31 +02001262
SenthilKumar Jegadeesan467210a2015-01-29 14:36:52 +05301263 simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
1264
1265 /* make sure that buf is null terminated */
1266 buf[sizeof(buf) - 1] = 0;
1267
Maharaja Kennadyrajanafcbc822016-08-23 15:35:36 +05301268 ret = sscanf(buf, "%llx %u", &mask, &log_level);
SenthilKumar Jegadeesan467210a2015-01-29 14:36:52 +05301269
1270 if (!ret)
1271 return -EINVAL;
1272
1273 if (ret == 1)
1274 /* default if user did not specify */
1275 log_level = ATH10K_DBGLOG_LEVEL_WARN;
Kalle Valof118a3e2014-01-03 12:59:31 +02001276
1277 mutex_lock(&ar->conf_mutex);
1278
1279 ar->debug.fw_dbglog_mask = mask;
SenthilKumar Jegadeesan467210a2015-01-29 14:36:52 +05301280 ar->debug.fw_dbglog_level = log_level;
Kalle Valof118a3e2014-01-03 12:59:31 +02001281
1282 if (ar->state == ATH10K_STATE_ON) {
SenthilKumar Jegadeesan467210a2015-01-29 14:36:52 +05301283 ret = ath10k_wmi_dbglog_cfg(ar, ar->debug.fw_dbglog_mask,
1284 ar->debug.fw_dbglog_level);
Kalle Valof118a3e2014-01-03 12:59:31 +02001285 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001286 ath10k_warn(ar, "dbglog cfg failed from debugfs: %d\n",
Kalle Valof118a3e2014-01-03 12:59:31 +02001287 ret);
1288 goto exit;
1289 }
1290 }
1291
1292 ret = count;
1293
1294exit:
1295 mutex_unlock(&ar->conf_mutex);
1296
1297 return ret;
1298}
1299
Ben Greear6cddcc72014-09-29 14:41:46 +03001300/* TODO: Would be nice to always support ethtool stats, would need to
1301 * move the stats storage out of ath10k_debug, or always have ath10k_debug
1302 * struct available..
1303 */
1304
1305/* This generally cooresponds to the debugfs fw_stats file */
1306static const char ath10k_gstrings_stats[][ETH_GSTRING_LEN] = {
1307 "tx_pkts_nic",
1308 "tx_bytes_nic",
1309 "rx_pkts_nic",
1310 "rx_bytes_nic",
1311 "d_noise_floor",
1312 "d_cycle_count",
1313 "d_phy_error",
1314 "d_rts_bad",
1315 "d_rts_good",
1316 "d_tx_power", /* in .5 dbM I think */
1317 "d_rx_crc_err", /* fcs_bad */
1318 "d_no_beacon",
1319 "d_tx_mpdus_queued",
1320 "d_tx_msdu_queued",
1321 "d_tx_msdu_dropped",
1322 "d_local_enqued",
1323 "d_local_freed",
1324 "d_tx_ppdu_hw_queued",
1325 "d_tx_ppdu_reaped",
1326 "d_tx_fifo_underrun",
1327 "d_tx_ppdu_abort",
1328 "d_tx_mpdu_requed",
1329 "d_tx_excessive_retries",
1330 "d_tx_hw_rate",
1331 "d_tx_dropped_sw_retries",
1332 "d_tx_illegal_rate",
1333 "d_tx_continuous_xretries",
1334 "d_tx_timeout",
1335 "d_tx_mpdu_txop_limit",
1336 "d_pdev_resets",
1337 "d_rx_mid_ppdu_route_change",
1338 "d_rx_status",
1339 "d_rx_extra_frags_ring0",
1340 "d_rx_extra_frags_ring1",
1341 "d_rx_extra_frags_ring2",
1342 "d_rx_extra_frags_ring3",
1343 "d_rx_msdu_htt",
1344 "d_rx_mpdu_htt",
1345 "d_rx_msdu_stack",
1346 "d_rx_mpdu_stack",
1347 "d_rx_phy_err",
1348 "d_rx_phy_err_drops",
1349 "d_rx_mpdu_errors", /* FCS, MIC, ENC */
1350 "d_fw_crash_count",
1351 "d_fw_warm_reset_count",
1352 "d_fw_cold_reset_count",
1353};
1354
1355#define ATH10K_SSTATS_LEN ARRAY_SIZE(ath10k_gstrings_stats)
1356
1357void ath10k_debug_get_et_strings(struct ieee80211_hw *hw,
1358 struct ieee80211_vif *vif,
1359 u32 sset, u8 *data)
1360{
1361 if (sset == ETH_SS_STATS)
1362 memcpy(data, *ath10k_gstrings_stats,
1363 sizeof(ath10k_gstrings_stats));
1364}
1365
1366int ath10k_debug_get_et_sset_count(struct ieee80211_hw *hw,
1367 struct ieee80211_vif *vif, int sset)
1368{
1369 if (sset == ETH_SS_STATS)
1370 return ATH10K_SSTATS_LEN;
1371
1372 return 0;
1373}
1374
1375void ath10k_debug_get_et_stats(struct ieee80211_hw *hw,
1376 struct ieee80211_vif *vif,
1377 struct ethtool_stats *stats, u64 *data)
1378{
1379 struct ath10k *ar = hw->priv;
1380 static const struct ath10k_fw_stats_pdev zero_stats = {};
1381 const struct ath10k_fw_stats_pdev *pdev_stats;
1382 int i = 0, ret;
1383
1384 mutex_lock(&ar->conf_mutex);
1385
1386 if (ar->state == ATH10K_STATE_ON) {
1387 ret = ath10k_debug_fw_stats_request(ar);
1388 if (ret) {
1389 /* just print a warning and try to use older results */
1390 ath10k_warn(ar,
1391 "failed to get fw stats for ethtool: %d\n",
1392 ret);
1393 }
1394 }
1395
1396 pdev_stats = list_first_entry_or_null(&ar->debug.fw_stats.pdevs,
1397 struct ath10k_fw_stats_pdev,
1398 list);
1399 if (!pdev_stats) {
1400 /* no results available so just return zeroes */
1401 pdev_stats = &zero_stats;
1402 }
1403
1404 spin_lock_bh(&ar->data_lock);
1405
1406 data[i++] = pdev_stats->hw_reaped; /* ppdu reaped */
1407 data[i++] = 0; /* tx bytes */
1408 data[i++] = pdev_stats->htt_mpdus;
1409 data[i++] = 0; /* rx bytes */
1410 data[i++] = pdev_stats->ch_noise_floor;
1411 data[i++] = pdev_stats->cycle_count;
1412 data[i++] = pdev_stats->phy_err_count;
1413 data[i++] = pdev_stats->rts_bad;
1414 data[i++] = pdev_stats->rts_good;
1415 data[i++] = pdev_stats->chan_tx_power;
1416 data[i++] = pdev_stats->fcs_bad;
1417 data[i++] = pdev_stats->no_beacons;
1418 data[i++] = pdev_stats->mpdu_enqued;
1419 data[i++] = pdev_stats->msdu_enqued;
1420 data[i++] = pdev_stats->wmm_drop;
1421 data[i++] = pdev_stats->local_enqued;
1422 data[i++] = pdev_stats->local_freed;
1423 data[i++] = pdev_stats->hw_queued;
1424 data[i++] = pdev_stats->hw_reaped;
1425 data[i++] = pdev_stats->underrun;
1426 data[i++] = pdev_stats->tx_abort;
1427 data[i++] = pdev_stats->mpdus_requed;
1428 data[i++] = pdev_stats->tx_ko;
1429 data[i++] = pdev_stats->data_rc;
1430 data[i++] = pdev_stats->sw_retry_failure;
1431 data[i++] = pdev_stats->illgl_rate_phy_err;
1432 data[i++] = pdev_stats->pdev_cont_xretry;
1433 data[i++] = pdev_stats->pdev_tx_timeout;
1434 data[i++] = pdev_stats->txop_ovf;
1435 data[i++] = pdev_stats->pdev_resets;
1436 data[i++] = pdev_stats->mid_ppdu_route_change;
1437 data[i++] = pdev_stats->status_rcvd;
1438 data[i++] = pdev_stats->r0_frags;
1439 data[i++] = pdev_stats->r1_frags;
1440 data[i++] = pdev_stats->r2_frags;
1441 data[i++] = pdev_stats->r3_frags;
1442 data[i++] = pdev_stats->htt_msdus;
1443 data[i++] = pdev_stats->htt_mpdus;
1444 data[i++] = pdev_stats->loc_msdus;
1445 data[i++] = pdev_stats->loc_mpdus;
1446 data[i++] = pdev_stats->phy_errs;
1447 data[i++] = pdev_stats->phy_err_drop;
1448 data[i++] = pdev_stats->mpdu_errs;
1449 data[i++] = ar->stats.fw_crash_counter;
1450 data[i++] = ar->stats.fw_warm_reset_counter;
1451 data[i++] = ar->stats.fw_cold_reset_counter;
1452
1453 spin_unlock_bh(&ar->data_lock);
1454
1455 mutex_unlock(&ar->conf_mutex);
1456
1457 WARN_ON(i != ATH10K_SSTATS_LEN);
1458}
1459
Kalle Valof118a3e2014-01-03 12:59:31 +02001460static const struct file_operations fops_fw_dbglog = {
1461 .read = ath10k_read_fw_dbglog,
1462 .write = ath10k_write_fw_dbglog,
1463 .open = simple_open,
1464 .owner = THIS_MODULE,
1465 .llseek = default_llseek,
1466};
1467
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001468static int ath10k_debug_cal_data_fetch(struct ath10k *ar)
Kalle Valo7869b4f2014-09-24 14:16:58 +03001469{
Kalle Valo7869b4f2014-09-24 14:16:58 +03001470 u32 hi_addr;
1471 __le32 addr;
1472 int ret;
1473
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001474 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo7869b4f2014-09-24 14:16:58 +03001475
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001476 if (WARN_ON(ar->hw_params.cal_data_len > ATH10K_DEBUG_CAL_DATA_LEN))
1477 return -EINVAL;
Kalle Valo7869b4f2014-09-24 14:16:58 +03001478
1479 hi_addr = host_interest_item_address(HI_ITEM(hi_board_data));
1480
1481 ret = ath10k_hif_diag_read(ar, hi_addr, &addr, sizeof(addr));
1482 if (ret) {
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001483 ath10k_warn(ar, "failed to read hi_board_data address: %d\n",
1484 ret);
1485 return ret;
Kalle Valo7869b4f2014-09-24 14:16:58 +03001486 }
1487
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001488 ret = ath10k_hif_diag_read(ar, le32_to_cpu(addr), ar->debug.cal_data,
Raja Mani0b8e3c42016-03-18 11:44:22 +02001489 ar->hw_params.cal_data_len);
Kalle Valo7869b4f2014-09-24 14:16:58 +03001490 if (ret) {
1491 ath10k_warn(ar, "failed to read calibration data: %d\n", ret);
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001492 return ret;
Kalle Valo7869b4f2014-09-24 14:16:58 +03001493 }
1494
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001495 return 0;
1496}
Kalle Valo7869b4f2014-09-24 14:16:58 +03001497
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001498static int ath10k_debug_cal_data_open(struct inode *inode, struct file *file)
1499{
1500 struct ath10k *ar = inode->i_private;
1501
1502 mutex_lock(&ar->conf_mutex);
1503
1504 if (ar->state == ATH10K_STATE_ON ||
1505 ar->state == ATH10K_STATE_UTF) {
1506 ath10k_debug_cal_data_fetch(ar);
1507 }
1508
1509 file->private_data = ar;
Kalle Valo7869b4f2014-09-24 14:16:58 +03001510 mutex_unlock(&ar->conf_mutex);
1511
1512 return 0;
Kalle Valo7869b4f2014-09-24 14:16:58 +03001513}
1514
1515static ssize_t ath10k_debug_cal_data_read(struct file *file,
1516 char __user *user_buf,
1517 size_t count, loff_t *ppos)
1518{
Raja Mani0b8e3c42016-03-18 11:44:22 +02001519 struct ath10k *ar = file->private_data;
Kalle Valo7869b4f2014-09-24 14:16:58 +03001520
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001521 mutex_lock(&ar->conf_mutex);
Kalle Valo7869b4f2014-09-24 14:16:58 +03001522
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001523 count = simple_read_from_buffer(user_buf, count, ppos,
1524 ar->debug.cal_data,
1525 ar->hw_params.cal_data_len);
Kalle Valo7869b4f2014-09-24 14:16:58 +03001526
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001527 mutex_unlock(&ar->conf_mutex);
1528
1529 return count;
Kalle Valo7869b4f2014-09-24 14:16:58 +03001530}
1531
Ashok Raj Nagarajanb3e71d72015-03-19 16:38:00 +05301532static ssize_t ath10k_write_ani_enable(struct file *file,
1533 const char __user *user_buf,
1534 size_t count, loff_t *ppos)
1535{
1536 struct ath10k *ar = file->private_data;
1537 int ret;
1538 u8 enable;
1539
1540 if (kstrtou8_from_user(user_buf, count, 0, &enable))
1541 return -EINVAL;
1542
1543 mutex_lock(&ar->conf_mutex);
1544
1545 if (ar->ani_enabled == enable) {
1546 ret = count;
1547 goto exit;
1548 }
1549
1550 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->ani_enable,
1551 enable);
1552 if (ret) {
1553 ath10k_warn(ar, "ani_enable failed from debugfs: %d\n", ret);
1554 goto exit;
1555 }
1556 ar->ani_enabled = enable;
1557
1558 ret = count;
1559
1560exit:
1561 mutex_unlock(&ar->conf_mutex);
1562
1563 return ret;
1564}
1565
1566static ssize_t ath10k_read_ani_enable(struct file *file, char __user *user_buf,
1567 size_t count, loff_t *ppos)
1568{
1569 struct ath10k *ar = file->private_data;
1570 int len = 0;
1571 char buf[32];
1572
1573 len = scnprintf(buf, sizeof(buf) - len, "%d\n",
1574 ar->ani_enabled);
1575
1576 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1577}
1578
1579static const struct file_operations fops_ani_enable = {
1580 .read = ath10k_read_ani_enable,
1581 .write = ath10k_write_ani_enable,
1582 .open = simple_open,
1583 .owner = THIS_MODULE,
1584 .llseek = default_llseek,
1585};
1586
Kalle Valo7869b4f2014-09-24 14:16:58 +03001587static const struct file_operations fops_cal_data = {
1588 .open = ath10k_debug_cal_data_open,
1589 .read = ath10k_debug_cal_data_read,
Kalle Valo7869b4f2014-09-24 14:16:58 +03001590 .owner = THIS_MODULE,
1591 .llseek = default_llseek,
1592};
1593
Peter Oha7bd3e92014-12-02 13:07:14 +02001594static ssize_t ath10k_read_nf_cal_period(struct file *file,
1595 char __user *user_buf,
1596 size_t count, loff_t *ppos)
1597{
1598 struct ath10k *ar = file->private_data;
1599 unsigned int len;
1600 char buf[32];
1601
1602 len = scnprintf(buf, sizeof(buf), "%d\n",
1603 ar->debug.nf_cal_period);
1604
1605 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1606}
1607
1608static ssize_t ath10k_write_nf_cal_period(struct file *file,
1609 const char __user *user_buf,
1610 size_t count, loff_t *ppos)
1611{
1612 struct ath10k *ar = file->private_data;
1613 unsigned long period;
1614 int ret;
1615
1616 ret = kstrtoul_from_user(user_buf, count, 0, &period);
1617 if (ret)
1618 return ret;
1619
1620 if (period > WMI_PDEV_PARAM_CAL_PERIOD_MAX)
1621 return -EINVAL;
1622
1623 /* there's no way to switch back to the firmware default */
1624 if (period == 0)
1625 return -EINVAL;
1626
1627 mutex_lock(&ar->conf_mutex);
1628
1629 ar->debug.nf_cal_period = period;
1630
1631 if (ar->state != ATH10K_STATE_ON) {
1632 /* firmware is not running, nothing else to do */
1633 ret = count;
1634 goto exit;
1635 }
1636
1637 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->cal_period,
1638 ar->debug.nf_cal_period);
1639 if (ret) {
1640 ath10k_warn(ar, "cal period cfg failed from debugfs: %d\n",
1641 ret);
1642 goto exit;
1643 }
1644
1645 ret = count;
1646
1647exit:
1648 mutex_unlock(&ar->conf_mutex);
1649
1650 return ret;
1651}
1652
1653static const struct file_operations fops_nf_cal_period = {
1654 .read = ath10k_read_nf_cal_period,
1655 .write = ath10k_write_nf_cal_period,
1656 .open = simple_open,
1657 .owner = THIS_MODULE,
1658 .llseek = default_llseek,
1659};
1660
Maharaja Kennadyrajan29542662015-10-05 17:56:38 +03001661#define ATH10K_TPC_CONFIG_BUF_SIZE (1024 * 1024)
1662
1663static int ath10k_debug_tpc_stats_request(struct ath10k *ar)
1664{
1665 int ret;
1666 unsigned long time_left;
1667
1668 lockdep_assert_held(&ar->conf_mutex);
1669
1670 reinit_completion(&ar->debug.tpc_complete);
1671
1672 ret = ath10k_wmi_pdev_get_tpc_config(ar, WMI_TPC_CONFIG_PARAM);
1673 if (ret) {
1674 ath10k_warn(ar, "failed to request tpc config: %d\n", ret);
1675 return ret;
1676 }
1677
1678 time_left = wait_for_completion_timeout(&ar->debug.tpc_complete,
1679 1 * HZ);
1680 if (time_left == 0)
1681 return -ETIMEDOUT;
1682
1683 return 0;
1684}
1685
1686void ath10k_debug_tpc_stats_process(struct ath10k *ar,
1687 struct ath10k_tpc_stats *tpc_stats)
1688{
1689 spin_lock_bh(&ar->data_lock);
1690
1691 kfree(ar->debug.tpc_stats);
1692 ar->debug.tpc_stats = tpc_stats;
1693 complete(&ar->debug.tpc_complete);
1694
1695 spin_unlock_bh(&ar->data_lock);
1696}
1697
1698static void ath10k_tpc_stats_print(struct ath10k_tpc_stats *tpc_stats,
1699 unsigned int j, char *buf, unsigned int *len)
1700{
1701 unsigned int i, buf_len;
1702 static const char table_str[][5] = { "CDD",
1703 "STBC",
1704 "TXBF" };
1705 static const char pream_str[][6] = { "CCK",
1706 "OFDM",
1707 "HT20",
1708 "HT40",
1709 "VHT20",
1710 "VHT40",
1711 "VHT80",
1712 "HTCUP" };
1713
1714 buf_len = ATH10K_TPC_CONFIG_BUF_SIZE;
1715 *len += scnprintf(buf + *len, buf_len - *len,
1716 "********************************\n");
1717 *len += scnprintf(buf + *len, buf_len - *len,
1718 "******************* %s POWER TABLE ****************\n",
1719 table_str[j]);
1720 *len += scnprintf(buf + *len, buf_len - *len,
1721 "********************************\n");
1722 *len += scnprintf(buf + *len, buf_len - *len,
1723 "No. Preamble Rate_code tpc_value1 tpc_value2 tpc_value3\n");
1724
1725 for (i = 0; i < tpc_stats->rate_max; i++) {
1726 *len += scnprintf(buf + *len, buf_len - *len,
1727 "%8d %s 0x%2x %s\n", i,
1728 pream_str[tpc_stats->tpc_table[j].pream_idx[i]],
1729 tpc_stats->tpc_table[j].rate_code[i],
1730 tpc_stats->tpc_table[j].tpc_value[i]);
1731 }
1732
1733 *len += scnprintf(buf + *len, buf_len - *len,
1734 "***********************************\n");
1735}
1736
1737static void ath10k_tpc_stats_fill(struct ath10k *ar,
1738 struct ath10k_tpc_stats *tpc_stats,
1739 char *buf)
1740{
1741 unsigned int len, j, buf_len;
1742
1743 len = 0;
1744 buf_len = ATH10K_TPC_CONFIG_BUF_SIZE;
1745
1746 spin_lock_bh(&ar->data_lock);
1747
1748 if (!tpc_stats) {
1749 ath10k_warn(ar, "failed to get tpc stats\n");
1750 goto unlock;
1751 }
1752
1753 len += scnprintf(buf + len, buf_len - len, "\n");
1754 len += scnprintf(buf + len, buf_len - len,
1755 "*************************************\n");
1756 len += scnprintf(buf + len, buf_len - len,
1757 "TPC config for channel %4d mode %d\n",
1758 tpc_stats->chan_freq,
1759 tpc_stats->phy_mode);
1760 len += scnprintf(buf + len, buf_len - len,
1761 "*************************************\n");
1762 len += scnprintf(buf + len, buf_len - len,
1763 "CTL = 0x%2x Reg. Domain = %2d\n",
1764 tpc_stats->ctl,
1765 tpc_stats->reg_domain);
1766 len += scnprintf(buf + len, buf_len - len,
1767 "Antenna Gain = %2d Reg. Max Antenna Gain = %2d\n",
1768 tpc_stats->twice_antenna_gain,
1769 tpc_stats->twice_antenna_reduction);
1770 len += scnprintf(buf + len, buf_len - len,
1771 "Power Limit = %2d Reg. Max Power = %2d\n",
1772 tpc_stats->power_limit,
1773 tpc_stats->twice_max_rd_power / 2);
1774 len += scnprintf(buf + len, buf_len - len,
1775 "Num tx chains = %2d Num supported rates = %2d\n",
1776 tpc_stats->num_tx_chain,
1777 tpc_stats->rate_max);
1778
1779 for (j = 0; j < tpc_stats->num_tx_chain ; j++) {
1780 switch (j) {
1781 case WMI_TPC_TABLE_TYPE_CDD:
1782 if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) {
1783 len += scnprintf(buf + len, buf_len - len,
1784 "CDD not supported\n");
1785 break;
1786 }
1787
1788 ath10k_tpc_stats_print(tpc_stats, j, buf, &len);
1789 break;
1790 case WMI_TPC_TABLE_TYPE_STBC:
1791 if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) {
1792 len += scnprintf(buf + len, buf_len - len,
1793 "STBC not supported\n");
1794 break;
1795 }
1796
1797 ath10k_tpc_stats_print(tpc_stats, j, buf, &len);
1798 break;
1799 case WMI_TPC_TABLE_TYPE_TXBF:
1800 if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) {
1801 len += scnprintf(buf + len, buf_len - len,
1802 "TXBF not supported\n***************************\n");
1803 break;
1804 }
1805
1806 ath10k_tpc_stats_print(tpc_stats, j, buf, &len);
1807 break;
1808 default:
1809 len += scnprintf(buf + len, buf_len - len,
1810 "Invalid Type\n");
1811 break;
1812 }
1813 }
1814
1815unlock:
1816 spin_unlock_bh(&ar->data_lock);
1817
1818 if (len >= buf_len)
1819 buf[len - 1] = 0;
1820 else
1821 buf[len] = 0;
1822}
1823
1824static int ath10k_tpc_stats_open(struct inode *inode, struct file *file)
1825{
1826 struct ath10k *ar = inode->i_private;
1827 void *buf = NULL;
1828 int ret;
1829
1830 mutex_lock(&ar->conf_mutex);
1831
1832 if (ar->state != ATH10K_STATE_ON) {
1833 ret = -ENETDOWN;
1834 goto err_unlock;
1835 }
1836
1837 buf = vmalloc(ATH10K_TPC_CONFIG_BUF_SIZE);
1838 if (!buf) {
1839 ret = -ENOMEM;
1840 goto err_unlock;
1841 }
1842
1843 ret = ath10k_debug_tpc_stats_request(ar);
1844 if (ret) {
1845 ath10k_warn(ar, "failed to request tpc config stats: %d\n",
1846 ret);
1847 goto err_free;
1848 }
1849
1850 ath10k_tpc_stats_fill(ar, ar->debug.tpc_stats, buf);
1851 file->private_data = buf;
1852
1853 mutex_unlock(&ar->conf_mutex);
1854 return 0;
1855
1856err_free:
1857 vfree(buf);
1858
1859err_unlock:
1860 mutex_unlock(&ar->conf_mutex);
1861 return ret;
1862}
1863
1864static int ath10k_tpc_stats_release(struct inode *inode, struct file *file)
1865{
1866 vfree(file->private_data);
1867
1868 return 0;
1869}
1870
1871static ssize_t ath10k_tpc_stats_read(struct file *file, char __user *user_buf,
1872 size_t count, loff_t *ppos)
1873{
1874 const char *buf = file->private_data;
1875 unsigned int len = strlen(buf);
1876
1877 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1878}
1879
1880static const struct file_operations fops_tpc_stats = {
1881 .open = ath10k_tpc_stats_open,
1882 .release = ath10k_tpc_stats_release,
1883 .read = ath10k_tpc_stats_read,
1884 .owner = THIS_MODULE,
1885 .llseek = default_llseek,
1886};
1887
Kalle Valodb66ea02013-09-03 11:44:03 +03001888int ath10k_debug_start(struct ath10k *ar)
1889{
Kalle Valoa3d135e2013-09-03 11:44:10 +03001890 int ret;
1891
Kalle Valo60631c52013-10-08 21:45:25 +03001892 lockdep_assert_held(&ar->conf_mutex);
1893
Kalle Valoa3d135e2013-09-03 11:44:10 +03001894 ret = ath10k_debug_htt_stats_req(ar);
1895 if (ret)
1896 /* continue normally anyway, this isn't serious */
Michal Kazior7aa7a722014-08-25 12:09:38 +02001897 ath10k_warn(ar, "failed to start htt stats workqueue: %d\n",
1898 ret);
Kalle Valoa3d135e2013-09-03 11:44:10 +03001899
Kalle Valof118a3e2014-01-03 12:59:31 +02001900 if (ar->debug.fw_dbglog_mask) {
SenthilKumar Jegadeesan467210a2015-01-29 14:36:52 +05301901 ret = ath10k_wmi_dbglog_cfg(ar, ar->debug.fw_dbglog_mask,
1902 ATH10K_DBGLOG_LEVEL_WARN);
Kalle Valof118a3e2014-01-03 12:59:31 +02001903 if (ret)
1904 /* not serious */
Michal Kazior7aa7a722014-08-25 12:09:38 +02001905 ath10k_warn(ar, "failed to enable dbglog during start: %d",
Kalle Valof118a3e2014-01-03 12:59:31 +02001906 ret);
1907 }
1908
Rajkumar Manoharan90174452014-10-03 08:02:33 +03001909 if (ar->debug.pktlog_filter) {
1910 ret = ath10k_wmi_pdev_pktlog_enable(ar,
1911 ar->debug.pktlog_filter);
1912 if (ret)
1913 /* not serious */
1914 ath10k_warn(ar,
1915 "failed to enable pktlog filter %x: %d\n",
1916 ar->debug.pktlog_filter, ret);
1917 } else {
1918 ret = ath10k_wmi_pdev_pktlog_disable(ar);
1919 if (ret)
1920 /* not serious */
1921 ath10k_warn(ar, "failed to disable pktlog: %d\n", ret);
1922 }
1923
Peter Oha7bd3e92014-12-02 13:07:14 +02001924 if (ar->debug.nf_cal_period) {
1925 ret = ath10k_wmi_pdev_set_param(ar,
1926 ar->wmi.pdev_param->cal_period,
1927 ar->debug.nf_cal_period);
1928 if (ret)
1929 /* not serious */
1930 ath10k_warn(ar, "cal period cfg failed from debug start: %d\n",
1931 ret);
1932 }
1933
Rajkumar Manoharan90174452014-10-03 08:02:33 +03001934 return ret;
Kalle Valodb66ea02013-09-03 11:44:03 +03001935}
1936
1937void ath10k_debug_stop(struct ath10k *ar)
1938{
Kalle Valo60631c52013-10-08 21:45:25 +03001939 lockdep_assert_held(&ar->conf_mutex);
1940
Marty Faltesekf67b107d2016-10-10 19:00:04 +03001941 ath10k_debug_cal_data_fetch(ar);
1942
Kalle Valo60631c52013-10-08 21:45:25 +03001943 /* Must not use _sync to avoid deadlock, we do that in
1944 * ath10k_debug_destroy(). The check for htt_stats_mask is to avoid
1945 * warning from del_timer(). */
1946 if (ar->debug.htt_stats_mask != 0)
1947 cancel_delayed_work(&ar->debug.htt_stats_dwork);
Janusz Dziedzicd3856232014-06-02 21:19:46 +03001948
Rajkumar Manoharan90174452014-10-03 08:02:33 +03001949 ath10k_wmi_pdev_pktlog_disable(ar);
Kalle Valodb66ea02013-09-03 11:44:03 +03001950}
1951
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001952static ssize_t ath10k_write_simulate_radar(struct file *file,
1953 const char __user *user_buf,
1954 size_t count, loff_t *ppos)
1955{
1956 struct ath10k *ar = file->private_data;
Mohammed Shafi Shajakhan6a528eb2017-02-22 21:03:11 +05301957 struct ath10k_vif *arvif;
1958
1959 /* Just check for for the first vif alone, as all the vifs will be
1960 * sharing the same channel and if the channel is disabled, all the
1961 * vifs will share the same 'is_started' state.
1962 */
1963 arvif = list_first_entry(&ar->arvifs, typeof(*arvif), list);
1964 if (!arvif->is_started)
1965 return -EINVAL;
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001966
1967 ieee80211_radar_detected(ar->hw);
1968
1969 return count;
1970}
1971
1972static const struct file_operations fops_simulate_radar = {
1973 .write = ath10k_write_simulate_radar,
1974 .open = simple_open,
1975 .owner = THIS_MODULE,
1976 .llseek = default_llseek,
1977};
1978
1979#define ATH10K_DFS_STAT(s, p) (\
1980 len += scnprintf(buf + len, size - len, "%-28s : %10u\n", s, \
1981 ar->debug.dfs_stats.p))
1982
1983#define ATH10K_DFS_POOL_STAT(s, p) (\
1984 len += scnprintf(buf + len, size - len, "%-28s : %10u\n", s, \
1985 ar->debug.dfs_pool_stats.p))
1986
1987static ssize_t ath10k_read_dfs_stats(struct file *file, char __user *user_buf,
1988 size_t count, loff_t *ppos)
1989{
1990 int retval = 0, len = 0;
1991 const int size = 8000;
1992 struct ath10k *ar = file->private_data;
1993 char *buf;
1994
1995 buf = kzalloc(size, GFP_KERNEL);
1996 if (buf == NULL)
1997 return -ENOMEM;
1998
1999 if (!ar->dfs_detector) {
2000 len += scnprintf(buf + len, size - len, "DFS not enabled\n");
2001 goto exit;
2002 }
2003
2004 ar->debug.dfs_pool_stats =
2005 ar->dfs_detector->get_stats(ar->dfs_detector);
2006
2007 len += scnprintf(buf + len, size - len, "Pulse detector statistics:\n");
2008
2009 ATH10K_DFS_STAT("reported phy errors", phy_errors);
2010 ATH10K_DFS_STAT("pulse events reported", pulses_total);
2011 ATH10K_DFS_STAT("DFS pulses detected", pulses_detected);
2012 ATH10K_DFS_STAT("DFS pulses discarded", pulses_discarded);
2013 ATH10K_DFS_STAT("Radars detected", radar_detected);
2014
2015 len += scnprintf(buf + len, size - len, "Global Pool statistics:\n");
2016 ATH10K_DFS_POOL_STAT("Pool references", pool_reference);
2017 ATH10K_DFS_POOL_STAT("Pulses allocated", pulse_allocated);
2018 ATH10K_DFS_POOL_STAT("Pulses alloc error", pulse_alloc_error);
2019 ATH10K_DFS_POOL_STAT("Pulses in use", pulse_used);
2020 ATH10K_DFS_POOL_STAT("Seqs. allocated", pseq_allocated);
2021 ATH10K_DFS_POOL_STAT("Seqs. alloc error", pseq_alloc_error);
2022 ATH10K_DFS_POOL_STAT("Seqs. in use", pseq_used);
2023
2024exit:
2025 if (len > size)
2026 len = size;
2027
2028 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
2029 kfree(buf);
2030
2031 return retval;
2032}
2033
2034static const struct file_operations fops_dfs_stats = {
2035 .read = ath10k_read_dfs_stats,
2036 .open = simple_open,
2037 .owner = THIS_MODULE,
2038 .llseek = default_llseek,
2039};
2040
Rajkumar Manoharan90174452014-10-03 08:02:33 +03002041static ssize_t ath10k_write_pktlog_filter(struct file *file,
2042 const char __user *ubuf,
2043 size_t count, loff_t *ppos)
2044{
2045 struct ath10k *ar = file->private_data;
2046 u32 filter;
2047 int ret;
2048
2049 if (kstrtouint_from_user(ubuf, count, 0, &filter))
2050 return -EINVAL;
2051
2052 mutex_lock(&ar->conf_mutex);
2053
2054 if (ar->state != ATH10K_STATE_ON) {
2055 ar->debug.pktlog_filter = filter;
2056 ret = count;
2057 goto out;
2058 }
2059
Anilkumar Kolli9ddc4862016-03-11 11:46:39 +05302060 if (filter == ar->debug.pktlog_filter) {
2061 ret = count;
2062 goto out;
2063 }
2064
2065 if (filter) {
Rajkumar Manoharan90174452014-10-03 08:02:33 +03002066 ret = ath10k_wmi_pdev_pktlog_enable(ar, filter);
2067 if (ret) {
2068 ath10k_warn(ar, "failed to enable pktlog filter %x: %d\n",
2069 ar->debug.pktlog_filter, ret);
2070 goto out;
2071 }
2072 } else {
2073 ret = ath10k_wmi_pdev_pktlog_disable(ar);
2074 if (ret) {
2075 ath10k_warn(ar, "failed to disable pktlog: %d\n", ret);
2076 goto out;
2077 }
2078 }
2079
2080 ar->debug.pktlog_filter = filter;
2081 ret = count;
2082
2083out:
2084 mutex_unlock(&ar->conf_mutex);
2085 return ret;
2086}
2087
2088static ssize_t ath10k_read_pktlog_filter(struct file *file, char __user *ubuf,
2089 size_t count, loff_t *ppos)
2090{
2091 char buf[32];
2092 struct ath10k *ar = file->private_data;
2093 int len = 0;
2094
2095 mutex_lock(&ar->conf_mutex);
2096 len = scnprintf(buf, sizeof(buf) - len, "%08x\n",
2097 ar->debug.pktlog_filter);
2098 mutex_unlock(&ar->conf_mutex);
2099
2100 return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2101}
2102
2103static const struct file_operations fops_pktlog_filter = {
2104 .read = ath10k_read_pktlog_filter,
2105 .write = ath10k_write_pktlog_filter,
2106 .open = simple_open
2107};
2108
Rajkumar Manoharan63fb32d2015-03-15 20:36:20 +05302109static ssize_t ath10k_write_quiet_period(struct file *file,
2110 const char __user *ubuf,
2111 size_t count, loff_t *ppos)
2112{
2113 struct ath10k *ar = file->private_data;
2114 u32 period;
2115
2116 if (kstrtouint_from_user(ubuf, count, 0, &period))
2117 return -EINVAL;
2118
2119 if (period < ATH10K_QUIET_PERIOD_MIN) {
2120 ath10k_warn(ar, "Quiet period %u can not be lesser than 25ms\n",
2121 period);
2122 return -EINVAL;
2123 }
2124 mutex_lock(&ar->conf_mutex);
2125 ar->thermal.quiet_period = period;
Rajkumar Manoharan8515b5c2015-03-15 20:36:22 +05302126 ath10k_thermal_set_throttling(ar);
Rajkumar Manoharan63fb32d2015-03-15 20:36:20 +05302127 mutex_unlock(&ar->conf_mutex);
2128
2129 return count;
2130}
2131
2132static ssize_t ath10k_read_quiet_period(struct file *file, char __user *ubuf,
2133 size_t count, loff_t *ppos)
2134{
2135 char buf[32];
2136 struct ath10k *ar = file->private_data;
2137 int len = 0;
2138
2139 mutex_lock(&ar->conf_mutex);
2140 len = scnprintf(buf, sizeof(buf) - len, "%d\n",
2141 ar->thermal.quiet_period);
2142 mutex_unlock(&ar->conf_mutex);
2143
2144 return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2145}
2146
2147static const struct file_operations fops_quiet_period = {
2148 .read = ath10k_read_quiet_period,
2149 .write = ath10k_write_quiet_period,
2150 .open = simple_open
2151};
2152
Yanbo Li844fa572015-10-31 11:07:21 +02002153static ssize_t ath10k_write_btcoex(struct file *file,
2154 const char __user *ubuf,
2155 size_t count, loff_t *ppos)
2156{
2157 struct ath10k *ar = file->private_data;
2158 char buf[32];
2159 size_t buf_size;
Mohammed Shafi Shajakhan87be0542016-04-05 20:58:26 +05302160 int ret;
Yanbo Li844fa572015-10-31 11:07:21 +02002161 bool val;
Rajkumar Manoharan39136242016-05-27 20:15:59 +05302162 u32 pdev_param;
Yanbo Li844fa572015-10-31 11:07:21 +02002163
2164 buf_size = min(count, (sizeof(buf) - 1));
2165 if (copy_from_user(buf, ubuf, buf_size))
2166 return -EFAULT;
2167
2168 buf[buf_size] = '\0';
2169
2170 if (strtobool(buf, &val) != 0)
2171 return -EINVAL;
2172
2173 mutex_lock(&ar->conf_mutex);
2174
Mohammed Shafi Shajakhanc28e6f02016-02-23 12:58:36 +05302175 if (ar->state != ATH10K_STATE_ON &&
2176 ar->state != ATH10K_STATE_RESTARTED) {
2177 ret = -ENETDOWN;
2178 goto exit;
2179 }
2180
Mohammed Shafi Shajakhan87be0542016-04-05 20:58:26 +05302181 if (!(test_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags) ^ val)) {
2182 ret = count;
Yanbo Li844fa572015-10-31 11:07:21 +02002183 goto exit;
Mohammed Shafi Shajakhan87be0542016-04-05 20:58:26 +05302184 }
Yanbo Li844fa572015-10-31 11:07:21 +02002185
Rajkumar Manoharan39136242016-05-27 20:15:59 +05302186 pdev_param = ar->wmi.pdev_param->enable_btcoex;
2187 if (test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM,
2188 ar->running_fw->fw_file.fw_features)) {
2189 ret = ath10k_wmi_pdev_set_param(ar, pdev_param, val);
2190 if (ret) {
2191 ath10k_warn(ar, "failed to enable btcoex: %d\n", ret);
2192 ret = count;
2193 goto exit;
2194 }
2195 } else {
2196 ath10k_info(ar, "restarting firmware due to btcoex change");
2197 queue_work(ar->workqueue, &ar->restart_work);
2198 }
2199
Yanbo Li844fa572015-10-31 11:07:21 +02002200 if (val)
2201 set_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
2202 else
2203 clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
2204
Mohammed Shafi Shajakhanc28e6f02016-02-23 12:58:36 +05302205 ret = count;
Yanbo Li844fa572015-10-31 11:07:21 +02002206
2207exit:
2208 mutex_unlock(&ar->conf_mutex);
2209
Mohammed Shafi Shajakhanc28e6f02016-02-23 12:58:36 +05302210 return ret;
Yanbo Li844fa572015-10-31 11:07:21 +02002211}
2212
2213static ssize_t ath10k_read_btcoex(struct file *file, char __user *ubuf,
2214 size_t count, loff_t *ppos)
2215{
2216 char buf[32];
2217 struct ath10k *ar = file->private_data;
2218 int len = 0;
2219
2220 mutex_lock(&ar->conf_mutex);
2221 len = scnprintf(buf, sizeof(buf) - len, "%d\n",
2222 test_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags));
2223 mutex_unlock(&ar->conf_mutex);
2224
2225 return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2226}
2227
2228static const struct file_operations fops_btcoex = {
2229 .read = ath10k_read_btcoex,
2230 .write = ath10k_write_btcoex,
2231 .open = simple_open
2232};
2233
Mohammed Shafi Shajakhancc61a1b2016-03-16 18:13:32 +05302234static ssize_t ath10k_write_peer_stats(struct file *file,
2235 const char __user *ubuf,
2236 size_t count, loff_t *ppos)
2237{
2238 struct ath10k *ar = file->private_data;
2239 char buf[32];
2240 size_t buf_size;
Mohammed Shafi Shajakhan87be0542016-04-05 20:58:26 +05302241 int ret;
Mohammed Shafi Shajakhancc61a1b2016-03-16 18:13:32 +05302242 bool val;
2243
2244 buf_size = min(count, (sizeof(buf) - 1));
2245 if (copy_from_user(buf, ubuf, buf_size))
2246 return -EFAULT;
2247
2248 buf[buf_size] = '\0';
2249
2250 if (strtobool(buf, &val) != 0)
2251 return -EINVAL;
2252
2253 mutex_lock(&ar->conf_mutex);
2254
2255 if (ar->state != ATH10K_STATE_ON &&
2256 ar->state != ATH10K_STATE_RESTARTED) {
2257 ret = -ENETDOWN;
2258 goto exit;
2259 }
2260
Mohammed Shafi Shajakhan87be0542016-04-05 20:58:26 +05302261 if (!(test_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags) ^ val)) {
2262 ret = count;
Mohammed Shafi Shajakhancc61a1b2016-03-16 18:13:32 +05302263 goto exit;
Mohammed Shafi Shajakhan87be0542016-04-05 20:58:26 +05302264 }
Mohammed Shafi Shajakhancc61a1b2016-03-16 18:13:32 +05302265
2266 if (val)
2267 set_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags);
2268 else
2269 clear_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags);
2270
2271 ath10k_info(ar, "restarting firmware due to Peer stats change");
2272
2273 queue_work(ar->workqueue, &ar->restart_work);
2274 ret = count;
2275
2276exit:
2277 mutex_unlock(&ar->conf_mutex);
2278 return ret;
2279}
2280
2281static ssize_t ath10k_read_peer_stats(struct file *file, char __user *ubuf,
2282 size_t count, loff_t *ppos)
2283
2284{
2285 char buf[32];
2286 struct ath10k *ar = file->private_data;
2287 int len = 0;
2288
2289 mutex_lock(&ar->conf_mutex);
2290 len = scnprintf(buf, sizeof(buf) - len, "%d\n",
2291 test_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags));
2292 mutex_unlock(&ar->conf_mutex);
2293
2294 return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2295}
2296
2297static const struct file_operations fops_peer_stats = {
2298 .read = ath10k_read_peer_stats,
2299 .write = ath10k_write_peer_stats,
2300 .open = simple_open
2301};
2302
Kalle Valo9e100c42015-11-25 15:38:41 +02002303static ssize_t ath10k_debug_fw_checksums_read(struct file *file,
2304 char __user *user_buf,
2305 size_t count, loff_t *ppos)
2306{
2307 struct ath10k *ar = file->private_data;
2308 unsigned int len = 0, buf_len = 4096;
2309 ssize_t ret_cnt;
2310 char *buf;
2311
2312 buf = kzalloc(buf_len, GFP_KERNEL);
2313 if (!buf)
2314 return -ENOMEM;
2315
2316 mutex_lock(&ar->conf_mutex);
2317
Kalle Valo9e100c42015-11-25 15:38:41 +02002318 len += scnprintf(buf + len, buf_len - len,
2319 "firmware-N.bin\t\t%08x\n",
Kalle Valo7ebf7212016-04-20 19:44:51 +03002320 crc32_le(0, ar->normal_mode_fw.fw_file.firmware->data,
2321 ar->normal_mode_fw.fw_file.firmware->size));
Kalle Valo9e100c42015-11-25 15:38:41 +02002322 len += scnprintf(buf + len, buf_len - len,
2323 "athwlan\t\t\t%08x\n",
Kalle Valo7ebf7212016-04-20 19:44:51 +03002324 crc32_le(0, ar->normal_mode_fw.fw_file.firmware_data,
2325 ar->normal_mode_fw.fw_file.firmware_len));
Kalle Valo9e100c42015-11-25 15:38:41 +02002326 len += scnprintf(buf + len, buf_len - len,
2327 "otp\t\t\t%08x\n",
Kalle Valo7ebf7212016-04-20 19:44:51 +03002328 crc32_le(0, ar->normal_mode_fw.fw_file.otp_data,
2329 ar->normal_mode_fw.fw_file.otp_len));
Kalle Valo9e100c42015-11-25 15:38:41 +02002330 len += scnprintf(buf + len, buf_len - len,
2331 "codeswap\t\t%08x\n",
Kalle Valo7ebf7212016-04-20 19:44:51 +03002332 crc32_le(0, ar->normal_mode_fw.fw_file.codeswap_data,
2333 ar->normal_mode_fw.fw_file.codeswap_len));
Kalle Valo9e100c42015-11-25 15:38:41 +02002334 len += scnprintf(buf + len, buf_len - len,
2335 "board-N.bin\t\t%08x\n",
Kalle Valo7ebf7212016-04-20 19:44:51 +03002336 crc32_le(0, ar->normal_mode_fw.board->data,
2337 ar->normal_mode_fw.board->size));
Kalle Valo9e100c42015-11-25 15:38:41 +02002338 len += scnprintf(buf + len, buf_len - len,
2339 "board\t\t\t%08x\n",
Kalle Valo7ebf7212016-04-20 19:44:51 +03002340 crc32_le(0, ar->normal_mode_fw.board_data,
2341 ar->normal_mode_fw.board_len));
Kalle Valo9e100c42015-11-25 15:38:41 +02002342
2343 ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
2344
2345 mutex_unlock(&ar->conf_mutex);
2346
2347 kfree(buf);
2348 return ret_cnt;
2349}
2350
2351static const struct file_operations fops_fw_checksums = {
2352 .read = ath10k_debug_fw_checksums_read,
2353 .open = simple_open,
2354 .owner = THIS_MODULE,
2355 .llseek = default_llseek,
2356};
2357
Kalle Valo5e3dd152013-06-12 20:52:10 +03002358int ath10k_debug_create(struct ath10k *ar)
2359{
Ben Greear384914b2014-08-25 08:37:32 +03002360 ar->debug.fw_crash_data = vzalloc(sizeof(*ar->debug.fw_crash_data));
Michal Kaziore13cf7a2014-09-04 09:13:08 +02002361 if (!ar->debug.fw_crash_data)
2362 return -ENOMEM;
Ben Greear384914b2014-08-25 08:37:32 +03002363
Marty Faltesekf67b107d2016-10-10 19:00:04 +03002364 ar->debug.cal_data = vzalloc(ATH10K_DEBUG_CAL_DATA_LEN);
2365 if (!ar->debug.cal_data)
2366 return -ENOMEM;
2367
Michal Kazior53268492014-09-25 12:33:50 +02002368 INIT_LIST_HEAD(&ar->debug.fw_stats.pdevs);
Michal Kazior7b6b1532015-02-15 16:50:40 +02002369 INIT_LIST_HEAD(&ar->debug.fw_stats.vdevs);
Michal Kazior53268492014-09-25 12:33:50 +02002370 INIT_LIST_HEAD(&ar->debug.fw_stats.peers);
Mohammed Shafi Shajakhan4a49ae92016-06-30 15:23:47 +03002371 INIT_LIST_HEAD(&ar->debug.fw_stats.peers_extd);
Michal Kazior53268492014-09-25 12:33:50 +02002372
Michal Kaziore13cf7a2014-09-04 09:13:08 +02002373 return 0;
2374}
2375
2376void ath10k_debug_destroy(struct ath10k *ar)
2377{
2378 vfree(ar->debug.fw_crash_data);
2379 ar->debug.fw_crash_data = NULL;
Michal Kazior53268492014-09-25 12:33:50 +02002380
Marty Faltesekf67b107d2016-10-10 19:00:04 +03002381 vfree(ar->debug.cal_data);
2382 ar->debug.cal_data = NULL;
2383
Michal Kazior53268492014-09-25 12:33:50 +02002384 ath10k_debug_fw_stats_reset(ar);
Maharaja Kennadyrajan29542662015-10-05 17:56:38 +03002385
2386 kfree(ar->debug.tpc_stats);
Michal Kaziore13cf7a2014-09-04 09:13:08 +02002387}
2388
2389int ath10k_debug_register(struct ath10k *ar)
2390{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002391 ar->debug.debugfs_phy = debugfs_create_dir("ath10k",
2392 ar->hw->wiphy->debugfsdir);
Michal Kazioradb43b22014-09-04 12:36:45 +02002393 if (IS_ERR_OR_NULL(ar->debug.debugfs_phy)) {
2394 if (IS_ERR(ar->debug.debugfs_phy))
2395 return PTR_ERR(ar->debug.debugfs_phy);
Kalle Valod8bb26b2014-09-14 12:50:33 +03002396
2397 return -ENOMEM;
Michal Kazioradb43b22014-09-04 12:36:45 +02002398 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002399
Kalle Valoa3d135e2013-09-03 11:44:10 +03002400 INIT_DELAYED_WORK(&ar->debug.htt_stats_dwork,
2401 ath10k_debug_htt_stats_dwork);
2402
Maharaja Kennadyrajan29542662015-10-05 17:56:38 +03002403 init_completion(&ar->debug.tpc_complete);
Michal Kazior60ef4012014-09-25 12:33:48 +02002404 init_completion(&ar->debug.fw_stats_complete);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002405
2406 debugfs_create_file("fw_stats", S_IRUSR, ar->debug.debugfs_phy, ar,
2407 &fops_fw_stats);
2408
Ben Greearf51dbe72014-09-29 14:41:46 +03002409 debugfs_create_file("fw_reset_stats", S_IRUSR, ar->debug.debugfs_phy,
2410 ar, &fops_fw_reset_stats);
2411
Kalle Valo5e3dd152013-06-12 20:52:10 +03002412 debugfs_create_file("wmi_services", S_IRUSR, ar->debug.debugfs_phy, ar,
2413 &fops_wmi_services);
2414
Mohammed Shafi Shajakhan8bf1ba12015-11-24 22:26:37 +05302415 debugfs_create_file("simulate_fw_crash", S_IRUSR | S_IWUSR,
2416 ar->debug.debugfs_phy, ar, &fops_simulate_fw_crash);
Michal Kazior278c4a82013-07-22 14:08:51 +02002417
Ben Greear384914b2014-08-25 08:37:32 +03002418 debugfs_create_file("fw_crash_dump", S_IRUSR, ar->debug.debugfs_phy,
2419 ar, &fops_fw_crash_dump);
2420
Yanbo Li077a3802014-11-25 12:24:33 +02002421 debugfs_create_file("reg_addr", S_IRUSR | S_IWUSR,
2422 ar->debug.debugfs_phy, ar, &fops_reg_addr);
2423
2424 debugfs_create_file("reg_value", S_IRUSR | S_IWUSR,
2425 ar->debug.debugfs_phy, ar, &fops_reg_value);
2426
Yanbo Li9f65ad22014-11-25 12:24:48 +02002427 debugfs_create_file("mem_value", S_IRUSR | S_IWUSR,
2428 ar->debug.debugfs_phy, ar, &fops_mem_value);
2429
Kalle Valo763b8cd2013-09-01 11:22:21 +03002430 debugfs_create_file("chip_id", S_IRUSR, ar->debug.debugfs_phy,
2431 ar, &fops_chip_id);
2432
Mohammed Shafi Shajakhan8bf1ba12015-11-24 22:26:37 +05302433 debugfs_create_file("htt_stats_mask", S_IRUSR | S_IWUSR,
2434 ar->debug.debugfs_phy, ar, &fops_htt_stats_mask);
Kalle Valoa3d135e2013-09-03 11:44:10 +03002435
Janusz Dziedzicd3856232014-06-02 21:19:46 +03002436 debugfs_create_file("htt_max_amsdu_ampdu", S_IRUSR | S_IWUSR,
2437 ar->debug.debugfs_phy, ar,
2438 &fops_htt_max_amsdu_ampdu);
2439
Mohammed Shafi Shajakhan8bf1ba12015-11-24 22:26:37 +05302440 debugfs_create_file("fw_dbglog", S_IRUSR | S_IWUSR,
2441 ar->debug.debugfs_phy, ar, &fops_fw_dbglog);
Kalle Valof118a3e2014-01-03 12:59:31 +02002442
Kalle Valo7869b4f2014-09-24 14:16:58 +03002443 debugfs_create_file("cal_data", S_IRUSR, ar->debug.debugfs_phy,
2444 ar, &fops_cal_data);
2445
Ashok Raj Nagarajanb3e71d72015-03-19 16:38:00 +05302446 debugfs_create_file("ani_enable", S_IRUSR | S_IWUSR,
2447 ar->debug.debugfs_phy, ar, &fops_ani_enable);
2448
Peter Oha7bd3e92014-12-02 13:07:14 +02002449 debugfs_create_file("nf_cal_period", S_IRUSR | S_IWUSR,
2450 ar->debug.debugfs_phy, ar, &fops_nf_cal_period);
2451
Masahiro Yamada97f26452016-08-03 13:45:50 -07002452 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) {
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002453 debugfs_create_file("dfs_simulate_radar", S_IWUSR,
2454 ar->debug.debugfs_phy, ar,
2455 &fops_simulate_radar);
2456
Marek Puzyniak7d9b40b2013-11-20 10:00:28 +02002457 debugfs_create_bool("dfs_block_radar_events", S_IWUSR,
2458 ar->debug.debugfs_phy,
2459 &ar->dfs_block_radar_events);
2460
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002461 debugfs_create_file("dfs_stats", S_IRUSR,
2462 ar->debug.debugfs_phy, ar,
2463 &fops_dfs_stats);
2464 }
2465
Rajkumar Manoharan90174452014-10-03 08:02:33 +03002466 debugfs_create_file("pktlog_filter", S_IRUGO | S_IWUSR,
2467 ar->debug.debugfs_phy, ar, &fops_pktlog_filter);
2468
Rajkumar Manoharan63fb32d2015-03-15 20:36:20 +05302469 debugfs_create_file("quiet_period", S_IRUGO | S_IWUSR,
2470 ar->debug.debugfs_phy, ar, &fops_quiet_period);
2471
Maharaja Kennadyrajan29542662015-10-05 17:56:38 +03002472 debugfs_create_file("tpc_stats", S_IRUSR,
2473 ar->debug.debugfs_phy, ar, &fops_tpc_stats);
2474
Yanbo Li844fa572015-10-31 11:07:21 +02002475 if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map))
2476 debugfs_create_file("btcoex", S_IRUGO | S_IWUSR,
2477 ar->debug.debugfs_phy, ar, &fops_btcoex);
2478
Mohammed Shafi Shajakhancc61a1b2016-03-16 18:13:32 +05302479 if (test_bit(WMI_SERVICE_PEER_STATS, ar->wmi.svc_map))
2480 debugfs_create_file("peer_stats", S_IRUGO | S_IWUSR,
2481 ar->debug.debugfs_phy, ar,
2482 &fops_peer_stats);
2483
Kalle Valo9e100c42015-11-25 15:38:41 +02002484 debugfs_create_file("fw_checksums", S_IRUSR,
2485 ar->debug.debugfs_phy, ar, &fops_fw_checksums);
2486
Kalle Valo5e3dd152013-06-12 20:52:10 +03002487 return 0;
2488}
Kalle Valodb66ea02013-09-03 11:44:03 +03002489
Michal Kaziore13cf7a2014-09-04 09:13:08 +02002490void ath10k_debug_unregister(struct ath10k *ar)
Kalle Valo60631c52013-10-08 21:45:25 +03002491{
2492 cancel_delayed_work_sync(&ar->debug.htt_stats_dwork);
2493}
2494
Kalle Valo5e3dd152013-06-12 20:52:10 +03002495#endif /* CONFIG_ATH10K_DEBUGFS */
2496
2497#ifdef CONFIG_ATH10K_DEBUG
Michal Kazior7aa7a722014-08-25 12:09:38 +02002498void ath10k_dbg(struct ath10k *ar, enum ath10k_debug_mask mask,
2499 const char *fmt, ...)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002500{
2501 struct va_format vaf;
2502 va_list args;
2503
2504 va_start(args, fmt);
2505
2506 vaf.fmt = fmt;
2507 vaf.va = &args;
2508
2509 if (ath10k_debug_mask & mask)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002510 dev_printk(KERN_DEBUG, ar->dev, "%pV", &vaf);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002511
Michal Kaziord35a6c12014-09-02 11:00:21 +03002512 trace_ath10k_log_dbg(ar, mask, &vaf);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002513
2514 va_end(args);
2515}
2516EXPORT_SYMBOL(ath10k_dbg);
2517
Michal Kazior7aa7a722014-08-25 12:09:38 +02002518void ath10k_dbg_dump(struct ath10k *ar,
2519 enum ath10k_debug_mask mask,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002520 const char *msg, const char *prefix,
2521 const void *buf, size_t len)
2522{
Michal Kazior45724a82014-09-23 10:22:53 +02002523 char linebuf[256];
2524 unsigned int linebuflen;
2525 const void *ptr;
2526
Kalle Valo5e3dd152013-06-12 20:52:10 +03002527 if (ath10k_debug_mask & mask) {
2528 if (msg)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002529 ath10k_dbg(ar, mask, "%s\n", msg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002530
Michal Kazior45724a82014-09-23 10:22:53 +02002531 for (ptr = buf; (ptr - buf) < len; ptr += 16) {
2532 linebuflen = 0;
2533 linebuflen += scnprintf(linebuf + linebuflen,
2534 sizeof(linebuf) - linebuflen,
2535 "%s%08x: ",
2536 (prefix ? prefix : ""),
2537 (unsigned int)(ptr - buf));
2538 hex_dump_to_buffer(ptr, len - (ptr - buf), 16, 1,
2539 linebuf + linebuflen,
2540 sizeof(linebuf) - linebuflen, true);
2541 dev_printk(KERN_DEBUG, ar->dev, "%s\n", linebuf);
2542 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002543 }
2544
2545 /* tracing code doesn't like null strings :/ */
Michal Kaziord35a6c12014-09-02 11:00:21 +03002546 trace_ath10k_log_dbg_dump(ar, msg ? msg : "", prefix ? prefix : "",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002547 buf, len);
2548}
2549EXPORT_SYMBOL(ath10k_dbg_dump);
2550
2551#endif /* CONFIG_ATH10K_DEBUG */