5237369cd5215e5a17bb4981b9e445aa7aeaca62
[pandora-kernel.git] / drivers / net / wireless / ath / ath6kl / debug.c
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "core.h"
18
19 #include <linux/circ_buf.h>
20 #include <linux/fs.h>
21
22 #include "debug.h"
23 #include "target.h"
24
25 struct ath6kl_fwlog_slot {
26         __le32 timestamp;
27         __le32 length;
28
29         /* max ATH6KL_FWLOG_PAYLOAD_SIZE bytes */
30         u8 payload[0];
31 };
32
33 #define ATH6KL_FWLOG_SIZE 32768
34 #define ATH6KL_FWLOG_SLOT_SIZE (sizeof(struct ath6kl_fwlog_slot) + \
35                                 ATH6KL_FWLOG_PAYLOAD_SIZE)
36 #define ATH6KL_FWLOG_VALID_MASK 0x1ffff
37
38 int ath6kl_printk(const char *level, const char *fmt, ...)
39 {
40         struct va_format vaf;
41         va_list args;
42         int rtn;
43
44         va_start(args, fmt);
45
46         vaf.fmt = fmt;
47         vaf.va = &args;
48
49         rtn = printk("%sath6kl: %pV", level, &vaf);
50
51         va_end(args);
52
53         return rtn;
54 }
55
56 #ifdef CONFIG_ATH6KL_DEBUG
57
58 #define REG_OUTPUT_LEN_PER_LINE 25
59 #define REGTYPE_STR_LEN         100
60
61 struct ath6kl_diag_reg_info {
62         u32 reg_start;
63         u32 reg_end;
64         const char *reg_info;
65 };
66
67 static const struct ath6kl_diag_reg_info diag_reg[] = {
68         { 0x20000, 0x200fc, "General DMA and Rx registers" },
69         { 0x28000, 0x28900, "MAC PCU register & keycache" },
70         { 0x20800, 0x20a40, "QCU" },
71         { 0x21000, 0x212f0, "DCU" },
72         { 0x4000,  0x42e4, "RTC" },
73         { 0x540000, 0x540000 + (256 * 1024), "RAM" },
74         { 0x29800, 0x2B210, "Base Band" },
75         { 0x1C000, 0x1C748, "Analog" },
76 };
77
78 void ath6kl_dump_registers(struct ath6kl_device *dev,
79                            struct ath6kl_irq_proc_registers *irq_proc_reg,
80                            struct ath6kl_irq_enable_reg *irq_enable_reg)
81 {
82
83         ath6kl_dbg(ATH6KL_DBG_ANY, ("<------- Register Table -------->\n"));
84
85         if (irq_proc_reg != NULL) {
86                 ath6kl_dbg(ATH6KL_DBG_ANY,
87                         "Host Int status:           0x%x\n",
88                         irq_proc_reg->host_int_status);
89                 ath6kl_dbg(ATH6KL_DBG_ANY,
90                            "CPU Int status:            0x%x\n",
91                         irq_proc_reg->cpu_int_status);
92                 ath6kl_dbg(ATH6KL_DBG_ANY,
93                            "Error Int status:          0x%x\n",
94                         irq_proc_reg->error_int_status);
95                 ath6kl_dbg(ATH6KL_DBG_ANY,
96                            "Counter Int status:        0x%x\n",
97                         irq_proc_reg->counter_int_status);
98                 ath6kl_dbg(ATH6KL_DBG_ANY,
99                            "Mbox Frame:                0x%x\n",
100                         irq_proc_reg->mbox_frame);
101                 ath6kl_dbg(ATH6KL_DBG_ANY,
102                            "Rx Lookahead Valid:        0x%x\n",
103                         irq_proc_reg->rx_lkahd_valid);
104                 ath6kl_dbg(ATH6KL_DBG_ANY,
105                            "Rx Lookahead 0:            0x%x\n",
106                         irq_proc_reg->rx_lkahd[0]);
107                 ath6kl_dbg(ATH6KL_DBG_ANY,
108                            "Rx Lookahead 1:            0x%x\n",
109                         irq_proc_reg->rx_lkahd[1]);
110
111                 if (dev->ar->mbox_info.gmbox_addr != 0) {
112                         /*
113                          * If the target supports GMBOX hardware, dump some
114                          * additional state.
115                          */
116                         ath6kl_dbg(ATH6KL_DBG_ANY,
117                                 "GMBOX Host Int status 2:   0x%x\n",
118                                 irq_proc_reg->host_int_status2);
119                         ath6kl_dbg(ATH6KL_DBG_ANY,
120                                 "GMBOX RX Avail:            0x%x\n",
121                                 irq_proc_reg->gmbox_rx_avail);
122                         ath6kl_dbg(ATH6KL_DBG_ANY,
123                                 "GMBOX lookahead alias 0:   0x%x\n",
124                                 irq_proc_reg->rx_gmbox_lkahd_alias[0]);
125                         ath6kl_dbg(ATH6KL_DBG_ANY,
126                                 "GMBOX lookahead alias 1:   0x%x\n",
127                                 irq_proc_reg->rx_gmbox_lkahd_alias[1]);
128                 }
129
130         }
131
132         if (irq_enable_reg != NULL) {
133                 ath6kl_dbg(ATH6KL_DBG_ANY,
134                         "Int status Enable:         0x%x\n",
135                         irq_enable_reg->int_status_en);
136                 ath6kl_dbg(ATH6KL_DBG_ANY, "Counter Int status Enable: 0x%x\n",
137                         irq_enable_reg->cntr_int_status_en);
138         }
139         ath6kl_dbg(ATH6KL_DBG_ANY, "<------------------------------->\n");
140 }
141
142 static void dump_cred_dist(struct htc_endpoint_credit_dist *ep_dist)
143 {
144         ath6kl_dbg(ATH6KL_DBG_ANY,
145                    "--- endpoint: %d  svc_id: 0x%X ---\n",
146                    ep_dist->endpoint, ep_dist->svc_id);
147         ath6kl_dbg(ATH6KL_DBG_ANY, " dist_flags     : 0x%X\n",
148                    ep_dist->dist_flags);
149         ath6kl_dbg(ATH6KL_DBG_ANY, " cred_norm      : %d\n",
150                    ep_dist->cred_norm);
151         ath6kl_dbg(ATH6KL_DBG_ANY, " cred_min       : %d\n",
152                    ep_dist->cred_min);
153         ath6kl_dbg(ATH6KL_DBG_ANY, " credits        : %d\n",
154                    ep_dist->credits);
155         ath6kl_dbg(ATH6KL_DBG_ANY, " cred_assngd    : %d\n",
156                    ep_dist->cred_assngd);
157         ath6kl_dbg(ATH6KL_DBG_ANY, " seek_cred      : %d\n",
158                    ep_dist->seek_cred);
159         ath6kl_dbg(ATH6KL_DBG_ANY, " cred_sz        : %d\n",
160                    ep_dist->cred_sz);
161         ath6kl_dbg(ATH6KL_DBG_ANY, " cred_per_msg   : %d\n",
162                    ep_dist->cred_per_msg);
163         ath6kl_dbg(ATH6KL_DBG_ANY, " cred_to_dist   : %d\n",
164                    ep_dist->cred_to_dist);
165         ath6kl_dbg(ATH6KL_DBG_ANY, " txq_depth      : %d\n",
166                    get_queue_depth(&((struct htc_endpoint *)
167                                      ep_dist->htc_rsvd)->txq));
168         ath6kl_dbg(ATH6KL_DBG_ANY,
169                    "----------------------------------\n");
170 }
171
172 void dump_cred_dist_stats(struct htc_target *target)
173 {
174         struct htc_endpoint_credit_dist *ep_list;
175
176         if (!AR_DBG_LVL_CHECK(ATH6KL_DBG_TRC))
177                 return;
178
179         list_for_each_entry(ep_list, &target->cred_dist_list, list)
180                 dump_cred_dist(ep_list);
181
182         ath6kl_dbg(ATH6KL_DBG_HTC_SEND, "ctxt:%p dist:%p\n",
183                    target->cred_dist_cntxt, NULL);
184         ath6kl_dbg(ATH6KL_DBG_TRC, "credit distribution, total : %d, free : %d\n",
185                    target->cred_dist_cntxt->total_avail_credits,
186                    target->cred_dist_cntxt->cur_free_credits);
187 }
188
189 static int ath6kl_debugfs_open(struct inode *inode, struct file *file)
190 {
191         file->private_data = inode->i_private;
192         return 0;
193 }
194
195 void ath6kl_debug_war(struct ath6kl *ar, enum ath6kl_war war)
196 {
197         switch (war) {
198         case ATH6KL_WAR_INVALID_RATE:
199                 ar->debug.war_stats.invalid_rate++;
200                 break;
201         }
202 }
203
204 static ssize_t read_file_war_stats(struct file *file, char __user *user_buf,
205                                    size_t count, loff_t *ppos)
206 {
207         struct ath6kl *ar = file->private_data;
208         char *buf;
209         unsigned int len = 0, buf_len = 1500;
210         ssize_t ret_cnt;
211
212         buf = kzalloc(buf_len, GFP_KERNEL);
213         if (!buf)
214                 return -ENOMEM;
215
216         len += scnprintf(buf + len, buf_len - len, "\n");
217         len += scnprintf(buf + len, buf_len - len, "%25s\n",
218                          "Workaround stats");
219         len += scnprintf(buf + len, buf_len - len, "%25s\n\n",
220                          "=================");
221         len += scnprintf(buf + len, buf_len - len, "%20s %10u\n",
222                          "Invalid rates", ar->debug.war_stats.invalid_rate);
223
224         if (WARN_ON(len > buf_len))
225                 len = buf_len;
226
227         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
228
229         kfree(buf);
230         return ret_cnt;
231 }
232
233 static const struct file_operations fops_war_stats = {
234         .read = read_file_war_stats,
235         .open = ath6kl_debugfs_open,
236         .owner = THIS_MODULE,
237         .llseek = default_llseek,
238 };
239
240 static void ath6kl_debug_fwlog_add(struct ath6kl *ar, const void *buf,
241                                    size_t buf_len)
242 {
243         struct circ_buf *fwlog = &ar->debug.fwlog_buf;
244         size_t space;
245         int i;
246
247         /* entries must all be equal size */
248         if (WARN_ON(buf_len != ATH6KL_FWLOG_SLOT_SIZE))
249                 return;
250
251         space = CIRC_SPACE(fwlog->head, fwlog->tail, ATH6KL_FWLOG_SIZE);
252         if (space < buf_len)
253                 /* discard oldest slot */
254                 fwlog->tail = (fwlog->tail + ATH6KL_FWLOG_SLOT_SIZE) &
255                         (ATH6KL_FWLOG_SIZE - 1);
256
257         for (i = 0; i < buf_len; i += space) {
258                 space = CIRC_SPACE_TO_END(fwlog->head, fwlog->tail,
259                                           ATH6KL_FWLOG_SIZE);
260
261                 if ((size_t) space > buf_len - i)
262                         space = buf_len - i;
263
264                 memcpy(&fwlog->buf[fwlog->head], buf, space);
265                 fwlog->head = (fwlog->head + space) & (ATH6KL_FWLOG_SIZE - 1);
266         }
267
268 }
269
270 void ath6kl_debug_fwlog_event(struct ath6kl *ar, const void *buf, size_t len)
271 {
272         struct ath6kl_fwlog_slot *slot = ar->debug.fwlog_tmp;
273         size_t slot_len;
274
275         if (WARN_ON(len > ATH6KL_FWLOG_PAYLOAD_SIZE))
276                 return;
277
278         spin_lock_bh(&ar->debug.fwlog_lock);
279
280         slot->timestamp = cpu_to_le32(jiffies);
281         slot->length = cpu_to_le32(len);
282         memcpy(slot->payload, buf, len);
283
284         slot_len = sizeof(*slot) + len;
285
286         if (slot_len < ATH6KL_FWLOG_SLOT_SIZE)
287                 memset(slot->payload + len, 0,
288                        ATH6KL_FWLOG_SLOT_SIZE - slot_len);
289
290         ath6kl_debug_fwlog_add(ar, slot, ATH6KL_FWLOG_SLOT_SIZE);
291
292         spin_unlock_bh(&ar->debug.fwlog_lock);
293 }
294
295 static bool ath6kl_debug_fwlog_empty(struct ath6kl *ar)
296 {
297         return CIRC_CNT(ar->debug.fwlog_buf.head,
298                         ar->debug.fwlog_buf.tail,
299                         ATH6KL_FWLOG_SLOT_SIZE) == 0;
300 }
301
302 static ssize_t ath6kl_fwlog_read(struct file *file, char __user *user_buf,
303                                  size_t count, loff_t *ppos)
304 {
305         struct ath6kl *ar = file->private_data;
306         struct circ_buf *fwlog = &ar->debug.fwlog_buf;
307         size_t len = 0, buf_len = count;
308         ssize_t ret_cnt;
309         char *buf;
310         int ccnt;
311
312         buf = vmalloc(buf_len);
313         if (!buf)
314                 return -ENOMEM;
315
316         /* read undelivered logs from firmware */
317         ath6kl_read_fwlogs(ar);
318
319         spin_lock_bh(&ar->debug.fwlog_lock);
320
321         while (len < buf_len && !ath6kl_debug_fwlog_empty(ar)) {
322                 ccnt = CIRC_CNT_TO_END(fwlog->head, fwlog->tail,
323                                        ATH6KL_FWLOG_SIZE);
324
325                 if ((size_t) ccnt > buf_len - len)
326                         ccnt = buf_len - len;
327
328                 memcpy(buf + len, &fwlog->buf[fwlog->tail], ccnt);
329                 len += ccnt;
330
331                 fwlog->tail = (fwlog->tail + ccnt) &
332                         (ATH6KL_FWLOG_SIZE - 1);
333         }
334
335         spin_unlock_bh(&ar->debug.fwlog_lock);
336
337         if (WARN_ON(len > buf_len))
338                 len = buf_len;
339
340         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
341
342         vfree(buf);
343
344         return ret_cnt;
345 }
346
347 static const struct file_operations fops_fwlog = {
348         .open = ath6kl_debugfs_open,
349         .read = ath6kl_fwlog_read,
350         .owner = THIS_MODULE,
351         .llseek = default_llseek,
352 };
353
354 static ssize_t ath6kl_fwlog_mask_read(struct file *file, char __user *user_buf,
355                                       size_t count, loff_t *ppos)
356 {
357         struct ath6kl *ar = file->private_data;
358         char buf[16];
359         int len;
360
361         len = snprintf(buf, sizeof(buf), "0x%x\n", ar->debug.fwlog_mask);
362
363         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
364 }
365
366 static ssize_t ath6kl_fwlog_mask_write(struct file *file,
367                                        const char __user *user_buf,
368                                        size_t count, loff_t *ppos)
369 {
370         struct ath6kl *ar = file->private_data;
371         int ret;
372
373         ret = kstrtou32_from_user(user_buf, count, 0, &ar->debug.fwlog_mask);
374         if (ret)
375                 return ret;
376
377         ret = ath6kl_wmi_config_debug_module_cmd(ar->wmi,
378                                                  ATH6KL_FWLOG_VALID_MASK,
379                                                  ar->debug.fwlog_mask);
380         if (ret)
381                 return ret;
382
383         return count;
384 }
385
386 static const struct file_operations fops_fwlog_mask = {
387         .open = ath6kl_debugfs_open,
388         .read = ath6kl_fwlog_mask_read,
389         .write = ath6kl_fwlog_mask_write,
390         .owner = THIS_MODULE,
391         .llseek = default_llseek,
392 };
393
394 static ssize_t read_file_tgt_stats(struct file *file, char __user *user_buf,
395                                    size_t count, loff_t *ppos)
396 {
397         struct ath6kl *ar = file->private_data;
398         struct target_stats *tgt_stats = &ar->target_stats;
399         char *buf;
400         unsigned int len = 0, buf_len = 1500;
401         int i;
402         long left;
403         ssize_t ret_cnt;
404
405         buf = kzalloc(buf_len, GFP_KERNEL);
406         if (!buf)
407                 return -ENOMEM;
408
409         if (down_interruptible(&ar->sem)) {
410                 kfree(buf);
411                 return -EBUSY;
412         }
413
414         set_bit(STATS_UPDATE_PEND, &ar->flag);
415
416         if (ath6kl_wmi_get_stats_cmd(ar->wmi)) {
417                 up(&ar->sem);
418                 kfree(buf);
419                 return -EIO;
420         }
421
422         left = wait_event_interruptible_timeout(ar->event_wq,
423                                                 !test_bit(STATS_UPDATE_PEND,
424                                                 &ar->flag), WMI_TIMEOUT);
425
426         up(&ar->sem);
427
428         if (left <= 0) {
429                 kfree(buf);
430                 return -ETIMEDOUT;
431         }
432
433         len += scnprintf(buf + len, buf_len - len, "\n");
434         len += scnprintf(buf + len, buf_len - len, "%25s\n",
435                          "Target Tx stats");
436         len += scnprintf(buf + len, buf_len - len, "%25s\n\n",
437                          "=================");
438         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
439                          "Ucast packets", tgt_stats->tx_ucast_pkt);
440         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
441                          "Bcast packets", tgt_stats->tx_bcast_pkt);
442         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
443                          "Ucast byte", tgt_stats->tx_ucast_byte);
444         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
445                          "Bcast byte", tgt_stats->tx_bcast_byte);
446         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
447                          "Rts success cnt", tgt_stats->tx_rts_success_cnt);
448         for (i = 0; i < 4; i++)
449                 len += scnprintf(buf + len, buf_len - len,
450                                  "%18s %d %10llu\n", "PER on ac",
451                                  i, tgt_stats->tx_pkt_per_ac[i]);
452         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
453                          "Error", tgt_stats->tx_err);
454         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
455                          "Fail count", tgt_stats->tx_fail_cnt);
456         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
457                          "Retry count", tgt_stats->tx_retry_cnt);
458         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
459                          "Multi retry cnt", tgt_stats->tx_mult_retry_cnt);
460         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
461                          "Rts fail cnt", tgt_stats->tx_rts_fail_cnt);
462         len += scnprintf(buf + len, buf_len - len, "%25s %10llu\n\n",
463                          "TKIP counter measure used",
464                          tgt_stats->tkip_cnter_measures_invoked);
465
466         len += scnprintf(buf + len, buf_len - len, "%25s\n",
467                          "Target Rx stats");
468         len += scnprintf(buf + len, buf_len - len, "%25s\n",
469                          "=================");
470
471         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
472                          "Ucast packets", tgt_stats->rx_ucast_pkt);
473         len += scnprintf(buf + len, buf_len - len, "%20s %10d\n",
474                          "Ucast Rate", tgt_stats->rx_ucast_rate);
475         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
476                          "Bcast packets", tgt_stats->rx_bcast_pkt);
477         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
478                          "Ucast byte", tgt_stats->rx_ucast_byte);
479         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
480                          "Bcast byte", tgt_stats->rx_bcast_byte);
481         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
482                          "Fragmented pkt", tgt_stats->rx_frgment_pkt);
483         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
484                          "Error", tgt_stats->rx_err);
485         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
486                          "CRC Err", tgt_stats->rx_crc_err);
487         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
488                          "Key chache miss", tgt_stats->rx_key_cache_miss);
489         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
490                          "Decrypt Err", tgt_stats->rx_decrypt_err);
491         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
492                          "Duplicate frame", tgt_stats->rx_dupl_frame);
493         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
494                          "Tkip Mic failure", tgt_stats->tkip_local_mic_fail);
495         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
496                          "TKIP format err", tgt_stats->tkip_fmt_err);
497         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
498                          "CCMP format Err", tgt_stats->ccmp_fmt_err);
499         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n\n",
500                          "CCMP Replay Err", tgt_stats->ccmp_replays);
501
502         len += scnprintf(buf + len, buf_len - len, "%25s\n",
503                          "Misc Target stats");
504         len += scnprintf(buf + len, buf_len - len, "%25s\n",
505                          "=================");
506         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
507                          "Beacon Miss count", tgt_stats->cs_bmiss_cnt);
508         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
509                          "Num Connects", tgt_stats->cs_connect_cnt);
510         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
511                          "Num disconnects", tgt_stats->cs_discon_cnt);
512         len += scnprintf(buf + len, buf_len - len, "%20s %10d\n",
513                          "Beacon avg rssi", tgt_stats->cs_ave_beacon_rssi);
514
515         if (len > buf_len)
516                 len = buf_len;
517
518         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
519
520         kfree(buf);
521         return ret_cnt;
522 }
523
524 static const struct file_operations fops_tgt_stats = {
525         .read = read_file_tgt_stats,
526         .open = ath6kl_debugfs_open,
527         .owner = THIS_MODULE,
528         .llseek = default_llseek,
529 };
530
531 #define print_credit_info(fmt_str, ep_list_field)               \
532         (len += scnprintf(buf + len, buf_len - len, fmt_str,    \
533                          ep_list->ep_list_field))
534 #define CREDIT_INFO_DISPLAY_STRING_LEN  200
535 #define CREDIT_INFO_LEN 128
536
537 static ssize_t read_file_credit_dist_stats(struct file *file,
538                                            char __user *user_buf,
539                                            size_t count, loff_t *ppos)
540 {
541         struct ath6kl *ar = file->private_data;
542         struct htc_target *target = ar->htc_target;
543         struct htc_endpoint_credit_dist *ep_list;
544         char *buf;
545         unsigned int buf_len, len = 0;
546         ssize_t ret_cnt;
547
548         buf_len = CREDIT_INFO_DISPLAY_STRING_LEN +
549                   get_queue_depth(&target->cred_dist_list) * CREDIT_INFO_LEN;
550         buf = kzalloc(buf_len, GFP_KERNEL);
551         if (!buf)
552                 return -ENOMEM;
553
554         len += scnprintf(buf + len, buf_len - len, "%25s%5d\n",
555                          "Total Avail Credits: ",
556                          target->cred_dist_cntxt->total_avail_credits);
557         len += scnprintf(buf + len, buf_len - len, "%25s%5d\n",
558                          "Free credits :",
559                          target->cred_dist_cntxt->cur_free_credits);
560
561         len += scnprintf(buf + len, buf_len - len,
562                          " Epid  Flags    Cred_norm  Cred_min  Credits  Cred_assngd"
563                          "  Seek_cred  Cred_sz  Cred_per_msg  Cred_to_dist"
564                          "  qdepth\n");
565
566         list_for_each_entry(ep_list, &target->cred_dist_list, list) {
567                 print_credit_info("  %2d", endpoint);
568                 print_credit_info("%10x", dist_flags);
569                 print_credit_info("%8d", cred_norm);
570                 print_credit_info("%9d", cred_min);
571                 print_credit_info("%9d", credits);
572                 print_credit_info("%10d", cred_assngd);
573                 print_credit_info("%13d", seek_cred);
574                 print_credit_info("%12d", cred_sz);
575                 print_credit_info("%9d", cred_per_msg);
576                 print_credit_info("%14d", cred_to_dist);
577                 len += scnprintf(buf + len, buf_len - len, "%12d\n",
578                                  get_queue_depth(&((struct htc_endpoint *)
579                                                  ep_list->htc_rsvd)->txq));
580         }
581
582         if (len > buf_len)
583                 len = buf_len;
584
585         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
586         kfree(buf);
587         return ret_cnt;
588 }
589
590 static const struct file_operations fops_credit_dist_stats = {
591         .read = read_file_credit_dist_stats,
592         .open = ath6kl_debugfs_open,
593         .owner = THIS_MODULE,
594         .llseek = default_llseek,
595 };
596
597 static unsigned long ath6kl_get_num_reg(void)
598 {
599         int i;
600         unsigned long n_reg = 0;
601
602         for (i = 0; i < ARRAY_SIZE(diag_reg); i++)
603                 n_reg = n_reg +
604                      (diag_reg[i].reg_end - diag_reg[i].reg_start) / 4 + 1;
605
606         return n_reg;
607 }
608
609 static bool ath6kl_dbg_is_diag_reg_valid(u32 reg_addr)
610 {
611         int i;
612
613         for (i = 0; i < ARRAY_SIZE(diag_reg); i++) {
614                 if (reg_addr >= diag_reg[i].reg_start &&
615                     reg_addr <= diag_reg[i].reg_end)
616                         return true;
617         }
618
619         return false;
620 }
621
622 static ssize_t ath6kl_regread_read(struct file *file, char __user *user_buf,
623                                     size_t count, loff_t *ppos)
624 {
625         struct ath6kl *ar = file->private_data;
626         u8 buf[50];
627         unsigned int len = 0;
628
629         if (ar->debug.dbgfs_diag_reg)
630                 len += scnprintf(buf + len, sizeof(buf) - len, "0x%x\n",
631                                 ar->debug.dbgfs_diag_reg);
632         else
633                 len += scnprintf(buf + len, sizeof(buf) - len,
634                                  "All diag registers\n");
635
636         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
637 }
638
639 static ssize_t ath6kl_regread_write(struct file *file,
640                                     const char __user *user_buf,
641                                     size_t count, loff_t *ppos)
642 {
643         struct ath6kl *ar = file->private_data;
644         u8 buf[50];
645         unsigned int len;
646         unsigned long reg_addr;
647
648         len = min(count, sizeof(buf) - 1);
649         if (copy_from_user(buf, user_buf, len))
650                 return -EFAULT;
651
652         buf[len] = '\0';
653
654         if (strict_strtoul(buf, 0, &reg_addr))
655                 return -EINVAL;
656
657         if ((reg_addr % 4) != 0)
658                 return -EINVAL;
659
660         if (reg_addr && !ath6kl_dbg_is_diag_reg_valid(reg_addr))
661                 return -EINVAL;
662
663         ar->debug.dbgfs_diag_reg = reg_addr;
664
665         return count;
666 }
667
668 static const struct file_operations fops_diag_reg_read = {
669         .read = ath6kl_regread_read,
670         .write = ath6kl_regread_write,
671         .open = ath6kl_debugfs_open,
672         .owner = THIS_MODULE,
673         .llseek = default_llseek,
674 };
675
676 static int ath6kl_regdump_open(struct inode *inode, struct file *file)
677 {
678         struct ath6kl *ar = inode->i_private;
679         u8 *buf;
680         unsigned long int reg_len;
681         unsigned int len = 0, n_reg;
682         u32 addr;
683         __le32 reg_val;
684         int i, status;
685
686         /* Dump all the registers if no register is specified */
687         if (!ar->debug.dbgfs_diag_reg)
688                 n_reg = ath6kl_get_num_reg();
689         else
690                 n_reg = 1;
691
692         reg_len = n_reg * REG_OUTPUT_LEN_PER_LINE;
693         if (n_reg > 1)
694                 reg_len += REGTYPE_STR_LEN;
695
696         buf = vmalloc(reg_len);
697         if (!buf)
698                 return -ENOMEM;
699
700         if (n_reg == 1) {
701                 addr = ar->debug.dbgfs_diag_reg;
702
703                 status = ath6kl_diag_read32(ar,
704                                 TARG_VTOP(ar->target_type, addr),
705                                 (u32 *)&reg_val);
706                 if (status)
707                         goto fail_reg_read;
708
709                 len += scnprintf(buf + len, reg_len - len,
710                                  "0x%06x 0x%08x\n", addr, le32_to_cpu(reg_val));
711                 goto done;
712         }
713
714         for (i = 0; i < ARRAY_SIZE(diag_reg); i++) {
715                 len += scnprintf(buf + len, reg_len - len,
716                                 "%s\n", diag_reg[i].reg_info);
717                 for (addr = diag_reg[i].reg_start;
718                      addr <= diag_reg[i].reg_end; addr += 4) {
719                         status = ath6kl_diag_read32(ar,
720                                         TARG_VTOP(ar->target_type, addr),
721                                         (u32 *)&reg_val);
722                         if (status)
723                                 goto fail_reg_read;
724
725                         len += scnprintf(buf + len, reg_len - len,
726                                         "0x%06x 0x%08x\n",
727                                         addr, le32_to_cpu(reg_val));
728                 }
729         }
730
731 done:
732         file->private_data = buf;
733         return 0;
734
735 fail_reg_read:
736         ath6kl_warn("Unable to read memory:%u\n", addr);
737         vfree(buf);
738         return -EIO;
739 }
740
741 static ssize_t ath6kl_regdump_read(struct file *file, char __user *user_buf,
742                                   size_t count, loff_t *ppos)
743 {
744         u8 *buf = file->private_data;
745         return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
746 }
747
748 static int ath6kl_regdump_release(struct inode *inode, struct file *file)
749 {
750         vfree(file->private_data);
751         return 0;
752 }
753
754 static const struct file_operations fops_reg_dump = {
755         .open = ath6kl_regdump_open,
756         .read = ath6kl_regdump_read,
757         .release = ath6kl_regdump_release,
758         .owner = THIS_MODULE,
759         .llseek = default_llseek,
760 };
761
762 static ssize_t ath6kl_lrssi_roam_write(struct file *file,
763                                        const char __user *user_buf,
764                                        size_t count, loff_t *ppos)
765 {
766         struct ath6kl *ar = file->private_data;
767         unsigned long lrssi_roam_threshold;
768         char buf[32];
769         ssize_t len;
770
771         len = min(count, sizeof(buf) - 1);
772         if (copy_from_user(buf, user_buf, len))
773                 return -EFAULT;
774
775         buf[len] = '\0';
776         if (strict_strtoul(buf, 0, &lrssi_roam_threshold))
777                 return -EINVAL;
778
779         ar->lrssi_roam_threshold = lrssi_roam_threshold;
780
781         ath6kl_wmi_set_roam_lrssi_cmd(ar->wmi, ar->lrssi_roam_threshold);
782
783         return count;
784 }
785
786 static ssize_t ath6kl_lrssi_roam_read(struct file *file,
787                                       char __user *user_buf,
788                                       size_t count, loff_t *ppos)
789 {
790         struct ath6kl *ar = file->private_data;
791         char buf[32];
792         unsigned int len;
793
794         len = snprintf(buf, sizeof(buf), "%u\n", ar->lrssi_roam_threshold);
795
796         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
797 }
798
799 static const struct file_operations fops_lrssi_roam_threshold = {
800         .read = ath6kl_lrssi_roam_read,
801         .write = ath6kl_lrssi_roam_write,
802         .open = ath6kl_debugfs_open,
803         .owner = THIS_MODULE,
804         .llseek = default_llseek,
805 };
806
807 static ssize_t ath6kl_regwrite_read(struct file *file,
808                                     char __user *user_buf,
809                                     size_t count, loff_t *ppos)
810 {
811         struct ath6kl *ar = file->private_data;
812         u8 buf[32];
813         unsigned int len = 0;
814
815         len = scnprintf(buf, sizeof(buf), "Addr: 0x%x Val: 0x%x\n",
816                         ar->debug.diag_reg_addr_wr, ar->debug.diag_reg_val_wr);
817
818         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
819 }
820
821 static ssize_t ath6kl_regwrite_write(struct file *file,
822                                      const char __user *user_buf,
823                                      size_t count, loff_t *ppos)
824 {
825         struct ath6kl *ar = file->private_data;
826         char buf[32];
827         char *sptr, *token;
828         unsigned int len = 0;
829         u32 reg_addr, reg_val;
830
831         len = min(count, sizeof(buf) - 1);
832         if (copy_from_user(buf, user_buf, len))
833                 return -EFAULT;
834
835         buf[len] = '\0';
836         sptr = buf;
837
838         token = strsep(&sptr, "=");
839         if (!token)
840                 return -EINVAL;
841
842         if (kstrtou32(token, 0, &reg_addr))
843                 return -EINVAL;
844
845         if (!ath6kl_dbg_is_diag_reg_valid(reg_addr))
846                 return -EINVAL;
847
848         if (kstrtou32(sptr, 0, &reg_val))
849                 return -EINVAL;
850
851         ar->debug.diag_reg_addr_wr = reg_addr;
852         ar->debug.diag_reg_val_wr = reg_val;
853
854         if (ath6kl_diag_write32(ar, ar->debug.diag_reg_addr_wr,
855                                 cpu_to_le32(ar->debug.diag_reg_val_wr)))
856                 return -EIO;
857
858         return count;
859 }
860
861 static const struct file_operations fops_diag_reg_write = {
862         .read = ath6kl_regwrite_read,
863         .write = ath6kl_regwrite_write,
864         .open = ath6kl_debugfs_open,
865         .owner = THIS_MODULE,
866         .llseek = default_llseek,
867 };
868
869 int ath6kl_debug_init(struct ath6kl *ar)
870 {
871         ar->debug.fwlog_buf.buf = vmalloc(ATH6KL_FWLOG_SIZE);
872         if (ar->debug.fwlog_buf.buf == NULL)
873                 return -ENOMEM;
874
875         ar->debug.fwlog_tmp = kmalloc(ATH6KL_FWLOG_SLOT_SIZE, GFP_KERNEL);
876         if (ar->debug.fwlog_tmp == NULL) {
877                 vfree(ar->debug.fwlog_buf.buf);
878                 return -ENOMEM;
879         }
880
881         spin_lock_init(&ar->debug.fwlog_lock);
882
883         /*
884          * Actually we are lying here but don't know how to read the mask
885          * value from the firmware.
886          */
887         ar->debug.fwlog_mask = 0;
888
889         ar->debugfs_phy = debugfs_create_dir("ath6kl",
890                                              ar->wdev->wiphy->debugfsdir);
891         if (!ar->debugfs_phy) {
892                 vfree(ar->debug.fwlog_buf.buf);
893                 kfree(ar->debug.fwlog_tmp);
894                 return -ENOMEM;
895         }
896
897         debugfs_create_file("tgt_stats", S_IRUSR, ar->debugfs_phy, ar,
898                             &fops_tgt_stats);
899
900         debugfs_create_file("credit_dist_stats", S_IRUSR, ar->debugfs_phy, ar,
901                             &fops_credit_dist_stats);
902
903         debugfs_create_file("fwlog", S_IRUSR, ar->debugfs_phy, ar,
904                             &fops_fwlog);
905
906         debugfs_create_file("fwlog_mask", S_IRUSR | S_IWUSR, ar->debugfs_phy,
907                             ar, &fops_fwlog_mask);
908
909         debugfs_create_file("reg_addr", S_IRUSR | S_IWUSR, ar->debugfs_phy, ar,
910                             &fops_diag_reg_read);
911
912         debugfs_create_file("reg_dump", S_IRUSR, ar->debugfs_phy, ar,
913                             &fops_reg_dump);
914
915         debugfs_create_file("lrssi_roam_threshold", S_IRUSR | S_IWUSR,
916                             ar->debugfs_phy, ar, &fops_lrssi_roam_threshold);
917
918         debugfs_create_file("reg_write", S_IRUSR | S_IWUSR,
919                             ar->debugfs_phy, ar, &fops_diag_reg_write);
920
921         debugfs_create_file("war_stats", S_IRUSR, ar->debugfs_phy, ar,
922                             &fops_war_stats);
923
924         return 0;
925 }
926
927 void ath6kl_debug_cleanup(struct ath6kl *ar)
928 {
929         vfree(ar->debug.fwlog_buf.buf);
930         kfree(ar->debug.fwlog_tmp);
931 }
932
933 #endif