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 mt792x_dev *dev = data;
10 
11 	mt792x_mutex_acquire(dev);
12 	mt76_wr(dev, dev->mt76.debugfs_reg, val);
13 	mt792x_mutex_release(dev);
14 
15 	return 0;
16 }
17 
18 static int
19 mt7921_reg_get(void *data, u64 *val)
20 {
21 	struct mt792x_dev *dev = data;
22 
23 	mt792x_mutex_acquire(dev);
24 	*val = mt76_rr(dev, dev->mt76.debugfs_reg);
25 	mt792x_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 mt792x_dev *dev = data;
36 
37 	mt792x_mutex_acquire(dev);
38 
39 	dev->fw_debug = (u8)val;
40 	mt7921_mcu_fw_log_2_host(dev, dev->fw_debug);
41 
42 	mt792x_mutex_release(dev);
43 
44 	return 0;
45 }
46 
47 static int
48 mt7921_fw_debug_get(void *data, u64 *val)
49 {
50 	struct mt792x_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 DEFINE_SHOW_ATTRIBUTE(mt792x_tx_stats);
61 
62 static void
63 mt7921_seq_puts_array(struct seq_file *file, const char *str,
64 		      s8 *val, int len)
65 {
66 	int i;
67 
68 	seq_printf(file, "%-16s:", str);
69 	for (i = 0; i < len; i++)
70 		if (val[i] == 127)
71 			seq_printf(file, " %6s", "N.A");
72 		else
73 			seq_printf(file, " %6d", val[i]);
74 	seq_puts(file, "\n");
75 }
76 
77 #define mt7921_print_txpwr_entry(prefix, rate)				\
78 ({									\
79 	mt7921_seq_puts_array(s, #prefix " (user)",			\
80 			      txpwr.data[TXPWR_USER].rate,		\
81 			      ARRAY_SIZE(txpwr.data[TXPWR_USER].rate)); \
82 	mt7921_seq_puts_array(s, #prefix " (eeprom)",			\
83 			      txpwr.data[TXPWR_EEPROM].rate,		\
84 			      ARRAY_SIZE(txpwr.data[TXPWR_EEPROM].rate)); \
85 	mt7921_seq_puts_array(s, #prefix " (tmac)",			\
86 			      txpwr.data[TXPWR_MAC].rate,		\
87 			      ARRAY_SIZE(txpwr.data[TXPWR_MAC].rate));	\
88 })
89 
90 static int
91 mt7921_txpwr(struct seq_file *s, void *data)
92 {
93 	struct mt792x_dev *dev = dev_get_drvdata(s->private);
94 	struct mt7921_txpwr txpwr;
95 	int ret;
96 
97 	mt792x_mutex_acquire(dev);
98 	ret = mt7921_get_txpwr_info(dev, &txpwr);
99 	mt792x_mutex_release(dev);
100 
101 	if (ret)
102 		return ret;
103 
104 	seq_printf(s, "Tx power table (channel %d)\n", txpwr.ch);
105 	seq_printf(s, "%-16s  %6s %6s %6s %6s\n",
106 		   " ", "1m", "2m", "5m", "11m");
107 	mt7921_print_txpwr_entry(CCK, cck);
108 
109 	seq_printf(s, "%-16s  %6s %6s %6s %6s %6s %6s %6s %6s\n",
110 		   " ", "6m", "9m", "12m", "18m", "24m", "36m",
111 		   "48m", "54m");
112 	mt7921_print_txpwr_entry(OFDM, ofdm);
113 
114 	seq_printf(s, "%-16s  %6s %6s %6s %6s %6s %6s %6s %6s\n",
115 		   " ", "mcs0", "mcs1", "mcs2", "mcs3", "mcs4", "mcs5",
116 		   "mcs6", "mcs7");
117 	mt7921_print_txpwr_entry(HT20, ht20);
118 
119 	seq_printf(s, "%-16s  %6s %6s %6s %6s %6s %6s %6s %6s %6s\n",
120 		   " ", "mcs0", "mcs1", "mcs2", "mcs3", "mcs4", "mcs5",
121 		   "mcs6", "mcs7", "mcs32");
122 	mt7921_print_txpwr_entry(HT40, ht40);
123 
124 	seq_printf(s, "%-16s  %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s\n",
125 		   " ", "mcs0", "mcs1", "mcs2", "mcs3", "mcs4", "mcs5",
126 		   "mcs6", "mcs7", "mcs8", "mcs9", "mcs10", "mcs11");
127 	mt7921_print_txpwr_entry(VHT20, vht20);
128 	mt7921_print_txpwr_entry(VHT40, vht40);
129 	mt7921_print_txpwr_entry(VHT80, vht80);
130 	mt7921_print_txpwr_entry(VHT160, vht160);
131 	mt7921_print_txpwr_entry(HE26, he26);
132 	mt7921_print_txpwr_entry(HE52, he52);
133 	mt7921_print_txpwr_entry(HE106, he106);
134 	mt7921_print_txpwr_entry(HE242, he242);
135 	mt7921_print_txpwr_entry(HE484, he484);
136 	mt7921_print_txpwr_entry(HE996, he996);
137 	mt7921_print_txpwr_entry(HE996x2, he996x2);
138 
139 	return 0;
140 }
141 
142 static int
143 mt7921_pm_set(void *data, u64 val)
144 {
145 	struct mt792x_dev *dev = data;
146 	struct mt76_connac_pm *pm = &dev->pm;
147 
148 	if (mt76_is_usb(&dev->mt76))
149 		return -EOPNOTSUPP;
150 
151 	mutex_lock(&dev->mt76.mutex);
152 
153 	if (val == pm->enable_user)
154 		goto out;
155 
156 	if (!pm->enable_user) {
157 		pm->stats.last_wake_event = jiffies;
158 		pm->stats.last_doze_event = jiffies;
159 	}
160 	/* make sure the chip is awake here and ps_work is scheduled
161 	 * just at end of the this routine.
162 	 */
163 	pm->enable = false;
164 	mt76_connac_pm_wake(&dev->mphy, pm);
165 
166 	pm->enable_user = val;
167 	mt7921_set_runtime_pm(dev);
168 	mt76_connac_power_save_sched(&dev->mphy, pm);
169 out:
170 	mutex_unlock(&dev->mt76.mutex);
171 
172 	return 0;
173 }
174 
175 static int
176 mt7921_pm_get(void *data, u64 *val)
177 {
178 	struct mt792x_dev *dev = data;
179 
180 	*val = dev->pm.enable_user;
181 
182 	return 0;
183 }
184 
185 DEFINE_DEBUGFS_ATTRIBUTE(fops_pm, mt7921_pm_get, mt7921_pm_set, "%lld\n");
186 
187 static int
188 mt7921_deep_sleep_set(void *data, u64 val)
189 {
190 	struct mt792x_dev *dev = data;
191 	struct mt76_connac_pm *pm = &dev->pm;
192 	bool monitor = !!(dev->mphy.hw->conf.flags & IEEE80211_CONF_MONITOR);
193 	bool enable = !!val;
194 
195 	if (mt76_is_usb(&dev->mt76))
196 		return -EOPNOTSUPP;
197 
198 	mt792x_mutex_acquire(dev);
199 	if (pm->ds_enable_user == enable)
200 		goto out;
201 
202 	pm->ds_enable_user = enable;
203 	pm->ds_enable = enable && !monitor;
204 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
205 out:
206 	mt792x_mutex_release(dev);
207 
208 	return 0;
209 }
210 
211 static int
212 mt7921_deep_sleep_get(void *data, u64 *val)
213 {
214 	struct mt792x_dev *dev = data;
215 
216 	*val = dev->pm.ds_enable_user;
217 
218 	return 0;
219 }
220 
221 DEFINE_DEBUGFS_ATTRIBUTE(fops_ds, mt7921_deep_sleep_get,
222 			 mt7921_deep_sleep_set, "%lld\n");
223 
224 DEFINE_DEBUGFS_ATTRIBUTE(fops_pm_idle_timeout, mt792x_pm_idle_timeout_get,
225 			 mt792x_pm_idle_timeout_set, "%lld\n");
226 
227 static int mt7921_chip_reset(void *data, u64 val)
228 {
229 	struct mt792x_dev *dev = data;
230 	int ret = 0;
231 
232 	switch (val) {
233 	case 1:
234 		/* Reset wifisys directly. */
235 		mt792x_reset(&dev->mt76);
236 		break;
237 	default:
238 		/* Collect the core dump before reset wifisys. */
239 		mt792x_mutex_acquire(dev);
240 		ret = mt76_connac_mcu_chip_config(&dev->mt76);
241 		mt792x_mutex_release(dev);
242 		break;
243 	}
244 
245 	return ret;
246 }
247 
248 DEFINE_DEBUGFS_ATTRIBUTE(fops_reset, NULL, mt7921_chip_reset, "%lld\n");
249 
250 static int
251 mt7921s_sched_quota_read(struct seq_file *s, void *data)
252 {
253 	struct mt792x_dev *dev = dev_get_drvdata(s->private);
254 	struct mt76_sdio *sdio = &dev->mt76.sdio;
255 
256 	seq_printf(s, "pse_data_quota\t%d\n", sdio->sched.pse_data_quota);
257 	seq_printf(s, "ple_data_quota\t%d\n", sdio->sched.ple_data_quota);
258 	seq_printf(s, "pse_mcu_quota\t%d\n", sdio->sched.pse_mcu_quota);
259 	seq_printf(s, "sched_deficit\t%d\n", sdio->sched.deficit);
260 
261 	return 0;
262 }
263 
264 int mt7921_init_debugfs(struct mt792x_dev *dev)
265 {
266 	struct dentry *dir;
267 
268 	dir = mt76_register_debugfs_fops(&dev->mphy, &fops_regval);
269 	if (!dir)
270 		return -ENOMEM;
271 
272 	if (mt76_is_mmio(&dev->mt76))
273 		debugfs_create_devm_seqfile(dev->mt76.dev, "xmit-queues",
274 					    dir, mt792x_queues_read);
275 	else
276 		debugfs_create_devm_seqfile(dev->mt76.dev, "xmit-queues",
277 					    dir, mt76_queues_read);
278 
279 	debugfs_create_devm_seqfile(dev->mt76.dev, "acq", dir,
280 				    mt792x_queues_acq);
281 	debugfs_create_devm_seqfile(dev->mt76.dev, "txpower_sku", dir,
282 				    mt7921_txpwr);
283 	debugfs_create_file("tx_stats", 0400, dir, dev, &mt792x_tx_stats_fops);
284 	debugfs_create_file("fw_debug", 0600, dir, dev, &fops_fw_debug);
285 	debugfs_create_file("runtime-pm", 0600, dir, dev, &fops_pm);
286 	debugfs_create_file("idle-timeout", 0600, dir, dev,
287 			    &fops_pm_idle_timeout);
288 	debugfs_create_file("chip_reset", 0600, dir, dev, &fops_reset);
289 	debugfs_create_devm_seqfile(dev->mt76.dev, "runtime_pm_stats", dir,
290 				    mt792x_pm_stats);
291 	debugfs_create_file("deep-sleep", 0600, dir, dev, &fops_ds);
292 	if (mt76_is_sdio(&dev->mt76))
293 		debugfs_create_devm_seqfile(dev->mt76.dev, "sched-quota", dir,
294 					    mt7921s_sched_quota_read);
295 	return 0;
296 }
297