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 & MU counters */
182 	cnt = mt76_rr(dev, MT_MIB_SDR34(ext_phy));
183 	seq_printf(s, "Tx multi-user Beamforming counts: %ld\n",
184 		   FIELD_GET(MT_MIB_MU_BF_TX_CNT, cnt));
185 	cnt = mt76_rr(dev, MT_MIB_DR8(ext_phy));
186 	seq_printf(s, "Tx multi-user MPDU counts: %d\n", cnt);
187 	cnt = mt76_rr(dev, MT_MIB_DR9(ext_phy));
188 	seq_printf(s, "Tx multi-user successful MPDU counts: %d\n", cnt);
189 	cnt = mt76_rr(dev, MT_MIB_DR11(ext_phy));
190 	seq_printf(s, "Tx single-user successful MPDU counts: %d\n", cnt);
191 
192 	seq_puts(s, "\n");
193 }
194 
195 static int
196 mt7915_tx_stats_read(struct seq_file *file, void *data)
197 {
198 	struct mt7915_dev *dev = file->private;
199 	int stat[8], i, n;
200 
201 	mt7915_ampdu_stat_read_phy(&dev->phy, file);
202 	mt7915_txbf_stat_read_phy(&dev->phy, file);
203 
204 	mt7915_ampdu_stat_read_phy(mt7915_ext_phy(dev), file);
205 	mt7915_txbf_stat_read_phy(mt7915_ext_phy(dev), file);
206 
207 	/* Tx amsdu info */
208 	seq_puts(file, "Tx MSDU stat:\n");
209 	for (i = 0, n = 0; i < ARRAY_SIZE(stat); i++) {
210 		stat[i] = mt76_rr(dev,  MT_PLE_AMSDU_PACK_MSDU_CNT(i));
211 		n += stat[i];
212 	}
213 
214 	for (i = 0; i < ARRAY_SIZE(stat); i++) {
215 		seq_printf(file, "AMSDU pack count of %d MSDU in TXD: 0x%x ",
216 			   i + 1, stat[i]);
217 		if (n != 0)
218 			seq_printf(file, "(%d%%)\n", stat[i] * 100 / n);
219 		else
220 			seq_puts(file, "\n");
221 	}
222 
223 	return 0;
224 }
225 
226 static int
227 mt7915_tx_stats_open(struct inode *inode, struct file *f)
228 {
229 	return single_open(f, mt7915_tx_stats_read, inode->i_private);
230 }
231 
232 static const struct file_operations fops_tx_stats = {
233 	.open = mt7915_tx_stats_open,
234 	.read = seq_read,
235 	.llseek = seq_lseek,
236 	.release = single_release,
237 };
238 
239 static int mt7915_read_temperature(struct seq_file *s, void *data)
240 {
241 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
242 	int temp;
243 
244 	/* cpu */
245 	temp = mt7915_mcu_get_temperature(dev, 0);
246 	seq_printf(s, "Temperature: %d\n", temp);
247 
248 	return 0;
249 }
250 
251 static int
252 mt7915_queues_acq(struct seq_file *s, void *data)
253 {
254 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
255 	int i;
256 
257 	for (i = 0; i < 16; i++) {
258 		int j, acs = i / 4, index = i % 4;
259 		u32 ctrl, val, qlen = 0;
260 
261 		val = mt76_rr(dev, MT_PLE_AC_QEMPTY(acs, index));
262 		ctrl = BIT(31) | BIT(15) | (acs << 8);
263 
264 		for (j = 0; j < 32; j++) {
265 			if (val & BIT(j))
266 				continue;
267 
268 			mt76_wr(dev, MT_PLE_FL_Q0_CTRL,
269 				ctrl | (j + (index << 5)));
270 			qlen += mt76_get_field(dev, MT_PLE_FL_Q3_CTRL,
271 					       GENMASK(11, 0));
272 		}
273 		seq_printf(s, "AC%d%d: queued=%d\n", acs, index, qlen);
274 	}
275 
276 	return 0;
277 }
278 
279 static int
280 mt7915_queues_read(struct seq_file *s, void *data)
281 {
282 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
283 	static const struct {
284 		char *queue;
285 		int id;
286 	} queue_map[] = {
287 		{ "WFDMA0", MT_TXQ_BE },
288 		{ "MCUWM", MT_TXQ_MCU },
289 		{ "MCUWA", MT_TXQ_MCU_WA },
290 		{ "MCUFWQ", MT_TXQ_FWDL },
291 	};
292 	int i;
293 
294 	for (i = 0; i < ARRAY_SIZE(queue_map); i++) {
295 		struct mt76_sw_queue *q = &dev->mt76.q_tx[queue_map[i].id];
296 
297 		if (!q->q)
298 			continue;
299 
300 		seq_printf(s,
301 			   "%s:	queued=%d head=%d tail=%d\n",
302 			   queue_map[i].queue, q->q->queued, q->q->head,
303 			   q->q->tail);
304 	}
305 
306 	return 0;
307 }
308 
309 static void
310 mt7915_puts_rate_txpower(struct seq_file *s, s8 *delta,
311 			 s8 txpower_cur, int band)
312 {
313 	static const char * const sku_group_name[] = {
314 		"CCK", "OFDM", "HT20", "HT40",
315 		"VHT20", "VHT40", "VHT80", "VHT160",
316 		"RU26", "RU52", "RU106", "RU242/SU20",
317 		"RU484/SU40", "RU996/SU80", "RU2x996/SU160"
318 	};
319 	s8 txpower[MT7915_SKU_RATE_NUM];
320 	int i, idx = 0;
321 
322 	for (i = 0; i < MT7915_SKU_RATE_NUM; i++)
323 		txpower[i] = DIV_ROUND_UP(txpower_cur + delta[i], 2);
324 
325 	for (i = 0; i < MAX_SKU_RATE_GROUP_NUM; i++) {
326 		const struct sku_group *sku = &mt7915_sku_groups[i];
327 		u32 offset = sku->offset[band];
328 
329 		if (!offset) {
330 			idx += sku->len;
331 			continue;
332 		}
333 
334 		mt76_seq_puts_array(s, sku_group_name[i],
335 				    txpower + idx, sku->len);
336 		idx += sku->len;
337 	}
338 }
339 
340 static int
341 mt7915_read_rate_txpower(struct seq_file *s, void *data)
342 {
343 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
344 	struct mt76_phy *mphy = &dev->mphy;
345 	enum nl80211_band band = mphy->chandef.chan->band;
346 	s8 *delta = dev->rate_power[band];
347 	s8 txpower_base = mphy->txpower_cur - delta[MT7915_SKU_MAX_DELTA_IDX];
348 
349 	seq_puts(s, "Band 0:\n");
350 	mt7915_puts_rate_txpower(s, delta, txpower_base, band);
351 
352 	if (dev->mt76.phy2) {
353 		mphy = dev->mt76.phy2;
354 		band = mphy->chandef.chan->band;
355 		delta = dev->rate_power[band];
356 		txpower_base = mphy->txpower_cur -
357 			       delta[MT7915_SKU_MAX_DELTA_IDX];
358 
359 		seq_puts(s, "Band 1:\n");
360 		mt7915_puts_rate_txpower(s, delta, txpower_base, band);
361 	}
362 
363 	return 0;
364 }
365 
366 int mt7915_init_debugfs(struct mt7915_dev *dev)
367 {
368 	struct dentry *dir;
369 
370 	dir = mt76_register_debugfs(&dev->mt76);
371 	if (!dir)
372 		return -ENOMEM;
373 
374 	debugfs_create_devm_seqfile(dev->mt76.dev, "queues", dir,
375 				    mt7915_queues_read);
376 	debugfs_create_devm_seqfile(dev->mt76.dev, "acq", dir,
377 				    mt7915_queues_acq);
378 	debugfs_create_file("tx_stats", 0400, dir, dev, &fops_tx_stats);
379 	debugfs_create_file("dbdc", 0600, dir, dev, &fops_dbdc);
380 	debugfs_create_file("fw_debug", 0600, dir, dev, &fops_fw_debug);
381 	debugfs_create_u32("dfs_hw_pattern", 0400, dir, &dev->hw_pattern);
382 	/* test knobs */
383 	debugfs_create_file("radar_trigger", 0200, dir, dev,
384 			    &fops_radar_trigger);
385 	debugfs_create_file("ser_trigger", 0200, dir, dev, &fops_ser_trigger);
386 	debugfs_create_devm_seqfile(dev->mt76.dev, "temperature", dir,
387 				    mt7915_read_temperature);
388 	debugfs_create_devm_seqfile(dev->mt76.dev, "txpower_sku", dir,
389 				    mt7915_read_rate_txpower);
390 
391 	return 0;
392 }
393 
394 #ifdef CONFIG_MAC80211_DEBUGFS
395 /** per-station debugfs **/
396 
397 /* usage: <tx mode> <ldpc> <stbc> <bw> <gi> <nss> <mcs> */
398 static int mt7915_sta_fixed_rate_set(void *data, u64 rate)
399 {
400 	struct ieee80211_sta *sta = data;
401 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
402 
403 	return mt7915_mcu_set_fixed_rate(msta->vif->dev, sta, rate);
404 }
405 
406 DEFINE_DEBUGFS_ATTRIBUTE(fops_fixed_rate, NULL,
407 			 mt7915_sta_fixed_rate_set, "%llx\n");
408 
409 static int
410 mt7915_sta_stats_read(struct seq_file *s, void *data)
411 {
412 	struct ieee80211_sta *sta = s->private;
413 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
414 	struct mt7915_sta_stats *stats = &msta->stats;
415 	struct rate_info *rate = &stats->prob_rate;
416 	static const char * const bw[] = {
417 		"BW20", "BW5", "BW10", "BW40",
418 		"BW80", "BW160", "BW_HE_RU"
419 	};
420 
421 	if (!rate->legacy && !rate->flags)
422 		return 0;
423 
424 	seq_puts(s, "Probing rate - ");
425 	if (rate->flags & RATE_INFO_FLAGS_MCS)
426 		seq_puts(s, "HT ");
427 	else if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
428 		seq_puts(s, "VHT ");
429 	else if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
430 		seq_puts(s, "HE ");
431 	else
432 		seq_printf(s, "Bitrate %d\n", rate->legacy);
433 
434 	if (rate->flags) {
435 		seq_printf(s, "%s NSS%d MCS%d ",
436 			   bw[rate->bw], rate->nss, rate->mcs);
437 
438 		if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
439 			seq_puts(s, "SGI ");
440 		else if (rate->he_gi)
441 			seq_puts(s, "HE GI ");
442 
443 		if (rate->he_dcm)
444 			seq_puts(s, "DCM ");
445 	}
446 
447 	seq_printf(s, "\nPPDU PER: %ld.%1ld%%\n",
448 		   stats->per / 10, stats->per % 10);
449 
450 	return 0;
451 }
452 
453 static int
454 mt7915_sta_stats_open(struct inode *inode, struct file *f)
455 {
456 	return single_open(f, mt7915_sta_stats_read, inode->i_private);
457 }
458 
459 static const struct file_operations fops_sta_stats = {
460 	.open = mt7915_sta_stats_open,
461 	.read = seq_read,
462 	.llseek = seq_lseek,
463 	.release = single_release,
464 };
465 
466 void mt7915_sta_add_debugfs(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
467 			    struct ieee80211_sta *sta, struct dentry *dir)
468 {
469 	debugfs_create_file("fixed_rate", 0600, dir, sta, &fops_fixed_rate);
470 	debugfs_create_file("stats", 0400, dir, sta, &fops_sta_stats);
471 }
472 #endif
473