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