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