iwlwifi: improve station debugfs
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-debugfs.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19  * USA
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *****************************************************************************/
28 #include <linux/ieee80211.h>
29 #include <net/mac80211.h>
30
31
32 #include "iwl-dev.h"
33 #include "iwl-debug.h"
34 #include "iwl-core.h"
35 #include "iwl-io.h"
36 #include "iwl-calib.h"
37
38 /* create and remove of files */
39 #define DEBUGFS_ADD_FILE(name, parent, mode) do {                       \
40         if (!debugfs_create_file(#name, mode, parent, priv,             \
41                                  &iwl_dbgfs_##name##_ops))              \
42                 goto err;                                               \
43 } while (0)
44
45 #define DEBUGFS_ADD_BOOL(name, parent, ptr) do {                        \
46         struct dentry *__tmp;                                           \
47         __tmp = debugfs_create_bool(#name, S_IWUSR | S_IRUSR,           \
48                                     parent, ptr);                       \
49         if (IS_ERR(__tmp) || !__tmp)                                    \
50                 goto err;                                               \
51 } while (0)
52
53 #define DEBUGFS_ADD_X32(name, parent, ptr) do {                         \
54         struct dentry *__tmp;                                           \
55         __tmp = debugfs_create_x32(#name, S_IWUSR | S_IRUSR,            \
56                                    parent, ptr);                        \
57         if (IS_ERR(__tmp) || !__tmp)                                    \
58                 goto err;                                               \
59 } while (0)
60
61 /* file operation */
62 #define DEBUGFS_READ_FUNC(name)                                         \
63 static ssize_t iwl_dbgfs_##name##_read(struct file *file,               \
64                                         char __user *user_buf,          \
65                                         size_t count, loff_t *ppos);
66
67 #define DEBUGFS_WRITE_FUNC(name)                                        \
68 static ssize_t iwl_dbgfs_##name##_write(struct file *file,              \
69                                         const char __user *user_buf,    \
70                                         size_t count, loff_t *ppos);
71
72
73 static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
74 {
75         file->private_data = inode->i_private;
76         return 0;
77 }
78
79 #define DEBUGFS_READ_FILE_OPS(name)                                     \
80         DEBUGFS_READ_FUNC(name);                                        \
81 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
82         .read = iwl_dbgfs_##name##_read,                                \
83         .open = iwl_dbgfs_open_file_generic,                            \
84 };
85
86 #define DEBUGFS_WRITE_FILE_OPS(name)                                    \
87         DEBUGFS_WRITE_FUNC(name);                                       \
88 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
89         .write = iwl_dbgfs_##name##_write,                              \
90         .open = iwl_dbgfs_open_file_generic,                            \
91 };
92
93
94 #define DEBUGFS_READ_WRITE_FILE_OPS(name)                               \
95         DEBUGFS_READ_FUNC(name);                                        \
96         DEBUGFS_WRITE_FUNC(name);                                       \
97 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
98         .write = iwl_dbgfs_##name##_write,                              \
99         .read = iwl_dbgfs_##name##_read,                                \
100         .open = iwl_dbgfs_open_file_generic,                            \
101 };
102
103 static ssize_t iwl_dbgfs_tx_statistics_read(struct file *file,
104                                                 char __user *user_buf,
105                                                 size_t count, loff_t *ppos) {
106
107         struct iwl_priv *priv = file->private_data;
108         char *buf;
109         int pos = 0;
110
111         int cnt;
112         ssize_t ret;
113         const size_t bufsz = 100 +
114                 sizeof(char) * 50 * (MANAGEMENT_MAX + CONTROL_MAX);
115         buf = kzalloc(bufsz, GFP_KERNEL);
116         if (!buf)
117                 return -ENOMEM;
118         pos += scnprintf(buf + pos, bufsz - pos, "Management:\n");
119         for (cnt = 0; cnt < MANAGEMENT_MAX; cnt++) {
120                 pos += scnprintf(buf + pos, bufsz - pos,
121                                  "\t%25s\t\t: %u\n",
122                                  get_mgmt_string(cnt),
123                                  priv->tx_stats.mgmt[cnt]);
124         }
125         pos += scnprintf(buf + pos, bufsz - pos, "Control\n");
126         for (cnt = 0; cnt < CONTROL_MAX; cnt++) {
127                 pos += scnprintf(buf + pos, bufsz - pos,
128                                  "\t%25s\t\t: %u\n",
129                                  get_ctrl_string(cnt),
130                                  priv->tx_stats.ctrl[cnt]);
131         }
132         pos += scnprintf(buf + pos, bufsz - pos, "Data:\n");
133         pos += scnprintf(buf + pos, bufsz - pos, "\tcnt: %u\n",
134                          priv->tx_stats.data_cnt);
135         pos += scnprintf(buf + pos, bufsz - pos, "\tbytes: %llu\n",
136                          priv->tx_stats.data_bytes);
137         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
138         kfree(buf);
139         return ret;
140 }
141
142 static ssize_t iwl_dbgfs_clear_traffic_statistics_write(struct file *file,
143                                         const char __user *user_buf,
144                                         size_t count, loff_t *ppos)
145 {
146         struct iwl_priv *priv = file->private_data;
147         u32 clear_flag;
148         char buf[8];
149         int buf_size;
150
151         memset(buf, 0, sizeof(buf));
152         buf_size = min(count, sizeof(buf) -  1);
153         if (copy_from_user(buf, user_buf, buf_size))
154                 return -EFAULT;
155         if (sscanf(buf, "%x", &clear_flag) != 1)
156                 return -EFAULT;
157         iwl_clear_traffic_stats(priv);
158
159         return count;
160 }
161
162 static ssize_t iwl_dbgfs_rx_statistics_read(struct file *file,
163                                                 char __user *user_buf,
164                                                 size_t count, loff_t *ppos) {
165
166         struct iwl_priv *priv = file->private_data;
167         char *buf;
168         int pos = 0;
169         int cnt;
170         ssize_t ret;
171         const size_t bufsz = 100 +
172                 sizeof(char) * 50 * (MANAGEMENT_MAX + CONTROL_MAX);
173         buf = kzalloc(bufsz, GFP_KERNEL);
174         if (!buf)
175                 return -ENOMEM;
176
177         pos += scnprintf(buf + pos, bufsz - pos, "Management:\n");
178         for (cnt = 0; cnt < MANAGEMENT_MAX; cnt++) {
179                 pos += scnprintf(buf + pos, bufsz - pos,
180                                  "\t%25s\t\t: %u\n",
181                                  get_mgmt_string(cnt),
182                                  priv->rx_stats.mgmt[cnt]);
183         }
184         pos += scnprintf(buf + pos, bufsz - pos, "Control:\n");
185         for (cnt = 0; cnt < CONTROL_MAX; cnt++) {
186                 pos += scnprintf(buf + pos, bufsz - pos,
187                                  "\t%25s\t\t: %u\n",
188                                  get_ctrl_string(cnt),
189                                  priv->rx_stats.ctrl[cnt]);
190         }
191         pos += scnprintf(buf + pos, bufsz - pos, "Data:\n");
192         pos += scnprintf(buf + pos, bufsz - pos, "\tcnt: %u\n",
193                          priv->rx_stats.data_cnt);
194         pos += scnprintf(buf + pos, bufsz - pos, "\tbytes: %llu\n",
195                          priv->rx_stats.data_bytes);
196
197         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
198         kfree(buf);
199         return ret;
200 }
201
202 #define BYTE1_MASK 0x000000ff;
203 #define BYTE2_MASK 0x0000ffff;
204 #define BYTE3_MASK 0x00ffffff;
205 static ssize_t iwl_dbgfs_sram_read(struct file *file,
206                                         char __user *user_buf,
207                                         size_t count, loff_t *ppos)
208 {
209         u32 val;
210         char *buf;
211         ssize_t ret;
212         int i;
213         int pos = 0;
214         struct iwl_priv *priv = file->private_data;
215         size_t bufsz;
216
217         /* default is to dump the entire data segment */
218         if (!priv->dbgfs_sram_offset && !priv->dbgfs_sram_len) {
219                 priv->dbgfs_sram_offset = 0x800000;
220                 if (priv->ucode_type == UCODE_INIT)
221                         priv->dbgfs_sram_len = priv->ucode_init_data.len;
222                 else
223                         priv->dbgfs_sram_len = priv->ucode_data.len;
224         }
225         bufsz =  30 + priv->dbgfs_sram_len * sizeof(char) * 10;
226         buf = kmalloc(bufsz, GFP_KERNEL);
227         if (!buf)
228                 return -ENOMEM;
229         pos += scnprintf(buf + pos, bufsz - pos, "sram_len: 0x%x\n",
230                         priv->dbgfs_sram_len);
231         pos += scnprintf(buf + pos, bufsz - pos, "sram_offset: 0x%x\n",
232                         priv->dbgfs_sram_offset);
233         for (i = priv->dbgfs_sram_len; i > 0; i -= 4) {
234                 val = iwl_read_targ_mem(priv, priv->dbgfs_sram_offset + \
235                                         priv->dbgfs_sram_len - i);
236                 if (i < 4) {
237                         switch (i) {
238                         case 1:
239                                 val &= BYTE1_MASK;
240                                 break;
241                         case 2:
242                                 val &= BYTE2_MASK;
243                                 break;
244                         case 3:
245                                 val &= BYTE3_MASK;
246                                 break;
247                         }
248                 }
249                 if (!(i % 16))
250                         pos += scnprintf(buf + pos, bufsz - pos, "\n");
251                 pos += scnprintf(buf + pos, bufsz - pos, "0x%08x ", val);
252         }
253         pos += scnprintf(buf + pos, bufsz - pos, "\n");
254
255         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
256         kfree(buf);
257         return ret;
258 }
259
260 static ssize_t iwl_dbgfs_sram_write(struct file *file,
261                                         const char __user *user_buf,
262                                         size_t count, loff_t *ppos)
263 {
264         struct iwl_priv *priv = file->private_data;
265         char buf[64];
266         int buf_size;
267         u32 offset, len;
268
269         memset(buf, 0, sizeof(buf));
270         buf_size = min(count, sizeof(buf) -  1);
271         if (copy_from_user(buf, user_buf, buf_size))
272                 return -EFAULT;
273
274         if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
275                 priv->dbgfs_sram_offset = offset;
276                 priv->dbgfs_sram_len = len;
277         } else {
278                 priv->dbgfs_sram_offset = 0;
279                 priv->dbgfs_sram_len = 0;
280         }
281
282         return count;
283 }
284
285 static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
286                                         size_t count, loff_t *ppos)
287 {
288         struct iwl_priv *priv = file->private_data;
289         struct iwl_station_entry *station;
290         int max_sta = priv->hw_params.max_stations;
291         char *buf;
292         int i, j, pos = 0;
293         ssize_t ret;
294         /* Add 30 for initial string */
295         const size_t bufsz = 30 + sizeof(char) * 500 * (priv->num_stations);
296
297         buf = kmalloc(bufsz, GFP_KERNEL);
298         if (!buf)
299                 return -ENOMEM;
300
301         pos += scnprintf(buf + pos, bufsz - pos, "num of stations: %d\n\n",
302                         priv->num_stations);
303
304         for (i = 0; i < max_sta; i++) {
305                 station = &priv->stations[i];
306                 if (!station->used)
307                         continue;
308                 pos += scnprintf(buf + pos, bufsz - pos,
309                                  "station %d - addr: %pM, flags: %#x\n",
310                                  i, station->sta.sta.addr,
311                                  station->sta.station_flags_msk);
312                 pos += scnprintf(buf + pos, bufsz - pos,
313                                 "TID\tseq_num\ttxq_id\tframes\ttfds\t");
314                 pos += scnprintf(buf + pos, bufsz - pos,
315                                 "start_idx\tbitmap\t\t\trate_n_flags\n");
316
317                 for (j = 0; j < MAX_TID_COUNT; j++) {
318                         pos += scnprintf(buf + pos, bufsz - pos,
319                                 "%d:\t%#x\t%#x\t%u\t%u\t%u\t\t%#.16llx\t%#x",
320                                 j, station->tid[j].seq_number,
321                                 station->tid[j].agg.txq_id,
322                                 station->tid[j].agg.frame_count,
323                                 station->tid[j].tfds_in_queue,
324                                 station->tid[j].agg.start_idx,
325                                 station->tid[j].agg.bitmap,
326                                 station->tid[j].agg.rate_n_flags);
327
328                         if (station->tid[j].agg.wait_for_ba)
329                                 pos += scnprintf(buf + pos, bufsz - pos,
330                                                  " - waitforba");
331                         pos += scnprintf(buf + pos, bufsz - pos, "\n");
332                 }
333
334                 pos += scnprintf(buf + pos, bufsz - pos, "\n");
335         }
336
337         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
338         kfree(buf);
339         return ret;
340 }
341
342 static ssize_t iwl_dbgfs_nvm_read(struct file *file,
343                                        char __user *user_buf,
344                                        size_t count,
345                                        loff_t *ppos)
346 {
347         ssize_t ret;
348         struct iwl_priv *priv = file->private_data;
349         int pos = 0, ofs = 0, buf_size = 0;
350         const u8 *ptr;
351         char *buf;
352         u16 eeprom_ver;
353         size_t eeprom_len = priv->cfg->eeprom_size;
354         buf_size = 4 * eeprom_len + 256;
355
356         if (eeprom_len % 16) {
357                 IWL_ERR(priv, "NVM size is not multiple of 16.\n");
358                 return -ENODATA;
359         }
360
361         ptr = priv->eeprom;
362         if (!ptr) {
363                 IWL_ERR(priv, "Invalid EEPROM/OTP memory\n");
364                 return -ENOMEM;
365         }
366
367         /* 4 characters for byte 0xYY */
368         buf = kzalloc(buf_size, GFP_KERNEL);
369         if (!buf) {
370                 IWL_ERR(priv, "Can not allocate Buffer\n");
371                 return -ENOMEM;
372         }
373         eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
374         pos += scnprintf(buf + pos, buf_size - pos, "NVM Type: %s, "
375                         "version: 0x%x\n",
376                         (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
377                          ? "OTP" : "EEPROM", eeprom_ver);
378         for (ofs = 0 ; ofs < eeprom_len ; ofs += 16) {
379                 pos += scnprintf(buf + pos, buf_size - pos, "0x%.4x ", ofs);
380                 hex_dump_to_buffer(ptr + ofs, 16 , 16, 2, buf + pos,
381                                    buf_size - pos, 0);
382                 pos += strlen(buf + pos);
383                 if (buf_size - pos > 0)
384                         buf[pos++] = '\n';
385         }
386
387         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
388         kfree(buf);
389         return ret;
390 }
391
392 static ssize_t iwl_dbgfs_log_event_read(struct file *file,
393                                          char __user *user_buf,
394                                          size_t count, loff_t *ppos)
395 {
396         struct iwl_priv *priv = file->private_data;
397         char *buf;
398         int pos = 0;
399         ssize_t ret = -ENOMEM;
400
401         ret = pos = priv->cfg->ops->lib->dump_nic_event_log(
402                                         priv, true, &buf, true);
403         if (buf) {
404                 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
405                 kfree(buf);
406         }
407         return ret;
408 }
409
410 static ssize_t iwl_dbgfs_log_event_write(struct file *file,
411                                         const char __user *user_buf,
412                                         size_t count, loff_t *ppos)
413 {
414         struct iwl_priv *priv = file->private_data;
415         u32 event_log_flag;
416         char buf[8];
417         int buf_size;
418
419         memset(buf, 0, sizeof(buf));
420         buf_size = min(count, sizeof(buf) -  1);
421         if (copy_from_user(buf, user_buf, buf_size))
422                 return -EFAULT;
423         if (sscanf(buf, "%d", &event_log_flag) != 1)
424                 return -EFAULT;
425         if (event_log_flag == 1)
426                 priv->cfg->ops->lib->dump_nic_event_log(priv, true,
427                                                         NULL, false);
428
429         return count;
430 }
431
432
433
434 static ssize_t iwl_dbgfs_channels_read(struct file *file, char __user *user_buf,
435                                        size_t count, loff_t *ppos)
436 {
437         struct iwl_priv *priv = file->private_data;
438         struct ieee80211_channel *channels = NULL;
439         const struct ieee80211_supported_band *supp_band = NULL;
440         int pos = 0, i, bufsz = PAGE_SIZE;
441         char *buf;
442         ssize_t ret;
443
444         if (!test_bit(STATUS_GEO_CONFIGURED, &priv->status))
445                 return -EAGAIN;
446
447         buf = kzalloc(bufsz, GFP_KERNEL);
448         if (!buf) {
449                 IWL_ERR(priv, "Can not allocate Buffer\n");
450                 return -ENOMEM;
451         }
452
453         supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_2GHZ);
454         if (supp_band) {
455                 channels = supp_band->channels;
456
457                 pos += scnprintf(buf + pos, bufsz - pos,
458                                 "Displaying %d channels in 2.4GHz band 802.11bg):\n",
459                                 supp_band->n_channels);
460
461                 for (i = 0; i < supp_band->n_channels; i++)
462                         pos += scnprintf(buf + pos, bufsz - pos,
463                                         "%d: %ddBm: BSS%s%s, %s.\n",
464                                         ieee80211_frequency_to_channel(
465                                         channels[i].center_freq),
466                                         channels[i].max_power,
467                                         channels[i].flags & IEEE80211_CHAN_RADAR ?
468                                         " (IEEE 802.11h required)" : "",
469                                         ((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
470                                         || (channels[i].flags &
471                                         IEEE80211_CHAN_RADAR)) ? "" :
472                                         ", IBSS",
473                                         channels[i].flags &
474                                         IEEE80211_CHAN_PASSIVE_SCAN ?
475                                         "passive only" : "active/passive");
476         }
477         supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_5GHZ);
478         if (supp_band) {
479                 channels = supp_band->channels;
480
481                 pos += scnprintf(buf + pos, bufsz - pos,
482                                 "Displaying %d channels in 5.2GHz band (802.11a)\n",
483                                 supp_band->n_channels);
484
485                 for (i = 0; i < supp_band->n_channels; i++)
486                         pos += scnprintf(buf + pos, bufsz - pos,
487                                         "%d: %ddBm: BSS%s%s, %s.\n",
488                                         ieee80211_frequency_to_channel(
489                                         channels[i].center_freq),
490                                         channels[i].max_power,
491                                         channels[i].flags & IEEE80211_CHAN_RADAR ?
492                                         " (IEEE 802.11h required)" : "",
493                                         ((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
494                                         || (channels[i].flags &
495                                         IEEE80211_CHAN_RADAR)) ? "" :
496                                         ", IBSS",
497                                         channels[i].flags &
498                                         IEEE80211_CHAN_PASSIVE_SCAN ?
499                                         "passive only" : "active/passive");
500         }
501         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
502         kfree(buf);
503         return ret;
504 }
505
506 static ssize_t iwl_dbgfs_status_read(struct file *file,
507                                                 char __user *user_buf,
508                                                 size_t count, loff_t *ppos) {
509
510         struct iwl_priv *priv = file->private_data;
511         char buf[512];
512         int pos = 0;
513         const size_t bufsz = sizeof(buf);
514
515         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_HCMD_ACTIVE:\t %d\n",
516                 test_bit(STATUS_HCMD_ACTIVE, &priv->status));
517         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_INT_ENABLED:\t %d\n",
518                 test_bit(STATUS_INT_ENABLED, &priv->status));
519         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_RF_KILL_HW:\t %d\n",
520                 test_bit(STATUS_RF_KILL_HW, &priv->status));
521         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_CT_KILL:\t\t %d\n",
522                 test_bit(STATUS_CT_KILL, &priv->status));
523         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_INIT:\t\t %d\n",
524                 test_bit(STATUS_INIT, &priv->status));
525         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_ALIVE:\t\t %d\n",
526                 test_bit(STATUS_ALIVE, &priv->status));
527         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_READY:\t\t %d\n",
528                 test_bit(STATUS_READY, &priv->status));
529         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_TEMPERATURE:\t %d\n",
530                 test_bit(STATUS_TEMPERATURE, &priv->status));
531         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_GEO_CONFIGURED:\t %d\n",
532                 test_bit(STATUS_GEO_CONFIGURED, &priv->status));
533         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_EXIT_PENDING:\t %d\n",
534                 test_bit(STATUS_EXIT_PENDING, &priv->status));
535         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_STATISTICS:\t %d\n",
536                 test_bit(STATUS_STATISTICS, &priv->status));
537         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCANNING:\t %d\n",
538                 test_bit(STATUS_SCANNING, &priv->status));
539         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCAN_ABORTING:\t %d\n",
540                 test_bit(STATUS_SCAN_ABORTING, &priv->status));
541         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCAN_HW:\t\t %d\n",
542                 test_bit(STATUS_SCAN_HW, &priv->status));
543         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_POWER_PMI:\t %d\n",
544                 test_bit(STATUS_POWER_PMI, &priv->status));
545         pos += scnprintf(buf + pos, bufsz - pos, "STATUS_FW_ERROR:\t %d\n",
546                 test_bit(STATUS_FW_ERROR, &priv->status));
547         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
548 }
549
550 static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
551                                         char __user *user_buf,
552                                         size_t count, loff_t *ppos) {
553
554         struct iwl_priv *priv = file->private_data;
555         int pos = 0;
556         int cnt = 0;
557         char *buf;
558         int bufsz = 24 * 64; /* 24 items * 64 char per item */
559         ssize_t ret;
560
561         buf = kzalloc(bufsz, GFP_KERNEL);
562         if (!buf) {
563                 IWL_ERR(priv, "Can not allocate Buffer\n");
564                 return -ENOMEM;
565         }
566
567         pos += scnprintf(buf + pos, bufsz - pos,
568                         "Interrupt Statistics Report:\n");
569
570         pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
571                 priv->isr_stats.hw);
572         pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
573                 priv->isr_stats.sw);
574         if (priv->isr_stats.sw > 0) {
575                 pos += scnprintf(buf + pos, bufsz - pos,
576                         "\tLast Restarting Code:  0x%X\n",
577                         priv->isr_stats.sw_err);
578         }
579 #ifdef CONFIG_IWLWIFI_DEBUG
580         pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
581                 priv->isr_stats.sch);
582         pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
583                 priv->isr_stats.alive);
584 #endif
585         pos += scnprintf(buf + pos, bufsz - pos,
586                 "HW RF KILL switch toggled:\t %u\n",
587                 priv->isr_stats.rfkill);
588
589         pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
590                 priv->isr_stats.ctkill);
591
592         pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
593                 priv->isr_stats.wakeup);
594
595         pos += scnprintf(buf + pos, bufsz - pos,
596                 "Rx command responses:\t\t %u\n",
597                 priv->isr_stats.rx);
598         for (cnt = 0; cnt < REPLY_MAX; cnt++) {
599                 if (priv->isr_stats.rx_handlers[cnt] > 0)
600                         pos += scnprintf(buf + pos, bufsz - pos,
601                                 "\tRx handler[%36s]:\t\t %u\n",
602                                 get_cmd_string(cnt),
603                                 priv->isr_stats.rx_handlers[cnt]);
604         }
605
606         pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
607                 priv->isr_stats.tx);
608
609         pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
610                 priv->isr_stats.unhandled);
611
612         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
613         kfree(buf);
614         return ret;
615 }
616
617 static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
618                                          const char __user *user_buf,
619                                          size_t count, loff_t *ppos)
620 {
621         struct iwl_priv *priv = file->private_data;
622         char buf[8];
623         int buf_size;
624         u32 reset_flag;
625
626         memset(buf, 0, sizeof(buf));
627         buf_size = min(count, sizeof(buf) -  1);
628         if (copy_from_user(buf, user_buf, buf_size))
629                 return -EFAULT;
630         if (sscanf(buf, "%x", &reset_flag) != 1)
631                 return -EFAULT;
632         if (reset_flag == 0)
633                 iwl_clear_isr_stats(priv);
634
635         return count;
636 }
637
638 static ssize_t iwl_dbgfs_qos_read(struct file *file, char __user *user_buf,
639                                        size_t count, loff_t *ppos)
640 {
641         struct iwl_priv *priv = file->private_data;
642         int pos = 0, i;
643         char buf[256];
644         const size_t bufsz = sizeof(buf);
645
646         for (i = 0; i < AC_NUM; i++) {
647                 pos += scnprintf(buf + pos, bufsz - pos,
648                         "\tcw_min\tcw_max\taifsn\ttxop\n");
649                 pos += scnprintf(buf + pos, bufsz - pos,
650                                 "AC[%d]\t%u\t%u\t%u\t%u\n", i,
651                                 priv->qos_data.def_qos_parm.ac[i].cw_min,
652                                 priv->qos_data.def_qos_parm.ac[i].cw_max,
653                                 priv->qos_data.def_qos_parm.ac[i].aifsn,
654                                 priv->qos_data.def_qos_parm.ac[i].edca_txop);
655         }
656         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
657 }
658
659 static ssize_t iwl_dbgfs_led_read(struct file *file, char __user *user_buf,
660                                   size_t count, loff_t *ppos)
661 {
662         struct iwl_priv *priv = file->private_data;
663         int pos = 0;
664         char buf[256];
665         const size_t bufsz = sizeof(buf);
666
667         pos += scnprintf(buf + pos, bufsz - pos,
668                          "allow blinking: %s\n",
669                          (priv->allow_blinking) ? "True" : "False");
670         if (priv->allow_blinking) {
671                 pos += scnprintf(buf + pos, bufsz - pos,
672                                  "Led blinking rate: %u\n",
673                                  priv->last_blink_rate);
674                 pos += scnprintf(buf + pos, bufsz - pos,
675                                  "Last blink time: %lu\n",
676                                  priv->last_blink_time);
677         }
678
679         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
680 }
681
682 static ssize_t iwl_dbgfs_thermal_throttling_read(struct file *file,
683                                 char __user *user_buf,
684                                 size_t count, loff_t *ppos)
685 {
686         struct iwl_priv *priv = file->private_data;
687         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
688         struct iwl_tt_restriction *restriction;
689         char buf[100];
690         int pos = 0;
691         const size_t bufsz = sizeof(buf);
692
693         pos += scnprintf(buf + pos, bufsz - pos,
694                         "Thermal Throttling Mode: %s\n",
695                         tt->advanced_tt ? "Advance" : "Legacy");
696         pos += scnprintf(buf + pos, bufsz - pos,
697                         "Thermal Throttling State: %d\n",
698                         tt->state);
699         if (tt->advanced_tt) {
700                 restriction = tt->restriction + tt->state;
701                 pos += scnprintf(buf + pos, bufsz - pos,
702                                 "Tx mode: %d\n",
703                                 restriction->tx_stream);
704                 pos += scnprintf(buf + pos, bufsz - pos,
705                                 "Rx mode: %d\n",
706                                 restriction->rx_stream);
707                 pos += scnprintf(buf + pos, bufsz - pos,
708                                 "HT mode: %d\n",
709                                 restriction->is_ht);
710         }
711         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
712 }
713
714 static ssize_t iwl_dbgfs_disable_ht40_write(struct file *file,
715                                          const char __user *user_buf,
716                                          size_t count, loff_t *ppos)
717 {
718         struct iwl_priv *priv = file->private_data;
719         char buf[8];
720         int buf_size;
721         int ht40;
722
723         memset(buf, 0, sizeof(buf));
724         buf_size = min(count, sizeof(buf) -  1);
725         if (copy_from_user(buf, user_buf, buf_size))
726                 return -EFAULT;
727         if (sscanf(buf, "%d", &ht40) != 1)
728                 return -EFAULT;
729         if (!iwl_is_associated(priv))
730                 priv->disable_ht40 = ht40 ? true : false;
731         else {
732                 IWL_ERR(priv, "Sta associated with AP - "
733                         "Change to 40MHz channel support is not allowed\n");
734                 return -EINVAL;
735         }
736
737         return count;
738 }
739
740 static ssize_t iwl_dbgfs_disable_ht40_read(struct file *file,
741                                          char __user *user_buf,
742                                          size_t count, loff_t *ppos)
743 {
744         struct iwl_priv *priv = file->private_data;
745         char buf[100];
746         int pos = 0;
747         const size_t bufsz = sizeof(buf);
748
749         pos += scnprintf(buf + pos, bufsz - pos,
750                         "11n 40MHz Mode: %s\n",
751                         priv->disable_ht40 ? "Disabled" : "Enabled");
752         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
753 }
754
755 static ssize_t iwl_dbgfs_sleep_level_override_write(struct file *file,
756                                                     const char __user *user_buf,
757                                                     size_t count, loff_t *ppos)
758 {
759         struct iwl_priv *priv = file->private_data;
760         char buf[8];
761         int buf_size;
762         int value;
763
764         memset(buf, 0, sizeof(buf));
765         buf_size = min(count, sizeof(buf) -  1);
766         if (copy_from_user(buf, user_buf, buf_size))
767                 return -EFAULT;
768
769         if (sscanf(buf, "%d", &value) != 1)
770                 return -EINVAL;
771
772         /*
773          * Our users expect 0 to be "CAM", but 0 isn't actually
774          * valid here. However, let's not confuse them and present
775          * IWL_POWER_INDEX_1 as "1", not "0".
776          */
777         if (value == 0)
778                 return -EINVAL;
779         else if (value > 0)
780                 value -= 1;
781
782         if (value != -1 && (value < 0 || value >= IWL_POWER_NUM))
783                 return -EINVAL;
784
785         if (!iwl_is_ready_rf(priv))
786                 return -EAGAIN;
787
788         priv->power_data.debug_sleep_level_override = value;
789
790         mutex_lock(&priv->mutex);
791         iwl_power_update_mode(priv, true);
792         mutex_unlock(&priv->mutex);
793
794         return count;
795 }
796
797 static ssize_t iwl_dbgfs_sleep_level_override_read(struct file *file,
798                                                    char __user *user_buf,
799                                                    size_t count, loff_t *ppos)
800 {
801         struct iwl_priv *priv = file->private_data;
802         char buf[10];
803         int pos, value;
804         const size_t bufsz = sizeof(buf);
805
806         /* see the write function */
807         value = priv->power_data.debug_sleep_level_override;
808         if (value >= 0)
809                 value += 1;
810
811         pos = scnprintf(buf, bufsz, "%d\n", value);
812         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
813 }
814
815 static ssize_t iwl_dbgfs_current_sleep_command_read(struct file *file,
816                                                     char __user *user_buf,
817                                                     size_t count, loff_t *ppos)
818 {
819         struct iwl_priv *priv = file->private_data;
820         char buf[200];
821         int pos = 0, i;
822         const size_t bufsz = sizeof(buf);
823         struct iwl_powertable_cmd *cmd = &priv->power_data.sleep_cmd;
824
825         pos += scnprintf(buf + pos, bufsz - pos,
826                          "flags: %#.2x\n", le16_to_cpu(cmd->flags));
827         pos += scnprintf(buf + pos, bufsz - pos,
828                          "RX/TX timeout: %d/%d usec\n",
829                          le32_to_cpu(cmd->rx_data_timeout),
830                          le32_to_cpu(cmd->tx_data_timeout));
831         for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
832                 pos += scnprintf(buf + pos, bufsz - pos,
833                                  "sleep_interval[%d]: %d\n", i,
834                                  le32_to_cpu(cmd->sleep_interval[i]));
835
836         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
837 }
838
839 DEBUGFS_READ_WRITE_FILE_OPS(sram);
840 DEBUGFS_READ_WRITE_FILE_OPS(log_event);
841 DEBUGFS_READ_FILE_OPS(nvm);
842 DEBUGFS_READ_FILE_OPS(stations);
843 DEBUGFS_READ_FILE_OPS(channels);
844 DEBUGFS_READ_FILE_OPS(status);
845 DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
846 DEBUGFS_READ_FILE_OPS(qos);
847 DEBUGFS_READ_FILE_OPS(led);
848 DEBUGFS_READ_FILE_OPS(thermal_throttling);
849 DEBUGFS_READ_WRITE_FILE_OPS(disable_ht40);
850 DEBUGFS_READ_WRITE_FILE_OPS(sleep_level_override);
851 DEBUGFS_READ_FILE_OPS(current_sleep_command);
852
853 static ssize_t iwl_dbgfs_traffic_log_read(struct file *file,
854                                          char __user *user_buf,
855                                          size_t count, loff_t *ppos)
856 {
857         struct iwl_priv *priv = file->private_data;
858         int pos = 0, ofs = 0;
859         int cnt = 0, entry;
860         struct iwl_tx_queue *txq;
861         struct iwl_queue *q;
862         struct iwl_rx_queue *rxq = &priv->rxq;
863         char *buf;
864         int bufsz = ((IWL_TRAFFIC_ENTRIES * IWL_TRAFFIC_ENTRY_SIZE * 64) * 2) +
865                 (priv->cfg->num_of_queues * 32 * 8) + 400;
866         const u8 *ptr;
867         ssize_t ret;
868
869         if (!priv->txq) {
870                 IWL_ERR(priv, "txq not ready\n");
871                 return -EAGAIN;
872         }
873         buf = kzalloc(bufsz, GFP_KERNEL);
874         if (!buf) {
875                 IWL_ERR(priv, "Can not allocate buffer\n");
876                 return -ENOMEM;
877         }
878         pos += scnprintf(buf + pos, bufsz - pos, "Tx Queue\n");
879         for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
880                 txq = &priv->txq[cnt];
881                 q = &txq->q;
882                 pos += scnprintf(buf + pos, bufsz - pos,
883                                 "q[%d]: read_ptr: %u, write_ptr: %u\n",
884                                 cnt, q->read_ptr, q->write_ptr);
885         }
886         if (priv->tx_traffic && (iwl_debug_level & IWL_DL_TX)) {
887                 ptr = priv->tx_traffic;
888                 pos += scnprintf(buf + pos, bufsz - pos,
889                                 "Tx Traffic idx: %u\n", priv->tx_traffic_idx);
890                 for (cnt = 0, ofs = 0; cnt < IWL_TRAFFIC_ENTRIES; cnt++) {
891                         for (entry = 0; entry < IWL_TRAFFIC_ENTRY_SIZE / 16;
892                              entry++,  ofs += 16) {
893                                 pos += scnprintf(buf + pos, bufsz - pos,
894                                                 "0x%.4x ", ofs);
895                                 hex_dump_to_buffer(ptr + ofs, 16, 16, 2,
896                                                    buf + pos, bufsz - pos, 0);
897                                 pos += strlen(buf + pos);
898                                 if (bufsz - pos > 0)
899                                         buf[pos++] = '\n';
900                         }
901                 }
902         }
903
904         pos += scnprintf(buf + pos, bufsz - pos, "Rx Queue\n");
905         pos += scnprintf(buf + pos, bufsz - pos,
906                         "read: %u, write: %u\n",
907                          rxq->read, rxq->write);
908
909         if (priv->rx_traffic && (iwl_debug_level & IWL_DL_RX)) {
910                 ptr = priv->rx_traffic;
911                 pos += scnprintf(buf + pos, bufsz - pos,
912                                 "Rx Traffic idx: %u\n", priv->rx_traffic_idx);
913                 for (cnt = 0, ofs = 0; cnt < IWL_TRAFFIC_ENTRIES; cnt++) {
914                         for (entry = 0; entry < IWL_TRAFFIC_ENTRY_SIZE / 16;
915                              entry++,  ofs += 16) {
916                                 pos += scnprintf(buf + pos, bufsz - pos,
917                                                 "0x%.4x ", ofs);
918                                 hex_dump_to_buffer(ptr + ofs, 16, 16, 2,
919                                                    buf + pos, bufsz - pos, 0);
920                                 pos += strlen(buf + pos);
921                                 if (bufsz - pos > 0)
922                                         buf[pos++] = '\n';
923                         }
924                 }
925         }
926
927         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
928         kfree(buf);
929         return ret;
930 }
931
932 static ssize_t iwl_dbgfs_traffic_log_write(struct file *file,
933                                          const char __user *user_buf,
934                                          size_t count, loff_t *ppos)
935 {
936         struct iwl_priv *priv = file->private_data;
937         char buf[8];
938         int buf_size;
939         int traffic_log;
940
941         memset(buf, 0, sizeof(buf));
942         buf_size = min(count, sizeof(buf) -  1);
943         if (copy_from_user(buf, user_buf, buf_size))
944                 return -EFAULT;
945         if (sscanf(buf, "%d", &traffic_log) != 1)
946                 return -EFAULT;
947         if (traffic_log == 0)
948                 iwl_reset_traffic_log(priv);
949
950         return count;
951 }
952
953 static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
954                                                 char __user *user_buf,
955                                                 size_t count, loff_t *ppos) {
956
957         struct iwl_priv *priv = file->private_data;
958         struct iwl_tx_queue *txq;
959         struct iwl_queue *q;
960         char *buf;
961         int pos = 0;
962         int cnt;
963         int ret;
964         const size_t bufsz = sizeof(char) * 64 * priv->cfg->num_of_queues;
965
966         if (!priv->txq) {
967                 IWL_ERR(priv, "txq not ready\n");
968                 return -EAGAIN;
969         }
970         buf = kzalloc(bufsz, GFP_KERNEL);
971         if (!buf)
972                 return -ENOMEM;
973
974         for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
975                 txq = &priv->txq[cnt];
976                 q = &txq->q;
977                 pos += scnprintf(buf + pos, bufsz - pos,
978                                 "hwq %.2d: read=%u write=%u stop=%d"
979                                 " swq_id=%#.2x (ac %d/hwq %d)\n",
980                                 cnt, q->read_ptr, q->write_ptr,
981                                 !!test_bit(cnt, priv->queue_stopped),
982                                 txq->swq_id,
983                                 txq->swq_id & 0x80 ? txq->swq_id & 3 :
984                                 txq->swq_id,
985                                 txq->swq_id & 0x80 ? (txq->swq_id >> 2) &
986                                 0x1f : txq->swq_id);
987                 if (cnt >= 4)
988                         continue;
989                 /* for the ACs, display the stop count too */
990                 pos += scnprintf(buf + pos, bufsz - pos,
991                                 "        stop-count: %d\n",
992                                 atomic_read(&priv->queue_stop_count[cnt]));
993         }
994         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
995         kfree(buf);
996         return ret;
997 }
998
999 static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
1000                                                 char __user *user_buf,
1001                                                 size_t count, loff_t *ppos) {
1002
1003         struct iwl_priv *priv = file->private_data;
1004         struct iwl_rx_queue *rxq = &priv->rxq;
1005         char buf[256];
1006         int pos = 0;
1007         const size_t bufsz = sizeof(buf);
1008
1009         pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
1010                                                 rxq->read);
1011         pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
1012                                                 rxq->write);
1013         pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
1014                                                 rxq->free_count);
1015         pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
1016                          le16_to_cpu(rxq->rb_stts->closed_rb_num) &  0x0FFF);
1017         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1018 }
1019
1020 static ssize_t iwl_dbgfs_ucode_rx_stats_read(struct file *file,
1021                                         char __user *user_buf,
1022                                         size_t count, loff_t *ppos)
1023 {
1024         struct iwl_priv *priv = file->private_data;
1025         return priv->cfg->ops->lib->debugfs_ops.rx_stats_read(file,
1026                         user_buf, count, ppos);
1027 }
1028
1029 static ssize_t iwl_dbgfs_ucode_tx_stats_read(struct file *file,
1030                                         char __user *user_buf,
1031                                         size_t count, loff_t *ppos)
1032 {
1033         struct iwl_priv *priv = file->private_data;
1034         return priv->cfg->ops->lib->debugfs_ops.tx_stats_read(file,
1035                         user_buf, count, ppos);
1036 }
1037
1038 static ssize_t iwl_dbgfs_ucode_general_stats_read(struct file *file,
1039                                         char __user *user_buf,
1040                                         size_t count, loff_t *ppos)
1041 {
1042         struct iwl_priv *priv = file->private_data;
1043         return priv->cfg->ops->lib->debugfs_ops.general_stats_read(file,
1044                         user_buf, count, ppos);
1045 }
1046
1047 static ssize_t iwl_dbgfs_sensitivity_read(struct file *file,
1048                                         char __user *user_buf,
1049                                         size_t count, loff_t *ppos) {
1050
1051         struct iwl_priv *priv = file->private_data;
1052         int pos = 0;
1053         int cnt = 0;
1054         char *buf;
1055         int bufsz = sizeof(struct iwl_sensitivity_data) * 4 + 100;
1056         ssize_t ret;
1057         struct iwl_sensitivity_data *data;
1058
1059         data = &priv->sensitivity_data;
1060         buf = kzalloc(bufsz, GFP_KERNEL);
1061         if (!buf) {
1062                 IWL_ERR(priv, "Can not allocate Buffer\n");
1063                 return -ENOMEM;
1064         }
1065
1066         pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_ofdm:\t\t\t %u\n",
1067                         data->auto_corr_ofdm);
1068         pos += scnprintf(buf + pos, bufsz - pos,
1069                         "auto_corr_ofdm_mrc:\t\t %u\n",
1070                         data->auto_corr_ofdm_mrc);
1071         pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_ofdm_x1:\t\t %u\n",
1072                         data->auto_corr_ofdm_x1);
1073         pos += scnprintf(buf + pos, bufsz - pos,
1074                         "auto_corr_ofdm_mrc_x1:\t\t %u\n",
1075                         data->auto_corr_ofdm_mrc_x1);
1076         pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_cck:\t\t\t %u\n",
1077                         data->auto_corr_cck);
1078         pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_cck_mrc:\t\t %u\n",
1079                         data->auto_corr_cck_mrc);
1080         pos += scnprintf(buf + pos, bufsz - pos,
1081                         "last_bad_plcp_cnt_ofdm:\t\t %u\n",
1082                         data->last_bad_plcp_cnt_ofdm);
1083         pos += scnprintf(buf + pos, bufsz - pos, "last_fa_cnt_ofdm:\t\t %u\n",
1084                         data->last_fa_cnt_ofdm);
1085         pos += scnprintf(buf + pos, bufsz - pos,
1086                         "last_bad_plcp_cnt_cck:\t\t %u\n",
1087                         data->last_bad_plcp_cnt_cck);
1088         pos += scnprintf(buf + pos, bufsz - pos, "last_fa_cnt_cck:\t\t %u\n",
1089                         data->last_fa_cnt_cck);
1090         pos += scnprintf(buf + pos, bufsz - pos, "nrg_curr_state:\t\t\t %u\n",
1091                         data->nrg_curr_state);
1092         pos += scnprintf(buf + pos, bufsz - pos, "nrg_prev_state:\t\t\t %u\n",
1093                         data->nrg_prev_state);
1094         pos += scnprintf(buf + pos, bufsz - pos, "nrg_value:\t\t\t");
1095         for (cnt = 0; cnt < 10; cnt++) {
1096                 pos += scnprintf(buf + pos, bufsz - pos, " %u",
1097                                 data->nrg_value[cnt]);
1098         }
1099         pos += scnprintf(buf + pos, bufsz - pos, "\n");
1100         pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_rssi:\t\t");
1101         for (cnt = 0; cnt < NRG_NUM_PREV_STAT_L; cnt++) {
1102                 pos += scnprintf(buf + pos, bufsz - pos, " %u",
1103                                 data->nrg_silence_rssi[cnt]);
1104         }
1105         pos += scnprintf(buf + pos, bufsz - pos, "\n");
1106         pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_ref:\t\t %u\n",
1107                         data->nrg_silence_ref);
1108         pos += scnprintf(buf + pos, bufsz - pos, "nrg_energy_idx:\t\t\t %u\n",
1109                         data->nrg_energy_idx);
1110         pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_idx:\t\t %u\n",
1111                         data->nrg_silence_idx);
1112         pos += scnprintf(buf + pos, bufsz - pos, "nrg_th_cck:\t\t\t %u\n",
1113                         data->nrg_th_cck);
1114         pos += scnprintf(buf + pos, bufsz - pos,
1115                         "nrg_auto_corr_silence_diff:\t %u\n",
1116                         data->nrg_auto_corr_silence_diff);
1117         pos += scnprintf(buf + pos, bufsz - pos, "num_in_cck_no_fa:\t\t %u\n",
1118                         data->num_in_cck_no_fa);
1119         pos += scnprintf(buf + pos, bufsz - pos, "nrg_th_ofdm:\t\t\t %u\n",
1120                         data->nrg_th_ofdm);
1121
1122         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1123         kfree(buf);
1124         return ret;
1125 }
1126
1127
1128 static ssize_t iwl_dbgfs_chain_noise_read(struct file *file,
1129                                         char __user *user_buf,
1130                                         size_t count, loff_t *ppos) {
1131
1132         struct iwl_priv *priv = file->private_data;
1133         int pos = 0;
1134         int cnt = 0;
1135         char *buf;
1136         int bufsz = sizeof(struct iwl_chain_noise_data) * 4 + 100;
1137         ssize_t ret;
1138         struct iwl_chain_noise_data *data;
1139
1140         data = &priv->chain_noise_data;
1141         buf = kzalloc(bufsz, GFP_KERNEL);
1142         if (!buf) {
1143                 IWL_ERR(priv, "Can not allocate Buffer\n");
1144                 return -ENOMEM;
1145         }
1146
1147         pos += scnprintf(buf + pos, bufsz - pos, "active_chains:\t\t\t %u\n",
1148                         data->active_chains);
1149         pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_a:\t\t\t %u\n",
1150                         data->chain_noise_a);
1151         pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_b:\t\t\t %u\n",
1152                         data->chain_noise_b);
1153         pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_c:\t\t\t %u\n",
1154                         data->chain_noise_c);
1155         pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_a:\t\t\t %u\n",
1156                         data->chain_signal_a);
1157         pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_b:\t\t\t %u\n",
1158                         data->chain_signal_b);
1159         pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_c:\t\t\t %u\n",
1160                         data->chain_signal_c);
1161         pos += scnprintf(buf + pos, bufsz - pos, "beacon_count:\t\t\t %u\n",
1162                         data->beacon_count);
1163
1164         pos += scnprintf(buf + pos, bufsz - pos, "disconn_array:\t\t\t");
1165         for (cnt = 0; cnt < NUM_RX_CHAINS; cnt++) {
1166                 pos += scnprintf(buf + pos, bufsz - pos, " %u",
1167                                 data->disconn_array[cnt]);
1168         }
1169         pos += scnprintf(buf + pos, bufsz - pos, "\n");
1170         pos += scnprintf(buf + pos, bufsz - pos, "delta_gain_code:\t\t");
1171         for (cnt = 0; cnt < NUM_RX_CHAINS; cnt++) {
1172                 pos += scnprintf(buf + pos, bufsz - pos, " %u",
1173                                 data->delta_gain_code[cnt]);
1174         }
1175         pos += scnprintf(buf + pos, bufsz - pos, "\n");
1176         pos += scnprintf(buf + pos, bufsz - pos, "radio_write:\t\t\t %u\n",
1177                         data->radio_write);
1178         pos += scnprintf(buf + pos, bufsz - pos, "state:\t\t\t\t %u\n",
1179                         data->state);
1180
1181         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1182         kfree(buf);
1183         return ret;
1184 }
1185
1186 static ssize_t iwl_dbgfs_power_save_status_read(struct file *file,
1187                                                     char __user *user_buf,
1188                                                     size_t count, loff_t *ppos)
1189 {
1190         struct iwl_priv *priv = file->private_data;
1191         char buf[60];
1192         int pos = 0;
1193         const size_t bufsz = sizeof(buf);
1194         u32 pwrsave_status;
1195
1196         pwrsave_status = iwl_read32(priv, CSR_GP_CNTRL) &
1197                         CSR_GP_REG_POWER_SAVE_STATUS_MSK;
1198
1199         pos += scnprintf(buf + pos, bufsz - pos, "Power Save Status: ");
1200         pos += scnprintf(buf + pos, bufsz - pos, "%s\n",
1201                 (pwrsave_status == CSR_GP_REG_NO_POWER_SAVE) ? "none" :
1202                 (pwrsave_status == CSR_GP_REG_MAC_POWER_SAVE) ? "MAC" :
1203                 (pwrsave_status == CSR_GP_REG_PHY_POWER_SAVE) ? "PHY" :
1204                 "error");
1205
1206         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1207 }
1208
1209 static ssize_t iwl_dbgfs_clear_ucode_statistics_write(struct file *file,
1210                                          const char __user *user_buf,
1211                                          size_t count, loff_t *ppos)
1212 {
1213         struct iwl_priv *priv = file->private_data;
1214         char buf[8];
1215         int buf_size;
1216         int clear;
1217
1218         memset(buf, 0, sizeof(buf));
1219         buf_size = min(count, sizeof(buf) -  1);
1220         if (copy_from_user(buf, user_buf, buf_size))
1221                 return -EFAULT;
1222         if (sscanf(buf, "%d", &clear) != 1)
1223                 return -EFAULT;
1224
1225         /* make request to uCode to retrieve statistics information */
1226         mutex_lock(&priv->mutex);
1227         iwl_send_statistics_request(priv, CMD_SYNC, true);
1228         mutex_unlock(&priv->mutex);
1229
1230         return count;
1231 }
1232
1233 static ssize_t iwl_dbgfs_csr_write(struct file *file,
1234                                          const char __user *user_buf,
1235                                          size_t count, loff_t *ppos)
1236 {
1237         struct iwl_priv *priv = file->private_data;
1238         char buf[8];
1239         int buf_size;
1240         int csr;
1241
1242         memset(buf, 0, sizeof(buf));
1243         buf_size = min(count, sizeof(buf) -  1);
1244         if (copy_from_user(buf, user_buf, buf_size))
1245                 return -EFAULT;
1246         if (sscanf(buf, "%d", &csr) != 1)
1247                 return -EFAULT;
1248
1249         if (priv->cfg->ops->lib->dump_csr)
1250                 priv->cfg->ops->lib->dump_csr(priv);
1251
1252         return count;
1253 }
1254
1255 static ssize_t iwl_dbgfs_ucode_tracing_read(struct file *file,
1256                                         char __user *user_buf,
1257                                         size_t count, loff_t *ppos) {
1258
1259         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
1260         int pos = 0;
1261         char buf[128];
1262         const size_t bufsz = sizeof(buf);
1263
1264         pos += scnprintf(buf + pos, bufsz - pos, "ucode trace timer is %s\n",
1265                         priv->event_log.ucode_trace ? "On" : "Off");
1266         pos += scnprintf(buf + pos, bufsz - pos, "non_wraps_count:\t\t %u\n",
1267                         priv->event_log.non_wraps_count);
1268         pos += scnprintf(buf + pos, bufsz - pos, "wraps_once_count:\t\t %u\n",
1269                         priv->event_log.wraps_once_count);
1270         pos += scnprintf(buf + pos, bufsz - pos, "wraps_more_count:\t\t %u\n",
1271                         priv->event_log.wraps_more_count);
1272
1273         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1274 }
1275
1276 static ssize_t iwl_dbgfs_ucode_tracing_write(struct file *file,
1277                                          const char __user *user_buf,
1278                                          size_t count, loff_t *ppos)
1279 {
1280         struct iwl_priv *priv = file->private_data;
1281         char buf[8];
1282         int buf_size;
1283         int trace;
1284
1285         memset(buf, 0, sizeof(buf));
1286         buf_size = min(count, sizeof(buf) -  1);
1287         if (copy_from_user(buf, user_buf, buf_size))
1288                 return -EFAULT;
1289         if (sscanf(buf, "%d", &trace) != 1)
1290                 return -EFAULT;
1291
1292         if (trace) {
1293                 priv->event_log.ucode_trace = true;
1294                 /* schedule the ucode timer to occur in UCODE_TRACE_PERIOD */
1295                 mod_timer(&priv->ucode_trace,
1296                         jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
1297         } else {
1298                 priv->event_log.ucode_trace = false;
1299                 del_timer_sync(&priv->ucode_trace);
1300         }
1301
1302         return count;
1303 }
1304
1305 static ssize_t iwl_dbgfs_rxon_flags_read(struct file *file,
1306                                          char __user *user_buf,
1307                                          size_t count, loff_t *ppos) {
1308
1309         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
1310         int len = 0;
1311         char buf[20];
1312
1313         len = sprintf(buf, "0x%04X\n", le32_to_cpu(priv->active_rxon.flags));
1314         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1315 }
1316
1317 static ssize_t iwl_dbgfs_rxon_filter_flags_read(struct file *file,
1318                                                 char __user *user_buf,
1319                                                 size_t count, loff_t *ppos) {
1320
1321         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
1322         int len = 0;
1323         char buf[20];
1324
1325         len = sprintf(buf, "0x%04X\n",
1326                       le32_to_cpu(priv->active_rxon.filter_flags));
1327         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1328 }
1329
1330 static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
1331                                          char __user *user_buf,
1332                                          size_t count, loff_t *ppos)
1333 {
1334         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
1335         char *buf;
1336         int pos = 0;
1337         ssize_t ret = -EFAULT;
1338
1339         if (priv->cfg->ops->lib->dump_fh) {
1340                 ret = pos = priv->cfg->ops->lib->dump_fh(priv, &buf, true);
1341                 if (buf) {
1342                         ret = simple_read_from_buffer(user_buf,
1343                                                       count, ppos, buf, pos);
1344                         kfree(buf);
1345                 }
1346         }
1347
1348         return ret;
1349 }
1350
1351 static ssize_t iwl_dbgfs_missed_beacon_read(struct file *file,
1352                                         char __user *user_buf,
1353                                         size_t count, loff_t *ppos) {
1354
1355         struct iwl_priv *priv = file->private_data;
1356         int pos = 0;
1357         char buf[12];
1358         const size_t bufsz = sizeof(buf);
1359
1360         pos += scnprintf(buf + pos, bufsz - pos, "%d\n",
1361                         priv->missed_beacon_threshold);
1362
1363         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1364 }
1365
1366 static ssize_t iwl_dbgfs_missed_beacon_write(struct file *file,
1367                                          const char __user *user_buf,
1368                                          size_t count, loff_t *ppos)
1369 {
1370         struct iwl_priv *priv = file->private_data;
1371         char buf[8];
1372         int buf_size;
1373         int missed;
1374
1375         memset(buf, 0, sizeof(buf));
1376         buf_size = min(count, sizeof(buf) -  1);
1377         if (copy_from_user(buf, user_buf, buf_size))
1378                 return -EFAULT;
1379         if (sscanf(buf, "%d", &missed) != 1)
1380                 return -EINVAL;
1381
1382         if (missed < IWL_MISSED_BEACON_THRESHOLD_MIN ||
1383             missed > IWL_MISSED_BEACON_THRESHOLD_MAX)
1384                 priv->missed_beacon_threshold =
1385                         IWL_MISSED_BEACON_THRESHOLD_DEF;
1386         else
1387                 priv->missed_beacon_threshold = missed;
1388
1389         return count;
1390 }
1391
1392 static ssize_t iwl_dbgfs_plcp_delta_read(struct file *file,
1393                                         char __user *user_buf,
1394                                         size_t count, loff_t *ppos) {
1395
1396         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
1397         int pos = 0;
1398         char buf[12];
1399         const size_t bufsz = sizeof(buf);
1400
1401         pos += scnprintf(buf + pos, bufsz - pos, "%u\n",
1402                         priv->cfg->plcp_delta_threshold);
1403
1404         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1405 }
1406
1407 static ssize_t iwl_dbgfs_plcp_delta_write(struct file *file,
1408                                         const char __user *user_buf,
1409                                         size_t count, loff_t *ppos) {
1410
1411         struct iwl_priv *priv = file->private_data;
1412         char buf[8];
1413         int buf_size;
1414         int plcp;
1415
1416         memset(buf, 0, sizeof(buf));
1417         buf_size = min(count, sizeof(buf) -  1);
1418         if (copy_from_user(buf, user_buf, buf_size))
1419                 return -EFAULT;
1420         if (sscanf(buf, "%d", &plcp) != 1)
1421                 return -EINVAL;
1422         if ((plcp <= IWL_MAX_PLCP_ERR_THRESHOLD_MIN) ||
1423                 (plcp > IWL_MAX_PLCP_ERR_THRESHOLD_MAX))
1424                 priv->cfg->plcp_delta_threshold =
1425                         IWL_MAX_PLCP_ERR_THRESHOLD_DEF;
1426         else
1427                 priv->cfg->plcp_delta_threshold = plcp;
1428         return count;
1429 }
1430
1431 static ssize_t iwl_dbgfs_force_reset_read(struct file *file,
1432                                         char __user *user_buf,
1433                                         size_t count, loff_t *ppos) {
1434
1435         struct iwl_priv *priv = file->private_data;
1436         int i, pos = 0;
1437         char buf[300];
1438         const size_t bufsz = sizeof(buf);
1439         struct iwl_force_reset *force_reset;
1440
1441         for (i = 0; i < IWL_MAX_FORCE_RESET; i++) {
1442                 force_reset = &priv->force_reset[i];
1443                 pos += scnprintf(buf + pos, bufsz - pos,
1444                                 "Force reset method %d\n", i);
1445                 pos += scnprintf(buf + pos, bufsz - pos,
1446                                 "\tnumber of reset request: %d\n",
1447                                 force_reset->reset_request_count);
1448                 pos += scnprintf(buf + pos, bufsz - pos,
1449                                 "\tnumber of reset request success: %d\n",
1450                                 force_reset->reset_success_count);
1451                 pos += scnprintf(buf + pos, bufsz - pos,
1452                                 "\tnumber of reset request reject: %d\n",
1453                                 force_reset->reset_reject_count);
1454                 pos += scnprintf(buf + pos, bufsz - pos,
1455                                 "\treset duration: %lu\n",
1456                                 force_reset->reset_duration);
1457         }
1458         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1459 }
1460
1461 static ssize_t iwl_dbgfs_force_reset_write(struct file *file,
1462                                         const char __user *user_buf,
1463                                         size_t count, loff_t *ppos) {
1464
1465         struct iwl_priv *priv = file->private_data;
1466         char buf[8];
1467         int buf_size;
1468         int reset, ret;
1469
1470         memset(buf, 0, sizeof(buf));
1471         buf_size = min(count, sizeof(buf) -  1);
1472         if (copy_from_user(buf, user_buf, buf_size))
1473                 return -EFAULT;
1474         if (sscanf(buf, "%d", &reset) != 1)
1475                 return -EINVAL;
1476         switch (reset) {
1477         case IWL_RF_RESET:
1478         case IWL_FW_RESET:
1479                 ret = iwl_force_reset(priv, reset);
1480                 break;
1481         default:
1482                 return -EINVAL;
1483         }
1484         return ret ? ret : count;
1485 }
1486
1487 DEBUGFS_READ_FILE_OPS(rx_statistics);
1488 DEBUGFS_READ_FILE_OPS(tx_statistics);
1489 DEBUGFS_READ_WRITE_FILE_OPS(traffic_log);
1490 DEBUGFS_READ_FILE_OPS(rx_queue);
1491 DEBUGFS_READ_FILE_OPS(tx_queue);
1492 DEBUGFS_READ_FILE_OPS(ucode_rx_stats);
1493 DEBUGFS_READ_FILE_OPS(ucode_tx_stats);
1494 DEBUGFS_READ_FILE_OPS(ucode_general_stats);
1495 DEBUGFS_READ_FILE_OPS(sensitivity);
1496 DEBUGFS_READ_FILE_OPS(chain_noise);
1497 DEBUGFS_READ_FILE_OPS(power_save_status);
1498 DEBUGFS_WRITE_FILE_OPS(clear_ucode_statistics);
1499 DEBUGFS_WRITE_FILE_OPS(clear_traffic_statistics);
1500 DEBUGFS_WRITE_FILE_OPS(csr);
1501 DEBUGFS_READ_WRITE_FILE_OPS(ucode_tracing);
1502 DEBUGFS_READ_FILE_OPS(fh_reg);
1503 DEBUGFS_READ_WRITE_FILE_OPS(missed_beacon);
1504 DEBUGFS_READ_WRITE_FILE_OPS(plcp_delta);
1505 DEBUGFS_READ_WRITE_FILE_OPS(force_reset);
1506 DEBUGFS_READ_FILE_OPS(rxon_flags);
1507 DEBUGFS_READ_FILE_OPS(rxon_filter_flags);
1508
1509 /*
1510  * Create the debugfs files and directories
1511  *
1512  */
1513 int iwl_dbgfs_register(struct iwl_priv *priv, const char *name)
1514 {
1515         struct dentry *phyd = priv->hw->wiphy->debugfsdir;
1516         struct dentry *dir_drv, *dir_data, *dir_rf, *dir_debug;
1517
1518         dir_drv = debugfs_create_dir(name, phyd);
1519         if (!dir_drv)
1520                 return -ENOMEM;
1521
1522         priv->debugfs_dir = dir_drv;
1523
1524         dir_data = debugfs_create_dir("data", dir_drv);
1525         if (!dir_data)
1526                 goto err;
1527         dir_rf = debugfs_create_dir("rf", dir_drv);
1528         if (!dir_rf)
1529                 goto err;
1530         dir_debug = debugfs_create_dir("debug", dir_drv);
1531         if (!dir_debug)
1532                 goto err;
1533
1534         DEBUGFS_ADD_FILE(nvm, dir_data, S_IRUSR);
1535         DEBUGFS_ADD_FILE(sram, dir_data, S_IWUSR | S_IRUSR);
1536         DEBUGFS_ADD_FILE(log_event, dir_data, S_IWUSR | S_IRUSR);
1537         DEBUGFS_ADD_FILE(stations, dir_data, S_IRUSR);
1538         DEBUGFS_ADD_FILE(channels, dir_data, S_IRUSR);
1539         DEBUGFS_ADD_FILE(status, dir_data, S_IRUSR);
1540         DEBUGFS_ADD_FILE(interrupt, dir_data, S_IWUSR | S_IRUSR);
1541         DEBUGFS_ADD_FILE(qos, dir_data, S_IRUSR);
1542         DEBUGFS_ADD_FILE(led, dir_data, S_IRUSR);
1543         if (!priv->cfg->broken_powersave) {
1544                 DEBUGFS_ADD_FILE(sleep_level_override, dir_data,
1545                                  S_IWUSR | S_IRUSR);
1546                 DEBUGFS_ADD_FILE(current_sleep_command, dir_data, S_IRUSR);
1547         }
1548         DEBUGFS_ADD_FILE(thermal_throttling, dir_data, S_IRUSR);
1549         DEBUGFS_ADD_FILE(disable_ht40, dir_data, S_IWUSR | S_IRUSR);
1550         DEBUGFS_ADD_FILE(rx_statistics, dir_debug, S_IRUSR);
1551         DEBUGFS_ADD_FILE(tx_statistics, dir_debug, S_IRUSR);
1552         DEBUGFS_ADD_FILE(traffic_log, dir_debug, S_IWUSR | S_IRUSR);
1553         DEBUGFS_ADD_FILE(rx_queue, dir_debug, S_IRUSR);
1554         DEBUGFS_ADD_FILE(tx_queue, dir_debug, S_IRUSR);
1555         DEBUGFS_ADD_FILE(power_save_status, dir_debug, S_IRUSR);
1556         DEBUGFS_ADD_FILE(clear_ucode_statistics, dir_debug, S_IWUSR);
1557         DEBUGFS_ADD_FILE(clear_traffic_statistics, dir_debug, S_IWUSR);
1558         DEBUGFS_ADD_FILE(csr, dir_debug, S_IWUSR);
1559         DEBUGFS_ADD_FILE(fh_reg, dir_debug, S_IRUSR);
1560         DEBUGFS_ADD_FILE(missed_beacon, dir_debug, S_IWUSR);
1561         DEBUGFS_ADD_FILE(plcp_delta, dir_debug, S_IWUSR | S_IRUSR);
1562         DEBUGFS_ADD_FILE(force_reset, dir_debug, S_IWUSR | S_IRUSR);
1563         DEBUGFS_ADD_FILE(ucode_rx_stats, dir_debug, S_IRUSR);
1564         DEBUGFS_ADD_FILE(ucode_tx_stats, dir_debug, S_IRUSR);
1565         DEBUGFS_ADD_FILE(ucode_general_stats, dir_debug, S_IRUSR);
1566
1567         if (priv->cfg->sensitivity_calib_by_driver)
1568                 DEBUGFS_ADD_FILE(sensitivity, dir_debug, S_IRUSR);
1569         if (priv->cfg->chain_noise_calib_by_driver)
1570                 DEBUGFS_ADD_FILE(chain_noise, dir_debug, S_IRUSR);
1571         if (priv->cfg->ucode_tracing)
1572                 DEBUGFS_ADD_FILE(ucode_tracing, dir_debug, S_IWUSR | S_IRUSR);
1573         DEBUGFS_ADD_FILE(rxon_flags, dir_debug, S_IWUSR);
1574         DEBUGFS_ADD_FILE(rxon_filter_flags, dir_debug, S_IWUSR);
1575         if (priv->cfg->sensitivity_calib_by_driver)
1576                 DEBUGFS_ADD_BOOL(disable_sensitivity, dir_rf,
1577                                  &priv->disable_sens_cal);
1578         if (priv->cfg->chain_noise_calib_by_driver)
1579                 DEBUGFS_ADD_BOOL(disable_chain_noise, dir_rf,
1580                                  &priv->disable_chain_noise_cal);
1581         if (priv->cfg->tx_power_by_driver)
1582                 DEBUGFS_ADD_BOOL(disable_tx_power, dir_rf,
1583                                 &priv->disable_tx_power_cal);
1584         return 0;
1585
1586 err:
1587         IWL_ERR(priv, "Can't create the debugfs directory\n");
1588         iwl_dbgfs_unregister(priv);
1589         return -ENOMEM;
1590 }
1591 EXPORT_SYMBOL(iwl_dbgfs_register);
1592
1593 /**
1594  * Remove the debugfs files and directories
1595  *
1596  */
1597 void iwl_dbgfs_unregister(struct iwl_priv *priv)
1598 {
1599         if (!priv->debugfs_dir)
1600                 return;
1601
1602         debugfs_remove_recursive(priv->debugfs_dir);
1603         priv->debugfs_dir = NULL;
1604 }
1605 EXPORT_SYMBOL(iwl_dbgfs_unregister);
1606
1607
1608