xref: /linux/drivers/net/wireless/realtek/rtw88/debug.c (revision cb8e59cc87201af93dfbb6c3dccc8fcad72a09c2)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019  Realtek Corporation
3  */
4 
5 #include <linux/debugfs.h>
6 #include <linux/seq_file.h>
7 #include "main.h"
8 #include "coex.h"
9 #include "sec.h"
10 #include "fw.h"
11 #include "debug.h"
12 #include "phy.h"
13 
14 #ifdef CONFIG_RTW88_DEBUGFS
15 
16 struct rtw_debugfs_priv {
17 	struct rtw_dev *rtwdev;
18 	int (*cb_read)(struct seq_file *m, void *v);
19 	ssize_t (*cb_write)(struct file *filp, const char __user *buffer,
20 			    size_t count, loff_t *loff);
21 	union {
22 		u32 cb_data;
23 		u8 *buf;
24 		struct {
25 			u32 page_offset;
26 			u32 page_num;
27 		} rsvd_page;
28 		struct {
29 			u8 rf_path;
30 			u32 rf_addr;
31 			u32 rf_mask;
32 		};
33 		struct {
34 			u32 addr;
35 			u32 len;
36 		} read_reg;
37 	};
38 };
39 
40 static int rtw_debugfs_single_show(struct seq_file *m, void *v)
41 {
42 	struct rtw_debugfs_priv *debugfs_priv = m->private;
43 
44 	return debugfs_priv->cb_read(m, v);
45 }
46 
47 static ssize_t rtw_debugfs_common_write(struct file *filp,
48 					const char __user *buffer,
49 					size_t count, loff_t *loff)
50 {
51 	struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
52 
53 	return debugfs_priv->cb_write(filp, buffer, count, loff);
54 }
55 
56 static ssize_t rtw_debugfs_single_write(struct file *filp,
57 					const char __user *buffer,
58 					size_t count, loff_t *loff)
59 {
60 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
61 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
62 
63 	return debugfs_priv->cb_write(filp, buffer, count, loff);
64 }
65 
66 static int rtw_debugfs_single_open_rw(struct inode *inode, struct file *filp)
67 {
68 	return single_open(filp, rtw_debugfs_single_show, inode->i_private);
69 }
70 
71 static int rtw_debugfs_close(struct inode *inode, struct file *filp)
72 {
73 	return 0;
74 }
75 
76 static const struct file_operations file_ops_single_r = {
77 	.owner = THIS_MODULE,
78 	.open = rtw_debugfs_single_open_rw,
79 	.read = seq_read,
80 	.llseek = seq_lseek,
81 	.release = single_release,
82 };
83 
84 static const struct file_operations file_ops_single_rw = {
85 	.owner = THIS_MODULE,
86 	.open = rtw_debugfs_single_open_rw,
87 	.release = single_release,
88 	.read = seq_read,
89 	.llseek = seq_lseek,
90 	.write = rtw_debugfs_single_write,
91 };
92 
93 static const struct file_operations file_ops_common_write = {
94 	.owner = THIS_MODULE,
95 	.write = rtw_debugfs_common_write,
96 	.open = simple_open,
97 	.release = rtw_debugfs_close,
98 };
99 
100 static int rtw_debugfs_get_read_reg(struct seq_file *m, void *v)
101 {
102 	struct rtw_debugfs_priv *debugfs_priv = m->private;
103 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
104 	u32 val, len, addr;
105 
106 	len = debugfs_priv->read_reg.len;
107 	addr = debugfs_priv->read_reg.addr;
108 	switch (len) {
109 	case 1:
110 		val = rtw_read8(rtwdev, addr);
111 		seq_printf(m, "reg 0x%03x: 0x%02x\n", addr, val);
112 		break;
113 	case 2:
114 		val = rtw_read16(rtwdev, addr);
115 		seq_printf(m, "reg 0x%03x: 0x%04x\n", addr, val);
116 		break;
117 	case 4:
118 		val = rtw_read32(rtwdev, addr);
119 		seq_printf(m, "reg 0x%03x: 0x%08x\n", addr, val);
120 		break;
121 	}
122 	return 0;
123 }
124 
125 static int rtw_debugfs_get_rf_read(struct seq_file *m, void *v)
126 {
127 	struct rtw_debugfs_priv *debugfs_priv = m->private;
128 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
129 	u32 val, addr, mask;
130 	u8 path;
131 
132 	path = debugfs_priv->rf_path;
133 	addr = debugfs_priv->rf_addr;
134 	mask = debugfs_priv->rf_mask;
135 
136 	val = rtw_read_rf(rtwdev, path, addr, mask);
137 
138 	seq_printf(m, "rf_read path:%d addr:0x%08x mask:0x%08x val=0x%08x\n",
139 		   path, addr, mask, val);
140 
141 	return 0;
142 }
143 
144 static int rtw_debugfs_copy_from_user(char tmp[], int size,
145 				      const char __user *buffer, size_t count,
146 				      int num)
147 {
148 	int tmp_len;
149 
150 	if (count < num)
151 		return -EFAULT;
152 
153 	tmp_len = (count > size - 1 ? size - 1 : count);
154 
155 	if (!buffer || copy_from_user(tmp, buffer, tmp_len))
156 		return count;
157 
158 	tmp[tmp_len] = '\0';
159 
160 	return 0;
161 }
162 
163 static ssize_t rtw_debugfs_set_read_reg(struct file *filp,
164 					const char __user *buffer,
165 					size_t count, loff_t *loff)
166 {
167 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
168 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
169 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
170 	char tmp[32 + 1];
171 	u32 addr, len;
172 	int num;
173 
174 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 2);
175 
176 	num = sscanf(tmp, "%x %x", &addr, &len);
177 
178 	if (num !=  2)
179 		return count;
180 
181 	if (len != 1 && len != 2 && len != 4) {
182 		rtw_warn(rtwdev, "read reg setting wrong len\n");
183 		return -EINVAL;
184 	}
185 	debugfs_priv->read_reg.addr = addr;
186 	debugfs_priv->read_reg.len = len;
187 
188 	return count;
189 }
190 
191 static int rtw_debugfs_get_dump_cam(struct seq_file *m, void *v)
192 {
193 	struct rtw_debugfs_priv *debugfs_priv = m->private;
194 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
195 	u32 val, command;
196 	u32 hw_key_idx = debugfs_priv->cb_data << RTW_SEC_CAM_ENTRY_SHIFT;
197 	u32 read_cmd = RTW_SEC_CMD_POLLING;
198 	int i;
199 
200 	seq_printf(m, "cam entry%d\n", debugfs_priv->cb_data);
201 	seq_puts(m, "0x0      0x1      0x2     0x3     ");
202 	seq_puts(m, "0x4     0x5\n");
203 	mutex_lock(&rtwdev->mutex);
204 	for (i = 0; i <= 5; i++) {
205 		command = read_cmd | (hw_key_idx + i);
206 		rtw_write32(rtwdev, RTW_SEC_CMD_REG, command);
207 		val = rtw_read32(rtwdev, RTW_SEC_READ_REG);
208 		seq_printf(m, "%8.8x", val);
209 		if (i < 2)
210 			seq_puts(m, " ");
211 	}
212 	seq_puts(m, "\n");
213 	mutex_unlock(&rtwdev->mutex);
214 	return 0;
215 }
216 
217 static int rtw_debugfs_get_rsvd_page(struct seq_file *m, void *v)
218 {
219 	struct rtw_debugfs_priv *debugfs_priv = m->private;
220 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
221 	u8 page_size = rtwdev->chip->page_size;
222 	u32 buf_size = debugfs_priv->rsvd_page.page_num * page_size;
223 	u32 offset = debugfs_priv->rsvd_page.page_offset * page_size;
224 	u8 *buf;
225 	int i;
226 	int ret;
227 
228 	buf = vzalloc(buf_size);
229 	if (!buf)
230 		return -ENOMEM;
231 
232 	ret = rtw_dump_drv_rsvd_page(rtwdev, offset, buf_size, (u32 *)buf);
233 	if (ret) {
234 		rtw_err(rtwdev, "failed to dump rsvd page\n");
235 		vfree(buf);
236 		return ret;
237 	}
238 
239 	for (i = 0 ; i < buf_size ; i += 8) {
240 		if (i % page_size == 0)
241 			seq_printf(m, "PAGE %d\n", (i + offset) / page_size);
242 		seq_printf(m, "%2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
243 			   *(buf + i), *(buf + i + 1),
244 			   *(buf + i + 2), *(buf + i + 3),
245 			   *(buf + i + 4), *(buf + i + 5),
246 			   *(buf + i + 6), *(buf + i + 7));
247 	}
248 	vfree(buf);
249 
250 	return 0;
251 }
252 
253 static ssize_t rtw_debugfs_set_rsvd_page(struct file *filp,
254 					 const char __user *buffer,
255 					 size_t count, loff_t *loff)
256 {
257 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
258 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
259 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
260 	char tmp[32 + 1];
261 	u32 offset, page_num;
262 	int num;
263 
264 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 2);
265 
266 	num = sscanf(tmp, "%d %d", &offset, &page_num);
267 
268 	if (num != 2) {
269 		rtw_warn(rtwdev, "invalid arguments\n");
270 		return num;
271 	}
272 
273 	debugfs_priv->rsvd_page.page_offset = offset;
274 	debugfs_priv->rsvd_page.page_num = page_num;
275 
276 	return count;
277 }
278 
279 static ssize_t rtw_debugfs_set_single_input(struct file *filp,
280 					    const char __user *buffer,
281 					    size_t count, loff_t *loff)
282 {
283 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
284 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
285 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
286 	char tmp[32 + 1];
287 	u32 input;
288 	int num;
289 
290 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 1);
291 
292 	num = kstrtoint(tmp, 0, &input);
293 
294 	if (num) {
295 		rtw_warn(rtwdev, "kstrtoint failed\n");
296 		return num;
297 	}
298 
299 	debugfs_priv->cb_data = input;
300 
301 	return count;
302 }
303 
304 static ssize_t rtw_debugfs_set_write_reg(struct file *filp,
305 					 const char __user *buffer,
306 					 size_t count, loff_t *loff)
307 {
308 	struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
309 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
310 	char tmp[32 + 1];
311 	u32 addr, val, len;
312 	int num;
313 
314 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 3);
315 
316 	/* write BB/MAC register */
317 	num = sscanf(tmp, "%x %x %x", &addr, &val, &len);
318 
319 	if (num !=  3)
320 		return count;
321 
322 	switch (len) {
323 	case 1:
324 		rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
325 			"reg write8 0x%03x: 0x%08x\n", addr, val);
326 		rtw_write8(rtwdev, addr, (u8)val);
327 		break;
328 	case 2:
329 		rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
330 			"reg write16 0x%03x: 0x%08x\n", addr, val);
331 		rtw_write16(rtwdev, addr, (u16)val);
332 		break;
333 	case 4:
334 		rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
335 			"reg write32 0x%03x: 0x%08x\n", addr, val);
336 		rtw_write32(rtwdev, addr, (u32)val);
337 		break;
338 	default:
339 		rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
340 			"error write length = %d\n", len);
341 		break;
342 	}
343 
344 	return count;
345 }
346 
347 static ssize_t rtw_debugfs_set_rf_write(struct file *filp,
348 					const char __user *buffer,
349 					size_t count, loff_t *loff)
350 {
351 	struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
352 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
353 	char tmp[32 + 1];
354 	u32 path, addr, mask, val;
355 	int num;
356 
357 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 4);
358 
359 	num = sscanf(tmp, "%x %x %x %x", &path, &addr, &mask, &val);
360 
361 	if (num !=  4) {
362 		rtw_warn(rtwdev, "invalid args, [path] [addr] [mask] [val]\n");
363 		return count;
364 	}
365 
366 	rtw_write_rf(rtwdev, path, addr, mask, val);
367 	rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
368 		"write_rf path:%d addr:0x%08x mask:0x%08x, val:0x%08x\n",
369 		path, addr, mask, val);
370 
371 	return count;
372 }
373 
374 static ssize_t rtw_debugfs_set_rf_read(struct file *filp,
375 				       const char __user *buffer,
376 				       size_t count, loff_t *loff)
377 {
378 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
379 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
380 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
381 	char tmp[32 + 1];
382 	u32 path, addr, mask;
383 	int num;
384 
385 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 3);
386 
387 	num = sscanf(tmp, "%x %x %x", &path, &addr, &mask);
388 
389 	if (num !=  3) {
390 		rtw_warn(rtwdev, "invalid args, [path] [addr] [mask] [val]\n");
391 		return count;
392 	}
393 
394 	debugfs_priv->rf_path = path;
395 	debugfs_priv->rf_addr = addr;
396 	debugfs_priv->rf_mask = mask;
397 
398 	return count;
399 }
400 
401 static int rtw_debug_get_mac_page(struct seq_file *m, void *v)
402 {
403 	struct rtw_debugfs_priv *debugfs_priv = m->private;
404 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
405 	u32 val;
406 	u32 page = debugfs_priv->cb_data;
407 	int i, n;
408 	int max = 0xff;
409 
410 	val = rtw_read32(rtwdev, debugfs_priv->cb_data);
411 	for (n = 0; n <= max; ) {
412 		seq_printf(m, "\n%8.8x  ", n + page);
413 		for (i = 0; i < 4 && n <= max; i++, n += 4)
414 			seq_printf(m, "%8.8x    ",
415 				   rtw_read32(rtwdev, (page | n)));
416 	}
417 	seq_puts(m, "\n");
418 	return 0;
419 }
420 
421 static int rtw_debug_get_bb_page(struct seq_file *m, void *v)
422 {
423 	struct rtw_debugfs_priv *debugfs_priv = m->private;
424 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
425 	u32 val;
426 	u32 page = debugfs_priv->cb_data;
427 	int i, n;
428 	int max = 0xff;
429 
430 	val = rtw_read32(rtwdev, debugfs_priv->cb_data);
431 	for (n = 0; n <= max; ) {
432 		seq_printf(m, "\n%8.8x  ", n + page);
433 		for (i = 0; i < 4 && n <= max; i++, n += 4)
434 			seq_printf(m, "%8.8x    ",
435 				   rtw_read32(rtwdev, (page | n)));
436 	}
437 	seq_puts(m, "\n");
438 	return 0;
439 }
440 
441 static int rtw_debug_get_rf_dump(struct seq_file *m, void *v)
442 {
443 	struct rtw_debugfs_priv *debugfs_priv = m->private;
444 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
445 	u32 addr, offset, data;
446 	u8 path;
447 
448 	for (path = 0; path < rtwdev->hal.rf_path_num; path++) {
449 		seq_printf(m, "RF path:%d\n", path);
450 		for (addr = 0; addr < 0x100; addr += 4) {
451 			seq_printf(m, "%8.8x  ", addr);
452 			for (offset = 0; offset < 4; offset++) {
453 				data = rtw_read_rf(rtwdev, path, addr + offset,
454 						   0xffffffff);
455 				seq_printf(m, "%8.8x    ", data);
456 			}
457 			seq_puts(m, "\n");
458 		}
459 		seq_puts(m, "\n");
460 	}
461 
462 	return 0;
463 }
464 
465 static void rtw_print_cck_rate_txt(struct seq_file *m, u8 rate)
466 {
467 	static const char * const
468 	cck_rate[] = {"1M", "2M", "5.5M", "11M"};
469 	u8 idx = rate - DESC_RATE1M;
470 
471 	seq_printf(m, " CCK_%-5s", cck_rate[idx]);
472 }
473 
474 static void rtw_print_ofdm_rate_txt(struct seq_file *m, u8 rate)
475 {
476 	static const char * const
477 	ofdm_rate[] = {"6M", "9M", "12M", "18M", "24M", "36M", "48M", "54M"};
478 	u8 idx = rate - DESC_RATE6M;
479 
480 	seq_printf(m, " OFDM_%-4s", ofdm_rate[idx]);
481 }
482 
483 static void rtw_print_ht_rate_txt(struct seq_file *m, u8 rate)
484 {
485 	u8 mcs_n = rate - DESC_RATEMCS0;
486 
487 	seq_printf(m, " MCS%-6u", mcs_n);
488 }
489 
490 static void rtw_print_vht_rate_txt(struct seq_file *m, u8 rate)
491 {
492 	u8 idx = rate - DESC_RATEVHT1SS_MCS0;
493 	u8 n_ss, mcs_n;
494 
495 	/* n spatial stream */
496 	n_ss = 1 + idx / 10;
497 	/* MCS n */
498 	mcs_n = idx % 10;
499 	seq_printf(m, " VHT%uSMCS%u", n_ss, mcs_n);
500 }
501 
502 static void rtw_print_rate(struct seq_file *m, u8 rate)
503 {
504 	switch (rate) {
505 	case DESC_RATE1M...DESC_RATE11M:
506 		rtw_print_cck_rate_txt(m, rate);
507 		break;
508 	case DESC_RATE6M...DESC_RATE54M:
509 		rtw_print_ofdm_rate_txt(m, rate);
510 		break;
511 	case DESC_RATEMCS0...DESC_RATEMCS15:
512 		rtw_print_ht_rate_txt(m, rate);
513 		break;
514 	case DESC_RATEVHT1SS_MCS0...DESC_RATEVHT2SS_MCS9:
515 		rtw_print_vht_rate_txt(m, rate);
516 		break;
517 	default:
518 		seq_printf(m, " Unknown rate=0x%x\n", rate);
519 		break;
520 	}
521 }
522 
523 static int rtw_debugfs_get_tx_pwr_tbl(struct seq_file *m, void *v)
524 {
525 	struct rtw_debugfs_priv *debugfs_priv = m->private;
526 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
527 	struct rtw_hal *hal = &rtwdev->hal;
528 	u8 path, rate;
529 	struct rtw_power_params pwr_param = {0};
530 	u8 bw = hal->current_band_width;
531 	u8 ch = hal->current_channel;
532 	u8 regd = rtwdev->regd.txpwr_regd;
533 
534 	seq_printf(m, "%-4s %-10s %-3s%6s %-4s %4s (%-4s %-4s) %-4s\n",
535 		   "path", "rate", "pwr", "", "base", "", "byr", "lmt", "rem");
536 
537 	mutex_lock(&hal->tx_power_mutex);
538 	for (path = RF_PATH_A; path <= RF_PATH_B; path++) {
539 		/* there is no CCK rates used in 5G */
540 		if (hal->current_band_type == RTW_BAND_5G)
541 			rate = DESC_RATE6M;
542 		else
543 			rate = DESC_RATE1M;
544 
545 		/* now, not support vht 3ss and vht 4ss*/
546 		for (; rate <= DESC_RATEVHT2SS_MCS9; rate++) {
547 			/* now, not support ht 3ss and ht 4ss*/
548 			if (rate > DESC_RATEMCS15 &&
549 			    rate < DESC_RATEVHT1SS_MCS0)
550 				continue;
551 
552 			rtw_get_tx_power_params(rtwdev, path, rate, bw,
553 						ch, regd, &pwr_param);
554 
555 			seq_printf(m, "%4c ", path + 'A');
556 			rtw_print_rate(m, rate);
557 			seq_printf(m, " %3u(0x%02x) %4u %4d (%4d %4d) %4d\n",
558 				   hal->tx_pwr_tbl[path][rate],
559 				   hal->tx_pwr_tbl[path][rate],
560 				   pwr_param.pwr_base,
561 				   min_t(s8, pwr_param.pwr_offset,
562 					 pwr_param.pwr_limit),
563 				   pwr_param.pwr_offset, pwr_param.pwr_limit,
564 				   pwr_param.pwr_remnant);
565 		}
566 	}
567 
568 	mutex_unlock(&hal->tx_power_mutex);
569 
570 	return 0;
571 }
572 
573 static int rtw_debugfs_get_phy_info(struct seq_file *m, void *v)
574 {
575 	struct rtw_debugfs_priv *debugfs_priv = m->private;
576 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
577 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
578 	struct rtw_traffic_stats *stats = &rtwdev->stats;
579 	struct rtw_pkt_count *last_cnt = &dm_info->last_pkt_count;
580 	struct rtw_efuse *efuse = &rtwdev->efuse;
581 	struct ewma_evm *ewma_evm = dm_info->ewma_evm;
582 	struct ewma_snr *ewma_snr = dm_info->ewma_snr;
583 	u8 ss, rate_id;
584 
585 	seq_puts(m, "==========[Common Info]========\n");
586 	seq_printf(m, "Is link = %c\n", rtw_is_assoc(rtwdev) ? 'Y' : 'N');
587 	seq_printf(m, "Current CH(fc) = %u\n", rtwdev->hal.current_channel);
588 	seq_printf(m, "Current BW = %u\n", rtwdev->hal.current_band_width);
589 	seq_printf(m, "Current IGI = 0x%x\n", dm_info->igi_history[0]);
590 	seq_printf(m, "TP {Tx, Rx} = {%u, %u}Mbps\n\n",
591 		   stats->tx_throughput, stats->rx_throughput);
592 
593 	seq_puts(m, "==========[Tx Phy Info]========\n");
594 	seq_puts(m, "[Tx Rate] = ");
595 	rtw_print_rate(m, dm_info->tx_rate);
596 	seq_printf(m, "(0x%x)\n\n", dm_info->tx_rate);
597 
598 	seq_puts(m, "==========[Rx Phy Info]========\n");
599 	seq_printf(m, "[Rx Beacon Count] = %u\n", last_cnt->num_bcn_pkt);
600 	seq_puts(m, "[Rx Rate] = ");
601 	rtw_print_rate(m, dm_info->curr_rx_rate);
602 	seq_printf(m, "(0x%x)\n", dm_info->curr_rx_rate);
603 
604 	seq_puts(m, "[Rx Rate Count]:\n");
605 	seq_printf(m, " * CCK = {%u, %u, %u, %u}\n",
606 		   last_cnt->num_qry_pkt[DESC_RATE1M],
607 		   last_cnt->num_qry_pkt[DESC_RATE2M],
608 		   last_cnt->num_qry_pkt[DESC_RATE5_5M],
609 		   last_cnt->num_qry_pkt[DESC_RATE11M]);
610 
611 	seq_printf(m, " * OFDM = {%u, %u, %u, %u, %u, %u, %u, %u}\n",
612 		   last_cnt->num_qry_pkt[DESC_RATE6M],
613 		   last_cnt->num_qry_pkt[DESC_RATE9M],
614 		   last_cnt->num_qry_pkt[DESC_RATE12M],
615 		   last_cnt->num_qry_pkt[DESC_RATE18M],
616 		   last_cnt->num_qry_pkt[DESC_RATE24M],
617 		   last_cnt->num_qry_pkt[DESC_RATE36M],
618 		   last_cnt->num_qry_pkt[DESC_RATE48M],
619 		   last_cnt->num_qry_pkt[DESC_RATE54M]);
620 
621 	for (ss = 0; ss < efuse->hw_cap.nss; ss++) {
622 		rate_id = DESC_RATEMCS0 + ss * 8;
623 		seq_printf(m, " * HT_MCS[%u:%u] = {%u, %u, %u, %u, %u, %u, %u, %u}\n",
624 			   ss * 8, ss * 8 + 7,
625 			   last_cnt->num_qry_pkt[rate_id],
626 			   last_cnt->num_qry_pkt[rate_id + 1],
627 			   last_cnt->num_qry_pkt[rate_id + 2],
628 			   last_cnt->num_qry_pkt[rate_id + 3],
629 			   last_cnt->num_qry_pkt[rate_id + 4],
630 			   last_cnt->num_qry_pkt[rate_id + 5],
631 			   last_cnt->num_qry_pkt[rate_id + 6],
632 			   last_cnt->num_qry_pkt[rate_id + 7]);
633 	}
634 
635 	for (ss = 0; ss < efuse->hw_cap.nss; ss++) {
636 		rate_id = DESC_RATEVHT1SS_MCS0 + ss * 10;
637 		seq_printf(m, " * VHT_MCS-%uss MCS[0:9] = {%u, %u, %u, %u, %u, %u, %u, %u, %u, %u}\n",
638 			   ss + 1,
639 			   last_cnt->num_qry_pkt[rate_id],
640 			   last_cnt->num_qry_pkt[rate_id + 1],
641 			   last_cnt->num_qry_pkt[rate_id + 2],
642 			   last_cnt->num_qry_pkt[rate_id + 3],
643 			   last_cnt->num_qry_pkt[rate_id + 4],
644 			   last_cnt->num_qry_pkt[rate_id + 5],
645 			   last_cnt->num_qry_pkt[rate_id + 6],
646 			   last_cnt->num_qry_pkt[rate_id + 7],
647 			   last_cnt->num_qry_pkt[rate_id + 8],
648 			   last_cnt->num_qry_pkt[rate_id + 9]);
649 	}
650 
651 	seq_printf(m, "[RSSI(dBm)] = {%d, %d}\n",
652 		   dm_info->rssi[RF_PATH_A] - 100,
653 		   dm_info->rssi[RF_PATH_B] - 100);
654 	seq_printf(m, "[Rx EVM(dB)] = {-%d, -%d}\n",
655 		   dm_info->rx_evm_dbm[RF_PATH_A],
656 		   dm_info->rx_evm_dbm[RF_PATH_B]);
657 	seq_printf(m, "[Rx SNR] = {%d, %d}\n",
658 		   dm_info->rx_snr[RF_PATH_A],
659 		   dm_info->rx_snr[RF_PATH_B]);
660 	seq_printf(m, "[CFO_tail(KHz)] = {%d, %d}\n",
661 		   dm_info->cfo_tail[RF_PATH_A],
662 		   dm_info->cfo_tail[RF_PATH_B]);
663 
664 	if (dm_info->curr_rx_rate >= DESC_RATE11M) {
665 		seq_puts(m, "[Rx Average Status]:\n");
666 		seq_printf(m, " * OFDM, EVM: {-%d}, SNR: {%d}\n",
667 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_OFDM]),
668 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_OFDM_A]));
669 		seq_printf(m, " * 1SS, EVM: {-%d}, SNR: {%d}\n",
670 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_1SS]),
671 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_1SS_A]));
672 		seq_printf(m, " * 2SS, EVM: {-%d, -%d}, SNR: {%d, %d}\n",
673 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_2SS_A]),
674 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_2SS_B]),
675 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_2SS_A]),
676 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_2SS_B]));
677 	}
678 
679 	seq_puts(m, "[Rx Counter]:\n");
680 	seq_printf(m, " * CCA (CCK, OFDM, Total) = (%u, %u, %u)\n",
681 		   dm_info->cck_cca_cnt,
682 		   dm_info->ofdm_cca_cnt,
683 		   dm_info->total_cca_cnt);
684 	seq_printf(m, " * False Alarm (CCK, OFDM, Total) = (%u, %u, %u)\n",
685 		   dm_info->cck_fa_cnt,
686 		   dm_info->ofdm_fa_cnt,
687 		   dm_info->total_fa_cnt);
688 	seq_printf(m, " * CCK cnt (ok, err) = (%u, %u)\n",
689 		   dm_info->cck_ok_cnt, dm_info->cck_err_cnt);
690 	seq_printf(m, " * OFDM cnt (ok, err) = (%u, %u)\n",
691 		   dm_info->ofdm_ok_cnt, dm_info->ofdm_err_cnt);
692 	seq_printf(m, " * HT cnt (ok, err) = (%u, %u)\n",
693 		   dm_info->ht_ok_cnt, dm_info->ht_err_cnt);
694 	seq_printf(m, " * VHT cnt (ok, err) = (%u, %u)\n",
695 		   dm_info->vht_ok_cnt, dm_info->vht_err_cnt);
696 
697 	return 0;
698 }
699 
700 static int rtw_debugfs_get_coex_info(struct seq_file *m, void *v)
701 {
702 	struct rtw_debugfs_priv *debugfs_priv = m->private;
703 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
704 
705 	rtw_coex_display_coex_info(rtwdev, m);
706 
707 	return 0;
708 }
709 
710 static ssize_t rtw_debugfs_set_coex_enable(struct file *filp,
711 					   const char __user *buffer,
712 					   size_t count, loff_t *loff)
713 {
714 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
715 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
716 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
717 	struct rtw_coex *coex = &rtwdev->coex;
718 	char tmp[32 + 1];
719 	bool enable;
720 	int ret;
721 
722 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 1);
723 
724 	ret = kstrtobool(tmp, &enable);
725 	if (ret) {
726 		rtw_warn(rtwdev, "invalid arguments\n");
727 		return ret;
728 	}
729 
730 	mutex_lock(&rtwdev->mutex);
731 	coex->stop_dm = enable == 0;
732 	mutex_unlock(&rtwdev->mutex);
733 
734 	return count;
735 }
736 
737 static int rtw_debugfs_get_coex_enable(struct seq_file *m, void *v)
738 {
739 	struct rtw_debugfs_priv *debugfs_priv = m->private;
740 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
741 	struct rtw_coex *coex = &rtwdev->coex;
742 
743 	seq_printf(m, "coex mechanism %s\n",
744 		   coex->stop_dm ? "disabled" : "enabled");
745 
746 	return 0;
747 }
748 
749 #define rtw_debug_impl_mac(page, addr)				\
750 static struct rtw_debugfs_priv rtw_debug_priv_mac_ ##page = {	\
751 	.cb_read = rtw_debug_get_mac_page,			\
752 	.cb_data = addr,					\
753 }
754 
755 rtw_debug_impl_mac(0, 0x0000);
756 rtw_debug_impl_mac(1, 0x0100);
757 rtw_debug_impl_mac(2, 0x0200);
758 rtw_debug_impl_mac(3, 0x0300);
759 rtw_debug_impl_mac(4, 0x0400);
760 rtw_debug_impl_mac(5, 0x0500);
761 rtw_debug_impl_mac(6, 0x0600);
762 rtw_debug_impl_mac(7, 0x0700);
763 rtw_debug_impl_mac(10, 0x1000);
764 rtw_debug_impl_mac(11, 0x1100);
765 rtw_debug_impl_mac(12, 0x1200);
766 rtw_debug_impl_mac(13, 0x1300);
767 rtw_debug_impl_mac(14, 0x1400);
768 rtw_debug_impl_mac(15, 0x1500);
769 rtw_debug_impl_mac(16, 0x1600);
770 rtw_debug_impl_mac(17, 0x1700);
771 
772 #define rtw_debug_impl_bb(page, addr)			\
773 static struct rtw_debugfs_priv rtw_debug_priv_bb_ ##page = {	\
774 	.cb_read = rtw_debug_get_bb_page,			\
775 	.cb_data = addr,					\
776 }
777 
778 rtw_debug_impl_bb(8, 0x0800);
779 rtw_debug_impl_bb(9, 0x0900);
780 rtw_debug_impl_bb(a, 0x0a00);
781 rtw_debug_impl_bb(b, 0x0b00);
782 rtw_debug_impl_bb(c, 0x0c00);
783 rtw_debug_impl_bb(d, 0x0d00);
784 rtw_debug_impl_bb(e, 0x0e00);
785 rtw_debug_impl_bb(f, 0x0f00);
786 rtw_debug_impl_bb(18, 0x1800);
787 rtw_debug_impl_bb(19, 0x1900);
788 rtw_debug_impl_bb(1a, 0x1a00);
789 rtw_debug_impl_bb(1b, 0x1b00);
790 rtw_debug_impl_bb(1c, 0x1c00);
791 rtw_debug_impl_bb(1d, 0x1d00);
792 rtw_debug_impl_bb(1e, 0x1e00);
793 rtw_debug_impl_bb(1f, 0x1f00);
794 rtw_debug_impl_bb(2c, 0x2c00);
795 rtw_debug_impl_bb(2d, 0x2d00);
796 rtw_debug_impl_bb(40, 0x4000);
797 rtw_debug_impl_bb(41, 0x4100);
798 
799 static struct rtw_debugfs_priv rtw_debug_priv_rf_dump = {
800 	.cb_read = rtw_debug_get_rf_dump,
801 };
802 
803 static struct rtw_debugfs_priv rtw_debug_priv_tx_pwr_tbl = {
804 	.cb_read = rtw_debugfs_get_tx_pwr_tbl,
805 };
806 
807 static struct rtw_debugfs_priv rtw_debug_priv_write_reg = {
808 	.cb_write = rtw_debugfs_set_write_reg,
809 };
810 
811 static struct rtw_debugfs_priv rtw_debug_priv_rf_write = {
812 	.cb_write = rtw_debugfs_set_rf_write,
813 };
814 
815 static struct rtw_debugfs_priv rtw_debug_priv_rf_read = {
816 	.cb_write = rtw_debugfs_set_rf_read,
817 	.cb_read = rtw_debugfs_get_rf_read,
818 };
819 
820 static struct rtw_debugfs_priv rtw_debug_priv_read_reg = {
821 	.cb_write = rtw_debugfs_set_read_reg,
822 	.cb_read = rtw_debugfs_get_read_reg,
823 };
824 
825 static struct rtw_debugfs_priv rtw_debug_priv_dump_cam = {
826 	.cb_write = rtw_debugfs_set_single_input,
827 	.cb_read = rtw_debugfs_get_dump_cam,
828 };
829 
830 static struct rtw_debugfs_priv rtw_debug_priv_rsvd_page = {
831 	.cb_write = rtw_debugfs_set_rsvd_page,
832 	.cb_read = rtw_debugfs_get_rsvd_page,
833 };
834 
835 static struct rtw_debugfs_priv rtw_debug_priv_phy_info = {
836 	.cb_read = rtw_debugfs_get_phy_info,
837 };
838 
839 static struct rtw_debugfs_priv rtw_debug_priv_coex_enable = {
840 	.cb_write = rtw_debugfs_set_coex_enable,
841 	.cb_read = rtw_debugfs_get_coex_enable,
842 };
843 
844 static struct rtw_debugfs_priv rtw_debug_priv_coex_info = {
845 	.cb_read = rtw_debugfs_get_coex_info,
846 };
847 
848 #define rtw_debugfs_add_core(name, mode, fopname, parent)		\
849 	do {								\
850 		rtw_debug_priv_ ##name.rtwdev = rtwdev;			\
851 		if (!debugfs_create_file(#name, mode,			\
852 					 parent, &rtw_debug_priv_ ##name,\
853 					 &file_ops_ ##fopname))		\
854 			pr_debug("Unable to initialize debugfs:%s\n",	\
855 			       #name);					\
856 	} while (0)
857 
858 #define rtw_debugfs_add_w(name)						\
859 	rtw_debugfs_add_core(name, S_IFREG | 0222, common_write, debugfs_topdir)
860 #define rtw_debugfs_add_rw(name)					\
861 	rtw_debugfs_add_core(name, S_IFREG | 0666, single_rw, debugfs_topdir)
862 #define rtw_debugfs_add_r(name)						\
863 	rtw_debugfs_add_core(name, S_IFREG | 0444, single_r, debugfs_topdir)
864 
865 void rtw_debugfs_init(struct rtw_dev *rtwdev)
866 {
867 	struct dentry *debugfs_topdir;
868 
869 	debugfs_topdir = debugfs_create_dir("rtw88",
870 					    rtwdev->hw->wiphy->debugfsdir);
871 	rtw_debugfs_add_w(write_reg);
872 	rtw_debugfs_add_rw(read_reg);
873 	rtw_debugfs_add_w(rf_write);
874 	rtw_debugfs_add_rw(rf_read);
875 	rtw_debugfs_add_rw(dump_cam);
876 	rtw_debugfs_add_rw(rsvd_page);
877 	rtw_debugfs_add_r(phy_info);
878 	rtw_debugfs_add_r(coex_info);
879 	rtw_debugfs_add_rw(coex_enable);
880 	rtw_debugfs_add_r(mac_0);
881 	rtw_debugfs_add_r(mac_1);
882 	rtw_debugfs_add_r(mac_2);
883 	rtw_debugfs_add_r(mac_3);
884 	rtw_debugfs_add_r(mac_4);
885 	rtw_debugfs_add_r(mac_5);
886 	rtw_debugfs_add_r(mac_6);
887 	rtw_debugfs_add_r(mac_7);
888 	rtw_debugfs_add_r(bb_8);
889 	rtw_debugfs_add_r(bb_9);
890 	rtw_debugfs_add_r(bb_a);
891 	rtw_debugfs_add_r(bb_b);
892 	rtw_debugfs_add_r(bb_c);
893 	rtw_debugfs_add_r(bb_d);
894 	rtw_debugfs_add_r(bb_e);
895 	rtw_debugfs_add_r(bb_f);
896 	rtw_debugfs_add_r(mac_10);
897 	rtw_debugfs_add_r(mac_11);
898 	rtw_debugfs_add_r(mac_12);
899 	rtw_debugfs_add_r(mac_13);
900 	rtw_debugfs_add_r(mac_14);
901 	rtw_debugfs_add_r(mac_15);
902 	rtw_debugfs_add_r(mac_16);
903 	rtw_debugfs_add_r(mac_17);
904 	rtw_debugfs_add_r(bb_18);
905 	rtw_debugfs_add_r(bb_19);
906 	rtw_debugfs_add_r(bb_1a);
907 	rtw_debugfs_add_r(bb_1b);
908 	rtw_debugfs_add_r(bb_1c);
909 	rtw_debugfs_add_r(bb_1d);
910 	rtw_debugfs_add_r(bb_1e);
911 	rtw_debugfs_add_r(bb_1f);
912 	if (rtwdev->chip->id == RTW_CHIP_TYPE_8822C) {
913 		rtw_debugfs_add_r(bb_2c);
914 		rtw_debugfs_add_r(bb_2d);
915 		rtw_debugfs_add_r(bb_40);
916 		rtw_debugfs_add_r(bb_41);
917 	}
918 	rtw_debugfs_add_r(rf_dump);
919 	rtw_debugfs_add_r(tx_pwr_tbl);
920 }
921 
922 #endif /* CONFIG_RTW88_DEBUGFS */
923 
924 #ifdef CONFIG_RTW88_DEBUG
925 
926 void __rtw_dbg(struct rtw_dev *rtwdev, enum rtw_debug_mask mask,
927 	       const char *fmt, ...)
928 {
929 	struct va_format vaf = {
930 		.fmt = fmt,
931 	};
932 	va_list args;
933 
934 	va_start(args, fmt);
935 	vaf.va = &args;
936 
937 	if (rtw_debug_mask & mask)
938 		dev_printk(KERN_DEBUG, rtwdev->dev, "%pV", &vaf);
939 
940 	va_end(args);
941 }
942 EXPORT_SYMBOL(__rtw_dbg);
943 
944 #endif /* CONFIG_RTW88_DEBUG */
945