blob: e0199796ecb0fd1bc4a4d4d39884e8de6ec6efcc [file] [log] [blame]
developer22c7ab62022-01-24 11:13:32 +08001/* Copyright (C) 2021-2022 Mediatek Inc. */
2#define _GNU_SOURCE
3
4#include <unl.h>
5
6#include "atenl.h"
7
8#define to_rssi(_rcpi) ((_rcpi - 220) / 2)
9
10struct atenl_nl_priv {
11 struct atenl *an;
12 struct unl unl;
13 struct nl_msg *msg;
14 int attr;
15 void *res;
16};
17
18struct atenl_nl_ops {
19 int set;
20 int dump;
21 int (*ops)(struct atenl *an, struct atenl_data *data,
22 struct atenl_nl_priv *nl_priv);
23};
24
25static struct nla_policy testdata_policy[NUM_MT76_TM_ATTRS] = {
26 [MT76_TM_ATTR_STATE] = { .type = NLA_U8 },
27 [MT76_TM_ATTR_MTD_PART] = { .type = NLA_STRING },
28 [MT76_TM_ATTR_MTD_OFFSET] = { .type = NLA_U32 },
developerbb73fa22022-12-28 22:40:23 +080029 [MT76_TM_ATTR_BAND_IDX] = { .type = NLA_U8 },
developer3247ce22023-09-19 13:29:11 +080030 [MT76_TM_ATTR_SKU_EN] = { .type = NLA_U8 },
developer22c7ab62022-01-24 11:13:32 +080031 [MT76_TM_ATTR_TX_COUNT] = { .type = NLA_U32 },
32 [MT76_TM_ATTR_TX_LENGTH] = { .type = NLA_U32 },
33 [MT76_TM_ATTR_TX_RATE_MODE] = { .type = NLA_U8 },
34 [MT76_TM_ATTR_TX_RATE_NSS] = { .type = NLA_U8 },
35 [MT76_TM_ATTR_TX_RATE_IDX] = { .type = NLA_U8 },
36 [MT76_TM_ATTR_TX_RATE_SGI] = { .type = NLA_U8 },
37 [MT76_TM_ATTR_TX_RATE_LDPC] = { .type = NLA_U8 },
38 [MT76_TM_ATTR_TX_RATE_STBC] = { .type = NLA_U8 },
39 [MT76_TM_ATTR_TX_LTF] = { .type = NLA_U8 },
40 [MT76_TM_ATTR_TX_POWER_CONTROL] = { .type = NLA_U8 },
41 [MT76_TM_ATTR_TX_ANTENNA] = { .type = NLA_U8 },
42 [MT76_TM_ATTR_FREQ_OFFSET] = { .type = NLA_U32 },
43 [MT76_TM_ATTR_STATS] = { .type = NLA_NESTED },
developer454c4652022-08-31 20:39:29 +080044 [MT76_TM_ATTR_PRECAL] = { .type = NLA_NESTED },
45 [MT76_TM_ATTR_PRECAL_INFO] = { .type = NLA_NESTED },
developer22c7ab62022-01-24 11:13:32 +080046};
47
48static struct nla_policy stats_policy[NUM_MT76_TM_STATS_ATTRS] = {
49 [MT76_TM_STATS_ATTR_TX_PENDING] = { .type = NLA_U32 },
50 [MT76_TM_STATS_ATTR_TX_QUEUED] = { .type = NLA_U32 },
51 [MT76_TM_STATS_ATTR_TX_DONE] = { .type = NLA_U32 },
52 [MT76_TM_STATS_ATTR_RX_PACKETS] = { .type = NLA_U64 },
53 [MT76_TM_STATS_ATTR_RX_FCS_ERROR] = { .type = NLA_U64 },
54};
55
56static struct nla_policy rx_policy[NUM_MT76_TM_RX_ATTRS] = {
57 [MT76_TM_RX_ATTR_FREQ_OFFSET] = { .type = NLA_U32 },
58 [MT76_TM_RX_ATTR_RCPI] = { .type = NLA_NESTED },
developer3247ce22023-09-19 13:29:11 +080059 [MT76_TM_RX_ATTR_RSSI] = { .type = NLA_NESTED },
developer22c7ab62022-01-24 11:13:32 +080060 [MT76_TM_RX_ATTR_IB_RSSI] = { .type = NLA_NESTED },
61 [MT76_TM_RX_ATTR_WB_RSSI] = { .type = NLA_NESTED },
62 [MT76_TM_RX_ATTR_SNR] = { .type = NLA_U8 },
63};
64
65struct he_sgi {
66 enum mt76_testmode_tx_mode tx_mode;
67 u8 sgi;
68 u8 tx_ltf;
69};
70
71#define HE_SGI_GROUP(_tx_mode, _sgi, _tx_ltf) \
72 { .tx_mode = MT76_TM_TX_MODE_##_tx_mode, .sgi = _sgi, .tx_ltf = _tx_ltf }
73static const struct he_sgi he_sgi_groups[] = {
74 HE_SGI_GROUP(HE_SU, 0, 0),
75 HE_SGI_GROUP(HE_SU, 0, 1),
76 HE_SGI_GROUP(HE_SU, 1, 1),
77 HE_SGI_GROUP(HE_SU, 2, 2),
78 HE_SGI_GROUP(HE_SU, 0, 2),
79 HE_SGI_GROUP(HE_EXT_SU, 0, 0),
80 HE_SGI_GROUP(HE_EXT_SU, 0, 1),
81 HE_SGI_GROUP(HE_EXT_SU, 1, 1),
82 HE_SGI_GROUP(HE_EXT_SU, 2, 2),
83 HE_SGI_GROUP(HE_EXT_SU, 0, 2),
84 HE_SGI_GROUP(HE_TB, 1, 0),
85 HE_SGI_GROUP(HE_TB, 1, 1),
86 HE_SGI_GROUP(HE_TB, 2, 2),
87 HE_SGI_GROUP(HE_MU, 0, 2),
88 HE_SGI_GROUP(HE_MU, 0, 1),
89 HE_SGI_GROUP(HE_MU, 1, 1),
90 HE_SGI_GROUP(HE_MU, 2, 2),
91};
92#undef HE_SGI_LTF_GROUP
93
94static u8 phy_type_to_attr(u8 phy_type)
95{
96 static const u8 phy_type_to_attr[] = {
97 [ATENL_PHY_TYPE_CCK] = MT76_TM_TX_MODE_CCK,
98 [ATENL_PHY_TYPE_OFDM] = MT76_TM_TX_MODE_OFDM,
99 [ATENL_PHY_TYPE_HT] = MT76_TM_TX_MODE_HT,
100 [ATENL_PHY_TYPE_HT_GF] = MT76_TM_TX_MODE_HT,
101 [ATENL_PHY_TYPE_VHT] = MT76_TM_TX_MODE_VHT,
102 [ATENL_PHY_TYPE_HE_SU] = MT76_TM_TX_MODE_HE_SU,
103 [ATENL_PHY_TYPE_HE_EXT_SU] = MT76_TM_TX_MODE_HE_EXT_SU,
104 [ATENL_PHY_TYPE_HE_TB] = MT76_TM_TX_MODE_HE_TB,
105 [ATENL_PHY_TYPE_HE_MU] = MT76_TM_TX_MODE_HE_MU,
developer86869102023-05-15 13:52:35 +0800106 [ATENL_PHY_TYPE_EHT_SU] = MT76_TM_TX_MODE_EHT_SU,
107 [ATENL_PHY_TYPE_EHT_TRIG] = MT76_TM_TX_MODE_EHT_TRIG,
108 [ATENL_PHY_TYPE_EHT_MU] = MT76_TM_TX_MODE_EHT_MU,
developer22c7ab62022-01-24 11:13:32 +0800109 };
110
111 if (phy_type >= ARRAY_SIZE(phy_type_to_attr))
112 return 0;
113
114 return phy_type_to_attr[phy_type];
115}
116
117static void
118atenl_set_attr_state(struct atenl *an, struct nl_msg *msg,
119 u8 band, enum mt76_testmode_state state)
120{
121 if (get_band_val(an, band, cur_state) == state)
122 return;
123
124 nla_put_u8(msg, MT76_TM_ATTR_STATE, state);
125 set_band_val(an, band, cur_state, state);
126}
127
128static void
129atenl_set_attr_antenna(struct atenl *an, struct nl_msg *msg, u8 tx_antenna)
130{
131 if (!tx_antenna)
132 return;
developera7019752022-12-06 14:35:28 +0800133
134 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, tx_antenna);
developer22c7ab62022-01-24 11:13:32 +0800135}
136
137static int
138atenl_nl_set_attr(struct atenl *an, struct atenl_data *data,
139 struct atenl_nl_priv *nl_priv)
140{
141 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
142 struct nl_msg *msg = nl_priv->msg;
143 u32 val = ntohl(*(u32 *)hdr->data);
144 int attr = nl_priv->attr;
145 void *ptr, *a;
146
147 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
148 if (!ptr)
149 return -ENOMEM;
150
151 switch (attr) {
152 case MT76_TM_ATTR_TX_ANTENNA:
153 atenl_set_attr_antenna(an, msg, val);
154 break;
155 case MT76_TM_ATTR_FREQ_OFFSET:
156 nla_put_u32(msg, attr, val);
157 break;
158 case MT76_TM_ATTR_TX_POWER:
159 a = nla_nest_start(msg, MT76_TM_ATTR_TX_POWER);
160 if (!a)
161 return -ENOMEM;
162 nla_put_u8(msg, 0, val);
163 nla_nest_end(msg, a);
164 break;
165 default:
166 nla_put_u8(msg, attr, val);
167 break;
168 }
169
170 nla_nest_end(msg, ptr);
171
172 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
173}
174
175static int
176atenl_nl_set_cfg(struct atenl *an, struct atenl_data *data,
177 struct atenl_nl_priv *nl_priv)
178{
179 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
180 struct nl_msg *msg = nl_priv->msg;
181 enum atenl_cmd cmd = data->cmd;
182 u32 *v = (u32 *)hdr->data;
183 u8 type = ntohl(v[0]);
184 u8 enable = ntohl(v[1]);
185 void *ptr, *cfg;
186
187 if (cmd == HQA_CMD_SET_TSSI) {
188 type = 0;
189 enable = 1;
190 }
191
192 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
193 if (!ptr)
194 return -ENOMEM;
195
196 cfg = nla_nest_start(msg, MT76_TM_ATTR_CFG);
197 if (!cfg)
198 return -ENOMEM;
199
200 if (nla_put_u8(msg, 0, type) ||
201 nla_put_u8(msg, 1, enable))
202 return -EINVAL;
203
204 nla_nest_end(msg, cfg);
205
206 nla_nest_end(msg, ptr);
207
208 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
209}
210
211static int
212atenl_nl_set_tx(struct atenl *an, struct atenl_data *data,
213 struct atenl_nl_priv *nl_priv)
214{
215 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
216 struct nl_msg *msg = nl_priv->msg;
217 u32 *v = (u32 *)hdr->data;
218 u8 *addr1 = hdr->data + 36;
219 u8 *addr2 = addr1 + ETH_ALEN;
220 u8 *addr3 = addr2 + ETH_ALEN;
developerf30d4472022-05-30 16:40:23 +0800221 u8 def_mac[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
developer22c7ab62022-01-24 11:13:32 +0800222 void *ptr, *a;
223
developerf30d4472022-05-30 16:40:23 +0800224 if (get_band_val(an, an->cur_band, use_tx_time))
225 set_band_val(an, an->cur_band, tx_time, ntohl(v[7]));
226 else
227 set_band_val(an, an->cur_band, tx_mpdu_len, ntohl(v[7]));
228
developer22c7ab62022-01-24 11:13:32 +0800229 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
230 if (!ptr)
231 return -ENOMEM;
232
developer22c7ab62022-01-24 11:13:32 +0800233 a = nla_nest_start(msg, MT76_TM_ATTR_MAC_ADDRS);
234 if (!a)
235 return -ENOMEM;
236
developerf30d4472022-05-30 16:40:23 +0800237 nla_put(msg, 0, ETH_ALEN, use_default_addr(addr1) ? def_mac : addr1);
238 nla_put(msg, 1, ETH_ALEN, use_default_addr(addr2) ? def_mac : addr2);
239 nla_put(msg, 2, ETH_ALEN, use_default_addr(addr3) ? def_mac : addr3);
developer22c7ab62022-01-24 11:13:32 +0800240
241 nla_nest_end(msg, a);
242
243 nla_nest_end(msg, ptr);
244
245 *(u32 *)(hdr->data + 2) = data->ext_id;
246
247 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
248}
249
250static int
251atenl_nl_tx(struct atenl *an, struct atenl_data *data, struct atenl_nl_priv *nl_priv)
252{
253#define USE_SPE_IDX BIT(31)
254 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
255 struct nl_msg *msg = nl_priv->msg;
256 u32 *v = (u32 *)hdr->data;
257 u8 band = ntohl(v[2]);
258 void *ptr;
259 int ret;
260
261 if (band >= MAX_BAND_NUM)
262 return -EINVAL;
263
264 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
265 if (!ptr)
266 return -ENOMEM;
267
268 if (data->ext_cmd == HQA_EXT_CMD_STOP_TX) {
269 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
270 } else {
271 u32 tx_count = ntohl(v[3]);
272 u8 tx_rate_mode = phy_type_to_attr(ntohl(v[4]));
273 u8 aid = ntohl(v[11]);
274 u8 sgi = ntohl(v[13]);
275 u32 tx_antenna = ntohl(v[14]);
276 void *a;
277
278 if (sgi > 5)
279 return -EINVAL;
280
281 if (!tx_count)
282 tx_count = 10000000;
283
284 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, tx_count);
285 nla_put_u32(msg, MT76_TM_ATTR_TX_IPG, ntohl(v[12]));
286 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, tx_rate_mode);
287 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, ntohl(v[5]));
288 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_STBC, ntohl(v[7]));
289 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_LDPC, ntohl(v[8]));
290 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_NSS, ntohl(v[15]));
291
developerf30d4472022-05-30 16:40:23 +0800292 if (get_band_val(an, band, use_tx_time))
293 nla_put_u32(msg, MT76_TM_ATTR_TX_TIME,
294 get_band_val(an, band, tx_time));
developer86869102023-05-15 13:52:35 +0800295 else if (get_band_val(an, band, tx_mpdu_len))
developera398c862022-07-13 10:25:35 +0800296 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH,
297 get_band_val(an, band, tx_mpdu_len));
developerf30d4472022-05-30 16:40:23 +0800298
developer22c7ab62022-01-24 11:13:32 +0800299 /* for chips after 7915, tx need to use at least wcid = 1 */
300 if (!is_mt7915(an) && !aid)
301 aid = 1;
302 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
303
304 if (tx_antenna & USE_SPE_IDX) {
305 nla_put_u8(msg, MT76_TM_ATTR_TX_SPE_IDX,
306 tx_antenna & ~USE_SPE_IDX);
307 } else {
308 nla_put_u8(msg, MT76_TM_ATTR_TX_SPE_IDX, 0);
309 atenl_set_attr_antenna(an, msg, tx_antenna);
310 }
311
developere2e8ccc2023-10-13 18:41:57 +0800312 if (!is_connac3(an) && tx_rate_mode >= MT76_TM_TX_MODE_HE_SU) {
developer22c7ab62022-01-24 11:13:32 +0800313 u8 ofs = sgi;
314 size_t i;
315
316 for (i = 0; i < ARRAY_SIZE(he_sgi_groups); i++)
317 if (he_sgi_groups[i].tx_mode == tx_rate_mode)
318 break;
319
320 if ((i + ofs) >= ARRAY_SIZE(he_sgi_groups))
321 return -EINVAL;
322
323 sgi = he_sgi_groups[i + ofs].sgi;
324 nla_put_u8(msg, MT76_TM_ATTR_TX_LTF,
325 he_sgi_groups[i + ofs].tx_ltf);
326 }
327 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_SGI, sgi);
328
329 a = nla_nest_start(msg, MT76_TM_ATTR_TX_POWER);
330 if (!a)
331 return -ENOMEM;
332 nla_put_u8(msg, 0, ntohl(v[6]));
333 nla_nest_end(msg, a);
334
335 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_TX_FRAMES);
336 }
337
338 nla_nest_end(msg, ptr);
339
340 ret = unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
341 if (ret)
342 return ret;
343
344 *(u32 *)(hdr->data + 2) = data->ext_id;
345
346 return 0;
347}
348
349static int
350atenl_nl_rx(struct atenl *an, struct atenl_data *data, struct atenl_nl_priv *nl_priv)
351{
352 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
353 struct atenl_band *anb = &an->anb[an->cur_band];
354 struct nl_msg *msg = nl_priv->msg;
355 u32 *v = (u32 *)hdr->data;
356 u8 band = ntohl(v[2]);
357 void *ptr;
358
359 if (band >= MAX_BAND_NUM)
360 return -EINVAL;
361
362 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
363 if (!ptr)
364 return -ENOMEM;
365
366 if (data->ext_cmd == HQA_EXT_CMD_STOP_RX) {
367 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
368 } else {
369 v = (u32 *)(hdr->data + 18);
370
371 atenl_set_attr_antenna(an, msg, ntohl(v[0]));
developere2e8ccc2023-10-13 18:41:57 +0800372 if (is_connac3(an)) {
developer86869102023-05-15 13:52:35 +0800373 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE,
374 phy_type_to_attr(ntohl(v[2])));
375 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_SGI, ntohl(v[3]));
376 nla_put_u8(msg, MT76_TM_ATTR_AID, ntohl(v[4]));
377 } else {
378 nla_put_u8(msg, MT76_TM_ATTR_AID, ntohl(v[1]));
379 }
developer22c7ab62022-01-24 11:13:32 +0800380 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_RX_FRAMES);
381
382 anb->reset_rx_cnt = false;
383
384 /* clear history buffer */
385 memset(&anb->rx_stat, 0, sizeof(anb->rx_stat));
386 }
387
388 nla_nest_end(msg, ptr);
389
390 *(u32 *)(hdr->data + 2) = data->ext_id;
391
392 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
393}
394
395static int
396atenl_off_ch_scan(struct atenl *an, struct atenl_data *data,
397 struct atenl_nl_priv *nl_priv)
398{
399 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
400 struct nl_msg *msg = nl_priv->msg;
401 u32 *v = (u32 *)hdr->data;
402 u8 ch = ntohl(v[2]);
403 u8 bw = ntohl(v[4]);
404 u8 tx_path = ntohl(v[5]);
405 u8 status = ntohl(v[6]);
406 void *ptr;
407
408 if (!status)
409 ch = 0; /* stop */
410
411 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
412 if (!ptr)
413 return -ENOMEM;
414
415 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_CH, ch);
416 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_CENTER_CH,
417 atenl_get_center_channel(bw, CH_BAND_5GHZ, ch));
418 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_BW, bw);
419 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_PATH, tx_path);
420
421 nla_nest_end(msg, ptr);
422
developerf30d4472022-05-30 16:40:23 +0800423 *(u32 *)(hdr->data + 2) = data->ext_id;
developer22c7ab62022-01-24 11:13:32 +0800424
425 return 0;
426}
427
428static int atenl_nl_dump_cb(struct nl_msg *msg, void *arg)
429{
430 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
431 struct nlattr *tb1[NUM_MT76_TM_ATTRS];
432 struct nlattr *tb2[NUM_MT76_TM_STATS_ATTRS];
433 struct nlattr *nl_attr;
434 int attr = nl_priv->attr;
435 u64 *res = nl_priv->res;
436
437 nl_attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
438 if (!nl_attr) {
developerf30d4472022-05-30 16:40:23 +0800439 atenl_err("Testdata attribute not found\n");
developer22c7ab62022-01-24 11:13:32 +0800440 return NL_SKIP;
441 }
442
443 nla_parse_nested(tb1, MT76_TM_ATTR_MAX, nl_attr, testdata_policy);
444 nla_parse_nested(tb2, MT76_TM_STATS_ATTR_MAX,
445 tb1[MT76_TM_ATTR_STATS], stats_policy);
446
447 if (attr == MT76_TM_STATS_ATTR_TX_DONE)
448 *res = nla_get_u32(tb2[MT76_TM_STATS_ATTR_TX_DONE]);
449
450 return NL_SKIP;
451}
452
453static int
454atenl_nl_dump_attr(struct atenl *an, struct atenl_data *data,
455 struct atenl_nl_priv *nl_priv)
456{
457 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
458 struct nl_msg *msg = nl_priv->msg;
459 void *ptr;
460 u64 res = 0;
461
462 nl_priv->res = (void *)&res;
463
464 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
465 if (!ptr)
466 return -ENOMEM;
467 nla_put_flag(msg, MT76_TM_ATTR_STATS);
468 nla_nest_end(msg, ptr);
469
470 unl_genl_request(&nl_priv->unl, msg, atenl_nl_dump_cb, (void *)nl_priv);
471
472 if (nl_priv->attr == MT76_TM_STATS_ATTR_TX_DONE)
473 *(u32 *)(hdr->data + 2 + 4 * an->cur_band) = htonl(res);
474
475 return 0;
476}
477
478static int atenl_nl_continuous_tx(struct atenl *an,
479 struct atenl_data *data,
480 struct atenl_nl_priv *nl_priv)
481{
482 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
483 struct nl_msg *msg = nl_priv->msg;
484 u32 *v = (u32 *)hdr->data;
485 u8 band = ntohl(v[0]);
486 bool enable = ntohl(v[1]);
487 void *ptr;
488
489 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
490 if (!ptr)
491 return -ENOMEM;
492
493 if (band >= MAX_BAND_NUM)
494 return -EINVAL;
495
496 if (!enable) {
497 int phy = get_band_val(an, band, phy_idx);
498 char cmd[64];
499
500 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
501 nla_nest_end(msg, ptr);
502 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
503
504 sprintf(cmd, "iw dev mon%d del", phy);
505 system(cmd);
506 sprintf(cmd, "iw phy phy%d interface add mon%d type monitor", phy, phy);
507 system(cmd);
508 sprintf(cmd, "ifconfig mon%d up", phy);
509 system(cmd);
510
511 return 0;
512 }
513
514 if (get_band_val(an, band, rf_mode) != ATENL_RF_MODE_TEST)
515 return 0;
516
517 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, ntohl(v[2]));
518 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, phy_type_to_attr(ntohl(v[3])));
519 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, ntohl(v[6]));
520
521 atenl_dbg("%s: enable = %d, ant=%u, tx_rate_mode=%u, rate_idx=%u\n",
522 __func__, enable, ntohl(v[2]), ntohl(v[3]), ntohl(v[6]));
523
524 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_TX_CONT);
525
526 nla_nest_end(msg, ptr);
527
528 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
529}
530
531static int atenl_nl_get_rx_info_cb(struct nl_msg *msg, void *arg)
532{
533 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
534 struct atenl *an = nl_priv->an;
535 struct atenl_band *anb = &an->anb[an->cur_band];
536 struct atenl_data *data = nl_priv->res;
537 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
538 struct atenl_rx_info_hdr *rx_hdr;
539 struct atenl_rx_info_band *rx_band;
540 struct atenl_rx_info_user *rx_user;
541 struct atenl_rx_info_path *rx_path;
542 struct atenl_rx_info_comm *rx_comm;
543 struct nlattr *tb1[NUM_MT76_TM_ATTRS];
544 struct nlattr *tb2[NUM_MT76_TM_STATS_ATTRS];
545 struct nlattr *tb3[NUM_MT76_TM_RX_ATTRS];
546 struct nlattr *nl_attr, *cur;
547 struct atenl_rx_stat rx_cur, rx_diff = {};
548 u32 rcpi[4] = {};
549 u32 type_num = htonl(4);
550 s32 ib_rssi[4] = {}, wb_rssi[4] = {};
551 u8 path = an->anb[an->cur_band].chainmask;
552 u8 path_num = __builtin_popcount(path);
553 u8 *buf = hdr->data + 2;
554 int i, rem;
555
556 *(u32 *)buf = type_num;
557 buf += sizeof(type_num);
558
559#define RX_PUT_HDR(_hdr, _type, _val, _size) do { \
560 _hdr->type = htonl(_type); \
561 _hdr->val = htonl(_val); \
562 _hdr->len = htonl(_size); \
563 buf += sizeof(*_hdr); \
564 } while (0)
565
566 rx_hdr = (struct atenl_rx_info_hdr *)buf;
567 RX_PUT_HDR(rx_hdr, 0, BIT(an->cur_band), sizeof(*rx_band));
568 rx_band = (struct atenl_rx_info_band *)buf;
569 buf += sizeof(*rx_band);
570
571 rx_hdr = (struct atenl_rx_info_hdr *)buf;
572 RX_PUT_HDR(rx_hdr, 1, path, path_num * sizeof(*rx_path));
573 rx_path = (struct atenl_rx_info_path *)buf;
574 buf += path_num * sizeof(*rx_path);
575
576 rx_hdr = (struct atenl_rx_info_hdr *)buf;
577 RX_PUT_HDR(rx_hdr, 2, GENMASK(15, 0), 16 * sizeof(*rx_user));
578 rx_user = (struct atenl_rx_info_user *)buf;
579 buf += 16 * sizeof(*rx_user);
580
581 rx_hdr = (struct atenl_rx_info_hdr *)buf;
582 RX_PUT_HDR(rx_hdr, 3, BIT(0), sizeof(*rx_comm));
583 rx_comm = (struct atenl_rx_info_comm *)buf;
584 buf += sizeof(*rx_comm);
585
586 hdr->len = htons(buf - hdr->data);
587
588 nl_attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
589 if (!nl_attr) {
developerf30d4472022-05-30 16:40:23 +0800590 atenl_err("Testdata attribute not found\n");
developer22c7ab62022-01-24 11:13:32 +0800591 return NL_SKIP;
592 }
593
594 nla_parse_nested(tb1, MT76_TM_ATTR_MAX, nl_attr, testdata_policy);
595 nla_parse_nested(tb2, MT76_TM_STATS_ATTR_MAX,
596 tb1[MT76_TM_ATTR_STATS], stats_policy);
597
598 rx_cur.total = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_PACKETS]);
599 rx_cur.err_cnt = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_FCS_ERROR]);
600 rx_cur.len_mismatch = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_LEN_MISMATCH]);
601 rx_cur.ok_cnt = rx_cur.total - rx_cur.err_cnt - rx_cur.len_mismatch;
602
developerf30d4472022-05-30 16:40:23 +0800603 if (!anb->reset_rx_cnt ||
604 get_band_val(an, an->cur_band, cur_state) == MT76_TM_STATE_RX_FRAMES) {
developer22c7ab62022-01-24 11:13:32 +0800605#define RX_COUNT_DIFF(_field) \
developerf30d4472022-05-30 16:40:23 +0800606 rx_diff._field = (rx_cur._field) - (anb->rx_stat._field);
developer22c7ab62022-01-24 11:13:32 +0800607 RX_COUNT_DIFF(total);
608 RX_COUNT_DIFF(err_cnt);
609 RX_COUNT_DIFF(len_mismatch);
610 RX_COUNT_DIFF(ok_cnt);
611#undef RX_COUNT_DIFF
612
613 memcpy(&anb->rx_stat, &rx_cur, sizeof(anb->rx_stat));
614 }
615
616 rx_band->mac_rx_mdrdy_cnt = htonl((u32)rx_diff.total);
617 rx_band->mac_rx_fcs_err_cnt = htonl((u32)rx_diff.err_cnt);
618 rx_band->mac_rx_fcs_ok_cnt = htonl((u32)rx_diff.ok_cnt);
619 rx_band->mac_rx_len_mismatch = htonl((u32)rx_diff.len_mismatch);
620 rx_user->fcs_error_cnt = htonl((u32)rx_diff.err_cnt);
621
622 nla_parse_nested(tb3, MT76_TM_RX_ATTR_MAX,
623 tb2[MT76_TM_STATS_ATTR_LAST_RX], rx_policy);
624
625 rx_user->freq_offset = htonl(nla_get_u32(tb3[MT76_TM_RX_ATTR_FREQ_OFFSET]));
626 rx_user->snr = htonl(nla_get_u8(tb3[MT76_TM_RX_ATTR_SNR]));
627
628 i = 0;
629 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_RCPI], rem) {
630 if (nla_len(cur) != 1 || i >= 4)
631 break;
632
633 rcpi[i++] = nla_get_u8(cur);
634 }
635
636 i = 0;
637 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_IB_RSSI], rem) {
638 if (nla_len(cur) != 1 || i >= 4)
639 break;
640
641 ib_rssi[i++] = (s8)nla_get_u8(cur);
642 }
643
644 i = 0;
645 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_WB_RSSI], rem) {
646 if (nla_len(cur) != 1 || i >= 4)
647 break;
648
649 wb_rssi[i++] = (s8)nla_get_u8(cur);
650 }
651
652 for (i = 0; i < 4; i++) {
653 struct atenl_rx_info_path *path = &rx_path[i];
654
655 path->rcpi = htonl(rcpi[i]);
656 path->rssi = htonl(to_rssi((u8)rcpi[i]));
657 path->fagc_ib_rssi = htonl(ib_rssi[i]);
658 path->fagc_wb_rssi = htonl(wb_rssi[i]);
659 }
660
661 return NL_SKIP;
662}
663
664static int atenl_nl_get_rx_info(struct atenl *an, struct atenl_data *data,
665 struct atenl_nl_priv *nl_priv)
666{
667 struct nl_msg *msg = nl_priv->msg;
668 void *ptr;
669
670 nl_priv->an = an;
671 nl_priv->res = (void *)data;
672
673 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
674 if (!ptr)
675 return -ENOMEM;
676
677 nla_put_flag(msg, MT76_TM_ATTR_STATS);
678
679 nla_nest_end(msg, ptr);
680
681 return unl_genl_request(&nl_priv->unl, msg, atenl_nl_get_rx_info_cb,
682 (void *)nl_priv);
683}
684
685static int
686atenl_nl_set_ru(struct atenl *an, struct atenl_data *data,
687 struct atenl_nl_priv *nl_priv)
688{
689 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
690 struct nl_msg *msg;
691 u32 *v = (u32 *)(hdr->data + 4);
692 u32 seg0_num = ntohl(v[0]); /* v[1] seg1_num unused */
693 void *ptr;
694 int i, ret;
695
696 if (seg0_num > 8)
697 return -EINVAL;
698
699 for (i = 0, v = &v[2]; i < seg0_num; i++, v += 11) {
700 u32 ru_alloc = ntohl(v[1]);
701 u32 aid = ntohl(v[2]);
702 u32 ru_idx = ntohl(v[3]);
703 u32 mcs = ntohl(v[4]);
704 u32 ldpc = ntohl(v[5]);
705 u32 nss = ntohl(v[6]);
706 u32 tx_length = ntohl(v[8]);
707 char buf[10];
708
709 if (unl_genl_init(&nl_priv->unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +0800710 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +0800711 return 2;
712 }
713
714 msg = unl_genl_msg(&nl_priv->unl, NL80211_CMD_TESTMODE, false);
715 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
716
717 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
718 if (!ptr)
719 return -ENOMEM;
720
721 if (i == 0)
722 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_IDLE);
723
724 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
725 nla_put_u8(msg, MT76_TM_ATTR_RU_IDX, ru_idx);
726 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, mcs);
727 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_LDPC, ldpc);
728 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_NSS, nss);
developera398c862022-07-13 10:25:35 +0800729 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH, tx_length);
developer22c7ab62022-01-24 11:13:32 +0800730
731 ret = snprintf(buf, sizeof(buf), "%x", ru_alloc);
732 if (snprintf_error(sizeof(buf), ret))
733 return -EINVAL;
734
735 nla_put_u8(msg, MT76_TM_ATTR_RU_ALLOC, strtol(buf, NULL, 2));
736
737 nla_nest_end(msg, ptr);
738
739 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
740
741 unl_free(&nl_priv->unl);
742 }
743
744 return 0;
745}
746
developerf30d4472022-05-30 16:40:23 +0800747static int
748atenl_nl_ibf_init(struct atenl *an, u8 band)
749{
750 struct atenl_nl_priv nl_priv = {};
751 struct nl_msg *msg;
752 void *ptr, *a;
753 int ret;
754
755 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
756 atenl_err("Failed to connect to nl80211\n");
757 return 2;
758 }
759
760 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
761 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
762
763 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
764 if (!ptr) {
765 ret = -ENOMEM;
766 goto out;
767 }
768
769 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, MT76_TM_TX_MODE_HT);
770 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, an->ibf_mcs);
771 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, an->ibf_ant);
772 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_INIT);
773
774 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
775 if (!a) {
776 ret = -ENOMEM;
777 goto out;
778 }
779 nla_put_u16(msg, 0, 1);
780 nla_nest_end(msg, a);
781
782 nla_nest_end(msg, ptr);
783
784 ret = unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
785
786out:
787 unl_free(&nl_priv.unl);
788 return ret;
789}
790
791static int
792atenl_nl_ibf_e2p_update(struct atenl *an)
793{
794 struct atenl_nl_priv nl_priv = {};
795 struct nl_msg *msg;
796 void *ptr, *a;
797 int ret;
798
799 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
800 atenl_err("Failed to connect to nl80211\n");
801 return 2;
802 }
803
804 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
805 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
806
807 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
808 if (!ptr) {
809 ret = -ENOMEM;
810 goto out;
811 }
812
813 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_E2P_UPDATE);
814 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
815 if (!a) {
816 ret = -ENOMEM;
817 goto out;
818 }
819 nla_put_u16(msg, 0, 0);
820 nla_nest_end(msg, a);
821
822 nla_nest_end(msg, ptr);
823
824 ret = unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
825
826out:
827 unl_free(&nl_priv.unl);
828 return ret;
829}
830
developer25a27292023-07-21 13:42:14 +0800831void
developerf30d4472022-05-30 16:40:23 +0800832atenl_get_ibf_cal_result(struct atenl *an)
833{
developer0cd09622023-10-11 15:56:01 +0800834 u16 offset, group_size = 40;
developerf30d4472022-05-30 16:40:23 +0800835
836 if (an->adie_id == 0x7975)
837 offset = 0x651;
developerc2309682024-11-15 19:28:46 +0800838 else
developerf30d4472022-05-30 16:40:23 +0800839 offset = 0x60a;
840
developer0cd09622023-10-11 15:56:01 +0800841 if (is_mt7996(an)) {
842 offset = 0xc00;
843 group_size = 46;
844 }
845
846 /* per group size = 40 or 46, for group 0-8 */
847 atenl_eeprom_read_from_driver(an, offset, group_size * 9);
developerf30d4472022-05-30 16:40:23 +0800848}
849
850static int
851atenl_nl_ibf_set_val(struct atenl *an, struct atenl_data *data,
852 struct atenl_nl_priv *nl_priv)
developer22c7ab62022-01-24 11:13:32 +0800853{
854#define MT_IBF(_act) MT76_TM_TXBF_ACT_##_act
855 static const u8 bf_act_map[] = {
856 [TXBF_ACT_IBF_PHASE_COMP] = MT_IBF(PHASE_COMP),
857 [TXBF_ACT_IBF_PROF_UPDATE] = MT_IBF(IBF_PROF_UPDATE),
858 [TXBF_ACT_EBF_PROF_UPDATE] = MT_IBF(EBF_PROF_UPDATE),
859 [TXBF_ACT_IBF_PHASE_CAL] = MT_IBF(PHASE_CAL),
860 };
861#undef MT_IBF
862 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
863 struct nl_msg *msg = nl_priv->msg;
864 u32 *v = (u32 *)(hdr->data + 4);
865 u32 action = ntohl(v[0]);
developerdc430562022-09-13 10:57:15 +0800866 u16 val[8], is_atenl = 1;
developerf30d4472022-05-30 16:40:23 +0800867 u8 tmp_ant;
developer22c7ab62022-01-24 11:13:32 +0800868 void *ptr, *a;
developer22c7ab62022-01-24 11:13:32 +0800869 int i;
870
871 for (i = 0; i < 8; i++)
872 val[i] = ntohl(v[i + 1]);
873
874 atenl_dbg("%s: action = %u, val = %u, %u, %u, %u, %u\n",
875 __func__, action, val[0], val[1], val[2], val[3], val[4]);
876
877 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
878 if (!ptr)
879 return -ENOMEM;
880
881 switch (action) {
developer22c7ab62022-01-24 11:13:32 +0800882 case TXBF_ACT_CHANNEL:
developerf30d4472022-05-30 16:40:23 +0800883 an->cur_band = val[1];
884 /* a sanity to prevent script band idx error */
885 if (val[0] > 14)
886 an->cur_band = 1;
887 atenl_nl_ibf_init(an, an->cur_band);
888 atenl_set_channel(an, 0, an->cur_band, val[0], 0, 0);
889
890 nla_put_u8(msg, MT76_TM_ATTR_AID, 0);
developer22c7ab62022-01-24 11:13:32 +0800891 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_UPDATE_CH);
892 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
893 if (!a)
894 return -ENOMEM;
895 nla_put_u16(msg, 0, 0);
896 nla_nest_end(msg, a);
897 break;
898 case TXBF_ACT_MCS:
developerf30d4472022-05-30 16:40:23 +0800899 tmp_ant = (1 << DIV_ROUND_UP(val[0], 8)) - 1 ?: 1;
900 /* sometimes the correct band idx will be set after this action,
901 * so maintain a temp variable to allow mcs update in anthor action.
902 */
903 an->ibf_mcs = val[0];
developerf30d4472022-05-30 16:40:23 +0800904 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, an->ibf_mcs);
developere2e8ccc2023-10-13 18:41:57 +0800905 if (!is_connac3(an)) {
developer0cd09622023-10-11 15:56:01 +0800906 an->ibf_ant = tmp_ant;
907 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, an->ibf_ant);
908 }
developer22c7ab62022-01-24 11:13:32 +0800909 break;
910 case TXBF_ACT_TX_ANT:
911 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, val[0]);
912 break;
913 case TXBF_ACT_RX_START:
914 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_RX_FRAMES);
915 break;
916 case TXBF_ACT_RX_ANT:
917 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, val[0]);
918 break;
919 case TXBF_ACT_TX_PKT:
920 nla_put_u8(msg, MT76_TM_ATTR_AID, val[1]);
developer22c7ab62022-01-24 11:13:32 +0800921 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_TX_PREP);
developer86869102023-05-15 13:52:35 +0800922 if (!val[2])
923 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, 0xFFFFFFFF);
924 else
925 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, val[2]);
developera398c862022-07-13 10:25:35 +0800926 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH, 1024);
developer22c7ab62022-01-24 11:13:32 +0800927 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
928 if (!a)
929 return -ENOMEM;
930
931 for (i = 0; i < 5; i++)
932 nla_put_u16(msg, i, val[i]);
933 nla_nest_end(msg, a);
934
935 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_TX_FRAMES);
936 break;
937 case TXBF_ACT_IBF_PHASE_COMP:
developerf30d4472022-05-30 16:40:23 +0800938 nla_put_u8(msg, MT76_TM_ATTR_AID, 1);
developer22c7ab62022-01-24 11:13:32 +0800939 case TXBF_ACT_IBF_PROF_UPDATE:
940 case TXBF_ACT_EBF_PROF_UPDATE:
941 case TXBF_ACT_IBF_PHASE_CAL:
942 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, bf_act_map[action]);
943 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
944 if (!a)
945 return -ENOMEM;
developer0cd09622023-10-11 15:56:01 +0800946 /* Note: litepoint may send random number for lna_gain_level,
947 * reset to 1 (mid gain) and 0 for wifi 7 and wifi 6, respectively
948 */
developerdc430562022-09-13 10:57:15 +0800949 if (action == TXBF_ACT_IBF_PHASE_CAL)
developere2e8ccc2023-10-13 18:41:57 +0800950 val[4] = is_connac3(an) ? 1 : 0;
developer22c7ab62022-01-24 11:13:32 +0800951 for (i = 0; i < 5; i++)
952 nla_put_u16(msg, i, val[i]);
developerdc430562022-09-13 10:57:15 +0800953 /* Used to distinguish between command mode and HQADLL mode */
954 nla_put_u16(msg, 5, is_atenl);
developer22c7ab62022-01-24 11:13:32 +0800955 nla_nest_end(msg, a);
956 break;
957 case TXBF_ACT_IBF_PHASE_E2P_UPDATE:
developerf30d4472022-05-30 16:40:23 +0800958 atenl_nl_ibf_e2p_update(an);
959 atenl_get_ibf_cal_result(an);
developer22c7ab62022-01-24 11:13:32 +0800960
961 nla_put_u8(msg, MT76_TM_ATTR_AID, 0);
962 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_INIT);
963
964 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
965 if (!a)
966 return -ENOMEM;
967 nla_put_u16(msg, 0, 0);
968 nla_nest_end(msg, a);
969 break;
developerf30d4472022-05-30 16:40:23 +0800970 case TXBF_ACT_INIT:
971 case TXBF_ACT_POWER:
developer22c7ab62022-01-24 11:13:32 +0800972 default:
973 break;
974 }
975
976 nla_nest_end(msg, ptr);
977
developerf30d4472022-05-30 16:40:23 +0800978 *(u32 *)(hdr->data + 2) = data->ext_id;
developer22c7ab62022-01-24 11:13:32 +0800979
980 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
981}
982
983static int
984atenl_nl_ibf_get_status(struct atenl *an, struct atenl_data *data,
985 struct atenl_nl_priv *nl_priv)
986{
987 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
988 u32 status = htonl(1);
989
developerf30d4472022-05-30 16:40:23 +0800990 *(u32 *)(hdr->data + 2) = data->ext_id;
developer22c7ab62022-01-24 11:13:32 +0800991 memcpy(hdr->data + 6, &status, 4);
992
993 return 0;
994}
995
996static int
997atenl_nl_ibf_profile_update_all(struct atenl *an, struct atenl_data *data,
998 struct atenl_nl_priv *nl_priv)
999{
1000 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
1001 struct nl_msg *msg;
1002 void *ptr, *a;
1003 u32 *v = (u32 *)(hdr->data + 4);
1004 u16 pfmu_idx = ntohl(v[0]);
1005 int i;
1006
1007 for (i = 0, v = &v[5]; i < 64; i++, v += 5) {
1008 int j;
1009
1010 if (unl_genl_init(&nl_priv->unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001011 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001012 return 2;
1013 }
1014
1015 msg = unl_genl_msg(&nl_priv->unl, NL80211_CMD_TESTMODE, false);
1016 nla_put_u32(msg, NL80211_ATTR_WIPHY,
1017 get_band_val(an, an->cur_band, phy_idx));
1018
1019 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1020 if (!ptr)
1021 return -ENOMEM;
1022
1023 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_PROF_UPDATE_ALL);
1024 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
1025 if (!a)
1026 return -ENOMEM;
1027 nla_put_u16(msg, 0, pfmu_idx);
1028
1029 for (j = 0; j < 5; j++)
1030 nla_put_u16(msg, j + 1, ntohl(v[j]));
1031 nla_nest_end(msg, a);
1032
1033 nla_nest_end(msg, ptr);
1034
1035 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
1036
1037 unl_free(&nl_priv->unl);
1038 }
1039
developerf30d4472022-05-30 16:40:23 +08001040 *(u32 *)(hdr->data + 2) = data->ext_id;
developer22c7ab62022-01-24 11:13:32 +08001041
1042 return 0;
1043}
1044
1045#define NL_OPS_GROUP(cmd, ...) [HQA_CMD_##cmd] = { __VA_ARGS__ }
1046static const struct atenl_nl_ops nl_ops[] = {
1047 NL_OPS_GROUP(SET_TX_PATH, .set=MT76_TM_ATTR_TX_ANTENNA),
1048 NL_OPS_GROUP(SET_TX_POWER, .set=MT76_TM_ATTR_TX_POWER),
1049 NL_OPS_GROUP(SET_RX_PATH, .set=MT76_TM_ATTR_TX_ANTENNA),
1050 NL_OPS_GROUP(SET_FREQ_OFFSET, .set=MT76_TM_ATTR_FREQ_OFFSET),
1051 NL_OPS_GROUP(SET_CFG, .ops=atenl_nl_set_cfg),
1052 NL_OPS_GROUP(SET_TSSI, .ops=atenl_nl_set_cfg),
1053 NL_OPS_GROUP(CONTINUOUS_TX, .ops=atenl_nl_continuous_tx),
1054 NL_OPS_GROUP(GET_TX_INFO, .dump=MT76_TM_STATS_ATTR_TX_DONE),
1055 NL_OPS_GROUP(GET_RX_INFO, .ops=atenl_nl_get_rx_info, .dump=true),
1056 NL_OPS_GROUP(SET_RU, .ops=atenl_nl_set_ru),
1057};
1058#undef NL_OPS_GROUP
1059
1060#define NL_OPS_EXT(cmd, ...) [HQA_EXT_CMD_##cmd] = { __VA_ARGS__ }
1061static const struct atenl_nl_ops nl_ops_ext[] = {
1062 NL_OPS_EXT(SET_TX, .ops=atenl_nl_set_tx),
1063 NL_OPS_EXT(START_TX, .ops=atenl_nl_tx),
1064 NL_OPS_EXT(STOP_TX, .ops=atenl_nl_tx),
1065 NL_OPS_EXT(START_RX, .ops=atenl_nl_rx),
1066 NL_OPS_EXT(STOP_RX, .ops=atenl_nl_rx),
1067 NL_OPS_EXT(OFF_CH_SCAN, .ops=atenl_off_ch_scan),
1068 NL_OPS_EXT(IBF_SET_VAL, .ops=atenl_nl_ibf_set_val),
1069 NL_OPS_EXT(IBF_GET_STATUS, .ops=atenl_nl_ibf_get_status),
1070 NL_OPS_EXT(IBF_PROF_UPDATE_ALL, .ops=atenl_nl_ibf_profile_update_all),
1071};
1072#undef NL_OPS_EXT
1073
1074int atenl_nl_process(struct atenl *an, struct atenl_data *data)
1075{
1076 struct atenl_nl_priv nl_priv = {};
1077 const struct atenl_nl_ops *ops;
1078 struct nl_msg *msg;
1079 int ret = 0;
1080
1081 if (data->ext_cmd != 0)
1082 ops = &nl_ops_ext[data->ext_cmd];
1083 else
1084 ops = &nl_ops[data->cmd];
1085
1086 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001087 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001088 return -1;
1089 }
1090
1091 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, !!ops->dump);
1092 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
1093 nl_priv.msg = msg;
1094
1095 if (ops->ops) {
1096 ret = ops->ops(an, data, &nl_priv);
1097 } else if (ops->dump) {
1098 nl_priv.attr = ops->dump;
1099 ret = atenl_nl_dump_attr(an, data, &nl_priv);
1100 } else {
1101 nl_priv.attr = ops->set;
1102 ret = atenl_nl_set_attr(an, data, &nl_priv);
1103 }
1104
1105 if (ret)
developerf30d4472022-05-30 16:40:23 +08001106 atenl_err("command process error: 0x%x (0x%x)\n", data->cmd_id, data->ext_id);
developer22c7ab62022-01-24 11:13:32 +08001107
1108 unl_free(&nl_priv.unl);
1109
1110 return ret;
1111}
1112
1113int atenl_nl_process_many(struct atenl *an, struct atenl_data *data)
1114{
1115 struct atenl_nl_priv nl_priv = {};
1116 const struct atenl_nl_ops *ops;
1117 int ret = 0;
1118
1119 if (data->ext_cmd != 0)
1120 ops = &nl_ops_ext[data->ext_cmd];
1121 else
1122 ops = &nl_ops[data->cmd];
1123
1124 if (ops->ops)
1125 ret = ops->ops(an, data, &nl_priv);
1126
1127 return ret;
1128}
1129
1130int atenl_nl_set_state(struct atenl *an, u8 band,
1131 enum mt76_testmode_state state)
1132{
1133 struct atenl_nl_priv nl_priv = {};
1134 struct nl_msg *msg;
1135 void *ptr;
1136
1137 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001138 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001139 return 2;
1140 }
1141
1142 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1143 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
1144
1145 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1146 if (!ptr)
1147 return -ENOMEM;
1148
1149 atenl_set_attr_state(an, msg, band, state);
1150
1151 nla_nest_end(msg, ptr);
1152
1153 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1154
1155 unl_free(&nl_priv.unl);
1156
1157 return 0;
1158}
1159
developerf30d4472022-05-30 16:40:23 +08001160int atenl_nl_set_aid(struct atenl *an, u8 band, u8 aid)
1161{
1162 struct atenl_nl_priv nl_priv = {};
1163 struct nl_msg *msg;
1164 void *ptr;
1165
1166 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1167 atenl_err("Failed to connect to nl80211\n");
1168 return 2;
1169 }
1170
1171 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1172 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
1173
1174 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1175 if (!ptr)
1176 return -ENOMEM;
1177
1178 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
1179
1180 nla_nest_end(msg, ptr);
1181
1182 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1183
1184 unl_free(&nl_priv.unl);
1185
1186 return 0;
1187}
1188
developerf0677ad2024-11-15 19:28:25 +08001189static int atenl_nl_check_flash_cb(struct nl_msg *msg, void *arg)
developer22c7ab62022-01-24 11:13:32 +08001190{
1191 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
1192 struct atenl *an = nl_priv->an;
1193 struct nlattr *tb[NUM_MT76_TM_ATTRS];
1194 struct nlattr *attr;
1195
1196 attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
1197 if (!attr)
1198 return NL_SKIP;
1199
1200 nla_parse_nested(tb, MT76_TM_ATTR_MAX, attr, testdata_policy);
developer86045992024-06-03 16:24:42 +08001201 an->band_idx = nla_get_u32(tb[MT76_TM_ATTR_BAND_IDX]);
1202
developer22c7ab62022-01-24 11:13:32 +08001203 if (!tb[MT76_TM_ATTR_MTD_PART] || !tb[MT76_TM_ATTR_MTD_OFFSET])
1204 return NL_SKIP;
1205
developerf0677ad2024-11-15 19:28:25 +08001206 an->flash_part = strdup(nla_get_string(tb[MT76_TM_ATTR_MTD_PART]));
1207 an->flash_offset = nla_get_u32(tb[MT76_TM_ATTR_MTD_OFFSET]);
developer22c7ab62022-01-24 11:13:32 +08001208
1209 return NL_SKIP;
1210}
1211
developerf0677ad2024-11-15 19:28:25 +08001212int atenl_nl_check_flash(struct atenl *an)
developer22c7ab62022-01-24 11:13:32 +08001213{
1214 struct atenl_nl_priv nl_priv = { .an = an };
1215 struct nl_msg *msg;
1216
1217 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001218 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001219 return 2;
1220 }
1221
developerf0677ad2024-11-15 19:28:25 +08001222 /* User has a specified flash partition */
1223 if (an->flash_part)
1224 return 0;
1225
developer22c7ab62022-01-24 11:13:32 +08001226 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, true);
1227 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
developerf0677ad2024-11-15 19:28:25 +08001228 unl_genl_request(&nl_priv.unl, msg, atenl_nl_check_flash_cb,
1229 (void *)&nl_priv);
developer22c7ab62022-01-24 11:13:32 +08001230
1231 unl_free(&nl_priv.unl);
1232
1233 return 0;
1234}
1235
1236int atenl_nl_write_eeprom(struct atenl *an, u32 offset, u8 *val, int len)
1237{
1238 struct atenl_nl_priv nl_priv = {};
1239 struct nl_msg *msg;
1240 void *ptr, *a;
1241 int i;
1242
1243 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001244 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001245 return 2;
1246 }
1247
1248 if (len > 16)
1249 return -EINVAL;
1250
1251 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1252 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1253
1254 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1255 if (!ptr)
1256 return -ENOMEM;
1257
1258 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1259 MT76_TM_EEPROM_ACTION_UPDATE_DATA);
1260 nla_put_u32(msg, MT76_TM_ATTR_EEPROM_OFFSET, offset);
1261
1262 a = nla_nest_start(msg, MT76_TM_ATTR_EEPROM_VAL);
1263 if (!a)
1264 return -ENOMEM;
1265
1266 for (i = 0; i < len; i++)
1267 if (nla_put_u8(msg, i, val[i]))
1268 goto out;
1269
1270 nla_nest_end(msg, a);
1271
1272 nla_nest_end(msg, ptr);
1273
1274 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1275
1276 unl_free(&nl_priv.unl);
1277
1278out:
1279 return 0;
1280}
1281
developer48fbcb62022-03-10 14:24:55 +08001282int atenl_nl_write_efuse_all(struct atenl *an)
developer22c7ab62022-01-24 11:13:32 +08001283{
1284 struct atenl_nl_priv nl_priv = {};
1285 struct nl_msg *msg;
1286 void *ptr;
1287
1288 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001289 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001290 return 2;
1291 }
1292
1293 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1294 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1295
1296 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1297 if (!ptr)
1298 return -ENOMEM;
1299
1300 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1301 MT76_TM_EEPROM_ACTION_WRITE_TO_EFUSE);
1302
1303 nla_nest_end(msg, ptr);
1304
1305 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1306
1307 unl_free(&nl_priv.unl);
1308
1309 return 0;
1310}
1311
developerdcf5eb32024-09-03 17:06:32 +08001312int atenl_nl_write_ext_eeprom_all(struct atenl *an)
1313{
1314 struct atenl_nl_priv nl_priv = {};
1315 struct nl_msg *msg;
1316 void *ptr;
1317
1318 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1319 atenl_err("Failed to connect to nl80211\n");
1320 return 2;
1321 }
1322
1323 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1324 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1325
1326 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1327 if (!ptr)
1328 return -ENOMEM;
1329
1330 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1331 MT76_TM_EEPROM_ACTION_WRITE_TO_EXT_EEPROM);
1332
1333 nla_nest_end(msg, ptr);
1334
1335 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1336
1337 unl_free(&nl_priv.unl);
1338
1339 return 0;
1340}
1341
developer22c7ab62022-01-24 11:13:32 +08001342int atenl_nl_update_buffer_mode(struct atenl *an)
1343{
1344 struct atenl_nl_priv nl_priv = {};
1345 struct nl_msg *msg;
1346 void *ptr;
1347
1348 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001349 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001350 return 2;
1351 }
1352
1353 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1354 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1355
1356 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1357 if (!ptr)
1358 return -ENOMEM;
1359
1360 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1361 MT76_TM_EEPROM_ACTION_UPDATE_BUFFER_MODE);
1362
1363 nla_nest_end(msg, ptr);
1364
1365 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1366
1367 unl_free(&nl_priv.unl);
1368
1369 return 0;
1370}
1371
developer454c4652022-08-31 20:39:29 +08001372static int atenl_nl_precal_sync_from_driver_cb(struct nl_msg *msg, void *arg)
1373{
1374 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
1375 struct atenl *an = nl_priv->an;
1376 struct nlattr *tb[NUM_MT76_TM_ATTRS];
1377 struct nlattr *attr, *cur;
1378 int i, rem, prek_offset = nl_priv->attr;
1379
1380
1381 attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
1382 if (!attr)
1383 return NL_SKIP;
1384
1385 nla_parse_nested(tb, MT76_TM_ATTR_MAX, attr, testdata_policy);
1386
1387 if (!tb[MT76_TM_ATTR_PRECAL_INFO] && !tb[MT76_TM_ATTR_PRECAL]) {
1388 atenl_info("No Pre cal data or info!\n");
1389 return NL_SKIP;
1390 }
1391
1392 if (tb[MT76_TM_ATTR_PRECAL_INFO]) {
1393 i = 0;
1394 nla_for_each_nested(cur, tb[MT76_TM_ATTR_PRECAL_INFO], rem) {
1395 an->cal_info[i] = (u32) nla_get_u32(cur);
1396 i++;
1397 }
1398 return NL_SKIP;
1399 }
1400
1401 if (tb[MT76_TM_ATTR_PRECAL] && an->cal) {
1402 i = prek_offset;
1403 nla_for_each_nested(cur, tb[MT76_TM_ATTR_PRECAL], rem) {
1404 an->cal[i] = (u8) nla_get_u8(cur);
1405 i++;
1406 }
1407 return NL_SKIP;
1408 }
1409 atenl_info("No data found for pre-cal!\n");
1410
1411 return NL_SKIP;
1412}
1413
1414static int
1415atenl_nl_precal_sync_partition(struct atenl_nl_priv *nl_priv, enum mt76_testmode_attr attr,
1416 int prek_type, int prek_offset)
1417{
1418 int ret;
1419 void *ptr;
1420 struct nl_msg *msg;
1421 struct atenl *an = nl_priv->an;
1422
1423 msg = unl_genl_msg(&(nl_priv->unl), NL80211_CMD_TESTMODE, true);
1424 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
1425 nl_priv->msg = msg;
1426 nl_priv->attr = prek_offset;
1427
1428 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1429 if (!ptr)
1430 return -ENOMEM;
1431
1432 nla_put_flag(msg, attr);
1433 if (attr == MT76_TM_ATTR_PRECAL)
1434 nla_put_u8(msg, MT76_TM_ATTR_PRECAL_INFO, prek_type);
1435 nla_nest_end(msg, ptr);
1436
1437 ret = unl_genl_request(&(nl_priv->unl), msg, atenl_nl_precal_sync_from_driver_cb, (void *)nl_priv);
1438
1439 if (ret) {
1440 atenl_err("command process error!\n");
1441 return ret;
1442 }
1443
1444 return 0;
1445}
1446
1447int atenl_nl_precal_sync_from_driver(struct atenl *an, enum prek_ops ops)
1448{
developer8e1f5142023-03-31 17:43:25 +08001449#define GROUP_IND_MASK BIT(0)
1450#define GROUP_IND_MASK_7996 GENMASK(2, 0)
1451#define DPD_IND_MASK GENMASK(3, 1)
1452#define DPD_IND_MASK_7996 GENMASK(5, 3)
developer454c4652022-08-31 20:39:29 +08001453 int ret;
1454 u32 i, times, group_size, dpd_size, total_size, transmit_size, offs;
developer8e1f5142023-03-31 17:43:25 +08001455 u32 dpd_per_chan_size, dpd_chan_ratio[3], total_ratio;
1456 u32 size, base, base_idx, dpd_base_map, *size_ptr;
1457 u8 cal_indicator, group_ind_mask, dpd_ind_mask, *precal_info;
developer454c4652022-08-31 20:39:29 +08001458 struct atenl_nl_priv nl_priv = { .an = an };
1459
1460 offs = an->eeprom_prek_offs;
1461 cal_indicator = an->eeprom_data[offs];
developere2e8ccc2023-10-13 18:41:57 +08001462 group_ind_mask = is_connac3(an) ? GROUP_IND_MASK_7996 : GROUP_IND_MASK;
1463 dpd_ind_mask = is_connac3(an) ? DPD_IND_MASK_7996 : DPD_IND_MASK;
developer454c4652022-08-31 20:39:29 +08001464
1465 if (cal_indicator) {
1466 precal_info = an->eeprom_data + an->eeprom_size;
1467 memcpy(an->cal_info, precal_info, PRE_CAL_INFO);
1468 group_size = an->cal_info[0];
1469 dpd_size = an->cal_info[1];
1470 total_size = group_size + dpd_size;
developer8e1f5142023-03-31 17:43:25 +08001471 dpd_chan_ratio[0] = (an->cal_info[2] >> DPD_INFO_6G_SHIFT) &
1472 DPD_INFO_MASK;
1473 dpd_chan_ratio[1] = (an->cal_info[2] >> DPD_INFO_5G_SHIFT) &
1474 DPD_INFO_MASK;
1475 dpd_chan_ratio[2] = (an->cal_info[2] >> DPD_INFO_2G_SHIFT) &
1476 DPD_INFO_MASK;
1477 dpd_per_chan_size = (an->cal_info[2] >> DPD_INFO_CH_SHIFT) &
1478 DPD_INFO_MASK;
1479 total_ratio = dpd_chan_ratio[0] + dpd_chan_ratio[1] +
1480 dpd_chan_ratio[2];
developer454c4652022-08-31 20:39:29 +08001481 }
1482
1483 switch (ops){
1484 case PREK_SYNC_ALL:
1485 size_ptr = &total_size;
1486 base_idx = 0;
developer8e1f5142023-03-31 17:43:25 +08001487 dpd_base_map = 0;
developer454c4652022-08-31 20:39:29 +08001488 goto start;
1489 case PREK_SYNC_GROUP:
1490 size_ptr = &group_size;
1491 base_idx = 0;
developer8e1f5142023-03-31 17:43:25 +08001492 dpd_base_map = 0;
developer454c4652022-08-31 20:39:29 +08001493 goto start;
1494 case PREK_SYNC_DPD_6G:
1495 size_ptr = &dpd_size;
1496 base_idx = 0;
developere2e8ccc2023-10-13 18:41:57 +08001497 dpd_base_map = is_connac3(an) ? GENMASK(2, 1) : 0;
developer454c4652022-08-31 20:39:29 +08001498 goto start;
1499 case PREK_SYNC_DPD_5G:
1500 size_ptr = &dpd_size;
1501 base_idx = 1;
developere2e8ccc2023-10-13 18:41:57 +08001502 dpd_base_map = is_connac3(an) ? BIT(2) : BIT(0);
developer454c4652022-08-31 20:39:29 +08001503 goto start;
1504 case PREK_SYNC_DPD_2G:
1505 size_ptr = &dpd_size;
1506 base_idx = 2;
developere2e8ccc2023-10-13 18:41:57 +08001507 dpd_base_map = is_connac3(an) ? 0 : GENMASK(1, 0);
developer454c4652022-08-31 20:39:29 +08001508
1509start:
1510 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1511 atenl_err("Failed to connect to nl80211\n");
1512 return 2;
1513 }
1514
1515 ret = atenl_nl_precal_sync_partition(&nl_priv, MT76_TM_ATTR_PRECAL_INFO, 0, 0);
1516 if (ret || !an->cal_info)
1517 goto out;
1518
1519 group_size = an->cal_info[0];
1520 dpd_size = an->cal_info[1];
1521 total_size = group_size + dpd_size;
developer8e1f5142023-03-31 17:43:25 +08001522 dpd_chan_ratio[0] = (an->cal_info[2] >> DPD_INFO_6G_SHIFT) &
1523 DPD_INFO_MASK;
1524 dpd_chan_ratio[1] = (an->cal_info[2] >> DPD_INFO_5G_SHIFT) &
1525 DPD_INFO_MASK;
1526 dpd_chan_ratio[2] = (an->cal_info[2] >> DPD_INFO_2G_SHIFT) &
1527 DPD_INFO_MASK;
1528 dpd_per_chan_size = (an->cal_info[2] >> DPD_INFO_CH_SHIFT) &
1529 DPD_INFO_MASK;
1530 total_ratio = dpd_chan_ratio[0] + dpd_chan_ratio[1] +
1531 dpd_chan_ratio[2];
developer454c4652022-08-31 20:39:29 +08001532 transmit_size = an->cal_info[3];
1533
1534 size = *size_ptr;
developer8e1f5142023-03-31 17:43:25 +08001535 if (size_ptr == &dpd_size)
1536 size = size / total_ratio * dpd_chan_ratio[base_idx];
1537
developer454c4652022-08-31 20:39:29 +08001538 base = 0;
developer8e1f5142023-03-31 17:43:25 +08001539 for (i = 0; i < 3; i++) {
1540 if (dpd_base_map & BIT(i))
1541 base += dpd_chan_ratio[i] * dpd_per_chan_size *
1542 MT_EE_CAL_UNIT;
developer454c4652022-08-31 20:39:29 +08001543 }
1544 base += (size_ptr == &dpd_size) ? group_size : 0;
1545
1546 if (!an->cal)
1547 an->cal = (u8 *) calloc(size, sizeof(u8));
1548 times = size / transmit_size + 1;
1549 for (i = 0; i < times; i++) {
1550 ret = atenl_nl_precal_sync_partition(&nl_priv, MT76_TM_ATTR_PRECAL, ops,
1551 i * transmit_size);
1552 if (ret)
1553 goto out;
1554 }
1555
1556 ret = atenl_eeprom_update_precal(an, base, size);
1557 break;
1558 case PREK_CLEAN_GROUP:
developer8e1f5142023-03-31 17:43:25 +08001559 if (!(cal_indicator & group_ind_mask))
developer454c4652022-08-31 20:39:29 +08001560 return 0;
developer8e1f5142023-03-31 17:43:25 +08001561 an->cal_info[4] = cal_indicator & group_ind_mask;
developer454c4652022-08-31 20:39:29 +08001562 ret = atenl_eeprom_update_precal(an, 0, group_size);
1563 break;
1564 case PREK_CLEAN_DPD:
developer8e1f5142023-03-31 17:43:25 +08001565 if (!(cal_indicator & dpd_ind_mask))
developer454c4652022-08-31 20:39:29 +08001566 return 0;
developer8e1f5142023-03-31 17:43:25 +08001567 an->cal_info[4] = cal_indicator & dpd_ind_mask;
developer454c4652022-08-31 20:39:29 +08001568 ret = atenl_eeprom_update_precal(an, group_size, dpd_size);
1569 break;
1570 default:
developerc2309682024-11-15 19:28:46 +08001571 ret = -EINVAL;
developer454c4652022-08-31 20:39:29 +08001572 break;
1573 }
1574
1575out:
1576 unl_free(&nl_priv.unl);
1577 return ret;
1578}