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