blob: e7f7a348f12237ab8ec144911342af3bd7bf4993 [file] [log] [blame]
developer95671ae2023-09-25 14:16:05 +08001From 8cbf5b49e3ecfa038198362d6141c195d12753bd Mon Sep 17 00:00:00 2001
developer281084d2023-06-19 12:03:50 +08002From: StanleyYP Wang <StanleyYP.Wang@mediatek.com>
3Date: Fri, 31 Mar 2023 11:27:24 +0800
developer95671ae2023-09-25 14:16:05 +08004Subject: [PATCH 1010/1024] wifi: mt76: testmode: add testmode pre-calibration
developer281084d2023-06-19 12:03:50 +08005 support
6
7Signed-off-by: StanleyYP Wang <StanleyYP.Wang@mediatek.com>
8Change-Id: If8a6cc02fa20e35f079c826e0571e8c04c3f9c7e
9---
10 mac80211.c | 21 ---
11 mt76.h | 22 +++
12 mt76_connac_mcu.h | 2 +
13 mt7996/eeprom.c | 66 +++++++
14 mt7996/eeprom.h | 47 +++++
15 mt7996/mcu.c | 5 +
16 mt7996/mt7996.h | 7 +
17 mt7996/testmode.c | 437 ++++++++++++++++++++++++++++++++++++++++++++++
18 mt7996/testmode.h | 20 ++-
19 testmode.c | 12 ++
20 testmode.h | 8 +
21 tools/fields.c | 8 +
22 12 files changed, 632 insertions(+), 23 deletions(-)
23
24diff --git a/mac80211.c b/mac80211.c
developer95671ae2023-09-25 14:16:05 +080025index 5740ba061..ef4b83244 100644
developer281084d2023-06-19 12:03:50 +080026--- a/mac80211.c
27+++ b/mac80211.c
28@@ -7,27 +7,6 @@
29 #include <net/page_pool.h>
30 #include "mt76.h"
31
32-#define CHAN2G(_idx, _freq) { \
33- .band = NL80211_BAND_2GHZ, \
34- .center_freq = (_freq), \
35- .hw_value = (_idx), \
36- .max_power = 30, \
37-}
38-
39-#define CHAN5G(_idx, _freq) { \
40- .band = NL80211_BAND_5GHZ, \
41- .center_freq = (_freq), \
42- .hw_value = (_idx), \
43- .max_power = 30, \
44-}
45-
46-#define CHAN6G(_idx, _freq) { \
47- .band = NL80211_BAND_6GHZ, \
48- .center_freq = (_freq), \
49- .hw_value = (_idx), \
50- .max_power = 30, \
51-}
52-
53 static const struct ieee80211_channel mt76_channels_2ghz[] = {
54 CHAN2G(1, 2412),
55 CHAN2G(2, 2417),
56diff --git a/mt76.h b/mt76.h
developer95671ae2023-09-25 14:16:05 +080057index ad1123c4a..f58a955d6 100644
developer281084d2023-06-19 12:03:50 +080058--- a/mt76.h
59+++ b/mt76.h
60@@ -18,6 +18,27 @@
61 #include "util.h"
62 #include "testmode.h"
63
64+#define CHAN2G(_idx, _freq) { \
65+ .band = NL80211_BAND_2GHZ, \
66+ .center_freq = (_freq), \
67+ .hw_value = (_idx), \
68+ .max_power = 30, \
69+}
70+
71+#define CHAN5G(_idx, _freq) { \
72+ .band = NL80211_BAND_5GHZ, \
73+ .center_freq = (_freq), \
74+ .hw_value = (_idx), \
75+ .max_power = 30, \
76+}
77+
78+#define CHAN6G(_idx, _freq) { \
79+ .band = NL80211_BAND_6GHZ, \
80+ .center_freq = (_freq), \
81+ .hw_value = (_idx), \
82+ .max_power = 30, \
83+}
84+
85 #define MT_MCU_RING_SIZE 32
86 #define MT_RX_BUF_SIZE 2048
87 #define MT_SKB_HEAD_LEN 256
developer95671ae2023-09-25 14:16:05 +080088@@ -660,6 +681,7 @@ struct mt76_testmode_ops {
developer281084d2023-06-19 12:03:50 +080089 int (*dump_stats)(struct mt76_phy *phy, struct sk_buff *msg);
90 void (*reset_rx_stats)(struct mt76_phy *phy);
91 void (*tx_stop)(struct mt76_phy *phy);
92+ int (*dump_precal)(struct mt76_phy *mphy, struct sk_buff *msg, int flag, int type);
93 };
94
95 #define MT_TM_FW_RX_COUNT BIT(0)
96diff --git a/mt76_connac_mcu.h b/mt76_connac_mcu.h
developer95671ae2023-09-25 14:16:05 +080097index 6d9b0df2d..b2c22568d 100644
developer281084d2023-06-19 12:03:50 +080098--- a/mt76_connac_mcu.h
99+++ b/mt76_connac_mcu.h
developer95671ae2023-09-25 14:16:05 +0800100@@ -1021,8 +1021,10 @@ enum {
developer281084d2023-06-19 12:03:50 +0800101 MCU_UNI_EVENT_RDD_REPORT = 0x11,
developer95671ae2023-09-25 14:16:05 +0800102 MCU_UNI_EVENT_ROC = 0x27,
103 MCU_UNI_EVENT_TX_DONE = 0x2d,
developer281084d2023-06-19 12:03:50 +0800104+ MCU_UNI_EVENT_BF = 0x33,
developer95671ae2023-09-25 14:16:05 +0800105 MCU_UNI_EVENT_THERMAL = 0x35,
106 MCU_UNI_EVENT_NIC_CAPAB = 0x43,
developer281084d2023-06-19 12:03:50 +0800107+ MCU_UNI_EVENT_TESTMODE_CTRL = 0x46,
developer95671ae2023-09-25 14:16:05 +0800108 MCU_UNI_EVENT_PER_STA_INFO = 0x6d,
109 MCU_UNI_EVENT_ALL_STA_INFO = 0x6e,
developer281084d2023-06-19 12:03:50 +0800110 };
developer281084d2023-06-19 12:03:50 +0800111diff --git a/mt7996/eeprom.c b/mt7996/eeprom.c
developer95671ae2023-09-25 14:16:05 +0800112index bdb81018b..374f0e558 100644
developer281084d2023-06-19 12:03:50 +0800113--- a/mt7996/eeprom.c
114+++ b/mt7996/eeprom.c
115@@ -12,6 +12,42 @@ static bool testmode_enable;
116 module_param(testmode_enable, bool, 0644);
117 MODULE_PARM_DESC(testmode_enable, "Enable testmode");
118
119+const struct ieee80211_channel dpd_2g_ch_list_bw20[] = {
120+ CHAN2G(3, 2422),
121+ CHAN2G(7, 2442),
122+ CHAN2G(11, 2462)
123+};
124+
125+const struct ieee80211_channel dpd_5g_ch_list_bw160[] = {
126+ CHAN5G(50, 5250),
127+ CHAN5G(114, 5570),
128+ CHAN5G(163, 5815)
129+};
130+
131+const struct ieee80211_channel dpd_6g_ch_list_bw160[] = {
132+ CHAN6G(15, 6025),
133+ CHAN6G(47, 6185),
134+ CHAN6G(79, 6345),
135+ CHAN6G(111, 6505),
136+ CHAN6G(143, 6665),
137+ CHAN6G(175, 6825),
138+ CHAN6G(207, 6985)
139+};
140+
141+const struct ieee80211_channel dpd_6g_ch_list_bw320[] = {
142+ CHAN6G(31, 6105),
143+ CHAN6G(63, 6265),
144+ CHAN6G(95, 6425),
145+ CHAN6G(127, 6585),
146+ CHAN6G(159, 6745),
147+ CHAN6G(191, 6905)
148+};
149+
150+const u32 dpd_2g_bw20_ch_num = ARRAY_SIZE(dpd_2g_ch_list_bw20);
151+const u32 dpd_5g_bw160_ch_num = ARRAY_SIZE(dpd_5g_ch_list_bw160);
152+const u32 dpd_6g_bw160_ch_num = ARRAY_SIZE(dpd_6g_ch_list_bw160);
153+const u32 dpd_6g_bw320_ch_num = ARRAY_SIZE(dpd_6g_ch_list_bw320);
154+
155 static int mt7996_check_eeprom(struct mt7996_dev *dev)
156 {
157 u8 *eeprom = dev->mt76.eeprom.data;
developer95671ae2023-09-25 14:16:05 +0800158@@ -36,6 +72,36 @@ static char *mt7996_eeprom_name(struct mt7996_dev *dev)
developer281084d2023-06-19 12:03:50 +0800159 return MT7996_EEPROM_DEFAULT;
160 }
161
162+int
163+mt7996_get_dpd_per_band_size(struct mt7996_dev *dev, enum nl80211_band band)
164+{
165+ /* handle different sku */
166+ static const u8 band_to_idx[] = {
167+ [NL80211_BAND_2GHZ] = MT_BAND0,
168+ [NL80211_BAND_5GHZ] = MT_BAND1,
169+ [NL80211_BAND_6GHZ] = MT_BAND2,
170+ };
171+ struct mt7996_phy *phy = __mt7996_phy(dev, band_to_idx[band]);
172+ struct mt76_phy *mphy;
173+ int dpd_size;
174+
175+ if (!phy)
176+ return 0;
177+
178+ mphy = phy->mt76;
179+
180+ if (band == NL80211_BAND_2GHZ)
181+ dpd_size = dpd_2g_bw20_ch_num * DPD_PER_CH_BW20_SIZE;
182+ else if (band == NL80211_BAND_5GHZ)
183+ dpd_size = mphy->sband_5g.sband.n_channels * DPD_PER_CH_BW20_SIZE +
184+ dpd_5g_bw160_ch_num * DPD_PER_CH_GT_BW20_SIZE;
185+ else
186+ dpd_size = mphy->sband_6g.sband.n_channels * DPD_PER_CH_BW20_SIZE +
187+ (dpd_6g_bw160_ch_num + dpd_6g_bw320_ch_num) * DPD_PER_CH_GT_BW20_SIZE;
188+
189+ return dpd_size;
190+}
191+
192 static int
193 mt7996_eeprom_load_default(struct mt7996_dev *dev)
194 {
195diff --git a/mt7996/eeprom.h b/mt7996/eeprom.h
developer95671ae2023-09-25 14:16:05 +0800196index 343e65e18..7ff290f40 100644
developer281084d2023-06-19 12:03:50 +0800197--- a/mt7996/eeprom.h
198+++ b/mt7996/eeprom.h
199@@ -14,6 +14,7 @@ enum mt7996_eeprom_field {
200 MT_EE_MAC_ADDR = 0x004,
201 MT_EE_MAC_ADDR2 = 0x00a,
202 MT_EE_WIFI_CONF = 0x190,
203+ MT_EE_DO_PRE_CAL = 0x1a5,
204 MT_EE_TESTMODE_EN = 0x1af,
205 MT_EE_MAC_ADDR3 = 0x2c0,
206 MT_EE_RATE_DELTA_2G = 0x1400,
207@@ -31,6 +32,52 @@ enum mt7996_eeprom_field {
208 #define MT_EE_WIFI_CONF1_BAND_SEL GENMASK(5, 3)
209 #define MT_EE_WIFI_CONF2_BAND_SEL GENMASK(2, 0)
210
211+#define MT_EE_WIFI_CAL_GROUP_2G BIT(0)
212+#define MT_EE_WIFI_CAL_GROUP_5G BIT(1)
213+#define MT_EE_WIFI_CAL_GROUP_6G BIT(2)
214+#define MT_EE_WIFI_CAL_GROUP GENMASK(2, 0)
215+#define MT_EE_WIFI_CAL_DPD_2G BIT(3)
216+#define MT_EE_WIFI_CAL_DPD_5G BIT(4)
217+#define MT_EE_WIFI_CAL_DPD_6G BIT(5)
218+#define MT_EE_WIFI_CAL_DPD GENMASK(5, 3)
219+
220+#define MT_EE_CAL_UNIT 1024
221+#define MT_EE_CAL_GROUP_SIZE_2G (4 * MT_EE_CAL_UNIT)
222+#define MT_EE_CAL_GROUP_SIZE_5G (45 * MT_EE_CAL_UNIT)
223+#define MT_EE_CAL_GROUP_SIZE_6G (125 * MT_EE_CAL_UNIT)
224+#define MT_EE_CAL_ADCDCOC_SIZE_2G (4 * 4)
225+#define MT_EE_CAL_ADCDCOC_SIZE_5G (4 * 4)
226+#define MT_EE_CAL_ADCDCOC_SIZE_6G (4 * 5)
227+#define MT_EE_CAL_GROUP_SIZE (MT_EE_CAL_GROUP_SIZE_2G + \
228+ MT_EE_CAL_GROUP_SIZE_5G + \
229+ MT_EE_CAL_GROUP_SIZE_6G + \
230+ MT_EE_CAL_ADCDCOC_SIZE_2G + \
231+ MT_EE_CAL_ADCDCOC_SIZE_5G + \
232+ MT_EE_CAL_ADCDCOC_SIZE_6G)
233+
234+#define DPD_PER_CH_LEGACY_SIZE (4 * MT_EE_CAL_UNIT)
235+#define DPD_PER_CH_MEM_SIZE (13 * MT_EE_CAL_UNIT)
236+#define DPD_PER_CH_OTFG0_SIZE (2 * MT_EE_CAL_UNIT)
237+#define DPD_PER_CH_BW20_SIZE (DPD_PER_CH_LEGACY_SIZE + DPD_PER_CH_OTFG0_SIZE)
238+#define DPD_PER_CH_GT_BW20_SIZE (DPD_PER_CH_MEM_SIZE + DPD_PER_CH_OTFG0_SIZE)
239+#define MT_EE_CAL_DPD_SIZE (780 * MT_EE_CAL_UNIT)
240+
241+extern const struct ieee80211_channel dpd_2g_ch_list_bw20[];
242+extern const u32 dpd_2g_bw20_ch_num;
243+extern const struct ieee80211_channel dpd_5g_ch_list_bw160[];
244+extern const u32 dpd_5g_bw160_ch_num;
245+extern const struct ieee80211_channel dpd_6g_ch_list_bw160[];
246+extern const u32 dpd_6g_bw160_ch_num;
247+extern const struct ieee80211_channel dpd_6g_ch_list_bw320[];
248+extern const u32 dpd_6g_bw320_ch_num;
249+
250+#define RF_DPD_FLAT_CAL BIT(28)
251+#define RF_PRE_CAL BIT(29)
252+#define RF_DPD_FLAT_5G_CAL GENMASK(29, 28)
253+#define RF_DPD_FLAT_5G_MEM_CAL (BIT(30) | BIT(28))
254+#define RF_DPD_FLAT_6G_CAL GENMASK(30, 28)
255+#define RF_DPD_FLAT_6G_MEM_CAL (BIT(31) | BIT(28))
256+
257 #define MT_EE_WIFI_CONF1_TX_PATH_BAND0 GENMASK(5, 3)
258 #define MT_EE_WIFI_CONF2_TX_PATH_BAND1 GENMASK(2, 0)
259 #define MT_EE_WIFI_CONF2_TX_PATH_BAND2 GENMASK(5, 3)
260diff --git a/mt7996/mcu.c b/mt7996/mcu.c
developer95671ae2023-09-25 14:16:05 +0800261index 554f40ca2..dd3374a99 100644
developer281084d2023-06-19 12:03:50 +0800262--- a/mt7996/mcu.c
263+++ b/mt7996/mcu.c
developer95671ae2023-09-25 14:16:05 +0800264@@ -623,6 +623,11 @@ mt7996_mcu_uni_rx_unsolicited_event(struct mt7996_dev *dev, struct sk_buff *skb)
265 case MCU_UNI_EVENT_ALL_STA_INFO:
266 mt7996_mcu_rx_all_sta_info_event(dev, skb);
developer281084d2023-06-19 12:03:50 +0800267 break;
268+#ifdef CONFIG_NL80211_TESTMODE
269+ case MCU_UNI_EVENT_TESTMODE_CTRL:
270+ mt7996_tm_rf_test_event(dev, skb);
271+ break;
272+#endif
273 default:
274 break;
275 }
276diff --git a/mt7996/mt7996.h b/mt7996/mt7996.h
developer95671ae2023-09-25 14:16:05 +0800277index 2f4bbee70..91d759ec4 100644
developer281084d2023-06-19 12:03:50 +0800278--- a/mt7996/mt7996.h
279+++ b/mt7996/mt7996.h
developer95671ae2023-09-25 14:16:05 +0800280@@ -333,6 +333,9 @@ struct mt7996_dev {
developer281084d2023-06-19 12:03:50 +0800281 struct dentry *debugfs_dir;
282 struct rchan *relay_fwlog;
283
284+ void *cal;
285+ u32 cur_prek_offset;
286+
287 struct {
288 u8 table_mask;
289 u8 n_agrt;
developer95671ae2023-09-25 14:16:05 +0800290@@ -466,6 +469,7 @@ int mt7996_eeprom_parse_hw_cap(struct mt7996_dev *dev, struct mt7996_phy *phy);
developer281084d2023-06-19 12:03:50 +0800291 int mt7996_eeprom_get_target_power(struct mt7996_dev *dev,
292 struct ieee80211_channel *chan);
293 s8 mt7996_eeprom_get_power_delta(struct mt7996_dev *dev, int band);
294+int mt7996_get_dpd_per_band_size(struct mt7996_dev *dev, enum nl80211_band band);
295 int mt7996_dma_init(struct mt7996_dev *dev);
296 void mt7996_dma_reset(struct mt7996_dev *dev, bool force);
297 void mt7996_dma_prefetch(struct mt7996_dev *dev);
developer95671ae2023-09-25 14:16:05 +0800298@@ -549,6 +553,9 @@ int mt7996_mcu_set_tx_power_ctrl(struct mt7996_phy *phy, u8 power_ctrl_id, u8 da
developer281084d2023-06-19 12:03:50 +0800299 int mt7996_mcu_get_tx_power_info(struct mt7996_phy *phy, u8 category, void *event);
300 int mt7996_mcu_set_scs(struct mt7996_phy *phy, u8 enable);
301 void mt7996_mcu_scs_sta_poll(struct work_struct *work);
302+#ifdef CONFIG_NL80211_TESTMODE
303+void mt7996_tm_rf_test_event(struct mt7996_dev *dev, struct sk_buff *skb);
304+#endif
305
306 static inline u8 mt7996_max_interface_num(struct mt7996_dev *dev)
307 {
308diff --git a/mt7996/testmode.c b/mt7996/testmode.c
developer95671ae2023-09-25 14:16:05 +0800309index fb041c336..8ceea00b1 100644
developer281084d2023-06-19 12:03:50 +0800310--- a/mt7996/testmode.c
311+++ b/mt7996/testmode.c
312@@ -7,6 +7,8 @@
313 #include "mac.h"
314 #include "mcu.h"
315 #include "testmode.h"
316+#include "eeprom.h"
317+#include "mtk_mcu.h"
318
319 enum {
320 TM_CHANGED_TXPOWER,
developer95671ae2023-09-25 14:16:05 +0800321@@ -396,6 +398,436 @@ mt7996_tm_set_tx_cont(struct mt7996_phy *phy, bool en)
developer281084d2023-06-19 12:03:50 +0800322 }
323 }
324
325+static int
326+mt7996_tm_group_prek(struct mt7996_phy *phy, enum mt76_testmode_state state)
327+{
328+ u8 *eeprom;
329+ u32 i, group_size, dpd_size, size, offs, *pre_cal;
330+ int ret = 0;
331+ struct mt7996_dev *dev = phy->dev;
332+ struct mt76_dev *mdev = &dev->mt76;
333+ struct mt7996_tm_req req = {
334+ .rf_test = {
335+ .tag = cpu_to_le16(UNI_RF_TEST_CTRL),
336+ .len = cpu_to_le16(sizeof(req.rf_test)),
337+ .action = RF_ACTION_IN_RF_TEST,
338+ .icap_len = RF_TEST_ICAP_LEN,
339+ .op.rf.func_idx = cpu_to_le32(RF_TEST_RE_CAL),
340+ .op.rf.param.cal_param.func_data = cpu_to_le32(RF_PRE_CAL),
341+ .op.rf.param.cal_param.band_idx = phy->mt76->band_idx,
342+ },
343+ };
344+
345+ if (!dev->flash_mode) {
346+ dev_err(dev->mt76.dev, "Currently not in FLASH or BIN FILE mode, return!\n");
347+ return -EOPNOTSUPP;
348+ }
349+
350+ eeprom = mdev->eeprom.data;
351+ dev->cur_prek_offset = 0;
352+ group_size = MT_EE_CAL_GROUP_SIZE;
353+ dpd_size = MT_EE_CAL_DPD_SIZE;
354+ size = group_size + dpd_size;
355+ offs = MT_EE_DO_PRE_CAL;
356+
357+ switch (state) {
358+ case MT76_TM_STATE_GROUP_PREK:
359+ if (!dev->cal) {
360+ dev->cal = devm_kzalloc(mdev->dev, size, GFP_KERNEL);
361+ if (!dev->cal)
362+ return -ENOMEM;
363+ }
364+
365+ ret = mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(TESTMODE_CTRL), &req,
366+ sizeof(req), false);
367+ wait_event_timeout(mdev->mcu.wait, dev->cur_prek_offset == group_size,
368+ 30 * HZ);
369+
370+ if (ret) {
371+ dev_err(dev->mt76.dev, "Group Pre-cal: mcu send msg failed!\n");
372+ return ret;
373+ }
374+
375+ if (!ret)
376+ eeprom[offs] |= MT_EE_WIFI_CAL_GROUP;
377+ break;
378+ case MT76_TM_STATE_GROUP_PREK_DUMP:
379+ pre_cal = (u32 *)dev->cal;
380+ if (!pre_cal) {
381+ dev_info(dev->mt76.dev, "Not group pre-cal yet!\n");
382+ return ret;
383+ }
384+ dev_info(dev->mt76.dev, "Group Pre-Cal:\n");
385+ for (i = 0; i < (group_size / sizeof(u32)); i += 4) {
386+ dev_info(dev->mt76.dev, "[0x%08lx] 0x%8x 0x%8x 0x%8x 0x%8x\n",
387+ i * sizeof(u32), pre_cal[i], pre_cal[i + 1],
388+ pre_cal[i + 2], pre_cal[i + 3]);
389+ }
390+ break;
391+ case MT76_TM_STATE_GROUP_PREK_CLEAN:
392+ pre_cal = (u32 *)dev->cal;
393+ if (!pre_cal)
394+ return ret;
395+ memset(pre_cal, 0, group_size);
396+ eeprom[offs] &= ~MT_EE_WIFI_CAL_GROUP;
397+ break;
398+ default:
399+ return -EINVAL;
400+ }
401+
402+ return ret;
403+}
404+
405+static int
406+mt7996_tm_dpd_prek_send_req(struct mt7996_phy *phy, struct mt7996_tm_req *req,
407+ const struct ieee80211_channel *chan_list, u32 channel_size,
408+ enum nl80211_chan_width width, u32 func_data)
409+{
410+ struct mt7996_dev *dev = phy->dev;
411+ struct mt76_phy *mphy = phy->mt76;
412+ struct cfg80211_chan_def chandef_backup, *chandef = &mphy->chandef;
413+ struct ieee80211_channel chan_backup;
414+ int i, ret;
415+
416+ if (!chan_list)
417+ return -EOPNOTSUPP;
418+
419+ req->rf_test.op.rf.param.cal_param.func_data = cpu_to_le32(func_data);
420+
421+ memcpy(&chan_backup, chandef->chan, sizeof(struct ieee80211_channel));
422+ memcpy(&chandef_backup, chandef, sizeof(struct cfg80211_chan_def));
423+
424+ for (i = 0; i < channel_size; i++) {
425+ memcpy(chandef->chan, &chan_list[i], sizeof(struct ieee80211_channel));
426+ chandef->width = width;
427+
428+ /* set channel switch reason */
429+ mphy->hw->conf.flags |= IEEE80211_CONF_OFFCHANNEL;
430+ mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_SWITCH);
431+
432+ ret = mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(TESTMODE_CTRL), req,
433+ sizeof(*req), false);
434+ if (ret) {
435+ dev_err(dev->mt76.dev, "DPD Pre-cal: mcu send msg failed!\n");
436+ goto out;
437+ }
438+ }
439+
440+out:
441+ mphy->hw->conf.flags &= ~IEEE80211_CONF_OFFCHANNEL;
442+ memcpy(chandef, &chandef_backup, sizeof(struct cfg80211_chan_def));
443+ memcpy(chandef->chan, &chan_backup, sizeof(struct ieee80211_channel));
444+ mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_SWITCH);
445+
446+ return ret;
447+}
448+
449+static int
450+mt7996_tm_dpd_prek(struct mt7996_phy *phy, enum mt76_testmode_state state)
451+{
452+ struct mt7996_dev *dev = phy->dev;
453+ struct mt76_dev *mdev = &dev->mt76;
454+ struct mt76_phy *mphy = phy->mt76;
455+ struct mt7996_tm_req req = {
456+ .rf_test = {
457+ .tag = cpu_to_le16(UNI_RF_TEST_CTRL),
458+ .len = cpu_to_le16(sizeof(req.rf_test)),
459+ .action = RF_ACTION_IN_RF_TEST,
460+ .icap_len = RF_TEST_ICAP_LEN,
461+ .op.rf.func_idx = cpu_to_le32(RF_TEST_RE_CAL),
462+ .op.rf.param.cal_param.band_idx = phy->mt76->band_idx,
463+ },
464+ };
465+ u32 i, j, group_size, dpd_size, size, offs, *pre_cal;
466+ u32 wait_on_prek_offset = 0;
467+ u8 do_precal, *eeprom;
468+ int ret = 0;
469+
470+ if (!dev->flash_mode) {
471+ dev_err(dev->mt76.dev, "Currently not in FLASH or BIN FILE mode, return!\n");
472+ return -EOPNOTSUPP;
473+ }
474+
475+ eeprom = mdev->eeprom.data;
476+ dev->cur_prek_offset = 0;
477+ group_size = MT_EE_CAL_GROUP_SIZE;
478+ dpd_size = MT_EE_CAL_DPD_SIZE;
479+ size = group_size + dpd_size;
480+ offs = MT_EE_DO_PRE_CAL;
481+
482+ if (!dev->cal && state < MT76_TM_STATE_DPD_DUMP) {
483+ dev->cal = devm_kzalloc(mdev->dev, size, GFP_KERNEL);
484+ if (!dev->cal)
485+ return -ENOMEM;
486+ }
487+
488+ switch (state) {
489+ case MT76_TM_STATE_DPD_2G:
490+ ret = mt7996_tm_dpd_prek_send_req(phy, &req, dpd_2g_ch_list_bw20,
491+ dpd_2g_bw20_ch_num,
492+ NL80211_CHAN_WIDTH_20, RF_DPD_FLAT_CAL);
493+ wait_on_prek_offset += dpd_2g_bw20_ch_num * DPD_PER_CH_BW20_SIZE;
494+ wait_event_timeout(mdev->mcu.wait,
495+ dev->cur_prek_offset == wait_on_prek_offset, 30 * HZ);
496+
497+ do_precal = MT_EE_WIFI_CAL_DPD_2G;
498+ break;
499+ case MT76_TM_STATE_DPD_5G:
500+ /* 5g channel bw20 calibration */
501+ ret = mt7996_tm_dpd_prek_send_req(phy, &req, mphy->sband_5g.sband.channels,
502+ mphy->sband_5g.sband.n_channels,
503+ NL80211_CHAN_WIDTH_20, RF_DPD_FLAT_5G_CAL);
504+ if (ret)
505+ return ret;
506+ wait_on_prek_offset += mphy->sband_5g.sband.n_channels * DPD_PER_CH_BW20_SIZE;
507+ wait_event_timeout(mdev->mcu.wait,
508+ dev->cur_prek_offset == wait_on_prek_offset, 30 * HZ);
509+
510+ /* 5g channel bw160 calibration */
511+ ret = mt7996_tm_dpd_prek_send_req(phy, &req, dpd_5g_ch_list_bw160,
512+ dpd_5g_bw160_ch_num,
513+ NL80211_CHAN_WIDTH_160, RF_DPD_FLAT_5G_MEM_CAL);
514+ wait_on_prek_offset += dpd_5g_bw160_ch_num * DPD_PER_CH_GT_BW20_SIZE;
515+ wait_event_timeout(mdev->mcu.wait,
516+ dev->cur_prek_offset == wait_on_prek_offset, 30 * HZ);
517+
518+ do_precal = MT_EE_WIFI_CAL_DPD_5G;
519+ break;
520+ case MT76_TM_STATE_DPD_6G:
521+ /* 6g channel bw20 calibration */
522+ ret = mt7996_tm_dpd_prek_send_req(phy, &req, mphy->sband_6g.sband.channels,
523+ mphy->sband_6g.sband.n_channels,
524+ NL80211_CHAN_WIDTH_20, RF_DPD_FLAT_6G_CAL);
525+ if (ret)
526+ return ret;
527+ wait_on_prek_offset += mphy->sband_6g.sband.n_channels * DPD_PER_CH_BW20_SIZE;
528+ wait_event_timeout(mdev->mcu.wait,
529+ dev->cur_prek_offset == wait_on_prek_offset, 30 * HZ);
530+
531+ /* 6g channel bw160 calibration */
532+ ret = mt7996_tm_dpd_prek_send_req(phy, &req, dpd_6g_ch_list_bw160,
533+ dpd_6g_bw160_ch_num,
534+ NL80211_CHAN_WIDTH_160, RF_DPD_FLAT_6G_MEM_CAL);
535+ if (ret)
536+ return ret;
537+ wait_on_prek_offset += dpd_6g_bw160_ch_num * DPD_PER_CH_GT_BW20_SIZE;
538+ wait_event_timeout(mdev->mcu.wait,
539+ dev->cur_prek_offset == wait_on_prek_offset, 30 * HZ);
540+
541+ /* 6g channel bw320 calibration */
542+ ret = mt7996_tm_dpd_prek_send_req(phy, &req, dpd_6g_ch_list_bw320,
543+ dpd_6g_bw320_ch_num,
544+ NL80211_CHAN_WIDTH_320, RF_DPD_FLAT_6G_MEM_CAL);
545+ wait_on_prek_offset += dpd_6g_bw320_ch_num * DPD_PER_CH_GT_BW20_SIZE;
546+ wait_event_timeout(mdev->mcu.wait,
547+ dev->cur_prek_offset == wait_on_prek_offset, 30 * HZ);
548+
549+ do_precal = MT_EE_WIFI_CAL_DPD_6G;
550+ break;
551+ case MT76_TM_STATE_DPD_DUMP:
552+ if (!dev->cal) {
553+ dev_info(dev->mt76.dev, "Not DPD pre-cal yet!\n");
554+ return ret;
555+ }
556+ pre_cal = (u32 *)dev->cal;
557+ dev_info(dev->mt76.dev, "DPD Pre-Cal:\n");
558+ for (i = 0; i < dpd_size / sizeof(u32); i += 4) {
559+ j = i + (group_size / sizeof(u32));
560+ dev_info(dev->mt76.dev, "[0x%08lx] 0x%8x 0x%8x 0x%8x 0x%8x\n",
561+ j * sizeof(u32), pre_cal[j], pre_cal[j + 1],
562+ pre_cal[j + 2], pre_cal[j + 3]);
563+ }
564+ return 0;
565+ case MT76_TM_STATE_DPD_CLEAN:
566+ pre_cal = (u32 *)dev->cal;
567+ if (!pre_cal)
568+ return ret;
569+ memset(pre_cal + (group_size / sizeof(u32)), 0, dpd_size);
570+ do_precal = MT_EE_WIFI_CAL_DPD;
571+ eeprom[offs] &= ~do_precal;
572+ return 0;
573+ default:
574+ return -EINVAL;
575+ }
576+
577+ if (!ret)
578+ eeprom[offs] |= do_precal;
579+
580+ return ret;
581+}
582+
583+static int
584+mt7996_tm_dump_precal(struct mt76_phy *mphy, struct sk_buff *msg, int flag, int type)
585+{
586+#define DPD_PER_CHAN_SIZE_MASK GENMASK(31, 30)
587+#define DPD_2G_RATIO_MASK GENMASK(29, 20)
588+#define DPD_5G_RATIO_MASK GENMASK(19, 10)
589+#define DPD_6G_RATIO_MASK GENMASK(9, 0)
590+ struct mt7996_phy *phy = mphy->priv;
591+ struct mt7996_dev *dev = phy->dev;
592+ u32 i, group_size, dpd_size, total_size, size, dpd_info = 0;
593+ u32 dpd_size_2g, dpd_size_5g, dpd_size_6g;
594+ u32 base, offs, transmit_size = 1000;
595+ u8 *pre_cal, *eeprom;
596+ void *precal;
597+ enum prek_ops {
598+ PREK_GET_INFO,
599+ PREK_SYNC_ALL,
600+ PREK_SYNC_GROUP,
601+ PREK_SYNC_DPD_2G,
602+ PREK_SYNC_DPD_5G,
603+ PREK_SYNC_DPD_6G,
604+ PREK_CLEAN_GROUP,
605+ PREK_CLEAN_DPD,
606+ };
607+
608+ if (!dev->cal) {
609+ dev_info(dev->mt76.dev, "Not pre-cal yet!\n");
610+ return 0;
611+ }
612+
613+ group_size = MT_EE_CAL_GROUP_SIZE;
614+ dpd_size = MT_EE_CAL_DPD_SIZE;
615+ total_size = group_size + dpd_size;
616+ pre_cal = dev->cal;
617+ eeprom = dev->mt76.eeprom.data;
618+ offs = MT_EE_DO_PRE_CAL;
619+
620+ dpd_size_2g = mt7996_get_dpd_per_band_size(dev, NL80211_BAND_2GHZ);
621+ dpd_size_5g = mt7996_get_dpd_per_band_size(dev, NL80211_BAND_5GHZ);
622+ dpd_size_6g = mt7996_get_dpd_per_band_size(dev, NL80211_BAND_6GHZ);
623+
624+ switch (type) {
625+ case PREK_SYNC_ALL:
626+ base = 0;
627+ size = total_size;
628+ break;
629+ case PREK_SYNC_GROUP:
630+ base = 0;
631+ size = group_size;
632+ break;
633+ case PREK_SYNC_DPD_2G:
634+ base = group_size;
635+ size = dpd_size_2g;
636+ break;
637+ case PREK_SYNC_DPD_5G:
638+ base = group_size + dpd_size_2g;
639+ size = dpd_size_5g;
640+ break;
641+ case PREK_SYNC_DPD_6G:
642+ base = group_size + dpd_size_2g + dpd_size_5g;
643+ size = dpd_size_6g;
644+ break;
645+ case PREK_GET_INFO:
646+ break;
647+ default:
648+ return 0;
649+ }
650+
651+ if (!flag) {
652+ if (eeprom[offs] & MT_EE_WIFI_CAL_DPD) {
653+ dpd_info |= u32_encode_bits(1, DPD_PER_CHAN_SIZE_MASK) |
654+ u32_encode_bits(dpd_size_2g / MT_EE_CAL_UNIT,
655+ DPD_2G_RATIO_MASK) |
656+ u32_encode_bits(dpd_size_5g / MT_EE_CAL_UNIT,
657+ DPD_5G_RATIO_MASK) |
658+ u32_encode_bits(dpd_size_6g / MT_EE_CAL_UNIT,
659+ DPD_6G_RATIO_MASK);
660+ }
661+ dev->cur_prek_offset = 0;
662+ precal = nla_nest_start(msg, MT76_TM_ATTR_PRECAL_INFO);
663+ if (!precal)
664+ return -ENOMEM;
665+ nla_put_u32(msg, 0, group_size);
666+ nla_put_u32(msg, 1, dpd_size);
667+ nla_put_u32(msg, 2, dpd_info);
668+ nla_put_u32(msg, 3, transmit_size);
669+ nla_put_u32(msg, 4, eeprom[offs]);
670+ nla_nest_end(msg, precal);
671+ } else {
672+ precal = nla_nest_start(msg, MT76_TM_ATTR_PRECAL);
673+ if (!precal)
674+ return -ENOMEM;
675+
676+ transmit_size = (dev->cur_prek_offset + transmit_size < size) ?
677+ transmit_size : (size - dev->cur_prek_offset);
678+ for (i = 0; i < transmit_size; i++) {
679+ if (nla_put_u8(msg, i, pre_cal[base + dev->cur_prek_offset + i]))
680+ return -ENOMEM;
681+ }
682+ dev->cur_prek_offset += transmit_size;
683+
684+ nla_nest_end(msg, precal);
685+ }
686+
687+ return 0;
688+}
689+
690+static void
691+mt7996_tm_re_cal_event(struct mt7996_dev *dev, struct mt7996_tm_rf_test_result *result,
692+ struct mt7996_tm_rf_test_data *data)
693+{
694+ u32 base, dpd_size_2g, dpd_size_5g, dpd_size_6g, cal_idx, cal_type, len = 0;
695+ u8 *pre_cal;
696+
697+ pre_cal = dev->cal;
698+ dpd_size_2g = mt7996_get_dpd_per_band_size(dev, NL80211_BAND_2GHZ);
699+ dpd_size_5g = mt7996_get_dpd_per_band_size(dev, NL80211_BAND_5GHZ);
700+ dpd_size_6g = mt7996_get_dpd_per_band_size(dev, NL80211_BAND_6GHZ);
701+
702+ cal_idx = le32_to_cpu(data->cal_idx);
703+ cal_type = le32_to_cpu(data->cal_type);
704+ len = le32_to_cpu(result->payload_length);
705+ len = len - sizeof(struct mt7996_tm_rf_test_data);
706+
707+ switch (cal_type) {
708+ case RF_PRE_CAL:
709+ base = 0;
710+ break;
711+ case RF_DPD_FLAT_CAL:
712+ base = MT_EE_CAL_GROUP_SIZE;
713+ break;
714+ case RF_DPD_FLAT_5G_CAL:
715+ case RF_DPD_FLAT_5G_MEM_CAL:
716+ base = MT_EE_CAL_GROUP_SIZE + dpd_size_2g;
717+ break;
718+ case RF_DPD_FLAT_6G_CAL:
719+ case RF_DPD_FLAT_6G_MEM_CAL:
720+ base = MT_EE_CAL_GROUP_SIZE + dpd_size_2g + dpd_size_5g;
721+ break;
722+ default:
723+ dev_info(dev->mt76.dev, "Unknown calibration type!\n");
724+ return;
725+ }
726+ pre_cal += (base + dev->cur_prek_offset);
727+
728+ memcpy(pre_cal, data->cal_data, len);
729+ dev->cur_prek_offset += len;
730+}
731+
732+void mt7996_tm_rf_test_event(struct mt7996_dev *dev, struct sk_buff *skb)
733+{
734+ struct mt7996_tm_event *event;
735+ struct mt7996_tm_rf_test_result *result;
736+ struct mt7996_tm_rf_test_data *data;
737+ static u32 event_type;
738+
739+ skb_pull(skb, sizeof(struct mt7996_mcu_rxd));
740+ event = (struct mt7996_tm_event *)skb->data;
741+ result = (struct mt7996_tm_rf_test_result *)&event->result;
742+ data = (struct mt7996_tm_rf_test_data *)result->data;
743+
744+ event_type = le32_to_cpu(result->func_idx);
745+
746+ switch (event_type) {
747+ case RF_TEST_RE_CAL:
748+ mt7996_tm_re_cal_event(dev, result, data);
749+ break;
750+ default:
751+ break;
752+ }
753+}
754+
755 static void
756 mt7996_tm_update_params(struct mt7996_phy *phy, u32 changed)
757 {
developer95671ae2023-09-25 14:16:05 +0800758@@ -452,6 +884,10 @@ mt7996_tm_set_state(struct mt76_phy *mphy, enum mt76_testmode_state state)
developer281084d2023-06-19 12:03:50 +0800759 else if (prev_state == MT76_TM_STATE_OFF ||
760 state == MT76_TM_STATE_OFF)
761 mt7996_tm_init(phy, !(state == MT76_TM_STATE_OFF));
762+ else if (state >= MT76_TM_STATE_GROUP_PREK && state <= MT76_TM_STATE_GROUP_PREK_CLEAN)
763+ return mt7996_tm_group_prek(phy, state);
764+ else if (state >= MT76_TM_STATE_DPD_2G && state <= MT76_TM_STATE_DPD_CLEAN)
765+ return mt7996_tm_dpd_prek(phy, state);
766
767 if ((state == MT76_TM_STATE_IDLE &&
768 prev_state == MT76_TM_STATE_OFF) ||
developer95671ae2023-09-25 14:16:05 +0800769@@ -671,4 +1107,5 @@ const struct mt76_testmode_ops mt7996_testmode_ops = {
developer281084d2023-06-19 12:03:50 +0800770 .dump_stats = mt7996_tm_dump_stats,
771 .reset_rx_stats = mt7996_tm_reset_trx_stats,
772 .tx_stop = mt7996_tm_tx_stop,
773+ .dump_precal = mt7996_tm_dump_precal,
774 };
775diff --git a/mt7996/testmode.h b/mt7996/testmode.h
developer95671ae2023-09-25 14:16:05 +0800776index e4d55a61a..17c1456d3 100644
developer281084d2023-06-19 12:03:50 +0800777--- a/mt7996/testmode.h
778+++ b/mt7996/testmode.h
779@@ -34,6 +34,12 @@ enum bw_mapping_method {
780 NUM_BW_MAP,
781 };
782
783+struct tm_cal_param {
784+ __le32 func_data;
785+ u8 band_idx;
786+ u8 rsv[3];
787+};
788+
789 struct mt7996_tm_rf_test {
790 __le16 tag;
791 __le16 len;
792@@ -50,7 +56,7 @@ struct mt7996_tm_rf_test {
793 union {
794 __le32 func_data;
795 __le32 cal_dump;
796-
797+ struct tm_cal_param cal_param;
798 u8 _pad[80];
799 } param;
800 } rf;
801@@ -63,10 +69,16 @@ struct mt7996_tm_req {
802 struct mt7996_tm_rf_test rf_test;
803 } __packed;
804
805+struct mt7996_tm_rf_test_data {
806+ __le32 cal_idx;
807+ __le32 cal_type;
808+ u8 cal_data[0];
809+} __packed;
810+
811 struct mt7996_tm_rf_test_result {
812 __le32 func_idx;
813 __le32 payload_length;
814- u8 event[0];
815+ u8 data[0];
816 };
817
818 struct mt7996_tm_event {
819@@ -77,6 +89,8 @@ struct mt7996_tm_event {
820 struct mt7996_tm_rf_test_result result;
821 } __packed;
822
823+#define RF_TEST_RE_CAL 0x01
824+
825 enum {
826 RF_ACTION_SWITCH_TO_RF_TEST,
827 RF_ACTION_IN_RF_TEST,
828@@ -84,6 +98,8 @@ enum {
829 RF_ACTION_GET,
830 };
831
832+#define RF_TEST_ICAP_LEN 120
833+
834 enum {
835 RF_OPER_NORMAL,
836 RF_OPER_RF_TEST,
837diff --git a/testmode.c b/testmode.c
developer95671ae2023-09-25 14:16:05 +0800838index bbe8230fd..e66b54ae5 100644
developer281084d2023-06-19 12:03:50 +0800839--- a/testmode.c
840+++ b/testmode.c
developer95671ae2023-09-25 14:16:05 +0800841@@ -631,6 +631,18 @@ int mt76_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *msg,
developer281084d2023-06-19 12:03:50 +0800842
843 mutex_lock(&dev->mutex);
844
845+ if (tb[MT76_TM_ATTR_PRECAL] || tb[MT76_TM_ATTR_PRECAL_INFO]) {
846+ int flag, type;
847+
848+ err = -EINVAL;
849+ flag = tb[MT76_TM_ATTR_PRECAL] ? 1 : 0;
850+ type = flag ? nla_get_u8(tb[MT76_TM_ATTR_PRECAL_INFO]) : 0;
851+ if (dev->test_ops->dump_precal)
852+ err = dev->test_ops->dump_precal(phy, msg, flag, type);
853+
854+ goto out;
855+ }
856+
857 if (tb[MT76_TM_ATTR_STATS]) {
858 err = -EINVAL;
859
860diff --git a/testmode.h b/testmode.h
developer95671ae2023-09-25 14:16:05 +0800861index 141bb8625..db8ff53db 100644
developer281084d2023-06-19 12:03:50 +0800862--- a/testmode.h
863+++ b/testmode.h
developer95671ae2023-09-25 14:16:05 +0800864@@ -219,6 +219,14 @@ enum mt76_testmode_state {
developer281084d2023-06-19 12:03:50 +0800865 MT76_TM_STATE_TX_FRAMES,
866 MT76_TM_STATE_RX_FRAMES,
867 MT76_TM_STATE_TX_CONT,
868+ MT76_TM_STATE_GROUP_PREK,
869+ MT76_TM_STATE_GROUP_PREK_DUMP,
870+ MT76_TM_STATE_GROUP_PREK_CLEAN,
871+ MT76_TM_STATE_DPD_2G,
872+ MT76_TM_STATE_DPD_5G,
873+ MT76_TM_STATE_DPD_6G,
874+ MT76_TM_STATE_DPD_DUMP,
875+ MT76_TM_STATE_DPD_CLEAN,
876 MT76_TM_STATE_ON,
877
878 /* keep last */
879diff --git a/tools/fields.c b/tools/fields.c
developer95671ae2023-09-25 14:16:05 +0800880index 055f90f3c..b01227638 100644
developer281084d2023-06-19 12:03:50 +0800881--- a/tools/fields.c
882+++ b/tools/fields.c
883@@ -11,6 +11,14 @@ static const char * const testmode_state[] = {
884 [MT76_TM_STATE_TX_FRAMES] = "tx_frames",
885 [MT76_TM_STATE_RX_FRAMES] = "rx_frames",
886 [MT76_TM_STATE_TX_CONT] = "tx_cont",
887+ [MT76_TM_STATE_GROUP_PREK] = "group_prek",
888+ [MT76_TM_STATE_GROUP_PREK_DUMP] = "group_prek_dump",
889+ [MT76_TM_STATE_GROUP_PREK_CLEAN] = "group_prek_clean",
890+ [MT76_TM_STATE_DPD_2G] = "dpd_2g",
891+ [MT76_TM_STATE_DPD_5G] = "dpd_5g",
892+ [MT76_TM_STATE_DPD_6G] = "dpd_6g",
893+ [MT76_TM_STATE_DPD_DUMP] = "dpd_dump",
894+ [MT76_TM_STATE_DPD_CLEAN] = "dpd_clean",
895 };
896
897 static const char * const testmode_tx_mode[] = {
898--
8992.39.2
900