blob: baf2944cc033196c7d0a081523803764e8bb39d3 [file] [log] [blame]
developerb9b4cd12022-10-11 13:18:59 +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 },
developer13655da2023-01-10 19:53:25 +080029 [MT76_TM_ATTR_BAND_IDX] = { .type = NLA_U8 },
developerbd9fa1e2023-10-16 11:04:00 +080030 [MT76_TM_ATTR_SKU_EN] = { .type = NLA_U8 },
developerb9b4cd12022-10-11 13:18:59 +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 },
44 [MT76_TM_ATTR_PRECAL] = { .type = NLA_NESTED },
45 [MT76_TM_ATTR_PRECAL_INFO] = { .type = NLA_NESTED },
46};
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 },
developerbd9fa1e2023-10-16 11:04:00 +080059 [MT76_TM_RX_ATTR_RSSI] = { .type = NLA_NESTED },
developerb9b4cd12022-10-11 13:18:59 +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,
developer7af0f762023-05-22 15:16:16 +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,
developerb9b4cd12022-10-11 13:18:59 +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;
developer17bb0a82022-12-13 15:52:04 +0800133
134 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, tx_antenna);
developerb9b4cd12022-10-11 13:18:59 +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;
221 u8 def_mac[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
222 void *ptr, *a;
223
224 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
229 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
230 if (!ptr)
231 return -ENOMEM;
232
233 a = nla_nest_start(msg, MT76_TM_ATTR_MAC_ADDRS);
234 if (!a)
235 return -ENOMEM;
236
237 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);
240
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
292 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));
developer7af0f762023-05-22 15:16:16 +0800295 else if (get_band_val(an, band, tx_mpdu_len))
developerb9b4cd12022-10-11 13:18:59 +0800296 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH,
297 get_band_val(an, band, tx_mpdu_len));
298
299 /* 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
developer2299de92023-10-27 15:40:47 +0800312 if (!is_connac3(an) && tx_rate_mode >= MT76_TM_TX_MODE_HE_SU) {
developerb9b4cd12022-10-11 13:18:59 +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]));
developer2299de92023-10-27 15:40:47 +0800372 if (is_connac3(an)) {
developer7af0f762023-05-22 15:16:16 +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 }
developerb9b4cd12022-10-11 13:18:59 +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
423 *(u32 *)(hdr->data + 2) = data->ext_id;
424
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) {
439 atenl_err("Testdata attribute not found\n");
440 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) {
590 atenl_err("Testdata attribute not found\n");
591 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
603 if (!anb->reset_rx_cnt ||
604 get_band_val(an, an->cur_band, cur_state) == MT76_TM_STATE_RX_FRAMES) {
605#define RX_COUNT_DIFF(_field) \
606 rx_diff._field = (rx_cur._field) - (anb->rx_stat._field);
607 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) {
710 atenl_err("Failed to connect to nl80211\n");
711 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);
729 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH, tx_length);
730
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
747static 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
developerf9b00212023-07-31 12:27:06 +0800831void
developerb9b4cd12022-10-11 13:18:59 +0800832atenl_get_ibf_cal_result(struct atenl *an)
833{
developerbd9fa1e2023-10-16 11:04:00 +0800834 u16 offset, group_size = 40;
developerb9b4cd12022-10-11 13:18:59 +0800835
836 if (an->adie_id == 0x7975)
837 offset = 0x651;
838 else if (an->adie_id == 0x7976)
839 offset = 0x60a;
840
developerbd9fa1e2023-10-16 11:04:00 +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);
developerb9b4cd12022-10-11 13:18:59 +0800848}
849
850static int
851atenl_nl_ibf_set_val(struct atenl *an, struct atenl_data *data,
852 struct atenl_nl_priv *nl_priv)
853{
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]);
866 u16 val[8], is_atenl = 1;
867 u8 tmp_ant;
868 void *ptr, *a;
869 char cmd[64];
870 int i;
871
872 for (i = 0; i < 8; i++)
873 val[i] = ntohl(v[i + 1]);
874
875 atenl_dbg("%s: action = %u, val = %u, %u, %u, %u, %u\n",
876 __func__, action, val[0], val[1], val[2], val[3], val[4]);
877
878 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
879 if (!ptr)
880 return -ENOMEM;
881
882 switch (action) {
883 case TXBF_ACT_CHANNEL:
884 an->cur_band = val[1];
885 /* a sanity to prevent script band idx error */
886 if (val[0] > 14)
887 an->cur_band = 1;
888 atenl_nl_ibf_init(an, an->cur_band);
889 atenl_set_channel(an, 0, an->cur_band, val[0], 0, 0);
890
891 nla_put_u8(msg, MT76_TM_ATTR_AID, 0);
892 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_UPDATE_CH);
893 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
894 if (!a)
895 return -ENOMEM;
896 nla_put_u16(msg, 0, 0);
897 nla_nest_end(msg, a);
898 break;
899 case TXBF_ACT_MCS:
900 tmp_ant = (1 << DIV_ROUND_UP(val[0], 8)) - 1 ?: 1;
901 /* sometimes the correct band idx will be set after this action,
902 * so maintain a temp variable to allow mcs update in anthor action.
903 */
904 an->ibf_mcs = val[0];
developerb9b4cd12022-10-11 13:18:59 +0800905 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, an->ibf_mcs);
developer2299de92023-10-27 15:40:47 +0800906 if (!is_connac3(an)) {
developerbd9fa1e2023-10-16 11:04:00 +0800907 an->ibf_ant = tmp_ant;
908 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, an->ibf_ant);
909 }
developerb9b4cd12022-10-11 13:18:59 +0800910 break;
911 case TXBF_ACT_TX_ANT:
912 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, val[0]);
913 break;
914 case TXBF_ACT_RX_START:
915 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_RX_FRAMES);
916 break;
917 case TXBF_ACT_RX_ANT:
918 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, val[0]);
919 break;
920 case TXBF_ACT_TX_PKT:
921 nla_put_u8(msg, MT76_TM_ATTR_AID, val[1]);
922 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_TX_PREP);
developer7af0f762023-05-22 15:16:16 +0800923 if (!val[2])
924 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, 0xFFFFFFFF);
925 else
926 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, val[2]);
developerb9b4cd12022-10-11 13:18:59 +0800927 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH, 1024);
928 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
929 if (!a)
930 return -ENOMEM;
931
932 for (i = 0; i < 5; i++)
933 nla_put_u16(msg, i, val[i]);
934 nla_nest_end(msg, a);
935
936 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_TX_FRAMES);
937 break;
938 case TXBF_ACT_IBF_PHASE_COMP:
939 nla_put_u8(msg, MT76_TM_ATTR_AID, 1);
940 case TXBF_ACT_IBF_PROF_UPDATE:
941 case TXBF_ACT_EBF_PROF_UPDATE:
942 case TXBF_ACT_IBF_PHASE_CAL:
943 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, bf_act_map[action]);
944 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
945 if (!a)
946 return -ENOMEM;
developerbd9fa1e2023-10-16 11:04:00 +0800947 /* Note: litepoint may send random number for lna_gain_level,
948 * reset to 1 (mid gain) and 0 for wifi 7 and wifi 6, respectively
949 */
developerb9b4cd12022-10-11 13:18:59 +0800950 if (action == TXBF_ACT_IBF_PHASE_CAL)
developer2299de92023-10-27 15:40:47 +0800951 val[4] = is_connac3(an) ? 1 : 0;
developerb9b4cd12022-10-11 13:18:59 +0800952 for (i = 0; i < 5; i++)
953 nla_put_u16(msg, i, val[i]);
954 /* Used to distinguish between command mode and HQADLL mode */
955 nla_put_u16(msg, 5, is_atenl);
956 nla_nest_end(msg, a);
957 break;
958 case TXBF_ACT_IBF_PHASE_E2P_UPDATE:
959 atenl_nl_ibf_e2p_update(an);
960 atenl_get_ibf_cal_result(an);
961
962 nla_put_u8(msg, MT76_TM_ATTR_AID, 0);
963 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_INIT);
964
965 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
966 if (!a)
967 return -ENOMEM;
968 nla_put_u16(msg, 0, 0);
969 nla_nest_end(msg, a);
970 break;
971 case TXBF_ACT_INIT:
972 case TXBF_ACT_POWER:
973 default:
974 break;
975 }
976
977 nla_nest_end(msg, ptr);
978
979 *(u32 *)(hdr->data + 2) = data->ext_id;
980
981 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
982}
983
984static int
985atenl_nl_ibf_get_status(struct atenl *an, struct atenl_data *data,
986 struct atenl_nl_priv *nl_priv)
987{
988 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
989 u32 status = htonl(1);
990
991 *(u32 *)(hdr->data + 2) = data->ext_id;
992 memcpy(hdr->data + 6, &status, 4);
993
994 return 0;
995}
996
997static int
998atenl_nl_ibf_profile_update_all(struct atenl *an, struct atenl_data *data,
999 struct atenl_nl_priv *nl_priv)
1000{
1001 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
1002 struct nl_msg *msg;
1003 void *ptr, *a;
1004 u32 *v = (u32 *)(hdr->data + 4);
1005 u16 pfmu_idx = ntohl(v[0]);
1006 int i;
1007
1008 for (i = 0, v = &v[5]; i < 64; i++, v += 5) {
1009 int j;
1010
1011 if (unl_genl_init(&nl_priv->unl, "nl80211") < 0) {
1012 atenl_err("Failed to connect to nl80211\n");
1013 return 2;
1014 }
1015
1016 msg = unl_genl_msg(&nl_priv->unl, NL80211_CMD_TESTMODE, false);
1017 nla_put_u32(msg, NL80211_ATTR_WIPHY,
1018 get_band_val(an, an->cur_band, phy_idx));
1019
1020 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1021 if (!ptr)
1022 return -ENOMEM;
1023
1024 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_PROF_UPDATE_ALL);
1025 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
1026 if (!a)
1027 return -ENOMEM;
1028 nla_put_u16(msg, 0, pfmu_idx);
1029
1030 for (j = 0; j < 5; j++)
1031 nla_put_u16(msg, j + 1, ntohl(v[j]));
1032 nla_nest_end(msg, a);
1033
1034 nla_nest_end(msg, ptr);
1035
1036 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
1037
1038 unl_free(&nl_priv->unl);
1039 }
1040
1041 *(u32 *)(hdr->data + 2) = data->ext_id;
1042
1043 return 0;
1044}
1045
1046#define NL_OPS_GROUP(cmd, ...) [HQA_CMD_##cmd] = { __VA_ARGS__ }
1047static const struct atenl_nl_ops nl_ops[] = {
1048 NL_OPS_GROUP(SET_TX_PATH, .set=MT76_TM_ATTR_TX_ANTENNA),
1049 NL_OPS_GROUP(SET_TX_POWER, .set=MT76_TM_ATTR_TX_POWER),
1050 NL_OPS_GROUP(SET_RX_PATH, .set=MT76_TM_ATTR_TX_ANTENNA),
1051 NL_OPS_GROUP(SET_FREQ_OFFSET, .set=MT76_TM_ATTR_FREQ_OFFSET),
1052 NL_OPS_GROUP(SET_CFG, .ops=atenl_nl_set_cfg),
1053 NL_OPS_GROUP(SET_TSSI, .ops=atenl_nl_set_cfg),
1054 NL_OPS_GROUP(CONTINUOUS_TX, .ops=atenl_nl_continuous_tx),
1055 NL_OPS_GROUP(GET_TX_INFO, .dump=MT76_TM_STATS_ATTR_TX_DONE),
1056 NL_OPS_GROUP(GET_RX_INFO, .ops=atenl_nl_get_rx_info, .dump=true),
1057 NL_OPS_GROUP(SET_RU, .ops=atenl_nl_set_ru),
1058};
1059#undef NL_OPS_GROUP
1060
1061#define NL_OPS_EXT(cmd, ...) [HQA_EXT_CMD_##cmd] = { __VA_ARGS__ }
1062static const struct atenl_nl_ops nl_ops_ext[] = {
1063 NL_OPS_EXT(SET_TX, .ops=atenl_nl_set_tx),
1064 NL_OPS_EXT(START_TX, .ops=atenl_nl_tx),
1065 NL_OPS_EXT(STOP_TX, .ops=atenl_nl_tx),
1066 NL_OPS_EXT(START_RX, .ops=atenl_nl_rx),
1067 NL_OPS_EXT(STOP_RX, .ops=atenl_nl_rx),
1068 NL_OPS_EXT(OFF_CH_SCAN, .ops=atenl_off_ch_scan),
1069 NL_OPS_EXT(IBF_SET_VAL, .ops=atenl_nl_ibf_set_val),
1070 NL_OPS_EXT(IBF_GET_STATUS, .ops=atenl_nl_ibf_get_status),
1071 NL_OPS_EXT(IBF_PROF_UPDATE_ALL, .ops=atenl_nl_ibf_profile_update_all),
1072};
1073#undef NL_OPS_EXT
1074
1075int atenl_nl_process(struct atenl *an, struct atenl_data *data)
1076{
1077 struct atenl_nl_priv nl_priv = {};
1078 const struct atenl_nl_ops *ops;
1079 struct nl_msg *msg;
1080 int ret = 0;
1081
1082 if (data->ext_cmd != 0)
1083 ops = &nl_ops_ext[data->ext_cmd];
1084 else
1085 ops = &nl_ops[data->cmd];
1086
1087 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1088 atenl_err("Failed to connect to nl80211\n");
1089 return -1;
1090 }
1091
1092 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, !!ops->dump);
1093 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
1094 nl_priv.msg = msg;
1095
1096 if (ops->ops) {
1097 ret = ops->ops(an, data, &nl_priv);
1098 } else if (ops->dump) {
1099 nl_priv.attr = ops->dump;
1100 ret = atenl_nl_dump_attr(an, data, &nl_priv);
1101 } else {
1102 nl_priv.attr = ops->set;
1103 ret = atenl_nl_set_attr(an, data, &nl_priv);
1104 }
1105
1106 if (ret)
1107 atenl_err("command process error: 0x%x (0x%x)\n", data->cmd_id, data->ext_id);
1108
1109 unl_free(&nl_priv.unl);
1110
1111 return ret;
1112}
1113
1114int atenl_nl_process_many(struct atenl *an, struct atenl_data *data)
1115{
1116 struct atenl_nl_priv nl_priv = {};
1117 const struct atenl_nl_ops *ops;
1118 int ret = 0;
1119
1120 if (data->ext_cmd != 0)
1121 ops = &nl_ops_ext[data->ext_cmd];
1122 else
1123 ops = &nl_ops[data->cmd];
1124
1125 if (ops->ops)
1126 ret = ops->ops(an, data, &nl_priv);
1127
1128 return ret;
1129}
1130
1131int atenl_nl_set_state(struct atenl *an, u8 band,
1132 enum mt76_testmode_state state)
1133{
1134 struct atenl_nl_priv nl_priv = {};
1135 struct nl_msg *msg;
1136 void *ptr;
1137
1138 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1139 atenl_err("Failed to connect to nl80211\n");
1140 return 2;
1141 }
1142
1143 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1144 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
1145
1146 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1147 if (!ptr)
1148 return -ENOMEM;
1149
1150 atenl_set_attr_state(an, msg, band, state);
1151
1152 nla_nest_end(msg, ptr);
1153
1154 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1155
1156 unl_free(&nl_priv.unl);
1157
1158 return 0;
1159}
1160
1161int atenl_nl_set_aid(struct atenl *an, u8 band, u8 aid)
1162{
1163 struct atenl_nl_priv nl_priv = {};
1164 struct nl_msg *msg;
1165 void *ptr;
1166
1167 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1168 atenl_err("Failed to connect to nl80211\n");
1169 return 2;
1170 }
1171
1172 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1173 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
1174
1175 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1176 if (!ptr)
1177 return -ENOMEM;
1178
1179 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
1180
1181 nla_nest_end(msg, ptr);
1182
1183 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1184
1185 unl_free(&nl_priv.unl);
1186
1187 return 0;
1188}
1189
1190static int atenl_nl_check_mtd_cb(struct nl_msg *msg, void *arg)
1191{
1192 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
1193 struct atenl *an = nl_priv->an;
1194 struct nlattr *tb[NUM_MT76_TM_ATTRS];
1195 struct nlattr *attr;
1196
1197 attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
1198 if (!attr)
1199 return NL_SKIP;
1200
1201 nla_parse_nested(tb, MT76_TM_ATTR_MAX, attr, testdata_policy);
developer9237f442024-06-14 17:13:04 +08001202 an->band_idx = nla_get_u32(tb[MT76_TM_ATTR_BAND_IDX]);
1203
developerb9b4cd12022-10-11 13:18:59 +08001204 if (!tb[MT76_TM_ATTR_MTD_PART] || !tb[MT76_TM_ATTR_MTD_OFFSET])
1205 return NL_SKIP;
1206
1207 an->mtd_part = strdup(nla_get_string(tb[MT76_TM_ATTR_MTD_PART]));
1208 an->mtd_offset = nla_get_u32(tb[MT76_TM_ATTR_MTD_OFFSET]);
developerb9b4cd12022-10-11 13:18:59 +08001209
1210 return NL_SKIP;
1211}
1212
1213int atenl_nl_check_mtd(struct atenl *an)
1214{
1215 struct atenl_nl_priv nl_priv = { .an = an };
1216 struct nl_msg *msg;
1217
1218 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1219 atenl_err("Failed to connect to nl80211\n");
1220 return 2;
1221 }
1222
1223 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, true);
1224 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1225 unl_genl_request(&nl_priv.unl, msg, atenl_nl_check_mtd_cb, (void *)&nl_priv);
1226
1227 unl_free(&nl_priv.unl);
1228
1229 return 0;
1230}
1231
1232int atenl_nl_write_eeprom(struct atenl *an, u32 offset, u8 *val, int len)
1233{
1234 struct atenl_nl_priv nl_priv = {};
1235 struct nl_msg *msg;
1236 void *ptr, *a;
1237 int i;
1238
1239 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1240 atenl_err("Failed to connect to nl80211\n");
1241 return 2;
1242 }
1243
1244 if (len > 16)
1245 return -EINVAL;
1246
1247 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1248 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1249
1250 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1251 if (!ptr)
1252 return -ENOMEM;
1253
1254 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1255 MT76_TM_EEPROM_ACTION_UPDATE_DATA);
1256 nla_put_u32(msg, MT76_TM_ATTR_EEPROM_OFFSET, offset);
1257
1258 a = nla_nest_start(msg, MT76_TM_ATTR_EEPROM_VAL);
1259 if (!a)
1260 return -ENOMEM;
1261
1262 for (i = 0; i < len; i++)
1263 if (nla_put_u8(msg, i, val[i]))
1264 goto out;
1265
1266 nla_nest_end(msg, a);
1267
1268 nla_nest_end(msg, ptr);
1269
1270 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1271
1272 unl_free(&nl_priv.unl);
1273
1274out:
1275 return 0;
1276}
1277
1278int atenl_nl_write_efuse_all(struct atenl *an)
1279{
1280 struct atenl_nl_priv nl_priv = {};
1281 struct nl_msg *msg;
1282 void *ptr;
1283
1284 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1285 atenl_err("Failed to connect to nl80211\n");
1286 return 2;
1287 }
1288
1289 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1290 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1291
1292 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1293 if (!ptr)
1294 return -ENOMEM;
1295
1296 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1297 MT76_TM_EEPROM_ACTION_WRITE_TO_EFUSE);
1298
1299 nla_nest_end(msg, ptr);
1300
1301 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1302
1303 unl_free(&nl_priv.unl);
1304
1305 return 0;
1306}
1307
1308int atenl_nl_update_buffer_mode(struct atenl *an)
1309{
1310 struct atenl_nl_priv nl_priv = {};
1311 struct nl_msg *msg;
1312 void *ptr;
1313
1314 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1315 atenl_err("Failed to connect to nl80211\n");
1316 return 2;
1317 }
1318
1319 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1320 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1321
1322 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1323 if (!ptr)
1324 return -ENOMEM;
1325
1326 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1327 MT76_TM_EEPROM_ACTION_UPDATE_BUFFER_MODE);
1328
1329 nla_nest_end(msg, ptr);
1330
1331 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1332
1333 unl_free(&nl_priv.unl);
1334
1335 return 0;
1336}
1337
1338static int atenl_nl_precal_sync_from_driver_cb(struct nl_msg *msg, void *arg)
1339{
1340 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
1341 struct atenl *an = nl_priv->an;
1342 struct nlattr *tb[NUM_MT76_TM_ATTRS];
1343 struct nlattr *attr, *cur;
1344 int i, rem, prek_offset = nl_priv->attr;
1345
1346
1347 attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
1348 if (!attr)
1349 return NL_SKIP;
1350
1351 nla_parse_nested(tb, MT76_TM_ATTR_MAX, attr, testdata_policy);
1352
1353 if (!tb[MT76_TM_ATTR_PRECAL_INFO] && !tb[MT76_TM_ATTR_PRECAL]) {
1354 atenl_info("No Pre cal data or info!\n");
1355 return NL_SKIP;
1356 }
1357
1358 if (tb[MT76_TM_ATTR_PRECAL_INFO]) {
1359 i = 0;
1360 nla_for_each_nested(cur, tb[MT76_TM_ATTR_PRECAL_INFO], rem) {
1361 an->cal_info[i] = (u32) nla_get_u32(cur);
1362 i++;
1363 }
1364 return NL_SKIP;
1365 }
1366
1367 if (tb[MT76_TM_ATTR_PRECAL] && an->cal) {
1368 i = prek_offset;
1369 nla_for_each_nested(cur, tb[MT76_TM_ATTR_PRECAL], rem) {
1370 an->cal[i] = (u8) nla_get_u8(cur);
1371 i++;
1372 }
1373 return NL_SKIP;
1374 }
1375 atenl_info("No data found for pre-cal!\n");
1376
1377 return NL_SKIP;
1378}
1379
1380static int
1381atenl_nl_precal_sync_partition(struct atenl_nl_priv *nl_priv, enum mt76_testmode_attr attr,
1382 int prek_type, int prek_offset)
1383{
1384 int ret;
1385 void *ptr;
1386 struct nl_msg *msg;
1387 struct atenl *an = nl_priv->an;
1388
1389 msg = unl_genl_msg(&(nl_priv->unl), NL80211_CMD_TESTMODE, true);
1390 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
1391 nl_priv->msg = msg;
1392 nl_priv->attr = prek_offset;
1393
1394 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1395 if (!ptr)
1396 return -ENOMEM;
1397
1398 nla_put_flag(msg, attr);
1399 if (attr == MT76_TM_ATTR_PRECAL)
1400 nla_put_u8(msg, MT76_TM_ATTR_PRECAL_INFO, prek_type);
1401 nla_nest_end(msg, ptr);
1402
1403 ret = unl_genl_request(&(nl_priv->unl), msg, atenl_nl_precal_sync_from_driver_cb, (void *)nl_priv);
1404
1405 if (ret) {
1406 atenl_err("command process error!\n");
1407 return ret;
1408 }
1409
1410 return 0;
1411}
1412
1413int atenl_nl_precal_sync_from_driver(struct atenl *an, enum prek_ops ops)
1414{
developer963a66b2023-04-11 13:34:56 +08001415#define GROUP_IND_MASK BIT(0)
1416#define GROUP_IND_MASK_7996 GENMASK(2, 0)
1417#define DPD_IND_MASK GENMASK(3, 1)
1418#define DPD_IND_MASK_7996 GENMASK(5, 3)
developerb9b4cd12022-10-11 13:18:59 +08001419 int ret;
1420 u32 i, times, group_size, dpd_size, total_size, transmit_size, offs;
developer963a66b2023-04-11 13:34:56 +08001421 u32 dpd_per_chan_size, dpd_chan_ratio[3], total_ratio;
1422 u32 size, base, base_idx, dpd_base_map, *size_ptr;
1423 u8 cal_indicator, group_ind_mask, dpd_ind_mask, *precal_info;
developerb9b4cd12022-10-11 13:18:59 +08001424 struct atenl_nl_priv nl_priv = { .an = an };
1425
1426 offs = an->eeprom_prek_offs;
1427 cal_indicator = an->eeprom_data[offs];
developer2299de92023-10-27 15:40:47 +08001428 group_ind_mask = is_connac3(an) ? GROUP_IND_MASK_7996 : GROUP_IND_MASK;
1429 dpd_ind_mask = is_connac3(an) ? DPD_IND_MASK_7996 : DPD_IND_MASK;
developerb9b4cd12022-10-11 13:18:59 +08001430
1431 if (cal_indicator) {
1432 precal_info = an->eeprom_data + an->eeprom_size;
1433 memcpy(an->cal_info, precal_info, PRE_CAL_INFO);
1434 group_size = an->cal_info[0];
1435 dpd_size = an->cal_info[1];
1436 total_size = group_size + dpd_size;
developer963a66b2023-04-11 13:34:56 +08001437 dpd_chan_ratio[0] = (an->cal_info[2] >> DPD_INFO_6G_SHIFT) &
1438 DPD_INFO_MASK;
1439 dpd_chan_ratio[1] = (an->cal_info[2] >> DPD_INFO_5G_SHIFT) &
1440 DPD_INFO_MASK;
1441 dpd_chan_ratio[2] = (an->cal_info[2] >> DPD_INFO_2G_SHIFT) &
1442 DPD_INFO_MASK;
1443 dpd_per_chan_size = (an->cal_info[2] >> DPD_INFO_CH_SHIFT) &
1444 DPD_INFO_MASK;
1445 total_ratio = dpd_chan_ratio[0] + dpd_chan_ratio[1] +
1446 dpd_chan_ratio[2];
developerb9b4cd12022-10-11 13:18:59 +08001447 }
1448
1449 switch (ops){
1450 case PREK_SYNC_ALL:
1451 size_ptr = &total_size;
1452 base_idx = 0;
developer963a66b2023-04-11 13:34:56 +08001453 dpd_base_map = 0;
developerb9b4cd12022-10-11 13:18:59 +08001454 goto start;
1455 case PREK_SYNC_GROUP:
1456 size_ptr = &group_size;
1457 base_idx = 0;
developer963a66b2023-04-11 13:34:56 +08001458 dpd_base_map = 0;
developerb9b4cd12022-10-11 13:18:59 +08001459 goto start;
1460 case PREK_SYNC_DPD_6G:
1461 size_ptr = &dpd_size;
1462 base_idx = 0;
developer2299de92023-10-27 15:40:47 +08001463 dpd_base_map = is_connac3(an) ? GENMASK(2, 1) : 0;
developerb9b4cd12022-10-11 13:18:59 +08001464 goto start;
1465 case PREK_SYNC_DPD_5G:
1466 size_ptr = &dpd_size;
1467 base_idx = 1;
developer2299de92023-10-27 15:40:47 +08001468 dpd_base_map = is_connac3(an) ? BIT(2) : BIT(0);
developerb9b4cd12022-10-11 13:18:59 +08001469 goto start;
1470 case PREK_SYNC_DPD_2G:
1471 size_ptr = &dpd_size;
1472 base_idx = 2;
developer2299de92023-10-27 15:40:47 +08001473 dpd_base_map = is_connac3(an) ? 0 : GENMASK(1, 0);
developerb9b4cd12022-10-11 13:18:59 +08001474
1475start:
1476 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1477 atenl_err("Failed to connect to nl80211\n");
1478 return 2;
1479 }
1480
1481 ret = atenl_nl_precal_sync_partition(&nl_priv, MT76_TM_ATTR_PRECAL_INFO, 0, 0);
1482 if (ret || !an->cal_info)
1483 goto out;
1484
1485 group_size = an->cal_info[0];
1486 dpd_size = an->cal_info[1];
1487 total_size = group_size + dpd_size;
developer963a66b2023-04-11 13:34:56 +08001488 dpd_chan_ratio[0] = (an->cal_info[2] >> DPD_INFO_6G_SHIFT) &
1489 DPD_INFO_MASK;
1490 dpd_chan_ratio[1] = (an->cal_info[2] >> DPD_INFO_5G_SHIFT) &
1491 DPD_INFO_MASK;
1492 dpd_chan_ratio[2] = (an->cal_info[2] >> DPD_INFO_2G_SHIFT) &
1493 DPD_INFO_MASK;
1494 dpd_per_chan_size = (an->cal_info[2] >> DPD_INFO_CH_SHIFT) &
1495 DPD_INFO_MASK;
1496 total_ratio = dpd_chan_ratio[0] + dpd_chan_ratio[1] +
1497 dpd_chan_ratio[2];
developerb9b4cd12022-10-11 13:18:59 +08001498 transmit_size = an->cal_info[3];
1499
1500 size = *size_ptr;
developer963a66b2023-04-11 13:34:56 +08001501 if (size_ptr == &dpd_size)
1502 size = size / total_ratio * dpd_chan_ratio[base_idx];
1503
developerb9b4cd12022-10-11 13:18:59 +08001504 base = 0;
developer963a66b2023-04-11 13:34:56 +08001505 for (i = 0; i < 3; i++) {
1506 if (dpd_base_map & BIT(i))
1507 base += dpd_chan_ratio[i] * dpd_per_chan_size *
1508 MT_EE_CAL_UNIT;
developerb9b4cd12022-10-11 13:18:59 +08001509 }
1510 base += (size_ptr == &dpd_size) ? group_size : 0;
1511
1512 if (!an->cal)
1513 an->cal = (u8 *) calloc(size, sizeof(u8));
1514 times = size / transmit_size + 1;
1515 for (i = 0; i < times; i++) {
1516 ret = atenl_nl_precal_sync_partition(&nl_priv, MT76_TM_ATTR_PRECAL, ops,
1517 i * transmit_size);
1518 if (ret)
1519 goto out;
1520 }
1521
1522 ret = atenl_eeprom_update_precal(an, base, size);
1523 break;
1524 case PREK_CLEAN_GROUP:
developer963a66b2023-04-11 13:34:56 +08001525 if (!(cal_indicator & group_ind_mask))
developerb9b4cd12022-10-11 13:18:59 +08001526 return 0;
developer963a66b2023-04-11 13:34:56 +08001527 an->cal_info[4] = cal_indicator & group_ind_mask;
developerb9b4cd12022-10-11 13:18:59 +08001528 ret = atenl_eeprom_update_precal(an, 0, group_size);
1529 break;
1530 case PREK_CLEAN_DPD:
developer963a66b2023-04-11 13:34:56 +08001531 if (!(cal_indicator & dpd_ind_mask))
developerb9b4cd12022-10-11 13:18:59 +08001532 return 0;
developer963a66b2023-04-11 13:34:56 +08001533 an->cal_info[4] = cal_indicator & dpd_ind_mask;
developerb9b4cd12022-10-11 13:18:59 +08001534 ret = atenl_eeprom_update_precal(an, group_size, dpd_size);
1535 break;
1536 default:
1537 break;
1538 }
1539
1540out:
1541 unl_free(&nl_priv.unl);
1542 return ret;
1543}