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 static int
10 mt7915_implicit_txbf_set(void *data, u64 val)
11 {
12 	struct mt7915_dev *dev = data;
13 
14 	if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
15 		return -EBUSY;
16 
17 	dev->ibf = !!val;
18 
19 	return mt7915_mcu_set_txbf_type(dev);
20 }
21 
22 static int
23 mt7915_implicit_txbf_get(void *data, u64 *val)
24 {
25 	struct mt7915_dev *dev = data;
26 
27 	*val = dev->ibf;
28 
29 	return 0;
30 }
31 
32 DEFINE_DEBUGFS_ATTRIBUTE(fops_implicit_txbf, mt7915_implicit_txbf_get,
33 			 mt7915_implicit_txbf_set, "%lld\n");
34 
35 /* test knob of system layer 1/2 error recovery */
36 static int mt7915_ser_trigger_set(void *data, u64 val)
37 {
38 	enum {
39 		SER_SET_RECOVER_L1 = 1,
40 		SER_SET_RECOVER_L2,
41 		SER_ENABLE = 2,
42 		SER_RECOVER
43 	};
44 	struct mt7915_dev *dev = data;
45 	int ret = 0;
46 
47 	switch (val) {
48 	case SER_SET_RECOVER_L1:
49 	case SER_SET_RECOVER_L2:
50 		ret = mt7915_mcu_set_ser(dev, SER_ENABLE, BIT(val), 0);
51 		if (ret)
52 			return ret;
53 
54 		return mt7915_mcu_set_ser(dev, SER_RECOVER, val, 0);
55 	default:
56 		break;
57 	}
58 
59 	return ret;
60 }
61 
62 DEFINE_DEBUGFS_ATTRIBUTE(fops_ser_trigger, NULL,
63 			 mt7915_ser_trigger_set, "%lld\n");
64 
65 static int
66 mt7915_radar_trigger(void *data, u64 val)
67 {
68 	struct mt7915_dev *dev = data;
69 
70 	return mt7915_mcu_rdd_cmd(dev, RDD_RADAR_EMULATE, 1, 0, 0);
71 }
72 
73 DEFINE_DEBUGFS_ATTRIBUTE(fops_radar_trigger, NULL,
74 			 mt7915_radar_trigger, "%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 % 4) + 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_show(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 DEFINE_SHOW_ATTRIBUTE(mt7915_tx_stats);
226 
227 static int mt7915_read_temperature(struct seq_file *s, void *data)
228 {
229 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
230 	int temp;
231 
232 	/* cpu */
233 	temp = mt7915_mcu_get_temperature(dev, 0);
234 	seq_printf(s, "Temperature: %d\n", temp);
235 
236 	return 0;
237 }
238 
239 static int
240 mt7915_queues_acq(struct seq_file *s, void *data)
241 {
242 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
243 	int i;
244 
245 	for (i = 0; i < 16; i++) {
246 		int j, acs = i / 4, index = i % 4;
247 		u32 ctrl, val, qlen = 0;
248 
249 		val = mt76_rr(dev, MT_PLE_AC_QEMPTY(acs, index));
250 		ctrl = BIT(31) | BIT(15) | (acs << 8);
251 
252 		for (j = 0; j < 32; j++) {
253 			if (val & BIT(j))
254 				continue;
255 
256 			mt76_wr(dev, MT_PLE_FL_Q0_CTRL,
257 				ctrl | (j + (index << 5)));
258 			qlen += mt76_get_field(dev, MT_PLE_FL_Q3_CTRL,
259 					       GENMASK(11, 0));
260 		}
261 		seq_printf(s, "AC%d%d: queued=%d\n", acs, index, qlen);
262 	}
263 
264 	return 0;
265 }
266 
267 static int
268 mt7915_queues_read(struct seq_file *s, void *data)
269 {
270 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
271 	struct mt76_phy *mphy_ext = dev->mt76.phy2;
272 	struct mt76_queue *ext_q = mphy_ext ? mphy_ext->q_tx[MT_TXQ_BE] : NULL;
273 	struct {
274 		struct mt76_queue *q;
275 		char *queue;
276 	} queue_map[] = {
277 		{ dev->mphy.q_tx[MT_TXQ_BE],	 "WFDMA0" },
278 		{ ext_q,			 "WFDMA1" },
279 		{ dev->mphy.q_tx[MT_TXQ_BE],	 "WFDMA0" },
280 		{ dev->mt76.q_mcu[MT_MCUQ_WM],	 "MCUWM"  },
281 		{ dev->mt76.q_mcu[MT_MCUQ_WA],	 "MCUWA"  },
282 		{ dev->mt76.q_mcu[MT_MCUQ_FWDL], "MCUFWQ" },
283 	};
284 	int i;
285 
286 	for (i = 0; i < ARRAY_SIZE(queue_map); i++) {
287 		struct mt76_queue *q = queue_map[i].q;
288 
289 		if (!q)
290 			continue;
291 
292 		seq_printf(s,
293 			   "%s:	queued=%d head=%d tail=%d\n",
294 			   queue_map[i].queue, q->queued, q->head,
295 			   q->tail);
296 	}
297 
298 	return 0;
299 }
300 
301 static void
302 mt7915_puts_rate_txpower(struct seq_file *s, struct mt7915_phy *phy)
303 {
304 	static const char * const sku_group_name[] = {
305 		"CCK", "OFDM", "HT20", "HT40",
306 		"VHT20", "VHT40", "VHT80", "VHT160",
307 		"RU26", "RU52", "RU106", "RU242/SU20",
308 		"RU484/SU40", "RU996/SU80", "RU2x996/SU160"
309 	};
310 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
311 	bool ext_phy = phy != &dev->phy;
312 	u32 reg_base;
313 	int i, idx = 0;
314 
315 	if (!phy)
316 		return;
317 
318 	reg_base = MT_TMAC_FP0R0(ext_phy);
319 	seq_printf(s, "\nBand %d\n", ext_phy);
320 
321 	for (i = 0; i < ARRAY_SIZE(mt7915_sku_group_len); i++) {
322 		u8 cnt, mcs_num = mt7915_sku_group_len[i];
323 		s8 txpower[12];
324 		int j;
325 
326 		if (i == SKU_HT_BW20 || i == SKU_HT_BW40) {
327 			mcs_num = 8;
328 		} else if (i >= SKU_VHT_BW20 && i <= SKU_VHT_BW160) {
329 			mcs_num = 10;
330 		} else if (i == SKU_HE_RU26) {
331 			reg_base = MT_TMAC_FP0R18(ext_phy);
332 			idx = 0;
333 		}
334 
335 		for (j = 0, cnt = 0; j < DIV_ROUND_UP(mcs_num, 4); j++) {
336 			u32 val;
337 
338 			if (i == SKU_VHT_BW160 && idx == 60) {
339 				reg_base = MT_TMAC_FP0R15(ext_phy);
340 				idx = 0;
341 			}
342 
343 			val = mt76_rr(dev, reg_base + (idx / 4) * 4);
344 
345 			if (idx && idx % 4)
346 				val >>= (idx % 4) * 8;
347 
348 			while (val > 0 && cnt < mcs_num) {
349 				s8 pwr = FIELD_GET(MT_TMAC_FP_MASK, val);
350 
351 				txpower[cnt++] = pwr;
352 				val >>= 8;
353 				idx++;
354 			}
355 		}
356 
357 		mt76_seq_puts_array(s, sku_group_name[i], txpower, mcs_num);
358 	}
359 }
360 
361 static int
362 mt7915_read_rate_txpower(struct seq_file *s, void *data)
363 {
364 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
365 
366 	mt7915_puts_rate_txpower(s, &dev->phy);
367 	mt7915_puts_rate_txpower(s, mt7915_ext_phy(dev));
368 
369 	return 0;
370 }
371 
372 int mt7915_init_debugfs(struct mt7915_dev *dev)
373 {
374 	struct dentry *dir;
375 
376 	dir = mt76_register_debugfs(&dev->mt76);
377 	if (!dir)
378 		return -ENOMEM;
379 
380 	debugfs_create_devm_seqfile(dev->mt76.dev, "queues", dir,
381 				    mt7915_queues_read);
382 	debugfs_create_devm_seqfile(dev->mt76.dev, "acq", dir,
383 				    mt7915_queues_acq);
384 	debugfs_create_file("tx_stats", 0400, dir, dev, &mt7915_tx_stats_fops);
385 	debugfs_create_file("fw_debug", 0600, dir, dev, &fops_fw_debug);
386 	debugfs_create_file("implicit_txbf", 0600, dir, dev,
387 			    &fops_implicit_txbf);
388 	debugfs_create_u32("dfs_hw_pattern", 0400, dir, &dev->hw_pattern);
389 	/* test knobs */
390 	debugfs_create_file("radar_trigger", 0200, dir, dev,
391 			    &fops_radar_trigger);
392 	debugfs_create_file("ser_trigger", 0200, dir, dev, &fops_ser_trigger);
393 	debugfs_create_devm_seqfile(dev->mt76.dev, "temperature", dir,
394 				    mt7915_read_temperature);
395 	debugfs_create_devm_seqfile(dev->mt76.dev, "txpower_sku", dir,
396 				    mt7915_read_rate_txpower);
397 
398 	return 0;
399 }
400 
401 #ifdef CONFIG_MAC80211_DEBUGFS
402 /** per-station debugfs **/
403 
404 /* usage: <tx mode> <ldpc> <stbc> <bw> <gi> <nss> <mcs> */
405 static int mt7915_sta_fixed_rate_set(void *data, u64 rate)
406 {
407 	struct ieee80211_sta *sta = data;
408 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
409 
410 	return mt7915_mcu_set_fixed_rate(msta->vif->phy->dev, sta, rate);
411 }
412 
413 DEFINE_DEBUGFS_ATTRIBUTE(fops_fixed_rate, NULL,
414 			 mt7915_sta_fixed_rate_set, "%llx\n");
415 
416 static int
417 mt7915_sta_stats_show(struct seq_file *s, void *data)
418 {
419 	struct ieee80211_sta *sta = s->private;
420 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
421 	struct mt7915_sta_stats *stats = &msta->stats;
422 	struct rate_info *rate = &stats->prob_rate;
423 	static const char * const bw[] = {
424 		"BW20", "BW5", "BW10", "BW40",
425 		"BW80", "BW160", "BW_HE_RU"
426 	};
427 
428 	if (!rate->legacy && !rate->flags)
429 		return 0;
430 
431 	seq_puts(s, "Probing rate - ");
432 	if (rate->flags & RATE_INFO_FLAGS_MCS)
433 		seq_puts(s, "HT ");
434 	else if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
435 		seq_puts(s, "VHT ");
436 	else if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
437 		seq_puts(s, "HE ");
438 	else
439 		seq_printf(s, "Bitrate %d\n", rate->legacy);
440 
441 	if (rate->flags) {
442 		seq_printf(s, "%s NSS%d MCS%d ",
443 			   bw[rate->bw], rate->nss, rate->mcs);
444 
445 		if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
446 			seq_puts(s, "SGI ");
447 		else if (rate->he_gi)
448 			seq_puts(s, "HE GI ");
449 
450 		if (rate->he_dcm)
451 			seq_puts(s, "DCM ");
452 	}
453 
454 	seq_printf(s, "\nPPDU PER: %ld.%1ld%%\n",
455 		   stats->per / 10, stats->per % 10);
456 
457 	return 0;
458 }
459 
460 DEFINE_SHOW_ATTRIBUTE(mt7915_sta_stats);
461 
462 void mt7915_sta_add_debugfs(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
463 			    struct ieee80211_sta *sta, struct dentry *dir)
464 {
465 	debugfs_create_file("fixed_rate", 0600, dir, sta, &fops_fixed_rate);
466 	debugfs_create_file("stats", 0400, dir, sta, &mt7915_sta_stats_fops);
467 }
468 #endif
469