1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (C) 2022 MediaTek Inc.
4 */
5
6 #include <linux/relay.h>
7 #include "mt7996.h"
8 #include "eeprom.h"
9 #include "mcu.h"
10 #include "mac.h"
11
12 #define FW_BIN_LOG_MAGIC 0x44d9c99a
13
14 /** global debugfs **/
15
16 struct hw_queue_map {
17 const char *name;
18 u8 index;
19 u8 pid;
20 u8 qid;
21 };
22
23 static int
mt7996_implicit_txbf_set(void * data,u64 val)24 mt7996_implicit_txbf_set(void *data, u64 val)
25 {
26 struct mt7996_dev *dev = data;
27
28 /* The existing connected stations shall reconnect to apply
29 * new implicit txbf configuration.
30 */
31 dev->ibf = !!val;
32
33 return mt7996_mcu_set_txbf(dev, BF_HW_EN_UPDATE);
34 }
35
36 static int
mt7996_implicit_txbf_get(void * data,u64 * val)37 mt7996_implicit_txbf_get(void *data, u64 *val)
38 {
39 struct mt7996_dev *dev = data;
40
41 *val = dev->ibf;
42
43 return 0;
44 }
45
46 DEFINE_DEBUGFS_ATTRIBUTE(fops_implicit_txbf, mt7996_implicit_txbf_get,
47 mt7996_implicit_txbf_set, "%lld\n");
48
49 /* test knob of system error recovery */
50 static ssize_t
mt7996_sys_recovery_set(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)51 mt7996_sys_recovery_set(struct file *file, const char __user *user_buf,
52 size_t count, loff_t *ppos)
53 {
54 struct mt7996_dev *dev = file->private_data;
55 char buf[16], *sep;
56 int ret = 0;
57 u16 band, val;
58
59 if (count >= sizeof(buf))
60 return -EINVAL;
61
62 if (copy_from_user(buf, user_buf, count))
63 return -EFAULT;
64
65 if (count && buf[count - 1] == '\n')
66 buf[count - 1] = '\0';
67 else
68 buf[count] = '\0';
69
70 sep = strchr(buf, ',');
71 if (!sep)
72 return -EINVAL;
73
74 *sep = 0;
75 if (kstrtou16(buf, 0, &band) || kstrtou16(sep + 1, 0, &val))
76 return -EINVAL;
77
78 switch (val) {
79 /*
80 * <band>,0: grab firmware current SER state.
81 * <band>,1: trigger & enable system error L1 recovery.
82 * <band>,2: trigger & enable system error L2 recovery.
83 * <band>,3: trigger & enable system error L3 rx abort.
84 * <band>,4: trigger & enable system error L3 tx abort
85 * <band>,5: trigger & enable system error L3 tx disable.
86 * <band>,6: trigger & enable system error L3 bf recovery.
87 * <band>,7: trigger & enable system error L4 mdp recovery.
88 * <band>,8: trigger & enable system error full recovery.
89 * <band>,9: trigger firmware crash.
90 */
91 case UNI_CMD_SER_QUERY:
92 ret = mt7996_mcu_set_ser(dev, UNI_CMD_SER_QUERY, 0, band);
93 break;
94 case UNI_CMD_SER_SET_RECOVER_L1:
95 case UNI_CMD_SER_SET_RECOVER_L2:
96 case UNI_CMD_SER_SET_RECOVER_L3_RX_ABORT:
97 case UNI_CMD_SER_SET_RECOVER_L3_TX_ABORT:
98 case UNI_CMD_SER_SET_RECOVER_L3_TX_DISABLE:
99 case UNI_CMD_SER_SET_RECOVER_L3_BF:
100 case UNI_CMD_SER_SET_RECOVER_L4_MDP:
101 ret = mt7996_mcu_set_ser(dev, UNI_CMD_SER_SET, BIT(val), band);
102 if (ret)
103 return ret;
104
105 ret = mt7996_mcu_set_ser(dev, UNI_CMD_SER_TRIGGER, val, band);
106 break;
107
108 /* enable full chip reset */
109 case UNI_CMD_SER_SET_RECOVER_FULL:
110 mt76_set(dev, MT_WFDMA0_MCU_HOST_INT_ENA, MT_MCU_CMD_WDT_MASK);
111 dev->recovery.state |= MT_MCU_CMD_WDT_MASK;
112 mt7996_reset(dev);
113 break;
114
115 /* WARNING: trigger firmware crash */
116 case UNI_CMD_SER_SET_SYSTEM_ASSERT:
117 ret = mt7996_mcu_trigger_assert(dev);
118 if (ret)
119 return ret;
120 break;
121 default:
122 break;
123 }
124
125 return ret ? ret : count;
126 }
127
128 static ssize_t
mt7996_sys_recovery_get(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)129 mt7996_sys_recovery_get(struct file *file, char __user *user_buf,
130 size_t count, loff_t *ppos)
131 {
132 struct mt7996_dev *dev = file->private_data;
133 char *buff;
134 int desc = 0;
135 ssize_t ret;
136 static const size_t bufsz = 1024;
137
138 buff = kmalloc(bufsz, GFP_KERNEL);
139 if (!buff)
140 return -ENOMEM;
141
142 /* HELP */
143 desc += scnprintf(buff + desc, bufsz - desc,
144 "Please echo the correct value ...\n");
145 desc += scnprintf(buff + desc, bufsz - desc,
146 "<band>,0: grab firmware transient SER state\n");
147 desc += scnprintf(buff + desc, bufsz - desc,
148 "<band>,1: trigger system error L1 recovery\n");
149 desc += scnprintf(buff + desc, bufsz - desc,
150 "<band>,2: trigger system error L2 recovery\n");
151 desc += scnprintf(buff + desc, bufsz - desc,
152 "<band>,3: trigger system error L3 rx abort\n");
153 desc += scnprintf(buff + desc, bufsz - desc,
154 "<band>,4: trigger system error L3 tx abort\n");
155 desc += scnprintf(buff + desc, bufsz - desc,
156 "<band>,5: trigger system error L3 tx disable\n");
157 desc += scnprintf(buff + desc, bufsz - desc,
158 "<band>,6: trigger system error L3 bf recovery\n");
159 desc += scnprintf(buff + desc, bufsz - desc,
160 "<band>,7: trigger system error L4 mdp recovery\n");
161 desc += scnprintf(buff + desc, bufsz - desc,
162 "<band>,8: trigger system error full recovery\n");
163 desc += scnprintf(buff + desc, bufsz - desc,
164 "<band>,9: trigger firmware crash\n");
165
166 /* SER statistics */
167 desc += scnprintf(buff + desc, bufsz - desc,
168 "\nlet's dump firmware SER statistics...\n");
169 desc += scnprintf(buff + desc, bufsz - desc,
170 "::E R , SER_STATUS = 0x%08x\n",
171 mt76_rr(dev, MT_SWDEF_SER_STATS));
172 desc += scnprintf(buff + desc, bufsz - desc,
173 "::E R , SER_PLE_ERR = 0x%08x\n",
174 mt76_rr(dev, MT_SWDEF_PLE_STATS));
175 desc += scnprintf(buff + desc, bufsz - desc,
176 "::E R , SER_PLE_ERR_1 = 0x%08x\n",
177 mt76_rr(dev, MT_SWDEF_PLE1_STATS));
178 desc += scnprintf(buff + desc, bufsz - desc,
179 "::E R , SER_PLE_ERR_AMSDU = 0x%08x\n",
180 mt76_rr(dev, MT_SWDEF_PLE_AMSDU_STATS));
181 desc += scnprintf(buff + desc, bufsz - desc,
182 "::E R , SER_PSE_ERR = 0x%08x\n",
183 mt76_rr(dev, MT_SWDEF_PSE_STATS));
184 desc += scnprintf(buff + desc, bufsz - desc,
185 "::E R , SER_PSE_ERR_1 = 0x%08x\n",
186 mt76_rr(dev, MT_SWDEF_PSE1_STATS));
187 desc += scnprintf(buff + desc, bufsz - desc,
188 "::E R , SER_LMAC_WISR6_B0 = 0x%08x\n",
189 mt76_rr(dev, MT_SWDEF_LAMC_WISR6_BN0_STATS));
190 desc += scnprintf(buff + desc, bufsz - desc,
191 "::E R , SER_LMAC_WISR6_B1 = 0x%08x\n",
192 mt76_rr(dev, MT_SWDEF_LAMC_WISR6_BN1_STATS));
193 desc += scnprintf(buff + desc, bufsz - desc,
194 "::E R , SER_LMAC_WISR6_B2 = 0x%08x\n",
195 mt76_rr(dev, MT_SWDEF_LAMC_WISR6_BN2_STATS));
196 desc += scnprintf(buff + desc, bufsz - desc,
197 "::E R , SER_LMAC_WISR7_B0 = 0x%08x\n",
198 mt76_rr(dev, MT_SWDEF_LAMC_WISR7_BN0_STATS));
199 desc += scnprintf(buff + desc, bufsz - desc,
200 "::E R , SER_LMAC_WISR7_B1 = 0x%08x\n",
201 mt76_rr(dev, MT_SWDEF_LAMC_WISR7_BN1_STATS));
202 desc += scnprintf(buff + desc, bufsz - desc,
203 "::E R , SER_LMAC_WISR7_B2 = 0x%08x\n",
204 mt76_rr(dev, MT_SWDEF_LAMC_WISR7_BN2_STATS));
205 desc += scnprintf(buff + desc, bufsz - desc,
206 "\nSYS_RESET_COUNT: WM %d, WA %d\n",
207 dev->recovery.wm_reset_count,
208 dev->recovery.wa_reset_count);
209
210 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
211 kfree(buff);
212 return ret;
213 }
214
215 static const struct file_operations mt7996_sys_recovery_ops = {
216 .write = mt7996_sys_recovery_set,
217 .read = mt7996_sys_recovery_get,
218 .open = simple_open,
219 .llseek = default_llseek,
220 };
221
222 static int
mt7996_radar_trigger(void * data,u64 val)223 mt7996_radar_trigger(void *data, u64 val)
224 {
225 struct mt7996_dev *dev = data;
226
227 if (val > MT_RX_SEL2)
228 return -EINVAL;
229
230 if (val == MT_RX_SEL2 && !dev->rdd2_phy) {
231 dev_err(dev->mt76.dev, "Background radar is not enabled\n");
232 return -EINVAL;
233 }
234
235 return mt7996_mcu_rdd_cmd(dev, RDD_RADAR_EMULATE,
236 val, 0, 0);
237 }
238
239 DEFINE_DEBUGFS_ATTRIBUTE(fops_radar_trigger, NULL,
240 mt7996_radar_trigger, "%lld\n");
241
242 static int
mt7996_rdd_monitor(struct seq_file * s,void * data)243 mt7996_rdd_monitor(struct seq_file *s, void *data)
244 {
245 struct mt7996_dev *dev = dev_get_drvdata(s->private);
246 struct cfg80211_chan_def *chandef = &dev->rdd2_chandef;
247 const char *bw;
248 int ret = 0;
249
250 mutex_lock(&dev->mt76.mutex);
251
252 if (!cfg80211_chandef_valid(chandef)) {
253 ret = -EINVAL;
254 goto out;
255 }
256
257 if (!dev->rdd2_phy) {
258 seq_puts(s, "not running\n");
259 goto out;
260 }
261
262 switch (chandef->width) {
263 case NL80211_CHAN_WIDTH_40:
264 bw = "40";
265 break;
266 case NL80211_CHAN_WIDTH_80:
267 bw = "80";
268 break;
269 case NL80211_CHAN_WIDTH_160:
270 bw = "160";
271 break;
272 case NL80211_CHAN_WIDTH_80P80:
273 bw = "80P80";
274 break;
275 default:
276 bw = "20";
277 break;
278 }
279
280 seq_printf(s, "channel %d (%d MHz) width %s MHz center1: %d MHz\n",
281 chandef->chan->hw_value, chandef->chan->center_freq,
282 bw, chandef->center_freq1);
283 out:
284 mutex_unlock(&dev->mt76.mutex);
285
286 return ret;
287 }
288
289 static int
mt7996_fw_debug_wm_set(void * data,u64 val)290 mt7996_fw_debug_wm_set(void *data, u64 val)
291 {
292 struct mt7996_dev *dev = data;
293 enum {
294 DEBUG_TXCMD = 62,
295 DEBUG_CMD_RPT_TX,
296 DEBUG_CMD_RPT_TRIG,
297 DEBUG_SPL,
298 DEBUG_RPT_RX,
299 DEBUG_RPT_RA = 68,
300 } debug;
301 bool tx, rx, en;
302 int ret;
303
304 dev->fw_debug_wm = val ? MCU_FW_LOG_TO_HOST : 0;
305
306 if (dev->fw_debug_bin)
307 val = MCU_FW_LOG_RELAY;
308 else
309 val = dev->fw_debug_wm;
310
311 tx = dev->fw_debug_wm || (dev->fw_debug_bin & BIT(1));
312 rx = dev->fw_debug_wm || (dev->fw_debug_bin & BIT(2));
313 en = dev->fw_debug_wm || (dev->fw_debug_bin & BIT(0));
314
315 ret = mt7996_mcu_fw_log_2_host(dev, MCU_FW_LOG_WM, val);
316 if (ret)
317 return ret;
318
319 for (debug = DEBUG_TXCMD; debug <= DEBUG_RPT_RA; debug++) {
320 if (debug == 67)
321 continue;
322
323 if (debug == DEBUG_RPT_RX)
324 val = en && rx;
325 else
326 val = en && tx;
327
328 ret = mt7996_mcu_fw_dbg_ctrl(dev, debug, val);
329 if (ret)
330 return ret;
331 }
332
333 return 0;
334 }
335
336 static int
mt7996_fw_debug_wm_get(void * data,u64 * val)337 mt7996_fw_debug_wm_get(void *data, u64 *val)
338 {
339 struct mt7996_dev *dev = data;
340
341 *val = dev->fw_debug_wm;
342
343 return 0;
344 }
345
346 DEFINE_DEBUGFS_ATTRIBUTE(fops_fw_debug_wm, mt7996_fw_debug_wm_get,
347 mt7996_fw_debug_wm_set, "%lld\n");
348
349 static int
mt7996_fw_debug_wa_set(void * data,u64 val)350 mt7996_fw_debug_wa_set(void *data, u64 val)
351 {
352 struct mt7996_dev *dev = data;
353 int ret;
354
355 dev->fw_debug_wa = val ? MCU_FW_LOG_TO_HOST : 0;
356
357 ret = mt7996_mcu_fw_log_2_host(dev, MCU_FW_LOG_WA, dev->fw_debug_wa);
358 if (ret)
359 return ret;
360
361 return mt7996_mcu_wa_cmd(dev, MCU_WA_PARAM_CMD(SET), MCU_WA_PARAM_PDMA_RX,
362 !!dev->fw_debug_wa, 0);
363 }
364
365 static int
mt7996_fw_debug_wa_get(void * data,u64 * val)366 mt7996_fw_debug_wa_get(void *data, u64 *val)
367 {
368 struct mt7996_dev *dev = data;
369
370 *val = dev->fw_debug_wa;
371
372 return 0;
373 }
374
375 DEFINE_DEBUGFS_ATTRIBUTE(fops_fw_debug_wa, mt7996_fw_debug_wa_get,
376 mt7996_fw_debug_wa_set, "%lld\n");
377
378 static struct dentry *
create_buf_file_cb(const char * filename,struct dentry * parent,umode_t mode,struct rchan_buf * buf,int * is_global)379 create_buf_file_cb(const char *filename, struct dentry *parent, umode_t mode,
380 struct rchan_buf *buf, int *is_global)
381 {
382 struct dentry *f;
383
384 f = debugfs_create_file("fwlog_data", mode, parent, buf,
385 &relay_file_operations);
386 if (IS_ERR(f))
387 return NULL;
388
389 *is_global = 1;
390
391 return f;
392 }
393
394 static int
remove_buf_file_cb(struct dentry * f)395 remove_buf_file_cb(struct dentry *f)
396 {
397 debugfs_remove(f);
398
399 return 0;
400 }
401
402 static int
mt7996_fw_debug_bin_set(void * data,u64 val)403 mt7996_fw_debug_bin_set(void *data, u64 val)
404 {
405 static struct rchan_callbacks relay_cb = {
406 .create_buf_file = create_buf_file_cb,
407 .remove_buf_file = remove_buf_file_cb,
408 };
409 struct mt7996_dev *dev = data;
410
411 if (!dev->relay_fwlog)
412 dev->relay_fwlog = relay_open("fwlog_data", dev->debugfs_dir,
413 1500, 512, &relay_cb, NULL);
414 if (!dev->relay_fwlog)
415 return -ENOMEM;
416
417 dev->fw_debug_bin = val;
418
419 relay_reset(dev->relay_fwlog);
420
421 return mt7996_fw_debug_wm_set(dev, dev->fw_debug_wm);
422 }
423
424 static int
mt7996_fw_debug_bin_get(void * data,u64 * val)425 mt7996_fw_debug_bin_get(void *data, u64 *val)
426 {
427 struct mt7996_dev *dev = data;
428
429 *val = dev->fw_debug_bin;
430
431 return 0;
432 }
433
434 DEFINE_DEBUGFS_ATTRIBUTE(fops_fw_debug_bin, mt7996_fw_debug_bin_get,
435 mt7996_fw_debug_bin_set, "%lld\n");
436
437 static int
mt7996_fw_util_wa_show(struct seq_file * file,void * data)438 mt7996_fw_util_wa_show(struct seq_file *file, void *data)
439 {
440 struct mt7996_dev *dev = file->private;
441
442 if (dev->fw_debug_wa)
443 return mt7996_mcu_wa_cmd(dev, MCU_WA_PARAM_CMD(QUERY),
444 MCU_WA_PARAM_CPU_UTIL, 0, 0);
445
446 return 0;
447 }
448
449 DEFINE_SHOW_ATTRIBUTE(mt7996_fw_util_wa);
450
451 static void
mt7996_ampdu_stat_read_phy(struct mt7996_phy * phy,struct seq_file * file)452 mt7996_ampdu_stat_read_phy(struct mt7996_phy *phy, struct seq_file *file)
453 {
454 struct mt7996_dev *dev = phy->dev;
455 int bound[15], range[8], i;
456 u8 band_idx = phy->mt76->band_idx;
457
458 /* Tx ampdu stat */
459 for (i = 0; i < ARRAY_SIZE(range); i++)
460 range[i] = mt76_rr(dev, MT_MIB_ARNG(band_idx, i));
461
462 for (i = 0; i < ARRAY_SIZE(bound); i++)
463 bound[i] = MT_MIB_ARNCR_RANGE(range[i / 2], i % 2) + 1;
464
465 seq_printf(file, "\nPhy %s, Phy band %d\n",
466 wiphy_name(phy->mt76->hw->wiphy), band_idx);
467
468 seq_printf(file, "Length: %8d | ", bound[0]);
469 for (i = 0; i < ARRAY_SIZE(bound) - 1; i++)
470 seq_printf(file, "%3d -%3d | ",
471 bound[i] + 1, bound[i + 1]);
472
473 seq_puts(file, "\nCount: ");
474 for (i = 0; i < ARRAY_SIZE(bound); i++)
475 seq_printf(file, "%8d | ", phy->mt76->aggr_stats[i]);
476 seq_puts(file, "\n");
477
478 seq_printf(file, "BA miss count: %d\n", phy->mib.ba_miss_cnt);
479 }
480
481 static void
mt7996_txbf_stat_read_phy(struct mt7996_phy * phy,struct seq_file * s)482 mt7996_txbf_stat_read_phy(struct mt7996_phy *phy, struct seq_file *s)
483 {
484 struct mt76_mib_stats *mib = &phy->mib;
485 static const char * const bw[] = {
486 "BW20", "BW40", "BW80", "BW160", "BW320"
487 };
488
489 /* Tx Beamformer monitor */
490 seq_puts(s, "\nTx Beamformer applied PPDU counts: ");
491
492 seq_printf(s, "iBF: %d, eBF: %d\n",
493 mib->tx_bf_ibf_ppdu_cnt,
494 mib->tx_bf_ebf_ppdu_cnt);
495
496 /* Tx Beamformer Rx feedback monitor */
497 seq_puts(s, "Tx Beamformer Rx feedback statistics: ");
498
499 seq_printf(s, "All: %d, EHT: %d, HE: %d, VHT: %d, HT: %d, ",
500 mib->tx_bf_rx_fb_all_cnt,
501 mib->tx_bf_rx_fb_eht_cnt,
502 mib->tx_bf_rx_fb_he_cnt,
503 mib->tx_bf_rx_fb_vht_cnt,
504 mib->tx_bf_rx_fb_ht_cnt);
505
506 seq_printf(s, "%s, NC: %d, NR: %d\n",
507 bw[mib->tx_bf_rx_fb_bw],
508 mib->tx_bf_rx_fb_nc_cnt,
509 mib->tx_bf_rx_fb_nr_cnt);
510
511 /* Tx Beamformee Rx NDPA & Tx feedback report */
512 seq_printf(s, "Tx Beamformee successful feedback frames: %d\n",
513 mib->tx_bf_fb_cpl_cnt);
514 seq_printf(s, "Tx Beamformee feedback triggered counts: %d\n",
515 mib->tx_bf_fb_trig_cnt);
516
517 /* Tx SU & MU counters */
518 seq_printf(s, "Tx multi-user Beamforming counts: %d\n",
519 mib->tx_mu_bf_cnt);
520 seq_printf(s, "Tx multi-user MPDU counts: %d\n", mib->tx_mu_mpdu_cnt);
521 seq_printf(s, "Tx multi-user successful MPDU counts: %d\n",
522 mib->tx_mu_acked_mpdu_cnt);
523 seq_printf(s, "Tx single-user successful MPDU counts: %d\n",
524 mib->tx_su_acked_mpdu_cnt);
525
526 seq_puts(s, "\n");
527 }
528
529 static void
mt7996_tx_stats_show_phy(struct seq_file * file,struct mt7996_phy * phy)530 mt7996_tx_stats_show_phy(struct seq_file *file, struct mt7996_phy *phy)
531 {
532 struct mt76_mib_stats *mib = &phy->mib;
533 u32 attempts, success, per;
534 int i;
535
536 mt7996_mac_update_stats(phy);
537 mt7996_ampdu_stat_read_phy(phy, file);
538
539 attempts = mib->tx_mpdu_attempts_cnt;
540 success = mib->tx_mpdu_success_cnt;
541 per = attempts ? 100 - success * 100 / attempts : 100;
542 seq_printf(file, "Tx attempts: %8u (MPDUs)\n", attempts);
543 seq_printf(file, "Tx success: %8u (MPDUs)\n", success);
544 seq_printf(file, "Tx PER: %u%%\n", per);
545
546 mt7996_txbf_stat_read_phy(phy, file);
547
548 /* Tx amsdu info */
549 seq_puts(file, "Tx MSDU statistics:\n");
550 for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++) {
551 seq_printf(file, "AMSDU pack count of %d MSDU in TXD: %8d ",
552 i + 1, mib->tx_amsdu[i]);
553 if (mib->tx_amsdu_cnt)
554 seq_printf(file, "(%3d%%)\n",
555 mib->tx_amsdu[i] * 100 / mib->tx_amsdu_cnt);
556 else
557 seq_puts(file, "\n");
558 }
559 }
560
561 static int
mt7996_tx_stats_show(struct seq_file * file,void * data)562 mt7996_tx_stats_show(struct seq_file *file, void *data)
563 {
564 struct mt7996_dev *dev = file->private;
565 struct mt7996_phy *phy = &dev->phy;
566
567 mutex_lock(&dev->mt76.mutex);
568
569 mt7996_tx_stats_show_phy(file, phy);
570 phy = mt7996_phy2(dev);
571 if (phy)
572 mt7996_tx_stats_show_phy(file, phy);
573 phy = mt7996_phy3(dev);
574 if (phy)
575 mt7996_tx_stats_show_phy(file, phy);
576
577 mutex_unlock(&dev->mt76.mutex);
578
579 return 0;
580 }
581
582 DEFINE_SHOW_ATTRIBUTE(mt7996_tx_stats);
583
584 static void
mt7996_hw_queue_read(struct seq_file * s,u32 size,const struct hw_queue_map * map)585 mt7996_hw_queue_read(struct seq_file *s, u32 size,
586 const struct hw_queue_map *map)
587 {
588 struct mt7996_phy *phy = s->private;
589 struct mt7996_dev *dev = phy->dev;
590 u32 i, val;
591
592 val = mt76_rr(dev, MT_FL_Q_EMPTY);
593 for (i = 0; i < size; i++) {
594 u32 ctrl, head, tail, queued;
595
596 if (val & BIT(map[i].index))
597 continue;
598
599 ctrl = BIT(31) | (map[i].pid << 10) | ((u32)map[i].qid << 24);
600 mt76_wr(dev, MT_FL_Q0_CTRL, ctrl);
601
602 head = mt76_get_field(dev, MT_FL_Q2_CTRL,
603 GENMASK(11, 0));
604 tail = mt76_get_field(dev, MT_FL_Q2_CTRL,
605 GENMASK(27, 16));
606 queued = mt76_get_field(dev, MT_FL_Q3_CTRL,
607 GENMASK(11, 0));
608
609 seq_printf(s, "\t%s: ", map[i].name);
610 seq_printf(s, "queued:0x%03x head:0x%03x tail:0x%03x\n",
611 queued, head, tail);
612 }
613 }
614
615 static void
mt7996_sta_hw_queue_read(void * data,struct ieee80211_sta * sta)616 mt7996_sta_hw_queue_read(void *data, struct ieee80211_sta *sta)
617 {
618 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
619 struct mt7996_vif *mvif = msta->vif;
620 struct mt7996_dev *dev = mvif->deflink.phy->dev;
621 struct ieee80211_link_sta *link_sta;
622 struct seq_file *s = data;
623 struct ieee80211_vif *vif;
624 unsigned int link_id;
625
626 vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv);
627
628 rcu_read_lock();
629
630 for_each_sta_active_link(vif, sta, link_sta, link_id) {
631 struct mt7996_sta_link *msta_link;
632 struct mt76_vif_link *mlink;
633 u8 ac;
634
635 mlink = rcu_dereference(mvif->mt76.link[link_id]);
636 if (!mlink)
637 continue;
638
639 msta_link = rcu_dereference(msta->link[link_id]);
640 if (!msta_link)
641 continue;
642
643 for (ac = 0; ac < 4; ac++) {
644 u32 idx = msta_link->wcid.idx >> 5, qlen, ctrl, val;
645 u8 offs = msta_link->wcid.idx & GENMASK(4, 0);
646
647 ctrl = BIT(31) | BIT(11) | (ac << 24);
648 val = mt76_rr(dev, MT_PLE_AC_QEMPTY(ac, idx));
649
650 if (val & BIT(offs))
651 continue;
652
653 mt76_wr(dev,
654 MT_FL_Q0_CTRL, ctrl | msta_link->wcid.idx);
655 qlen = mt76_get_field(dev, MT_FL_Q3_CTRL,
656 GENMASK(11, 0));
657 seq_printf(s, "\tSTA %pM wcid %d: AC%d%d queued:%d\n",
658 sta->addr, msta_link->wcid.idx,
659 mlink->wmm_idx, ac, qlen);
660 }
661 }
662
663 rcu_read_unlock();
664 }
665
666 static int
mt7996_hw_queues_show(struct seq_file * file,void * data)667 mt7996_hw_queues_show(struct seq_file *file, void *data)
668 {
669 struct mt7996_dev *dev = file->private;
670 struct mt7996_phy *phy = &dev->phy;
671 static const struct hw_queue_map ple_queue_map[] = {
672 { "CPU_Q0", 0, 1, MT_CTX0 },
673 { "CPU_Q1", 1, 1, MT_CTX0 + 1 },
674 { "CPU_Q2", 2, 1, MT_CTX0 + 2 },
675 { "CPU_Q3", 3, 1, MT_CTX0 + 3 },
676 { "ALTX_Q0", 8, 2, MT_LMAC_ALTX0 },
677 { "BMC_Q0", 9, 2, MT_LMAC_BMC0 },
678 { "BCN_Q0", 10, 2, MT_LMAC_BCN0 },
679 { "PSMP_Q0", 11, 2, MT_LMAC_PSMP0 },
680 { "ALTX_Q1", 12, 2, MT_LMAC_ALTX0 + 4 },
681 { "BMC_Q1", 13, 2, MT_LMAC_BMC0 + 4 },
682 { "BCN_Q1", 14, 2, MT_LMAC_BCN0 + 4 },
683 { "PSMP_Q1", 15, 2, MT_LMAC_PSMP0 + 4 },
684 };
685 static const struct hw_queue_map pse_queue_map[] = {
686 { "CPU Q0", 0, 1, MT_CTX0 },
687 { "CPU Q1", 1, 1, MT_CTX0 + 1 },
688 { "CPU Q2", 2, 1, MT_CTX0 + 2 },
689 { "CPU Q3", 3, 1, MT_CTX0 + 3 },
690 { "HIF_Q0", 8, 0, MT_HIF0 },
691 { "HIF_Q1", 9, 0, MT_HIF0 + 1 },
692 { "HIF_Q2", 10, 0, MT_HIF0 + 2 },
693 { "HIF_Q3", 11, 0, MT_HIF0 + 3 },
694 { "HIF_Q4", 12, 0, MT_HIF0 + 4 },
695 { "HIF_Q5", 13, 0, MT_HIF0 + 5 },
696 { "LMAC_Q", 16, 2, 0 },
697 { "MDP_TXQ", 17, 2, 1 },
698 { "MDP_RXQ", 18, 2, 2 },
699 { "SEC_TXQ", 19, 2, 3 },
700 { "SEC_RXQ", 20, 2, 4 },
701 };
702 u32 val, head, tail;
703
704 /* ple queue */
705 val = mt76_rr(dev, MT_PLE_FREEPG_CNT);
706 head = mt76_get_field(dev, MT_PLE_FREEPG_HEAD_TAIL, GENMASK(11, 0));
707 tail = mt76_get_field(dev, MT_PLE_FREEPG_HEAD_TAIL, GENMASK(27, 16));
708 seq_puts(file, "PLE page info:\n");
709 seq_printf(file,
710 "\tTotal free page: 0x%08x head: 0x%03x tail: 0x%03x\n",
711 val, head, tail);
712
713 val = mt76_rr(dev, MT_PLE_PG_HIF_GROUP);
714 head = mt76_get_field(dev, MT_PLE_HIF_PG_INFO, GENMASK(11, 0));
715 tail = mt76_get_field(dev, MT_PLE_HIF_PG_INFO, GENMASK(27, 16));
716 seq_printf(file, "\tHIF free page: 0x%03x res: 0x%03x used: 0x%03x\n",
717 val, head, tail);
718
719 seq_puts(file, "PLE non-empty queue info:\n");
720 mt7996_hw_queue_read(file, ARRAY_SIZE(ple_queue_map),
721 &ple_queue_map[0]);
722
723 /* iterate per-sta ple queue */
724 ieee80211_iterate_stations_atomic(phy->mt76->hw,
725 mt7996_sta_hw_queue_read, file);
726 phy = mt7996_phy2(dev);
727 if (phy)
728 ieee80211_iterate_stations_atomic(phy->mt76->hw,
729 mt7996_sta_hw_queue_read, file);
730 phy = mt7996_phy3(dev);
731 if (phy)
732 ieee80211_iterate_stations_atomic(phy->mt76->hw,
733 mt7996_sta_hw_queue_read, file);
734
735 /* pse queue */
736 seq_puts(file, "PSE non-empty queue info:\n");
737 mt7996_hw_queue_read(file, ARRAY_SIZE(pse_queue_map),
738 &pse_queue_map[0]);
739
740 return 0;
741 }
742
743 DEFINE_SHOW_ATTRIBUTE(mt7996_hw_queues);
744
745 static int
mt7996_xmit_queues_show(struct seq_file * file,void * data)746 mt7996_xmit_queues_show(struct seq_file *file, void *data)
747 {
748 struct mt7996_dev *dev = file->private;
749 struct mt7996_phy *phy;
750 struct {
751 struct mt76_queue *q;
752 char *queue;
753 } queue_map[] = {
754 { dev->mphy.q_tx[MT_TXQ_BE], " MAIN0" },
755 { NULL, " MAIN1" },
756 { NULL, " MAIN2" },
757 { dev->mt76.q_mcu[MT_MCUQ_WM], " MCUWM" },
758 { dev->mt76.q_mcu[MT_MCUQ_WA], " MCUWA" },
759 { dev->mt76.q_mcu[MT_MCUQ_FWDL], "MCUFWDL" },
760 };
761 int i;
762
763 phy = mt7996_phy2(dev);
764 if (phy)
765 queue_map[1].q = phy->mt76->q_tx[MT_TXQ_BE];
766
767 phy = mt7996_phy3(dev);
768 if (phy)
769 queue_map[2].q = phy->mt76->q_tx[MT_TXQ_BE];
770
771 seq_puts(file, " queue | hw-queued | head | tail |\n");
772 for (i = 0; i < ARRAY_SIZE(queue_map); i++) {
773 struct mt76_queue *q = queue_map[i].q;
774
775 if (!q)
776 continue;
777
778 seq_printf(file, " %s | %9d | %9d | %9d |\n",
779 queue_map[i].queue, q->queued, q->head,
780 q->tail);
781 }
782
783 return 0;
784 }
785
786 DEFINE_SHOW_ATTRIBUTE(mt7996_xmit_queues);
787
788 static int
mt7996_twt_stats(struct seq_file * s,void * data)789 mt7996_twt_stats(struct seq_file *s, void *data)
790 {
791 struct mt7996_dev *dev = dev_get_drvdata(s->private);
792 struct mt7996_twt_flow *iter;
793
794 rcu_read_lock();
795
796 seq_puts(s, " wcid | id | flags | exp | mantissa");
797 seq_puts(s, " | duration | tsf |\n");
798 list_for_each_entry_rcu(iter, &dev->twt_list, list)
799 seq_printf(s,
800 "%9d | %8d | %5c%c%c%c | %8d | %8d | %8d | %14lld |\n",
801 iter->wcid, iter->id,
802 iter->sched ? 's' : 'u',
803 iter->protection ? 'p' : '-',
804 iter->trigger ? 't' : '-',
805 iter->flowtype ? '-' : 'a',
806 iter->exp, iter->mantissa,
807 iter->duration, iter->tsf);
808
809 rcu_read_unlock();
810
811 return 0;
812 }
813
814 /* The index of RF registers use the generic regidx, combined with two parts:
815 * WF selection [31:24] and offset [23:0].
816 */
817 static int
mt7996_rf_regval_get(void * data,u64 * val)818 mt7996_rf_regval_get(void *data, u64 *val)
819 {
820 struct mt7996_dev *dev = data;
821 u32 regval;
822 int ret;
823
824 ret = mt7996_mcu_rf_regval(dev, dev->mt76.debugfs_reg, ®val, false);
825 if (ret)
826 return ret;
827
828 *val = regval;
829
830 return 0;
831 }
832
833 static int
mt7996_rf_regval_set(void * data,u64 val)834 mt7996_rf_regval_set(void *data, u64 val)
835 {
836 struct mt7996_dev *dev = data;
837 u32 val32 = val;
838
839 return mt7996_mcu_rf_regval(dev, dev->mt76.debugfs_reg, &val32, true);
840 }
841
842 DEFINE_DEBUGFS_ATTRIBUTE(fops_rf_regval, mt7996_rf_regval_get,
843 mt7996_rf_regval_set, "0x%08llx\n");
844
mt7996_init_debugfs(struct mt7996_dev * dev)845 int mt7996_init_debugfs(struct mt7996_dev *dev)
846 {
847 struct dentry *dir;
848
849 dir = mt76_register_debugfs_fops(&dev->mphy, NULL);
850 if (!dir)
851 return -ENOMEM;
852
853 debugfs_create_file("hw-queues", 0400, dir, dev,
854 &mt7996_hw_queues_fops);
855 debugfs_create_file("xmit-queues", 0400, dir, dev,
856 &mt7996_xmit_queues_fops);
857 debugfs_create_file("tx_stats", 0400, dir, dev, &mt7996_tx_stats_fops);
858 debugfs_create_file("sys_recovery", 0600, dir, dev,
859 &mt7996_sys_recovery_ops);
860 debugfs_create_file("fw_debug_wm", 0600, dir, dev, &fops_fw_debug_wm);
861 debugfs_create_file("fw_debug_wa", 0600, dir, dev, &fops_fw_debug_wa);
862 debugfs_create_file("fw_debug_bin", 0600, dir, dev, &fops_fw_debug_bin);
863 /* TODO: wm fw cpu utilization */
864 debugfs_create_file("fw_util_wa", 0400, dir, dev,
865 &mt7996_fw_util_wa_fops);
866 debugfs_create_file("implicit_txbf", 0600, dir, dev,
867 &fops_implicit_txbf);
868 debugfs_create_devm_seqfile(dev->mt76.dev, "twt_stats", dir,
869 mt7996_twt_stats);
870 debugfs_create_file("rf_regval", 0600, dir, dev, &fops_rf_regval);
871
872 debugfs_create_u32("dfs_hw_pattern", 0400, dir, &dev->hw_pattern);
873 debugfs_create_file("radar_trigger", 0200, dir, dev,
874 &fops_radar_trigger);
875 debugfs_create_devm_seqfile(dev->mt76.dev, "rdd_monitor", dir,
876 mt7996_rdd_monitor);
877
878 dev->debugfs_dir = dir;
879
880 return 0;
881 }
882
883 static void
mt7996_debugfs_write_fwlog(struct mt7996_dev * dev,const void * hdr,int hdrlen,const void * data,int len)884 mt7996_debugfs_write_fwlog(struct mt7996_dev *dev, const void *hdr, int hdrlen,
885 const void *data, int len)
886 {
887 static DEFINE_SPINLOCK(lock);
888 unsigned long flags;
889 void *dest;
890
891 spin_lock_irqsave(&lock, flags);
892 dest = relay_reserve(dev->relay_fwlog, hdrlen + len + 4);
893 if (dest) {
894 *(u32 *)dest = hdrlen + len;
895 dest += 4;
896
897 if (hdrlen) {
898 memcpy(dest, hdr, hdrlen);
899 dest += hdrlen;
900 }
901
902 memcpy(dest, data, len);
903 relay_flush(dev->relay_fwlog);
904 }
905 spin_unlock_irqrestore(&lock, flags);
906 }
907
mt7996_debugfs_rx_fw_monitor(struct mt7996_dev * dev,const void * data,int len)908 void mt7996_debugfs_rx_fw_monitor(struct mt7996_dev *dev, const void *data, int len)
909 {
910 struct {
911 __le32 magic;
912 u8 version;
913 u8 _rsv;
914 __le16 serial_id;
915 __le32 timestamp;
916 __le16 msg_type;
917 __le16 len;
918 } hdr = {
919 .version = 0x1,
920 .magic = cpu_to_le32(FW_BIN_LOG_MAGIC),
921 .msg_type = cpu_to_le16(PKT_TYPE_RX_FW_MONITOR),
922 };
923
924 if (!dev->relay_fwlog)
925 return;
926
927 hdr.serial_id = cpu_to_le16(dev->fw_debug_seq++);
928 hdr.timestamp = cpu_to_le32(mt76_rr(dev, MT_LPON_FRCR(0)));
929 hdr.len = *(__le16 *)data;
930 mt7996_debugfs_write_fwlog(dev, &hdr, sizeof(hdr), data, len);
931 }
932
mt7996_debugfs_rx_log(struct mt7996_dev * dev,const void * data,int len)933 bool mt7996_debugfs_rx_log(struct mt7996_dev *dev, const void *data, int len)
934 {
935 if (get_unaligned_le32(data) != FW_BIN_LOG_MAGIC)
936 return false;
937
938 if (dev->relay_fwlog)
939 mt7996_debugfs_write_fwlog(dev, NULL, 0, data, len);
940
941 return true;
942 }
943
944 #ifdef CONFIG_MAC80211_DEBUGFS
945 /** per-station debugfs **/
946
mt7996_sta_fixed_rate_set(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)947 static ssize_t mt7996_sta_fixed_rate_set(struct file *file,
948 const char __user *user_buf,
949 size_t count, loff_t *ppos)
950 {
951 #define SHORT_PREAMBLE 0
952 #define LONG_PREAMBLE 1
953 struct ieee80211_sta *sta = file->private_data;
954 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
955 struct mt7996_dev *dev = msta->vif->deflink.phy->dev;
956 struct mt7996_sta_link *msta_link = &msta->deflink;
957 struct ra_rate phy = {};
958 char buf[100];
959 int ret;
960 u16 gi, ltf;
961
962 if (count >= sizeof(buf))
963 return -EINVAL;
964
965 if (copy_from_user(buf, user_buf, count))
966 return -EFAULT;
967
968 if (count && buf[count - 1] == '\n')
969 buf[count - 1] = '\0';
970 else
971 buf[count] = '\0';
972
973 /* mode - cck: 0, ofdm: 1, ht: 2, gf: 3, vht: 4, he_su: 8, he_er: 9 EHT: 15
974 * bw - bw20: 0, bw40: 1, bw80: 2, bw160: 3, BW320: 4
975 * nss - vht: 1~4, he: 1~4, eht: 1~4, others: ignore
976 * mcs - cck: 0~4, ofdm: 0~7, ht: 0~32, vht: 0~9, he_su: 0~11, he_er: 0~2, eht: 0~13
977 * gi - (ht/vht) lgi: 0, sgi: 1; (he) 0.8us: 0, 1.6us: 1, 3.2us: 2
978 * preamble - short: 1, long: 0
979 * ldpc - off: 0, on: 1
980 * stbc - off: 0, on: 1
981 * ltf - 1xltf: 0, 2xltf: 1, 4xltf: 2
982 */
983 if (sscanf(buf, "%hhu %hhu %hhu %hhu %hu %hhu %hhu %hhu %hhu %hu",
984 &phy.mode, &phy.bw, &phy.mcs, &phy.nss, &gi,
985 &phy.preamble, &phy.stbc, &phy.ldpc, &phy.spe, <f) != 10) {
986 dev_warn(dev->mt76.dev,
987 "format: Mode BW MCS NSS GI Preamble STBC LDPC SPE ltf\n");
988 goto out;
989 }
990
991 phy.wlan_idx = cpu_to_le16(msta_link->wcid.idx);
992 phy.gi = cpu_to_le16(gi);
993 phy.ltf = cpu_to_le16(ltf);
994 phy.ldpc = phy.ldpc ? 7 : 0;
995 phy.preamble = phy.preamble ? SHORT_PREAMBLE : LONG_PREAMBLE;
996
997 ret = mt7996_mcu_set_fixed_rate_ctrl(dev, &phy, 0);
998 if (ret)
999 return -EFAULT;
1000
1001 out:
1002 return count;
1003 }
1004
1005 static const struct file_operations fops_fixed_rate = {
1006 .write = mt7996_sta_fixed_rate_set,
1007 .open = simple_open,
1008 .owner = THIS_MODULE,
1009 .llseek = default_llseek,
1010 };
1011
1012 static int
mt7996_queues_show(struct seq_file * s,void * data)1013 mt7996_queues_show(struct seq_file *s, void *data)
1014 {
1015 struct ieee80211_sta *sta = s->private;
1016
1017 mt7996_sta_hw_queue_read(s, sta);
1018
1019 return 0;
1020 }
1021
1022 DEFINE_SHOW_ATTRIBUTE(mt7996_queues);
1023
mt7996_sta_add_debugfs(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct dentry * dir)1024 void mt7996_sta_add_debugfs(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1025 struct ieee80211_sta *sta, struct dentry *dir)
1026 {
1027 debugfs_create_file("fixed_rate", 0600, dir, sta, &fops_fixed_rate);
1028 debugfs_create_file("hw-queues", 0400, dir, sta, &mt7996_queues_fops);
1029 }
1030
1031 #endif
1032