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