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