blob: b9193565fc10685e48dc8044c535934809909832 [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 },
developer454c4652022-08-31 20:39:29 +080029 [MT76_TM_ATTR_IS_MAIN_PHY] = { .type = NLA_U8 },
developer22c7ab62022-01-24 11:13:32 +080030 [MT76_TM_ATTR_TX_COUNT] = { .type = NLA_U32 },
31 [MT76_TM_ATTR_TX_LENGTH] = { .type = NLA_U32 },
32 [MT76_TM_ATTR_TX_RATE_MODE] = { .type = NLA_U8 },
33 [MT76_TM_ATTR_TX_RATE_NSS] = { .type = NLA_U8 },
34 [MT76_TM_ATTR_TX_RATE_IDX] = { .type = NLA_U8 },
35 [MT76_TM_ATTR_TX_RATE_SGI] = { .type = NLA_U8 },
36 [MT76_TM_ATTR_TX_RATE_LDPC] = { .type = NLA_U8 },
37 [MT76_TM_ATTR_TX_RATE_STBC] = { .type = NLA_U8 },
38 [MT76_TM_ATTR_TX_LTF] = { .type = NLA_U8 },
39 [MT76_TM_ATTR_TX_POWER_CONTROL] = { .type = NLA_U8 },
40 [MT76_TM_ATTR_TX_ANTENNA] = { .type = NLA_U8 },
41 [MT76_TM_ATTR_FREQ_OFFSET] = { .type = NLA_U32 },
42 [MT76_TM_ATTR_STATS] = { .type = NLA_NESTED },
developer454c4652022-08-31 20:39:29 +080043 [MT76_TM_ATTR_PRECAL] = { .type = NLA_NESTED },
44 [MT76_TM_ATTR_PRECAL_INFO] = { .type = NLA_NESTED },
developer22c7ab62022-01-24 11:13:32 +080045};
46
47static struct nla_policy stats_policy[NUM_MT76_TM_STATS_ATTRS] = {
48 [MT76_TM_STATS_ATTR_TX_PENDING] = { .type = NLA_U32 },
49 [MT76_TM_STATS_ATTR_TX_QUEUED] = { .type = NLA_U32 },
50 [MT76_TM_STATS_ATTR_TX_DONE] = { .type = NLA_U32 },
51 [MT76_TM_STATS_ATTR_RX_PACKETS] = { .type = NLA_U64 },
52 [MT76_TM_STATS_ATTR_RX_FCS_ERROR] = { .type = NLA_U64 },
53};
54
55static struct nla_policy rx_policy[NUM_MT76_TM_RX_ATTRS] = {
56 [MT76_TM_RX_ATTR_FREQ_OFFSET] = { .type = NLA_U32 },
57 [MT76_TM_RX_ATTR_RCPI] = { .type = NLA_NESTED },
58 [MT76_TM_RX_ATTR_IB_RSSI] = { .type = NLA_NESTED },
59 [MT76_TM_RX_ATTR_WB_RSSI] = { .type = NLA_NESTED },
60 [MT76_TM_RX_ATTR_SNR] = { .type = NLA_U8 },
61};
62
63struct he_sgi {
64 enum mt76_testmode_tx_mode tx_mode;
65 u8 sgi;
66 u8 tx_ltf;
67};
68
69#define HE_SGI_GROUP(_tx_mode, _sgi, _tx_ltf) \
70 { .tx_mode = MT76_TM_TX_MODE_##_tx_mode, .sgi = _sgi, .tx_ltf = _tx_ltf }
71static const struct he_sgi he_sgi_groups[] = {
72 HE_SGI_GROUP(HE_SU, 0, 0),
73 HE_SGI_GROUP(HE_SU, 0, 1),
74 HE_SGI_GROUP(HE_SU, 1, 1),
75 HE_SGI_GROUP(HE_SU, 2, 2),
76 HE_SGI_GROUP(HE_SU, 0, 2),
77 HE_SGI_GROUP(HE_EXT_SU, 0, 0),
78 HE_SGI_GROUP(HE_EXT_SU, 0, 1),
79 HE_SGI_GROUP(HE_EXT_SU, 1, 1),
80 HE_SGI_GROUP(HE_EXT_SU, 2, 2),
81 HE_SGI_GROUP(HE_EXT_SU, 0, 2),
82 HE_SGI_GROUP(HE_TB, 1, 0),
83 HE_SGI_GROUP(HE_TB, 1, 1),
84 HE_SGI_GROUP(HE_TB, 2, 2),
85 HE_SGI_GROUP(HE_MU, 0, 2),
86 HE_SGI_GROUP(HE_MU, 0, 1),
87 HE_SGI_GROUP(HE_MU, 1, 1),
88 HE_SGI_GROUP(HE_MU, 2, 2),
89};
90#undef HE_SGI_LTF_GROUP
91
92static u8 phy_type_to_attr(u8 phy_type)
93{
94 static const u8 phy_type_to_attr[] = {
95 [ATENL_PHY_TYPE_CCK] = MT76_TM_TX_MODE_CCK,
96 [ATENL_PHY_TYPE_OFDM] = MT76_TM_TX_MODE_OFDM,
97 [ATENL_PHY_TYPE_HT] = MT76_TM_TX_MODE_HT,
98 [ATENL_PHY_TYPE_HT_GF] = MT76_TM_TX_MODE_HT,
99 [ATENL_PHY_TYPE_VHT] = MT76_TM_TX_MODE_VHT,
100 [ATENL_PHY_TYPE_HE_SU] = MT76_TM_TX_MODE_HE_SU,
101 [ATENL_PHY_TYPE_HE_EXT_SU] = MT76_TM_TX_MODE_HE_EXT_SU,
102 [ATENL_PHY_TYPE_HE_TB] = MT76_TM_TX_MODE_HE_TB,
103 [ATENL_PHY_TYPE_HE_MU] = MT76_TM_TX_MODE_HE_MU,
104 };
105
106 if (phy_type >= ARRAY_SIZE(phy_type_to_attr))
107 return 0;
108
109 return phy_type_to_attr[phy_type];
110}
111
112static void
113atenl_set_attr_state(struct atenl *an, struct nl_msg *msg,
114 u8 band, enum mt76_testmode_state state)
115{
116 if (get_band_val(an, band, cur_state) == state)
117 return;
118
119 nla_put_u8(msg, MT76_TM_ATTR_STATE, state);
120 set_band_val(an, band, cur_state, state);
121}
122
123static void
124atenl_set_attr_antenna(struct atenl *an, struct nl_msg *msg, u8 tx_antenna)
125{
126 if (!tx_antenna)
127 return;
developer22c7ab62022-01-24 11:13:32 +0800128 if (is_mt7915(an))
129 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA,
130 tx_antenna << (2 * an->cur_band));
131 else if (is_mt7916(an) || is_mt7986(an))
132 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, tx_antenna);
133}
134
135static int
136atenl_nl_set_attr(struct atenl *an, struct atenl_data *data,
137 struct atenl_nl_priv *nl_priv)
138{
139 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
140 struct nl_msg *msg = nl_priv->msg;
141 u32 val = ntohl(*(u32 *)hdr->data);
142 int attr = nl_priv->attr;
143 void *ptr, *a;
144
145 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
146 if (!ptr)
147 return -ENOMEM;
148
149 switch (attr) {
150 case MT76_TM_ATTR_TX_ANTENNA:
151 atenl_set_attr_antenna(an, msg, val);
152 break;
153 case MT76_TM_ATTR_FREQ_OFFSET:
154 nla_put_u32(msg, attr, val);
155 break;
156 case MT76_TM_ATTR_TX_POWER:
157 a = nla_nest_start(msg, MT76_TM_ATTR_TX_POWER);
158 if (!a)
159 return -ENOMEM;
160 nla_put_u8(msg, 0, val);
161 nla_nest_end(msg, a);
162 break;
163 default:
164 nla_put_u8(msg, attr, val);
165 break;
166 }
167
168 nla_nest_end(msg, ptr);
169
170 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
171}
172
173static int
174atenl_nl_set_cfg(struct atenl *an, struct atenl_data *data,
175 struct atenl_nl_priv *nl_priv)
176{
177 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
178 struct nl_msg *msg = nl_priv->msg;
179 enum atenl_cmd cmd = data->cmd;
180 u32 *v = (u32 *)hdr->data;
181 u8 type = ntohl(v[0]);
182 u8 enable = ntohl(v[1]);
183 void *ptr, *cfg;
184
185 if (cmd == HQA_CMD_SET_TSSI) {
186 type = 0;
187 enable = 1;
188 }
189
190 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
191 if (!ptr)
192 return -ENOMEM;
193
194 cfg = nla_nest_start(msg, MT76_TM_ATTR_CFG);
195 if (!cfg)
196 return -ENOMEM;
197
198 if (nla_put_u8(msg, 0, type) ||
199 nla_put_u8(msg, 1, enable))
200 return -EINVAL;
201
202 nla_nest_end(msg, cfg);
203
204 nla_nest_end(msg, ptr);
205
206 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
207}
208
209static int
210atenl_nl_set_tx(struct atenl *an, struct atenl_data *data,
211 struct atenl_nl_priv *nl_priv)
212{
213 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
214 struct nl_msg *msg = nl_priv->msg;
215 u32 *v = (u32 *)hdr->data;
216 u8 *addr1 = hdr->data + 36;
217 u8 *addr2 = addr1 + ETH_ALEN;
218 u8 *addr3 = addr2 + ETH_ALEN;
developerf30d4472022-05-30 16:40:23 +0800219 u8 def_mac[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
developer22c7ab62022-01-24 11:13:32 +0800220 void *ptr, *a;
221
developerf30d4472022-05-30 16:40:23 +0800222 if (get_band_val(an, an->cur_band, use_tx_time))
223 set_band_val(an, an->cur_band, tx_time, ntohl(v[7]));
224 else
225 set_band_val(an, an->cur_band, tx_mpdu_len, ntohl(v[7]));
226
developer22c7ab62022-01-24 11:13:32 +0800227 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
228 if (!ptr)
229 return -ENOMEM;
230
developer22c7ab62022-01-24 11:13:32 +0800231 a = nla_nest_start(msg, MT76_TM_ATTR_MAC_ADDRS);
232 if (!a)
233 return -ENOMEM;
234
developerf30d4472022-05-30 16:40:23 +0800235 nla_put(msg, 0, ETH_ALEN, use_default_addr(addr1) ? def_mac : addr1);
236 nla_put(msg, 1, ETH_ALEN, use_default_addr(addr2) ? def_mac : addr2);
237 nla_put(msg, 2, ETH_ALEN, use_default_addr(addr3) ? def_mac : addr3);
developer22c7ab62022-01-24 11:13:32 +0800238
239 nla_nest_end(msg, a);
240
241 nla_nest_end(msg, ptr);
242
243 *(u32 *)(hdr->data + 2) = data->ext_id;
244
245 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
246}
247
248static int
249atenl_nl_tx(struct atenl *an, struct atenl_data *data, struct atenl_nl_priv *nl_priv)
250{
251#define USE_SPE_IDX BIT(31)
252 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
253 struct nl_msg *msg = nl_priv->msg;
254 u32 *v = (u32 *)hdr->data;
255 u8 band = ntohl(v[2]);
256 void *ptr;
257 int ret;
258
259 if (band >= MAX_BAND_NUM)
260 return -EINVAL;
261
262 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
263 if (!ptr)
264 return -ENOMEM;
265
266 if (data->ext_cmd == HQA_EXT_CMD_STOP_TX) {
267 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
268 } else {
269 u32 tx_count = ntohl(v[3]);
270 u8 tx_rate_mode = phy_type_to_attr(ntohl(v[4]));
271 u8 aid = ntohl(v[11]);
272 u8 sgi = ntohl(v[13]);
273 u32 tx_antenna = ntohl(v[14]);
274 void *a;
275
276 if (sgi > 5)
277 return -EINVAL;
278
279 if (!tx_count)
280 tx_count = 10000000;
281
282 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, tx_count);
283 nla_put_u32(msg, MT76_TM_ATTR_TX_IPG, ntohl(v[12]));
284 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, tx_rate_mode);
285 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, ntohl(v[5]));
286 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_STBC, ntohl(v[7]));
287 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_LDPC, ntohl(v[8]));
288 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_NSS, ntohl(v[15]));
289
developerf30d4472022-05-30 16:40:23 +0800290 if (get_band_val(an, band, use_tx_time))
291 nla_put_u32(msg, MT76_TM_ATTR_TX_TIME,
292 get_band_val(an, band, tx_time));
293 else
developera398c862022-07-13 10:25:35 +0800294 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH,
295 get_band_val(an, band, tx_mpdu_len));
developerf30d4472022-05-30 16:40:23 +0800296
developer22c7ab62022-01-24 11:13:32 +0800297 /* for chips after 7915, tx need to use at least wcid = 1 */
298 if (!is_mt7915(an) && !aid)
299 aid = 1;
300 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
301
302 if (tx_antenna & USE_SPE_IDX) {
303 nla_put_u8(msg, MT76_TM_ATTR_TX_SPE_IDX,
304 tx_antenna & ~USE_SPE_IDX);
305 } else {
306 nla_put_u8(msg, MT76_TM_ATTR_TX_SPE_IDX, 0);
307 atenl_set_attr_antenna(an, msg, tx_antenna);
308 }
309
310 if (tx_rate_mode >= MT76_TM_TX_MODE_HE_SU) {
311 u8 ofs = sgi;
312 size_t i;
313
314 for (i = 0; i < ARRAY_SIZE(he_sgi_groups); i++)
315 if (he_sgi_groups[i].tx_mode == tx_rate_mode)
316 break;
317
318 if ((i + ofs) >= ARRAY_SIZE(he_sgi_groups))
319 return -EINVAL;
320
321 sgi = he_sgi_groups[i + ofs].sgi;
322 nla_put_u8(msg, MT76_TM_ATTR_TX_LTF,
323 he_sgi_groups[i + ofs].tx_ltf);
324 }
325 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_SGI, sgi);
326
327 a = nla_nest_start(msg, MT76_TM_ATTR_TX_POWER);
328 if (!a)
329 return -ENOMEM;
330 nla_put_u8(msg, 0, ntohl(v[6]));
331 nla_nest_end(msg, a);
332
333 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_TX_FRAMES);
334 }
335
336 nla_nest_end(msg, ptr);
337
338 ret = unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
339 if (ret)
340 return ret;
341
342 *(u32 *)(hdr->data + 2) = data->ext_id;
343
344 return 0;
345}
346
347static int
348atenl_nl_rx(struct atenl *an, struct atenl_data *data, struct atenl_nl_priv *nl_priv)
349{
350 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
351 struct atenl_band *anb = &an->anb[an->cur_band];
352 struct nl_msg *msg = nl_priv->msg;
353 u32 *v = (u32 *)hdr->data;
354 u8 band = ntohl(v[2]);
355 void *ptr;
356
357 if (band >= MAX_BAND_NUM)
358 return -EINVAL;
359
360 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
361 if (!ptr)
362 return -ENOMEM;
363
364 if (data->ext_cmd == HQA_EXT_CMD_STOP_RX) {
365 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
366 } else {
367 v = (u32 *)(hdr->data + 18);
368
369 atenl_set_attr_antenna(an, msg, ntohl(v[0]));
370 nla_put_u8(msg, MT76_TM_ATTR_AID, ntohl(v[1]));
371 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_RX_FRAMES);
372
373 anb->reset_rx_cnt = false;
374
375 /* clear history buffer */
376 memset(&anb->rx_stat, 0, sizeof(anb->rx_stat));
377 }
378
379 nla_nest_end(msg, ptr);
380
381 *(u32 *)(hdr->data + 2) = data->ext_id;
382
383 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
384}
385
386static int
387atenl_off_ch_scan(struct atenl *an, struct atenl_data *data,
388 struct atenl_nl_priv *nl_priv)
389{
390 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
391 struct nl_msg *msg = nl_priv->msg;
392 u32 *v = (u32 *)hdr->data;
393 u8 ch = ntohl(v[2]);
394 u8 bw = ntohl(v[4]);
395 u8 tx_path = ntohl(v[5]);
396 u8 status = ntohl(v[6]);
397 void *ptr;
398
399 if (!status)
400 ch = 0; /* stop */
401
402 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
403 if (!ptr)
404 return -ENOMEM;
405
406 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_CH, ch);
407 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_CENTER_CH,
408 atenl_get_center_channel(bw, CH_BAND_5GHZ, ch));
409 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_BW, bw);
410 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_PATH, tx_path);
411
412 nla_nest_end(msg, ptr);
413
developerf30d4472022-05-30 16:40:23 +0800414 *(u32 *)(hdr->data + 2) = data->ext_id;
developer22c7ab62022-01-24 11:13:32 +0800415
416 return 0;
417}
418
419static int atenl_nl_dump_cb(struct nl_msg *msg, void *arg)
420{
421 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
422 struct nlattr *tb1[NUM_MT76_TM_ATTRS];
423 struct nlattr *tb2[NUM_MT76_TM_STATS_ATTRS];
424 struct nlattr *nl_attr;
425 int attr = nl_priv->attr;
426 u64 *res = nl_priv->res;
427
428 nl_attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
429 if (!nl_attr) {
developerf30d4472022-05-30 16:40:23 +0800430 atenl_err("Testdata attribute not found\n");
developer22c7ab62022-01-24 11:13:32 +0800431 return NL_SKIP;
432 }
433
434 nla_parse_nested(tb1, MT76_TM_ATTR_MAX, nl_attr, testdata_policy);
435 nla_parse_nested(tb2, MT76_TM_STATS_ATTR_MAX,
436 tb1[MT76_TM_ATTR_STATS], stats_policy);
437
438 if (attr == MT76_TM_STATS_ATTR_TX_DONE)
439 *res = nla_get_u32(tb2[MT76_TM_STATS_ATTR_TX_DONE]);
440
441 return NL_SKIP;
442}
443
444static int
445atenl_nl_dump_attr(struct atenl *an, struct atenl_data *data,
446 struct atenl_nl_priv *nl_priv)
447{
448 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
449 struct nl_msg *msg = nl_priv->msg;
450 void *ptr;
451 u64 res = 0;
452
453 nl_priv->res = (void *)&res;
454
455 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
456 if (!ptr)
457 return -ENOMEM;
458 nla_put_flag(msg, MT76_TM_ATTR_STATS);
459 nla_nest_end(msg, ptr);
460
461 unl_genl_request(&nl_priv->unl, msg, atenl_nl_dump_cb, (void *)nl_priv);
462
463 if (nl_priv->attr == MT76_TM_STATS_ATTR_TX_DONE)
464 *(u32 *)(hdr->data + 2 + 4 * an->cur_band) = htonl(res);
465
466 return 0;
467}
468
469static int atenl_nl_continuous_tx(struct atenl *an,
470 struct atenl_data *data,
471 struct atenl_nl_priv *nl_priv)
472{
473 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
474 struct nl_msg *msg = nl_priv->msg;
475 u32 *v = (u32 *)hdr->data;
476 u8 band = ntohl(v[0]);
477 bool enable = ntohl(v[1]);
478 void *ptr;
479
480 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
481 if (!ptr)
482 return -ENOMEM;
483
484 if (band >= MAX_BAND_NUM)
485 return -EINVAL;
486
487 if (!enable) {
488 int phy = get_band_val(an, band, phy_idx);
489 char cmd[64];
490
491 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
492 nla_nest_end(msg, ptr);
493 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
494
495 sprintf(cmd, "iw dev mon%d del", phy);
496 system(cmd);
497 sprintf(cmd, "iw phy phy%d interface add mon%d type monitor", phy, phy);
498 system(cmd);
499 sprintf(cmd, "ifconfig mon%d up", phy);
500 system(cmd);
501
502 return 0;
503 }
504
505 if (get_band_val(an, band, rf_mode) != ATENL_RF_MODE_TEST)
506 return 0;
507
508 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, ntohl(v[2]));
509 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, phy_type_to_attr(ntohl(v[3])));
510 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, ntohl(v[6]));
511
512 atenl_dbg("%s: enable = %d, ant=%u, tx_rate_mode=%u, rate_idx=%u\n",
513 __func__, enable, ntohl(v[2]), ntohl(v[3]), ntohl(v[6]));
514
515 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_TX_CONT);
516
517 nla_nest_end(msg, ptr);
518
519 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
520}
521
522static int atenl_nl_get_rx_info_cb(struct nl_msg *msg, void *arg)
523{
524 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
525 struct atenl *an = nl_priv->an;
526 struct atenl_band *anb = &an->anb[an->cur_band];
527 struct atenl_data *data = nl_priv->res;
528 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
529 struct atenl_rx_info_hdr *rx_hdr;
530 struct atenl_rx_info_band *rx_band;
531 struct atenl_rx_info_user *rx_user;
532 struct atenl_rx_info_path *rx_path;
533 struct atenl_rx_info_comm *rx_comm;
534 struct nlattr *tb1[NUM_MT76_TM_ATTRS];
535 struct nlattr *tb2[NUM_MT76_TM_STATS_ATTRS];
536 struct nlattr *tb3[NUM_MT76_TM_RX_ATTRS];
537 struct nlattr *nl_attr, *cur;
538 struct atenl_rx_stat rx_cur, rx_diff = {};
539 u32 rcpi[4] = {};
540 u32 type_num = htonl(4);
541 s32 ib_rssi[4] = {}, wb_rssi[4] = {};
542 u8 path = an->anb[an->cur_band].chainmask;
543 u8 path_num = __builtin_popcount(path);
544 u8 *buf = hdr->data + 2;
545 int i, rem;
546
547 *(u32 *)buf = type_num;
548 buf += sizeof(type_num);
549
550#define RX_PUT_HDR(_hdr, _type, _val, _size) do { \
551 _hdr->type = htonl(_type); \
552 _hdr->val = htonl(_val); \
553 _hdr->len = htonl(_size); \
554 buf += sizeof(*_hdr); \
555 } while (0)
556
557 rx_hdr = (struct atenl_rx_info_hdr *)buf;
558 RX_PUT_HDR(rx_hdr, 0, BIT(an->cur_band), sizeof(*rx_band));
559 rx_band = (struct atenl_rx_info_band *)buf;
560 buf += sizeof(*rx_band);
561
562 rx_hdr = (struct atenl_rx_info_hdr *)buf;
563 RX_PUT_HDR(rx_hdr, 1, path, path_num * sizeof(*rx_path));
564 rx_path = (struct atenl_rx_info_path *)buf;
565 buf += path_num * sizeof(*rx_path);
566
567 rx_hdr = (struct atenl_rx_info_hdr *)buf;
568 RX_PUT_HDR(rx_hdr, 2, GENMASK(15, 0), 16 * sizeof(*rx_user));
569 rx_user = (struct atenl_rx_info_user *)buf;
570 buf += 16 * sizeof(*rx_user);
571
572 rx_hdr = (struct atenl_rx_info_hdr *)buf;
573 RX_PUT_HDR(rx_hdr, 3, BIT(0), sizeof(*rx_comm));
574 rx_comm = (struct atenl_rx_info_comm *)buf;
575 buf += sizeof(*rx_comm);
576
577 hdr->len = htons(buf - hdr->data);
578
579 nl_attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
580 if (!nl_attr) {
developerf30d4472022-05-30 16:40:23 +0800581 atenl_err("Testdata attribute not found\n");
developer22c7ab62022-01-24 11:13:32 +0800582 return NL_SKIP;
583 }
584
585 nla_parse_nested(tb1, MT76_TM_ATTR_MAX, nl_attr, testdata_policy);
586 nla_parse_nested(tb2, MT76_TM_STATS_ATTR_MAX,
587 tb1[MT76_TM_ATTR_STATS], stats_policy);
588
589 rx_cur.total = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_PACKETS]);
590 rx_cur.err_cnt = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_FCS_ERROR]);
591 rx_cur.len_mismatch = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_LEN_MISMATCH]);
592 rx_cur.ok_cnt = rx_cur.total - rx_cur.err_cnt - rx_cur.len_mismatch;
593
developerf30d4472022-05-30 16:40:23 +0800594 if (!anb->reset_rx_cnt ||
595 get_band_val(an, an->cur_band, cur_state) == MT76_TM_STATE_RX_FRAMES) {
developer22c7ab62022-01-24 11:13:32 +0800596#define RX_COUNT_DIFF(_field) \
developerf30d4472022-05-30 16:40:23 +0800597 rx_diff._field = (rx_cur._field) - (anb->rx_stat._field);
developer22c7ab62022-01-24 11:13:32 +0800598 RX_COUNT_DIFF(total);
599 RX_COUNT_DIFF(err_cnt);
600 RX_COUNT_DIFF(len_mismatch);
601 RX_COUNT_DIFF(ok_cnt);
602#undef RX_COUNT_DIFF
603
604 memcpy(&anb->rx_stat, &rx_cur, sizeof(anb->rx_stat));
605 }
606
607 rx_band->mac_rx_mdrdy_cnt = htonl((u32)rx_diff.total);
608 rx_band->mac_rx_fcs_err_cnt = htonl((u32)rx_diff.err_cnt);
609 rx_band->mac_rx_fcs_ok_cnt = htonl((u32)rx_diff.ok_cnt);
610 rx_band->mac_rx_len_mismatch = htonl((u32)rx_diff.len_mismatch);
611 rx_user->fcs_error_cnt = htonl((u32)rx_diff.err_cnt);
612
613 nla_parse_nested(tb3, MT76_TM_RX_ATTR_MAX,
614 tb2[MT76_TM_STATS_ATTR_LAST_RX], rx_policy);
615
616 rx_user->freq_offset = htonl(nla_get_u32(tb3[MT76_TM_RX_ATTR_FREQ_OFFSET]));
617 rx_user->snr = htonl(nla_get_u8(tb3[MT76_TM_RX_ATTR_SNR]));
618
619 i = 0;
620 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_RCPI], rem) {
621 if (nla_len(cur) != 1 || i >= 4)
622 break;
623
624 rcpi[i++] = nla_get_u8(cur);
625 }
626
627 i = 0;
628 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_IB_RSSI], rem) {
629 if (nla_len(cur) != 1 || i >= 4)
630 break;
631
632 ib_rssi[i++] = (s8)nla_get_u8(cur);
633 }
634
635 i = 0;
636 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_WB_RSSI], rem) {
637 if (nla_len(cur) != 1 || i >= 4)
638 break;
639
640 wb_rssi[i++] = (s8)nla_get_u8(cur);
641 }
642
643 for (i = 0; i < 4; i++) {
644 struct atenl_rx_info_path *path = &rx_path[i];
645
646 path->rcpi = htonl(rcpi[i]);
647 path->rssi = htonl(to_rssi((u8)rcpi[i]));
648 path->fagc_ib_rssi = htonl(ib_rssi[i]);
649 path->fagc_wb_rssi = htonl(wb_rssi[i]);
650 }
651
652 return NL_SKIP;
653}
654
655static int atenl_nl_get_rx_info(struct atenl *an, struct atenl_data *data,
656 struct atenl_nl_priv *nl_priv)
657{
658 struct nl_msg *msg = nl_priv->msg;
659 void *ptr;
660
661 nl_priv->an = an;
662 nl_priv->res = (void *)data;
663
664 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
665 if (!ptr)
666 return -ENOMEM;
667
668 nla_put_flag(msg, MT76_TM_ATTR_STATS);
669
670 nla_nest_end(msg, ptr);
671
672 return unl_genl_request(&nl_priv->unl, msg, atenl_nl_get_rx_info_cb,
673 (void *)nl_priv);
674}
675
676static int
677atenl_nl_set_ru(struct atenl *an, struct atenl_data *data,
678 struct atenl_nl_priv *nl_priv)
679{
680 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
681 struct nl_msg *msg;
682 u32 *v = (u32 *)(hdr->data + 4);
683 u32 seg0_num = ntohl(v[0]); /* v[1] seg1_num unused */
684 void *ptr;
685 int i, ret;
686
687 if (seg0_num > 8)
688 return -EINVAL;
689
690 for (i = 0, v = &v[2]; i < seg0_num; i++, v += 11) {
691 u32 ru_alloc = ntohl(v[1]);
692 u32 aid = ntohl(v[2]);
693 u32 ru_idx = ntohl(v[3]);
694 u32 mcs = ntohl(v[4]);
695 u32 ldpc = ntohl(v[5]);
696 u32 nss = ntohl(v[6]);
697 u32 tx_length = ntohl(v[8]);
698 char buf[10];
699
700 if (unl_genl_init(&nl_priv->unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +0800701 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +0800702 return 2;
703 }
704
705 msg = unl_genl_msg(&nl_priv->unl, NL80211_CMD_TESTMODE, false);
706 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
707
708 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
709 if (!ptr)
710 return -ENOMEM;
711
712 if (i == 0)
713 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_IDLE);
714
715 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
716 nla_put_u8(msg, MT76_TM_ATTR_RU_IDX, ru_idx);
717 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, mcs);
718 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_LDPC, ldpc);
719 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_NSS, nss);
developera398c862022-07-13 10:25:35 +0800720 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH, tx_length);
developer22c7ab62022-01-24 11:13:32 +0800721
722 ret = snprintf(buf, sizeof(buf), "%x", ru_alloc);
723 if (snprintf_error(sizeof(buf), ret))
724 return -EINVAL;
725
726 nla_put_u8(msg, MT76_TM_ATTR_RU_ALLOC, strtol(buf, NULL, 2));
727
728 nla_nest_end(msg, ptr);
729
730 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
731
732 unl_free(&nl_priv->unl);
733 }
734
735 return 0;
736}
737
developerf30d4472022-05-30 16:40:23 +0800738static int
739atenl_nl_ibf_init(struct atenl *an, u8 band)
740{
741 struct atenl_nl_priv nl_priv = {};
742 struct nl_msg *msg;
743 void *ptr, *a;
744 int ret;
745
746 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
747 atenl_err("Failed to connect to nl80211\n");
748 return 2;
749 }
750
751 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
752 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
753
754 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
755 if (!ptr) {
756 ret = -ENOMEM;
757 goto out;
758 }
759
760 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, MT76_TM_TX_MODE_HT);
761 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, an->ibf_mcs);
762 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, an->ibf_ant);
763 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_INIT);
764
765 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
766 if (!a) {
767 ret = -ENOMEM;
768 goto out;
769 }
770 nla_put_u16(msg, 0, 1);
771 nla_nest_end(msg, a);
772
773 nla_nest_end(msg, ptr);
774
775 ret = unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
776
777out:
778 unl_free(&nl_priv.unl);
779 return ret;
780}
781
782static int
783atenl_nl_ibf_e2p_update(struct atenl *an)
784{
785 struct atenl_nl_priv nl_priv = {};
786 struct nl_msg *msg;
787 void *ptr, *a;
788 int ret;
789
790 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
791 atenl_err("Failed to connect to nl80211\n");
792 return 2;
793 }
794
795 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
796 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
797
798 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
799 if (!ptr) {
800 ret = -ENOMEM;
801 goto out;
802 }
803
804 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_E2P_UPDATE);
805 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
806 if (!a) {
807 ret = -ENOMEM;
808 goto out;
809 }
810 nla_put_u16(msg, 0, 0);
811 nla_nest_end(msg, a);
812
813 nla_nest_end(msg, ptr);
814
815 ret = unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
816
817out:
818 unl_free(&nl_priv.unl);
819 return ret;
820}
821
822static void
823atenl_get_ibf_cal_result(struct atenl *an)
824{
825 u16 offset;
826
827 if (an->adie_id == 0x7975)
828 offset = 0x651;
829 else if (an->adie_id == 0x7976)
830 offset = 0x60a;
831
832 /* per group size = 40, for group 0-8 */
833 atenl_eeprom_read_from_driver(an, offset, 40 * 9);
834}
835
836static int
837atenl_nl_ibf_set_val(struct atenl *an, struct atenl_data *data,
838 struct atenl_nl_priv *nl_priv)
developer22c7ab62022-01-24 11:13:32 +0800839{
840#define MT_IBF(_act) MT76_TM_TXBF_ACT_##_act
841 static const u8 bf_act_map[] = {
842 [TXBF_ACT_IBF_PHASE_COMP] = MT_IBF(PHASE_COMP),
843 [TXBF_ACT_IBF_PROF_UPDATE] = MT_IBF(IBF_PROF_UPDATE),
844 [TXBF_ACT_EBF_PROF_UPDATE] = MT_IBF(EBF_PROF_UPDATE),
845 [TXBF_ACT_IBF_PHASE_CAL] = MT_IBF(PHASE_CAL),
846 };
847#undef MT_IBF
848 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
849 struct nl_msg *msg = nl_priv->msg;
850 u32 *v = (u32 *)(hdr->data + 4);
851 u32 action = ntohl(v[0]);
developerdc430562022-09-13 10:57:15 +0800852 u16 val[8], is_atenl = 1;
developerf30d4472022-05-30 16:40:23 +0800853 u8 tmp_ant;
developer22c7ab62022-01-24 11:13:32 +0800854 void *ptr, *a;
855 char cmd[64];
856 int i;
857
858 for (i = 0; i < 8; i++)
859 val[i] = ntohl(v[i + 1]);
860
861 atenl_dbg("%s: action = %u, val = %u, %u, %u, %u, %u\n",
862 __func__, action, val[0], val[1], val[2], val[3], val[4]);
863
864 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
865 if (!ptr)
866 return -ENOMEM;
867
868 switch (action) {
developer22c7ab62022-01-24 11:13:32 +0800869 case TXBF_ACT_CHANNEL:
developerf30d4472022-05-30 16:40:23 +0800870 an->cur_band = val[1];
871 /* a sanity to prevent script band idx error */
872 if (val[0] > 14)
873 an->cur_band = 1;
874 atenl_nl_ibf_init(an, an->cur_band);
875 atenl_set_channel(an, 0, an->cur_band, val[0], 0, 0);
876
877 nla_put_u8(msg, MT76_TM_ATTR_AID, 0);
developer22c7ab62022-01-24 11:13:32 +0800878 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_UPDATE_CH);
879 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
880 if (!a)
881 return -ENOMEM;
882 nla_put_u16(msg, 0, 0);
883 nla_nest_end(msg, a);
884 break;
885 case TXBF_ACT_MCS:
developerf30d4472022-05-30 16:40:23 +0800886 tmp_ant = (1 << DIV_ROUND_UP(val[0], 8)) - 1 ?: 1;
887 /* sometimes the correct band idx will be set after this action,
888 * so maintain a temp variable to allow mcs update in anthor action.
889 */
890 an->ibf_mcs = val[0];
891 an->ibf_ant = tmp_ant;
892 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, an->ibf_mcs);
893 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, an->ibf_ant);
developer22c7ab62022-01-24 11:13:32 +0800894 break;
895 case TXBF_ACT_TX_ANT:
896 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, val[0]);
897 break;
898 case TXBF_ACT_RX_START:
899 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_RX_FRAMES);
900 break;
901 case TXBF_ACT_RX_ANT:
902 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, val[0]);
903 break;
904 case TXBF_ACT_TX_PKT:
905 nla_put_u8(msg, MT76_TM_ATTR_AID, val[1]);
developer22c7ab62022-01-24 11:13:32 +0800906 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_TX_PREP);
developerf30d4472022-05-30 16:40:23 +0800907 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, 10000000);
developera398c862022-07-13 10:25:35 +0800908 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH, 1024);
developer22c7ab62022-01-24 11:13:32 +0800909 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
910 if (!a)
911 return -ENOMEM;
912
913 for (i = 0; i < 5; i++)
914 nla_put_u16(msg, i, val[i]);
915 nla_nest_end(msg, a);
916
917 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_TX_FRAMES);
918 break;
919 case TXBF_ACT_IBF_PHASE_COMP:
developerf30d4472022-05-30 16:40:23 +0800920 nla_put_u8(msg, MT76_TM_ATTR_AID, 1);
developer22c7ab62022-01-24 11:13:32 +0800921 case TXBF_ACT_IBF_PROF_UPDATE:
922 case TXBF_ACT_EBF_PROF_UPDATE:
923 case TXBF_ACT_IBF_PHASE_CAL:
924 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, bf_act_map[action]);
925 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
926 if (!a)
927 return -ENOMEM;
developerdc430562022-09-13 10:57:15 +0800928 /* Note: litepoint may send random number for lna_gain_level, reset to 0 */
929 if (action == TXBF_ACT_IBF_PHASE_CAL)
930 val[4] = 0;
developer22c7ab62022-01-24 11:13:32 +0800931 for (i = 0; i < 5; i++)
932 nla_put_u16(msg, i, val[i]);
developerdc430562022-09-13 10:57:15 +0800933 /* Used to distinguish between command mode and HQADLL mode */
934 nla_put_u16(msg, 5, is_atenl);
developer22c7ab62022-01-24 11:13:32 +0800935 nla_nest_end(msg, a);
936 break;
937 case TXBF_ACT_IBF_PHASE_E2P_UPDATE:
developerf30d4472022-05-30 16:40:23 +0800938 atenl_nl_ibf_e2p_update(an);
939 atenl_get_ibf_cal_result(an);
developer22c7ab62022-01-24 11:13:32 +0800940
941 nla_put_u8(msg, MT76_TM_ATTR_AID, 0);
942 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_INIT);
943
944 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
945 if (!a)
946 return -ENOMEM;
947 nla_put_u16(msg, 0, 0);
948 nla_nest_end(msg, a);
949 break;
developerf30d4472022-05-30 16:40:23 +0800950 case TXBF_ACT_INIT:
951 case TXBF_ACT_POWER:
developer22c7ab62022-01-24 11:13:32 +0800952 default:
953 break;
954 }
955
956 nla_nest_end(msg, ptr);
957
developerf30d4472022-05-30 16:40:23 +0800958 *(u32 *)(hdr->data + 2) = data->ext_id;
developer22c7ab62022-01-24 11:13:32 +0800959
960 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
961}
962
963static int
964atenl_nl_ibf_get_status(struct atenl *an, struct atenl_data *data,
965 struct atenl_nl_priv *nl_priv)
966{
967 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
968 u32 status = htonl(1);
969
developerf30d4472022-05-30 16:40:23 +0800970 *(u32 *)(hdr->data + 2) = data->ext_id;
developer22c7ab62022-01-24 11:13:32 +0800971 memcpy(hdr->data + 6, &status, 4);
972
973 return 0;
974}
975
976static int
977atenl_nl_ibf_profile_update_all(struct atenl *an, struct atenl_data *data,
978 struct atenl_nl_priv *nl_priv)
979{
980 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
981 struct nl_msg *msg;
982 void *ptr, *a;
983 u32 *v = (u32 *)(hdr->data + 4);
984 u16 pfmu_idx = ntohl(v[0]);
985 int i;
986
987 for (i = 0, v = &v[5]; i < 64; i++, v += 5) {
988 int j;
989
990 if (unl_genl_init(&nl_priv->unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +0800991 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +0800992 return 2;
993 }
994
995 msg = unl_genl_msg(&nl_priv->unl, NL80211_CMD_TESTMODE, false);
996 nla_put_u32(msg, NL80211_ATTR_WIPHY,
997 get_band_val(an, an->cur_band, phy_idx));
998
999 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1000 if (!ptr)
1001 return -ENOMEM;
1002
1003 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_PROF_UPDATE_ALL);
1004 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
1005 if (!a)
1006 return -ENOMEM;
1007 nla_put_u16(msg, 0, pfmu_idx);
1008
1009 for (j = 0; j < 5; j++)
1010 nla_put_u16(msg, j + 1, ntohl(v[j]));
1011 nla_nest_end(msg, a);
1012
1013 nla_nest_end(msg, ptr);
1014
1015 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
1016
1017 unl_free(&nl_priv->unl);
1018 }
1019
developerf30d4472022-05-30 16:40:23 +08001020 *(u32 *)(hdr->data + 2) = data->ext_id;
developer22c7ab62022-01-24 11:13:32 +08001021
1022 return 0;
1023}
1024
1025#define NL_OPS_GROUP(cmd, ...) [HQA_CMD_##cmd] = { __VA_ARGS__ }
1026static const struct atenl_nl_ops nl_ops[] = {
1027 NL_OPS_GROUP(SET_TX_PATH, .set=MT76_TM_ATTR_TX_ANTENNA),
1028 NL_OPS_GROUP(SET_TX_POWER, .set=MT76_TM_ATTR_TX_POWER),
1029 NL_OPS_GROUP(SET_RX_PATH, .set=MT76_TM_ATTR_TX_ANTENNA),
1030 NL_OPS_GROUP(SET_FREQ_OFFSET, .set=MT76_TM_ATTR_FREQ_OFFSET),
1031 NL_OPS_GROUP(SET_CFG, .ops=atenl_nl_set_cfg),
1032 NL_OPS_GROUP(SET_TSSI, .ops=atenl_nl_set_cfg),
1033 NL_OPS_GROUP(CONTINUOUS_TX, .ops=atenl_nl_continuous_tx),
1034 NL_OPS_GROUP(GET_TX_INFO, .dump=MT76_TM_STATS_ATTR_TX_DONE),
1035 NL_OPS_GROUP(GET_RX_INFO, .ops=atenl_nl_get_rx_info, .dump=true),
1036 NL_OPS_GROUP(SET_RU, .ops=atenl_nl_set_ru),
1037};
1038#undef NL_OPS_GROUP
1039
1040#define NL_OPS_EXT(cmd, ...) [HQA_EXT_CMD_##cmd] = { __VA_ARGS__ }
1041static const struct atenl_nl_ops nl_ops_ext[] = {
1042 NL_OPS_EXT(SET_TX, .ops=atenl_nl_set_tx),
1043 NL_OPS_EXT(START_TX, .ops=atenl_nl_tx),
1044 NL_OPS_EXT(STOP_TX, .ops=atenl_nl_tx),
1045 NL_OPS_EXT(START_RX, .ops=atenl_nl_rx),
1046 NL_OPS_EXT(STOP_RX, .ops=atenl_nl_rx),
1047 NL_OPS_EXT(OFF_CH_SCAN, .ops=atenl_off_ch_scan),
1048 NL_OPS_EXT(IBF_SET_VAL, .ops=atenl_nl_ibf_set_val),
1049 NL_OPS_EXT(IBF_GET_STATUS, .ops=atenl_nl_ibf_get_status),
1050 NL_OPS_EXT(IBF_PROF_UPDATE_ALL, .ops=atenl_nl_ibf_profile_update_all),
1051};
1052#undef NL_OPS_EXT
1053
1054int atenl_nl_process(struct atenl *an, struct atenl_data *data)
1055{
1056 struct atenl_nl_priv nl_priv = {};
1057 const struct atenl_nl_ops *ops;
1058 struct nl_msg *msg;
1059 int ret = 0;
1060
1061 if (data->ext_cmd != 0)
1062 ops = &nl_ops_ext[data->ext_cmd];
1063 else
1064 ops = &nl_ops[data->cmd];
1065
1066 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001067 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001068 return -1;
1069 }
1070
1071 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, !!ops->dump);
1072 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
1073 nl_priv.msg = msg;
1074
1075 if (ops->ops) {
1076 ret = ops->ops(an, data, &nl_priv);
1077 } else if (ops->dump) {
1078 nl_priv.attr = ops->dump;
1079 ret = atenl_nl_dump_attr(an, data, &nl_priv);
1080 } else {
1081 nl_priv.attr = ops->set;
1082 ret = atenl_nl_set_attr(an, data, &nl_priv);
1083 }
1084
1085 if (ret)
developerf30d4472022-05-30 16:40:23 +08001086 atenl_err("command process error: 0x%x (0x%x)\n", data->cmd_id, data->ext_id);
developer22c7ab62022-01-24 11:13:32 +08001087
1088 unl_free(&nl_priv.unl);
1089
1090 return ret;
1091}
1092
1093int atenl_nl_process_many(struct atenl *an, struct atenl_data *data)
1094{
1095 struct atenl_nl_priv nl_priv = {};
1096 const struct atenl_nl_ops *ops;
1097 int ret = 0;
1098
1099 if (data->ext_cmd != 0)
1100 ops = &nl_ops_ext[data->ext_cmd];
1101 else
1102 ops = &nl_ops[data->cmd];
1103
1104 if (ops->ops)
1105 ret = ops->ops(an, data, &nl_priv);
1106
1107 return ret;
1108}
1109
1110int atenl_nl_set_state(struct atenl *an, u8 band,
1111 enum mt76_testmode_state state)
1112{
1113 struct atenl_nl_priv nl_priv = {};
1114 struct nl_msg *msg;
1115 void *ptr;
1116
1117 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001118 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001119 return 2;
1120 }
1121
1122 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1123 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
1124
1125 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1126 if (!ptr)
1127 return -ENOMEM;
1128
1129 atenl_set_attr_state(an, msg, band, state);
1130
1131 nla_nest_end(msg, ptr);
1132
1133 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1134
1135 unl_free(&nl_priv.unl);
1136
1137 return 0;
1138}
1139
developerf30d4472022-05-30 16:40:23 +08001140int atenl_nl_set_aid(struct atenl *an, u8 band, u8 aid)
1141{
1142 struct atenl_nl_priv nl_priv = {};
1143 struct nl_msg *msg;
1144 void *ptr;
1145
1146 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1147 atenl_err("Failed to connect to nl80211\n");
1148 return 2;
1149 }
1150
1151 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1152 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
1153
1154 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1155 if (!ptr)
1156 return -ENOMEM;
1157
1158 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
1159
1160 nla_nest_end(msg, ptr);
1161
1162 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1163
1164 unl_free(&nl_priv.unl);
1165
1166 return 0;
1167}
1168
developer22c7ab62022-01-24 11:13:32 +08001169static int atenl_nl_check_mtd_cb(struct nl_msg *msg, void *arg)
1170{
1171 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
1172 struct atenl *an = nl_priv->an;
1173 struct nlattr *tb[NUM_MT76_TM_ATTRS];
1174 struct nlattr *attr;
1175
1176 attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
1177 if (!attr)
1178 return NL_SKIP;
1179
1180 nla_parse_nested(tb, MT76_TM_ATTR_MAX, attr, testdata_policy);
1181 if (!tb[MT76_TM_ATTR_MTD_PART] || !tb[MT76_TM_ATTR_MTD_OFFSET])
1182 return NL_SKIP;
1183
1184 an->mtd_part = strdup(nla_get_string(tb[MT76_TM_ATTR_MTD_PART]));
1185 an->mtd_offset = nla_get_u32(tb[MT76_TM_ATTR_MTD_OFFSET]);
developer454c4652022-08-31 20:39:29 +08001186 an->is_main_phy = nla_get_u32(tb[MT76_TM_ATTR_IS_MAIN_PHY]);
developer22c7ab62022-01-24 11:13:32 +08001187
1188 return NL_SKIP;
1189}
1190
1191int atenl_nl_check_mtd(struct atenl *an)
1192{
1193 struct atenl_nl_priv nl_priv = { .an = an };
1194 struct nl_msg *msg;
1195
1196 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001197 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001198 return 2;
1199 }
1200
1201 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, true);
1202 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1203 unl_genl_request(&nl_priv.unl, msg, atenl_nl_check_mtd_cb, (void *)&nl_priv);
1204
1205 unl_free(&nl_priv.unl);
1206
1207 return 0;
1208}
1209
1210int atenl_nl_write_eeprom(struct atenl *an, u32 offset, u8 *val, int len)
1211{
1212 struct atenl_nl_priv nl_priv = {};
1213 struct nl_msg *msg;
1214 void *ptr, *a;
1215 int i;
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
1222 if (len > 16)
1223 return -EINVAL;
1224
1225 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1226 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1227
1228 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1229 if (!ptr)
1230 return -ENOMEM;
1231
1232 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1233 MT76_TM_EEPROM_ACTION_UPDATE_DATA);
1234 nla_put_u32(msg, MT76_TM_ATTR_EEPROM_OFFSET, offset);
1235
1236 a = nla_nest_start(msg, MT76_TM_ATTR_EEPROM_VAL);
1237 if (!a)
1238 return -ENOMEM;
1239
1240 for (i = 0; i < len; i++)
1241 if (nla_put_u8(msg, i, val[i]))
1242 goto out;
1243
1244 nla_nest_end(msg, a);
1245
1246 nla_nest_end(msg, ptr);
1247
1248 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1249
1250 unl_free(&nl_priv.unl);
1251
1252out:
1253 return 0;
1254}
1255
developer48fbcb62022-03-10 14:24:55 +08001256int atenl_nl_write_efuse_all(struct atenl *an)
developer22c7ab62022-01-24 11:13:32 +08001257{
1258 struct atenl_nl_priv nl_priv = {};
1259 struct nl_msg *msg;
1260 void *ptr;
1261
1262 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001263 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001264 return 2;
1265 }
1266
1267 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1268 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1269
1270 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1271 if (!ptr)
1272 return -ENOMEM;
1273
1274 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1275 MT76_TM_EEPROM_ACTION_WRITE_TO_EFUSE);
1276
1277 nla_nest_end(msg, ptr);
1278
1279 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1280
1281 unl_free(&nl_priv.unl);
1282
1283 return 0;
1284}
1285
1286int atenl_nl_update_buffer_mode(struct atenl *an)
1287{
1288 struct atenl_nl_priv nl_priv = {};
1289 struct nl_msg *msg;
1290 void *ptr;
1291
1292 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developerf30d4472022-05-30 16:40:23 +08001293 atenl_err("Failed to connect to nl80211\n");
developer22c7ab62022-01-24 11:13:32 +08001294 return 2;
1295 }
1296
1297 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1298 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1299
1300 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1301 if (!ptr)
1302 return -ENOMEM;
1303
1304 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1305 MT76_TM_EEPROM_ACTION_UPDATE_BUFFER_MODE);
1306
1307 nla_nest_end(msg, ptr);
1308
1309 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1310
1311 unl_free(&nl_priv.unl);
1312
1313 return 0;
1314}
1315
developer454c4652022-08-31 20:39:29 +08001316static int atenl_nl_precal_sync_from_driver_cb(struct nl_msg *msg, void *arg)
1317{
1318 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
1319 struct atenl *an = nl_priv->an;
1320 struct nlattr *tb[NUM_MT76_TM_ATTRS];
1321 struct nlattr *attr, *cur;
1322 int i, rem, prek_offset = nl_priv->attr;
1323
1324
1325 attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
1326 if (!attr)
1327 return NL_SKIP;
1328
1329 nla_parse_nested(tb, MT76_TM_ATTR_MAX, attr, testdata_policy);
1330
1331 if (!tb[MT76_TM_ATTR_PRECAL_INFO] && !tb[MT76_TM_ATTR_PRECAL]) {
1332 atenl_info("No Pre cal data or info!\n");
1333 return NL_SKIP;
1334 }
1335
1336 if (tb[MT76_TM_ATTR_PRECAL_INFO]) {
1337 i = 0;
1338 nla_for_each_nested(cur, tb[MT76_TM_ATTR_PRECAL_INFO], rem) {
1339 an->cal_info[i] = (u32) nla_get_u32(cur);
1340 i++;
1341 }
1342 return NL_SKIP;
1343 }
1344
1345 if (tb[MT76_TM_ATTR_PRECAL] && an->cal) {
1346 i = prek_offset;
1347 nla_for_each_nested(cur, tb[MT76_TM_ATTR_PRECAL], rem) {
1348 an->cal[i] = (u8) nla_get_u8(cur);
1349 i++;
1350 }
1351 return NL_SKIP;
1352 }
1353 atenl_info("No data found for pre-cal!\n");
1354
1355 return NL_SKIP;
1356}
1357
1358static int
1359atenl_nl_precal_sync_partition(struct atenl_nl_priv *nl_priv, enum mt76_testmode_attr attr,
1360 int prek_type, int prek_offset)
1361{
1362 int ret;
1363 void *ptr;
1364 struct nl_msg *msg;
1365 struct atenl *an = nl_priv->an;
1366
1367 msg = unl_genl_msg(&(nl_priv->unl), NL80211_CMD_TESTMODE, true);
1368 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
1369 nl_priv->msg = msg;
1370 nl_priv->attr = prek_offset;
1371
1372 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1373 if (!ptr)
1374 return -ENOMEM;
1375
1376 nla_put_flag(msg, attr);
1377 if (attr == MT76_TM_ATTR_PRECAL)
1378 nla_put_u8(msg, MT76_TM_ATTR_PRECAL_INFO, prek_type);
1379 nla_nest_end(msg, ptr);
1380
1381 ret = unl_genl_request(&(nl_priv->unl), msg, atenl_nl_precal_sync_from_driver_cb, (void *)nl_priv);
1382
1383 if (ret) {
1384 atenl_err("command process error!\n");
1385 return ret;
1386 }
1387
1388 return 0;
1389}
1390
1391int atenl_nl_precal_sync_from_driver(struct atenl *an, enum prek_ops ops)
1392{
1393#define GROUP_IND_MASK BIT(0)
1394#define DPD_IND_MASK GENMASK(3, 1)
1395 int ret;
1396 u32 i, times, group_size, dpd_size, total_size, transmit_size, offs;
1397 u32 dpd_per_chan_size, dpd_chan_num[3], total_chan_num;
1398 u32 size, base, base_idx, *size_ptr;
1399 u8 cal_indicator, *precal_info;
1400 struct atenl_nl_priv nl_priv = { .an = an };
1401
1402 offs = an->eeprom_prek_offs;
1403 cal_indicator = an->eeprom_data[offs];
1404
1405 if (cal_indicator) {
1406 precal_info = an->eeprom_data + an->eeprom_size;
1407 memcpy(an->cal_info, precal_info, PRE_CAL_INFO);
1408 group_size = an->cal_info[0];
1409 dpd_size = an->cal_info[1];
1410 total_size = group_size + dpd_size;
1411 dpd_chan_num[0] = (an->cal_info[2] >> DPD_INFO_6G_SHIFT) & DPD_INFO_MASK;
1412 dpd_chan_num[1] = (an->cal_info[2] >> DPD_INFO_5G_SHIFT) & DPD_INFO_MASK;
1413 dpd_chan_num[2] = (an->cal_info[2] >> DPD_INFO_2G_SHIFT) & DPD_INFO_MASK;
1414 dpd_per_chan_size = (an->cal_info[2] >> DPD_INFO_CH_SHIFT) & DPD_INFO_MASK;
1415 total_chan_num = dpd_chan_num[0] + dpd_chan_num[1] + dpd_chan_num[2];
1416 }
1417
1418 switch (ops){
1419 case PREK_SYNC_ALL:
1420 size_ptr = &total_size;
1421 base_idx = 0;
1422 goto start;
1423 case PREK_SYNC_GROUP:
1424 size_ptr = &group_size;
1425 base_idx = 0;
1426 goto start;
1427 case PREK_SYNC_DPD_6G:
1428 size_ptr = &dpd_size;
1429 base_idx = 0;
1430 goto start;
1431 case PREK_SYNC_DPD_5G:
1432 size_ptr = &dpd_size;
1433 base_idx = 1;
1434 goto start;
1435 case PREK_SYNC_DPD_2G:
1436 size_ptr = &dpd_size;
1437 base_idx = 2;
1438
1439start:
1440 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1441 atenl_err("Failed to connect to nl80211\n");
1442 return 2;
1443 }
1444
1445 ret = atenl_nl_precal_sync_partition(&nl_priv, MT76_TM_ATTR_PRECAL_INFO, 0, 0);
1446 if (ret || !an->cal_info)
1447 goto out;
1448
1449 group_size = an->cal_info[0];
1450 dpd_size = an->cal_info[1];
1451 total_size = group_size + dpd_size;
1452 dpd_chan_num[0] = (an->cal_info[2] >> DPD_INFO_6G_SHIFT) & DPD_INFO_MASK;
1453 dpd_chan_num[1] = (an->cal_info[2] >> DPD_INFO_5G_SHIFT) & DPD_INFO_MASK;
1454 dpd_chan_num[2] = (an->cal_info[2] >> DPD_INFO_2G_SHIFT) & DPD_INFO_MASK;
1455 dpd_per_chan_size = (an->cal_info[2] >> DPD_INFO_CH_SHIFT) & DPD_INFO_MASK;
1456 total_chan_num = dpd_chan_num[0] + dpd_chan_num[1] + dpd_chan_num[2];
1457 transmit_size = an->cal_info[3];
1458
1459 size = *size_ptr;
1460 size = (size_ptr == &dpd_size) ? (size / total_chan_num * dpd_chan_num[base_idx]) :
1461 size;
1462 base = 0;
1463 for (i = 0; i < base_idx; i++) {
1464 base += dpd_chan_num[i] * dpd_per_chan_size * MT_EE_CAL_UNIT;
1465 }
1466 base += (size_ptr == &dpd_size) ? group_size : 0;
1467
1468 if (!an->cal)
1469 an->cal = (u8 *) calloc(size, sizeof(u8));
1470 times = size / transmit_size + 1;
1471 for (i = 0; i < times; i++) {
1472 ret = atenl_nl_precal_sync_partition(&nl_priv, MT76_TM_ATTR_PRECAL, ops,
1473 i * transmit_size);
1474 if (ret)
1475 goto out;
1476 }
1477
1478 ret = atenl_eeprom_update_precal(an, base, size);
1479 break;
1480 case PREK_CLEAN_GROUP:
1481 if (!(cal_indicator & GROUP_IND_MASK))
1482 return 0;
1483 an->cal_info[4] = cal_indicator & (u8) ~GROUP_IND_MASK;
1484 ret = atenl_eeprom_update_precal(an, 0, group_size);
1485 break;
1486 case PREK_CLEAN_DPD:
1487 if (!(cal_indicator & DPD_IND_MASK))
1488 return 0;
1489 an->cal_info[4] = cal_indicator & (u8) ~DPD_IND_MASK;
1490 ret = atenl_eeprom_update_precal(an, group_size, dpd_size);
1491 break;
1492 default:
1493 break;
1494 }
1495
1496out:
1497 unl_free(&nl_priv.unl);
1498 return ret;
1499}