Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6] / drivers / net / wireless / iwlwifi / iwl-debugfs.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 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  * Tomas Winkler <tomas.winkler@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-4965.h"
38 #include "iwl-debug.h"
39 #include "iwl-core.h"
40 #include "iwl-io.h"
41
42
43 /* create and remove of files */
44 #define DEBUGFS_ADD_DIR(name, parent) do {                              \
45         dbgfs->dir_##name = debugfs_create_dir(#name, parent);          \
46         if (!(dbgfs->dir_##name))                                       \
47                 goto err;                                               \
48 } while (0)
49
50 #define DEBUGFS_ADD_FILE(name, parent) do {                             \
51         dbgfs->dbgfs_##parent##_files.file_##name =                     \
52         debugfs_create_file(#name, 0644, dbgfs->dir_##parent, priv,     \
53                                 &iwl_dbgfs_##name##_ops);               \
54         if (!(dbgfs->dbgfs_##parent##_files.file_##name))               \
55                 goto err;                                               \
56 } while (0)
57
58 #define DEBUGFS_REMOVE(name)  do {              \
59         debugfs_remove(name);                   \
60         name = NULL;                            \
61 } while (0);
62
63 /* file operation */
64 #define DEBUGFS_READ_FUNC(name)                                         \
65 static ssize_t iwl_dbgfs_##name##_read(struct file *file,               \
66                                         char __user *user_buf,          \
67                                         size_t count, loff_t *ppos);
68
69 #define DEBUGFS_WRITE_FUNC(name)                                        \
70 static ssize_t iwl_dbgfs_##name##_write(struct file *file,              \
71                                         const char __user *user_buf,    \
72                                         size_t count, loff_t *ppos);
73
74
75 static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
76 {
77         file->private_data = inode->i_private;
78         return 0;
79 }
80
81 #define DEBUGFS_READ_FILE_OPS(name)                                     \
82         DEBUGFS_READ_FUNC(name);                                        \
83 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
84         .read = iwl_dbgfs_##name##_read,                                \
85         .open = iwl_dbgfs_open_file_generic,                            \
86 };
87
88 #define DEBUGFS_READ_WRITE_FILE_OPS(name)                               \
89         DEBUGFS_READ_FUNC(name);                                        \
90         DEBUGFS_WRITE_FUNC(name);                                       \
91 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
92         .write = iwl_dbgfs_##name##_write,                              \
93         .read = iwl_dbgfs_##name##_read,                                \
94         .open = iwl_dbgfs_open_file_generic,                            \
95 };
96
97
98 static ssize_t iwl_dbgfs_tx_statistics_read(struct file *file,
99                                                 char __user *user_buf,
100                                                 size_t count, loff_t *ppos) {
101
102         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
103         char buf[256];
104         int pos = 0;
105         const size_t bufsz = sizeof(buf);
106
107         pos += scnprintf(buf + pos, bufsz - pos, "mgmt: %u\n",
108                                                 priv->tx_stats[0].cnt);
109         pos += scnprintf(buf + pos, bufsz - pos, "ctrl: %u\n",
110                                                 priv->tx_stats[1].cnt);
111         pos += scnprintf(buf + pos, bufsz - pos, "data: %u\n",
112                                                 priv->tx_stats[2].cnt);
113
114         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
115 }
116
117 static ssize_t iwl_dbgfs_rx_statistics_read(struct file *file,
118                                                 char __user *user_buf,
119                                                 size_t count, loff_t *ppos) {
120
121         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
122         char buf[256];
123         int pos = 0;
124         const size_t bufsz = sizeof(buf);
125
126         pos += scnprintf(buf + pos, bufsz - pos, "mgmt: %u\n",
127                                                 priv->rx_stats[0].cnt);
128         pos += scnprintf(buf + pos, bufsz - pos, "ctrl: %u\n",
129                                                 priv->rx_stats[1].cnt);
130         pos += scnprintf(buf + pos, bufsz - pos, "data: %u\n",
131                                                 priv->rx_stats[2].cnt);
132
133         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
134 }
135
136 #define BYTE1_MASK 0x000000ff;
137 #define BYTE2_MASK 0x0000ffff;
138 #define BYTE3_MASK 0x00ffffff;
139 static ssize_t iwl_dbgfs_sram_read(struct file *file,
140                                         char __user *user_buf,
141                                         size_t count, loff_t *ppos)
142 {
143         u32 val;
144         char buf[1024];
145         ssize_t ret;
146         int i;
147         int pos = 0;
148         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
149         const size_t bufsz = sizeof(buf);
150
151         printk(KERN_DEBUG "offset is: 0x%x\tlen is: 0x%x\n",
152         priv->dbgfs->sram_offset, priv->dbgfs->sram_len);
153
154         iwl_grab_nic_access(priv);
155         for (i = priv->dbgfs->sram_len; i > 0; i -= 4) {
156                 val = iwl_read_targ_mem(priv, priv->dbgfs->sram_offset + \
157                                         priv->dbgfs->sram_len - i);
158                 if (i < 4) {
159                         switch (i) {
160                         case 1:
161                                 val &= BYTE1_MASK;
162                                 break;
163                         case 2:
164                                 val &= BYTE2_MASK;
165                                 break;
166                         case 3:
167                                 val &= BYTE3_MASK;
168                                 break;
169                         }
170                 }
171                 pos += scnprintf(buf + pos, bufsz - pos, "0x%08x ", val);
172         }
173         pos += scnprintf(buf + pos, bufsz - pos, "\n");
174         iwl_release_nic_access(priv);
175
176         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
177         return ret;
178 }
179
180 static ssize_t iwl_dbgfs_sram_write(struct file *file,
181                                         const char __user *user_buf,
182                                         size_t count, loff_t *ppos)
183 {
184         struct iwl_priv *priv = file->private_data;
185         char buf[64];
186         int buf_size;
187         u32 offset, len;
188
189         memset(buf, 0, sizeof(buf));
190         buf_size = min(count, sizeof(buf) -  1);
191         if (copy_from_user(buf, user_buf, buf_size))
192                 return -EFAULT;
193
194         if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
195                 priv->dbgfs->sram_offset = offset;
196                 priv->dbgfs->sram_len = len;
197         } else {
198                 priv->dbgfs->sram_offset = 0;
199                 priv->dbgfs->sram_len = 0;
200         }
201
202         return count;
203 }
204
205 static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
206                                         size_t count, loff_t *ppos)
207 {
208         struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
209         struct iwl4965_station_entry *station;
210         int max_sta = priv->hw_params.max_stations;
211         char *buf;
212         int i, j, pos = 0;
213         ssize_t ret;
214         /* Add 30 for initial string */
215         const size_t bufsz = 30 + sizeof(char) * 500 * (priv->num_stations);
216         DECLARE_MAC_BUF(mac);
217
218         buf = kmalloc(bufsz, GFP_KERNEL);
219         if(!buf)
220                 return -ENOMEM;
221
222         pos += scnprintf(buf + pos, bufsz - pos, "num of stations: %d\n\n",
223                         priv->num_stations);
224
225         for (i = 0; i < max_sta; i++) {
226                 station = &priv->stations[i];
227                 if (station->used) {
228                         pos += scnprintf(buf + pos, bufsz - pos,
229                                         "station %d:\ngeneral data:\n", i+1);
230                         print_mac(mac, station->sta.sta.addr);
231                         pos += scnprintf(buf + pos, bufsz - pos, "id: %u\n",
232                                         station->sta.sta.sta_id);
233                         pos += scnprintf(buf + pos, bufsz - pos, "mode: %u\n",
234                                         station->sta.mode);
235                         pos += scnprintf(buf + pos, bufsz - pos,
236                                         "flags: 0x%x\n",
237                                         station->sta.station_flags_msk);
238                         pos += scnprintf(buf + pos, bufsz - pos,
239                                         "ps_status: %u\n", station->ps_status);
240                         pos += scnprintf(buf + pos, bufsz - pos, "tid data:\n");
241                         pos += scnprintf(buf + pos, bufsz - pos,
242                                         "seq_num\t\ttxq_id");
243 #ifdef CONFIG_IWL4965_HT
244                         pos += scnprintf(buf + pos, bufsz - pos,
245                                         "\tframe_count\twait_for_ba\t");
246                         pos += scnprintf(buf + pos, bufsz - pos,
247                                         "start_idx\tbitmap0\t");
248                         pos += scnprintf(buf + pos, bufsz - pos,
249                                         "bitmap1\trate_n_flags");
250 #endif
251                         pos += scnprintf(buf + pos, bufsz - pos, "\n");
252
253                         for (j = 0; j < MAX_TID_COUNT; j++) {
254                                 pos += scnprintf(buf + pos, bufsz - pos,
255                                                 "[%d]:\t\t%u", j,
256                                                 station->tid[j].seq_number);
257 #ifdef CONFIG_IWL4965_HT
258                                 pos += scnprintf(buf + pos, bufsz - pos,
259                                                 "\t%u\t\t%u\t\t%u\t\t",
260                                                 station->tid[j].agg.txq_id,
261                                                 station->tid[j].agg.frame_count,
262                                                 station->tid[j].agg.wait_for_ba);
263                                 pos += scnprintf(buf + pos, bufsz - pos,
264                                                 "%u\t%llu\t%u",
265                                                 station->tid[j].agg.start_idx,
266                                                 (unsigned long long)station->tid[j].agg.bitmap,
267                                                 station->tid[j].agg.rate_n_flags);
268 #endif
269                                 pos += scnprintf(buf + pos, bufsz - pos, "\n");
270                         }
271                         pos += scnprintf(buf + pos, bufsz - pos, "\n");
272                 }
273         }
274
275         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
276         kfree(buf);
277         return ret;
278 }
279
280
281 DEBUGFS_READ_WRITE_FILE_OPS(sram);
282 DEBUGFS_READ_FILE_OPS(stations);
283 DEBUGFS_READ_FILE_OPS(rx_statistics);
284 DEBUGFS_READ_FILE_OPS(tx_statistics);
285
286 /*
287  * Create the debugfs files and directories
288  *
289  */
290 int iwl_dbgfs_register(struct iwl_priv *priv, const char *name)
291 {
292         struct iwl_debugfs *dbgfs;
293
294         dbgfs = kzalloc(sizeof(struct iwl_debugfs), GFP_KERNEL);
295         if (!dbgfs) {
296                 goto err;
297         }
298
299         priv->dbgfs = dbgfs;
300         dbgfs->name = name;
301         dbgfs->dir_drv = debugfs_create_dir(name, NULL);
302         if (!dbgfs->dir_drv || IS_ERR(dbgfs->dir_drv)){
303                 goto err;
304         }
305
306         DEBUGFS_ADD_DIR(data, dbgfs->dir_drv);
307         DEBUGFS_ADD_FILE(sram, data);
308         DEBUGFS_ADD_FILE(stations, data);
309         DEBUGFS_ADD_FILE(rx_statistics, data);
310         DEBUGFS_ADD_FILE(tx_statistics, data);
311
312         return 0;
313
314 err:
315         IWL_ERROR("Can't open the debugfs directory\n");
316         iwl_dbgfs_unregister(priv);
317         return -ENOENT;
318 }
319 EXPORT_SYMBOL(iwl_dbgfs_register);
320
321 /**
322  * Remove the debugfs files and directories
323  *
324  */
325 void iwl_dbgfs_unregister(struct iwl_priv *priv)
326 {
327         if (!(priv->dbgfs))
328                 return;
329
330         DEBUGFS_REMOVE(priv->dbgfs->dbgfs_data_files.file_rx_statistics);
331         DEBUGFS_REMOVE(priv->dbgfs->dbgfs_data_files.file_tx_statistics);
332         DEBUGFS_REMOVE(priv->dbgfs->dbgfs_data_files.file_sram);
333         DEBUGFS_REMOVE(priv->dbgfs->dbgfs_data_files.file_stations);
334         DEBUGFS_REMOVE(priv->dbgfs->dir_data);
335         DEBUGFS_REMOVE(priv->dbgfs->dir_drv);
336         kfree(priv->dbgfs);
337         priv->dbgfs = NULL;
338 }
339 EXPORT_SYMBOL(iwl_dbgfs_unregister);
340
341