1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019 Realtek Corporation
5 #include <linux/debugfs.h>
6 #include <linux/seq_file.h>
13 #ifdef CONFIG_RTW88_DEBUGFS
15 struct rtw_debugfs_priv {
16 struct rtw_dev *rtwdev;
17 int (*cb_read)(struct seq_file *m, void *v);
18 ssize_t (*cb_write)(struct file *filp, const char __user *buffer,
19 size_t count, loff_t *loff);
39 static int rtw_debugfs_single_show(struct seq_file *m, void *v)
41 struct rtw_debugfs_priv *debugfs_priv = m->private;
43 return debugfs_priv->cb_read(m, v);
46 static ssize_t rtw_debugfs_common_write(struct file *filp,
47 const char __user *buffer,
48 size_t count, loff_t *loff)
50 struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
52 return debugfs_priv->cb_write(filp, buffer, count, loff);
55 static ssize_t rtw_debugfs_single_write(struct file *filp,
56 const char __user *buffer,
57 size_t count, loff_t *loff)
59 struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
60 struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
62 return debugfs_priv->cb_write(filp, buffer, count, loff);
65 static int rtw_debugfs_single_open_rw(struct inode *inode, struct file *filp)
67 return single_open(filp, rtw_debugfs_single_show, inode->i_private);
70 static int rtw_debugfs_close(struct inode *inode, struct file *filp)
75 static const struct file_operations file_ops_single_r = {
77 .open = rtw_debugfs_single_open_rw,
80 .release = single_release,
83 static const struct file_operations file_ops_single_rw = {
85 .open = rtw_debugfs_single_open_rw,
86 .release = single_release,
89 .write = rtw_debugfs_single_write,
92 static const struct file_operations file_ops_common_write = {
94 .write = rtw_debugfs_common_write,
96 .release = rtw_debugfs_close,
99 static int rtw_debugfs_get_read_reg(struct seq_file *m, void *v)
101 struct rtw_debugfs_priv *debugfs_priv = m->private;
102 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
105 len = debugfs_priv->read_reg.len;
106 addr = debugfs_priv->read_reg.addr;
109 val = rtw_read8(rtwdev, addr);
110 seq_printf(m, "reg 0x%03x: 0x%02x\n", addr, val);
113 val = rtw_read16(rtwdev, addr);
114 seq_printf(m, "reg 0x%03x: 0x%04x\n", addr, val);
117 val = rtw_read32(rtwdev, addr);
118 seq_printf(m, "reg 0x%03x: 0x%08x\n", addr, val);
124 static int rtw_debugfs_get_rf_read(struct seq_file *m, void *v)
126 struct rtw_debugfs_priv *debugfs_priv = m->private;
127 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
131 path = debugfs_priv->rf_path;
132 addr = debugfs_priv->rf_addr;
133 mask = debugfs_priv->rf_mask;
135 val = rtw_read_rf(rtwdev, path, addr, mask);
137 seq_printf(m, "rf_read path:%d addr:0x%08x mask:0x%08x val=0x%08x\n",
138 path, addr, mask, val);
143 static int rtw_debugfs_copy_from_user(char tmp[], int size,
144 const char __user *buffer, size_t count,
152 tmp_len = (count > size - 1 ? size - 1 : count);
154 if (!buffer || copy_from_user(tmp, buffer, tmp_len))
162 static ssize_t rtw_debugfs_set_read_reg(struct file *filp,
163 const char __user *buffer,
164 size_t count, loff_t *loff)
166 struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
167 struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
168 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
173 rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 2);
175 num = sscanf(tmp, "%x %x", &addr, &len);
180 if (len != 1 && len != 2 && len != 4) {
181 rtw_warn(rtwdev, "read reg setting wrong len\n");
184 debugfs_priv->read_reg.addr = addr;
185 debugfs_priv->read_reg.len = len;
190 static int rtw_debugfs_get_dump_cam(struct seq_file *m, void *v)
192 struct rtw_debugfs_priv *debugfs_priv = m->private;
193 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
195 u32 hw_key_idx = debugfs_priv->cb_data << RTW_SEC_CAM_ENTRY_SHIFT;
196 u32 read_cmd = RTW_SEC_CMD_POLLING;
199 seq_printf(m, "cam entry%d\n", debugfs_priv->cb_data);
200 seq_puts(m, "0x0 0x1 0x2 0x3 ");
201 seq_puts(m, "0x4 0x5\n");
202 mutex_lock(&rtwdev->mutex);
203 for (i = 0; i <= 5; i++) {
204 command = read_cmd | (hw_key_idx + i);
205 rtw_write32(rtwdev, RTW_SEC_CMD_REG, command);
206 val = rtw_read32(rtwdev, RTW_SEC_READ_REG);
207 seq_printf(m, "%8.8x", val);
212 mutex_unlock(&rtwdev->mutex);
216 static int rtw_debugfs_get_rsvd_page(struct seq_file *m, void *v)
218 struct rtw_debugfs_priv *debugfs_priv = m->private;
219 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
220 u8 page_size = rtwdev->chip->page_size;
221 u32 buf_size = debugfs_priv->rsvd_page.page_num * page_size;
222 u32 offset = debugfs_priv->rsvd_page.page_offset * page_size;
227 buf = vzalloc(buf_size);
231 ret = rtw_dump_drv_rsvd_page(rtwdev, offset, buf_size, (u32 *)buf);
233 rtw_err(rtwdev, "failed to dump rsvd page\n");
238 for (i = 0 ; i < buf_size ; i += 8) {
239 if (i % page_size == 0)
240 seq_printf(m, "PAGE %d\n", (i + offset) / page_size);
241 seq_printf(m, "%2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
242 *(buf + i), *(buf + i + 1),
243 *(buf + i + 2), *(buf + i + 3),
244 *(buf + i + 4), *(buf + i + 5),
245 *(buf + i + 6), *(buf + i + 7));
252 static ssize_t rtw_debugfs_set_rsvd_page(struct file *filp,
253 const char __user *buffer,
254 size_t count, loff_t *loff)
256 struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
257 struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
258 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
260 u32 offset, page_num;
263 rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 2);
265 num = sscanf(tmp, "%d %d", &offset, &page_num);
268 rtw_warn(rtwdev, "invalid arguments\n");
272 debugfs_priv->rsvd_page.page_offset = offset;
273 debugfs_priv->rsvd_page.page_num = page_num;
278 static ssize_t rtw_debugfs_set_single_input(struct file *filp,
279 const char __user *buffer,
280 size_t count, loff_t *loff)
282 struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
283 struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
284 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
289 rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 1);
291 num = kstrtoint(tmp, 0, &input);
294 rtw_warn(rtwdev, "kstrtoint failed\n");
298 debugfs_priv->cb_data = input;
303 static ssize_t rtw_debugfs_set_write_reg(struct file *filp,
304 const char __user *buffer,
305 size_t count, loff_t *loff)
307 struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
308 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
313 rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 3);
315 /* write BB/MAC register */
316 num = sscanf(tmp, "%x %x %x", &addr, &val, &len);
323 rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
324 "reg write8 0x%03x: 0x%08x\n", addr, val);
325 rtw_write8(rtwdev, addr, (u8)val);
328 rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
329 "reg write16 0x%03x: 0x%08x\n", addr, val);
330 rtw_write16(rtwdev, addr, (u16)val);
333 rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
334 "reg write32 0x%03x: 0x%08x\n", addr, val);
335 rtw_write32(rtwdev, addr, (u32)val);
338 rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
339 "error write length = %d\n", len);
346 static ssize_t rtw_debugfs_set_rf_write(struct file *filp,
347 const char __user *buffer,
348 size_t count, loff_t *loff)
350 struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
351 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
353 u32 path, addr, mask, val;
356 rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 4);
358 num = sscanf(tmp, "%x %x %x %x", &path, &addr, &mask, &val);
361 rtw_warn(rtwdev, "invalid args, [path] [addr] [mask] [val]\n");
365 rtw_write_rf(rtwdev, path, addr, mask, val);
366 rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
367 "write_rf path:%d addr:0x%08x mask:0x%08x, val:0x%08x\n",
368 path, addr, mask, val);
373 static ssize_t rtw_debugfs_set_rf_read(struct file *filp,
374 const char __user *buffer,
375 size_t count, loff_t *loff)
377 struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
378 struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
379 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
381 u32 path, addr, mask;
384 rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 3);
386 num = sscanf(tmp, "%x %x %x", &path, &addr, &mask);
389 rtw_warn(rtwdev, "invalid args, [path] [addr] [mask] [val]\n");
393 debugfs_priv->rf_path = path;
394 debugfs_priv->rf_addr = addr;
395 debugfs_priv->rf_mask = mask;
400 static int rtw_debug_get_mac_page(struct seq_file *m, void *v)
402 struct rtw_debugfs_priv *debugfs_priv = m->private;
403 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
405 u32 page = debugfs_priv->cb_data;
409 val = rtw_read32(rtwdev, debugfs_priv->cb_data);
410 for (n = 0; n <= max; ) {
411 seq_printf(m, "\n%8.8x ", n + page);
412 for (i = 0; i < 4 && n <= max; i++, n += 4)
413 seq_printf(m, "%8.8x ",
414 rtw_read32(rtwdev, (page | n)));
420 static int rtw_debug_get_bb_page(struct seq_file *m, void *v)
422 struct rtw_debugfs_priv *debugfs_priv = m->private;
423 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
425 u32 page = debugfs_priv->cb_data;
429 val = rtw_read32(rtwdev, debugfs_priv->cb_data);
430 for (n = 0; n <= max; ) {
431 seq_printf(m, "\n%8.8x ", n + page);
432 for (i = 0; i < 4 && n <= max; i++, n += 4)
433 seq_printf(m, "%8.8x ",
434 rtw_read32(rtwdev, (page | n)));
440 static int rtw_debug_get_rf_dump(struct seq_file *m, void *v)
442 struct rtw_debugfs_priv *debugfs_priv = m->private;
443 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
444 u32 addr, offset, data;
447 for (path = 0; path < rtwdev->hal.rf_path_num; path++) {
448 seq_printf(m, "RF path:%d\n", path);
449 for (addr = 0; addr < 0x100; addr += 4) {
450 seq_printf(m, "%8.8x ", addr);
451 for (offset = 0; offset < 4; offset++) {
452 data = rtw_read_rf(rtwdev, path, addr + offset,
454 seq_printf(m, "%8.8x ", data);
464 static void rtw_print_cck_rate_txt(struct seq_file *m, u8 rate)
466 static const char * const
467 cck_rate[] = {"1M", "2M", "5.5M", "11M"};
468 u8 idx = rate - DESC_RATE1M;
470 seq_printf(m, " CCK_%-5s", cck_rate[idx]);
473 static void rtw_print_ofdm_rate_txt(struct seq_file *m, u8 rate)
475 static const char * const
476 ofdm_rate[] = {"6M", "9M", "12M", "18M", "24M", "36M", "48M", "54M"};
477 u8 idx = rate - DESC_RATE6M;
479 seq_printf(m, " OFDM_%-4s", ofdm_rate[idx]);
482 static void rtw_print_ht_rate_txt(struct seq_file *m, u8 rate)
484 u8 mcs_n = rate - DESC_RATEMCS0;
486 seq_printf(m, " MCS%-6u", mcs_n);
489 static void rtw_print_vht_rate_txt(struct seq_file *m, u8 rate)
491 u8 idx = rate - DESC_RATEVHT1SS_MCS0;
494 /* n spatial stream */
498 seq_printf(m, " VHT%uSMCS%u", n_ss, mcs_n);
501 static int rtw_debugfs_get_tx_pwr_tbl(struct seq_file *m, void *v)
503 struct rtw_debugfs_priv *debugfs_priv = m->private;
504 struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
505 struct rtw_hal *hal = &rtwdev->hal;
506 void (*print_rate)(struct seq_file *, u8) = NULL;
508 struct rtw_power_params pwr_param = {0};
509 u8 bw = hal->current_band_width;
510 u8 ch = hal->current_channel;
511 u8 regd = rtwdev->regd.txpwr_regd;
513 seq_printf(m, "%-4s %-10s %-3s%6s %-4s %4s (%-4s %-4s)\n",
514 "path", "rate", "pwr", "", "base", "", "byr", "lmt");
516 mutex_lock(&hal->tx_power_mutex);
517 for (path = RF_PATH_A; path <= RF_PATH_B; path++) {
518 /* there is no CCK rates used in 5G */
519 if (hal->current_band_type == RTW_BAND_5G)
524 /* now, not support vht 3ss and vht 4ss*/
525 for (; rate <= DESC_RATEVHT2SS_MCS9; rate++) {
526 /* now, not support ht 3ss and ht 4ss*/
527 if (rate > DESC_RATEMCS15 &&
528 rate < DESC_RATEVHT1SS_MCS0)
532 case DESC_RATE1M...DESC_RATE11M:
533 print_rate = rtw_print_cck_rate_txt;
535 case DESC_RATE6M...DESC_RATE54M:
536 print_rate = rtw_print_ofdm_rate_txt;
538 case DESC_RATEMCS0...DESC_RATEMCS15:
539 print_rate = rtw_print_ht_rate_txt;
541 case DESC_RATEVHT1SS_MCS0...DESC_RATEVHT2SS_MCS9:
542 print_rate = rtw_print_vht_rate_txt;
549 rtw_get_tx_power_params(rtwdev, path, rate, bw,
550 ch, regd, &pwr_param);
552 seq_printf(m, "%4c ", path + 'A');
555 seq_printf(m, " %3u(0x%02x) %4u %4d (%4d %4d)\n",
556 hal->tx_pwr_tbl[path][rate],
557 hal->tx_pwr_tbl[path][rate],
559 min_t(s8, pwr_param.pwr_offset,
560 pwr_param.pwr_limit),
561 pwr_param.pwr_offset, pwr_param.pwr_limit);
565 mutex_unlock(&hal->tx_power_mutex);
570 #define rtw_debug_impl_mac(page, addr) \
571 static struct rtw_debugfs_priv rtw_debug_priv_mac_ ##page = { \
572 .cb_read = rtw_debug_get_mac_page, \
576 rtw_debug_impl_mac(0, 0x0000);
577 rtw_debug_impl_mac(1, 0x0100);
578 rtw_debug_impl_mac(2, 0x0200);
579 rtw_debug_impl_mac(3, 0x0300);
580 rtw_debug_impl_mac(4, 0x0400);
581 rtw_debug_impl_mac(5, 0x0500);
582 rtw_debug_impl_mac(6, 0x0600);
583 rtw_debug_impl_mac(7, 0x0700);
584 rtw_debug_impl_mac(10, 0x1000);
585 rtw_debug_impl_mac(11, 0x1100);
586 rtw_debug_impl_mac(12, 0x1200);
587 rtw_debug_impl_mac(13, 0x1300);
588 rtw_debug_impl_mac(14, 0x1400);
589 rtw_debug_impl_mac(15, 0x1500);
590 rtw_debug_impl_mac(16, 0x1600);
591 rtw_debug_impl_mac(17, 0x1700);
593 #define rtw_debug_impl_bb(page, addr) \
594 static struct rtw_debugfs_priv rtw_debug_priv_bb_ ##page = { \
595 .cb_read = rtw_debug_get_bb_page, \
599 rtw_debug_impl_bb(8, 0x0800);
600 rtw_debug_impl_bb(9, 0x0900);
601 rtw_debug_impl_bb(a, 0x0a00);
602 rtw_debug_impl_bb(b, 0x0b00);
603 rtw_debug_impl_bb(c, 0x0c00);
604 rtw_debug_impl_bb(d, 0x0d00);
605 rtw_debug_impl_bb(e, 0x0e00);
606 rtw_debug_impl_bb(f, 0x0f00);
607 rtw_debug_impl_bb(18, 0x1800);
608 rtw_debug_impl_bb(19, 0x1900);
609 rtw_debug_impl_bb(1a, 0x1a00);
610 rtw_debug_impl_bb(1b, 0x1b00);
611 rtw_debug_impl_bb(1c, 0x1c00);
612 rtw_debug_impl_bb(1d, 0x1d00);
613 rtw_debug_impl_bb(1e, 0x1e00);
614 rtw_debug_impl_bb(1f, 0x1f00);
615 rtw_debug_impl_bb(2c, 0x2c00);
616 rtw_debug_impl_bb(2d, 0x2d00);
617 rtw_debug_impl_bb(40, 0x4000);
618 rtw_debug_impl_bb(41, 0x4100);
620 static struct rtw_debugfs_priv rtw_debug_priv_rf_dump = {
621 .cb_read = rtw_debug_get_rf_dump,
624 static struct rtw_debugfs_priv rtw_debug_priv_tx_pwr_tbl = {
625 .cb_read = rtw_debugfs_get_tx_pwr_tbl,
628 static struct rtw_debugfs_priv rtw_debug_priv_write_reg = {
629 .cb_write = rtw_debugfs_set_write_reg,
632 static struct rtw_debugfs_priv rtw_debug_priv_rf_write = {
633 .cb_write = rtw_debugfs_set_rf_write,
636 static struct rtw_debugfs_priv rtw_debug_priv_rf_read = {
637 .cb_write = rtw_debugfs_set_rf_read,
638 .cb_read = rtw_debugfs_get_rf_read,
641 static struct rtw_debugfs_priv rtw_debug_priv_read_reg = {
642 .cb_write = rtw_debugfs_set_read_reg,
643 .cb_read = rtw_debugfs_get_read_reg,
646 static struct rtw_debugfs_priv rtw_debug_priv_dump_cam = {
647 .cb_write = rtw_debugfs_set_single_input,
648 .cb_read = rtw_debugfs_get_dump_cam,
651 static struct rtw_debugfs_priv rtw_debug_priv_rsvd_page = {
652 .cb_write = rtw_debugfs_set_rsvd_page,
653 .cb_read = rtw_debugfs_get_rsvd_page,
656 #define rtw_debugfs_add_core(name, mode, fopname, parent) \
658 rtw_debug_priv_ ##name.rtwdev = rtwdev; \
659 if (!debugfs_create_file(#name, mode, \
660 parent, &rtw_debug_priv_ ##name,\
661 &file_ops_ ##fopname)) \
662 pr_debug("Unable to initialize debugfs:%s\n", \
666 #define rtw_debugfs_add_w(name) \
667 rtw_debugfs_add_core(name, S_IFREG | 0222, common_write, debugfs_topdir)
668 #define rtw_debugfs_add_rw(name) \
669 rtw_debugfs_add_core(name, S_IFREG | 0666, single_rw, debugfs_topdir)
670 #define rtw_debugfs_add_r(name) \
671 rtw_debugfs_add_core(name, S_IFREG | 0444, single_r, debugfs_topdir)
673 void rtw_debugfs_init(struct rtw_dev *rtwdev)
675 struct dentry *debugfs_topdir;
677 debugfs_topdir = debugfs_create_dir("rtw88",
678 rtwdev->hw->wiphy->debugfsdir);
679 rtw_debugfs_add_w(write_reg);
680 rtw_debugfs_add_rw(read_reg);
681 rtw_debugfs_add_w(rf_write);
682 rtw_debugfs_add_rw(rf_read);
683 rtw_debugfs_add_rw(dump_cam);
684 rtw_debugfs_add_rw(rsvd_page);
685 rtw_debugfs_add_r(mac_0);
686 rtw_debugfs_add_r(mac_1);
687 rtw_debugfs_add_r(mac_2);
688 rtw_debugfs_add_r(mac_3);
689 rtw_debugfs_add_r(mac_4);
690 rtw_debugfs_add_r(mac_5);
691 rtw_debugfs_add_r(mac_6);
692 rtw_debugfs_add_r(mac_7);
693 rtw_debugfs_add_r(bb_8);
694 rtw_debugfs_add_r(bb_9);
695 rtw_debugfs_add_r(bb_a);
696 rtw_debugfs_add_r(bb_b);
697 rtw_debugfs_add_r(bb_c);
698 rtw_debugfs_add_r(bb_d);
699 rtw_debugfs_add_r(bb_e);
700 rtw_debugfs_add_r(bb_f);
701 rtw_debugfs_add_r(mac_10);
702 rtw_debugfs_add_r(mac_11);
703 rtw_debugfs_add_r(mac_12);
704 rtw_debugfs_add_r(mac_13);
705 rtw_debugfs_add_r(mac_14);
706 rtw_debugfs_add_r(mac_15);
707 rtw_debugfs_add_r(mac_16);
708 rtw_debugfs_add_r(mac_17);
709 rtw_debugfs_add_r(bb_18);
710 rtw_debugfs_add_r(bb_19);
711 rtw_debugfs_add_r(bb_1a);
712 rtw_debugfs_add_r(bb_1b);
713 rtw_debugfs_add_r(bb_1c);
714 rtw_debugfs_add_r(bb_1d);
715 rtw_debugfs_add_r(bb_1e);
716 rtw_debugfs_add_r(bb_1f);
717 if (rtwdev->chip->id == RTW_CHIP_TYPE_8822C) {
718 rtw_debugfs_add_r(bb_2c);
719 rtw_debugfs_add_r(bb_2d);
720 rtw_debugfs_add_r(bb_40);
721 rtw_debugfs_add_r(bb_41);
723 rtw_debugfs_add_r(rf_dump);
724 rtw_debugfs_add_r(tx_pwr_tbl);
727 #endif /* CONFIG_RTW88_DEBUGFS */
729 #ifdef CONFIG_RTW88_DEBUG
731 void __rtw_dbg(struct rtw_dev *rtwdev, enum rtw_debug_mask mask,
732 const char *fmt, ...)
734 struct va_format vaf = {
742 if (rtw_debug_mask & mask)
743 dev_printk(KERN_DEBUG, rtwdev->dev, "%pV", &vaf);
747 EXPORT_SYMBOL(__rtw_dbg);
749 #endif /* CONFIG_RTW88_DEBUG */