1 // SPDX-License-Identifier: GPL-2.0-or-later
4 <http://rt2x00.serialmonkey.com>
10 Abstract: rt2x00 debugfs specific routines.
13 #include <linux/debugfs.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/poll.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
22 #include "rt2x00lib.h"
23 #include "rt2x00dump.h"
25 #define MAX_LINE_LENGTH 64
27 struct rt2x00debug_crypto {
28 unsigned long success;
29 unsigned long icv_error;
30 unsigned long mic_error;
31 unsigned long key_error;
34 struct rt2x00debug_intf {
36 * Pointer to driver structure where
37 * this debugfs entry belongs to.
39 struct rt2x00_dev *rt2x00dev;
42 * Reference to the rt2x00debug structure
43 * which can be used to communicate with
46 const struct rt2x00debug *debug;
49 * Debugfs entries for:
53 * - device state flags file
54 * - device capability flags file
55 * - hardware restart file
57 * - csr offset/value files
58 * - eeprom offset/value files
59 * - bbp offset/value files
60 * - rf offset/value files
61 * - rfcsr offset/value files
67 struct dentry *driver_folder;
70 * The frame dump file only allows a single reader,
71 * so we need to store the current state here.
73 unsigned long frame_dump_flags;
74 #define FRAME_DUMP_FILE_OPEN 1
77 * We queue each frame before dumping it to the user,
78 * per read command we will pass a single skb structure
79 * so we should be prepared to queue multiple sk buffers
80 * before sending it to userspace.
82 struct sk_buff_head frame_dump_skbqueue;
83 wait_queue_head_t frame_dump_waitqueue;
86 * HW crypto statistics.
87 * All statistics are stored separately per cipher type.
89 struct rt2x00debug_crypto crypto_stats[CIPHER_MAX];
92 * Driver and chipset files will use a data buffer
93 * that has been created in advance. This will simplify
94 * the code since we can use the debugfs functions.
96 struct debugfs_blob_wrapper driver_blob;
97 struct debugfs_blob_wrapper chipset_blob;
100 * Requested offset for each register type.
102 unsigned int offset_csr;
103 unsigned int offset_eeprom;
104 unsigned int offset_bbp;
105 unsigned int offset_rf;
106 unsigned int offset_rfcsr;
109 void rt2x00debug_update_crypto(struct rt2x00_dev *rt2x00dev,
110 struct rxdone_entry_desc *rxdesc)
112 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
113 enum cipher cipher = rxdesc->cipher;
114 enum rx_crypto status = rxdesc->cipher_status;
116 if (cipher == CIPHER_TKIP_NO_MIC)
117 cipher = CIPHER_TKIP;
118 if (cipher == CIPHER_NONE || cipher >= CIPHER_MAX)
121 /* Remove CIPHER_NONE index */
124 intf->crypto_stats[cipher].success += (status == RX_CRYPTO_SUCCESS);
125 intf->crypto_stats[cipher].icv_error += (status == RX_CRYPTO_FAIL_ICV);
126 intf->crypto_stats[cipher].mic_error += (status == RX_CRYPTO_FAIL_MIC);
127 intf->crypto_stats[cipher].key_error += (status == RX_CRYPTO_FAIL_KEY);
130 void rt2x00debug_dump_frame(struct rt2x00_dev *rt2x00dev,
131 enum rt2x00_dump_type type, struct queue_entry *entry)
133 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
134 struct sk_buff *skb = entry->skb;
135 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
136 struct sk_buff *skbcopy;
137 struct rt2x00dump_hdr *dump_hdr;
138 struct timespec64 timestamp;
141 if (likely(!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)))
144 ktime_get_ts64(×tamp);
146 if (skb_queue_len(&intf->frame_dump_skbqueue) > 20) {
147 rt2x00_dbg(rt2x00dev, "txrx dump queue length exceeded\n");
152 if (skbdesc->flags & SKBDESC_DESC_IN_SKB)
153 data_len -= skbdesc->desc_len;
155 skbcopy = alloc_skb(sizeof(*dump_hdr) + skbdesc->desc_len + data_len,
158 rt2x00_dbg(rt2x00dev, "Failed to copy skb for dump\n");
162 dump_hdr = skb_put(skbcopy, sizeof(*dump_hdr));
163 dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
164 dump_hdr->header_length = cpu_to_le32(sizeof(*dump_hdr));
165 dump_hdr->desc_length = cpu_to_le32(skbdesc->desc_len);
166 dump_hdr->data_length = cpu_to_le32(data_len);
167 dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
168 dump_hdr->chip_rf = cpu_to_le16(rt2x00dev->chip.rf);
169 dump_hdr->chip_rev = cpu_to_le16(rt2x00dev->chip.rev);
170 dump_hdr->type = cpu_to_le16(type);
171 dump_hdr->queue_index = entry->queue->qid;
172 dump_hdr->entry_index = entry->entry_idx;
173 dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
174 dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_nsec /
177 if (!(skbdesc->flags & SKBDESC_DESC_IN_SKB))
178 skb_put_data(skbcopy, skbdesc->desc, skbdesc->desc_len);
179 skb_put_data(skbcopy, skb->data, skb->len);
181 skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
182 wake_up_interruptible(&intf->frame_dump_waitqueue);
185 * Verify that the file has not been closed while we were working.
187 if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
188 skb_queue_purge(&intf->frame_dump_skbqueue);
190 EXPORT_SYMBOL_GPL(rt2x00debug_dump_frame);
192 static int rt2x00debug_file_open(struct inode *inode, struct file *file)
194 struct rt2x00debug_intf *intf = inode->i_private;
196 file->private_data = inode->i_private;
198 if (!try_module_get(intf->debug->owner))
204 static int rt2x00debug_file_release(struct inode *inode, struct file *file)
206 struct rt2x00debug_intf *intf = file->private_data;
208 module_put(intf->debug->owner);
213 static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
215 struct rt2x00debug_intf *intf = inode->i_private;
218 retval = rt2x00debug_file_open(inode, file);
222 if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
223 rt2x00debug_file_release(inode, file);
230 static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
232 struct rt2x00debug_intf *intf = inode->i_private;
234 skb_queue_purge(&intf->frame_dump_skbqueue);
236 clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);
238 return rt2x00debug_file_release(inode, file);
241 static ssize_t rt2x00debug_read_queue_dump(struct file *file,
246 struct rt2x00debug_intf *intf = file->private_data;
251 if (file->f_flags & O_NONBLOCK)
255 wait_event_interruptible(intf->frame_dump_waitqueue,
257 skb_dequeue(&intf->frame_dump_skbqueue)));
261 status = min_t(size_t, skb->len, length);
262 if (copy_to_user(buf, skb->data, status)) {
275 static __poll_t rt2x00debug_poll_queue_dump(struct file *file,
278 struct rt2x00debug_intf *intf = file->private_data;
280 poll_wait(file, &intf->frame_dump_waitqueue, wait);
282 if (!skb_queue_empty(&intf->frame_dump_skbqueue))
283 return EPOLLOUT | EPOLLWRNORM;
288 static const struct file_operations rt2x00debug_fop_queue_dump = {
289 .owner = THIS_MODULE,
290 .read = rt2x00debug_read_queue_dump,
291 .poll = rt2x00debug_poll_queue_dump,
292 .open = rt2x00debug_open_queue_dump,
293 .release = rt2x00debug_release_queue_dump,
294 .llseek = default_llseek,
297 static ssize_t rt2x00debug_read_queue_stats(struct file *file,
302 struct rt2x00debug_intf *intf = file->private_data;
303 struct data_queue *queue;
304 unsigned long irqflags;
305 unsigned int lines = 1 + intf->rt2x00dev->data_queues;
313 data = kcalloc(lines, MAX_LINE_LENGTH, GFP_KERNEL);
318 sprintf(data, "qid\tflags\t\tcount\tlimit\tlength\tindex\tdma done\tdone\n");
320 queue_for_each(intf->rt2x00dev, queue) {
321 spin_lock_irqsave(&queue->index_lock, irqflags);
323 temp += sprintf(temp, "%d\t0x%.8x\t%d\t%d\t%d\t%d\t%d\t\t%d\n",
324 queue->qid, (unsigned int)queue->flags,
325 queue->count, queue->limit, queue->length,
326 queue->index[Q_INDEX],
327 queue->index[Q_INDEX_DMA_DONE],
328 queue->index[Q_INDEX_DONE]);
330 spin_unlock_irqrestore(&queue->index_lock, irqflags);
334 size = min(size, length);
336 if (copy_to_user(buf, data, size)) {
347 static const struct file_operations rt2x00debug_fop_queue_stats = {
348 .owner = THIS_MODULE,
349 .read = rt2x00debug_read_queue_stats,
350 .open = rt2x00debug_file_open,
351 .release = rt2x00debug_file_release,
352 .llseek = default_llseek,
355 #ifdef CONFIG_RT2X00_LIB_CRYPTO
356 static ssize_t rt2x00debug_read_crypto_stats(struct file *file,
361 struct rt2x00debug_intf *intf = file->private_data;
362 static const char * const name[] = { "WEP64", "WEP128", "TKIP", "AES" };
371 data = kcalloc(1 + CIPHER_MAX, MAX_LINE_LENGTH, GFP_KERNEL);
376 temp += sprintf(data, "cipher\tsuccess\ticv err\tmic err\tkey err\n");
378 for (i = 0; i < CIPHER_MAX; i++) {
379 temp += sprintf(temp, "%s\t%lu\t%lu\t%lu\t%lu\n", name[i],
380 intf->crypto_stats[i].success,
381 intf->crypto_stats[i].icv_error,
382 intf->crypto_stats[i].mic_error,
383 intf->crypto_stats[i].key_error);
387 size = min(size, length);
389 if (copy_to_user(buf, data, size)) {
400 static const struct file_operations rt2x00debug_fop_crypto_stats = {
401 .owner = THIS_MODULE,
402 .read = rt2x00debug_read_crypto_stats,
403 .open = rt2x00debug_file_open,
404 .release = rt2x00debug_file_release,
405 .llseek = default_llseek,
409 #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
410 static ssize_t rt2x00debug_read_##__name(struct file *file, \
415 struct rt2x00debug_intf *intf = file->private_data; \
416 const struct rt2x00debug *debug = intf->debug; \
419 unsigned int index = intf->offset_##__name; \
425 if (index >= debug->__name.word_count) \
428 index += (debug->__name.word_base / \
429 debug->__name.word_size); \
431 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
432 index *= debug->__name.word_size; \
434 value = debug->__name.read(intf->rt2x00dev, index); \
436 size = sprintf(line, __format, value); \
438 return simple_read_from_buffer(buf, length, offset, line, size); \
441 #define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
442 static ssize_t rt2x00debug_write_##__name(struct file *file, \
443 const char __user *buf,\
447 struct rt2x00debug_intf *intf = file->private_data; \
448 const struct rt2x00debug *debug = intf->debug; \
451 unsigned int index = intf->offset_##__name; \
457 if (index >= debug->__name.word_count) \
460 if (length > sizeof(line)) \
463 if (copy_from_user(line, buf, length)) \
467 size = strlen(line); \
468 value = simple_strtoul(line, NULL, 0); \
470 index += (debug->__name.word_base / \
471 debug->__name.word_size); \
473 if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
474 index *= debug->__name.word_size; \
476 debug->__name.write(intf->rt2x00dev, index, value); \
482 #define RT2X00DEBUGFS_OPS(__name, __format, __type) \
483 RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
484 RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
486 static const struct file_operations rt2x00debug_fop_##__name = {\
487 .owner = THIS_MODULE, \
488 .read = rt2x00debug_read_##__name, \
489 .write = rt2x00debug_write_##__name, \
490 .open = rt2x00debug_file_open, \
491 .release = rt2x00debug_file_release, \
492 .llseek = generic_file_llseek, \
495 RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
496 RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
497 RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
498 RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
499 RT2X00DEBUGFS_OPS(rfcsr, "0x%.2x\n", u8);
501 static ssize_t rt2x00debug_read_dev_flags(struct file *file,
506 struct rt2x00debug_intf *intf = file->private_data;
513 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
515 return simple_read_from_buffer(buf, length, offset, line, size);
518 static const struct file_operations rt2x00debug_fop_dev_flags = {
519 .owner = THIS_MODULE,
520 .read = rt2x00debug_read_dev_flags,
521 .open = rt2x00debug_file_open,
522 .release = rt2x00debug_file_release,
523 .llseek = default_llseek,
526 static ssize_t rt2x00debug_read_cap_flags(struct file *file,
531 struct rt2x00debug_intf *intf = file->private_data;
538 size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->cap_flags);
540 return simple_read_from_buffer(buf, length, offset, line, size);
543 static const struct file_operations rt2x00debug_fop_cap_flags = {
544 .owner = THIS_MODULE,
545 .read = rt2x00debug_read_cap_flags,
546 .open = rt2x00debug_file_open,
547 .release = rt2x00debug_file_release,
548 .llseek = default_llseek,
551 static ssize_t rt2x00debug_write_restart_hw(struct file *file,
552 const char __user *buf,
556 struct rt2x00debug_intf *intf = file->private_data;
557 struct rt2x00_dev *rt2x00dev = intf->rt2x00dev;
558 static unsigned long last_reset = INITIAL_JIFFIES;
560 if (!rt2x00_has_cap_restart_hw(rt2x00dev))
563 if (time_before(jiffies, last_reset + msecs_to_jiffies(2000)))
566 last_reset = jiffies;
568 ieee80211_restart_hw(rt2x00dev->hw);
572 static const struct file_operations rt2x00debug_restart_hw = {
573 .owner = THIS_MODULE,
574 .write = rt2x00debug_write_restart_hw,
576 .llseek = generic_file_llseek,
579 static void rt2x00debug_create_file_driver(const char *name,
580 struct rt2x00debug_intf *intf,
581 struct debugfs_blob_wrapper *blob)
585 data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
590 data += sprintf(data, "driver:\t%s\n", intf->rt2x00dev->ops->name);
591 data += sprintf(data, "version:\t%s\n", DRV_VERSION);
592 blob->size = strlen(blob->data);
594 debugfs_create_blob(name, 0400, intf->driver_folder, blob);
597 static void rt2x00debug_create_file_chipset(const char *name,
598 struct rt2x00debug_intf *intf,
599 struct debugfs_blob_wrapper *blob)
601 const struct rt2x00debug *debug = intf->debug;
604 data = kzalloc(9 * MAX_LINE_LENGTH, GFP_KERNEL);
609 data += sprintf(data, "rt chip:\t%04x\n", intf->rt2x00dev->chip.rt);
610 data += sprintf(data, "rf chip:\t%04x\n", intf->rt2x00dev->chip.rf);
611 data += sprintf(data, "revision:\t%04x\n", intf->rt2x00dev->chip.rev);
612 data += sprintf(data, "\n");
613 data += sprintf(data, "register\tbase\twords\twordsize\n");
614 #define RT2X00DEBUGFS_SPRINTF_REGISTER(__name) \
616 if (debug->__name.read) \
617 data += sprintf(data, __stringify(__name) \
619 debug->__name.word_base, \
620 debug->__name.word_count, \
621 debug->__name.word_size); \
623 RT2X00DEBUGFS_SPRINTF_REGISTER(csr);
624 RT2X00DEBUGFS_SPRINTF_REGISTER(eeprom);
625 RT2X00DEBUGFS_SPRINTF_REGISTER(bbp);
626 RT2X00DEBUGFS_SPRINTF_REGISTER(rf);
627 RT2X00DEBUGFS_SPRINTF_REGISTER(rfcsr);
628 #undef RT2X00DEBUGFS_SPRINTF_REGISTER
630 blob->size = strlen(blob->data);
632 debugfs_create_blob(name, 0400, intf->driver_folder, blob);
635 void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
637 const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
638 struct rt2x00debug_intf *intf;
639 struct dentry *queue_folder;
640 struct dentry *register_folder;
642 intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
644 rt2x00_err(rt2x00dev, "Failed to allocate debug handler\n");
649 intf->rt2x00dev = rt2x00dev;
650 rt2x00dev->debugfs_intf = intf;
652 intf->driver_folder =
653 debugfs_create_dir(intf->rt2x00dev->ops->name,
654 rt2x00dev->hw->wiphy->debugfsdir);
656 rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
657 rt2x00debug_create_file_chipset("chipset", intf, &intf->chipset_blob);
658 debugfs_create_file("dev_flags", 0400, intf->driver_folder, intf,
659 &rt2x00debug_fop_dev_flags);
660 debugfs_create_file("cap_flags", 0400, intf->driver_folder, intf,
661 &rt2x00debug_fop_cap_flags);
662 debugfs_create_file("restart_hw", 0200, intf->driver_folder, intf,
663 &rt2x00debug_restart_hw);
665 register_folder = debugfs_create_dir("register", intf->driver_folder);
667 #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
669 if (debug->__name.read) { \
670 debugfs_create_u32(__stringify(__name) "_offset", 0600, \
672 &(__intf)->offset_##__name); \
674 debugfs_create_file(__stringify(__name) "_value", 0600, \
675 register_folder, (__intf), \
676 &rt2x00debug_fop_##__name); \
680 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
681 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
682 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
683 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
684 RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rfcsr);
686 #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
688 queue_folder = debugfs_create_dir("queue", intf->driver_folder);
690 debugfs_create_file("dump", 0400, queue_folder, intf,
691 &rt2x00debug_fop_queue_dump);
693 skb_queue_head_init(&intf->frame_dump_skbqueue);
694 init_waitqueue_head(&intf->frame_dump_waitqueue);
696 debugfs_create_file("queue", 0400, queue_folder, intf,
697 &rt2x00debug_fop_queue_stats);
699 #ifdef CONFIG_RT2X00_LIB_CRYPTO
700 if (rt2x00_has_cap_hw_crypto(rt2x00dev))
701 debugfs_create_file("crypto", 0444, queue_folder, intf,
702 &rt2x00debug_fop_crypto_stats);
708 void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
710 struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
715 skb_queue_purge(&intf->frame_dump_skbqueue);
717 debugfs_remove_recursive(intf->driver_folder);
718 kfree(intf->chipset_blob.data);
719 kfree(intf->driver_blob.data);
722 rt2x00dev->debugfs_intf = NULL;