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