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