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 #include "reg.h"
14 #include "ps.h"
15 #include "regd.h"
16 
17 #ifdef CONFIG_RTW88_DEBUGFS
18 
19 struct rtw_debugfs_priv {
20 	struct rtw_dev *rtwdev;
21 	int (*cb_read)(struct seq_file *m, void *v);
22 	ssize_t (*cb_write)(struct file *filp, const char __user *buffer,
23 			    size_t count, loff_t *loff);
24 	union {
25 		u32 cb_data;
26 		u8 *buf;
27 		struct {
28 			u32 page_offset;
29 			u32 page_num;
30 		} rsvd_page;
31 		struct {
32 			u8 rf_path;
33 			u32 rf_addr;
34 			u32 rf_mask;
35 		};
36 		struct {
37 			u32 addr;
38 			u32 len;
39 		} read_reg;
40 		struct {
41 			u8 bit;
42 		} dm_cap;
43 	};
44 };
45 
46 static const char * const rtw_dm_cap_strs[] = {
47 	[RTW_DM_CAP_NA] = "NA",
48 	[RTW_DM_CAP_TXGAPK] = "TXGAPK",
49 };
50 
51 static int rtw_debugfs_single_show(struct seq_file *m, void *v)
52 {
53 	struct rtw_debugfs_priv *debugfs_priv = m->private;
54 
55 	return debugfs_priv->cb_read(m, v);
56 }
57 
58 static ssize_t rtw_debugfs_common_write(struct file *filp,
59 					const char __user *buffer,
60 					size_t count, loff_t *loff)
61 {
62 	struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
63 
64 	return debugfs_priv->cb_write(filp, buffer, count, loff);
65 }
66 
67 static ssize_t rtw_debugfs_single_write(struct file *filp,
68 					const char __user *buffer,
69 					size_t count, loff_t *loff)
70 {
71 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
72 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
73 
74 	return debugfs_priv->cb_write(filp, buffer, count, loff);
75 }
76 
77 static int rtw_debugfs_single_open_rw(struct inode *inode, struct file *filp)
78 {
79 	return single_open(filp, rtw_debugfs_single_show, inode->i_private);
80 }
81 
82 static int rtw_debugfs_close(struct inode *inode, struct file *filp)
83 {
84 	return 0;
85 }
86 
87 static const struct file_operations file_ops_single_r = {
88 	.owner = THIS_MODULE,
89 	.open = rtw_debugfs_single_open_rw,
90 	.read = seq_read,
91 	.llseek = seq_lseek,
92 	.release = single_release,
93 };
94 
95 static const struct file_operations file_ops_single_rw = {
96 	.owner = THIS_MODULE,
97 	.open = rtw_debugfs_single_open_rw,
98 	.release = single_release,
99 	.read = seq_read,
100 	.llseek = seq_lseek,
101 	.write = rtw_debugfs_single_write,
102 };
103 
104 static const struct file_operations file_ops_common_write = {
105 	.owner = THIS_MODULE,
106 	.write = rtw_debugfs_common_write,
107 	.open = simple_open,
108 	.release = rtw_debugfs_close,
109 };
110 
111 static int rtw_debugfs_get_read_reg(struct seq_file *m, void *v)
112 {
113 	struct rtw_debugfs_priv *debugfs_priv = m->private;
114 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
115 	u32 val, len, addr;
116 
117 	len = debugfs_priv->read_reg.len;
118 	addr = debugfs_priv->read_reg.addr;
119 	switch (len) {
120 	case 1:
121 		val = rtw_read8(rtwdev, addr);
122 		seq_printf(m, "reg 0x%03x: 0x%02x\n", addr, val);
123 		break;
124 	case 2:
125 		val = rtw_read16(rtwdev, addr);
126 		seq_printf(m, "reg 0x%03x: 0x%04x\n", addr, val);
127 		break;
128 	case 4:
129 		val = rtw_read32(rtwdev, addr);
130 		seq_printf(m, "reg 0x%03x: 0x%08x\n", addr, val);
131 		break;
132 	}
133 	return 0;
134 }
135 
136 static int rtw_debugfs_get_rf_read(struct seq_file *m, void *v)
137 {
138 	struct rtw_debugfs_priv *debugfs_priv = m->private;
139 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
140 	u32 val, addr, mask;
141 	u8 path;
142 
143 	path = debugfs_priv->rf_path;
144 	addr = debugfs_priv->rf_addr;
145 	mask = debugfs_priv->rf_mask;
146 
147 	mutex_lock(&rtwdev->mutex);
148 	val = rtw_read_rf(rtwdev, path, addr, mask);
149 	mutex_unlock(&rtwdev->mutex);
150 
151 	seq_printf(m, "rf_read path:%d addr:0x%08x mask:0x%08x val=0x%08x\n",
152 		   path, addr, mask, val);
153 
154 	return 0;
155 }
156 
157 static int rtw_debugfs_get_fix_rate(struct seq_file *m, void *v)
158 {
159 	struct rtw_debugfs_priv *debugfs_priv = m->private;
160 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
161 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
162 	u8 fix_rate = dm_info->fix_rate;
163 
164 	if (fix_rate >= DESC_RATE_MAX) {
165 		seq_printf(m, "Fix rate disabled, fix_rate = %u\n", fix_rate);
166 		return 0;
167 	}
168 
169 	seq_printf(m, "Data frames fixed at desc rate %u\n", fix_rate);
170 	return 0;
171 }
172 
173 static int rtw_debugfs_copy_from_user(char tmp[], int size,
174 				      const char __user *buffer, size_t count,
175 				      int num)
176 {
177 	int tmp_len;
178 
179 	memset(tmp, 0, size);
180 
181 	if (count < num)
182 		return -EFAULT;
183 
184 	tmp_len = (count > size - 1 ? size - 1 : count);
185 
186 	if (!buffer || copy_from_user(tmp, buffer, tmp_len))
187 		return count;
188 
189 	tmp[tmp_len] = '\0';
190 
191 	return 0;
192 }
193 
194 static ssize_t rtw_debugfs_set_read_reg(struct file *filp,
195 					const char __user *buffer,
196 					size_t count, loff_t *loff)
197 {
198 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
199 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
200 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
201 	char tmp[32 + 1];
202 	u32 addr, len;
203 	int num;
204 
205 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 2);
206 
207 	num = sscanf(tmp, "%x %x", &addr, &len);
208 
209 	if (num !=  2)
210 		return count;
211 
212 	if (len != 1 && len != 2 && len != 4) {
213 		rtw_warn(rtwdev, "read reg setting wrong len\n");
214 		return -EINVAL;
215 	}
216 	debugfs_priv->read_reg.addr = addr;
217 	debugfs_priv->read_reg.len = len;
218 
219 	return count;
220 }
221 
222 static int rtw_debugfs_get_dump_cam(struct seq_file *m, void *v)
223 {
224 	struct rtw_debugfs_priv *debugfs_priv = m->private;
225 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
226 	u32 val, command;
227 	u32 hw_key_idx = debugfs_priv->cb_data << RTW_SEC_CAM_ENTRY_SHIFT;
228 	u32 read_cmd = RTW_SEC_CMD_POLLING;
229 	int i;
230 
231 	seq_printf(m, "cam entry%d\n", debugfs_priv->cb_data);
232 	seq_puts(m, "0x0      0x1      0x2     0x3     ");
233 	seq_puts(m, "0x4     0x5\n");
234 	mutex_lock(&rtwdev->mutex);
235 	for (i = 0; i <= 5; i++) {
236 		command = read_cmd | (hw_key_idx + i);
237 		rtw_write32(rtwdev, RTW_SEC_CMD_REG, command);
238 		val = rtw_read32(rtwdev, RTW_SEC_READ_REG);
239 		seq_printf(m, "%8.8x", val);
240 		if (i < 2)
241 			seq_puts(m, " ");
242 	}
243 	seq_puts(m, "\n");
244 	mutex_unlock(&rtwdev->mutex);
245 	return 0;
246 }
247 
248 static int rtw_debugfs_get_rsvd_page(struct seq_file *m, void *v)
249 {
250 	struct rtw_debugfs_priv *debugfs_priv = m->private;
251 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
252 	u8 page_size = rtwdev->chip->page_size;
253 	u32 buf_size = debugfs_priv->rsvd_page.page_num * page_size;
254 	u32 offset = debugfs_priv->rsvd_page.page_offset * page_size;
255 	u8 *buf;
256 	int i;
257 	int ret;
258 
259 	buf = vzalloc(buf_size);
260 	if (!buf)
261 		return -ENOMEM;
262 
263 	ret = rtw_fw_dump_fifo(rtwdev, RTW_FW_FIFO_SEL_RSVD_PAGE, offset,
264 			       buf_size, (u32 *)buf);
265 	if (ret) {
266 		rtw_err(rtwdev, "failed to dump rsvd page\n");
267 		vfree(buf);
268 		return ret;
269 	}
270 
271 	for (i = 0 ; i < buf_size ; i += 8) {
272 		if (i % page_size == 0)
273 			seq_printf(m, "PAGE %d\n", (i + offset) / page_size);
274 		seq_printf(m, "%8ph\n", buf + i);
275 	}
276 	vfree(buf);
277 
278 	return 0;
279 }
280 
281 static ssize_t rtw_debugfs_set_rsvd_page(struct file *filp,
282 					 const char __user *buffer,
283 					 size_t count, loff_t *loff)
284 {
285 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
286 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
287 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
288 	char tmp[32 + 1];
289 	u32 offset, page_num;
290 	int num;
291 
292 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 2);
293 
294 	num = sscanf(tmp, "%d %d", &offset, &page_num);
295 
296 	if (num != 2) {
297 		rtw_warn(rtwdev, "invalid arguments\n");
298 		return -EINVAL;
299 	}
300 
301 	debugfs_priv->rsvd_page.page_offset = offset;
302 	debugfs_priv->rsvd_page.page_num = page_num;
303 
304 	return count;
305 }
306 
307 static ssize_t rtw_debugfs_set_single_input(struct file *filp,
308 					    const char __user *buffer,
309 					    size_t count, loff_t *loff)
310 {
311 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
312 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
313 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
314 	char tmp[32 + 1];
315 	u32 input;
316 	int num;
317 
318 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 1);
319 
320 	num = kstrtoint(tmp, 0, &input);
321 
322 	if (num) {
323 		rtw_warn(rtwdev, "kstrtoint failed\n");
324 		return num;
325 	}
326 
327 	debugfs_priv->cb_data = input;
328 
329 	return count;
330 }
331 
332 static ssize_t rtw_debugfs_set_write_reg(struct file *filp,
333 					 const char __user *buffer,
334 					 size_t count, loff_t *loff)
335 {
336 	struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
337 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
338 	char tmp[32 + 1];
339 	u32 addr, val, len;
340 	int num;
341 
342 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 3);
343 
344 	/* write BB/MAC register */
345 	num = sscanf(tmp, "%x %x %x", &addr, &val, &len);
346 
347 	if (num !=  3)
348 		return count;
349 
350 	switch (len) {
351 	case 1:
352 		rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
353 			"reg write8 0x%03x: 0x%08x\n", addr, val);
354 		rtw_write8(rtwdev, addr, (u8)val);
355 		break;
356 	case 2:
357 		rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
358 			"reg write16 0x%03x: 0x%08x\n", addr, val);
359 		rtw_write16(rtwdev, addr, (u16)val);
360 		break;
361 	case 4:
362 		rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
363 			"reg write32 0x%03x: 0x%08x\n", addr, val);
364 		rtw_write32(rtwdev, addr, (u32)val);
365 		break;
366 	default:
367 		rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
368 			"error write length = %d\n", len);
369 		break;
370 	}
371 
372 	return count;
373 }
374 
375 static ssize_t rtw_debugfs_set_h2c(struct file *filp,
376 				   const char __user *buffer,
377 				   size_t count, loff_t *loff)
378 {
379 	struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
380 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
381 	char tmp[32 + 1];
382 	u8 param[8];
383 	int num;
384 
385 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 3);
386 
387 	num = sscanf(tmp, "%hhx,%hhx,%hhx,%hhx,%hhx,%hhx,%hhx,%hhx",
388 		     &param[0], &param[1], &param[2], &param[3],
389 		     &param[4], &param[5], &param[6], &param[7]);
390 	if (num != 8) {
391 		rtw_warn(rtwdev, "invalid H2C command format for debug\n");
392 		return -EINVAL;
393 	}
394 
395 	mutex_lock(&rtwdev->mutex);
396 	rtw_fw_h2c_cmd_dbg(rtwdev, param);
397 	mutex_unlock(&rtwdev->mutex);
398 
399 	return count;
400 }
401 
402 static ssize_t rtw_debugfs_set_rf_write(struct file *filp,
403 					const char __user *buffer,
404 					size_t count, loff_t *loff)
405 {
406 	struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
407 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
408 	char tmp[32 + 1];
409 	u32 path, addr, mask, val;
410 	int num;
411 
412 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 4);
413 
414 	num = sscanf(tmp, "%x %x %x %x", &path, &addr, &mask, &val);
415 
416 	if (num !=  4) {
417 		rtw_warn(rtwdev, "invalid args, [path] [addr] [mask] [val]\n");
418 		return count;
419 	}
420 
421 	mutex_lock(&rtwdev->mutex);
422 	rtw_write_rf(rtwdev, path, addr, mask, val);
423 	mutex_unlock(&rtwdev->mutex);
424 	rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
425 		"write_rf path:%d addr:0x%08x mask:0x%08x, val:0x%08x\n",
426 		path, addr, mask, val);
427 
428 	return count;
429 }
430 
431 static ssize_t rtw_debugfs_set_rf_read(struct file *filp,
432 				       const char __user *buffer,
433 				       size_t count, loff_t *loff)
434 {
435 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
436 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
437 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
438 	char tmp[32 + 1];
439 	u32 path, addr, mask;
440 	int num;
441 
442 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 3);
443 
444 	num = sscanf(tmp, "%x %x %x", &path, &addr, &mask);
445 
446 	if (num !=  3) {
447 		rtw_warn(rtwdev, "invalid args, [path] [addr] [mask] [val]\n");
448 		return count;
449 	}
450 
451 	debugfs_priv->rf_path = path;
452 	debugfs_priv->rf_addr = addr;
453 	debugfs_priv->rf_mask = mask;
454 
455 	return count;
456 }
457 
458 static ssize_t rtw_debugfs_set_fix_rate(struct file *filp,
459 					const char __user *buffer,
460 					size_t count, loff_t *loff)
461 {
462 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
463 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
464 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
465 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
466 	u8 fix_rate;
467 	char tmp[32 + 1];
468 	int ret;
469 
470 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 1);
471 
472 	ret = kstrtou8(tmp, 0, &fix_rate);
473 	if (ret) {
474 		rtw_warn(rtwdev, "invalid args, [rate]\n");
475 		return ret;
476 	}
477 
478 	dm_info->fix_rate = fix_rate;
479 
480 	return count;
481 }
482 
483 static int rtw_debug_get_mac_page(struct seq_file *m, void *v)
484 {
485 	struct rtw_debugfs_priv *debugfs_priv = m->private;
486 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
487 	u32 page = debugfs_priv->cb_data;
488 	int i, n;
489 	int max = 0xff;
490 
491 	rtw_read32(rtwdev, debugfs_priv->cb_data);
492 	for (n = 0; n <= max; ) {
493 		seq_printf(m, "\n%8.8x  ", n + page);
494 		for (i = 0; i < 4 && n <= max; i++, n += 4)
495 			seq_printf(m, "%8.8x    ",
496 				   rtw_read32(rtwdev, (page | n)));
497 	}
498 	seq_puts(m, "\n");
499 	return 0;
500 }
501 
502 static int rtw_debug_get_bb_page(struct seq_file *m, void *v)
503 {
504 	struct rtw_debugfs_priv *debugfs_priv = m->private;
505 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
506 	u32 page = debugfs_priv->cb_data;
507 	int i, n;
508 	int max = 0xff;
509 
510 	rtw_read32(rtwdev, debugfs_priv->cb_data);
511 	for (n = 0; n <= max; ) {
512 		seq_printf(m, "\n%8.8x  ", n + page);
513 		for (i = 0; i < 4 && n <= max; i++, n += 4)
514 			seq_printf(m, "%8.8x    ",
515 				   rtw_read32(rtwdev, (page | n)));
516 	}
517 	seq_puts(m, "\n");
518 	return 0;
519 }
520 
521 static int rtw_debug_get_rf_dump(struct seq_file *m, void *v)
522 {
523 	struct rtw_debugfs_priv *debugfs_priv = m->private;
524 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
525 	u32 addr, offset, data;
526 	u8 path;
527 
528 	mutex_lock(&rtwdev->mutex);
529 
530 	for (path = 0; path < rtwdev->hal.rf_path_num; path++) {
531 		seq_printf(m, "RF path:%d\n", path);
532 		for (addr = 0; addr < 0x100; addr += 4) {
533 			seq_printf(m, "%8.8x  ", addr);
534 			for (offset = 0; offset < 4; offset++) {
535 				data = rtw_read_rf(rtwdev, path, addr + offset,
536 						   0xffffffff);
537 				seq_printf(m, "%8.8x    ", data);
538 			}
539 			seq_puts(m, "\n");
540 		}
541 		seq_puts(m, "\n");
542 	}
543 
544 	mutex_unlock(&rtwdev->mutex);
545 
546 	return 0;
547 }
548 
549 static void rtw_print_cck_rate_txt(struct seq_file *m, u8 rate)
550 {
551 	static const char * const
552 	cck_rate[] = {"1M", "2M", "5.5M", "11M"};
553 	u8 idx = rate - DESC_RATE1M;
554 
555 	seq_printf(m, " CCK_%-5s", cck_rate[idx]);
556 }
557 
558 static void rtw_print_ofdm_rate_txt(struct seq_file *m, u8 rate)
559 {
560 	static const char * const
561 	ofdm_rate[] = {"6M", "9M", "12M", "18M", "24M", "36M", "48M", "54M"};
562 	u8 idx = rate - DESC_RATE6M;
563 
564 	seq_printf(m, " OFDM_%-4s", ofdm_rate[idx]);
565 }
566 
567 static void rtw_print_ht_rate_txt(struct seq_file *m, u8 rate)
568 {
569 	u8 mcs_n = rate - DESC_RATEMCS0;
570 
571 	seq_printf(m, " MCS%-6u", mcs_n);
572 }
573 
574 static void rtw_print_vht_rate_txt(struct seq_file *m, u8 rate)
575 {
576 	u8 idx = rate - DESC_RATEVHT1SS_MCS0;
577 	u8 n_ss, mcs_n;
578 
579 	/* n spatial stream */
580 	n_ss = 1 + idx / 10;
581 	/* MCS n */
582 	mcs_n = idx % 10;
583 	seq_printf(m, " VHT%uSMCS%u", n_ss, mcs_n);
584 }
585 
586 static void rtw_print_rate(struct seq_file *m, u8 rate)
587 {
588 	switch (rate) {
589 	case DESC_RATE1M...DESC_RATE11M:
590 		rtw_print_cck_rate_txt(m, rate);
591 		break;
592 	case DESC_RATE6M...DESC_RATE54M:
593 		rtw_print_ofdm_rate_txt(m, rate);
594 		break;
595 	case DESC_RATEMCS0...DESC_RATEMCS15:
596 		rtw_print_ht_rate_txt(m, rate);
597 		break;
598 	case DESC_RATEVHT1SS_MCS0...DESC_RATEVHT2SS_MCS9:
599 		rtw_print_vht_rate_txt(m, rate);
600 		break;
601 	default:
602 		seq_printf(m, " Unknown rate=0x%x\n", rate);
603 		break;
604 	}
605 }
606 
607 #define case_REGD(src) \
608 	case RTW_REGD_##src: return #src
609 
610 static const char *rtw_get_regd_string(u8 regd)
611 {
612 	switch (regd) {
613 	case_REGD(FCC);
614 	case_REGD(MKK);
615 	case_REGD(ETSI);
616 	case_REGD(IC);
617 	case_REGD(KCC);
618 	case_REGD(ACMA);
619 	case_REGD(CHILE);
620 	case_REGD(UKRAINE);
621 	case_REGD(MEXICO);
622 	case_REGD(CN);
623 	case_REGD(WW);
624 	default:
625 		return "Unknown";
626 	}
627 }
628 
629 static int rtw_debugfs_get_tx_pwr_tbl(struct seq_file *m, void *v)
630 {
631 	struct rtw_debugfs_priv *debugfs_priv = m->private;
632 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
633 	struct rtw_hal *hal = &rtwdev->hal;
634 	u8 path, rate, bw, ch, regd;
635 	struct rtw_power_params pwr_param = {0};
636 
637 	mutex_lock(&rtwdev->mutex);
638 	bw = hal->current_band_width;
639 	ch = hal->current_channel;
640 	regd = rtw_regd_get(rtwdev);
641 
642 	seq_printf(m, "channel: %u\n", ch);
643 	seq_printf(m, "bandwidth: %u\n", bw);
644 	seq_printf(m, "regulatory: %s\n", rtw_get_regd_string(regd));
645 	seq_printf(m, "%-4s %-10s %-9s %-9s (%-4s %-4s %-4s) %-4s\n",
646 		   "path", "rate", "pwr", "base", "byr", "lmt", "sar", "rem");
647 
648 	mutex_lock(&hal->tx_power_mutex);
649 	for (path = RF_PATH_A; path <= RF_PATH_B; path++) {
650 		/* there is no CCK rates used in 5G */
651 		if (hal->current_band_type == RTW_BAND_5G)
652 			rate = DESC_RATE6M;
653 		else
654 			rate = DESC_RATE1M;
655 
656 		/* now, not support vht 3ss and vht 4ss*/
657 		for (; rate <= DESC_RATEVHT2SS_MCS9; rate++) {
658 			/* now, not support ht 3ss and ht 4ss*/
659 			if (rate > DESC_RATEMCS15 &&
660 			    rate < DESC_RATEVHT1SS_MCS0)
661 				continue;
662 
663 			rtw_get_tx_power_params(rtwdev, path, rate, bw,
664 						ch, regd, &pwr_param);
665 
666 			seq_printf(m, "%4c ", path + 'A');
667 			rtw_print_rate(m, rate);
668 			seq_printf(m, " %3u(0x%02x) %4u %4d (%4d %4d %4d) %4d\n",
669 				   hal->tx_pwr_tbl[path][rate],
670 				   hal->tx_pwr_tbl[path][rate],
671 				   pwr_param.pwr_base,
672 				   min3(pwr_param.pwr_offset,
673 					pwr_param.pwr_limit,
674 					pwr_param.pwr_sar),
675 				   pwr_param.pwr_offset, pwr_param.pwr_limit,
676 				   pwr_param.pwr_sar,
677 				   pwr_param.pwr_remnant);
678 		}
679 	}
680 
681 	mutex_unlock(&hal->tx_power_mutex);
682 	mutex_unlock(&rtwdev->mutex);
683 
684 	return 0;
685 }
686 
687 void rtw_debugfs_get_simple_phy_info(struct seq_file *m)
688 {
689 	struct rtw_debugfs_priv *debugfs_priv = m->private;
690 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
691 	struct rtw_hal *hal = &rtwdev->hal;
692 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
693 	struct rtw_traffic_stats *stats = &rtwdev->stats;
694 
695 	seq_printf(m, "%-40s = %ddBm/ %d\n", "RSSI/ STA Channel",
696 		   dm_info->rssi[RF_PATH_A] - 100, hal->current_channel);
697 
698 	seq_printf(m, "TP {Tx, Rx} = {%u, %u}Mbps\n",
699 		   stats->tx_throughput, stats->rx_throughput);
700 
701 	seq_puts(m, "[Tx Rate] = ");
702 	rtw_print_rate(m, dm_info->tx_rate);
703 	seq_printf(m, "(0x%x)\n", dm_info->tx_rate);
704 
705 	seq_puts(m, "[Rx Rate] = ");
706 	rtw_print_rate(m, dm_info->curr_rx_rate);
707 	seq_printf(m, "(0x%x)\n", dm_info->curr_rx_rate);
708 }
709 
710 static int rtw_debugfs_get_phy_info(struct seq_file *m, void *v)
711 {
712 	struct rtw_debugfs_priv *debugfs_priv = m->private;
713 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
714 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
715 	struct rtw_traffic_stats *stats = &rtwdev->stats;
716 	struct rtw_pkt_count *last_cnt = &dm_info->last_pkt_count;
717 	struct rtw_efuse *efuse = &rtwdev->efuse;
718 	struct ewma_evm *ewma_evm = dm_info->ewma_evm;
719 	struct ewma_snr *ewma_snr = dm_info->ewma_snr;
720 	u8 ss, rate_id;
721 
722 	seq_puts(m, "==========[Common Info]========\n");
723 	seq_printf(m, "Is link = %c\n", rtw_is_assoc(rtwdev) ? 'Y' : 'N');
724 	seq_printf(m, "Current CH(fc) = %u\n", rtwdev->hal.current_channel);
725 	seq_printf(m, "Current BW = %u\n", rtwdev->hal.current_band_width);
726 	seq_printf(m, "Current IGI = 0x%x\n", dm_info->igi_history[0]);
727 	seq_printf(m, "TP {Tx, Rx} = {%u, %u}Mbps\n",
728 		   stats->tx_throughput, stats->rx_throughput);
729 	seq_printf(m, "1SS for TX and RX = %c\n\n", rtwdev->hal.txrx_1ss ?
730 		   'Y' : 'N');
731 
732 	seq_puts(m, "==========[Tx Phy Info]========\n");
733 	seq_puts(m, "[Tx Rate] = ");
734 	rtw_print_rate(m, dm_info->tx_rate);
735 	seq_printf(m, "(0x%x)\n\n", dm_info->tx_rate);
736 
737 	seq_puts(m, "==========[Rx Phy Info]========\n");
738 	seq_printf(m, "[Rx Beacon Count] = %u\n", last_cnt->num_bcn_pkt);
739 	seq_puts(m, "[Rx Rate] = ");
740 	rtw_print_rate(m, dm_info->curr_rx_rate);
741 	seq_printf(m, "(0x%x)\n", dm_info->curr_rx_rate);
742 
743 	seq_puts(m, "[Rx Rate Count]:\n");
744 	seq_printf(m, " * CCK = {%u, %u, %u, %u}\n",
745 		   last_cnt->num_qry_pkt[DESC_RATE1M],
746 		   last_cnt->num_qry_pkt[DESC_RATE2M],
747 		   last_cnt->num_qry_pkt[DESC_RATE5_5M],
748 		   last_cnt->num_qry_pkt[DESC_RATE11M]);
749 
750 	seq_printf(m, " * OFDM = {%u, %u, %u, %u, %u, %u, %u, %u}\n",
751 		   last_cnt->num_qry_pkt[DESC_RATE6M],
752 		   last_cnt->num_qry_pkt[DESC_RATE9M],
753 		   last_cnt->num_qry_pkt[DESC_RATE12M],
754 		   last_cnt->num_qry_pkt[DESC_RATE18M],
755 		   last_cnt->num_qry_pkt[DESC_RATE24M],
756 		   last_cnt->num_qry_pkt[DESC_RATE36M],
757 		   last_cnt->num_qry_pkt[DESC_RATE48M],
758 		   last_cnt->num_qry_pkt[DESC_RATE54M]);
759 
760 	for (ss = 0; ss < efuse->hw_cap.nss; ss++) {
761 		rate_id = DESC_RATEMCS0 + ss * 8;
762 		seq_printf(m, " * HT_MCS[%u:%u] = {%u, %u, %u, %u, %u, %u, %u, %u}\n",
763 			   ss * 8, ss * 8 + 7,
764 			   last_cnt->num_qry_pkt[rate_id],
765 			   last_cnt->num_qry_pkt[rate_id + 1],
766 			   last_cnt->num_qry_pkt[rate_id + 2],
767 			   last_cnt->num_qry_pkt[rate_id + 3],
768 			   last_cnt->num_qry_pkt[rate_id + 4],
769 			   last_cnt->num_qry_pkt[rate_id + 5],
770 			   last_cnt->num_qry_pkt[rate_id + 6],
771 			   last_cnt->num_qry_pkt[rate_id + 7]);
772 	}
773 
774 	for (ss = 0; ss < efuse->hw_cap.nss; ss++) {
775 		rate_id = DESC_RATEVHT1SS_MCS0 + ss * 10;
776 		seq_printf(m, " * VHT_MCS-%uss MCS[0:9] = {%u, %u, %u, %u, %u, %u, %u, %u, %u, %u}\n",
777 			   ss + 1,
778 			   last_cnt->num_qry_pkt[rate_id],
779 			   last_cnt->num_qry_pkt[rate_id + 1],
780 			   last_cnt->num_qry_pkt[rate_id + 2],
781 			   last_cnt->num_qry_pkt[rate_id + 3],
782 			   last_cnt->num_qry_pkt[rate_id + 4],
783 			   last_cnt->num_qry_pkt[rate_id + 5],
784 			   last_cnt->num_qry_pkt[rate_id + 6],
785 			   last_cnt->num_qry_pkt[rate_id + 7],
786 			   last_cnt->num_qry_pkt[rate_id + 8],
787 			   last_cnt->num_qry_pkt[rate_id + 9]);
788 	}
789 
790 	seq_printf(m, "[RSSI(dBm)] = {%d, %d}\n",
791 		   dm_info->rssi[RF_PATH_A] - 100,
792 		   dm_info->rssi[RF_PATH_B] - 100);
793 	seq_printf(m, "[Rx EVM(dB)] = {-%d, -%d}\n",
794 		   dm_info->rx_evm_dbm[RF_PATH_A],
795 		   dm_info->rx_evm_dbm[RF_PATH_B]);
796 	seq_printf(m, "[Rx SNR] = {%d, %d}\n",
797 		   dm_info->rx_snr[RF_PATH_A],
798 		   dm_info->rx_snr[RF_PATH_B]);
799 	seq_printf(m, "[CFO_tail(KHz)] = {%d, %d}\n",
800 		   dm_info->cfo_tail[RF_PATH_A],
801 		   dm_info->cfo_tail[RF_PATH_B]);
802 
803 	if (dm_info->curr_rx_rate >= DESC_RATE11M) {
804 		seq_puts(m, "[Rx Average Status]:\n");
805 		seq_printf(m, " * OFDM, EVM: {-%d}, SNR: {%d}\n",
806 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_OFDM]),
807 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_OFDM_A]));
808 		seq_printf(m, " * 1SS, EVM: {-%d}, SNR: {%d}\n",
809 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_1SS]),
810 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_1SS_A]));
811 		seq_printf(m, " * 2SS, EVM: {-%d, -%d}, SNR: {%d, %d}\n",
812 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_2SS_A]),
813 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_2SS_B]),
814 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_2SS_A]),
815 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_2SS_B]));
816 	}
817 
818 	seq_puts(m, "[Rx Counter]:\n");
819 	seq_printf(m, " * CCA (CCK, OFDM, Total) = (%u, %u, %u)\n",
820 		   dm_info->cck_cca_cnt,
821 		   dm_info->ofdm_cca_cnt,
822 		   dm_info->total_cca_cnt);
823 	seq_printf(m, " * False Alarm (CCK, OFDM, Total) = (%u, %u, %u)\n",
824 		   dm_info->cck_fa_cnt,
825 		   dm_info->ofdm_fa_cnt,
826 		   dm_info->total_fa_cnt);
827 	seq_printf(m, " * CCK cnt (ok, err) = (%u, %u)\n",
828 		   dm_info->cck_ok_cnt, dm_info->cck_err_cnt);
829 	seq_printf(m, " * OFDM cnt (ok, err) = (%u, %u)\n",
830 		   dm_info->ofdm_ok_cnt, dm_info->ofdm_err_cnt);
831 	seq_printf(m, " * HT cnt (ok, err) = (%u, %u)\n",
832 		   dm_info->ht_ok_cnt, dm_info->ht_err_cnt);
833 	seq_printf(m, " * VHT cnt (ok, err) = (%u, %u)\n",
834 		   dm_info->vht_ok_cnt, dm_info->vht_err_cnt);
835 
836 	return 0;
837 }
838 
839 static int rtw_debugfs_get_coex_info(struct seq_file *m, void *v)
840 {
841 	struct rtw_debugfs_priv *debugfs_priv = m->private;
842 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
843 
844 	mutex_lock(&rtwdev->mutex);
845 	rtw_coex_display_coex_info(rtwdev, m);
846 	mutex_unlock(&rtwdev->mutex);
847 
848 	return 0;
849 }
850 
851 static ssize_t rtw_debugfs_set_coex_enable(struct file *filp,
852 					   const char __user *buffer,
853 					   size_t count, loff_t *loff)
854 {
855 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
856 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
857 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
858 	struct rtw_coex *coex = &rtwdev->coex;
859 	char tmp[32 + 1];
860 	bool enable;
861 	int ret;
862 
863 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 1);
864 
865 	ret = kstrtobool(tmp, &enable);
866 	if (ret) {
867 		rtw_warn(rtwdev, "invalid arguments\n");
868 		return ret;
869 	}
870 
871 	mutex_lock(&rtwdev->mutex);
872 	coex->manual_control = !enable;
873 	mutex_unlock(&rtwdev->mutex);
874 
875 	return count;
876 }
877 
878 static int rtw_debugfs_get_coex_enable(struct seq_file *m, void *v)
879 {
880 	struct rtw_debugfs_priv *debugfs_priv = m->private;
881 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
882 	struct rtw_coex *coex = &rtwdev->coex;
883 
884 	seq_printf(m, "coex mechanism %s\n",
885 		   coex->manual_control ? "disabled" : "enabled");
886 
887 	return 0;
888 }
889 
890 static ssize_t rtw_debugfs_set_edcca_enable(struct file *filp,
891 					    const char __user *buffer,
892 					    size_t count, loff_t *loff)
893 {
894 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
895 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
896 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
897 	bool input;
898 	int err;
899 
900 	err = kstrtobool_from_user(buffer, count, &input);
901 	if (err)
902 		return err;
903 
904 	rtw_edcca_enabled = input;
905 	rtw_phy_adaptivity_set_mode(rtwdev);
906 
907 	return count;
908 }
909 
910 static int rtw_debugfs_get_edcca_enable(struct seq_file *m, void *v)
911 {
912 	struct rtw_debugfs_priv *debugfs_priv = m->private;
913 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
914 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
915 
916 	seq_printf(m, "EDCCA %s: EDCCA mode %d\n",
917 		   rtw_edcca_enabled ? "enabled" : "disabled",
918 		   dm_info->edcca_mode);
919 	return 0;
920 }
921 
922 static ssize_t rtw_debugfs_set_fw_crash(struct file *filp,
923 					const char __user *buffer,
924 					size_t count, loff_t *loff)
925 {
926 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
927 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
928 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
929 	char tmp[32 + 1];
930 	bool input;
931 	int ret;
932 
933 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 1);
934 
935 	ret = kstrtobool(tmp, &input);
936 	if (ret)
937 		return -EINVAL;
938 
939 	if (!input)
940 		return -EINVAL;
941 
942 	if (test_bit(RTW_FLAG_RESTARTING, rtwdev->flags))
943 		return -EINPROGRESS;
944 
945 	mutex_lock(&rtwdev->mutex);
946 	rtw_leave_lps_deep(rtwdev);
947 	set_bit(RTW_FLAG_RESTART_TRIGGERING, rtwdev->flags);
948 	rtw_write8(rtwdev, REG_HRCV_MSG, 1);
949 	mutex_unlock(&rtwdev->mutex);
950 
951 	return count;
952 }
953 
954 static int rtw_debugfs_get_fw_crash(struct seq_file *m, void *v)
955 {
956 	struct rtw_debugfs_priv *debugfs_priv = m->private;
957 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
958 
959 	seq_printf(m, "%d\n",
960 		   test_bit(RTW_FLAG_RESTART_TRIGGERING, rtwdev->flags) ||
961 		   test_bit(RTW_FLAG_RESTARTING, rtwdev->flags));
962 	return 0;
963 }
964 
965 static ssize_t rtw_debugfs_set_force_lowest_basic_rate(struct file *filp,
966 						       const char __user *buffer,
967 						       size_t count, loff_t *loff)
968 {
969 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
970 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
971 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
972 	bool input;
973 	int err;
974 
975 	err = kstrtobool_from_user(buffer, count, &input);
976 	if (err)
977 		return err;
978 
979 	if (input)
980 		set_bit(RTW_FLAG_FORCE_LOWEST_RATE, rtwdev->flags);
981 	else
982 		clear_bit(RTW_FLAG_FORCE_LOWEST_RATE, rtwdev->flags);
983 
984 	return count;
985 }
986 
987 static int rtw_debugfs_get_force_lowest_basic_rate(struct seq_file *m, void *v)
988 {
989 	struct rtw_debugfs_priv *debugfs_priv = m->private;
990 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
991 
992 	seq_printf(m, "force lowest basic rate: %d\n",
993 		   test_bit(RTW_FLAG_FORCE_LOWEST_RATE, rtwdev->flags));
994 
995 	return 0;
996 }
997 
998 static ssize_t rtw_debugfs_set_dm_cap(struct file *filp,
999 				      const char __user *buffer,
1000 				      size_t count, loff_t *loff)
1001 {
1002 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
1003 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
1004 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
1005 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
1006 	int bit;
1007 	bool en;
1008 
1009 	if (kstrtoint_from_user(buffer, count, 10, &bit))
1010 		return -EINVAL;
1011 
1012 	en = bit > 0;
1013 	bit = abs(bit);
1014 
1015 	if (bit >= RTW_DM_CAP_NUM) {
1016 		rtw_warn(rtwdev, "unknown DM CAP %d\n", bit);
1017 		return -EINVAL;
1018 	}
1019 
1020 	if (en)
1021 		dm_info->dm_flags &= ~BIT(bit);
1022 	else
1023 		dm_info->dm_flags |= BIT(bit);
1024 
1025 	debugfs_priv->dm_cap.bit = bit;
1026 
1027 	return count;
1028 }
1029 
1030 static void dump_gapk_status(struct rtw_dev *rtwdev, struct seq_file *m)
1031 {
1032 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
1033 	struct rtw_gapk_info *txgapk = &rtwdev->dm_info.gapk;
1034 	int i, path;
1035 	u32 val;
1036 
1037 	seq_printf(m, "\n(%2d) %c%s\n\n", RTW_DM_CAP_TXGAPK,
1038 		   dm_info->dm_flags & BIT(RTW_DM_CAP_TXGAPK) ? '-' : '+',
1039 		   rtw_dm_cap_strs[RTW_DM_CAP_TXGAPK]);
1040 
1041 	mutex_lock(&rtwdev->mutex);
1042 
1043 	for (path = 0; path < rtwdev->hal.rf_path_num; path++) {
1044 		val = rtw_read_rf(rtwdev, path, RF_GAINTX, RFREG_MASK);
1045 		seq_printf(m, "path %d:\n0x%x = 0x%x\n", path, RF_GAINTX, val);
1046 
1047 		for (i = 0; i < RF_HW_OFFSET_NUM; i++)
1048 			seq_printf(m, "[TXGAPK] offset %d %d\n",
1049 				   txgapk->rf3f_fs[path][i], i);
1050 		seq_puts(m, "\n");
1051 	}
1052 	mutex_unlock(&rtwdev->mutex);
1053 }
1054 
1055 static int rtw_debugfs_get_dm_cap(struct seq_file *m, void *v)
1056 {
1057 	struct rtw_debugfs_priv *debugfs_priv = m->private;
1058 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
1059 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
1060 	int i;
1061 
1062 	switch (debugfs_priv->dm_cap.bit) {
1063 	case RTW_DM_CAP_TXGAPK:
1064 		dump_gapk_status(rtwdev, m);
1065 		break;
1066 	default:
1067 		for (i = 1; i < RTW_DM_CAP_NUM; i++) {
1068 			seq_printf(m, "(%2d) %c%s\n", i,
1069 				   dm_info->dm_flags & BIT(i) ? '-' : '+',
1070 				   rtw_dm_cap_strs[i]);
1071 		}
1072 		break;
1073 	}
1074 	debugfs_priv->dm_cap.bit = RTW_DM_CAP_NA;
1075 	return 0;
1076 }
1077 
1078 #define rtw_debug_impl_mac(page, addr)				\
1079 static struct rtw_debugfs_priv rtw_debug_priv_mac_ ##page = {	\
1080 	.cb_read = rtw_debug_get_mac_page,			\
1081 	.cb_data = addr,					\
1082 }
1083 
1084 rtw_debug_impl_mac(0, 0x0000);
1085 rtw_debug_impl_mac(1, 0x0100);
1086 rtw_debug_impl_mac(2, 0x0200);
1087 rtw_debug_impl_mac(3, 0x0300);
1088 rtw_debug_impl_mac(4, 0x0400);
1089 rtw_debug_impl_mac(5, 0x0500);
1090 rtw_debug_impl_mac(6, 0x0600);
1091 rtw_debug_impl_mac(7, 0x0700);
1092 rtw_debug_impl_mac(10, 0x1000);
1093 rtw_debug_impl_mac(11, 0x1100);
1094 rtw_debug_impl_mac(12, 0x1200);
1095 rtw_debug_impl_mac(13, 0x1300);
1096 rtw_debug_impl_mac(14, 0x1400);
1097 rtw_debug_impl_mac(15, 0x1500);
1098 rtw_debug_impl_mac(16, 0x1600);
1099 rtw_debug_impl_mac(17, 0x1700);
1100 
1101 #define rtw_debug_impl_bb(page, addr)			\
1102 static struct rtw_debugfs_priv rtw_debug_priv_bb_ ##page = {	\
1103 	.cb_read = rtw_debug_get_bb_page,			\
1104 	.cb_data = addr,					\
1105 }
1106 
1107 rtw_debug_impl_bb(8, 0x0800);
1108 rtw_debug_impl_bb(9, 0x0900);
1109 rtw_debug_impl_bb(a, 0x0a00);
1110 rtw_debug_impl_bb(b, 0x0b00);
1111 rtw_debug_impl_bb(c, 0x0c00);
1112 rtw_debug_impl_bb(d, 0x0d00);
1113 rtw_debug_impl_bb(e, 0x0e00);
1114 rtw_debug_impl_bb(f, 0x0f00);
1115 rtw_debug_impl_bb(18, 0x1800);
1116 rtw_debug_impl_bb(19, 0x1900);
1117 rtw_debug_impl_bb(1a, 0x1a00);
1118 rtw_debug_impl_bb(1b, 0x1b00);
1119 rtw_debug_impl_bb(1c, 0x1c00);
1120 rtw_debug_impl_bb(1d, 0x1d00);
1121 rtw_debug_impl_bb(1e, 0x1e00);
1122 rtw_debug_impl_bb(1f, 0x1f00);
1123 rtw_debug_impl_bb(2c, 0x2c00);
1124 rtw_debug_impl_bb(2d, 0x2d00);
1125 rtw_debug_impl_bb(40, 0x4000);
1126 rtw_debug_impl_bb(41, 0x4100);
1127 
1128 static struct rtw_debugfs_priv rtw_debug_priv_rf_dump = {
1129 	.cb_read = rtw_debug_get_rf_dump,
1130 };
1131 
1132 static struct rtw_debugfs_priv rtw_debug_priv_tx_pwr_tbl = {
1133 	.cb_read = rtw_debugfs_get_tx_pwr_tbl,
1134 };
1135 
1136 static struct rtw_debugfs_priv rtw_debug_priv_write_reg = {
1137 	.cb_write = rtw_debugfs_set_write_reg,
1138 };
1139 
1140 static struct rtw_debugfs_priv rtw_debug_priv_h2c = {
1141 	.cb_write = rtw_debugfs_set_h2c,
1142 };
1143 
1144 static struct rtw_debugfs_priv rtw_debug_priv_rf_write = {
1145 	.cb_write = rtw_debugfs_set_rf_write,
1146 };
1147 
1148 static struct rtw_debugfs_priv rtw_debug_priv_rf_read = {
1149 	.cb_write = rtw_debugfs_set_rf_read,
1150 	.cb_read = rtw_debugfs_get_rf_read,
1151 };
1152 
1153 static struct rtw_debugfs_priv rtw_debug_priv_read_reg = {
1154 	.cb_write = rtw_debugfs_set_read_reg,
1155 	.cb_read = rtw_debugfs_get_read_reg,
1156 };
1157 
1158 static struct rtw_debugfs_priv rtw_debug_priv_fix_rate = {
1159 	.cb_write = rtw_debugfs_set_fix_rate,
1160 	.cb_read = rtw_debugfs_get_fix_rate,
1161 };
1162 
1163 static struct rtw_debugfs_priv rtw_debug_priv_dump_cam = {
1164 	.cb_write = rtw_debugfs_set_single_input,
1165 	.cb_read = rtw_debugfs_get_dump_cam,
1166 };
1167 
1168 static struct rtw_debugfs_priv rtw_debug_priv_rsvd_page = {
1169 	.cb_write = rtw_debugfs_set_rsvd_page,
1170 	.cb_read = rtw_debugfs_get_rsvd_page,
1171 };
1172 
1173 static struct rtw_debugfs_priv rtw_debug_priv_phy_info = {
1174 	.cb_read = rtw_debugfs_get_phy_info,
1175 };
1176 
1177 static struct rtw_debugfs_priv rtw_debug_priv_coex_enable = {
1178 	.cb_write = rtw_debugfs_set_coex_enable,
1179 	.cb_read = rtw_debugfs_get_coex_enable,
1180 };
1181 
1182 static struct rtw_debugfs_priv rtw_debug_priv_coex_info = {
1183 	.cb_read = rtw_debugfs_get_coex_info,
1184 };
1185 
1186 static struct rtw_debugfs_priv rtw_debug_priv_edcca_enable = {
1187 	.cb_write = rtw_debugfs_set_edcca_enable,
1188 	.cb_read = rtw_debugfs_get_edcca_enable,
1189 };
1190 
1191 static struct rtw_debugfs_priv rtw_debug_priv_fw_crash = {
1192 	.cb_write = rtw_debugfs_set_fw_crash,
1193 	.cb_read = rtw_debugfs_get_fw_crash,
1194 };
1195 
1196 static struct rtw_debugfs_priv rtw_debug_priv_force_lowest_basic_rate = {
1197 	.cb_write = rtw_debugfs_set_force_lowest_basic_rate,
1198 	.cb_read = rtw_debugfs_get_force_lowest_basic_rate,
1199 };
1200 
1201 static struct rtw_debugfs_priv rtw_debug_priv_dm_cap = {
1202 	.cb_write = rtw_debugfs_set_dm_cap,
1203 	.cb_read = rtw_debugfs_get_dm_cap,
1204 };
1205 
1206 #define rtw_debugfs_add_core(name, mode, fopname, parent)		\
1207 	do {								\
1208 		rtw_debug_priv_ ##name.rtwdev = rtwdev;			\
1209 		if (!debugfs_create_file(#name, mode,			\
1210 					 parent, &rtw_debug_priv_ ##name,\
1211 					 &file_ops_ ##fopname))		\
1212 			pr_debug("Unable to initialize debugfs:%s\n",	\
1213 			       #name);					\
1214 	} while (0)
1215 
1216 #define rtw_debugfs_add_w(name)						\
1217 	rtw_debugfs_add_core(name, S_IFREG | 0222, common_write, debugfs_topdir)
1218 #define rtw_debugfs_add_rw(name)					\
1219 	rtw_debugfs_add_core(name, S_IFREG | 0666, single_rw, debugfs_topdir)
1220 #define rtw_debugfs_add_r(name)						\
1221 	rtw_debugfs_add_core(name, S_IFREG | 0444, single_r, debugfs_topdir)
1222 
1223 void rtw_debugfs_init(struct rtw_dev *rtwdev)
1224 {
1225 	struct dentry *debugfs_topdir;
1226 
1227 	debugfs_topdir = debugfs_create_dir("rtw88",
1228 					    rtwdev->hw->wiphy->debugfsdir);
1229 	rtw_debugfs_add_w(write_reg);
1230 	rtw_debugfs_add_rw(read_reg);
1231 	rtw_debugfs_add_w(rf_write);
1232 	rtw_debugfs_add_rw(rf_read);
1233 	rtw_debugfs_add_rw(fix_rate);
1234 	rtw_debugfs_add_rw(dump_cam);
1235 	rtw_debugfs_add_rw(rsvd_page);
1236 	rtw_debugfs_add_r(phy_info);
1237 	rtw_debugfs_add_r(coex_info);
1238 	rtw_debugfs_add_rw(coex_enable);
1239 	rtw_debugfs_add_w(h2c);
1240 	rtw_debugfs_add_r(mac_0);
1241 	rtw_debugfs_add_r(mac_1);
1242 	rtw_debugfs_add_r(mac_2);
1243 	rtw_debugfs_add_r(mac_3);
1244 	rtw_debugfs_add_r(mac_4);
1245 	rtw_debugfs_add_r(mac_5);
1246 	rtw_debugfs_add_r(mac_6);
1247 	rtw_debugfs_add_r(mac_7);
1248 	rtw_debugfs_add_r(bb_8);
1249 	rtw_debugfs_add_r(bb_9);
1250 	rtw_debugfs_add_r(bb_a);
1251 	rtw_debugfs_add_r(bb_b);
1252 	rtw_debugfs_add_r(bb_c);
1253 	rtw_debugfs_add_r(bb_d);
1254 	rtw_debugfs_add_r(bb_e);
1255 	rtw_debugfs_add_r(bb_f);
1256 	rtw_debugfs_add_r(mac_10);
1257 	rtw_debugfs_add_r(mac_11);
1258 	rtw_debugfs_add_r(mac_12);
1259 	rtw_debugfs_add_r(mac_13);
1260 	rtw_debugfs_add_r(mac_14);
1261 	rtw_debugfs_add_r(mac_15);
1262 	rtw_debugfs_add_r(mac_16);
1263 	rtw_debugfs_add_r(mac_17);
1264 	rtw_debugfs_add_r(bb_18);
1265 	rtw_debugfs_add_r(bb_19);
1266 	rtw_debugfs_add_r(bb_1a);
1267 	rtw_debugfs_add_r(bb_1b);
1268 	rtw_debugfs_add_r(bb_1c);
1269 	rtw_debugfs_add_r(bb_1d);
1270 	rtw_debugfs_add_r(bb_1e);
1271 	rtw_debugfs_add_r(bb_1f);
1272 	if (rtwdev->chip->id == RTW_CHIP_TYPE_8822C) {
1273 		rtw_debugfs_add_r(bb_2c);
1274 		rtw_debugfs_add_r(bb_2d);
1275 		rtw_debugfs_add_r(bb_40);
1276 		rtw_debugfs_add_r(bb_41);
1277 	}
1278 	rtw_debugfs_add_r(rf_dump);
1279 	rtw_debugfs_add_r(tx_pwr_tbl);
1280 	rtw_debugfs_add_rw(edcca_enable);
1281 	rtw_debugfs_add_rw(fw_crash);
1282 	rtw_debugfs_add_rw(force_lowest_basic_rate);
1283 	rtw_debugfs_add_rw(dm_cap);
1284 }
1285 
1286 #endif /* CONFIG_RTW88_DEBUGFS */
1287 
1288 #ifdef CONFIG_RTW88_DEBUG
1289 
1290 void __rtw_dbg(struct rtw_dev *rtwdev, enum rtw_debug_mask mask,
1291 	       const char *fmt, ...)
1292 {
1293 	struct va_format vaf = {
1294 		.fmt = fmt,
1295 	};
1296 	va_list args;
1297 
1298 	va_start(args, fmt);
1299 	vaf.va = &args;
1300 
1301 	if (rtw_debug_mask & mask)
1302 		dev_printk(KERN_DEBUG, rtwdev->dev, "%pV", &vaf);
1303 
1304 	va_end(args);
1305 }
1306 EXPORT_SYMBOL(__rtw_dbg);
1307 
1308 #endif /* CONFIG_RTW88_DEBUG */
1309