blob: 30eef7a5eaab1f997027a133660b3ec677fe98f6 [file] [log] [blame]
developerde9ecce2023-05-22 11:17:16 +08001From 058747d35c83a796712bec098cf6509965cf28cc Mon Sep 17 00:00:00 2001
developer1bc2ce22023-03-25 00:47:41 +08002From: StanleyYP Wang <StanleyYP.Wang@mediatek.com>
3Date: Wed, 1 Mar 2023 11:59:16 +0800
developerde9ecce2023-05-22 11:17:16 +08004Subject: [PATCH 20/22] mt76: testmode: add basic testmode support
developer1bc2ce22023-03-25 00:47:41 +08005
6Signed-off-by: StanleyYP Wang <StanleyYP.Wang@mediatek.com>
7---
8 eeprom.c | 6 +-
9 mac80211.c | 3 +-
developerde9ecce2023-05-22 11:17:16 +080010 mt76.h | 33 +++
developer1bc2ce22023-03-25 00:47:41 +080011 mt76_connac_mcu.h | 2 +
12 mt7996/Makefile | 2 +
13 mt7996/eeprom.c | 35 ++-
14 mt7996/eeprom.h | 1 +
15 mt7996/init.c | 7 +
16 mt7996/main.c | 15 ++
17 mt7996/mcu.c | 39 ++-
18 mt7996/mcu.h | 27 ++
19 mt7996/mt7996.h | 22 ++
developerde9ecce2023-05-22 11:17:16 +080020 mt7996/testmode.c | 632 ++++++++++++++++++++++++++++++++++++++++++++++
21 mt7996/testmode.h | 295 ++++++++++++++++++++++
22 testmode.c | 73 ++++--
developer1bc2ce22023-03-25 00:47:41 +080023 testmode.h | 60 +++++
24 tools/fields.c | 92 +++++++
developerde9ecce2023-05-22 11:17:16 +080025 17 files changed, 1315 insertions(+), 29 deletions(-)
developer1bc2ce22023-03-25 00:47:41 +080026 create mode 100644 mt7996/testmode.c
27 create mode 100644 mt7996/testmode.h
28
29diff --git a/eeprom.c b/eeprom.c
developerde9ecce2023-05-22 11:17:16 +080030index ea54b7af..263e5089 100644
developer1bc2ce22023-03-25 00:47:41 +080031--- a/eeprom.c
32+++ b/eeprom.c
33@@ -89,8 +89,10 @@ int mt76_get_of_eeprom(struct mt76_dev *dev, void *eep, int offset, int len)
34 }
35
36 #ifdef CONFIG_NL80211_TESTMODE
37- dev->test_mtd.name = devm_kstrdup(dev->dev, part, GFP_KERNEL);
38- dev->test_mtd.offset = offset;
39+ if (len == dev->eeprom.size) {
40+ dev->test_mtd.name = devm_kstrdup(dev->dev, part, GFP_KERNEL);
41+ dev->test_mtd.offset = offset;
42+ }
43 #endif
44
45 out_put_node:
46diff --git a/mac80211.c b/mac80211.c
developerde9ecce2023-05-22 11:17:16 +080047index 467afef9..d1cdaee8 100644
developer1bc2ce22023-03-25 00:47:41 +080048--- a/mac80211.c
49+++ b/mac80211.c
50@@ -826,7 +826,8 @@ void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
51 }
52
53 #ifdef CONFIG_NL80211_TESTMODE
54- if (phy->test.state == MT76_TM_STATE_RX_FRAMES) {
55+ if (!(phy->test.flag & MT_TM_FW_RX_COUNT) &&
56+ phy->test.state == MT76_TM_STATE_RX_FRAMES) {
57 phy->test.rx_stats.packets[q]++;
58 if (status->flag & RX_FLAG_FAILED_FCS_CRC)
59 phy->test.rx_stats.fcs_error[q]++;
60diff --git a/mt76.h b/mt76.h
developerde9ecce2023-05-22 11:17:16 +080061index 6b07b8fa..31d5dc37 100644
developer1bc2ce22023-03-25 00:47:41 +080062--- a/mt76.h
63+++ b/mt76.h
64@@ -652,8 +652,12 @@ struct mt76_testmode_ops {
65 int (*set_params)(struct mt76_phy *phy, struct nlattr **tb,
66 enum mt76_testmode_state new_state);
67 int (*dump_stats)(struct mt76_phy *phy, struct sk_buff *msg);
68+ void (*reset_rx_stats)(struct mt76_phy *phy);
69+ void (*tx_stop)(struct mt76_phy *phy);
70 };
71
72+#define MT_TM_FW_RX_COUNT BIT(0)
73+
74 struct mt76_testmode_data {
75 enum mt76_testmode_state state;
76
77@@ -669,6 +673,7 @@ struct mt76_testmode_data {
78 u8 tx_rate_sgi;
79 u8 tx_rate_ldpc;
80 u8 tx_rate_stbc;
81+ u16 tx_preamble_puncture;
82 u8 tx_ltf;
83
84 u8 tx_antenna_mask;
85@@ -678,6 +683,9 @@ struct mt76_testmode_data {
86 u32 tx_time;
87 u32 tx_ipg;
88
89+ bool ibf;
90+ bool ebf;
91+
92 u32 freq_offset;
93
94 u8 tx_power[4];
developerde9ecce2023-05-22 11:17:16 +080095@@ -692,7 +700,16 @@ struct mt76_testmode_data {
developer1bc2ce22023-03-25 00:47:41 +080096 struct {
97 u64 packets[__MT_RXQ_MAX];
98 u64 fcs_error[__MT_RXQ_MAX];
99+ u64 len_mismatch;
100 } rx_stats;
101+ u8 flag;
developerde9ecce2023-05-22 11:17:16 +0800102+
103+ struct {
104+ u8 type;
105+ u8 enable;
106+ } cfg;
107+
developer1bc2ce22023-03-25 00:47:41 +0800108+ u8 aid;
109 };
110
111 struct mt76_vif {
developerde9ecce2023-05-22 11:17:16 +0800112@@ -1258,6 +1275,22 @@ int mt76_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
113 int mt76_testmode_set_state(struct mt76_phy *phy, enum mt76_testmode_state state);
114 int mt76_testmode_alloc_skb(struct mt76_phy *phy, u32 len);
115
116+static inline void
117+mt76_testmode_param_set(struct mt76_testmode_data *td, u16 idx)
118+{
119+#ifdef CONFIG_NL80211_TESTMODE
120+ td->param_set[idx / 32] |= BIT(idx % 32);
121+#endif
122+}
123+
124+static inline bool
125+mt76_testmode_param_present(struct mt76_testmode_data *td, u16 idx)
126+{
127+#ifdef CONFIG_NL80211_TESTMODE
128+ return td->param_set[idx / 32] & BIT(idx % 32);
129+#endif
130+}
131+
132 static inline void mt76_testmode_reset(struct mt76_phy *phy, bool disable)
133 {
134 #ifdef CONFIG_NL80211_TESTMODE
developer1bc2ce22023-03-25 00:47:41 +0800135diff --git a/mt76_connac_mcu.h b/mt76_connac_mcu.h
developerde9ecce2023-05-22 11:17:16 +0800136index fbb1206f..17cb8128 100644
developer1bc2ce22023-03-25 00:47:41 +0800137--- a/mt76_connac_mcu.h
138+++ b/mt76_connac_mcu.h
139@@ -1218,10 +1218,12 @@ enum {
140 MCU_UNI_CMD_EFUSE_CTRL = 0x2d,
141 MCU_UNI_CMD_RA = 0x2f,
142 MCU_UNI_CMD_MURU = 0x31,
143+ MCU_UNI_CMD_TESTMODE_RX_STAT = 0x32,
144 MCU_UNI_CMD_BF = 0x33,
145 MCU_UNI_CMD_CHANNEL_SWITCH = 0x34,
146 MCU_UNI_CMD_THERMAL = 0x35,
147 MCU_UNI_CMD_VOW = 0x37,
148+ MCU_UNI_CMD_TESTMODE_CTRL = 0x46,
149 MCU_UNI_CMD_RRO = 0x57,
150 MCU_UNI_CMD_OFFCH_SCAN_CTRL = 0x58,
developerde9ecce2023-05-22 11:17:16 +0800151 MCU_UNI_CMD_ASSERT_DUMP = 0x6f,
developer1bc2ce22023-03-25 00:47:41 +0800152diff --git a/mt7996/Makefile b/mt7996/Makefile
developerde9ecce2023-05-22 11:17:16 +0800153index 07c8b555..bed9efd6 100644
developer1bc2ce22023-03-25 00:47:41 +0800154--- a/mt7996/Makefile
155+++ b/mt7996/Makefile
developerde9ecce2023-05-22 11:17:16 +0800156@@ -6,3 +6,5 @@ mt7996e-y := pci.o init.o dma.o eeprom.o main.o mcu.o mac.o \
developer1bc2ce22023-03-25 00:47:41 +0800157 debugfs.o mmio.o
developerde9ecce2023-05-22 11:17:16 +0800158
159 mt7996e-$(CONFIG_DEV_COREDUMP) += coredump.o
developer1bc2ce22023-03-25 00:47:41 +0800160+
161+mt7996e-$(CONFIG_NL80211_TESTMODE) += testmode.o
162diff --git a/mt7996/eeprom.c b/mt7996/eeprom.c
developerde9ecce2023-05-22 11:17:16 +0800163index 544b6c6f..f5e95460 100644
developer1bc2ce22023-03-25 00:47:41 +0800164--- a/mt7996/eeprom.c
165+++ b/mt7996/eeprom.c
166@@ -6,6 +6,11 @@
167 #include <linux/firmware.h>
168 #include "mt7996.h"
169 #include "eeprom.h"
170+#include <linux/moduleparam.h>
171+
172+static bool testmode_enable;
173+module_param(testmode_enable, bool, 0644);
174+MODULE_PARM_DESC(testmode_enable, "Enable testmode");
175
176 static int mt7996_check_eeprom(struct mt7996_dev *dev)
177 {
178@@ -23,7 +28,10 @@ static int mt7996_check_eeprom(struct mt7996_dev *dev)
179 static char *mt7996_eeprom_name(struct mt7996_dev *dev)
180 {
181 /* reserve for future variants */
182- return MT7996_EEPROM_DEFAULT;
183+ if (dev->testmode_enable)
184+ return MT7996_EEPROM_DEFAULT_TM;
185+ else
186+ return MT7996_EEPROM_DEFAULT;
187 }
188
189 static int
190@@ -52,21 +60,36 @@ out:
191 return ret;
192 }
193
194-static int mt7996_eeprom_load(struct mt7996_dev *dev)
195+int mt7996_eeprom_check_fw_mode(struct mt7996_dev *dev)
196 {
197+ u8 *eeprom;
198 int ret;
199
200+ /* load eeprom in flash or bin file mode to determine fw mode */
201 ret = mt76_eeprom_init(&dev->mt76, MT7996_EEPROM_SIZE);
202 if (ret < 0)
203 return ret;
204
205 if (ret) {
206 dev->flash_mode = true;
207- } else {
208- u8 free_block_num;
209- u32 block_num, i;
210- u32 eeprom_blk_size = MT7996_EEPROM_BLOCK_SIZE;
211+ eeprom = dev->mt76.eeprom.data;
212+ /* testmode enable priority: eeprom field > module parameter */
213+ dev->testmode_enable = !mt7996_check_eeprom(dev) ? eeprom[MT_EE_TESTMODE_EN] :
214+ testmode_enable;
215+ }
216+
217+ return ret;
218+}
219+
220+static int mt7996_eeprom_load(struct mt7996_dev *dev)
221+{
222+ int ret;
223+ u8 free_block_num;
224+ u32 block_num, i;
225+ u32 eeprom_blk_size = MT7996_EEPROM_BLOCK_SIZE;
226
227+ /* flash or bin file mode eeprom is loaded before mcu init */
228+ if (!dev->flash_mode) {
229 ret = mt7996_mcu_get_eeprom_free_block(dev, &free_block_num);
230 if (ret < 0)
231 return ret;
232diff --git a/mt7996/eeprom.h b/mt7996/eeprom.h
developerde9ecce2023-05-22 11:17:16 +0800233index 0c749774..0f8f0cd8 100644
developer1bc2ce22023-03-25 00:47:41 +0800234--- a/mt7996/eeprom.h
235+++ b/mt7996/eeprom.h
236@@ -14,6 +14,7 @@ enum mt7996_eeprom_field {
237 MT_EE_MAC_ADDR = 0x004,
238 MT_EE_MAC_ADDR2 = 0x00a,
239 MT_EE_WIFI_CONF = 0x190,
240+ MT_EE_TESTMODE_EN = 0x1af,
241 MT_EE_MAC_ADDR3 = 0x2c0,
242 MT_EE_RATE_DELTA_2G = 0x1400,
243 MT_EE_RATE_DELTA_5G = 0x147d,
244diff --git a/mt7996/init.c b/mt7996/init.c
developerde9ecce2023-05-22 11:17:16 +0800245index d44e3ae9..36ead6db 100644
developer1bc2ce22023-03-25 00:47:41 +0800246--- a/mt7996/init.c
247+++ b/mt7996/init.c
developerde9ecce2023-05-22 11:17:16 +0800248@@ -639,6 +639,10 @@ static int mt7996_init_hardware(struct mt7996_dev *dev)
developer1bc2ce22023-03-25 00:47:41 +0800249
250 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
251
252+ ret = mt7996_eeprom_check_fw_mode(dev);
253+ if (ret < 0)
254+ return ret;
255+
256 ret = mt7996_mcu_init(dev);
257 if (ret)
258 return ret;
developerde9ecce2023-05-22 11:17:16 +0800259@@ -1054,6 +1058,9 @@ int mt7996_register_device(struct mt7996_dev *dev)
developer1bc2ce22023-03-25 00:47:41 +0800260
261 mt7996_init_wiphy(hw);
262
263+#ifdef CONFIG_NL80211_TESTMODE
264+ dev->mt76.test_ops = &mt7996_testmode_ops;
265+#endif
266 /* init led callbacks */
267 if (IS_ENABLED(CONFIG_MT76_LEDS)) {
268 dev->mphy.leds.cdev.brightness_set = mt7996_led_set_brightness;
269diff --git a/mt7996/main.c b/mt7996/main.c
developerde9ecce2023-05-22 11:17:16 +0800270index 059cc420..3820b235 100644
developer1bc2ce22023-03-25 00:47:41 +0800271--- a/mt7996/main.c
272+++ b/mt7996/main.c
developerde9ecce2023-05-22 11:17:16 +0800273@@ -23,6 +23,17 @@ static bool mt7996_dev_running(struct mt7996_dev *dev)
developer1bc2ce22023-03-25 00:47:41 +0800274 return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
275 }
276
277+static void mt7996_testmode_disable_all(struct mt7996_dev *dev)
278+{
279+ struct mt7996_phy *phy;
280+ int i;
281+
282+ for (i = 0; i < __MT_MAX_BAND; i++) {
283+ phy = __mt7996_phy(dev, i);
284+ mt76_testmode_set_state(phy->mt76, MT76_TM_STATE_OFF);
285+ }
286+}
287+
288 int mt7996_run(struct ieee80211_hw *hw)
289 {
290 struct mt7996_dev *dev = mt7996_hw_dev(hw);
developerde9ecce2023-05-22 11:17:16 +0800291@@ -37,6 +48,8 @@ int mt7996_run(struct ieee80211_hw *hw)
developer1bc2ce22023-03-25 00:47:41 +0800292 goto out;
293 }
294
295+ mt7996_testmode_disable_all(dev);
296+
297 mt7996_mac_enable_nf(dev, phy->mt76->band_idx);
298
299 ret = mt7996_mcu_set_rts_thresh(phy, 0x92b);
developerde9ecce2023-05-22 11:17:16 +0800300@@ -1390,6 +1403,8 @@ const struct ieee80211_ops mt7996_ops = {
developer1bc2ce22023-03-25 00:47:41 +0800301 .sta_set_decap_offload = mt7996_sta_set_decap_offload,
302 .add_twt_setup = mt7996_mac_add_twt_setup,
303 .twt_teardown_request = mt7996_twt_teardown_request,
304+ CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
305+ CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
306 #ifdef CONFIG_MAC80211_DEBUGFS
307 .sta_add_debugfs = mt7996_sta_add_debugfs,
308 #endif
309diff --git a/mt7996/mcu.c b/mt7996/mcu.c
developerde9ecce2023-05-22 11:17:16 +0800310index 43a4f939..24adeb12 100644
developer1bc2ce22023-03-25 00:47:41 +0800311--- a/mt7996/mcu.c
312+++ b/mt7996/mcu.c
developerde9ecce2023-05-22 11:17:16 +0800313@@ -2467,7 +2467,10 @@ static int mt7996_load_ram(struct mt7996_dev *dev)
developer1bc2ce22023-03-25 00:47:41 +0800314 release_firmware(fw); \
315 } while (0)
316
317- LOAD_RAM(WM);
318+ if (dev->testmode_enable)
319+ LOAD_RAM(WM_TM);
320+ else
321+ LOAD_RAM(WM);
322 LOAD_RAM(DSP);
323 LOAD_RAM(WA);
324 #undef LOAD_RAM
developerde9ecce2023-05-22 11:17:16 +0800325@@ -4012,3 +4015,37 @@ int mt7996_mcu_set_rro(struct mt7996_dev *dev, u16 tag, u8 val)
developer1bc2ce22023-03-25 00:47:41 +0800326 return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(RRO), &req,
327 sizeof(req), true);
328 }
329+
330+int mt7996_mcu_set_tx_power_ctrl(struct mt7996_phy *phy, u8 power_ctrl_id, u8 data)
331+{
332+ struct mt7996_dev *dev = phy->dev;
333+ struct tx_power_ctrl req = {
334+ .tag = cpu_to_le16(power_ctrl_id),
335+ .len = cpu_to_le16(sizeof(req) - 4),
336+ .power_ctrl_id = power_ctrl_id,
337+ .band_idx = phy->mt76->band_idx,
338+ };
339+
340+ switch (power_ctrl_id) {
341+ case UNI_TXPOWER_SKU_POWER_LIMIT_CTRL:
342+ req.sku_enable = !!data;
343+ break;
344+ case UNI_TXPOWER_PERCENTAGE_CTRL:
345+ req.percentage_ctrl_enable = !!data;
346+ break;
347+ case UNI_TXPOWER_PERCENTAGE_DROP_CTRL:
348+ req.power_drop_level = data;
349+ break;
350+ case UNI_TXPOWER_BACKOFF_POWER_LIMIT_CTRL:
351+ req.bf_backoff_enable = !!data;
352+ break;
353+ case UNI_TXPOWER_ATE_MODE_CTRL:
354+ req.ate_mode_enable = !!data;
355+ break;
356+ default:
357+ req.sku_enable = !!data;
358+ }
359+
360+ return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(TXPOWER),
361+ &req, sizeof(req), false);
362+}
363diff --git a/mt7996/mcu.h b/mt7996/mcu.h
developerde9ecce2023-05-22 11:17:16 +0800364index 778deedf..ebc62713 100644
developer1bc2ce22023-03-25 00:47:41 +0800365--- a/mt7996/mcu.h
366+++ b/mt7996/mcu.h
367@@ -686,6 +686,33 @@ enum {
368 UNI_CMD_THERMAL_PROTECT_DUTY_CONFIG,
369 };
370
371+struct tx_power_ctrl {
372+ u8 _rsv[4];
373+
374+ __le16 tag;
375+ __le16 len;
376+
377+ u8 power_ctrl_id;
378+ union {
379+ bool sku_enable;
380+ bool ate_mode_enable;
381+ bool percentage_ctrl_enable;
382+ bool bf_backoff_enable;
383+ u8 power_drop_level;
384+ };
385+ u8 band_idx;
386+ u8 rsv[1];
387+} __packed;
388+
389+enum {
390+ UNI_TXPOWER_SKU_POWER_LIMIT_CTRL = 0,
391+ UNI_TXPOWER_PERCENTAGE_CTRL = 1,
392+ UNI_TXPOWER_PERCENTAGE_DROP_CTRL = 2,
393+ UNI_TXPOWER_BACKOFF_POWER_LIMIT_CTRL = 3,
394+ UNI_TXPOWER_POWER_LIMIT_TABLE_CTRL = 4,
395+ UNI_TXPOWER_ATE_MODE_CTRL = 6,
396+};
397+
398 enum {
399 UNI_CMD_ACCESS_REG_BASIC = 0x0,
400 UNI_CMD_ACCESS_RF_REG_BASIC,
401diff --git a/mt7996/mt7996.h b/mt7996/mt7996.h
developerde9ecce2023-05-22 11:17:16 +0800402index ab4521a4..9bf3bf1a 100644
developer1bc2ce22023-03-25 00:47:41 +0800403--- a/mt7996/mt7996.h
404+++ b/mt7996/mt7996.h
405@@ -30,9 +30,11 @@
406 #define MT7996_FIRMWARE_WA "mediatek/mt7996/mt7996_wa.bin"
407 #define MT7996_FIRMWARE_WM "mediatek/mt7996/mt7996_wm.bin"
408 #define MT7996_FIRMWARE_DSP "mediatek/mt7996/mt7996_dsp.bin"
409+#define MT7996_FIRMWARE_WM_TM "mediatek/mt7996/mt7996_wm_tm.bin"
410 #define MT7996_ROM_PATCH "mediatek/mt7996/mt7996_rom_patch.bin"
411
412 #define MT7996_EEPROM_DEFAULT "mediatek/mt7996/mt7996_eeprom.bin"
413+#define MT7996_EEPROM_DEFAULT_TM "mediatek/mt7996/mt7996_eeprom_tm.bin"
414 #define MT7996_EEPROM_SIZE 7680
415 #define MT7996_EEPROM_BLOCK_SIZE 16
416 #define MT7996_TOKEN_SIZE 8192
developerde9ecce2023-05-22 11:17:16 +0800417@@ -62,6 +64,7 @@ struct mt7996_dfs_pattern;
developer1bc2ce22023-03-25 00:47:41 +0800418
419 enum mt7996_ram_type {
420 MT7996_RAM_TYPE_WM = 0,
421+ MT7996_RAM_TYPE_WM_TM = MT7996_RAM_TYPE_WM,
422 MT7996_RAM_TYPE_WA,
423 MT7996_RAM_TYPE_DSP,
424 };
developerde9ecce2023-05-22 11:17:16 +0800425@@ -243,6 +246,20 @@ struct mt7996_phy {
developer1bc2ce22023-03-25 00:47:41 +0800426
427 struct mib_stats mib;
428 struct mt76_channel_state state_ts;
429+
430+#ifdef CONFIG_NL80211_TESTMODE
431+ struct {
432+ u32 *reg_backup;
433+
434+ s32 last_freq_offset;
435+ u8 last_rcpi[4];
436+ s8 last_ib_rssi[4];
437+ s8 last_wb_rssi[4];
438+ u8 last_snr;
439+
440+ u8 spe_idx;
441+ } test;
442+#endif
443 };
444
445 struct mt7996_dev {
developerde9ecce2023-05-22 11:17:16 +0800446@@ -302,6 +319,8 @@ struct mt7996_dev {
developer1bc2ce22023-03-25 00:47:41 +0800447 bool flash_mode:1;
448 bool has_eht:1;
449
450+ bool testmode_enable;
451+
452 bool ibf;
453 u8 fw_debug_wm;
454 u8 fw_debug_wa;
developerde9ecce2023-05-22 11:17:16 +0800455@@ -407,6 +426,7 @@ mt7996_phy3(struct mt7996_dev *dev)
developer1bc2ce22023-03-25 00:47:41 +0800456 extern const struct ieee80211_ops mt7996_ops;
457 extern struct pci_driver mt7996_pci_driver;
458 extern struct pci_driver mt7996_hif_driver;
459+extern const struct mt76_testmode_ops mt7996_testmode_ops;
460
461 struct mt7996_dev *mt7996_mmio_probe(struct device *pdev,
462 void __iomem *mem_base, u32 device_id);
developerde9ecce2023-05-22 11:17:16 +0800463@@ -416,6 +436,7 @@ u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif *mvif);
developer1bc2ce22023-03-25 00:47:41 +0800464 int mt7996_register_device(struct mt7996_dev *dev);
465 void mt7996_unregister_device(struct mt7996_dev *dev);
466 int mt7996_eeprom_init(struct mt7996_dev *dev);
467+int mt7996_eeprom_check_fw_mode(struct mt7996_dev *dev);
468 int mt7996_eeprom_parse_hw_cap(struct mt7996_dev *dev, struct mt7996_phy *phy);
469 int mt7996_eeprom_get_target_power(struct mt7996_dev *dev,
470 struct ieee80211_channel *chan);
developerde9ecce2023-05-22 11:17:16 +0800471@@ -492,6 +513,7 @@ int mt7996_mcu_fw_dbg_ctrl(struct mt7996_dev *dev, u32 module, u8 level);
472 int mt7996_mcu_trigger_assert(struct mt7996_dev *dev);
developer1bc2ce22023-03-25 00:47:41 +0800473 void mt7996_mcu_rx_event(struct mt7996_dev *dev, struct sk_buff *skb);
474 void mt7996_mcu_exit(struct mt7996_dev *dev);
475+int mt7996_mcu_set_tx_power_ctrl(struct mt7996_phy *phy, u8 power_ctrl_id, u8 data);
476
477 static inline u8 mt7996_max_interface_num(struct mt7996_dev *dev)
478 {
479diff --git a/mt7996/testmode.c b/mt7996/testmode.c
480new file mode 100644
developerde9ecce2023-05-22 11:17:16 +0800481index 00000000..6d7cdbd5
developer1bc2ce22023-03-25 00:47:41 +0800482--- /dev/null
483+++ b/mt7996/testmode.c
developerde9ecce2023-05-22 11:17:16 +0800484@@ -0,0 +1,632 @@
developer1bc2ce22023-03-25 00:47:41 +0800485+// SPDX-License-Identifier: ISC
486+/*
487+ * Copyright (C) 2022 MediaTek Inc.
488+ */
489+
490+#include "mt7996.h"
491+#include "mac.h"
492+#include "mcu.h"
493+#include "testmode.h"
494+
495+enum {
496+ TM_CHANGED_TXPOWER,
497+ TM_CHANGED_FREQ_OFFSET,
498+ TM_CHANGED_TX_LENGTH,
499+ TM_CHANGED_TX_TIME,
developerde9ecce2023-05-22 11:17:16 +0800500+ TM_CHANGED_CFG,
developer1bc2ce22023-03-25 00:47:41 +0800501+
502+ /* must be last */
503+ NUM_TM_CHANGED
504+};
505+
506+static const u8 tm_change_map[] = {
507+ [TM_CHANGED_TXPOWER] = MT76_TM_ATTR_TX_POWER,
508+ [TM_CHANGED_FREQ_OFFSET] = MT76_TM_ATTR_FREQ_OFFSET,
509+ [TM_CHANGED_TX_LENGTH] = MT76_TM_ATTR_TX_LENGTH,
510+ [TM_CHANGED_TX_TIME] = MT76_TM_ATTR_TX_TIME,
developerde9ecce2023-05-22 11:17:16 +0800511+ [TM_CHANGED_CFG] = MT76_TM_ATTR_CFG,
developer1bc2ce22023-03-25 00:47:41 +0800512+};
513+
514+static u8 mt7996_tm_bw_mapping(enum nl80211_chan_width width, enum bw_mapping_method method)
515+{
516+ static const u8 width_to_bw[][NUM_BW_MAP] = {
517+ [NL80211_CHAN_WIDTH_40] = {FW_CDBW_40MHZ, TM_CBW_40MHZ},
518+ [NL80211_CHAN_WIDTH_80] = {FW_CDBW_80MHZ, TM_CBW_80MHZ},
519+ [NL80211_CHAN_WIDTH_80P80] = {FW_CDBW_8080MHZ, TM_CBW_8080MHZ},
520+ [NL80211_CHAN_WIDTH_160] = {FW_CDBW_160MHZ, TM_CBW_160MHZ},
521+ [NL80211_CHAN_WIDTH_5] = {FW_CDBW_5MHZ, TM_CBW_5MHZ},
522+ [NL80211_CHAN_WIDTH_10] = {FW_CDBW_10MHZ, TM_CBW_10MHZ},
523+ [NL80211_CHAN_WIDTH_20] = {FW_CDBW_20MHZ, TM_CBW_20MHZ},
524+ [NL80211_CHAN_WIDTH_20_NOHT] = {FW_CDBW_20MHZ, TM_CBW_20MHZ},
525+ [NL80211_CHAN_WIDTH_320] = {FW_CDBW_320MHZ, TM_CBW_320MHZ},
526+ };
527+
528+ if (width >= ARRAY_SIZE(width_to_bw))
529+ return 0;
530+
531+ return width_to_bw[width][method];
532+}
533+
534+static u8 mt7996_tm_rate_to_phy(u8 tx_rate_mode)
535+{
536+ static const u8 rate_to_phy[] = {
537+ [MT76_TM_TX_MODE_CCK] = MT_PHY_TYPE_CCK,
538+ [MT76_TM_TX_MODE_OFDM] = MT_PHY_TYPE_OFDM,
539+ [MT76_TM_TX_MODE_HT] = MT_PHY_TYPE_HT,
540+ [MT76_TM_TX_MODE_VHT] = MT_PHY_TYPE_VHT,
541+ [MT76_TM_TX_MODE_HE_SU] = MT_PHY_TYPE_HE_SU,
542+ [MT76_TM_TX_MODE_HE_EXT_SU] = MT_PHY_TYPE_HE_EXT_SU,
543+ [MT76_TM_TX_MODE_HE_TB] = MT_PHY_TYPE_HE_TB,
544+ [MT76_TM_TX_MODE_HE_MU] = MT_PHY_TYPE_HE_MU,
545+ [MT76_TM_TX_MODE_EHT_SU] = MT_PHY_TYPE_EHT_SU,
546+ [MT76_TM_TX_MODE_EHT_TRIG] = MT_PHY_TYPE_EHT_TRIG,
547+ [MT76_TM_TX_MODE_EHT_MU] = MT_PHY_TYPE_EHT_MU,
548+ };
549+
550+ if (tx_rate_mode > MT76_TM_TX_MODE_MAX)
551+ return -EINVAL;
552+
553+ return rate_to_phy[tx_rate_mode];
554+}
555+
556+static int
557+mt7996_tm_set(struct mt7996_dev *dev, u32 func_idx, u32 data)
558+{
559+ struct mt7996_tm_req req = {
560+ .rf_test = {
561+ .tag = cpu_to_le16(UNI_RF_TEST_CTRL),
562+ .len = cpu_to_le16(sizeof(req.rf_test)),
563+ .action = RF_ACTION_SET,
564+ .op.rf.func_idx = func_idx,
565+ .op.rf.param.func_data = cpu_to_le32(data),
566+ },
567+ };
568+
569+ return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(TESTMODE_CTRL), &req,
570+ sizeof(req), false);
571+}
572+
573+static int
574+mt7996_tm_get(struct mt7996_dev *dev, u32 func_idx, u32 data, u32 *result)
575+{
576+ struct mt7996_tm_req req = {
577+ .rf_test = {
578+ .tag = cpu_to_le16(UNI_RF_TEST_CTRL),
579+ .len = cpu_to_le16(sizeof(req.rf_test)),
580+ .action = RF_ACTION_GET,
581+ .op.rf.func_idx = func_idx,
582+ .op.rf.param.func_data = cpu_to_le32(data),
583+ },
584+ };
585+ struct mt7996_tm_event *event;
586+ struct sk_buff *skb;
587+ int ret;
588+
589+ ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_WM_UNI_CMD_QUERY(TESTMODE_CTRL),
590+ &req, sizeof(req), true, &skb);
591+ if (ret)
592+ return ret;
593+
594+ event = (struct mt7996_tm_event *)skb->data;
595+ *result = event->result.payload_length;
596+
597+ dev_kfree_skb(skb);
598+
599+ return ret;
600+}
601+
602+static void
603+mt7996_tm_set_antenna(struct mt7996_phy *phy, u32 func_idx)
604+{
605+#define SPE_INDEX_MASK BIT(31)
606+#define RX_ANTENNA_MASK GENMASK(20, 16) /* RX antenna mask at most 5 bit */
607+ struct mt7996_dev *dev = phy->dev;
608+ struct mt76_testmode_data *td = &phy->mt76->test;
developerde9ecce2023-05-22 11:17:16 +0800609+ u32 antenna_mask;
developer1bc2ce22023-03-25 00:47:41 +0800610+
developerde9ecce2023-05-22 11:17:16 +0800611+ if (!mt76_testmode_param_present(td, MT76_TM_ATTR_TX_ANTENNA))
developer1bc2ce22023-03-25 00:47:41 +0800612+ return;
613+
614+ if (func_idx == SET_ID(TX_PATH))
615+ antenna_mask = td->tx_spe_idx ? (SPE_INDEX_MASK | td->tx_spe_idx) :
616+ td->tx_antenna_mask;
617+ else if (func_idx == SET_ID(RX_PATH))
618+ antenna_mask = u32_encode_bits(td->tx_antenna_mask, RX_ANTENNA_MASK);
619+ else
620+ return;
621+
622+ mt7996_tm_set(dev, func_idx, antenna_mask);
623+}
624+
625+static void
626+mt7996_tm_set_mac_addr(struct mt7996_dev *dev, u8 *addr, u32 func_idx)
627+{
628+#define REMAIN_PART_TAG BIT(18)
629+ u32 own_mac_first = 0, own_mac_remain = 0;
630+ int len = sizeof(u32);
631+
632+ memcpy(&own_mac_first, addr, len);
633+ mt7996_tm_set(dev, func_idx, own_mac_first);
634+ /* Set the remain part of mac address */
635+ memcpy(&own_mac_remain, addr + len, ETH_ALEN - len);
636+ mt7996_tm_set(dev, func_idx | REMAIN_PART_TAG, own_mac_remain);
637+}
638+
639+static int
640+mt7996_tm_rf_switch_mode(struct mt7996_dev *dev, u32 op_mode)
641+{
642+ struct mt7996_tm_req req = {
643+ .rf_test = {
644+ .tag = cpu_to_le16(UNI_RF_TEST_CTRL),
645+ .len = cpu_to_le16(sizeof(req.rf_test)),
646+ .action = RF_ACTION_SWITCH_TO_RF_TEST,
647+ .op.op_mode = cpu_to_le32(op_mode),
648+ },
649+ };
650+
651+ return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(TESTMODE_CTRL), &req,
652+ sizeof(req), false);
653+}
654+
655+static void
656+mt7996_tm_init(struct mt7996_phy *phy, bool en)
657+{
658+#define POWER_CTRL(type) UNI_TXPOWER_##type##_CTRL
659+ struct mt7996_dev *dev = phy->dev;
660+ u8 rf_test_mode = en ? RF_OPER_RF_TEST : RF_OPER_NORMAL;
661+
662+ if (!test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
663+ return;
664+
665+ mt7996_mcu_set_tx_power_ctrl(phy, POWER_CTRL(ATE_MODE), en);
666+ mt7996_mcu_set_tx_power_ctrl(phy, POWER_CTRL(SKU_POWER_LIMIT), !en);
667+ mt7996_mcu_set_tx_power_ctrl(phy, POWER_CTRL(PERCENTAGE_DROP), 100);
668+ mt7996_mcu_set_tx_power_ctrl(phy, POWER_CTRL(PERCENTAGE), !en);
669+ mt7996_mcu_set_tx_power_ctrl(phy, POWER_CTRL(BACKOFF_POWER_LIMIT), !en);
670+
671+ mt7996_tm_rf_switch_mode(dev, rf_test_mode);
672+
673+ mt7996_mcu_add_bss_info(phy, phy->monitor_vif, en);
674+ mt7996_mcu_add_sta(dev, phy->monitor_vif, NULL, en);
675+
676+ mt7996_tm_set(dev, SET_ID(BAND_IDX), phy->mt76->band_idx);
677+
678+ /* use firmware counter for RX stats */
679+ phy->mt76->test.flag |= MT_TM_FW_RX_COUNT;
680+}
681+
682+static void
683+mt7996_tm_update_channel(struct mt7996_phy *phy)
684+{
685+#define CHAN_FREQ_BW_80P80_TAG (SET_ID(CHAN_FREQ) | BIT(16))
686+ struct mt7996_dev *dev = phy->dev;
687+ struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
688+ struct ieee80211_channel *chan = chandef->chan;
689+ u8 width = chandef->width;
690+ static const u8 ch_band[] = {
691+ [NL80211_BAND_2GHZ] = 0,
692+ [NL80211_BAND_5GHZ] = 1,
693+ [NL80211_BAND_6GHZ] = 2,
694+ };
695+
696+ if (!chan || !chandef) {
697+ dev_info(dev->mt76.dev, "chandef not found, channel update failed!\n");
698+ return;
699+ }
700+
701+ /* system bw */
702+ mt7996_tm_set(dev, SET_ID(CBW), mt7996_tm_bw_mapping(width, BW_MAP_NL_TO_FW));
703+
704+ if (width == NL80211_CHAN_WIDTH_80P80) {
705+ width = NL80211_CHAN_WIDTH_160;
706+ mt7996_tm_set(dev, CHAN_FREQ_BW_80P80_TAG, chandef->center_freq2 * 1000);
707+ }
708+
709+ /* TODO: define per-packet bw */
710+ /* per-packet bw */
711+ mt7996_tm_set(dev, SET_ID(DBW), mt7996_tm_bw_mapping(width, BW_MAP_NL_TO_FW));
712+
713+ /* control channel selection index */
714+ mt7996_tm_set(dev, SET_ID(PRIMARY_CH), 0);
715+ mt7996_tm_set(dev, SET_ID(BAND), ch_band[chan->band]);
716+
717+ /* trigger switch channel calibration */
718+ mt7996_tm_set(dev, SET_ID(CHAN_FREQ), chandef->center_freq1 * 1000);
719+
720+ // TODO: update power limit table
721+}
722+
723+static void
724+mt7996_tm_tx_stop(struct mt76_phy *mphy)
725+{
726+ struct mt76_testmode_data *td = &mphy->test;
727+ struct mt7996_phy *phy = mphy->priv;
728+ struct mt7996_dev *dev = phy->dev;
729+
730+ mt7996_tm_set(dev, SET_ID(COMMAND), RF_CMD(STOP_TEST));
731+ td->tx_pending = 0;
732+}
733+
734+static void
735+mt7996_tm_set_tx_frames(struct mt7996_phy *phy, bool en)
736+{
737+#define FRAME_CONTROL 0x88
738+ struct mt76_testmode_data *td = &phy->mt76->test;
739+ struct mt7996_dev *dev = phy->dev;
740+
741+ //TODO: RU operation, replace mcs, nss, and ldpc
742+ if (en) {
743+ mt7996_tm_set(dev, SET_ID(MAC_HEADER), FRAME_CONTROL);
744+ mt7996_tm_set(dev, SET_ID(SEQ_CTRL), 0);
745+ mt7996_tm_set(dev, SET_ID(TX_COUNT), td->tx_count);
746+ mt7996_tm_set(dev, SET_ID(TX_MODE), mt7996_tm_rate_to_phy(td->tx_rate_mode));
747+ mt7996_tm_set(dev, SET_ID(TX_RATE), td->tx_rate_idx);
developerde9ecce2023-05-22 11:17:16 +0800748+
749+ if (mt76_testmode_param_present(td, MT76_TM_ATTR_TX_POWER))
750+ mt7996_tm_set(dev, SET_ID(POWER), td->tx_power[0]);
751+
752+ if (mt76_testmode_param_present(td, MT76_TM_ATTR_TX_TIME)) {
753+ mt7996_tm_set(dev, SET_ID(TX_LEN), 0);
754+ mt7996_tm_set(dev, SET_ID(TX_TIME), td->tx_time);
755+ } else {
756+ mt7996_tm_set(dev, SET_ID(TX_LEN), td->tx_mpdu_len);
757+ mt7996_tm_set(dev, SET_ID(TX_TIME), 0);
758+ }
759+
developer1bc2ce22023-03-25 00:47:41 +0800760+ mt7996_tm_set_antenna(phy, SET_ID(TX_PATH));
761+ mt7996_tm_set(dev, SET_ID(STBC), td->tx_rate_stbc);
762+ mt7996_tm_set(dev, SET_ID(ENCODE_MODE), td->tx_rate_ldpc);
763+ mt7996_tm_set(dev, SET_ID(IBF_ENABLE), td->ibf);
764+ mt7996_tm_set(dev, SET_ID(EBF_ENABLE), td->ebf);
765+ mt7996_tm_set(dev, SET_ID(IPG), td->tx_ipg);
766+ mt7996_tm_set(dev, SET_ID(GI), td->tx_rate_sgi);
767+ mt7996_tm_set(dev, SET_ID(NSS), td->tx_rate_nss);
768+ mt7996_tm_set(dev, SET_ID(AID_OFFSET), 0);
769+ mt7996_tm_set(dev, SET_ID(PUNCTURE), td->tx_preamble_puncture);
770+
771+ mt7996_tm_set(dev, SET_ID(MAX_PE), 2);
772+ mt7996_tm_set(dev, SET_ID(HW_TX_MODE), 0);
773+ mt7996_tm_update_channel(phy);
774+
775+ /* trigger firmware to start TX */
776+ mt7996_tm_set(dev, SET_ID(COMMAND), RF_CMD(START_TX));
777+ } else {
778+ mt7996_tm_tx_stop(phy->mt76);
779+ }
780+}
781+
782+static int
783+mt7996_tm_rx_stats_user_ctrl(struct mt7996_phy *phy, u16 user_idx)
784+{
785+ struct mt7996_dev *dev = phy->dev;
786+ struct mt7996_tm_rx_req req = {
787+ .band = phy->mt76->band_idx,
788+ .user_ctrl = {
789+ .tag = cpu_to_le16(UNI_TM_RX_STAT_SET_USER_CTRL),
790+ .len = cpu_to_le16(sizeof(req.user_ctrl)),
791+ .band_idx = phy->mt76->band_idx,
792+ .user_idx = cpu_to_le16(user_idx),
793+ },
794+ };
795+
796+ return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(TESTMODE_RX_STAT), &req,
797+ sizeof(req), false);
798+}
799+
800+static void
801+mt7996_tm_set_rx_frames(struct mt7996_phy *phy, bool en)
802+{
803+#define RX_MU_DISABLE 0xf800
804+ struct mt76_testmode_data *td = &phy->mt76->test;
805+ struct mt7996_dev *dev = phy->dev;
806+ int ret;
807+
808+ if (en) {
809+ ret = mt7996_tm_rx_stats_user_ctrl(phy, td->aid);
810+ if (ret) {
811+ dev_info(dev->mt76.dev, "Set RX stats user control failed!\n");
812+ return;
813+ }
814+
815+ mt7996_tm_update_channel(phy);
816+
817+ if (td->tx_rate_mode >= MT76_TM_TX_MODE_HE_MU) {
818+ if (td->aid)
819+ ret = mt7996_tm_set(dev, SET_ID(RX_MU_AID), td->aid);
820+ else
821+ ret = mt7996_tm_set(dev, SET_ID(RX_MU_AID), RX_MU_DISABLE);
822+ }
823+ mt7996_tm_set(dev, SET_ID(TX_MODE), mt7996_tm_rate_to_phy(td->tx_rate_mode));
824+ mt7996_tm_set(dev, SET_ID(GI), td->tx_rate_sgi);
825+ mt7996_tm_set_antenna(phy, SET_ID(RX_PATH));
826+ mt7996_tm_set(dev, SET_ID(MAX_PE), 2);
827+
828+ mt7996_tm_set_mac_addr(dev, td->addr[1], SET_ID(SA));
829+
830+ /* trigger firmware to start RX */
831+ mt7996_tm_set(dev, SET_ID(COMMAND), RF_CMD(START_RX));
832+ } else {
833+ /* trigger firmware to stop RX */
834+ mt7996_tm_set(dev, SET_ID(COMMAND), RF_CMD(STOP_TEST));
835+ }
836+}
837+
838+static void
839+mt7996_tm_set_tx_cont(struct mt7996_phy *phy, bool en)
840+{
841+#define CONT_WAVE_MODE_OFDM 3
842+ struct mt76_testmode_data *td = &phy->mt76->test;
843+ struct mt7996_dev *dev = phy->dev;
844+
845+ if (en) {
846+ mt7996_tm_update_channel(phy);
847+ mt7996_tm_set(dev, SET_ID(TX_MODE), mt7996_tm_rate_to_phy(td->tx_rate_mode));
848+ mt7996_tm_set(dev, SET_ID(TX_RATE), td->tx_rate_idx);
849+ /* fix payload is OFDM */
850+ mt7996_tm_set(dev, SET_ID(CONT_WAVE_MODE), CONT_WAVE_MODE_OFDM);
851+ mt7996_tm_set(dev, SET_ID(ANT_MASK), td->tx_antenna_mask);
852+
853+ /* trigger firmware to start CONT TX */
854+ mt7996_tm_set(dev, SET_ID(COMMAND), RF_CMD(CONT_WAVE));
855+ } else {
856+ /* trigger firmware to stop CONT TX */
857+ mt7996_tm_set(dev, SET_ID(COMMAND), RF_CMD(STOP_TEST));
858+ }
859+}
860+
861+static void
862+mt7996_tm_update_params(struct mt7996_phy *phy, u32 changed)
863+{
864+ struct mt76_testmode_data *td = &phy->mt76->test;
865+ struct mt7996_dev *dev = phy->dev;
866+ bool en = td->state != MT76_TM_STATE_OFF;
867+
868+ if (changed & BIT(TM_CHANGED_FREQ_OFFSET))
869+ mt7996_tm_set(dev, SET_ID(FREQ_OFFSET), en ? td->freq_offset : 0);
870+ if (changed & BIT(TM_CHANGED_TXPOWER))
871+ mt7996_tm_set(dev, SET_ID(POWER), td->tx_power[0]);
872+ if (changed & BIT(TM_CHANGED_TX_LENGTH)) {
873+ mt7996_tm_set(dev, SET_ID(TX_LEN), td->tx_mpdu_len);
874+ mt7996_tm_set(dev, SET_ID(TX_TIME), 0);
875+ }
876+ if (changed & BIT(TM_CHANGED_TX_TIME)) {
877+ mt7996_tm_set(dev, SET_ID(TX_LEN), 0);
878+ mt7996_tm_set(dev, SET_ID(TX_TIME), td->tx_time);
879+ }
developerde9ecce2023-05-22 11:17:16 +0800880+ if (changed & BIT(TM_CHANGED_CFG)) {
881+ u32 func_idx = td->cfg.enable ? SET_ID(CFG_ON) : SET_ID(CFG_OFF);
882+
883+ mt7996_tm_set(dev, func_idx, td->cfg.type);
884+ }
developer1bc2ce22023-03-25 00:47:41 +0800885+}
886+
887+static int
888+mt7996_tm_set_state(struct mt76_phy *mphy, enum mt76_testmode_state state)
889+{
890+ struct mt76_testmode_data *td = &mphy->test;
891+ struct mt7996_phy *phy = mphy->priv;
892+ enum mt76_testmode_state prev_state = td->state;
893+
894+ mphy->test.state = state;
895+
896+ if (prev_state != MT76_TM_STATE_OFF)
897+ mt7996_tm_set(phy->dev, SET_ID(BAND_IDX), mphy->band_idx);
898+
899+ if (prev_state == MT76_TM_STATE_TX_FRAMES ||
900+ state == MT76_TM_STATE_TX_FRAMES)
901+ mt7996_tm_set_tx_frames(phy, state == MT76_TM_STATE_TX_FRAMES);
902+ else if (prev_state == MT76_TM_STATE_RX_FRAMES ||
903+ state == MT76_TM_STATE_RX_FRAMES)
904+ mt7996_tm_set_rx_frames(phy, state == MT76_TM_STATE_RX_FRAMES);
905+ else if (prev_state == MT76_TM_STATE_TX_CONT ||
906+ state == MT76_TM_STATE_TX_CONT)
907+ mt7996_tm_set_tx_cont(phy, state == MT76_TM_STATE_TX_CONT);
908+ else if (prev_state == MT76_TM_STATE_OFF ||
909+ state == MT76_TM_STATE_OFF)
910+ mt7996_tm_init(phy, !(state == MT76_TM_STATE_OFF));
911+
912+ if ((state == MT76_TM_STATE_IDLE &&
913+ prev_state == MT76_TM_STATE_OFF) ||
914+ (state == MT76_TM_STATE_OFF &&
915+ prev_state == MT76_TM_STATE_IDLE)) {
916+ u32 changed = 0;
917+ int i;
918+
919+ for (i = 0; i < ARRAY_SIZE(tm_change_map); i++) {
920+ u16 cur = tm_change_map[i];
921+
developerde9ecce2023-05-22 11:17:16 +0800922+ if (mt76_testmode_param_present(td, cur))
developer1bc2ce22023-03-25 00:47:41 +0800923+ changed |= BIT(i);
924+ }
925+
926+ mt7996_tm_update_params(phy, changed);
927+ }
928+
929+ return 0;
930+}
931+
932+static int
933+mt7996_tm_set_params(struct mt76_phy *mphy, struct nlattr **tb,
934+ enum mt76_testmode_state new_state)
935+{
936+ struct mt76_testmode_data *td = &mphy->test;
937+ struct mt7996_phy *phy = mphy->priv;
938+ struct mt7996_dev *dev = phy->dev;
939+ u32 chainmask = mphy->chainmask, changed = 0;
940+ u8 band_idx = phy->mt76->band_idx;
941+ int i;
942+
943+ BUILD_BUG_ON(NUM_TM_CHANGED >= 32);
944+
945+ if (new_state == MT76_TM_STATE_OFF ||
946+ td->state == MT76_TM_STATE_OFF)
947+ return 0;
948+
949+ chainmask = chainmask >> dev->chainshift[band_idx];
950+ if (td->tx_antenna_mask > chainmask)
951+ return -EINVAL;
952+
953+ for (i = 0; i < ARRAY_SIZE(tm_change_map); i++) {
954+ if (tb[tm_change_map[i]])
955+ changed |= BIT(i);
956+ }
957+
958+ mt7996_tm_set(dev, SET_ID(BAND_IDX), mphy->band_idx);
959+ mt7996_tm_update_params(phy, changed);
960+
961+ return 0;
962+}
963+
964+static int
965+mt7996_tm_get_rx_stats(struct mt7996_phy *phy)
966+{
967+ struct mt7996_dev *dev = phy->dev;
968+ struct mt7996_tm_rx_req req = {
969+ .band = phy->mt76->band_idx,
970+ .rx_stat_all = {
971+ .tag = cpu_to_le16(UNI_TM_RX_STAT_GET_ALL_V2),
972+ .len = cpu_to_le16(sizeof(req.rx_stat_all)),
973+ .band_idx = phy->mt76->band_idx,
974+ },
975+ };
976+ struct mt76_testmode_data *td = &phy->mt76->test;
977+ struct mt7996_tm_rx_event *rx_stats;
978+ struct mt7996_tm_rx_event_stat_all *rx_stats_all;
979+ struct sk_buff *skb;
980+ enum mt76_rxq_id qid;
981+ int i, ret = 0;
982+ u32 mac_rx_mdrdy_cnt;
983+ u16 mac_rx_len_mismatch, fcs_err_count;
984+
985+ if (td->state != MT76_TM_STATE_RX_FRAMES)
986+ return 0;
987+
988+ ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_WM_UNI_CMD_QUERY(TESTMODE_RX_STAT),
989+ &req, sizeof(req), true, &skb);
990+
991+ if (ret)
992+ return ret;
993+
994+ rx_stats = (struct mt7996_tm_rx_event *)skb->data;
995+ rx_stats_all = &rx_stats->rx_stat_all;
996+
997+ phy->test.last_freq_offset = le32_to_cpu(rx_stats_all->user_info[0].freq_offset);
998+ phy->test.last_snr = le32_to_cpu(rx_stats_all->user_info[0].snr);
999+ for (i = 0; i < ARRAY_SIZE(phy->test.last_rcpi); i++) {
1000+ phy->test.last_rcpi[i] = le16_to_cpu(rx_stats_all->rxv_info[i].rcpi);
1001+ phy->test.last_ib_rssi[i] = rx_stats_all->fagc[i].ib_rssi;
1002+ phy->test.last_wb_rssi[i] = rx_stats_all->fagc[i].wb_rssi;
1003+ }
1004+
1005+ if (phy->mt76->band_idx == 2)
1006+ qid = MT_RXQ_BAND2;
1007+ else if (phy->mt76->band_idx == 1)
1008+ qid = MT_RXQ_BAND1;
1009+ else
1010+ qid = MT_RXQ_MAIN;
1011+
1012+ fcs_err_count = le16_to_cpu(rx_stats_all->band_info.mac_rx_fcs_err_cnt);
1013+ mac_rx_len_mismatch = le16_to_cpu(rx_stats_all->band_info.mac_rx_len_mismatch);
1014+ mac_rx_mdrdy_cnt = le32_to_cpu(rx_stats_all->band_info.mac_rx_mdrdy_cnt);
1015+ td->rx_stats.packets[qid] += mac_rx_mdrdy_cnt;
1016+ td->rx_stats.packets[qid] += fcs_err_count;
1017+ td->rx_stats.fcs_error[qid] += fcs_err_count;
1018+ td->rx_stats.len_mismatch += mac_rx_len_mismatch;
1019+
1020+ dev_kfree_skb(skb);
1021+
1022+ return ret;
1023+}
1024+
1025+static void
developerde9ecce2023-05-22 11:17:16 +08001026+mt7996_tm_reset_trx_stats(struct mt76_phy *mphy)
developer1bc2ce22023-03-25 00:47:41 +08001027+{
1028+ struct mt7996_phy *phy = mphy->priv;
1029+ struct mt7996_dev *dev = phy->dev;
1030+
1031+ memset(&mphy->test.rx_stats, 0, sizeof(mphy->test.rx_stats));
1032+ mt7996_tm_set(dev, SET_ID(TRX_COUNTER_RESET), 0);
1033+}
1034+
1035+static int
1036+mt7996_tm_get_tx_stats(struct mt7996_phy *phy)
1037+{
1038+ struct mt7996_dev *dev = phy->dev;
1039+ struct mt76_testmode_data *td = &phy->mt76->test;
1040+ int ret;
1041+
1042+ if (td->state != MT76_TM_STATE_TX_FRAMES)
1043+ return 0;
1044+
1045+ ret = mt7996_tm_get(dev, GET_ID(TXED_COUNT), 0, &td->tx_done);
1046+ if (ret)
1047+ return ret;
1048+
1049+ td->tx_pending = td->tx_count - td->tx_done;
1050+
1051+ return ret;
1052+}
1053+
1054+static int
1055+mt7996_tm_dump_stats(struct mt76_phy *mphy, struct sk_buff *msg)
1056+{
1057+ struct mt7996_phy *phy = mphy->priv;
1058+ void *rx, *rssi;
1059+ int i;
1060+
1061+ mt7996_tm_set(phy->dev, SET_ID(BAND_IDX), mphy->band_idx);
1062+ mt7996_tm_get_rx_stats(phy);
1063+ mt7996_tm_get_tx_stats(phy);
1064+
1065+ rx = nla_nest_start(msg, MT76_TM_STATS_ATTR_LAST_RX);
1066+ if (!rx)
1067+ return -ENOMEM;
1068+
1069+ if (nla_put_s32(msg, MT76_TM_RX_ATTR_FREQ_OFFSET, phy->test.last_freq_offset))
1070+ return -ENOMEM;
1071+
1072+ rssi = nla_nest_start(msg, MT76_TM_RX_ATTR_RCPI);
1073+ if (!rssi)
1074+ return -ENOMEM;
1075+
1076+ for (i = 0; i < ARRAY_SIZE(phy->test.last_rcpi); i++)
1077+ if (nla_put_u8(msg, i, phy->test.last_rcpi[i]))
1078+ return -ENOMEM;
1079+
1080+ nla_nest_end(msg, rssi);
1081+
1082+ rssi = nla_nest_start(msg, MT76_TM_RX_ATTR_IB_RSSI);
1083+ if (!rssi)
1084+ return -ENOMEM;
1085+
1086+ for (i = 0; i < ARRAY_SIZE(phy->test.last_ib_rssi); i++)
1087+ if (nla_put_s8(msg, i, phy->test.last_ib_rssi[i]))
1088+ return -ENOMEM;
1089+
1090+ nla_nest_end(msg, rssi);
1091+
1092+ rssi = nla_nest_start(msg, MT76_TM_RX_ATTR_WB_RSSI);
1093+ if (!rssi)
1094+ return -ENOMEM;
1095+
1096+ for (i = 0; i < ARRAY_SIZE(phy->test.last_wb_rssi); i++)
1097+ if (nla_put_s8(msg, i, phy->test.last_wb_rssi[i]))
1098+ return -ENOMEM;
1099+
1100+ nla_nest_end(msg, rssi);
1101+
1102+ if (nla_put_u8(msg, MT76_TM_RX_ATTR_SNR, phy->test.last_snr))
1103+ return -ENOMEM;
1104+
1105+ nla_nest_end(msg, rx);
1106+
1107+ return 0;
1108+}
1109+
1110+const struct mt76_testmode_ops mt7996_testmode_ops = {
1111+ .set_state = mt7996_tm_set_state,
1112+ .set_params = mt7996_tm_set_params,
1113+ .dump_stats = mt7996_tm_dump_stats,
developerde9ecce2023-05-22 11:17:16 +08001114+ .reset_rx_stats = mt7996_tm_reset_trx_stats,
developer1bc2ce22023-03-25 00:47:41 +08001115+ .tx_stop = mt7996_tm_tx_stop,
1116+};
1117diff --git a/mt7996/testmode.h b/mt7996/testmode.h
1118new file mode 100644
developerde9ecce2023-05-22 11:17:16 +08001119index 00000000..f00e51f4
developer1bc2ce22023-03-25 00:47:41 +08001120--- /dev/null
1121+++ b/mt7996/testmode.h
developerde9ecce2023-05-22 11:17:16 +08001122@@ -0,0 +1,295 @@
developer1bc2ce22023-03-25 00:47:41 +08001123+/* SPDX-License-Identifier: ISC */
1124+/* Copyright (C) 2020 MediaTek Inc. */
1125+
1126+#ifndef __MT7996_TESTMODE_H
1127+#define __MT7996_TESTMODE_H
1128+
1129+enum {
1130+ TM_CBW_20MHZ,
1131+ TM_CBW_40MHZ,
1132+ TM_CBW_80MHZ,
1133+ TM_CBW_10MHZ,
1134+ TM_CBW_5MHZ,
1135+ TM_CBW_160MHZ,
1136+ TM_CBW_8080MHZ,
1137+ TM_CBW_320MHZ = 12,
1138+};
1139+
1140+/* BW defined in FW hal_cal_flow_rom.h */
1141+enum {
1142+ FW_CDBW_20MHZ,
1143+ FW_CDBW_40MHZ,
1144+ FW_CDBW_80MHZ,
1145+ FW_CDBW_160MHZ,
1146+ FW_CDBW_320MHZ,
1147+ FW_CDBW_5MHZ,
1148+ FW_CDBW_10MHZ,
1149+ FW_CDBW_8080MHZ,
1150+};
1151+
1152+enum bw_mapping_method {
1153+ BW_MAP_NL_TO_FW,
1154+ BW_MAP_NL_TO_TM,
1155+
1156+ NUM_BW_MAP,
1157+};
1158+
1159+struct mt7996_tm_rf_test {
1160+ __le16 tag;
1161+ __le16 len;
1162+
1163+ u8 action;
1164+ u8 icap_len;
1165+ u8 _rsv[2];
1166+ union {
1167+ __le32 op_mode;
1168+ __le32 freq;
1169+
1170+ struct {
1171+ __le32 func_idx;
1172+ union {
1173+ __le32 func_data;
1174+ __le32 cal_dump;
1175+
1176+ u8 _pad[80];
1177+ } param;
1178+ } rf;
1179+ } op;
1180+} __packed;
1181+
1182+struct mt7996_tm_req {
1183+ u8 _rsv[4];
1184+
1185+ struct mt7996_tm_rf_test rf_test;
1186+} __packed;
1187+
1188+struct mt7996_tm_rf_test_result {
1189+ __le32 func_idx;
1190+ __le32 payload_length;
1191+ u8 event[0];
1192+};
1193+
1194+struct mt7996_tm_event {
1195+ u8 _rsv[4];
1196+
1197+ __le16 tag;
1198+ __le16 len;
1199+ struct mt7996_tm_rf_test_result result;
1200+} __packed;
1201+
1202+enum {
1203+ RF_ACTION_SWITCH_TO_RF_TEST,
1204+ RF_ACTION_IN_RF_TEST,
1205+ RF_ACTION_SET = 3,
1206+ RF_ACTION_GET,
1207+};
1208+
1209+enum {
1210+ RF_OPER_NORMAL,
1211+ RF_OPER_RF_TEST,
1212+ RF_OPER_ICAP,
1213+ RF_OPER_ICAP_OVERLAP,
1214+ RF_OPER_WIFI_SPECTRUM,
1215+};
1216+
1217+enum {
1218+ UNI_RF_TEST_CTRL,
1219+};
1220+
1221+#define RF_CMD(cmd) RF_TEST_CMD_##cmd
1222+
1223+enum {
1224+ RF_TEST_CMD_STOP_TEST = 0,
1225+ RF_TEST_CMD_START_TX = 1,
1226+ RF_TEST_CMD_START_RX = 2,
1227+ RF_TEST_CMD_CONT_WAVE = 10,
1228+ RF_TEST_CMD_TX_COMMIT = 18,
1229+ RF_TEST_CMD_RX_COMMIT = 19,
1230+};
1231+
1232+#define SET_ID(id) RF_TEST_ID_SET_##id
1233+#define GET_ID(id) RF_TEST_ID_GET_##id
1234+
1235+enum {
1236+ RF_TEST_ID_SET_COMMAND = 1,
1237+ RF_TEST_ID_SET_POWER = 2,
1238+ RF_TEST_ID_SET_TX_RATE = 3,
1239+ RF_TEST_ID_SET_TX_MODE = 4,
1240+ RF_TEST_ID_SET_TX_LEN = 6,
1241+ RF_TEST_ID_SET_TX_COUNT = 7,
1242+ RF_TEST_ID_SET_IPG = 8,
1243+ RF_TEST_ID_SET_GI = 16,
1244+ RF_TEST_ID_SET_STBC = 17,
1245+ RF_TEST_ID_SET_CHAN_FREQ = 18,
1246+ RF_TEST_ID_GET_TXED_COUNT = 32,
1247+ RF_TEST_ID_SET_CONT_WAVE_MODE = 65,
1248+ RF_TEST_ID_SET_DA = 68,
1249+ RF_TEST_ID_SET_SA = 69,
1250+ RF_TEST_ID_SET_CBW = 71,
1251+ RF_TEST_ID_SET_DBW = 72,
1252+ RF_TEST_ID_SET_PRIMARY_CH = 73,
1253+ RF_TEST_ID_SET_ENCODE_MODE = 74,
1254+ RF_TEST_ID_SET_BAND = 90,
1255+ RF_TEST_ID_SET_TRX_COUNTER_RESET = 91,
1256+ RF_TEST_ID_SET_MAC_HEADER = 101,
1257+ RF_TEST_ID_SET_SEQ_CTRL = 102,
developerde9ecce2023-05-22 11:17:16 +08001258+ RF_TEST_ID_SET_PAYLOAD = 103,
developer1bc2ce22023-03-25 00:47:41 +08001259+ RF_TEST_ID_SET_BAND_IDX = 104,
1260+ RF_TEST_ID_SET_RX_PATH = 106,
1261+ RF_TEST_ID_SET_FREQ_OFFSET = 107,
1262+ RF_TEST_ID_GET_FREQ_OFFSET = 108,
1263+ RF_TEST_ID_SET_TX_PATH = 113,
1264+ RF_TEST_ID_SET_NSS = 114,
1265+ RF_TEST_ID_SET_ANT_MASK = 115,
1266+ RF_TEST_ID_SET_IBF_ENABLE = 126,
1267+ RF_TEST_ID_SET_EBF_ENABLE = 127,
1268+ RF_TEST_ID_GET_TX_POWER = 136,
1269+ RF_TEST_ID_SET_RX_MU_AID = 157,
1270+ RF_TEST_ID_SET_HW_TX_MODE = 167,
1271+ RF_TEST_ID_SET_PUNCTURE = 168,
developerde9ecce2023-05-22 11:17:16 +08001272+ RF_TEST_ID_SET_CFG_ON = 176,
1273+ RF_TEST_ID_SET_CFG_OFF = 177,
developer1bc2ce22023-03-25 00:47:41 +08001274+ RF_TEST_ID_SET_BSSID = 189,
1275+ RF_TEST_ID_SET_TX_TIME = 190,
1276+ RF_TEST_ID_SET_MAX_PE = 191,
1277+ RF_TEST_ID_SET_AID_OFFSET = 204,
1278+};
1279+
1280+struct mt7996_tm_rx_stat_user_ctrl {
1281+ __le16 tag;
1282+ __le16 len;
1283+
1284+ u8 band_idx;
1285+ u8 rsv;
1286+ __le16 user_idx;
1287+} __packed;
1288+
1289+struct mt7996_tm_rx_stat_all {
1290+ __le16 tag;
1291+ __le16 len;
1292+
1293+ u8 band_idx;
1294+ u8 rsv[3];
1295+} __packed;
1296+
1297+struct mt7996_tm_rx_req {
1298+ u8 band;
1299+ u8 _rsv[3];
1300+
1301+ union {
1302+ struct mt7996_tm_rx_stat_user_ctrl user_ctrl;
1303+ struct mt7996_tm_rx_stat_all rx_stat_all;
1304+ };
1305+} __packed;
1306+
1307+enum {
1308+ UNI_TM_RX_STAT_SET_USER_CTRL = 7,
1309+ UNI_TM_RX_STAT_GET_ALL_V2 = 9,
1310+};
1311+
1312+struct rx_band_info {
1313+ /* mac part */
1314+ __le16 mac_rx_fcs_err_cnt;
1315+ __le16 mac_rx_len_mismatch;
1316+ __le16 mac_rx_fcs_ok_cnt;
1317+ u8 rsv1[2];
1318+ __le32 mac_rx_mdrdy_cnt;
1319+
1320+ /* phy part */
1321+ __le16 phy_rx_fcs_err_cnt_cck;
1322+ __le16 phy_rx_fcs_err_cnt_ofdm;
1323+ __le16 phy_rx_pd_cck;
1324+ __le16 phy_rx_pd_ofdm;
1325+ __le16 phy_rx_sig_err_cck;
1326+ __le16 phy_rx_sfd_err_cck;
1327+ __le16 phy_rx_sig_err_ofdm;
1328+ __le16 phy_rx_tag_err_ofdm;
1329+ __le16 phy_rx_mdrdy_cnt_cck;
1330+ __le16 phy_rx_mdrdy_cnt_ofdm;
1331+} __packed;
1332+
1333+struct rx_band_info_ext {
1334+ /* mac part */
1335+ __le32 mac_rx_mpdu_cnt;
1336+
1337+ /* phy part */
1338+ u8 rsv[4];
1339+} __packed;
1340+
1341+struct rx_common_info {
1342+ __le16 rx_fifo_full;
1343+ u8 rsv[2];
1344+ __le32 aci_hit_low;
1345+ __le32 aci_hit_high;
1346+} __packed;
1347+
1348+struct rx_common_info_ext {
1349+ __le32 driver_rx_count;
1350+ __le32 sinr;
1351+ __le32 mu_pkt_count;
1352+
1353+ /* mac part */
1354+ u8 _rsv[4];
1355+
1356+ /* phy part */
1357+ u8 sig_mcs;
1358+ u8 rsv[3];
1359+} __packed;
1360+
1361+struct rx_rxv_info {
1362+ __le16 rcpi;
1363+ s16 rssi;
1364+ s16 snr;
1365+ s16 adc_rssi;
1366+} __packed;
1367+
1368+struct rx_rssi_info {
1369+ s8 ib_rssi;
1370+ s8 wb_rssi;
1371+ u8 rsv[2];
1372+} __packed;
1373+
1374+struct rx_user_info {
1375+ s32 freq_offset;
1376+ s32 snr;
1377+ __le32 fcs_err_count;
1378+} __packed;
1379+
1380+struct rx_user_info_ext {
1381+ s8 ne_var_db_all_user;
1382+ u8 rsv[3];
1383+} __packed;
1384+
1385+#define MAX_ANTENNA_NUM 8
1386+#define MAX_USER_NUM 16
1387+
1388+struct mt7996_tm_rx_event_stat_all {
1389+ __le16 tag;
1390+ __le16 len;
1391+
1392+ struct rx_band_info band_info;
1393+ struct rx_band_info_ext band_info_ext;
1394+ struct rx_common_info common_info;
1395+ struct rx_common_info_ext common_info_ext;
1396+
1397+ /* RXV info */
1398+ struct rx_rxv_info rxv_info[MAX_ANTENNA_NUM];
1399+
1400+ /* RSSI info */
1401+ struct rx_rssi_info fagc[MAX_ANTENNA_NUM];
1402+ struct rx_rssi_info inst[MAX_ANTENNA_NUM];
1403+
1404+ /* User info */
1405+ struct rx_user_info user_info[MAX_USER_NUM];
1406+ struct rx_user_info_ext user_info_ext[MAX_USER_NUM];
1407+} __packed;
1408+
1409+struct mt7996_tm_rx_event {
1410+ u8 _rsv[4];
1411+
1412+ union {
1413+ struct mt7996_tm_rx_event_stat_all rx_stat_all;
1414+ };
1415+} __packed;
1416+
1417+#endif
1418diff --git a/testmode.c b/testmode.c
developerde9ecce2023-05-22 11:17:16 +08001419index 0d2bae9f..fc68c2af 100644
developer1bc2ce22023-03-25 00:47:41 +08001420--- a/testmode.c
1421+++ b/testmode.c
1422@@ -2,6 +2,7 @@
1423 /* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
1424
1425 #include <linux/random.h>
1426+#include "mt76_connac.h"
1427 #include "mt76.h"
1428
1429 const struct nla_policy mt76_tm_policy[NUM_MT76_TM_ATTRS] = {
1430@@ -81,6 +82,11 @@ mt76_testmode_max_mpdu_len(struct mt76_phy *phy, u8 tx_rate_mode)
1431 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991)
1432 return IEEE80211_MAX_MPDU_LEN_VHT_7991;
1433 return IEEE80211_MAX_MPDU_LEN_VHT_11454;
1434+ case MT76_TM_TX_MODE_EHT_SU:
1435+ case MT76_TM_TX_MODE_EHT_TRIG:
1436+ case MT76_TM_TX_MODE_EHT_MU:
1437+ /* TODO: check the limit */
1438+ return UINT_MAX;
1439 case MT76_TM_TX_MODE_CCK:
1440 case MT76_TM_TX_MODE_OFDM:
1441 default:
1442@@ -182,6 +188,9 @@ mt76_testmode_tx_init(struct mt76_phy *phy)
1443 u8 max_nss = hweight8(phy->antenna_mask);
1444 int ret;
1445
1446+ if (is_mt7996(phy->dev))
1447+ return 0;
1448+
1449 ret = mt76_testmode_alloc_skb(phy, td->tx_mpdu_len);
1450 if (ret)
1451 return ret;
1452@@ -274,7 +283,9 @@ mt76_testmode_tx_start(struct mt76_phy *phy)
1453 td->tx_queued = 0;
1454 td->tx_done = 0;
1455 td->tx_pending = td->tx_count;
1456- mt76_worker_schedule(&dev->tx_worker);
1457+
1458+ if (!is_mt7996(dev))
1459+ mt76_worker_schedule(&dev->tx_worker);
1460 }
1461
1462 static void
1463@@ -283,6 +294,11 @@ mt76_testmode_tx_stop(struct mt76_phy *phy)
1464 struct mt76_testmode_data *td = &phy->test;
1465 struct mt76_dev *dev = phy->dev;
1466
1467+ if (is_mt7996(dev) && dev->test_ops->tx_stop) {
1468+ dev->test_ops->tx_stop(phy);
1469+ return;
1470+ }
1471+
1472 mt76_worker_disable(&dev->tx_worker);
1473
1474 td->tx_pending = 0;
developerde9ecce2023-05-22 11:17:16 +08001475@@ -295,22 +311,11 @@ mt76_testmode_tx_stop(struct mt76_phy *phy)
1476 mt76_testmode_free_skb(phy);
1477 }
1478
1479-static inline void
1480-mt76_testmode_param_set(struct mt76_testmode_data *td, u16 idx)
1481-{
1482- td->param_set[idx / 32] |= BIT(idx % 32);
1483-}
1484-
1485-static inline bool
1486-mt76_testmode_param_present(struct mt76_testmode_data *td, u16 idx)
1487-{
1488- return td->param_set[idx / 32] & BIT(idx % 32);
1489-}
1490-
1491 static void
developer1bc2ce22023-03-25 00:47:41 +08001492 mt76_testmode_init_defaults(struct mt76_phy *phy)
1493 {
1494 struct mt76_testmode_data *td = &phy->test;
1495+ u8 addr[ETH_ALEN] = {phy->band_idx, 0x11, 0x22, 0xaa, 0xbb, 0xcc};
1496
1497 if (td->tx_mpdu_len > 0)
1498 return;
developerde9ecce2023-05-22 11:17:16 +08001499@@ -318,11 +323,18 @@ mt76_testmode_init_defaults(struct mt76_phy *phy)
developer1bc2ce22023-03-25 00:47:41 +08001500 td->tx_mpdu_len = 1024;
1501 td->tx_count = 1;
1502 td->tx_rate_mode = MT76_TM_TX_MODE_OFDM;
1503+ td->tx_rate_idx = 7;
1504 td->tx_rate_nss = 1;
1505+ /* 0xffff for OFDMA no puncture */
1506+ td->tx_preamble_puncture = ~(td->tx_preamble_puncture & 0);
1507+ td->tx_ipg = 50;
developerde9ecce2023-05-22 11:17:16 +08001508+
1509+ /* rx stat user config */
1510+ td->aid = 1;
developer1bc2ce22023-03-25 00:47:41 +08001511
1512- memcpy(td->addr[0], phy->macaddr, ETH_ALEN);
1513- memcpy(td->addr[1], phy->macaddr, ETH_ALEN);
1514- memcpy(td->addr[2], phy->macaddr, ETH_ALEN);
developer1bc2ce22023-03-25 00:47:41 +08001515+ memcpy(td->addr[0], addr, ETH_ALEN);
1516+ memcpy(td->addr[1], addr, ETH_ALEN);
1517+ memcpy(td->addr[2], addr, ETH_ALEN);
1518 }
1519
1520 static int
developerde9ecce2023-05-22 11:17:16 +08001521@@ -352,7 +364,7 @@ __mt76_testmode_set_state(struct mt76_phy *phy, enum mt76_testmode_state state)
developer1bc2ce22023-03-25 00:47:41 +08001522 if (state == MT76_TM_STATE_TX_FRAMES)
1523 mt76_testmode_tx_start(phy);
1524 else if (state == MT76_TM_STATE_RX_FRAMES) {
1525- memset(&phy->test.rx_stats, 0, sizeof(phy->test.rx_stats));
1526+ dev->test_ops->reset_rx_stats(phy);
1527 }
1528
1529 phy->test.state = state;
developerde9ecce2023-05-22 11:17:16 +08001530@@ -453,7 +465,8 @@ int mt76_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
developer1bc2ce22023-03-25 00:47:41 +08001531 mt76_tm_get_u8(tb[MT76_TM_ATTR_TX_DUTY_CYCLE],
1532 &td->tx_duty_cycle, 0, 99) ||
1533 mt76_tm_get_u8(tb[MT76_TM_ATTR_TX_POWER_CONTROL],
1534- &td->tx_power_control, 0, 1))
1535+ &td->tx_power_control, 0, 1) ||
1536+ mt76_tm_get_u8(tb[MT76_TM_ATTR_AID], &td->aid, 0, 16))
1537 goto out;
1538
1539 if (tb[MT76_TM_ATTR_TX_LENGTH]) {
developerde9ecce2023-05-22 11:17:16 +08001540@@ -493,7 +506,9 @@ int mt76_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
developer1bc2ce22023-03-25 00:47:41 +08001541 idx >= ARRAY_SIZE(td->tx_power))
1542 goto out;
1543
1544- td->tx_power[idx++] = nla_get_u8(cur);
1545+ err = mt76_tm_get_u8(cur, &td->tx_power[idx++], 0, 63);
1546+ if (err)
1547+ return err;
1548 }
1549 }
1550
developerde9ecce2023-05-22 11:17:16 +08001551@@ -511,6 +526,22 @@ int mt76_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1552 }
1553 }
1554
1555+ if (tb[MT76_TM_ATTR_CFG]) {
1556+ struct nlattr *cur;
1557+ int rem, idx = 0;
1558+
1559+ nla_for_each_nested(cur, tb[MT76_TM_ATTR_CFG], rem) {
1560+ if (nla_len(cur) != 1 || idx >= 2)
1561+ goto out;
1562+
1563+ if (idx == 0)
1564+ td->cfg.type = nla_get_u8(cur);
1565+ else
1566+ td->cfg.enable = nla_get_u8(cur);
1567+ idx++;
1568+ }
1569+ }
1570+
1571 if (dev->test_ops->set_params) {
1572 err = dev->test_ops->set_params(phy, tb, state);
1573 if (err)
1574@@ -560,6 +591,9 @@ mt76_testmode_dump_stats(struct mt76_phy *phy, struct sk_buff *msg)
developer1bc2ce22023-03-25 00:47:41 +08001575 nla_put_u64_64bit(msg, MT76_TM_STATS_ATTR_RX_PACKETS, rx_packets,
1576 MT76_TM_STATS_ATTR_PAD) ||
1577 nla_put_u64_64bit(msg, MT76_TM_STATS_ATTR_RX_FCS_ERROR, rx_fcs_error,
1578+ MT76_TM_STATS_ATTR_PAD) ||
1579+ nla_put_u64_64bit(msg, MT76_TM_STATS_ATTR_RX_LEN_MISMATCH,
1580+ td->rx_stats.len_mismatch,
1581 MT76_TM_STATS_ATTR_PAD))
1582 return -EMSGSIZE;
1583
developerde9ecce2023-05-22 11:17:16 +08001584@@ -624,6 +658,7 @@ int mt76_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *msg,
developer1bc2ce22023-03-25 00:47:41 +08001585 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_SGI, td->tx_rate_sgi) ||
1586 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_LDPC, td->tx_rate_ldpc) ||
1587 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_STBC, td->tx_rate_stbc) ||
1588+ nla_put_u8(msg, MT76_TM_ATTR_AID, td->aid) ||
1589 (mt76_testmode_param_present(td, MT76_TM_ATTR_TX_LTF) &&
1590 nla_put_u8(msg, MT76_TM_ATTR_TX_LTF, td->tx_ltf)) ||
1591 (mt76_testmode_param_present(td, MT76_TM_ATTR_TX_ANTENNA) &&
1592diff --git a/testmode.h b/testmode.h
developerde9ecce2023-05-22 11:17:16 +08001593index a40cd74b..8d0b9702 100644
developer1bc2ce22023-03-25 00:47:41 +08001594--- a/testmode.h
1595+++ b/testmode.h
1596@@ -39,6 +39,11 @@
1597 *
1598 * @MT76_TM_ATTR_STATS: statistics (nested, see &enum mt76_testmode_stats_attr)
1599 *
1600+ * @MT76_TM_ATTR_PRECAL: Pre-cal data (u8)
1601+ * @MT76_TM_ATTR_PRECAL_INFO: group size, dpd size, dpd_info, transmit size,
1602+ * eeprom cal indicator (u32),
1603+ * dpd_info = [dpd_per_chan_size, chan_num_2g,
1604+ * chan_num_5g, chan_num_6g]
1605 * @MT76_TM_ATTR_TX_SPE_IDX: tx spatial extension index (u8)
1606 *
1607 * @MT76_TM_ATTR_TX_DUTY_CYCLE: packet tx duty cycle (u8)
1608@@ -48,6 +53,29 @@
1609 * @MT76_TM_ATTR_DRV_DATA: driver specific netlink attrs (nested)
1610 *
1611 * @MT76_TM_ATTR_MAC_ADDRS: array of nested MAC addresses (nested)
1612+ *
1613+ * @MT76_TM_ATTR_EEPROM_ACTION: eeprom setting actions
1614+ * (u8, see &enum mt76_testmode_eeprom_action)
1615+ * @MT76_TM_ATTR_EEPROM_OFFSET: offset of eeprom data block for writing (u32)
1616+ * @MT76_TM_ATTR_EEPROM_VAL: values for writing into a 16-byte data block
1617+ * (nested, u8 attrs)
1618+ *
1619+ * @MT76_TM_ATTR_CFG: config testmode rf feature (nested, see &mt76_testmode_cfg)
1620+ * @MT76_TM_ATTR_TXBF_ACT: txbf setting actions (u8)
1621+ * @MT76_TM_ATTR_TXBF_PARAM: txbf parameters (nested)
1622+ *
1623+ * @MT76_TM_ATTR_OFF_CH_SCAN_CH: config the channel of background chain (ZWDFS) (u8)
1624+ * @MT76_TM_ATTR_OFF_CH_SCAN_CENTER_CH: config the center channel of background chain (ZWDFS) (u8)
1625+ * @MT76_TM_ATTR_OFF_CH_SCAN_BW: config the bandwidth of background chain (ZWDFS) (u8)
1626+ * @MT76_TM_ATTR_OFF_CH_SCAN_PATH: config the tx path of background chain (ZWDFS) (u8)
1627+ *
1628+ * @MT76_TM_ATTR_IPI_THRESHOLD: config the IPI index you want to read (u8)
1629+ * @MT76_TM_ATTR_IPI_PERIOD: config the time period for reading
1630+ * the histogram of specific IPI index (u8)
1631+ * @MT76_TM_ATTR_IPI_ANTENNA_INDEX: config the antenna index for reading
1632+ * the histogram of specific IPI index (u8)
1633+ * @MT76_TM_ATTR_IPI_RESET: Reset the IPI counter
1634+ *
1635 */
1636 enum mt76_testmode_attr {
1637 MT76_TM_ATTR_UNSPEC,
1638@@ -76,6 +104,8 @@ enum mt76_testmode_attr {
1639 MT76_TM_ATTR_FREQ_OFFSET,
1640
1641 MT76_TM_ATTR_STATS,
1642+ MT76_TM_ATTR_PRECAL,
1643+ MT76_TM_ATTR_PRECAL_INFO,
1644
1645 MT76_TM_ATTR_TX_SPE_IDX,
1646
1647@@ -86,6 +116,27 @@ enum mt76_testmode_attr {
1648 MT76_TM_ATTR_DRV_DATA,
1649
1650 MT76_TM_ATTR_MAC_ADDRS,
1651+ MT76_TM_ATTR_AID,
1652+ MT76_TM_ATTR_RU_ALLOC,
1653+ MT76_TM_ATTR_RU_IDX,
1654+
1655+ MT76_TM_ATTR_EEPROM_ACTION,
1656+ MT76_TM_ATTR_EEPROM_OFFSET,
1657+ MT76_TM_ATTR_EEPROM_VAL,
1658+
1659+ MT76_TM_ATTR_CFG,
1660+ MT76_TM_ATTR_TXBF_ACT,
1661+ MT76_TM_ATTR_TXBF_PARAM,
1662+
1663+ MT76_TM_ATTR_OFF_CH_SCAN_CH,
1664+ MT76_TM_ATTR_OFF_CH_SCAN_CENTER_CH,
1665+ MT76_TM_ATTR_OFF_CH_SCAN_BW,
1666+ MT76_TM_ATTR_OFF_CH_SCAN_PATH,
1667+
1668+ MT76_TM_ATTR_IPI_THRESHOLD,
1669+ MT76_TM_ATTR_IPI_PERIOD,
1670+ MT76_TM_ATTR_IPI_ANTENNA_INDEX,
1671+ MT76_TM_ATTR_IPI_RESET,
1672
1673 /* keep last */
1674 NUM_MT76_TM_ATTRS,
1675@@ -103,6 +154,8 @@ enum mt76_testmode_attr {
1676 * @MT76_TM_STATS_ATTR_RX_FCS_ERROR: number of rx packets with FCS error (u64)
1677 * @MT76_TM_STATS_ATTR_LAST_RX: information about the last received packet
1678 * see &enum mt76_testmode_rx_attr
1679+ * @MT76_TM_STATS_ATTR_RX_LEN_MISMATCH: number of rx packets with length
1680+ * mismatch error (u64)
1681 */
1682 enum mt76_testmode_stats_attr {
1683 MT76_TM_STATS_ATTR_UNSPEC,
1684@@ -115,6 +168,7 @@ enum mt76_testmode_stats_attr {
1685 MT76_TM_STATS_ATTR_RX_PACKETS,
1686 MT76_TM_STATS_ATTR_RX_FCS_ERROR,
1687 MT76_TM_STATS_ATTR_LAST_RX,
1688+ MT76_TM_STATS_ATTR_RX_LEN_MISMATCH,
1689
1690 /* keep last */
1691 NUM_MT76_TM_STATS_ATTRS,
1692@@ -179,6 +233,9 @@ enum mt76_testmode_state {
1693 * @MT76_TM_TX_MODE_HE_EXT_SU: 802.11ax extended-range SU
1694 * @MT76_TM_TX_MODE_HE_TB: 802.11ax trigger-based
1695 * @MT76_TM_TX_MODE_HE_MU: 802.11ax multi-user MIMO
1696+ * @MT76_TM_TX_MODE_EHT_SU: 802.11be single-user MIMO
1697+ * @MT76_TM_TX_MODE_EHT_TRIG: 802.11be trigger-based
1698+ * @MT76_TM_TX_MODE_EHT_MU: 802.11be multi-user MIMO
1699 */
1700 enum mt76_testmode_tx_mode {
1701 MT76_TM_TX_MODE_CCK,
1702@@ -189,6 +246,9 @@ enum mt76_testmode_tx_mode {
1703 MT76_TM_TX_MODE_HE_EXT_SU,
1704 MT76_TM_TX_MODE_HE_TB,
1705 MT76_TM_TX_MODE_HE_MU,
1706+ MT76_TM_TX_MODE_EHT_SU,
1707+ MT76_TM_TX_MODE_EHT_TRIG,
1708+ MT76_TM_TX_MODE_EHT_MU,
1709
1710 /* keep last */
1711 NUM_MT76_TM_TX_MODES,
1712diff --git a/tools/fields.c b/tools/fields.c
developerde9ecce2023-05-22 11:17:16 +08001713index e3f69089..e5cf7c53 100644
developer1bc2ce22023-03-25 00:47:41 +08001714--- a/tools/fields.c
1715+++ b/tools/fields.c
1716@@ -10,6 +10,7 @@ static const char * const testmode_state[] = {
1717 [MT76_TM_STATE_IDLE] = "idle",
1718 [MT76_TM_STATE_TX_FRAMES] = "tx_frames",
1719 [MT76_TM_STATE_RX_FRAMES] = "rx_frames",
1720+ [MT76_TM_STATE_TX_CONT] = "tx_cont",
1721 };
1722
1723 static const char * const testmode_tx_mode[] = {
1724@@ -21,6 +22,9 @@ static const char * const testmode_tx_mode[] = {
1725 [MT76_TM_TX_MODE_HE_EXT_SU] = "he_ext_su",
1726 [MT76_TM_TX_MODE_HE_TB] = "he_tb",
1727 [MT76_TM_TX_MODE_HE_MU] = "he_mu",
1728+ [MT76_TM_TX_MODE_EHT_SU] = "eht_su",
1729+ [MT76_TM_TX_MODE_EHT_TRIG] = "eht_tb",
1730+ [MT76_TM_TX_MODE_EHT_MU] = "eht_mu",
1731 };
1732
1733 static void print_enum(const struct tm_field *field, struct nlattr *attr)
1734@@ -201,6 +205,62 @@ static void print_extra_stats(const struct tm_field *field, struct nlattr **tb)
1735 printf("%srx_per=%.02f%%\n", prefix, 100 * failed / total);
1736 }
1737
1738+static bool parse_mac(const struct tm_field *field, int idx,
1739+ struct nl_msg *msg, const char *val)
1740+{
1741+#define ETH_ALEN 6
1742+ bool ret = true;
1743+ char *str, *cur, *ap;
1744+ void *a;
1745+
1746+ str = strdup(val);
1747+ ap = str;
1748+
1749+ a = nla_nest_start(msg, idx);
1750+
1751+ idx = 0;
1752+ while ((cur = strsep(&ap, ",")) != NULL) {
1753+ unsigned char addr[ETH_ALEN];
1754+ char *val, *tmp = cur;
1755+ int i = 0;
1756+
1757+ while ((val = strsep(&tmp, ":")) != NULL) {
1758+ if (i >= ETH_ALEN)
1759+ break;
1760+
1761+ addr[i++] = strtoul(val, NULL, 16);
1762+ }
1763+
1764+ nla_put(msg, idx, ETH_ALEN, addr);
1765+
1766+ idx++;
1767+ }
1768+
1769+ nla_nest_end(msg, a);
1770+
1771+ free(str);
1772+
1773+ return ret;
1774+}
1775+
1776+static void print_mac(const struct tm_field *field, struct nlattr *attr)
1777+{
1778+#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
1779+#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
1780+ unsigned char addr[3][6];
1781+ struct nlattr *cur;
1782+ int idx = 0;
1783+ int rem;
1784+
1785+ nla_for_each_nested(cur, attr, rem) {
1786+ if (nla_len(cur) != 6)
1787+ continue;
1788+ memcpy(addr[idx++], nla_data(cur), 6);
1789+ }
1790+
1791+ printf("" MACSTR "," MACSTR "," MACSTR "",
1792+ MAC2STR(addr[0]), MAC2STR(addr[1]), MAC2STR(addr[2]));
1793+}
1794
1795 #define FIELD_GENERIC(_field, _name, ...) \
1796 [FIELD_NAME(_field)] = { \
1797@@ -250,6 +310,13 @@ static void print_extra_stats(const struct tm_field *field, struct nlattr **tb)
1798 ##__VA_ARGS__ \
1799 )
1800
1801+#define FIELD_MAC(_field, _name) \
1802+ [FIELD_NAME(_field)] = { \
1803+ .name = _name, \
1804+ .parse = parse_mac, \
1805+ .print = print_mac \
1806+ }
1807+
1808 #define FIELD_NAME(_field) MT76_TM_RX_ATTR_##_field
1809 static const struct tm_field rx_fields[NUM_MT76_TM_RX_ATTRS] = {
1810 FIELD_RO(s32, FREQ_OFFSET, "freq_offset"),
1811@@ -274,6 +341,7 @@ static const struct tm_field stats_fields[NUM_MT76_TM_STATS_ATTRS] = {
1812 FIELD_RO(u32, TX_DONE, "tx_done"),
1813 FIELD_RO(u64, RX_PACKETS, "rx_packets"),
1814 FIELD_RO(u64, RX_FCS_ERROR, "rx_fcs_error"),
1815+ FIELD_RO(u64, RX_LEN_MISMATCH, "rx_len_mismatch"),
1816 FIELD_NESTED_RO(LAST_RX, rx, "last_"),
1817 };
1818 static struct nla_policy stats_policy[NUM_MT76_TM_STATS_ATTRS] = {
1819@@ -282,6 +350,7 @@ static struct nla_policy stats_policy[NUM_MT76_TM_STATS_ATTRS] = {
1820 [MT76_TM_STATS_ATTR_TX_DONE] = { .type = NLA_U32 },
1821 [MT76_TM_STATS_ATTR_RX_PACKETS] = { .type = NLA_U64 },
1822 [MT76_TM_STATS_ATTR_RX_FCS_ERROR] = { .type = NLA_U64 },
1823+ [MT76_TM_STATS_ATTR_RX_LEN_MISMATCH] = { .type = NLA_U64 },
1824 };
1825 #undef FIELD_NAME
1826
1827@@ -300,12 +369,20 @@ static const struct tm_field testdata_fields[NUM_MT76_TM_ATTRS] = {
1828 FIELD(u8, TX_RATE_LDPC, "tx_rate_ldpc"),
1829 FIELD(u8, TX_RATE_STBC, "tx_rate_stbc"),
1830 FIELD(u8, TX_LTF, "tx_ltf"),
1831+ FIELD(u8, TX_DUTY_CYCLE, "tx_duty_cycle"),
1832+ FIELD(u32, TX_IPG, "tx_ipg"),
1833+ FIELD(u32, TX_TIME, "tx_time"),
1834 FIELD(u8, TX_POWER_CONTROL, "tx_power_control"),
1835 FIELD_ARRAY(u8, TX_POWER, "tx_power"),
1836 FIELD(u8, TX_ANTENNA, "tx_antenna"),
1837 FIELD(u32, FREQ_OFFSET, "freq_offset"),
1838+ FIELD(u8, AID, "aid"),
1839+ FIELD(u8, RU_ALLOC, "ru_alloc"),
1840+ FIELD(u8, RU_IDX, "ru_idx"),
1841+ FIELD_MAC(MAC_ADDRS, "mac_addrs"),
1842 FIELD_NESTED_RO(STATS, stats, "",
1843 .print_extra = print_extra_stats),
1844+
1845 };
1846 #undef FIELD_NAME
1847
1848@@ -322,10 +399,25 @@ static struct nla_policy testdata_policy[NUM_MT76_TM_ATTRS] = {
1849 [MT76_TM_ATTR_TX_RATE_LDPC] = { .type = NLA_U8 },
1850 [MT76_TM_ATTR_TX_RATE_STBC] = { .type = NLA_U8 },
1851 [MT76_TM_ATTR_TX_LTF] = { .type = NLA_U8 },
1852+ [MT76_TM_ATTR_TX_DUTY_CYCLE] = { .type = NLA_U8 },
1853+ [MT76_TM_ATTR_TX_IPG] = { .type = NLA_U32 },
1854+ [MT76_TM_ATTR_TX_TIME] = { .type = NLA_U32 },
1855 [MT76_TM_ATTR_TX_POWER_CONTROL] = { .type = NLA_U8 },
1856 [MT76_TM_ATTR_TX_ANTENNA] = { .type = NLA_U8 },
1857+ [MT76_TM_ATTR_TX_SPE_IDX] = { .type = NLA_U8 },
1858 [MT76_TM_ATTR_FREQ_OFFSET] = { .type = NLA_U32 },
1859+ [MT76_TM_ATTR_AID] = { .type = NLA_U8 },
1860+ [MT76_TM_ATTR_RU_ALLOC] = { .type = NLA_U8 },
1861+ [MT76_TM_ATTR_RU_IDX] = { .type = NLA_U8 },
1862 [MT76_TM_ATTR_STATS] = { .type = NLA_NESTED },
1863+ [MT76_TM_ATTR_TXBF_ACT] = { .type = NLA_U8 },
1864+ [MT76_TM_ATTR_OFF_CH_SCAN_CH] = { .type = NLA_U8 },
1865+ [MT76_TM_ATTR_OFF_CH_SCAN_CENTER_CH] = { .type = NLA_U8 },
1866+ [MT76_TM_ATTR_OFF_CH_SCAN_BW] = { .type = NLA_U8 },
1867+ [MT76_TM_ATTR_IPI_THRESHOLD] = { .type = NLA_U8 },
1868+ [MT76_TM_ATTR_IPI_PERIOD] = { .type = NLA_U32 },
1869+ [MT76_TM_ATTR_IPI_ANTENNA_INDEX] = { .type = NLA_U8 },
1870+ [MT76_TM_ATTR_IPI_RESET] = { .type = NLA_U8 },
1871 };
1872
1873 const struct tm_field msg_field = {
1874--
developerde9ecce2023-05-22 11:17:16 +080018752.39.2
developer1bc2ce22023-03-25 00:47:41 +08001876