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