Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / net / wireless / ath / ath10k / debug.c
blob8f0fd41dfd4b4a5c29582589244b179be50b759c
1 /*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <linux/module.h>
19 #include <linux/debugfs.h>
20 #include <linux/vmalloc.h>
21 #include <linux/utsname.h>
22 #include <linux/crc32.h>
23 #include <linux/firmware.h>
25 #include "core.h"
26 #include "debug.h"
27 #include "hif.h"
28 #include "wmi-ops.h"
30 /* ms */
31 #define ATH10K_DEBUG_HTT_STATS_INTERVAL 1000
33 #define ATH10K_FW_CRASH_DUMP_VERSION 1
35 /**
36 * enum ath10k_fw_crash_dump_type - types of data in the dump file
37 * @ATH10K_FW_CRASH_DUMP_REGDUMP: Register crash dump in binary format
39 enum ath10k_fw_crash_dump_type {
40 ATH10K_FW_CRASH_DUMP_REGISTERS = 0,
42 ATH10K_FW_CRASH_DUMP_MAX,
45 struct ath10k_tlv_dump_data {
46 /* see ath10k_fw_crash_dump_type above */
47 __le32 type;
49 /* in bytes */
50 __le32 tlv_len;
52 /* pad to 32-bit boundaries as needed */
53 u8 tlv_data[];
54 } __packed;
56 struct ath10k_dump_file_data {
57 /* dump file information */
59 /* "ATH10K-FW-DUMP" */
60 char df_magic[16];
62 __le32 len;
64 /* file dump version */
65 __le32 version;
67 /* some info we can get from ath10k struct that might help */
69 u8 uuid[16];
71 __le32 chip_id;
73 /* 0 for now, in place for later hardware */
74 __le32 bus_type;
76 __le32 target_version;
77 __le32 fw_version_major;
78 __le32 fw_version_minor;
79 __le32 fw_version_release;
80 __le32 fw_version_build;
81 __le32 phy_capability;
82 __le32 hw_min_tx_power;
83 __le32 hw_max_tx_power;
84 __le32 ht_cap_info;
85 __le32 vht_cap_info;
86 __le32 num_rf_chains;
88 /* firmware version string */
89 char fw_ver[ETHTOOL_FWVERS_LEN];
91 /* Kernel related information */
93 /* time-of-day stamp */
94 __le64 tv_sec;
96 /* time-of-day stamp, nano-seconds */
97 __le64 tv_nsec;
99 /* LINUX_VERSION_CODE */
100 __le32 kernel_ver_code;
102 /* VERMAGIC_STRING */
103 char kernel_ver[64];
105 /* room for growth w/out changing binary format */
106 u8 unused[128];
108 /* struct ath10k_tlv_dump_data + more */
109 u8 data[0];
110 } __packed;
112 void ath10k_info(struct ath10k *ar, const char *fmt, ...)
114 struct va_format vaf = {
115 .fmt = fmt,
117 va_list args;
119 va_start(args, fmt);
120 vaf.va = &args;
121 dev_info(ar->dev, "%pV", &vaf);
122 trace_ath10k_log_info(ar, &vaf);
123 va_end(args);
125 EXPORT_SYMBOL(ath10k_info);
127 void ath10k_debug_print_hwfw_info(struct ath10k *ar)
129 const struct firmware *firmware;
130 char fw_features[128] = {};
131 u32 crc = 0;
133 ath10k_core_get_fw_features_str(ar, fw_features, sizeof(fw_features));
135 ath10k_info(ar, "%s target 0x%08x chip_id 0x%08x sub %04x:%04x",
136 ar->hw_params.name,
137 ar->target_version,
138 ar->chip_id,
139 ar->id.subsystem_vendor, ar->id.subsystem_device);
141 ath10k_info(ar, "kconfig debug %d debugfs %d tracing %d dfs %d testmode %d\n",
142 IS_ENABLED(CONFIG_ATH10K_DEBUG),
143 IS_ENABLED(CONFIG_ATH10K_DEBUGFS),
144 IS_ENABLED(CONFIG_ATH10K_TRACING),
145 IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED),
146 IS_ENABLED(CONFIG_NL80211_TESTMODE));
148 firmware = ar->normal_mode_fw.fw_file.firmware;
149 if (firmware)
150 crc = crc32_le(0, firmware->data, firmware->size);
152 ath10k_info(ar, "firmware ver %s api %d features %s crc32 %08x\n",
153 ar->hw->wiphy->fw_version,
154 ar->fw_api,
155 fw_features,
156 crc);
159 void ath10k_debug_print_board_info(struct ath10k *ar)
161 char boardinfo[100];
163 if (ar->id.bmi_ids_valid)
164 scnprintf(boardinfo, sizeof(boardinfo), "%d:%d",
165 ar->id.bmi_chip_id, ar->id.bmi_board_id);
166 else
167 scnprintf(boardinfo, sizeof(boardinfo), "N/A");
169 ath10k_info(ar, "board_file api %d bmi_id %s crc32 %08x",
170 ar->bd_api,
171 boardinfo,
172 crc32_le(0, ar->normal_mode_fw.board->data,
173 ar->normal_mode_fw.board->size));
176 void ath10k_debug_print_boot_info(struct ath10k *ar)
178 ath10k_info(ar, "htt-ver %d.%d wmi-op %d htt-op %d cal %s max-sta %d raw %d hwcrypto %d\n",
179 ar->htt.target_version_major,
180 ar->htt.target_version_minor,
181 ar->normal_mode_fw.fw_file.wmi_op_version,
182 ar->normal_mode_fw.fw_file.htt_op_version,
183 ath10k_cal_mode_str(ar->cal_mode),
184 ar->max_num_stations,
185 test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags),
186 !test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags));
189 void ath10k_print_driver_info(struct ath10k *ar)
191 ath10k_debug_print_hwfw_info(ar);
192 ath10k_debug_print_board_info(ar);
193 ath10k_debug_print_boot_info(ar);
195 EXPORT_SYMBOL(ath10k_print_driver_info);
197 void ath10k_err(struct ath10k *ar, const char *fmt, ...)
199 struct va_format vaf = {
200 .fmt = fmt,
202 va_list args;
204 va_start(args, fmt);
205 vaf.va = &args;
206 dev_err(ar->dev, "%pV", &vaf);
207 trace_ath10k_log_err(ar, &vaf);
208 va_end(args);
210 EXPORT_SYMBOL(ath10k_err);
212 void ath10k_warn(struct ath10k *ar, const char *fmt, ...)
214 struct va_format vaf = {
215 .fmt = fmt,
217 va_list args;
219 va_start(args, fmt);
220 vaf.va = &args;
221 dev_warn_ratelimited(ar->dev, "%pV", &vaf);
222 trace_ath10k_log_warn(ar, &vaf);
224 va_end(args);
226 EXPORT_SYMBOL(ath10k_warn);
228 #ifdef CONFIG_ATH10K_DEBUGFS
230 static ssize_t ath10k_read_wmi_services(struct file *file,
231 char __user *user_buf,
232 size_t count, loff_t *ppos)
234 struct ath10k *ar = file->private_data;
235 char *buf;
236 unsigned int len = 0, buf_len = 4096;
237 const char *name;
238 ssize_t ret_cnt;
239 bool enabled;
240 int i;
242 buf = kzalloc(buf_len, GFP_KERNEL);
243 if (!buf)
244 return -ENOMEM;
246 mutex_lock(&ar->conf_mutex);
248 if (len > buf_len)
249 len = buf_len;
251 spin_lock_bh(&ar->data_lock);
252 for (i = 0; i < WMI_SERVICE_MAX; i++) {
253 enabled = test_bit(i, ar->wmi.svc_map);
254 name = wmi_service_name(i);
256 if (!name) {
257 if (enabled)
258 len += scnprintf(buf + len, buf_len - len,
259 "%-40s %s (bit %d)\n",
260 "unknown", "enabled", i);
262 continue;
265 len += scnprintf(buf + len, buf_len - len,
266 "%-40s %s\n",
267 name, enabled ? "enabled" : "-");
269 spin_unlock_bh(&ar->data_lock);
271 ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
273 mutex_unlock(&ar->conf_mutex);
275 kfree(buf);
276 return ret_cnt;
279 static const struct file_operations fops_wmi_services = {
280 .read = ath10k_read_wmi_services,
281 .open = simple_open,
282 .owner = THIS_MODULE,
283 .llseek = default_llseek,
286 static void ath10k_fw_stats_pdevs_free(struct list_head *head)
288 struct ath10k_fw_stats_pdev *i, *tmp;
290 list_for_each_entry_safe(i, tmp, head, list) {
291 list_del(&i->list);
292 kfree(i);
296 static void ath10k_fw_stats_vdevs_free(struct list_head *head)
298 struct ath10k_fw_stats_vdev *i, *tmp;
300 list_for_each_entry_safe(i, tmp, head, list) {
301 list_del(&i->list);
302 kfree(i);
306 static void ath10k_fw_stats_peers_free(struct list_head *head)
308 struct ath10k_fw_stats_peer *i, *tmp;
310 list_for_each_entry_safe(i, tmp, head, list) {
311 list_del(&i->list);
312 kfree(i);
316 static void ath10k_fw_extd_stats_peers_free(struct list_head *head)
318 struct ath10k_fw_extd_stats_peer *i, *tmp;
320 list_for_each_entry_safe(i, tmp, head, list) {
321 list_del(&i->list);
322 kfree(i);
326 static void ath10k_debug_fw_stats_reset(struct ath10k *ar)
328 spin_lock_bh(&ar->data_lock);
329 ar->debug.fw_stats_done = false;
330 ar->debug.fw_stats.extended = false;
331 ath10k_fw_stats_pdevs_free(&ar->debug.fw_stats.pdevs);
332 ath10k_fw_stats_vdevs_free(&ar->debug.fw_stats.vdevs);
333 ath10k_fw_stats_peers_free(&ar->debug.fw_stats.peers);
334 ath10k_fw_extd_stats_peers_free(&ar->debug.fw_stats.peers_extd);
335 spin_unlock_bh(&ar->data_lock);
338 void ath10k_debug_fw_stats_process(struct ath10k *ar, struct sk_buff *skb)
340 struct ath10k_fw_stats stats = {};
341 bool is_start, is_started, is_end;
342 size_t num_peers;
343 size_t num_vdevs;
344 int ret;
346 INIT_LIST_HEAD(&stats.pdevs);
347 INIT_LIST_HEAD(&stats.vdevs);
348 INIT_LIST_HEAD(&stats.peers);
349 INIT_LIST_HEAD(&stats.peers_extd);
351 spin_lock_bh(&ar->data_lock);
352 ret = ath10k_wmi_pull_fw_stats(ar, skb, &stats);
353 if (ret) {
354 ath10k_warn(ar, "failed to pull fw stats: %d\n", ret);
355 goto free;
358 /* Stat data may exceed htc-wmi buffer limit. In such case firmware
359 * splits the stats data and delivers it in a ping-pong fashion of
360 * request cmd-update event.
362 * However there is no explicit end-of-data. Instead start-of-data is
363 * used as an implicit one. This works as follows:
364 * a) discard stat update events until one with pdev stats is
365 * delivered - this skips session started at end of (b)
366 * b) consume stat update events until another one with pdev stats is
367 * delivered which is treated as end-of-data and is itself discarded
369 if (ath10k_peer_stats_enabled(ar))
370 ath10k_sta_update_rx_duration(ar, &stats);
372 if (ar->debug.fw_stats_done) {
373 if (!ath10k_peer_stats_enabled(ar))
374 ath10k_warn(ar, "received unsolicited stats update event\n");
376 goto free;
379 num_peers = ath10k_wmi_fw_stats_num_peers(&ar->debug.fw_stats.peers);
380 num_vdevs = ath10k_wmi_fw_stats_num_vdevs(&ar->debug.fw_stats.vdevs);
381 is_start = (list_empty(&ar->debug.fw_stats.pdevs) &&
382 !list_empty(&stats.pdevs));
383 is_end = (!list_empty(&ar->debug.fw_stats.pdevs) &&
384 !list_empty(&stats.pdevs));
386 if (is_start)
387 list_splice_tail_init(&stats.pdevs, &ar->debug.fw_stats.pdevs);
389 if (is_end)
390 ar->debug.fw_stats_done = true;
392 is_started = !list_empty(&ar->debug.fw_stats.pdevs);
394 if (is_started && !is_end) {
395 if (num_peers >= ATH10K_MAX_NUM_PEER_IDS) {
396 /* Although this is unlikely impose a sane limit to
397 * prevent firmware from DoS-ing the host.
399 ath10k_fw_stats_peers_free(&ar->debug.fw_stats.peers);
400 ath10k_warn(ar, "dropping fw peer stats\n");
401 goto free;
404 if (num_vdevs >= BITS_PER_LONG) {
405 ath10k_fw_stats_vdevs_free(&ar->debug.fw_stats.vdevs);
406 ath10k_warn(ar, "dropping fw vdev stats\n");
407 goto free;
410 list_splice_tail_init(&stats.peers, &ar->debug.fw_stats.peers);
411 list_splice_tail_init(&stats.vdevs, &ar->debug.fw_stats.vdevs);
412 list_splice_tail_init(&stats.peers_extd,
413 &ar->debug.fw_stats.peers_extd);
416 complete(&ar->debug.fw_stats_complete);
418 free:
419 /* In some cases lists have been spliced and cleared. Free up
420 * resources if that is not the case.
422 ath10k_fw_stats_pdevs_free(&stats.pdevs);
423 ath10k_fw_stats_vdevs_free(&stats.vdevs);
424 ath10k_fw_stats_peers_free(&stats.peers);
425 ath10k_fw_extd_stats_peers_free(&stats.peers_extd);
427 spin_unlock_bh(&ar->data_lock);
430 static int ath10k_debug_fw_stats_request(struct ath10k *ar)
432 unsigned long timeout, time_left;
433 int ret;
435 lockdep_assert_held(&ar->conf_mutex);
437 timeout = jiffies + msecs_to_jiffies(1 * HZ);
439 ath10k_debug_fw_stats_reset(ar);
441 for (;;) {
442 if (time_after(jiffies, timeout))
443 return -ETIMEDOUT;
445 reinit_completion(&ar->debug.fw_stats_complete);
447 ret = ath10k_wmi_request_stats(ar, ar->fw_stats_req_mask);
448 if (ret) {
449 ath10k_warn(ar, "could not request stats (%d)\n", ret);
450 return ret;
453 time_left =
454 wait_for_completion_timeout(&ar->debug.fw_stats_complete,
455 1 * HZ);
456 if (!time_left)
457 return -ETIMEDOUT;
459 spin_lock_bh(&ar->data_lock);
460 if (ar->debug.fw_stats_done) {
461 spin_unlock_bh(&ar->data_lock);
462 break;
464 spin_unlock_bh(&ar->data_lock);
467 return 0;
470 static int ath10k_fw_stats_open(struct inode *inode, struct file *file)
472 struct ath10k *ar = inode->i_private;
473 void *buf = NULL;
474 int ret;
476 mutex_lock(&ar->conf_mutex);
478 if (ar->state != ATH10K_STATE_ON) {
479 ret = -ENETDOWN;
480 goto err_unlock;
483 buf = vmalloc(ATH10K_FW_STATS_BUF_SIZE);
484 if (!buf) {
485 ret = -ENOMEM;
486 goto err_unlock;
489 ret = ath10k_debug_fw_stats_request(ar);
490 if (ret) {
491 ath10k_warn(ar, "failed to request fw stats: %d\n", ret);
492 goto err_free;
495 ret = ath10k_wmi_fw_stats_fill(ar, &ar->debug.fw_stats, buf);
496 if (ret) {
497 ath10k_warn(ar, "failed to fill fw stats: %d\n", ret);
498 goto err_free;
501 file->private_data = buf;
503 mutex_unlock(&ar->conf_mutex);
504 return 0;
506 err_free:
507 vfree(buf);
509 err_unlock:
510 mutex_unlock(&ar->conf_mutex);
511 return ret;
514 static int ath10k_fw_stats_release(struct inode *inode, struct file *file)
516 vfree(file->private_data);
518 return 0;
521 static ssize_t ath10k_fw_stats_read(struct file *file, char __user *user_buf,
522 size_t count, loff_t *ppos)
524 const char *buf = file->private_data;
525 unsigned int len = strlen(buf);
527 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
530 static const struct file_operations fops_fw_stats = {
531 .open = ath10k_fw_stats_open,
532 .release = ath10k_fw_stats_release,
533 .read = ath10k_fw_stats_read,
534 .owner = THIS_MODULE,
535 .llseek = default_llseek,
538 static ssize_t ath10k_debug_fw_reset_stats_read(struct file *file,
539 char __user *user_buf,
540 size_t count, loff_t *ppos)
542 struct ath10k *ar = file->private_data;
543 int ret, len, buf_len;
544 char *buf;
546 buf_len = 500;
547 buf = kmalloc(buf_len, GFP_KERNEL);
548 if (!buf)
549 return -ENOMEM;
551 spin_lock_bh(&ar->data_lock);
553 len = 0;
554 len += scnprintf(buf + len, buf_len - len,
555 "fw_crash_counter\t\t%d\n", ar->stats.fw_crash_counter);
556 len += scnprintf(buf + len, buf_len - len,
557 "fw_warm_reset_counter\t\t%d\n",
558 ar->stats.fw_warm_reset_counter);
559 len += scnprintf(buf + len, buf_len - len,
560 "fw_cold_reset_counter\t\t%d\n",
561 ar->stats.fw_cold_reset_counter);
563 spin_unlock_bh(&ar->data_lock);
565 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
567 kfree(buf);
569 return ret;
572 static const struct file_operations fops_fw_reset_stats = {
573 .open = simple_open,
574 .read = ath10k_debug_fw_reset_stats_read,
575 .owner = THIS_MODULE,
576 .llseek = default_llseek,
579 /* This is a clean assert crash in firmware. */
580 static int ath10k_debug_fw_assert(struct ath10k *ar)
582 struct wmi_vdev_install_key_cmd *cmd;
583 struct sk_buff *skb;
585 skb = ath10k_wmi_alloc_skb(ar, sizeof(*cmd) + 16);
586 if (!skb)
587 return -ENOMEM;
589 cmd = (struct wmi_vdev_install_key_cmd *)skb->data;
590 memset(cmd, 0, sizeof(*cmd));
592 /* big enough number so that firmware asserts */
593 cmd->vdev_id = __cpu_to_le32(0x7ffe);
595 return ath10k_wmi_cmd_send(ar, skb,
596 ar->wmi.cmd->vdev_install_key_cmdid);
599 static ssize_t ath10k_read_simulate_fw_crash(struct file *file,
600 char __user *user_buf,
601 size_t count, loff_t *ppos)
603 const char buf[] =
604 "To simulate firmware crash write one of the keywords to this file:\n"
605 "`soft` - this will send WMI_FORCE_FW_HANG_ASSERT to firmware if FW supports that command.\n"
606 "`hard` - this will send to firmware command with illegal parameters causing firmware crash.\n"
607 "`assert` - this will send special illegal parameter to firmware to cause assert failure and crash.\n"
608 "`hw-restart` - this will simply queue hw restart without fw/hw actually crashing.\n";
610 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
613 /* Simulate firmware crash:
614 * 'soft': Call wmi command causing firmware hang. This firmware hang is
615 * recoverable by warm firmware reset.
616 * 'hard': Force firmware crash by setting any vdev parameter for not allowed
617 * vdev id. This is hard firmware crash because it is recoverable only by cold
618 * firmware reset.
620 static ssize_t ath10k_write_simulate_fw_crash(struct file *file,
621 const char __user *user_buf,
622 size_t count, loff_t *ppos)
624 struct ath10k *ar = file->private_data;
625 char buf[32];
626 int ret;
628 simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
630 /* make sure that buf is null terminated */
631 buf[sizeof(buf) - 1] = 0;
633 /* drop the possible '\n' from the end */
634 if (buf[count - 1] == '\n')
635 buf[count - 1] = 0;
637 mutex_lock(&ar->conf_mutex);
639 if (ar->state != ATH10K_STATE_ON &&
640 ar->state != ATH10K_STATE_RESTARTED) {
641 ret = -ENETDOWN;
642 goto exit;
645 if (!strcmp(buf, "soft")) {
646 ath10k_info(ar, "simulating soft firmware crash\n");
647 ret = ath10k_wmi_force_fw_hang(ar, WMI_FORCE_FW_HANG_ASSERT, 0);
648 } else if (!strcmp(buf, "hard")) {
649 ath10k_info(ar, "simulating hard firmware crash\n");
650 /* 0x7fff is vdev id, and it is always out of range for all
651 * firmware variants in order to force a firmware crash.
653 ret = ath10k_wmi_vdev_set_param(ar, 0x7fff,
654 ar->wmi.vdev_param->rts_threshold,
656 } else if (!strcmp(buf, "assert")) {
657 ath10k_info(ar, "simulating firmware assert crash\n");
658 ret = ath10k_debug_fw_assert(ar);
659 } else if (!strcmp(buf, "hw-restart")) {
660 ath10k_info(ar, "user requested hw restart\n");
661 queue_work(ar->workqueue, &ar->restart_work);
662 ret = 0;
663 } else {
664 ret = -EINVAL;
665 goto exit;
668 if (ret) {
669 ath10k_warn(ar, "failed to simulate firmware crash: %d\n", ret);
670 goto exit;
673 ret = count;
675 exit:
676 mutex_unlock(&ar->conf_mutex);
677 return ret;
680 static const struct file_operations fops_simulate_fw_crash = {
681 .read = ath10k_read_simulate_fw_crash,
682 .write = ath10k_write_simulate_fw_crash,
683 .open = simple_open,
684 .owner = THIS_MODULE,
685 .llseek = default_llseek,
688 static ssize_t ath10k_read_chip_id(struct file *file, char __user *user_buf,
689 size_t count, loff_t *ppos)
691 struct ath10k *ar = file->private_data;
692 unsigned int len;
693 char buf[50];
695 len = scnprintf(buf, sizeof(buf), "0x%08x\n", ar->chip_id);
697 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
700 static const struct file_operations fops_chip_id = {
701 .read = ath10k_read_chip_id,
702 .open = simple_open,
703 .owner = THIS_MODULE,
704 .llseek = default_llseek,
707 struct ath10k_fw_crash_data *
708 ath10k_debug_get_new_fw_crash_data(struct ath10k *ar)
710 struct ath10k_fw_crash_data *crash_data = ar->debug.fw_crash_data;
712 lockdep_assert_held(&ar->data_lock);
714 crash_data->crashed_since_read = true;
715 uuid_le_gen(&crash_data->uuid);
716 getnstimeofday(&crash_data->timestamp);
718 return crash_data;
720 EXPORT_SYMBOL(ath10k_debug_get_new_fw_crash_data);
722 static struct ath10k_dump_file_data *ath10k_build_dump_file(struct ath10k *ar)
724 struct ath10k_fw_crash_data *crash_data = ar->debug.fw_crash_data;
725 struct ath10k_dump_file_data *dump_data;
726 struct ath10k_tlv_dump_data *dump_tlv;
727 int hdr_len = sizeof(*dump_data);
728 unsigned int len, sofar = 0;
729 unsigned char *buf;
731 len = hdr_len;
732 len += sizeof(*dump_tlv) + sizeof(crash_data->registers);
734 sofar += hdr_len;
736 /* This is going to get big when we start dumping FW RAM and such,
737 * so go ahead and use vmalloc.
739 buf = vzalloc(len);
740 if (!buf)
741 return NULL;
743 spin_lock_bh(&ar->data_lock);
745 if (!crash_data->crashed_since_read) {
746 spin_unlock_bh(&ar->data_lock);
747 vfree(buf);
748 return NULL;
751 dump_data = (struct ath10k_dump_file_data *)(buf);
752 strlcpy(dump_data->df_magic, "ATH10K-FW-DUMP",
753 sizeof(dump_data->df_magic));
754 dump_data->len = cpu_to_le32(len);
756 dump_data->version = cpu_to_le32(ATH10K_FW_CRASH_DUMP_VERSION);
758 memcpy(dump_data->uuid, &crash_data->uuid, sizeof(dump_data->uuid));
759 dump_data->chip_id = cpu_to_le32(ar->chip_id);
760 dump_data->bus_type = cpu_to_le32(0);
761 dump_data->target_version = cpu_to_le32(ar->target_version);
762 dump_data->fw_version_major = cpu_to_le32(ar->fw_version_major);
763 dump_data->fw_version_minor = cpu_to_le32(ar->fw_version_minor);
764 dump_data->fw_version_release = cpu_to_le32(ar->fw_version_release);
765 dump_data->fw_version_build = cpu_to_le32(ar->fw_version_build);
766 dump_data->phy_capability = cpu_to_le32(ar->phy_capability);
767 dump_data->hw_min_tx_power = cpu_to_le32(ar->hw_min_tx_power);
768 dump_data->hw_max_tx_power = cpu_to_le32(ar->hw_max_tx_power);
769 dump_data->ht_cap_info = cpu_to_le32(ar->ht_cap_info);
770 dump_data->vht_cap_info = cpu_to_le32(ar->vht_cap_info);
771 dump_data->num_rf_chains = cpu_to_le32(ar->num_rf_chains);
773 strlcpy(dump_data->fw_ver, ar->hw->wiphy->fw_version,
774 sizeof(dump_data->fw_ver));
776 dump_data->kernel_ver_code = 0;
777 strlcpy(dump_data->kernel_ver, init_utsname()->release,
778 sizeof(dump_data->kernel_ver));
780 dump_data->tv_sec = cpu_to_le64(crash_data->timestamp.tv_sec);
781 dump_data->tv_nsec = cpu_to_le64(crash_data->timestamp.tv_nsec);
783 /* Gather crash-dump */
784 dump_tlv = (struct ath10k_tlv_dump_data *)(buf + sofar);
785 dump_tlv->type = cpu_to_le32(ATH10K_FW_CRASH_DUMP_REGISTERS);
786 dump_tlv->tlv_len = cpu_to_le32(sizeof(crash_data->registers));
787 memcpy(dump_tlv->tlv_data, &crash_data->registers,
788 sizeof(crash_data->registers));
789 sofar += sizeof(*dump_tlv) + sizeof(crash_data->registers);
791 ar->debug.fw_crash_data->crashed_since_read = false;
793 spin_unlock_bh(&ar->data_lock);
795 return dump_data;
798 static int ath10k_fw_crash_dump_open(struct inode *inode, struct file *file)
800 struct ath10k *ar = inode->i_private;
801 struct ath10k_dump_file_data *dump;
803 dump = ath10k_build_dump_file(ar);
804 if (!dump)
805 return -ENODATA;
807 file->private_data = dump;
809 return 0;
812 static ssize_t ath10k_fw_crash_dump_read(struct file *file,
813 char __user *user_buf,
814 size_t count, loff_t *ppos)
816 struct ath10k_dump_file_data *dump_file = file->private_data;
818 return simple_read_from_buffer(user_buf, count, ppos,
819 dump_file,
820 le32_to_cpu(dump_file->len));
823 static int ath10k_fw_crash_dump_release(struct inode *inode,
824 struct file *file)
826 vfree(file->private_data);
828 return 0;
831 static const struct file_operations fops_fw_crash_dump = {
832 .open = ath10k_fw_crash_dump_open,
833 .read = ath10k_fw_crash_dump_read,
834 .release = ath10k_fw_crash_dump_release,
835 .owner = THIS_MODULE,
836 .llseek = default_llseek,
839 static ssize_t ath10k_reg_addr_read(struct file *file,
840 char __user *user_buf,
841 size_t count, loff_t *ppos)
843 struct ath10k *ar = file->private_data;
844 u8 buf[32];
845 unsigned int len = 0;
846 u32 reg_addr;
848 mutex_lock(&ar->conf_mutex);
849 reg_addr = ar->debug.reg_addr;
850 mutex_unlock(&ar->conf_mutex);
852 len += scnprintf(buf + len, sizeof(buf) - len, "0x%x\n", reg_addr);
854 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
857 static ssize_t ath10k_reg_addr_write(struct file *file,
858 const char __user *user_buf,
859 size_t count, loff_t *ppos)
861 struct ath10k *ar = file->private_data;
862 u32 reg_addr;
863 int ret;
865 ret = kstrtou32_from_user(user_buf, count, 0, &reg_addr);
866 if (ret)
867 return ret;
869 if (!IS_ALIGNED(reg_addr, 4))
870 return -EFAULT;
872 mutex_lock(&ar->conf_mutex);
873 ar->debug.reg_addr = reg_addr;
874 mutex_unlock(&ar->conf_mutex);
876 return count;
879 static const struct file_operations fops_reg_addr = {
880 .read = ath10k_reg_addr_read,
881 .write = ath10k_reg_addr_write,
882 .open = simple_open,
883 .owner = THIS_MODULE,
884 .llseek = default_llseek,
887 static ssize_t ath10k_reg_value_read(struct file *file,
888 char __user *user_buf,
889 size_t count, loff_t *ppos)
891 struct ath10k *ar = file->private_data;
892 u8 buf[48];
893 unsigned int len;
894 u32 reg_addr, reg_val;
895 int ret;
897 mutex_lock(&ar->conf_mutex);
899 if (ar->state != ATH10K_STATE_ON &&
900 ar->state != ATH10K_STATE_UTF) {
901 ret = -ENETDOWN;
902 goto exit;
905 reg_addr = ar->debug.reg_addr;
907 reg_val = ath10k_hif_read32(ar, reg_addr);
908 len = scnprintf(buf, sizeof(buf), "0x%08x:0x%08x\n", reg_addr, reg_val);
910 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
912 exit:
913 mutex_unlock(&ar->conf_mutex);
915 return ret;
918 static ssize_t ath10k_reg_value_write(struct file *file,
919 const char __user *user_buf,
920 size_t count, loff_t *ppos)
922 struct ath10k *ar = file->private_data;
923 u32 reg_addr, reg_val;
924 int ret;
926 mutex_lock(&ar->conf_mutex);
928 if (ar->state != ATH10K_STATE_ON &&
929 ar->state != ATH10K_STATE_UTF) {
930 ret = -ENETDOWN;
931 goto exit;
934 reg_addr = ar->debug.reg_addr;
936 ret = kstrtou32_from_user(user_buf, count, 0, &reg_val);
937 if (ret)
938 goto exit;
940 ath10k_hif_write32(ar, reg_addr, reg_val);
942 ret = count;
944 exit:
945 mutex_unlock(&ar->conf_mutex);
947 return ret;
950 static const struct file_operations fops_reg_value = {
951 .read = ath10k_reg_value_read,
952 .write = ath10k_reg_value_write,
953 .open = simple_open,
954 .owner = THIS_MODULE,
955 .llseek = default_llseek,
958 static ssize_t ath10k_mem_value_read(struct file *file,
959 char __user *user_buf,
960 size_t count, loff_t *ppos)
962 struct ath10k *ar = file->private_data;
963 u8 *buf;
964 int ret;
966 if (*ppos < 0)
967 return -EINVAL;
969 if (!count)
970 return 0;
972 mutex_lock(&ar->conf_mutex);
974 buf = vmalloc(count);
975 if (!buf) {
976 ret = -ENOMEM;
977 goto exit;
980 if (ar->state != ATH10K_STATE_ON &&
981 ar->state != ATH10K_STATE_UTF) {
982 ret = -ENETDOWN;
983 goto exit;
986 ret = ath10k_hif_diag_read(ar, *ppos, buf, count);
987 if (ret) {
988 ath10k_warn(ar, "failed to read address 0x%08x via diagnose window fnrom debugfs: %d\n",
989 (u32)(*ppos), ret);
990 goto exit;
993 ret = copy_to_user(user_buf, buf, count);
994 if (ret) {
995 ret = -EFAULT;
996 goto exit;
999 count -= ret;
1000 *ppos += count;
1001 ret = count;
1003 exit:
1004 vfree(buf);
1005 mutex_unlock(&ar->conf_mutex);
1007 return ret;
1010 static ssize_t ath10k_mem_value_write(struct file *file,
1011 const char __user *user_buf,
1012 size_t count, loff_t *ppos)
1014 struct ath10k *ar = file->private_data;
1015 u8 *buf;
1016 int ret;
1018 if (*ppos < 0)
1019 return -EINVAL;
1021 if (!count)
1022 return 0;
1024 mutex_lock(&ar->conf_mutex);
1026 buf = vmalloc(count);
1027 if (!buf) {
1028 ret = -ENOMEM;
1029 goto exit;
1032 if (ar->state != ATH10K_STATE_ON &&
1033 ar->state != ATH10K_STATE_UTF) {
1034 ret = -ENETDOWN;
1035 goto exit;
1038 ret = copy_from_user(buf, user_buf, count);
1039 if (ret) {
1040 ret = -EFAULT;
1041 goto exit;
1044 ret = ath10k_hif_diag_write(ar, *ppos, buf, count);
1045 if (ret) {
1046 ath10k_warn(ar, "failed to write address 0x%08x via diagnose window from debugfs: %d\n",
1047 (u32)(*ppos), ret);
1048 goto exit;
1051 *ppos += count;
1052 ret = count;
1054 exit:
1055 vfree(buf);
1056 mutex_unlock(&ar->conf_mutex);
1058 return ret;
1061 static const struct file_operations fops_mem_value = {
1062 .read = ath10k_mem_value_read,
1063 .write = ath10k_mem_value_write,
1064 .open = simple_open,
1065 .owner = THIS_MODULE,
1066 .llseek = default_llseek,
1069 static int ath10k_debug_htt_stats_req(struct ath10k *ar)
1071 u64 cookie;
1072 int ret;
1074 lockdep_assert_held(&ar->conf_mutex);
1076 if (ar->debug.htt_stats_mask == 0)
1077 /* htt stats are disabled */
1078 return 0;
1080 if (ar->state != ATH10K_STATE_ON)
1081 return 0;
1083 cookie = get_jiffies_64();
1085 ret = ath10k_htt_h2t_stats_req(&ar->htt, ar->debug.htt_stats_mask,
1086 cookie);
1087 if (ret) {
1088 ath10k_warn(ar, "failed to send htt stats request: %d\n", ret);
1089 return ret;
1092 queue_delayed_work(ar->workqueue, &ar->debug.htt_stats_dwork,
1093 msecs_to_jiffies(ATH10K_DEBUG_HTT_STATS_INTERVAL));
1095 return 0;
1098 static void ath10k_debug_htt_stats_dwork(struct work_struct *work)
1100 struct ath10k *ar = container_of(work, struct ath10k,
1101 debug.htt_stats_dwork.work);
1103 mutex_lock(&ar->conf_mutex);
1105 ath10k_debug_htt_stats_req(ar);
1107 mutex_unlock(&ar->conf_mutex);
1110 static ssize_t ath10k_read_htt_stats_mask(struct file *file,
1111 char __user *user_buf,
1112 size_t count, loff_t *ppos)
1114 struct ath10k *ar = file->private_data;
1115 char buf[32];
1116 unsigned int len;
1118 len = scnprintf(buf, sizeof(buf), "%lu\n", ar->debug.htt_stats_mask);
1120 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1123 static ssize_t ath10k_write_htt_stats_mask(struct file *file,
1124 const char __user *user_buf,
1125 size_t count, loff_t *ppos)
1127 struct ath10k *ar = file->private_data;
1128 unsigned long mask;
1129 int ret;
1131 ret = kstrtoul_from_user(user_buf, count, 0, &mask);
1132 if (ret)
1133 return ret;
1135 /* max 8 bit masks (for now) */
1136 if (mask > 0xff)
1137 return -E2BIG;
1139 mutex_lock(&ar->conf_mutex);
1141 ar->debug.htt_stats_mask = mask;
1143 ret = ath10k_debug_htt_stats_req(ar);
1144 if (ret)
1145 goto out;
1147 ret = count;
1149 out:
1150 mutex_unlock(&ar->conf_mutex);
1152 return ret;
1155 static const struct file_operations fops_htt_stats_mask = {
1156 .read = ath10k_read_htt_stats_mask,
1157 .write = ath10k_write_htt_stats_mask,
1158 .open = simple_open,
1159 .owner = THIS_MODULE,
1160 .llseek = default_llseek,
1163 static ssize_t ath10k_read_htt_max_amsdu_ampdu(struct file *file,
1164 char __user *user_buf,
1165 size_t count, loff_t *ppos)
1167 struct ath10k *ar = file->private_data;
1168 char buf[64];
1169 u8 amsdu, ampdu;
1170 unsigned int len;
1172 mutex_lock(&ar->conf_mutex);
1174 amsdu = ar->htt.max_num_amsdu;
1175 ampdu = ar->htt.max_num_ampdu;
1176 mutex_unlock(&ar->conf_mutex);
1178 len = scnprintf(buf, sizeof(buf), "%u %u\n", amsdu, ampdu);
1180 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1183 static ssize_t ath10k_write_htt_max_amsdu_ampdu(struct file *file,
1184 const char __user *user_buf,
1185 size_t count, loff_t *ppos)
1187 struct ath10k *ar = file->private_data;
1188 int res;
1189 char buf[64];
1190 unsigned int amsdu, ampdu;
1192 simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
1194 /* make sure that buf is null terminated */
1195 buf[sizeof(buf) - 1] = 0;
1197 res = sscanf(buf, "%u %u", &amsdu, &ampdu);
1199 if (res != 2)
1200 return -EINVAL;
1202 mutex_lock(&ar->conf_mutex);
1204 res = ath10k_htt_h2t_aggr_cfg_msg(&ar->htt, ampdu, amsdu);
1205 if (res)
1206 goto out;
1208 res = count;
1209 ar->htt.max_num_amsdu = amsdu;
1210 ar->htt.max_num_ampdu = ampdu;
1212 out:
1213 mutex_unlock(&ar->conf_mutex);
1214 return res;
1217 static const struct file_operations fops_htt_max_amsdu_ampdu = {
1218 .read = ath10k_read_htt_max_amsdu_ampdu,
1219 .write = ath10k_write_htt_max_amsdu_ampdu,
1220 .open = simple_open,
1221 .owner = THIS_MODULE,
1222 .llseek = default_llseek,
1225 static ssize_t ath10k_read_fw_dbglog(struct file *file,
1226 char __user *user_buf,
1227 size_t count, loff_t *ppos)
1229 struct ath10k *ar = file->private_data;
1230 unsigned int len;
1231 char buf[64];
1233 len = scnprintf(buf, sizeof(buf), "0x%08x %u\n",
1234 ar->debug.fw_dbglog_mask, ar->debug.fw_dbglog_level);
1236 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1239 static ssize_t ath10k_write_fw_dbglog(struct file *file,
1240 const char __user *user_buf,
1241 size_t count, loff_t *ppos)
1243 struct ath10k *ar = file->private_data;
1244 int ret;
1245 char buf[64];
1246 unsigned int log_level, mask;
1248 simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
1250 /* make sure that buf is null terminated */
1251 buf[sizeof(buf) - 1] = 0;
1253 ret = sscanf(buf, "%x %u", &mask, &log_level);
1255 if (!ret)
1256 return -EINVAL;
1258 if (ret == 1)
1259 /* default if user did not specify */
1260 log_level = ATH10K_DBGLOG_LEVEL_WARN;
1262 mutex_lock(&ar->conf_mutex);
1264 ar->debug.fw_dbglog_mask = mask;
1265 ar->debug.fw_dbglog_level = log_level;
1267 if (ar->state == ATH10K_STATE_ON) {
1268 ret = ath10k_wmi_dbglog_cfg(ar, ar->debug.fw_dbglog_mask,
1269 ar->debug.fw_dbglog_level);
1270 if (ret) {
1271 ath10k_warn(ar, "dbglog cfg failed from debugfs: %d\n",
1272 ret);
1273 goto exit;
1277 ret = count;
1279 exit:
1280 mutex_unlock(&ar->conf_mutex);
1282 return ret;
1285 /* TODO: Would be nice to always support ethtool stats, would need to
1286 * move the stats storage out of ath10k_debug, or always have ath10k_debug
1287 * struct available..
1290 /* This generally cooresponds to the debugfs fw_stats file */
1291 static const char ath10k_gstrings_stats[][ETH_GSTRING_LEN] = {
1292 "tx_pkts_nic",
1293 "tx_bytes_nic",
1294 "rx_pkts_nic",
1295 "rx_bytes_nic",
1296 "d_noise_floor",
1297 "d_cycle_count",
1298 "d_phy_error",
1299 "d_rts_bad",
1300 "d_rts_good",
1301 "d_tx_power", /* in .5 dbM I think */
1302 "d_rx_crc_err", /* fcs_bad */
1303 "d_no_beacon",
1304 "d_tx_mpdus_queued",
1305 "d_tx_msdu_queued",
1306 "d_tx_msdu_dropped",
1307 "d_local_enqued",
1308 "d_local_freed",
1309 "d_tx_ppdu_hw_queued",
1310 "d_tx_ppdu_reaped",
1311 "d_tx_fifo_underrun",
1312 "d_tx_ppdu_abort",
1313 "d_tx_mpdu_requed",
1314 "d_tx_excessive_retries",
1315 "d_tx_hw_rate",
1316 "d_tx_dropped_sw_retries",
1317 "d_tx_illegal_rate",
1318 "d_tx_continuous_xretries",
1319 "d_tx_timeout",
1320 "d_tx_mpdu_txop_limit",
1321 "d_pdev_resets",
1322 "d_rx_mid_ppdu_route_change",
1323 "d_rx_status",
1324 "d_rx_extra_frags_ring0",
1325 "d_rx_extra_frags_ring1",
1326 "d_rx_extra_frags_ring2",
1327 "d_rx_extra_frags_ring3",
1328 "d_rx_msdu_htt",
1329 "d_rx_mpdu_htt",
1330 "d_rx_msdu_stack",
1331 "d_rx_mpdu_stack",
1332 "d_rx_phy_err",
1333 "d_rx_phy_err_drops",
1334 "d_rx_mpdu_errors", /* FCS, MIC, ENC */
1335 "d_fw_crash_count",
1336 "d_fw_warm_reset_count",
1337 "d_fw_cold_reset_count",
1340 #define ATH10K_SSTATS_LEN ARRAY_SIZE(ath10k_gstrings_stats)
1342 void ath10k_debug_get_et_strings(struct ieee80211_hw *hw,
1343 struct ieee80211_vif *vif,
1344 u32 sset, u8 *data)
1346 if (sset == ETH_SS_STATS)
1347 memcpy(data, *ath10k_gstrings_stats,
1348 sizeof(ath10k_gstrings_stats));
1351 int ath10k_debug_get_et_sset_count(struct ieee80211_hw *hw,
1352 struct ieee80211_vif *vif, int sset)
1354 if (sset == ETH_SS_STATS)
1355 return ATH10K_SSTATS_LEN;
1357 return 0;
1360 void ath10k_debug_get_et_stats(struct ieee80211_hw *hw,
1361 struct ieee80211_vif *vif,
1362 struct ethtool_stats *stats, u64 *data)
1364 struct ath10k *ar = hw->priv;
1365 static const struct ath10k_fw_stats_pdev zero_stats = {};
1366 const struct ath10k_fw_stats_pdev *pdev_stats;
1367 int i = 0, ret;
1369 mutex_lock(&ar->conf_mutex);
1371 if (ar->state == ATH10K_STATE_ON) {
1372 ret = ath10k_debug_fw_stats_request(ar);
1373 if (ret) {
1374 /* just print a warning and try to use older results */
1375 ath10k_warn(ar,
1376 "failed to get fw stats for ethtool: %d\n",
1377 ret);
1381 pdev_stats = list_first_entry_or_null(&ar->debug.fw_stats.pdevs,
1382 struct ath10k_fw_stats_pdev,
1383 list);
1384 if (!pdev_stats) {
1385 /* no results available so just return zeroes */
1386 pdev_stats = &zero_stats;
1389 spin_lock_bh(&ar->data_lock);
1391 data[i++] = pdev_stats->hw_reaped; /* ppdu reaped */
1392 data[i++] = 0; /* tx bytes */
1393 data[i++] = pdev_stats->htt_mpdus;
1394 data[i++] = 0; /* rx bytes */
1395 data[i++] = pdev_stats->ch_noise_floor;
1396 data[i++] = pdev_stats->cycle_count;
1397 data[i++] = pdev_stats->phy_err_count;
1398 data[i++] = pdev_stats->rts_bad;
1399 data[i++] = pdev_stats->rts_good;
1400 data[i++] = pdev_stats->chan_tx_power;
1401 data[i++] = pdev_stats->fcs_bad;
1402 data[i++] = pdev_stats->no_beacons;
1403 data[i++] = pdev_stats->mpdu_enqued;
1404 data[i++] = pdev_stats->msdu_enqued;
1405 data[i++] = pdev_stats->wmm_drop;
1406 data[i++] = pdev_stats->local_enqued;
1407 data[i++] = pdev_stats->local_freed;
1408 data[i++] = pdev_stats->hw_queued;
1409 data[i++] = pdev_stats->hw_reaped;
1410 data[i++] = pdev_stats->underrun;
1411 data[i++] = pdev_stats->tx_abort;
1412 data[i++] = pdev_stats->mpdus_requed;
1413 data[i++] = pdev_stats->tx_ko;
1414 data[i++] = pdev_stats->data_rc;
1415 data[i++] = pdev_stats->sw_retry_failure;
1416 data[i++] = pdev_stats->illgl_rate_phy_err;
1417 data[i++] = pdev_stats->pdev_cont_xretry;
1418 data[i++] = pdev_stats->pdev_tx_timeout;
1419 data[i++] = pdev_stats->txop_ovf;
1420 data[i++] = pdev_stats->pdev_resets;
1421 data[i++] = pdev_stats->mid_ppdu_route_change;
1422 data[i++] = pdev_stats->status_rcvd;
1423 data[i++] = pdev_stats->r0_frags;
1424 data[i++] = pdev_stats->r1_frags;
1425 data[i++] = pdev_stats->r2_frags;
1426 data[i++] = pdev_stats->r3_frags;
1427 data[i++] = pdev_stats->htt_msdus;
1428 data[i++] = pdev_stats->htt_mpdus;
1429 data[i++] = pdev_stats->loc_msdus;
1430 data[i++] = pdev_stats->loc_mpdus;
1431 data[i++] = pdev_stats->phy_errs;
1432 data[i++] = pdev_stats->phy_err_drop;
1433 data[i++] = pdev_stats->mpdu_errs;
1434 data[i++] = ar->stats.fw_crash_counter;
1435 data[i++] = ar->stats.fw_warm_reset_counter;
1436 data[i++] = ar->stats.fw_cold_reset_counter;
1438 spin_unlock_bh(&ar->data_lock);
1440 mutex_unlock(&ar->conf_mutex);
1442 WARN_ON(i != ATH10K_SSTATS_LEN);
1445 static const struct file_operations fops_fw_dbglog = {
1446 .read = ath10k_read_fw_dbglog,
1447 .write = ath10k_write_fw_dbglog,
1448 .open = simple_open,
1449 .owner = THIS_MODULE,
1450 .llseek = default_llseek,
1453 static int ath10k_debug_cal_data_open(struct inode *inode, struct file *file)
1455 struct ath10k *ar = inode->i_private;
1456 void *buf;
1457 u32 hi_addr;
1458 __le32 addr;
1459 int ret;
1461 mutex_lock(&ar->conf_mutex);
1463 if (ar->state != ATH10K_STATE_ON &&
1464 ar->state != ATH10K_STATE_UTF) {
1465 ret = -ENETDOWN;
1466 goto err;
1469 buf = vmalloc(ar->hw_params.cal_data_len);
1470 if (!buf) {
1471 ret = -ENOMEM;
1472 goto err;
1475 hi_addr = host_interest_item_address(HI_ITEM(hi_board_data));
1477 ret = ath10k_hif_diag_read(ar, hi_addr, &addr, sizeof(addr));
1478 if (ret) {
1479 ath10k_warn(ar, "failed to read hi_board_data address: %d\n", ret);
1480 goto err_vfree;
1483 ret = ath10k_hif_diag_read(ar, le32_to_cpu(addr), buf,
1484 ar->hw_params.cal_data_len);
1485 if (ret) {
1486 ath10k_warn(ar, "failed to read calibration data: %d\n", ret);
1487 goto err_vfree;
1490 file->private_data = buf;
1492 mutex_unlock(&ar->conf_mutex);
1494 return 0;
1496 err_vfree:
1497 vfree(buf);
1499 err:
1500 mutex_unlock(&ar->conf_mutex);
1502 return ret;
1505 static ssize_t ath10k_debug_cal_data_read(struct file *file,
1506 char __user *user_buf,
1507 size_t count, loff_t *ppos)
1509 struct ath10k *ar = file->private_data;
1510 void *buf = file->private_data;
1512 return simple_read_from_buffer(user_buf, count, ppos,
1513 buf, ar->hw_params.cal_data_len);
1516 static int ath10k_debug_cal_data_release(struct inode *inode,
1517 struct file *file)
1519 vfree(file->private_data);
1521 return 0;
1524 static ssize_t ath10k_write_ani_enable(struct file *file,
1525 const char __user *user_buf,
1526 size_t count, loff_t *ppos)
1528 struct ath10k *ar = file->private_data;
1529 int ret;
1530 u8 enable;
1532 if (kstrtou8_from_user(user_buf, count, 0, &enable))
1533 return -EINVAL;
1535 mutex_lock(&ar->conf_mutex);
1537 if (ar->ani_enabled == enable) {
1538 ret = count;
1539 goto exit;
1542 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->ani_enable,
1543 enable);
1544 if (ret) {
1545 ath10k_warn(ar, "ani_enable failed from debugfs: %d\n", ret);
1546 goto exit;
1548 ar->ani_enabled = enable;
1550 ret = count;
1552 exit:
1553 mutex_unlock(&ar->conf_mutex);
1555 return ret;
1558 static ssize_t ath10k_read_ani_enable(struct file *file, char __user *user_buf,
1559 size_t count, loff_t *ppos)
1561 struct ath10k *ar = file->private_data;
1562 int len = 0;
1563 char buf[32];
1565 len = scnprintf(buf, sizeof(buf) - len, "%d\n",
1566 ar->ani_enabled);
1568 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1571 static const struct file_operations fops_ani_enable = {
1572 .read = ath10k_read_ani_enable,
1573 .write = ath10k_write_ani_enable,
1574 .open = simple_open,
1575 .owner = THIS_MODULE,
1576 .llseek = default_llseek,
1579 static const struct file_operations fops_cal_data = {
1580 .open = ath10k_debug_cal_data_open,
1581 .read = ath10k_debug_cal_data_read,
1582 .release = ath10k_debug_cal_data_release,
1583 .owner = THIS_MODULE,
1584 .llseek = default_llseek,
1587 static ssize_t ath10k_read_nf_cal_period(struct file *file,
1588 char __user *user_buf,
1589 size_t count, loff_t *ppos)
1591 struct ath10k *ar = file->private_data;
1592 unsigned int len;
1593 char buf[32];
1595 len = scnprintf(buf, sizeof(buf), "%d\n",
1596 ar->debug.nf_cal_period);
1598 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1601 static ssize_t ath10k_write_nf_cal_period(struct file *file,
1602 const char __user *user_buf,
1603 size_t count, loff_t *ppos)
1605 struct ath10k *ar = file->private_data;
1606 unsigned long period;
1607 int ret;
1609 ret = kstrtoul_from_user(user_buf, count, 0, &period);
1610 if (ret)
1611 return ret;
1613 if (period > WMI_PDEV_PARAM_CAL_PERIOD_MAX)
1614 return -EINVAL;
1616 /* there's no way to switch back to the firmware default */
1617 if (period == 0)
1618 return -EINVAL;
1620 mutex_lock(&ar->conf_mutex);
1622 ar->debug.nf_cal_period = period;
1624 if (ar->state != ATH10K_STATE_ON) {
1625 /* firmware is not running, nothing else to do */
1626 ret = count;
1627 goto exit;
1630 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->cal_period,
1631 ar->debug.nf_cal_period);
1632 if (ret) {
1633 ath10k_warn(ar, "cal period cfg failed from debugfs: %d\n",
1634 ret);
1635 goto exit;
1638 ret = count;
1640 exit:
1641 mutex_unlock(&ar->conf_mutex);
1643 return ret;
1646 static const struct file_operations fops_nf_cal_period = {
1647 .read = ath10k_read_nf_cal_period,
1648 .write = ath10k_write_nf_cal_period,
1649 .open = simple_open,
1650 .owner = THIS_MODULE,
1651 .llseek = default_llseek,
1654 #define ATH10K_TPC_CONFIG_BUF_SIZE (1024 * 1024)
1656 static int ath10k_debug_tpc_stats_request(struct ath10k *ar)
1658 int ret;
1659 unsigned long time_left;
1661 lockdep_assert_held(&ar->conf_mutex);
1663 reinit_completion(&ar->debug.tpc_complete);
1665 ret = ath10k_wmi_pdev_get_tpc_config(ar, WMI_TPC_CONFIG_PARAM);
1666 if (ret) {
1667 ath10k_warn(ar, "failed to request tpc config: %d\n", ret);
1668 return ret;
1671 time_left = wait_for_completion_timeout(&ar->debug.tpc_complete,
1672 1 * HZ);
1673 if (time_left == 0)
1674 return -ETIMEDOUT;
1676 return 0;
1679 void ath10k_debug_tpc_stats_process(struct ath10k *ar,
1680 struct ath10k_tpc_stats *tpc_stats)
1682 spin_lock_bh(&ar->data_lock);
1684 kfree(ar->debug.tpc_stats);
1685 ar->debug.tpc_stats = tpc_stats;
1686 complete(&ar->debug.tpc_complete);
1688 spin_unlock_bh(&ar->data_lock);
1691 static void ath10k_tpc_stats_print(struct ath10k_tpc_stats *tpc_stats,
1692 unsigned int j, char *buf, unsigned int *len)
1694 unsigned int i, buf_len;
1695 static const char table_str[][5] = { "CDD",
1696 "STBC",
1697 "TXBF" };
1698 static const char pream_str[][6] = { "CCK",
1699 "OFDM",
1700 "HT20",
1701 "HT40",
1702 "VHT20",
1703 "VHT40",
1704 "VHT80",
1705 "HTCUP" };
1707 buf_len = ATH10K_TPC_CONFIG_BUF_SIZE;
1708 *len += scnprintf(buf + *len, buf_len - *len,
1709 "********************************\n");
1710 *len += scnprintf(buf + *len, buf_len - *len,
1711 "******************* %s POWER TABLE ****************\n",
1712 table_str[j]);
1713 *len += scnprintf(buf + *len, buf_len - *len,
1714 "********************************\n");
1715 *len += scnprintf(buf + *len, buf_len - *len,
1716 "No. Preamble Rate_code tpc_value1 tpc_value2 tpc_value3\n");
1718 for (i = 0; i < tpc_stats->rate_max; i++) {
1719 *len += scnprintf(buf + *len, buf_len - *len,
1720 "%8d %s 0x%2x %s\n", i,
1721 pream_str[tpc_stats->tpc_table[j].pream_idx[i]],
1722 tpc_stats->tpc_table[j].rate_code[i],
1723 tpc_stats->tpc_table[j].tpc_value[i]);
1726 *len += scnprintf(buf + *len, buf_len - *len,
1727 "***********************************\n");
1730 static void ath10k_tpc_stats_fill(struct ath10k *ar,
1731 struct ath10k_tpc_stats *tpc_stats,
1732 char *buf)
1734 unsigned int len, j, buf_len;
1736 len = 0;
1737 buf_len = ATH10K_TPC_CONFIG_BUF_SIZE;
1739 spin_lock_bh(&ar->data_lock);
1741 if (!tpc_stats) {
1742 ath10k_warn(ar, "failed to get tpc stats\n");
1743 goto unlock;
1746 len += scnprintf(buf + len, buf_len - len, "\n");
1747 len += scnprintf(buf + len, buf_len - len,
1748 "*************************************\n");
1749 len += scnprintf(buf + len, buf_len - len,
1750 "TPC config for channel %4d mode %d\n",
1751 tpc_stats->chan_freq,
1752 tpc_stats->phy_mode);
1753 len += scnprintf(buf + len, buf_len - len,
1754 "*************************************\n");
1755 len += scnprintf(buf + len, buf_len - len,
1756 "CTL = 0x%2x Reg. Domain = %2d\n",
1757 tpc_stats->ctl,
1758 tpc_stats->reg_domain);
1759 len += scnprintf(buf + len, buf_len - len,
1760 "Antenna Gain = %2d Reg. Max Antenna Gain = %2d\n",
1761 tpc_stats->twice_antenna_gain,
1762 tpc_stats->twice_antenna_reduction);
1763 len += scnprintf(buf + len, buf_len - len,
1764 "Power Limit = %2d Reg. Max Power = %2d\n",
1765 tpc_stats->power_limit,
1766 tpc_stats->twice_max_rd_power / 2);
1767 len += scnprintf(buf + len, buf_len - len,
1768 "Num tx chains = %2d Num supported rates = %2d\n",
1769 tpc_stats->num_tx_chain,
1770 tpc_stats->rate_max);
1772 for (j = 0; j < tpc_stats->num_tx_chain ; j++) {
1773 switch (j) {
1774 case WMI_TPC_TABLE_TYPE_CDD:
1775 if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) {
1776 len += scnprintf(buf + len, buf_len - len,
1777 "CDD not supported\n");
1778 break;
1781 ath10k_tpc_stats_print(tpc_stats, j, buf, &len);
1782 break;
1783 case WMI_TPC_TABLE_TYPE_STBC:
1784 if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) {
1785 len += scnprintf(buf + len, buf_len - len,
1786 "STBC not supported\n");
1787 break;
1790 ath10k_tpc_stats_print(tpc_stats, j, buf, &len);
1791 break;
1792 case WMI_TPC_TABLE_TYPE_TXBF:
1793 if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) {
1794 len += scnprintf(buf + len, buf_len - len,
1795 "TXBF not supported\n***************************\n");
1796 break;
1799 ath10k_tpc_stats_print(tpc_stats, j, buf, &len);
1800 break;
1801 default:
1802 len += scnprintf(buf + len, buf_len - len,
1803 "Invalid Type\n");
1804 break;
1808 unlock:
1809 spin_unlock_bh(&ar->data_lock);
1811 if (len >= buf_len)
1812 buf[len - 1] = 0;
1813 else
1814 buf[len] = 0;
1817 static int ath10k_tpc_stats_open(struct inode *inode, struct file *file)
1819 struct ath10k *ar = inode->i_private;
1820 void *buf = NULL;
1821 int ret;
1823 mutex_lock(&ar->conf_mutex);
1825 if (ar->state != ATH10K_STATE_ON) {
1826 ret = -ENETDOWN;
1827 goto err_unlock;
1830 buf = vmalloc(ATH10K_TPC_CONFIG_BUF_SIZE);
1831 if (!buf) {
1832 ret = -ENOMEM;
1833 goto err_unlock;
1836 ret = ath10k_debug_tpc_stats_request(ar);
1837 if (ret) {
1838 ath10k_warn(ar, "failed to request tpc config stats: %d\n",
1839 ret);
1840 goto err_free;
1843 ath10k_tpc_stats_fill(ar, ar->debug.tpc_stats, buf);
1844 file->private_data = buf;
1846 mutex_unlock(&ar->conf_mutex);
1847 return 0;
1849 err_free:
1850 vfree(buf);
1852 err_unlock:
1853 mutex_unlock(&ar->conf_mutex);
1854 return ret;
1857 static int ath10k_tpc_stats_release(struct inode *inode, struct file *file)
1859 vfree(file->private_data);
1861 return 0;
1864 static ssize_t ath10k_tpc_stats_read(struct file *file, char __user *user_buf,
1865 size_t count, loff_t *ppos)
1867 const char *buf = file->private_data;
1868 unsigned int len = strlen(buf);
1870 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1873 static const struct file_operations fops_tpc_stats = {
1874 .open = ath10k_tpc_stats_open,
1875 .release = ath10k_tpc_stats_release,
1876 .read = ath10k_tpc_stats_read,
1877 .owner = THIS_MODULE,
1878 .llseek = default_llseek,
1881 int ath10k_debug_start(struct ath10k *ar)
1883 int ret;
1885 lockdep_assert_held(&ar->conf_mutex);
1887 ret = ath10k_debug_htt_stats_req(ar);
1888 if (ret)
1889 /* continue normally anyway, this isn't serious */
1890 ath10k_warn(ar, "failed to start htt stats workqueue: %d\n",
1891 ret);
1893 if (ar->debug.fw_dbglog_mask) {
1894 ret = ath10k_wmi_dbglog_cfg(ar, ar->debug.fw_dbglog_mask,
1895 ATH10K_DBGLOG_LEVEL_WARN);
1896 if (ret)
1897 /* not serious */
1898 ath10k_warn(ar, "failed to enable dbglog during start: %d",
1899 ret);
1902 if (ar->debug.pktlog_filter) {
1903 ret = ath10k_wmi_pdev_pktlog_enable(ar,
1904 ar->debug.pktlog_filter);
1905 if (ret)
1906 /* not serious */
1907 ath10k_warn(ar,
1908 "failed to enable pktlog filter %x: %d\n",
1909 ar->debug.pktlog_filter, ret);
1910 } else {
1911 ret = ath10k_wmi_pdev_pktlog_disable(ar);
1912 if (ret)
1913 /* not serious */
1914 ath10k_warn(ar, "failed to disable pktlog: %d\n", ret);
1917 if (ar->debug.nf_cal_period) {
1918 ret = ath10k_wmi_pdev_set_param(ar,
1919 ar->wmi.pdev_param->cal_period,
1920 ar->debug.nf_cal_period);
1921 if (ret)
1922 /* not serious */
1923 ath10k_warn(ar, "cal period cfg failed from debug start: %d\n",
1924 ret);
1927 return ret;
1930 void ath10k_debug_stop(struct ath10k *ar)
1932 lockdep_assert_held(&ar->conf_mutex);
1934 /* Must not use _sync to avoid deadlock, we do that in
1935 * ath10k_debug_destroy(). The check for htt_stats_mask is to avoid
1936 * warning from del_timer(). */
1937 if (ar->debug.htt_stats_mask != 0)
1938 cancel_delayed_work(&ar->debug.htt_stats_dwork);
1940 ath10k_wmi_pdev_pktlog_disable(ar);
1943 static ssize_t ath10k_write_simulate_radar(struct file *file,
1944 const char __user *user_buf,
1945 size_t count, loff_t *ppos)
1947 struct ath10k *ar = file->private_data;
1949 ieee80211_radar_detected(ar->hw);
1951 return count;
1954 static const struct file_operations fops_simulate_radar = {
1955 .write = ath10k_write_simulate_radar,
1956 .open = simple_open,
1957 .owner = THIS_MODULE,
1958 .llseek = default_llseek,
1961 #define ATH10K_DFS_STAT(s, p) (\
1962 len += scnprintf(buf + len, size - len, "%-28s : %10u\n", s, \
1963 ar->debug.dfs_stats.p))
1965 #define ATH10K_DFS_POOL_STAT(s, p) (\
1966 len += scnprintf(buf + len, size - len, "%-28s : %10u\n", s, \
1967 ar->debug.dfs_pool_stats.p))
1969 static ssize_t ath10k_read_dfs_stats(struct file *file, char __user *user_buf,
1970 size_t count, loff_t *ppos)
1972 int retval = 0, len = 0;
1973 const int size = 8000;
1974 struct ath10k *ar = file->private_data;
1975 char *buf;
1977 buf = kzalloc(size, GFP_KERNEL);
1978 if (buf == NULL)
1979 return -ENOMEM;
1981 if (!ar->dfs_detector) {
1982 len += scnprintf(buf + len, size - len, "DFS not enabled\n");
1983 goto exit;
1986 ar->debug.dfs_pool_stats =
1987 ar->dfs_detector->get_stats(ar->dfs_detector);
1989 len += scnprintf(buf + len, size - len, "Pulse detector statistics:\n");
1991 ATH10K_DFS_STAT("reported phy errors", phy_errors);
1992 ATH10K_DFS_STAT("pulse events reported", pulses_total);
1993 ATH10K_DFS_STAT("DFS pulses detected", pulses_detected);
1994 ATH10K_DFS_STAT("DFS pulses discarded", pulses_discarded);
1995 ATH10K_DFS_STAT("Radars detected", radar_detected);
1997 len += scnprintf(buf + len, size - len, "Global Pool statistics:\n");
1998 ATH10K_DFS_POOL_STAT("Pool references", pool_reference);
1999 ATH10K_DFS_POOL_STAT("Pulses allocated", pulse_allocated);
2000 ATH10K_DFS_POOL_STAT("Pulses alloc error", pulse_alloc_error);
2001 ATH10K_DFS_POOL_STAT("Pulses in use", pulse_used);
2002 ATH10K_DFS_POOL_STAT("Seqs. allocated", pseq_allocated);
2003 ATH10K_DFS_POOL_STAT("Seqs. alloc error", pseq_alloc_error);
2004 ATH10K_DFS_POOL_STAT("Seqs. in use", pseq_used);
2006 exit:
2007 if (len > size)
2008 len = size;
2010 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
2011 kfree(buf);
2013 return retval;
2016 static const struct file_operations fops_dfs_stats = {
2017 .read = ath10k_read_dfs_stats,
2018 .open = simple_open,
2019 .owner = THIS_MODULE,
2020 .llseek = default_llseek,
2023 static ssize_t ath10k_write_pktlog_filter(struct file *file,
2024 const char __user *ubuf,
2025 size_t count, loff_t *ppos)
2027 struct ath10k *ar = file->private_data;
2028 u32 filter;
2029 int ret;
2031 if (kstrtouint_from_user(ubuf, count, 0, &filter))
2032 return -EINVAL;
2034 mutex_lock(&ar->conf_mutex);
2036 if (ar->state != ATH10K_STATE_ON) {
2037 ar->debug.pktlog_filter = filter;
2038 ret = count;
2039 goto out;
2042 if (filter == ar->debug.pktlog_filter) {
2043 ret = count;
2044 goto out;
2047 if (filter) {
2048 ret = ath10k_wmi_pdev_pktlog_enable(ar, filter);
2049 if (ret) {
2050 ath10k_warn(ar, "failed to enable pktlog filter %x: %d\n",
2051 ar->debug.pktlog_filter, ret);
2052 goto out;
2054 } else {
2055 ret = ath10k_wmi_pdev_pktlog_disable(ar);
2056 if (ret) {
2057 ath10k_warn(ar, "failed to disable pktlog: %d\n", ret);
2058 goto out;
2062 ar->debug.pktlog_filter = filter;
2063 ret = count;
2065 out:
2066 mutex_unlock(&ar->conf_mutex);
2067 return ret;
2070 static ssize_t ath10k_read_pktlog_filter(struct file *file, char __user *ubuf,
2071 size_t count, loff_t *ppos)
2073 char buf[32];
2074 struct ath10k *ar = file->private_data;
2075 int len = 0;
2077 mutex_lock(&ar->conf_mutex);
2078 len = scnprintf(buf, sizeof(buf) - len, "%08x\n",
2079 ar->debug.pktlog_filter);
2080 mutex_unlock(&ar->conf_mutex);
2082 return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2085 static const struct file_operations fops_pktlog_filter = {
2086 .read = ath10k_read_pktlog_filter,
2087 .write = ath10k_write_pktlog_filter,
2088 .open = simple_open
2091 static ssize_t ath10k_write_quiet_period(struct file *file,
2092 const char __user *ubuf,
2093 size_t count, loff_t *ppos)
2095 struct ath10k *ar = file->private_data;
2096 u32 period;
2098 if (kstrtouint_from_user(ubuf, count, 0, &period))
2099 return -EINVAL;
2101 if (period < ATH10K_QUIET_PERIOD_MIN) {
2102 ath10k_warn(ar, "Quiet period %u can not be lesser than 25ms\n",
2103 period);
2104 return -EINVAL;
2106 mutex_lock(&ar->conf_mutex);
2107 ar->thermal.quiet_period = period;
2108 ath10k_thermal_set_throttling(ar);
2109 mutex_unlock(&ar->conf_mutex);
2111 return count;
2114 static ssize_t ath10k_read_quiet_period(struct file *file, char __user *ubuf,
2115 size_t count, loff_t *ppos)
2117 char buf[32];
2118 struct ath10k *ar = file->private_data;
2119 int len = 0;
2121 mutex_lock(&ar->conf_mutex);
2122 len = scnprintf(buf, sizeof(buf) - len, "%d\n",
2123 ar->thermal.quiet_period);
2124 mutex_unlock(&ar->conf_mutex);
2126 return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2129 static const struct file_operations fops_quiet_period = {
2130 .read = ath10k_read_quiet_period,
2131 .write = ath10k_write_quiet_period,
2132 .open = simple_open
2135 static ssize_t ath10k_write_btcoex(struct file *file,
2136 const char __user *ubuf,
2137 size_t count, loff_t *ppos)
2139 struct ath10k *ar = file->private_data;
2140 char buf[32];
2141 size_t buf_size;
2142 int ret;
2143 bool val;
2144 u32 pdev_param;
2146 buf_size = min(count, (sizeof(buf) - 1));
2147 if (copy_from_user(buf, ubuf, buf_size))
2148 return -EFAULT;
2150 buf[buf_size] = '\0';
2152 if (strtobool(buf, &val) != 0)
2153 return -EINVAL;
2155 mutex_lock(&ar->conf_mutex);
2157 if (ar->state != ATH10K_STATE_ON &&
2158 ar->state != ATH10K_STATE_RESTARTED) {
2159 ret = -ENETDOWN;
2160 goto exit;
2163 if (!(test_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags) ^ val)) {
2164 ret = count;
2165 goto exit;
2168 pdev_param = ar->wmi.pdev_param->enable_btcoex;
2169 if (test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM,
2170 ar->running_fw->fw_file.fw_features)) {
2171 ret = ath10k_wmi_pdev_set_param(ar, pdev_param, val);
2172 if (ret) {
2173 ath10k_warn(ar, "failed to enable btcoex: %d\n", ret);
2174 ret = count;
2175 goto exit;
2177 } else {
2178 ath10k_info(ar, "restarting firmware due to btcoex change");
2179 queue_work(ar->workqueue, &ar->restart_work);
2182 if (val)
2183 set_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
2184 else
2185 clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
2187 ret = count;
2189 exit:
2190 mutex_unlock(&ar->conf_mutex);
2192 return ret;
2195 static ssize_t ath10k_read_btcoex(struct file *file, char __user *ubuf,
2196 size_t count, loff_t *ppos)
2198 char buf[32];
2199 struct ath10k *ar = file->private_data;
2200 int len = 0;
2202 mutex_lock(&ar->conf_mutex);
2203 len = scnprintf(buf, sizeof(buf) - len, "%d\n",
2204 test_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags));
2205 mutex_unlock(&ar->conf_mutex);
2207 return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2210 static const struct file_operations fops_btcoex = {
2211 .read = ath10k_read_btcoex,
2212 .write = ath10k_write_btcoex,
2213 .open = simple_open
2216 static ssize_t ath10k_write_peer_stats(struct file *file,
2217 const char __user *ubuf,
2218 size_t count, loff_t *ppos)
2220 struct ath10k *ar = file->private_data;
2221 char buf[32];
2222 size_t buf_size;
2223 int ret;
2224 bool val;
2226 buf_size = min(count, (sizeof(buf) - 1));
2227 if (copy_from_user(buf, ubuf, buf_size))
2228 return -EFAULT;
2230 buf[buf_size] = '\0';
2232 if (strtobool(buf, &val) != 0)
2233 return -EINVAL;
2235 mutex_lock(&ar->conf_mutex);
2237 if (ar->state != ATH10K_STATE_ON &&
2238 ar->state != ATH10K_STATE_RESTARTED) {
2239 ret = -ENETDOWN;
2240 goto exit;
2243 if (!(test_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags) ^ val)) {
2244 ret = count;
2245 goto exit;
2248 if (val)
2249 set_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags);
2250 else
2251 clear_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags);
2253 ath10k_info(ar, "restarting firmware due to Peer stats change");
2255 queue_work(ar->workqueue, &ar->restart_work);
2256 ret = count;
2258 exit:
2259 mutex_unlock(&ar->conf_mutex);
2260 return ret;
2263 static ssize_t ath10k_read_peer_stats(struct file *file, char __user *ubuf,
2264 size_t count, loff_t *ppos)
2267 char buf[32];
2268 struct ath10k *ar = file->private_data;
2269 int len = 0;
2271 mutex_lock(&ar->conf_mutex);
2272 len = scnprintf(buf, sizeof(buf) - len, "%d\n",
2273 test_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags));
2274 mutex_unlock(&ar->conf_mutex);
2276 return simple_read_from_buffer(ubuf, count, ppos, buf, len);
2279 static const struct file_operations fops_peer_stats = {
2280 .read = ath10k_read_peer_stats,
2281 .write = ath10k_write_peer_stats,
2282 .open = simple_open
2285 static ssize_t ath10k_debug_fw_checksums_read(struct file *file,
2286 char __user *user_buf,
2287 size_t count, loff_t *ppos)
2289 struct ath10k *ar = file->private_data;
2290 unsigned int len = 0, buf_len = 4096;
2291 ssize_t ret_cnt;
2292 char *buf;
2294 buf = kzalloc(buf_len, GFP_KERNEL);
2295 if (!buf)
2296 return -ENOMEM;
2298 mutex_lock(&ar->conf_mutex);
2300 len += scnprintf(buf + len, buf_len - len,
2301 "firmware-N.bin\t\t%08x\n",
2302 crc32_le(0, ar->normal_mode_fw.fw_file.firmware->data,
2303 ar->normal_mode_fw.fw_file.firmware->size));
2304 len += scnprintf(buf + len, buf_len - len,
2305 "athwlan\t\t\t%08x\n",
2306 crc32_le(0, ar->normal_mode_fw.fw_file.firmware_data,
2307 ar->normal_mode_fw.fw_file.firmware_len));
2308 len += scnprintf(buf + len, buf_len - len,
2309 "otp\t\t\t%08x\n",
2310 crc32_le(0, ar->normal_mode_fw.fw_file.otp_data,
2311 ar->normal_mode_fw.fw_file.otp_len));
2312 len += scnprintf(buf + len, buf_len - len,
2313 "codeswap\t\t%08x\n",
2314 crc32_le(0, ar->normal_mode_fw.fw_file.codeswap_data,
2315 ar->normal_mode_fw.fw_file.codeswap_len));
2316 len += scnprintf(buf + len, buf_len - len,
2317 "board-N.bin\t\t%08x\n",
2318 crc32_le(0, ar->normal_mode_fw.board->data,
2319 ar->normal_mode_fw.board->size));
2320 len += scnprintf(buf + len, buf_len - len,
2321 "board\t\t\t%08x\n",
2322 crc32_le(0, ar->normal_mode_fw.board_data,
2323 ar->normal_mode_fw.board_len));
2325 ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
2327 mutex_unlock(&ar->conf_mutex);
2329 kfree(buf);
2330 return ret_cnt;
2333 static const struct file_operations fops_fw_checksums = {
2334 .read = ath10k_debug_fw_checksums_read,
2335 .open = simple_open,
2336 .owner = THIS_MODULE,
2337 .llseek = default_llseek,
2340 int ath10k_debug_create(struct ath10k *ar)
2342 ar->debug.fw_crash_data = vzalloc(sizeof(*ar->debug.fw_crash_data));
2343 if (!ar->debug.fw_crash_data)
2344 return -ENOMEM;
2346 INIT_LIST_HEAD(&ar->debug.fw_stats.pdevs);
2347 INIT_LIST_HEAD(&ar->debug.fw_stats.vdevs);
2348 INIT_LIST_HEAD(&ar->debug.fw_stats.peers);
2349 INIT_LIST_HEAD(&ar->debug.fw_stats.peers_extd);
2351 return 0;
2354 void ath10k_debug_destroy(struct ath10k *ar)
2356 vfree(ar->debug.fw_crash_data);
2357 ar->debug.fw_crash_data = NULL;
2359 ath10k_debug_fw_stats_reset(ar);
2361 kfree(ar->debug.tpc_stats);
2364 int ath10k_debug_register(struct ath10k *ar)
2366 ar->debug.debugfs_phy = debugfs_create_dir("ath10k",
2367 ar->hw->wiphy->debugfsdir);
2368 if (IS_ERR_OR_NULL(ar->debug.debugfs_phy)) {
2369 if (IS_ERR(ar->debug.debugfs_phy))
2370 return PTR_ERR(ar->debug.debugfs_phy);
2372 return -ENOMEM;
2375 INIT_DELAYED_WORK(&ar->debug.htt_stats_dwork,
2376 ath10k_debug_htt_stats_dwork);
2378 init_completion(&ar->debug.tpc_complete);
2379 init_completion(&ar->debug.fw_stats_complete);
2381 debugfs_create_file("fw_stats", S_IRUSR, ar->debug.debugfs_phy, ar,
2382 &fops_fw_stats);
2384 debugfs_create_file("fw_reset_stats", S_IRUSR, ar->debug.debugfs_phy,
2385 ar, &fops_fw_reset_stats);
2387 debugfs_create_file("wmi_services", S_IRUSR, ar->debug.debugfs_phy, ar,
2388 &fops_wmi_services);
2390 debugfs_create_file("simulate_fw_crash", S_IRUSR | S_IWUSR,
2391 ar->debug.debugfs_phy, ar, &fops_simulate_fw_crash);
2393 debugfs_create_file("fw_crash_dump", S_IRUSR, ar->debug.debugfs_phy,
2394 ar, &fops_fw_crash_dump);
2396 debugfs_create_file("reg_addr", S_IRUSR | S_IWUSR,
2397 ar->debug.debugfs_phy, ar, &fops_reg_addr);
2399 debugfs_create_file("reg_value", S_IRUSR | S_IWUSR,
2400 ar->debug.debugfs_phy, ar, &fops_reg_value);
2402 debugfs_create_file("mem_value", S_IRUSR | S_IWUSR,
2403 ar->debug.debugfs_phy, ar, &fops_mem_value);
2405 debugfs_create_file("chip_id", S_IRUSR, ar->debug.debugfs_phy,
2406 ar, &fops_chip_id);
2408 debugfs_create_file("htt_stats_mask", S_IRUSR | S_IWUSR,
2409 ar->debug.debugfs_phy, ar, &fops_htt_stats_mask);
2411 debugfs_create_file("htt_max_amsdu_ampdu", S_IRUSR | S_IWUSR,
2412 ar->debug.debugfs_phy, ar,
2413 &fops_htt_max_amsdu_ampdu);
2415 debugfs_create_file("fw_dbglog", S_IRUSR | S_IWUSR,
2416 ar->debug.debugfs_phy, ar, &fops_fw_dbglog);
2418 debugfs_create_file("cal_data", S_IRUSR, ar->debug.debugfs_phy,
2419 ar, &fops_cal_data);
2421 debugfs_create_file("ani_enable", S_IRUSR | S_IWUSR,
2422 ar->debug.debugfs_phy, ar, &fops_ani_enable);
2424 debugfs_create_file("nf_cal_period", S_IRUSR | S_IWUSR,
2425 ar->debug.debugfs_phy, ar, &fops_nf_cal_period);
2427 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) {
2428 debugfs_create_file("dfs_simulate_radar", S_IWUSR,
2429 ar->debug.debugfs_phy, ar,
2430 &fops_simulate_radar);
2432 debugfs_create_bool("dfs_block_radar_events", S_IWUSR,
2433 ar->debug.debugfs_phy,
2434 &ar->dfs_block_radar_events);
2436 debugfs_create_file("dfs_stats", S_IRUSR,
2437 ar->debug.debugfs_phy, ar,
2438 &fops_dfs_stats);
2441 debugfs_create_file("pktlog_filter", S_IRUGO | S_IWUSR,
2442 ar->debug.debugfs_phy, ar, &fops_pktlog_filter);
2444 debugfs_create_file("quiet_period", S_IRUGO | S_IWUSR,
2445 ar->debug.debugfs_phy, ar, &fops_quiet_period);
2447 debugfs_create_file("tpc_stats", S_IRUSR,
2448 ar->debug.debugfs_phy, ar, &fops_tpc_stats);
2450 if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map))
2451 debugfs_create_file("btcoex", S_IRUGO | S_IWUSR,
2452 ar->debug.debugfs_phy, ar, &fops_btcoex);
2454 if (test_bit(WMI_SERVICE_PEER_STATS, ar->wmi.svc_map))
2455 debugfs_create_file("peer_stats", S_IRUGO | S_IWUSR,
2456 ar->debug.debugfs_phy, ar,
2457 &fops_peer_stats);
2459 debugfs_create_file("fw_checksums", S_IRUSR,
2460 ar->debug.debugfs_phy, ar, &fops_fw_checksums);
2462 return 0;
2465 void ath10k_debug_unregister(struct ath10k *ar)
2467 cancel_delayed_work_sync(&ar->debug.htt_stats_dwork);
2470 #endif /* CONFIG_ATH10K_DEBUGFS */
2472 #ifdef CONFIG_ATH10K_DEBUG
2473 void ath10k_dbg(struct ath10k *ar, enum ath10k_debug_mask mask,
2474 const char *fmt, ...)
2476 struct va_format vaf;
2477 va_list args;
2479 va_start(args, fmt);
2481 vaf.fmt = fmt;
2482 vaf.va = &args;
2484 if (ath10k_debug_mask & mask)
2485 dev_printk(KERN_DEBUG, ar->dev, "%pV", &vaf);
2487 trace_ath10k_log_dbg(ar, mask, &vaf);
2489 va_end(args);
2491 EXPORT_SYMBOL(ath10k_dbg);
2493 void ath10k_dbg_dump(struct ath10k *ar,
2494 enum ath10k_debug_mask mask,
2495 const char *msg, const char *prefix,
2496 const void *buf, size_t len)
2498 char linebuf[256];
2499 unsigned int linebuflen;
2500 const void *ptr;
2502 if (ath10k_debug_mask & mask) {
2503 if (msg)
2504 ath10k_dbg(ar, mask, "%s\n", msg);
2506 for (ptr = buf; (ptr - buf) < len; ptr += 16) {
2507 linebuflen = 0;
2508 linebuflen += scnprintf(linebuf + linebuflen,
2509 sizeof(linebuf) - linebuflen,
2510 "%s%08x: ",
2511 (prefix ? prefix : ""),
2512 (unsigned int)(ptr - buf));
2513 hex_dump_to_buffer(ptr, len - (ptr - buf), 16, 1,
2514 linebuf + linebuflen,
2515 sizeof(linebuf) - linebuflen, true);
2516 dev_printk(KERN_DEBUG, ar->dev, "%s\n", linebuf);
2520 /* tracing code doesn't like null strings :/ */
2521 trace_ath10k_log_dbg_dump(ar, msg ? msg : "", prefix ? prefix : "",
2522 buf, len);
2524 EXPORT_SYMBOL(ath10k_dbg_dump);
2526 #endif /* CONFIG_ATH10K_DEBUG */