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