xref: /linux/drivers/net/wireless/ath/carl9170/debug.c (revision ece30e1daf6a9c95d6f9d0518534a0b9c8c7fd42)
1 /*
2  * Atheros CARL9170 driver
3  *
4  * debug(fs) probing
5  *
6  * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7  * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; see the file COPYING.  If not, see
21  * http://www.gnu.org/licenses/.
22  *
23  * This file incorporates work covered by the following copyright and
24  * permission notice:
25  *    Copyright (c) 2008-2009 Atheros Communications, Inc.
26  *
27  *    Permission to use, copy, modify, and/or distribute this software for any
28  *    purpose with or without fee is hereby granted, provided that the above
29  *    copyright notice and this permission notice appear in all copies.
30  *
31  *    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32  *    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33  *    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34  *    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35  *    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36  *    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37  *    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38  */
39 
40 #include <linux/slab.h>
41 #include <linux/module.h>
42 #include <linux/seq_file.h>
43 #include <linux/vmalloc.h>
44 #include "carl9170.h"
45 #include "cmd.h"
46 
47 #define ADD(buf, off, max, fmt, args...)				\
48 	off += scnprintf(&buf[off], max - off, fmt, ##args)
49 
50 
51 struct carl9170_debugfs_fops {
52 	unsigned int read_bufsize;
53 	umode_t attr;
54 	char *(*read)(struct ar9170 *ar, char *buf, size_t bufsize,
55 		      ssize_t *len);
56 	ssize_t (*write)(struct ar9170 *aru, const char *buf, size_t size);
57 
58 	enum carl9170_device_state req_dev_state;
59 };
60 
61 static ssize_t carl9170_debugfs_read(struct file *file, char __user *userbuf,
62 				     size_t count, loff_t *ppos)
63 {
64 	const struct carl9170_debugfs_fops *dfops;
65 	struct ar9170 *ar;
66 	char *buf = NULL, *res_buf = NULL;
67 	ssize_t ret = 0;
68 	int err = 0;
69 
70 	if (!count)
71 		return 0;
72 
73 	ar = file->private_data;
74 
75 	if (!ar)
76 		return -ENODEV;
77 	dfops = debugfs_get_aux(file);
78 
79 	if (!dfops->read)
80 		return -ENOSYS;
81 
82 	if (dfops->read_bufsize) {
83 		buf = vmalloc(dfops->read_bufsize);
84 		if (!buf)
85 			return -ENOMEM;
86 	}
87 
88 	mutex_lock(&ar->mutex);
89 	if (!CHK_DEV_STATE(ar, dfops->req_dev_state)) {
90 		err = -ENODEV;
91 		res_buf = buf;
92 		goto out_free;
93 	}
94 
95 	res_buf = dfops->read(ar, buf, dfops->read_bufsize, &ret);
96 
97 	if (ret > 0)
98 		err = simple_read_from_buffer(userbuf, count, ppos,
99 					      res_buf, ret);
100 	else
101 		err = ret;
102 
103 	WARN_ON_ONCE(dfops->read_bufsize && (res_buf != buf));
104 
105 out_free:
106 	vfree(res_buf);
107 	mutex_unlock(&ar->mutex);
108 	return err;
109 }
110 
111 static ssize_t carl9170_debugfs_write(struct file *file,
112 	const char __user *userbuf, size_t count, loff_t *ppos)
113 {
114 	const struct carl9170_debugfs_fops *dfops;
115 	struct ar9170 *ar;
116 	char *buf = NULL;
117 	int err = 0;
118 
119 	if (!count)
120 		return 0;
121 
122 	if (count >= PAGE_SIZE)
123 		return -E2BIG;
124 
125 	ar = file->private_data;
126 
127 	if (!ar)
128 		return -ENODEV;
129 	dfops = debugfs_get_aux(file);
130 
131 	if (!dfops->write)
132 		return -ENOSYS;
133 
134 	buf = vmalloc(count + 1);
135 	if (!buf)
136 		return -ENOMEM;
137 
138 	if (copy_from_user(buf, userbuf, count)) {
139 		err = -EFAULT;
140 		goto out_free;
141 	}
142 	buf[count] = '\0';
143 
144 	if (mutex_trylock(&ar->mutex) == 0) {
145 		err = -EAGAIN;
146 		goto out_free;
147 	}
148 
149 	if (!CHK_DEV_STATE(ar, dfops->req_dev_state)) {
150 		err = -ENODEV;
151 		goto out_unlock;
152 	}
153 
154 	err = dfops->write(ar, buf, count);
155 	if (err)
156 		goto out_unlock;
157 
158 out_unlock:
159 	mutex_unlock(&ar->mutex);
160 
161 out_free:
162 	vfree(buf);
163 	return err;
164 }
165 
166 static struct debugfs_short_fops debugfs_fops = {
167 	.read	= carl9170_debugfs_read,
168 	.write	= carl9170_debugfs_write,
169 };
170 
171 #define __DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize,	\
172 			       _attr, _dstate)				\
173 static const struct carl9170_debugfs_fops carl_debugfs_##name ##_ops = {\
174 	.read_bufsize = _read_bufsize,					\
175 	.read = _read,							\
176 	.write = _write,						\
177 	.attr = _attr,							\
178 	.req_dev_state = _dstate,					\
179 }
180 
181 #define DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize, _attr)	\
182 	__DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize,	\
183 			       _attr, CARL9170_STARTED)			\
184 
185 #define DEBUGFS_DECLARE_RO_FILE(name, _read_bufsize)			\
186 	DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read,	\
187 			     NULL, _read_bufsize, 0400)
188 
189 #define DEBUGFS_DECLARE_WO_FILE(name)					\
190 	DEBUGFS_DECLARE_FILE(name, NULL, carl9170_debugfs_##name ##_write,\
191 			     0, 0200)
192 
193 #define DEBUGFS_DECLARE_RW_FILE(name, _read_bufsize)			\
194 	DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read,	\
195 			     carl9170_debugfs_##name ##_write,		\
196 			     _read_bufsize, 0600)
197 
198 #define __DEBUGFS_DECLARE_RW_FILE(name, _read_bufsize, _dstate)		\
199 	__DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read,	\
200 			     carl9170_debugfs_##name ##_write,		\
201 			     _read_bufsize, 0600, _dstate)
202 
203 #define DEBUGFS_READONLY_FILE(name, _read_bufsize, fmt, value...)	\
204 static char *carl9170_debugfs_ ##name ## _read(struct ar9170 *ar,	\
205 					     char *buf, size_t buf_size,\
206 					     ssize_t *len)		\
207 {									\
208 	ADD(buf, *len, buf_size, fmt "\n", ##value);			\
209 	return buf;							\
210 }									\
211 DEBUGFS_DECLARE_RO_FILE(name, _read_bufsize)
212 
213 static char *carl9170_debugfs_mem_usage_read(struct ar9170 *ar, char *buf,
214 					     size_t bufsize, ssize_t *len)
215 {
216 	spin_lock_bh(&ar->mem_lock);
217 
218 	ADD(buf, *len, bufsize, "jar: [%*pb]\n",
219 	    ar->fw.mem_blocks, ar->mem_bitmap);
220 
221 	ADD(buf, *len, bufsize, "cookies: used:%3d / total:%3d, allocs:%d\n",
222 	    bitmap_weight(ar->mem_bitmap, ar->fw.mem_blocks),
223 	    ar->fw.mem_blocks, atomic_read(&ar->mem_allocs));
224 
225 	ADD(buf, *len, bufsize, "memory: free:%3d (%3d KiB) / total:%3d KiB)\n",
226 	    atomic_read(&ar->mem_free_blocks),
227 	    (atomic_read(&ar->mem_free_blocks) * ar->fw.mem_block_size) / 1024,
228 	    (ar->fw.mem_blocks * ar->fw.mem_block_size) / 1024);
229 
230 	spin_unlock_bh(&ar->mem_lock);
231 
232 	return buf;
233 }
234 DEBUGFS_DECLARE_RO_FILE(mem_usage, 512);
235 
236 static char *carl9170_debugfs_qos_stat_read(struct ar9170 *ar, char *buf,
237 					    size_t bufsize, ssize_t *len)
238 {
239 	ADD(buf, *len, bufsize, "%s QoS AC\n", modparam_noht ? "Hardware" :
240 	    "Software");
241 
242 	ADD(buf, *len, bufsize, "[     VO            VI       "
243 				 "     BE            BK      ]\n");
244 
245 	spin_lock_bh(&ar->tx_stats_lock);
246 	ADD(buf, *len, bufsize, "[length/limit  length/limit  "
247 				 "length/limit  length/limit ]\n"
248 				"[   %3d/%3d       %3d/%3d    "
249 				 "   %3d/%3d       %3d/%3d   ]\n\n",
250 	    ar->tx_stats[0].len, ar->tx_stats[0].limit,
251 	    ar->tx_stats[1].len, ar->tx_stats[1].limit,
252 	    ar->tx_stats[2].len, ar->tx_stats[2].limit,
253 	    ar->tx_stats[3].len, ar->tx_stats[3].limit);
254 
255 	ADD(buf, *len, bufsize, "[    total         total     "
256 				 "    total         total    ]\n"
257 				"[%10d    %10d    %10d    %10d   ]\n\n",
258 	    ar->tx_stats[0].count, ar->tx_stats[1].count,
259 	    ar->tx_stats[2].count, ar->tx_stats[3].count);
260 
261 	spin_unlock_bh(&ar->tx_stats_lock);
262 
263 	ADD(buf, *len, bufsize, "[  pend/waittx   pend/waittx "
264 				 "  pend/waittx   pend/waittx]\n"
265 				"[   %3d/%3d       %3d/%3d    "
266 				 "   %3d/%3d       %3d/%3d   ]\n\n",
267 	    skb_queue_len(&ar->tx_pending[0]),
268 	    skb_queue_len(&ar->tx_status[0]),
269 	    skb_queue_len(&ar->tx_pending[1]),
270 	    skb_queue_len(&ar->tx_status[1]),
271 	    skb_queue_len(&ar->tx_pending[2]),
272 	    skb_queue_len(&ar->tx_status[2]),
273 	    skb_queue_len(&ar->tx_pending[3]),
274 	    skb_queue_len(&ar->tx_status[3]));
275 
276 	return buf;
277 }
278 DEBUGFS_DECLARE_RO_FILE(qos_stat, 512);
279 
280 static void carl9170_debugfs_format_frame(struct ar9170 *ar,
281 	struct sk_buff *skb, const char *prefix, char *buf,
282 	ssize_t *off, ssize_t bufsize)
283 {
284 	struct _carl9170_tx_superframe *txc = (void *) skb->data;
285 	struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
286 	struct carl9170_tx_info *arinfo = (void *) txinfo->rate_driver_data;
287 	struct ieee80211_hdr *hdr = (void *) txc->frame_data;
288 
289 	ADD(buf, *off, bufsize, "%s %p, c:%2x, DA:%pM, sq:%4d, mc:%.4x, "
290 	    "pc:%.8x, to:%d ms\n", prefix, skb, txc->s.cookie,
291 	    ieee80211_get_DA(hdr), get_seq_h(hdr),
292 	    le16_to_cpu(txc->f.mac_control), le32_to_cpu(txc->f.phy_control),
293 	    jiffies_to_msecs(jiffies - arinfo->timeout));
294 }
295 
296 
297 static char *carl9170_debugfs_ampdu_state_read(struct ar9170 *ar, char *buf,
298 					       size_t bufsize, ssize_t *len)
299 {
300 	struct carl9170_sta_tid *iter;
301 	struct sk_buff *skb;
302 	int cnt = 0, fc;
303 	int offset;
304 
305 	rcu_read_lock();
306 	list_for_each_entry_rcu(iter, &ar->tx_ampdu_list, list) {
307 
308 		spin_lock_bh(&iter->lock);
309 		ADD(buf, *len, bufsize, "Entry: #%2d TID:%1d, BSN:%4d, "
310 		    "SNX:%4d, HSN:%4d, BAW:%2d, state:%1d, toggles:%d\n",
311 		    cnt, iter->tid, iter->bsn, iter->snx, iter->hsn,
312 		    iter->max, iter->state, iter->counter);
313 
314 		ADD(buf, *len, bufsize, "\tWindow:  [%*pb,W]\n",
315 		    CARL9170_BAW_BITS, iter->bitmap);
316 
317 #define BM_STR_OFF(offset)					\
318 	((CARL9170_BAW_BITS - (offset) - 1) / 4 +		\
319 	 (CARL9170_BAW_BITS - (offset) - 1) / 32 + 1)
320 
321 		offset = BM_STR_OFF(0);
322 		ADD(buf, *len, bufsize, "\tBase Seq: %*s\n", offset, "T");
323 
324 		offset = BM_STR_OFF(SEQ_DIFF(iter->snx, iter->bsn));
325 		ADD(buf, *len, bufsize, "\tNext Seq: %*s\n", offset, "W");
326 
327 		offset = BM_STR_OFF(((int)iter->hsn - (int)iter->bsn) %
328 				     CARL9170_BAW_BITS);
329 		ADD(buf, *len, bufsize, "\tLast Seq: %*s\n", offset, "N");
330 
331 		ADD(buf, *len, bufsize, "\tPre-Aggregation reorder buffer: "
332 		    " currently queued:%d\n", skb_queue_len(&iter->queue));
333 
334 		fc = 0;
335 		skb_queue_walk(&iter->queue, skb) {
336 			char prefix[32];
337 
338 			snprintf(prefix, sizeof(prefix), "\t\t%3d :", fc);
339 			carl9170_debugfs_format_frame(ar, skb, prefix, buf,
340 						      len, bufsize);
341 
342 			fc++;
343 		}
344 		spin_unlock_bh(&iter->lock);
345 		cnt++;
346 	}
347 	rcu_read_unlock();
348 
349 	return buf;
350 }
351 DEBUGFS_DECLARE_RO_FILE(ampdu_state, 8000);
352 
353 static void carl9170_debugfs_queue_dump(struct ar9170 *ar, char *buf,
354 	ssize_t *len, size_t bufsize, struct sk_buff_head *queue)
355 {
356 	struct sk_buff *skb;
357 	char prefix[16];
358 	int fc = 0;
359 
360 	spin_lock_bh(&queue->lock);
361 	skb_queue_walk(queue, skb) {
362 		snprintf(prefix, sizeof(prefix), "%3d :", fc);
363 		carl9170_debugfs_format_frame(ar, skb, prefix, buf,
364 					      len, bufsize);
365 		fc++;
366 	}
367 	spin_unlock_bh(&queue->lock);
368 }
369 
370 #define DEBUGFS_QUEUE_DUMP(q, qi)					\
371 static char *carl9170_debugfs_##q ##_##qi ##_read(struct ar9170 *ar,	\
372 	char *buf, size_t bufsize, ssize_t *len)			\
373 {									\
374 	carl9170_debugfs_queue_dump(ar, buf, len, bufsize, &ar->q[qi]);	\
375 	return buf;							\
376 }									\
377 DEBUGFS_DECLARE_RO_FILE(q##_##qi, 8000);
378 
379 static char *carl9170_debugfs_sta_psm_read(struct ar9170 *ar, char *buf,
380 					   size_t bufsize, ssize_t *len)
381 {
382 	ADD(buf, *len, bufsize, "psm state: %s\n", (ar->ps.off_override ?
383 	    "FORCE CAM" : (ar->ps.state ? "PSM" : "CAM")));
384 
385 	ADD(buf, *len, bufsize, "sleep duration: %d ms.\n", ar->ps.sleep_ms);
386 	ADD(buf, *len, bufsize, "last power-state transition: %d ms ago.\n",
387 	    jiffies_to_msecs(jiffies - ar->ps.last_action));
388 	ADD(buf, *len, bufsize, "last CAM->PSM transition: %d ms ago.\n",
389 	    jiffies_to_msecs(jiffies - ar->ps.last_slept));
390 
391 	return buf;
392 }
393 DEBUGFS_DECLARE_RO_FILE(sta_psm, 160);
394 
395 static char *carl9170_debugfs_tx_stuck_read(struct ar9170 *ar, char *buf,
396 					    size_t bufsize, ssize_t *len)
397 {
398 	int i;
399 
400 	for (i = 0; i < ar->hw->queues; i++) {
401 		ADD(buf, *len, bufsize, "TX queue [%d]: %10d max:%10d ms.\n",
402 		    i, ieee80211_queue_stopped(ar->hw, i) ?
403 		    jiffies_to_msecs(jiffies - ar->queue_stop_timeout[i]) : 0,
404 		    jiffies_to_msecs(ar->max_queue_stop_timeout[i]));
405 
406 		ar->max_queue_stop_timeout[i] = 0;
407 	}
408 
409 	return buf;
410 }
411 DEBUGFS_DECLARE_RO_FILE(tx_stuck, 180);
412 
413 static char *carl9170_debugfs_phy_noise_read(struct ar9170 *ar, char *buf,
414 					     size_t bufsize, ssize_t *len)
415 {
416 	int err;
417 
418 	err = carl9170_get_noisefloor(ar);
419 	if (err) {
420 		*len = err;
421 		return buf;
422 	}
423 
424 	ADD(buf, *len, bufsize, "Chain 0: %10d dBm, ext. chan.:%10d dBm\n",
425 	    ar->noise[0], ar->noise[2]);
426 	ADD(buf, *len, bufsize, "Chain 2: %10d dBm, ext. chan.:%10d dBm\n",
427 	    ar->noise[1], ar->noise[3]);
428 
429 	return buf;
430 }
431 DEBUGFS_DECLARE_RO_FILE(phy_noise, 180);
432 
433 static char *carl9170_debugfs_vif_dump_read(struct ar9170 *ar, char *buf,
434 					    size_t bufsize, ssize_t *len)
435 {
436 	struct carl9170_vif_info *iter;
437 	int i = 0;
438 
439 	ADD(buf, *len, bufsize, "registered VIFs:%d \\ %d\n",
440 	    ar->vifs, ar->fw.vif_num);
441 
442 	ADD(buf, *len, bufsize, "VIF bitmap: [%*pb]\n",
443 	    ar->fw.vif_num, &ar->vif_bitmap);
444 
445 	rcu_read_lock();
446 	list_for_each_entry_rcu(iter, &ar->vif_list, list) {
447 		struct ieee80211_vif *vif = carl9170_get_vif(iter);
448 		ADD(buf, *len, bufsize, "\t%d = [%s VIF, id:%d, type:%x "
449 		    " mac:%pM %s]\n", i, (carl9170_get_main_vif(ar) == vif ?
450 		    "Master" : " Slave"), iter->id, vif->type, vif->addr,
451 		    iter->enable_beacon ? "beaconing " : "");
452 		i++;
453 	}
454 	rcu_read_unlock();
455 
456 	return buf;
457 }
458 DEBUGFS_DECLARE_RO_FILE(vif_dump, 8000);
459 
460 #define UPDATE_COUNTER(ar, name)	({				\
461 	u32 __tmp[ARRAY_SIZE(name##_regs)];				\
462 	unsigned int __i, __err = -ENODEV;				\
463 									\
464 	for (__i = 0; __i < ARRAY_SIZE(name##_regs); __i++) {		\
465 		__tmp[__i] = name##_regs[__i].reg;			\
466 		ar->debug.stats.name##_counter[__i] = 0;		\
467 	}								\
468 									\
469 	if (IS_STARTED(ar))						\
470 		__err = carl9170_read_mreg(ar, ARRAY_SIZE(name##_regs),	\
471 			__tmp, ar->debug.stats.name##_counter);		\
472 	(__err); })
473 
474 #define TALLY_SUM_UP(ar, name)	do {					\
475 	unsigned int __i;						\
476 									\
477 	for (__i = 0; __i < ARRAY_SIZE(name##_regs); __i++) {		\
478 		ar->debug.stats.name##_sum[__i] +=			\
479 			ar->debug.stats.name##_counter[__i];		\
480 	}								\
481 } while (0)
482 
483 #define DEBUGFS_HW_TALLY_FILE(name, f)					\
484 static char *carl9170_debugfs_##name ## _read(struct ar9170 *ar,	\
485 	 char *dum, size_t bufsize, ssize_t *ret)			\
486 {									\
487 	char *buf;							\
488 	int i, max_len, err;						\
489 									\
490 	max_len = ARRAY_SIZE(name##_regs) * 80;				\
491 	buf = vmalloc(max_len);						\
492 	if (!buf)							\
493 		return NULL;						\
494 									\
495 	err = UPDATE_COUNTER(ar, name);					\
496 	if (err) {							\
497 		*ret = err;						\
498 		return buf;						\
499 	}								\
500 									\
501 	TALLY_SUM_UP(ar, name);						\
502 									\
503 	for (i = 0; i < ARRAY_SIZE(name##_regs); i++) {			\
504 		ADD(buf, *ret, max_len, "%22s = %" f "[+%" f "]\n",	\
505 		    name##_regs[i].nreg, ar->debug.stats.name ##_sum[i],\
506 		    ar->debug.stats.name ##_counter[i]);		\
507 	}								\
508 									\
509 	return buf;							\
510 }									\
511 DEBUGFS_DECLARE_RO_FILE(name, 0);
512 
513 #define DEBUGFS_HW_REG_FILE(name, f)					\
514 static char *carl9170_debugfs_##name ## _read(struct ar9170 *ar,	\
515 	char *dum, size_t bufsize, ssize_t *ret)			\
516 {									\
517 	char *buf;							\
518 	int i, max_len, err;						\
519 									\
520 	max_len = ARRAY_SIZE(name##_regs) * 80;				\
521 	buf = vmalloc(max_len);						\
522 	if (!buf)							\
523 		return NULL;						\
524 									\
525 	err = UPDATE_COUNTER(ar, name);					\
526 	if (err) {							\
527 		*ret = err;						\
528 		return buf;						\
529 	}								\
530 									\
531 	for (i = 0; i < ARRAY_SIZE(name##_regs); i++) {			\
532 		ADD(buf, *ret, max_len, "%22s = %" f "\n",		\
533 		    name##_regs[i].nreg,				\
534 		    ar->debug.stats.name##_counter[i]);			\
535 	}								\
536 									\
537 	return buf;							\
538 }									\
539 DEBUGFS_DECLARE_RO_FILE(name, 0);
540 
541 static ssize_t carl9170_debugfs_hw_ioread32_write(struct ar9170 *ar,
542 	const char *buf, size_t count)
543 {
544 	int err = 0, i, n = 0, max_len = 32, res;
545 	unsigned int reg, tmp;
546 
547 	if (!count)
548 		return 0;
549 
550 	if (count > max_len)
551 		return -E2BIG;
552 
553 	res = sscanf(buf, "0x%X %d", &reg, &n);
554 	if (res < 1) {
555 		err = -EINVAL;
556 		goto out;
557 	}
558 
559 	if (res == 1)
560 		n = 1;
561 
562 	if (n > 15) {
563 		err = -EMSGSIZE;
564 		goto out;
565 	}
566 
567 	if ((reg >= 0x280000) || ((reg + (n << 2)) >= 0x280000)) {
568 		err = -EADDRNOTAVAIL;
569 		goto out;
570 	}
571 
572 	if (reg & 3) {
573 		err = -EINVAL;
574 		goto out;
575 	}
576 
577 	for (i = 0; i < n; i++) {
578 		err = carl9170_read_reg(ar, reg + (i << 2), &tmp);
579 		if (err)
580 			goto out;
581 
582 		ar->debug.ring[ar->debug.ring_tail].reg = reg + (i << 2);
583 		ar->debug.ring[ar->debug.ring_tail].value = tmp;
584 		ar->debug.ring_tail++;
585 		ar->debug.ring_tail %= CARL9170_DEBUG_RING_SIZE;
586 	}
587 
588 out:
589 	return err ? err : count;
590 }
591 
592 static char *carl9170_debugfs_hw_ioread32_read(struct ar9170 *ar, char *buf,
593 					       size_t bufsize, ssize_t *ret)
594 {
595 	int i = 0;
596 
597 	while (ar->debug.ring_head != ar->debug.ring_tail) {
598 		ADD(buf, *ret, bufsize, "%.8x = %.8x\n",
599 		    ar->debug.ring[ar->debug.ring_head].reg,
600 		    ar->debug.ring[ar->debug.ring_head].value);
601 
602 		ar->debug.ring_head++;
603 		ar->debug.ring_head %= CARL9170_DEBUG_RING_SIZE;
604 
605 		if (i++ == 64)
606 			break;
607 	}
608 	ar->debug.ring_head = ar->debug.ring_tail;
609 	return buf;
610 }
611 DEBUGFS_DECLARE_RW_FILE(hw_ioread32, CARL9170_DEBUG_RING_SIZE * 40);
612 
613 static ssize_t carl9170_debugfs_bug_write(struct ar9170 *ar, const char *buf,
614 					  size_t count)
615 {
616 	int err;
617 
618 	if (count < 1)
619 		return -EINVAL;
620 
621 	switch (buf[0]) {
622 	case 'F':
623 		ar->needs_full_reset = true;
624 		break;
625 
626 	case 'R':
627 		if (!IS_STARTED(ar)) {
628 			err = -EAGAIN;
629 			goto out;
630 		}
631 
632 		ar->needs_full_reset = false;
633 		break;
634 
635 	case 'M':
636 		err = carl9170_mac_reset(ar);
637 		if (err < 0)
638 			count = err;
639 
640 		goto out;
641 
642 	case 'P':
643 		err = carl9170_set_channel(ar, ar->hw->conf.chandef.chan,
644 			cfg80211_get_chandef_type(&ar->hw->conf.chandef));
645 		if (err < 0)
646 			count = err;
647 
648 		goto out;
649 
650 	default:
651 		return -EINVAL;
652 	}
653 
654 	carl9170_restart(ar, CARL9170_RR_USER_REQUEST);
655 
656 out:
657 	return count;
658 }
659 
660 static char *carl9170_debugfs_bug_read(struct ar9170 *ar, char *buf,
661 				       size_t bufsize, ssize_t *ret)
662 {
663 	ADD(buf, *ret, bufsize, "[P]hy reinit, [R]estart, [F]ull usb reset, "
664 	    "[M]ac reset\n");
665 	ADD(buf, *ret, bufsize, "firmware restarts:%d, last reason:%d\n",
666 		ar->restart_counter, ar->last_reason);
667 	ADD(buf, *ret, bufsize, "phy reinit errors:%d (%d)\n",
668 		ar->total_chan_fail, ar->chan_fail);
669 	ADD(buf, *ret, bufsize, "reported firmware errors:%d\n",
670 		ar->fw.err_counter);
671 	ADD(buf, *ret, bufsize, "reported firmware BUGs:%d\n",
672 		ar->fw.bug_counter);
673 	ADD(buf, *ret, bufsize, "pending restart requests:%d\n",
674 		atomic_read(&ar->pending_restarts));
675 	return buf;
676 }
677 __DEBUGFS_DECLARE_RW_FILE(bug, 400, CARL9170_STOPPED);
678 
679 static const char *const erp_modes[] = {
680 	[CARL9170_ERP_INVALID] = "INVALID",
681 	[CARL9170_ERP_AUTO] = "Automatic",
682 	[CARL9170_ERP_MAC80211] = "Set by MAC80211",
683 	[CARL9170_ERP_OFF] = "Force Off",
684 	[CARL9170_ERP_RTS] = "Force RTS",
685 	[CARL9170_ERP_CTS] = "Force CTS"
686 };
687 
688 static char *carl9170_debugfs_erp_read(struct ar9170 *ar, char *buf,
689 				       size_t bufsize, ssize_t *ret)
690 {
691 	ADD(buf, *ret, bufsize, "ERP Setting: (%d) -> %s\n", ar->erp_mode,
692 	    erp_modes[ar->erp_mode]);
693 	return buf;
694 }
695 
696 static ssize_t carl9170_debugfs_erp_write(struct ar9170 *ar, const char *buf,
697 					  size_t count)
698 {
699 	int res, val;
700 
701 	if (count < 1)
702 		return -EINVAL;
703 
704 	res = sscanf(buf, "%d", &val);
705 	if (res != 1)
706 		return -EINVAL;
707 
708 	if (!((val > CARL9170_ERP_INVALID) &&
709 	      (val < __CARL9170_ERP_NUM)))
710 		return -EINVAL;
711 
712 	ar->erp_mode = val;
713 	return count;
714 }
715 
716 DEBUGFS_DECLARE_RW_FILE(erp, 80);
717 
718 static ssize_t carl9170_debugfs_hw_iowrite32_write(struct ar9170 *ar,
719 	const char *buf, size_t count)
720 {
721 	int err = 0, max_len = 22, res;
722 	u32 reg, val;
723 
724 	if (!count)
725 		return 0;
726 
727 	if (count > max_len)
728 		return -E2BIG;
729 
730 	res = sscanf(buf, "0x%X 0x%X", &reg, &val);
731 	if (res != 2) {
732 		err = -EINVAL;
733 		goto out;
734 	}
735 
736 	if (reg <= 0x100000 || reg >= 0x280000) {
737 		err = -EADDRNOTAVAIL;
738 		goto out;
739 	}
740 
741 	if (reg & 3) {
742 		err = -EINVAL;
743 		goto out;
744 	}
745 
746 	err = carl9170_write_reg(ar, reg, val);
747 	if (err)
748 		goto out;
749 
750 out:
751 	return err ? err : count;
752 }
753 DEBUGFS_DECLARE_WO_FILE(hw_iowrite32);
754 
755 DEBUGFS_HW_TALLY_FILE(hw_tx_tally, "u");
756 DEBUGFS_HW_TALLY_FILE(hw_rx_tally, "u");
757 DEBUGFS_HW_TALLY_FILE(hw_phy_errors, "u");
758 DEBUGFS_HW_REG_FILE(hw_wlan_queue, ".8x");
759 DEBUGFS_HW_REG_FILE(hw_pta_queue, ".8x");
760 DEBUGFS_HW_REG_FILE(hw_ampdu_info, ".8x");
761 DEBUGFS_QUEUE_DUMP(tx_status, 0);
762 DEBUGFS_QUEUE_DUMP(tx_status, 1);
763 DEBUGFS_QUEUE_DUMP(tx_status, 2);
764 DEBUGFS_QUEUE_DUMP(tx_status, 3);
765 DEBUGFS_QUEUE_DUMP(tx_pending, 0);
766 DEBUGFS_QUEUE_DUMP(tx_pending, 1);
767 DEBUGFS_QUEUE_DUMP(tx_pending, 2);
768 DEBUGFS_QUEUE_DUMP(tx_pending, 3);
769 DEBUGFS_READONLY_FILE(usb_tx_anch_urbs, 20, "%d",
770 		      atomic_read(&ar->tx_anch_urbs));
771 DEBUGFS_READONLY_FILE(usb_rx_anch_urbs, 20, "%d",
772 		      atomic_read(&ar->rx_anch_urbs));
773 DEBUGFS_READONLY_FILE(usb_rx_work_urbs, 20, "%d",
774 		      atomic_read(&ar->rx_work_urbs));
775 DEBUGFS_READONLY_FILE(usb_rx_pool_urbs, 20, "%d",
776 		      atomic_read(&ar->rx_pool_urbs));
777 
778 DEBUGFS_READONLY_FILE(tx_total_queued, 20, "%d",
779 		      atomic_read(&ar->tx_total_queued));
780 DEBUGFS_READONLY_FILE(tx_ampdu_scheduler, 20, "%d",
781 		      atomic_read(&ar->tx_ampdu_scheduler));
782 
783 DEBUGFS_READONLY_FILE(tx_total_pending, 20, "%d",
784 		      atomic_read(&ar->tx_total_pending));
785 
786 DEBUGFS_READONLY_FILE(tx_ampdu_list_len, 20, "%d",
787 		      ar->tx_ampdu_list_len);
788 
789 DEBUGFS_READONLY_FILE(tx_ampdu_upload, 20, "%d",
790 		      atomic_read(&ar->tx_ampdu_upload));
791 
792 DEBUGFS_READONLY_FILE(tx_janitor_last_run, 64, "last run:%d ms ago",
793 	jiffies_to_msecs(jiffies - ar->tx_janitor_last_run));
794 
795 DEBUGFS_READONLY_FILE(tx_dropped, 20, "%d", ar->tx_dropped);
796 
797 DEBUGFS_READONLY_FILE(rx_dropped, 20, "%d", ar->rx_dropped);
798 DEBUGFS_READONLY_FILE(rx_phy_errors, 20, "%d", ar->rx_phy_errors);
799 
800 DEBUGFS_READONLY_FILE(sniffer_enabled, 20, "%d", ar->sniffer_enabled);
801 DEBUGFS_READONLY_FILE(rx_software_decryption, 20, "%d",
802 		      ar->rx_software_decryption);
803 DEBUGFS_READONLY_FILE(ampdu_factor, 20, "%d",
804 		      ar->current_factor);
805 DEBUGFS_READONLY_FILE(ampdu_density, 20, "%d",
806 		      ar->current_density);
807 
808 DEBUGFS_READONLY_FILE(beacon_int, 20, "%d TU", ar->global_beacon_int);
809 DEBUGFS_READONLY_FILE(pretbtt, 20, "%d TU", ar->global_pretbtt);
810 
811 void carl9170_debugfs_register(struct ar9170 *ar)
812 {
813 	ar->debug_dir = debugfs_create_dir(KBUILD_MODNAME,
814 		ar->hw->wiphy->debugfsdir);
815 
816 #define DEBUGFS_ADD(name)						\
817 	debugfs_create_file_aux(#name, carl_debugfs_##name ##_ops.attr,	\
818 			    ar->debug_dir, ar, &carl_debugfs_##name ## _ops, \
819 			    &debugfs_fops)
820 
821 	DEBUGFS_ADD(usb_tx_anch_urbs);
822 	DEBUGFS_ADD(usb_rx_pool_urbs);
823 	DEBUGFS_ADD(usb_rx_anch_urbs);
824 	DEBUGFS_ADD(usb_rx_work_urbs);
825 
826 	DEBUGFS_ADD(tx_total_queued);
827 	DEBUGFS_ADD(tx_total_pending);
828 	DEBUGFS_ADD(tx_dropped);
829 	DEBUGFS_ADD(tx_stuck);
830 	DEBUGFS_ADD(tx_ampdu_upload);
831 	DEBUGFS_ADD(tx_ampdu_scheduler);
832 	DEBUGFS_ADD(tx_ampdu_list_len);
833 
834 	DEBUGFS_ADD(rx_dropped);
835 	DEBUGFS_ADD(rx_phy_errors);
836 	DEBUGFS_ADD(sniffer_enabled);
837 	DEBUGFS_ADD(rx_software_decryption);
838 
839 	DEBUGFS_ADD(mem_usage);
840 	DEBUGFS_ADD(qos_stat);
841 	DEBUGFS_ADD(sta_psm);
842 	DEBUGFS_ADD(ampdu_state);
843 
844 	DEBUGFS_ADD(hw_tx_tally);
845 	DEBUGFS_ADD(hw_rx_tally);
846 	DEBUGFS_ADD(hw_phy_errors);
847 	DEBUGFS_ADD(phy_noise);
848 
849 	DEBUGFS_ADD(hw_wlan_queue);
850 	DEBUGFS_ADD(hw_pta_queue);
851 	DEBUGFS_ADD(hw_ampdu_info);
852 
853 	DEBUGFS_ADD(ampdu_density);
854 	DEBUGFS_ADD(ampdu_factor);
855 
856 	DEBUGFS_ADD(tx_janitor_last_run);
857 
858 	DEBUGFS_ADD(tx_status_0);
859 	DEBUGFS_ADD(tx_status_1);
860 	DEBUGFS_ADD(tx_status_2);
861 	DEBUGFS_ADD(tx_status_3);
862 
863 	DEBUGFS_ADD(tx_pending_0);
864 	DEBUGFS_ADD(tx_pending_1);
865 	DEBUGFS_ADD(tx_pending_2);
866 	DEBUGFS_ADD(tx_pending_3);
867 
868 	DEBUGFS_ADD(hw_ioread32);
869 	DEBUGFS_ADD(hw_iowrite32);
870 	DEBUGFS_ADD(bug);
871 
872 	DEBUGFS_ADD(erp);
873 
874 	DEBUGFS_ADD(vif_dump);
875 
876 	DEBUGFS_ADD(beacon_int);
877 	DEBUGFS_ADD(pretbtt);
878 
879 #undef DEBUGFS_ADD
880 }
881 
882 void carl9170_debugfs_unregister(struct ar9170 *ar)
883 {
884 	debugfs_remove_recursive(ar->debug_dir);
885 }
886