1 // SPDX-License-Identifier: ISC
2 
3 #include "mt7615.h"
4 
5 static int
6 mt7615_reg_set(void *data, u64 val)
7 {
8 	struct mt7615_dev *dev = data;
9 
10 	mt7615_mutex_acquire(dev);
11 	mt76_wr(dev, dev->mt76.debugfs_reg, val);
12 	mt7615_mutex_release(dev);
13 
14 	return 0;
15 }
16 
17 static int
18 mt7615_reg_get(void *data, u64 *val)
19 {
20 	struct mt7615_dev *dev = data;
21 
22 	mt7615_mutex_acquire(dev);
23 	*val = mt76_rr(dev, dev->mt76.debugfs_reg);
24 	mt7615_mutex_release(dev);
25 
26 	return 0;
27 }
28 
29 DEFINE_DEBUGFS_ATTRIBUTE(fops_regval, mt7615_reg_get, mt7615_reg_set,
30 			 "0x%08llx\n");
31 
32 static int
33 mt7615_radar_pattern_set(void *data, u64 val)
34 {
35 	struct mt7615_dev *dev = data;
36 	int err;
37 
38 	if (!mt7615_wait_for_mcu_init(dev))
39 		return 0;
40 
41 	mt7615_mutex_acquire(dev);
42 	err = mt7615_mcu_rdd_send_pattern(dev);
43 	mt7615_mutex_release(dev);
44 
45 	return err;
46 }
47 
48 DEFINE_DEBUGFS_ATTRIBUTE(fops_radar_pattern, NULL,
49 			 mt7615_radar_pattern_set, "%lld\n");
50 
51 static int mt7615_config(void *data, u64 val)
52 {
53 	struct mt7615_dev *dev = data;
54 	int ret;
55 
56 	mt7615_mutex_acquire(dev);
57 	ret = mt76_connac_mcu_chip_config(&dev->mt76);
58 	mt7615_mutex_release(dev);
59 
60 	return ret;
61 }
62 
63 DEFINE_DEBUGFS_ATTRIBUTE(fops_config, NULL, mt7615_config, "%lld\n");
64 
65 static int
66 mt7615_scs_set(void *data, u64 val)
67 {
68 	struct mt7615_dev *dev = data;
69 	struct mt7615_phy *ext_phy;
70 
71 	if (!mt7615_wait_for_mcu_init(dev))
72 		return 0;
73 
74 	mt7615_mac_set_scs(&dev->phy, val);
75 	ext_phy = mt7615_ext_phy(dev);
76 	if (ext_phy)
77 		mt7615_mac_set_scs(ext_phy, val);
78 
79 	return 0;
80 }
81 
82 static int
83 mt7615_scs_get(void *data, u64 *val)
84 {
85 	struct mt7615_dev *dev = data;
86 
87 	*val = dev->phy.scs_en;
88 
89 	return 0;
90 }
91 
92 DEFINE_DEBUGFS_ATTRIBUTE(fops_scs, mt7615_scs_get,
93 			 mt7615_scs_set, "%lld\n");
94 
95 static int
96 mt7615_pm_set(void *data, u64 val)
97 {
98 	struct mt7615_dev *dev = data;
99 	struct mt76_connac_pm *pm = &dev->pm;
100 	int ret = 0;
101 
102 	if (!mt7615_wait_for_mcu_init(dev))
103 		return 0;
104 
105 	if (!mt7615_firmware_offload(dev) || mt76_is_usb(&dev->mt76))
106 		return -EOPNOTSUPP;
107 
108 	if (val == pm->enable)
109 		return 0;
110 
111 	mt7615_mutex_acquire(dev);
112 
113 	if (dev->phy.n_beacon_vif) {
114 		ret = -EBUSY;
115 		goto out;
116 	}
117 
118 	if (!pm->enable) {
119 		pm->stats.last_wake_event = jiffies;
120 		pm->stats.last_doze_event = jiffies;
121 	}
122 	pm->enable = val;
123 out:
124 	mt7615_mutex_release(dev);
125 
126 	return ret;
127 }
128 
129 static int
130 mt7615_pm_get(void *data, u64 *val)
131 {
132 	struct mt7615_dev *dev = data;
133 
134 	*val = dev->pm.enable;
135 
136 	return 0;
137 }
138 
139 DEFINE_DEBUGFS_ATTRIBUTE(fops_pm, mt7615_pm_get, mt7615_pm_set, "%lld\n");
140 
141 static int
142 mt7615_pm_stats(struct seq_file *s, void *data)
143 {
144 	struct mt7615_dev *dev = dev_get_drvdata(s->private);
145 	struct mt76_connac_pm *pm = &dev->pm;
146 	unsigned long awake_time = pm->stats.awake_time;
147 	unsigned long doze_time = pm->stats.doze_time;
148 
149 	if (!test_bit(MT76_STATE_PM, &dev->mphy.state))
150 		awake_time += jiffies - pm->stats.last_wake_event;
151 	else
152 		doze_time += jiffies - pm->stats.last_doze_event;
153 
154 	seq_printf(s, "awake time: %14u\ndoze time: %15u\n",
155 		   jiffies_to_msecs(awake_time),
156 		   jiffies_to_msecs(doze_time));
157 
158 	return 0;
159 }
160 
161 static int
162 mt7615_pm_idle_timeout_set(void *data, u64 val)
163 {
164 	struct mt7615_dev *dev = data;
165 
166 	dev->pm.idle_timeout = msecs_to_jiffies(val);
167 
168 	return 0;
169 }
170 
171 static int
172 mt7615_pm_idle_timeout_get(void *data, u64 *val)
173 {
174 	struct mt7615_dev *dev = data;
175 
176 	*val = jiffies_to_msecs(dev->pm.idle_timeout);
177 
178 	return 0;
179 }
180 
181 DEFINE_DEBUGFS_ATTRIBUTE(fops_pm_idle_timeout, mt7615_pm_idle_timeout_get,
182 			 mt7615_pm_idle_timeout_set, "%lld\n");
183 
184 static int
185 mt7615_dbdc_set(void *data, u64 val)
186 {
187 	struct mt7615_dev *dev = data;
188 
189 	if (!mt7615_wait_for_mcu_init(dev))
190 		return 0;
191 
192 	if (val)
193 		mt7615_register_ext_phy(dev);
194 	else
195 		mt7615_unregister_ext_phy(dev);
196 
197 	return 0;
198 }
199 
200 static int
201 mt7615_dbdc_get(void *data, u64 *val)
202 {
203 	struct mt7615_dev *dev = data;
204 
205 	*val = !!mt7615_ext_phy(dev);
206 
207 	return 0;
208 }
209 
210 DEFINE_DEBUGFS_ATTRIBUTE(fops_dbdc, mt7615_dbdc_get,
211 			 mt7615_dbdc_set, "%lld\n");
212 
213 static int
214 mt7615_fw_debug_set(void *data, u64 val)
215 {
216 	struct mt7615_dev *dev = data;
217 
218 	if (!mt7615_wait_for_mcu_init(dev))
219 		return 0;
220 
221 	dev->fw_debug = val;
222 
223 	mt7615_mutex_acquire(dev);
224 	mt7615_mcu_fw_log_2_host(dev, dev->fw_debug ? 2 : 0);
225 	mt7615_mutex_release(dev);
226 
227 	return 0;
228 }
229 
230 static int
231 mt7615_fw_debug_get(void *data, u64 *val)
232 {
233 	struct mt7615_dev *dev = data;
234 
235 	*val = dev->fw_debug;
236 
237 	return 0;
238 }
239 
240 DEFINE_DEBUGFS_ATTRIBUTE(fops_fw_debug, mt7615_fw_debug_get,
241 			 mt7615_fw_debug_set, "%lld\n");
242 
243 static int
244 mt7615_reset_test_set(void *data, u64 val)
245 {
246 	struct mt7615_dev *dev = data;
247 	struct sk_buff *skb;
248 
249 	if (!mt7615_wait_for_mcu_init(dev))
250 		return 0;
251 
252 	skb = alloc_skb(1, GFP_KERNEL);
253 	if (!skb)
254 		return -ENOMEM;
255 
256 	skb_put(skb, 1);
257 
258 	mt7615_mutex_acquire(dev);
259 	mt76_tx_queue_skb_raw(dev, dev->mphy.q_tx[0], skb, 0);
260 	mt7615_mutex_release(dev);
261 
262 	return 0;
263 }
264 
265 DEFINE_DEBUGFS_ATTRIBUTE(fops_reset_test, NULL,
266 			 mt7615_reset_test_set, "%lld\n");
267 
268 static void
269 mt7615_ampdu_stat_read_phy(struct mt7615_phy *phy,
270 			   struct seq_file *file)
271 {
272 	struct mt7615_dev *dev = file->private;
273 	u32 reg = is_mt7663(&dev->mt76) ? MT_MIB_ARNG(0) : MT_AGG_ASRCR0;
274 	bool ext_phy = phy != &dev->phy;
275 	int bound[7], i, range;
276 
277 	if (!phy)
278 		return;
279 
280 	range = mt76_rr(dev, reg);
281 	for (i = 0; i < 4; i++)
282 		bound[i] = MT_AGG_ASRCR_RANGE(range, i) + 1;
283 
284 	range = mt76_rr(dev, reg + 4);
285 	for (i = 0; i < 3; i++)
286 		bound[i + 4] = MT_AGG_ASRCR_RANGE(range, i) + 1;
287 
288 	seq_printf(file, "\nPhy %d\n", ext_phy);
289 
290 	seq_printf(file, "Length: %8d | ", bound[0]);
291 	for (i = 0; i < ARRAY_SIZE(bound) - 1; i++)
292 		seq_printf(file, "%3d -%3d | ",
293 			   bound[i], bound[i + 1]);
294 	seq_puts(file, "\nCount:  ");
295 
296 	range = ext_phy ? ARRAY_SIZE(dev->mt76.aggr_stats) / 2 : 0;
297 	for (i = 0; i < ARRAY_SIZE(bound); i++)
298 		seq_printf(file, "%8d | ", dev->mt76.aggr_stats[i + range]);
299 	seq_puts(file, "\n");
300 
301 	seq_printf(file, "BA miss count: %d\n", phy->mib.ba_miss_cnt);
302 	seq_printf(file, "PER: %ld.%1ld%%\n",
303 		   phy->mib.aggr_per / 10, phy->mib.aggr_per % 10);
304 }
305 
306 static int
307 mt7615_ampdu_stat_show(struct seq_file *file, void *data)
308 {
309 	struct mt7615_dev *dev = file->private;
310 
311 	mt7615_mutex_acquire(dev);
312 
313 	mt7615_ampdu_stat_read_phy(&dev->phy, file);
314 	mt7615_ampdu_stat_read_phy(mt7615_ext_phy(dev), file);
315 
316 	mt7615_mutex_release(dev);
317 
318 	return 0;
319 }
320 
321 DEFINE_SHOW_ATTRIBUTE(mt7615_ampdu_stat);
322 
323 static void
324 mt7615_radio_read_phy(struct mt7615_phy *phy, struct seq_file *s)
325 {
326 	struct mt7615_dev *dev = dev_get_drvdata(s->private);
327 	bool ext_phy = phy != &dev->phy;
328 
329 	if (!phy)
330 		return;
331 
332 	seq_printf(s, "Radio %d sensitivity: ofdm=%d cck=%d\n", ext_phy,
333 		   phy->ofdm_sensitivity, phy->cck_sensitivity);
334 	seq_printf(s, "Radio %d false CCA: ofdm=%d cck=%d\n", ext_phy,
335 		   phy->false_cca_ofdm, phy->false_cca_cck);
336 }
337 
338 static int
339 mt7615_radio_read(struct seq_file *s, void *data)
340 {
341 	struct mt7615_dev *dev = dev_get_drvdata(s->private);
342 
343 	mt7615_radio_read_phy(&dev->phy, s);
344 	mt7615_radio_read_phy(mt7615_ext_phy(dev), s);
345 
346 	return 0;
347 }
348 
349 static int
350 mt7615_queues_acq(struct seq_file *s, void *data)
351 {
352 	struct mt7615_dev *dev = dev_get_drvdata(s->private);
353 	int i;
354 
355 	mt7615_mutex_acquire(dev);
356 
357 	for (i = 0; i < 16; i++) {
358 		int j, wmm_idx = i % MT7615_MAX_WMM_SETS;
359 		int acs = i / MT7615_MAX_WMM_SETS;
360 		u32 ctrl, val, qlen = 0;
361 
362 		if (wmm_idx == 3 && is_mt7663(&dev->mt76))
363 			continue;
364 
365 		val = mt76_rr(dev, MT_PLE_AC_QEMPTY(acs, wmm_idx));
366 		ctrl = BIT(31) | BIT(15) | (acs << 8);
367 
368 		for (j = 0; j < 32; j++) {
369 			if (val & BIT(j))
370 				continue;
371 
372 			mt76_wr(dev, MT_PLE_FL_Q0_CTRL,
373 				ctrl | (j + (wmm_idx << 5)));
374 			qlen += mt76_get_field(dev, MT_PLE_FL_Q3_CTRL,
375 					       GENMASK(11, 0));
376 		}
377 		seq_printf(s, "AC%d%d: queued=%d\n", wmm_idx, acs, qlen);
378 	}
379 
380 	mt7615_mutex_release(dev);
381 
382 	return 0;
383 }
384 
385 static int
386 mt7615_queues_read(struct seq_file *s, void *data)
387 {
388 	struct mt7615_dev *dev = dev_get_drvdata(s->private);
389 	struct {
390 		struct mt76_queue *q;
391 		char *queue;
392 	} queue_map[] = {
393 		{ dev->mphy.q_tx[MT_TXQ_BE], "PDMA0" },
394 		{ dev->mt76.q_mcu[MT_MCUQ_WM], "MCUQ" },
395 		{ dev->mt76.q_mcu[MT_MCUQ_FWDL], "MCUFWQ" },
396 	};
397 	int i;
398 
399 	for (i = 0; i < ARRAY_SIZE(queue_map); i++) {
400 		struct mt76_queue *q = queue_map[i].q;
401 
402 		seq_printf(s,
403 			   "%s:	queued=%d head=%d tail=%d\n",
404 			   queue_map[i].queue, q->queued, q->head,
405 			   q->tail);
406 	}
407 
408 	return 0;
409 }
410 
411 static int
412 mt7615_rf_reg_set(void *data, u64 val)
413 {
414 	struct mt7615_dev *dev = data;
415 
416 	mt7615_rf_wr(dev, dev->debugfs_rf_wf, dev->debugfs_rf_reg, val);
417 
418 	return 0;
419 }
420 
421 static int
422 mt7615_rf_reg_get(void *data, u64 *val)
423 {
424 	struct mt7615_dev *dev = data;
425 
426 	*val = mt7615_rf_rr(dev, dev->debugfs_rf_wf, dev->debugfs_rf_reg);
427 
428 	return 0;
429 }
430 
431 DEFINE_DEBUGFS_ATTRIBUTE(fops_rf_reg, mt7615_rf_reg_get, mt7615_rf_reg_set,
432 			 "0x%08llx\n");
433 
434 static ssize_t
435 mt7615_ext_mac_addr_read(struct file *file, char __user *userbuf,
436 			 size_t count, loff_t *ppos)
437 {
438 	struct mt7615_dev *dev = file->private_data;
439 	char buf[32 * ((ETH_ALEN * 3) + 4) + 1];
440 	u8 addr[ETH_ALEN];
441 	int ofs = 0;
442 	int i;
443 
444 	for (i = 0; i < 32; i++) {
445 		if (!(dev->muar_mask & BIT(i)))
446 			continue;
447 
448 		mt76_wr(dev, MT_WF_RMAC_MAR1,
449 			FIELD_PREP(MT_WF_RMAC_MAR1_IDX, i * 2) |
450 			MT_WF_RMAC_MAR1_START);
451 		put_unaligned_le32(mt76_rr(dev, MT_WF_RMAC_MAR0), addr);
452 		put_unaligned_le16((mt76_rr(dev, MT_WF_RMAC_MAR1) &
453 				    MT_WF_RMAC_MAR1_ADDR), addr + 4);
454 		ofs += snprintf(buf + ofs, sizeof(buf) - ofs, "%d=%pM\n", i, addr);
455 	}
456 
457 	return simple_read_from_buffer(userbuf, count, ppos, buf, ofs);
458 }
459 
460 static ssize_t
461 mt7615_ext_mac_addr_write(struct file *file, const char __user *userbuf,
462 			  size_t count, loff_t *ppos)
463 {
464 	struct mt7615_dev *dev = file->private_data;
465 	unsigned long idx = 0;
466 	u8 addr[ETH_ALEN];
467 	char buf[32];
468 	char *p;
469 
470 	if (count > sizeof(buf))
471 		return -EINVAL;
472 
473 	if (copy_from_user(buf, userbuf, count))
474 		return -EFAULT;
475 
476 	buf[sizeof(buf) - 1] = '\0';
477 
478 	p = strchr(buf, '=');
479 	if (p) {
480 		*p = 0;
481 		p++;
482 
483 		if (kstrtoul(buf, 0, &idx) || idx > 31)
484 			return -EINVAL;
485 	} else {
486 		idx = 0;
487 		p = buf;
488 	}
489 
490 	if (!mac_pton(p, addr))
491 		return -EINVAL;
492 
493 	if (is_valid_ether_addr(addr)) {
494 		dev->muar_mask |= BIT(idx);
495 	} else {
496 		memset(addr, 0, sizeof(addr));
497 		dev->muar_mask &= ~BIT(idx);
498 	}
499 
500 	mt76_rmw_field(dev, MT_WF_RMAC_MORE(0), MT_WF_RMAC_MORE_MUAR_MODE, 1);
501 	mt76_wr(dev, MT_WF_RMAC_MAR0, get_unaligned_le32(addr));
502 	mt76_wr(dev, MT_WF_RMAC_MAR1,
503 		get_unaligned_le16(addr + 4) |
504 		FIELD_PREP(MT_WF_RMAC_MAR1_IDX, idx * 2) |
505 		MT_WF_RMAC_MAR1_START |
506 		MT_WF_RMAC_MAR1_WRITE);
507 
508 	mt76_rmw_field(dev, MT_WF_RMAC_MORE(0), MT_WF_RMAC_MORE_MUAR_MODE, !!dev->muar_mask);
509 
510 	return count;
511 }
512 
513 static const struct file_operations fops_ext_mac_addr = {
514 	.open = simple_open,
515 	.llseek = generic_file_llseek,
516 	.read = mt7615_ext_mac_addr_read,
517 	.write = mt7615_ext_mac_addr_write,
518 	.owner = THIS_MODULE,
519 };
520 
521 static int
522 mt7663s_sched_quota_read(struct seq_file *s, void *data)
523 {
524 	struct mt7615_dev *dev = dev_get_drvdata(s->private);
525 	struct mt76_sdio *sdio = &dev->mt76.sdio;
526 
527 	seq_printf(s, "pse_data_quota\t%d\n", sdio->sched.pse_data_quota);
528 	seq_printf(s, "ple_data_quota\t%d\n", sdio->sched.ple_data_quota);
529 	seq_printf(s, "pse_mcu_quota\t%d\n", sdio->sched.pse_mcu_quota);
530 	seq_printf(s, "sched_deficit\t%d\n", sdio->sched.deficit);
531 
532 	return 0;
533 }
534 
535 int mt7615_init_debugfs(struct mt7615_dev *dev)
536 {
537 	struct dentry *dir;
538 
539 	dir = mt76_register_debugfs_fops(&dev->mphy, &fops_regval);
540 	if (!dir)
541 		return -ENOMEM;
542 
543 	if (is_mt7615(&dev->mt76))
544 		debugfs_create_devm_seqfile(dev->mt76.dev, "xmit-queues", dir,
545 					    mt7615_queues_read);
546 	else
547 		debugfs_create_devm_seqfile(dev->mt76.dev, "xmit-queues", dir,
548 					    mt76_queues_read);
549 	debugfs_create_devm_seqfile(dev->mt76.dev, "acq", dir,
550 				    mt7615_queues_acq);
551 	debugfs_create_file("ampdu_stat", 0400, dir, dev, &mt7615_ampdu_stat_fops);
552 	debugfs_create_file("scs", 0600, dir, dev, &fops_scs);
553 	debugfs_create_file("dbdc", 0600, dir, dev, &fops_dbdc);
554 	debugfs_create_file("fw_debug", 0600, dir, dev, &fops_fw_debug);
555 	debugfs_create_file("runtime-pm", 0600, dir, dev, &fops_pm);
556 	debugfs_create_file("idle-timeout", 0600, dir, dev,
557 			    &fops_pm_idle_timeout);
558 	debugfs_create_devm_seqfile(dev->mt76.dev, "runtime_pm_stats", dir,
559 				    mt7615_pm_stats);
560 	debugfs_create_devm_seqfile(dev->mt76.dev, "radio", dir,
561 				    mt7615_radio_read);
562 
563 	if (is_mt7615(&dev->mt76)) {
564 		debugfs_create_u32("dfs_hw_pattern", 0400, dir,
565 				   &dev->hw_pattern);
566 		/* test pattern knobs */
567 		debugfs_create_u8("pattern_len", 0600, dir,
568 				  &dev->radar_pattern.n_pulses);
569 		debugfs_create_u32("pulse_period", 0600, dir,
570 				   &dev->radar_pattern.period);
571 		debugfs_create_u16("pulse_width", 0600, dir,
572 				   &dev->radar_pattern.width);
573 		debugfs_create_u16("pulse_power", 0600, dir,
574 				   &dev->radar_pattern.power);
575 		debugfs_create_file("radar_trigger", 0200, dir, dev,
576 				    &fops_radar_pattern);
577 	}
578 
579 	debugfs_create_file("reset_test", 0200, dir, dev,
580 			    &fops_reset_test);
581 	debugfs_create_file("ext_mac_addr", 0600, dir, dev, &fops_ext_mac_addr);
582 
583 	debugfs_create_u32("rf_wfidx", 0600, dir, &dev->debugfs_rf_wf);
584 	debugfs_create_u32("rf_regidx", 0600, dir, &dev->debugfs_rf_reg);
585 	debugfs_create_file_unsafe("rf_regval", 0600, dir, dev,
586 				   &fops_rf_reg);
587 	if (is_mt7663(&dev->mt76))
588 		debugfs_create_file("chip_config", 0600, dir, dev,
589 				    &fops_config);
590 	if (mt76_is_sdio(&dev->mt76))
591 		debugfs_create_devm_seqfile(dev->mt76.dev, "sched-quota", dir,
592 					    mt7663s_sched_quota_read);
593 
594 	return 0;
595 }
596 EXPORT_SYMBOL_GPL(mt7615_init_debugfs);
597