iwl-debugfs.c revision d3a571971e5af241074947fc80f6284677f6e014
1/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 *  Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/debugfs.h>
32
33#include <linux/ieee80211.h>
34#include <net/mac80211.h>
35
36
37#include "iwl-dev.h"
38#include "iwl-debug.h"
39#include "iwl-core.h"
40#include "iwl-io.h"
41#include "iwl-calib.h"
42
43/* create and remove of files */
44#define DEBUGFS_ADD_FILE(name, parent, mode) do {			\
45	if (!debugfs_create_file(#name, mode, parent, priv,		\
46				 &iwl_dbgfs_##name##_ops))		\
47		goto err;						\
48} while (0)
49
50#define DEBUGFS_ADD_BOOL(name, parent, ptr) do {			\
51	struct dentry *__tmp;						\
52	__tmp = debugfs_create_bool(#name, S_IWUSR | S_IRUSR,		\
53				    parent, ptr);			\
54	if (IS_ERR(__tmp) || !__tmp)					\
55		goto err;						\
56} while (0)
57
58#define DEBUGFS_ADD_X32(name, parent, ptr) do {				\
59	struct dentry *__tmp;						\
60	__tmp = debugfs_create_x32(#name, S_IWUSR | S_IRUSR,		\
61				   parent, ptr);			\
62	if (IS_ERR(__tmp) || !__tmp)					\
63		goto err;						\
64} while (0)
65
66/* file operation */
67#define DEBUGFS_READ_FUNC(name)                                         \
68static ssize_t iwl_dbgfs_##name##_read(struct file *file,               \
69					char __user *user_buf,          \
70					size_t count, loff_t *ppos);
71
72#define DEBUGFS_WRITE_FUNC(name)                                        \
73static ssize_t iwl_dbgfs_##name##_write(struct file *file,              \
74					const char __user *user_buf,    \
75					size_t count, loff_t *ppos);
76
77
78static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
79{
80	file->private_data = inode->i_private;
81	return 0;
82}
83
84#define DEBUGFS_READ_FILE_OPS(name)                                     \
85	DEBUGFS_READ_FUNC(name);                                        \
86static const struct file_operations iwl_dbgfs_##name##_ops = {          \
87	.read = iwl_dbgfs_##name##_read,                       		\
88	.open = iwl_dbgfs_open_file_generic,                    	\
89};
90
91#define DEBUGFS_WRITE_FILE_OPS(name)                                    \
92	DEBUGFS_WRITE_FUNC(name);                                       \
93static const struct file_operations iwl_dbgfs_##name##_ops = {          \
94	.write = iwl_dbgfs_##name##_write,                              \
95	.open = iwl_dbgfs_open_file_generic,                    	\
96};
97
98
99#define DEBUGFS_READ_WRITE_FILE_OPS(name)                               \
100	DEBUGFS_READ_FUNC(name);                                        \
101	DEBUGFS_WRITE_FUNC(name);                                       \
102static const struct file_operations iwl_dbgfs_##name##_ops = {          \
103	.write = iwl_dbgfs_##name##_write,                              \
104	.read = iwl_dbgfs_##name##_read,                                \
105	.open = iwl_dbgfs_open_file_generic,                            \
106};
107
108
109static ssize_t iwl_dbgfs_tx_statistics_read(struct file *file,
110						char __user *user_buf,
111						size_t count, loff_t *ppos) {
112
113	struct iwl_priv *priv = file->private_data;
114	char *buf;
115	int pos = 0;
116
117	int cnt;
118	ssize_t ret;
119	const size_t bufsz = 100 +
120		sizeof(char) * 50 * (MANAGEMENT_MAX + CONTROL_MAX);
121	buf = kzalloc(bufsz, GFP_KERNEL);
122	if (!buf)
123		return -ENOMEM;
124	pos += scnprintf(buf + pos, bufsz - pos, "Management:\n");
125	for (cnt = 0; cnt < MANAGEMENT_MAX; cnt++) {
126		pos += scnprintf(buf + pos, bufsz - pos,
127				 "\t%25s\t\t: %u\n",
128				 get_mgmt_string(cnt),
129				 priv->tx_stats.mgmt[cnt]);
130	}
131	pos += scnprintf(buf + pos, bufsz - pos, "Control\n");
132	for (cnt = 0; cnt < CONTROL_MAX; cnt++) {
133		pos += scnprintf(buf + pos, bufsz - pos,
134				 "\t%25s\t\t: %u\n",
135				 get_ctrl_string(cnt),
136				 priv->tx_stats.ctrl[cnt]);
137	}
138	pos += scnprintf(buf + pos, bufsz - pos, "Data:\n");
139	pos += scnprintf(buf + pos, bufsz - pos, "\tcnt: %u\n",
140			 priv->tx_stats.data_cnt);
141	pos += scnprintf(buf + pos, bufsz - pos, "\tbytes: %llu\n",
142			 priv->tx_stats.data_bytes);
143	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
144	kfree(buf);
145	return ret;
146}
147
148static ssize_t iwl_dbgfs_clear_traffic_statistics_write(struct file *file,
149					const char __user *user_buf,
150					size_t count, loff_t *ppos)
151{
152	struct iwl_priv *priv = file->private_data;
153	u32 clear_flag;
154	char buf[8];
155	int buf_size;
156
157	memset(buf, 0, sizeof(buf));
158	buf_size = min(count, sizeof(buf) -  1);
159	if (copy_from_user(buf, user_buf, buf_size))
160		return -EFAULT;
161	if (sscanf(buf, "%x", &clear_flag) != 1)
162		return -EFAULT;
163	iwl_clear_traffic_stats(priv);
164
165	return count;
166}
167
168static ssize_t iwl_dbgfs_rx_statistics_read(struct file *file,
169						char __user *user_buf,
170						size_t count, loff_t *ppos) {
171
172	struct iwl_priv *priv = file->private_data;
173	char *buf;
174	int pos = 0;
175	int cnt;
176	ssize_t ret;
177	const size_t bufsz = 100 +
178		sizeof(char) * 50 * (MANAGEMENT_MAX + CONTROL_MAX);
179	buf = kzalloc(bufsz, GFP_KERNEL);
180	if (!buf)
181		return -ENOMEM;
182
183	pos += scnprintf(buf + pos, bufsz - pos, "Management:\n");
184	for (cnt = 0; cnt < MANAGEMENT_MAX; cnt++) {
185		pos += scnprintf(buf + pos, bufsz - pos,
186				 "\t%25s\t\t: %u\n",
187				 get_mgmt_string(cnt),
188				 priv->rx_stats.mgmt[cnt]);
189	}
190	pos += scnprintf(buf + pos, bufsz - pos, "Control:\n");
191	for (cnt = 0; cnt < CONTROL_MAX; cnt++) {
192		pos += scnprintf(buf + pos, bufsz - pos,
193				 "\t%25s\t\t: %u\n",
194				 get_ctrl_string(cnt),
195				 priv->rx_stats.ctrl[cnt]);
196	}
197	pos += scnprintf(buf + pos, bufsz - pos, "Data:\n");
198	pos += scnprintf(buf + pos, bufsz - pos, "\tcnt: %u\n",
199			 priv->rx_stats.data_cnt);
200	pos += scnprintf(buf + pos, bufsz - pos, "\tbytes: %llu\n",
201			 priv->rx_stats.data_bytes);
202
203	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
204	kfree(buf);
205	return ret;
206}
207
208#define BYTE1_MASK 0x000000ff;
209#define BYTE2_MASK 0x0000ffff;
210#define BYTE3_MASK 0x00ffffff;
211static ssize_t iwl_dbgfs_sram_read(struct file *file,
212					char __user *user_buf,
213					size_t count, loff_t *ppos)
214{
215	u32 val;
216	char *buf;
217	ssize_t ret;
218	int i;
219	int pos = 0;
220	struct iwl_priv *priv = file->private_data;
221	size_t bufsz;
222
223	/* default is to dump the entire data segment */
224	if (!priv->dbgfs_sram_offset && !priv->dbgfs_sram_len) {
225		priv->dbgfs_sram_offset = 0x800000;
226		if (priv->ucode_type == UCODE_INIT)
227			priv->dbgfs_sram_len = priv->ucode_init_data.len;
228		else
229			priv->dbgfs_sram_len = priv->ucode_data.len;
230	}
231	bufsz =  30 + priv->dbgfs_sram_len * sizeof(char) * 10;
232	buf = kmalloc(bufsz, GFP_KERNEL);
233	if (!buf)
234		return -ENOMEM;
235	pos += scnprintf(buf + pos, bufsz - pos, "sram_len: 0x%x\n",
236			priv->dbgfs_sram_len);
237	pos += scnprintf(buf + pos, bufsz - pos, "sram_offset: 0x%x\n",
238			priv->dbgfs_sram_offset);
239	for (i = priv->dbgfs_sram_len; i > 0; i -= 4) {
240		val = iwl_read_targ_mem(priv, priv->dbgfs_sram_offset + \
241					priv->dbgfs_sram_len - i);
242		if (i < 4) {
243			switch (i) {
244			case 1:
245				val &= BYTE1_MASK;
246				break;
247			case 2:
248				val &= BYTE2_MASK;
249				break;
250			case 3:
251				val &= BYTE3_MASK;
252				break;
253			}
254		}
255		if (!(i % 16))
256			pos += scnprintf(buf + pos, bufsz - pos, "\n");
257		pos += scnprintf(buf + pos, bufsz - pos, "0x%08x ", val);
258	}
259	pos += scnprintf(buf + pos, bufsz - pos, "\n");
260
261	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
262	kfree(buf);
263	return ret;
264}
265
266static ssize_t iwl_dbgfs_sram_write(struct file *file,
267					const char __user *user_buf,
268					size_t count, loff_t *ppos)
269{
270	struct iwl_priv *priv = file->private_data;
271	char buf[64];
272	int buf_size;
273	u32 offset, len;
274
275	memset(buf, 0, sizeof(buf));
276	buf_size = min(count, sizeof(buf) -  1);
277	if (copy_from_user(buf, user_buf, buf_size))
278		return -EFAULT;
279
280	if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
281		priv->dbgfs_sram_offset = offset;
282		priv->dbgfs_sram_len = len;
283	} else {
284		priv->dbgfs_sram_offset = 0;
285		priv->dbgfs_sram_len = 0;
286	}
287
288	return count;
289}
290
291static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
292					size_t count, loff_t *ppos)
293{
294	struct iwl_priv *priv = file->private_data;
295	struct iwl_station_entry *station;
296	int max_sta = priv->hw_params.max_stations;
297	char *buf;
298	int i, j, pos = 0;
299	ssize_t ret;
300	/* Add 30 for initial string */
301	const size_t bufsz = 30 + sizeof(char) * 500 * (priv->num_stations);
302
303	buf = kmalloc(bufsz, GFP_KERNEL);
304	if (!buf)
305		return -ENOMEM;
306
307	pos += scnprintf(buf + pos, bufsz - pos, "num of stations: %d\n\n",
308			priv->num_stations);
309
310	for (i = 0; i < max_sta; i++) {
311		station = &priv->stations[i];
312		if (station->used) {
313			pos += scnprintf(buf + pos, bufsz - pos,
314					"station %d:\ngeneral data:\n", i+1);
315			pos += scnprintf(buf + pos, bufsz - pos, "id: %u\n",
316					station->sta.sta.sta_id);
317			pos += scnprintf(buf + pos, bufsz - pos, "mode: %u\n",
318					station->sta.mode);
319			pos += scnprintf(buf + pos, bufsz - pos,
320					"flags: 0x%x\n",
321					station->sta.station_flags_msk);
322			pos += scnprintf(buf + pos, bufsz - pos, "tid data:\n");
323			pos += scnprintf(buf + pos, bufsz - pos,
324					"seq_num\t\ttxq_id");
325			pos += scnprintf(buf + pos, bufsz - pos,
326					"\tframe_count\twait_for_ba\t");
327			pos += scnprintf(buf + pos, bufsz - pos,
328					"start_idx\tbitmap0\t");
329			pos += scnprintf(buf + pos, bufsz - pos,
330					"bitmap1\trate_n_flags");
331			pos += scnprintf(buf + pos, bufsz - pos, "\n");
332
333			for (j = 0; j < MAX_TID_COUNT; j++) {
334				pos += scnprintf(buf + pos, bufsz - pos,
335						"[%d]:\t\t%u", j,
336						station->tid[j].seq_number);
337				pos += scnprintf(buf + pos, bufsz - pos,
338						"\t%u\t\t%u\t\t%u\t\t",
339						station->tid[j].agg.txq_id,
340						station->tid[j].agg.frame_count,
341						station->tid[j].agg.wait_for_ba);
342				pos += scnprintf(buf + pos, bufsz - pos,
343						"%u\t%llu\t%u",
344						station->tid[j].agg.start_idx,
345						(unsigned long long)station->tid[j].agg.bitmap,
346						station->tid[j].agg.rate_n_flags);
347				pos += scnprintf(buf + pos, bufsz - pos, "\n");
348			}
349			pos += scnprintf(buf + pos, bufsz - pos, "\n");
350		}
351	}
352
353	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
354	kfree(buf);
355	return ret;
356}
357
358static ssize_t iwl_dbgfs_nvm_read(struct file *file,
359				       char __user *user_buf,
360				       size_t count,
361				       loff_t *ppos)
362{
363	ssize_t ret;
364	struct iwl_priv *priv = file->private_data;
365	int pos = 0, ofs = 0, buf_size = 0;
366	const u8 *ptr;
367	char *buf;
368	u16 eeprom_ver;
369	size_t eeprom_len = priv->cfg->eeprom_size;
370	buf_size = 4 * eeprom_len + 256;
371
372	if (eeprom_len % 16) {
373		IWL_ERR(priv, "NVM size is not multiple of 16.\n");
374		return -ENODATA;
375	}
376
377	ptr = priv->eeprom;
378	if (!ptr) {
379		IWL_ERR(priv, "Invalid EEPROM/OTP memory\n");
380		return -ENOMEM;
381	}
382
383	/* 4 characters for byte 0xYY */
384	buf = kzalloc(buf_size, GFP_KERNEL);
385	if (!buf) {
386		IWL_ERR(priv, "Can not allocate Buffer\n");
387		return -ENOMEM;
388	}
389	eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
390	pos += scnprintf(buf + pos, buf_size - pos, "NVM Type: %s, "
391			"version: 0x%x\n",
392			(priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
393			 ? "OTP" : "EEPROM", eeprom_ver);
394	for (ofs = 0 ; ofs < eeprom_len ; ofs += 16) {
395		pos += scnprintf(buf + pos, buf_size - pos, "0x%.4x ", ofs);
396		hex_dump_to_buffer(ptr + ofs, 16 , 16, 2, buf + pos,
397				   buf_size - pos, 0);
398		pos += strlen(buf + pos);
399		if (buf_size - pos > 0)
400			buf[pos++] = '\n';
401	}
402
403	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
404	kfree(buf);
405	return ret;
406}
407
408static ssize_t iwl_dbgfs_log_event_read(struct file *file,
409					 char __user *user_buf,
410					 size_t count, loff_t *ppos)
411{
412	struct iwl_priv *priv = file->private_data;
413	char *buf;
414	int pos = 0;
415	ssize_t ret = -ENOMEM;
416
417	ret = pos = priv->cfg->ops->lib->dump_nic_event_log(
418					priv, true, &buf, true);
419	if (buf) {
420		ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
421		kfree(buf);
422	}
423	return ret;
424}
425
426static ssize_t iwl_dbgfs_log_event_write(struct file *file,
427					const char __user *user_buf,
428					size_t count, loff_t *ppos)
429{
430	struct iwl_priv *priv = file->private_data;
431	u32 event_log_flag;
432	char buf[8];
433	int buf_size;
434
435	memset(buf, 0, sizeof(buf));
436	buf_size = min(count, sizeof(buf) -  1);
437	if (copy_from_user(buf, user_buf, buf_size))
438		return -EFAULT;
439	if (sscanf(buf, "%d", &event_log_flag) != 1)
440		return -EFAULT;
441	if (event_log_flag == 1)
442		priv->cfg->ops->lib->dump_nic_event_log(priv, true,
443							NULL, false);
444
445	return count;
446}
447
448
449
450static ssize_t iwl_dbgfs_channels_read(struct file *file, char __user *user_buf,
451				       size_t count, loff_t *ppos)
452{
453	struct iwl_priv *priv = file->private_data;
454	struct ieee80211_channel *channels = NULL;
455	const struct ieee80211_supported_band *supp_band = NULL;
456	int pos = 0, i, bufsz = PAGE_SIZE;
457	char *buf;
458	ssize_t ret;
459
460	if (!test_bit(STATUS_GEO_CONFIGURED, &priv->status))
461		return -EAGAIN;
462
463	buf = kzalloc(bufsz, GFP_KERNEL);
464	if (!buf) {
465		IWL_ERR(priv, "Can not allocate Buffer\n");
466		return -ENOMEM;
467	}
468
469	supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_2GHZ);
470	if (supp_band) {
471		channels = supp_band->channels;
472
473		pos += scnprintf(buf + pos, bufsz - pos,
474				"Displaying %d channels in 2.4GHz band 802.11bg):\n",
475				supp_band->n_channels);
476
477		for (i = 0; i < supp_band->n_channels; i++)
478			pos += scnprintf(buf + pos, bufsz - pos,
479					"%d: %ddBm: BSS%s%s, %s.\n",
480					ieee80211_frequency_to_channel(
481					channels[i].center_freq),
482					channels[i].max_power,
483					channels[i].flags & IEEE80211_CHAN_RADAR ?
484					" (IEEE 802.11h required)" : "",
485					((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
486					|| (channels[i].flags &
487					IEEE80211_CHAN_RADAR)) ? "" :
488					", IBSS",
489					channels[i].flags &
490					IEEE80211_CHAN_PASSIVE_SCAN ?
491					"passive only" : "active/passive");
492	}
493	supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_5GHZ);
494	if (supp_band) {
495		channels = supp_band->channels;
496
497		pos += scnprintf(buf + pos, bufsz - pos,
498				"Displaying %d channels in 5.2GHz band (802.11a)\n",
499				supp_band->n_channels);
500
501		for (i = 0; i < supp_band->n_channels; i++)
502			pos += scnprintf(buf + pos, bufsz - pos,
503					"%d: %ddBm: BSS%s%s, %s.\n",
504					ieee80211_frequency_to_channel(
505					channels[i].center_freq),
506					channels[i].max_power,
507					channels[i].flags & IEEE80211_CHAN_RADAR ?
508					" (IEEE 802.11h required)" : "",
509					((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
510					|| (channels[i].flags &
511					IEEE80211_CHAN_RADAR)) ? "" :
512					", IBSS",
513					channels[i].flags &
514					IEEE80211_CHAN_PASSIVE_SCAN ?
515					"passive only" : "active/passive");
516	}
517	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
518	kfree(buf);
519	return ret;
520}
521
522static ssize_t iwl_dbgfs_status_read(struct file *file,
523						char __user *user_buf,
524						size_t count, loff_t *ppos) {
525
526	struct iwl_priv *priv = file->private_data;
527	char buf[512];
528	int pos = 0;
529	const size_t bufsz = sizeof(buf);
530
531	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_HCMD_ACTIVE:\t %d\n",
532		test_bit(STATUS_HCMD_ACTIVE, &priv->status));
533	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_HCMD_SYNC_ACTIVE: %d\n",
534		test_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status));
535	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_INT_ENABLED:\t %d\n",
536		test_bit(STATUS_INT_ENABLED, &priv->status));
537	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_RF_KILL_HW:\t %d\n",
538		test_bit(STATUS_RF_KILL_HW, &priv->status));
539	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_CT_KILL:\t\t %d\n",
540		test_bit(STATUS_CT_KILL, &priv->status));
541	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_INIT:\t\t %d\n",
542		test_bit(STATUS_INIT, &priv->status));
543	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_ALIVE:\t\t %d\n",
544		test_bit(STATUS_ALIVE, &priv->status));
545	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_READY:\t\t %d\n",
546		test_bit(STATUS_READY, &priv->status));
547	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_TEMPERATURE:\t %d\n",
548		test_bit(STATUS_TEMPERATURE, &priv->status));
549	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_GEO_CONFIGURED:\t %d\n",
550		test_bit(STATUS_GEO_CONFIGURED, &priv->status));
551	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_EXIT_PENDING:\t %d\n",
552		test_bit(STATUS_EXIT_PENDING, &priv->status));
553	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_STATISTICS:\t %d\n",
554		test_bit(STATUS_STATISTICS, &priv->status));
555	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCANNING:\t %d\n",
556		test_bit(STATUS_SCANNING, &priv->status));
557	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCAN_ABORTING:\t %d\n",
558		test_bit(STATUS_SCAN_ABORTING, &priv->status));
559	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCAN_HW:\t\t %d\n",
560		test_bit(STATUS_SCAN_HW, &priv->status));
561	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_POWER_PMI:\t %d\n",
562		test_bit(STATUS_POWER_PMI, &priv->status));
563	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_FW_ERROR:\t %d\n",
564		test_bit(STATUS_FW_ERROR, &priv->status));
565	pos += scnprintf(buf + pos, bufsz - pos, "STATUS_MODE_PENDING:\t %d\n",
566		test_bit(STATUS_MODE_PENDING, &priv->status));
567	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
568}
569
570static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
571					char __user *user_buf,
572					size_t count, loff_t *ppos) {
573
574	struct iwl_priv *priv = file->private_data;
575	int pos = 0;
576	int cnt = 0;
577	char *buf;
578	int bufsz = 24 * 64; /* 24 items * 64 char per item */
579	ssize_t ret;
580
581	buf = kzalloc(bufsz, GFP_KERNEL);
582	if (!buf) {
583		IWL_ERR(priv, "Can not allocate Buffer\n");
584		return -ENOMEM;
585	}
586
587	pos += scnprintf(buf + pos, bufsz - pos,
588			"Interrupt Statistics Report:\n");
589
590	pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
591		priv->isr_stats.hw);
592	pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
593		priv->isr_stats.sw);
594	if (priv->isr_stats.sw > 0) {
595		pos += scnprintf(buf + pos, bufsz - pos,
596			"\tLast Restarting Code:  0x%X\n",
597			priv->isr_stats.sw_err);
598	}
599#ifdef CONFIG_IWLWIFI_DEBUG
600	pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
601		priv->isr_stats.sch);
602	pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
603		priv->isr_stats.alive);
604#endif
605	pos += scnprintf(buf + pos, bufsz - pos,
606		"HW RF KILL switch toggled:\t %u\n",
607		priv->isr_stats.rfkill);
608
609	pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
610		priv->isr_stats.ctkill);
611
612	pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
613		priv->isr_stats.wakeup);
614
615	pos += scnprintf(buf + pos, bufsz - pos,
616		"Rx command responses:\t\t %u\n",
617		priv->isr_stats.rx);
618	for (cnt = 0; cnt < REPLY_MAX; cnt++) {
619		if (priv->isr_stats.rx_handlers[cnt] > 0)
620			pos += scnprintf(buf + pos, bufsz - pos,
621				"\tRx handler[%36s]:\t\t %u\n",
622				get_cmd_string(cnt),
623				priv->isr_stats.rx_handlers[cnt]);
624	}
625
626	pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
627		priv->isr_stats.tx);
628
629	pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
630		priv->isr_stats.unhandled);
631
632	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
633	kfree(buf);
634	return ret;
635}
636
637static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
638					 const char __user *user_buf,
639					 size_t count, loff_t *ppos)
640{
641	struct iwl_priv *priv = file->private_data;
642	char buf[8];
643	int buf_size;
644	u32 reset_flag;
645
646	memset(buf, 0, sizeof(buf));
647	buf_size = min(count, sizeof(buf) -  1);
648	if (copy_from_user(buf, user_buf, buf_size))
649		return -EFAULT;
650	if (sscanf(buf, "%x", &reset_flag) != 1)
651		return -EFAULT;
652	if (reset_flag == 0)
653		iwl_clear_isr_stats(priv);
654
655	return count;
656}
657
658static ssize_t iwl_dbgfs_qos_read(struct file *file, char __user *user_buf,
659				       size_t count, loff_t *ppos)
660{
661	struct iwl_priv *priv = file->private_data;
662	int pos = 0, i;
663	char buf[256];
664	const size_t bufsz = sizeof(buf);
665	ssize_t ret;
666
667	for (i = 0; i < AC_NUM; i++) {
668		pos += scnprintf(buf + pos, bufsz - pos,
669			"\tcw_min\tcw_max\taifsn\ttxop\n");
670		pos += scnprintf(buf + pos, bufsz - pos,
671				"AC[%d]\t%u\t%u\t%u\t%u\n", i,
672				priv->qos_data.def_qos_parm.ac[i].cw_min,
673				priv->qos_data.def_qos_parm.ac[i].cw_max,
674				priv->qos_data.def_qos_parm.ac[i].aifsn,
675				priv->qos_data.def_qos_parm.ac[i].edca_txop);
676	}
677	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
678	return ret;
679}
680
681static ssize_t iwl_dbgfs_led_read(struct file *file, char __user *user_buf,
682				  size_t count, loff_t *ppos)
683{
684	struct iwl_priv *priv = file->private_data;
685	int pos = 0;
686	char buf[256];
687	const size_t bufsz = sizeof(buf);
688	ssize_t ret;
689
690	pos += scnprintf(buf + pos, bufsz - pos,
691			 "allow blinking: %s\n",
692			 (priv->allow_blinking) ? "True" : "False");
693	if (priv->allow_blinking) {
694		pos += scnprintf(buf + pos, bufsz - pos,
695				 "Led blinking rate: %u\n",
696				 priv->last_blink_rate);
697		pos += scnprintf(buf + pos, bufsz - pos,
698				 "Last blink time: %lu\n",
699				 priv->last_blink_time);
700	}
701
702	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
703	return ret;
704}
705
706static ssize_t iwl_dbgfs_thermal_throttling_read(struct file *file,
707				char __user *user_buf,
708				size_t count, loff_t *ppos)
709{
710	struct iwl_priv *priv = file->private_data;
711	struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
712	struct iwl_tt_restriction *restriction;
713	char buf[100];
714	int pos = 0;
715	const size_t bufsz = sizeof(buf);
716	ssize_t ret;
717
718	pos += scnprintf(buf + pos, bufsz - pos,
719			"Thermal Throttling Mode: %s\n",
720			tt->advanced_tt ? "Advance" : "Legacy");
721	pos += scnprintf(buf + pos, bufsz - pos,
722			"Thermal Throttling State: %d\n",
723			tt->state);
724	if (tt->advanced_tt) {
725		restriction = tt->restriction + tt->state;
726		pos += scnprintf(buf + pos, bufsz - pos,
727				"Tx mode: %d\n",
728				restriction->tx_stream);
729		pos += scnprintf(buf + pos, bufsz - pos,
730				"Rx mode: %d\n",
731				restriction->rx_stream);
732		pos += scnprintf(buf + pos, bufsz - pos,
733				"HT mode: %d\n",
734				restriction->is_ht);
735	}
736	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
737	return ret;
738}
739
740static ssize_t iwl_dbgfs_disable_ht40_write(struct file *file,
741					 const char __user *user_buf,
742					 size_t count, loff_t *ppos)
743{
744	struct iwl_priv *priv = file->private_data;
745	char buf[8];
746	int buf_size;
747	int ht40;
748
749	memset(buf, 0, sizeof(buf));
750	buf_size = min(count, sizeof(buf) -  1);
751	if (copy_from_user(buf, user_buf, buf_size))
752		return -EFAULT;
753	if (sscanf(buf, "%d", &ht40) != 1)
754		return -EFAULT;
755	if (!iwl_is_associated(priv))
756		priv->disable_ht40 = ht40 ? true : false;
757	else {
758		IWL_ERR(priv, "Sta associated with AP - "
759			"Change to 40MHz channel support is not allowed\n");
760		return -EINVAL;
761	}
762
763	return count;
764}
765
766static ssize_t iwl_dbgfs_disable_ht40_read(struct file *file,
767					 char __user *user_buf,
768					 size_t count, loff_t *ppos)
769{
770	struct iwl_priv *priv = file->private_data;
771	char buf[100];
772	int pos = 0;
773	const size_t bufsz = sizeof(buf);
774	ssize_t ret;
775
776	pos += scnprintf(buf + pos, bufsz - pos,
777			"11n 40MHz Mode: %s\n",
778			priv->disable_ht40 ? "Disabled" : "Enabled");
779	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
780	return ret;
781}
782
783static ssize_t iwl_dbgfs_sleep_level_override_write(struct file *file,
784						    const char __user *user_buf,
785						    size_t count, loff_t *ppos)
786{
787	struct iwl_priv *priv = file->private_data;
788	char buf[8];
789	int buf_size;
790	int value;
791
792	memset(buf, 0, sizeof(buf));
793	buf_size = min(count, sizeof(buf) -  1);
794	if (copy_from_user(buf, user_buf, buf_size))
795		return -EFAULT;
796
797	if (sscanf(buf, "%d", &value) != 1)
798		return -EINVAL;
799
800	/*
801	 * Our users expect 0 to be "CAM", but 0 isn't actually
802	 * valid here. However, let's not confuse them and present
803	 * IWL_POWER_INDEX_1 as "1", not "0".
804	 */
805	if (value == 0)
806		return -EINVAL;
807	else if (value > 0)
808		value -= 1;
809
810	if (value != -1 && (value < 0 || value >= IWL_POWER_NUM))
811		return -EINVAL;
812
813	if (!iwl_is_ready_rf(priv))
814		return -EAGAIN;
815
816	priv->power_data.debug_sleep_level_override = value;
817
818	mutex_lock(&priv->mutex);
819	iwl_power_update_mode(priv, true);
820	mutex_unlock(&priv->mutex);
821
822	return count;
823}
824
825static ssize_t iwl_dbgfs_sleep_level_override_read(struct file *file,
826						   char __user *user_buf,
827						   size_t count, loff_t *ppos)
828{
829	struct iwl_priv *priv = file->private_data;
830	char buf[10];
831	int pos, value;
832	const size_t bufsz = sizeof(buf);
833
834	/* see the write function */
835	value = priv->power_data.debug_sleep_level_override;
836	if (value >= 0)
837		value += 1;
838
839	pos = scnprintf(buf, bufsz, "%d\n", value);
840	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
841}
842
843static ssize_t iwl_dbgfs_current_sleep_command_read(struct file *file,
844						    char __user *user_buf,
845						    size_t count, loff_t *ppos)
846{
847	struct iwl_priv *priv = file->private_data;
848	char buf[200];
849	int pos = 0, i;
850	const size_t bufsz = sizeof(buf);
851	struct iwl_powertable_cmd *cmd = &priv->power_data.sleep_cmd;
852
853	pos += scnprintf(buf + pos, bufsz - pos,
854			 "flags: %#.2x\n", le16_to_cpu(cmd->flags));
855	pos += scnprintf(buf + pos, bufsz - pos,
856			 "RX/TX timeout: %d/%d usec\n",
857			 le32_to_cpu(cmd->rx_data_timeout),
858			 le32_to_cpu(cmd->tx_data_timeout));
859	for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
860		pos += scnprintf(buf + pos, bufsz - pos,
861				 "sleep_interval[%d]: %d\n", i,
862				 le32_to_cpu(cmd->sleep_interval[i]));
863
864	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
865}
866
867DEBUGFS_READ_WRITE_FILE_OPS(sram);
868DEBUGFS_READ_WRITE_FILE_OPS(log_event);
869DEBUGFS_READ_FILE_OPS(nvm);
870DEBUGFS_READ_FILE_OPS(stations);
871DEBUGFS_READ_FILE_OPS(channels);
872DEBUGFS_READ_FILE_OPS(status);
873DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
874DEBUGFS_READ_FILE_OPS(qos);
875DEBUGFS_READ_FILE_OPS(led);
876DEBUGFS_READ_FILE_OPS(thermal_throttling);
877DEBUGFS_READ_WRITE_FILE_OPS(disable_ht40);
878DEBUGFS_READ_WRITE_FILE_OPS(sleep_level_override);
879DEBUGFS_READ_FILE_OPS(current_sleep_command);
880
881static ssize_t iwl_dbgfs_traffic_log_read(struct file *file,
882					 char __user *user_buf,
883					 size_t count, loff_t *ppos)
884{
885	struct iwl_priv *priv = file->private_data;
886	int pos = 0, ofs = 0;
887	int cnt = 0, entry;
888	struct iwl_tx_queue *txq;
889	struct iwl_queue *q;
890	struct iwl_rx_queue *rxq = &priv->rxq;
891	char *buf;
892	int bufsz = ((IWL_TRAFFIC_ENTRIES * IWL_TRAFFIC_ENTRY_SIZE * 64) * 2) +
893		(priv->cfg->num_of_queues * 32 * 8) + 400;
894	const u8 *ptr;
895	ssize_t ret;
896
897	if (!priv->txq) {
898		IWL_ERR(priv, "txq not ready\n");
899		return -EAGAIN;
900	}
901	buf = kzalloc(bufsz, GFP_KERNEL);
902	if (!buf) {
903		IWL_ERR(priv, "Can not allocate buffer\n");
904		return -ENOMEM;
905	}
906	pos += scnprintf(buf + pos, bufsz - pos, "Tx Queue\n");
907	for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
908		txq = &priv->txq[cnt];
909		q = &txq->q;
910		pos += scnprintf(buf + pos, bufsz - pos,
911				"q[%d]: read_ptr: %u, write_ptr: %u\n",
912				cnt, q->read_ptr, q->write_ptr);
913	}
914	if (priv->tx_traffic && (iwl_debug_level & IWL_DL_TX)) {
915		ptr = priv->tx_traffic;
916		pos += scnprintf(buf + pos, bufsz - pos,
917				"Tx Traffic idx: %u\n",	priv->tx_traffic_idx);
918		for (cnt = 0, ofs = 0; cnt < IWL_TRAFFIC_ENTRIES; cnt++) {
919			for (entry = 0; entry < IWL_TRAFFIC_ENTRY_SIZE / 16;
920			     entry++,  ofs += 16) {
921				pos += scnprintf(buf + pos, bufsz - pos,
922						"0x%.4x ", ofs);
923				hex_dump_to_buffer(ptr + ofs, 16, 16, 2,
924						   buf + pos, bufsz - pos, 0);
925				pos += strlen(buf + pos);
926				if (bufsz - pos > 0)
927					buf[pos++] = '\n';
928			}
929		}
930	}
931
932	pos += scnprintf(buf + pos, bufsz - pos, "Rx Queue\n");
933	pos += scnprintf(buf + pos, bufsz - pos,
934			"read: %u, write: %u\n",
935			 rxq->read, rxq->write);
936
937	if (priv->rx_traffic && (iwl_debug_level & IWL_DL_RX)) {
938		ptr = priv->rx_traffic;
939		pos += scnprintf(buf + pos, bufsz - pos,
940				"Rx Traffic idx: %u\n",	priv->rx_traffic_idx);
941		for (cnt = 0, ofs = 0; cnt < IWL_TRAFFIC_ENTRIES; cnt++) {
942			for (entry = 0; entry < IWL_TRAFFIC_ENTRY_SIZE / 16;
943			     entry++,  ofs += 16) {
944				pos += scnprintf(buf + pos, bufsz - pos,
945						"0x%.4x ", ofs);
946				hex_dump_to_buffer(ptr + ofs, 16, 16, 2,
947						   buf + pos, bufsz - pos, 0);
948				pos += strlen(buf + pos);
949				if (bufsz - pos > 0)
950					buf[pos++] = '\n';
951			}
952		}
953	}
954
955	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
956	kfree(buf);
957	return ret;
958}
959
960static ssize_t iwl_dbgfs_traffic_log_write(struct file *file,
961					 const char __user *user_buf,
962					 size_t count, loff_t *ppos)
963{
964	struct iwl_priv *priv = file->private_data;
965	char buf[8];
966	int buf_size;
967	int traffic_log;
968
969	memset(buf, 0, sizeof(buf));
970	buf_size = min(count, sizeof(buf) -  1);
971	if (copy_from_user(buf, user_buf, buf_size))
972		return -EFAULT;
973	if (sscanf(buf, "%d", &traffic_log) != 1)
974		return -EFAULT;
975	if (traffic_log == 0)
976		iwl_reset_traffic_log(priv);
977
978	return count;
979}
980
981static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
982						char __user *user_buf,
983						size_t count, loff_t *ppos) {
984
985	struct iwl_priv *priv = file->private_data;
986	struct iwl_tx_queue *txq;
987	struct iwl_queue *q;
988	char *buf;
989	int pos = 0;
990	int cnt;
991	int ret;
992	const size_t bufsz = sizeof(char) * 64 * priv->cfg->num_of_queues;
993
994	if (!priv->txq) {
995		IWL_ERR(priv, "txq not ready\n");
996		return -EAGAIN;
997	}
998	buf = kzalloc(bufsz, GFP_KERNEL);
999	if (!buf)
1000		return -ENOMEM;
1001
1002	for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
1003		txq = &priv->txq[cnt];
1004		q = &txq->q;
1005		pos += scnprintf(buf + pos, bufsz - pos,
1006				"hwq %.2d: read=%u write=%u stop=%d"
1007				" swq_id=%#.2x (ac %d/hwq %d)\n",
1008				cnt, q->read_ptr, q->write_ptr,
1009				!!test_bit(cnt, priv->queue_stopped),
1010				txq->swq_id,
1011				txq->swq_id & 0x80 ? txq->swq_id & 3 :
1012				txq->swq_id,
1013				txq->swq_id & 0x80 ? (txq->swq_id >> 2) &
1014				0x1f : txq->swq_id);
1015		if (cnt >= 4)
1016			continue;
1017		/* for the ACs, display the stop count too */
1018		pos += scnprintf(buf + pos, bufsz - pos,
1019				"        stop-count: %d\n",
1020				atomic_read(&priv->queue_stop_count[cnt]));
1021	}
1022	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1023	kfree(buf);
1024	return ret;
1025}
1026
1027static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
1028						char __user *user_buf,
1029						size_t count, loff_t *ppos) {
1030
1031	struct iwl_priv *priv = file->private_data;
1032	struct iwl_rx_queue *rxq = &priv->rxq;
1033	char buf[256];
1034	int pos = 0;
1035	const size_t bufsz = sizeof(buf);
1036
1037	pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
1038						rxq->read);
1039	pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
1040						rxq->write);
1041	pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
1042						rxq->free_count);
1043	pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
1044			 le16_to_cpu(rxq->rb_stts->closed_rb_num) &  0x0FFF);
1045	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1046}
1047
1048static int iwl_dbgfs_statistics_flag(struct iwl_priv *priv, char *buf,
1049				     int bufsz)
1050{
1051	int p = 0;
1052
1053	p += scnprintf(buf + p, bufsz - p,
1054		"Statistics Flag(0x%X):\n",
1055		le32_to_cpu(priv->statistics.flag));
1056	if (le32_to_cpu(priv->statistics.flag) & UCODE_STATISTICS_CLEAR_MSK)
1057		p += scnprintf(buf + p, bufsz - p,
1058		"\tStatistics have been cleared\n");
1059	p += scnprintf(buf + p, bufsz - p,
1060		"\tOperational Frequency: %s\n",
1061		(le32_to_cpu(priv->statistics.flag) &
1062		UCODE_STATISTICS_FREQUENCY_MSK)
1063		 ? "2.4 GHz" : "5.2 GHz");
1064	p += scnprintf(buf + p, bufsz - p,
1065		"\tTGj Narrow Band: %s\n",
1066		(le32_to_cpu(priv->statistics.flag) &
1067		UCODE_STATISTICS_NARROW_BAND_MSK)
1068		 ? "enabled" : "disabled");
1069	return p;
1070}
1071
1072static const char ucode_stats_header[] =
1073	"%-32s     current  acumulative       delta         max\n";
1074static const char ucode_stats_short_format[] =
1075	"  %-30s %10u\n";
1076static const char ucode_stats_format[] =
1077	"  %-30s %10u  %10u  %10u  %10u\n";
1078
1079static ssize_t iwl_dbgfs_ucode_rx_stats_read(struct file *file,
1080					char __user *user_buf,
1081					size_t count, loff_t *ppos)
1082{
1083	struct iwl_priv *priv = file->private_data;
1084	int pos = 0;
1085	char *buf;
1086	int bufsz = sizeof(struct statistics_rx_phy) * 40 +
1087		sizeof(struct statistics_rx_non_phy) * 40 +
1088		sizeof(struct statistics_rx_ht_phy) * 40 + 400;
1089	ssize_t ret;
1090	struct statistics_rx_phy *ofdm, *accum_ofdm, *delta_ofdm, *max_ofdm;
1091	struct statistics_rx_phy *cck, *accum_cck, *delta_cck, *max_cck;
1092	struct statistics_rx_non_phy *general, *accum_general;
1093	struct statistics_rx_non_phy *delta_general, *max_general;
1094	struct statistics_rx_ht_phy *ht, *accum_ht, *delta_ht, *max_ht;
1095
1096	if (!iwl_is_alive(priv))
1097		return -EAGAIN;
1098
1099	buf = kzalloc(bufsz, GFP_KERNEL);
1100	if (!buf) {
1101		IWL_ERR(priv, "Can not allocate Buffer\n");
1102		return -ENOMEM;
1103	}
1104
1105	/* the statistic information display here is based on
1106	 * the last statistics notification from uCode
1107	 * might not reflect the current uCode activity
1108	 */
1109	ofdm = &priv->statistics.rx.ofdm;
1110	cck = &priv->statistics.rx.cck;
1111	general = &priv->statistics.rx.general;
1112	ht = &priv->statistics.rx.ofdm_ht;
1113	accum_ofdm = &priv->accum_statistics.rx.ofdm;
1114	accum_cck = &priv->accum_statistics.rx.cck;
1115	accum_general = &priv->accum_statistics.rx.general;
1116	accum_ht = &priv->accum_statistics.rx.ofdm_ht;
1117	delta_ofdm = &priv->delta_statistics.rx.ofdm;
1118	delta_cck = &priv->delta_statistics.rx.cck;
1119	delta_general = &priv->delta_statistics.rx.general;
1120	delta_ht = &priv->delta_statistics.rx.ofdm_ht;
1121	max_ofdm = &priv->max_delta.rx.ofdm;
1122	max_cck = &priv->max_delta.rx.cck;
1123	max_general = &priv->max_delta.rx.general;
1124	max_ht = &priv->max_delta.rx.ofdm_ht;
1125
1126	pos += iwl_dbgfs_statistics_flag(priv, buf, bufsz);
1127	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_header,
1128			 "Statistics_Rx - OFDM:");
1129	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1130			 "ina_cnt:", le32_to_cpu(ofdm->ina_cnt),
1131			 accum_ofdm->ina_cnt,
1132			 delta_ofdm->ina_cnt, max_ofdm->ina_cnt);
1133	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1134			 "fina_cnt:",
1135			 le32_to_cpu(ofdm->fina_cnt), accum_ofdm->fina_cnt,
1136			 delta_ofdm->fina_cnt, max_ofdm->fina_cnt);
1137	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1138			 "plcp_err:",
1139			 le32_to_cpu(ofdm->plcp_err), accum_ofdm->plcp_err,
1140			 delta_ofdm->plcp_err, max_ofdm->plcp_err);
1141	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1142			 "crc32_err:",
1143			 le32_to_cpu(ofdm->crc32_err), accum_ofdm->crc32_err,
1144			 delta_ofdm->crc32_err, max_ofdm->crc32_err);
1145	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1146			 "overrun_err:",
1147			 le32_to_cpu(ofdm->overrun_err),
1148			 accum_ofdm->overrun_err,
1149			 delta_ofdm->overrun_err, max_ofdm->overrun_err);
1150	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1151			 "early_overrun_err:",
1152			 le32_to_cpu(ofdm->early_overrun_err),
1153			 accum_ofdm->early_overrun_err,
1154			 delta_ofdm->early_overrun_err,
1155			 max_ofdm->early_overrun_err);
1156	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1157			 "crc32_good:",
1158			 le32_to_cpu(ofdm->crc32_good),
1159			 accum_ofdm->crc32_good,
1160			 delta_ofdm->crc32_good, max_ofdm->crc32_good);
1161	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1162			 "false_alarm_cnt:",
1163			 le32_to_cpu(ofdm->false_alarm_cnt),
1164			 accum_ofdm->false_alarm_cnt,
1165			 delta_ofdm->false_alarm_cnt,
1166			 max_ofdm->false_alarm_cnt);
1167	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1168			 "fina_sync_err_cnt:",
1169			 le32_to_cpu(ofdm->fina_sync_err_cnt),
1170			 accum_ofdm->fina_sync_err_cnt,
1171			 delta_ofdm->fina_sync_err_cnt,
1172			 max_ofdm->fina_sync_err_cnt);
1173	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1174			 "sfd_timeout:",
1175			 le32_to_cpu(ofdm->sfd_timeout),
1176			 accum_ofdm->sfd_timeout,
1177			 delta_ofdm->sfd_timeout,
1178			 max_ofdm->sfd_timeout);
1179	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1180			 "fina_timeout:",
1181			 le32_to_cpu(ofdm->fina_timeout),
1182			 accum_ofdm->fina_timeout,
1183			 delta_ofdm->fina_timeout,
1184			 max_ofdm->fina_timeout);
1185	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1186			 "unresponded_rts:",
1187			 le32_to_cpu(ofdm->unresponded_rts),
1188			 accum_ofdm->unresponded_rts,
1189			 delta_ofdm->unresponded_rts,
1190			 max_ofdm->unresponded_rts);
1191	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1192			"rxe_frame_lmt_ovrun:",
1193			 le32_to_cpu(ofdm->rxe_frame_limit_overrun),
1194			 accum_ofdm->rxe_frame_limit_overrun,
1195			 delta_ofdm->rxe_frame_limit_overrun,
1196			 max_ofdm->rxe_frame_limit_overrun);
1197	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1198			 "sent_ack_cnt:",
1199			 le32_to_cpu(ofdm->sent_ack_cnt),
1200			 accum_ofdm->sent_ack_cnt,
1201			 delta_ofdm->sent_ack_cnt,
1202			 max_ofdm->sent_ack_cnt);
1203	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1204			 "sent_cts_cnt:",
1205			 le32_to_cpu(ofdm->sent_cts_cnt),
1206			 accum_ofdm->sent_cts_cnt,
1207			 delta_ofdm->sent_cts_cnt, max_ofdm->sent_cts_cnt);
1208	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1209			 "sent_ba_rsp_cnt:",
1210			 le32_to_cpu(ofdm->sent_ba_rsp_cnt),
1211			 accum_ofdm->sent_ba_rsp_cnt,
1212			 delta_ofdm->sent_ba_rsp_cnt,
1213			 max_ofdm->sent_ba_rsp_cnt);
1214	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1215			 "dsp_self_kill:",
1216			 le32_to_cpu(ofdm->dsp_self_kill),
1217			 accum_ofdm->dsp_self_kill,
1218			 delta_ofdm->dsp_self_kill,
1219			 max_ofdm->dsp_self_kill);
1220	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1221			 "mh_format_err:",
1222			 le32_to_cpu(ofdm->mh_format_err),
1223			 accum_ofdm->mh_format_err,
1224			 delta_ofdm->mh_format_err,
1225			 max_ofdm->mh_format_err);
1226	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1227			 "re_acq_main_rssi_sum:",
1228			 le32_to_cpu(ofdm->re_acq_main_rssi_sum),
1229			 accum_ofdm->re_acq_main_rssi_sum,
1230			 delta_ofdm->re_acq_main_rssi_sum,
1231			max_ofdm->re_acq_main_rssi_sum);
1232
1233	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_header,
1234			 "Statistics_Rx - CCK:");
1235	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1236			 "ina_cnt:",
1237			 le32_to_cpu(cck->ina_cnt), accum_cck->ina_cnt,
1238			 delta_cck->ina_cnt, max_cck->ina_cnt);
1239	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1240			 "fina_cnt:",
1241			 le32_to_cpu(cck->fina_cnt), accum_cck->fina_cnt,
1242			 delta_cck->fina_cnt, max_cck->fina_cnt);
1243	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1244			 "plcp_err:",
1245			 le32_to_cpu(cck->plcp_err), accum_cck->plcp_err,
1246			 delta_cck->plcp_err, max_cck->plcp_err);
1247	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1248			 "crc32_err:",
1249			 le32_to_cpu(cck->crc32_err), accum_cck->crc32_err,
1250			 delta_cck->crc32_err, max_cck->crc32_err);
1251	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1252			 "overrun_err:",
1253			 le32_to_cpu(cck->overrun_err),
1254			 accum_cck->overrun_err,
1255			 delta_cck->overrun_err, max_cck->overrun_err);
1256	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1257			 "early_overrun_err:",
1258			 le32_to_cpu(cck->early_overrun_err),
1259			 accum_cck->early_overrun_err,
1260			 delta_cck->early_overrun_err,
1261			 max_cck->early_overrun_err);
1262	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1263			 "crc32_good:",
1264			 le32_to_cpu(cck->crc32_good), accum_cck->crc32_good,
1265			 delta_cck->crc32_good,
1266			 max_cck->crc32_good);
1267	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1268			 "false_alarm_cnt:",
1269			 le32_to_cpu(cck->false_alarm_cnt),
1270			 accum_cck->false_alarm_cnt,
1271			 delta_cck->false_alarm_cnt, max_cck->false_alarm_cnt);
1272	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1273			 "fina_sync_err_cnt:",
1274			 le32_to_cpu(cck->fina_sync_err_cnt),
1275			 accum_cck->fina_sync_err_cnt,
1276			 delta_cck->fina_sync_err_cnt,
1277			 max_cck->fina_sync_err_cnt);
1278	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1279			 "sfd_timeout:",
1280			 le32_to_cpu(cck->sfd_timeout),
1281			 accum_cck->sfd_timeout,
1282			 delta_cck->sfd_timeout, max_cck->sfd_timeout);
1283	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1284			 "fina_timeout:",
1285			 le32_to_cpu(cck->fina_timeout),
1286			 accum_cck->fina_timeout,
1287			 delta_cck->fina_timeout, max_cck->fina_timeout);
1288	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1289			 "unresponded_rts:",
1290			 le32_to_cpu(cck->unresponded_rts),
1291			 accum_cck->unresponded_rts,
1292			 delta_cck->unresponded_rts,
1293			 max_cck->unresponded_rts);
1294	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1295			"rxe_frame_lmt_ovrun:",
1296			 le32_to_cpu(cck->rxe_frame_limit_overrun),
1297			 accum_cck->rxe_frame_limit_overrun,
1298			 delta_cck->rxe_frame_limit_overrun,
1299			 max_cck->rxe_frame_limit_overrun);
1300	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1301			 "sent_ack_cnt:",
1302			 le32_to_cpu(cck->sent_ack_cnt),
1303			 accum_cck->sent_ack_cnt,
1304			 delta_cck->sent_ack_cnt,
1305			 max_cck->sent_ack_cnt);
1306	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1307			 "sent_cts_cnt:",
1308			 le32_to_cpu(cck->sent_cts_cnt),
1309			 accum_cck->sent_cts_cnt,
1310			 delta_cck->sent_cts_cnt,
1311			 max_cck->sent_cts_cnt);
1312	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1313			 "sent_ba_rsp_cnt:",
1314			 le32_to_cpu(cck->sent_ba_rsp_cnt),
1315			 accum_cck->sent_ba_rsp_cnt,
1316			 delta_cck->sent_ba_rsp_cnt,
1317			 max_cck->sent_ba_rsp_cnt);
1318	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1319			 "dsp_self_kill:",
1320			 le32_to_cpu(cck->dsp_self_kill),
1321			 accum_cck->dsp_self_kill,
1322			 delta_cck->dsp_self_kill,
1323			 max_cck->dsp_self_kill);
1324	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1325			 "mh_format_err:",
1326			 le32_to_cpu(cck->mh_format_err),
1327			 accum_cck->mh_format_err,
1328			 delta_cck->mh_format_err, max_cck->mh_format_err);
1329	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1330			 "re_acq_main_rssi_sum:",
1331			 le32_to_cpu(cck->re_acq_main_rssi_sum),
1332			 accum_cck->re_acq_main_rssi_sum,
1333			 delta_cck->re_acq_main_rssi_sum,
1334			 max_cck->re_acq_main_rssi_sum);
1335
1336	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_header,
1337			"Statistics_Rx - GENERAL:");
1338	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1339			 "bogus_cts:",
1340			 le32_to_cpu(general->bogus_cts),
1341			 accum_general->bogus_cts,
1342			 delta_general->bogus_cts, max_general->bogus_cts);
1343	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1344			 "bogus_ack:",
1345			 le32_to_cpu(general->bogus_ack),
1346			 accum_general->bogus_ack,
1347			 delta_general->bogus_ack, max_general->bogus_ack);
1348	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1349			 "non_bssid_frames:",
1350			 le32_to_cpu(general->non_bssid_frames),
1351			 accum_general->non_bssid_frames,
1352			 delta_general->non_bssid_frames,
1353			 max_general->non_bssid_frames);
1354	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1355			 "filtered_frames:",
1356			 le32_to_cpu(general->filtered_frames),
1357			 accum_general->filtered_frames,
1358			 delta_general->filtered_frames,
1359			 max_general->filtered_frames);
1360	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1361			 "non_channel_beacons:",
1362			 le32_to_cpu(general->non_channel_beacons),
1363			 accum_general->non_channel_beacons,
1364			 delta_general->non_channel_beacons,
1365			 max_general->non_channel_beacons);
1366	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1367			 "channel_beacons:",
1368			 le32_to_cpu(general->channel_beacons),
1369			 accum_general->channel_beacons,
1370			 delta_general->channel_beacons,
1371			 max_general->channel_beacons);
1372	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1373			 "num_missed_bcon:",
1374			 le32_to_cpu(general->num_missed_bcon),
1375			 accum_general->num_missed_bcon,
1376			 delta_general->num_missed_bcon,
1377			 max_general->num_missed_bcon);
1378	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1379			"adc_rx_saturation_time:",
1380			 le32_to_cpu(general->adc_rx_saturation_time),
1381			 accum_general->adc_rx_saturation_time,
1382			 delta_general->adc_rx_saturation_time,
1383			 max_general->adc_rx_saturation_time);
1384	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1385			"ina_detect_search_tm:",
1386			 le32_to_cpu(general->ina_detection_search_time),
1387			 accum_general->ina_detection_search_time,
1388			 delta_general->ina_detection_search_time,
1389			 max_general->ina_detection_search_time);
1390	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1391			 "beacon_silence_rssi_a:",
1392			 le32_to_cpu(general->beacon_silence_rssi_a),
1393			 accum_general->beacon_silence_rssi_a,
1394			 delta_general->beacon_silence_rssi_a,
1395			 max_general->beacon_silence_rssi_a);
1396	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1397			 "beacon_silence_rssi_b:",
1398			 le32_to_cpu(general->beacon_silence_rssi_b),
1399			 accum_general->beacon_silence_rssi_b,
1400			 delta_general->beacon_silence_rssi_b,
1401			 max_general->beacon_silence_rssi_b);
1402	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1403			 "beacon_silence_rssi_c:",
1404			 le32_to_cpu(general->beacon_silence_rssi_c),
1405			 accum_general->beacon_silence_rssi_c,
1406			 delta_general->beacon_silence_rssi_c,
1407			 max_general->beacon_silence_rssi_c);
1408	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1409			"interference_data_flag:",
1410			 le32_to_cpu(general->interference_data_flag),
1411			 accum_general->interference_data_flag,
1412			 delta_general->interference_data_flag,
1413			 max_general->interference_data_flag);
1414	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1415			 "channel_load:",
1416			 le32_to_cpu(general->channel_load),
1417			 accum_general->channel_load,
1418			 delta_general->channel_load,
1419			 max_general->channel_load);
1420	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1421			 "dsp_false_alarms:",
1422			 le32_to_cpu(general->dsp_false_alarms),
1423			 accum_general->dsp_false_alarms,
1424			 delta_general->dsp_false_alarms,
1425			 max_general->dsp_false_alarms);
1426	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1427			 "beacon_rssi_a:",
1428			 le32_to_cpu(general->beacon_rssi_a),
1429			 accum_general->beacon_rssi_a,
1430			 delta_general->beacon_rssi_a,
1431			 max_general->beacon_rssi_a);
1432	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1433			 "beacon_rssi_b:",
1434			 le32_to_cpu(general->beacon_rssi_b),
1435			 accum_general->beacon_rssi_b,
1436			 delta_general->beacon_rssi_b,
1437			 max_general->beacon_rssi_b);
1438	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1439			 "beacon_rssi_c:",
1440			 le32_to_cpu(general->beacon_rssi_c),
1441			 accum_general->beacon_rssi_c,
1442			 delta_general->beacon_rssi_c,
1443			 max_general->beacon_rssi_c);
1444	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1445			 "beacon_energy_a:",
1446			 le32_to_cpu(general->beacon_energy_a),
1447			 accum_general->beacon_energy_a,
1448			 delta_general->beacon_energy_a,
1449			 max_general->beacon_energy_a);
1450	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1451			 "beacon_energy_b:",
1452			 le32_to_cpu(general->beacon_energy_b),
1453			 accum_general->beacon_energy_b,
1454			 delta_general->beacon_energy_b,
1455			 max_general->beacon_energy_b);
1456	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1457			 "beacon_energy_c:",
1458			 le32_to_cpu(general->beacon_energy_c),
1459			 accum_general->beacon_energy_c,
1460			 delta_general->beacon_energy_c,
1461			 max_general->beacon_energy_c);
1462
1463	pos += scnprintf(buf + pos, bufsz - pos, "Statistics_Rx - OFDM_HT:\n");
1464	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_header,
1465			"Statistics_Rx - OFDM_HT:");
1466	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1467			 "plcp_err:",
1468			 le32_to_cpu(ht->plcp_err), accum_ht->plcp_err,
1469			 delta_ht->plcp_err, max_ht->plcp_err);
1470	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1471			 "overrun_err:",
1472			 le32_to_cpu(ht->overrun_err), accum_ht->overrun_err,
1473			 delta_ht->overrun_err, max_ht->overrun_err);
1474	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1475			 "early_overrun_err:",
1476			 le32_to_cpu(ht->early_overrun_err),
1477			 accum_ht->early_overrun_err,
1478			 delta_ht->early_overrun_err,
1479			 max_ht->early_overrun_err);
1480	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1481			 "crc32_good:",
1482			 le32_to_cpu(ht->crc32_good), accum_ht->crc32_good,
1483			 delta_ht->crc32_good, max_ht->crc32_good);
1484	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1485			 "crc32_err:",
1486			 le32_to_cpu(ht->crc32_err), accum_ht->crc32_err,
1487			 delta_ht->crc32_err, max_ht->crc32_err);
1488	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1489			 "mh_format_err:",
1490			 le32_to_cpu(ht->mh_format_err),
1491			 accum_ht->mh_format_err,
1492			 delta_ht->mh_format_err, max_ht->mh_format_err);
1493	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1494			 "agg_crc32_good:",
1495			 le32_to_cpu(ht->agg_crc32_good),
1496			 accum_ht->agg_crc32_good,
1497			 delta_ht->agg_crc32_good, max_ht->agg_crc32_good);
1498	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1499			 "agg_mpdu_cnt:",
1500			 le32_to_cpu(ht->agg_mpdu_cnt),
1501			 accum_ht->agg_mpdu_cnt,
1502			 delta_ht->agg_mpdu_cnt, max_ht->agg_mpdu_cnt);
1503	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1504			 "agg_cnt:",
1505			 le32_to_cpu(ht->agg_cnt), accum_ht->agg_cnt,
1506			 delta_ht->agg_cnt, max_ht->agg_cnt);
1507	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1508			 "unsupport_mcs:",
1509			 le32_to_cpu(ht->unsupport_mcs),
1510			 accum_ht->unsupport_mcs,
1511			 delta_ht->unsupport_mcs, max_ht->unsupport_mcs);
1512
1513	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1514	kfree(buf);
1515	return ret;
1516}
1517
1518static ssize_t iwl_dbgfs_ucode_tx_stats_read(struct file *file,
1519					char __user *user_buf,
1520					size_t count, loff_t *ppos)
1521{
1522	struct iwl_priv *priv = file->private_data;
1523	int pos = 0;
1524	char *buf;
1525	int bufsz = (sizeof(struct statistics_tx) * 48) + 250;
1526	ssize_t ret;
1527	struct statistics_tx *tx, *accum_tx, *delta_tx, *max_tx;
1528
1529	if (!iwl_is_alive(priv))
1530		return -EAGAIN;
1531
1532	buf = kzalloc(bufsz, GFP_KERNEL);
1533	if (!buf) {
1534		IWL_ERR(priv, "Can not allocate Buffer\n");
1535		return -ENOMEM;
1536	}
1537
1538	/* the statistic information display here is based on
1539	 * the last statistics notification from uCode
1540	 * might not reflect the current uCode activity
1541	 */
1542	tx = &priv->statistics.tx;
1543	accum_tx = &priv->accum_statistics.tx;
1544	delta_tx = &priv->delta_statistics.tx;
1545	max_tx = &priv->max_delta.tx;
1546	pos += iwl_dbgfs_statistics_flag(priv, buf, bufsz);
1547	pos += scnprintf(buf + pos, bufsz - pos,  ucode_stats_header,
1548			"Statistics_Tx:");
1549	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1550			 "preamble:",
1551			 le32_to_cpu(tx->preamble_cnt),
1552			 accum_tx->preamble_cnt,
1553			 delta_tx->preamble_cnt, max_tx->preamble_cnt);
1554	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1555			 "rx_detected_cnt:",
1556			 le32_to_cpu(tx->rx_detected_cnt),
1557			 accum_tx->rx_detected_cnt,
1558			 delta_tx->rx_detected_cnt, max_tx->rx_detected_cnt);
1559	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1560			 "bt_prio_defer_cnt:",
1561			 le32_to_cpu(tx->bt_prio_defer_cnt),
1562			 accum_tx->bt_prio_defer_cnt,
1563			 delta_tx->bt_prio_defer_cnt,
1564			 max_tx->bt_prio_defer_cnt);
1565	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1566			 "bt_prio_kill_cnt:",
1567			 le32_to_cpu(tx->bt_prio_kill_cnt),
1568			 accum_tx->bt_prio_kill_cnt,
1569			 delta_tx->bt_prio_kill_cnt,
1570			 max_tx->bt_prio_kill_cnt);
1571	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1572			 "few_bytes_cnt:",
1573			 le32_to_cpu(tx->few_bytes_cnt),
1574			 accum_tx->few_bytes_cnt,
1575			 delta_tx->few_bytes_cnt, max_tx->few_bytes_cnt);
1576	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1577			 "cts_timeout:",
1578			 le32_to_cpu(tx->cts_timeout), accum_tx->cts_timeout,
1579			 delta_tx->cts_timeout, max_tx->cts_timeout);
1580	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1581			 "ack_timeout:",
1582			 le32_to_cpu(tx->ack_timeout),
1583			 accum_tx->ack_timeout,
1584			 delta_tx->ack_timeout, max_tx->ack_timeout);
1585	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1586			 "expected_ack_cnt:",
1587			 le32_to_cpu(tx->expected_ack_cnt),
1588			 accum_tx->expected_ack_cnt,
1589			 delta_tx->expected_ack_cnt,
1590			 max_tx->expected_ack_cnt);
1591	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1592			 "actual_ack_cnt:",
1593			 le32_to_cpu(tx->actual_ack_cnt),
1594			 accum_tx->actual_ack_cnt,
1595			 delta_tx->actual_ack_cnt,
1596			 max_tx->actual_ack_cnt);
1597	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1598			 "dump_msdu_cnt:",
1599			 le32_to_cpu(tx->dump_msdu_cnt),
1600			 accum_tx->dump_msdu_cnt,
1601			 delta_tx->dump_msdu_cnt,
1602			 max_tx->dump_msdu_cnt);
1603	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1604			 "abort_nxt_frame_mismatch:",
1605			 le32_to_cpu(tx->burst_abort_next_frame_mismatch_cnt),
1606			 accum_tx->burst_abort_next_frame_mismatch_cnt,
1607			 delta_tx->burst_abort_next_frame_mismatch_cnt,
1608			 max_tx->burst_abort_next_frame_mismatch_cnt);
1609	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1610			 "abort_missing_nxt_frame:",
1611			 le32_to_cpu(tx->burst_abort_missing_next_frame_cnt),
1612			 accum_tx->burst_abort_missing_next_frame_cnt,
1613			 delta_tx->burst_abort_missing_next_frame_cnt,
1614			 max_tx->burst_abort_missing_next_frame_cnt);
1615	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1616			 "cts_timeout_collision:",
1617			 le32_to_cpu(tx->cts_timeout_collision),
1618			 accum_tx->cts_timeout_collision,
1619			 delta_tx->cts_timeout_collision,
1620			 max_tx->cts_timeout_collision);
1621	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1622			"ack_ba_timeout_collision:",
1623			 le32_to_cpu(tx->ack_or_ba_timeout_collision),
1624			 accum_tx->ack_or_ba_timeout_collision,
1625			 delta_tx->ack_or_ba_timeout_collision,
1626			 max_tx->ack_or_ba_timeout_collision);
1627	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1628			 "agg ba_timeout:",
1629			 le32_to_cpu(tx->agg.ba_timeout),
1630			 accum_tx->agg.ba_timeout,
1631			 delta_tx->agg.ba_timeout,
1632			 max_tx->agg.ba_timeout);
1633	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1634			"agg ba_resched_frames:",
1635			 le32_to_cpu(tx->agg.ba_reschedule_frames),
1636			 accum_tx->agg.ba_reschedule_frames,
1637			 delta_tx->agg.ba_reschedule_frames,
1638			 max_tx->agg.ba_reschedule_frames);
1639	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1640			"agg scd_query_agg_frame:",
1641			 le32_to_cpu(tx->agg.scd_query_agg_frame_cnt),
1642			 accum_tx->agg.scd_query_agg_frame_cnt,
1643			 delta_tx->agg.scd_query_agg_frame_cnt,
1644			 max_tx->agg.scd_query_agg_frame_cnt);
1645	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1646			 "agg scd_query_no_agg:",
1647			 le32_to_cpu(tx->agg.scd_query_no_agg),
1648			 accum_tx->agg.scd_query_no_agg,
1649			 delta_tx->agg.scd_query_no_agg,
1650			 max_tx->agg.scd_query_no_agg);
1651	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1652			 "agg scd_query_agg:",
1653			 le32_to_cpu(tx->agg.scd_query_agg),
1654			 accum_tx->agg.scd_query_agg,
1655			 delta_tx->agg.scd_query_agg,
1656			 max_tx->agg.scd_query_agg);
1657	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1658			"agg scd_query_mismatch:",
1659			 le32_to_cpu(tx->agg.scd_query_mismatch),
1660			 accum_tx->agg.scd_query_mismatch,
1661			 delta_tx->agg.scd_query_mismatch,
1662			 max_tx->agg.scd_query_mismatch);
1663	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1664			 "agg frame_not_ready:",
1665			 le32_to_cpu(tx->agg.frame_not_ready),
1666			 accum_tx->agg.frame_not_ready,
1667			 delta_tx->agg.frame_not_ready,
1668			 max_tx->agg.frame_not_ready);
1669	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1670			 "agg underrun:",
1671			 le32_to_cpu(tx->agg.underrun),
1672			 accum_tx->agg.underrun,
1673			 delta_tx->agg.underrun, max_tx->agg.underrun);
1674	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1675			 "agg bt_prio_kill:",
1676			 le32_to_cpu(tx->agg.bt_prio_kill),
1677			 accum_tx->agg.bt_prio_kill,
1678			 delta_tx->agg.bt_prio_kill,
1679			 max_tx->agg.bt_prio_kill);
1680	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1681			 "agg rx_ba_rsp_cnt:",
1682			 le32_to_cpu(tx->agg.rx_ba_rsp_cnt),
1683			 accum_tx->agg.rx_ba_rsp_cnt,
1684			 delta_tx->agg.rx_ba_rsp_cnt,
1685			 max_tx->agg.rx_ba_rsp_cnt);
1686
1687	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1688	kfree(buf);
1689	return ret;
1690}
1691
1692static ssize_t iwl_dbgfs_ucode_general_stats_read(struct file *file,
1693					char __user *user_buf,
1694					size_t count, loff_t *ppos)
1695{
1696	struct iwl_priv *priv = file->private_data;
1697	int pos = 0;
1698	char *buf;
1699	int bufsz = sizeof(struct statistics_general) * 10 + 300;
1700	ssize_t ret;
1701	struct statistics_general *general, *accum_general;
1702	struct statistics_general *delta_general, *max_general;
1703	struct statistics_dbg *dbg, *accum_dbg, *delta_dbg, *max_dbg;
1704	struct statistics_div *div, *accum_div, *delta_div, *max_div;
1705
1706	if (!iwl_is_alive(priv))
1707		return -EAGAIN;
1708
1709	buf = kzalloc(bufsz, GFP_KERNEL);
1710	if (!buf) {
1711		IWL_ERR(priv, "Can not allocate Buffer\n");
1712		return -ENOMEM;
1713	}
1714
1715	/* the statistic information display here is based on
1716	 * the last statistics notification from uCode
1717	 * might not reflect the current uCode activity
1718	 */
1719	general = &priv->statistics.general;
1720	dbg = &priv->statistics.general.dbg;
1721	div = &priv->statistics.general.div;
1722	accum_general = &priv->accum_statistics.general;
1723	delta_general = &priv->delta_statistics.general;
1724	max_general = &priv->max_delta.general;
1725	accum_dbg = &priv->accum_statistics.general.dbg;
1726	delta_dbg = &priv->delta_statistics.general.dbg;
1727	max_dbg = &priv->max_delta.general.dbg;
1728	accum_div = &priv->accum_statistics.general.div;
1729	delta_div = &priv->delta_statistics.general.div;
1730	max_div = &priv->max_delta.general.div;
1731	pos += iwl_dbgfs_statistics_flag(priv, buf, bufsz);
1732	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_header,
1733			"Statistics_General:");
1734	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_short_format,
1735			 "temperature:",
1736			 le32_to_cpu(general->temperature));
1737	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_short_format,
1738			 "temperature_m:",
1739			 le32_to_cpu(general->temperature_m));
1740	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1741			 "burst_check:",
1742			 le32_to_cpu(dbg->burst_check),
1743			 accum_dbg->burst_check,
1744			 delta_dbg->burst_check, max_dbg->burst_check);
1745	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1746			 "burst_count:",
1747			 le32_to_cpu(dbg->burst_count),
1748			 accum_dbg->burst_count,
1749			 delta_dbg->burst_count, max_dbg->burst_count);
1750	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1751			 "sleep_time:",
1752			 le32_to_cpu(general->sleep_time),
1753			 accum_general->sleep_time,
1754			 delta_general->sleep_time, max_general->sleep_time);
1755	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1756			 "slots_out:",
1757			 le32_to_cpu(general->slots_out),
1758			 accum_general->slots_out,
1759			 delta_general->slots_out, max_general->slots_out);
1760	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1761			 "slots_idle:",
1762			 le32_to_cpu(general->slots_idle),
1763			 accum_general->slots_idle,
1764			 delta_general->slots_idle, max_general->slots_idle);
1765	pos += scnprintf(buf + pos, bufsz - pos, "ttl_timestamp:\t\t\t%u\n",
1766			 le32_to_cpu(general->ttl_timestamp));
1767	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1768			 "tx_on_a:",
1769			 le32_to_cpu(div->tx_on_a), accum_div->tx_on_a,
1770			 delta_div->tx_on_a, max_div->tx_on_a);
1771	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1772			 "tx_on_b:",
1773			 le32_to_cpu(div->tx_on_b), accum_div->tx_on_b,
1774			 delta_div->tx_on_b, max_div->tx_on_b);
1775	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1776			 "exec_time:",
1777			 le32_to_cpu(div->exec_time), accum_div->exec_time,
1778			 delta_div->exec_time, max_div->exec_time);
1779	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1780			 "probe_time:",
1781			 le32_to_cpu(div->probe_time), accum_div->probe_time,
1782			 delta_div->probe_time, max_div->probe_time);
1783	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1784			 "rx_enable_counter:",
1785			 le32_to_cpu(general->rx_enable_counter),
1786			 accum_general->rx_enable_counter,
1787			 delta_general->rx_enable_counter,
1788			 max_general->rx_enable_counter);
1789	pos += scnprintf(buf + pos, bufsz - pos, ucode_stats_format,
1790			 "num_of_sos_states:",
1791			 le32_to_cpu(general->num_of_sos_states),
1792			 accum_general->num_of_sos_states,
1793			 delta_general->num_of_sos_states,
1794			 max_general->num_of_sos_states);
1795	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1796	kfree(buf);
1797	return ret;
1798}
1799
1800static ssize_t iwl_dbgfs_sensitivity_read(struct file *file,
1801					char __user *user_buf,
1802					size_t count, loff_t *ppos) {
1803
1804	struct iwl_priv *priv = file->private_data;
1805	int pos = 0;
1806	int cnt = 0;
1807	char *buf;
1808	int bufsz = sizeof(struct iwl_sensitivity_data) * 4 + 100;
1809	ssize_t ret;
1810	struct iwl_sensitivity_data *data;
1811
1812	data = &priv->sensitivity_data;
1813	buf = kzalloc(bufsz, GFP_KERNEL);
1814	if (!buf) {
1815		IWL_ERR(priv, "Can not allocate Buffer\n");
1816		return -ENOMEM;
1817	}
1818
1819	pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_ofdm:\t\t\t %u\n",
1820			data->auto_corr_ofdm);
1821	pos += scnprintf(buf + pos, bufsz - pos,
1822			"auto_corr_ofdm_mrc:\t\t %u\n",
1823			data->auto_corr_ofdm_mrc);
1824	pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_ofdm_x1:\t\t %u\n",
1825			data->auto_corr_ofdm_x1);
1826	pos += scnprintf(buf + pos, bufsz - pos,
1827			"auto_corr_ofdm_mrc_x1:\t\t %u\n",
1828			data->auto_corr_ofdm_mrc_x1);
1829	pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_cck:\t\t\t %u\n",
1830			data->auto_corr_cck);
1831	pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_cck_mrc:\t\t %u\n",
1832			data->auto_corr_cck_mrc);
1833	pos += scnprintf(buf + pos, bufsz - pos,
1834			"last_bad_plcp_cnt_ofdm:\t\t %u\n",
1835			data->last_bad_plcp_cnt_ofdm);
1836	pos += scnprintf(buf + pos, bufsz - pos, "last_fa_cnt_ofdm:\t\t %u\n",
1837			data->last_fa_cnt_ofdm);
1838	pos += scnprintf(buf + pos, bufsz - pos,
1839			"last_bad_plcp_cnt_cck:\t\t %u\n",
1840			data->last_bad_plcp_cnt_cck);
1841	pos += scnprintf(buf + pos, bufsz - pos, "last_fa_cnt_cck:\t\t %u\n",
1842			data->last_fa_cnt_cck);
1843	pos += scnprintf(buf + pos, bufsz - pos, "nrg_curr_state:\t\t\t %u\n",
1844			data->nrg_curr_state);
1845	pos += scnprintf(buf + pos, bufsz - pos, "nrg_prev_state:\t\t\t %u\n",
1846			data->nrg_prev_state);
1847	pos += scnprintf(buf + pos, bufsz - pos, "nrg_value:\t\t\t");
1848	for (cnt = 0; cnt < 10; cnt++) {
1849		pos += scnprintf(buf + pos, bufsz - pos, " %u",
1850				data->nrg_value[cnt]);
1851	}
1852	pos += scnprintf(buf + pos, bufsz - pos, "\n");
1853	pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_rssi:\t\t");
1854	for (cnt = 0; cnt < NRG_NUM_PREV_STAT_L; cnt++) {
1855		pos += scnprintf(buf + pos, bufsz - pos, " %u",
1856				data->nrg_silence_rssi[cnt]);
1857	}
1858	pos += scnprintf(buf + pos, bufsz - pos, "\n");
1859	pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_ref:\t\t %u\n",
1860			data->nrg_silence_ref);
1861	pos += scnprintf(buf + pos, bufsz - pos, "nrg_energy_idx:\t\t\t %u\n",
1862			data->nrg_energy_idx);
1863	pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_idx:\t\t %u\n",
1864			data->nrg_silence_idx);
1865	pos += scnprintf(buf + pos, bufsz - pos, "nrg_th_cck:\t\t\t %u\n",
1866			data->nrg_th_cck);
1867	pos += scnprintf(buf + pos, bufsz - pos,
1868			"nrg_auto_corr_silence_diff:\t %u\n",
1869			data->nrg_auto_corr_silence_diff);
1870	pos += scnprintf(buf + pos, bufsz - pos, "num_in_cck_no_fa:\t\t %u\n",
1871			data->num_in_cck_no_fa);
1872	pos += scnprintf(buf + pos, bufsz - pos, "nrg_th_ofdm:\t\t\t %u\n",
1873			data->nrg_th_ofdm);
1874
1875	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1876	kfree(buf);
1877	return ret;
1878}
1879
1880
1881static ssize_t iwl_dbgfs_chain_noise_read(struct file *file,
1882					char __user *user_buf,
1883					size_t count, loff_t *ppos) {
1884
1885	struct iwl_priv *priv = file->private_data;
1886	int pos = 0;
1887	int cnt = 0;
1888	char *buf;
1889	int bufsz = sizeof(struct iwl_chain_noise_data) * 4 + 100;
1890	ssize_t ret;
1891	struct iwl_chain_noise_data *data;
1892
1893	data = &priv->chain_noise_data;
1894	buf = kzalloc(bufsz, GFP_KERNEL);
1895	if (!buf) {
1896		IWL_ERR(priv, "Can not allocate Buffer\n");
1897		return -ENOMEM;
1898	}
1899
1900	pos += scnprintf(buf + pos, bufsz - pos, "active_chains:\t\t\t %u\n",
1901			data->active_chains);
1902	pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_a:\t\t\t %u\n",
1903			data->chain_noise_a);
1904	pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_b:\t\t\t %u\n",
1905			data->chain_noise_b);
1906	pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_c:\t\t\t %u\n",
1907			data->chain_noise_c);
1908	pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_a:\t\t\t %u\n",
1909			data->chain_signal_a);
1910	pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_b:\t\t\t %u\n",
1911			data->chain_signal_b);
1912	pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_c:\t\t\t %u\n",
1913			data->chain_signal_c);
1914	pos += scnprintf(buf + pos, bufsz - pos, "beacon_count:\t\t\t %u\n",
1915			data->beacon_count);
1916
1917	pos += scnprintf(buf + pos, bufsz - pos, "disconn_array:\t\t\t");
1918	for (cnt = 0; cnt < NUM_RX_CHAINS; cnt++) {
1919		pos += scnprintf(buf + pos, bufsz - pos, " %u",
1920				data->disconn_array[cnt]);
1921	}
1922	pos += scnprintf(buf + pos, bufsz - pos, "\n");
1923	pos += scnprintf(buf + pos, bufsz - pos, "delta_gain_code:\t\t");
1924	for (cnt = 0; cnt < NUM_RX_CHAINS; cnt++) {
1925		pos += scnprintf(buf + pos, bufsz - pos, " %u",
1926				data->delta_gain_code[cnt]);
1927	}
1928	pos += scnprintf(buf + pos, bufsz - pos, "\n");
1929	pos += scnprintf(buf + pos, bufsz - pos, "radio_write:\t\t\t %u\n",
1930			data->radio_write);
1931	pos += scnprintf(buf + pos, bufsz - pos, "state:\t\t\t\t %u\n",
1932			data->state);
1933
1934	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1935	kfree(buf);
1936	return ret;
1937}
1938
1939static ssize_t iwl_dbgfs_tx_power_read(struct file *file,
1940					char __user *user_buf,
1941					size_t count, loff_t *ppos) {
1942
1943	struct iwl_priv *priv = file->private_data;
1944	char buf[128];
1945	int pos = 0;
1946	const size_t bufsz = sizeof(buf);
1947	struct statistics_tx *tx;
1948
1949	if (!iwl_is_alive(priv))
1950		pos += scnprintf(buf + pos, bufsz - pos, "N/A\n");
1951	else {
1952		tx = &priv->statistics.tx;
1953		if (tx->tx_power.ant_a ||
1954		    tx->tx_power.ant_b ||
1955		    tx->tx_power.ant_c) {
1956			pos += scnprintf(buf + pos, bufsz - pos,
1957				"tx power: (1/2 dB step)\n");
1958			if ((priv->cfg->valid_tx_ant & ANT_A) &&
1959			    tx->tx_power.ant_a)
1960				pos += scnprintf(buf + pos, bufsz - pos,
1961						"\tantenna A: 0x%X\n",
1962						tx->tx_power.ant_a);
1963			if ((priv->cfg->valid_tx_ant & ANT_B) &&
1964			    tx->tx_power.ant_b)
1965				pos += scnprintf(buf + pos, bufsz - pos,
1966						"\tantenna B: 0x%X\n",
1967						tx->tx_power.ant_b);
1968			if ((priv->cfg->valid_tx_ant & ANT_C) &&
1969			    tx->tx_power.ant_c)
1970				pos += scnprintf(buf + pos, bufsz - pos,
1971						"\tantenna C: 0x%X\n",
1972						tx->tx_power.ant_c);
1973		} else
1974			pos += scnprintf(buf + pos, bufsz - pos, "N/A\n");
1975	}
1976	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1977}
1978
1979static ssize_t iwl_dbgfs_power_save_status_read(struct file *file,
1980						    char __user *user_buf,
1981						    size_t count, loff_t *ppos)
1982{
1983	struct iwl_priv *priv = file->private_data;
1984	char buf[60];
1985	int pos = 0;
1986	const size_t bufsz = sizeof(buf);
1987	u32 pwrsave_status;
1988
1989	pwrsave_status = iwl_read32(priv, CSR_GP_CNTRL) &
1990			CSR_GP_REG_POWER_SAVE_STATUS_MSK;
1991
1992	pos += scnprintf(buf + pos, bufsz - pos, "Power Save Status: ");
1993	pos += scnprintf(buf + pos, bufsz - pos, "%s\n",
1994		(pwrsave_status == CSR_GP_REG_NO_POWER_SAVE) ? "none" :
1995		(pwrsave_status == CSR_GP_REG_MAC_POWER_SAVE) ? "MAC" :
1996		(pwrsave_status == CSR_GP_REG_PHY_POWER_SAVE) ? "PHY" :
1997		"error");
1998
1999	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2000}
2001
2002static ssize_t iwl_dbgfs_clear_ucode_statistics_write(struct file *file,
2003					 const char __user *user_buf,
2004					 size_t count, loff_t *ppos)
2005{
2006	struct iwl_priv *priv = file->private_data;
2007	char buf[8];
2008	int buf_size;
2009	int clear;
2010
2011	memset(buf, 0, sizeof(buf));
2012	buf_size = min(count, sizeof(buf) -  1);
2013	if (copy_from_user(buf, user_buf, buf_size))
2014		return -EFAULT;
2015	if (sscanf(buf, "%d", &clear) != 1)
2016		return -EFAULT;
2017
2018	/* make request to uCode to retrieve statistics information */
2019	mutex_lock(&priv->mutex);
2020	iwl_send_statistics_request(priv, CMD_SYNC, true);
2021	mutex_unlock(&priv->mutex);
2022
2023	return count;
2024}
2025
2026static ssize_t iwl_dbgfs_csr_write(struct file *file,
2027					 const char __user *user_buf,
2028					 size_t count, loff_t *ppos)
2029{
2030	struct iwl_priv *priv = file->private_data;
2031	char buf[8];
2032	int buf_size;
2033	int csr;
2034
2035	memset(buf, 0, sizeof(buf));
2036	buf_size = min(count, sizeof(buf) -  1);
2037	if (copy_from_user(buf, user_buf, buf_size))
2038		return -EFAULT;
2039	if (sscanf(buf, "%d", &csr) != 1)
2040		return -EFAULT;
2041
2042	if (priv->cfg->ops->lib->dump_csr)
2043		priv->cfg->ops->lib->dump_csr(priv);
2044
2045	return count;
2046}
2047
2048static ssize_t iwl_dbgfs_ucode_tracing_read(struct file *file,
2049					char __user *user_buf,
2050					size_t count, loff_t *ppos) {
2051
2052	struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
2053	int pos = 0;
2054	char buf[128];
2055	const size_t bufsz = sizeof(buf);
2056	ssize_t ret;
2057
2058	pos += scnprintf(buf + pos, bufsz - pos, "ucode trace timer is %s\n",
2059			priv->event_log.ucode_trace ? "On" : "Off");
2060	pos += scnprintf(buf + pos, bufsz - pos, "non_wraps_count:\t\t %u\n",
2061			priv->event_log.non_wraps_count);
2062	pos += scnprintf(buf + pos, bufsz - pos, "wraps_once_count:\t\t %u\n",
2063			priv->event_log.wraps_once_count);
2064	pos += scnprintf(buf + pos, bufsz - pos, "wraps_more_count:\t\t %u\n",
2065			priv->event_log.wraps_more_count);
2066
2067	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2068	return ret;
2069}
2070
2071static ssize_t iwl_dbgfs_ucode_tracing_write(struct file *file,
2072					 const char __user *user_buf,
2073					 size_t count, loff_t *ppos)
2074{
2075	struct iwl_priv *priv = file->private_data;
2076	char buf[8];
2077	int buf_size;
2078	int trace;
2079
2080	memset(buf, 0, sizeof(buf));
2081	buf_size = min(count, sizeof(buf) -  1);
2082	if (copy_from_user(buf, user_buf, buf_size))
2083		return -EFAULT;
2084	if (sscanf(buf, "%d", &trace) != 1)
2085		return -EFAULT;
2086
2087	if (trace) {
2088		priv->event_log.ucode_trace = true;
2089		/* schedule the ucode timer to occur in UCODE_TRACE_PERIOD */
2090		mod_timer(&priv->ucode_trace,
2091			jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
2092	} else {
2093		priv->event_log.ucode_trace = false;
2094		del_timer_sync(&priv->ucode_trace);
2095	}
2096
2097	return count;
2098}
2099
2100static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
2101					 char __user *user_buf,
2102					 size_t count, loff_t *ppos)
2103{
2104	struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
2105	char *buf;
2106	int pos = 0;
2107	ssize_t ret = -EFAULT;
2108
2109	if (priv->cfg->ops->lib->dump_fh) {
2110		ret = pos = priv->cfg->ops->lib->dump_fh(priv, &buf, true);
2111		if (buf) {
2112			ret = simple_read_from_buffer(user_buf,
2113						      count, ppos, buf, pos);
2114			kfree(buf);
2115		}
2116	}
2117
2118	return ret;
2119}
2120
2121static ssize_t iwl_dbgfs_missed_beacon_read(struct file *file,
2122					char __user *user_buf,
2123					size_t count, loff_t *ppos) {
2124
2125	struct iwl_priv *priv = file->private_data;
2126	int pos = 0;
2127	char buf[12];
2128	const size_t bufsz = sizeof(buf);
2129	ssize_t ret;
2130
2131	pos += scnprintf(buf + pos, bufsz - pos, "%d\n",
2132			priv->missed_beacon_threshold);
2133
2134	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2135	return ret;
2136}
2137
2138static ssize_t iwl_dbgfs_missed_beacon_write(struct file *file,
2139					 const char __user *user_buf,
2140					 size_t count, loff_t *ppos)
2141{
2142	struct iwl_priv *priv = file->private_data;
2143	char buf[8];
2144	int buf_size;
2145	int missed;
2146
2147	memset(buf, 0, sizeof(buf));
2148	buf_size = min(count, sizeof(buf) -  1);
2149	if (copy_from_user(buf, user_buf, buf_size))
2150		return -EFAULT;
2151	if (sscanf(buf, "%d", &missed) != 1)
2152		return -EINVAL;
2153
2154	if (missed < IWL_MISSED_BEACON_THRESHOLD_MIN ||
2155	    missed > IWL_MISSED_BEACON_THRESHOLD_MAX)
2156		priv->missed_beacon_threshold =
2157			IWL_MISSED_BEACON_THRESHOLD_DEF;
2158	else
2159		priv->missed_beacon_threshold = missed;
2160
2161	return count;
2162}
2163
2164static ssize_t iwl_dbgfs_internal_scan_write(struct file *file,
2165					 const char __user *user_buf,
2166					 size_t count, loff_t *ppos)
2167{
2168	struct iwl_priv *priv = file->private_data;
2169	char buf[8];
2170	int buf_size;
2171	int scan;
2172
2173	memset(buf, 0, sizeof(buf));
2174	buf_size = min(count, sizeof(buf) -  1);
2175	if (copy_from_user(buf, user_buf, buf_size))
2176		return -EFAULT;
2177	if (sscanf(buf, "%d", &scan) != 1)
2178		return -EINVAL;
2179
2180	iwl_internal_short_hw_scan(priv);
2181
2182	return count;
2183}
2184
2185static ssize_t iwl_dbgfs_plcp_delta_read(struct file *file,
2186					char __user *user_buf,
2187					size_t count, loff_t *ppos) {
2188
2189	struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
2190	int pos = 0;
2191	char buf[12];
2192	const size_t bufsz = sizeof(buf);
2193	ssize_t ret;
2194
2195	pos += scnprintf(buf + pos, bufsz - pos, "%u\n",
2196			priv->cfg->plcp_delta_threshold);
2197
2198	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2199	return ret;
2200}
2201
2202static ssize_t iwl_dbgfs_plcp_delta_write(struct file *file,
2203					const char __user *user_buf,
2204					size_t count, loff_t *ppos) {
2205
2206	struct iwl_priv *priv = file->private_data;
2207	char buf[8];
2208	int buf_size;
2209	int plcp;
2210
2211	memset(buf, 0, sizeof(buf));
2212	buf_size = min(count, sizeof(buf) -  1);
2213	if (copy_from_user(buf, user_buf, buf_size))
2214		return -EFAULT;
2215	if (sscanf(buf, "%d", &plcp) != 1)
2216		return -EINVAL;
2217	if ((plcp <= IWL_MAX_PLCP_ERR_THRESHOLD_MIN) ||
2218		(plcp > IWL_MAX_PLCP_ERR_THRESHOLD_MAX))
2219		priv->cfg->plcp_delta_threshold =
2220			IWL_MAX_PLCP_ERR_THRESHOLD_DEF;
2221	else
2222		priv->cfg->plcp_delta_threshold = plcp;
2223	return count;
2224}
2225
2226DEBUGFS_READ_FILE_OPS(rx_statistics);
2227DEBUGFS_READ_FILE_OPS(tx_statistics);
2228DEBUGFS_READ_WRITE_FILE_OPS(traffic_log);
2229DEBUGFS_READ_FILE_OPS(rx_queue);
2230DEBUGFS_READ_FILE_OPS(tx_queue);
2231DEBUGFS_READ_FILE_OPS(ucode_rx_stats);
2232DEBUGFS_READ_FILE_OPS(ucode_tx_stats);
2233DEBUGFS_READ_FILE_OPS(ucode_general_stats);
2234DEBUGFS_READ_FILE_OPS(sensitivity);
2235DEBUGFS_READ_FILE_OPS(chain_noise);
2236DEBUGFS_READ_FILE_OPS(tx_power);
2237DEBUGFS_READ_FILE_OPS(power_save_status);
2238DEBUGFS_WRITE_FILE_OPS(clear_ucode_statistics);
2239DEBUGFS_WRITE_FILE_OPS(clear_traffic_statistics);
2240DEBUGFS_WRITE_FILE_OPS(csr);
2241DEBUGFS_READ_WRITE_FILE_OPS(ucode_tracing);
2242DEBUGFS_READ_FILE_OPS(fh_reg);
2243DEBUGFS_READ_WRITE_FILE_OPS(missed_beacon);
2244DEBUGFS_WRITE_FILE_OPS(internal_scan);
2245DEBUGFS_READ_WRITE_FILE_OPS(plcp_delta);
2246
2247/*
2248 * Create the debugfs files and directories
2249 *
2250 */
2251int iwl_dbgfs_register(struct iwl_priv *priv, const char *name)
2252{
2253	struct dentry *phyd = priv->hw->wiphy->debugfsdir;
2254	struct dentry *dir_drv, *dir_data, *dir_rf, *dir_debug;
2255
2256	dir_drv = debugfs_create_dir(name, phyd);
2257	if (!dir_drv)
2258		return -ENOMEM;
2259
2260	priv->debugfs_dir = dir_drv;
2261
2262	dir_data = debugfs_create_dir("data", dir_drv);
2263	if (!dir_data)
2264		goto err;
2265	dir_rf = debugfs_create_dir("rf", dir_drv);
2266	if (!dir_rf)
2267		goto err;
2268	dir_debug = debugfs_create_dir("debug", dir_drv);
2269	if (!dir_debug)
2270		goto err;
2271
2272	DEBUGFS_ADD_FILE(nvm, dir_data, S_IRUSR);
2273	DEBUGFS_ADD_FILE(sram, dir_data, S_IWUSR | S_IRUSR);
2274	DEBUGFS_ADD_FILE(log_event, dir_data, S_IWUSR | S_IRUSR);
2275	DEBUGFS_ADD_FILE(stations, dir_data, S_IRUSR);
2276	DEBUGFS_ADD_FILE(channels, dir_data, S_IRUSR);
2277	DEBUGFS_ADD_FILE(status, dir_data, S_IRUSR);
2278	DEBUGFS_ADD_FILE(interrupt, dir_data, S_IWUSR | S_IRUSR);
2279	DEBUGFS_ADD_FILE(qos, dir_data, S_IRUSR);
2280	DEBUGFS_ADD_FILE(led, dir_data, S_IRUSR);
2281	DEBUGFS_ADD_FILE(sleep_level_override, dir_data, S_IWUSR | S_IRUSR);
2282	DEBUGFS_ADD_FILE(current_sleep_command, dir_data, S_IRUSR);
2283	DEBUGFS_ADD_FILE(thermal_throttling, dir_data, S_IRUSR);
2284	DEBUGFS_ADD_FILE(disable_ht40, dir_data, S_IWUSR | S_IRUSR);
2285	DEBUGFS_ADD_FILE(rx_statistics, dir_debug, S_IRUSR);
2286	DEBUGFS_ADD_FILE(tx_statistics, dir_debug, S_IRUSR);
2287	DEBUGFS_ADD_FILE(traffic_log, dir_debug, S_IWUSR | S_IRUSR);
2288	DEBUGFS_ADD_FILE(rx_queue, dir_debug, S_IRUSR);
2289	DEBUGFS_ADD_FILE(tx_queue, dir_debug, S_IRUSR);
2290	DEBUGFS_ADD_FILE(tx_power, dir_debug, S_IRUSR);
2291	DEBUGFS_ADD_FILE(power_save_status, dir_debug, S_IRUSR);
2292	DEBUGFS_ADD_FILE(clear_ucode_statistics, dir_debug, S_IWUSR);
2293	DEBUGFS_ADD_FILE(clear_traffic_statistics, dir_debug, S_IWUSR);
2294	DEBUGFS_ADD_FILE(csr, dir_debug, S_IWUSR);
2295	DEBUGFS_ADD_FILE(fh_reg, dir_debug, S_IRUSR);
2296	DEBUGFS_ADD_FILE(missed_beacon, dir_debug, S_IWUSR);
2297	DEBUGFS_ADD_FILE(internal_scan, dir_debug, S_IWUSR);
2298	DEBUGFS_ADD_FILE(plcp_delta, dir_debug, S_IWUSR | S_IRUSR);
2299	if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) != CSR_HW_REV_TYPE_3945) {
2300		DEBUGFS_ADD_FILE(ucode_rx_stats, dir_debug, S_IRUSR);
2301		DEBUGFS_ADD_FILE(ucode_tx_stats, dir_debug, S_IRUSR);
2302		DEBUGFS_ADD_FILE(ucode_general_stats, dir_debug, S_IRUSR);
2303		DEBUGFS_ADD_FILE(sensitivity, dir_debug, S_IRUSR);
2304		DEBUGFS_ADD_FILE(chain_noise, dir_debug, S_IRUSR);
2305		DEBUGFS_ADD_FILE(ucode_tracing, dir_debug, S_IWUSR | S_IRUSR);
2306	}
2307	DEBUGFS_ADD_BOOL(disable_sensitivity, dir_rf, &priv->disable_sens_cal);
2308	DEBUGFS_ADD_BOOL(disable_chain_noise, dir_rf,
2309			 &priv->disable_chain_noise_cal);
2310	if (((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965) ||
2311	    ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_3945))
2312		DEBUGFS_ADD_BOOL(disable_tx_power, dir_rf,
2313				&priv->disable_tx_power_cal);
2314	return 0;
2315
2316err:
2317	IWL_ERR(priv, "Can't create the debugfs directory\n");
2318	iwl_dbgfs_unregister(priv);
2319	return -ENOMEM;
2320}
2321EXPORT_SYMBOL(iwl_dbgfs_register);
2322
2323/**
2324 * Remove the debugfs files and directories
2325 *
2326 */
2327void iwl_dbgfs_unregister(struct iwl_priv *priv)
2328{
2329	if (!priv->debugfs_dir)
2330		return;
2331
2332	debugfs_remove_recursive(priv->debugfs_dir);
2333	priv->debugfs_dir = NULL;
2334}
2335EXPORT_SYMBOL(iwl_dbgfs_unregister);
2336
2337
2338
2339