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