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