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