1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include "mt7915.h"
5 #include "eeprom.h"
6 
7 /** global debugfs **/
8 
9 /* test knob of system layer 1/2 error recovery */
10 static int mt7915_ser_trigger_set(void *data, u64 val)
11 {
12 	enum {
13 		SER_SET_RECOVER_L1 = 1,
14 		SER_SET_RECOVER_L2,
15 		SER_ENABLE = 2,
16 		SER_RECOVER
17 	};
18 	struct mt7915_dev *dev = data;
19 	int ret = 0;
20 
21 	switch (val) {
22 	case SER_SET_RECOVER_L1:
23 	case SER_SET_RECOVER_L2:
24 		/* fall through */
25 		ret = mt7915_mcu_set_ser(dev, SER_ENABLE, BIT(val), 0);
26 		if (ret)
27 			return ret;
28 
29 		return mt7915_mcu_set_ser(dev, SER_RECOVER, val, 0);
30 	default:
31 		break;
32 	}
33 
34 	return ret;
35 }
36 
37 DEFINE_DEBUGFS_ATTRIBUTE(fops_ser_trigger, NULL,
38 			 mt7915_ser_trigger_set, "%lld\n");
39 
40 static int
41 mt7915_radar_trigger(void *data, u64 val)
42 {
43 	struct mt7915_dev *dev = data;
44 
45 	return mt7915_mcu_rdd_cmd(dev, RDD_RADAR_EMULATE, 1, 0, 0);
46 }
47 
48 DEFINE_DEBUGFS_ATTRIBUTE(fops_radar_trigger, NULL,
49 			 mt7915_radar_trigger, "%lld\n");
50 
51 static int
52 mt7915_dbdc_set(void *data, u64 val)
53 {
54 	struct mt7915_dev *dev = data;
55 
56 	if (val)
57 		mt7915_register_ext_phy(dev);
58 	else
59 		mt7915_unregister_ext_phy(dev);
60 
61 	return 0;
62 }
63 
64 static int
65 mt7915_dbdc_get(void *data, u64 *val)
66 {
67 	struct mt7915_dev *dev = data;
68 
69 	*val = !!mt7915_ext_phy(dev);
70 
71 	return 0;
72 }
73 
74 DEFINE_DEBUGFS_ATTRIBUTE(fops_dbdc, mt7915_dbdc_get,
75 			 mt7915_dbdc_set, "%lld\n");
76 
77 static int
78 mt7915_fw_debug_set(void *data, u64 val)
79 {
80 	struct mt7915_dev *dev = data;
81 	enum {
82 		DEBUG_TXCMD = 62,
83 		DEBUG_CMD_RPT_TX,
84 		DEBUG_CMD_RPT_TRIG,
85 		DEBUG_SPL,
86 		DEBUG_RPT_RX,
87 	} debug;
88 
89 	dev->fw_debug = !!val;
90 
91 	mt7915_mcu_fw_log_2_host(dev, dev->fw_debug ? 2 : 0);
92 
93 	for (debug = DEBUG_TXCMD; debug <= DEBUG_RPT_RX; debug++)
94 		mt7915_mcu_fw_dbg_ctrl(dev, debug, dev->fw_debug);
95 
96 	return 0;
97 }
98 
99 static int
100 mt7915_fw_debug_get(void *data, u64 *val)
101 {
102 	struct mt7915_dev *dev = data;
103 
104 	*val = dev->fw_debug;
105 
106 	return 0;
107 }
108 
109 DEFINE_DEBUGFS_ATTRIBUTE(fops_fw_debug, mt7915_fw_debug_get,
110 			 mt7915_fw_debug_set, "%lld\n");
111 
112 static void
113 mt7915_ampdu_stat_read_phy(struct mt7915_phy *phy,
114 			   struct seq_file *file)
115 {
116 	struct mt7915_dev *dev = file->private;
117 	bool ext_phy = phy != &dev->phy;
118 	int bound[15], range[4], i, n;
119 
120 	if (!phy)
121 		return;
122 
123 	/* Tx ampdu stat */
124 	for (i = 0; i < ARRAY_SIZE(range); i++)
125 		range[i] = mt76_rr(dev, MT_MIB_ARNG(ext_phy, i));
126 
127 	for (i = 0; i < ARRAY_SIZE(bound); i++)
128 		bound[i] = MT_MIB_ARNCR_RANGE(range[i / 4], i) + 1;
129 
130 	seq_printf(file, "\nPhy %d\n", ext_phy);
131 
132 	seq_printf(file, "Length: %8d | ", bound[0]);
133 	for (i = 0; i < ARRAY_SIZE(bound) - 1; i++)
134 		seq_printf(file, "%3d -%3d | ",
135 			   bound[i] + 1, bound[i + 1]);
136 
137 	seq_puts(file, "\nCount:  ");
138 	n = ext_phy ? ARRAY_SIZE(dev->mt76.aggr_stats) / 2 : 0;
139 	for (i = 0; i < ARRAY_SIZE(bound); i++)
140 		seq_printf(file, "%8d | ", dev->mt76.aggr_stats[i + n]);
141 	seq_puts(file, "\n");
142 
143 	seq_printf(file, "BA miss count: %d\n", phy->mib.ba_miss_cnt);
144 }
145 
146 static void
147 mt7915_txbf_stat_read_phy(struct mt7915_phy *phy, struct seq_file *s)
148 {
149 	struct mt7915_dev *dev = s->private;
150 	bool ext_phy = phy != &dev->phy;
151 	int cnt;
152 
153 	if (!phy)
154 		return;
155 
156 	/* Tx Beamformer monitor */
157 	seq_puts(s, "\nTx Beamformer applied PPDU counts: ");
158 
159 	cnt = mt76_rr(dev, MT_ETBF_TX_APP_CNT(ext_phy));
160 	seq_printf(s, "iBF: %ld, eBF: %ld\n",
161 		   FIELD_GET(MT_ETBF_TX_IBF_CNT, cnt),
162 		   FIELD_GET(MT_ETBF_TX_EBF_CNT, cnt));
163 
164 	/* Tx Beamformer Rx feedback monitor */
165 	seq_puts(s, "Tx Beamformer Rx feedback statistics: ");
166 
167 	cnt = mt76_rr(dev, MT_ETBF_RX_FB_CNT(ext_phy));
168 	seq_printf(s, "All: %ld, HE: %ld, VHT: %ld, HT: %ld\n",
169 		   FIELD_GET(MT_ETBF_RX_FB_ALL, cnt),
170 		   FIELD_GET(MT_ETBF_RX_FB_HE, cnt),
171 		   FIELD_GET(MT_ETBF_RX_FB_VHT, cnt),
172 		   FIELD_GET(MT_ETBF_RX_FB_HT, cnt));
173 
174 	/* Tx Beamformee Rx NDPA & Tx feedback report */
175 	cnt = mt76_rr(dev, MT_ETBF_TX_NDP_BFRP(ext_phy));
176 	seq_printf(s, "Tx Beamformee successful feedback frames: %ld\n",
177 		   FIELD_GET(MT_ETBF_TX_FB_CPL, cnt));
178 	seq_printf(s, "Tx Beamformee feedback triggered counts: %ld\n",
179 		   FIELD_GET(MT_ETBF_TX_FB_TRI, cnt));
180 
181 	/* Tx SU counters */
182 	cnt = mt76_rr(dev, MT_MIB_DR11(ext_phy));
183 	seq_printf(s, "Tx single-user successful MPDU counts: %d\n", cnt);
184 
185 	seq_puts(s, "\n");
186 }
187 
188 static int
189 mt7915_tx_stats_read(struct seq_file *file, void *data)
190 {
191 	struct mt7915_dev *dev = file->private;
192 	int stat[8], i, n;
193 
194 	mt7915_ampdu_stat_read_phy(&dev->phy, file);
195 	mt7915_txbf_stat_read_phy(&dev->phy, file);
196 
197 	mt7915_ampdu_stat_read_phy(mt7915_ext_phy(dev), file);
198 	mt7915_txbf_stat_read_phy(mt7915_ext_phy(dev), file);
199 
200 	/* Tx amsdu info */
201 	seq_puts(file, "Tx MSDU stat:\n");
202 	for (i = 0, n = 0; i < ARRAY_SIZE(stat); i++) {
203 		stat[i] = mt76_rr(dev,  MT_PLE_AMSDU_PACK_MSDU_CNT(i));
204 		n += stat[i];
205 	}
206 
207 	for (i = 0; i < ARRAY_SIZE(stat); i++) {
208 		seq_printf(file, "AMSDU pack count of %d MSDU in TXD: 0x%x ",
209 			   i + 1, stat[i]);
210 		if (n != 0)
211 			seq_printf(file, "(%d%%)\n", stat[i] * 100 / n);
212 		else
213 			seq_puts(file, "\n");
214 	}
215 
216 	return 0;
217 }
218 
219 static int
220 mt7915_tx_stats_open(struct inode *inode, struct file *f)
221 {
222 	return single_open(f, mt7915_tx_stats_read, inode->i_private);
223 }
224 
225 static const struct file_operations fops_tx_stats = {
226 	.open = mt7915_tx_stats_open,
227 	.read = seq_read,
228 	.llseek = seq_lseek,
229 	.release = single_release,
230 };
231 
232 static int mt7915_read_temperature(struct seq_file *s, void *data)
233 {
234 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
235 	int temp;
236 
237 	/* cpu */
238 	temp = mt7915_mcu_get_temperature(dev, 0);
239 	seq_printf(s, "Temperature: %d\n", temp);
240 
241 	return 0;
242 }
243 
244 static int
245 mt7915_queues_acq(struct seq_file *s, void *data)
246 {
247 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
248 	int i;
249 
250 	for (i = 0; i < 16; i++) {
251 		int j, acs = i / 4, index = i % 4;
252 		u32 ctrl, val, qlen = 0;
253 
254 		val = mt76_rr(dev, MT_PLE_AC_QEMPTY(acs, index));
255 		ctrl = BIT(31) | BIT(15) | (acs << 8);
256 
257 		for (j = 0; j < 32; j++) {
258 			if (val & BIT(j))
259 				continue;
260 
261 			mt76_wr(dev, MT_PLE_FL_Q0_CTRL,
262 				ctrl | (j + (index << 5)));
263 			qlen += mt76_get_field(dev, MT_PLE_FL_Q3_CTRL,
264 					       GENMASK(11, 0));
265 		}
266 		seq_printf(s, "AC%d%d: queued=%d\n", acs, index, qlen);
267 	}
268 
269 	return 0;
270 }
271 
272 static int
273 mt7915_queues_read(struct seq_file *s, void *data)
274 {
275 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
276 	static const struct {
277 		char *queue;
278 		int id;
279 	} queue_map[] = {
280 		{ "WFDMA0", MT_TXQ_BE },
281 		{ "MCUWM", MT_TXQ_MCU },
282 		{ "MCUWA", MT_TXQ_MCU_WA },
283 		{ "MCUFWQ", MT_TXQ_FWDL },
284 	};
285 	int i;
286 
287 	for (i = 0; i < ARRAY_SIZE(queue_map); i++) {
288 		struct mt76_sw_queue *q = &dev->mt76.q_tx[queue_map[i].id];
289 
290 		if (!q->q)
291 			continue;
292 
293 		seq_printf(s,
294 			   "%s:	queued=%d head=%d tail=%d\n",
295 			   queue_map[i].queue, q->q->queued, q->q->head,
296 			   q->q->tail);
297 	}
298 
299 	return 0;
300 }
301 
302 static void
303 mt7915_puts_rate_txpower(struct seq_file *s, s8 *delta,
304 			 s8 txpower_cur, int band)
305 {
306 	static const char * const sku_group_name[] = {
307 		"CCK", "OFDM", "HT20", "HT40",
308 		"VHT20", "VHT40", "VHT80", "VHT160",
309 		"RU26", "RU52", "RU106", "RU242/SU20",
310 		"RU484/SU40", "RU996/SU80", "RU2x996/SU160"
311 	};
312 	s8 txpower[MT7915_SKU_RATE_NUM];
313 	int i, idx = 0;
314 
315 	for (i = 0; i < MT7915_SKU_RATE_NUM; i++)
316 		txpower[i] = DIV_ROUND_UP(txpower_cur + delta[i], 2);
317 
318 	for (i = 0; i < MAX_SKU_RATE_GROUP_NUM; i++) {
319 		const struct sku_group *sku = &mt7915_sku_groups[i];
320 		u32 offset = sku->offset[band];
321 
322 		if (!offset) {
323 			idx += sku->len;
324 			continue;
325 		}
326 
327 		mt76_seq_puts_array(s, sku_group_name[i],
328 				    txpower + idx, sku->len);
329 		idx += sku->len;
330 	}
331 }
332 
333 static int
334 mt7915_read_rate_txpower(struct seq_file *s, void *data)
335 {
336 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
337 	struct mt76_phy *mphy = &dev->mphy;
338 	enum nl80211_band band = mphy->chandef.chan->band;
339 	s8 *delta = dev->rate_power[band];
340 	s8 txpower_base = mphy->txpower_cur - delta[MT7915_SKU_MAX_DELTA_IDX];
341 
342 	seq_puts(s, "Band 0:\n");
343 	mt7915_puts_rate_txpower(s, delta, txpower_base, band);
344 
345 	if (dev->mt76.phy2) {
346 		mphy = dev->mt76.phy2;
347 		band = mphy->chandef.chan->band;
348 		delta = dev->rate_power[band];
349 		txpower_base = mphy->txpower_cur -
350 			       delta[MT7915_SKU_MAX_DELTA_IDX];
351 
352 		seq_puts(s, "Band 1:\n");
353 		mt7915_puts_rate_txpower(s, delta, txpower_base, band);
354 	}
355 
356 	return 0;
357 }
358 
359 int mt7915_init_debugfs(struct mt7915_dev *dev)
360 {
361 	struct dentry *dir;
362 
363 	dir = mt76_register_debugfs(&dev->mt76);
364 	if (!dir)
365 		return -ENOMEM;
366 
367 	debugfs_create_devm_seqfile(dev->mt76.dev, "queues", dir,
368 				    mt7915_queues_read);
369 	debugfs_create_devm_seqfile(dev->mt76.dev, "acq", dir,
370 				    mt7915_queues_acq);
371 	debugfs_create_file("tx_stats", 0400, dir, dev, &fops_tx_stats);
372 	debugfs_create_file("dbdc", 0600, dir, dev, &fops_dbdc);
373 	debugfs_create_file("fw_debug", 0600, dir, dev, &fops_fw_debug);
374 	debugfs_create_u32("dfs_hw_pattern", 0400, dir, &dev->hw_pattern);
375 	/* test knobs */
376 	debugfs_create_file("radar_trigger", 0200, dir, dev,
377 			    &fops_radar_trigger);
378 	debugfs_create_file("ser_trigger", 0200, dir, dev, &fops_ser_trigger);
379 	debugfs_create_devm_seqfile(dev->mt76.dev, "temperature", dir,
380 				    mt7915_read_temperature);
381 	debugfs_create_devm_seqfile(dev->mt76.dev, "txpower_sku", dir,
382 				    mt7915_read_rate_txpower);
383 
384 	return 0;
385 }
386 
387 /** per-station debugfs **/
388 
389 /* usage: <tx mode> <ldpc> <stbc> <bw> <gi> <nss> <mcs> */
390 static int mt7915_sta_fixed_rate_set(void *data, u64 rate)
391 {
392 	struct ieee80211_sta *sta = data;
393 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
394 
395 	return mt7915_mcu_set_fixed_rate(msta->vif->dev, sta, rate);
396 }
397 
398 DEFINE_DEBUGFS_ATTRIBUTE(fops_fixed_rate, NULL,
399 			 mt7915_sta_fixed_rate_set, "%llx\n");
400 
401 static int
402 mt7915_sta_stats_read(struct seq_file *s, void *data)
403 {
404 	struct ieee80211_sta *sta = s->private;
405 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
406 	struct mt7915_sta_stats *stats = &msta->stats;
407 	struct rate_info *rate = &stats->prob_rate;
408 	static const char * const bw[] = {
409 		"BW20", "BW5", "BW10", "BW40",
410 		"BW80", "BW160", "BW_HE_RU"
411 	};
412 
413 	if (!rate->legacy && !rate->flags)
414 		return 0;
415 
416 	seq_puts(s, "Probing rate - ");
417 	if (rate->flags & RATE_INFO_FLAGS_MCS)
418 		seq_puts(s, "HT ");
419 	else if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
420 		seq_puts(s, "VHT ");
421 	else if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
422 		seq_puts(s, "HE ");
423 	else
424 		seq_printf(s, "Bitrate %d\n", rate->legacy);
425 
426 	if (rate->flags) {
427 		seq_printf(s, "%s NSS%d MCS%d ",
428 			   bw[rate->bw], rate->nss, rate->mcs);
429 
430 		if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
431 			seq_puts(s, "SGI ");
432 		else if (rate->he_gi)
433 			seq_puts(s, "HE GI ");
434 
435 		if (rate->he_dcm)
436 			seq_puts(s, "DCM ");
437 	}
438 
439 	seq_printf(s, "\nPPDU PER: %ld.%1ld%%\n",
440 		   stats->per / 10, stats->per % 10);
441 
442 	return 0;
443 }
444 
445 static int
446 mt7915_sta_stats_open(struct inode *inode, struct file *f)
447 {
448 	return single_open(f, mt7915_sta_stats_read, inode->i_private);
449 }
450 
451 static const struct file_operations fops_sta_stats = {
452 	.open = mt7915_sta_stats_open,
453 	.read = seq_read,
454 	.llseek = seq_lseek,
455 	.release = single_release,
456 };
457 
458 void mt7915_sta_add_debugfs(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
459 			    struct ieee80211_sta *sta, struct dentry *dir)
460 {
461 	debugfs_create_file("fixed_rate", 0600, dir, sta, &fops_fixed_rate);
462 	debugfs_create_file("stats", 0400, dir, sta, &fops_sta_stats);
463 }
464