Merge remote-tracking branch 'asoc/fix/rcar' into asoc-rcar
[cascardo/linux.git] / drivers / net / wireless / ath / ath6kl / debug.c
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include "core.h"
19
20 #include <linux/skbuff.h>
21 #include <linux/fs.h>
22 #include <linux/vmalloc.h>
23 #include <linux/export.h>
24
25 #include "debug.h"
26 #include "target.h"
27
28 struct ath6kl_fwlog_slot {
29         __le32 timestamp;
30         __le32 length;
31
32         /* max ATH6KL_FWLOG_PAYLOAD_SIZE bytes */
33         u8 payload[0];
34 };
35
36 #define ATH6KL_FWLOG_MAX_ENTRIES 20
37
38 #define ATH6KL_FWLOG_VALID_MASK 0x1ffff
39
40 int ath6kl_printk(const char *level, const char *fmt, ...)
41 {
42         struct va_format vaf;
43         va_list args;
44         int rtn;
45
46         va_start(args, fmt);
47
48         vaf.fmt = fmt;
49         vaf.va = &args;
50
51         rtn = printk("%sath6kl: %pV", level, &vaf);
52
53         va_end(args);
54
55         return rtn;
56 }
57 EXPORT_SYMBOL(ath6kl_printk);
58
59 int ath6kl_info(const char *fmt, ...)
60 {
61         struct va_format vaf = {
62                 .fmt = fmt,
63         };
64         va_list args;
65         int ret;
66
67         va_start(args, fmt);
68         vaf.va = &args;
69         ret = ath6kl_printk(KERN_INFO, "%pV", &vaf);
70         trace_ath6kl_log_info(&vaf);
71         va_end(args);
72
73         return ret;
74 }
75 EXPORT_SYMBOL(ath6kl_info);
76
77 int ath6kl_err(const char *fmt, ...)
78 {
79         struct va_format vaf = {
80                 .fmt = fmt,
81         };
82         va_list args;
83         int ret;
84
85         va_start(args, fmt);
86         vaf.va = &args;
87         ret = ath6kl_printk(KERN_ERR, "%pV", &vaf);
88         trace_ath6kl_log_err(&vaf);
89         va_end(args);
90
91         return ret;
92 }
93 EXPORT_SYMBOL(ath6kl_err);
94
95 int ath6kl_warn(const char *fmt, ...)
96 {
97         struct va_format vaf = {
98                 .fmt = fmt,
99         };
100         va_list args;
101         int ret;
102
103         va_start(args, fmt);
104         vaf.va = &args;
105         ret = ath6kl_printk(KERN_WARNING, "%pV", &vaf);
106         trace_ath6kl_log_warn(&vaf);
107         va_end(args);
108
109         return ret;
110 }
111 EXPORT_SYMBOL(ath6kl_warn);
112
113 #ifdef CONFIG_ATH6KL_DEBUG
114
115 void ath6kl_dbg(enum ATH6K_DEBUG_MASK mask, const char *fmt, ...)
116 {
117         struct va_format vaf;
118         va_list args;
119
120         va_start(args, fmt);
121
122         vaf.fmt = fmt;
123         vaf.va = &args;
124
125         if (debug_mask & mask)
126                 ath6kl_printk(KERN_DEBUG, "%pV", &vaf);
127
128         trace_ath6kl_log_dbg(mask, &vaf);
129
130         va_end(args);
131 }
132 EXPORT_SYMBOL(ath6kl_dbg);
133
134 void ath6kl_dbg_dump(enum ATH6K_DEBUG_MASK mask,
135                      const char *msg, const char *prefix,
136                      const void *buf, size_t len)
137 {
138         if (debug_mask & mask) {
139                 if (msg)
140                         ath6kl_dbg(mask, "%s\n", msg);
141
142                 print_hex_dump_bytes(prefix, DUMP_PREFIX_OFFSET, buf, len);
143         }
144
145         /* tracing code doesn't like null strings :/ */
146         trace_ath6kl_log_dbg_dump(msg ? msg : "", prefix ? prefix : "",
147                                   buf, len);
148 }
149 EXPORT_SYMBOL(ath6kl_dbg_dump);
150
151 #define REG_OUTPUT_LEN_PER_LINE 25
152 #define REGTYPE_STR_LEN         100
153
154 struct ath6kl_diag_reg_info {
155         u32 reg_start;
156         u32 reg_end;
157         const char *reg_info;
158 };
159
160 static const struct ath6kl_diag_reg_info diag_reg[] = {
161         { 0x20000, 0x200fc, "General DMA and Rx registers" },
162         { 0x28000, 0x28900, "MAC PCU register & keycache" },
163         { 0x20800, 0x20a40, "QCU" },
164         { 0x21000, 0x212f0, "DCU" },
165         { 0x4000,  0x42e4, "RTC" },
166         { 0x540000, 0x540000 + (256 * 1024), "RAM" },
167         { 0x29800, 0x2B210, "Base Band" },
168         { 0x1C000, 0x1C748, "Analog" },
169 };
170
171 void ath6kl_dump_registers(struct ath6kl_device *dev,
172                            struct ath6kl_irq_proc_registers *irq_proc_reg,
173                            struct ath6kl_irq_enable_reg *irq_enable_reg)
174 {
175         ath6kl_dbg(ATH6KL_DBG_IRQ, ("<------- Register Table -------->\n"));
176
177         if (irq_proc_reg != NULL) {
178                 ath6kl_dbg(ATH6KL_DBG_IRQ,
179                            "Host Int status:           0x%x\n",
180                            irq_proc_reg->host_int_status);
181                 ath6kl_dbg(ATH6KL_DBG_IRQ,
182                            "CPU Int status:            0x%x\n",
183                            irq_proc_reg->cpu_int_status);
184                 ath6kl_dbg(ATH6KL_DBG_IRQ,
185                            "Error Int status:          0x%x\n",
186                            irq_proc_reg->error_int_status);
187                 ath6kl_dbg(ATH6KL_DBG_IRQ,
188                            "Counter Int status:        0x%x\n",
189                            irq_proc_reg->counter_int_status);
190                 ath6kl_dbg(ATH6KL_DBG_IRQ,
191                            "Mbox Frame:                0x%x\n",
192                            irq_proc_reg->mbox_frame);
193                 ath6kl_dbg(ATH6KL_DBG_IRQ,
194                            "Rx Lookahead Valid:        0x%x\n",
195                            irq_proc_reg->rx_lkahd_valid);
196                 ath6kl_dbg(ATH6KL_DBG_IRQ,
197                            "Rx Lookahead 0:            0x%x\n",
198                            irq_proc_reg->rx_lkahd[0]);
199                 ath6kl_dbg(ATH6KL_DBG_IRQ,
200                            "Rx Lookahead 1:            0x%x\n",
201                            irq_proc_reg->rx_lkahd[1]);
202
203                 if (dev->ar->mbox_info.gmbox_addr != 0) {
204                         /*
205                          * If the target supports GMBOX hardware, dump some
206                          * additional state.
207                          */
208                         ath6kl_dbg(ATH6KL_DBG_IRQ,
209                                    "GMBOX Host Int status 2:   0x%x\n",
210                                    irq_proc_reg->host_int_status2);
211                         ath6kl_dbg(ATH6KL_DBG_IRQ,
212                                    "GMBOX RX Avail:            0x%x\n",
213                                    irq_proc_reg->gmbox_rx_avail);
214                         ath6kl_dbg(ATH6KL_DBG_IRQ,
215                                    "GMBOX lookahead alias 0:   0x%x\n",
216                                    irq_proc_reg->rx_gmbox_lkahd_alias[0]);
217                         ath6kl_dbg(ATH6KL_DBG_IRQ,
218                                    "GMBOX lookahead alias 1:   0x%x\n",
219                                    irq_proc_reg->rx_gmbox_lkahd_alias[1]);
220                 }
221         }
222
223         if (irq_enable_reg != NULL) {
224                 ath6kl_dbg(ATH6KL_DBG_IRQ,
225                            "Int status Enable:         0x%x\n",
226                            irq_enable_reg->int_status_en);
227                 ath6kl_dbg(ATH6KL_DBG_IRQ, "Counter Int status Enable: 0x%x\n",
228                            irq_enable_reg->cntr_int_status_en);
229         }
230         ath6kl_dbg(ATH6KL_DBG_IRQ, "<------------------------------->\n");
231 }
232
233 static void dump_cred_dist(struct htc_endpoint_credit_dist *ep_dist)
234 {
235         ath6kl_dbg(ATH6KL_DBG_CREDIT,
236                    "--- endpoint: %d  svc_id: 0x%X ---\n",
237                    ep_dist->endpoint, ep_dist->svc_id);
238         ath6kl_dbg(ATH6KL_DBG_CREDIT, " dist_flags     : 0x%X\n",
239                    ep_dist->dist_flags);
240         ath6kl_dbg(ATH6KL_DBG_CREDIT, " cred_norm      : %d\n",
241                    ep_dist->cred_norm);
242         ath6kl_dbg(ATH6KL_DBG_CREDIT, " cred_min       : %d\n",
243                    ep_dist->cred_min);
244         ath6kl_dbg(ATH6KL_DBG_CREDIT, " credits        : %d\n",
245                    ep_dist->credits);
246         ath6kl_dbg(ATH6KL_DBG_CREDIT, " cred_assngd    : %d\n",
247                    ep_dist->cred_assngd);
248         ath6kl_dbg(ATH6KL_DBG_CREDIT, " seek_cred      : %d\n",
249                    ep_dist->seek_cred);
250         ath6kl_dbg(ATH6KL_DBG_CREDIT, " cred_sz        : %d\n",
251                    ep_dist->cred_sz);
252         ath6kl_dbg(ATH6KL_DBG_CREDIT, " cred_per_msg   : %d\n",
253                    ep_dist->cred_per_msg);
254         ath6kl_dbg(ATH6KL_DBG_CREDIT, " cred_to_dist   : %d\n",
255                    ep_dist->cred_to_dist);
256         ath6kl_dbg(ATH6KL_DBG_CREDIT, " txq_depth      : %d\n",
257                    get_queue_depth(&ep_dist->htc_ep->txq));
258         ath6kl_dbg(ATH6KL_DBG_CREDIT,
259                    "----------------------------------\n");
260 }
261
262 /* FIXME: move to htc.c */
263 void dump_cred_dist_stats(struct htc_target *target)
264 {
265         struct htc_endpoint_credit_dist *ep_list;
266
267         list_for_each_entry(ep_list, &target->cred_dist_list, list)
268                 dump_cred_dist(ep_list);
269
270         ath6kl_dbg(ATH6KL_DBG_CREDIT,
271                    "credit distribution total %d free %d\n",
272                    target->credit_info->total_avail_credits,
273                    target->credit_info->cur_free_credits);
274 }
275
276 void ath6kl_debug_war(struct ath6kl *ar, enum ath6kl_war war)
277 {
278         switch (war) {
279         case ATH6KL_WAR_INVALID_RATE:
280                 ar->debug.war_stats.invalid_rate++;
281                 break;
282         }
283 }
284
285 static ssize_t read_file_war_stats(struct file *file, char __user *user_buf,
286                                    size_t count, loff_t *ppos)
287 {
288         struct ath6kl *ar = file->private_data;
289         char *buf;
290         unsigned int len = 0, buf_len = 1500;
291         ssize_t ret_cnt;
292
293         buf = kzalloc(buf_len, GFP_KERNEL);
294         if (!buf)
295                 return -ENOMEM;
296
297         len += scnprintf(buf + len, buf_len - len, "\n");
298         len += scnprintf(buf + len, buf_len - len, "%25s\n",
299                          "Workaround stats");
300         len += scnprintf(buf + len, buf_len - len, "%25s\n\n",
301                          "=================");
302         len += scnprintf(buf + len, buf_len - len, "%20s %10u\n",
303                          "Invalid rates", ar->debug.war_stats.invalid_rate);
304
305         if (WARN_ON(len > buf_len))
306                 len = buf_len;
307
308         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
309
310         kfree(buf);
311         return ret_cnt;
312 }
313
314 static const struct file_operations fops_war_stats = {
315         .read = read_file_war_stats,
316         .open = simple_open,
317         .owner = THIS_MODULE,
318         .llseek = default_llseek,
319 };
320
321 void ath6kl_debug_fwlog_event(struct ath6kl *ar, const void *buf, size_t len)
322 {
323         struct ath6kl_fwlog_slot *slot;
324         struct sk_buff *skb;
325         size_t slot_len;
326
327         if (WARN_ON(len > ATH6KL_FWLOG_PAYLOAD_SIZE))
328                 return;
329
330         slot_len = sizeof(*slot) + ATH6KL_FWLOG_PAYLOAD_SIZE;
331
332         skb = alloc_skb(slot_len, GFP_KERNEL);
333         if (!skb)
334                 return;
335
336         slot = (struct ath6kl_fwlog_slot *) skb_put(skb, slot_len);
337         slot->timestamp = cpu_to_le32(jiffies);
338         slot->length = cpu_to_le32(len);
339         memcpy(slot->payload, buf, len);
340
341         /* Need to pad each record to fixed length ATH6KL_FWLOG_PAYLOAD_SIZE */
342         memset(slot->payload + len, 0, ATH6KL_FWLOG_PAYLOAD_SIZE - len);
343
344         spin_lock(&ar->debug.fwlog_queue.lock);
345
346         __skb_queue_tail(&ar->debug.fwlog_queue, skb);
347         complete(&ar->debug.fwlog_completion);
348
349         /* drop oldest entries */
350         while (skb_queue_len(&ar->debug.fwlog_queue) >
351                ATH6KL_FWLOG_MAX_ENTRIES) {
352                 skb = __skb_dequeue(&ar->debug.fwlog_queue);
353                 kfree_skb(skb);
354         }
355
356         spin_unlock(&ar->debug.fwlog_queue.lock);
357
358         return;
359 }
360
361 static int ath6kl_fwlog_open(struct inode *inode, struct file *file)
362 {
363         struct ath6kl *ar = inode->i_private;
364
365         if (ar->debug.fwlog_open)
366                 return -EBUSY;
367
368         ar->debug.fwlog_open = true;
369
370         file->private_data = inode->i_private;
371         return 0;
372 }
373
374 static int ath6kl_fwlog_release(struct inode *inode, struct file *file)
375 {
376         struct ath6kl *ar = inode->i_private;
377
378         ar->debug.fwlog_open = false;
379
380         return 0;
381 }
382
383 static ssize_t ath6kl_fwlog_read(struct file *file, char __user *user_buf,
384                                  size_t count, loff_t *ppos)
385 {
386         struct ath6kl *ar = file->private_data;
387         struct sk_buff *skb;
388         ssize_t ret_cnt;
389         size_t len = 0;
390         char *buf;
391
392         buf = vmalloc(count);
393         if (!buf)
394                 return -ENOMEM;
395
396         /* read undelivered logs from firmware */
397         ath6kl_read_fwlogs(ar);
398
399         spin_lock(&ar->debug.fwlog_queue.lock);
400
401         while ((skb = __skb_dequeue(&ar->debug.fwlog_queue))) {
402                 if (skb->len > count - len) {
403                         /* not enough space, put skb back and leave */
404                         __skb_queue_head(&ar->debug.fwlog_queue, skb);
405                         break;
406                 }
407
408
409                 memcpy(buf + len, skb->data, skb->len);
410                 len += skb->len;
411
412                 kfree_skb(skb);
413         }
414
415         spin_unlock(&ar->debug.fwlog_queue.lock);
416
417         /* FIXME: what to do if len == 0? */
418
419         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
420
421         vfree(buf);
422
423         return ret_cnt;
424 }
425
426 static const struct file_operations fops_fwlog = {
427         .open = ath6kl_fwlog_open,
428         .release = ath6kl_fwlog_release,
429         .read = ath6kl_fwlog_read,
430         .owner = THIS_MODULE,
431         .llseek = default_llseek,
432 };
433
434 static ssize_t ath6kl_fwlog_block_read(struct file *file,
435                                        char __user *user_buf,
436                                        size_t count,
437                                        loff_t *ppos)
438 {
439         struct ath6kl *ar = file->private_data;
440         struct sk_buff *skb;
441         ssize_t ret_cnt;
442         size_t len = 0, not_copied;
443         char *buf;
444         int ret;
445
446         buf = vmalloc(count);
447         if (!buf)
448                 return -ENOMEM;
449
450         spin_lock(&ar->debug.fwlog_queue.lock);
451
452         if (skb_queue_len(&ar->debug.fwlog_queue) == 0) {
453                 /* we must init under queue lock */
454                 init_completion(&ar->debug.fwlog_completion);
455
456                 spin_unlock(&ar->debug.fwlog_queue.lock);
457
458                 ret = wait_for_completion_interruptible(
459                         &ar->debug.fwlog_completion);
460                 if (ret == -ERESTARTSYS) {
461                         vfree(buf);
462                         return ret;
463                 }
464
465                 spin_lock(&ar->debug.fwlog_queue.lock);
466         }
467
468         while ((skb = __skb_dequeue(&ar->debug.fwlog_queue))) {
469                 if (skb->len > count - len) {
470                         /* not enough space, put skb back and leave */
471                         __skb_queue_head(&ar->debug.fwlog_queue, skb);
472                         break;
473                 }
474
475
476                 memcpy(buf + len, skb->data, skb->len);
477                 len += skb->len;
478
479                 kfree_skb(skb);
480         }
481
482         spin_unlock(&ar->debug.fwlog_queue.lock);
483
484         /* FIXME: what to do if len == 0? */
485
486         not_copied = copy_to_user(user_buf, buf, len);
487         if (not_copied != 0) {
488                 ret_cnt = -EFAULT;
489                 goto out;
490         }
491
492         *ppos = *ppos + len;
493
494         ret_cnt = len;
495
496 out:
497         vfree(buf);
498
499         return ret_cnt;
500 }
501
502 static const struct file_operations fops_fwlog_block = {
503         .open = ath6kl_fwlog_open,
504         .release = ath6kl_fwlog_release,
505         .read = ath6kl_fwlog_block_read,
506         .owner = THIS_MODULE,
507         .llseek = default_llseek,
508 };
509
510 static ssize_t ath6kl_fwlog_mask_read(struct file *file, char __user *user_buf,
511                                       size_t count, loff_t *ppos)
512 {
513         struct ath6kl *ar = file->private_data;
514         char buf[16];
515         int len;
516
517         len = snprintf(buf, sizeof(buf), "0x%x\n", ar->debug.fwlog_mask);
518
519         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
520 }
521
522 static ssize_t ath6kl_fwlog_mask_write(struct file *file,
523                                        const char __user *user_buf,
524                                        size_t count, loff_t *ppos)
525 {
526         struct ath6kl *ar = file->private_data;
527         int ret;
528
529         ret = kstrtou32_from_user(user_buf, count, 0, &ar->debug.fwlog_mask);
530         if (ret)
531                 return ret;
532
533         ret = ath6kl_wmi_config_debug_module_cmd(ar->wmi,
534                                                  ATH6KL_FWLOG_VALID_MASK,
535                                                  ar->debug.fwlog_mask);
536         if (ret)
537                 return ret;
538
539         return count;
540 }
541
542 static const struct file_operations fops_fwlog_mask = {
543         .open = simple_open,
544         .read = ath6kl_fwlog_mask_read,
545         .write = ath6kl_fwlog_mask_write,
546         .owner = THIS_MODULE,
547         .llseek = default_llseek,
548 };
549
550 static ssize_t read_file_tgt_stats(struct file *file, char __user *user_buf,
551                                    size_t count, loff_t *ppos)
552 {
553         struct ath6kl *ar = file->private_data;
554         struct ath6kl_vif *vif;
555         struct target_stats *tgt_stats;
556         char *buf;
557         unsigned int len = 0, buf_len = 1500;
558         int i;
559         long left;
560         ssize_t ret_cnt;
561
562         vif = ath6kl_vif_first(ar);
563         if (!vif)
564                 return -EIO;
565
566         tgt_stats = &vif->target_stats;
567
568         buf = kzalloc(buf_len, GFP_KERNEL);
569         if (!buf)
570                 return -ENOMEM;
571
572         if (down_interruptible(&ar->sem)) {
573                 kfree(buf);
574                 return -EBUSY;
575         }
576
577         set_bit(STATS_UPDATE_PEND, &vif->flags);
578
579         if (ath6kl_wmi_get_stats_cmd(ar->wmi, 0)) {
580                 up(&ar->sem);
581                 kfree(buf);
582                 return -EIO;
583         }
584
585         left = wait_event_interruptible_timeout(ar->event_wq,
586                                                 !test_bit(STATS_UPDATE_PEND,
587                                                 &vif->flags), WMI_TIMEOUT);
588
589         up(&ar->sem);
590
591         if (left <= 0) {
592                 kfree(buf);
593                 return -ETIMEDOUT;
594         }
595
596         len += scnprintf(buf + len, buf_len - len, "\n");
597         len += scnprintf(buf + len, buf_len - len, "%25s\n",
598                          "Target Tx stats");
599         len += scnprintf(buf + len, buf_len - len, "%25s\n\n",
600                          "=================");
601         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
602                          "Ucast packets", tgt_stats->tx_ucast_pkt);
603         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
604                          "Bcast packets", tgt_stats->tx_bcast_pkt);
605         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
606                          "Ucast byte", tgt_stats->tx_ucast_byte);
607         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
608                          "Bcast byte", tgt_stats->tx_bcast_byte);
609         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
610                          "Rts success cnt", tgt_stats->tx_rts_success_cnt);
611         for (i = 0; i < 4; i++)
612                 len += scnprintf(buf + len, buf_len - len,
613                                  "%18s %d %10llu\n", "PER on ac",
614                                  i, tgt_stats->tx_pkt_per_ac[i]);
615         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
616                          "Error", tgt_stats->tx_err);
617         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
618                          "Fail count", tgt_stats->tx_fail_cnt);
619         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
620                          "Retry count", tgt_stats->tx_retry_cnt);
621         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
622                          "Multi retry cnt", tgt_stats->tx_mult_retry_cnt);
623         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
624                          "Rts fail cnt", tgt_stats->tx_rts_fail_cnt);
625         len += scnprintf(buf + len, buf_len - len, "%25s %10llu\n\n",
626                          "TKIP counter measure used",
627                          tgt_stats->tkip_cnter_measures_invoked);
628
629         len += scnprintf(buf + len, buf_len - len, "%25s\n",
630                          "Target Rx stats");
631         len += scnprintf(buf + len, buf_len - len, "%25s\n",
632                          "=================");
633
634         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
635                          "Ucast packets", tgt_stats->rx_ucast_pkt);
636         len += scnprintf(buf + len, buf_len - len, "%20s %10d\n",
637                          "Ucast Rate", tgt_stats->rx_ucast_rate);
638         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
639                          "Bcast packets", tgt_stats->rx_bcast_pkt);
640         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
641                          "Ucast byte", tgt_stats->rx_ucast_byte);
642         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
643                          "Bcast byte", tgt_stats->rx_bcast_byte);
644         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
645                          "Fragmented pkt", tgt_stats->rx_frgment_pkt);
646         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
647                          "Error", tgt_stats->rx_err);
648         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
649                          "CRC Err", tgt_stats->rx_crc_err);
650         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
651                          "Key chache miss", tgt_stats->rx_key_cache_miss);
652         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
653                          "Decrypt Err", tgt_stats->rx_decrypt_err);
654         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
655                          "Duplicate frame", tgt_stats->rx_dupl_frame);
656         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
657                          "Tkip Mic failure", tgt_stats->tkip_local_mic_fail);
658         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
659                          "TKIP format err", tgt_stats->tkip_fmt_err);
660         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
661                          "CCMP format Err", tgt_stats->ccmp_fmt_err);
662         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n\n",
663                          "CCMP Replay Err", tgt_stats->ccmp_replays);
664
665         len += scnprintf(buf + len, buf_len - len, "%25s\n",
666                          "Misc Target stats");
667         len += scnprintf(buf + len, buf_len - len, "%25s\n",
668                          "=================");
669         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
670                          "Beacon Miss count", tgt_stats->cs_bmiss_cnt);
671         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
672                          "Num Connects", tgt_stats->cs_connect_cnt);
673         len += scnprintf(buf + len, buf_len - len, "%20s %10llu\n",
674                          "Num disconnects", tgt_stats->cs_discon_cnt);
675         len += scnprintf(buf + len, buf_len - len, "%20s %10d\n",
676                          "Beacon avg rssi", tgt_stats->cs_ave_beacon_rssi);
677         len += scnprintf(buf + len, buf_len - len, "%20s %10d\n",
678                          "ARP pkt received", tgt_stats->arp_received);
679         len += scnprintf(buf + len, buf_len - len, "%20s %10d\n",
680                          "ARP pkt matched", tgt_stats->arp_matched);
681         len += scnprintf(buf + len, buf_len - len, "%20s %10d\n",
682                          "ARP pkt replied", tgt_stats->arp_replied);
683
684         if (len > buf_len)
685                 len = buf_len;
686
687         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
688
689         kfree(buf);
690         return ret_cnt;
691 }
692
693 static const struct file_operations fops_tgt_stats = {
694         .read = read_file_tgt_stats,
695         .open = simple_open,
696         .owner = THIS_MODULE,
697         .llseek = default_llseek,
698 };
699
700 #define print_credit_info(fmt_str, ep_list_field)               \
701         (len += scnprintf(buf + len, buf_len - len, fmt_str,    \
702                          ep_list->ep_list_field))
703 #define CREDIT_INFO_DISPLAY_STRING_LEN  200
704 #define CREDIT_INFO_LEN 128
705
706 static ssize_t read_file_credit_dist_stats(struct file *file,
707                                            char __user *user_buf,
708                                            size_t count, loff_t *ppos)
709 {
710         struct ath6kl *ar = file->private_data;
711         struct htc_target *target = ar->htc_target;
712         struct htc_endpoint_credit_dist *ep_list;
713         char *buf;
714         unsigned int buf_len, len = 0;
715         ssize_t ret_cnt;
716
717         buf_len = CREDIT_INFO_DISPLAY_STRING_LEN +
718                   get_queue_depth(&target->cred_dist_list) * CREDIT_INFO_LEN;
719         buf = kzalloc(buf_len, GFP_KERNEL);
720         if (!buf)
721                 return -ENOMEM;
722
723         len += scnprintf(buf + len, buf_len - len, "%25s%5d\n",
724                          "Total Avail Credits: ",
725                          target->credit_info->total_avail_credits);
726         len += scnprintf(buf + len, buf_len - len, "%25s%5d\n",
727                          "Free credits :",
728                          target->credit_info->cur_free_credits);
729
730         len += scnprintf(buf + len, buf_len - len,
731                          " Epid  Flags    Cred_norm  Cred_min  Credits  Cred_assngd"
732                          "  Seek_cred  Cred_sz  Cred_per_msg  Cred_to_dist"
733                          "  qdepth\n");
734
735         list_for_each_entry(ep_list, &target->cred_dist_list, list) {
736                 print_credit_info("  %2d", endpoint);
737                 print_credit_info("%10x", dist_flags);
738                 print_credit_info("%8d", cred_norm);
739                 print_credit_info("%9d", cred_min);
740                 print_credit_info("%9d", credits);
741                 print_credit_info("%10d", cred_assngd);
742                 print_credit_info("%13d", seek_cred);
743                 print_credit_info("%12d", cred_sz);
744                 print_credit_info("%9d", cred_per_msg);
745                 print_credit_info("%14d", cred_to_dist);
746                 len += scnprintf(buf + len, buf_len - len, "%12d\n",
747                                  get_queue_depth(&ep_list->htc_ep->txq));
748         }
749
750         if (len > buf_len)
751                 len = buf_len;
752
753         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
754         kfree(buf);
755         return ret_cnt;
756 }
757
758 static const struct file_operations fops_credit_dist_stats = {
759         .read = read_file_credit_dist_stats,
760         .open = simple_open,
761         .owner = THIS_MODULE,
762         .llseek = default_llseek,
763 };
764
765 static unsigned int print_endpoint_stat(struct htc_target *target, char *buf,
766                                         unsigned int buf_len, unsigned int len,
767                                         int offset, const char *name)
768 {
769         int i;
770         struct htc_endpoint_stats *ep_st;
771         u32 *counter;
772
773         len += scnprintf(buf + len, buf_len - len, "%s:", name);
774         for (i = 0; i < ENDPOINT_MAX; i++) {
775                 ep_st = &target->endpoint[i].ep_st;
776                 counter = ((u32 *) ep_st) + (offset / 4);
777                 len += scnprintf(buf + len, buf_len - len, " %u", *counter);
778         }
779         len += scnprintf(buf + len, buf_len - len, "\n");
780
781         return len;
782 }
783
784 static ssize_t ath6kl_endpoint_stats_read(struct file *file,
785                                           char __user *user_buf,
786                                           size_t count, loff_t *ppos)
787 {
788         struct ath6kl *ar = file->private_data;
789         struct htc_target *target = ar->htc_target;
790         char *buf;
791         unsigned int buf_len, len = 0;
792         ssize_t ret_cnt;
793
794         buf_len = sizeof(struct htc_endpoint_stats) / sizeof(u32) *
795                 (25 + ENDPOINT_MAX * 11);
796         buf = kmalloc(buf_len, GFP_KERNEL);
797         if (!buf)
798                 return -ENOMEM;
799
800 #define EPSTAT(name)                                                    \
801         do {                                                            \
802                 len = print_endpoint_stat(target, buf, buf_len, len,    \
803                                           offsetof(struct htc_endpoint_stats, \
804                                                    name),               \
805                                           #name);                       \
806         } while (0)
807
808         EPSTAT(cred_low_indicate);
809         EPSTAT(tx_issued);
810         EPSTAT(tx_pkt_bundled);
811         EPSTAT(tx_bundles);
812         EPSTAT(tx_dropped);
813         EPSTAT(tx_cred_rpt);
814         EPSTAT(cred_rpt_from_rx);
815         EPSTAT(cred_rpt_from_other);
816         EPSTAT(cred_rpt_ep0);
817         EPSTAT(cred_from_rx);
818         EPSTAT(cred_from_other);
819         EPSTAT(cred_from_ep0);
820         EPSTAT(cred_cosumd);
821         EPSTAT(cred_retnd);
822         EPSTAT(rx_pkts);
823         EPSTAT(rx_lkahds);
824         EPSTAT(rx_bundl);
825         EPSTAT(rx_bundle_lkahd);
826         EPSTAT(rx_bundle_from_hdr);
827         EPSTAT(rx_alloc_thresh_hit);
828         EPSTAT(rxalloc_thresh_byte);
829 #undef EPSTAT
830
831         if (len > buf_len)
832                 len = buf_len;
833
834         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
835         kfree(buf);
836         return ret_cnt;
837 }
838
839 static ssize_t ath6kl_endpoint_stats_write(struct file *file,
840                                            const char __user *user_buf,
841                                            size_t count, loff_t *ppos)
842 {
843         struct ath6kl *ar = file->private_data;
844         struct htc_target *target = ar->htc_target;
845         int ret, i;
846         u32 val;
847         struct htc_endpoint_stats *ep_st;
848
849         ret = kstrtou32_from_user(user_buf, count, 0, &val);
850         if (ret)
851                 return ret;
852         if (val == 0) {
853                 for (i = 0; i < ENDPOINT_MAX; i++) {
854                         ep_st = &target->endpoint[i].ep_st;
855                         memset(ep_st, 0, sizeof(*ep_st));
856                 }
857         }
858
859         return count;
860 }
861
862 static const struct file_operations fops_endpoint_stats = {
863         .open = simple_open,
864         .read = ath6kl_endpoint_stats_read,
865         .write = ath6kl_endpoint_stats_write,
866         .owner = THIS_MODULE,
867         .llseek = default_llseek,
868 };
869
870 static unsigned long ath6kl_get_num_reg(void)
871 {
872         int i;
873         unsigned long n_reg = 0;
874
875         for (i = 0; i < ARRAY_SIZE(diag_reg); i++)
876                 n_reg = n_reg +
877                      (diag_reg[i].reg_end - diag_reg[i].reg_start) / 4 + 1;
878
879         return n_reg;
880 }
881
882 static bool ath6kl_dbg_is_diag_reg_valid(u32 reg_addr)
883 {
884         int i;
885
886         for (i = 0; i < ARRAY_SIZE(diag_reg); i++) {
887                 if (reg_addr >= diag_reg[i].reg_start &&
888                     reg_addr <= diag_reg[i].reg_end)
889                         return true;
890         }
891
892         return false;
893 }
894
895 static ssize_t ath6kl_regread_read(struct file *file, char __user *user_buf,
896                                     size_t count, loff_t *ppos)
897 {
898         struct ath6kl *ar = file->private_data;
899         u8 buf[50];
900         unsigned int len = 0;
901
902         if (ar->debug.dbgfs_diag_reg)
903                 len += scnprintf(buf + len, sizeof(buf) - len, "0x%x\n",
904                                 ar->debug.dbgfs_diag_reg);
905         else
906                 len += scnprintf(buf + len, sizeof(buf) - len,
907                                  "All diag registers\n");
908
909         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
910 }
911
912 static ssize_t ath6kl_regread_write(struct file *file,
913                                     const char __user *user_buf,
914                                     size_t count, loff_t *ppos)
915 {
916         struct ath6kl *ar = file->private_data;
917         unsigned long reg_addr;
918
919         if (kstrtoul_from_user(user_buf, count, 0, &reg_addr))
920                 return -EINVAL;
921
922         if ((reg_addr % 4) != 0)
923                 return -EINVAL;
924
925         if (reg_addr && !ath6kl_dbg_is_diag_reg_valid(reg_addr))
926                 return -EINVAL;
927
928         ar->debug.dbgfs_diag_reg = reg_addr;
929
930         return count;
931 }
932
933 static const struct file_operations fops_diag_reg_read = {
934         .read = ath6kl_regread_read,
935         .write = ath6kl_regread_write,
936         .open = simple_open,
937         .owner = THIS_MODULE,
938         .llseek = default_llseek,
939 };
940
941 static int ath6kl_regdump_open(struct inode *inode, struct file *file)
942 {
943         struct ath6kl *ar = inode->i_private;
944         u8 *buf;
945         unsigned long int reg_len;
946         unsigned int len = 0, n_reg;
947         u32 addr;
948         __le32 reg_val;
949         int i, status;
950
951         /* Dump all the registers if no register is specified */
952         if (!ar->debug.dbgfs_diag_reg)
953                 n_reg = ath6kl_get_num_reg();
954         else
955                 n_reg = 1;
956
957         reg_len = n_reg * REG_OUTPUT_LEN_PER_LINE;
958         if (n_reg > 1)
959                 reg_len += REGTYPE_STR_LEN;
960
961         buf = vmalloc(reg_len);
962         if (!buf)
963                 return -ENOMEM;
964
965         if (n_reg == 1) {
966                 addr = ar->debug.dbgfs_diag_reg;
967
968                 status = ath6kl_diag_read32(ar,
969                                 TARG_VTOP(ar->target_type, addr),
970                                 (u32 *)&reg_val);
971                 if (status)
972                         goto fail_reg_read;
973
974                 len += scnprintf(buf + len, reg_len - len,
975                                  "0x%06x 0x%08x\n", addr, le32_to_cpu(reg_val));
976                 goto done;
977         }
978
979         for (i = 0; i < ARRAY_SIZE(diag_reg); i++) {
980                 len += scnprintf(buf + len, reg_len - len,
981                                 "%s\n", diag_reg[i].reg_info);
982                 for (addr = diag_reg[i].reg_start;
983                      addr <= diag_reg[i].reg_end; addr += 4) {
984                         status = ath6kl_diag_read32(ar,
985                                         TARG_VTOP(ar->target_type, addr),
986                                         (u32 *)&reg_val);
987                         if (status)
988                                 goto fail_reg_read;
989
990                         len += scnprintf(buf + len, reg_len - len,
991                                         "0x%06x 0x%08x\n",
992                                         addr, le32_to_cpu(reg_val));
993                 }
994         }
995
996 done:
997         file->private_data = buf;
998         return 0;
999
1000 fail_reg_read:
1001         ath6kl_warn("Unable to read memory:%u\n", addr);
1002         vfree(buf);
1003         return -EIO;
1004 }
1005
1006 static ssize_t ath6kl_regdump_read(struct file *file, char __user *user_buf,
1007                                   size_t count, loff_t *ppos)
1008 {
1009         u8 *buf = file->private_data;
1010         return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
1011 }
1012
1013 static int ath6kl_regdump_release(struct inode *inode, struct file *file)
1014 {
1015         vfree(file->private_data);
1016         return 0;
1017 }
1018
1019 static const struct file_operations fops_reg_dump = {
1020         .open = ath6kl_regdump_open,
1021         .read = ath6kl_regdump_read,
1022         .release = ath6kl_regdump_release,
1023         .owner = THIS_MODULE,
1024         .llseek = default_llseek,
1025 };
1026
1027 static ssize_t ath6kl_lrssi_roam_write(struct file *file,
1028                                        const char __user *user_buf,
1029                                        size_t count, loff_t *ppos)
1030 {
1031         struct ath6kl *ar = file->private_data;
1032         unsigned long lrssi_roam_threshold;
1033
1034         if (kstrtoul_from_user(user_buf, count, 0, &lrssi_roam_threshold))
1035                 return -EINVAL;
1036
1037         ar->lrssi_roam_threshold = lrssi_roam_threshold;
1038
1039         ath6kl_wmi_set_roam_lrssi_cmd(ar->wmi, ar->lrssi_roam_threshold);
1040
1041         return count;
1042 }
1043
1044 static ssize_t ath6kl_lrssi_roam_read(struct file *file,
1045                                       char __user *user_buf,
1046                                       size_t count, loff_t *ppos)
1047 {
1048         struct ath6kl *ar = file->private_data;
1049         char buf[32];
1050         unsigned int len;
1051
1052         len = snprintf(buf, sizeof(buf), "%u\n", ar->lrssi_roam_threshold);
1053
1054         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1055 }
1056
1057 static const struct file_operations fops_lrssi_roam_threshold = {
1058         .read = ath6kl_lrssi_roam_read,
1059         .write = ath6kl_lrssi_roam_write,
1060         .open = simple_open,
1061         .owner = THIS_MODULE,
1062         .llseek = default_llseek,
1063 };
1064
1065 static ssize_t ath6kl_regwrite_read(struct file *file,
1066                                     char __user *user_buf,
1067                                     size_t count, loff_t *ppos)
1068 {
1069         struct ath6kl *ar = file->private_data;
1070         u8 buf[32];
1071         unsigned int len = 0;
1072
1073         len = scnprintf(buf, sizeof(buf), "Addr: 0x%x Val: 0x%x\n",
1074                         ar->debug.diag_reg_addr_wr, ar->debug.diag_reg_val_wr);
1075
1076         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1077 }
1078
1079 static ssize_t ath6kl_regwrite_write(struct file *file,
1080                                      const char __user *user_buf,
1081                                      size_t count, loff_t *ppos)
1082 {
1083         struct ath6kl *ar = file->private_data;
1084         char buf[32];
1085         char *sptr, *token;
1086         unsigned int len = 0;
1087         u32 reg_addr, reg_val;
1088
1089         len = min(count, sizeof(buf) - 1);
1090         if (copy_from_user(buf, user_buf, len))
1091                 return -EFAULT;
1092
1093         buf[len] = '\0';
1094         sptr = buf;
1095
1096         token = strsep(&sptr, "=");
1097         if (!token)
1098                 return -EINVAL;
1099
1100         if (kstrtou32(token, 0, &reg_addr))
1101                 return -EINVAL;
1102
1103         if (!ath6kl_dbg_is_diag_reg_valid(reg_addr))
1104                 return -EINVAL;
1105
1106         if (kstrtou32(sptr, 0, &reg_val))
1107                 return -EINVAL;
1108
1109         ar->debug.diag_reg_addr_wr = reg_addr;
1110         ar->debug.diag_reg_val_wr = reg_val;
1111
1112         if (ath6kl_diag_write32(ar, ar->debug.diag_reg_addr_wr,
1113                                 cpu_to_le32(ar->debug.diag_reg_val_wr)))
1114                 return -EIO;
1115
1116         return count;
1117 }
1118
1119 static const struct file_operations fops_diag_reg_write = {
1120         .read = ath6kl_regwrite_read,
1121         .write = ath6kl_regwrite_write,
1122         .open = simple_open,
1123         .owner = THIS_MODULE,
1124         .llseek = default_llseek,
1125 };
1126
1127 int ath6kl_debug_roam_tbl_event(struct ath6kl *ar, const void *buf,
1128                                 size_t len)
1129 {
1130         const struct wmi_target_roam_tbl *tbl;
1131         u16 num_entries;
1132
1133         if (len < sizeof(*tbl))
1134                 return -EINVAL;
1135
1136         tbl = (const struct wmi_target_roam_tbl *) buf;
1137         num_entries = le16_to_cpu(tbl->num_entries);
1138         if (sizeof(*tbl) + num_entries * sizeof(struct wmi_bss_roam_info) >
1139             len)
1140                 return -EINVAL;
1141
1142         if (ar->debug.roam_tbl == NULL ||
1143             ar->debug.roam_tbl_len < (unsigned int) len) {
1144                 kfree(ar->debug.roam_tbl);
1145                 ar->debug.roam_tbl = kmalloc(len, GFP_ATOMIC);
1146                 if (ar->debug.roam_tbl == NULL)
1147                         return -ENOMEM;
1148         }
1149
1150         memcpy(ar->debug.roam_tbl, buf, len);
1151         ar->debug.roam_tbl_len = len;
1152
1153         if (test_bit(ROAM_TBL_PEND, &ar->flag)) {
1154                 clear_bit(ROAM_TBL_PEND, &ar->flag);
1155                 wake_up(&ar->event_wq);
1156         }
1157
1158         return 0;
1159 }
1160
1161 static ssize_t ath6kl_roam_table_read(struct file *file, char __user *user_buf,
1162                                       size_t count, loff_t *ppos)
1163 {
1164         struct ath6kl *ar = file->private_data;
1165         int ret;
1166         long left;
1167         struct wmi_target_roam_tbl *tbl;
1168         u16 num_entries, i;
1169         char *buf;
1170         unsigned int len, buf_len;
1171         ssize_t ret_cnt;
1172
1173         if (down_interruptible(&ar->sem))
1174                 return -EBUSY;
1175
1176         set_bit(ROAM_TBL_PEND, &ar->flag);
1177
1178         ret = ath6kl_wmi_get_roam_tbl_cmd(ar->wmi);
1179         if (ret) {
1180                 up(&ar->sem);
1181                 return ret;
1182         }
1183
1184         left = wait_event_interruptible_timeout(
1185                 ar->event_wq, !test_bit(ROAM_TBL_PEND, &ar->flag), WMI_TIMEOUT);
1186         up(&ar->sem);
1187
1188         if (left <= 0)
1189                 return -ETIMEDOUT;
1190
1191         if (ar->debug.roam_tbl == NULL)
1192                 return -ENOMEM;
1193
1194         tbl = (struct wmi_target_roam_tbl *) ar->debug.roam_tbl;
1195         num_entries = le16_to_cpu(tbl->num_entries);
1196
1197         buf_len = 100 + num_entries * 100;
1198         buf = kzalloc(buf_len, GFP_KERNEL);
1199         if (buf == NULL)
1200                 return -ENOMEM;
1201         len = 0;
1202         len += scnprintf(buf + len, buf_len - len,
1203                          "roam_mode=%u\n\n"
1204                          "# roam_util bssid rssi rssidt last_rssi util bias\n",
1205                          le16_to_cpu(tbl->roam_mode));
1206
1207         for (i = 0; i < num_entries; i++) {
1208                 struct wmi_bss_roam_info *info = &tbl->info[i];
1209                 len += scnprintf(buf + len, buf_len - len,
1210                                  "%d %pM %d %d %d %d %d\n",
1211                                  a_sle32_to_cpu(info->roam_util), info->bssid,
1212                                  info->rssi, info->rssidt, info->last_rssi,
1213                                  info->util, info->bias);
1214         }
1215
1216         if (len > buf_len)
1217                 len = buf_len;
1218
1219         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
1220
1221         kfree(buf);
1222         return ret_cnt;
1223 }
1224
1225 static const struct file_operations fops_roam_table = {
1226         .read = ath6kl_roam_table_read,
1227         .open = simple_open,
1228         .owner = THIS_MODULE,
1229         .llseek = default_llseek,
1230 };
1231
1232 static ssize_t ath6kl_force_roam_write(struct file *file,
1233                                        const char __user *user_buf,
1234                                        size_t count, loff_t *ppos)
1235 {
1236         struct ath6kl *ar = file->private_data;
1237         int ret;
1238         char buf[20];
1239         size_t len;
1240         u8 bssid[ETH_ALEN];
1241
1242         len = min(count, sizeof(buf) - 1);
1243         if (copy_from_user(buf, user_buf, len))
1244                 return -EFAULT;
1245         buf[len] = '\0';
1246
1247         if (!mac_pton(buf, bssid))
1248                 return -EINVAL;
1249
1250         ret = ath6kl_wmi_force_roam_cmd(ar->wmi, bssid);
1251         if (ret)
1252                 return ret;
1253
1254         return count;
1255 }
1256
1257 static const struct file_operations fops_force_roam = {
1258         .write = ath6kl_force_roam_write,
1259         .open = simple_open,
1260         .owner = THIS_MODULE,
1261         .llseek = default_llseek,
1262 };
1263
1264 static ssize_t ath6kl_roam_mode_write(struct file *file,
1265                                       const char __user *user_buf,
1266                                       size_t count, loff_t *ppos)
1267 {
1268         struct ath6kl *ar = file->private_data;
1269         int ret;
1270         char buf[20];
1271         size_t len;
1272         enum wmi_roam_mode mode;
1273
1274         len = min(count, sizeof(buf) - 1);
1275         if (copy_from_user(buf, user_buf, len))
1276                 return -EFAULT;
1277         buf[len] = '\0';
1278         if (len > 0 && buf[len - 1] == '\n')
1279                 buf[len - 1] = '\0';
1280
1281         if (strcasecmp(buf, "default") == 0)
1282                 mode = WMI_DEFAULT_ROAM_MODE;
1283         else if (strcasecmp(buf, "bssbias") == 0)
1284                 mode = WMI_HOST_BIAS_ROAM_MODE;
1285         else if (strcasecmp(buf, "lock") == 0)
1286                 mode = WMI_LOCK_BSS_MODE;
1287         else
1288                 return -EINVAL;
1289
1290         ret = ath6kl_wmi_set_roam_mode_cmd(ar->wmi, mode);
1291         if (ret)
1292                 return ret;
1293
1294         return count;
1295 }
1296
1297 static const struct file_operations fops_roam_mode = {
1298         .write = ath6kl_roam_mode_write,
1299         .open = simple_open,
1300         .owner = THIS_MODULE,
1301         .llseek = default_llseek,
1302 };
1303
1304 void ath6kl_debug_set_keepalive(struct ath6kl *ar, u8 keepalive)
1305 {
1306         ar->debug.keepalive = keepalive;
1307 }
1308
1309 static ssize_t ath6kl_keepalive_read(struct file *file, char __user *user_buf,
1310                                      size_t count, loff_t *ppos)
1311 {
1312         struct ath6kl *ar = file->private_data;
1313         char buf[16];
1314         int len;
1315
1316         len = snprintf(buf, sizeof(buf), "%u\n", ar->debug.keepalive);
1317
1318         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1319 }
1320
1321 static ssize_t ath6kl_keepalive_write(struct file *file,
1322                                       const char __user *user_buf,
1323                                       size_t count, loff_t *ppos)
1324 {
1325         struct ath6kl *ar = file->private_data;
1326         int ret;
1327         u8 val;
1328
1329         ret = kstrtou8_from_user(user_buf, count, 0, &val);
1330         if (ret)
1331                 return ret;
1332
1333         ret = ath6kl_wmi_set_keepalive_cmd(ar->wmi, 0, val);
1334         if (ret)
1335                 return ret;
1336
1337         return count;
1338 }
1339
1340 static const struct file_operations fops_keepalive = {
1341         .open = simple_open,
1342         .read = ath6kl_keepalive_read,
1343         .write = ath6kl_keepalive_write,
1344         .owner = THIS_MODULE,
1345         .llseek = default_llseek,
1346 };
1347
1348 void ath6kl_debug_set_disconnect_timeout(struct ath6kl *ar, u8 timeout)
1349 {
1350         ar->debug.disc_timeout = timeout;
1351 }
1352
1353 static ssize_t ath6kl_disconnect_timeout_read(struct file *file,
1354                                               char __user *user_buf,
1355                                               size_t count, loff_t *ppos)
1356 {
1357         struct ath6kl *ar = file->private_data;
1358         char buf[16];
1359         int len;
1360
1361         len = snprintf(buf, sizeof(buf), "%u\n", ar->debug.disc_timeout);
1362
1363         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1364 }
1365
1366 static ssize_t ath6kl_disconnect_timeout_write(struct file *file,
1367                                                const char __user *user_buf,
1368                                                size_t count, loff_t *ppos)
1369 {
1370         struct ath6kl *ar = file->private_data;
1371         int ret;
1372         u8 val;
1373
1374         ret = kstrtou8_from_user(user_buf, count, 0, &val);
1375         if (ret)
1376                 return ret;
1377
1378         ret = ath6kl_wmi_disctimeout_cmd(ar->wmi, 0, val);
1379         if (ret)
1380                 return ret;
1381
1382         return count;
1383 }
1384
1385 static const struct file_operations fops_disconnect_timeout = {
1386         .open = simple_open,
1387         .read = ath6kl_disconnect_timeout_read,
1388         .write = ath6kl_disconnect_timeout_write,
1389         .owner = THIS_MODULE,
1390         .llseek = default_llseek,
1391 };
1392
1393 static ssize_t ath6kl_create_qos_write(struct file *file,
1394                                                 const char __user *user_buf,
1395                                                 size_t count, loff_t *ppos)
1396 {
1397         struct ath6kl *ar = file->private_data;
1398         struct ath6kl_vif *vif;
1399         char buf[200];
1400         ssize_t len;
1401         char *sptr, *token;
1402         struct wmi_create_pstream_cmd pstream;
1403         u32 val32;
1404         u16 val16;
1405
1406         vif = ath6kl_vif_first(ar);
1407         if (!vif)
1408                 return -EIO;
1409
1410         len = min(count, sizeof(buf) - 1);
1411         if (copy_from_user(buf, user_buf, len))
1412                 return -EFAULT;
1413         buf[len] = '\0';
1414         sptr = buf;
1415
1416         token = strsep(&sptr, " ");
1417         if (!token)
1418                 return -EINVAL;
1419         if (kstrtou8(token, 0, &pstream.user_pri))
1420                 return -EINVAL;
1421
1422         token = strsep(&sptr, " ");
1423         if (!token)
1424                 return -EINVAL;
1425         if (kstrtou8(token, 0, &pstream.traffic_direc))
1426                 return -EINVAL;
1427
1428         token = strsep(&sptr, " ");
1429         if (!token)
1430                 return -EINVAL;
1431         if (kstrtou8(token, 0, &pstream.traffic_class))
1432                 return -EINVAL;
1433
1434         token = strsep(&sptr, " ");
1435         if (!token)
1436                 return -EINVAL;
1437         if (kstrtou8(token, 0, &pstream.traffic_type))
1438                 return -EINVAL;
1439
1440         token = strsep(&sptr, " ");
1441         if (!token)
1442                 return -EINVAL;
1443         if (kstrtou8(token, 0, &pstream.voice_psc_cap))
1444                 return -EINVAL;
1445
1446         token = strsep(&sptr, " ");
1447         if (!token)
1448                 return -EINVAL;
1449         if (kstrtou32(token, 0, &val32))
1450                 return -EINVAL;
1451         pstream.min_service_int = cpu_to_le32(val32);
1452
1453         token = strsep(&sptr, " ");
1454         if (!token)
1455                 return -EINVAL;
1456         if (kstrtou32(token, 0, &val32))
1457                 return -EINVAL;
1458         pstream.max_service_int = cpu_to_le32(val32);
1459
1460         token = strsep(&sptr, " ");
1461         if (!token)
1462                 return -EINVAL;
1463         if (kstrtou32(token, 0, &val32))
1464                 return -EINVAL;
1465         pstream.inactivity_int = cpu_to_le32(val32);
1466
1467         token = strsep(&sptr, " ");
1468         if (!token)
1469                 return -EINVAL;
1470         if (kstrtou32(token, 0, &val32))
1471                 return -EINVAL;
1472         pstream.suspension_int = cpu_to_le32(val32);
1473
1474         token = strsep(&sptr, " ");
1475         if (!token)
1476                 return -EINVAL;
1477         if (kstrtou32(token, 0, &val32))
1478                 return -EINVAL;
1479         pstream.service_start_time = cpu_to_le32(val32);
1480
1481         token = strsep(&sptr, " ");
1482         if (!token)
1483                 return -EINVAL;
1484         if (kstrtou8(token, 0, &pstream.tsid))
1485                 return -EINVAL;
1486
1487         token = strsep(&sptr, " ");
1488         if (!token)
1489                 return -EINVAL;
1490         if (kstrtou16(token, 0, &val16))
1491                 return -EINVAL;
1492         pstream.nominal_msdu = cpu_to_le16(val16);
1493
1494         token = strsep(&sptr, " ");
1495         if (!token)
1496                 return -EINVAL;
1497         if (kstrtou16(token, 0, &val16))
1498                 return -EINVAL;
1499         pstream.max_msdu = cpu_to_le16(val16);
1500
1501         token = strsep(&sptr, " ");
1502         if (!token)
1503                 return -EINVAL;
1504         if (kstrtou32(token, 0, &val32))
1505                 return -EINVAL;
1506         pstream.min_data_rate = cpu_to_le32(val32);
1507
1508         token = strsep(&sptr, " ");
1509         if (!token)
1510                 return -EINVAL;
1511         if (kstrtou32(token, 0, &val32))
1512                 return -EINVAL;
1513         pstream.mean_data_rate = cpu_to_le32(val32);
1514
1515         token = strsep(&sptr, " ");
1516         if (!token)
1517                 return -EINVAL;
1518         if (kstrtou32(token, 0, &val32))
1519                 return -EINVAL;
1520         pstream.peak_data_rate = cpu_to_le32(val32);
1521
1522         token = strsep(&sptr, " ");
1523         if (!token)
1524                 return -EINVAL;
1525         if (kstrtou32(token, 0, &val32))
1526                 return -EINVAL;
1527         pstream.max_burst_size = cpu_to_le32(val32);
1528
1529         token = strsep(&sptr, " ");
1530         if (!token)
1531                 return -EINVAL;
1532         if (kstrtou32(token, 0, &val32))
1533                 return -EINVAL;
1534         pstream.delay_bound = cpu_to_le32(val32);
1535
1536         token = strsep(&sptr, " ");
1537         if (!token)
1538                 return -EINVAL;
1539         if (kstrtou32(token, 0, &val32))
1540                 return -EINVAL;
1541         pstream.min_phy_rate = cpu_to_le32(val32);
1542
1543         token = strsep(&sptr, " ");
1544         if (!token)
1545                 return -EINVAL;
1546         if (kstrtou32(token, 0, &val32))
1547                 return -EINVAL;
1548         pstream.sba = cpu_to_le32(val32);
1549
1550         token = strsep(&sptr, " ");
1551         if (!token)
1552                 return -EINVAL;
1553         if (kstrtou32(token, 0, &val32))
1554                 return -EINVAL;
1555         pstream.medium_time = cpu_to_le32(val32);
1556
1557         pstream.nominal_phy = le32_to_cpu(pstream.min_phy_rate) / 1000000;
1558
1559         ath6kl_wmi_create_pstream_cmd(ar->wmi, vif->fw_vif_idx, &pstream);
1560
1561         return count;
1562 }
1563
1564 static const struct file_operations fops_create_qos = {
1565         .write = ath6kl_create_qos_write,
1566         .open = simple_open,
1567         .owner = THIS_MODULE,
1568         .llseek = default_llseek,
1569 };
1570
1571 static ssize_t ath6kl_delete_qos_write(struct file *file,
1572                                 const char __user *user_buf,
1573                                 size_t count, loff_t *ppos)
1574 {
1575         struct ath6kl *ar = file->private_data;
1576         struct ath6kl_vif *vif;
1577         char buf[100];
1578         ssize_t len;
1579         char *sptr, *token;
1580         u8 traffic_class;
1581         u8 tsid;
1582
1583         vif = ath6kl_vif_first(ar);
1584         if (!vif)
1585                 return -EIO;
1586
1587         len = min(count, sizeof(buf) - 1);
1588         if (copy_from_user(buf, user_buf, len))
1589                 return -EFAULT;
1590         buf[len] = '\0';
1591         sptr = buf;
1592
1593         token = strsep(&sptr, " ");
1594         if (!token)
1595                 return -EINVAL;
1596         if (kstrtou8(token, 0, &traffic_class))
1597                 return -EINVAL;
1598
1599         token = strsep(&sptr, " ");
1600         if (!token)
1601                 return -EINVAL;
1602         if (kstrtou8(token, 0, &tsid))
1603                 return -EINVAL;
1604
1605         ath6kl_wmi_delete_pstream_cmd(ar->wmi, vif->fw_vif_idx,
1606                                       traffic_class, tsid);
1607
1608         return count;
1609 }
1610
1611 static const struct file_operations fops_delete_qos = {
1612         .write = ath6kl_delete_qos_write,
1613         .open = simple_open,
1614         .owner = THIS_MODULE,
1615         .llseek = default_llseek,
1616 };
1617
1618 static ssize_t ath6kl_bgscan_int_write(struct file *file,
1619                                 const char __user *user_buf,
1620                                 size_t count, loff_t *ppos)
1621 {
1622         struct ath6kl *ar = file->private_data;
1623         struct ath6kl_vif *vif;
1624         u16 bgscan_int;
1625         char buf[32];
1626         ssize_t len;
1627
1628         vif = ath6kl_vif_first(ar);
1629         if (!vif)
1630                 return -EIO;
1631
1632         len = min(count, sizeof(buf) - 1);
1633         if (copy_from_user(buf, user_buf, len))
1634                 return -EFAULT;
1635
1636         buf[len] = '\0';
1637         if (kstrtou16(buf, 0, &bgscan_int))
1638                 return -EINVAL;
1639
1640         if (bgscan_int == 0)
1641                 bgscan_int = 0xffff;
1642
1643         vif->bg_scan_period = bgscan_int;
1644
1645         ath6kl_wmi_scanparams_cmd(ar->wmi, 0, 0, 0, bgscan_int, 0, 0, 0, 3,
1646                                   0, 0, 0);
1647
1648         return count;
1649 }
1650
1651 static const struct file_operations fops_bgscan_int = {
1652         .write = ath6kl_bgscan_int_write,
1653         .open = simple_open,
1654         .owner = THIS_MODULE,
1655         .llseek = default_llseek,
1656 };
1657
1658 static ssize_t ath6kl_listen_int_write(struct file *file,
1659                                        const char __user *user_buf,
1660                                        size_t count, loff_t *ppos)
1661 {
1662         struct ath6kl *ar = file->private_data;
1663         struct ath6kl_vif *vif;
1664         u16 listen_interval;
1665         char buf[32];
1666         ssize_t len;
1667
1668         vif = ath6kl_vif_first(ar);
1669         if (!vif)
1670                 return -EIO;
1671
1672         len = min(count, sizeof(buf) - 1);
1673         if (copy_from_user(buf, user_buf, len))
1674                 return -EFAULT;
1675
1676         buf[len] = '\0';
1677         if (kstrtou16(buf, 0, &listen_interval))
1678                 return -EINVAL;
1679
1680         if ((listen_interval < 15) || (listen_interval > 3000))
1681                 return -EINVAL;
1682
1683         vif->listen_intvl_t = listen_interval;
1684         ath6kl_wmi_listeninterval_cmd(ar->wmi, vif->fw_vif_idx,
1685                                       vif->listen_intvl_t, 0);
1686
1687         return count;
1688 }
1689
1690 static ssize_t ath6kl_listen_int_read(struct file *file,
1691                                       char __user *user_buf,
1692                                       size_t count, loff_t *ppos)
1693 {
1694         struct ath6kl *ar = file->private_data;
1695         struct ath6kl_vif *vif;
1696         char buf[32];
1697         int len;
1698
1699         vif = ath6kl_vif_first(ar);
1700         if (!vif)
1701                 return -EIO;
1702
1703         len = scnprintf(buf, sizeof(buf), "%u\n", vif->listen_intvl_t);
1704
1705         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1706 }
1707
1708 static const struct file_operations fops_listen_int = {
1709         .read = ath6kl_listen_int_read,
1710         .write = ath6kl_listen_int_write,
1711         .open = simple_open,
1712         .owner = THIS_MODULE,
1713         .llseek = default_llseek,
1714 };
1715
1716 static ssize_t ath6kl_power_params_write(struct file *file,
1717                                                 const char __user *user_buf,
1718                                                 size_t count, loff_t *ppos)
1719 {
1720         struct ath6kl *ar = file->private_data;
1721         u8 buf[100];
1722         unsigned int len = 0;
1723         char *sptr, *token;
1724         u16 idle_period, ps_poll_num, dtim,
1725                 tx_wakeup, num_tx;
1726
1727         len = min(count, sizeof(buf) - 1);
1728         if (copy_from_user(buf, user_buf, len))
1729                 return -EFAULT;
1730         buf[len] = '\0';
1731         sptr = buf;
1732
1733         token = strsep(&sptr, " ");
1734         if (!token)
1735                 return -EINVAL;
1736         if (kstrtou16(token, 0, &idle_period))
1737                 return -EINVAL;
1738
1739         token = strsep(&sptr, " ");
1740         if (!token)
1741                 return -EINVAL;
1742         if (kstrtou16(token, 0, &ps_poll_num))
1743                 return -EINVAL;
1744
1745         token = strsep(&sptr, " ");
1746         if (!token)
1747                 return -EINVAL;
1748         if (kstrtou16(token, 0, &dtim))
1749                 return -EINVAL;
1750
1751         token = strsep(&sptr, " ");
1752         if (!token)
1753                 return -EINVAL;
1754         if (kstrtou16(token, 0, &tx_wakeup))
1755                 return -EINVAL;
1756
1757         token = strsep(&sptr, " ");
1758         if (!token)
1759                 return -EINVAL;
1760         if (kstrtou16(token, 0, &num_tx))
1761                 return -EINVAL;
1762
1763         ath6kl_wmi_pmparams_cmd(ar->wmi, 0, idle_period, ps_poll_num,
1764                                 dtim, tx_wakeup, num_tx, 0);
1765
1766         return count;
1767 }
1768
1769 static const struct file_operations fops_power_params = {
1770         .write = ath6kl_power_params_write,
1771         .open = simple_open,
1772         .owner = THIS_MODULE,
1773         .llseek = default_llseek,
1774 };
1775
1776 void ath6kl_debug_init(struct ath6kl *ar)
1777 {
1778         skb_queue_head_init(&ar->debug.fwlog_queue);
1779         init_completion(&ar->debug.fwlog_completion);
1780
1781         /*
1782          * Actually we are lying here but don't know how to read the mask
1783          * value from the firmware.
1784          */
1785         ar->debug.fwlog_mask = 0;
1786 }
1787
1788 /*
1789  * Initialisation needs to happen in two stages as fwlog events can come
1790  * before cfg80211 is initialised, and debugfs depends on cfg80211
1791  * initialisation.
1792  */
1793 int ath6kl_debug_init_fs(struct ath6kl *ar)
1794 {
1795         ar->debugfs_phy = debugfs_create_dir("ath6kl",
1796                                              ar->wiphy->debugfsdir);
1797         if (!ar->debugfs_phy)
1798                 return -ENOMEM;
1799
1800         debugfs_create_file("tgt_stats", S_IRUSR, ar->debugfs_phy, ar,
1801                             &fops_tgt_stats);
1802
1803         if (ar->hif_type == ATH6KL_HIF_TYPE_SDIO)
1804                 debugfs_create_file("credit_dist_stats", S_IRUSR,
1805                                     ar->debugfs_phy, ar,
1806                                     &fops_credit_dist_stats);
1807
1808         debugfs_create_file("endpoint_stats", S_IRUSR | S_IWUSR,
1809                             ar->debugfs_phy, ar, &fops_endpoint_stats);
1810
1811         debugfs_create_file("fwlog", S_IRUSR, ar->debugfs_phy, ar,
1812                             &fops_fwlog);
1813
1814         debugfs_create_file("fwlog_block", S_IRUSR, ar->debugfs_phy, ar,
1815                             &fops_fwlog_block);
1816
1817         debugfs_create_file("fwlog_mask", S_IRUSR | S_IWUSR, ar->debugfs_phy,
1818                             ar, &fops_fwlog_mask);
1819
1820         debugfs_create_file("reg_addr", S_IRUSR | S_IWUSR, ar->debugfs_phy, ar,
1821                             &fops_diag_reg_read);
1822
1823         debugfs_create_file("reg_dump", S_IRUSR, ar->debugfs_phy, ar,
1824                             &fops_reg_dump);
1825
1826         debugfs_create_file("lrssi_roam_threshold", S_IRUSR | S_IWUSR,
1827                             ar->debugfs_phy, ar, &fops_lrssi_roam_threshold);
1828
1829         debugfs_create_file("reg_write", S_IRUSR | S_IWUSR,
1830                             ar->debugfs_phy, ar, &fops_diag_reg_write);
1831
1832         debugfs_create_file("war_stats", S_IRUSR, ar->debugfs_phy, ar,
1833                             &fops_war_stats);
1834
1835         debugfs_create_file("roam_table", S_IRUSR, ar->debugfs_phy, ar,
1836                             &fops_roam_table);
1837
1838         debugfs_create_file("force_roam", S_IWUSR, ar->debugfs_phy, ar,
1839                             &fops_force_roam);
1840
1841         debugfs_create_file("roam_mode", S_IWUSR, ar->debugfs_phy, ar,
1842                             &fops_roam_mode);
1843
1844         debugfs_create_file("keepalive", S_IRUSR | S_IWUSR, ar->debugfs_phy, ar,
1845                             &fops_keepalive);
1846
1847         debugfs_create_file("disconnect_timeout", S_IRUSR | S_IWUSR,
1848                             ar->debugfs_phy, ar, &fops_disconnect_timeout);
1849
1850         debugfs_create_file("create_qos", S_IWUSR, ar->debugfs_phy, ar,
1851                             &fops_create_qos);
1852
1853         debugfs_create_file("delete_qos", S_IWUSR, ar->debugfs_phy, ar,
1854                             &fops_delete_qos);
1855
1856         debugfs_create_file("bgscan_interval", S_IWUSR,
1857                             ar->debugfs_phy, ar, &fops_bgscan_int);
1858
1859         debugfs_create_file("listen_interval", S_IRUSR | S_IWUSR,
1860                             ar->debugfs_phy, ar, &fops_listen_int);
1861
1862         debugfs_create_file("power_params", S_IWUSR, ar->debugfs_phy, ar,
1863                             &fops_power_params);
1864
1865         return 0;
1866 }
1867
1868 void ath6kl_debug_cleanup(struct ath6kl *ar)
1869 {
1870         skb_queue_purge(&ar->debug.fwlog_queue);
1871         complete(&ar->debug.fwlog_completion);
1872         kfree(ar->debug.roam_tbl);
1873 }
1874
1875 #endif