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_h2c(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 	u8 param[8];
355 	int num;
356 
357 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 3);
358 
359 	num = sscanf(tmp, "%hhx,%hhx,%hhx,%hhx,%hhx,%hhx,%hhx,%hhx",
360 		     &param[0], &param[1], &param[2], &param[3],
361 		     &param[4], &param[5], &param[6], &param[7]);
362 	if (num != 8) {
363 		rtw_info(rtwdev, "invalid H2C command format for debug\n");
364 		return -EINVAL;
365 	}
366 
367 	rtw_fw_h2c_cmd_dbg(rtwdev, param);
368 
369 	return count;
370 }
371 
372 static ssize_t rtw_debugfs_set_rf_write(struct file *filp,
373 					const char __user *buffer,
374 					size_t count, loff_t *loff)
375 {
376 	struct rtw_debugfs_priv *debugfs_priv = filp->private_data;
377 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
378 	char tmp[32 + 1];
379 	u32 path, addr, mask, val;
380 	int num;
381 
382 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 4);
383 
384 	num = sscanf(tmp, "%x %x %x %x", &path, &addr, &mask, &val);
385 
386 	if (num !=  4) {
387 		rtw_warn(rtwdev, "invalid args, [path] [addr] [mask] [val]\n");
388 		return count;
389 	}
390 
391 	rtw_write_rf(rtwdev, path, addr, mask, val);
392 	rtw_dbg(rtwdev, RTW_DBG_DEBUGFS,
393 		"write_rf path:%d addr:0x%08x mask:0x%08x, val:0x%08x\n",
394 		path, addr, mask, val);
395 
396 	return count;
397 }
398 
399 static ssize_t rtw_debugfs_set_rf_read(struct file *filp,
400 				       const char __user *buffer,
401 				       size_t count, loff_t *loff)
402 {
403 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
404 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
405 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
406 	char tmp[32 + 1];
407 	u32 path, addr, mask;
408 	int num;
409 
410 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 3);
411 
412 	num = sscanf(tmp, "%x %x %x", &path, &addr, &mask);
413 
414 	if (num !=  3) {
415 		rtw_warn(rtwdev, "invalid args, [path] [addr] [mask] [val]\n");
416 		return count;
417 	}
418 
419 	debugfs_priv->rf_path = path;
420 	debugfs_priv->rf_addr = addr;
421 	debugfs_priv->rf_mask = mask;
422 
423 	return count;
424 }
425 
426 static int rtw_debug_get_mac_page(struct seq_file *m, void *v)
427 {
428 	struct rtw_debugfs_priv *debugfs_priv = m->private;
429 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
430 	u32 page = debugfs_priv->cb_data;
431 	int i, n;
432 	int max = 0xff;
433 
434 	rtw_read32(rtwdev, debugfs_priv->cb_data);
435 	for (n = 0; n <= max; ) {
436 		seq_printf(m, "\n%8.8x  ", n + page);
437 		for (i = 0; i < 4 && n <= max; i++, n += 4)
438 			seq_printf(m, "%8.8x    ",
439 				   rtw_read32(rtwdev, (page | n)));
440 	}
441 	seq_puts(m, "\n");
442 	return 0;
443 }
444 
445 static int rtw_debug_get_bb_page(struct seq_file *m, void *v)
446 {
447 	struct rtw_debugfs_priv *debugfs_priv = m->private;
448 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
449 	u32 page = debugfs_priv->cb_data;
450 	int i, n;
451 	int max = 0xff;
452 
453 	rtw_read32(rtwdev, debugfs_priv->cb_data);
454 	for (n = 0; n <= max; ) {
455 		seq_printf(m, "\n%8.8x  ", n + page);
456 		for (i = 0; i < 4 && n <= max; i++, n += 4)
457 			seq_printf(m, "%8.8x    ",
458 				   rtw_read32(rtwdev, (page | n)));
459 	}
460 	seq_puts(m, "\n");
461 	return 0;
462 }
463 
464 static int rtw_debug_get_rf_dump(struct seq_file *m, void *v)
465 {
466 	struct rtw_debugfs_priv *debugfs_priv = m->private;
467 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
468 	u32 addr, offset, data;
469 	u8 path;
470 
471 	for (path = 0; path < rtwdev->hal.rf_path_num; path++) {
472 		seq_printf(m, "RF path:%d\n", path);
473 		for (addr = 0; addr < 0x100; addr += 4) {
474 			seq_printf(m, "%8.8x  ", addr);
475 			for (offset = 0; offset < 4; offset++) {
476 				data = rtw_read_rf(rtwdev, path, addr + offset,
477 						   0xffffffff);
478 				seq_printf(m, "%8.8x    ", data);
479 			}
480 			seq_puts(m, "\n");
481 		}
482 		seq_puts(m, "\n");
483 	}
484 
485 	return 0;
486 }
487 
488 static void rtw_print_cck_rate_txt(struct seq_file *m, u8 rate)
489 {
490 	static const char * const
491 	cck_rate[] = {"1M", "2M", "5.5M", "11M"};
492 	u8 idx = rate - DESC_RATE1M;
493 
494 	seq_printf(m, " CCK_%-5s", cck_rate[idx]);
495 }
496 
497 static void rtw_print_ofdm_rate_txt(struct seq_file *m, u8 rate)
498 {
499 	static const char * const
500 	ofdm_rate[] = {"6M", "9M", "12M", "18M", "24M", "36M", "48M", "54M"};
501 	u8 idx = rate - DESC_RATE6M;
502 
503 	seq_printf(m, " OFDM_%-4s", ofdm_rate[idx]);
504 }
505 
506 static void rtw_print_ht_rate_txt(struct seq_file *m, u8 rate)
507 {
508 	u8 mcs_n = rate - DESC_RATEMCS0;
509 
510 	seq_printf(m, " MCS%-6u", mcs_n);
511 }
512 
513 static void rtw_print_vht_rate_txt(struct seq_file *m, u8 rate)
514 {
515 	u8 idx = rate - DESC_RATEVHT1SS_MCS0;
516 	u8 n_ss, mcs_n;
517 
518 	/* n spatial stream */
519 	n_ss = 1 + idx / 10;
520 	/* MCS n */
521 	mcs_n = idx % 10;
522 	seq_printf(m, " VHT%uSMCS%u", n_ss, mcs_n);
523 }
524 
525 static void rtw_print_rate(struct seq_file *m, u8 rate)
526 {
527 	switch (rate) {
528 	case DESC_RATE1M...DESC_RATE11M:
529 		rtw_print_cck_rate_txt(m, rate);
530 		break;
531 	case DESC_RATE6M...DESC_RATE54M:
532 		rtw_print_ofdm_rate_txt(m, rate);
533 		break;
534 	case DESC_RATEMCS0...DESC_RATEMCS15:
535 		rtw_print_ht_rate_txt(m, rate);
536 		break;
537 	case DESC_RATEVHT1SS_MCS0...DESC_RATEVHT2SS_MCS9:
538 		rtw_print_vht_rate_txt(m, rate);
539 		break;
540 	default:
541 		seq_printf(m, " Unknown rate=0x%x\n", rate);
542 		break;
543 	}
544 }
545 
546 static int rtw_debugfs_get_tx_pwr_tbl(struct seq_file *m, void *v)
547 {
548 	struct rtw_debugfs_priv *debugfs_priv = m->private;
549 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
550 	struct rtw_hal *hal = &rtwdev->hal;
551 	u8 path, rate;
552 	struct rtw_power_params pwr_param = {0};
553 	u8 bw = hal->current_band_width;
554 	u8 ch = hal->current_channel;
555 	u8 regd = rtwdev->regd.txpwr_regd;
556 
557 	seq_printf(m, "%-4s %-10s %-3s%6s %-4s %4s (%-4s %-4s) %-4s\n",
558 		   "path", "rate", "pwr", "", "base", "", "byr", "lmt", "rem");
559 
560 	mutex_lock(&hal->tx_power_mutex);
561 	for (path = RF_PATH_A; path <= RF_PATH_B; path++) {
562 		/* there is no CCK rates used in 5G */
563 		if (hal->current_band_type == RTW_BAND_5G)
564 			rate = DESC_RATE6M;
565 		else
566 			rate = DESC_RATE1M;
567 
568 		/* now, not support vht 3ss and vht 4ss*/
569 		for (; rate <= DESC_RATEVHT2SS_MCS9; rate++) {
570 			/* now, not support ht 3ss and ht 4ss*/
571 			if (rate > DESC_RATEMCS15 &&
572 			    rate < DESC_RATEVHT1SS_MCS0)
573 				continue;
574 
575 			rtw_get_tx_power_params(rtwdev, path, rate, bw,
576 						ch, regd, &pwr_param);
577 
578 			seq_printf(m, "%4c ", path + 'A');
579 			rtw_print_rate(m, rate);
580 			seq_printf(m, " %3u(0x%02x) %4u %4d (%4d %4d) %4d\n",
581 				   hal->tx_pwr_tbl[path][rate],
582 				   hal->tx_pwr_tbl[path][rate],
583 				   pwr_param.pwr_base,
584 				   min_t(s8, pwr_param.pwr_offset,
585 					 pwr_param.pwr_limit),
586 				   pwr_param.pwr_offset, pwr_param.pwr_limit,
587 				   pwr_param.pwr_remnant);
588 		}
589 	}
590 
591 	mutex_unlock(&hal->tx_power_mutex);
592 
593 	return 0;
594 }
595 
596 static int rtw_debugfs_get_phy_info(struct seq_file *m, void *v)
597 {
598 	struct rtw_debugfs_priv *debugfs_priv = m->private;
599 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
600 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
601 	struct rtw_traffic_stats *stats = &rtwdev->stats;
602 	struct rtw_pkt_count *last_cnt = &dm_info->last_pkt_count;
603 	struct rtw_efuse *efuse = &rtwdev->efuse;
604 	struct ewma_evm *ewma_evm = dm_info->ewma_evm;
605 	struct ewma_snr *ewma_snr = dm_info->ewma_snr;
606 	u8 ss, rate_id;
607 
608 	seq_puts(m, "==========[Common Info]========\n");
609 	seq_printf(m, "Is link = %c\n", rtw_is_assoc(rtwdev) ? 'Y' : 'N');
610 	seq_printf(m, "Current CH(fc) = %u\n", rtwdev->hal.current_channel);
611 	seq_printf(m, "Current BW = %u\n", rtwdev->hal.current_band_width);
612 	seq_printf(m, "Current IGI = 0x%x\n", dm_info->igi_history[0]);
613 	seq_printf(m, "TP {Tx, Rx} = {%u, %u}Mbps\n\n",
614 		   stats->tx_throughput, stats->rx_throughput);
615 
616 	seq_puts(m, "==========[Tx Phy Info]========\n");
617 	seq_puts(m, "[Tx Rate] = ");
618 	rtw_print_rate(m, dm_info->tx_rate);
619 	seq_printf(m, "(0x%x)\n\n", dm_info->tx_rate);
620 
621 	seq_puts(m, "==========[Rx Phy Info]========\n");
622 	seq_printf(m, "[Rx Beacon Count] = %u\n", last_cnt->num_bcn_pkt);
623 	seq_puts(m, "[Rx Rate] = ");
624 	rtw_print_rate(m, dm_info->curr_rx_rate);
625 	seq_printf(m, "(0x%x)\n", dm_info->curr_rx_rate);
626 
627 	seq_puts(m, "[Rx Rate Count]:\n");
628 	seq_printf(m, " * CCK = {%u, %u, %u, %u}\n",
629 		   last_cnt->num_qry_pkt[DESC_RATE1M],
630 		   last_cnt->num_qry_pkt[DESC_RATE2M],
631 		   last_cnt->num_qry_pkt[DESC_RATE5_5M],
632 		   last_cnt->num_qry_pkt[DESC_RATE11M]);
633 
634 	seq_printf(m, " * OFDM = {%u, %u, %u, %u, %u, %u, %u, %u}\n",
635 		   last_cnt->num_qry_pkt[DESC_RATE6M],
636 		   last_cnt->num_qry_pkt[DESC_RATE9M],
637 		   last_cnt->num_qry_pkt[DESC_RATE12M],
638 		   last_cnt->num_qry_pkt[DESC_RATE18M],
639 		   last_cnt->num_qry_pkt[DESC_RATE24M],
640 		   last_cnt->num_qry_pkt[DESC_RATE36M],
641 		   last_cnt->num_qry_pkt[DESC_RATE48M],
642 		   last_cnt->num_qry_pkt[DESC_RATE54M]);
643 
644 	for (ss = 0; ss < efuse->hw_cap.nss; ss++) {
645 		rate_id = DESC_RATEMCS0 + ss * 8;
646 		seq_printf(m, " * HT_MCS[%u:%u] = {%u, %u, %u, %u, %u, %u, %u, %u}\n",
647 			   ss * 8, ss * 8 + 7,
648 			   last_cnt->num_qry_pkt[rate_id],
649 			   last_cnt->num_qry_pkt[rate_id + 1],
650 			   last_cnt->num_qry_pkt[rate_id + 2],
651 			   last_cnt->num_qry_pkt[rate_id + 3],
652 			   last_cnt->num_qry_pkt[rate_id + 4],
653 			   last_cnt->num_qry_pkt[rate_id + 5],
654 			   last_cnt->num_qry_pkt[rate_id + 6],
655 			   last_cnt->num_qry_pkt[rate_id + 7]);
656 	}
657 
658 	for (ss = 0; ss < efuse->hw_cap.nss; ss++) {
659 		rate_id = DESC_RATEVHT1SS_MCS0 + ss * 10;
660 		seq_printf(m, " * VHT_MCS-%uss MCS[0:9] = {%u, %u, %u, %u, %u, %u, %u, %u, %u, %u}\n",
661 			   ss + 1,
662 			   last_cnt->num_qry_pkt[rate_id],
663 			   last_cnt->num_qry_pkt[rate_id + 1],
664 			   last_cnt->num_qry_pkt[rate_id + 2],
665 			   last_cnt->num_qry_pkt[rate_id + 3],
666 			   last_cnt->num_qry_pkt[rate_id + 4],
667 			   last_cnt->num_qry_pkt[rate_id + 5],
668 			   last_cnt->num_qry_pkt[rate_id + 6],
669 			   last_cnt->num_qry_pkt[rate_id + 7],
670 			   last_cnt->num_qry_pkt[rate_id + 8],
671 			   last_cnt->num_qry_pkt[rate_id + 9]);
672 	}
673 
674 	seq_printf(m, "[RSSI(dBm)] = {%d, %d}\n",
675 		   dm_info->rssi[RF_PATH_A] - 100,
676 		   dm_info->rssi[RF_PATH_B] - 100);
677 	seq_printf(m, "[Rx EVM(dB)] = {-%d, -%d}\n",
678 		   dm_info->rx_evm_dbm[RF_PATH_A],
679 		   dm_info->rx_evm_dbm[RF_PATH_B]);
680 	seq_printf(m, "[Rx SNR] = {%d, %d}\n",
681 		   dm_info->rx_snr[RF_PATH_A],
682 		   dm_info->rx_snr[RF_PATH_B]);
683 	seq_printf(m, "[CFO_tail(KHz)] = {%d, %d}\n",
684 		   dm_info->cfo_tail[RF_PATH_A],
685 		   dm_info->cfo_tail[RF_PATH_B]);
686 
687 	if (dm_info->curr_rx_rate >= DESC_RATE11M) {
688 		seq_puts(m, "[Rx Average Status]:\n");
689 		seq_printf(m, " * OFDM, EVM: {-%d}, SNR: {%d}\n",
690 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_OFDM]),
691 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_OFDM_A]));
692 		seq_printf(m, " * 1SS, EVM: {-%d}, SNR: {%d}\n",
693 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_1SS]),
694 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_1SS_A]));
695 		seq_printf(m, " * 2SS, EVM: {-%d, -%d}, SNR: {%d, %d}\n",
696 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_2SS_A]),
697 			   (u8)ewma_evm_read(&ewma_evm[RTW_EVM_2SS_B]),
698 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_2SS_A]),
699 			   (u8)ewma_snr_read(&ewma_snr[RTW_SNR_2SS_B]));
700 	}
701 
702 	seq_puts(m, "[Rx Counter]:\n");
703 	seq_printf(m, " * CCA (CCK, OFDM, Total) = (%u, %u, %u)\n",
704 		   dm_info->cck_cca_cnt,
705 		   dm_info->ofdm_cca_cnt,
706 		   dm_info->total_cca_cnt);
707 	seq_printf(m, " * False Alarm (CCK, OFDM, Total) = (%u, %u, %u)\n",
708 		   dm_info->cck_fa_cnt,
709 		   dm_info->ofdm_fa_cnt,
710 		   dm_info->total_fa_cnt);
711 	seq_printf(m, " * CCK cnt (ok, err) = (%u, %u)\n",
712 		   dm_info->cck_ok_cnt, dm_info->cck_err_cnt);
713 	seq_printf(m, " * OFDM cnt (ok, err) = (%u, %u)\n",
714 		   dm_info->ofdm_ok_cnt, dm_info->ofdm_err_cnt);
715 	seq_printf(m, " * HT cnt (ok, err) = (%u, %u)\n",
716 		   dm_info->ht_ok_cnt, dm_info->ht_err_cnt);
717 	seq_printf(m, " * VHT cnt (ok, err) = (%u, %u)\n",
718 		   dm_info->vht_ok_cnt, dm_info->vht_err_cnt);
719 
720 	return 0;
721 }
722 
723 static int rtw_debugfs_get_coex_info(struct seq_file *m, void *v)
724 {
725 	struct rtw_debugfs_priv *debugfs_priv = m->private;
726 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
727 
728 	rtw_coex_display_coex_info(rtwdev, m);
729 
730 	return 0;
731 }
732 
733 static ssize_t rtw_debugfs_set_coex_enable(struct file *filp,
734 					   const char __user *buffer,
735 					   size_t count, loff_t *loff)
736 {
737 	struct seq_file *seqpriv = (struct seq_file *)filp->private_data;
738 	struct rtw_debugfs_priv *debugfs_priv = seqpriv->private;
739 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
740 	struct rtw_coex *coex = &rtwdev->coex;
741 	char tmp[32 + 1];
742 	bool enable;
743 	int ret;
744 
745 	rtw_debugfs_copy_from_user(tmp, sizeof(tmp), buffer, count, 1);
746 
747 	ret = kstrtobool(tmp, &enable);
748 	if (ret) {
749 		rtw_warn(rtwdev, "invalid arguments\n");
750 		return ret;
751 	}
752 
753 	mutex_lock(&rtwdev->mutex);
754 	coex->stop_dm = enable == 0;
755 	mutex_unlock(&rtwdev->mutex);
756 
757 	return count;
758 }
759 
760 static int rtw_debugfs_get_coex_enable(struct seq_file *m, void *v)
761 {
762 	struct rtw_debugfs_priv *debugfs_priv = m->private;
763 	struct rtw_dev *rtwdev = debugfs_priv->rtwdev;
764 	struct rtw_coex *coex = &rtwdev->coex;
765 
766 	seq_printf(m, "coex mechanism %s\n",
767 		   coex->stop_dm ? "disabled" : "enabled");
768 
769 	return 0;
770 }
771 
772 #define rtw_debug_impl_mac(page, addr)				\
773 static struct rtw_debugfs_priv rtw_debug_priv_mac_ ##page = {	\
774 	.cb_read = rtw_debug_get_mac_page,			\
775 	.cb_data = addr,					\
776 }
777 
778 rtw_debug_impl_mac(0, 0x0000);
779 rtw_debug_impl_mac(1, 0x0100);
780 rtw_debug_impl_mac(2, 0x0200);
781 rtw_debug_impl_mac(3, 0x0300);
782 rtw_debug_impl_mac(4, 0x0400);
783 rtw_debug_impl_mac(5, 0x0500);
784 rtw_debug_impl_mac(6, 0x0600);
785 rtw_debug_impl_mac(7, 0x0700);
786 rtw_debug_impl_mac(10, 0x1000);
787 rtw_debug_impl_mac(11, 0x1100);
788 rtw_debug_impl_mac(12, 0x1200);
789 rtw_debug_impl_mac(13, 0x1300);
790 rtw_debug_impl_mac(14, 0x1400);
791 rtw_debug_impl_mac(15, 0x1500);
792 rtw_debug_impl_mac(16, 0x1600);
793 rtw_debug_impl_mac(17, 0x1700);
794 
795 #define rtw_debug_impl_bb(page, addr)			\
796 static struct rtw_debugfs_priv rtw_debug_priv_bb_ ##page = {	\
797 	.cb_read = rtw_debug_get_bb_page,			\
798 	.cb_data = addr,					\
799 }
800 
801 rtw_debug_impl_bb(8, 0x0800);
802 rtw_debug_impl_bb(9, 0x0900);
803 rtw_debug_impl_bb(a, 0x0a00);
804 rtw_debug_impl_bb(b, 0x0b00);
805 rtw_debug_impl_bb(c, 0x0c00);
806 rtw_debug_impl_bb(d, 0x0d00);
807 rtw_debug_impl_bb(e, 0x0e00);
808 rtw_debug_impl_bb(f, 0x0f00);
809 rtw_debug_impl_bb(18, 0x1800);
810 rtw_debug_impl_bb(19, 0x1900);
811 rtw_debug_impl_bb(1a, 0x1a00);
812 rtw_debug_impl_bb(1b, 0x1b00);
813 rtw_debug_impl_bb(1c, 0x1c00);
814 rtw_debug_impl_bb(1d, 0x1d00);
815 rtw_debug_impl_bb(1e, 0x1e00);
816 rtw_debug_impl_bb(1f, 0x1f00);
817 rtw_debug_impl_bb(2c, 0x2c00);
818 rtw_debug_impl_bb(2d, 0x2d00);
819 rtw_debug_impl_bb(40, 0x4000);
820 rtw_debug_impl_bb(41, 0x4100);
821 
822 static struct rtw_debugfs_priv rtw_debug_priv_rf_dump = {
823 	.cb_read = rtw_debug_get_rf_dump,
824 };
825 
826 static struct rtw_debugfs_priv rtw_debug_priv_tx_pwr_tbl = {
827 	.cb_read = rtw_debugfs_get_tx_pwr_tbl,
828 };
829 
830 static struct rtw_debugfs_priv rtw_debug_priv_write_reg = {
831 	.cb_write = rtw_debugfs_set_write_reg,
832 };
833 
834 static struct rtw_debugfs_priv rtw_debug_priv_h2c = {
835 	.cb_write = rtw_debugfs_set_h2c,
836 };
837 
838 static struct rtw_debugfs_priv rtw_debug_priv_rf_write = {
839 	.cb_write = rtw_debugfs_set_rf_write,
840 };
841 
842 static struct rtw_debugfs_priv rtw_debug_priv_rf_read = {
843 	.cb_write = rtw_debugfs_set_rf_read,
844 	.cb_read = rtw_debugfs_get_rf_read,
845 };
846 
847 static struct rtw_debugfs_priv rtw_debug_priv_read_reg = {
848 	.cb_write = rtw_debugfs_set_read_reg,
849 	.cb_read = rtw_debugfs_get_read_reg,
850 };
851 
852 static struct rtw_debugfs_priv rtw_debug_priv_dump_cam = {
853 	.cb_write = rtw_debugfs_set_single_input,
854 	.cb_read = rtw_debugfs_get_dump_cam,
855 };
856 
857 static struct rtw_debugfs_priv rtw_debug_priv_rsvd_page = {
858 	.cb_write = rtw_debugfs_set_rsvd_page,
859 	.cb_read = rtw_debugfs_get_rsvd_page,
860 };
861 
862 static struct rtw_debugfs_priv rtw_debug_priv_phy_info = {
863 	.cb_read = rtw_debugfs_get_phy_info,
864 };
865 
866 static struct rtw_debugfs_priv rtw_debug_priv_coex_enable = {
867 	.cb_write = rtw_debugfs_set_coex_enable,
868 	.cb_read = rtw_debugfs_get_coex_enable,
869 };
870 
871 static struct rtw_debugfs_priv rtw_debug_priv_coex_info = {
872 	.cb_read = rtw_debugfs_get_coex_info,
873 };
874 
875 #define rtw_debugfs_add_core(name, mode, fopname, parent)		\
876 	do {								\
877 		rtw_debug_priv_ ##name.rtwdev = rtwdev;			\
878 		if (!debugfs_create_file(#name, mode,			\
879 					 parent, &rtw_debug_priv_ ##name,\
880 					 &file_ops_ ##fopname))		\
881 			pr_debug("Unable to initialize debugfs:%s\n",	\
882 			       #name);					\
883 	} while (0)
884 
885 #define rtw_debugfs_add_w(name)						\
886 	rtw_debugfs_add_core(name, S_IFREG | 0222, common_write, debugfs_topdir)
887 #define rtw_debugfs_add_rw(name)					\
888 	rtw_debugfs_add_core(name, S_IFREG | 0666, single_rw, debugfs_topdir)
889 #define rtw_debugfs_add_r(name)						\
890 	rtw_debugfs_add_core(name, S_IFREG | 0444, single_r, debugfs_topdir)
891 
892 void rtw_debugfs_init(struct rtw_dev *rtwdev)
893 {
894 	struct dentry *debugfs_topdir;
895 
896 	debugfs_topdir = debugfs_create_dir("rtw88",
897 					    rtwdev->hw->wiphy->debugfsdir);
898 	rtw_debugfs_add_w(write_reg);
899 	rtw_debugfs_add_rw(read_reg);
900 	rtw_debugfs_add_w(rf_write);
901 	rtw_debugfs_add_rw(rf_read);
902 	rtw_debugfs_add_rw(dump_cam);
903 	rtw_debugfs_add_rw(rsvd_page);
904 	rtw_debugfs_add_r(phy_info);
905 	rtw_debugfs_add_r(coex_info);
906 	rtw_debugfs_add_rw(coex_enable);
907 	rtw_debugfs_add_w(h2c);
908 	rtw_debugfs_add_r(mac_0);
909 	rtw_debugfs_add_r(mac_1);
910 	rtw_debugfs_add_r(mac_2);
911 	rtw_debugfs_add_r(mac_3);
912 	rtw_debugfs_add_r(mac_4);
913 	rtw_debugfs_add_r(mac_5);
914 	rtw_debugfs_add_r(mac_6);
915 	rtw_debugfs_add_r(mac_7);
916 	rtw_debugfs_add_r(bb_8);
917 	rtw_debugfs_add_r(bb_9);
918 	rtw_debugfs_add_r(bb_a);
919 	rtw_debugfs_add_r(bb_b);
920 	rtw_debugfs_add_r(bb_c);
921 	rtw_debugfs_add_r(bb_d);
922 	rtw_debugfs_add_r(bb_e);
923 	rtw_debugfs_add_r(bb_f);
924 	rtw_debugfs_add_r(mac_10);
925 	rtw_debugfs_add_r(mac_11);
926 	rtw_debugfs_add_r(mac_12);
927 	rtw_debugfs_add_r(mac_13);
928 	rtw_debugfs_add_r(mac_14);
929 	rtw_debugfs_add_r(mac_15);
930 	rtw_debugfs_add_r(mac_16);
931 	rtw_debugfs_add_r(mac_17);
932 	rtw_debugfs_add_r(bb_18);
933 	rtw_debugfs_add_r(bb_19);
934 	rtw_debugfs_add_r(bb_1a);
935 	rtw_debugfs_add_r(bb_1b);
936 	rtw_debugfs_add_r(bb_1c);
937 	rtw_debugfs_add_r(bb_1d);
938 	rtw_debugfs_add_r(bb_1e);
939 	rtw_debugfs_add_r(bb_1f);
940 	if (rtwdev->chip->id == RTW_CHIP_TYPE_8822C) {
941 		rtw_debugfs_add_r(bb_2c);
942 		rtw_debugfs_add_r(bb_2d);
943 		rtw_debugfs_add_r(bb_40);
944 		rtw_debugfs_add_r(bb_41);
945 	}
946 	rtw_debugfs_add_r(rf_dump);
947 	rtw_debugfs_add_r(tx_pwr_tbl);
948 }
949 
950 #endif /* CONFIG_RTW88_DEBUGFS */
951 
952 #ifdef CONFIG_RTW88_DEBUG
953 
954 void __rtw_dbg(struct rtw_dev *rtwdev, enum rtw_debug_mask mask,
955 	       const char *fmt, ...)
956 {
957 	struct va_format vaf = {
958 		.fmt = fmt,
959 	};
960 	va_list args;
961 
962 	va_start(args, fmt);
963 	vaf.va = &args;
964 
965 	if (rtw_debug_mask & mask)
966 		dev_printk(KERN_DEBUG, rtwdev->dev, "%pV", &vaf);
967 
968 	va_end(args);
969 }
970 EXPORT_SYMBOL(__rtw_dbg);
971 
972 #endif /* CONFIG_RTW88_DEBUG */
973