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