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