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) + 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 	.owner = THIS_MODULE,
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 	struct mt76_phy *mphy_ext = dev->mt76.phy2;
284 	struct mt76_queue *ext_q = mphy_ext ? mphy_ext->q_tx[MT_TXQ_BE] : NULL;
285 	struct {
286 		struct mt76_queue *q;
287 		char *queue;
288 	} queue_map[] = {
289 		{ dev->mphy.q_tx[MT_TXQ_BE],	 "WFDMA0" },
290 		{ ext_q,			 "WFDMA1" },
291 		{ dev->mphy.q_tx[MT_TXQ_BE],	 "WFDMA0" },
292 		{ dev->mt76.q_mcu[MT_MCUQ_WM],	 "MCUWM"  },
293 		{ dev->mt76.q_mcu[MT_MCUQ_WA],	 "MCUWA"  },
294 		{ dev->mt76.q_mcu[MT_MCUQ_FWDL], "MCUFWQ" },
295 	};
296 	int i;
297 
298 	for (i = 0; i < ARRAY_SIZE(queue_map); i++) {
299 		struct mt76_queue *q = queue_map[i].q;
300 
301 		if (!q)
302 			continue;
303 
304 		seq_printf(s,
305 			   "%s:	queued=%d head=%d tail=%d\n",
306 			   queue_map[i].queue, q->queued, q->head,
307 			   q->tail);
308 	}
309 
310 	return 0;
311 }
312 
313 static void
314 mt7915_puts_rate_txpower(struct seq_file *s, s8 *delta,
315 			 s8 txpower_cur, int band)
316 {
317 	static const char * const sku_group_name[] = {
318 		"CCK", "OFDM", "HT20", "HT40",
319 		"VHT20", "VHT40", "VHT80", "VHT160",
320 		"RU26", "RU52", "RU106", "RU242/SU20",
321 		"RU484/SU40", "RU996/SU80", "RU2x996/SU160"
322 	};
323 	s8 txpower[MT7915_SKU_RATE_NUM];
324 	int i, idx = 0;
325 
326 	for (i = 0; i < MT7915_SKU_RATE_NUM; i++)
327 		txpower[i] = DIV_ROUND_UP(txpower_cur + delta[i], 2);
328 
329 	for (i = 0; i < MAX_SKU_RATE_GROUP_NUM; i++) {
330 		const struct sku_group *sku = &mt7915_sku_groups[i];
331 		u32 offset = sku->offset[band];
332 
333 		if (!offset) {
334 			idx += sku->len;
335 			continue;
336 		}
337 
338 		mt76_seq_puts_array(s, sku_group_name[i],
339 				    txpower + idx, sku->len);
340 		idx += sku->len;
341 	}
342 }
343 
344 static int
345 mt7915_read_rate_txpower(struct seq_file *s, void *data)
346 {
347 	struct mt7915_dev *dev = dev_get_drvdata(s->private);
348 	struct mt76_phy *mphy = &dev->mphy;
349 	enum nl80211_band band = mphy->chandef.chan->band;
350 	s8 *delta = dev->rate_power[band];
351 	s8 txpower_base = mphy->txpower_cur - delta[MT7915_SKU_MAX_DELTA_IDX];
352 
353 	seq_puts(s, "Band 0:\n");
354 	mt7915_puts_rate_txpower(s, delta, txpower_base, band);
355 
356 	if (dev->mt76.phy2) {
357 		mphy = dev->mt76.phy2;
358 		band = mphy->chandef.chan->band;
359 		delta = dev->rate_power[band];
360 		txpower_base = mphy->txpower_cur -
361 			       delta[MT7915_SKU_MAX_DELTA_IDX];
362 
363 		seq_puts(s, "Band 1:\n");
364 		mt7915_puts_rate_txpower(s, delta, txpower_base, band);
365 	}
366 
367 	return 0;
368 }
369 
370 int mt7915_init_debugfs(struct mt7915_dev *dev)
371 {
372 	struct dentry *dir;
373 
374 	dir = mt76_register_debugfs(&dev->mt76);
375 	if (!dir)
376 		return -ENOMEM;
377 
378 	debugfs_create_devm_seqfile(dev->mt76.dev, "queues", dir,
379 				    mt7915_queues_read);
380 	debugfs_create_devm_seqfile(dev->mt76.dev, "acq", dir,
381 				    mt7915_queues_acq);
382 	debugfs_create_file("tx_stats", 0400, dir, dev, &fops_tx_stats);
383 	debugfs_create_file("fw_debug", 0600, dir, dev, &fops_fw_debug);
384 	debugfs_create_file("implicit_txbf", 0600, dir, dev,
385 			    &fops_implicit_txbf);
386 	debugfs_create_u32("dfs_hw_pattern", 0400, dir, &dev->hw_pattern);
387 	/* test knobs */
388 	debugfs_create_file("radar_trigger", 0200, dir, dev,
389 			    &fops_radar_trigger);
390 	debugfs_create_file("ser_trigger", 0200, dir, dev, &fops_ser_trigger);
391 	debugfs_create_devm_seqfile(dev->mt76.dev, "temperature", dir,
392 				    mt7915_read_temperature);
393 	debugfs_create_devm_seqfile(dev->mt76.dev, "txpower_sku", dir,
394 				    mt7915_read_rate_txpower);
395 
396 	return 0;
397 }
398 
399 #ifdef CONFIG_MAC80211_DEBUGFS
400 /** per-station debugfs **/
401 
402 /* usage: <tx mode> <ldpc> <stbc> <bw> <gi> <nss> <mcs> */
403 static int mt7915_sta_fixed_rate_set(void *data, u64 rate)
404 {
405 	struct ieee80211_sta *sta = data;
406 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
407 
408 	return mt7915_mcu_set_fixed_rate(msta->vif->phy->dev, sta, rate);
409 }
410 
411 DEFINE_DEBUGFS_ATTRIBUTE(fops_fixed_rate, NULL,
412 			 mt7915_sta_fixed_rate_set, "%llx\n");
413 
414 static int
415 mt7915_sta_stats_read(struct seq_file *s, void *data)
416 {
417 	struct ieee80211_sta *sta = s->private;
418 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
419 	struct mt7915_sta_stats *stats = &msta->stats;
420 	struct rate_info *rate = &stats->prob_rate;
421 	static const char * const bw[] = {
422 		"BW20", "BW5", "BW10", "BW40",
423 		"BW80", "BW160", "BW_HE_RU"
424 	};
425 
426 	if (!rate->legacy && !rate->flags)
427 		return 0;
428 
429 	seq_puts(s, "Probing rate - ");
430 	if (rate->flags & RATE_INFO_FLAGS_MCS)
431 		seq_puts(s, "HT ");
432 	else if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
433 		seq_puts(s, "VHT ");
434 	else if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
435 		seq_puts(s, "HE ");
436 	else
437 		seq_printf(s, "Bitrate %d\n", rate->legacy);
438 
439 	if (rate->flags) {
440 		seq_printf(s, "%s NSS%d MCS%d ",
441 			   bw[rate->bw], rate->nss, rate->mcs);
442 
443 		if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
444 			seq_puts(s, "SGI ");
445 		else if (rate->he_gi)
446 			seq_puts(s, "HE GI ");
447 
448 		if (rate->he_dcm)
449 			seq_puts(s, "DCM ");
450 	}
451 
452 	seq_printf(s, "\nPPDU PER: %ld.%1ld%%\n",
453 		   stats->per / 10, stats->per % 10);
454 
455 	return 0;
456 }
457 
458 static int
459 mt7915_sta_stats_open(struct inode *inode, struct file *f)
460 {
461 	return single_open(f, mt7915_sta_stats_read, inode->i_private);
462 }
463 
464 static const struct file_operations fops_sta_stats = {
465 	.open = mt7915_sta_stats_open,
466 	.read = seq_read,
467 	.llseek = seq_lseek,
468 	.release = single_release,
469 	.owner = THIS_MODULE,
470 };
471 
472 void mt7915_sta_add_debugfs(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
473 			    struct ieee80211_sta *sta, struct dentry *dir)
474 {
475 	debugfs_create_file("fixed_rate", 0600, dir, sta, &fops_fixed_rate);
476 	debugfs_create_file("stats", 0400, dir, sta, &fops_sta_stats);
477 }
478 #endif
479