Linux Audio

Check our new training course

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