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