xref: /linux/drivers/net/wireless/ath/carl9170/debug.c (revision 2ab002c755bfa88777e3f2db884d531f3010736c)
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);
135 	if (!buf)
136 		return -ENOMEM;
137 
138 	if (copy_from_user(buf, userbuf, count)) {
139 		err = -EFAULT;
140 		goto out_free;
141 	}
142 
143 	if (mutex_trylock(&ar->mutex) == 0) {
144 		err = -EAGAIN;
145 		goto out_free;
146 	}
147 
148 	if (!CHK_DEV_STATE(ar, dfops->req_dev_state)) {
149 		err = -ENODEV;
150 		goto out_unlock;
151 	}
152 
153 	err = dfops->write(ar, buf, count);
154 	if (err)
155 		goto out_unlock;
156 
157 out_unlock:
158 	mutex_unlock(&ar->mutex);
159 
160 out_free:
161 	vfree(buf);
162 	return err;
163 }
164 
165 static struct debugfs_short_fops debugfs_fops = {
166 	.read	= carl9170_debugfs_read,
167 	.write	= carl9170_debugfs_write,
168 };
169 
170 #define __DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize,	\
171 			       _attr, _dstate)				\
172 static const struct carl9170_debugfs_fops carl_debugfs_##name ##_ops = {\
173 	.read_bufsize = _read_bufsize,					\
174 	.read = _read,							\
175 	.write = _write,						\
176 	.attr = _attr,							\
177 	.req_dev_state = _dstate,					\
178 }
179 
180 #define DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize, _attr)	\
181 	__DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize,	\
182 			       _attr, CARL9170_STARTED)			\
183 
184 #define DEBUGFS_DECLARE_RO_FILE(name, _read_bufsize)			\
185 	DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read,	\
186 			     NULL, _read_bufsize, 0400)
187 
188 #define DEBUGFS_DECLARE_WO_FILE(name)					\
189 	DEBUGFS_DECLARE_FILE(name, NULL, carl9170_debugfs_##name ##_write,\
190 			     0, 0200)
191 
192 #define DEBUGFS_DECLARE_RW_FILE(name, _read_bufsize)			\
193 	DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read,	\
194 			     carl9170_debugfs_##name ##_write,		\
195 			     _read_bufsize, 0600)
196 
197 #define __DEBUGFS_DECLARE_RW_FILE(name, _read_bufsize, _dstate)		\
198 	__DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read,	\
199 			     carl9170_debugfs_##name ##_write,		\
200 			     _read_bufsize, 0600, _dstate)
201 
202 #define DEBUGFS_READONLY_FILE(name, _read_bufsize, fmt, value...)	\
203 static char *carl9170_debugfs_ ##name ## _read(struct ar9170 *ar,	\
204 					     char *buf, size_t buf_size,\
205 					     ssize_t *len)		\
206 {									\
207 	ADD(buf, *len, buf_size, fmt "\n", ##value);			\
208 	return buf;							\
209 }									\
210 DEBUGFS_DECLARE_RO_FILE(name, _read_bufsize)
211 
212 static char *carl9170_debugfs_mem_usage_read(struct ar9170 *ar, char *buf,
213 					     size_t bufsize, ssize_t *len)
214 {
215 	spin_lock_bh(&ar->mem_lock);
216 
217 	ADD(buf, *len, bufsize, "jar: [%*pb]\n",
218 	    ar->fw.mem_blocks, ar->mem_bitmap);
219 
220 	ADD(buf, *len, bufsize, "cookies: used:%3d / total:%3d, allocs:%d\n",
221 	    bitmap_weight(ar->mem_bitmap, ar->fw.mem_blocks),
222 	    ar->fw.mem_blocks, atomic_read(&ar->mem_allocs));
223 
224 	ADD(buf, *len, bufsize, "memory: free:%3d (%3d KiB) / total:%3d KiB)\n",
225 	    atomic_read(&ar->mem_free_blocks),
226 	    (atomic_read(&ar->mem_free_blocks) * ar->fw.mem_block_size) / 1024,
227 	    (ar->fw.mem_blocks * ar->fw.mem_block_size) / 1024);
228 
229 	spin_unlock_bh(&ar->mem_lock);
230 
231 	return buf;
232 }
233 DEBUGFS_DECLARE_RO_FILE(mem_usage, 512);
234 
235 static char *carl9170_debugfs_qos_stat_read(struct ar9170 *ar, char *buf,
236 					    size_t bufsize, ssize_t *len)
237 {
238 	ADD(buf, *len, bufsize, "%s QoS AC\n", modparam_noht ? "Hardware" :
239 	    "Software");
240 
241 	ADD(buf, *len, bufsize, "[     VO            VI       "
242 				 "     BE            BK      ]\n");
243 
244 	spin_lock_bh(&ar->tx_stats_lock);
245 	ADD(buf, *len, bufsize, "[length/limit  length/limit  "
246 				 "length/limit  length/limit ]\n"
247 				"[   %3d/%3d       %3d/%3d    "
248 				 "   %3d/%3d       %3d/%3d   ]\n\n",
249 	    ar->tx_stats[0].len, ar->tx_stats[0].limit,
250 	    ar->tx_stats[1].len, ar->tx_stats[1].limit,
251 	    ar->tx_stats[2].len, ar->tx_stats[2].limit,
252 	    ar->tx_stats[3].len, ar->tx_stats[3].limit);
253 
254 	ADD(buf, *len, bufsize, "[    total         total     "
255 				 "    total         total    ]\n"
256 				"[%10d    %10d    %10d    %10d   ]\n\n",
257 	    ar->tx_stats[0].count, ar->tx_stats[1].count,
258 	    ar->tx_stats[2].count, ar->tx_stats[3].count);
259 
260 	spin_unlock_bh(&ar->tx_stats_lock);
261 
262 	ADD(buf, *len, bufsize, "[  pend/waittx   pend/waittx "
263 				 "  pend/waittx   pend/waittx]\n"
264 				"[   %3d/%3d       %3d/%3d    "
265 				 "   %3d/%3d       %3d/%3d   ]\n\n",
266 	    skb_queue_len(&ar->tx_pending[0]),
267 	    skb_queue_len(&ar->tx_status[0]),
268 	    skb_queue_len(&ar->tx_pending[1]),
269 	    skb_queue_len(&ar->tx_status[1]),
270 	    skb_queue_len(&ar->tx_pending[2]),
271 	    skb_queue_len(&ar->tx_status[2]),
272 	    skb_queue_len(&ar->tx_pending[3]),
273 	    skb_queue_len(&ar->tx_status[3]));
274 
275 	return buf;
276 }
277 DEBUGFS_DECLARE_RO_FILE(qos_stat, 512);
278 
279 static void carl9170_debugfs_format_frame(struct ar9170 *ar,
280 	struct sk_buff *skb, const char *prefix, char *buf,
281 	ssize_t *off, ssize_t bufsize)
282 {
283 	struct _carl9170_tx_superframe *txc = (void *) skb->data;
284 	struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
285 	struct carl9170_tx_info *arinfo = (void *) txinfo->rate_driver_data;
286 	struct ieee80211_hdr *hdr = (void *) txc->frame_data;
287 
288 	ADD(buf, *off, bufsize, "%s %p, c:%2x, DA:%pM, sq:%4d, mc:%.4x, "
289 	    "pc:%.8x, to:%d ms\n", prefix, skb, txc->s.cookie,
290 	    ieee80211_get_DA(hdr), get_seq_h(hdr),
291 	    le16_to_cpu(txc->f.mac_control), le32_to_cpu(txc->f.phy_control),
292 	    jiffies_to_msecs(jiffies - arinfo->timeout));
293 }
294 
295 
296 static char *carl9170_debugfs_ampdu_state_read(struct ar9170 *ar, char *buf,
297 					       size_t bufsize, ssize_t *len)
298 {
299 	struct carl9170_sta_tid *iter;
300 	struct sk_buff *skb;
301 	int cnt = 0, fc;
302 	int offset;
303 
304 	rcu_read_lock();
305 	list_for_each_entry_rcu(iter, &ar->tx_ampdu_list, list) {
306 
307 		spin_lock_bh(&iter->lock);
308 		ADD(buf, *len, bufsize, "Entry: #%2d TID:%1d, BSN:%4d, "
309 		    "SNX:%4d, HSN:%4d, BAW:%2d, state:%1d, toggles:%d\n",
310 		    cnt, iter->tid, iter->bsn, iter->snx, iter->hsn,
311 		    iter->max, iter->state, iter->counter);
312 
313 		ADD(buf, *len, bufsize, "\tWindow:  [%*pb,W]\n",
314 		    CARL9170_BAW_BITS, iter->bitmap);
315 
316 #define BM_STR_OFF(offset)					\
317 	((CARL9170_BAW_BITS - (offset) - 1) / 4 +		\
318 	 (CARL9170_BAW_BITS - (offset) - 1) / 32 + 1)
319 
320 		offset = BM_STR_OFF(0);
321 		ADD(buf, *len, bufsize, "\tBase Seq: %*s\n", offset, "T");
322 
323 		offset = BM_STR_OFF(SEQ_DIFF(iter->snx, iter->bsn));
324 		ADD(buf, *len, bufsize, "\tNext Seq: %*s\n", offset, "W");
325 
326 		offset = BM_STR_OFF(((int)iter->hsn - (int)iter->bsn) %
327 				     CARL9170_BAW_BITS);
328 		ADD(buf, *len, bufsize, "\tLast Seq: %*s\n", offset, "N");
329 
330 		ADD(buf, *len, bufsize, "\tPre-Aggregation reorder buffer: "
331 		    " currently queued:%d\n", skb_queue_len(&iter->queue));
332 
333 		fc = 0;
334 		skb_queue_walk(&iter->queue, skb) {
335 			char prefix[32];
336 
337 			snprintf(prefix, sizeof(prefix), "\t\t%3d :", fc);
338 			carl9170_debugfs_format_frame(ar, skb, prefix, buf,
339 						      len, bufsize);
340 
341 			fc++;
342 		}
343 		spin_unlock_bh(&iter->lock);
344 		cnt++;
345 	}
346 	rcu_read_unlock();
347 
348 	return buf;
349 }
350 DEBUGFS_DECLARE_RO_FILE(ampdu_state, 8000);
351 
352 static void carl9170_debugfs_queue_dump(struct ar9170 *ar, char *buf,
353 	ssize_t *len, size_t bufsize, struct sk_buff_head *queue)
354 {
355 	struct sk_buff *skb;
356 	char prefix[16];
357 	int fc = 0;
358 
359 	spin_lock_bh(&queue->lock);
360 	skb_queue_walk(queue, skb) {
361 		snprintf(prefix, sizeof(prefix), "%3d :", fc);
362 		carl9170_debugfs_format_frame(ar, skb, prefix, buf,
363 					      len, bufsize);
364 		fc++;
365 	}
366 	spin_unlock_bh(&queue->lock);
367 }
368 
369 #define DEBUGFS_QUEUE_DUMP(q, qi)					\
370 static char *carl9170_debugfs_##q ##_##qi ##_read(struct ar9170 *ar,	\
371 	char *buf, size_t bufsize, ssize_t *len)			\
372 {									\
373 	carl9170_debugfs_queue_dump(ar, buf, len, bufsize, &ar->q[qi]);	\
374 	return buf;							\
375 }									\
376 DEBUGFS_DECLARE_RO_FILE(q##_##qi, 8000);
377 
378 static char *carl9170_debugfs_sta_psm_read(struct ar9170 *ar, char *buf,
379 					   size_t bufsize, ssize_t *len)
380 {
381 	ADD(buf, *len, bufsize, "psm state: %s\n", (ar->ps.off_override ?
382 	    "FORCE CAM" : (ar->ps.state ? "PSM" : "CAM")));
383 
384 	ADD(buf, *len, bufsize, "sleep duration: %d ms.\n", ar->ps.sleep_ms);
385 	ADD(buf, *len, bufsize, "last power-state transition: %d ms ago.\n",
386 	    jiffies_to_msecs(jiffies - ar->ps.last_action));
387 	ADD(buf, *len, bufsize, "last CAM->PSM transition: %d ms ago.\n",
388 	    jiffies_to_msecs(jiffies - ar->ps.last_slept));
389 
390 	return buf;
391 }
392 DEBUGFS_DECLARE_RO_FILE(sta_psm, 160);
393 
394 static char *carl9170_debugfs_tx_stuck_read(struct ar9170 *ar, char *buf,
395 					    size_t bufsize, ssize_t *len)
396 {
397 	int i;
398 
399 	for (i = 0; i < ar->hw->queues; i++) {
400 		ADD(buf, *len, bufsize, "TX queue [%d]: %10d max:%10d ms.\n",
401 		    i, ieee80211_queue_stopped(ar->hw, i) ?
402 		    jiffies_to_msecs(jiffies - ar->queue_stop_timeout[i]) : 0,
403 		    jiffies_to_msecs(ar->max_queue_stop_timeout[i]));
404 
405 		ar->max_queue_stop_timeout[i] = 0;
406 	}
407 
408 	return buf;
409 }
410 DEBUGFS_DECLARE_RO_FILE(tx_stuck, 180);
411 
412 static char *carl9170_debugfs_phy_noise_read(struct ar9170 *ar, char *buf,
413 					     size_t bufsize, ssize_t *len)
414 {
415 	int err;
416 
417 	err = carl9170_get_noisefloor(ar);
418 	if (err) {
419 		*len = err;
420 		return buf;
421 	}
422 
423 	ADD(buf, *len, bufsize, "Chain 0: %10d dBm, ext. chan.:%10d dBm\n",
424 	    ar->noise[0], ar->noise[2]);
425 	ADD(buf, *len, bufsize, "Chain 2: %10d dBm, ext. chan.:%10d dBm\n",
426 	    ar->noise[1], ar->noise[3]);
427 
428 	return buf;
429 }
430 DEBUGFS_DECLARE_RO_FILE(phy_noise, 180);
431 
432 static char *carl9170_debugfs_vif_dump_read(struct ar9170 *ar, char *buf,
433 					    size_t bufsize, ssize_t *len)
434 {
435 	struct carl9170_vif_info *iter;
436 	int i = 0;
437 
438 	ADD(buf, *len, bufsize, "registered VIFs:%d \\ %d\n",
439 	    ar->vifs, ar->fw.vif_num);
440 
441 	ADD(buf, *len, bufsize, "VIF bitmap: [%*pb]\n",
442 	    ar->fw.vif_num, &ar->vif_bitmap);
443 
444 	rcu_read_lock();
445 	list_for_each_entry_rcu(iter, &ar->vif_list, list) {
446 		struct ieee80211_vif *vif = carl9170_get_vif(iter);
447 		ADD(buf, *len, bufsize, "\t%d = [%s VIF, id:%d, type:%x "
448 		    " mac:%pM %s]\n", i, (carl9170_get_main_vif(ar) == vif ?
449 		    "Master" : " Slave"), iter->id, vif->type, vif->addr,
450 		    iter->enable_beacon ? "beaconing " : "");
451 		i++;
452 	}
453 	rcu_read_unlock();
454 
455 	return buf;
456 }
457 DEBUGFS_DECLARE_RO_FILE(vif_dump, 8000);
458 
459 #define UPDATE_COUNTER(ar, name)	({				\
460 	u32 __tmp[ARRAY_SIZE(name##_regs)];				\
461 	unsigned int __i, __err = -ENODEV;				\
462 									\
463 	for (__i = 0; __i < ARRAY_SIZE(name##_regs); __i++) {		\
464 		__tmp[__i] = name##_regs[__i].reg;			\
465 		ar->debug.stats.name##_counter[__i] = 0;		\
466 	}								\
467 									\
468 	if (IS_STARTED(ar))						\
469 		__err = carl9170_read_mreg(ar, ARRAY_SIZE(name##_regs),	\
470 			__tmp, ar->debug.stats.name##_counter);		\
471 	(__err); })
472 
473 #define TALLY_SUM_UP(ar, name)	do {					\
474 	unsigned int __i;						\
475 									\
476 	for (__i = 0; __i < ARRAY_SIZE(name##_regs); __i++) {		\
477 		ar->debug.stats.name##_sum[__i] +=			\
478 			ar->debug.stats.name##_counter[__i];		\
479 	}								\
480 } while (0)
481 
482 #define DEBUGFS_HW_TALLY_FILE(name, f)					\
483 static char *carl9170_debugfs_##name ## _read(struct ar9170 *ar,	\
484 	 char *dum, size_t bufsize, ssize_t *ret)			\
485 {									\
486 	char *buf;							\
487 	int i, max_len, err;						\
488 									\
489 	max_len = ARRAY_SIZE(name##_regs) * 80;				\
490 	buf = vmalloc(max_len);						\
491 	if (!buf)							\
492 		return NULL;						\
493 									\
494 	err = UPDATE_COUNTER(ar, name);					\
495 	if (err) {							\
496 		*ret = err;						\
497 		return buf;						\
498 	}								\
499 									\
500 	TALLY_SUM_UP(ar, name);						\
501 									\
502 	for (i = 0; i < ARRAY_SIZE(name##_regs); i++) {			\
503 		ADD(buf, *ret, max_len, "%22s = %" f "[+%" f "]\n",	\
504 		    name##_regs[i].nreg, ar->debug.stats.name ##_sum[i],\
505 		    ar->debug.stats.name ##_counter[i]);		\
506 	}								\
507 									\
508 	return buf;							\
509 }									\
510 DEBUGFS_DECLARE_RO_FILE(name, 0);
511 
512 #define DEBUGFS_HW_REG_FILE(name, f)					\
513 static char *carl9170_debugfs_##name ## _read(struct ar9170 *ar,	\
514 	char *dum, size_t bufsize, ssize_t *ret)			\
515 {									\
516 	char *buf;							\
517 	int i, max_len, err;						\
518 									\
519 	max_len = ARRAY_SIZE(name##_regs) * 80;				\
520 	buf = vmalloc(max_len);						\
521 	if (!buf)							\
522 		return NULL;						\
523 									\
524 	err = UPDATE_COUNTER(ar, name);					\
525 	if (err) {							\
526 		*ret = err;						\
527 		return buf;						\
528 	}								\
529 									\
530 	for (i = 0; i < ARRAY_SIZE(name##_regs); i++) {			\
531 		ADD(buf, *ret, max_len, "%22s = %" f "\n",		\
532 		    name##_regs[i].nreg,				\
533 		    ar->debug.stats.name##_counter[i]);			\
534 	}								\
535 									\
536 	return buf;							\
537 }									\
538 DEBUGFS_DECLARE_RO_FILE(name, 0);
539 
540 static ssize_t carl9170_debugfs_hw_ioread32_write(struct ar9170 *ar,
541 	const char *buf, size_t count)
542 {
543 	int err = 0, i, n = 0, max_len = 32, res;
544 	unsigned int reg, tmp;
545 
546 	if (!count)
547 		return 0;
548 
549 	if (count > max_len)
550 		return -E2BIG;
551 
552 	res = sscanf(buf, "0x%X %d", &reg, &n);
553 	if (res < 1) {
554 		err = -EINVAL;
555 		goto out;
556 	}
557 
558 	if (res == 1)
559 		n = 1;
560 
561 	if (n > 15) {
562 		err = -EMSGSIZE;
563 		goto out;
564 	}
565 
566 	if ((reg >= 0x280000) || ((reg + (n << 2)) >= 0x280000)) {
567 		err = -EADDRNOTAVAIL;
568 		goto out;
569 	}
570 
571 	if (reg & 3) {
572 		err = -EINVAL;
573 		goto out;
574 	}
575 
576 	for (i = 0; i < n; i++) {
577 		err = carl9170_read_reg(ar, reg + (i << 2), &tmp);
578 		if (err)
579 			goto out;
580 
581 		ar->debug.ring[ar->debug.ring_tail].reg = reg + (i << 2);
582 		ar->debug.ring[ar->debug.ring_tail].value = tmp;
583 		ar->debug.ring_tail++;
584 		ar->debug.ring_tail %= CARL9170_DEBUG_RING_SIZE;
585 	}
586 
587 out:
588 	return err ? err : count;
589 }
590 
591 static char *carl9170_debugfs_hw_ioread32_read(struct ar9170 *ar, char *buf,
592 					       size_t bufsize, ssize_t *ret)
593 {
594 	int i = 0;
595 
596 	while (ar->debug.ring_head != ar->debug.ring_tail) {
597 		ADD(buf, *ret, bufsize, "%.8x = %.8x\n",
598 		    ar->debug.ring[ar->debug.ring_head].reg,
599 		    ar->debug.ring[ar->debug.ring_head].value);
600 
601 		ar->debug.ring_head++;
602 		ar->debug.ring_head %= CARL9170_DEBUG_RING_SIZE;
603 
604 		if (i++ == 64)
605 			break;
606 	}
607 	ar->debug.ring_head = ar->debug.ring_tail;
608 	return buf;
609 }
610 DEBUGFS_DECLARE_RW_FILE(hw_ioread32, CARL9170_DEBUG_RING_SIZE * 40);
611 
612 static ssize_t carl9170_debugfs_bug_write(struct ar9170 *ar, const char *buf,
613 					  size_t count)
614 {
615 	int err;
616 
617 	if (count < 1)
618 		return -EINVAL;
619 
620 	switch (buf[0]) {
621 	case 'F':
622 		ar->needs_full_reset = true;
623 		break;
624 
625 	case 'R':
626 		if (!IS_STARTED(ar)) {
627 			err = -EAGAIN;
628 			goto out;
629 		}
630 
631 		ar->needs_full_reset = false;
632 		break;
633 
634 	case 'M':
635 		err = carl9170_mac_reset(ar);
636 		if (err < 0)
637 			count = err;
638 
639 		goto out;
640 
641 	case 'P':
642 		err = carl9170_set_channel(ar, ar->hw->conf.chandef.chan,
643 			cfg80211_get_chandef_type(&ar->hw->conf.chandef));
644 		if (err < 0)
645 			count = err;
646 
647 		goto out;
648 
649 	default:
650 		return -EINVAL;
651 	}
652 
653 	carl9170_restart(ar, CARL9170_RR_USER_REQUEST);
654 
655 out:
656 	return count;
657 }
658 
659 static char *carl9170_debugfs_bug_read(struct ar9170 *ar, char *buf,
660 				       size_t bufsize, ssize_t *ret)
661 {
662 	ADD(buf, *ret, bufsize, "[P]hy reinit, [R]estart, [F]ull usb reset, "
663 	    "[M]ac reset\n");
664 	ADD(buf, *ret, bufsize, "firmware restarts:%d, last reason:%d\n",
665 		ar->restart_counter, ar->last_reason);
666 	ADD(buf, *ret, bufsize, "phy reinit errors:%d (%d)\n",
667 		ar->total_chan_fail, ar->chan_fail);
668 	ADD(buf, *ret, bufsize, "reported firmware errors:%d\n",
669 		ar->fw.err_counter);
670 	ADD(buf, *ret, bufsize, "reported firmware BUGs:%d\n",
671 		ar->fw.bug_counter);
672 	ADD(buf, *ret, bufsize, "pending restart requests:%d\n",
673 		atomic_read(&ar->pending_restarts));
674 	return buf;
675 }
676 __DEBUGFS_DECLARE_RW_FILE(bug, 400, CARL9170_STOPPED);
677 
678 static const char *const erp_modes[] = {
679 	[CARL9170_ERP_INVALID] = "INVALID",
680 	[CARL9170_ERP_AUTO] = "Automatic",
681 	[CARL9170_ERP_MAC80211] = "Set by MAC80211",
682 	[CARL9170_ERP_OFF] = "Force Off",
683 	[CARL9170_ERP_RTS] = "Force RTS",
684 	[CARL9170_ERP_CTS] = "Force CTS"
685 };
686 
687 static char *carl9170_debugfs_erp_read(struct ar9170 *ar, char *buf,
688 				       size_t bufsize, ssize_t *ret)
689 {
690 	ADD(buf, *ret, bufsize, "ERP Setting: (%d) -> %s\n", ar->erp_mode,
691 	    erp_modes[ar->erp_mode]);
692 	return buf;
693 }
694 
695 static ssize_t carl9170_debugfs_erp_write(struct ar9170 *ar, const char *buf,
696 					  size_t count)
697 {
698 	int res, val;
699 
700 	if (count < 1)
701 		return -EINVAL;
702 
703 	res = sscanf(buf, "%d", &val);
704 	if (res != 1)
705 		return -EINVAL;
706 
707 	if (!((val > CARL9170_ERP_INVALID) &&
708 	      (val < __CARL9170_ERP_NUM)))
709 		return -EINVAL;
710 
711 	ar->erp_mode = val;
712 	return count;
713 }
714 
715 DEBUGFS_DECLARE_RW_FILE(erp, 80);
716 
717 static ssize_t carl9170_debugfs_hw_iowrite32_write(struct ar9170 *ar,
718 	const char *buf, size_t count)
719 {
720 	int err = 0, max_len = 22, res;
721 	u32 reg, val;
722 
723 	if (!count)
724 		return 0;
725 
726 	if (count > max_len)
727 		return -E2BIG;
728 
729 	res = sscanf(buf, "0x%X 0x%X", &reg, &val);
730 	if (res != 2) {
731 		err = -EINVAL;
732 		goto out;
733 	}
734 
735 	if (reg <= 0x100000 || reg >= 0x280000) {
736 		err = -EADDRNOTAVAIL;
737 		goto out;
738 	}
739 
740 	if (reg & 3) {
741 		err = -EINVAL;
742 		goto out;
743 	}
744 
745 	err = carl9170_write_reg(ar, reg, val);
746 	if (err)
747 		goto out;
748 
749 out:
750 	return err ? err : count;
751 }
752 DEBUGFS_DECLARE_WO_FILE(hw_iowrite32);
753 
754 DEBUGFS_HW_TALLY_FILE(hw_tx_tally, "u");
755 DEBUGFS_HW_TALLY_FILE(hw_rx_tally, "u");
756 DEBUGFS_HW_TALLY_FILE(hw_phy_errors, "u");
757 DEBUGFS_HW_REG_FILE(hw_wlan_queue, ".8x");
758 DEBUGFS_HW_REG_FILE(hw_pta_queue, ".8x");
759 DEBUGFS_HW_REG_FILE(hw_ampdu_info, ".8x");
760 DEBUGFS_QUEUE_DUMP(tx_status, 0);
761 DEBUGFS_QUEUE_DUMP(tx_status, 1);
762 DEBUGFS_QUEUE_DUMP(tx_status, 2);
763 DEBUGFS_QUEUE_DUMP(tx_status, 3);
764 DEBUGFS_QUEUE_DUMP(tx_pending, 0);
765 DEBUGFS_QUEUE_DUMP(tx_pending, 1);
766 DEBUGFS_QUEUE_DUMP(tx_pending, 2);
767 DEBUGFS_QUEUE_DUMP(tx_pending, 3);
768 DEBUGFS_READONLY_FILE(usb_tx_anch_urbs, 20, "%d",
769 		      atomic_read(&ar->tx_anch_urbs));
770 DEBUGFS_READONLY_FILE(usb_rx_anch_urbs, 20, "%d",
771 		      atomic_read(&ar->rx_anch_urbs));
772 DEBUGFS_READONLY_FILE(usb_rx_work_urbs, 20, "%d",
773 		      atomic_read(&ar->rx_work_urbs));
774 DEBUGFS_READONLY_FILE(usb_rx_pool_urbs, 20, "%d",
775 		      atomic_read(&ar->rx_pool_urbs));
776 
777 DEBUGFS_READONLY_FILE(tx_total_queued, 20, "%d",
778 		      atomic_read(&ar->tx_total_queued));
779 DEBUGFS_READONLY_FILE(tx_ampdu_scheduler, 20, "%d",
780 		      atomic_read(&ar->tx_ampdu_scheduler));
781 
782 DEBUGFS_READONLY_FILE(tx_total_pending, 20, "%d",
783 		      atomic_read(&ar->tx_total_pending));
784 
785 DEBUGFS_READONLY_FILE(tx_ampdu_list_len, 20, "%d",
786 		      ar->tx_ampdu_list_len);
787 
788 DEBUGFS_READONLY_FILE(tx_ampdu_upload, 20, "%d",
789 		      atomic_read(&ar->tx_ampdu_upload));
790 
791 DEBUGFS_READONLY_FILE(tx_janitor_last_run, 64, "last run:%d ms ago",
792 	jiffies_to_msecs(jiffies - ar->tx_janitor_last_run));
793 
794 DEBUGFS_READONLY_FILE(tx_dropped, 20, "%d", ar->tx_dropped);
795 
796 DEBUGFS_READONLY_FILE(rx_dropped, 20, "%d", ar->rx_dropped);
797 
798 DEBUGFS_READONLY_FILE(sniffer_enabled, 20, "%d", ar->sniffer_enabled);
799 DEBUGFS_READONLY_FILE(rx_software_decryption, 20, "%d",
800 		      ar->rx_software_decryption);
801 DEBUGFS_READONLY_FILE(ampdu_factor, 20, "%d",
802 		      ar->current_factor);
803 DEBUGFS_READONLY_FILE(ampdu_density, 20, "%d",
804 		      ar->current_density);
805 
806 DEBUGFS_READONLY_FILE(beacon_int, 20, "%d TU", ar->global_beacon_int);
807 DEBUGFS_READONLY_FILE(pretbtt, 20, "%d TU", ar->global_pretbtt);
808 
809 void carl9170_debugfs_register(struct ar9170 *ar)
810 {
811 	ar->debug_dir = debugfs_create_dir(KBUILD_MODNAME,
812 		ar->hw->wiphy->debugfsdir);
813 
814 #define DEBUGFS_ADD(name)						\
815 	debugfs_create_file_aux(#name, carl_debugfs_##name ##_ops.attr,	\
816 			    ar->debug_dir, ar, &carl_debugfs_##name ## _ops, \
817 			    &debugfs_fops)
818 
819 	DEBUGFS_ADD(usb_tx_anch_urbs);
820 	DEBUGFS_ADD(usb_rx_pool_urbs);
821 	DEBUGFS_ADD(usb_rx_anch_urbs);
822 	DEBUGFS_ADD(usb_rx_work_urbs);
823 
824 	DEBUGFS_ADD(tx_total_queued);
825 	DEBUGFS_ADD(tx_total_pending);
826 	DEBUGFS_ADD(tx_dropped);
827 	DEBUGFS_ADD(tx_stuck);
828 	DEBUGFS_ADD(tx_ampdu_upload);
829 	DEBUGFS_ADD(tx_ampdu_scheduler);
830 	DEBUGFS_ADD(tx_ampdu_list_len);
831 
832 	DEBUGFS_ADD(rx_dropped);
833 	DEBUGFS_ADD(sniffer_enabled);
834 	DEBUGFS_ADD(rx_software_decryption);
835 
836 	DEBUGFS_ADD(mem_usage);
837 	DEBUGFS_ADD(qos_stat);
838 	DEBUGFS_ADD(sta_psm);
839 	DEBUGFS_ADD(ampdu_state);
840 
841 	DEBUGFS_ADD(hw_tx_tally);
842 	DEBUGFS_ADD(hw_rx_tally);
843 	DEBUGFS_ADD(hw_phy_errors);
844 	DEBUGFS_ADD(phy_noise);
845 
846 	DEBUGFS_ADD(hw_wlan_queue);
847 	DEBUGFS_ADD(hw_pta_queue);
848 	DEBUGFS_ADD(hw_ampdu_info);
849 
850 	DEBUGFS_ADD(ampdu_density);
851 	DEBUGFS_ADD(ampdu_factor);
852 
853 	DEBUGFS_ADD(tx_janitor_last_run);
854 
855 	DEBUGFS_ADD(tx_status_0);
856 	DEBUGFS_ADD(tx_status_1);
857 	DEBUGFS_ADD(tx_status_2);
858 	DEBUGFS_ADD(tx_status_3);
859 
860 	DEBUGFS_ADD(tx_pending_0);
861 	DEBUGFS_ADD(tx_pending_1);
862 	DEBUGFS_ADD(tx_pending_2);
863 	DEBUGFS_ADD(tx_pending_3);
864 
865 	DEBUGFS_ADD(hw_ioread32);
866 	DEBUGFS_ADD(hw_iowrite32);
867 	DEBUGFS_ADD(bug);
868 
869 	DEBUGFS_ADD(erp);
870 
871 	DEBUGFS_ADD(vif_dump);
872 
873 	DEBUGFS_ADD(beacon_int);
874 	DEBUGFS_ADD(pretbtt);
875 
876 #undef DEBUGFS_ADD
877 }
878 
879 void carl9170_debugfs_unregister(struct ar9170 *ar)
880 {
881 	debugfs_remove_recursive(ar->debug_dir);
882 }
883