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