1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include "mt7921.h"
5 #include "eeprom.h"
6 
7 static int
8 mt7921_reg_set(void *data, u64 val)
9 {
10 	struct mt7921_dev *dev = data;
11 
12 	mt7921_mutex_acquire(dev);
13 	mt76_wr(dev, dev->mt76.debugfs_reg, val);
14 	mt7921_mutex_release(dev);
15 
16 	return 0;
17 }
18 
19 static int
20 mt7921_reg_get(void *data, u64 *val)
21 {
22 	struct mt7921_dev *dev = data;
23 
24 	mt7921_mutex_acquire(dev);
25 	*val = mt76_rr(dev, dev->mt76.debugfs_reg);
26 	mt7921_mutex_release(dev);
27 
28 	return 0;
29 }
30 
31 DEFINE_DEBUGFS_ATTRIBUTE(fops_regval, mt7921_reg_get, mt7921_reg_set,
32 			 "0x%08llx\n");
33 static int
34 mt7921_fw_debug_set(void *data, u64 val)
35 {
36 	struct mt7921_dev *dev = data;
37 
38 	mt7921_mutex_acquire(dev);
39 
40 	dev->fw_debug = (u8)val;
41 	mt7921_mcu_fw_log_2_host(dev, dev->fw_debug);
42 
43 	mt7921_mutex_release(dev);
44 
45 	return 0;
46 }
47 
48 static int
49 mt7921_fw_debug_get(void *data, u64 *val)
50 {
51 	struct mt7921_dev *dev = data;
52 
53 	*val = dev->fw_debug;
54 
55 	return 0;
56 }
57 
58 DEFINE_DEBUGFS_ATTRIBUTE(fops_fw_debug, mt7921_fw_debug_get,
59 			 mt7921_fw_debug_set, "%lld\n");
60 
61 static void
62 mt7921_ampdu_stat_read_phy(struct mt7921_phy *phy,
63 			   struct seq_file *file)
64 {
65 	struct mt7921_dev *dev = file->private;
66 	int bound[15], range[4], i;
67 
68 	if (!phy)
69 		return;
70 
71 	mt7921_mac_update_mib_stats(phy);
72 
73 	/* Tx ampdu stat */
74 	for (i = 0; i < ARRAY_SIZE(range); i++)
75 		range[i] = mt76_rr(dev, MT_MIB_ARNG(0, i));
76 
77 	for (i = 0; i < ARRAY_SIZE(bound); i++)
78 		bound[i] = MT_MIB_ARNCR_RANGE(range[i / 4], i % 4) + 1;
79 
80 	seq_printf(file, "\nPhy0\n");
81 
82 	seq_printf(file, "Length: %8d | ", bound[0]);
83 	for (i = 0; i < ARRAY_SIZE(bound) - 1; i++)
84 		seq_printf(file, "%3d  %3d | ", bound[i] + 1, bound[i + 1]);
85 
86 	seq_puts(file, "\nCount:  ");
87 	for (i = 0; i < ARRAY_SIZE(bound); i++)
88 		seq_printf(file, "%8d | ", dev->mt76.aggr_stats[i]);
89 	seq_puts(file, "\n");
90 
91 	seq_printf(file, "BA miss count: %d\n", phy->mib.ba_miss_cnt);
92 }
93 
94 static int
95 mt7921_tx_stats_show(struct seq_file *file, void *data)
96 {
97 	struct mt7921_dev *dev = file->private;
98 	struct mt7921_phy *phy = &dev->phy;
99 	struct mib_stats *mib = &phy->mib;
100 	int i;
101 
102 	mt7921_mutex_acquire(dev);
103 
104 	mt7921_ampdu_stat_read_phy(phy, file);
105 
106 	seq_puts(file, "Tx MSDU stat:\n");
107 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++) {
108 		seq_printf(file, "AMSDU pack count of %d MSDU in TXD: %8d ",
109 			   i + 1, mib->tx_amsdu[i]);
110 		if (mib->tx_amsdu_cnt)
111 			seq_printf(file, "(%3d%%)\n",
112 				   mib->tx_amsdu[i] * 100 / mib->tx_amsdu_cnt);
113 		else
114 			seq_puts(file, "\n");
115 	}
116 
117 	mt7921_mutex_release(dev);
118 
119 	return 0;
120 }
121 
122 DEFINE_SHOW_ATTRIBUTE(mt7921_tx_stats);
123 
124 static int
125 mt7921_queues_acq(struct seq_file *s, void *data)
126 {
127 	struct mt7921_dev *dev = dev_get_drvdata(s->private);
128 	int i;
129 
130 	mt7921_mutex_acquire(dev);
131 
132 	for (i = 0; i < 16; i++) {
133 		int j, acs = i / 4, index = i % 4;
134 		u32 ctrl, val, qlen = 0;
135 
136 		val = mt76_rr(dev, MT_PLE_AC_QEMPTY(acs, index));
137 		ctrl = BIT(31) | BIT(15) | (acs << 8);
138 
139 		for (j = 0; j < 32; j++) {
140 			if (val & BIT(j))
141 				continue;
142 
143 			mt76_wr(dev, MT_PLE_FL_Q0_CTRL,
144 				ctrl | (j + (index << 5)));
145 			qlen += mt76_get_field(dev, MT_PLE_FL_Q3_CTRL,
146 					       GENMASK(11, 0));
147 		}
148 		seq_printf(s, "AC%d%d: queued=%d\n", acs, index, qlen);
149 	}
150 
151 	mt7921_mutex_release(dev);
152 
153 	return 0;
154 }
155 
156 static int
157 mt7921_queues_read(struct seq_file *s, void *data)
158 {
159 	struct mt7921_dev *dev = dev_get_drvdata(s->private);
160 	struct {
161 		struct mt76_queue *q;
162 		char *queue;
163 	} queue_map[] = {
164 		{ dev->mphy.q_tx[MT_TXQ_BE],	 "WFDMA0" },
165 		{ dev->mt76.q_mcu[MT_MCUQ_WM],	 "MCUWM"  },
166 		{ dev->mt76.q_mcu[MT_MCUQ_FWDL], "MCUFWQ" },
167 	};
168 	int i;
169 
170 	for (i = 0; i < ARRAY_SIZE(queue_map); i++) {
171 		struct mt76_queue *q = queue_map[i].q;
172 
173 		if (!q)
174 			continue;
175 
176 		seq_printf(s,
177 			   "%s:	queued=%d head=%d tail=%d\n",
178 			   queue_map[i].queue, q->queued, q->head,
179 			   q->tail);
180 	}
181 
182 	return 0;
183 }
184 
185 static void
186 mt7921_seq_puts_array(struct seq_file *file, const char *str,
187 		      s8 *val, int len)
188 {
189 	int i;
190 
191 	seq_printf(file, "%-16s:", str);
192 	for (i = 0; i < len; i++)
193 		if (val[i] == 127)
194 			seq_printf(file, " %6s", "N.A");
195 		else
196 			seq_printf(file, " %6d", val[i]);
197 	seq_puts(file, "\n");
198 }
199 
200 #define mt7921_print_txpwr_entry(prefix, rate)				\
201 ({									\
202 	mt7921_seq_puts_array(s, #prefix " (user)",			\
203 			      txpwr.data[TXPWR_USER].rate,		\
204 			      ARRAY_SIZE(txpwr.data[TXPWR_USER].rate)); \
205 	mt7921_seq_puts_array(s, #prefix " (eeprom)",			\
206 			      txpwr.data[TXPWR_EEPROM].rate,		\
207 			      ARRAY_SIZE(txpwr.data[TXPWR_EEPROM].rate)); \
208 	mt7921_seq_puts_array(s, #prefix " (tmac)",			\
209 			      txpwr.data[TXPWR_MAC].rate,		\
210 			      ARRAY_SIZE(txpwr.data[TXPWR_MAC].rate));	\
211 })
212 
213 static int
214 mt7921_txpwr(struct seq_file *s, void *data)
215 {
216 	struct mt7921_dev *dev = dev_get_drvdata(s->private);
217 	struct mt7921_txpwr txpwr;
218 	int ret;
219 
220 	mt7921_mutex_acquire(dev);
221 	ret = mt7921_get_txpwr_info(dev, &txpwr);
222 	mt7921_mutex_release(dev);
223 
224 	if (ret)
225 		return ret;
226 
227 	seq_printf(s, "Tx power table (channel %d)\n", txpwr.ch);
228 	seq_printf(s, "%-16s  %6s %6s %6s %6s\n",
229 		   " ", "1m", "2m", "5m", "11m");
230 	mt7921_print_txpwr_entry(CCK, cck);
231 
232 	seq_printf(s, "%-16s  %6s %6s %6s %6s %6s %6s %6s %6s\n",
233 		   " ", "6m", "9m", "12m", "18m", "24m", "36m",
234 		   "48m", "54m");
235 	mt7921_print_txpwr_entry(OFDM, ofdm);
236 
237 	seq_printf(s, "%-16s  %6s %6s %6s %6s %6s %6s %6s %6s\n",
238 		   " ", "mcs0", "mcs1", "mcs2", "mcs3", "mcs4", "mcs5",
239 		   "mcs6", "mcs7");
240 	mt7921_print_txpwr_entry(HT20, ht20);
241 
242 	seq_printf(s, "%-16s  %6s %6s %6s %6s %6s %6s %6s %6s %6s\n",
243 		   " ", "mcs0", "mcs1", "mcs2", "mcs3", "mcs4", "mcs5",
244 		   "mcs6", "mcs7", "mcs32");
245 	mt7921_print_txpwr_entry(HT40, ht40);
246 
247 	seq_printf(s, "%-16s  %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s\n",
248 		   " ", "mcs0", "mcs1", "mcs2", "mcs3", "mcs4", "mcs5",
249 		   "mcs6", "mcs7", "mcs8", "mcs9", "mcs10", "mcs11");
250 	mt7921_print_txpwr_entry(VHT20, vht20);
251 	mt7921_print_txpwr_entry(VHT40, vht40);
252 	mt7921_print_txpwr_entry(VHT80, vht80);
253 	mt7921_print_txpwr_entry(VHT160, vht160);
254 	mt7921_print_txpwr_entry(HE26, he26);
255 	mt7921_print_txpwr_entry(HE52, he52);
256 	mt7921_print_txpwr_entry(HE106, he106);
257 	mt7921_print_txpwr_entry(HE242, he242);
258 	mt7921_print_txpwr_entry(HE484, he484);
259 	mt7921_print_txpwr_entry(HE996, he996);
260 	mt7921_print_txpwr_entry(HE996x2, he996x2);
261 
262 	return 0;
263 }
264 
265 static void
266 mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
267 {
268 	struct mt7921_dev *dev = priv;
269 
270 	mt7921_mcu_set_beacon_filter(dev, vif, dev->pm.enable);
271 }
272 
273 static int
274 mt7921_pm_set(void *data, u64 val)
275 {
276 	struct mt7921_dev *dev = data;
277 	struct mt76_connac_pm *pm = &dev->pm;
278 
279 	mt7921_mutex_acquire(dev);
280 
281 	if (val == pm->enable)
282 		goto out;
283 
284 	if (!pm->enable) {
285 		pm->stats.last_wake_event = jiffies;
286 		pm->stats.last_doze_event = jiffies;
287 	}
288 	pm->enable = val;
289 
290 	ieee80211_iterate_active_interfaces(mt76_hw(dev),
291 					    IEEE80211_IFACE_ITER_RESUME_ALL,
292 					    mt7921_pm_interface_iter, dev);
293 
294 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
295 
296 out:
297 	mt7921_mutex_release(dev);
298 
299 	return 0;
300 }
301 
302 static int
303 mt7921_pm_get(void *data, u64 *val)
304 {
305 	struct mt7921_dev *dev = data;
306 
307 	*val = dev->pm.enable;
308 
309 	return 0;
310 }
311 
312 DEFINE_DEBUGFS_ATTRIBUTE(fops_pm, mt7921_pm_get, mt7921_pm_set, "%lld\n");
313 
314 static int
315 mt7921_deep_sleep_set(void *data, u64 val)
316 {
317 	struct mt7921_dev *dev = data;
318 	struct mt76_connac_pm *pm = &dev->pm;
319 	bool enable = !!val;
320 
321 	mt7921_mutex_acquire(dev);
322 	if (pm->ds_enable != enable) {
323 		mt76_connac_mcu_set_deep_sleep(&dev->mt76, enable);
324 		pm->ds_enable = enable;
325 	}
326 	mt7921_mutex_release(dev);
327 
328 	return 0;
329 }
330 
331 static int
332 mt7921_deep_sleep_get(void *data, u64 *val)
333 {
334 	struct mt7921_dev *dev = data;
335 
336 	*val = dev->pm.ds_enable;
337 
338 	return 0;
339 }
340 
341 DEFINE_DEBUGFS_ATTRIBUTE(fops_ds, mt7921_deep_sleep_get,
342 			 mt7921_deep_sleep_set, "%lld\n");
343 
344 static int
345 mt7921_pm_stats(struct seq_file *s, void *data)
346 {
347 	struct mt7921_dev *dev = dev_get_drvdata(s->private);
348 	struct mt76_connac_pm *pm = &dev->pm;
349 
350 	unsigned long awake_time = pm->stats.awake_time;
351 	unsigned long doze_time = pm->stats.doze_time;
352 
353 	if (!test_bit(MT76_STATE_PM, &dev->mphy.state))
354 		awake_time += jiffies - pm->stats.last_wake_event;
355 	else
356 		doze_time += jiffies - pm->stats.last_doze_event;
357 
358 	seq_printf(s, "awake time: %14u\ndoze time: %15u\n",
359 		   jiffies_to_msecs(awake_time),
360 		   jiffies_to_msecs(doze_time));
361 
362 	seq_printf(s, "low power wakes: %9d\n", pm->stats.lp_wake);
363 
364 	return 0;
365 }
366 
367 static int
368 mt7921_pm_idle_timeout_set(void *data, u64 val)
369 {
370 	struct mt7921_dev *dev = data;
371 
372 	dev->pm.idle_timeout = msecs_to_jiffies(val);
373 
374 	return 0;
375 }
376 
377 static int
378 mt7921_pm_idle_timeout_get(void *data, u64 *val)
379 {
380 	struct mt7921_dev *dev = data;
381 
382 	*val = jiffies_to_msecs(dev->pm.idle_timeout);
383 
384 	return 0;
385 }
386 
387 DEFINE_DEBUGFS_ATTRIBUTE(fops_pm_idle_timeout, mt7921_pm_idle_timeout_get,
388 			 mt7921_pm_idle_timeout_set, "%lld\n");
389 
390 static int mt7921_chip_reset(void *data, u64 val)
391 {
392 	struct mt7921_dev *dev = data;
393 	int ret = 0;
394 
395 	switch (val) {
396 	case 1:
397 		/* Reset wifisys directly. */
398 		mt7921_reset(&dev->mt76);
399 		break;
400 	default:
401 		/* Collect the core dump before reset wifisys. */
402 		mt7921_mutex_acquire(dev);
403 		ret = mt76_connac_mcu_chip_config(&dev->mt76);
404 		mt7921_mutex_release(dev);
405 		break;
406 	}
407 
408 	return ret;
409 }
410 
411 DEFINE_DEBUGFS_ATTRIBUTE(fops_reset, NULL, mt7921_chip_reset, "%lld\n");
412 
413 static int
414 mt7921s_sched_quota_read(struct seq_file *s, void *data)
415 {
416 	struct mt7921_dev *dev = dev_get_drvdata(s->private);
417 	struct mt76_sdio *sdio = &dev->mt76.sdio;
418 
419 	seq_printf(s, "pse_data_quota\t%d\n", sdio->sched.pse_data_quota);
420 	seq_printf(s, "ple_data_quota\t%d\n", sdio->sched.ple_data_quota);
421 	seq_printf(s, "pse_mcu_quota\t%d\n", sdio->sched.pse_mcu_quota);
422 	seq_printf(s, "sched_deficit\t%d\n", sdio->sched.deficit);
423 
424 	return 0;
425 }
426 
427 int mt7921_init_debugfs(struct mt7921_dev *dev)
428 {
429 	struct dentry *dir;
430 
431 	dir = mt76_register_debugfs_fops(&dev->mphy, &fops_regval);
432 	if (!dir)
433 		return -ENOMEM;
434 
435 	debugfs_create_devm_seqfile(dev->mt76.dev, "queues", dir,
436 				    mt7921_queues_read);
437 	debugfs_create_devm_seqfile(dev->mt76.dev, "acq", dir,
438 				    mt7921_queues_acq);
439 	debugfs_create_devm_seqfile(dev->mt76.dev, "txpower_sku", dir,
440 				    mt7921_txpwr);
441 	debugfs_create_file("tx_stats", 0400, dir, dev, &mt7921_tx_stats_fops);
442 	debugfs_create_file("fw_debug", 0600, dir, dev, &fops_fw_debug);
443 	debugfs_create_file("runtime-pm", 0600, dir, dev, &fops_pm);
444 	debugfs_create_file("idle-timeout", 0600, dir, dev,
445 			    &fops_pm_idle_timeout);
446 	debugfs_create_file("chip_reset", 0600, dir, dev, &fops_reset);
447 	debugfs_create_devm_seqfile(dev->mt76.dev, "runtime_pm_stats", dir,
448 				    mt7921_pm_stats);
449 	debugfs_create_file("deep-sleep", 0600, dir, dev, &fops_ds);
450 	if (mt76_is_sdio(&dev->mt76))
451 		debugfs_create_devm_seqfile(dev->mt76.dev, "sched-quota", dir,
452 					    mt7921s_sched_quota_read);
453 	return 0;
454 }
455