blob: 368ffbd8c3b4410b4911cca86928cc683ec64401 [file] [log] [blame]
developer3abe1ad2022-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 },
29 [MT76_TM_ATTR_TX_COUNT] = { .type = NLA_U32 },
30 [MT76_TM_ATTR_TX_LENGTH] = { .type = NLA_U32 },
31 [MT76_TM_ATTR_TX_RATE_MODE] = { .type = NLA_U8 },
32 [MT76_TM_ATTR_TX_RATE_NSS] = { .type = NLA_U8 },
33 [MT76_TM_ATTR_TX_RATE_IDX] = { .type = NLA_U8 },
34 [MT76_TM_ATTR_TX_RATE_SGI] = { .type = NLA_U8 },
35 [MT76_TM_ATTR_TX_RATE_LDPC] = { .type = NLA_U8 },
36 [MT76_TM_ATTR_TX_RATE_STBC] = { .type = NLA_U8 },
37 [MT76_TM_ATTR_TX_LTF] = { .type = NLA_U8 },
38 [MT76_TM_ATTR_TX_POWER_CONTROL] = { .type = NLA_U8 },
39 [MT76_TM_ATTR_TX_ANTENNA] = { .type = NLA_U8 },
40 [MT76_TM_ATTR_FREQ_OFFSET] = { .type = NLA_U32 },
41 [MT76_TM_ATTR_STATS] = { .type = NLA_NESTED },
42};
43
44static struct nla_policy stats_policy[NUM_MT76_TM_STATS_ATTRS] = {
45 [MT76_TM_STATS_ATTR_TX_PENDING] = { .type = NLA_U32 },
46 [MT76_TM_STATS_ATTR_TX_QUEUED] = { .type = NLA_U32 },
47 [MT76_TM_STATS_ATTR_TX_DONE] = { .type = NLA_U32 },
48 [MT76_TM_STATS_ATTR_RX_PACKETS] = { .type = NLA_U64 },
49 [MT76_TM_STATS_ATTR_RX_FCS_ERROR] = { .type = NLA_U64 },
50};
51
52static struct nla_policy rx_policy[NUM_MT76_TM_RX_ATTRS] = {
53 [MT76_TM_RX_ATTR_FREQ_OFFSET] = { .type = NLA_U32 },
54 [MT76_TM_RX_ATTR_RCPI] = { .type = NLA_NESTED },
55 [MT76_TM_RX_ATTR_IB_RSSI] = { .type = NLA_NESTED },
56 [MT76_TM_RX_ATTR_WB_RSSI] = { .type = NLA_NESTED },
57 [MT76_TM_RX_ATTR_SNR] = { .type = NLA_U8 },
58};
59
60struct he_sgi {
61 enum mt76_testmode_tx_mode tx_mode;
62 u8 sgi;
63 u8 tx_ltf;
64};
65
66#define HE_SGI_GROUP(_tx_mode, _sgi, _tx_ltf) \
67 { .tx_mode = MT76_TM_TX_MODE_##_tx_mode, .sgi = _sgi, .tx_ltf = _tx_ltf }
68static const struct he_sgi he_sgi_groups[] = {
69 HE_SGI_GROUP(HE_SU, 0, 0),
70 HE_SGI_GROUP(HE_SU, 0, 1),
71 HE_SGI_GROUP(HE_SU, 1, 1),
72 HE_SGI_GROUP(HE_SU, 2, 2),
73 HE_SGI_GROUP(HE_SU, 0, 2),
74 HE_SGI_GROUP(HE_EXT_SU, 0, 0),
75 HE_SGI_GROUP(HE_EXT_SU, 0, 1),
76 HE_SGI_GROUP(HE_EXT_SU, 1, 1),
77 HE_SGI_GROUP(HE_EXT_SU, 2, 2),
78 HE_SGI_GROUP(HE_EXT_SU, 0, 2),
79 HE_SGI_GROUP(HE_TB, 1, 0),
80 HE_SGI_GROUP(HE_TB, 1, 1),
81 HE_SGI_GROUP(HE_TB, 2, 2),
82 HE_SGI_GROUP(HE_MU, 0, 2),
83 HE_SGI_GROUP(HE_MU, 0, 1),
84 HE_SGI_GROUP(HE_MU, 1, 1),
85 HE_SGI_GROUP(HE_MU, 2, 2),
86};
87#undef HE_SGI_LTF_GROUP
88
89static u8 phy_type_to_attr(u8 phy_type)
90{
91 static const u8 phy_type_to_attr[] = {
92 [ATENL_PHY_TYPE_CCK] = MT76_TM_TX_MODE_CCK,
93 [ATENL_PHY_TYPE_OFDM] = MT76_TM_TX_MODE_OFDM,
94 [ATENL_PHY_TYPE_HT] = MT76_TM_TX_MODE_HT,
95 [ATENL_PHY_TYPE_HT_GF] = MT76_TM_TX_MODE_HT,
96 [ATENL_PHY_TYPE_VHT] = MT76_TM_TX_MODE_VHT,
97 [ATENL_PHY_TYPE_HE_SU] = MT76_TM_TX_MODE_HE_SU,
98 [ATENL_PHY_TYPE_HE_EXT_SU] = MT76_TM_TX_MODE_HE_EXT_SU,
99 [ATENL_PHY_TYPE_HE_TB] = MT76_TM_TX_MODE_HE_TB,
100 [ATENL_PHY_TYPE_HE_MU] = MT76_TM_TX_MODE_HE_MU,
101 };
102
103 if (phy_type >= ARRAY_SIZE(phy_type_to_attr))
104 return 0;
105
106 return phy_type_to_attr[phy_type];
107}
108
109static void
110atenl_set_attr_state(struct atenl *an, struct nl_msg *msg,
111 u8 band, enum mt76_testmode_state state)
112{
113 if (get_band_val(an, band, cur_state) == state)
114 return;
115
116 nla_put_u8(msg, MT76_TM_ATTR_STATE, state);
117 set_band_val(an, band, cur_state, state);
118}
119
120static void
121atenl_set_attr_antenna(struct atenl *an, struct nl_msg *msg, u8 tx_antenna)
122{
123 if (!tx_antenna)
124 return;
125
126 if (is_mt7915(an))
127 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA,
128 tx_antenna << (2 * an->cur_band));
129 else if (is_mt7916(an) || is_mt7986(an))
130 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, tx_antenna);
131}
132
133static int
134atenl_nl_set_attr(struct atenl *an, struct atenl_data *data,
135 struct atenl_nl_priv *nl_priv)
136{
137 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
138 struct nl_msg *msg = nl_priv->msg;
139 u32 val = ntohl(*(u32 *)hdr->data);
140 int attr = nl_priv->attr;
141 void *ptr, *a;
142
143 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
144 if (!ptr)
145 return -ENOMEM;
146
147 switch (attr) {
148 case MT76_TM_ATTR_TX_ANTENNA:
149 atenl_set_attr_antenna(an, msg, val);
150 break;
151 case MT76_TM_ATTR_FREQ_OFFSET:
152 nla_put_u32(msg, attr, val);
153 break;
154 case MT76_TM_ATTR_TX_POWER:
155 a = nla_nest_start(msg, MT76_TM_ATTR_TX_POWER);
156 if (!a)
157 return -ENOMEM;
158 nla_put_u8(msg, 0, val);
159 nla_nest_end(msg, a);
160 break;
161 default:
162 nla_put_u8(msg, attr, val);
163 break;
164 }
165
166 nla_nest_end(msg, ptr);
167
168 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
169}
170
171static int
172atenl_nl_set_cfg(struct atenl *an, struct atenl_data *data,
173 struct atenl_nl_priv *nl_priv)
174{
175 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
176 struct nl_msg *msg = nl_priv->msg;
177 enum atenl_cmd cmd = data->cmd;
178 u32 *v = (u32 *)hdr->data;
179 u8 type = ntohl(v[0]);
180 u8 enable = ntohl(v[1]);
181 void *ptr, *cfg;
182
183 if (cmd == HQA_CMD_SET_TSSI) {
184 type = 0;
185 enable = 1;
186 }
187
188 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
189 if (!ptr)
190 return -ENOMEM;
191
192 cfg = nla_nest_start(msg, MT76_TM_ATTR_CFG);
193 if (!cfg)
194 return -ENOMEM;
195
196 if (nla_put_u8(msg, 0, type) ||
197 nla_put_u8(msg, 1, enable))
198 return -EINVAL;
199
200 nla_nest_end(msg, cfg);
201
202 nla_nest_end(msg, ptr);
203
204 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
205}
206
207static int
208atenl_nl_set_tx(struct atenl *an, struct atenl_data *data,
209 struct atenl_nl_priv *nl_priv)
210{
211 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
212 struct nl_msg *msg = nl_priv->msg;
213 u32 *v = (u32 *)hdr->data;
214 u8 *addr1 = hdr->data + 36;
215 u8 *addr2 = addr1 + ETH_ALEN;
216 u8 *addr3 = addr2 + ETH_ALEN;
developer5698c9c2022-05-30 16:40:23 +0800217 u8 def_mac[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
developer3abe1ad2022-01-24 11:13:32 +0800218 void *ptr, *a;
219
developer5698c9c2022-05-30 16:40:23 +0800220 if (get_band_val(an, an->cur_band, use_tx_time))
221 set_band_val(an, an->cur_band, tx_time, ntohl(v[7]));
222 else
223 set_band_val(an, an->cur_band, tx_mpdu_len, ntohl(v[7]));
224
developer3abe1ad2022-01-24 11:13:32 +0800225 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
226 if (!ptr)
227 return -ENOMEM;
228
developer3abe1ad2022-01-24 11:13:32 +0800229 a = nla_nest_start(msg, MT76_TM_ATTR_MAC_ADDRS);
230 if (!a)
231 return -ENOMEM;
232
developer5698c9c2022-05-30 16:40:23 +0800233 nla_put(msg, 0, ETH_ALEN, use_default_addr(addr1) ? def_mac : addr1);
234 nla_put(msg, 1, ETH_ALEN, use_default_addr(addr2) ? def_mac : addr2);
235 nla_put(msg, 2, ETH_ALEN, use_default_addr(addr3) ? def_mac : addr3);
developer3abe1ad2022-01-24 11:13:32 +0800236
237 nla_nest_end(msg, a);
238
239 nla_nest_end(msg, ptr);
240
241 *(u32 *)(hdr->data + 2) = data->ext_id;
242
243 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
244}
245
246static int
247atenl_nl_tx(struct atenl *an, struct atenl_data *data, struct atenl_nl_priv *nl_priv)
248{
249#define USE_SPE_IDX BIT(31)
250 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
251 struct nl_msg *msg = nl_priv->msg;
252 u32 *v = (u32 *)hdr->data;
253 u8 band = ntohl(v[2]);
254 void *ptr;
255 int ret;
256
257 if (band >= MAX_BAND_NUM)
258 return -EINVAL;
259
260 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
261 if (!ptr)
262 return -ENOMEM;
263
264 if (data->ext_cmd == HQA_EXT_CMD_STOP_TX) {
265 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
266 } else {
267 u32 tx_count = ntohl(v[3]);
268 u8 tx_rate_mode = phy_type_to_attr(ntohl(v[4]));
269 u8 aid = ntohl(v[11]);
270 u8 sgi = ntohl(v[13]);
271 u32 tx_antenna = ntohl(v[14]);
272 void *a;
273
274 if (sgi > 5)
275 return -EINVAL;
276
277 if (!tx_count)
278 tx_count = 10000000;
279
280 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, tx_count);
281 nla_put_u32(msg, MT76_TM_ATTR_TX_IPG, ntohl(v[12]));
282 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, tx_rate_mode);
283 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, ntohl(v[5]));
284 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_STBC, ntohl(v[7]));
285 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_LDPC, ntohl(v[8]));
286 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_NSS, ntohl(v[15]));
287
developer5698c9c2022-05-30 16:40:23 +0800288 if (get_band_val(an, band, use_tx_time))
289 nla_put_u32(msg, MT76_TM_ATTR_TX_TIME,
290 get_band_val(an, band, tx_time));
291 else
developer93dadcc2022-07-13 10:25:35 +0800292 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH,
293 get_band_val(an, band, tx_mpdu_len));
developer5698c9c2022-05-30 16:40:23 +0800294
developer3abe1ad2022-01-24 11:13:32 +0800295 /* for chips after 7915, tx need to use at least wcid = 1 */
296 if (!is_mt7915(an) && !aid)
297 aid = 1;
298 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
299
300 if (tx_antenna & USE_SPE_IDX) {
301 nla_put_u8(msg, MT76_TM_ATTR_TX_SPE_IDX,
302 tx_antenna & ~USE_SPE_IDX);
303 } else {
304 nla_put_u8(msg, MT76_TM_ATTR_TX_SPE_IDX, 0);
305 atenl_set_attr_antenna(an, msg, tx_antenna);
306 }
307
308 if (tx_rate_mode >= MT76_TM_TX_MODE_HE_SU) {
309 u8 ofs = sgi;
310 size_t i;
311
312 for (i = 0; i < ARRAY_SIZE(he_sgi_groups); i++)
313 if (he_sgi_groups[i].tx_mode == tx_rate_mode)
314 break;
315
316 if ((i + ofs) >= ARRAY_SIZE(he_sgi_groups))
317 return -EINVAL;
318
319 sgi = he_sgi_groups[i + ofs].sgi;
320 nla_put_u8(msg, MT76_TM_ATTR_TX_LTF,
321 he_sgi_groups[i + ofs].tx_ltf);
322 }
323 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_SGI, sgi);
324
325 a = nla_nest_start(msg, MT76_TM_ATTR_TX_POWER);
326 if (!a)
327 return -ENOMEM;
328 nla_put_u8(msg, 0, ntohl(v[6]));
329 nla_nest_end(msg, a);
330
331 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_TX_FRAMES);
332 }
333
334 nla_nest_end(msg, ptr);
335
336 ret = unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
337 if (ret)
338 return ret;
339
340 *(u32 *)(hdr->data + 2) = data->ext_id;
341
342 return 0;
343}
344
345static int
346atenl_nl_rx(struct atenl *an, struct atenl_data *data, struct atenl_nl_priv *nl_priv)
347{
348 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
349 struct atenl_band *anb = &an->anb[an->cur_band];
350 struct nl_msg *msg = nl_priv->msg;
351 u32 *v = (u32 *)hdr->data;
352 u8 band = ntohl(v[2]);
353 void *ptr;
354
355 if (band >= MAX_BAND_NUM)
356 return -EINVAL;
357
358 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
359 if (!ptr)
360 return -ENOMEM;
361
362 if (data->ext_cmd == HQA_EXT_CMD_STOP_RX) {
363 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
364 } else {
365 v = (u32 *)(hdr->data + 18);
366
367 atenl_set_attr_antenna(an, msg, ntohl(v[0]));
368 nla_put_u8(msg, MT76_TM_ATTR_AID, ntohl(v[1]));
369 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_RX_FRAMES);
370
371 anb->reset_rx_cnt = false;
372
373 /* clear history buffer */
374 memset(&anb->rx_stat, 0, sizeof(anb->rx_stat));
375 }
376
377 nla_nest_end(msg, ptr);
378
379 *(u32 *)(hdr->data + 2) = data->ext_id;
380
381 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
382}
383
384static int
385atenl_off_ch_scan(struct atenl *an, struct atenl_data *data,
386 struct atenl_nl_priv *nl_priv)
387{
388 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
389 struct nl_msg *msg = nl_priv->msg;
390 u32 *v = (u32 *)hdr->data;
391 u8 ch = ntohl(v[2]);
392 u8 bw = ntohl(v[4]);
393 u8 tx_path = ntohl(v[5]);
394 u8 status = ntohl(v[6]);
395 void *ptr;
396
397 if (!status)
398 ch = 0; /* stop */
399
400 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
401 if (!ptr)
402 return -ENOMEM;
403
404 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_CH, ch);
405 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_CENTER_CH,
406 atenl_get_center_channel(bw, CH_BAND_5GHZ, ch));
407 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_BW, bw);
408 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_PATH, tx_path);
409
410 nla_nest_end(msg, ptr);
411
developer5698c9c2022-05-30 16:40:23 +0800412 *(u32 *)(hdr->data + 2) = data->ext_id;
developer3abe1ad2022-01-24 11:13:32 +0800413
414 return 0;
415}
416
417static int atenl_nl_dump_cb(struct nl_msg *msg, void *arg)
418{
419 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
420 struct nlattr *tb1[NUM_MT76_TM_ATTRS];
421 struct nlattr *tb2[NUM_MT76_TM_STATS_ATTRS];
422 struct nlattr *nl_attr;
423 int attr = nl_priv->attr;
424 u64 *res = nl_priv->res;
425
426 nl_attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
427 if (!nl_attr) {
developer5698c9c2022-05-30 16:40:23 +0800428 atenl_err("Testdata attribute not found\n");
developer3abe1ad2022-01-24 11:13:32 +0800429 return NL_SKIP;
430 }
431
432 nla_parse_nested(tb1, MT76_TM_ATTR_MAX, nl_attr, testdata_policy);
433 nla_parse_nested(tb2, MT76_TM_STATS_ATTR_MAX,
434 tb1[MT76_TM_ATTR_STATS], stats_policy);
435
436 if (attr == MT76_TM_STATS_ATTR_TX_DONE)
437 *res = nla_get_u32(tb2[MT76_TM_STATS_ATTR_TX_DONE]);
438
439 return NL_SKIP;
440}
441
442static int
443atenl_nl_dump_attr(struct atenl *an, struct atenl_data *data,
444 struct atenl_nl_priv *nl_priv)
445{
446 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
447 struct nl_msg *msg = nl_priv->msg;
448 void *ptr;
449 u64 res = 0;
450
451 nl_priv->res = (void *)&res;
452
453 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
454 if (!ptr)
455 return -ENOMEM;
456 nla_put_flag(msg, MT76_TM_ATTR_STATS);
457 nla_nest_end(msg, ptr);
458
459 unl_genl_request(&nl_priv->unl, msg, atenl_nl_dump_cb, (void *)nl_priv);
460
461 if (nl_priv->attr == MT76_TM_STATS_ATTR_TX_DONE)
462 *(u32 *)(hdr->data + 2 + 4 * an->cur_band) = htonl(res);
463
464 return 0;
465}
466
467static int atenl_nl_continuous_tx(struct atenl *an,
468 struct atenl_data *data,
469 struct atenl_nl_priv *nl_priv)
470{
471 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
472 struct nl_msg *msg = nl_priv->msg;
473 u32 *v = (u32 *)hdr->data;
474 u8 band = ntohl(v[0]);
475 bool enable = ntohl(v[1]);
476 void *ptr;
477
478 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
479 if (!ptr)
480 return -ENOMEM;
481
482 if (band >= MAX_BAND_NUM)
483 return -EINVAL;
484
485 if (!enable) {
486 int phy = get_band_val(an, band, phy_idx);
487 char cmd[64];
488
489 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
490 nla_nest_end(msg, ptr);
491 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
492
493 sprintf(cmd, "iw dev mon%d del", phy);
494 system(cmd);
495 sprintf(cmd, "iw phy phy%d interface add mon%d type monitor", phy, phy);
496 system(cmd);
497 sprintf(cmd, "ifconfig mon%d up", phy);
498 system(cmd);
499
500 return 0;
501 }
502
503 if (get_band_val(an, band, rf_mode) != ATENL_RF_MODE_TEST)
504 return 0;
505
506 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, ntohl(v[2]));
507 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, phy_type_to_attr(ntohl(v[3])));
508 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, ntohl(v[6]));
509
510 atenl_dbg("%s: enable = %d, ant=%u, tx_rate_mode=%u, rate_idx=%u\n",
511 __func__, enable, ntohl(v[2]), ntohl(v[3]), ntohl(v[6]));
512
513 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_TX_CONT);
514
515 nla_nest_end(msg, ptr);
516
517 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
518}
519
520static int atenl_nl_get_rx_info_cb(struct nl_msg *msg, void *arg)
521{
522 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
523 struct atenl *an = nl_priv->an;
524 struct atenl_band *anb = &an->anb[an->cur_band];
525 struct atenl_data *data = nl_priv->res;
526 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
527 struct atenl_rx_info_hdr *rx_hdr;
528 struct atenl_rx_info_band *rx_band;
529 struct atenl_rx_info_user *rx_user;
530 struct atenl_rx_info_path *rx_path;
531 struct atenl_rx_info_comm *rx_comm;
532 struct nlattr *tb1[NUM_MT76_TM_ATTRS];
533 struct nlattr *tb2[NUM_MT76_TM_STATS_ATTRS];
534 struct nlattr *tb3[NUM_MT76_TM_RX_ATTRS];
535 struct nlattr *nl_attr, *cur;
536 struct atenl_rx_stat rx_cur, rx_diff = {};
537 u32 rcpi[4] = {};
538 u32 type_num = htonl(4);
539 s32 ib_rssi[4] = {}, wb_rssi[4] = {};
540 u8 path = an->anb[an->cur_band].chainmask;
541 u8 path_num = __builtin_popcount(path);
542 u8 *buf = hdr->data + 2;
543 int i, rem;
544
545 *(u32 *)buf = type_num;
546 buf += sizeof(type_num);
547
548#define RX_PUT_HDR(_hdr, _type, _val, _size) do { \
549 _hdr->type = htonl(_type); \
550 _hdr->val = htonl(_val); \
551 _hdr->len = htonl(_size); \
552 buf += sizeof(*_hdr); \
553 } while (0)
554
555 rx_hdr = (struct atenl_rx_info_hdr *)buf;
556 RX_PUT_HDR(rx_hdr, 0, BIT(an->cur_band), sizeof(*rx_band));
557 rx_band = (struct atenl_rx_info_band *)buf;
558 buf += sizeof(*rx_band);
559
560 rx_hdr = (struct atenl_rx_info_hdr *)buf;
561 RX_PUT_HDR(rx_hdr, 1, path, path_num * sizeof(*rx_path));
562 rx_path = (struct atenl_rx_info_path *)buf;
563 buf += path_num * sizeof(*rx_path);
564
565 rx_hdr = (struct atenl_rx_info_hdr *)buf;
566 RX_PUT_HDR(rx_hdr, 2, GENMASK(15, 0), 16 * sizeof(*rx_user));
567 rx_user = (struct atenl_rx_info_user *)buf;
568 buf += 16 * sizeof(*rx_user);
569
570 rx_hdr = (struct atenl_rx_info_hdr *)buf;
571 RX_PUT_HDR(rx_hdr, 3, BIT(0), sizeof(*rx_comm));
572 rx_comm = (struct atenl_rx_info_comm *)buf;
573 buf += sizeof(*rx_comm);
574
575 hdr->len = htons(buf - hdr->data);
576
577 nl_attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
578 if (!nl_attr) {
developer5698c9c2022-05-30 16:40:23 +0800579 atenl_err("Testdata attribute not found\n");
developer3abe1ad2022-01-24 11:13:32 +0800580 return NL_SKIP;
581 }
582
583 nla_parse_nested(tb1, MT76_TM_ATTR_MAX, nl_attr, testdata_policy);
584 nla_parse_nested(tb2, MT76_TM_STATS_ATTR_MAX,
585 tb1[MT76_TM_ATTR_STATS], stats_policy);
586
587 rx_cur.total = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_PACKETS]);
588 rx_cur.err_cnt = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_FCS_ERROR]);
589 rx_cur.len_mismatch = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_LEN_MISMATCH]);
590 rx_cur.ok_cnt = rx_cur.total - rx_cur.err_cnt - rx_cur.len_mismatch;
591
developer5698c9c2022-05-30 16:40:23 +0800592 if (!anb->reset_rx_cnt ||
593 get_band_val(an, an->cur_band, cur_state) == MT76_TM_STATE_RX_FRAMES) {
developer3abe1ad2022-01-24 11:13:32 +0800594#define RX_COUNT_DIFF(_field) \
developer5698c9c2022-05-30 16:40:23 +0800595 rx_diff._field = (rx_cur._field) - (anb->rx_stat._field);
developer3abe1ad2022-01-24 11:13:32 +0800596 RX_COUNT_DIFF(total);
597 RX_COUNT_DIFF(err_cnt);
598 RX_COUNT_DIFF(len_mismatch);
599 RX_COUNT_DIFF(ok_cnt);
600#undef RX_COUNT_DIFF
601
602 memcpy(&anb->rx_stat, &rx_cur, sizeof(anb->rx_stat));
603 }
604
605 rx_band->mac_rx_mdrdy_cnt = htonl((u32)rx_diff.total);
606 rx_band->mac_rx_fcs_err_cnt = htonl((u32)rx_diff.err_cnt);
607 rx_band->mac_rx_fcs_ok_cnt = htonl((u32)rx_diff.ok_cnt);
608 rx_band->mac_rx_len_mismatch = htonl((u32)rx_diff.len_mismatch);
609 rx_user->fcs_error_cnt = htonl((u32)rx_diff.err_cnt);
610
611 nla_parse_nested(tb3, MT76_TM_RX_ATTR_MAX,
612 tb2[MT76_TM_STATS_ATTR_LAST_RX], rx_policy);
613
614 rx_user->freq_offset = htonl(nla_get_u32(tb3[MT76_TM_RX_ATTR_FREQ_OFFSET]));
615 rx_user->snr = htonl(nla_get_u8(tb3[MT76_TM_RX_ATTR_SNR]));
616
617 i = 0;
618 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_RCPI], rem) {
619 if (nla_len(cur) != 1 || i >= 4)
620 break;
621
622 rcpi[i++] = nla_get_u8(cur);
623 }
624
625 i = 0;
626 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_IB_RSSI], rem) {
627 if (nla_len(cur) != 1 || i >= 4)
628 break;
629
630 ib_rssi[i++] = (s8)nla_get_u8(cur);
631 }
632
633 i = 0;
634 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_WB_RSSI], rem) {
635 if (nla_len(cur) != 1 || i >= 4)
636 break;
637
638 wb_rssi[i++] = (s8)nla_get_u8(cur);
639 }
640
641 for (i = 0; i < 4; i++) {
642 struct atenl_rx_info_path *path = &rx_path[i];
643
644 path->rcpi = htonl(rcpi[i]);
645 path->rssi = htonl(to_rssi((u8)rcpi[i]));
646 path->fagc_ib_rssi = htonl(ib_rssi[i]);
647 path->fagc_wb_rssi = htonl(wb_rssi[i]);
648 }
649
650 return NL_SKIP;
651}
652
653static int atenl_nl_get_rx_info(struct atenl *an, struct atenl_data *data,
654 struct atenl_nl_priv *nl_priv)
655{
656 struct nl_msg *msg = nl_priv->msg;
657 void *ptr;
658
659 nl_priv->an = an;
660 nl_priv->res = (void *)data;
661
662 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
663 if (!ptr)
664 return -ENOMEM;
665
666 nla_put_flag(msg, MT76_TM_ATTR_STATS);
667
668 nla_nest_end(msg, ptr);
669
670 return unl_genl_request(&nl_priv->unl, msg, atenl_nl_get_rx_info_cb,
671 (void *)nl_priv);
672}
673
674static int
675atenl_nl_set_ru(struct atenl *an, struct atenl_data *data,
676 struct atenl_nl_priv *nl_priv)
677{
678 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
679 struct nl_msg *msg;
680 u32 *v = (u32 *)(hdr->data + 4);
681 u32 seg0_num = ntohl(v[0]); /* v[1] seg1_num unused */
682 void *ptr;
683 int i, ret;
684
685 if (seg0_num > 8)
686 return -EINVAL;
687
688 for (i = 0, v = &v[2]; i < seg0_num; i++, v += 11) {
689 u32 ru_alloc = ntohl(v[1]);
690 u32 aid = ntohl(v[2]);
691 u32 ru_idx = ntohl(v[3]);
692 u32 mcs = ntohl(v[4]);
693 u32 ldpc = ntohl(v[5]);
694 u32 nss = ntohl(v[6]);
695 u32 tx_length = ntohl(v[8]);
696 char buf[10];
697
698 if (unl_genl_init(&nl_priv->unl, "nl80211") < 0) {
developer5698c9c2022-05-30 16:40:23 +0800699 atenl_err("Failed to connect to nl80211\n");
developer3abe1ad2022-01-24 11:13:32 +0800700 return 2;
701 }
702
703 msg = unl_genl_msg(&nl_priv->unl, NL80211_CMD_TESTMODE, false);
704 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
705
706 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
707 if (!ptr)
708 return -ENOMEM;
709
710 if (i == 0)
711 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_IDLE);
712
713 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
714 nla_put_u8(msg, MT76_TM_ATTR_RU_IDX, ru_idx);
715 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, mcs);
716 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_LDPC, ldpc);
717 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_NSS, nss);
developer93dadcc2022-07-13 10:25:35 +0800718 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH, tx_length);
developer3abe1ad2022-01-24 11:13:32 +0800719
720 ret = snprintf(buf, sizeof(buf), "%x", ru_alloc);
721 if (snprintf_error(sizeof(buf), ret))
722 return -EINVAL;
723
724 nla_put_u8(msg, MT76_TM_ATTR_RU_ALLOC, strtol(buf, NULL, 2));
725
726 nla_nest_end(msg, ptr);
727
728 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
729
730 unl_free(&nl_priv->unl);
731 }
732
733 return 0;
734}
735
developer5698c9c2022-05-30 16:40:23 +0800736static int
737atenl_nl_ibf_init(struct atenl *an, u8 band)
738{
739 struct atenl_nl_priv nl_priv = {};
740 struct nl_msg *msg;
741 void *ptr, *a;
742 int ret;
743
744 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
745 atenl_err("Failed to connect to nl80211\n");
746 return 2;
747 }
748
749 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
750 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
751
752 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
753 if (!ptr) {
754 ret = -ENOMEM;
755 goto out;
756 }
757
758 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, MT76_TM_TX_MODE_HT);
759 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, an->ibf_mcs);
760 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, an->ibf_ant);
761 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_INIT);
762
763 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
764 if (!a) {
765 ret = -ENOMEM;
766 goto out;
767 }
768 nla_put_u16(msg, 0, 1);
769 nla_nest_end(msg, a);
770
771 nla_nest_end(msg, ptr);
772
773 ret = unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
774
775out:
776 unl_free(&nl_priv.unl);
777 return ret;
778}
779
780static int
781atenl_nl_ibf_e2p_update(struct atenl *an)
782{
783 struct atenl_nl_priv nl_priv = {};
784 struct nl_msg *msg;
785 void *ptr, *a;
786 int ret;
787
788 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
789 atenl_err("Failed to connect to nl80211\n");
790 return 2;
791 }
792
793 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
794 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
795
796 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
797 if (!ptr) {
798 ret = -ENOMEM;
799 goto out;
800 }
801
802 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_E2P_UPDATE);
803 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
804 if (!a) {
805 ret = -ENOMEM;
806 goto out;
807 }
808 nla_put_u16(msg, 0, 0);
809 nla_nest_end(msg, a);
810
811 nla_nest_end(msg, ptr);
812
813 ret = unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
814
815out:
816 unl_free(&nl_priv.unl);
817 return ret;
818}
819
820static void
821atenl_get_ibf_cal_result(struct atenl *an)
822{
823 u16 offset;
824
825 if (an->adie_id == 0x7975)
826 offset = 0x651;
827 else if (an->adie_id == 0x7976)
828 offset = 0x60a;
829
830 /* per group size = 40, for group 0-8 */
831 atenl_eeprom_read_from_driver(an, offset, 40 * 9);
832}
833
834static int
835atenl_nl_ibf_set_val(struct atenl *an, struct atenl_data *data,
836 struct atenl_nl_priv *nl_priv)
developer3abe1ad2022-01-24 11:13:32 +0800837{
838#define MT_IBF(_act) MT76_TM_TXBF_ACT_##_act
839 static const u8 bf_act_map[] = {
840 [TXBF_ACT_IBF_PHASE_COMP] = MT_IBF(PHASE_COMP),
841 [TXBF_ACT_IBF_PROF_UPDATE] = MT_IBF(IBF_PROF_UPDATE),
842 [TXBF_ACT_EBF_PROF_UPDATE] = MT_IBF(EBF_PROF_UPDATE),
843 [TXBF_ACT_IBF_PHASE_CAL] = MT_IBF(PHASE_CAL),
844 };
845#undef MT_IBF
846 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
847 struct nl_msg *msg = nl_priv->msg;
848 u32 *v = (u32 *)(hdr->data + 4);
849 u32 action = ntohl(v[0]);
850 u16 val[8];
developer5698c9c2022-05-30 16:40:23 +0800851 u8 tmp_ant;
developer3abe1ad2022-01-24 11:13:32 +0800852 void *ptr, *a;
853 char cmd[64];
854 int i;
855
856 for (i = 0; i < 8; i++)
857 val[i] = ntohl(v[i + 1]);
858
859 atenl_dbg("%s: action = %u, val = %u, %u, %u, %u, %u\n",
860 __func__, action, val[0], val[1], val[2], val[3], val[4]);
861
862 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
863 if (!ptr)
864 return -ENOMEM;
865
866 switch (action) {
developer3abe1ad2022-01-24 11:13:32 +0800867 case TXBF_ACT_CHANNEL:
developer5698c9c2022-05-30 16:40:23 +0800868 an->cur_band = val[1];
869 /* a sanity to prevent script band idx error */
870 if (val[0] > 14)
871 an->cur_band = 1;
872 atenl_nl_ibf_init(an, an->cur_band);
873 atenl_set_channel(an, 0, an->cur_band, val[0], 0, 0);
874
875 nla_put_u8(msg, MT76_TM_ATTR_AID, 0);
developer3abe1ad2022-01-24 11:13:32 +0800876 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_UPDATE_CH);
877 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
878 if (!a)
879 return -ENOMEM;
880 nla_put_u16(msg, 0, 0);
881 nla_nest_end(msg, a);
882 break;
883 case TXBF_ACT_MCS:
developer5698c9c2022-05-30 16:40:23 +0800884 tmp_ant = (1 << DIV_ROUND_UP(val[0], 8)) - 1 ?: 1;
885 /* sometimes the correct band idx will be set after this action,
886 * so maintain a temp variable to allow mcs update in anthor action.
887 */
888 an->ibf_mcs = val[0];
889 an->ibf_ant = tmp_ant;
890 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, an->ibf_mcs);
891 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, an->ibf_ant);
developer3abe1ad2022-01-24 11:13:32 +0800892 break;
893 case TXBF_ACT_TX_ANT:
894 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, val[0]);
895 break;
896 case TXBF_ACT_RX_START:
897 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_RX_FRAMES);
898 break;
899 case TXBF_ACT_RX_ANT:
900 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, val[0]);
901 break;
902 case TXBF_ACT_TX_PKT:
903 nla_put_u8(msg, MT76_TM_ATTR_AID, val[1]);
developer3abe1ad2022-01-24 11:13:32 +0800904 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_TX_PREP);
developer5698c9c2022-05-30 16:40:23 +0800905 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, 10000000);
developer93dadcc2022-07-13 10:25:35 +0800906 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH, 1024);
developer3abe1ad2022-01-24 11:13:32 +0800907 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
908 if (!a)
909 return -ENOMEM;
910
911 for (i = 0; i < 5; i++)
912 nla_put_u16(msg, i, val[i]);
913 nla_nest_end(msg, a);
914
915 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_TX_FRAMES);
916 break;
917 case TXBF_ACT_IBF_PHASE_COMP:
developer5698c9c2022-05-30 16:40:23 +0800918 nla_put_u8(msg, MT76_TM_ATTR_AID, 1);
developer3abe1ad2022-01-24 11:13:32 +0800919 case TXBF_ACT_IBF_PROF_UPDATE:
920 case TXBF_ACT_EBF_PROF_UPDATE:
921 case TXBF_ACT_IBF_PHASE_CAL:
922 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, bf_act_map[action]);
923 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
924 if (!a)
925 return -ENOMEM;
926
927 for (i = 0; i < 5; i++)
928 nla_put_u16(msg, i, val[i]);
929 nla_nest_end(msg, a);
930 break;
931 case TXBF_ACT_IBF_PHASE_E2P_UPDATE:
developer5698c9c2022-05-30 16:40:23 +0800932 atenl_nl_ibf_e2p_update(an);
933 atenl_get_ibf_cal_result(an);
developer3abe1ad2022-01-24 11:13:32 +0800934
935 nla_put_u8(msg, MT76_TM_ATTR_AID, 0);
936 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_INIT);
937
938 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
939 if (!a)
940 return -ENOMEM;
941 nla_put_u16(msg, 0, 0);
942 nla_nest_end(msg, a);
943 break;
developer5698c9c2022-05-30 16:40:23 +0800944 case TXBF_ACT_INIT:
945 case TXBF_ACT_POWER:
developer3abe1ad2022-01-24 11:13:32 +0800946 default:
947 break;
948 }
949
950 nla_nest_end(msg, ptr);
951
developer5698c9c2022-05-30 16:40:23 +0800952 *(u32 *)(hdr->data + 2) = data->ext_id;
developer3abe1ad2022-01-24 11:13:32 +0800953
954 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
955}
956
957static int
958atenl_nl_ibf_get_status(struct atenl *an, struct atenl_data *data,
959 struct atenl_nl_priv *nl_priv)
960{
961 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
962 u32 status = htonl(1);
963
developer5698c9c2022-05-30 16:40:23 +0800964 *(u32 *)(hdr->data + 2) = data->ext_id;
developer3abe1ad2022-01-24 11:13:32 +0800965 memcpy(hdr->data + 6, &status, 4);
966
967 return 0;
968}
969
970static int
971atenl_nl_ibf_profile_update_all(struct atenl *an, struct atenl_data *data,
972 struct atenl_nl_priv *nl_priv)
973{
974 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
975 struct nl_msg *msg;
976 void *ptr, *a;
977 u32 *v = (u32 *)(hdr->data + 4);
978 u16 pfmu_idx = ntohl(v[0]);
979 int i;
980
981 for (i = 0, v = &v[5]; i < 64; i++, v += 5) {
982 int j;
983
984 if (unl_genl_init(&nl_priv->unl, "nl80211") < 0) {
developer5698c9c2022-05-30 16:40:23 +0800985 atenl_err("Failed to connect to nl80211\n");
developer3abe1ad2022-01-24 11:13:32 +0800986 return 2;
987 }
988
989 msg = unl_genl_msg(&nl_priv->unl, NL80211_CMD_TESTMODE, false);
990 nla_put_u32(msg, NL80211_ATTR_WIPHY,
991 get_band_val(an, an->cur_band, phy_idx));
992
993 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
994 if (!ptr)
995 return -ENOMEM;
996
997 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_PROF_UPDATE_ALL);
998 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
999 if (!a)
1000 return -ENOMEM;
1001 nla_put_u16(msg, 0, pfmu_idx);
1002
1003 for (j = 0; j < 5; j++)
1004 nla_put_u16(msg, j + 1, ntohl(v[j]));
1005 nla_nest_end(msg, a);
1006
1007 nla_nest_end(msg, ptr);
1008
1009 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
1010
1011 unl_free(&nl_priv->unl);
1012 }
1013
developer5698c9c2022-05-30 16:40:23 +08001014 *(u32 *)(hdr->data + 2) = data->ext_id;
developer3abe1ad2022-01-24 11:13:32 +08001015
1016 return 0;
1017}
1018
1019#define NL_OPS_GROUP(cmd, ...) [HQA_CMD_##cmd] = { __VA_ARGS__ }
1020static const struct atenl_nl_ops nl_ops[] = {
1021 NL_OPS_GROUP(SET_TX_PATH, .set=MT76_TM_ATTR_TX_ANTENNA),
1022 NL_OPS_GROUP(SET_TX_POWER, .set=MT76_TM_ATTR_TX_POWER),
1023 NL_OPS_GROUP(SET_RX_PATH, .set=MT76_TM_ATTR_TX_ANTENNA),
1024 NL_OPS_GROUP(SET_FREQ_OFFSET, .set=MT76_TM_ATTR_FREQ_OFFSET),
1025 NL_OPS_GROUP(SET_CFG, .ops=atenl_nl_set_cfg),
1026 NL_OPS_GROUP(SET_TSSI, .ops=atenl_nl_set_cfg),
1027 NL_OPS_GROUP(CONTINUOUS_TX, .ops=atenl_nl_continuous_tx),
1028 NL_OPS_GROUP(GET_TX_INFO, .dump=MT76_TM_STATS_ATTR_TX_DONE),
1029 NL_OPS_GROUP(GET_RX_INFO, .ops=atenl_nl_get_rx_info, .dump=true),
1030 NL_OPS_GROUP(SET_RU, .ops=atenl_nl_set_ru),
1031};
1032#undef NL_OPS_GROUP
1033
1034#define NL_OPS_EXT(cmd, ...) [HQA_EXT_CMD_##cmd] = { __VA_ARGS__ }
1035static const struct atenl_nl_ops nl_ops_ext[] = {
1036 NL_OPS_EXT(SET_TX, .ops=atenl_nl_set_tx),
1037 NL_OPS_EXT(START_TX, .ops=atenl_nl_tx),
1038 NL_OPS_EXT(STOP_TX, .ops=atenl_nl_tx),
1039 NL_OPS_EXT(START_RX, .ops=atenl_nl_rx),
1040 NL_OPS_EXT(STOP_RX, .ops=atenl_nl_rx),
1041 NL_OPS_EXT(OFF_CH_SCAN, .ops=atenl_off_ch_scan),
1042 NL_OPS_EXT(IBF_SET_VAL, .ops=atenl_nl_ibf_set_val),
1043 NL_OPS_EXT(IBF_GET_STATUS, .ops=atenl_nl_ibf_get_status),
1044 NL_OPS_EXT(IBF_PROF_UPDATE_ALL, .ops=atenl_nl_ibf_profile_update_all),
1045};
1046#undef NL_OPS_EXT
1047
1048int atenl_nl_process(struct atenl *an, struct atenl_data *data)
1049{
1050 struct atenl_nl_priv nl_priv = {};
1051 const struct atenl_nl_ops *ops;
1052 struct nl_msg *msg;
1053 int ret = 0;
1054
1055 if (data->ext_cmd != 0)
1056 ops = &nl_ops_ext[data->ext_cmd];
1057 else
1058 ops = &nl_ops[data->cmd];
1059
1060 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developer5698c9c2022-05-30 16:40:23 +08001061 atenl_err("Failed to connect to nl80211\n");
developer3abe1ad2022-01-24 11:13:32 +08001062 return -1;
1063 }
1064
1065 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, !!ops->dump);
1066 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
1067 nl_priv.msg = msg;
1068
1069 if (ops->ops) {
1070 ret = ops->ops(an, data, &nl_priv);
1071 } else if (ops->dump) {
1072 nl_priv.attr = ops->dump;
1073 ret = atenl_nl_dump_attr(an, data, &nl_priv);
1074 } else {
1075 nl_priv.attr = ops->set;
1076 ret = atenl_nl_set_attr(an, data, &nl_priv);
1077 }
1078
1079 if (ret)
developer5698c9c2022-05-30 16:40:23 +08001080 atenl_err("command process error: 0x%x (0x%x)\n", data->cmd_id, data->ext_id);
developer3abe1ad2022-01-24 11:13:32 +08001081
1082 unl_free(&nl_priv.unl);
1083
1084 return ret;
1085}
1086
1087int atenl_nl_process_many(struct atenl *an, struct atenl_data *data)
1088{
1089 struct atenl_nl_priv nl_priv = {};
1090 const struct atenl_nl_ops *ops;
1091 int ret = 0;
1092
1093 if (data->ext_cmd != 0)
1094 ops = &nl_ops_ext[data->ext_cmd];
1095 else
1096 ops = &nl_ops[data->cmd];
1097
1098 if (ops->ops)
1099 ret = ops->ops(an, data, &nl_priv);
1100
1101 return ret;
1102}
1103
1104int atenl_nl_set_state(struct atenl *an, u8 band,
1105 enum mt76_testmode_state state)
1106{
1107 struct atenl_nl_priv nl_priv = {};
1108 struct nl_msg *msg;
1109 void *ptr;
1110
1111 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developer5698c9c2022-05-30 16:40:23 +08001112 atenl_err("Failed to connect to nl80211\n");
developer3abe1ad2022-01-24 11:13:32 +08001113 return 2;
1114 }
1115
1116 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1117 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
1118
1119 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1120 if (!ptr)
1121 return -ENOMEM;
1122
1123 atenl_set_attr_state(an, msg, band, state);
1124
1125 nla_nest_end(msg, ptr);
1126
1127 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1128
1129 unl_free(&nl_priv.unl);
1130
1131 return 0;
1132}
1133
developer5698c9c2022-05-30 16:40:23 +08001134int atenl_nl_set_aid(struct atenl *an, u8 band, u8 aid)
1135{
1136 struct atenl_nl_priv nl_priv = {};
1137 struct nl_msg *msg;
1138 void *ptr;
1139
1140 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1141 atenl_err("Failed to connect to nl80211\n");
1142 return 2;
1143 }
1144
1145 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1146 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
1147
1148 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1149 if (!ptr)
1150 return -ENOMEM;
1151
1152 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
1153
1154 nla_nest_end(msg, ptr);
1155
1156 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1157
1158 unl_free(&nl_priv.unl);
1159
1160 return 0;
1161}
1162
developer3abe1ad2022-01-24 11:13:32 +08001163static int atenl_nl_check_mtd_cb(struct nl_msg *msg, void *arg)
1164{
1165 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
1166 struct atenl *an = nl_priv->an;
1167 struct nlattr *tb[NUM_MT76_TM_ATTRS];
1168 struct nlattr *attr;
1169
1170 attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
1171 if (!attr)
1172 return NL_SKIP;
1173
1174 nla_parse_nested(tb, MT76_TM_ATTR_MAX, attr, testdata_policy);
1175 if (!tb[MT76_TM_ATTR_MTD_PART] || !tb[MT76_TM_ATTR_MTD_OFFSET])
1176 return NL_SKIP;
1177
1178 an->mtd_part = strdup(nla_get_string(tb[MT76_TM_ATTR_MTD_PART]));
1179 an->mtd_offset = nla_get_u32(tb[MT76_TM_ATTR_MTD_OFFSET]);
1180
1181 return NL_SKIP;
1182}
1183
1184int atenl_nl_check_mtd(struct atenl *an)
1185{
1186 struct atenl_nl_priv nl_priv = { .an = an };
1187 struct nl_msg *msg;
1188
1189 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developer5698c9c2022-05-30 16:40:23 +08001190 atenl_err("Failed to connect to nl80211\n");
developer3abe1ad2022-01-24 11:13:32 +08001191 return 2;
1192 }
1193
1194 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, true);
1195 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1196 unl_genl_request(&nl_priv.unl, msg, atenl_nl_check_mtd_cb, (void *)&nl_priv);
1197
1198 unl_free(&nl_priv.unl);
1199
1200 return 0;
1201}
1202
1203int atenl_nl_write_eeprom(struct atenl *an, u32 offset, u8 *val, int len)
1204{
1205 struct atenl_nl_priv nl_priv = {};
1206 struct nl_msg *msg;
1207 void *ptr, *a;
1208 int i;
1209
1210 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developer5698c9c2022-05-30 16:40:23 +08001211 atenl_err("Failed to connect to nl80211\n");
developer3abe1ad2022-01-24 11:13:32 +08001212 return 2;
1213 }
1214
1215 if (len > 16)
1216 return -EINVAL;
1217
1218 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1219 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1220
1221 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1222 if (!ptr)
1223 return -ENOMEM;
1224
1225 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1226 MT76_TM_EEPROM_ACTION_UPDATE_DATA);
1227 nla_put_u32(msg, MT76_TM_ATTR_EEPROM_OFFSET, offset);
1228
1229 a = nla_nest_start(msg, MT76_TM_ATTR_EEPROM_VAL);
1230 if (!a)
1231 return -ENOMEM;
1232
1233 for (i = 0; i < len; i++)
1234 if (nla_put_u8(msg, i, val[i]))
1235 goto out;
1236
1237 nla_nest_end(msg, a);
1238
1239 nla_nest_end(msg, ptr);
1240
1241 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1242
1243 unl_free(&nl_priv.unl);
1244
1245out:
1246 return 0;
1247}
1248
developer9b7cdad2022-03-10 14:24:55 +08001249int atenl_nl_write_efuse_all(struct atenl *an)
developer3abe1ad2022-01-24 11:13:32 +08001250{
1251 struct atenl_nl_priv nl_priv = {};
1252 struct nl_msg *msg;
1253 void *ptr;
1254
1255 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developer5698c9c2022-05-30 16:40:23 +08001256 atenl_err("Failed to connect to nl80211\n");
developer3abe1ad2022-01-24 11:13:32 +08001257 return 2;
1258 }
1259
1260 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1261 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1262
1263 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1264 if (!ptr)
1265 return -ENOMEM;
1266
1267 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1268 MT76_TM_EEPROM_ACTION_WRITE_TO_EFUSE);
1269
1270 nla_nest_end(msg, ptr);
1271
1272 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1273
1274 unl_free(&nl_priv.unl);
1275
1276 return 0;
1277}
1278
1279int atenl_nl_update_buffer_mode(struct atenl *an)
1280{
1281 struct atenl_nl_priv nl_priv = {};
1282 struct nl_msg *msg;
1283 void *ptr;
1284
1285 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
developer5698c9c2022-05-30 16:40:23 +08001286 atenl_err("Failed to connect to nl80211\n");
developer3abe1ad2022-01-24 11:13:32 +08001287 return 2;
1288 }
1289
1290 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1291 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1292
1293 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1294 if (!ptr)
1295 return -ENOMEM;
1296
1297 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1298 MT76_TM_EEPROM_ACTION_UPDATE_BUFFER_MODE);
1299
1300 nla_nest_end(msg, ptr);
1301
1302 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1303
1304 unl_free(&nl_priv.unl);
1305
1306 return 0;
1307}
1308