From: John W. Linville <linville@tuxdriver.com>
Date: Fri, 29 Jun 2012 16:07:37 +0000 (-0400)
Subject: Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless
X-Git-Tag: firefly_0821_release~3680^2~2381^2~338^2^2
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=42fb0b0278e6b9a44bee8adec051de5f43e10b2b;p=firefly-linux-kernel-4.4.55.git

Merge branch 'master' of git://git./linux/kernel/git/linville/wireless
---

42fb0b0278e6b9a44bee8adec051de5f43e10b2b
diff --cc drivers/net/wireless/iwlwifi/dvm/debugfs.c
index 8a2d9e643b14,000000000000..b0eff1c340c7
mode 100644,000000..100644
--- a/drivers/net/wireless/iwlwifi/dvm/debugfs.c
+++ b/drivers/net/wireless/iwlwifi/dvm/debugfs.c
@@@ -1,2426 -1,0 +1,2432 @@@
 +/******************************************************************************
 + *
 + * GPL LICENSE SUMMARY
 + *
 + * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
 + *
 + * This program is free software; you can redistribute it and/or modify
 + * it under the terms of version 2 of the GNU General Public License as
 + * published by the Free Software Foundation.
 + *
 + * This program is distributed in the hope that it will be useful, but
 + * WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 + * General Public License for more details.
 + *
 + * You should have received a copy of the GNU General Public License
 + * along with this program; if not, write to the Free Software
 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
 + * USA
 + *
 + * The full GNU General Public License is included in this distribution
 + * in the file called LICENSE.GPL.
 + *
 + * Contact Information:
 + *  Intel Linux Wireless <ilw@linux.intel.com>
 + * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 + *****************************************************************************/
 +
 +#include <linux/slab.h>
 +#include <linux/kernel.h>
 +#include <linux/module.h>
 +#include <linux/debugfs.h>
 +#include <linux/ieee80211.h>
 +#include <net/mac80211.h>
 +#include "iwl-debug.h"
 +#include "iwl-io.h"
 +#include "dev.h"
 +#include "agn.h"
 +
 +/* create and remove of files */
 +#define DEBUGFS_ADD_FILE(name, parent, mode) do {			\
 +	if (!debugfs_create_file(#name, mode, parent, priv,		\
 +				 &iwl_dbgfs_##name##_ops))		\
 +		goto err;						\
 +} while (0)
 +
 +#define DEBUGFS_ADD_BOOL(name, parent, ptr) do {			\
 +	struct dentry *__tmp;						\
 +	__tmp = debugfs_create_bool(#name, S_IWUSR | S_IRUSR,		\
 +				    parent, ptr);			\
 +	if (IS_ERR(__tmp) || !__tmp)					\
 +		goto err;						\
 +} while (0)
 +
 +#define DEBUGFS_ADD_X32(name, parent, ptr) do {				\
 +	struct dentry *__tmp;						\
 +	__tmp = debugfs_create_x32(#name, S_IWUSR | S_IRUSR,		\
 +				   parent, ptr);			\
 +	if (IS_ERR(__tmp) || !__tmp)					\
 +		goto err;						\
 +} while (0)
 +
 +#define DEBUGFS_ADD_U32(name, parent, ptr, mode) do {			\
 +	struct dentry *__tmp;						\
 +	__tmp = debugfs_create_u32(#name, mode,				\
 +				   parent, ptr);			\
 +	if (IS_ERR(__tmp) || !__tmp)					\
 +		goto err;						\
 +} while (0)
 +
 +/* file operation */
 +#define DEBUGFS_READ_FUNC(name)                                         \
 +static ssize_t iwl_dbgfs_##name##_read(struct file *file,               \
 +					char __user *user_buf,          \
 +					size_t count, loff_t *ppos);
 +
 +#define DEBUGFS_WRITE_FUNC(name)                                        \
 +static ssize_t iwl_dbgfs_##name##_write(struct file *file,              \
 +					const char __user *user_buf,    \
 +					size_t count, loff_t *ppos);
 +
 +
 +#define DEBUGFS_READ_FILE_OPS(name)                                     \
 +	DEBUGFS_READ_FUNC(name);                                        \
 +static const struct file_operations iwl_dbgfs_##name##_ops = {          \
 +	.read = iwl_dbgfs_##name##_read,                       		\
 +	.open = simple_open,						\
 +	.llseek = generic_file_llseek,					\
 +};
 +
 +#define DEBUGFS_WRITE_FILE_OPS(name)                                    \
 +	DEBUGFS_WRITE_FUNC(name);                                       \
 +static const struct file_operations iwl_dbgfs_##name##_ops = {          \
 +	.write = iwl_dbgfs_##name##_write,                              \
 +	.open = simple_open,						\
 +	.llseek = generic_file_llseek,					\
 +};
 +
 +
 +#define DEBUGFS_READ_WRITE_FILE_OPS(name)                               \
 +	DEBUGFS_READ_FUNC(name);                                        \
 +	DEBUGFS_WRITE_FUNC(name);                                       \
 +static const struct file_operations iwl_dbgfs_##name##_ops = {          \
 +	.write = iwl_dbgfs_##name##_write,                              \
 +	.read = iwl_dbgfs_##name##_read,                                \
 +	.open = simple_open,						\
 +	.llseek = generic_file_llseek,					\
 +};
 +
 +static ssize_t iwl_dbgfs_sram_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	u32 val = 0;
 +	char *buf;
 +	ssize_t ret;
 +	int i = 0;
 +	bool device_format = false;
 +	int offset = 0;
 +	int len = 0;
 +	int pos = 0;
 +	int sram;
 +	struct iwl_priv *priv = file->private_data;
 +	const struct fw_img *img;
 +	size_t bufsz;
 +
 +	/* default is to dump the entire data segment */
 +	if (!priv->dbgfs_sram_offset && !priv->dbgfs_sram_len) {
 +		priv->dbgfs_sram_offset = 0x800000;
 +		if (!priv->ucode_loaded)
 +			return -EINVAL;
 +		img = &priv->fw->img[priv->cur_ucode];
 +		priv->dbgfs_sram_len = img->sec[IWL_UCODE_SECTION_DATA].len;
 +	}
 +	len = priv->dbgfs_sram_len;
 +
 +	if (len == -4) {
 +		device_format = true;
 +		len = 4;
 +	}
 +
 +	bufsz =  50 + len * 4;
 +	buf = kmalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "sram_len: 0x%x\n",
 +			 len);
 +	pos += scnprintf(buf + pos, bufsz - pos, "sram_offset: 0x%x\n",
 +			priv->dbgfs_sram_offset);
 +
 +	/* adjust sram address since reads are only on even u32 boundaries */
 +	offset = priv->dbgfs_sram_offset & 0x3;
 +	sram = priv->dbgfs_sram_offset & ~0x3;
 +
 +	/* read the first u32 from sram */
 +	val = iwl_read_targ_mem(priv->trans, sram);
 +
 +	for (; len; len--) {
 +		/* put the address at the start of every line */
 +		if (i == 0)
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +				"%08X: ", sram + offset);
 +
 +		if (device_format)
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +				"%02x", (val >> (8 * (3 - offset))) & 0xff);
 +		else
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +				"%02x ", (val >> (8 * offset)) & 0xff);
 +
 +		/* if all bytes processed, read the next u32 from sram */
 +		if (++offset == 4) {
 +			sram += 4;
 +			offset = 0;
 +			val = iwl_read_targ_mem(priv->trans, sram);
 +		}
 +
 +		/* put in extra spaces and split lines for human readability */
 +		if (++i == 16) {
 +			i = 0;
 +			pos += scnprintf(buf + pos, bufsz - pos, "\n");
 +		} else if (!(i & 7)) {
 +			pos += scnprintf(buf + pos, bufsz - pos, "   ");
 +		} else if (!(i & 3)) {
 +			pos += scnprintf(buf + pos, bufsz - pos, " ");
 +		}
 +	}
 +	if (i)
 +		pos += scnprintf(buf + pos, bufsz - pos, "\n");
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_sram_write(struct file *file,
 +					const char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[64];
 +	int buf_size;
 +	u32 offset, len;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +
 +	if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
 +		priv->dbgfs_sram_offset = offset;
 +		priv->dbgfs_sram_len = len;
 +	} else if (sscanf(buf, "%x", &offset) == 1) {
 +		priv->dbgfs_sram_offset = offset;
 +		priv->dbgfs_sram_len = -4;
 +	} else {
 +		priv->dbgfs_sram_offset = 0;
 +		priv->dbgfs_sram_len = 0;
 +	}
 +
 +	return count;
 +}
 +
 +static ssize_t iwl_dbgfs_wowlan_sram_read(struct file *file,
 +					  char __user *user_buf,
 +					  size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	const struct fw_img *img = &priv->fw->img[IWL_UCODE_WOWLAN];
 +
 +	if (!priv->wowlan_sram)
 +		return -ENODATA;
 +
 +	return simple_read_from_buffer(user_buf, count, ppos,
 +				       priv->wowlan_sram,
 +				       img->sec[IWL_UCODE_SECTION_DATA].len);
 +}
 +static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	struct iwl_station_entry *station;
 +	struct iwl_tid_data *tid_data;
 +	char *buf;
 +	int i, j, pos = 0;
 +	ssize_t ret;
 +	/* Add 30 for initial string */
 +	const size_t bufsz = 30 + sizeof(char) * 500 * (priv->num_stations);
 +
 +	buf = kmalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "num of stations: %d\n\n",
 +			priv->num_stations);
 +
 +	for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
 +		station = &priv->stations[i];
 +		if (!station->used)
 +			continue;
 +		pos += scnprintf(buf + pos, bufsz - pos,
 +				 "station %d - addr: %pM, flags: %#x\n",
 +				 i, station->sta.sta.addr,
 +				 station->sta.station_flags_msk);
 +		pos += scnprintf(buf + pos, bufsz - pos,
 +				"TID seqno  next_rclmd "
 +				"rate_n_flags state txq\n");
 +
 +		for (j = 0; j < IWL_MAX_TID_COUNT; j++) {
 +			tid_data = &priv->tid_data[i][j];
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +				"%d:  0x%.4x 0x%.4x     0x%.8x   "
 +				"%d     %.2d",
 +				j, tid_data->seq_number,
 +				tid_data->next_reclaimed,
 +				tid_data->agg.rate_n_flags,
 +				tid_data->agg.state,
 +				tid_data->agg.txq_id);
 +
 +			if (tid_data->agg.wait_for_ba)
 +				pos += scnprintf(buf + pos, bufsz - pos,
 +						 " - waitforba");
 +			pos += scnprintf(buf + pos, bufsz - pos, "\n");
 +		}
 +
 +		pos += scnprintf(buf + pos, bufsz - pos, "\n");
 +	}
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_nvm_read(struct file *file,
 +				       char __user *user_buf,
 +				       size_t count,
 +				       loff_t *ppos)
 +{
 +	ssize_t ret;
 +	struct iwl_priv *priv = file->private_data;
 +	int pos = 0, ofs = 0, buf_size = 0;
 +	const u8 *ptr;
 +	char *buf;
 +	u16 eeprom_ver;
 +	size_t eeprom_len = priv->eeprom_blob_size;
 +	buf_size = 4 * eeprom_len + 256;
 +
 +	if (eeprom_len % 16)
 +		return -ENODATA;
 +
 +	ptr = priv->eeprom_blob;
 +	if (!ptr)
 +		return -ENOMEM;
 +
 +	/* 4 characters for byte 0xYY */
 +	buf = kzalloc(buf_size, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	eeprom_ver = priv->eeprom_data->eeprom_version;
 +	pos += scnprintf(buf + pos, buf_size - pos,
 +			 "NVM version: 0x%x\n", eeprom_ver);
 +	for (ofs = 0 ; ofs < eeprom_len ; ofs += 16) {
 +		pos += scnprintf(buf + pos, buf_size - pos, "0x%.4x ", ofs);
 +		hex_dump_to_buffer(ptr + ofs, 16 , 16, 2, buf + pos,
 +				   buf_size - pos, 0);
 +		pos += strlen(buf + pos);
 +		if (buf_size - pos > 0)
 +			buf[pos++] = '\n';
 +	}
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_channels_read(struct file *file, char __user *user_buf,
 +				       size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	struct ieee80211_channel *channels = NULL;
 +	const struct ieee80211_supported_band *supp_band = NULL;
 +	int pos = 0, i, bufsz = PAGE_SIZE;
 +	char *buf;
 +	ssize_t ret;
 +
 +	buf = kzalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_2GHZ);
 +	if (supp_band) {
 +		channels = supp_band->channels;
 +
 +		pos += scnprintf(buf + pos, bufsz - pos,
 +				"Displaying %d channels in 2.4GHz band 802.11bg):\n",
 +				supp_band->n_channels);
 +
 +		for (i = 0; i < supp_band->n_channels; i++)
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +					"%d: %ddBm: BSS%s%s, %s.\n",
 +					channels[i].hw_value,
 +					channels[i].max_power,
 +					channels[i].flags & IEEE80211_CHAN_RADAR ?
 +					" (IEEE 802.11h required)" : "",
 +					((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
 +					|| (channels[i].flags &
 +					IEEE80211_CHAN_RADAR)) ? "" :
 +					", IBSS",
 +					channels[i].flags &
 +					IEEE80211_CHAN_PASSIVE_SCAN ?
 +					"passive only" : "active/passive");
 +	}
 +	supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_5GHZ);
 +	if (supp_band) {
 +		channels = supp_band->channels;
 +
 +		pos += scnprintf(buf + pos, bufsz - pos,
 +				"Displaying %d channels in 5.2GHz band (802.11a)\n",
 +				supp_band->n_channels);
 +
 +		for (i = 0; i < supp_band->n_channels; i++)
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +					"%d: %ddBm: BSS%s%s, %s.\n",
 +					channels[i].hw_value,
 +					channels[i].max_power,
 +					channels[i].flags & IEEE80211_CHAN_RADAR ?
 +					" (IEEE 802.11h required)" : "",
 +					((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
 +					|| (channels[i].flags &
 +					IEEE80211_CHAN_RADAR)) ? "" :
 +					", IBSS",
 +					channels[i].flags &
 +					IEEE80211_CHAN_PASSIVE_SCAN ?
 +					"passive only" : "active/passive");
 +	}
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_status_read(struct file *file,
 +						char __user *user_buf,
 +						size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[512];
 +	int pos = 0;
 +	const size_t bufsz = sizeof(buf);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_RF_KILL_HW:\t %d\n",
 +		test_bit(STATUS_RF_KILL_HW, &priv->status));
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_CT_KILL:\t\t %d\n",
 +		test_bit(STATUS_CT_KILL, &priv->status));
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_ALIVE:\t\t %d\n",
 +		test_bit(STATUS_ALIVE, &priv->status));
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_READY:\t\t %d\n",
 +		test_bit(STATUS_READY, &priv->status));
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_EXIT_PENDING:\t %d\n",
 +		test_bit(STATUS_EXIT_PENDING, &priv->status));
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_STATISTICS:\t %d\n",
 +		test_bit(STATUS_STATISTICS, &priv->status));
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCANNING:\t %d\n",
 +		test_bit(STATUS_SCANNING, &priv->status));
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCAN_ABORTING:\t %d\n",
 +		test_bit(STATUS_SCAN_ABORTING, &priv->status));
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCAN_HW:\t\t %d\n",
 +		test_bit(STATUS_SCAN_HW, &priv->status));
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_POWER_PMI:\t %d\n",
 +		test_bit(STATUS_POWER_PMI, &priv->status));
 +	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_FW_ERROR:\t %d\n",
 +		test_bit(STATUS_FW_ERROR, &priv->status));
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_rx_handlers_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +
 +	int pos = 0;
 +	int cnt = 0;
 +	char *buf;
 +	int bufsz = 24 * 64; /* 24 items * 64 char per item */
 +	ssize_t ret;
 +
 +	buf = kzalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	for (cnt = 0; cnt < REPLY_MAX; cnt++) {
 +		if (priv->rx_handlers_stats[cnt] > 0)
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +				"\tRx handler[%36s]:\t\t %u\n",
 +				iwl_dvm_get_cmd_string(cnt),
 +				priv->rx_handlers_stats[cnt]);
 +	}
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_rx_handlers_write(struct file *file,
 +					 const char __user *user_buf,
 +					 size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +
 +	char buf[8];
 +	int buf_size;
 +	u32 reset_flag;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +	if (sscanf(buf, "%x", &reset_flag) != 1)
 +		return -EFAULT;
 +	if (reset_flag == 0)
 +		memset(&priv->rx_handlers_stats[0], 0,
 +			sizeof(priv->rx_handlers_stats));
 +
 +	return count;
 +}
 +
 +static ssize_t iwl_dbgfs_qos_read(struct file *file, char __user *user_buf,
 +				       size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	struct iwl_rxon_context *ctx;
 +	int pos = 0, i;
 +	char buf[256 * NUM_IWL_RXON_CTX];
 +	const size_t bufsz = sizeof(buf);
 +
 +	for_each_context(priv, ctx) {
 +		pos += scnprintf(buf + pos, bufsz - pos, "context %d:\n",
 +				 ctx->ctxid);
 +		for (i = 0; i < AC_NUM; i++) {
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +				"\tcw_min\tcw_max\taifsn\ttxop\n");
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +				"AC[%d]\t%u\t%u\t%u\t%u\n", i,
 +				ctx->qos_data.def_qos_parm.ac[i].cw_min,
 +				ctx->qos_data.def_qos_parm.ac[i].cw_max,
 +				ctx->qos_data.def_qos_parm.ac[i].aifsn,
 +				ctx->qos_data.def_qos_parm.ac[i].edca_txop);
 +		}
 +		pos += scnprintf(buf + pos, bufsz - pos, "\n");
 +	}
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_thermal_throttling_read(struct file *file,
 +				char __user *user_buf,
 +				size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
 +	struct iwl_tt_restriction *restriction;
 +	char buf[100];
 +	int pos = 0;
 +	const size_t bufsz = sizeof(buf);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"Thermal Throttling Mode: %s\n",
 +			tt->advanced_tt ? "Advance" : "Legacy");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"Thermal Throttling State: %d\n",
 +			tt->state);
 +	if (tt->advanced_tt) {
 +		restriction = tt->restriction + tt->state;
 +		pos += scnprintf(buf + pos, bufsz - pos,
 +				"Tx mode: %d\n",
 +				restriction->tx_stream);
 +		pos += scnprintf(buf + pos, bufsz - pos,
 +				"Rx mode: %d\n",
 +				restriction->rx_stream);
 +		pos += scnprintf(buf + pos, bufsz - pos,
 +				"HT mode: %d\n",
 +				restriction->is_ht);
 +	}
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_disable_ht40_write(struct file *file,
 +					 const char __user *user_buf,
 +					 size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	int buf_size;
 +	int ht40;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +	if (sscanf(buf, "%d", &ht40) != 1)
 +		return -EFAULT;
 +	if (!iwl_is_any_associated(priv))
 +		priv->disable_ht40 = ht40 ? true : false;
 +	else
 +		return -EINVAL;
 +
 +	return count;
 +}
 +
 +static ssize_t iwl_dbgfs_disable_ht40_read(struct file *file,
 +					 char __user *user_buf,
 +					 size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[100];
 +	int pos = 0;
 +	const size_t bufsz = sizeof(buf);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"11n 40MHz Mode: %s\n",
 +			priv->disable_ht40 ? "Disabled" : "Enabled");
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_temperature_read(struct file *file,
 +					 char __user *user_buf,
 +					 size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	int pos = 0;
 +	const size_t bufsz = sizeof(buf);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "%d\n", priv->temperature);
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +
 +static ssize_t iwl_dbgfs_sleep_level_override_write(struct file *file,
 +						    const char __user *user_buf,
 +						    size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	int buf_size;
 +	int value;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +
 +	if (sscanf(buf, "%d", &value) != 1)
 +		return -EINVAL;
 +
 +	/*
 +	 * Our users expect 0 to be "CAM", but 0 isn't actually
 +	 * valid here. However, let's not confuse them and present
 +	 * IWL_POWER_INDEX_1 as "1", not "0".
 +	 */
 +	if (value == 0)
 +		return -EINVAL;
 +	else if (value > 0)
 +		value -= 1;
 +
 +	if (value != -1 && (value < 0 || value >= IWL_POWER_NUM))
 +		return -EINVAL;
 +
 +	if (!iwl_is_ready_rf(priv))
 +		return -EAGAIN;
 +
 +	priv->power_data.debug_sleep_level_override = value;
 +
 +	mutex_lock(&priv->mutex);
 +	iwl_power_update_mode(priv, true);
 +	mutex_unlock(&priv->mutex);
 +
 +	return count;
 +}
 +
 +static ssize_t iwl_dbgfs_sleep_level_override_read(struct file *file,
 +						   char __user *user_buf,
 +						   size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[10];
 +	int pos, value;
 +	const size_t bufsz = sizeof(buf);
 +
 +	/* see the write function */
 +	value = priv->power_data.debug_sleep_level_override;
 +	if (value >= 0)
 +		value += 1;
 +
 +	pos = scnprintf(buf, bufsz, "%d\n", value);
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_current_sleep_command_read(struct file *file,
 +						    char __user *user_buf,
 +						    size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[200];
 +	int pos = 0, i;
 +	const size_t bufsz = sizeof(buf);
 +	struct iwl_powertable_cmd *cmd = &priv->power_data.sleep_cmd;
 +
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "flags: %#.2x\n", le16_to_cpu(cmd->flags));
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "RX/TX timeout: %d/%d usec\n",
 +			 le32_to_cpu(cmd->rx_data_timeout),
 +			 le32_to_cpu(cmd->tx_data_timeout));
 +	for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
 +		pos += scnprintf(buf + pos, bufsz - pos,
 +				 "sleep_interval[%d]: %d\n", i,
 +				 le32_to_cpu(cmd->sleep_interval[i]));
 +
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +DEBUGFS_READ_WRITE_FILE_OPS(sram);
 +DEBUGFS_READ_FILE_OPS(wowlan_sram);
 +DEBUGFS_READ_FILE_OPS(nvm);
 +DEBUGFS_READ_FILE_OPS(stations);
 +DEBUGFS_READ_FILE_OPS(channels);
 +DEBUGFS_READ_FILE_OPS(status);
 +DEBUGFS_READ_WRITE_FILE_OPS(rx_handlers);
 +DEBUGFS_READ_FILE_OPS(qos);
 +DEBUGFS_READ_FILE_OPS(thermal_throttling);
 +DEBUGFS_READ_WRITE_FILE_OPS(disable_ht40);
 +DEBUGFS_READ_FILE_OPS(temperature);
 +DEBUGFS_READ_WRITE_FILE_OPS(sleep_level_override);
 +DEBUGFS_READ_FILE_OPS(current_sleep_command);
 +
 +static const char *fmt_value = "  %-30s %10u\n";
 +static const char *fmt_hex   = "  %-30s       0x%02X\n";
 +static const char *fmt_table = "  %-30s %10u  %10u  %10u  %10u\n";
 +static const char *fmt_header =
 +	"%-32s    current  cumulative       delta         max\n";
 +
 +static int iwl_statistics_flag(struct iwl_priv *priv, char *buf, int bufsz)
 +{
 +	int p = 0;
 +	u32 flag;
 +
 +	lockdep_assert_held(&priv->statistics.lock);
 +
 +	flag = le32_to_cpu(priv->statistics.flag);
 +
 +	p += scnprintf(buf + p, bufsz - p, "Statistics Flag(0x%X):\n", flag);
 +	if (flag & UCODE_STATISTICS_CLEAR_MSK)
 +		p += scnprintf(buf + p, bufsz - p,
 +		"\tStatistics have been cleared\n");
 +	p += scnprintf(buf + p, bufsz - p, "\tOperational Frequency: %s\n",
 +		(flag & UCODE_STATISTICS_FREQUENCY_MSK)
 +		? "2.4 GHz" : "5.2 GHz");
 +	p += scnprintf(buf + p, bufsz - p, "\tTGj Narrow Band: %s\n",
 +		(flag & UCODE_STATISTICS_NARROW_BAND_MSK)
 +		 ? "enabled" : "disabled");
 +
 +	return p;
 +}
 +
 +static ssize_t iwl_dbgfs_ucode_rx_stats_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	int pos = 0;
 +	char *buf;
 +	int bufsz = sizeof(struct statistics_rx_phy) * 40 +
 +		    sizeof(struct statistics_rx_non_phy) * 40 +
 +		    sizeof(struct statistics_rx_ht_phy) * 40 + 400;
 +	ssize_t ret;
 +	struct statistics_rx_phy *ofdm, *accum_ofdm, *delta_ofdm, *max_ofdm;
 +	struct statistics_rx_phy *cck, *accum_cck, *delta_cck, *max_cck;
 +	struct statistics_rx_non_phy *general, *accum_general;
 +	struct statistics_rx_non_phy *delta_general, *max_general;
 +	struct statistics_rx_ht_phy *ht, *accum_ht, *delta_ht, *max_ht;
 +
 +	if (!iwl_is_alive(priv))
 +		return -EAGAIN;
 +
 +	buf = kzalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	/*
 +	 * the statistic information display here is based on
 +	 * the last statistics notification from uCode
 +	 * might not reflect the current uCode activity
 +	 */
 +	spin_lock_bh(&priv->statistics.lock);
 +	ofdm = &priv->statistics.rx_ofdm;
 +	cck = &priv->statistics.rx_cck;
 +	general = &priv->statistics.rx_non_phy;
 +	ht = &priv->statistics.rx_ofdm_ht;
 +	accum_ofdm = &priv->accum_stats.rx_ofdm;
 +	accum_cck = &priv->accum_stats.rx_cck;
 +	accum_general = &priv->accum_stats.rx_non_phy;
 +	accum_ht = &priv->accum_stats.rx_ofdm_ht;
 +	delta_ofdm = &priv->delta_stats.rx_ofdm;
 +	delta_cck = &priv->delta_stats.rx_cck;
 +	delta_general = &priv->delta_stats.rx_non_phy;
 +	delta_ht = &priv->delta_stats.rx_ofdm_ht;
 +	max_ofdm = &priv->max_delta_stats.rx_ofdm;
 +	max_cck = &priv->max_delta_stats.rx_cck;
 +	max_general = &priv->max_delta_stats.rx_non_phy;
 +	max_ht = &priv->max_delta_stats.rx_ofdm_ht;
 +
 +	pos += iwl_statistics_flag(priv, buf, bufsz);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_header, "Statistics_Rx - OFDM:");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "ina_cnt:",
 +			 le32_to_cpu(ofdm->ina_cnt),
 +			 accum_ofdm->ina_cnt,
 +			 delta_ofdm->ina_cnt, max_ofdm->ina_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "fina_cnt:",
 +			 le32_to_cpu(ofdm->fina_cnt), accum_ofdm->fina_cnt,
 +			 delta_ofdm->fina_cnt, max_ofdm->fina_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "plcp_err:",
 +			 le32_to_cpu(ofdm->plcp_err), accum_ofdm->plcp_err,
 +			 delta_ofdm->plcp_err, max_ofdm->plcp_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "crc32_err:",
 +			 le32_to_cpu(ofdm->crc32_err), accum_ofdm->crc32_err,
 +			 delta_ofdm->crc32_err, max_ofdm->crc32_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "overrun_err:",
 +			 le32_to_cpu(ofdm->overrun_err),
 +			 accum_ofdm->overrun_err, delta_ofdm->overrun_err,
 +			 max_ofdm->overrun_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "early_overrun_err:",
 +			 le32_to_cpu(ofdm->early_overrun_err),
 +			 accum_ofdm->early_overrun_err,
 +			 delta_ofdm->early_overrun_err,
 +			 max_ofdm->early_overrun_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "crc32_good:",
 +			 le32_to_cpu(ofdm->crc32_good),
 +			 accum_ofdm->crc32_good, delta_ofdm->crc32_good,
 +			 max_ofdm->crc32_good);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "false_alarm_cnt:",
 +			 le32_to_cpu(ofdm->false_alarm_cnt),
 +			 accum_ofdm->false_alarm_cnt,
 +			 delta_ofdm->false_alarm_cnt,
 +			 max_ofdm->false_alarm_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "fina_sync_err_cnt:",
 +			 le32_to_cpu(ofdm->fina_sync_err_cnt),
 +			 accum_ofdm->fina_sync_err_cnt,
 +			 delta_ofdm->fina_sync_err_cnt,
 +			 max_ofdm->fina_sync_err_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "sfd_timeout:",
 +			 le32_to_cpu(ofdm->sfd_timeout),
 +			 accum_ofdm->sfd_timeout, delta_ofdm->sfd_timeout,
 +			 max_ofdm->sfd_timeout);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "fina_timeout:",
 +			 le32_to_cpu(ofdm->fina_timeout),
 +			 accum_ofdm->fina_timeout, delta_ofdm->fina_timeout,
 +			 max_ofdm->fina_timeout);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "unresponded_rts:",
 +			 le32_to_cpu(ofdm->unresponded_rts),
 +			 accum_ofdm->unresponded_rts,
 +			 delta_ofdm->unresponded_rts,
 +			 max_ofdm->unresponded_rts);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "rxe_frame_lmt_ovrun:",
 +			 le32_to_cpu(ofdm->rxe_frame_limit_overrun),
 +			 accum_ofdm->rxe_frame_limit_overrun,
 +			 delta_ofdm->rxe_frame_limit_overrun,
 +			 max_ofdm->rxe_frame_limit_overrun);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "sent_ack_cnt:",
 +			 le32_to_cpu(ofdm->sent_ack_cnt),
 +			 accum_ofdm->sent_ack_cnt, delta_ofdm->sent_ack_cnt,
 +			 max_ofdm->sent_ack_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "sent_cts_cnt:",
 +			 le32_to_cpu(ofdm->sent_cts_cnt),
 +			 accum_ofdm->sent_cts_cnt, delta_ofdm->sent_cts_cnt,
 +			 max_ofdm->sent_cts_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "sent_ba_rsp_cnt:",
 +			 le32_to_cpu(ofdm->sent_ba_rsp_cnt),
 +			 accum_ofdm->sent_ba_rsp_cnt,
 +			 delta_ofdm->sent_ba_rsp_cnt,
 +			 max_ofdm->sent_ba_rsp_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "dsp_self_kill:",
 +			 le32_to_cpu(ofdm->dsp_self_kill),
 +			 accum_ofdm->dsp_self_kill,
 +			 delta_ofdm->dsp_self_kill,
 +			 max_ofdm->dsp_self_kill);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "mh_format_err:",
 +			 le32_to_cpu(ofdm->mh_format_err),
 +			 accum_ofdm->mh_format_err,
 +			 delta_ofdm->mh_format_err,
 +			 max_ofdm->mh_format_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "re_acq_main_rssi_sum:",
 +			 le32_to_cpu(ofdm->re_acq_main_rssi_sum),
 +			 accum_ofdm->re_acq_main_rssi_sum,
 +			 delta_ofdm->re_acq_main_rssi_sum,
 +			 max_ofdm->re_acq_main_rssi_sum);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_header, "Statistics_Rx - CCK:");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "ina_cnt:",
 +			 le32_to_cpu(cck->ina_cnt), accum_cck->ina_cnt,
 +			 delta_cck->ina_cnt, max_cck->ina_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "fina_cnt:",
 +			 le32_to_cpu(cck->fina_cnt), accum_cck->fina_cnt,
 +			 delta_cck->fina_cnt, max_cck->fina_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "plcp_err:",
 +			 le32_to_cpu(cck->plcp_err), accum_cck->plcp_err,
 +			 delta_cck->plcp_err, max_cck->plcp_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "crc32_err:",
 +			 le32_to_cpu(cck->crc32_err), accum_cck->crc32_err,
 +			 delta_cck->crc32_err, max_cck->crc32_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "overrun_err:",
 +			 le32_to_cpu(cck->overrun_err),
 +			 accum_cck->overrun_err, delta_cck->overrun_err,
 +			 max_cck->overrun_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "early_overrun_err:",
 +			 le32_to_cpu(cck->early_overrun_err),
 +			 accum_cck->early_overrun_err,
 +			 delta_cck->early_overrun_err,
 +			 max_cck->early_overrun_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "crc32_good:",
 +			 le32_to_cpu(cck->crc32_good), accum_cck->crc32_good,
 +			 delta_cck->crc32_good, max_cck->crc32_good);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "false_alarm_cnt:",
 +			 le32_to_cpu(cck->false_alarm_cnt),
 +			 accum_cck->false_alarm_cnt,
 +			 delta_cck->false_alarm_cnt, max_cck->false_alarm_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "fina_sync_err_cnt:",
 +			 le32_to_cpu(cck->fina_sync_err_cnt),
 +			 accum_cck->fina_sync_err_cnt,
 +			 delta_cck->fina_sync_err_cnt,
 +			 max_cck->fina_sync_err_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "sfd_timeout:",
 +			 le32_to_cpu(cck->sfd_timeout),
 +			 accum_cck->sfd_timeout, delta_cck->sfd_timeout,
 +			 max_cck->sfd_timeout);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "fina_timeout:",
 +			 le32_to_cpu(cck->fina_timeout),
 +			 accum_cck->fina_timeout, delta_cck->fina_timeout,
 +			 max_cck->fina_timeout);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "unresponded_rts:",
 +			 le32_to_cpu(cck->unresponded_rts),
 +			 accum_cck->unresponded_rts, delta_cck->unresponded_rts,
 +			 max_cck->unresponded_rts);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "rxe_frame_lmt_ovrun:",
 +			 le32_to_cpu(cck->rxe_frame_limit_overrun),
 +			 accum_cck->rxe_frame_limit_overrun,
 +			 delta_cck->rxe_frame_limit_overrun,
 +			 max_cck->rxe_frame_limit_overrun);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "sent_ack_cnt:",
 +			 le32_to_cpu(cck->sent_ack_cnt),
 +			 accum_cck->sent_ack_cnt, delta_cck->sent_ack_cnt,
 +			 max_cck->sent_ack_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "sent_cts_cnt:",
 +			 le32_to_cpu(cck->sent_cts_cnt),
 +			 accum_cck->sent_cts_cnt, delta_cck->sent_cts_cnt,
 +			 max_cck->sent_cts_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "sent_ba_rsp_cnt:",
 +			 le32_to_cpu(cck->sent_ba_rsp_cnt),
 +			 accum_cck->sent_ba_rsp_cnt,
 +			 delta_cck->sent_ba_rsp_cnt,
 +			 max_cck->sent_ba_rsp_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "dsp_self_kill:",
 +			 le32_to_cpu(cck->dsp_self_kill),
 +			 accum_cck->dsp_self_kill, delta_cck->dsp_self_kill,
 +			 max_cck->dsp_self_kill);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "mh_format_err:",
 +			 le32_to_cpu(cck->mh_format_err),
 +			 accum_cck->mh_format_err, delta_cck->mh_format_err,
 +			 max_cck->mh_format_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "re_acq_main_rssi_sum:",
 +			 le32_to_cpu(cck->re_acq_main_rssi_sum),
 +			 accum_cck->re_acq_main_rssi_sum,
 +			 delta_cck->re_acq_main_rssi_sum,
 +			 max_cck->re_acq_main_rssi_sum);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_header, "Statistics_Rx - GENERAL:");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "bogus_cts:",
 +			 le32_to_cpu(general->bogus_cts),
 +			 accum_general->bogus_cts, delta_general->bogus_cts,
 +			 max_general->bogus_cts);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "bogus_ack:",
 +			 le32_to_cpu(general->bogus_ack),
 +			 accum_general->bogus_ack, delta_general->bogus_ack,
 +			 max_general->bogus_ack);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "non_bssid_frames:",
 +			 le32_to_cpu(general->non_bssid_frames),
 +			 accum_general->non_bssid_frames,
 +			 delta_general->non_bssid_frames,
 +			 max_general->non_bssid_frames);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "filtered_frames:",
 +			 le32_to_cpu(general->filtered_frames),
 +			 accum_general->filtered_frames,
 +			 delta_general->filtered_frames,
 +			 max_general->filtered_frames);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "non_channel_beacons:",
 +			 le32_to_cpu(general->non_channel_beacons),
 +			 accum_general->non_channel_beacons,
 +			 delta_general->non_channel_beacons,
 +			 max_general->non_channel_beacons);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "channel_beacons:",
 +			 le32_to_cpu(general->channel_beacons),
 +			 accum_general->channel_beacons,
 +			 delta_general->channel_beacons,
 +			 max_general->channel_beacons);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "num_missed_bcon:",
 +			 le32_to_cpu(general->num_missed_bcon),
 +			 accum_general->num_missed_bcon,
 +			 delta_general->num_missed_bcon,
 +			 max_general->num_missed_bcon);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "adc_rx_saturation_time:",
 +			 le32_to_cpu(general->adc_rx_saturation_time),
 +			 accum_general->adc_rx_saturation_time,
 +			 delta_general->adc_rx_saturation_time,
 +			 max_general->adc_rx_saturation_time);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "ina_detect_search_tm:",
 +			 le32_to_cpu(general->ina_detection_search_time),
 +			 accum_general->ina_detection_search_time,
 +			 delta_general->ina_detection_search_time,
 +			 max_general->ina_detection_search_time);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "beacon_silence_rssi_a:",
 +			 le32_to_cpu(general->beacon_silence_rssi_a),
 +			 accum_general->beacon_silence_rssi_a,
 +			 delta_general->beacon_silence_rssi_a,
 +			 max_general->beacon_silence_rssi_a);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "beacon_silence_rssi_b:",
 +			 le32_to_cpu(general->beacon_silence_rssi_b),
 +			 accum_general->beacon_silence_rssi_b,
 +			 delta_general->beacon_silence_rssi_b,
 +			 max_general->beacon_silence_rssi_b);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "beacon_silence_rssi_c:",
 +			 le32_to_cpu(general->beacon_silence_rssi_c),
 +			 accum_general->beacon_silence_rssi_c,
 +			 delta_general->beacon_silence_rssi_c,
 +			 max_general->beacon_silence_rssi_c);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "interference_data_flag:",
 +			 le32_to_cpu(general->interference_data_flag),
 +			 accum_general->interference_data_flag,
 +			 delta_general->interference_data_flag,
 +			 max_general->interference_data_flag);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "channel_load:",
 +			 le32_to_cpu(general->channel_load),
 +			 accum_general->channel_load,
 +			 delta_general->channel_load,
 +			 max_general->channel_load);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "dsp_false_alarms:",
 +			 le32_to_cpu(general->dsp_false_alarms),
 +			 accum_general->dsp_false_alarms,
 +			 delta_general->dsp_false_alarms,
 +			 max_general->dsp_false_alarms);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "beacon_rssi_a:",
 +			 le32_to_cpu(general->beacon_rssi_a),
 +			 accum_general->beacon_rssi_a,
 +			 delta_general->beacon_rssi_a,
 +			 max_general->beacon_rssi_a);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "beacon_rssi_b:",
 +			 le32_to_cpu(general->beacon_rssi_b),
 +			 accum_general->beacon_rssi_b,
 +			 delta_general->beacon_rssi_b,
 +			 max_general->beacon_rssi_b);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "beacon_rssi_c:",
 +			 le32_to_cpu(general->beacon_rssi_c),
 +			 accum_general->beacon_rssi_c,
 +			 delta_general->beacon_rssi_c,
 +			 max_general->beacon_rssi_c);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "beacon_energy_a:",
 +			 le32_to_cpu(general->beacon_energy_a),
 +			 accum_general->beacon_energy_a,
 +			 delta_general->beacon_energy_a,
 +			 max_general->beacon_energy_a);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "beacon_energy_b:",
 +			 le32_to_cpu(general->beacon_energy_b),
 +			 accum_general->beacon_energy_b,
 +			 delta_general->beacon_energy_b,
 +			 max_general->beacon_energy_b);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "beacon_energy_c:",
 +			 le32_to_cpu(general->beacon_energy_c),
 +			 accum_general->beacon_energy_c,
 +			 delta_general->beacon_energy_c,
 +			 max_general->beacon_energy_c);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_header, "Statistics_Rx - OFDM_HT:");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "plcp_err:",
 +			 le32_to_cpu(ht->plcp_err), accum_ht->plcp_err,
 +			 delta_ht->plcp_err, max_ht->plcp_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "overrun_err:",
 +			 le32_to_cpu(ht->overrun_err), accum_ht->overrun_err,
 +			 delta_ht->overrun_err, max_ht->overrun_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "early_overrun_err:",
 +			 le32_to_cpu(ht->early_overrun_err),
 +			 accum_ht->early_overrun_err,
 +			 delta_ht->early_overrun_err,
 +			 max_ht->early_overrun_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "crc32_good:",
 +			 le32_to_cpu(ht->crc32_good), accum_ht->crc32_good,
 +			 delta_ht->crc32_good, max_ht->crc32_good);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "crc32_err:",
 +			 le32_to_cpu(ht->crc32_err), accum_ht->crc32_err,
 +			 delta_ht->crc32_err, max_ht->crc32_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "mh_format_err:",
 +			 le32_to_cpu(ht->mh_format_err),
 +			 accum_ht->mh_format_err,
 +			 delta_ht->mh_format_err, max_ht->mh_format_err);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg_crc32_good:",
 +			 le32_to_cpu(ht->agg_crc32_good),
 +			 accum_ht->agg_crc32_good,
 +			 delta_ht->agg_crc32_good, max_ht->agg_crc32_good);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg_mpdu_cnt:",
 +			 le32_to_cpu(ht->agg_mpdu_cnt),
 +			 accum_ht->agg_mpdu_cnt,
 +			 delta_ht->agg_mpdu_cnt, max_ht->agg_mpdu_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg_cnt:",
 +			 le32_to_cpu(ht->agg_cnt), accum_ht->agg_cnt,
 +			 delta_ht->agg_cnt, max_ht->agg_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "unsupport_mcs:",
 +			 le32_to_cpu(ht->unsupport_mcs),
 +			 accum_ht->unsupport_mcs,
 +			 delta_ht->unsupport_mcs, max_ht->unsupport_mcs);
 +
 +	spin_unlock_bh(&priv->statistics.lock);
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_ucode_tx_stats_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	int pos = 0;
 +	char *buf;
 +	int bufsz = (sizeof(struct statistics_tx) * 48) + 250;
 +	ssize_t ret;
 +	struct statistics_tx *tx, *accum_tx, *delta_tx, *max_tx;
 +
 +	if (!iwl_is_alive(priv))
 +		return -EAGAIN;
 +
 +	buf = kzalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	/* the statistic information display here is based on
 +	 * the last statistics notification from uCode
 +	 * might not reflect the current uCode activity
 +	 */
 +	spin_lock_bh(&priv->statistics.lock);
 +
 +	tx = &priv->statistics.tx;
 +	accum_tx = &priv->accum_stats.tx;
 +	delta_tx = &priv->delta_stats.tx;
 +	max_tx = &priv->max_delta_stats.tx;
 +
 +	pos += iwl_statistics_flag(priv, buf, bufsz);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_header, "Statistics_Tx:");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "preamble:",
 +			 le32_to_cpu(tx->preamble_cnt),
 +			 accum_tx->preamble_cnt,
 +			 delta_tx->preamble_cnt, max_tx->preamble_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "rx_detected_cnt:",
 +			 le32_to_cpu(tx->rx_detected_cnt),
 +			 accum_tx->rx_detected_cnt,
 +			 delta_tx->rx_detected_cnt, max_tx->rx_detected_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "bt_prio_defer_cnt:",
 +			 le32_to_cpu(tx->bt_prio_defer_cnt),
 +			 accum_tx->bt_prio_defer_cnt,
 +			 delta_tx->bt_prio_defer_cnt,
 +			 max_tx->bt_prio_defer_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "bt_prio_kill_cnt:",
 +			 le32_to_cpu(tx->bt_prio_kill_cnt),
 +			 accum_tx->bt_prio_kill_cnt,
 +			 delta_tx->bt_prio_kill_cnt,
 +			 max_tx->bt_prio_kill_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "few_bytes_cnt:",
 +			 le32_to_cpu(tx->few_bytes_cnt),
 +			 accum_tx->few_bytes_cnt,
 +			 delta_tx->few_bytes_cnt, max_tx->few_bytes_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "cts_timeout:",
 +			 le32_to_cpu(tx->cts_timeout), accum_tx->cts_timeout,
 +			 delta_tx->cts_timeout, max_tx->cts_timeout);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "ack_timeout:",
 +			 le32_to_cpu(tx->ack_timeout),
 +			 accum_tx->ack_timeout,
 +			 delta_tx->ack_timeout, max_tx->ack_timeout);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "expected_ack_cnt:",
 +			 le32_to_cpu(tx->expected_ack_cnt),
 +			 accum_tx->expected_ack_cnt,
 +			 delta_tx->expected_ack_cnt,
 +			 max_tx->expected_ack_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "actual_ack_cnt:",
 +			 le32_to_cpu(tx->actual_ack_cnt),
 +			 accum_tx->actual_ack_cnt,
 +			 delta_tx->actual_ack_cnt,
 +			 max_tx->actual_ack_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "dump_msdu_cnt:",
 +			 le32_to_cpu(tx->dump_msdu_cnt),
 +			 accum_tx->dump_msdu_cnt,
 +			 delta_tx->dump_msdu_cnt,
 +			 max_tx->dump_msdu_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "abort_nxt_frame_mismatch:",
 +			 le32_to_cpu(tx->burst_abort_next_frame_mismatch_cnt),
 +			 accum_tx->burst_abort_next_frame_mismatch_cnt,
 +			 delta_tx->burst_abort_next_frame_mismatch_cnt,
 +			 max_tx->burst_abort_next_frame_mismatch_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "abort_missing_nxt_frame:",
 +			 le32_to_cpu(tx->burst_abort_missing_next_frame_cnt),
 +			 accum_tx->burst_abort_missing_next_frame_cnt,
 +			 delta_tx->burst_abort_missing_next_frame_cnt,
 +			 max_tx->burst_abort_missing_next_frame_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "cts_timeout_collision:",
 +			 le32_to_cpu(tx->cts_timeout_collision),
 +			 accum_tx->cts_timeout_collision,
 +			 delta_tx->cts_timeout_collision,
 +			 max_tx->cts_timeout_collision);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "ack_ba_timeout_collision:",
 +			 le32_to_cpu(tx->ack_or_ba_timeout_collision),
 +			 accum_tx->ack_or_ba_timeout_collision,
 +			 delta_tx->ack_or_ba_timeout_collision,
 +			 max_tx->ack_or_ba_timeout_collision);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg ba_timeout:",
 +			 le32_to_cpu(tx->agg.ba_timeout),
 +			 accum_tx->agg.ba_timeout,
 +			 delta_tx->agg.ba_timeout,
 +			 max_tx->agg.ba_timeout);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg ba_resched_frames:",
 +			 le32_to_cpu(tx->agg.ba_reschedule_frames),
 +			 accum_tx->agg.ba_reschedule_frames,
 +			 delta_tx->agg.ba_reschedule_frames,
 +			 max_tx->agg.ba_reschedule_frames);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg scd_query_agg_frame:",
 +			 le32_to_cpu(tx->agg.scd_query_agg_frame_cnt),
 +			 accum_tx->agg.scd_query_agg_frame_cnt,
 +			 delta_tx->agg.scd_query_agg_frame_cnt,
 +			 max_tx->agg.scd_query_agg_frame_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg scd_query_no_agg:",
 +			 le32_to_cpu(tx->agg.scd_query_no_agg),
 +			 accum_tx->agg.scd_query_no_agg,
 +			 delta_tx->agg.scd_query_no_agg,
 +			 max_tx->agg.scd_query_no_agg);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg scd_query_agg:",
 +			 le32_to_cpu(tx->agg.scd_query_agg),
 +			 accum_tx->agg.scd_query_agg,
 +			 delta_tx->agg.scd_query_agg,
 +			 max_tx->agg.scd_query_agg);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg scd_query_mismatch:",
 +			 le32_to_cpu(tx->agg.scd_query_mismatch),
 +			 accum_tx->agg.scd_query_mismatch,
 +			 delta_tx->agg.scd_query_mismatch,
 +			 max_tx->agg.scd_query_mismatch);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg frame_not_ready:",
 +			 le32_to_cpu(tx->agg.frame_not_ready),
 +			 accum_tx->agg.frame_not_ready,
 +			 delta_tx->agg.frame_not_ready,
 +			 max_tx->agg.frame_not_ready);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg underrun:",
 +			 le32_to_cpu(tx->agg.underrun),
 +			 accum_tx->agg.underrun,
 +			 delta_tx->agg.underrun, max_tx->agg.underrun);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg bt_prio_kill:",
 +			 le32_to_cpu(tx->agg.bt_prio_kill),
 +			 accum_tx->agg.bt_prio_kill,
 +			 delta_tx->agg.bt_prio_kill,
 +			 max_tx->agg.bt_prio_kill);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "agg rx_ba_rsp_cnt:",
 +			 le32_to_cpu(tx->agg.rx_ba_rsp_cnt),
 +			 accum_tx->agg.rx_ba_rsp_cnt,
 +			 delta_tx->agg.rx_ba_rsp_cnt,
 +			 max_tx->agg.rx_ba_rsp_cnt);
 +
 +	if (tx->tx_power.ant_a || tx->tx_power.ant_b || tx->tx_power.ant_c) {
 +		pos += scnprintf(buf + pos, bufsz - pos,
 +			"tx power: (1/2 dB step)\n");
 +		if ((priv->eeprom_data->valid_tx_ant & ANT_A) &&
 +		    tx->tx_power.ant_a)
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +					fmt_hex, "antenna A:",
 +					tx->tx_power.ant_a);
 +		if ((priv->eeprom_data->valid_tx_ant & ANT_B) &&
 +		    tx->tx_power.ant_b)
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +					fmt_hex, "antenna B:",
 +					tx->tx_power.ant_b);
 +		if ((priv->eeprom_data->valid_tx_ant & ANT_C) &&
 +		    tx->tx_power.ant_c)
 +			pos += scnprintf(buf + pos, bufsz - pos,
 +					fmt_hex, "antenna C:",
 +					tx->tx_power.ant_c);
 +	}
 +
 +	spin_unlock_bh(&priv->statistics.lock);
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_ucode_general_stats_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	int pos = 0;
 +	char *buf;
 +	int bufsz = sizeof(struct statistics_general) * 10 + 300;
 +	ssize_t ret;
 +	struct statistics_general_common *general, *accum_general;
 +	struct statistics_general_common *delta_general, *max_general;
 +	struct statistics_dbg *dbg, *accum_dbg, *delta_dbg, *max_dbg;
 +	struct statistics_div *div, *accum_div, *delta_div, *max_div;
 +
 +	if (!iwl_is_alive(priv))
 +		return -EAGAIN;
 +
 +	buf = kzalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	/* the statistic information display here is based on
 +	 * the last statistics notification from uCode
 +	 * might not reflect the current uCode activity
 +	 */
 +
 +	spin_lock_bh(&priv->statistics.lock);
 +
 +	general = &priv->statistics.common;
 +	dbg = &priv->statistics.common.dbg;
 +	div = &priv->statistics.common.div;
 +	accum_general = &priv->accum_stats.common;
 +	accum_dbg = &priv->accum_stats.common.dbg;
 +	accum_div = &priv->accum_stats.common.div;
 +	delta_general = &priv->delta_stats.common;
 +	max_general = &priv->max_delta_stats.common;
 +	delta_dbg = &priv->delta_stats.common.dbg;
 +	max_dbg = &priv->max_delta_stats.common.dbg;
 +	delta_div = &priv->delta_stats.common.div;
 +	max_div = &priv->max_delta_stats.common.div;
 +
 +	pos += iwl_statistics_flag(priv, buf, bufsz);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_header, "Statistics_General:");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_value, "temperature:",
 +			 le32_to_cpu(general->temperature));
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_value, "temperature_m:",
 +			 le32_to_cpu(general->temperature_m));
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_value, "ttl_timestamp:",
 +			 le32_to_cpu(general->ttl_timestamp));
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "burst_check:",
 +			 le32_to_cpu(dbg->burst_check),
 +			 accum_dbg->burst_check,
 +			 delta_dbg->burst_check, max_dbg->burst_check);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "burst_count:",
 +			 le32_to_cpu(dbg->burst_count),
 +			 accum_dbg->burst_count,
 +			 delta_dbg->burst_count, max_dbg->burst_count);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "wait_for_silence_timeout_count:",
 +			 le32_to_cpu(dbg->wait_for_silence_timeout_cnt),
 +			 accum_dbg->wait_for_silence_timeout_cnt,
 +			 delta_dbg->wait_for_silence_timeout_cnt,
 +			 max_dbg->wait_for_silence_timeout_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "sleep_time:",
 +			 le32_to_cpu(general->sleep_time),
 +			 accum_general->sleep_time,
 +			 delta_general->sleep_time, max_general->sleep_time);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "slots_out:",
 +			 le32_to_cpu(general->slots_out),
 +			 accum_general->slots_out,
 +			 delta_general->slots_out, max_general->slots_out);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "slots_idle:",
 +			 le32_to_cpu(general->slots_idle),
 +			 accum_general->slots_idle,
 +			 delta_general->slots_idle, max_general->slots_idle);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "tx_on_a:",
 +			 le32_to_cpu(div->tx_on_a), accum_div->tx_on_a,
 +			 delta_div->tx_on_a, max_div->tx_on_a);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "tx_on_b:",
 +			 le32_to_cpu(div->tx_on_b), accum_div->tx_on_b,
 +			 delta_div->tx_on_b, max_div->tx_on_b);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "exec_time:",
 +			 le32_to_cpu(div->exec_time), accum_div->exec_time,
 +			 delta_div->exec_time, max_div->exec_time);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "probe_time:",
 +			 le32_to_cpu(div->probe_time), accum_div->probe_time,
 +			 delta_div->probe_time, max_div->probe_time);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "rx_enable_counter:",
 +			 le32_to_cpu(general->rx_enable_counter),
 +			 accum_general->rx_enable_counter,
 +			 delta_general->rx_enable_counter,
 +			 max_general->rx_enable_counter);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 fmt_table, "num_of_sos_states:",
 +			 le32_to_cpu(general->num_of_sos_states),
 +			 accum_general->num_of_sos_states,
 +			 delta_general->num_of_sos_states,
 +			 max_general->num_of_sos_states);
 +
 +	spin_unlock_bh(&priv->statistics.lock);
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_ucode_bt_stats_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
 +	int pos = 0;
 +	char *buf;
 +	int bufsz = (sizeof(struct statistics_bt_activity) * 24) + 200;
 +	ssize_t ret;
 +	struct statistics_bt_activity *bt, *accum_bt;
 +
 +	if (!iwl_is_alive(priv))
 +		return -EAGAIN;
 +
 +	if (!priv->bt_enable_flag)
 +		return -EINVAL;
 +
 +	/* make request to uCode to retrieve statistics information */
 +	mutex_lock(&priv->mutex);
 +	ret = iwl_send_statistics_request(priv, CMD_SYNC, false);
 +	mutex_unlock(&priv->mutex);
 +
 +	if (ret)
 +		return -EAGAIN;
 +	buf = kzalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	/*
 +	 * the statistic information display here is based on
 +	 * the last statistics notification from uCode
 +	 * might not reflect the current uCode activity
 +	 */
 +
 +	spin_lock_bh(&priv->statistics.lock);
 +
 +	bt = &priv->statistics.bt_activity;
 +	accum_bt = &priv->accum_stats.bt_activity;
 +
 +	pos += iwl_statistics_flag(priv, buf, bufsz);
 +	pos += scnprintf(buf + pos, bufsz - pos, "Statistics_BT:\n");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"\t\t\tcurrent\t\t\taccumulative\n");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "hi_priority_tx_req_cnt:\t\t%u\t\t\t%u\n",
 +			 le32_to_cpu(bt->hi_priority_tx_req_cnt),
 +			 accum_bt->hi_priority_tx_req_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "hi_priority_tx_denied_cnt:\t%u\t\t\t%u\n",
 +			 le32_to_cpu(bt->hi_priority_tx_denied_cnt),
 +			 accum_bt->hi_priority_tx_denied_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "lo_priority_tx_req_cnt:\t\t%u\t\t\t%u\n",
 +			 le32_to_cpu(bt->lo_priority_tx_req_cnt),
 +			 accum_bt->lo_priority_tx_req_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "lo_priority_tx_denied_cnt:\t%u\t\t\t%u\n",
 +			 le32_to_cpu(bt->lo_priority_tx_denied_cnt),
 +			 accum_bt->lo_priority_tx_denied_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "hi_priority_rx_req_cnt:\t\t%u\t\t\t%u\n",
 +			 le32_to_cpu(bt->hi_priority_rx_req_cnt),
 +			 accum_bt->hi_priority_rx_req_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "hi_priority_rx_denied_cnt:\t%u\t\t\t%u\n",
 +			 le32_to_cpu(bt->hi_priority_rx_denied_cnt),
 +			 accum_bt->hi_priority_rx_denied_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "lo_priority_rx_req_cnt:\t\t%u\t\t\t%u\n",
 +			 le32_to_cpu(bt->lo_priority_rx_req_cnt),
 +			 accum_bt->lo_priority_rx_req_cnt);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "lo_priority_rx_denied_cnt:\t%u\t\t\t%u\n",
 +			 le32_to_cpu(bt->lo_priority_rx_denied_cnt),
 +			 accum_bt->lo_priority_rx_denied_cnt);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "(rx)num_bt_kills:\t\t%u\t\t\t%u\n",
 +			 le32_to_cpu(priv->statistics.num_bt_kills),
 +			 priv->statistics.accum_num_bt_kills);
 +
 +	spin_unlock_bh(&priv->statistics.lock);
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_reply_tx_error_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
 +	int pos = 0;
 +	char *buf;
 +	int bufsz = (sizeof(struct reply_tx_error_statistics) * 24) +
 +		(sizeof(struct reply_agg_tx_error_statistics) * 24) + 200;
 +	ssize_t ret;
 +
 +	if (!iwl_is_alive(priv))
 +		return -EAGAIN;
 +
 +	buf = kzalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "Statistics_TX_Error:\n");
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_POSTPONE_DELAY),
 +			 priv->reply_tx_stats.pp_delay);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_POSTPONE_FEW_BYTES),
 +			 priv->reply_tx_stats.pp_few_bytes);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_POSTPONE_BT_PRIO),
 +			 priv->reply_tx_stats.pp_bt_prio);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_POSTPONE_QUIET_PERIOD),
 +			 priv->reply_tx_stats.pp_quiet_period);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_POSTPONE_CALC_TTAK),
 +			 priv->reply_tx_stats.pp_calc_ttak);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
 +			 iwl_get_tx_fail_reason(
 +				TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY),
 +			 priv->reply_tx_stats.int_crossed_retry);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_SHORT_LIMIT),
 +			 priv->reply_tx_stats.short_limit);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_LONG_LIMIT),
 +			 priv->reply_tx_stats.long_limit);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_FIFO_UNDERRUN),
 +			 priv->reply_tx_stats.fifo_underrun);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_DRAIN_FLOW),
 +			 priv->reply_tx_stats.drain_flow);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_RFKILL_FLUSH),
 +			 priv->reply_tx_stats.rfkill_flush);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_LIFE_EXPIRE),
 +			 priv->reply_tx_stats.life_expire);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_DEST_PS),
 +			 priv->reply_tx_stats.dest_ps);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_HOST_ABORTED),
 +			 priv->reply_tx_stats.host_abort);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_BT_RETRY),
 +			 priv->reply_tx_stats.pp_delay);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_STA_INVALID),
 +			 priv->reply_tx_stats.sta_invalid);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_FRAG_DROPPED),
 +			 priv->reply_tx_stats.frag_drop);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_TID_DISABLE),
 +			 priv->reply_tx_stats.tid_disable);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_FIFO_FLUSHED),
 +			 priv->reply_tx_stats.fifo_flush);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
 +			 iwl_get_tx_fail_reason(
 +				TX_STATUS_FAIL_INSUFFICIENT_CF_POLL),
 +			 priv->reply_tx_stats.insuff_cf_poll);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_tx_fail_reason(TX_STATUS_FAIL_PASSIVE_NO_RX),
 +			 priv->reply_tx_stats.fail_hw_drop);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
 +			 iwl_get_tx_fail_reason(
 +				TX_STATUS_FAIL_NO_BEACON_ON_RADAR),
 +			 priv->reply_tx_stats.sta_color_mismatch);
 +	pos += scnprintf(buf + pos, bufsz - pos, "UNKNOWN:\t\t\t%u\n",
 +			 priv->reply_tx_stats.unknown);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "\nStatistics_Agg_TX_Error:\n");
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_UNDERRUN_MSK),
 +			 priv->reply_agg_tx_stats.underrun);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_BT_PRIO_MSK),
 +			 priv->reply_agg_tx_stats.bt_prio);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_FEW_BYTES_MSK),
 +			 priv->reply_agg_tx_stats.few_bytes);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_ABORT_MSK),
 +			 priv->reply_agg_tx_stats.abort);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(
 +				AGG_TX_STATE_LAST_SENT_TTL_MSK),
 +			 priv->reply_agg_tx_stats.last_sent_ttl);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(
 +				AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK),
 +			 priv->reply_agg_tx_stats.last_sent_try);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(
 +				AGG_TX_STATE_LAST_SENT_BT_KILL_MSK),
 +			 priv->reply_agg_tx_stats.last_sent_bt_kill);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_SCD_QUERY_MSK),
 +			 priv->reply_agg_tx_stats.scd_query);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(
 +				AGG_TX_STATE_TEST_BAD_CRC32_MSK),
 +			 priv->reply_agg_tx_stats.bad_crc32);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_RESPONSE_MSK),
 +			 priv->reply_agg_tx_stats.response);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_DUMP_TX_MSK),
 +			 priv->reply_agg_tx_stats.dump_tx);
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
 +			 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_DELAY_TX_MSK),
 +			 priv->reply_agg_tx_stats.delay_tx);
 +	pos += scnprintf(buf + pos, bufsz - pos, "UNKNOWN:\t\t\t%u\n",
 +			 priv->reply_agg_tx_stats.unknown);
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_sensitivity_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	int pos = 0;
 +	int cnt = 0;
 +	char *buf;
 +	int bufsz = sizeof(struct iwl_sensitivity_data) * 4 + 100;
 +	ssize_t ret;
 +	struct iwl_sensitivity_data *data;
 +
 +	data = &priv->sensitivity_data;
 +	buf = kzalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_ofdm:\t\t\t %u\n",
 +			data->auto_corr_ofdm);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"auto_corr_ofdm_mrc:\t\t %u\n",
 +			data->auto_corr_ofdm_mrc);
 +	pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_ofdm_x1:\t\t %u\n",
 +			data->auto_corr_ofdm_x1);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"auto_corr_ofdm_mrc_x1:\t\t %u\n",
 +			data->auto_corr_ofdm_mrc_x1);
 +	pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_cck:\t\t\t %u\n",
 +			data->auto_corr_cck);
 +	pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_cck_mrc:\t\t %u\n",
 +			data->auto_corr_cck_mrc);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"last_bad_plcp_cnt_ofdm:\t\t %u\n",
 +			data->last_bad_plcp_cnt_ofdm);
 +	pos += scnprintf(buf + pos, bufsz - pos, "last_fa_cnt_ofdm:\t\t %u\n",
 +			data->last_fa_cnt_ofdm);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"last_bad_plcp_cnt_cck:\t\t %u\n",
 +			data->last_bad_plcp_cnt_cck);
 +	pos += scnprintf(buf + pos, bufsz - pos, "last_fa_cnt_cck:\t\t %u\n",
 +			data->last_fa_cnt_cck);
 +	pos += scnprintf(buf + pos, bufsz - pos, "nrg_curr_state:\t\t\t %u\n",
 +			data->nrg_curr_state);
 +	pos += scnprintf(buf + pos, bufsz - pos, "nrg_prev_state:\t\t\t %u\n",
 +			data->nrg_prev_state);
 +	pos += scnprintf(buf + pos, bufsz - pos, "nrg_value:\t\t\t");
 +	for (cnt = 0; cnt < 10; cnt++) {
 +		pos += scnprintf(buf + pos, bufsz - pos, " %u",
 +				data->nrg_value[cnt]);
 +	}
 +	pos += scnprintf(buf + pos, bufsz - pos, "\n");
 +	pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_rssi:\t\t");
 +	for (cnt = 0; cnt < NRG_NUM_PREV_STAT_L; cnt++) {
 +		pos += scnprintf(buf + pos, bufsz - pos, " %u",
 +				data->nrg_silence_rssi[cnt]);
 +	}
 +	pos += scnprintf(buf + pos, bufsz - pos, "\n");
 +	pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_ref:\t\t %u\n",
 +			data->nrg_silence_ref);
 +	pos += scnprintf(buf + pos, bufsz - pos, "nrg_energy_idx:\t\t\t %u\n",
 +			data->nrg_energy_idx);
 +	pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_idx:\t\t %u\n",
 +			data->nrg_silence_idx);
 +	pos += scnprintf(buf + pos, bufsz - pos, "nrg_th_cck:\t\t\t %u\n",
 +			data->nrg_th_cck);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"nrg_auto_corr_silence_diff:\t %u\n",
 +			data->nrg_auto_corr_silence_diff);
 +	pos += scnprintf(buf + pos, bufsz - pos, "num_in_cck_no_fa:\t\t %u\n",
 +			data->num_in_cck_no_fa);
 +	pos += scnprintf(buf + pos, bufsz - pos, "nrg_th_ofdm:\t\t\t %u\n",
 +			data->nrg_th_ofdm);
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +
 +static ssize_t iwl_dbgfs_chain_noise_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	int pos = 0;
 +	int cnt = 0;
 +	char *buf;
 +	int bufsz = sizeof(struct iwl_chain_noise_data) * 4 + 100;
 +	ssize_t ret;
 +	struct iwl_chain_noise_data *data;
 +
 +	data = &priv->chain_noise_data;
 +	buf = kzalloc(bufsz, GFP_KERNEL);
 +	if (!buf)
 +		return -ENOMEM;
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "active_chains:\t\t\t %u\n",
 +			data->active_chains);
 +	pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_a:\t\t\t %u\n",
 +			data->chain_noise_a);
 +	pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_b:\t\t\t %u\n",
 +			data->chain_noise_b);
 +	pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_c:\t\t\t %u\n",
 +			data->chain_noise_c);
 +	pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_a:\t\t\t %u\n",
 +			data->chain_signal_a);
 +	pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_b:\t\t\t %u\n",
 +			data->chain_signal_b);
 +	pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_c:\t\t\t %u\n",
 +			data->chain_signal_c);
 +	pos += scnprintf(buf + pos, bufsz - pos, "beacon_count:\t\t\t %u\n",
 +			data->beacon_count);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "disconn_array:\t\t\t");
 +	for (cnt = 0; cnt < NUM_RX_CHAINS; cnt++) {
 +		pos += scnprintf(buf + pos, bufsz - pos, " %u",
 +				data->disconn_array[cnt]);
 +	}
 +	pos += scnprintf(buf + pos, bufsz - pos, "\n");
 +	pos += scnprintf(buf + pos, bufsz - pos, "delta_gain_code:\t\t");
 +	for (cnt = 0; cnt < NUM_RX_CHAINS; cnt++) {
 +		pos += scnprintf(buf + pos, bufsz - pos, " %u",
 +				data->delta_gain_code[cnt]);
 +	}
 +	pos += scnprintf(buf + pos, bufsz - pos, "\n");
 +	pos += scnprintf(buf + pos, bufsz - pos, "radio_write:\t\t\t %u\n",
 +			data->radio_write);
 +	pos += scnprintf(buf + pos, bufsz - pos, "state:\t\t\t\t %u\n",
 +			data->state);
 +
 +	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	kfree(buf);
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_power_save_status_read(struct file *file,
 +						    char __user *user_buf,
 +						    size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[60];
 +	int pos = 0;
 +	const size_t bufsz = sizeof(buf);
 +	u32 pwrsave_status;
 +
 +	pwrsave_status = iwl_read32(priv->trans, CSR_GP_CNTRL) &
 +			CSR_GP_REG_POWER_SAVE_STATUS_MSK;
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "Power Save Status: ");
 +	pos += scnprintf(buf + pos, bufsz - pos, "%s\n",
 +		(pwrsave_status == CSR_GP_REG_NO_POWER_SAVE) ? "none" :
 +		(pwrsave_status == CSR_GP_REG_MAC_POWER_SAVE) ? "MAC" :
 +		(pwrsave_status == CSR_GP_REG_PHY_POWER_SAVE) ? "PHY" :
 +		"error");
 +
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_clear_ucode_statistics_write(struct file *file,
 +					 const char __user *user_buf,
 +					 size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	int buf_size;
 +	int clear;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +	if (sscanf(buf, "%d", &clear) != 1)
 +		return -EFAULT;
 +
 +	/* make request to uCode to retrieve statistics information */
 +	mutex_lock(&priv->mutex);
 +	iwl_send_statistics_request(priv, CMD_SYNC, true);
 +	mutex_unlock(&priv->mutex);
 +
 +	return count;
 +}
 +
 +static ssize_t iwl_dbgfs_ucode_tracing_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	int pos = 0;
 +	char buf[128];
 +	const size_t bufsz = sizeof(buf);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "ucode trace timer is %s\n",
 +			priv->event_log.ucode_trace ? "On" : "Off");
 +	pos += scnprintf(buf + pos, bufsz - pos, "non_wraps_count:\t\t %u\n",
 +			priv->event_log.non_wraps_count);
 +	pos += scnprintf(buf + pos, bufsz - pos, "wraps_once_count:\t\t %u\n",
 +			priv->event_log.wraps_once_count);
 +	pos += scnprintf(buf + pos, bufsz - pos, "wraps_more_count:\t\t %u\n",
 +			priv->event_log.wraps_more_count);
 +
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_ucode_tracing_write(struct file *file,
 +					 const char __user *user_buf,
 +					 size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	int buf_size;
 +	int trace;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +	if (sscanf(buf, "%d", &trace) != 1)
 +		return -EFAULT;
 +
 +	if (trace) {
 +		priv->event_log.ucode_trace = true;
 +		if (iwl_is_alive(priv)) {
 +			/* start collecting data now */
 +			mod_timer(&priv->ucode_trace, jiffies);
 +		}
 +	} else {
 +		priv->event_log.ucode_trace = false;
 +		del_timer_sync(&priv->ucode_trace);
 +	}
 +
 +	return count;
 +}
 +
 +static ssize_t iwl_dbgfs_rxon_flags_read(struct file *file,
 +					 char __user *user_buf,
 +					 size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	int len = 0;
 +	char buf[20];
 +
 +	len = sprintf(buf, "0x%04X\n",
 +		le32_to_cpu(priv->contexts[IWL_RXON_CTX_BSS].active.flags));
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
 +}
 +
 +static ssize_t iwl_dbgfs_rxon_filter_flags_read(struct file *file,
 +						char __user *user_buf,
 +						size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	int len = 0;
 +	char buf[20];
 +
 +	len = sprintf(buf, "0x%04X\n",
 +		le32_to_cpu(priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags));
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
 +}
 +
 +static ssize_t iwl_dbgfs_missed_beacon_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	int pos = 0;
 +	char buf[12];
 +	const size_t bufsz = sizeof(buf);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "%d\n",
 +			priv->missed_beacon_threshold);
 +
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_missed_beacon_write(struct file *file,
 +					 const char __user *user_buf,
 +					 size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	int buf_size;
 +	int missed;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +	if (sscanf(buf, "%d", &missed) != 1)
 +		return -EINVAL;
 +
 +	if (missed < IWL_MISSED_BEACON_THRESHOLD_MIN ||
 +	    missed > IWL_MISSED_BEACON_THRESHOLD_MAX)
 +		priv->missed_beacon_threshold =
 +			IWL_MISSED_BEACON_THRESHOLD_DEF;
 +	else
 +		priv->missed_beacon_threshold = missed;
 +
 +	return count;
 +}
 +
 +static ssize_t iwl_dbgfs_plcp_delta_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	int pos = 0;
 +	char buf[12];
 +	const size_t bufsz = sizeof(buf);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "%u\n",
 +			priv->plcp_delta_threshold);
 +
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_plcp_delta_write(struct file *file,
 +					const char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	int buf_size;
 +	int plcp;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +	if (sscanf(buf, "%d", &plcp) != 1)
 +		return -EINVAL;
 +	if ((plcp < IWL_MAX_PLCP_ERR_THRESHOLD_MIN) ||
 +		(plcp > IWL_MAX_PLCP_ERR_THRESHOLD_MAX))
 +		priv->plcp_delta_threshold =
 +			IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE;
 +	else
 +		priv->plcp_delta_threshold = plcp;
 +	return count;
 +}
 +
 +static ssize_t iwl_dbgfs_rf_reset_read(struct file *file,
 +				       char __user *user_buf,
 +				       size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	int pos = 0;
 +	char buf[300];
 +	const size_t bufsz = sizeof(buf);
 +	struct iwl_rf_reset *rf_reset = &priv->rf_reset;
 +
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"RF reset statistics\n");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"\tnumber of reset request: %d\n",
 +			rf_reset->reset_request_count);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"\tnumber of reset request success: %d\n",
 +			rf_reset->reset_success_count);
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			"\tnumber of reset request reject: %d\n",
 +			rf_reset->reset_reject_count);
 +
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_rf_reset_write(struct file *file,
 +					const char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	int ret;
 +
 +	ret = iwl_force_rf_reset(priv, true);
 +	return ret ? ret : count;
 +}
 +
 +static ssize_t iwl_dbgfs_txfifo_flush_write(struct file *file,
 +					const char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	int buf_size;
 +	int flush;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +	if (sscanf(buf, "%d", &flush) != 1)
 +		return -EINVAL;
 +
 +	if (iwl_is_rfkill(priv))
 +		return -EFAULT;
 +
 +	iwlagn_dev_txfifo_flush(priv, IWL_DROP_ALL);
 +
 +	return count;
 +}
 +
 +static ssize_t iwl_dbgfs_bt_traffic_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
 +	int pos = 0;
 +	char buf[200];
 +	const size_t bufsz = sizeof(buf);
 +
 +	if (!priv->bt_enable_flag) {
 +		pos += scnprintf(buf + pos, bufsz - pos, "BT coex disabled\n");
 +		return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +	}
 +	pos += scnprintf(buf + pos, bufsz - pos, "BT enable flag: 0x%x\n",
 +		priv->bt_enable_flag);
 +	pos += scnprintf(buf + pos, bufsz - pos, "BT in %s mode\n",
 +		priv->bt_full_concurrent ? "full concurrency" : "3-wire");
 +	pos += scnprintf(buf + pos, bufsz - pos, "BT status: %s, "
 +			 "last traffic notif: %d\n",
 +		priv->bt_status ? "On" : "Off", priv->last_bt_traffic_load);
 +	pos += scnprintf(buf + pos, bufsz - pos, "ch_announcement: %d, "
 +			 "kill_ack_mask: %x, kill_cts_mask: %x\n",
 +		priv->bt_ch_announce, priv->kill_ack_mask,
 +		priv->kill_cts_mask);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos, "bluetooth traffic load: ");
 +	switch (priv->bt_traffic_load) {
 +	case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
 +		pos += scnprintf(buf + pos, bufsz - pos, "Continuous\n");
 +		break;
 +	case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
 +		pos += scnprintf(buf + pos, bufsz - pos, "High\n");
 +		break;
 +	case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
 +		pos += scnprintf(buf + pos, bufsz - pos, "Low\n");
 +		break;
 +	case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
 +	default:
 +		pos += scnprintf(buf + pos, bufsz - pos, "None\n");
 +		break;
 +	}
 +
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_protection_mode_read(struct file *file,
 +					char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
 +
 +	int pos = 0;
 +	char buf[40];
 +	const size_t bufsz = sizeof(buf);
 +
 +	if (priv->cfg->ht_params)
 +		pos += scnprintf(buf + pos, bufsz - pos,
 +			 "use %s for aggregation\n",
 +			 (priv->hw_params.use_rts_for_aggregation) ?
 +				"rts/cts" : "cts-to-self");
 +	else
 +		pos += scnprintf(buf + pos, bufsz - pos, "N/A");
 +
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_protection_mode_write(struct file *file,
 +					const char __user *user_buf,
 +					size_t count, loff_t *ppos) {
 +
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	int buf_size;
 +	int rts;
 +
 +	if (!priv->cfg->ht_params)
 +		return -EINVAL;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +	if (sscanf(buf, "%d", &rts) != 1)
 +		return -EINVAL;
 +	if (rts)
 +		priv->hw_params.use_rts_for_aggregation = true;
 +	else
 +		priv->hw_params.use_rts_for_aggregation = false;
 +	return count;
 +}
 +
 +static int iwl_cmd_echo_test(struct iwl_priv *priv)
 +{
 +	int ret;
 +	struct iwl_host_cmd cmd = {
 +		.id = REPLY_ECHO,
 +		.len = { 0 },
 +		.flags = CMD_SYNC,
 +	};
 +
 +	ret = iwl_dvm_send_cmd(priv, &cmd);
 +	if (ret)
 +		IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
 +	else
 +		IWL_DEBUG_INFO(priv, "echo testing pass\n");
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_echo_test_write(struct file *file,
 +					const char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	int buf_size;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +
 +	iwl_cmd_echo_test(priv);
 +	return count;
 +}
 +
++#ifdef CONFIG_IWLWIFI_DEBUG
 +static ssize_t iwl_dbgfs_log_event_read(struct file *file,
 +					 char __user *user_buf,
 +					 size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char *buf;
 +	int pos = 0;
 +	ssize_t ret = -ENOMEM;
 +
 +	ret = pos = iwl_dump_nic_event_log(priv, true, &buf, true);
 +	if (buf) {
 +		ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +		kfree(buf);
 +	}
 +	return ret;
 +}
 +
 +static ssize_t iwl_dbgfs_log_event_write(struct file *file,
 +					const char __user *user_buf,
 +					size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	u32 event_log_flag;
 +	char buf[8];
 +	int buf_size;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) -  1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +	if (sscanf(buf, "%d", &event_log_flag) != 1)
 +		return -EFAULT;
 +	if (event_log_flag == 1)
 +		iwl_dump_nic_event_log(priv, true, NULL, false);
 +
 +	return count;
 +}
++#endif
 +
 +static ssize_t iwl_dbgfs_calib_disabled_read(struct file *file,
 +					 char __user *user_buf,
 +					 size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[120];
 +	int pos = 0;
 +	const size_t bufsz = sizeof(buf);
 +
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "Sensitivity calibrations %s\n",
 +			 (priv->calib_disabled &
 +					IWL_SENSITIVITY_CALIB_DISABLED) ?
 +			 "DISABLED" : "ENABLED");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "Chain noise calibrations %s\n",
 +			 (priv->calib_disabled &
 +					IWL_CHAIN_NOISE_CALIB_DISABLED) ?
 +			 "DISABLED" : "ENABLED");
 +	pos += scnprintf(buf + pos, bufsz - pos,
 +			 "Tx power calibrations %s\n",
 +			 (priv->calib_disabled &
 +					IWL_TX_POWER_CALIB_DISABLED) ?
 +			 "DISABLED" : "ENABLED");
 +
 +	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
 +}
 +
 +static ssize_t iwl_dbgfs_calib_disabled_write(struct file *file,
 +					      const char __user *user_buf,
 +					      size_t count, loff_t *ppos)
 +{
 +	struct iwl_priv *priv = file->private_data;
 +	char buf[8];
 +	u32 calib_disabled;
 +	int buf_size;
 +
 +	memset(buf, 0, sizeof(buf));
 +	buf_size = min(count, sizeof(buf) - 1);
 +	if (copy_from_user(buf, user_buf, buf_size))
 +		return -EFAULT;
 +	if (sscanf(buf, "%x", &calib_disabled) != 1)
 +		return -EFAULT;
 +
 +	priv->calib_disabled = calib_disabled;
 +
 +	return count;
 +}
 +
 +DEBUGFS_READ_FILE_OPS(ucode_rx_stats);
 +DEBUGFS_READ_FILE_OPS(ucode_tx_stats);
 +DEBUGFS_READ_FILE_OPS(ucode_general_stats);
 +DEBUGFS_READ_FILE_OPS(sensitivity);
 +DEBUGFS_READ_FILE_OPS(chain_noise);
 +DEBUGFS_READ_FILE_OPS(power_save_status);
 +DEBUGFS_WRITE_FILE_OPS(clear_ucode_statistics);
 +DEBUGFS_READ_WRITE_FILE_OPS(ucode_tracing);
 +DEBUGFS_READ_WRITE_FILE_OPS(missed_beacon);
 +DEBUGFS_READ_WRITE_FILE_OPS(plcp_delta);
 +DEBUGFS_READ_WRITE_FILE_OPS(rf_reset);
 +DEBUGFS_READ_FILE_OPS(rxon_flags);
 +DEBUGFS_READ_FILE_OPS(rxon_filter_flags);
 +DEBUGFS_WRITE_FILE_OPS(txfifo_flush);
 +DEBUGFS_READ_FILE_OPS(ucode_bt_stats);
 +DEBUGFS_READ_FILE_OPS(bt_traffic);
 +DEBUGFS_READ_WRITE_FILE_OPS(protection_mode);
 +DEBUGFS_READ_FILE_OPS(reply_tx_error);
 +DEBUGFS_WRITE_FILE_OPS(echo_test);
++#ifdef CONFIG_IWLWIFI_DEBUG
 +DEBUGFS_READ_WRITE_FILE_OPS(log_event);
++#endif
 +DEBUGFS_READ_WRITE_FILE_OPS(calib_disabled);
 +
 +/*
 + * Create the debugfs files and directories
 + *
 + */
 +int iwl_dbgfs_register(struct iwl_priv *priv, const char *name)
 +{
 +	struct dentry *phyd = priv->hw->wiphy->debugfsdir;
 +	struct dentry *dir_drv, *dir_data, *dir_rf, *dir_debug;
 +
 +	dir_drv = debugfs_create_dir(name, phyd);
 +	if (!dir_drv)
 +		return -ENOMEM;
 +
 +	priv->debugfs_dir = dir_drv;
 +
 +	dir_data = debugfs_create_dir("data", dir_drv);
 +	if (!dir_data)
 +		goto err;
 +	dir_rf = debugfs_create_dir("rf", dir_drv);
 +	if (!dir_rf)
 +		goto err;
 +	dir_debug = debugfs_create_dir("debug", dir_drv);
 +	if (!dir_debug)
 +		goto err;
 +
 +	DEBUGFS_ADD_FILE(nvm, dir_data, S_IRUSR);
 +	DEBUGFS_ADD_FILE(sram, dir_data, S_IWUSR | S_IRUSR);
 +	DEBUGFS_ADD_FILE(wowlan_sram, dir_data, S_IRUSR);
 +	DEBUGFS_ADD_FILE(stations, dir_data, S_IRUSR);
 +	DEBUGFS_ADD_FILE(channels, dir_data, S_IRUSR);
 +	DEBUGFS_ADD_FILE(status, dir_data, S_IRUSR);
 +	DEBUGFS_ADD_FILE(rx_handlers, dir_data, S_IWUSR | S_IRUSR);
 +	DEBUGFS_ADD_FILE(qos, dir_data, S_IRUSR);
 +	DEBUGFS_ADD_FILE(sleep_level_override, dir_data, S_IWUSR | S_IRUSR);
 +	DEBUGFS_ADD_FILE(current_sleep_command, dir_data, S_IRUSR);
 +	DEBUGFS_ADD_FILE(thermal_throttling, dir_data, S_IRUSR);
 +	DEBUGFS_ADD_FILE(disable_ht40, dir_data, S_IWUSR | S_IRUSR);
 +	DEBUGFS_ADD_FILE(temperature, dir_data, S_IRUSR);
 +
 +	DEBUGFS_ADD_FILE(power_save_status, dir_debug, S_IRUSR);
 +	DEBUGFS_ADD_FILE(clear_ucode_statistics, dir_debug, S_IWUSR);
 +	DEBUGFS_ADD_FILE(missed_beacon, dir_debug, S_IWUSR);
 +	DEBUGFS_ADD_FILE(plcp_delta, dir_debug, S_IWUSR | S_IRUSR);
 +	DEBUGFS_ADD_FILE(rf_reset, dir_debug, S_IWUSR | S_IRUSR);
 +	DEBUGFS_ADD_FILE(ucode_rx_stats, dir_debug, S_IRUSR);
 +	DEBUGFS_ADD_FILE(ucode_tx_stats, dir_debug, S_IRUSR);
 +	DEBUGFS_ADD_FILE(ucode_general_stats, dir_debug, S_IRUSR);
 +	DEBUGFS_ADD_FILE(txfifo_flush, dir_debug, S_IWUSR);
 +	DEBUGFS_ADD_FILE(protection_mode, dir_debug, S_IWUSR | S_IRUSR);
 +	DEBUGFS_ADD_FILE(sensitivity, dir_debug, S_IRUSR);
 +	DEBUGFS_ADD_FILE(chain_noise, dir_debug, S_IRUSR);
 +	DEBUGFS_ADD_FILE(ucode_tracing, dir_debug, S_IWUSR | S_IRUSR);
 +	DEBUGFS_ADD_FILE(ucode_bt_stats, dir_debug, S_IRUSR);
 +	DEBUGFS_ADD_FILE(reply_tx_error, dir_debug, S_IRUSR);
 +	DEBUGFS_ADD_FILE(rxon_flags, dir_debug, S_IWUSR);
 +	DEBUGFS_ADD_FILE(rxon_filter_flags, dir_debug, S_IWUSR);
 +	DEBUGFS_ADD_FILE(echo_test, dir_debug, S_IWUSR);
++#ifdef CONFIG_IWLWIFI_DEBUG
 +	DEBUGFS_ADD_FILE(log_event, dir_debug, S_IWUSR | S_IRUSR);
++#endif
 +
 +	if (iwl_advanced_bt_coexist(priv))
 +		DEBUGFS_ADD_FILE(bt_traffic, dir_debug, S_IRUSR);
 +
 +	/* Calibrations disabled/enabled status*/
 +	DEBUGFS_ADD_FILE(calib_disabled, dir_rf, S_IWUSR | S_IRUSR);
 +
 +	if (iwl_trans_dbgfs_register(priv->trans, dir_debug))
 +		goto err;
 +	return 0;
 +
 +err:
 +	IWL_ERR(priv, "Can't create the debugfs directory\n");
 +	iwl_dbgfs_unregister(priv);
 +	return -ENOMEM;
 +}
 +
 +/**
 + * Remove the debugfs files and directories
 + *
 + */
 +void iwl_dbgfs_unregister(struct iwl_priv *priv)
 +{
 +	if (!priv->debugfs_dir)
 +		return;
 +
 +	debugfs_remove_recursive(priv->debugfs_dir);
 +	priv->debugfs_dir = NULL;
 +}
diff --cc drivers/net/wireless/iwlwifi/dvm/mac80211.c
index b83ca358eb18,000000000000..a5f7bce96325
mode 100644,000000..100644
--- a/drivers/net/wireless/iwlwifi/dvm/mac80211.c
+++ b/drivers/net/wireless/iwlwifi/dvm/mac80211.c
@@@ -1,1640 -1,0 +1,1652 @@@
 +/******************************************************************************
 + *
 + * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
 + *
 + * Portions of this file are derived from the ipw3945 project, as well
 + * as portions of the ieee80211 subsystem header files.
 + *
 + * This program is free software; you can redistribute it and/or modify it
 + * under the terms of version 2 of the GNU General Public License as
 + * published by the Free Software Foundation.
 + *
 + * This program is distributed in the hope that it will be useful, but WITHOUT
 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 + * more details.
 + *
 + * You should have received a copy of the GNU General Public License along with
 + * this program; if not, write to the Free Software Foundation, Inc.,
 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 + *
 + * The full GNU General Public License is included in this distribution in the
 + * file called LICENSE.
 + *
 + * Contact Information:
 + *  Intel Linux Wireless <ilw@linux.intel.com>
 + * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 + *
 + *****************************************************************************/
 +#include <linux/kernel.h>
 +#include <linux/module.h>
 +#include <linux/init.h>
 +#include <linux/slab.h>
 +#include <linux/dma-mapping.h>
 +#include <linux/delay.h>
 +#include <linux/sched.h>
 +#include <linux/skbuff.h>
 +#include <linux/netdevice.h>
 +#include <linux/etherdevice.h>
 +#include <linux/if_arp.h>
 +
 +#include <net/ieee80211_radiotap.h>
 +#include <net/mac80211.h>
 +
 +#include <asm/div64.h>
 +
 +#include "iwl-io.h"
 +#include "iwl-trans.h"
 +#include "iwl-op-mode.h"
 +#include "iwl-modparams.h"
 +
 +#include "dev.h"
 +#include "calib.h"
 +#include "agn.h"
 +
 +/*****************************************************************************
 + *
 + * mac80211 entry point functions
 + *
 + *****************************************************************************/
 +
 +static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
 +	{
 +		.max = 1,
 +		.types = BIT(NL80211_IFTYPE_STATION),
 +	},
 +	{
 +		.max = 1,
 +		.types = BIT(NL80211_IFTYPE_AP),
 +	},
 +};
 +
 +static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
 +	{
 +		.max = 2,
 +		.types = BIT(NL80211_IFTYPE_STATION),
 +	},
 +};
 +
 +static const struct ieee80211_iface_limit iwlagn_p2p_sta_go_limits[] = {
 +	{
 +		.max = 1,
 +		.types = BIT(NL80211_IFTYPE_STATION),
 +	},
 +	{
 +		.max = 1,
 +		.types = BIT(NL80211_IFTYPE_P2P_GO) |
 +			 BIT(NL80211_IFTYPE_AP),
 +	},
 +};
 +
 +static const struct ieee80211_iface_limit iwlagn_p2p_2sta_limits[] = {
 +	{
 +		.max = 2,
 +		.types = BIT(NL80211_IFTYPE_STATION),
 +	},
 +	{
 +		.max = 1,
 +		.types = BIT(NL80211_IFTYPE_P2P_CLIENT),
 +	},
 +};
 +
 +static const struct ieee80211_iface_combination
 +iwlagn_iface_combinations_dualmode[] = {
 +	{ .num_different_channels = 1,
 +	  .max_interfaces = 2,
 +	  .beacon_int_infra_match = true,
 +	  .limits = iwlagn_sta_ap_limits,
 +	  .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
 +	},
 +	{ .num_different_channels = 1,
 +	  .max_interfaces = 2,
 +	  .limits = iwlagn_2sta_limits,
 +	  .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
 +	},
 +};
 +
 +static const struct ieee80211_iface_combination
 +iwlagn_iface_combinations_p2p[] = {
 +	{ .num_different_channels = 1,
 +	  .max_interfaces = 2,
 +	  .beacon_int_infra_match = true,
 +	  .limits = iwlagn_p2p_sta_go_limits,
 +	  .n_limits = ARRAY_SIZE(iwlagn_p2p_sta_go_limits),
 +	},
 +	{ .num_different_channels = 1,
 +	  .max_interfaces = 2,
 +	  .limits = iwlagn_p2p_2sta_limits,
 +	  .n_limits = ARRAY_SIZE(iwlagn_p2p_2sta_limits),
 +	},
 +};
 +
 +/*
 + * Not a mac80211 entry point function, but it fits in with all the
 + * other mac80211 functions grouped here.
 + */
 +int iwlagn_mac_setup_register(struct iwl_priv *priv,
 +			      const struct iwl_ucode_capabilities *capa)
 +{
 +	int ret;
 +	struct ieee80211_hw *hw = priv->hw;
 +	struct iwl_rxon_context *ctx;
 +
 +	hw->rate_control_algorithm = "iwl-agn-rs";
 +
 +	/* Tell mac80211 our characteristics */
 +	hw->flags = IEEE80211_HW_SIGNAL_DBM |
 +		    IEEE80211_HW_AMPDU_AGGREGATION |
 +		    IEEE80211_HW_NEED_DTIM_PERIOD |
 +		    IEEE80211_HW_SPECTRUM_MGMT |
 +		    IEEE80211_HW_REPORTS_TX_ACK_STATUS |
 +		    IEEE80211_HW_QUEUE_CONTROL |
 +		    IEEE80211_HW_SUPPORTS_PS |
 +		    IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
 +		    IEEE80211_HW_WANT_MONITOR_VIF |
 +		    IEEE80211_HW_SCAN_WHILE_IDLE;
 +
 +	hw->offchannel_tx_hw_queue = IWL_AUX_QUEUE;
 +	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FMT;
 +
 +	/*
 +	 * Including the following line will crash some AP's.  This
 +	 * workaround removes the stimulus which causes the crash until
 +	 * the AP software can be fixed.
 +	hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
 +	 */
 +
 +	if (priv->eeprom_data->sku & EEPROM_SKU_CAP_11N_ENABLE)
 +		hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
 +			     IEEE80211_HW_SUPPORTS_STATIC_SMPS;
 +
 +#ifndef CONFIG_IWLWIFI_EXPERIMENTAL_MFP
 +	/* enable 11w if the uCode advertise */
 +	if (capa->flags & IWL_UCODE_TLV_FLAGS_MFP)
 +#endif /* !CONFIG_IWLWIFI_EXPERIMENTAL_MFP */
 +		hw->flags |= IEEE80211_HW_MFP_CAPABLE;
 +
 +	hw->sta_data_size = sizeof(struct iwl_station_priv);
 +	hw->vif_data_size = sizeof(struct iwl_vif_priv);
 +
 +	for_each_context(priv, ctx) {
 +		hw->wiphy->interface_modes |= ctx->interface_modes;
 +		hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
 +	}
 +
 +	BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
 +
 +	if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)) {
 +		hw->wiphy->iface_combinations = iwlagn_iface_combinations_p2p;
 +		hw->wiphy->n_iface_combinations =
 +			ARRAY_SIZE(iwlagn_iface_combinations_p2p);
 +	} else if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
 +		hw->wiphy->iface_combinations =
 +			iwlagn_iface_combinations_dualmode;
 +		hw->wiphy->n_iface_combinations =
 +			ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
 +	}
 +
 +	hw->wiphy->max_remain_on_channel_duration = 1000;
 +
 +	hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
 +			    WIPHY_FLAG_DISABLE_BEACON_HINTS |
 +			    WIPHY_FLAG_IBSS_RSN;
 +
 +#ifdef CONFIG_PM_SLEEP
 +	if (priv->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
 +	    priv->trans->ops->wowlan_suspend &&
 +	    device_can_wakeup(priv->trans->dev)) {
 +		hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
 +					  WIPHY_WOWLAN_DISCONNECT |
 +					  WIPHY_WOWLAN_EAP_IDENTITY_REQ |
 +					  WIPHY_WOWLAN_RFKILL_RELEASE;
 +		if (!iwlwifi_mod_params.sw_crypto)
 +			hw->wiphy->wowlan.flags |=
 +				WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
 +				WIPHY_WOWLAN_GTK_REKEY_FAILURE;
 +
 +		hw->wiphy->wowlan.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
 +		hw->wiphy->wowlan.pattern_min_len =
 +					IWLAGN_WOWLAN_MIN_PATTERN_LEN;
 +		hw->wiphy->wowlan.pattern_max_len =
 +					IWLAGN_WOWLAN_MAX_PATTERN_LEN;
 +	}
 +#endif
 +
 +	if (iwlwifi_mod_params.power_save)
 +		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
 +	else
 +		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
 +
 +	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
 +	/* we create the 802.11 header and a max-length SSID element */
 +	hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 34;
 +
 +	/*
 +	 * We don't use all queues: 4 and 9 are unused and any
 +	 * aggregation queue gets mapped down to the AC queue.
 +	 */
 +	hw->queues = IWLAGN_FIRST_AMPDU_QUEUE;
 +
 +	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
 +
 +	if (priv->eeprom_data->bands[IEEE80211_BAND_2GHZ].n_channels)
 +		priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
 +			&priv->eeprom_data->bands[IEEE80211_BAND_2GHZ];
 +	if (priv->eeprom_data->bands[IEEE80211_BAND_5GHZ].n_channels)
 +		priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
 +			&priv->eeprom_data->bands[IEEE80211_BAND_5GHZ];
 +
 +	hw->wiphy->hw_version = priv->trans->hw_id;
 +
 +	iwl_leds_init(priv);
 +
 +	ret = ieee80211_register_hw(priv->hw);
 +	if (ret) {
 +		IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
 +		iwl_leds_exit(priv);
 +		return ret;
 +	}
 +	priv->mac80211_registered = 1;
 +
 +	return 0;
 +}
 +
 +void iwlagn_mac_unregister(struct iwl_priv *priv)
 +{
 +	if (!priv->mac80211_registered)
 +		return;
 +	iwl_leds_exit(priv);
 +	ieee80211_unregister_hw(priv->hw);
 +	priv->mac80211_registered = 0;
 +}
 +
 +static int __iwl_up(struct iwl_priv *priv)
 +{
 +	struct iwl_rxon_context *ctx;
 +	int ret;
 +
 +	lockdep_assert_held(&priv->mutex);
 +
 +	if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
 +		IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
 +		return -EIO;
 +	}
 +
 +	for_each_context(priv, ctx) {
 +		ret = iwlagn_alloc_bcast_station(priv, ctx);
 +		if (ret) {
 +			iwl_dealloc_bcast_stations(priv);
 +			return ret;
 +		}
 +	}
 +
 +	ret = iwl_run_init_ucode(priv);
 +	if (ret) {
 +		IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
 +		goto error;
 +	}
 +
 +	ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
 +	if (ret) {
 +		IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
 +		goto error;
 +	}
 +
 +	ret = iwl_alive_start(priv);
 +	if (ret)
 +		goto error;
 +	return 0;
 +
 + error:
 +	set_bit(STATUS_EXIT_PENDING, &priv->status);
 +	iwl_down(priv);
 +	clear_bit(STATUS_EXIT_PENDING, &priv->status);
 +
 +	IWL_ERR(priv, "Unable to initialize device.\n");
 +	return ret;
 +}
 +
 +static int iwlagn_mac_start(struct ieee80211_hw *hw)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	int ret;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +
 +	/* we should be verifying the device is ready to be opened */
 +	mutex_lock(&priv->mutex);
 +	ret = __iwl_up(priv);
 +	mutex_unlock(&priv->mutex);
 +	if (ret)
 +		return ret;
 +
 +	IWL_DEBUG_INFO(priv, "Start UP work done.\n");
 +
 +	/* Now we should be done, and the READY bit should be set. */
 +	if (WARN_ON(!test_bit(STATUS_READY, &priv->status)))
 +		ret = -EIO;
 +
 +	iwlagn_led_enable(priv);
 +
 +	priv->is_open = 1;
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +	return 0;
 +}
 +
 +static void iwlagn_mac_stop(struct ieee80211_hw *hw)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +
 +	if (!priv->is_open)
 +		return;
 +
 +	priv->is_open = 0;
 +
 +	mutex_lock(&priv->mutex);
 +	iwl_down(priv);
 +	mutex_unlock(&priv->mutex);
 +
 +	iwl_cancel_deferred_work(priv);
 +
 +	flush_workqueue(priv->workqueue);
 +
 +	/* User space software may expect getting rfkill changes
 +	 * even if interface is down, trans->down will leave the RF
 +	 * kill interrupt enabled
 +	 */
 +	iwl_trans_stop_hw(priv->trans, false);
 +
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +}
 +
 +static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
 +				      struct ieee80211_vif *vif,
 +				      struct cfg80211_gtk_rekey_data *data)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +
 +	if (iwlwifi_mod_params.sw_crypto)
 +		return;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +	mutex_lock(&priv->mutex);
 +
 +	if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
 +		goto out;
 +
 +	memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
 +	memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
 +	priv->replay_ctr =
 +		cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
 +	priv->have_rekey_data = true;
 +
 + out:
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +}
 +
 +#ifdef CONFIG_PM_SLEEP
 +
 +static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
 +			      struct cfg80211_wowlan *wowlan)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
 +	int ret;
 +
 +	if (WARN_ON(!wowlan))
 +		return -EINVAL;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +	mutex_lock(&priv->mutex);
 +
 +	/* Don't attempt WoWLAN when not associated, tear down instead. */
 +	if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
 +	    !iwl_is_associated_ctx(ctx)) {
 +		ret = 1;
 +		goto out;
 +	}
 +
 +	ret = iwlagn_suspend(priv, wowlan);
 +	if (ret)
 +		goto error;
 +
 +	iwl_trans_wowlan_suspend(priv->trans);
 +
 +	goto out;
 +
 + error:
 +	priv->wowlan = false;
 +	iwlagn_prepare_restart(priv);
 +	ieee80211_restart_hw(priv->hw);
 + out:
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +
 +	return ret;
 +}
 +
 +static int iwlagn_mac_resume(struct ieee80211_hw *hw)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
 +	struct ieee80211_vif *vif;
 +	unsigned long flags;
 +	u32 base, status = 0xffffffff;
 +	int ret = -EIO;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +	mutex_lock(&priv->mutex);
 +
 +	iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
 +			  CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
 +
 +	base = priv->device_pointers.error_event_table;
 +	if (iwlagn_hw_valid_rtc_data_addr(base)) {
 +		spin_lock_irqsave(&priv->trans->reg_lock, flags);
 +		ret = iwl_grab_nic_access_silent(priv->trans);
 +		if (likely(ret == 0)) {
 +			iwl_write32(priv->trans, HBUS_TARG_MEM_RADDR, base);
 +			status = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
 +			iwl_release_nic_access(priv->trans);
 +		}
 +		spin_unlock_irqrestore(&priv->trans->reg_lock, flags);
 +
 +#ifdef CONFIG_IWLWIFI_DEBUGFS
 +		if (ret == 0) {
 +			const struct fw_img *img;
 +
 +			img = &(priv->fw->img[IWL_UCODE_WOWLAN]);
 +			if (!priv->wowlan_sram) {
 +				priv->wowlan_sram =
 +				   kzalloc(img->sec[IWL_UCODE_SECTION_DATA].len,
 +						GFP_KERNEL);
 +			}
 +
 +			if (priv->wowlan_sram)
 +				_iwl_read_targ_mem_dwords(
 +				      priv->trans, 0x800000,
 +				      priv->wowlan_sram,
 +				      img->sec[IWL_UCODE_SECTION_DATA].len / 4);
 +		}
 +#endif
 +	}
 +
 +	/* we'll clear ctx->vif during iwlagn_prepare_restart() */
 +	vif = ctx->vif;
 +
 +	priv->wowlan = false;
 +
 +	iwlagn_prepare_restart(priv);
 +
 +	memset((void *)&ctx->active, 0, sizeof(ctx->active));
 +	iwl_connection_init_rx_config(priv, ctx);
 +	iwlagn_set_rxon_chain(priv, ctx);
 +
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +
 +	ieee80211_resume_disconnect(vif);
 +
 +	return 1;
 +}
 +
 +static void iwlagn_mac_set_wakeup(struct ieee80211_hw *hw, bool enabled)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +
 +	device_set_wakeup_enable(priv->trans->dev, enabled);
 +}
 +#endif
 +
 +static void iwlagn_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +
 +	IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
 +		     ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
 +
 +	if (iwlagn_tx_skb(priv, skb))
 +		dev_kfree_skb_any(skb);
 +}
 +
 +static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
 +				       struct ieee80211_vif *vif,
 +				       struct ieee80211_key_conf *keyconf,
 +				       struct ieee80211_sta *sta,
 +				       u32 iv32, u16 *phase1key)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +
 +	iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
 +}
 +
 +static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
 +			      struct ieee80211_vif *vif,
 +			      struct ieee80211_sta *sta,
 +			      struct ieee80211_key_conf *key)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
 +	struct iwl_rxon_context *ctx = vif_priv->ctx;
 +	int ret;
 +	bool is_default_wep_key = false;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +
 +	if (iwlwifi_mod_params.sw_crypto) {
 +		IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
 +		return -EOPNOTSUPP;
 +	}
 +
 +	switch (key->cipher) {
 +	case WLAN_CIPHER_SUITE_TKIP:
 +		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
 +		/* fall through */
 +	case WLAN_CIPHER_SUITE_CCMP:
 +		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
 +		break;
 +	default:
 +		break;
 +	}
 +
 +	/*
 +	 * We could program these keys into the hardware as well, but we
 +	 * don't expect much multicast traffic in IBSS and having keys
 +	 * for more stations is probably more useful.
 +	 *
 +	 * Mark key TX-only and return 0.
 +	 */
 +	if (vif->type == NL80211_IFTYPE_ADHOC &&
 +	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
 +		key->hw_key_idx = WEP_INVALID_OFFSET;
 +		return 0;
 +	}
 +
 +	/* If they key was TX-only, accept deletion */
 +	if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
 +		return 0;
 +
 +	mutex_lock(&priv->mutex);
 +	iwl_scan_cancel_timeout(priv, 100);
 +
 +	BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
 +
 +	/*
 +	 * If we are getting WEP group key and we didn't receive any key mapping
 +	 * so far, we are in legacy wep mode (group key only), otherwise we are
 +	 * in 1X mode.
 +	 * In legacy wep mode, we use another host command to the uCode.
 +	 */
 +	if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
 +	     key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
 +		if (cmd == SET_KEY)
 +			is_default_wep_key = !ctx->key_mapping_keys;
 +		else
 +			is_default_wep_key =
 +				key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
 +	}
 +
 +
 +	switch (cmd) {
 +	case SET_KEY:
 +		if (is_default_wep_key) {
 +			ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
 +			break;
 +		}
 +		ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
 +		if (ret) {
 +			/*
 +			 * can't add key for RX, but we don't need it
 +			 * in the device for TX so still return 0
 +			 */
 +			ret = 0;
 +			key->hw_key_idx = WEP_INVALID_OFFSET;
 +		}
 +
 +		IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
 +		break;
 +	case DISABLE_KEY:
 +		if (is_default_wep_key)
 +			ret = iwl_remove_default_wep_key(priv, ctx, key);
 +		else
 +			ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
 +
 +		IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
 +		break;
 +	default:
 +		ret = -EINVAL;
 +	}
 +
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +
 +	return ret;
 +}
 +
 +static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
 +				   struct ieee80211_vif *vif,
 +				   enum ieee80211_ampdu_mlme_action action,
 +				   struct ieee80211_sta *sta, u16 tid, u16 *ssn,
 +				   u8 buf_size)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	int ret = -EINVAL;
 +	struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
 +
 +	IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
 +		     sta->addr, tid);
 +
 +	if (!(priv->eeprom_data->sku & EEPROM_SKU_CAP_11N_ENABLE))
 +		return -EACCES;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +	mutex_lock(&priv->mutex);
 +
 +	switch (action) {
 +	case IEEE80211_AMPDU_RX_START:
 +		if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
 +			break;
 +		IWL_DEBUG_HT(priv, "start Rx\n");
 +		ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
 +		break;
 +	case IEEE80211_AMPDU_RX_STOP:
 +		IWL_DEBUG_HT(priv, "stop Rx\n");
 +		ret = iwl_sta_rx_agg_stop(priv, sta, tid);
 +		break;
 +	case IEEE80211_AMPDU_TX_START:
 +		if (!priv->trans->ops->txq_enable)
 +			break;
 +		if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
 +			break;
 +		IWL_DEBUG_HT(priv, "start Tx\n");
 +		ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
 +		break;
 +	case IEEE80211_AMPDU_TX_STOP:
 +		IWL_DEBUG_HT(priv, "stop Tx\n");
 +		ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
 +		if ((ret == 0) && (priv->agg_tids_count > 0)) {
 +			priv->agg_tids_count--;
 +			IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
 +				     priv->agg_tids_count);
 +		}
 +		if (!priv->agg_tids_count &&
 +		    priv->hw_params.use_rts_for_aggregation) {
 +			/*
 +			 * switch off RTS/CTS if it was previously enabled
 +			 */
 +			sta_priv->lq_sta.lq.general_params.flags &=
 +				~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
 +			iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
 +					&sta_priv->lq_sta.lq, CMD_ASYNC, false);
 +		}
 +		break;
 +	case IEEE80211_AMPDU_TX_OPERATIONAL:
 +		ret = iwlagn_tx_agg_oper(priv, vif, sta, tid, buf_size);
 +		break;
 +	}
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +	return ret;
 +}
 +
 +static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
 +			      struct ieee80211_vif *vif,
 +			      struct ieee80211_sta *sta)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
 +	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
 +	bool is_ap = vif->type == NL80211_IFTYPE_STATION;
 +	int ret;
 +	u8 sta_id;
 +
 +	IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
 +			sta->addr);
 +	sta_priv->sta_id = IWL_INVALID_STATION;
 +
 +	atomic_set(&sta_priv->pending_frames, 0);
 +	if (vif->type == NL80211_IFTYPE_AP)
 +		sta_priv->client = true;
 +
 +	ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
 +				     is_ap, sta, &sta_id);
 +	if (ret) {
 +		IWL_ERR(priv, "Unable to add station %pM (%d)\n",
 +			sta->addr, ret);
 +		/* Should we return success if return code is EEXIST ? */
 +		return ret;
 +	}
 +
 +	sta_priv->sta_id = sta_id;
 +
 +	return 0;
 +}
 +
 +static int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
 +				 struct ieee80211_vif *vif,
 +				 struct ieee80211_sta *sta)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
 +	int ret;
 +
 +	IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n", sta->addr);
 +
 +	if (vif->type == NL80211_IFTYPE_STATION) {
 +		/*
 +		 * Station will be removed from device when the RXON
 +		 * is set to unassociated -- just deactivate it here
 +		 * to avoid re-programming it.
 +		 */
 +		ret = 0;
 +		iwl_deactivate_station(priv, sta_priv->sta_id, sta->addr);
 +	} else {
 +		ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
 +		if (ret)
 +			IWL_DEBUG_QUIET_RFKILL(priv,
 +				"Error removing station %pM\n", sta->addr);
 +	}
 +	return ret;
 +}
 +
 +static int iwlagn_mac_sta_state(struct ieee80211_hw *hw,
 +				struct ieee80211_vif *vif,
 +				struct ieee80211_sta *sta,
 +				enum ieee80211_sta_state old_state,
 +				enum ieee80211_sta_state new_state)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
 +	enum {
 +		NONE, ADD, REMOVE, HT_RATE_INIT, ADD_RATE_INIT,
 +	} op = NONE;
 +	int ret;
 +
 +	IWL_DEBUG_MAC80211(priv, "station %pM state change %d->%d\n",
 +			   sta->addr, old_state, new_state);
 +
 +	mutex_lock(&priv->mutex);
 +	if (vif->type == NL80211_IFTYPE_STATION) {
 +		if (old_state == IEEE80211_STA_NOTEXIST &&
 +		    new_state == IEEE80211_STA_NONE)
 +			op = ADD;
 +		else if (old_state == IEEE80211_STA_NONE &&
 +			 new_state == IEEE80211_STA_NOTEXIST)
 +			op = REMOVE;
 +		else if (old_state == IEEE80211_STA_AUTH &&
 +			 new_state == IEEE80211_STA_ASSOC)
 +			op = HT_RATE_INIT;
 +	} else {
 +		if (old_state == IEEE80211_STA_AUTH &&
 +		    new_state == IEEE80211_STA_ASSOC)
 +			op = ADD_RATE_INIT;
 +		else if (old_state == IEEE80211_STA_ASSOC &&
 +			 new_state == IEEE80211_STA_AUTH)
 +			op = REMOVE;
 +	}
 +
 +	switch (op) {
 +	case ADD:
 +		ret = iwlagn_mac_sta_add(hw, vif, sta);
++		if (ret)
++			break;
++		/*
++		 * Clear the in-progress flag, the AP station entry was added
++		 * but we'll initialize LQ only when we've associated (which
++		 * would also clear the in-progress flag). This is necessary
++		 * in case we never initialize LQ because association fails.
++		 */
++		spin_lock_bh(&priv->sta_lock);
++		priv->stations[iwl_sta_id(sta)].used &=
++			~IWL_STA_UCODE_INPROGRESS;
++		spin_unlock_bh(&priv->sta_lock);
 +		break;
 +	case REMOVE:
 +		ret = iwlagn_mac_sta_remove(hw, vif, sta);
 +		break;
 +	case ADD_RATE_INIT:
 +		ret = iwlagn_mac_sta_add(hw, vif, sta);
 +		if (ret)
 +			break;
 +		/* Initialize rate scaling */
 +		IWL_DEBUG_INFO(priv,
 +			       "Initializing rate scaling for station %pM\n",
 +			       sta->addr);
 +		iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
 +		ret = 0;
 +		break;
 +	case HT_RATE_INIT:
 +		/* Initialize rate scaling */
 +		ret = iwl_sta_update_ht(priv, vif_priv->ctx, sta);
 +		if (ret)
 +			break;
 +		IWL_DEBUG_INFO(priv,
 +			       "Initializing rate scaling for station %pM\n",
 +			       sta->addr);
 +		iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
 +		ret = 0;
 +		break;
 +	default:
 +		ret = 0;
 +		break;
 +	}
 +
 +	/*
 +	 * mac80211 might WARN if we fail, but due the way we
 +	 * (badly) handle hard rfkill, we might fail here
 +	 */
 +	if (iwl_is_rfkill(priv))
 +		ret = 0;
 +
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +
 +	return ret;
 +}
 +
 +static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
 +				      struct ieee80211_channel_switch *ch_switch)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct ieee80211_conf *conf = &hw->conf;
 +	struct ieee80211_channel *channel = ch_switch->channel;
 +	struct iwl_ht_config *ht_conf = &priv->current_ht_config;
 +	/*
 +	 * MULTI-FIXME
 +	 * When we add support for multiple interfaces, we need to
 +	 * revisit this. The channel switch command in the device
 +	 * only affects the BSS context, but what does that really
 +	 * mean? And what if we get a CSA on the second interface?
 +	 * This needs a lot of work.
 +	 */
 +	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
 +	u16 ch;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +
 +	mutex_lock(&priv->mutex);
 +
 +	if (iwl_is_rfkill(priv))
 +		goto out;
 +
 +	if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
 +	    test_bit(STATUS_SCANNING, &priv->status) ||
 +	    test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
 +		goto out;
 +
 +	if (!iwl_is_associated_ctx(ctx))
 +		goto out;
 +
 +	if (!priv->lib->set_channel_switch)
 +		goto out;
 +
 +	ch = channel->hw_value;
 +	if (le16_to_cpu(ctx->active.channel) == ch)
 +		goto out;
 +
 +	priv->current_ht_config.smps = conf->smps_mode;
 +
 +	/* Configure HT40 channels */
 +	ctx->ht.enabled = conf_is_ht(conf);
 +	if (ctx->ht.enabled)
 +		iwlagn_config_ht40(conf, ctx);
 +	else
 +		ctx->ht.is_40mhz = false;
 +
 +	if ((le16_to_cpu(ctx->staging.channel) != ch))
 +		ctx->staging.flags = 0;
 +
 +	iwl_set_rxon_channel(priv, channel, ctx);
 +	iwl_set_rxon_ht(priv, ht_conf);
 +	iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
 +
 +	/*
 +	 * at this point, staging_rxon has the
 +	 * configuration for channel switch
 +	 */
 +	set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
 +	priv->switch_channel = cpu_to_le16(ch);
 +	if (priv->lib->set_channel_switch(priv, ch_switch)) {
 +		clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
 +		priv->switch_channel = 0;
 +		ieee80211_chswitch_done(ctx->vif, false);
 +	}
 +
 +out:
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +}
 +
 +void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
 +{
 +	/*
 +	 * MULTI-FIXME
 +	 * See iwlagn_mac_channel_switch.
 +	 */
 +	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
 +
 +	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 +		return;
 +
 +	if (test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
 +		ieee80211_chswitch_done(ctx->vif, is_success);
 +}
 +
 +static void iwlagn_configure_filter(struct ieee80211_hw *hw,
 +				    unsigned int changed_flags,
 +				    unsigned int *total_flags,
 +				    u64 multicast)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	__le32 filter_or = 0, filter_nand = 0;
 +	struct iwl_rxon_context *ctx;
 +
 +#define CHK(test, flag)	do { \
 +	if (*total_flags & (test))		\
 +		filter_or |= (flag);		\
 +	else					\
 +		filter_nand |= (flag);		\
 +	} while (0)
 +
 +	IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
 +			changed_flags, *total_flags);
 +
 +	CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
 +	/* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
 +	CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
 +	CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
 +
 +#undef CHK
 +
 +	mutex_lock(&priv->mutex);
 +
 +	for_each_context(priv, ctx) {
 +		ctx->staging.filter_flags &= ~filter_nand;
 +		ctx->staging.filter_flags |= filter_or;
 +
 +		/*
 +		 * Not committing directly because hardware can perform a scan,
 +		 * but we'll eventually commit the filter flags change anyway.
 +		 */
 +	}
 +
 +	mutex_unlock(&priv->mutex);
 +
 +	/*
 +	 * Receiving all multicast frames is always enabled by the
 +	 * default flags setup in iwl_connection_init_rx_config()
 +	 * since we currently do not support programming multicast
 +	 * filters into the device.
 +	 */
 +	*total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
 +			FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
 +}
 +
 +static void iwlagn_mac_flush(struct ieee80211_hw *hw, bool drop)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +
 +	mutex_lock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +
 +	if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
 +		IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
 +		goto done;
 +	}
 +	if (iwl_is_rfkill(priv)) {
 +		IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
 +		goto done;
 +	}
 +
 +	/*
 +	 * mac80211 will not push any more frames for transmit
 +	 * until the flush is completed
 +	 */
 +	if (drop) {
 +		IWL_DEBUG_MAC80211(priv, "send flush command\n");
 +		if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
 +			IWL_ERR(priv, "flush request fail\n");
 +			goto done;
 +		}
 +	}
 +	IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
 +	iwl_trans_wait_tx_queue_empty(priv->trans);
 +done:
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +}
 +
 +static int iwlagn_mac_remain_on_channel(struct ieee80211_hw *hw,
 +				     struct ieee80211_channel *channel,
 +				     enum nl80211_channel_type channel_type,
 +				     int duration)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
 +	int err = 0;
 +
 +	if (!(priv->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
 +		return -EOPNOTSUPP;
 +
 +	if (!(ctx->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)))
 +		return -EOPNOTSUPP;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +	mutex_lock(&priv->mutex);
 +
 +	if (test_bit(STATUS_SCAN_HW, &priv->status)) {
 +		/* mac80211 should not scan while ROC or ROC while scanning */
 +		if (WARN_ON_ONCE(priv->scan_type != IWL_SCAN_RADIO_RESET)) {
 +			err = -EBUSY;
 +			goto out;
 +		}
 +
 +		iwl_scan_cancel_timeout(priv, 100);
 +
 +		if (test_bit(STATUS_SCAN_HW, &priv->status)) {
 +			err = -EBUSY;
 +			goto out;
 +		}
 +	}
 +
 +	priv->hw_roc_channel = channel;
 +	priv->hw_roc_chantype = channel_type;
 +	/* convert from ms to TU */
 +	priv->hw_roc_duration = DIV_ROUND_UP(1000 * duration, 1024);
 +	priv->hw_roc_start_notified = false;
 +	cancel_delayed_work(&priv->hw_roc_disable_work);
 +
 +	if (!ctx->is_active) {
 +		static const struct iwl_qos_info default_qos_data = {
 +			.def_qos_parm = {
 +				.ac[0] = {
 +					.cw_min = cpu_to_le16(3),
 +					.cw_max = cpu_to_le16(7),
 +					.aifsn = 2,
 +					.edca_txop = cpu_to_le16(1504),
 +				},
 +				.ac[1] = {
 +					.cw_min = cpu_to_le16(7),
 +					.cw_max = cpu_to_le16(15),
 +					.aifsn = 2,
 +					.edca_txop = cpu_to_le16(3008),
 +				},
 +				.ac[2] = {
 +					.cw_min = cpu_to_le16(15),
 +					.cw_max = cpu_to_le16(1023),
 +					.aifsn = 3,
 +				},
 +				.ac[3] = {
 +					.cw_min = cpu_to_le16(15),
 +					.cw_max = cpu_to_le16(1023),
 +					.aifsn = 7,
 +				},
 +			},
 +		};
 +
 +		ctx->is_active = true;
 +		ctx->qos_data = default_qos_data;
 +		ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
 +		memcpy(ctx->staging.node_addr,
 +		       priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
 +		       ETH_ALEN);
 +		memcpy(ctx->staging.bssid_addr,
 +		       priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
 +		       ETH_ALEN);
 +		err = iwlagn_commit_rxon(priv, ctx);
 +		if (err)
 +			goto out;
 +		ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
 +					     RXON_FILTER_PROMISC_MSK |
 +					     RXON_FILTER_CTL2HOST_MSK;
 +
 +		err = iwlagn_commit_rxon(priv, ctx);
 +		if (err) {
 +			iwlagn_disable_roc(priv);
 +			goto out;
 +		}
 +		priv->hw_roc_setup = true;
 +	}
 +
 +	err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
 +	if (err)
 +		iwlagn_disable_roc(priv);
 +
 + out:
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +
 +	return err;
 +}
 +
 +static int iwlagn_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +
 +	if (!(priv->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
 +		return -EOPNOTSUPP;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +	mutex_lock(&priv->mutex);
 +	iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
 +	iwlagn_disable_roc(priv);
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +
 +	return 0;
 +}
 +
 +static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
 +				     enum ieee80211_rssi_event rssi_event)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +	mutex_lock(&priv->mutex);
 +
 +	if (priv->cfg->bt_params &&
 +			priv->cfg->bt_params->advanced_bt_coexist) {
 +		if (rssi_event == RSSI_EVENT_LOW)
 +			priv->bt_enable_pspoll = true;
 +		else if (rssi_event == RSSI_EVENT_HIGH)
 +			priv->bt_enable_pspoll = false;
 +
 +		iwlagn_send_advance_bt_config(priv);
 +	} else {
 +		IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
 +				"ignoring RSSI callback\n");
 +	}
 +
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +}
 +
 +static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
 +			      struct ieee80211_sta *sta, bool set)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +
 +	queue_work(priv->workqueue, &priv->beacon_update);
 +
 +	return 0;
 +}
 +
 +static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
 +			      struct ieee80211_vif *vif, u16 queue,
 +			      const struct ieee80211_tx_queue_params *params)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
 +	struct iwl_rxon_context *ctx = vif_priv->ctx;
 +	int q;
 +
 +	if (WARN_ON(!ctx))
 +		return -EINVAL;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +
 +	if (!iwl_is_ready_rf(priv)) {
 +		IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
 +		return -EIO;
 +	}
 +
 +	if (queue >= AC_NUM) {
 +		IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
 +		return 0;
 +	}
 +
 +	q = AC_NUM - 1 - queue;
 +
 +	mutex_lock(&priv->mutex);
 +
 +	ctx->qos_data.def_qos_parm.ac[q].cw_min =
 +		cpu_to_le16(params->cw_min);
 +	ctx->qos_data.def_qos_parm.ac[q].cw_max =
 +		cpu_to_le16(params->cw_max);
 +	ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
 +	ctx->qos_data.def_qos_parm.ac[q].edca_txop =
 +			cpu_to_le16((params->txop * 32));
 +
 +	ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
 +
 +	mutex_unlock(&priv->mutex);
 +
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +	return 0;
 +}
 +
 +static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +
 +	return priv->ibss_manager == IWL_IBSS_MANAGER;
 +}
 +
 +static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
 +{
 +	iwl_connection_init_rx_config(priv, ctx);
 +
 +	iwlagn_set_rxon_chain(priv, ctx);
 +
 +	return iwlagn_commit_rxon(priv, ctx);
 +}
 +
 +static int iwl_setup_interface(struct iwl_priv *priv,
 +			       struct iwl_rxon_context *ctx)
 +{
 +	struct ieee80211_vif *vif = ctx->vif;
 +	int err, ac;
 +
 +	lockdep_assert_held(&priv->mutex);
 +
 +	/*
 +	 * This variable will be correct only when there's just
 +	 * a single context, but all code using it is for hardware
 +	 * that supports only one context.
 +	 */
 +	priv->iw_mode = vif->type;
 +
 +	ctx->is_active = true;
 +
 +	err = iwl_set_mode(priv, ctx);
 +	if (err) {
 +		if (!ctx->always_active)
 +			ctx->is_active = false;
 +		return err;
 +	}
 +
 +	if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
 +	    vif->type == NL80211_IFTYPE_ADHOC) {
 +		/*
 +		 * pretend to have high BT traffic as long as we
 +		 * are operating in IBSS mode, as this will cause
 +		 * the rate scaling etc. to behave as intended.
 +		 */
 +		priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
 +	}
 +
 +	/* set up queue mappings */
 +	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
 +		vif->hw_queue[ac] = ctx->ac_to_queue[ac];
 +
 +	if (vif->type == NL80211_IFTYPE_AP)
 +		vif->cab_queue = ctx->mcast_queue;
 +	else
 +		vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
 +
 +	return 0;
 +}
 +
 +static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
 +				    struct ieee80211_vif *vif)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
 +	struct iwl_rxon_context *tmp, *ctx = NULL;
 +	int err;
 +	enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
 +	bool reset = false;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
 +			   viftype, vif->addr);
 +
 +	cancel_delayed_work_sync(&priv->hw_roc_disable_work);
 +
 +	mutex_lock(&priv->mutex);
 +
 +	iwlagn_disable_roc(priv);
 +
 +	if (!iwl_is_ready_rf(priv)) {
 +		IWL_WARN(priv, "Try to add interface when device not ready\n");
 +		err = -EINVAL;
 +		goto out;
 +	}
 +
 +	for_each_context(priv, tmp) {
 +		u32 possible_modes =
 +			tmp->interface_modes | tmp->exclusive_interface_modes;
 +
 +		if (tmp->vif) {
 +			/* On reset we need to add the same interface again */
 +			if (tmp->vif == vif) {
 +				reset = true;
 +				ctx = tmp;
 +				break;
 +			}
 +
 +			/* check if this busy context is exclusive */
 +			if (tmp->exclusive_interface_modes &
 +						BIT(tmp->vif->type)) {
 +				err = -EINVAL;
 +				goto out;
 +			}
 +			continue;
 +		}
 +
 +		if (!(possible_modes & BIT(viftype)))
 +			continue;
 +
 +		/* have maybe usable context w/o interface */
 +		ctx = tmp;
 +		break;
 +	}
 +
 +	if (!ctx) {
 +		err = -EOPNOTSUPP;
 +		goto out;
 +	}
 +
 +	vif_priv->ctx = ctx;
 +	ctx->vif = vif;
 +
 +	err = iwl_setup_interface(priv, ctx);
 +	if (!err || reset)
 +		goto out;
 +
 +	ctx->vif = NULL;
 +	priv->iw_mode = NL80211_IFTYPE_STATION;
 + out:
 +	mutex_unlock(&priv->mutex);
 +
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +	return err;
 +}
 +
 +static void iwl_teardown_interface(struct iwl_priv *priv,
 +				   struct ieee80211_vif *vif,
 +				   bool mode_change)
 +{
 +	struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
 +
 +	lockdep_assert_held(&priv->mutex);
 +
 +	if (priv->scan_vif == vif) {
 +		iwl_scan_cancel_timeout(priv, 200);
 +		iwl_force_scan_end(priv);
 +	}
 +
 +	if (!mode_change) {
 +		iwl_set_mode(priv, ctx);
 +		if (!ctx->always_active)
 +			ctx->is_active = false;
 +	}
 +
 +	/*
 +	 * When removing the IBSS interface, overwrite the
 +	 * BT traffic load with the stored one from the last
 +	 * notification, if any. If this is a device that
 +	 * doesn't implement this, this has no effect since
 +	 * both values are the same and zero.
 +	 */
 +	if (vif->type == NL80211_IFTYPE_ADHOC)
 +		priv->bt_traffic_load = priv->last_bt_traffic_load;
 +}
 +
 +static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
 +			      struct ieee80211_vif *vif)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +
 +	mutex_lock(&priv->mutex);
 +
 +	if (WARN_ON(ctx->vif != vif)) {
 +		struct iwl_rxon_context *tmp;
 +		IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
 +		for_each_context(priv, tmp)
 +			IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
 +				tmp->ctxid, tmp, tmp->vif);
 +	}
 +	ctx->vif = NULL;
 +
 +	iwl_teardown_interface(priv, vif, false);
 +
 +	mutex_unlock(&priv->mutex);
 +
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +
 +}
 +
 +static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
 +				       struct ieee80211_vif *vif,
 +				       enum nl80211_iftype newtype, bool newp2p)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_rxon_context *ctx, *tmp;
 +	enum nl80211_iftype newviftype = newtype;
 +	u32 interface_modes;
 +	int err;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +
 +	newtype = ieee80211_iftype_p2p(newtype, newp2p);
 +
 +	mutex_lock(&priv->mutex);
 +
 +	ctx = iwl_rxon_ctx_from_vif(vif);
 +
 +	/*
 +	 * To simplify this code, only support changes on the
 +	 * BSS context. The PAN context is usually reassigned
 +	 * by creating/removing P2P interfaces anyway.
 +	 */
 +	if (ctx->ctxid != IWL_RXON_CTX_BSS) {
 +		err = -EBUSY;
 +		goto out;
 +	}
 +
 +	if (!ctx->vif || !iwl_is_ready_rf(priv)) {
 +		/*
 +		 * Huh? But wait ... this can maybe happen when
 +		 * we're in the middle of a firmware restart!
 +		 */
 +		err = -EBUSY;
 +		goto out;
 +	}
 +
 +	/* Check if the switch is supported in the same context */
 +	interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
 +	if (!(interface_modes & BIT(newtype))) {
 +		err = -EBUSY;
 +		goto out;
 +	}
 +
 +	if (ctx->exclusive_interface_modes & BIT(newtype)) {
 +		for_each_context(priv, tmp) {
 +			if (ctx == tmp)
 +				continue;
 +
 +			if (!tmp->is_active)
 +				continue;
 +
 +			/*
 +			 * The current mode switch would be exclusive, but
 +			 * another context is active ... refuse the switch.
 +			 */
 +			err = -EBUSY;
 +			goto out;
 +		}
 +	}
 +
 +	/* success */
 +	iwl_teardown_interface(priv, vif, true);
 +	vif->type = newviftype;
 +	vif->p2p = newp2p;
 +	err = iwl_setup_interface(priv, ctx);
 +	WARN_ON(err);
 +	/*
 +	 * We've switched internally, but submitting to the
 +	 * device may have failed for some reason. Mask this
 +	 * error, because otherwise mac80211 will not switch
 +	 * (and set the interface type back) and we'll be
 +	 * out of sync with it.
 +	 */
 +	err = 0;
 +
 + out:
 +	mutex_unlock(&priv->mutex);
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +
 +	return err;
 +}
 +
 +static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
 +			      struct ieee80211_vif *vif,
 +			      struct cfg80211_scan_request *req)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	int ret;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +
 +	if (req->n_channels == 0)
 +		return -EINVAL;
 +
 +	mutex_lock(&priv->mutex);
 +
 +	/*
 +	 * If an internal scan is in progress, just set
 +	 * up the scan_request as per above.
 +	 */
 +	if (priv->scan_type != IWL_SCAN_NORMAL) {
 +		IWL_DEBUG_SCAN(priv,
 +			       "SCAN request during internal scan - defer\n");
 +		priv->scan_request = req;
 +		priv->scan_vif = vif;
 +		ret = 0;
 +	} else {
 +		priv->scan_request = req;
 +		priv->scan_vif = vif;
 +		/*
 +		 * mac80211 will only ask for one band at a time
 +		 * so using channels[0] here is ok
 +		 */
 +		ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
 +					req->channels[0]->band);
 +		if (ret) {
 +			priv->scan_request = NULL;
 +			priv->scan_vif = NULL;
 +		}
 +	}
 +
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +
 +	mutex_unlock(&priv->mutex);
 +
 +	return ret;
 +}
 +
 +static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
 +{
 +	struct iwl_addsta_cmd cmd = {
 +		.mode = STA_CONTROL_MODIFY_MSK,
 +		.station_flags_msk = STA_FLG_PWR_SAVE_MSK,
 +		.sta.sta_id = sta_id,
 +	};
 +
 +	iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
 +}
 +
 +static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
 +				  struct ieee80211_vif *vif,
 +				  enum sta_notify_cmd cmd,
 +				  struct ieee80211_sta *sta)
 +{
 +	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 +	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
 +	int sta_id;
 +
 +	IWL_DEBUG_MAC80211(priv, "enter\n");
 +
 +	switch (cmd) {
 +	case STA_NOTIFY_SLEEP:
 +		WARN_ON(!sta_priv->client);
 +		sta_priv->asleep = true;
 +		if (atomic_read(&sta_priv->pending_frames) > 0)
 +			ieee80211_sta_block_awake(hw, sta, true);
 +		break;
 +	case STA_NOTIFY_AWAKE:
 +		WARN_ON(!sta_priv->client);
 +		if (!sta_priv->asleep)
 +			break;
 +		sta_priv->asleep = false;
 +		sta_id = iwl_sta_id(sta);
 +		if (sta_id != IWL_INVALID_STATION)
 +			iwl_sta_modify_ps_wake(priv, sta_id);
 +		break;
 +	default:
 +		break;
 +	}
 +	IWL_DEBUG_MAC80211(priv, "leave\n");
 +}
 +
 +struct ieee80211_ops iwlagn_hw_ops = {
 +	.tx = iwlagn_mac_tx,
 +	.start = iwlagn_mac_start,
 +	.stop = iwlagn_mac_stop,
 +#ifdef CONFIG_PM_SLEEP
 +	.suspend = iwlagn_mac_suspend,
 +	.resume = iwlagn_mac_resume,
 +	.set_wakeup = iwlagn_mac_set_wakeup,
 +#endif
 +	.add_interface = iwlagn_mac_add_interface,
 +	.remove_interface = iwlagn_mac_remove_interface,
 +	.change_interface = iwlagn_mac_change_interface,
 +	.config = iwlagn_mac_config,
 +	.configure_filter = iwlagn_configure_filter,
 +	.set_key = iwlagn_mac_set_key,
 +	.update_tkip_key = iwlagn_mac_update_tkip_key,
 +	.set_rekey_data = iwlagn_mac_set_rekey_data,
 +	.conf_tx = iwlagn_mac_conf_tx,
 +	.bss_info_changed = iwlagn_bss_info_changed,
 +	.ampdu_action = iwlagn_mac_ampdu_action,
 +	.hw_scan = iwlagn_mac_hw_scan,
 +	.sta_notify = iwlagn_mac_sta_notify,
 +	.sta_state = iwlagn_mac_sta_state,
 +	.channel_switch = iwlagn_mac_channel_switch,
 +	.flush = iwlagn_mac_flush,
 +	.tx_last_beacon = iwlagn_mac_tx_last_beacon,
 +	.remain_on_channel = iwlagn_mac_remain_on_channel,
 +	.cancel_remain_on_channel = iwlagn_mac_cancel_remain_on_channel,
 +	.rssi_callback = iwlagn_mac_rssi_callback,
 +	CFG80211_TESTMODE_CMD(iwlagn_mac_testmode_cmd)
 +	CFG80211_TESTMODE_DUMP(iwlagn_mac_testmode_dump)
 +	.set_tim = iwlagn_mac_set_tim,
 +};
 +
 +/* This function both allocates and initializes hw and priv. */
 +struct ieee80211_hw *iwl_alloc_all(void)
 +{
 +	struct iwl_priv *priv;
 +	struct iwl_op_mode *op_mode;
 +	/* mac80211 allocates memory for this device instance, including
 +	 *   space for this driver's private structure */
 +	struct ieee80211_hw *hw;
 +
 +	hw = ieee80211_alloc_hw(sizeof(struct iwl_priv) +
 +				sizeof(struct iwl_op_mode), &iwlagn_hw_ops);
 +	if (!hw)
 +		goto out;
 +
 +	op_mode = hw->priv;
 +	priv = IWL_OP_MODE_GET_DVM(op_mode);
 +	priv->hw = hw;
 +
 +out:
 +	return hw;
 +}