Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
   2/*
   3 * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
   4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
   5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
   6 */
   7#include <linux/vmalloc.h>
   8#include <linux/err.h>
   9#include <linux/ieee80211.h>
  10#include <linux/netdevice.h>
  11#include <linux/dmi.h>
  12
  13#include "mvm.h"
  14#include "sta.h"
  15#include "iwl-io.h"
  16#include "debugfs.h"
  17#include "iwl-modparams.h"
  18#include "iwl-drv.h"
  19#include "fw/error-dump.h"
  20#include "fw/api/phy-ctxt.h"
  21
  22static ssize_t iwl_dbgfs_ctdp_budget_read(struct file *file,
  23					  char __user *user_buf,
  24					  size_t count, loff_t *ppos)
  25{
  26	struct iwl_mvm *mvm = file->private_data;
  27	char buf[16];
  28	int pos, budget;
  29
  30	if (!iwl_mvm_is_ctdp_supported(mvm))
  31		return -EOPNOTSUPP;
  32
  33	if (!iwl_mvm_firmware_running(mvm) ||
  34	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
  35		return -EIO;
  36
  37	mutex_lock(&mvm->mutex);
  38	budget = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_REPORT, 0);
  39	mutex_unlock(&mvm->mutex);
  40
  41	if (budget < 0)
  42		return budget;
  43
  44	pos = scnprintf(buf, sizeof(buf), "%d\n", budget);
  45
  46	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  47}
  48
  49static ssize_t iwl_dbgfs_stop_ctdp_write(struct iwl_mvm *mvm, char *buf,
  50					 size_t count, loff_t *ppos)
  51{
  52	int ret;
  53	bool force;
  54
  55	if (!kstrtobool(buf, &force))
  56		IWL_DEBUG_INFO(mvm,
  57			       "force start is %d [0=disabled, 1=enabled]\n",
  58			       force);
  59
  60	/* we allow skipping cap support check and force stop ctdp
  61	 * statistics collection and with guerantee that it is
  62	 * safe to use.
  63	 */
  64	if (!force && !iwl_mvm_is_ctdp_supported(mvm))
  65		return -EOPNOTSUPP;
  66
  67	if (!iwl_mvm_firmware_running(mvm) ||
  68	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
  69		return -EIO;
  70
  71	mutex_lock(&mvm->mutex);
  72	ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_STOP, 0);
  73	mutex_unlock(&mvm->mutex);
  74
  75	return ret ?: count;
  76}
  77
  78static ssize_t iwl_dbgfs_start_ctdp_write(struct iwl_mvm *mvm,
  79					  char *buf, size_t count,
  80					  loff_t *ppos)
  81{
  82	int ret;
  83	bool force;
  84
  85	if (!kstrtobool(buf, &force))
  86		IWL_DEBUG_INFO(mvm,
  87			       "force start is %d [0=disabled, 1=enabled]\n",
  88			       force);
  89
  90	/* we allow skipping cap support check and force enable ctdp
  91	 * for statistics collection and with guerantee that it is
  92	 * safe to use.
  93	 */
  94	if (!force && !iwl_mvm_is_ctdp_supported(mvm))
  95		return -EOPNOTSUPP;
  96
  97	if (!iwl_mvm_firmware_running(mvm) ||
  98	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
  99		return -EIO;
 100
 101	mutex_lock(&mvm->mutex);
 102	ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START, 0);
 103	mutex_unlock(&mvm->mutex);
 104
 105	return ret ?: count;
 106}
 107
 108static ssize_t iwl_dbgfs_force_ctkill_write(struct iwl_mvm *mvm, char *buf,
 109					    size_t count, loff_t *ppos)
 110{
 111	if (!iwl_mvm_firmware_running(mvm) ||
 112	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
 113		return -EIO;
 114
 115	iwl_mvm_enter_ctkill(mvm);
 116
 117	return count;
 118}
 119
 120static ssize_t iwl_dbgfs_tx_flush_write(struct iwl_mvm *mvm, char *buf,
 121					size_t count, loff_t *ppos)
 122{
 123	int ret;
 124	u32 flush_arg;
 125
 126	if (!iwl_mvm_firmware_running(mvm) ||
 127	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
 128		return -EIO;
 129
 130	if (kstrtou32(buf, 0, &flush_arg))
 131		return -EINVAL;
 132
 133	if (iwl_mvm_has_new_tx_api(mvm)) {
 134		IWL_DEBUG_TX_QUEUES(mvm,
 135				    "FLUSHING all tids queues on sta_id = %d\n",
 136				    flush_arg);
 137		mutex_lock(&mvm->mutex);
 138		ret = iwl_mvm_flush_sta_tids(mvm, flush_arg, 0xFFFF)
 139			? : count;
 140		mutex_unlock(&mvm->mutex);
 141		return ret;
 142	}
 143
 144	IWL_DEBUG_TX_QUEUES(mvm, "FLUSHING queues mask to flush = 0x%x\n",
 145			    flush_arg);
 146
 147	mutex_lock(&mvm->mutex);
 148	ret =  iwl_mvm_flush_tx_path(mvm, flush_arg) ? : count;
 149	mutex_unlock(&mvm->mutex);
 150
 151	return ret;
 152}
 153
 154static ssize_t iwl_dbgfs_sta_drain_write(struct iwl_mvm *mvm, char *buf,
 155					 size_t count, loff_t *ppos)
 156{
 157	struct iwl_mvm_sta *mvmsta;
 158	int sta_id, drain, ret;
 159
 160	if (!iwl_mvm_firmware_running(mvm) ||
 161	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
 162		return -EIO;
 163
 164	if (sscanf(buf, "%d %d", &sta_id, &drain) != 2)
 165		return -EINVAL;
 166	if (sta_id < 0 || sta_id >= mvm->fw->ucode_capa.num_stations)
 167		return -EINVAL;
 168	if (drain < 0 || drain > 1)
 169		return -EINVAL;
 170
 171	mutex_lock(&mvm->mutex);
 172
 173	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
 174
 175	if (!mvmsta)
 176		ret = -ENOENT;
 177	else
 178		ret = iwl_mvm_drain_sta(mvm, mvmsta, drain) ? : count;
 179
 180	mutex_unlock(&mvm->mutex);
 181
 182	return ret;
 183}
 184
 185static ssize_t iwl_dbgfs_sram_read(struct file *file, char __user *user_buf,
 186				   size_t count, loff_t *ppos)
 187{
 188	struct iwl_mvm *mvm = file->private_data;
 189	const struct fw_img *img;
 190	unsigned int ofs, len;
 191	size_t ret;
 192	u8 *ptr;
 193
 194	if (!iwl_mvm_firmware_running(mvm))
 195		return -EINVAL;
 196
 197	/* default is to dump the entire data segment */
 198	img = &mvm->fw->img[mvm->fwrt.cur_fw_img];
 199	ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
 200	len = img->sec[IWL_UCODE_SECTION_DATA].len;
 201
 202	if (mvm->dbgfs_sram_len) {
 203		ofs = mvm->dbgfs_sram_offset;
 204		len = mvm->dbgfs_sram_len;
 205	}
 206
 207	ptr = kzalloc(len, GFP_KERNEL);
 208	if (!ptr)
 209		return -ENOMEM;
 210
 211	iwl_trans_read_mem_bytes(mvm->trans, ofs, ptr, len);
 212
 213	ret = simple_read_from_buffer(user_buf, count, ppos, ptr, len);
 214
 215	kfree(ptr);
 216
 217	return ret;
 218}
 219
 220static ssize_t iwl_dbgfs_sram_write(struct iwl_mvm *mvm, char *buf,
 221				    size_t count, loff_t *ppos)
 222{
 223	const struct fw_img *img;
 224	u32 offset, len;
 225	u32 img_offset, img_len;
 226
 227	if (!iwl_mvm_firmware_running(mvm))
 228		return -EINVAL;
 229
 230	img = &mvm->fw->img[mvm->fwrt.cur_fw_img];
 231	img_offset = img->sec[IWL_UCODE_SECTION_DATA].offset;
 232	img_len = img->sec[IWL_UCODE_SECTION_DATA].len;
 233
 234	if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
 235		if ((offset & 0x3) || (len & 0x3))
 236			return -EINVAL;
 237
 238		if (offset + len > img_offset + img_len)
 239			return -EINVAL;
 240
 241		mvm->dbgfs_sram_offset = offset;
 242		mvm->dbgfs_sram_len = len;
 243	} else {
 244		mvm->dbgfs_sram_offset = 0;
 245		mvm->dbgfs_sram_len = 0;
 246	}
 247
 248	return count;
 249}
 250
 251static ssize_t iwl_dbgfs_set_nic_temperature_read(struct file *file,
 252						  char __user *user_buf,
 253						  size_t count, loff_t *ppos)
 254{
 255	struct iwl_mvm *mvm = file->private_data;
 256	char buf[16];
 257	int pos;
 258
 259	if (!mvm->temperature_test)
 260		pos = scnprintf(buf, sizeof(buf), "disabled\n");
 261	else
 262		pos = scnprintf(buf, sizeof(buf), "%d\n", mvm->temperature);
 263
 264	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 265}
 266
 267/*
 268 * Set NIC Temperature
 269 * Cause the driver to ignore the actual NIC temperature reported by the FW
 270 * Enable: any value between IWL_MVM_DEBUG_SET_TEMPERATURE_MIN -
 271 * IWL_MVM_DEBUG_SET_TEMPERATURE_MAX
 272 * Disable: IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE
 273 */
 274static ssize_t iwl_dbgfs_set_nic_temperature_write(struct iwl_mvm *mvm,
 275						   char *buf, size_t count,
 276						   loff_t *ppos)
 277{
 278	int temperature;
 279
 280	if (!iwl_mvm_firmware_running(mvm) && !mvm->temperature_test)
 281		return -EIO;
 282
 283	if (kstrtoint(buf, 10, &temperature))
 284		return -EINVAL;
 285	/* not a legal temperature */
 286	if ((temperature > IWL_MVM_DEBUG_SET_TEMPERATURE_MAX &&
 287	     temperature != IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE) ||
 288	    temperature < IWL_MVM_DEBUG_SET_TEMPERATURE_MIN)
 289		return -EINVAL;
 290
 291	mutex_lock(&mvm->mutex);
 292	if (temperature == IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE) {
 293		if (!mvm->temperature_test)
 294			goto out;
 295
 296		mvm->temperature_test = false;
 297		/* Since we can't read the temp while awake, just set
 298		 * it to zero until we get the next RX stats from the
 299		 * firmware.
 300		 */
 301		mvm->temperature = 0;
 302	} else {
 303		mvm->temperature_test = true;
 304		mvm->temperature = temperature;
 305	}
 306	IWL_DEBUG_TEMP(mvm, "%sabling debug set temperature (temp = %d)\n",
 307		       mvm->temperature_test ? "En" : "Dis",
 308		       mvm->temperature);
 309	/* handle the temperature change */
 310	iwl_mvm_tt_handler(mvm);
 311
 312out:
 313	mutex_unlock(&mvm->mutex);
 314
 315	return count;
 316}
 317
 318static ssize_t iwl_dbgfs_nic_temp_read(struct file *file,
 319				       char __user *user_buf,
 320				       size_t count, loff_t *ppos)
 321{
 322	struct iwl_mvm *mvm = file->private_data;
 323	char buf[16];
 324	int pos, ret;
 325	s32 temp;
 326
 327	if (!iwl_mvm_firmware_running(mvm))
 328		return -EIO;
 329
 330	mutex_lock(&mvm->mutex);
 331	ret = iwl_mvm_get_temp(mvm, &temp);
 332	mutex_unlock(&mvm->mutex);
 333
 334	if (ret)
 335		return -EIO;
 336
 337	pos = scnprintf(buf, sizeof(buf), "%d\n", temp);
 338
 339	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 340}
 341
 342#ifdef CONFIG_ACPI
 343static ssize_t iwl_dbgfs_sar_geo_profile_read(struct file *file,
 344					      char __user *user_buf,
 345					      size_t count, loff_t *ppos)
 346{
 347	struct iwl_mvm *mvm = file->private_data;
 348	char buf[256];
 349	int pos = 0;
 350	int bufsz = sizeof(buf);
 351	int tbl_idx;
 352
 353	if (!iwl_mvm_firmware_running(mvm))
 354		return -EIO;
 355
 356	mutex_lock(&mvm->mutex);
 357	tbl_idx = iwl_mvm_get_sar_geo_profile(mvm);
 358	if (tbl_idx < 0) {
 359		mutex_unlock(&mvm->mutex);
 360		return tbl_idx;
 361	}
 362
 363	if (!tbl_idx) {
 364		pos = scnprintf(buf, bufsz,
 365				"SAR geographic profile disabled\n");
 366	} else {
 367		pos += scnprintf(buf + pos, bufsz - pos,
 368				 "Use geographic profile %d\n", tbl_idx);
 369		pos += scnprintf(buf + pos, bufsz - pos,
 370				 "2.4GHz:\n\tChain A offset: %u dBm\n\tChain B offset: %u dBm\n\tmax tx power: %u dBm\n",
 371				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].chains[0],
 372				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].chains[1],
 373				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].max);
 374		pos += scnprintf(buf + pos, bufsz - pos,
 375				 "5.2GHz:\n\tChain A offset: %u dBm\n\tChain B offset: %u dBm\n\tmax tx power: %u dBm\n",
 376				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].chains[0],
 377				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].chains[1],
 378				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].max);
 379	}
 380	mutex_unlock(&mvm->mutex);
 381
 382	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 383}
 384
 385static ssize_t iwl_dbgfs_wifi_6e_enable_read(struct file *file,
 386					     char __user *user_buf,
 387					     size_t count, loff_t *ppos)
 388{
 389	struct iwl_mvm *mvm = file->private_data;
 390	int err, pos;
 391	char buf[12];
 392	u32 value;
 393
 394	err = iwl_acpi_get_dsm_u32(mvm->fwrt.dev, 0,
 395				   DSM_FUNC_ENABLE_6E,
 396				   &iwl_guid, &value);
 397	if (err)
 398		return err;
 399
 400	pos = sprintf(buf, "0x%08x\n", value);
 401
 402	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 403}
 404#endif
 405
 406static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
 407				       size_t count, loff_t *ppos)
 408{
 409	struct iwl_mvm *mvm = file->private_data;
 410	struct ieee80211_sta *sta;
 411	char buf[400];
 412	int i, pos = 0, bufsz = sizeof(buf);
 413
 414	mutex_lock(&mvm->mutex);
 415
 416	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
 417		pos += scnprintf(buf + pos, bufsz - pos, "%.2d: ", i);
 418		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
 419						lockdep_is_held(&mvm->mutex));
 420		if (!sta)
 421			pos += scnprintf(buf + pos, bufsz - pos, "N/A\n");
 422		else if (IS_ERR(sta))
 423			pos += scnprintf(buf + pos, bufsz - pos, "%ld\n",
 424					 PTR_ERR(sta));
 425		else
 426			pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
 427					 sta->addr);
 428	}
 429
 430	mutex_unlock(&mvm->mutex);
 431
 432	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 433}
 434
 435static ssize_t iwl_dbgfs_rs_data_read(struct ieee80211_link_sta *link_sta,
 436				      struct iwl_mvm_sta *mvmsta,
 437				      struct iwl_mvm *mvm,
 438				      struct iwl_mvm_link_sta *mvm_link_sta,
 439				      char __user *user_buf,
 440				      size_t count, loff_t *ppos)
 441{
 442	struct iwl_lq_sta_rs_fw *lq_sta = &mvm_link_sta->lq_sta.rs_fw;
 
 
 
 443	static const size_t bufsz = 2048;
 444	char *buff;
 445	int desc = 0;
 446	ssize_t ret;
 447
 448	buff = kmalloc(bufsz, GFP_KERNEL);
 449	if (!buff)
 450		return -ENOMEM;
 451
 
 
 452	desc += scnprintf(buff + desc, bufsz - desc, "sta_id %d\n",
 453			  lq_sta->pers.sta_id);
 454	desc += scnprintf(buff + desc, bufsz - desc,
 455			  "fixed rate 0x%X\n",
 456			  lq_sta->pers.dbg_fixed_rate);
 457	desc += scnprintf(buff + desc, bufsz - desc,
 458			  "A-MPDU size limit %d\n",
 459			  lq_sta->pers.dbg_agg_frame_count_lim);
 460	desc += scnprintf(buff + desc, bufsz - desc,
 461			  "valid_tx_ant %s%s\n",
 462		(iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "",
 463		(iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : "");
 464	desc += scnprintf(buff + desc, bufsz - desc,
 465			  "last tx rate=0x%X ",
 466			  lq_sta->last_rate_n_flags);
 467
 468	desc += rs_pretty_print_rate(buff + desc, bufsz - desc,
 469				     lq_sta->last_rate_n_flags);
 470	if (desc < bufsz - 1)
 471		buff[desc++] = '\n';
 
 472
 473	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
 474	kfree(buff);
 475	return ret;
 476}
 477
 478static ssize_t iwl_dbgfs_amsdu_len_write(struct ieee80211_link_sta *link_sta,
 479					 struct iwl_mvm_sta *mvmsta,
 480					 struct iwl_mvm *mvm,
 481					 struct iwl_mvm_link_sta *mvm_link_sta,
 482					 char *buf, size_t count,
 483					 loff_t *ppos)
 484{
 
 485	int i;
 486	u16 amsdu_len;
 487
 488	if (kstrtou16(buf, 0, &amsdu_len))
 489		return -EINVAL;
 490
 491	/* only change from debug set <-> debug unset */
 492	if (amsdu_len && mvm_link_sta->orig_amsdu_len)
 493		return -EBUSY;
 494
 495	if (amsdu_len) {
 496		mvm_link_sta->orig_amsdu_len = link_sta->agg.max_amsdu_len;
 497		link_sta->agg.max_amsdu_len = amsdu_len;
 498		link_sta->agg.max_amsdu_len = amsdu_len;
 499		for (i = 0; i < ARRAY_SIZE(link_sta->agg.max_tid_amsdu_len); i++)
 500			link_sta->agg.max_tid_amsdu_len[i] = amsdu_len;
 501	} else {
 502		link_sta->agg.max_amsdu_len = mvm_link_sta->orig_amsdu_len;
 503		mvm_link_sta->orig_amsdu_len = 0;
 504	}
 505
 506	ieee80211_sta_recalc_aggregates(link_sta->sta);
 507
 508	return count;
 509}
 510
 511static ssize_t iwl_dbgfs_amsdu_len_read(struct ieee80211_link_sta *link_sta,
 512					struct iwl_mvm_sta *mvmsta,
 513					struct iwl_mvm *mvm,
 514					struct iwl_mvm_link_sta *mvm_link_sta,
 515					char __user *user_buf,
 516					size_t count, loff_t *ppos)
 517{
 
 
 
 518	char buf[32];
 519	int pos;
 520
 521	pos = scnprintf(buf, sizeof(buf), "current %d ",
 522			link_sta->agg.max_amsdu_len);
 523	pos += scnprintf(buf + pos, sizeof(buf) - pos, "stored %d\n",
 524			 mvm_link_sta->orig_amsdu_len);
 525
 526	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 527}
 528
 529static ssize_t iwl_dbgfs_disable_power_off_read(struct file *file,
 530						char __user *user_buf,
 531						size_t count, loff_t *ppos)
 532{
 533	struct iwl_mvm *mvm = file->private_data;
 534	char buf[64];
 535	int bufsz = sizeof(buf);
 536	int pos = 0;
 537
 538	pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d0=%d\n",
 539			 mvm->disable_power_off);
 540	pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d3=%d\n",
 541			 mvm->disable_power_off_d3);
 542
 543	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 544}
 545
 546static ssize_t iwl_dbgfs_disable_power_off_write(struct iwl_mvm *mvm, char *buf,
 547						 size_t count, loff_t *ppos)
 548{
 549	int ret, val;
 550
 551	if (!iwl_mvm_firmware_running(mvm))
 552		return -EIO;
 553
 554	if (!strncmp("disable_power_off_d0=", buf, 21)) {
 555		if (sscanf(buf + 21, "%d", &val) != 1)
 556			return -EINVAL;
 557		mvm->disable_power_off = val;
 558	} else if (!strncmp("disable_power_off_d3=", buf, 21)) {
 559		if (sscanf(buf + 21, "%d", &val) != 1)
 560			return -EINVAL;
 561		mvm->disable_power_off_d3 = val;
 562	} else {
 563		return -EINVAL;
 564	}
 565
 566	mutex_lock(&mvm->mutex);
 567	ret = iwl_mvm_power_update_device(mvm);
 568	mutex_unlock(&mvm->mutex);
 569
 570	return ret ?: count;
 571}
 572
 573static
 574int iwl_mvm_coex_dump_mbox(struct iwl_bt_coex_profile_notif *notif, char *buf,
 575			   int pos, int bufsz)
 576{
 577	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw0:\n");
 578
 579	BT_MBOX_PRINT(0, LE_SLAVE_LAT, false);
 580	BT_MBOX_PRINT(0, LE_PROF1, false);
 581	BT_MBOX_PRINT(0, LE_PROF2, false);
 582	BT_MBOX_PRINT(0, LE_PROF_OTHER, false);
 583	BT_MBOX_PRINT(0, CHL_SEQ_N, false);
 584	BT_MBOX_PRINT(0, INBAND_S, false);
 585	BT_MBOX_PRINT(0, LE_MIN_RSSI, false);
 586	BT_MBOX_PRINT(0, LE_SCAN, false);
 587	BT_MBOX_PRINT(0, LE_ADV, false);
 588	BT_MBOX_PRINT(0, LE_MAX_TX_POWER, false);
 589	BT_MBOX_PRINT(0, OPEN_CON_1, true);
 590
 591	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw1:\n");
 592
 593	BT_MBOX_PRINT(1, BR_MAX_TX_POWER, false);
 594	BT_MBOX_PRINT(1, IP_SR, false);
 595	BT_MBOX_PRINT(1, LE_MSTR, false);
 596	BT_MBOX_PRINT(1, AGGR_TRFC_LD, false);
 597	BT_MBOX_PRINT(1, MSG_TYPE, false);
 598	BT_MBOX_PRINT(1, SSN, true);
 599
 600	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw2:\n");
 601
 602	BT_MBOX_PRINT(2, SNIFF_ACT, false);
 603	BT_MBOX_PRINT(2, PAG, false);
 604	BT_MBOX_PRINT(2, INQUIRY, false);
 605	BT_MBOX_PRINT(2, CONN, false);
 606	BT_MBOX_PRINT(2, SNIFF_INTERVAL, false);
 607	BT_MBOX_PRINT(2, DISC, false);
 608	BT_MBOX_PRINT(2, SCO_TX_ACT, false);
 609	BT_MBOX_PRINT(2, SCO_RX_ACT, false);
 610	BT_MBOX_PRINT(2, ESCO_RE_TX, false);
 611	BT_MBOX_PRINT(2, SCO_DURATION, true);
 612
 613	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw3:\n");
 614
 615	BT_MBOX_PRINT(3, SCO_STATE, false);
 616	BT_MBOX_PRINT(3, SNIFF_STATE, false);
 617	BT_MBOX_PRINT(3, A2DP_STATE, false);
 618	BT_MBOX_PRINT(3, A2DP_SRC, false);
 619	BT_MBOX_PRINT(3, ACL_STATE, false);
 620	BT_MBOX_PRINT(3, MSTR_STATE, false);
 621	BT_MBOX_PRINT(3, OBX_STATE, false);
 622	BT_MBOX_PRINT(3, OPEN_CON_2, false);
 623	BT_MBOX_PRINT(3, TRAFFIC_LOAD, false);
 624	BT_MBOX_PRINT(3, CHL_SEQN_LSB, false);
 625	BT_MBOX_PRINT(3, INBAND_P, false);
 626	BT_MBOX_PRINT(3, MSG_TYPE_2, false);
 627	BT_MBOX_PRINT(3, SSN_2, false);
 628	BT_MBOX_PRINT(3, UPDATE_REQUEST, true);
 629
 630	return pos;
 631}
 632
 633static ssize_t iwl_dbgfs_bt_notif_read(struct file *file, char __user *user_buf,
 634				       size_t count, loff_t *ppos)
 635{
 636	struct iwl_mvm *mvm = file->private_data;
 637	struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
 638	char *buf;
 639	int ret, pos = 0, bufsz = sizeof(char) * 1024;
 640
 641	buf = kmalloc(bufsz, GFP_KERNEL);
 642	if (!buf)
 643		return -ENOMEM;
 644
 645	mutex_lock(&mvm->mutex);
 646
 647	pos += iwl_mvm_coex_dump_mbox(notif, buf, pos, bufsz);
 648
 649	pos += scnprintf(buf + pos, bufsz - pos, "bt_ci_compliance = %d\n",
 650			 notif->bt_ci_compliance);
 651	pos += scnprintf(buf + pos, bufsz - pos, "primary_ch_lut = %d\n",
 652			 le32_to_cpu(notif->primary_ch_lut));
 653	pos += scnprintf(buf + pos, bufsz - pos, "secondary_ch_lut = %d\n",
 654			 le32_to_cpu(notif->secondary_ch_lut));
 655	pos += scnprintf(buf + pos,
 656			 bufsz - pos, "bt_activity_grading = %d\n",
 657			 le32_to_cpu(notif->bt_activity_grading));
 658	pos += scnprintf(buf + pos, bufsz - pos, "bt_rrc = %d\n",
 659			 notif->rrc_status & 0xF);
 660	pos += scnprintf(buf + pos, bufsz - pos, "bt_ttc = %d\n",
 661			 notif->ttc_status & 0xF);
 662
 663	pos += scnprintf(buf + pos, bufsz - pos, "sync_sco = %d\n",
 664			 IWL_MVM_BT_COEX_SYNC2SCO);
 665	pos += scnprintf(buf + pos, bufsz - pos, "mplut = %d\n",
 666			 IWL_MVM_BT_COEX_MPLUT);
 667
 668	mutex_unlock(&mvm->mutex);
 669
 670	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 671	kfree(buf);
 672
 673	return ret;
 674}
 675#undef BT_MBOX_PRINT
 676
 677static ssize_t iwl_dbgfs_bt_cmd_read(struct file *file, char __user *user_buf,
 678				     size_t count, loff_t *ppos)
 679{
 680	struct iwl_mvm *mvm = file->private_data;
 681	struct iwl_bt_coex_ci_cmd *cmd = &mvm->last_bt_ci_cmd;
 682	char buf[256];
 683	int bufsz = sizeof(buf);
 684	int pos = 0;
 685
 686	mutex_lock(&mvm->mutex);
 687
 688	pos += scnprintf(buf + pos, bufsz - pos, "Channel inhibition CMD\n");
 689	pos += scnprintf(buf + pos, bufsz - pos,
 690			 "\tPrimary Channel Bitmap 0x%016llx\n",
 691			 le64_to_cpu(cmd->bt_primary_ci));
 692	pos += scnprintf(buf + pos, bufsz - pos,
 693			 "\tSecondary Channel Bitmap 0x%016llx\n",
 694			 le64_to_cpu(cmd->bt_secondary_ci));
 695
 696	mutex_unlock(&mvm->mutex);
 697
 698	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 699}
 700
 701static ssize_t
 702iwl_dbgfs_bt_tx_prio_write(struct iwl_mvm *mvm, char *buf,
 703			   size_t count, loff_t *ppos)
 704{
 705	u32 bt_tx_prio;
 706
 707	if (sscanf(buf, "%u", &bt_tx_prio) != 1)
 708		return -EINVAL;
 709	if (bt_tx_prio > 4)
 710		return -EINVAL;
 711
 712	mvm->bt_tx_prio = bt_tx_prio;
 713
 714	return count;
 715}
 716
 717static ssize_t
 718iwl_dbgfs_bt_force_ant_write(struct iwl_mvm *mvm, char *buf,
 719			     size_t count, loff_t *ppos)
 720{
 721	static const char * const modes_str[BT_FORCE_ANT_MAX] = {
 722		[BT_FORCE_ANT_DIS] = "dis",
 723		[BT_FORCE_ANT_AUTO] = "auto",
 724		[BT_FORCE_ANT_BT] = "bt",
 725		[BT_FORCE_ANT_WIFI] = "wifi",
 726	};
 727	int ret, bt_force_ant_mode;
 728
 729	ret = match_string(modes_str, ARRAY_SIZE(modes_str), buf);
 730	if (ret < 0)
 731		return ret;
 732
 733	bt_force_ant_mode = ret;
 734	ret = 0;
 735	mutex_lock(&mvm->mutex);
 736	if (mvm->bt_force_ant_mode == bt_force_ant_mode)
 737		goto out;
 738
 739	mvm->bt_force_ant_mode = bt_force_ant_mode;
 740	IWL_DEBUG_COEX(mvm, "Force mode: %s\n",
 741		       modes_str[mvm->bt_force_ant_mode]);
 742
 743	if (iwl_mvm_firmware_running(mvm))
 744		ret = iwl_mvm_send_bt_init_conf(mvm);
 745	else
 746		ret = 0;
 747
 748out:
 749	mutex_unlock(&mvm->mutex);
 750	return ret ?: count;
 751}
 752
 753static ssize_t iwl_dbgfs_fw_ver_read(struct file *file, char __user *user_buf,
 754				     size_t count, loff_t *ppos)
 755{
 756	struct iwl_mvm *mvm = file->private_data;
 757	char *buff, *pos, *endpos;
 758	static const size_t bufsz = 1024;
 759	char _fw_name_pre[FW_NAME_PRE_BUFSIZE];
 760	int ret;
 761
 762	buff = kmalloc(bufsz, GFP_KERNEL);
 763	if (!buff)
 764		return -ENOMEM;
 765
 766	pos = buff;
 767	endpos = pos + bufsz;
 768
 769	pos += scnprintf(pos, endpos - pos, "FW prefix: %s\n",
 770			 iwl_drv_get_fwname_pre(mvm->trans, _fw_name_pre));
 771	pos += scnprintf(pos, endpos - pos, "FW: %s\n",
 772			 mvm->fwrt.fw->human_readable);
 773	pos += scnprintf(pos, endpos - pos, "Device: %s\n",
 774			 mvm->fwrt.trans->name);
 775	pos += scnprintf(pos, endpos - pos, "Bus: %s\n",
 776			 mvm->fwrt.dev->bus->name);
 777
 778	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
 779	kfree(buff);
 780
 781	return ret;
 782}
 783
 784static ssize_t iwl_dbgfs_tas_get_status_read(struct file *file,
 785					     char __user *user_buf,
 786					     size_t count, loff_t *ppos)
 787{
 788	struct iwl_mvm *mvm = file->private_data;
 789	struct iwl_mvm_tas_status_resp tas_rsp;
 790	struct iwl_mvm_tas_status_resp *rsp = &tas_rsp;
 791	static const size_t bufsz = 1024;
 792	char *buff, *pos, *endpos;
 793	const char * const tas_dis_reason[TAS_DISABLED_REASON_MAX] = {
 794		[TAS_DISABLED_DUE_TO_BIOS] =
 795			"Due To BIOS",
 796		[TAS_DISABLED_DUE_TO_SAR_6DBM] =
 797			"Due To SAR Limit Less Than 6 dBm",
 798		[TAS_DISABLED_REASON_INVALID] =
 799			"N/A",
 800	};
 801	const char * const tas_current_status[TAS_DYNA_STATUS_MAX] = {
 802		[TAS_DYNA_INACTIVE] = "INACTIVE",
 803		[TAS_DYNA_INACTIVE_MVM_MODE] =
 804			"inactive due to mvm mode",
 805		[TAS_DYNA_INACTIVE_TRIGGER_MODE] =
 806			"inactive due to trigger mode",
 807		[TAS_DYNA_INACTIVE_BLOCK_LISTED] =
 808			"inactive due to block listed",
 809		[TAS_DYNA_INACTIVE_UHB_NON_US] =
 810			"inactive due to uhb non US",
 811		[TAS_DYNA_ACTIVE] = "ACTIVE",
 812	};
 813	struct iwl_host_cmd hcmd = {
 814		.id = WIDE_ID(DEBUG_GROUP, GET_TAS_STATUS),
 815		.flags = CMD_WANT_SKB,
 816		.len = { 0, },
 817		.data = { NULL, },
 818	};
 819	int ret, i, tmp;
 820	bool tas_enabled = false;
 821	unsigned long dyn_status;
 822
 823	if (!iwl_mvm_firmware_running(mvm))
 824		return -ENODEV;
 825
 826	mutex_lock(&mvm->mutex);
 827	ret = iwl_mvm_send_cmd(mvm, &hcmd);
 828	mutex_unlock(&mvm->mutex);
 829	if (ret < 0)
 830		return ret;
 831
 832	buff = kzalloc(bufsz, GFP_KERNEL);
 833	if (!buff)
 834		return -ENOMEM;
 835	pos = buff;
 836	endpos = pos + bufsz;
 837
 838	rsp = (void *)hcmd.resp_pkt->data;
 839
 840	pos += scnprintf(pos, endpos - pos, "TAS Conclusion:\n");
 841	for (i = 0; i < rsp->in_dual_radio + 1; i++) {
 842		if (rsp->tas_status_mac[i].band != TAS_LMAC_BAND_INVALID &&
 843		    rsp->tas_status_mac[i].dynamic_status & BIT(TAS_DYNA_ACTIVE)) {
 844			pos += scnprintf(pos, endpos - pos, "\tON for ");
 845			switch (rsp->tas_status_mac[i].band) {
 846			case TAS_LMAC_BAND_HB:
 847				pos += scnprintf(pos, endpos - pos, "HB\n");
 848				break;
 849			case TAS_LMAC_BAND_LB:
 850				pos += scnprintf(pos, endpos - pos, "LB\n");
 851				break;
 852			case TAS_LMAC_BAND_UHB:
 853				pos += scnprintf(pos, endpos - pos, "UHB\n");
 854				break;
 855			case TAS_LMAC_BAND_INVALID:
 856				pos += scnprintf(pos, endpos - pos,
 857						 "INVALID BAND\n");
 858				break;
 859			default:
 860				pos += scnprintf(pos, endpos - pos,
 861						 "Unsupported band (%d)\n",
 862						 rsp->tas_status_mac[i].band);
 863				goto out;
 864			}
 865			tas_enabled = true;
 866		}
 867	}
 868	if (!tas_enabled)
 869		pos += scnprintf(pos, endpos - pos, "\tOFF\n");
 870
 871	pos += scnprintf(pos, endpos - pos, "TAS Report\n");
 872	pos += scnprintf(pos, endpos - pos, "TAS FW version: %d\n",
 873			 rsp->tas_fw_version);
 874	pos += scnprintf(pos, endpos - pos, "Is UHB enabled for USA?: %s\n",
 875			 rsp->is_uhb_for_usa_enable ? "True" : "False");
 876	pos += scnprintf(pos, endpos - pos, "Current MCC: 0x%x\n",
 877			 le16_to_cpu(rsp->curr_mcc));
 878
 879	pos += scnprintf(pos, endpos - pos, "Block list entries:");
 880	for (i = 0; i < APCI_WTAS_BLACK_LIST_MAX; i++)
 881		pos += scnprintf(pos, endpos - pos, " 0x%x",
 882				 le16_to_cpu(rsp->block_list[i]));
 883
 884	pos += scnprintf(pos, endpos - pos, "\nOEM name: %s\n",
 885			 dmi_get_system_info(DMI_SYS_VENDOR));
 886	pos += scnprintf(pos, endpos - pos, "\tVendor In Approved List: %s\n",
 887			 iwl_mvm_is_vendor_in_approved_list() ? "YES" : "NO");
 888	pos += scnprintf(pos, endpos - pos,
 889			 "\tDo TAS Support Dual Radio?: %s\n",
 890			 rsp->in_dual_radio ? "TRUE" : "FALSE");
 891
 892	for (i = 0; i < rsp->in_dual_radio + 1; i++) {
 893		if (rsp->tas_status_mac[i].static_status == 0) {
 894			pos += scnprintf(pos, endpos - pos,
 895					 "Static status: disabled\n");
 896			pos += scnprintf(pos, endpos - pos,
 897					 "Static disabled reason: %s (0)\n",
 898					 tas_dis_reason[0]);
 899			goto out;
 900		}
 901
 902		pos += scnprintf(pos, endpos - pos, "TAS status for ");
 903		switch (rsp->tas_status_mac[i].band) {
 904		case TAS_LMAC_BAND_HB:
 905			pos += scnprintf(pos, endpos - pos, "High band\n");
 906			break;
 907		case TAS_LMAC_BAND_LB:
 908			pos += scnprintf(pos, endpos - pos, "Low band\n");
 909			break;
 910		case TAS_LMAC_BAND_UHB:
 911			pos += scnprintf(pos, endpos - pos,
 912					 "Ultra high band\n");
 913			break;
 914		case TAS_LMAC_BAND_INVALID:
 915			pos += scnprintf(pos, endpos - pos,
 916					 "INVALID band\n");
 917			break;
 918		default:
 919			pos += scnprintf(pos, endpos - pos,
 920					 "Unsupported band (%d)\n",
 921					 rsp->tas_status_mac[i].band);
 922			goto out;
 923		}
 924		pos += scnprintf(pos, endpos - pos, "Static status: %sabled\n",
 925				 rsp->tas_status_mac[i].static_status ?
 926				 "En" : "Dis");
 927		pos += scnprintf(pos, endpos - pos,
 928				 "\tStatic Disabled Reason: ");
 929		if (rsp->tas_status_mac[i].static_dis_reason < TAS_DISABLED_REASON_MAX)
 930			pos += scnprintf(pos, endpos - pos, "%s (%d)\n",
 931					 tas_dis_reason[rsp->tas_status_mac[i].static_dis_reason],
 932					 rsp->tas_status_mac[i].static_dis_reason);
 933		else
 934			pos += scnprintf(pos, endpos - pos,
 935					 "unsupported value (%d)\n",
 936					 rsp->tas_status_mac[i].static_dis_reason);
 937
 938		pos += scnprintf(pos, endpos - pos, "Dynamic status:\n");
 939		dyn_status = (rsp->tas_status_mac[i].dynamic_status);
 940		for_each_set_bit(tmp, &dyn_status, sizeof(dyn_status)) {
 941			if (tmp >= 0 && tmp < TAS_DYNA_STATUS_MAX)
 942				pos += scnprintf(pos, endpos - pos,
 943						 "\t%s (%d)\n",
 944						 tas_current_status[tmp], tmp);
 945		}
 946
 947		pos += scnprintf(pos, endpos - pos,
 948				 "Is near disconnection?: %s\n",
 949				 rsp->tas_status_mac[i].near_disconnection ?
 950				 "True" : "False");
 951		tmp = le16_to_cpu(rsp->tas_status_mac[i].max_reg_pwr_limit);
 952		pos += scnprintf(pos, endpos - pos,
 953				 "Max. regulatory pwr limit (dBm): %d.%03d\n",
 954				 tmp / 8, 125 * (tmp % 8));
 955		tmp = le16_to_cpu(rsp->tas_status_mac[i].sar_limit);
 956		pos += scnprintf(pos, endpos - pos,
 957				 "SAR limit (dBm): %d.%03d\n",
 958				 tmp / 8, 125 * (tmp % 8));
 959	}
 960
 961out:
 962	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
 963	kfree(buff);
 964	iwl_free_resp(&hcmd);
 965	return ret;
 966}
 967
 968static ssize_t iwl_dbgfs_phy_integration_ver_read(struct file *file,
 969						  char __user *user_buf,
 970						  size_t count, loff_t *ppos)
 971{
 972	struct iwl_mvm *mvm = file->private_data;
 973	char *buf;
 974	size_t bufsz;
 975	int pos;
 976	ssize_t ret;
 977
 978	bufsz = mvm->fw->phy_integration_ver_len + 2;
 979	buf = kmalloc(bufsz, GFP_KERNEL);
 980	if (!buf)
 981		return -ENOMEM;
 982
 983	pos = scnprintf(buf, bufsz, "%.*s\n", mvm->fw->phy_integration_ver_len,
 984			mvm->fw->phy_integration_ver);
 985
 986	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 987
 988	kfree(buf);
 989	return ret;
 990}
 991
 992#define PRINT_STATS_LE32(_struct, _memb)				\
 993			 pos += scnprintf(buf + pos, bufsz - pos,	\
 994					  fmt_table, #_memb,		\
 995					  le32_to_cpu(_struct->_memb))
 996
 997static ssize_t iwl_dbgfs_fw_rx_stats_read(struct file *file,
 998					  char __user *user_buf, size_t count,
 999					  loff_t *ppos)
1000{
1001	struct iwl_mvm *mvm = file->private_data;
1002	static const char *fmt_table = "\t%-30s %10u\n";
1003	static const char *fmt_header = "%-32s\n";
1004	int pos = 0;
1005	char *buf;
1006	int ret;
1007	size_t bufsz;
1008	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1009					   WIDE_ID(SYSTEM_GROUP,
1010						   SYSTEM_STATISTICS_CMD),
1011					   IWL_FW_CMD_VER_UNKNOWN);
1012
1013	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
1014		return -EOPNOTSUPP;
1015
1016	if (iwl_mvm_has_new_rx_stats_api(mvm))
1017		bufsz = ((sizeof(struct mvm_statistics_rx) /
1018			  sizeof(__le32)) * 43) + (4 * 33) + 1;
1019	else
1020		/* 43 = size of each data line; 33 = size of each header */
1021		bufsz = ((sizeof(struct mvm_statistics_rx_v3) /
1022			  sizeof(__le32)) * 43) + (4 * 33) + 1;
1023
1024	buf = kzalloc(bufsz, GFP_KERNEL);
1025	if (!buf)
1026		return -ENOMEM;
1027
1028	mutex_lock(&mvm->mutex);
1029
1030	if (iwl_mvm_firmware_running(mvm))
1031		iwl_mvm_request_statistics(mvm, false);
1032
1033	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
1034			 "Statistics_Rx - OFDM");
1035	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1036		struct mvm_statistics_rx_phy_v2 *ofdm = &mvm->rx_stats_v3.ofdm;
1037
1038		PRINT_STATS_LE32(ofdm, ina_cnt);
1039		PRINT_STATS_LE32(ofdm, fina_cnt);
1040		PRINT_STATS_LE32(ofdm, plcp_err);
1041		PRINT_STATS_LE32(ofdm, crc32_err);
1042		PRINT_STATS_LE32(ofdm, overrun_err);
1043		PRINT_STATS_LE32(ofdm, early_overrun_err);
1044		PRINT_STATS_LE32(ofdm, crc32_good);
1045		PRINT_STATS_LE32(ofdm, false_alarm_cnt);
1046		PRINT_STATS_LE32(ofdm, fina_sync_err_cnt);
1047		PRINT_STATS_LE32(ofdm, sfd_timeout);
1048		PRINT_STATS_LE32(ofdm, fina_timeout);
1049		PRINT_STATS_LE32(ofdm, unresponded_rts);
1050		PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
1051		PRINT_STATS_LE32(ofdm, sent_ack_cnt);
1052		PRINT_STATS_LE32(ofdm, sent_cts_cnt);
1053		PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
1054		PRINT_STATS_LE32(ofdm, dsp_self_kill);
1055		PRINT_STATS_LE32(ofdm, mh_format_err);
1056		PRINT_STATS_LE32(ofdm, re_acq_main_rssi_sum);
1057		PRINT_STATS_LE32(ofdm, reserved);
1058	} else {
1059		struct mvm_statistics_rx_phy *ofdm = &mvm->rx_stats.ofdm;
1060
1061		PRINT_STATS_LE32(ofdm, unresponded_rts);
1062		PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
1063		PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
1064		PRINT_STATS_LE32(ofdm, dsp_self_kill);
1065		PRINT_STATS_LE32(ofdm, reserved);
1066	}
1067
1068	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
1069			 "Statistics_Rx - CCK");
1070	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1071		struct mvm_statistics_rx_phy_v2 *cck = &mvm->rx_stats_v3.cck;
1072
1073		PRINT_STATS_LE32(cck, ina_cnt);
1074		PRINT_STATS_LE32(cck, fina_cnt);
1075		PRINT_STATS_LE32(cck, plcp_err);
1076		PRINT_STATS_LE32(cck, crc32_err);
1077		PRINT_STATS_LE32(cck, overrun_err);
1078		PRINT_STATS_LE32(cck, early_overrun_err);
1079		PRINT_STATS_LE32(cck, crc32_good);
1080		PRINT_STATS_LE32(cck, false_alarm_cnt);
1081		PRINT_STATS_LE32(cck, fina_sync_err_cnt);
1082		PRINT_STATS_LE32(cck, sfd_timeout);
1083		PRINT_STATS_LE32(cck, fina_timeout);
1084		PRINT_STATS_LE32(cck, unresponded_rts);
1085		PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
1086		PRINT_STATS_LE32(cck, sent_ack_cnt);
1087		PRINT_STATS_LE32(cck, sent_cts_cnt);
1088		PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
1089		PRINT_STATS_LE32(cck, dsp_self_kill);
1090		PRINT_STATS_LE32(cck, mh_format_err);
1091		PRINT_STATS_LE32(cck, re_acq_main_rssi_sum);
1092		PRINT_STATS_LE32(cck, reserved);
1093	} else {
1094		struct mvm_statistics_rx_phy *cck = &mvm->rx_stats.cck;
1095
1096		PRINT_STATS_LE32(cck, unresponded_rts);
1097		PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
1098		PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
1099		PRINT_STATS_LE32(cck, dsp_self_kill);
1100		PRINT_STATS_LE32(cck, reserved);
1101	}
1102
1103	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
1104			 "Statistics_Rx - GENERAL");
1105	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1106		struct mvm_statistics_rx_non_phy_v3 *general =
1107			&mvm->rx_stats_v3.general;
1108
1109		PRINT_STATS_LE32(general, bogus_cts);
1110		PRINT_STATS_LE32(general, bogus_ack);
1111		PRINT_STATS_LE32(general, non_bssid_frames);
1112		PRINT_STATS_LE32(general, filtered_frames);
1113		PRINT_STATS_LE32(general, non_channel_beacons);
1114		PRINT_STATS_LE32(general, channel_beacons);
1115		PRINT_STATS_LE32(general, num_missed_bcon);
1116		PRINT_STATS_LE32(general, adc_rx_saturation_time);
1117		PRINT_STATS_LE32(general, ina_detection_search_time);
1118		PRINT_STATS_LE32(general, beacon_silence_rssi_a);
1119		PRINT_STATS_LE32(general, beacon_silence_rssi_b);
1120		PRINT_STATS_LE32(general, beacon_silence_rssi_c);
1121		PRINT_STATS_LE32(general, interference_data_flag);
1122		PRINT_STATS_LE32(general, channel_load);
1123		PRINT_STATS_LE32(general, dsp_false_alarms);
1124		PRINT_STATS_LE32(general, beacon_rssi_a);
1125		PRINT_STATS_LE32(general, beacon_rssi_b);
1126		PRINT_STATS_LE32(general, beacon_rssi_c);
1127		PRINT_STATS_LE32(general, beacon_energy_a);
1128		PRINT_STATS_LE32(general, beacon_energy_b);
1129		PRINT_STATS_LE32(general, beacon_energy_c);
1130		PRINT_STATS_LE32(general, num_bt_kills);
1131		PRINT_STATS_LE32(general, mac_id);
1132		PRINT_STATS_LE32(general, directed_data_mpdu);
1133	} else {
1134		struct mvm_statistics_rx_non_phy *general =
1135			&mvm->rx_stats.general;
1136
1137		PRINT_STATS_LE32(general, bogus_cts);
1138		PRINT_STATS_LE32(general, bogus_ack);
1139		PRINT_STATS_LE32(general, non_channel_beacons);
1140		PRINT_STATS_LE32(general, channel_beacons);
1141		PRINT_STATS_LE32(general, num_missed_bcon);
1142		PRINT_STATS_LE32(general, adc_rx_saturation_time);
1143		PRINT_STATS_LE32(general, ina_detection_search_time);
1144		PRINT_STATS_LE32(general, beacon_silence_rssi_a);
1145		PRINT_STATS_LE32(general, beacon_silence_rssi_b);
1146		PRINT_STATS_LE32(general, beacon_silence_rssi_c);
1147		PRINT_STATS_LE32(general, interference_data_flag);
1148		PRINT_STATS_LE32(general, channel_load);
1149		PRINT_STATS_LE32(general, beacon_rssi_a);
1150		PRINT_STATS_LE32(general, beacon_rssi_b);
1151		PRINT_STATS_LE32(general, beacon_rssi_c);
1152		PRINT_STATS_LE32(general, beacon_energy_a);
1153		PRINT_STATS_LE32(general, beacon_energy_b);
1154		PRINT_STATS_LE32(general, beacon_energy_c);
1155		PRINT_STATS_LE32(general, num_bt_kills);
1156		PRINT_STATS_LE32(general, mac_id);
1157	}
1158
1159	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
1160			 "Statistics_Rx - HT");
1161	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1162		struct mvm_statistics_rx_ht_phy_v1 *ht =
1163			&mvm->rx_stats_v3.ofdm_ht;
1164
1165		PRINT_STATS_LE32(ht, plcp_err);
1166		PRINT_STATS_LE32(ht, overrun_err);
1167		PRINT_STATS_LE32(ht, early_overrun_err);
1168		PRINT_STATS_LE32(ht, crc32_good);
1169		PRINT_STATS_LE32(ht, crc32_err);
1170		PRINT_STATS_LE32(ht, mh_format_err);
1171		PRINT_STATS_LE32(ht, agg_crc32_good);
1172		PRINT_STATS_LE32(ht, agg_mpdu_cnt);
1173		PRINT_STATS_LE32(ht, agg_cnt);
1174		PRINT_STATS_LE32(ht, unsupport_mcs);
1175	} else {
1176		struct mvm_statistics_rx_ht_phy *ht =
1177			&mvm->rx_stats.ofdm_ht;
1178
1179		PRINT_STATS_LE32(ht, mh_format_err);
1180		PRINT_STATS_LE32(ht, agg_mpdu_cnt);
1181		PRINT_STATS_LE32(ht, agg_cnt);
1182		PRINT_STATS_LE32(ht, unsupport_mcs);
1183	}
1184
1185	mutex_unlock(&mvm->mutex);
1186
1187	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1188	kfree(buf);
1189
1190	return ret;
1191}
1192#undef PRINT_STAT_LE32
1193
1194static ssize_t iwl_dbgfs_fw_system_stats_read(struct file *file,
1195					      char __user *user_buf,
1196					      size_t count, loff_t *ppos)
1197{
1198	char *buff, *pos, *endpos;
1199	int ret;
1200	size_t bufsz;
1201	int i;
1202	struct iwl_mvm_vif *mvmvif;
1203	struct ieee80211_vif *vif;
1204	struct iwl_mvm *mvm = file->private_data;
1205	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1206					   WIDE_ID(SYSTEM_GROUP,
1207						   SYSTEM_STATISTICS_CMD),
1208					   IWL_FW_CMD_VER_UNKNOWN);
1209
1210	/* in case of a wrong cmd version, allocate buffer only for error msg */
1211	bufsz = (cmd_ver == 1) ? 4096 : 64;
1212
1213	buff = kzalloc(bufsz, GFP_KERNEL);
1214	if (!buff)
1215		return -ENOMEM;
1216
1217	pos = buff;
1218	endpos = pos + bufsz;
1219
1220	if (cmd_ver != 1) {
1221		pos += scnprintf(pos, endpos - pos,
1222				 "System stats not supported:%d\n", cmd_ver);
1223		goto send_out;
1224	}
1225
1226	mutex_lock(&mvm->mutex);
1227	if (iwl_mvm_firmware_running(mvm))
1228		iwl_mvm_request_statistics(mvm, false);
1229
1230	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
1231		vif = iwl_mvm_rcu_dereference_vif_id(mvm, i, false);
1232		if (!vif)
1233			continue;
1234
1235		if (vif->type == NL80211_IFTYPE_STATION)
1236			break;
1237	}
1238
1239	if (i == NUM_MAC_INDEX_DRIVER || !vif) {
1240		pos += scnprintf(pos, endpos - pos, "vif is NULL\n");
1241		goto release_send_out;
1242	}
1243
1244	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1245	if (!mvmvif) {
1246		pos += scnprintf(pos, endpos - pos, "mvmvif is NULL\n");
1247		goto release_send_out;
1248	}
1249
1250	for_each_mvm_vif_valid_link(mvmvif, i) {
1251		struct iwl_mvm_vif_link_info *link_info = mvmvif->link[i];
1252
1253		pos += scnprintf(pos, endpos - pos,
1254				 "link_id %d", i);
1255		pos += scnprintf(pos, endpos - pos,
1256				 " num_beacons %d",
1257				 link_info->beacon_stats.num_beacons);
1258		pos += scnprintf(pos, endpos - pos,
1259				 " accu_num_beacons %d",
1260				 link_info->beacon_stats.accu_num_beacons);
1261		pos += scnprintf(pos, endpos - pos,
1262				 " avg_signal %d\n",
1263				 link_info->beacon_stats.avg_signal);
1264	}
1265
1266	pos += scnprintf(pos, endpos - pos,
1267			 "radio_stats.rx_time %lld\n",
1268			 mvm->radio_stats.rx_time);
1269	pos += scnprintf(pos, endpos - pos,
1270			 "radio_stats.tx_time %lld\n",
1271			 mvm->radio_stats.tx_time);
1272	pos += scnprintf(pos, endpos - pos,
1273			 "accu_radio_stats.rx_time %lld\n",
1274			 mvm->accu_radio_stats.rx_time);
1275	pos += scnprintf(pos, endpos - pos,
1276			 "accu_radio_stats.tx_time %lld\n",
1277			 mvm->accu_radio_stats.tx_time);
1278
1279release_send_out:
1280	mutex_unlock(&mvm->mutex);
1281
1282send_out:
1283	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
1284	kfree(buff);
1285
1286	return ret;
1287}
1288
1289static ssize_t iwl_dbgfs_frame_stats_read(struct iwl_mvm *mvm,
1290					  char __user *user_buf, size_t count,
1291					  loff_t *ppos,
1292					  struct iwl_mvm_frame_stats *stats)
1293{
1294	char *buff, *pos, *endpos;
1295	int idx, i;
1296	int ret;
1297	static const size_t bufsz = 1024;
1298
1299	buff = kmalloc(bufsz, GFP_KERNEL);
1300	if (!buff)
1301		return -ENOMEM;
1302
1303	spin_lock_bh(&mvm->drv_stats_lock);
1304
1305	pos = buff;
1306	endpos = pos + bufsz;
1307
1308	pos += scnprintf(pos, endpos - pos,
1309			 "Legacy/HT/VHT\t:\t%d/%d/%d\n",
1310			 stats->legacy_frames,
1311			 stats->ht_frames,
1312			 stats->vht_frames);
1313	pos += scnprintf(pos, endpos - pos, "20/40/80\t:\t%d/%d/%d\n",
1314			 stats->bw_20_frames,
1315			 stats->bw_40_frames,
1316			 stats->bw_80_frames);
1317	pos += scnprintf(pos, endpos - pos, "NGI/SGI\t\t:\t%d/%d\n",
1318			 stats->ngi_frames,
1319			 stats->sgi_frames);
1320	pos += scnprintf(pos, endpos - pos, "SISO/MIMO2\t:\t%d/%d\n",
1321			 stats->siso_frames,
1322			 stats->mimo2_frames);
1323	pos += scnprintf(pos, endpos - pos, "FAIL/SCSS\t:\t%d/%d\n",
1324			 stats->fail_frames,
1325			 stats->success_frames);
1326	pos += scnprintf(pos, endpos - pos, "MPDUs agg\t:\t%d\n",
1327			 stats->agg_frames);
1328	pos += scnprintf(pos, endpos - pos, "A-MPDUs\t\t:\t%d\n",
1329			 stats->ampdu_count);
1330	pos += scnprintf(pos, endpos - pos, "Avg MPDUs/A-MPDU:\t%d\n",
1331			 stats->ampdu_count > 0 ?
1332			 (stats->agg_frames / stats->ampdu_count) : 0);
1333
1334	pos += scnprintf(pos, endpos - pos, "Last Rates\n");
1335
1336	idx = stats->last_frame_idx - 1;
1337	for (i = 0; i < ARRAY_SIZE(stats->last_rates); i++) {
1338		idx = (idx + 1) % ARRAY_SIZE(stats->last_rates);
1339		if (stats->last_rates[idx] == 0)
1340			continue;
1341		pos += scnprintf(pos, endpos - pos, "Rate[%d]: ",
1342				 (int)(ARRAY_SIZE(stats->last_rates) - i));
1343		pos += rs_pretty_print_rate_v1(pos, endpos - pos,
1344					       stats->last_rates[idx]);
1345		if (pos < endpos - 1)
1346			*pos++ = '\n';
1347	}
1348	spin_unlock_bh(&mvm->drv_stats_lock);
1349
1350	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
1351	kfree(buff);
1352
1353	return ret;
1354}
1355
1356static ssize_t iwl_dbgfs_drv_rx_stats_read(struct file *file,
1357					   char __user *user_buf, size_t count,
1358					   loff_t *ppos)
1359{
1360	struct iwl_mvm *mvm = file->private_data;
1361
1362	return iwl_dbgfs_frame_stats_read(mvm, user_buf, count, ppos,
1363					  &mvm->drv_rx_stats);
1364}
1365
1366static ssize_t iwl_dbgfs_fw_restart_write(struct iwl_mvm *mvm, char *buf,
1367					  size_t count, loff_t *ppos)
1368{
1369	int __maybe_unused ret;
1370
1371	if (!iwl_mvm_firmware_running(mvm))
1372		return -EIO;
1373
1374	mutex_lock(&mvm->mutex);
1375
1376	/* allow one more restart that we're provoking here */
1377	if (mvm->fw_restart >= 0)
1378		mvm->fw_restart++;
1379
1380	if (count == 6 && !strcmp(buf, "nolog\n")) {
1381		set_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE, &mvm->status);
1382		set_bit(STATUS_SUPPRESS_CMD_ERROR_ONCE, &mvm->trans->status);
1383	}
1384
1385	/* take the return value to make compiler happy - it will fail anyway */
1386	ret = iwl_mvm_send_cmd_pdu(mvm,
1387				   WIDE_ID(LONG_GROUP, REPLY_ERROR),
1388				   0, 0, NULL);
1389
1390	mutex_unlock(&mvm->mutex);
1391
1392	return count;
1393}
1394
1395static ssize_t iwl_dbgfs_fw_nmi_write(struct iwl_mvm *mvm, char *buf,
1396				      size_t count, loff_t *ppos)
1397{
1398	if (!iwl_mvm_firmware_running(mvm))
1399		return -EIO;
1400
1401	if (count == 6 && !strcmp(buf, "nolog\n"))
1402		set_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE, &mvm->status);
1403
1404	iwl_force_nmi(mvm->trans);
1405
1406	return count;
1407}
1408
1409static ssize_t
1410iwl_dbgfs_scan_ant_rxchain_read(struct file *file,
1411				char __user *user_buf,
1412				size_t count, loff_t *ppos)
1413{
1414	struct iwl_mvm *mvm = file->private_data;
1415	int pos = 0;
1416	char buf[32];
1417	const size_t bufsz = sizeof(buf);
1418
1419	/* print which antennas were set for the scan command by the user */
1420	pos += scnprintf(buf + pos, bufsz - pos, "Antennas for scan: ");
1421	if (mvm->scan_rx_ant & ANT_A)
1422		pos += scnprintf(buf + pos, bufsz - pos, "A");
1423	if (mvm->scan_rx_ant & ANT_B)
1424		pos += scnprintf(buf + pos, bufsz - pos, "B");
1425	pos += scnprintf(buf + pos, bufsz - pos, " (%x)\n", mvm->scan_rx_ant);
1426
1427	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1428}
1429
1430static ssize_t
1431iwl_dbgfs_scan_ant_rxchain_write(struct iwl_mvm *mvm, char *buf,
1432				 size_t count, loff_t *ppos)
1433{
1434	u8 scan_rx_ant;
1435
1436	if (!iwl_mvm_firmware_running(mvm))
1437		return -EIO;
1438
1439	if (sscanf(buf, "%hhx", &scan_rx_ant) != 1)
1440		return -EINVAL;
1441	if (scan_rx_ant > ANT_ABC)
1442		return -EINVAL;
1443	if (scan_rx_ant & ~(iwl_mvm_get_valid_rx_ant(mvm)))
1444		return -EINVAL;
1445
1446	if (mvm->scan_rx_ant != scan_rx_ant) {
1447		mvm->scan_rx_ant = scan_rx_ant;
1448		if (fw_has_capa(&mvm->fw->ucode_capa,
1449				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1450			iwl_mvm_config_scan(mvm);
1451	}
1452
1453	return count;
1454}
1455
1456static ssize_t iwl_dbgfs_indirection_tbl_write(struct iwl_mvm *mvm,
1457					       char *buf, size_t count,
1458					       loff_t *ppos)
1459{
1460	struct iwl_rss_config_cmd cmd = {
1461		.flags = cpu_to_le32(IWL_RSS_ENABLE),
1462		.hash_mask = IWL_RSS_HASH_TYPE_IPV4_TCP |
1463			     IWL_RSS_HASH_TYPE_IPV4_UDP |
1464			     IWL_RSS_HASH_TYPE_IPV4_PAYLOAD |
1465			     IWL_RSS_HASH_TYPE_IPV6_TCP |
1466			     IWL_RSS_HASH_TYPE_IPV6_UDP |
1467			     IWL_RSS_HASH_TYPE_IPV6_PAYLOAD,
1468	};
1469	int ret, i, num_repeats, nbytes = count / 2;
1470
1471	ret = hex2bin(cmd.indirection_table, buf, nbytes);
1472	if (ret)
1473		return ret;
1474
1475	/*
1476	 * The input is the redirection table, partial or full.
1477	 * Repeat the pattern if needed.
1478	 * For example, input of 01020F will be repeated 42 times,
1479	 * indirecting RSS hash results to queues 1, 2, 15 (skipping
1480	 * queues 3 - 14).
1481	 */
1482	num_repeats = ARRAY_SIZE(cmd.indirection_table) / nbytes;
1483	for (i = 1; i < num_repeats; i++)
1484		memcpy(&cmd.indirection_table[i * nbytes],
1485		       cmd.indirection_table, nbytes);
1486	/* handle cut in the middle pattern for the last places */
1487	memcpy(&cmd.indirection_table[i * nbytes], cmd.indirection_table,
1488	       ARRAY_SIZE(cmd.indirection_table) % nbytes);
1489
1490	netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
1491
1492	mutex_lock(&mvm->mutex);
1493	if (iwl_mvm_firmware_running(mvm))
1494		ret = iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0,
1495					   sizeof(cmd), &cmd);
1496	else
1497		ret = 0;
1498	mutex_unlock(&mvm->mutex);
1499
1500	return ret ?: count;
1501}
1502
1503static ssize_t iwl_dbgfs_inject_packet_write(struct iwl_mvm *mvm,
1504					     char *buf, size_t count,
1505					     loff_t *ppos)
1506{
1507	struct iwl_op_mode *opmode = container_of((void *)mvm,
1508						  struct iwl_op_mode,
1509						  op_mode_specific);
1510	struct iwl_rx_cmd_buffer rxb = {
1511		._rx_page_order = 0,
1512		.truesize = 0, /* not used */
1513		._offset = 0,
1514	};
1515	struct iwl_rx_packet *pkt;
1516	int bin_len = count / 2;
1517	int ret = -EINVAL;
1518
1519	if (!iwl_mvm_firmware_running(mvm))
1520		return -EIO;
1521
1522	/* supporting only MQ RX */
1523	if (!mvm->trans->trans_cfg->mq_rx_supported)
1524		return -EOPNOTSUPP;
1525
1526	rxb._page = alloc_pages(GFP_ATOMIC, 0);
1527	if (!rxb._page)
1528		return -ENOMEM;
1529	pkt = rxb_addr(&rxb);
1530
1531	ret = hex2bin(page_address(rxb._page), buf, bin_len);
1532	if (ret)
1533		goto out;
1534
1535	/* avoid invalid memory access and malformed packet */
1536	if (bin_len < sizeof(*pkt) ||
1537	    bin_len != sizeof(*pkt) + iwl_rx_packet_payload_len(pkt))
1538		goto out;
1539
1540	local_bh_disable();
1541	iwl_mvm_rx_mq(opmode, NULL, &rxb);
1542	local_bh_enable();
1543	ret = 0;
1544
1545out:
1546	iwl_free_rxb(&rxb);
1547
1548	return ret ?: count;
1549}
1550
1551static int _iwl_dbgfs_inject_beacon_ie(struct iwl_mvm *mvm, char *bin, int len)
1552{
1553	struct ieee80211_vif *vif;
1554	struct iwl_mvm_vif *mvmvif;
1555	struct sk_buff *beacon;
1556	struct ieee80211_tx_info *info;
1557	struct iwl_mac_beacon_cmd beacon_cmd = {};
1558	unsigned int link_id;
1559	u8 rate;
1560	int i;
1561
1562	len /= 2;
1563
1564	/* Element len should be represented by u8 */
1565	if (len >= U8_MAX)
1566		return -EINVAL;
1567
1568	if (!iwl_mvm_firmware_running(mvm))
1569		return -EIO;
1570
1571	if (!iwl_mvm_has_new_tx_api(mvm) &&
1572	    !fw_has_api(&mvm->fw->ucode_capa,
1573			IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
1574		return -EINVAL;
1575
1576	mutex_lock(&mvm->mutex);
1577
1578	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
1579		vif = iwl_mvm_rcu_dereference_vif_id(mvm, i, false);
1580		if (!vif)
1581			continue;
1582
1583		if (vif->type == NL80211_IFTYPE_AP)
1584			break;
1585	}
1586
1587	if (i == NUM_MAC_INDEX_DRIVER || !vif)
1588		goto out_err;
1589
1590	mvm->hw->extra_beacon_tailroom = len;
1591
1592	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL, 0);
1593	if (!beacon)
1594		goto out_err;
1595
1596	if (len && hex2bin(skb_put_zero(beacon, len), bin, len)) {
1597		dev_kfree_skb(beacon);
1598		goto out_err;
1599	}
1600
1601	mvm->beacon_inject_active = true;
1602
1603	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1604	info = IEEE80211_SKB_CB(beacon);
1605	rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1606
1607	for_each_mvm_vif_valid_link(mvmvif, link_id) {
1608		beacon_cmd.flags =
1609			cpu_to_le16(iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw,
1610								      rate));
1611		beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1612		if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)
1613			beacon_cmd.link_id =
1614				cpu_to_le32(mvmvif->link[link_id]->fw_link_id);
1615		else
1616			beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);
1617
1618		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1619					 &beacon_cmd.tim_size,
1620					 beacon->data, beacon->len);
1621
1622		iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1623						 sizeof(beacon_cmd));
1624	}
1625	mutex_unlock(&mvm->mutex);
1626
1627	dev_kfree_skb(beacon);
1628
1629	return 0;
1630
1631out_err:
1632	mutex_unlock(&mvm->mutex);
1633	return -EINVAL;
1634}
1635
1636static ssize_t iwl_dbgfs_inject_beacon_ie_write(struct iwl_mvm *mvm,
1637						char *buf, size_t count,
1638						loff_t *ppos)
1639{
1640	int ret = _iwl_dbgfs_inject_beacon_ie(mvm, buf, count);
1641
1642	mvm->hw->extra_beacon_tailroom = 0;
1643	return ret ?: count;
1644}
1645
1646static ssize_t iwl_dbgfs_inject_beacon_ie_restore_write(struct iwl_mvm *mvm,
1647							char *buf,
1648							size_t count,
1649							loff_t *ppos)
1650{
1651	int ret = _iwl_dbgfs_inject_beacon_ie(mvm, NULL, 0);
1652
1653	mvm->hw->extra_beacon_tailroom = 0;
1654	mvm->beacon_inject_active = false;
1655	return ret ?: count;
1656}
1657
1658static ssize_t iwl_dbgfs_fw_dbg_conf_read(struct file *file,
1659					  char __user *user_buf,
1660					  size_t count, loff_t *ppos)
1661{
1662	struct iwl_mvm *mvm = file->private_data;
1663	int conf;
1664	char buf[8];
1665	const size_t bufsz = sizeof(buf);
1666	int pos = 0;
1667
1668	mutex_lock(&mvm->mutex);
1669	conf = mvm->fwrt.dump.conf;
1670	mutex_unlock(&mvm->mutex);
1671
1672	pos += scnprintf(buf + pos, bufsz - pos, "%d\n", conf);
1673
1674	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1675}
1676
1677static ssize_t iwl_dbgfs_fw_dbg_conf_write(struct iwl_mvm *mvm,
1678					   char *buf, size_t count,
1679					   loff_t *ppos)
1680{
1681	unsigned int conf_id;
1682	int ret;
1683
1684	if (!iwl_mvm_firmware_running(mvm))
1685		return -EIO;
1686
1687	ret = kstrtouint(buf, 0, &conf_id);
1688	if (ret)
1689		return ret;
1690
1691	if (WARN_ON(conf_id >= FW_DBG_CONF_MAX))
1692		return -EINVAL;
1693
1694	mutex_lock(&mvm->mutex);
1695	ret = iwl_fw_start_dbg_conf(&mvm->fwrt, conf_id);
1696	mutex_unlock(&mvm->mutex);
1697
1698	return ret ?: count;
1699}
1700
1701static ssize_t iwl_dbgfs_fw_dbg_collect_write(struct iwl_mvm *mvm,
1702					      char *buf, size_t count,
1703					      loff_t *ppos)
1704{
1705	if (count == 0)
1706		return 0;
1707
1708	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_USER_TRIGGER,
1709			       NULL);
1710
1711	iwl_fw_dbg_collect(&mvm->fwrt, FW_DBG_TRIGGER_USER, buf,
1712			   (count - 1), NULL);
1713
1714	return count;
1715}
1716
1717static ssize_t iwl_dbgfs_fw_dbg_clear_write(struct iwl_mvm *mvm,
1718					    char *buf, size_t count,
1719					    loff_t *ppos)
1720{
1721	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1722		return -EOPNOTSUPP;
1723
1724	mutex_lock(&mvm->mutex);
1725	iwl_fw_dbg_clear_monitor_buf(&mvm->fwrt);
1726	mutex_unlock(&mvm->mutex);
1727
1728	return count;
1729}
1730
1731static ssize_t iwl_dbgfs_dbg_time_point_write(struct iwl_mvm *mvm,
1732					      char *buf, size_t count,
1733					      loff_t *ppos)
1734{
1735	u32 timepoint;
1736
1737	if (kstrtou32(buf, 0, &timepoint))
1738		return -EINVAL;
1739
1740	if (timepoint == IWL_FW_INI_TIME_POINT_INVALID ||
1741	    timepoint >= IWL_FW_INI_TIME_POINT_NUM)
1742		return -EINVAL;
1743
1744	iwl_dbg_tlv_time_point(&mvm->fwrt, timepoint, NULL);
1745
1746	return count;
1747}
1748
1749#define MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz) \
1750	_MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1751#define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
1752	_MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1753#define MVM_DEBUGFS_ADD_FILE_ALIAS(alias, name, parent, mode) do {	\
1754		debugfs_create_file(alias, mode, parent, mvm,		\
1755				    &iwl_dbgfs_##name##_ops);		\
1756	} while (0)
1757#define MVM_DEBUGFS_ADD_FILE(name, parent, mode) \
1758	MVM_DEBUGFS_ADD_FILE_ALIAS(#name, name, parent, mode)
1759
1760static ssize_t
1761_iwl_dbgfs_link_sta_wrap_write(ssize_t (*real)(struct ieee80211_link_sta *,
1762					       struct iwl_mvm_sta *,
1763					       struct iwl_mvm *,
1764					       struct iwl_mvm_link_sta *,
1765					       char *,
1766					       size_t, loff_t *),
1767			   struct file *file,
1768			   char *buf, size_t buf_size, loff_t *ppos)
1769{
1770	struct ieee80211_link_sta *link_sta = file->private_data;
1771	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(link_sta->sta);
1772	struct iwl_mvm *mvm = iwl_mvm_vif_from_mac80211(mvmsta->vif)->mvm;
1773	struct iwl_mvm_link_sta *mvm_link_sta;
1774	ssize_t ret;
1775
1776	mutex_lock(&mvm->mutex);
1777
1778	mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_sta->link_id],
1779						 lockdep_is_held(&mvm->mutex));
1780	if (WARN_ON(!mvm_link_sta)) {
1781		mutex_unlock(&mvm->mutex);
1782		return -ENODEV;
1783	}
1784
1785	ret = real(link_sta, mvmsta, mvm, mvm_link_sta, buf, buf_size, ppos);
1786
1787	mutex_unlock(&mvm->mutex);
1788
1789	return ret;
1790}
1791
1792static ssize_t
1793_iwl_dbgfs_link_sta_wrap_read(ssize_t (*real)(struct ieee80211_link_sta *,
1794					      struct iwl_mvm_sta *,
1795					      struct iwl_mvm *,
1796					      struct iwl_mvm_link_sta *,
1797					      char __user *,
1798					      size_t, loff_t *),
1799			   struct file *file,
1800			   char __user *user_buf, size_t count, loff_t *ppos)
1801{
1802	struct ieee80211_link_sta *link_sta = file->private_data;
1803	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(link_sta->sta);
1804	struct iwl_mvm *mvm = iwl_mvm_vif_from_mac80211(mvmsta->vif)->mvm;
1805	struct iwl_mvm_link_sta *mvm_link_sta;
1806	ssize_t ret;
1807
1808	mutex_lock(&mvm->mutex);
1809
1810	mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_sta->link_id],
1811						 lockdep_is_held(&mvm->mutex));
1812	if (WARN_ON(!mvm_link_sta)) {
1813		mutex_unlock(&mvm->mutex);
1814		return -ENODEV;
1815	}
1816
1817	ret = real(link_sta, mvmsta, mvm, mvm_link_sta, user_buf, count, ppos);
1818
1819	mutex_unlock(&mvm->mutex);
1820
1821	return ret;
1822}
1823
1824#define MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, buflen)		\
1825static ssize_t _iwl_dbgfs_link_sta_##name##_write(struct file *file,	\
1826					 const char __user *user_buf,	\
1827					 size_t count, loff_t *ppos)	\
1828{									\
1829	char buf[buflen] = {};						\
1830	size_t buf_size = min(count, sizeof(buf) -  1);			\
1831									\
1832	if (copy_from_user(buf, user_buf, buf_size))			\
1833		return -EFAULT;						\
1834									\
1835	return _iwl_dbgfs_link_sta_wrap_write(iwl_dbgfs_##name##_write,	\
1836					      file,			\
1837					      buf, buf_size, ppos);	\
1838}									\
1839
1840#define MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)		\
1841static ssize_t _iwl_dbgfs_link_sta_##name##_read(struct file *file,	\
1842					 char __user *user_buf,		\
1843					 size_t count, loff_t *ppos)	\
1844{									\
1845	return _iwl_dbgfs_link_sta_wrap_read(iwl_dbgfs_##name##_read,	\
1846					     file,			\
1847					     user_buf, count, ppos);	\
1848}									\
1849
1850#define MVM_DEBUGFS_WRITE_LINK_STA_FILE_OPS(name, bufsz)		\
1851MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, bufsz)				\
1852static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = {	\
1853	.write = _iwl_dbgfs_link_sta_##name##_write,			\
1854	.open = simple_open,						\
1855	.llseek = generic_file_llseek,					\
1856}
1857
1858#define MVM_DEBUGFS_READ_LINK_STA_FILE_OPS(name)			\
1859MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)					\
1860static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = {	\
1861	.read = _iwl_dbgfs_link_sta_##name##_read,			\
1862	.open = simple_open,						\
1863	.llseek = generic_file_llseek,					\
1864}
1865
1866#define MVM_DEBUGFS_READ_WRITE_LINK_STA_FILE_OPS(name, bufsz)		\
1867MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)					\
1868MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, bufsz)				\
1869static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = {	\
1870	.read = _iwl_dbgfs_link_sta_##name##_read,			\
1871	.write = _iwl_dbgfs_link_sta_##name##_write,			\
1872	.open = simple_open,						\
1873	.llseek = generic_file_llseek,					\
1874}
1875
1876#define MVM_DEBUGFS_ADD_LINK_STA_FILE_ALIAS(alias, name, parent, mode)	\
1877		debugfs_create_file(alias, mode, parent, link_sta,	\
1878				    &iwl_dbgfs_link_sta_##name##_ops)
1879#define MVM_DEBUGFS_ADD_LINK_STA_FILE(name, parent, mode) \
1880	MVM_DEBUGFS_ADD_LINK_STA_FILE_ALIAS(#name, name, parent, mode)
1881
1882static ssize_t
1883iwl_dbgfs_prph_reg_read(struct file *file,
1884			char __user *user_buf,
1885			size_t count, loff_t *ppos)
1886{
1887	struct iwl_mvm *mvm = file->private_data;
1888	int pos = 0;
1889	char buf[32];
1890	const size_t bufsz = sizeof(buf);
1891
1892	if (!mvm->dbgfs_prph_reg_addr)
1893		return -EINVAL;
1894
1895	pos += scnprintf(buf + pos, bufsz - pos, "Reg 0x%x: (0x%x)\n",
1896		mvm->dbgfs_prph_reg_addr,
1897		iwl_read_prph(mvm->trans, mvm->dbgfs_prph_reg_addr));
1898
1899	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1900}
1901
1902static ssize_t
1903iwl_dbgfs_prph_reg_write(struct iwl_mvm *mvm, char *buf,
1904			 size_t count, loff_t *ppos)
1905{
1906	u8 args;
1907	u32 value;
1908
1909	args = sscanf(buf, "%i %i", &mvm->dbgfs_prph_reg_addr, &value);
1910	/* if we only want to set the reg address - nothing more to do */
1911	if (args == 1)
1912		goto out;
1913
1914	/* otherwise, make sure we have both address and value */
1915	if (args != 2)
1916		return -EINVAL;
1917
1918	iwl_write_prph(mvm->trans, mvm->dbgfs_prph_reg_addr, value);
1919
1920out:
1921	return count;
1922}
1923
1924static ssize_t
1925iwl_dbgfs_send_echo_cmd_write(struct iwl_mvm *mvm, char *buf,
1926			      size_t count, loff_t *ppos)
1927{
1928	int ret;
1929
1930	if (!iwl_mvm_firmware_running(mvm))
1931		return -EIO;
1932
1933	mutex_lock(&mvm->mutex);
1934	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1935	mutex_unlock(&mvm->mutex);
1936
1937	return ret ?: count;
1938}
1939
1940struct iwl_mvm_sniffer_apply {
1941	struct iwl_mvm *mvm;
1942	u8 *bssid;
1943	u16 aid;
1944};
1945
1946static bool iwl_mvm_sniffer_apply(struct iwl_notif_wait_data *notif_data,
1947				  struct iwl_rx_packet *pkt, void *data)
1948{
1949	struct iwl_mvm_sniffer_apply *apply = data;
1950
1951	apply->mvm->cur_aid = cpu_to_le16(apply->aid);
1952	memcpy(apply->mvm->cur_bssid, apply->bssid,
1953	       sizeof(apply->mvm->cur_bssid));
1954
1955	return true;
1956}
1957
1958static ssize_t
1959iwl_dbgfs_he_sniffer_params_write(struct iwl_mvm *mvm, char *buf,
1960				  size_t count, loff_t *ppos)
1961{
1962	struct iwl_notification_wait wait;
1963	struct iwl_he_monitor_cmd he_mon_cmd = {};
1964	struct iwl_mvm_sniffer_apply apply = {
1965		.mvm = mvm,
1966	};
1967	u16 wait_cmds[] = {
1968		WIDE_ID(DATA_PATH_GROUP, HE_AIR_SNIFFER_CONFIG_CMD),
1969	};
1970	u32 aid;
1971	int ret;
1972
1973	if (!iwl_mvm_firmware_running(mvm))
1974		return -EIO;
1975
1976	ret = sscanf(buf, "%x %2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", &aid,
1977		     &he_mon_cmd.bssid[0], &he_mon_cmd.bssid[1],
1978		     &he_mon_cmd.bssid[2], &he_mon_cmd.bssid[3],
1979		     &he_mon_cmd.bssid[4], &he_mon_cmd.bssid[5]);
1980	if (ret != 7)
1981		return -EINVAL;
1982
1983	he_mon_cmd.aid = cpu_to_le16(aid);
1984
1985	apply.aid = aid;
1986	apply.bssid = (void *)he_mon_cmd.bssid;
1987
1988	mutex_lock(&mvm->mutex);
1989
1990	/*
1991	 * Use the notification waiter to get our function triggered
1992	 * in sequence with other RX. This ensures that frames we get
1993	 * on the RX queue _before_ the new configuration is applied
1994	 * still have mvm->cur_aid pointing to the old AID, and that
1995	 * frames on the RX queue _after_ the firmware processed the
1996	 * new configuration (and sent the response, synchronously)
1997	 * get mvm->cur_aid correctly set to the new AID.
1998	 */
1999	iwl_init_notification_wait(&mvm->notif_wait, &wait,
2000				   wait_cmds, ARRAY_SIZE(wait_cmds),
2001				   iwl_mvm_sniffer_apply, &apply);
2002
2003	ret = iwl_mvm_send_cmd_pdu(mvm,
2004				   WIDE_ID(DATA_PATH_GROUP, HE_AIR_SNIFFER_CONFIG_CMD),
2005				   0,
2006				   sizeof(he_mon_cmd), &he_mon_cmd);
2007
2008	/* no need to really wait, we already did anyway */
2009	iwl_remove_notification(&mvm->notif_wait, &wait);
2010
2011	mutex_unlock(&mvm->mutex);
2012
2013	return ret ?: count;
2014}
2015
2016static ssize_t
2017iwl_dbgfs_he_sniffer_params_read(struct file *file, char __user *user_buf,
2018				 size_t count, loff_t *ppos)
2019{
2020	struct iwl_mvm *mvm = file->private_data;
2021	u8 buf[32];
2022	int len;
2023
2024	len = scnprintf(buf, sizeof(buf),
2025			"%d %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
2026			le16_to_cpu(mvm->cur_aid), mvm->cur_bssid[0],
2027			mvm->cur_bssid[1], mvm->cur_bssid[2], mvm->cur_bssid[3],
2028			mvm->cur_bssid[4], mvm->cur_bssid[5]);
2029
2030	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
2031}
2032
2033static ssize_t
2034iwl_dbgfs_uapsd_noagg_bssids_read(struct file *file, char __user *user_buf,
2035				  size_t count, loff_t *ppos)
2036{
2037	struct iwl_mvm *mvm = file->private_data;
2038	u8 buf[IWL_MVM_UAPSD_NOAGG_BSSIDS_NUM * ETH_ALEN * 3 + 1];
2039	unsigned int pos = 0;
2040	size_t bufsz = sizeof(buf);
2041	int i;
2042
2043	mutex_lock(&mvm->mutex);
2044
2045	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++)
2046		pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
2047				 mvm->uapsd_noagg_bssids[i].addr);
2048
2049	mutex_unlock(&mvm->mutex);
2050
2051	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2052}
2053
2054static ssize_t
2055iwl_dbgfs_ltr_config_write(struct iwl_mvm *mvm,
2056			   char *buf, size_t count, loff_t *ppos)
2057{
2058	int ret;
2059	struct iwl_ltr_config_cmd ltr_config = {0};
2060
2061	if (!iwl_mvm_firmware_running(mvm))
2062		return -EIO;
2063
2064	if (sscanf(buf, "%x,%x,%x,%x,%x,%x,%x",
2065		   &ltr_config.flags,
2066		   &ltr_config.static_long,
2067		   &ltr_config.static_short,
2068		   &ltr_config.ltr_cfg_values[0],
2069		   &ltr_config.ltr_cfg_values[1],
2070		   &ltr_config.ltr_cfg_values[2],
2071		   &ltr_config.ltr_cfg_values[3]) != 7) {
2072		return -EINVAL;
2073	}
2074
2075	mutex_lock(&mvm->mutex);
2076	ret = iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0, sizeof(ltr_config),
2077				   &ltr_config);
2078	mutex_unlock(&mvm->mutex);
2079
2080	if (ret)
2081		IWL_ERR(mvm, "failed to send ltr configuration cmd\n");
2082
2083	return ret ?: count;
2084}
2085
2086static ssize_t iwl_dbgfs_rfi_freq_table_write(struct iwl_mvm *mvm, char *buf,
2087					      size_t count, loff_t *ppos)
2088{
2089	int ret = 0;
2090	u16 op_id;
2091
2092	if (kstrtou16(buf, 10, &op_id))
2093		return -EINVAL;
2094
2095	/* value zero triggers re-sending the default table to the device */
2096	if (!op_id) {
2097		mutex_lock(&mvm->mutex);
2098		ret = iwl_rfi_send_config_cmd(mvm, NULL);
2099		mutex_unlock(&mvm->mutex);
2100	} else {
2101		ret = -EOPNOTSUPP; /* in the future a new table will be added */
2102	}
2103
2104	return ret ?: count;
2105}
2106
2107/* The size computation is as follows:
2108 * each number needs at most 3 characters, number of rows is the size of
2109 * the table; So, need 5 chars for the "freq: " part and each tuple afterwards
2110 * needs 6 characters for numbers and 5 for the punctuation around.
2111 */
2112#define IWL_RFI_BUF_SIZE (IWL_RFI_LUT_INSTALLED_SIZE *\
2113				(5 + IWL_RFI_LUT_ENTRY_CHANNELS_NUM * (6 + 5)))
2114
2115static ssize_t iwl_dbgfs_rfi_freq_table_read(struct file *file,
2116					     char __user *user_buf,
2117					     size_t count, loff_t *ppos)
2118{
2119	struct iwl_mvm *mvm = file->private_data;
2120	struct iwl_rfi_freq_table_resp_cmd *resp;
2121	u32 status;
2122	char buf[IWL_RFI_BUF_SIZE];
2123	int i, j, pos = 0;
2124
2125	resp = iwl_rfi_get_freq_table(mvm);
2126	if (IS_ERR(resp))
2127		return PTR_ERR(resp);
2128
2129	status = le32_to_cpu(resp->status);
2130	if (status != RFI_FREQ_TABLE_OK) {
2131		scnprintf(buf, IWL_RFI_BUF_SIZE, "status = %d\n", status);
2132		goto out;
2133	}
2134
2135	for (i = 0; i < ARRAY_SIZE(resp->table); i++) {
2136		pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos, "%d: ",
2137				 resp->table[i].freq);
2138
2139		for (j = 0; j < ARRAY_SIZE(resp->table[i].channels); j++)
2140			pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos,
2141					 "(%d, %d) ",
2142					 resp->table[i].channels[j],
2143					 resp->table[i].bands[j]);
2144		pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos, "\n");
2145	}
2146
2147out:
2148	kfree(resp);
2149	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2150}
2151
2152MVM_DEBUGFS_READ_WRITE_FILE_OPS(prph_reg, 64);
2153
2154/* Device wide debugfs entries */
2155MVM_DEBUGFS_READ_FILE_OPS(ctdp_budget);
2156MVM_DEBUGFS_WRITE_FILE_OPS(stop_ctdp, 8);
2157MVM_DEBUGFS_WRITE_FILE_OPS(start_ctdp, 8);
2158MVM_DEBUGFS_WRITE_FILE_OPS(force_ctkill, 8);
2159MVM_DEBUGFS_WRITE_FILE_OPS(tx_flush, 16);
2160MVM_DEBUGFS_WRITE_FILE_OPS(sta_drain, 8);
2161MVM_DEBUGFS_WRITE_FILE_OPS(send_echo_cmd, 8);
2162MVM_DEBUGFS_READ_WRITE_FILE_OPS(sram, 64);
2163MVM_DEBUGFS_READ_WRITE_FILE_OPS(set_nic_temperature, 64);
2164MVM_DEBUGFS_READ_FILE_OPS(nic_temp);
2165MVM_DEBUGFS_READ_FILE_OPS(stations);
2166MVM_DEBUGFS_READ_LINK_STA_FILE_OPS(rs_data);
2167MVM_DEBUGFS_READ_FILE_OPS(bt_notif);
2168MVM_DEBUGFS_READ_FILE_OPS(bt_cmd);
2169MVM_DEBUGFS_READ_WRITE_FILE_OPS(disable_power_off, 64);
2170MVM_DEBUGFS_READ_FILE_OPS(fw_rx_stats);
2171MVM_DEBUGFS_READ_FILE_OPS(drv_rx_stats);
2172MVM_DEBUGFS_READ_FILE_OPS(fw_system_stats);
2173MVM_DEBUGFS_READ_FILE_OPS(fw_ver);
2174MVM_DEBUGFS_READ_FILE_OPS(phy_integration_ver);
2175MVM_DEBUGFS_READ_FILE_OPS(tas_get_status);
2176MVM_DEBUGFS_WRITE_FILE_OPS(fw_restart, 10);
2177MVM_DEBUGFS_WRITE_FILE_OPS(fw_nmi, 10);
2178MVM_DEBUGFS_WRITE_FILE_OPS(bt_tx_prio, 10);
2179MVM_DEBUGFS_WRITE_FILE_OPS(bt_force_ant, 10);
2180MVM_DEBUGFS_READ_WRITE_FILE_OPS(scan_ant_rxchain, 8);
2181MVM_DEBUGFS_READ_WRITE_FILE_OPS(fw_dbg_conf, 8);
2182MVM_DEBUGFS_WRITE_FILE_OPS(fw_dbg_collect, 64);
2183MVM_DEBUGFS_WRITE_FILE_OPS(fw_dbg_clear, 64);
2184MVM_DEBUGFS_WRITE_FILE_OPS(dbg_time_point, 64);
2185MVM_DEBUGFS_WRITE_FILE_OPS(indirection_tbl,
2186			   (IWL_RSS_INDIRECTION_TABLE_SIZE * 2));
2187MVM_DEBUGFS_WRITE_FILE_OPS(inject_packet, 512);
2188MVM_DEBUGFS_WRITE_FILE_OPS(inject_beacon_ie, 512);
2189MVM_DEBUGFS_WRITE_FILE_OPS(inject_beacon_ie_restore, 512);
2190
2191MVM_DEBUGFS_READ_FILE_OPS(uapsd_noagg_bssids);
2192
2193#ifdef CONFIG_ACPI
2194MVM_DEBUGFS_READ_FILE_OPS(sar_geo_profile);
2195MVM_DEBUGFS_READ_FILE_OPS(wifi_6e_enable);
2196#endif
2197
2198MVM_DEBUGFS_READ_WRITE_LINK_STA_FILE_OPS(amsdu_len, 16);
2199
2200MVM_DEBUGFS_READ_WRITE_FILE_OPS(he_sniffer_params, 32);
2201
2202MVM_DEBUGFS_WRITE_FILE_OPS(ltr_config, 512);
2203MVM_DEBUGFS_READ_WRITE_FILE_OPS(rfi_freq_table, 16);
2204
2205static ssize_t iwl_dbgfs_mem_read(struct file *file, char __user *user_buf,
2206				  size_t count, loff_t *ppos)
2207{
2208	struct iwl_mvm *mvm = file->private_data;
2209	struct iwl_dbg_mem_access_cmd cmd = {};
2210	struct iwl_dbg_mem_access_rsp *rsp;
2211	struct iwl_host_cmd hcmd = {
2212		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
2213		.data = { &cmd, },
2214		.len = { sizeof(cmd) },
2215	};
2216	size_t delta;
2217	ssize_t ret, len;
2218
2219	if (!iwl_mvm_firmware_running(mvm))
2220		return -EIO;
2221
2222	hcmd.id = WIDE_ID(DEBUG_GROUP, *ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR);
2223	cmd.op = cpu_to_le32(DEBUG_MEM_OP_READ);
2224
2225	/* Take care of alignment of both the position and the length */
2226	delta = *ppos & 0x3;
2227	cmd.addr = cpu_to_le32(*ppos - delta);
2228	cmd.len = cpu_to_le32(min(ALIGN(count + delta, 4) / 4,
2229				  (size_t)DEBUG_MEM_MAX_SIZE_DWORDS));
2230
2231	mutex_lock(&mvm->mutex);
2232	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2233	mutex_unlock(&mvm->mutex);
2234
2235	if (ret < 0)
2236		return ret;
2237
2238	if (iwl_rx_packet_payload_len(hcmd.resp_pkt) < sizeof(*rsp)) {
2239		ret = -EIO;
2240		goto out;
2241	}
2242
2243	rsp = (void *)hcmd.resp_pkt->data;
2244	if (le32_to_cpu(rsp->status) != DEBUG_MEM_STATUS_SUCCESS) {
2245		ret = -ENXIO;
2246		goto out;
2247	}
2248
2249	len = min((size_t)le32_to_cpu(rsp->len) << 2,
2250		  iwl_rx_packet_payload_len(hcmd.resp_pkt) - sizeof(*rsp));
2251	len = min(len - delta, count);
2252	if (len < 0) {
2253		ret = -EFAULT;
2254		goto out;
2255	}
2256
2257	ret = len - copy_to_user(user_buf, (u8 *)rsp->data + delta, len);
2258	*ppos += ret;
2259
2260out:
2261	iwl_free_resp(&hcmd);
2262	return ret;
2263}
2264
2265static ssize_t iwl_dbgfs_mem_write(struct file *file,
2266				   const char __user *user_buf, size_t count,
2267				   loff_t *ppos)
2268{
2269	struct iwl_mvm *mvm = file->private_data;
2270	struct iwl_dbg_mem_access_cmd *cmd;
2271	struct iwl_dbg_mem_access_rsp *rsp;
2272	struct iwl_host_cmd hcmd = {};
2273	size_t cmd_size;
2274	size_t data_size;
2275	u32 op, len;
2276	ssize_t ret;
2277
2278	if (!iwl_mvm_firmware_running(mvm))
2279		return -EIO;
2280
2281	hcmd.id = WIDE_ID(DEBUG_GROUP, *ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR);
2282
2283	if (*ppos & 0x3 || count < 4) {
2284		op = DEBUG_MEM_OP_WRITE_BYTES;
2285		len = min(count, (size_t)(4 - (*ppos & 0x3)));
2286		data_size = len;
2287	} else {
2288		op = DEBUG_MEM_OP_WRITE;
2289		len = min(count >> 2, (size_t)DEBUG_MEM_MAX_SIZE_DWORDS);
2290		data_size = len << 2;
2291	}
2292
2293	cmd_size = sizeof(*cmd) + ALIGN(data_size, 4);
2294	cmd = kzalloc(cmd_size, GFP_KERNEL);
2295	if (!cmd)
2296		return -ENOMEM;
2297
2298	cmd->op = cpu_to_le32(op);
2299	cmd->len = cpu_to_le32(len);
2300	cmd->addr = cpu_to_le32(*ppos);
2301	if (copy_from_user((void *)cmd->data, user_buf, data_size)) {
2302		kfree(cmd);
2303		return -EFAULT;
2304	}
2305
2306	hcmd.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
2307	hcmd.data[0] = (void *)cmd;
2308	hcmd.len[0] = cmd_size;
2309
2310	mutex_lock(&mvm->mutex);
2311	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2312	mutex_unlock(&mvm->mutex);
2313
2314	kfree(cmd);
2315
2316	if (ret < 0)
2317		return ret;
2318
2319	if (iwl_rx_packet_payload_len(hcmd.resp_pkt) < sizeof(*rsp)) {
2320		ret = -EIO;
2321		goto out;
2322	}
2323
2324	rsp = (void *)hcmd.resp_pkt->data;
2325	if (rsp->status != DEBUG_MEM_STATUS_SUCCESS) {
2326		ret = -ENXIO;
2327		goto out;
2328	}
2329
2330	ret = data_size;
2331	*ppos += ret;
2332
2333out:
2334	iwl_free_resp(&hcmd);
2335	return ret;
2336}
2337
2338static const struct file_operations iwl_dbgfs_mem_ops = {
2339	.read = iwl_dbgfs_mem_read,
2340	.write = iwl_dbgfs_mem_write,
2341	.open = simple_open,
2342	.llseek = default_llseek,
2343};
2344
2345void iwl_mvm_link_sta_add_debugfs(struct ieee80211_hw *hw,
2346				  struct ieee80211_vif *vif,
2347				  struct ieee80211_link_sta *link_sta,
2348				  struct dentry *dir)
2349{
2350	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2351
2352	if (iwl_mvm_has_tlc_offload(mvm)) {
2353		MVM_DEBUGFS_ADD_LINK_STA_FILE(rs_data, dir, 0400);
2354	}
2355
2356	MVM_DEBUGFS_ADD_LINK_STA_FILE(amsdu_len, dir, 0600);
2357}
2358
2359void iwl_mvm_dbgfs_register(struct iwl_mvm *mvm)
2360{
2361	struct dentry *bcast_dir __maybe_unused;
2362
2363	spin_lock_init(&mvm->drv_stats_lock);
2364
2365	MVM_DEBUGFS_ADD_FILE(tx_flush, mvm->debugfs_dir, 0200);
2366	MVM_DEBUGFS_ADD_FILE(sta_drain, mvm->debugfs_dir, 0200);
2367	MVM_DEBUGFS_ADD_FILE(sram, mvm->debugfs_dir, 0600);
2368	MVM_DEBUGFS_ADD_FILE(set_nic_temperature, mvm->debugfs_dir, 0600);
2369	MVM_DEBUGFS_ADD_FILE(nic_temp, mvm->debugfs_dir, 0400);
2370	MVM_DEBUGFS_ADD_FILE(ctdp_budget, mvm->debugfs_dir, 0400);
2371	MVM_DEBUGFS_ADD_FILE(stop_ctdp, mvm->debugfs_dir, 0200);
2372	MVM_DEBUGFS_ADD_FILE(start_ctdp, mvm->debugfs_dir, 0200);
2373	MVM_DEBUGFS_ADD_FILE(force_ctkill, mvm->debugfs_dir, 0200);
2374	MVM_DEBUGFS_ADD_FILE(stations, mvm->debugfs_dir, 0400);
2375	MVM_DEBUGFS_ADD_FILE(bt_notif, mvm->debugfs_dir, 0400);
2376	MVM_DEBUGFS_ADD_FILE(bt_cmd, mvm->debugfs_dir, 0400);
2377	MVM_DEBUGFS_ADD_FILE(disable_power_off, mvm->debugfs_dir, 0600);
2378	MVM_DEBUGFS_ADD_FILE(fw_ver, mvm->debugfs_dir, 0400);
2379	MVM_DEBUGFS_ADD_FILE(fw_rx_stats, mvm->debugfs_dir, 0400);
2380	MVM_DEBUGFS_ADD_FILE(drv_rx_stats, mvm->debugfs_dir, 0400);
2381	MVM_DEBUGFS_ADD_FILE(fw_system_stats, mvm->debugfs_dir, 0400);
2382	MVM_DEBUGFS_ADD_FILE(fw_restart, mvm->debugfs_dir, 0200);
2383	MVM_DEBUGFS_ADD_FILE(fw_nmi, mvm->debugfs_dir, 0200);
2384	MVM_DEBUGFS_ADD_FILE(bt_tx_prio, mvm->debugfs_dir, 0200);
2385	MVM_DEBUGFS_ADD_FILE(bt_force_ant, mvm->debugfs_dir, 0200);
2386	MVM_DEBUGFS_ADD_FILE(scan_ant_rxchain, mvm->debugfs_dir, 0600);
2387	MVM_DEBUGFS_ADD_FILE(prph_reg, mvm->debugfs_dir, 0600);
2388	MVM_DEBUGFS_ADD_FILE(fw_dbg_conf, mvm->debugfs_dir, 0600);
2389	MVM_DEBUGFS_ADD_FILE(fw_dbg_collect, mvm->debugfs_dir, 0200);
2390	MVM_DEBUGFS_ADD_FILE(fw_dbg_clear, mvm->debugfs_dir, 0200);
2391	MVM_DEBUGFS_ADD_FILE(dbg_time_point, mvm->debugfs_dir, 0200);
2392	MVM_DEBUGFS_ADD_FILE(send_echo_cmd, mvm->debugfs_dir, 0200);
2393	MVM_DEBUGFS_ADD_FILE(indirection_tbl, mvm->debugfs_dir, 0200);
2394	MVM_DEBUGFS_ADD_FILE(inject_packet, mvm->debugfs_dir, 0200);
2395	MVM_DEBUGFS_ADD_FILE(inject_beacon_ie, mvm->debugfs_dir, 0200);
2396	MVM_DEBUGFS_ADD_FILE(inject_beacon_ie_restore, mvm->debugfs_dir, 0200);
2397	MVM_DEBUGFS_ADD_FILE(rfi_freq_table, mvm->debugfs_dir, 0600);
2398
2399	if (mvm->fw->phy_integration_ver)
2400		MVM_DEBUGFS_ADD_FILE(phy_integration_ver, mvm->debugfs_dir, 0400);
2401	MVM_DEBUGFS_ADD_FILE(tas_get_status, mvm->debugfs_dir, 0400);
2402#ifdef CONFIG_ACPI
2403	MVM_DEBUGFS_ADD_FILE(sar_geo_profile, mvm->debugfs_dir, 0400);
2404	MVM_DEBUGFS_ADD_FILE(wifi_6e_enable, mvm->debugfs_dir, 0400);
2405#endif
2406	MVM_DEBUGFS_ADD_FILE(he_sniffer_params, mvm->debugfs_dir, 0600);
2407
2408	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_SET_LTR_GEN2))
2409		MVM_DEBUGFS_ADD_FILE(ltr_config, mvm->debugfs_dir, 0200);
2410
2411	debugfs_create_bool("enable_scan_iteration_notif", 0600,
2412			    mvm->debugfs_dir, &mvm->scan_iter_notif_enabled);
2413	debugfs_create_bool("drop_bcn_ap_mode", 0600, mvm->debugfs_dir,
2414			    &mvm->drop_bcn_ap_mode);
2415
2416	MVM_DEBUGFS_ADD_FILE(uapsd_noagg_bssids, mvm->debugfs_dir, S_IRUSR);
2417
2418#ifdef CONFIG_PM_SLEEP
2419	MVM_DEBUGFS_ADD_FILE(d3_test, mvm->debugfs_dir, 0400);
2420	debugfs_create_bool("d3_wake_sysassert", 0600, mvm->debugfs_dir,
2421			    &mvm->d3_wake_sysassert);
2422	debugfs_create_u32("last_netdetect_scans", 0400, mvm->debugfs_dir,
2423			   &mvm->last_netdetect_scans);
2424#endif
2425
2426	debugfs_create_u8("ps_disabled", 0400, mvm->debugfs_dir,
2427			  &mvm->ps_disabled);
2428	debugfs_create_blob("nvm_hw", 0400, mvm->debugfs_dir,
2429			    &mvm->nvm_hw_blob);
2430	debugfs_create_blob("nvm_sw", 0400, mvm->debugfs_dir,
2431			    &mvm->nvm_sw_blob);
2432	debugfs_create_blob("nvm_calib", 0400, mvm->debugfs_dir,
2433			    &mvm->nvm_calib_blob);
2434	debugfs_create_blob("nvm_prod", 0400, mvm->debugfs_dir,
2435			    &mvm->nvm_prod_blob);
2436	debugfs_create_blob("nvm_phy_sku", 0400, mvm->debugfs_dir,
2437			    &mvm->nvm_phy_sku_blob);
2438	debugfs_create_blob("nvm_reg", S_IRUSR,
2439			    mvm->debugfs_dir, &mvm->nvm_reg_blob);
2440
2441	debugfs_create_file("mem", 0600, mvm->debugfs_dir, mvm,
2442			    &iwl_dbgfs_mem_ops);
2443
2444	/*
2445	 * Create a symlink with mac80211. It will be removed when mac80211
2446	 * exists (before the opmode exists which removes the target.)
2447	 */
2448	if (!IS_ERR(mvm->debugfs_dir)) {
2449		char buf[100];
2450
2451		snprintf(buf, 100, "../../%pd2", mvm->debugfs_dir->d_parent);
2452		debugfs_create_symlink("iwlwifi", mvm->hw->wiphy->debugfsdir,
2453				       buf);
2454	}
2455}
v6.2
   1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
   2/*
   3 * Copyright (C) 2012-2014, 2018-2022 Intel Corporation
   4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
   5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
   6 */
   7#include <linux/vmalloc.h>
   8#include <linux/err.h>
   9#include <linux/ieee80211.h>
  10#include <linux/netdevice.h>
 
  11
  12#include "mvm.h"
  13#include "sta.h"
  14#include "iwl-io.h"
  15#include "debugfs.h"
  16#include "iwl-modparams.h"
 
  17#include "fw/error-dump.h"
 
  18
  19static ssize_t iwl_dbgfs_ctdp_budget_read(struct file *file,
  20					  char __user *user_buf,
  21					  size_t count, loff_t *ppos)
  22{
  23	struct iwl_mvm *mvm = file->private_data;
  24	char buf[16];
  25	int pos, budget;
  26
  27	if (!iwl_mvm_is_ctdp_supported(mvm))
  28		return -EOPNOTSUPP;
  29
  30	if (!iwl_mvm_firmware_running(mvm) ||
  31	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
  32		return -EIO;
  33
  34	mutex_lock(&mvm->mutex);
  35	budget = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_REPORT, 0);
  36	mutex_unlock(&mvm->mutex);
  37
  38	if (budget < 0)
  39		return budget;
  40
  41	pos = scnprintf(buf, sizeof(buf), "%d\n", budget);
  42
  43	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  44}
  45
  46static ssize_t iwl_dbgfs_stop_ctdp_write(struct iwl_mvm *mvm, char *buf,
  47					 size_t count, loff_t *ppos)
  48{
  49	int ret;
 
  50
  51	if (!iwl_mvm_is_ctdp_supported(mvm))
 
 
 
 
 
 
 
 
 
  52		return -EOPNOTSUPP;
  53
  54	if (!iwl_mvm_firmware_running(mvm) ||
  55	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
  56		return -EIO;
  57
  58	mutex_lock(&mvm->mutex);
  59	ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_STOP, 0);
  60	mutex_unlock(&mvm->mutex);
  61
  62	return ret ?: count;
  63}
  64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  65static ssize_t iwl_dbgfs_force_ctkill_write(struct iwl_mvm *mvm, char *buf,
  66					    size_t count, loff_t *ppos)
  67{
  68	if (!iwl_mvm_firmware_running(mvm) ||
  69	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
  70		return -EIO;
  71
  72	iwl_mvm_enter_ctkill(mvm);
  73
  74	return count;
  75}
  76
  77static ssize_t iwl_dbgfs_tx_flush_write(struct iwl_mvm *mvm, char *buf,
  78					size_t count, loff_t *ppos)
  79{
  80	int ret;
  81	u32 flush_arg;
  82
  83	if (!iwl_mvm_firmware_running(mvm) ||
  84	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
  85		return -EIO;
  86
  87	if (kstrtou32(buf, 0, &flush_arg))
  88		return -EINVAL;
  89
  90	if (iwl_mvm_has_new_tx_api(mvm)) {
  91		IWL_DEBUG_TX_QUEUES(mvm,
  92				    "FLUSHING all tids queues on sta_id = %d\n",
  93				    flush_arg);
  94		mutex_lock(&mvm->mutex);
  95		ret = iwl_mvm_flush_sta_tids(mvm, flush_arg, 0xFFFF)
  96			? : count;
  97		mutex_unlock(&mvm->mutex);
  98		return ret;
  99	}
 100
 101	IWL_DEBUG_TX_QUEUES(mvm, "FLUSHING queues mask to flush = 0x%x\n",
 102			    flush_arg);
 103
 104	mutex_lock(&mvm->mutex);
 105	ret =  iwl_mvm_flush_tx_path(mvm, flush_arg) ? : count;
 106	mutex_unlock(&mvm->mutex);
 107
 108	return ret;
 109}
 110
 111static ssize_t iwl_dbgfs_sta_drain_write(struct iwl_mvm *mvm, char *buf,
 112					 size_t count, loff_t *ppos)
 113{
 114	struct iwl_mvm_sta *mvmsta;
 115	int sta_id, drain, ret;
 116
 117	if (!iwl_mvm_firmware_running(mvm) ||
 118	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
 119		return -EIO;
 120
 121	if (sscanf(buf, "%d %d", &sta_id, &drain) != 2)
 122		return -EINVAL;
 123	if (sta_id < 0 || sta_id >= mvm->fw->ucode_capa.num_stations)
 124		return -EINVAL;
 125	if (drain < 0 || drain > 1)
 126		return -EINVAL;
 127
 128	mutex_lock(&mvm->mutex);
 129
 130	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
 131
 132	if (!mvmsta)
 133		ret = -ENOENT;
 134	else
 135		ret = iwl_mvm_drain_sta(mvm, mvmsta, drain) ? : count;
 136
 137	mutex_unlock(&mvm->mutex);
 138
 139	return ret;
 140}
 141
 142static ssize_t iwl_dbgfs_sram_read(struct file *file, char __user *user_buf,
 143				   size_t count, loff_t *ppos)
 144{
 145	struct iwl_mvm *mvm = file->private_data;
 146	const struct fw_img *img;
 147	unsigned int ofs, len;
 148	size_t ret;
 149	u8 *ptr;
 150
 151	if (!iwl_mvm_firmware_running(mvm))
 152		return -EINVAL;
 153
 154	/* default is to dump the entire data segment */
 155	img = &mvm->fw->img[mvm->fwrt.cur_fw_img];
 156	ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
 157	len = img->sec[IWL_UCODE_SECTION_DATA].len;
 158
 159	if (mvm->dbgfs_sram_len) {
 160		ofs = mvm->dbgfs_sram_offset;
 161		len = mvm->dbgfs_sram_len;
 162	}
 163
 164	ptr = kzalloc(len, GFP_KERNEL);
 165	if (!ptr)
 166		return -ENOMEM;
 167
 168	iwl_trans_read_mem_bytes(mvm->trans, ofs, ptr, len);
 169
 170	ret = simple_read_from_buffer(user_buf, count, ppos, ptr, len);
 171
 172	kfree(ptr);
 173
 174	return ret;
 175}
 176
 177static ssize_t iwl_dbgfs_sram_write(struct iwl_mvm *mvm, char *buf,
 178				    size_t count, loff_t *ppos)
 179{
 180	const struct fw_img *img;
 181	u32 offset, len;
 182	u32 img_offset, img_len;
 183
 184	if (!iwl_mvm_firmware_running(mvm))
 185		return -EINVAL;
 186
 187	img = &mvm->fw->img[mvm->fwrt.cur_fw_img];
 188	img_offset = img->sec[IWL_UCODE_SECTION_DATA].offset;
 189	img_len = img->sec[IWL_UCODE_SECTION_DATA].len;
 190
 191	if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
 192		if ((offset & 0x3) || (len & 0x3))
 193			return -EINVAL;
 194
 195		if (offset + len > img_offset + img_len)
 196			return -EINVAL;
 197
 198		mvm->dbgfs_sram_offset = offset;
 199		mvm->dbgfs_sram_len = len;
 200	} else {
 201		mvm->dbgfs_sram_offset = 0;
 202		mvm->dbgfs_sram_len = 0;
 203	}
 204
 205	return count;
 206}
 207
 208static ssize_t iwl_dbgfs_set_nic_temperature_read(struct file *file,
 209						  char __user *user_buf,
 210						  size_t count, loff_t *ppos)
 211{
 212	struct iwl_mvm *mvm = file->private_data;
 213	char buf[16];
 214	int pos;
 215
 216	if (!mvm->temperature_test)
 217		pos = scnprintf(buf , sizeof(buf), "disabled\n");
 218	else
 219		pos = scnprintf(buf , sizeof(buf), "%d\n", mvm->temperature);
 220
 221	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 222}
 223
 224/*
 225 * Set NIC Temperature
 226 * Cause the driver to ignore the actual NIC temperature reported by the FW
 227 * Enable: any value between IWL_MVM_DEBUG_SET_TEMPERATURE_MIN -
 228 * IWL_MVM_DEBUG_SET_TEMPERATURE_MAX
 229 * Disable: IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE
 230 */
 231static ssize_t iwl_dbgfs_set_nic_temperature_write(struct iwl_mvm *mvm,
 232						   char *buf, size_t count,
 233						   loff_t *ppos)
 234{
 235	int temperature;
 236
 237	if (!iwl_mvm_firmware_running(mvm) && !mvm->temperature_test)
 238		return -EIO;
 239
 240	if (kstrtoint(buf, 10, &temperature))
 241		return -EINVAL;
 242	/* not a legal temperature */
 243	if ((temperature > IWL_MVM_DEBUG_SET_TEMPERATURE_MAX &&
 244	     temperature != IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE) ||
 245	    temperature < IWL_MVM_DEBUG_SET_TEMPERATURE_MIN)
 246		return -EINVAL;
 247
 248	mutex_lock(&mvm->mutex);
 249	if (temperature == IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE) {
 250		if (!mvm->temperature_test)
 251			goto out;
 252
 253		mvm->temperature_test = false;
 254		/* Since we can't read the temp while awake, just set
 255		 * it to zero until we get the next RX stats from the
 256		 * firmware.
 257		 */
 258		mvm->temperature = 0;
 259	} else {
 260		mvm->temperature_test = true;
 261		mvm->temperature = temperature;
 262	}
 263	IWL_DEBUG_TEMP(mvm, "%sabling debug set temperature (temp = %d)\n",
 264		       mvm->temperature_test ? "En" : "Dis" ,
 265		       mvm->temperature);
 266	/* handle the temperature change */
 267	iwl_mvm_tt_handler(mvm);
 268
 269out:
 270	mutex_unlock(&mvm->mutex);
 271
 272	return count;
 273}
 274
 275static ssize_t iwl_dbgfs_nic_temp_read(struct file *file,
 276				       char __user *user_buf,
 277				       size_t count, loff_t *ppos)
 278{
 279	struct iwl_mvm *mvm = file->private_data;
 280	char buf[16];
 281	int pos, ret;
 282	s32 temp;
 283
 284	if (!iwl_mvm_firmware_running(mvm))
 285		return -EIO;
 286
 287	mutex_lock(&mvm->mutex);
 288	ret = iwl_mvm_get_temp(mvm, &temp);
 289	mutex_unlock(&mvm->mutex);
 290
 291	if (ret)
 292		return -EIO;
 293
 294	pos = scnprintf(buf , sizeof(buf), "%d\n", temp);
 295
 296	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 297}
 298
 299#ifdef CONFIG_ACPI
 300static ssize_t iwl_dbgfs_sar_geo_profile_read(struct file *file,
 301					      char __user *user_buf,
 302					      size_t count, loff_t *ppos)
 303{
 304	struct iwl_mvm *mvm = file->private_data;
 305	char buf[256];
 306	int pos = 0;
 307	int bufsz = sizeof(buf);
 308	int tbl_idx;
 309
 310	if (!iwl_mvm_firmware_running(mvm))
 311		return -EIO;
 312
 313	mutex_lock(&mvm->mutex);
 314	tbl_idx = iwl_mvm_get_sar_geo_profile(mvm);
 315	if (tbl_idx < 0) {
 316		mutex_unlock(&mvm->mutex);
 317		return tbl_idx;
 318	}
 319
 320	if (!tbl_idx) {
 321		pos = scnprintf(buf, bufsz,
 322				"SAR geographic profile disabled\n");
 323	} else {
 324		pos += scnprintf(buf + pos, bufsz - pos,
 325				 "Use geographic profile %d\n", tbl_idx);
 326		pos += scnprintf(buf + pos, bufsz - pos,
 327				 "2.4GHz:\n\tChain A offset: %hhu dBm\n\tChain B offset: %hhu dBm\n\tmax tx power: %hhu dBm\n",
 328				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].chains[0],
 329				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].chains[1],
 330				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].max);
 331		pos += scnprintf(buf + pos, bufsz - pos,
 332				 "5.2GHz:\n\tChain A offset: %hhu dBm\n\tChain B offset: %hhu dBm\n\tmax tx power: %hhu dBm\n",
 333				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].chains[0],
 334				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].chains[1],
 335				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].max);
 336	}
 337	mutex_unlock(&mvm->mutex);
 338
 339	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 340}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 341#endif
 342
 343static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
 344				       size_t count, loff_t *ppos)
 345{
 346	struct iwl_mvm *mvm = file->private_data;
 347	struct ieee80211_sta *sta;
 348	char buf[400];
 349	int i, pos = 0, bufsz = sizeof(buf);
 350
 351	mutex_lock(&mvm->mutex);
 352
 353	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
 354		pos += scnprintf(buf + pos, bufsz - pos, "%.2d: ", i);
 355		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
 356						lockdep_is_held(&mvm->mutex));
 357		if (!sta)
 358			pos += scnprintf(buf + pos, bufsz - pos, "N/A\n");
 359		else if (IS_ERR(sta))
 360			pos += scnprintf(buf + pos, bufsz - pos, "%ld\n",
 361					 PTR_ERR(sta));
 362		else
 363			pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
 364					 sta->addr);
 365	}
 366
 367	mutex_unlock(&mvm->mutex);
 368
 369	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 370}
 371
 372static ssize_t iwl_dbgfs_rs_data_read(struct file *file, char __user *user_buf,
 
 
 
 
 373				      size_t count, loff_t *ppos)
 374{
 375	struct ieee80211_sta *sta = file->private_data;
 376	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 377	struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
 378	struct iwl_mvm *mvm = lq_sta->pers.drv;
 379	static const size_t bufsz = 2048;
 380	char *buff;
 381	int desc = 0;
 382	ssize_t ret;
 383
 384	buff = kmalloc(bufsz, GFP_KERNEL);
 385	if (!buff)
 386		return -ENOMEM;
 387
 388	mutex_lock(&mvm->mutex);
 389
 390	desc += scnprintf(buff + desc, bufsz - desc, "sta_id %d\n",
 391			  lq_sta->pers.sta_id);
 392	desc += scnprintf(buff + desc, bufsz - desc,
 393			  "fixed rate 0x%X\n",
 394			  lq_sta->pers.dbg_fixed_rate);
 395	desc += scnprintf(buff + desc, bufsz - desc,
 396			  "A-MPDU size limit %d\n",
 397			  lq_sta->pers.dbg_agg_frame_count_lim);
 398	desc += scnprintf(buff + desc, bufsz - desc,
 399			  "valid_tx_ant %s%s\n",
 400		(iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "",
 401		(iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : "");
 402	desc += scnprintf(buff + desc, bufsz - desc,
 403			  "last tx rate=0x%X ",
 404			  lq_sta->last_rate_n_flags);
 405
 406	desc += rs_pretty_print_rate(buff + desc, bufsz - desc,
 407				     lq_sta->last_rate_n_flags);
 408	if (desc < bufsz - 1)
 409		buff[desc++] = '\n';
 410	mutex_unlock(&mvm->mutex);
 411
 412	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
 413	kfree(buff);
 414	return ret;
 415}
 416
 417static ssize_t iwl_dbgfs_amsdu_len_write(struct ieee80211_sta *sta,
 
 
 
 418					 char *buf, size_t count,
 419					 loff_t *ppos)
 420{
 421	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 422	int i;
 423	u16 amsdu_len;
 424
 425	if (kstrtou16(buf, 0, &amsdu_len))
 426		return -EINVAL;
 427
 428	/* only change from debug set <-> debug unset */
 429	if (amsdu_len && mvmsta->orig_amsdu_len)
 430		return -EBUSY;
 431
 432	if (amsdu_len) {
 433		mvmsta->orig_amsdu_len = sta->cur->max_amsdu_len;
 434		sta->deflink.agg.max_amsdu_len = amsdu_len;
 435		sta->deflink.agg.max_amsdu_len = amsdu_len;
 436		for (i = 0; i < ARRAY_SIZE(sta->deflink.agg.max_tid_amsdu_len); i++)
 437			sta->deflink.agg.max_tid_amsdu_len[i] = amsdu_len;
 438	} else {
 439		sta->deflink.agg.max_amsdu_len = mvmsta->orig_amsdu_len;
 440		mvmsta->orig_amsdu_len = 0;
 441	}
 442
 
 
 443	return count;
 444}
 445
 446static ssize_t iwl_dbgfs_amsdu_len_read(struct file *file,
 
 
 
 447					char __user *user_buf,
 448					size_t count, loff_t *ppos)
 449{
 450	struct ieee80211_sta *sta = file->private_data;
 451	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 452
 453	char buf[32];
 454	int pos;
 455
 456	pos = scnprintf(buf, sizeof(buf), "current %d ", sta->cur->max_amsdu_len);
 
 457	pos += scnprintf(buf + pos, sizeof(buf) - pos, "stored %d\n",
 458			 mvmsta->orig_amsdu_len);
 459
 460	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 461}
 462
 463static ssize_t iwl_dbgfs_disable_power_off_read(struct file *file,
 464						char __user *user_buf,
 465						size_t count, loff_t *ppos)
 466{
 467	struct iwl_mvm *mvm = file->private_data;
 468	char buf[64];
 469	int bufsz = sizeof(buf);
 470	int pos = 0;
 471
 472	pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d0=%d\n",
 473			 mvm->disable_power_off);
 474	pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d3=%d\n",
 475			 mvm->disable_power_off_d3);
 476
 477	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 478}
 479
 480static ssize_t iwl_dbgfs_disable_power_off_write(struct iwl_mvm *mvm, char *buf,
 481						 size_t count, loff_t *ppos)
 482{
 483	int ret, val;
 484
 485	if (!iwl_mvm_firmware_running(mvm))
 486		return -EIO;
 487
 488	if (!strncmp("disable_power_off_d0=", buf, 21)) {
 489		if (sscanf(buf + 21, "%d", &val) != 1)
 490			return -EINVAL;
 491		mvm->disable_power_off = val;
 492	} else if (!strncmp("disable_power_off_d3=", buf, 21)) {
 493		if (sscanf(buf + 21, "%d", &val) != 1)
 494			return -EINVAL;
 495		mvm->disable_power_off_d3 = val;
 496	} else {
 497		return -EINVAL;
 498	}
 499
 500	mutex_lock(&mvm->mutex);
 501	ret = iwl_mvm_power_update_device(mvm);
 502	mutex_unlock(&mvm->mutex);
 503
 504	return ret ?: count;
 505}
 506
 507static
 508int iwl_mvm_coex_dump_mbox(struct iwl_bt_coex_profile_notif *notif, char *buf,
 509			   int pos, int bufsz)
 510{
 511	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw0:\n");
 512
 513	BT_MBOX_PRINT(0, LE_SLAVE_LAT, false);
 514	BT_MBOX_PRINT(0, LE_PROF1, false);
 515	BT_MBOX_PRINT(0, LE_PROF2, false);
 516	BT_MBOX_PRINT(0, LE_PROF_OTHER, false);
 517	BT_MBOX_PRINT(0, CHL_SEQ_N, false);
 518	BT_MBOX_PRINT(0, INBAND_S, false);
 519	BT_MBOX_PRINT(0, LE_MIN_RSSI, false);
 520	BT_MBOX_PRINT(0, LE_SCAN, false);
 521	BT_MBOX_PRINT(0, LE_ADV, false);
 522	BT_MBOX_PRINT(0, LE_MAX_TX_POWER, false);
 523	BT_MBOX_PRINT(0, OPEN_CON_1, true);
 524
 525	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw1:\n");
 526
 527	BT_MBOX_PRINT(1, BR_MAX_TX_POWER, false);
 528	BT_MBOX_PRINT(1, IP_SR, false);
 529	BT_MBOX_PRINT(1, LE_MSTR, false);
 530	BT_MBOX_PRINT(1, AGGR_TRFC_LD, false);
 531	BT_MBOX_PRINT(1, MSG_TYPE, false);
 532	BT_MBOX_PRINT(1, SSN, true);
 533
 534	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw2:\n");
 535
 536	BT_MBOX_PRINT(2, SNIFF_ACT, false);
 537	BT_MBOX_PRINT(2, PAG, false);
 538	BT_MBOX_PRINT(2, INQUIRY, false);
 539	BT_MBOX_PRINT(2, CONN, false);
 540	BT_MBOX_PRINT(2, SNIFF_INTERVAL, false);
 541	BT_MBOX_PRINT(2, DISC, false);
 542	BT_MBOX_PRINT(2, SCO_TX_ACT, false);
 543	BT_MBOX_PRINT(2, SCO_RX_ACT, false);
 544	BT_MBOX_PRINT(2, ESCO_RE_TX, false);
 545	BT_MBOX_PRINT(2, SCO_DURATION, true);
 546
 547	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw3:\n");
 548
 549	BT_MBOX_PRINT(3, SCO_STATE, false);
 550	BT_MBOX_PRINT(3, SNIFF_STATE, false);
 551	BT_MBOX_PRINT(3, A2DP_STATE, false);
 552	BT_MBOX_PRINT(3, A2DP_SRC, false);
 553	BT_MBOX_PRINT(3, ACL_STATE, false);
 554	BT_MBOX_PRINT(3, MSTR_STATE, false);
 555	BT_MBOX_PRINT(3, OBX_STATE, false);
 556	BT_MBOX_PRINT(3, OPEN_CON_2, false);
 557	BT_MBOX_PRINT(3, TRAFFIC_LOAD, false);
 558	BT_MBOX_PRINT(3, CHL_SEQN_LSB, false);
 559	BT_MBOX_PRINT(3, INBAND_P, false);
 560	BT_MBOX_PRINT(3, MSG_TYPE_2, false);
 561	BT_MBOX_PRINT(3, SSN_2, false);
 562	BT_MBOX_PRINT(3, UPDATE_REQUEST, true);
 563
 564	return pos;
 565}
 566
 567static ssize_t iwl_dbgfs_bt_notif_read(struct file *file, char __user *user_buf,
 568				       size_t count, loff_t *ppos)
 569{
 570	struct iwl_mvm *mvm = file->private_data;
 571	struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
 572	char *buf;
 573	int ret, pos = 0, bufsz = sizeof(char) * 1024;
 574
 575	buf = kmalloc(bufsz, GFP_KERNEL);
 576	if (!buf)
 577		return -ENOMEM;
 578
 579	mutex_lock(&mvm->mutex);
 580
 581	pos += iwl_mvm_coex_dump_mbox(notif, buf, pos, bufsz);
 582
 583	pos += scnprintf(buf + pos, bufsz - pos, "bt_ci_compliance = %d\n",
 584			 notif->bt_ci_compliance);
 585	pos += scnprintf(buf + pos, bufsz - pos, "primary_ch_lut = %d\n",
 586			 le32_to_cpu(notif->primary_ch_lut));
 587	pos += scnprintf(buf + pos, bufsz - pos, "secondary_ch_lut = %d\n",
 588			 le32_to_cpu(notif->secondary_ch_lut));
 589	pos += scnprintf(buf + pos,
 590			 bufsz - pos, "bt_activity_grading = %d\n",
 591			 le32_to_cpu(notif->bt_activity_grading));
 592	pos += scnprintf(buf + pos, bufsz - pos, "bt_rrc = %d\n",
 593			 notif->rrc_status & 0xF);
 594	pos += scnprintf(buf + pos, bufsz - pos, "bt_ttc = %d\n",
 595			 notif->ttc_status & 0xF);
 596
 597	pos += scnprintf(buf + pos, bufsz - pos, "sync_sco = %d\n",
 598			 IWL_MVM_BT_COEX_SYNC2SCO);
 599	pos += scnprintf(buf + pos, bufsz - pos, "mplut = %d\n",
 600			 IWL_MVM_BT_COEX_MPLUT);
 601
 602	mutex_unlock(&mvm->mutex);
 603
 604	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 605	kfree(buf);
 606
 607	return ret;
 608}
 609#undef BT_MBOX_PRINT
 610
 611static ssize_t iwl_dbgfs_bt_cmd_read(struct file *file, char __user *user_buf,
 612				     size_t count, loff_t *ppos)
 613{
 614	struct iwl_mvm *mvm = file->private_data;
 615	struct iwl_bt_coex_ci_cmd *cmd = &mvm->last_bt_ci_cmd;
 616	char buf[256];
 617	int bufsz = sizeof(buf);
 618	int pos = 0;
 619
 620	mutex_lock(&mvm->mutex);
 621
 622	pos += scnprintf(buf + pos, bufsz - pos, "Channel inhibition CMD\n");
 623	pos += scnprintf(buf + pos, bufsz - pos,
 624			 "\tPrimary Channel Bitmap 0x%016llx\n",
 625			 le64_to_cpu(cmd->bt_primary_ci));
 626	pos += scnprintf(buf + pos, bufsz - pos,
 627			 "\tSecondary Channel Bitmap 0x%016llx\n",
 628			 le64_to_cpu(cmd->bt_secondary_ci));
 629
 630	mutex_unlock(&mvm->mutex);
 631
 632	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 633}
 634
 635static ssize_t
 636iwl_dbgfs_bt_tx_prio_write(struct iwl_mvm *mvm, char *buf,
 637			   size_t count, loff_t *ppos)
 638{
 639	u32 bt_tx_prio;
 640
 641	if (sscanf(buf, "%u", &bt_tx_prio) != 1)
 642		return -EINVAL;
 643	if (bt_tx_prio > 4)
 644		return -EINVAL;
 645
 646	mvm->bt_tx_prio = bt_tx_prio;
 647
 648	return count;
 649}
 650
 651static ssize_t
 652iwl_dbgfs_bt_force_ant_write(struct iwl_mvm *mvm, char *buf,
 653			     size_t count, loff_t *ppos)
 654{
 655	static const char * const modes_str[BT_FORCE_ANT_MAX] = {
 656		[BT_FORCE_ANT_DIS] = "dis",
 657		[BT_FORCE_ANT_AUTO] = "auto",
 658		[BT_FORCE_ANT_BT] = "bt",
 659		[BT_FORCE_ANT_WIFI] = "wifi",
 660	};
 661	int ret, bt_force_ant_mode;
 662
 663	ret = match_string(modes_str, ARRAY_SIZE(modes_str), buf);
 664	if (ret < 0)
 665		return ret;
 666
 667	bt_force_ant_mode = ret;
 668	ret = 0;
 669	mutex_lock(&mvm->mutex);
 670	if (mvm->bt_force_ant_mode == bt_force_ant_mode)
 671		goto out;
 672
 673	mvm->bt_force_ant_mode = bt_force_ant_mode;
 674	IWL_DEBUG_COEX(mvm, "Force mode: %s\n",
 675		       modes_str[mvm->bt_force_ant_mode]);
 676
 677	if (iwl_mvm_firmware_running(mvm))
 678		ret = iwl_mvm_send_bt_init_conf(mvm);
 679	else
 680		ret = 0;
 681
 682out:
 683	mutex_unlock(&mvm->mutex);
 684	return ret ?: count;
 685}
 686
 687static ssize_t iwl_dbgfs_fw_ver_read(struct file *file, char __user *user_buf,
 688				     size_t count, loff_t *ppos)
 689{
 690	struct iwl_mvm *mvm = file->private_data;
 691	char *buff, *pos, *endpos;
 692	static const size_t bufsz = 1024;
 
 693	int ret;
 694
 695	buff = kmalloc(bufsz, GFP_KERNEL);
 696	if (!buff)
 697		return -ENOMEM;
 698
 699	pos = buff;
 700	endpos = pos + bufsz;
 701
 702	pos += scnprintf(pos, endpos - pos, "FW prefix: %s\n",
 703			 mvm->trans->cfg->fw_name_pre);
 704	pos += scnprintf(pos, endpos - pos, "FW: %s\n",
 705			 mvm->fwrt.fw->human_readable);
 706	pos += scnprintf(pos, endpos - pos, "Device: %s\n",
 707			 mvm->fwrt.trans->name);
 708	pos += scnprintf(pos, endpos - pos, "Bus: %s\n",
 709			 mvm->fwrt.dev->bus->name);
 710
 711	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
 712	kfree(buff);
 713
 714	return ret;
 715}
 716
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 717static ssize_t iwl_dbgfs_phy_integration_ver_read(struct file *file,
 718						  char __user *user_buf,
 719						  size_t count, loff_t *ppos)
 720{
 721	struct iwl_mvm *mvm = file->private_data;
 722	char *buf;
 723	size_t bufsz;
 724	int pos;
 725	ssize_t ret;
 726
 727	bufsz = mvm->fw->phy_integration_ver_len + 2;
 728	buf = kmalloc(bufsz, GFP_KERNEL);
 729	if (!buf)
 730		return -ENOMEM;
 731
 732	pos = scnprintf(buf, bufsz, "%.*s\n", mvm->fw->phy_integration_ver_len,
 733			mvm->fw->phy_integration_ver);
 734
 735	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 736
 737	kfree(buf);
 738	return ret;
 739}
 740
 741#define PRINT_STATS_LE32(_struct, _memb)				\
 742			 pos += scnprintf(buf + pos, bufsz - pos,	\
 743					  fmt_table, #_memb,		\
 744					  le32_to_cpu(_struct->_memb))
 745
 746static ssize_t iwl_dbgfs_fw_rx_stats_read(struct file *file,
 747					  char __user *user_buf, size_t count,
 748					  loff_t *ppos)
 749{
 750	struct iwl_mvm *mvm = file->private_data;
 751	static const char *fmt_table = "\t%-30s %10u\n";
 752	static const char *fmt_header = "%-32s\n";
 753	int pos = 0;
 754	char *buf;
 755	int ret;
 756	size_t bufsz;
 
 
 
 
 
 
 
 757
 758	if (iwl_mvm_has_new_rx_stats_api(mvm))
 759		bufsz = ((sizeof(struct mvm_statistics_rx) /
 760			  sizeof(__le32)) * 43) + (4 * 33) + 1;
 761	else
 762		/* 43 = size of each data line; 33 = size of each header */
 763		bufsz = ((sizeof(struct mvm_statistics_rx_v3) /
 764			  sizeof(__le32)) * 43) + (4 * 33) + 1;
 765
 766	buf = kzalloc(bufsz, GFP_KERNEL);
 767	if (!buf)
 768		return -ENOMEM;
 769
 770	mutex_lock(&mvm->mutex);
 771
 772	if (iwl_mvm_firmware_running(mvm))
 773		iwl_mvm_request_statistics(mvm, false);
 774
 775	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
 776			 "Statistics_Rx - OFDM");
 777	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
 778		struct mvm_statistics_rx_phy_v2 *ofdm = &mvm->rx_stats_v3.ofdm;
 779
 780		PRINT_STATS_LE32(ofdm, ina_cnt);
 781		PRINT_STATS_LE32(ofdm, fina_cnt);
 782		PRINT_STATS_LE32(ofdm, plcp_err);
 783		PRINT_STATS_LE32(ofdm, crc32_err);
 784		PRINT_STATS_LE32(ofdm, overrun_err);
 785		PRINT_STATS_LE32(ofdm, early_overrun_err);
 786		PRINT_STATS_LE32(ofdm, crc32_good);
 787		PRINT_STATS_LE32(ofdm, false_alarm_cnt);
 788		PRINT_STATS_LE32(ofdm, fina_sync_err_cnt);
 789		PRINT_STATS_LE32(ofdm, sfd_timeout);
 790		PRINT_STATS_LE32(ofdm, fina_timeout);
 791		PRINT_STATS_LE32(ofdm, unresponded_rts);
 792		PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
 793		PRINT_STATS_LE32(ofdm, sent_ack_cnt);
 794		PRINT_STATS_LE32(ofdm, sent_cts_cnt);
 795		PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
 796		PRINT_STATS_LE32(ofdm, dsp_self_kill);
 797		PRINT_STATS_LE32(ofdm, mh_format_err);
 798		PRINT_STATS_LE32(ofdm, re_acq_main_rssi_sum);
 799		PRINT_STATS_LE32(ofdm, reserved);
 800	} else {
 801		struct mvm_statistics_rx_phy *ofdm = &mvm->rx_stats.ofdm;
 802
 803		PRINT_STATS_LE32(ofdm, unresponded_rts);
 804		PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
 805		PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
 806		PRINT_STATS_LE32(ofdm, dsp_self_kill);
 807		PRINT_STATS_LE32(ofdm, reserved);
 808	}
 809
 810	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
 811			 "Statistics_Rx - CCK");
 812	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
 813		struct mvm_statistics_rx_phy_v2 *cck = &mvm->rx_stats_v3.cck;
 814
 815		PRINT_STATS_LE32(cck, ina_cnt);
 816		PRINT_STATS_LE32(cck, fina_cnt);
 817		PRINT_STATS_LE32(cck, plcp_err);
 818		PRINT_STATS_LE32(cck, crc32_err);
 819		PRINT_STATS_LE32(cck, overrun_err);
 820		PRINT_STATS_LE32(cck, early_overrun_err);
 821		PRINT_STATS_LE32(cck, crc32_good);
 822		PRINT_STATS_LE32(cck, false_alarm_cnt);
 823		PRINT_STATS_LE32(cck, fina_sync_err_cnt);
 824		PRINT_STATS_LE32(cck, sfd_timeout);
 825		PRINT_STATS_LE32(cck, fina_timeout);
 826		PRINT_STATS_LE32(cck, unresponded_rts);
 827		PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
 828		PRINT_STATS_LE32(cck, sent_ack_cnt);
 829		PRINT_STATS_LE32(cck, sent_cts_cnt);
 830		PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
 831		PRINT_STATS_LE32(cck, dsp_self_kill);
 832		PRINT_STATS_LE32(cck, mh_format_err);
 833		PRINT_STATS_LE32(cck, re_acq_main_rssi_sum);
 834		PRINT_STATS_LE32(cck, reserved);
 835	} else {
 836		struct mvm_statistics_rx_phy *cck = &mvm->rx_stats.cck;
 837
 838		PRINT_STATS_LE32(cck, unresponded_rts);
 839		PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
 840		PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
 841		PRINT_STATS_LE32(cck, dsp_self_kill);
 842		PRINT_STATS_LE32(cck, reserved);
 843	}
 844
 845	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
 846			 "Statistics_Rx - GENERAL");
 847	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
 848		struct mvm_statistics_rx_non_phy_v3 *general =
 849			&mvm->rx_stats_v3.general;
 850
 851		PRINT_STATS_LE32(general, bogus_cts);
 852		PRINT_STATS_LE32(general, bogus_ack);
 853		PRINT_STATS_LE32(general, non_bssid_frames);
 854		PRINT_STATS_LE32(general, filtered_frames);
 855		PRINT_STATS_LE32(general, non_channel_beacons);
 856		PRINT_STATS_LE32(general, channel_beacons);
 857		PRINT_STATS_LE32(general, num_missed_bcon);
 858		PRINT_STATS_LE32(general, adc_rx_saturation_time);
 859		PRINT_STATS_LE32(general, ina_detection_search_time);
 860		PRINT_STATS_LE32(general, beacon_silence_rssi_a);
 861		PRINT_STATS_LE32(general, beacon_silence_rssi_b);
 862		PRINT_STATS_LE32(general, beacon_silence_rssi_c);
 863		PRINT_STATS_LE32(general, interference_data_flag);
 864		PRINT_STATS_LE32(general, channel_load);
 865		PRINT_STATS_LE32(general, dsp_false_alarms);
 866		PRINT_STATS_LE32(general, beacon_rssi_a);
 867		PRINT_STATS_LE32(general, beacon_rssi_b);
 868		PRINT_STATS_LE32(general, beacon_rssi_c);
 869		PRINT_STATS_LE32(general, beacon_energy_a);
 870		PRINT_STATS_LE32(general, beacon_energy_b);
 871		PRINT_STATS_LE32(general, beacon_energy_c);
 872		PRINT_STATS_LE32(general, num_bt_kills);
 873		PRINT_STATS_LE32(general, mac_id);
 874		PRINT_STATS_LE32(general, directed_data_mpdu);
 875	} else {
 876		struct mvm_statistics_rx_non_phy *general =
 877			&mvm->rx_stats.general;
 878
 879		PRINT_STATS_LE32(general, bogus_cts);
 880		PRINT_STATS_LE32(general, bogus_ack);
 881		PRINT_STATS_LE32(general, non_channel_beacons);
 882		PRINT_STATS_LE32(general, channel_beacons);
 883		PRINT_STATS_LE32(general, num_missed_bcon);
 884		PRINT_STATS_LE32(general, adc_rx_saturation_time);
 885		PRINT_STATS_LE32(general, ina_detection_search_time);
 886		PRINT_STATS_LE32(general, beacon_silence_rssi_a);
 887		PRINT_STATS_LE32(general, beacon_silence_rssi_b);
 888		PRINT_STATS_LE32(general, beacon_silence_rssi_c);
 889		PRINT_STATS_LE32(general, interference_data_flag);
 890		PRINT_STATS_LE32(general, channel_load);
 891		PRINT_STATS_LE32(general, beacon_rssi_a);
 892		PRINT_STATS_LE32(general, beacon_rssi_b);
 893		PRINT_STATS_LE32(general, beacon_rssi_c);
 894		PRINT_STATS_LE32(general, beacon_energy_a);
 895		PRINT_STATS_LE32(general, beacon_energy_b);
 896		PRINT_STATS_LE32(general, beacon_energy_c);
 897		PRINT_STATS_LE32(general, num_bt_kills);
 898		PRINT_STATS_LE32(general, mac_id);
 899	}
 900
 901	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
 902			 "Statistics_Rx - HT");
 903	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
 904		struct mvm_statistics_rx_ht_phy_v1 *ht =
 905			&mvm->rx_stats_v3.ofdm_ht;
 906
 907		PRINT_STATS_LE32(ht, plcp_err);
 908		PRINT_STATS_LE32(ht, overrun_err);
 909		PRINT_STATS_LE32(ht, early_overrun_err);
 910		PRINT_STATS_LE32(ht, crc32_good);
 911		PRINT_STATS_LE32(ht, crc32_err);
 912		PRINT_STATS_LE32(ht, mh_format_err);
 913		PRINT_STATS_LE32(ht, agg_crc32_good);
 914		PRINT_STATS_LE32(ht, agg_mpdu_cnt);
 915		PRINT_STATS_LE32(ht, agg_cnt);
 916		PRINT_STATS_LE32(ht, unsupport_mcs);
 917	} else {
 918		struct mvm_statistics_rx_ht_phy *ht =
 919			&mvm->rx_stats.ofdm_ht;
 920
 921		PRINT_STATS_LE32(ht, mh_format_err);
 922		PRINT_STATS_LE32(ht, agg_mpdu_cnt);
 923		PRINT_STATS_LE32(ht, agg_cnt);
 924		PRINT_STATS_LE32(ht, unsupport_mcs);
 925	}
 926
 927	mutex_unlock(&mvm->mutex);
 928
 929	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 930	kfree(buf);
 931
 932	return ret;
 933}
 934#undef PRINT_STAT_LE32
 935
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 936static ssize_t iwl_dbgfs_frame_stats_read(struct iwl_mvm *mvm,
 937					  char __user *user_buf, size_t count,
 938					  loff_t *ppos,
 939					  struct iwl_mvm_frame_stats *stats)
 940{
 941	char *buff, *pos, *endpos;
 942	int idx, i;
 943	int ret;
 944	static const size_t bufsz = 1024;
 945
 946	buff = kmalloc(bufsz, GFP_KERNEL);
 947	if (!buff)
 948		return -ENOMEM;
 949
 950	spin_lock_bh(&mvm->drv_stats_lock);
 951
 952	pos = buff;
 953	endpos = pos + bufsz;
 954
 955	pos += scnprintf(pos, endpos - pos,
 956			 "Legacy/HT/VHT\t:\t%d/%d/%d\n",
 957			 stats->legacy_frames,
 958			 stats->ht_frames,
 959			 stats->vht_frames);
 960	pos += scnprintf(pos, endpos - pos, "20/40/80\t:\t%d/%d/%d\n",
 961			 stats->bw_20_frames,
 962			 stats->bw_40_frames,
 963			 stats->bw_80_frames);
 964	pos += scnprintf(pos, endpos - pos, "NGI/SGI\t\t:\t%d/%d\n",
 965			 stats->ngi_frames,
 966			 stats->sgi_frames);
 967	pos += scnprintf(pos, endpos - pos, "SISO/MIMO2\t:\t%d/%d\n",
 968			 stats->siso_frames,
 969			 stats->mimo2_frames);
 970	pos += scnprintf(pos, endpos - pos, "FAIL/SCSS\t:\t%d/%d\n",
 971			 stats->fail_frames,
 972			 stats->success_frames);
 973	pos += scnprintf(pos, endpos - pos, "MPDUs agg\t:\t%d\n",
 974			 stats->agg_frames);
 975	pos += scnprintf(pos, endpos - pos, "A-MPDUs\t\t:\t%d\n",
 976			 stats->ampdu_count);
 977	pos += scnprintf(pos, endpos - pos, "Avg MPDUs/A-MPDU:\t%d\n",
 978			 stats->ampdu_count > 0 ?
 979			 (stats->agg_frames / stats->ampdu_count) : 0);
 980
 981	pos += scnprintf(pos, endpos - pos, "Last Rates\n");
 982
 983	idx = stats->last_frame_idx - 1;
 984	for (i = 0; i < ARRAY_SIZE(stats->last_rates); i++) {
 985		idx = (idx + 1) % ARRAY_SIZE(stats->last_rates);
 986		if (stats->last_rates[idx] == 0)
 987			continue;
 988		pos += scnprintf(pos, endpos - pos, "Rate[%d]: ",
 989				 (int)(ARRAY_SIZE(stats->last_rates) - i));
 990		pos += rs_pretty_print_rate_v1(pos, endpos - pos,
 991					       stats->last_rates[idx]);
 992		if (pos < endpos - 1)
 993			*pos++ = '\n';
 994	}
 995	spin_unlock_bh(&mvm->drv_stats_lock);
 996
 997	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
 998	kfree(buff);
 999
1000	return ret;
1001}
1002
1003static ssize_t iwl_dbgfs_drv_rx_stats_read(struct file *file,
1004					   char __user *user_buf, size_t count,
1005					   loff_t *ppos)
1006{
1007	struct iwl_mvm *mvm = file->private_data;
1008
1009	return iwl_dbgfs_frame_stats_read(mvm, user_buf, count, ppos,
1010					  &mvm->drv_rx_stats);
1011}
1012
1013static ssize_t iwl_dbgfs_fw_restart_write(struct iwl_mvm *mvm, char *buf,
1014					  size_t count, loff_t *ppos)
1015{
1016	int __maybe_unused ret;
1017
1018	if (!iwl_mvm_firmware_running(mvm))
1019		return -EIO;
1020
1021	mutex_lock(&mvm->mutex);
1022
1023	/* allow one more restart that we're provoking here */
1024	if (mvm->fw_restart >= 0)
1025		mvm->fw_restart++;
1026
1027	if (count == 6 && !strcmp(buf, "nolog\n")) {
1028		set_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE, &mvm->status);
1029		set_bit(STATUS_SUPPRESS_CMD_ERROR_ONCE, &mvm->trans->status);
1030	}
1031
1032	/* take the return value to make compiler happy - it will fail anyway */
1033	ret = iwl_mvm_send_cmd_pdu(mvm,
1034				   WIDE_ID(LONG_GROUP, REPLY_ERROR),
1035				   0, 0, NULL);
1036
1037	mutex_unlock(&mvm->mutex);
1038
1039	return count;
1040}
1041
1042static ssize_t iwl_dbgfs_fw_nmi_write(struct iwl_mvm *mvm, char *buf,
1043				      size_t count, loff_t *ppos)
1044{
1045	if (!iwl_mvm_firmware_running(mvm))
1046		return -EIO;
1047
1048	if (count == 6 && !strcmp(buf, "nolog\n"))
1049		set_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE, &mvm->status);
1050
1051	iwl_force_nmi(mvm->trans);
1052
1053	return count;
1054}
1055
1056static ssize_t
1057iwl_dbgfs_scan_ant_rxchain_read(struct file *file,
1058				char __user *user_buf,
1059				size_t count, loff_t *ppos)
1060{
1061	struct iwl_mvm *mvm = file->private_data;
1062	int pos = 0;
1063	char buf[32];
1064	const size_t bufsz = sizeof(buf);
1065
1066	/* print which antennas were set for the scan command by the user */
1067	pos += scnprintf(buf + pos, bufsz - pos, "Antennas for scan: ");
1068	if (mvm->scan_rx_ant & ANT_A)
1069		pos += scnprintf(buf + pos, bufsz - pos, "A");
1070	if (mvm->scan_rx_ant & ANT_B)
1071		pos += scnprintf(buf + pos, bufsz - pos, "B");
1072	pos += scnprintf(buf + pos, bufsz - pos, " (%hhx)\n", mvm->scan_rx_ant);
1073
1074	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1075}
1076
1077static ssize_t
1078iwl_dbgfs_scan_ant_rxchain_write(struct iwl_mvm *mvm, char *buf,
1079				 size_t count, loff_t *ppos)
1080{
1081	u8 scan_rx_ant;
1082
1083	if (!iwl_mvm_firmware_running(mvm))
1084		return -EIO;
1085
1086	if (sscanf(buf, "%hhx", &scan_rx_ant) != 1)
1087		return -EINVAL;
1088	if (scan_rx_ant > ANT_ABC)
1089		return -EINVAL;
1090	if (scan_rx_ant & ~(iwl_mvm_get_valid_rx_ant(mvm)))
1091		return -EINVAL;
1092
1093	if (mvm->scan_rx_ant != scan_rx_ant) {
1094		mvm->scan_rx_ant = scan_rx_ant;
1095		if (fw_has_capa(&mvm->fw->ucode_capa,
1096				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1097			iwl_mvm_config_scan(mvm);
1098	}
1099
1100	return count;
1101}
1102
1103static ssize_t iwl_dbgfs_indirection_tbl_write(struct iwl_mvm *mvm,
1104					       char *buf, size_t count,
1105					       loff_t *ppos)
1106{
1107	struct iwl_rss_config_cmd cmd = {
1108		.flags = cpu_to_le32(IWL_RSS_ENABLE),
1109		.hash_mask = IWL_RSS_HASH_TYPE_IPV4_TCP |
1110			     IWL_RSS_HASH_TYPE_IPV4_UDP |
1111			     IWL_RSS_HASH_TYPE_IPV4_PAYLOAD |
1112			     IWL_RSS_HASH_TYPE_IPV6_TCP |
1113			     IWL_RSS_HASH_TYPE_IPV6_UDP |
1114			     IWL_RSS_HASH_TYPE_IPV6_PAYLOAD,
1115	};
1116	int ret, i, num_repeats, nbytes = count / 2;
1117
1118	ret = hex2bin(cmd.indirection_table, buf, nbytes);
1119	if (ret)
1120		return ret;
1121
1122	/*
1123	 * The input is the redirection table, partial or full.
1124	 * Repeat the pattern if needed.
1125	 * For example, input of 01020F will be repeated 42 times,
1126	 * indirecting RSS hash results to queues 1, 2, 15 (skipping
1127	 * queues 3 - 14).
1128	 */
1129	num_repeats = ARRAY_SIZE(cmd.indirection_table) / nbytes;
1130	for (i = 1; i < num_repeats; i++)
1131		memcpy(&cmd.indirection_table[i * nbytes],
1132		       cmd.indirection_table, nbytes);
1133	/* handle cut in the middle pattern for the last places */
1134	memcpy(&cmd.indirection_table[i * nbytes], cmd.indirection_table,
1135	       ARRAY_SIZE(cmd.indirection_table) % nbytes);
1136
1137	netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
1138
1139	mutex_lock(&mvm->mutex);
1140	if (iwl_mvm_firmware_running(mvm))
1141		ret = iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0,
1142					   sizeof(cmd), &cmd);
1143	else
1144		ret = 0;
1145	mutex_unlock(&mvm->mutex);
1146
1147	return ret ?: count;
1148}
1149
1150static ssize_t iwl_dbgfs_inject_packet_write(struct iwl_mvm *mvm,
1151					     char *buf, size_t count,
1152					     loff_t *ppos)
1153{
1154	struct iwl_op_mode *opmode = container_of((void *)mvm,
1155						  struct iwl_op_mode,
1156						  op_mode_specific);
1157	struct iwl_rx_cmd_buffer rxb = {
1158		._rx_page_order = 0,
1159		.truesize = 0, /* not used */
1160		._offset = 0,
1161	};
1162	struct iwl_rx_packet *pkt;
1163	int bin_len = count / 2;
1164	int ret = -EINVAL;
1165
1166	if (!iwl_mvm_firmware_running(mvm))
1167		return -EIO;
1168
1169	/* supporting only MQ RX */
1170	if (!mvm->trans->trans_cfg->mq_rx_supported)
1171		return -ENOTSUPP;
1172
1173	rxb._page = alloc_pages(GFP_ATOMIC, 0);
1174	if (!rxb._page)
1175		return -ENOMEM;
1176	pkt = rxb_addr(&rxb);
1177
1178	ret = hex2bin(page_address(rxb._page), buf, bin_len);
1179	if (ret)
1180		goto out;
1181
1182	/* avoid invalid memory access and malformed packet */
1183	if (bin_len < sizeof(*pkt) ||
1184	    bin_len != sizeof(*pkt) + iwl_rx_packet_payload_len(pkt))
1185		goto out;
1186
1187	local_bh_disable();
1188	iwl_mvm_rx_mq(opmode, NULL, &rxb);
1189	local_bh_enable();
1190	ret = 0;
1191
1192out:
1193	iwl_free_rxb(&rxb);
1194
1195	return ret ?: count;
1196}
1197
1198static int _iwl_dbgfs_inject_beacon_ie(struct iwl_mvm *mvm, char *bin, int len)
1199{
1200	struct ieee80211_vif *vif;
1201	struct iwl_mvm_vif *mvmvif;
1202	struct sk_buff *beacon;
1203	struct ieee80211_tx_info *info;
1204	struct iwl_mac_beacon_cmd beacon_cmd = {};
 
1205	u8 rate;
1206	int i;
1207
1208	len /= 2;
1209
1210	/* Element len should be represented by u8 */
1211	if (len >= U8_MAX)
1212		return -EINVAL;
1213
1214	if (!iwl_mvm_firmware_running(mvm))
1215		return -EIO;
1216
1217	if (!iwl_mvm_has_new_tx_api(mvm) &&
1218	    !fw_has_api(&mvm->fw->ucode_capa,
1219			IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
1220		return -EINVAL;
1221
1222	mutex_lock(&mvm->mutex);
1223
1224	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
1225		vif = iwl_mvm_rcu_dereference_vif_id(mvm, i, false);
1226		if (!vif)
1227			continue;
1228
1229		if (vif->type == NL80211_IFTYPE_AP)
1230			break;
1231	}
1232
1233	if (i == NUM_MAC_INDEX_DRIVER || !vif)
1234		goto out_err;
1235
1236	mvm->hw->extra_beacon_tailroom = len;
1237
1238	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL, 0);
1239	if (!beacon)
1240		goto out_err;
1241
1242	if (len && hex2bin(skb_put_zero(beacon, len), bin, len)) {
1243		dev_kfree_skb(beacon);
1244		goto out_err;
1245	}
1246
1247	mvm->beacon_inject_active = true;
1248
1249	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1250	info = IEEE80211_SKB_CB(beacon);
1251	rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1252
1253	beacon_cmd.flags =
1254		cpu_to_le16(iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw, rate));
1255	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1256	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1257
1258	iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1259				 &beacon_cmd.tim_size,
1260				 beacon->data, beacon->len);
 
 
1261
1262	iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1263					 sizeof(beacon_cmd));
 
 
 
 
 
1264	mutex_unlock(&mvm->mutex);
1265
1266	dev_kfree_skb(beacon);
1267
1268	return 0;
1269
1270out_err:
1271	mutex_unlock(&mvm->mutex);
1272	return -EINVAL;
1273}
1274
1275static ssize_t iwl_dbgfs_inject_beacon_ie_write(struct iwl_mvm *mvm,
1276						char *buf, size_t count,
1277						loff_t *ppos)
1278{
1279	int ret = _iwl_dbgfs_inject_beacon_ie(mvm, buf, count);
1280
1281	mvm->hw->extra_beacon_tailroom = 0;
1282	return ret ?: count;
1283}
1284
1285static ssize_t iwl_dbgfs_inject_beacon_ie_restore_write(struct iwl_mvm *mvm,
1286							char *buf,
1287							size_t count,
1288							loff_t *ppos)
1289{
1290	int ret = _iwl_dbgfs_inject_beacon_ie(mvm, NULL, 0);
1291
1292	mvm->hw->extra_beacon_tailroom = 0;
1293	mvm->beacon_inject_active = false;
1294	return ret ?: count;
1295}
1296
1297static ssize_t iwl_dbgfs_fw_dbg_conf_read(struct file *file,
1298					  char __user *user_buf,
1299					  size_t count, loff_t *ppos)
1300{
1301	struct iwl_mvm *mvm = file->private_data;
1302	int conf;
1303	char buf[8];
1304	const size_t bufsz = sizeof(buf);
1305	int pos = 0;
1306
1307	mutex_lock(&mvm->mutex);
1308	conf = mvm->fwrt.dump.conf;
1309	mutex_unlock(&mvm->mutex);
1310
1311	pos += scnprintf(buf + pos, bufsz - pos, "%d\n", conf);
1312
1313	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1314}
1315
1316static ssize_t iwl_dbgfs_fw_dbg_conf_write(struct iwl_mvm *mvm,
1317					   char *buf, size_t count,
1318					   loff_t *ppos)
1319{
1320	unsigned int conf_id;
1321	int ret;
1322
1323	if (!iwl_mvm_firmware_running(mvm))
1324		return -EIO;
1325
1326	ret = kstrtouint(buf, 0, &conf_id);
1327	if (ret)
1328		return ret;
1329
1330	if (WARN_ON(conf_id >= FW_DBG_CONF_MAX))
1331		return -EINVAL;
1332
1333	mutex_lock(&mvm->mutex);
1334	ret = iwl_fw_start_dbg_conf(&mvm->fwrt, conf_id);
1335	mutex_unlock(&mvm->mutex);
1336
1337	return ret ?: count;
1338}
1339
1340static ssize_t iwl_dbgfs_fw_dbg_collect_write(struct iwl_mvm *mvm,
1341					      char *buf, size_t count,
1342					      loff_t *ppos)
1343{
1344	if (count == 0)
1345		return 0;
1346
1347	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_USER_TRIGGER,
1348			       NULL);
1349
1350	iwl_fw_dbg_collect(&mvm->fwrt, FW_DBG_TRIGGER_USER, buf,
1351			   (count - 1), NULL);
1352
1353	return count;
1354}
1355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1356static ssize_t iwl_dbgfs_dbg_time_point_write(struct iwl_mvm *mvm,
1357					      char *buf, size_t count,
1358					      loff_t *ppos)
1359{
1360	u32 timepoint;
1361
1362	if (kstrtou32(buf, 0, &timepoint))
1363		return -EINVAL;
1364
1365	if (timepoint == IWL_FW_INI_TIME_POINT_INVALID ||
1366	    timepoint >= IWL_FW_INI_TIME_POINT_NUM)
1367		return -EINVAL;
1368
1369	iwl_dbg_tlv_time_point(&mvm->fwrt, timepoint, NULL);
1370
1371	return count;
1372}
1373
1374#define MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz) \
1375	_MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1376#define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
1377	_MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1378#define MVM_DEBUGFS_ADD_FILE_ALIAS(alias, name, parent, mode) do {	\
1379		debugfs_create_file(alias, mode, parent, mvm,		\
1380				    &iwl_dbgfs_##name##_ops);		\
1381	} while (0)
1382#define MVM_DEBUGFS_ADD_FILE(name, parent, mode) \
1383	MVM_DEBUGFS_ADD_FILE_ALIAS(#name, name, parent, mode)
1384
1385#define MVM_DEBUGFS_WRITE_STA_FILE_OPS(name, bufsz) \
1386	_MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct ieee80211_sta)
1387#define MVM_DEBUGFS_READ_WRITE_STA_FILE_OPS(name, bufsz) \
1388	_MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct ieee80211_sta)
 
 
 
 
 
 
 
 
 
 
 
1389
1390#define MVM_DEBUGFS_ADD_STA_FILE_ALIAS(alias, name, parent, mode) do {	\
1391		debugfs_create_file(alias, mode, parent, sta,		\
1392				    &iwl_dbgfs_##name##_ops);		\
1393	} while (0)
1394#define MVM_DEBUGFS_ADD_STA_FILE(name, parent, mode) \
1395	MVM_DEBUGFS_ADD_STA_FILE_ALIAS(#name, name, parent, mode)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1396
1397static ssize_t
1398iwl_dbgfs_prph_reg_read(struct file *file,
1399			char __user *user_buf,
1400			size_t count, loff_t *ppos)
1401{
1402	struct iwl_mvm *mvm = file->private_data;
1403	int pos = 0;
1404	char buf[32];
1405	const size_t bufsz = sizeof(buf);
1406
1407	if (!mvm->dbgfs_prph_reg_addr)
1408		return -EINVAL;
1409
1410	pos += scnprintf(buf + pos, bufsz - pos, "Reg 0x%x: (0x%x)\n",
1411		mvm->dbgfs_prph_reg_addr,
1412		iwl_read_prph(mvm->trans, mvm->dbgfs_prph_reg_addr));
1413
1414	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1415}
1416
1417static ssize_t
1418iwl_dbgfs_prph_reg_write(struct iwl_mvm *mvm, char *buf,
1419			 size_t count, loff_t *ppos)
1420{
1421	u8 args;
1422	u32 value;
1423
1424	args = sscanf(buf, "%i %i", &mvm->dbgfs_prph_reg_addr, &value);
1425	/* if we only want to set the reg address - nothing more to do */
1426	if (args == 1)
1427		goto out;
1428
1429	/* otherwise, make sure we have both address and value */
1430	if (args != 2)
1431		return -EINVAL;
1432
1433	iwl_write_prph(mvm->trans, mvm->dbgfs_prph_reg_addr, value);
1434
1435out:
1436	return count;
1437}
1438
1439static ssize_t
1440iwl_dbgfs_send_echo_cmd_write(struct iwl_mvm *mvm, char *buf,
1441			      size_t count, loff_t *ppos)
1442{
1443	int ret;
1444
1445	if (!iwl_mvm_firmware_running(mvm))
1446		return -EIO;
1447
1448	mutex_lock(&mvm->mutex);
1449	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1450	mutex_unlock(&mvm->mutex);
1451
1452	return ret ?: count;
1453}
1454
1455struct iwl_mvm_sniffer_apply {
1456	struct iwl_mvm *mvm;
1457	u8 *bssid;
1458	u16 aid;
1459};
1460
1461static bool iwl_mvm_sniffer_apply(struct iwl_notif_wait_data *notif_data,
1462				  struct iwl_rx_packet *pkt, void *data)
1463{
1464	struct iwl_mvm_sniffer_apply *apply = data;
1465
1466	apply->mvm->cur_aid = cpu_to_le16(apply->aid);
1467	memcpy(apply->mvm->cur_bssid, apply->bssid,
1468	       sizeof(apply->mvm->cur_bssid));
1469
1470	return true;
1471}
1472
1473static ssize_t
1474iwl_dbgfs_he_sniffer_params_write(struct iwl_mvm *mvm, char *buf,
1475				  size_t count, loff_t *ppos)
1476{
1477	struct iwl_notification_wait wait;
1478	struct iwl_he_monitor_cmd he_mon_cmd = {};
1479	struct iwl_mvm_sniffer_apply apply = {
1480		.mvm = mvm,
1481	};
1482	u16 wait_cmds[] = {
1483		WIDE_ID(DATA_PATH_GROUP, HE_AIR_SNIFFER_CONFIG_CMD),
1484	};
1485	u32 aid;
1486	int ret;
1487
1488	if (!iwl_mvm_firmware_running(mvm))
1489		return -EIO;
1490
1491	ret = sscanf(buf, "%x %2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", &aid,
1492		     &he_mon_cmd.bssid[0], &he_mon_cmd.bssid[1],
1493		     &he_mon_cmd.bssid[2], &he_mon_cmd.bssid[3],
1494		     &he_mon_cmd.bssid[4], &he_mon_cmd.bssid[5]);
1495	if (ret != 7)
1496		return -EINVAL;
1497
1498	he_mon_cmd.aid = cpu_to_le16(aid);
1499
1500	apply.aid = aid;
1501	apply.bssid = (void *)he_mon_cmd.bssid;
1502
1503	mutex_lock(&mvm->mutex);
1504
1505	/*
1506	 * Use the notification waiter to get our function triggered
1507	 * in sequence with other RX. This ensures that frames we get
1508	 * on the RX queue _before_ the new configuration is applied
1509	 * still have mvm->cur_aid pointing to the old AID, and that
1510	 * frames on the RX queue _after_ the firmware processed the
1511	 * new configuration (and sent the response, synchronously)
1512	 * get mvm->cur_aid correctly set to the new AID.
1513	 */
1514	iwl_init_notification_wait(&mvm->notif_wait, &wait,
1515				   wait_cmds, ARRAY_SIZE(wait_cmds),
1516				   iwl_mvm_sniffer_apply, &apply);
1517
1518	ret = iwl_mvm_send_cmd_pdu(mvm,
1519				   WIDE_ID(DATA_PATH_GROUP, HE_AIR_SNIFFER_CONFIG_CMD),
1520				   0,
1521				   sizeof(he_mon_cmd), &he_mon_cmd);
1522
1523	/* no need to really wait, we already did anyway */
1524	iwl_remove_notification(&mvm->notif_wait, &wait);
1525
1526	mutex_unlock(&mvm->mutex);
1527
1528	return ret ?: count;
1529}
1530
1531static ssize_t
1532iwl_dbgfs_he_sniffer_params_read(struct file *file, char __user *user_buf,
1533				 size_t count, loff_t *ppos)
1534{
1535	struct iwl_mvm *mvm = file->private_data;
1536	u8 buf[32];
1537	int len;
1538
1539	len = scnprintf(buf, sizeof(buf),
1540			"%d %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
1541			le16_to_cpu(mvm->cur_aid), mvm->cur_bssid[0],
1542			mvm->cur_bssid[1], mvm->cur_bssid[2], mvm->cur_bssid[3],
1543			mvm->cur_bssid[4], mvm->cur_bssid[5]);
1544
1545	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1546}
1547
1548static ssize_t
1549iwl_dbgfs_uapsd_noagg_bssids_read(struct file *file, char __user *user_buf,
1550				  size_t count, loff_t *ppos)
1551{
1552	struct iwl_mvm *mvm = file->private_data;
1553	u8 buf[IWL_MVM_UAPSD_NOAGG_BSSIDS_NUM * ETH_ALEN * 3 + 1];
1554	unsigned int pos = 0;
1555	size_t bufsz = sizeof(buf);
1556	int i;
1557
1558	mutex_lock(&mvm->mutex);
1559
1560	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++)
1561		pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
1562				 mvm->uapsd_noagg_bssids[i].addr);
1563
1564	mutex_unlock(&mvm->mutex);
1565
1566	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1567}
1568
1569static ssize_t
1570iwl_dbgfs_ltr_config_write(struct iwl_mvm *mvm,
1571			   char *buf, size_t count, loff_t *ppos)
1572{
1573	int ret;
1574	struct iwl_ltr_config_cmd ltr_config = {0};
1575
1576	if (!iwl_mvm_firmware_running(mvm))
1577		return -EIO;
1578
1579	if (sscanf(buf, "%x,%x,%x,%x,%x,%x,%x",
1580		   &ltr_config.flags,
1581		   &ltr_config.static_long,
1582		   &ltr_config.static_short,
1583		   &ltr_config.ltr_cfg_values[0],
1584		   &ltr_config.ltr_cfg_values[1],
1585		   &ltr_config.ltr_cfg_values[2],
1586		   &ltr_config.ltr_cfg_values[3]) != 7) {
1587		return -EINVAL;
1588	}
1589
1590	mutex_lock(&mvm->mutex);
1591	ret = iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0, sizeof(ltr_config),
1592				   &ltr_config);
1593	mutex_unlock(&mvm->mutex);
1594
1595	if (ret)
1596		IWL_ERR(mvm, "failed to send ltr configuration cmd\n");
1597
1598	return ret ?: count;
1599}
1600
1601static ssize_t iwl_dbgfs_rfi_freq_table_write(struct iwl_mvm *mvm, char *buf,
1602					      size_t count, loff_t *ppos)
1603{
1604	int ret = 0;
1605	u16 op_id;
1606
1607	if (kstrtou16(buf, 10, &op_id))
1608		return -EINVAL;
1609
1610	/* value zero triggers re-sending the default table to the device */
1611	if (!op_id) {
1612		mutex_lock(&mvm->mutex);
1613		ret = iwl_rfi_send_config_cmd(mvm, NULL);
1614		mutex_unlock(&mvm->mutex);
1615	} else {
1616		ret = -EOPNOTSUPP; /* in the future a new table will be added */
1617	}
1618
1619	return ret ?: count;
1620}
1621
1622/* The size computation is as follows:
1623 * each number needs at most 3 characters, number of rows is the size of
1624 * the table; So, need 5 chars for the "freq: " part and each tuple afterwards
1625 * needs 6 characters for numbers and 5 for the punctuation around.
1626 */
1627#define IWL_RFI_BUF_SIZE (IWL_RFI_LUT_INSTALLED_SIZE *\
1628				(5 + IWL_RFI_LUT_ENTRY_CHANNELS_NUM * (6 + 5)))
1629
1630static ssize_t iwl_dbgfs_rfi_freq_table_read(struct file *file,
1631					     char __user *user_buf,
1632					     size_t count, loff_t *ppos)
1633{
1634	struct iwl_mvm *mvm = file->private_data;
1635	struct iwl_rfi_freq_table_resp_cmd *resp;
1636	u32 status;
1637	char buf[IWL_RFI_BUF_SIZE];
1638	int i, j, pos = 0;
1639
1640	resp = iwl_rfi_get_freq_table(mvm);
1641	if (IS_ERR(resp))
1642		return PTR_ERR(resp);
1643
1644	status = le32_to_cpu(resp->status);
1645	if (status != RFI_FREQ_TABLE_OK) {
1646		scnprintf(buf, IWL_RFI_BUF_SIZE, "status = %d\n", status);
1647		goto out;
1648	}
1649
1650	for (i = 0; i < ARRAY_SIZE(resp->table); i++) {
1651		pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos, "%d: ",
1652				 resp->table[i].freq);
1653
1654		for (j = 0; j < ARRAY_SIZE(resp->table[i].channels); j++)
1655			pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos,
1656					 "(%d, %d) ",
1657					 resp->table[i].channels[j],
1658					 resp->table[i].bands[j]);
1659		pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos, "\n");
1660	}
1661
1662out:
1663	kfree(resp);
1664	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1665}
1666
1667MVM_DEBUGFS_READ_WRITE_FILE_OPS(prph_reg, 64);
1668
1669/* Device wide debugfs entries */
1670MVM_DEBUGFS_READ_FILE_OPS(ctdp_budget);
1671MVM_DEBUGFS_WRITE_FILE_OPS(stop_ctdp, 8);
 
1672MVM_DEBUGFS_WRITE_FILE_OPS(force_ctkill, 8);
1673MVM_DEBUGFS_WRITE_FILE_OPS(tx_flush, 16);
1674MVM_DEBUGFS_WRITE_FILE_OPS(sta_drain, 8);
1675MVM_DEBUGFS_WRITE_FILE_OPS(send_echo_cmd, 8);
1676MVM_DEBUGFS_READ_WRITE_FILE_OPS(sram, 64);
1677MVM_DEBUGFS_READ_WRITE_FILE_OPS(set_nic_temperature, 64);
1678MVM_DEBUGFS_READ_FILE_OPS(nic_temp);
1679MVM_DEBUGFS_READ_FILE_OPS(stations);
1680MVM_DEBUGFS_READ_FILE_OPS(rs_data);
1681MVM_DEBUGFS_READ_FILE_OPS(bt_notif);
1682MVM_DEBUGFS_READ_FILE_OPS(bt_cmd);
1683MVM_DEBUGFS_READ_WRITE_FILE_OPS(disable_power_off, 64);
1684MVM_DEBUGFS_READ_FILE_OPS(fw_rx_stats);
1685MVM_DEBUGFS_READ_FILE_OPS(drv_rx_stats);
 
1686MVM_DEBUGFS_READ_FILE_OPS(fw_ver);
1687MVM_DEBUGFS_READ_FILE_OPS(phy_integration_ver);
 
1688MVM_DEBUGFS_WRITE_FILE_OPS(fw_restart, 10);
1689MVM_DEBUGFS_WRITE_FILE_OPS(fw_nmi, 10);
1690MVM_DEBUGFS_WRITE_FILE_OPS(bt_tx_prio, 10);
1691MVM_DEBUGFS_WRITE_FILE_OPS(bt_force_ant, 10);
1692MVM_DEBUGFS_READ_WRITE_FILE_OPS(scan_ant_rxchain, 8);
1693MVM_DEBUGFS_READ_WRITE_FILE_OPS(fw_dbg_conf, 8);
1694MVM_DEBUGFS_WRITE_FILE_OPS(fw_dbg_collect, 64);
 
1695MVM_DEBUGFS_WRITE_FILE_OPS(dbg_time_point, 64);
1696MVM_DEBUGFS_WRITE_FILE_OPS(indirection_tbl,
1697			   (IWL_RSS_INDIRECTION_TABLE_SIZE * 2));
1698MVM_DEBUGFS_WRITE_FILE_OPS(inject_packet, 512);
1699MVM_DEBUGFS_WRITE_FILE_OPS(inject_beacon_ie, 512);
1700MVM_DEBUGFS_WRITE_FILE_OPS(inject_beacon_ie_restore, 512);
1701
1702MVM_DEBUGFS_READ_FILE_OPS(uapsd_noagg_bssids);
1703
1704#ifdef CONFIG_ACPI
1705MVM_DEBUGFS_READ_FILE_OPS(sar_geo_profile);
 
1706#endif
1707
1708MVM_DEBUGFS_READ_WRITE_STA_FILE_OPS(amsdu_len, 16);
1709
1710MVM_DEBUGFS_READ_WRITE_FILE_OPS(he_sniffer_params, 32);
1711
1712MVM_DEBUGFS_WRITE_FILE_OPS(ltr_config, 512);
1713MVM_DEBUGFS_READ_WRITE_FILE_OPS(rfi_freq_table, 16);
1714
1715static ssize_t iwl_dbgfs_mem_read(struct file *file, char __user *user_buf,
1716				  size_t count, loff_t *ppos)
1717{
1718	struct iwl_mvm *mvm = file->private_data;
1719	struct iwl_dbg_mem_access_cmd cmd = {};
1720	struct iwl_dbg_mem_access_rsp *rsp;
1721	struct iwl_host_cmd hcmd = {
1722		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
1723		.data = { &cmd, },
1724		.len = { sizeof(cmd) },
1725	};
1726	size_t delta;
1727	ssize_t ret, len;
1728
1729	if (!iwl_mvm_firmware_running(mvm))
1730		return -EIO;
1731
1732	hcmd.id = WIDE_ID(DEBUG_GROUP, *ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR);
1733	cmd.op = cpu_to_le32(DEBUG_MEM_OP_READ);
1734
1735	/* Take care of alignment of both the position and the length */
1736	delta = *ppos & 0x3;
1737	cmd.addr = cpu_to_le32(*ppos - delta);
1738	cmd.len = cpu_to_le32(min(ALIGN(count + delta, 4) / 4,
1739				  (size_t)DEBUG_MEM_MAX_SIZE_DWORDS));
1740
1741	mutex_lock(&mvm->mutex);
1742	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1743	mutex_unlock(&mvm->mutex);
1744
1745	if (ret < 0)
1746		return ret;
1747
 
 
 
 
 
1748	rsp = (void *)hcmd.resp_pkt->data;
1749	if (le32_to_cpu(rsp->status) != DEBUG_MEM_STATUS_SUCCESS) {
1750		ret = -ENXIO;
1751		goto out;
1752	}
1753
1754	len = min((size_t)le32_to_cpu(rsp->len) << 2,
1755		  iwl_rx_packet_payload_len(hcmd.resp_pkt) - sizeof(*rsp));
1756	len = min(len - delta, count);
1757	if (len < 0) {
1758		ret = -EFAULT;
1759		goto out;
1760	}
1761
1762	ret = len - copy_to_user(user_buf, (u8 *)rsp->data + delta, len);
1763	*ppos += ret;
1764
1765out:
1766	iwl_free_resp(&hcmd);
1767	return ret;
1768}
1769
1770static ssize_t iwl_dbgfs_mem_write(struct file *file,
1771				   const char __user *user_buf, size_t count,
1772				   loff_t *ppos)
1773{
1774	struct iwl_mvm *mvm = file->private_data;
1775	struct iwl_dbg_mem_access_cmd *cmd;
1776	struct iwl_dbg_mem_access_rsp *rsp;
1777	struct iwl_host_cmd hcmd = {};
1778	size_t cmd_size;
1779	size_t data_size;
1780	u32 op, len;
1781	ssize_t ret;
1782
1783	if (!iwl_mvm_firmware_running(mvm))
1784		return -EIO;
1785
1786	hcmd.id = WIDE_ID(DEBUG_GROUP, *ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR);
1787
1788	if (*ppos & 0x3 || count < 4) {
1789		op = DEBUG_MEM_OP_WRITE_BYTES;
1790		len = min(count, (size_t)(4 - (*ppos & 0x3)));
1791		data_size = len;
1792	} else {
1793		op = DEBUG_MEM_OP_WRITE;
1794		len = min(count >> 2, (size_t)DEBUG_MEM_MAX_SIZE_DWORDS);
1795		data_size = len << 2;
1796	}
1797
1798	cmd_size = sizeof(*cmd) + ALIGN(data_size, 4);
1799	cmd = kzalloc(cmd_size, GFP_KERNEL);
1800	if (!cmd)
1801		return -ENOMEM;
1802
1803	cmd->op = cpu_to_le32(op);
1804	cmd->len = cpu_to_le32(len);
1805	cmd->addr = cpu_to_le32(*ppos);
1806	if (copy_from_user((void *)cmd->data, user_buf, data_size)) {
1807		kfree(cmd);
1808		return -EFAULT;
1809	}
1810
1811	hcmd.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
1812	hcmd.data[0] = (void *)cmd;
1813	hcmd.len[0] = cmd_size;
1814
1815	mutex_lock(&mvm->mutex);
1816	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1817	mutex_unlock(&mvm->mutex);
1818
1819	kfree(cmd);
1820
1821	if (ret < 0)
1822		return ret;
1823
 
 
 
 
 
1824	rsp = (void *)hcmd.resp_pkt->data;
1825	if (rsp->status != DEBUG_MEM_STATUS_SUCCESS) {
1826		ret = -ENXIO;
1827		goto out;
1828	}
1829
1830	ret = data_size;
1831	*ppos += ret;
1832
1833out:
1834	iwl_free_resp(&hcmd);
1835	return ret;
1836}
1837
1838static const struct file_operations iwl_dbgfs_mem_ops = {
1839	.read = iwl_dbgfs_mem_read,
1840	.write = iwl_dbgfs_mem_write,
1841	.open = simple_open,
1842	.llseek = default_llseek,
1843};
1844
1845void iwl_mvm_sta_add_debugfs(struct ieee80211_hw *hw,
1846			     struct ieee80211_vif *vif,
1847			     struct ieee80211_sta *sta,
1848			     struct dentry *dir)
1849{
1850	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1851
1852	if (iwl_mvm_has_tlc_offload(mvm)) {
1853		MVM_DEBUGFS_ADD_STA_FILE(rs_data, dir, 0400);
1854	}
1855	MVM_DEBUGFS_ADD_STA_FILE(amsdu_len, dir, 0600);
 
1856}
1857
1858void iwl_mvm_dbgfs_register(struct iwl_mvm *mvm)
1859{
1860	struct dentry *bcast_dir __maybe_unused;
1861
1862	spin_lock_init(&mvm->drv_stats_lock);
1863
1864	MVM_DEBUGFS_ADD_FILE(tx_flush, mvm->debugfs_dir, 0200);
1865	MVM_DEBUGFS_ADD_FILE(sta_drain, mvm->debugfs_dir, 0200);
1866	MVM_DEBUGFS_ADD_FILE(sram, mvm->debugfs_dir, 0600);
1867	MVM_DEBUGFS_ADD_FILE(set_nic_temperature, mvm->debugfs_dir, 0600);
1868	MVM_DEBUGFS_ADD_FILE(nic_temp, mvm->debugfs_dir, 0400);
1869	MVM_DEBUGFS_ADD_FILE(ctdp_budget, mvm->debugfs_dir, 0400);
1870	MVM_DEBUGFS_ADD_FILE(stop_ctdp, mvm->debugfs_dir, 0200);
 
1871	MVM_DEBUGFS_ADD_FILE(force_ctkill, mvm->debugfs_dir, 0200);
1872	MVM_DEBUGFS_ADD_FILE(stations, mvm->debugfs_dir, 0400);
1873	MVM_DEBUGFS_ADD_FILE(bt_notif, mvm->debugfs_dir, 0400);
1874	MVM_DEBUGFS_ADD_FILE(bt_cmd, mvm->debugfs_dir, 0400);
1875	MVM_DEBUGFS_ADD_FILE(disable_power_off, mvm->debugfs_dir, 0600);
1876	MVM_DEBUGFS_ADD_FILE(fw_ver, mvm->debugfs_dir, 0400);
1877	MVM_DEBUGFS_ADD_FILE(fw_rx_stats, mvm->debugfs_dir, 0400);
1878	MVM_DEBUGFS_ADD_FILE(drv_rx_stats, mvm->debugfs_dir, 0400);
 
1879	MVM_DEBUGFS_ADD_FILE(fw_restart, mvm->debugfs_dir, 0200);
1880	MVM_DEBUGFS_ADD_FILE(fw_nmi, mvm->debugfs_dir, 0200);
1881	MVM_DEBUGFS_ADD_FILE(bt_tx_prio, mvm->debugfs_dir, 0200);
1882	MVM_DEBUGFS_ADD_FILE(bt_force_ant, mvm->debugfs_dir, 0200);
1883	MVM_DEBUGFS_ADD_FILE(scan_ant_rxchain, mvm->debugfs_dir, 0600);
1884	MVM_DEBUGFS_ADD_FILE(prph_reg, mvm->debugfs_dir, 0600);
1885	MVM_DEBUGFS_ADD_FILE(fw_dbg_conf, mvm->debugfs_dir, 0600);
1886	MVM_DEBUGFS_ADD_FILE(fw_dbg_collect, mvm->debugfs_dir, 0200);
 
1887	MVM_DEBUGFS_ADD_FILE(dbg_time_point, mvm->debugfs_dir, 0200);
1888	MVM_DEBUGFS_ADD_FILE(send_echo_cmd, mvm->debugfs_dir, 0200);
1889	MVM_DEBUGFS_ADD_FILE(indirection_tbl, mvm->debugfs_dir, 0200);
1890	MVM_DEBUGFS_ADD_FILE(inject_packet, mvm->debugfs_dir, 0200);
1891	MVM_DEBUGFS_ADD_FILE(inject_beacon_ie, mvm->debugfs_dir, 0200);
1892	MVM_DEBUGFS_ADD_FILE(inject_beacon_ie_restore, mvm->debugfs_dir, 0200);
1893	MVM_DEBUGFS_ADD_FILE(rfi_freq_table, mvm->debugfs_dir, 0600);
1894
1895	if (mvm->fw->phy_integration_ver)
1896		MVM_DEBUGFS_ADD_FILE(phy_integration_ver, mvm->debugfs_dir, 0400);
 
1897#ifdef CONFIG_ACPI
1898	MVM_DEBUGFS_ADD_FILE(sar_geo_profile, mvm->debugfs_dir, 0400);
 
1899#endif
1900	MVM_DEBUGFS_ADD_FILE(he_sniffer_params, mvm->debugfs_dir, 0600);
1901
1902	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_SET_LTR_GEN2))
1903		MVM_DEBUGFS_ADD_FILE(ltr_config, mvm->debugfs_dir, 0200);
1904
1905	debugfs_create_bool("enable_scan_iteration_notif", 0600,
1906			    mvm->debugfs_dir, &mvm->scan_iter_notif_enabled);
1907	debugfs_create_bool("drop_bcn_ap_mode", 0600, mvm->debugfs_dir,
1908			    &mvm->drop_bcn_ap_mode);
1909
1910	MVM_DEBUGFS_ADD_FILE(uapsd_noagg_bssids, mvm->debugfs_dir, S_IRUSR);
1911
1912#ifdef CONFIG_PM_SLEEP
1913	MVM_DEBUGFS_ADD_FILE(d3_test, mvm->debugfs_dir, 0400);
1914	debugfs_create_bool("d3_wake_sysassert", 0600, mvm->debugfs_dir,
1915			    &mvm->d3_wake_sysassert);
1916	debugfs_create_u32("last_netdetect_scans", 0400, mvm->debugfs_dir,
1917			   &mvm->last_netdetect_scans);
1918#endif
1919
1920	debugfs_create_u8("ps_disabled", 0400, mvm->debugfs_dir,
1921			  &mvm->ps_disabled);
1922	debugfs_create_blob("nvm_hw", 0400, mvm->debugfs_dir,
1923			    &mvm->nvm_hw_blob);
1924	debugfs_create_blob("nvm_sw", 0400, mvm->debugfs_dir,
1925			    &mvm->nvm_sw_blob);
1926	debugfs_create_blob("nvm_calib", 0400, mvm->debugfs_dir,
1927			    &mvm->nvm_calib_blob);
1928	debugfs_create_blob("nvm_prod", 0400, mvm->debugfs_dir,
1929			    &mvm->nvm_prod_blob);
1930	debugfs_create_blob("nvm_phy_sku", 0400, mvm->debugfs_dir,
1931			    &mvm->nvm_phy_sku_blob);
1932	debugfs_create_blob("nvm_reg", S_IRUSR,
1933			    mvm->debugfs_dir, &mvm->nvm_reg_blob);
1934
1935	debugfs_create_file("mem", 0600, mvm->debugfs_dir, mvm,
1936			    &iwl_dbgfs_mem_ops);
1937
1938	/*
1939	 * Create a symlink with mac80211. It will be removed when mac80211
1940	 * exists (before the opmode exists which removes the target.)
1941	 */
1942	if (!IS_ERR(mvm->debugfs_dir)) {
1943		char buf[100];
1944
1945		snprintf(buf, 100, "../../%pd2", mvm->debugfs_dir->d_parent);
1946		debugfs_create_symlink("iwlwifi", mvm->hw->wiphy->debugfsdir,
1947				       buf);
1948	}
1949}