blob: 47e3d76e72b6e394692524f3b4603dc124d50e2a [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;
217 u8 default_addr[ETH_ALEN] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
218 void *ptr, *a;
219
220 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
221 if (!ptr)
222 return -ENOMEM;
223
224 if (get_band_val(an, an->cur_band, use_tx_time))
225 nla_put_u32(msg, MT76_TM_ATTR_TX_TIME, ntohl(v[7]));
226 else
227 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH, ntohl(v[7]));
228
229 a = nla_nest_start(msg, MT76_TM_ATTR_MAC_ADDRS);
230 if (!a)
231 return -ENOMEM;
232
233 if (is_multicast_ether_addr(addr1))
234 nla_put(msg, 0, ETH_ALEN, default_addr);
235 else
236 nla_put(msg, 0, ETH_ALEN, addr1);
237 nla_put(msg, 1, ETH_ALEN, addr2);
238 nla_put(msg, 2, ETH_ALEN, addr3);
239
240 nla_nest_end(msg, a);
241
242 nla_nest_end(msg, ptr);
243
244 *(u32 *)(hdr->data + 2) = data->ext_id;
245
246 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
247}
248
249static int
250atenl_nl_tx(struct atenl *an, struct atenl_data *data, struct atenl_nl_priv *nl_priv)
251{
252#define USE_SPE_IDX BIT(31)
253 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
254 struct nl_msg *msg = nl_priv->msg;
255 u32 *v = (u32 *)hdr->data;
256 u8 band = ntohl(v[2]);
257 void *ptr;
258 int ret;
259
260 if (band >= MAX_BAND_NUM)
261 return -EINVAL;
262
263 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
264 if (!ptr)
265 return -ENOMEM;
266
267 if (data->ext_cmd == HQA_EXT_CMD_STOP_TX) {
268 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
269 } else {
270 u32 tx_count = ntohl(v[3]);
271 u8 tx_rate_mode = phy_type_to_attr(ntohl(v[4]));
272 u8 aid = ntohl(v[11]);
273 u8 sgi = ntohl(v[13]);
274 u32 tx_antenna = ntohl(v[14]);
275 void *a;
276
277 if (sgi > 5)
278 return -EINVAL;
279
280 if (!tx_count)
281 tx_count = 10000000;
282
283 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, tx_count);
284 nla_put_u32(msg, MT76_TM_ATTR_TX_IPG, ntohl(v[12]));
285 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, tx_rate_mode);
286 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, ntohl(v[5]));
287 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_STBC, ntohl(v[7]));
288 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_LDPC, ntohl(v[8]));
289 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_NSS, ntohl(v[15]));
290
291 /* for chips after 7915, tx need to use at least wcid = 1 */
292 if (!is_mt7915(an) && !aid)
293 aid = 1;
294 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
295
296 if (tx_antenna & USE_SPE_IDX) {
297 nla_put_u8(msg, MT76_TM_ATTR_TX_SPE_IDX,
298 tx_antenna & ~USE_SPE_IDX);
299 } else {
300 nla_put_u8(msg, MT76_TM_ATTR_TX_SPE_IDX, 0);
301 atenl_set_attr_antenna(an, msg, tx_antenna);
302 }
303
304 if (tx_rate_mode >= MT76_TM_TX_MODE_HE_SU) {
305 u8 ofs = sgi;
306 size_t i;
307
308 for (i = 0; i < ARRAY_SIZE(he_sgi_groups); i++)
309 if (he_sgi_groups[i].tx_mode == tx_rate_mode)
310 break;
311
312 if ((i + ofs) >= ARRAY_SIZE(he_sgi_groups))
313 return -EINVAL;
314
315 sgi = he_sgi_groups[i + ofs].sgi;
316 nla_put_u8(msg, MT76_TM_ATTR_TX_LTF,
317 he_sgi_groups[i + ofs].tx_ltf);
318 }
319 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_SGI, sgi);
320
321 a = nla_nest_start(msg, MT76_TM_ATTR_TX_POWER);
322 if (!a)
323 return -ENOMEM;
324 nla_put_u8(msg, 0, ntohl(v[6]));
325 nla_nest_end(msg, a);
326
327 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_TX_FRAMES);
328 }
329
330 nla_nest_end(msg, ptr);
331
332 ret = unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
333 if (ret)
334 return ret;
335
336 *(u32 *)(hdr->data + 2) = data->ext_id;
337
338 return 0;
339}
340
341static int
342atenl_nl_rx(struct atenl *an, struct atenl_data *data, struct atenl_nl_priv *nl_priv)
343{
344 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
345 struct atenl_band *anb = &an->anb[an->cur_band];
346 struct nl_msg *msg = nl_priv->msg;
347 u32 *v = (u32 *)hdr->data;
348 u8 band = ntohl(v[2]);
349 void *ptr;
350
351 if (band >= MAX_BAND_NUM)
352 return -EINVAL;
353
354 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
355 if (!ptr)
356 return -ENOMEM;
357
358 if (data->ext_cmd == HQA_EXT_CMD_STOP_RX) {
359 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
360 } else {
361 v = (u32 *)(hdr->data + 18);
362
363 atenl_set_attr_antenna(an, msg, ntohl(v[0]));
364 nla_put_u8(msg, MT76_TM_ATTR_AID, ntohl(v[1]));
365 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_RX_FRAMES);
366
367 anb->reset_rx_cnt = false;
368
369 /* clear history buffer */
370 memset(&anb->rx_stat, 0, sizeof(anb->rx_stat));
371 }
372
373 nla_nest_end(msg, ptr);
374
375 *(u32 *)(hdr->data + 2) = data->ext_id;
376
377 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
378}
379
380static int
381atenl_off_ch_scan(struct atenl *an, struct atenl_data *data,
382 struct atenl_nl_priv *nl_priv)
383{
384 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
385 struct nl_msg *msg = nl_priv->msg;
386 u32 *v = (u32 *)hdr->data;
387 u8 ch = ntohl(v[2]);
388 u8 bw = ntohl(v[4]);
389 u8 tx_path = ntohl(v[5]);
390 u8 status = ntohl(v[6]);
391 void *ptr;
392
393 if (!status)
394 ch = 0; /* stop */
395
396 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
397 if (!ptr)
398 return -ENOMEM;
399
400 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_CH, ch);
401 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_CENTER_CH,
402 atenl_get_center_channel(bw, CH_BAND_5GHZ, ch));
403 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_BW, bw);
404 nla_put_u8(msg, MT76_TM_ATTR_OFF_CH_SCAN_PATH, tx_path);
405
406 nla_nest_end(msg, ptr);
407
408 memcpy(hdr->data + 2, &data->ext_id, 4);
409
410 return 0;
411}
412
413static int atenl_nl_dump_cb(struct nl_msg *msg, void *arg)
414{
415 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
416 struct nlattr *tb1[NUM_MT76_TM_ATTRS];
417 struct nlattr *tb2[NUM_MT76_TM_STATS_ATTRS];
418 struct nlattr *nl_attr;
419 int attr = nl_priv->attr;
420 u64 *res = nl_priv->res;
421
422 nl_attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
423 if (!nl_attr) {
424 fprintf(stderr, "Testdata attribute not found\n");
425 return NL_SKIP;
426 }
427
428 nla_parse_nested(tb1, MT76_TM_ATTR_MAX, nl_attr, testdata_policy);
429 nla_parse_nested(tb2, MT76_TM_STATS_ATTR_MAX,
430 tb1[MT76_TM_ATTR_STATS], stats_policy);
431
432 if (attr == MT76_TM_STATS_ATTR_TX_DONE)
433 *res = nla_get_u32(tb2[MT76_TM_STATS_ATTR_TX_DONE]);
434
435 return NL_SKIP;
436}
437
438static int
439atenl_nl_dump_attr(struct atenl *an, struct atenl_data *data,
440 struct atenl_nl_priv *nl_priv)
441{
442 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
443 struct nl_msg *msg = nl_priv->msg;
444 void *ptr;
445 u64 res = 0;
446
447 nl_priv->res = (void *)&res;
448
449 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
450 if (!ptr)
451 return -ENOMEM;
452 nla_put_flag(msg, MT76_TM_ATTR_STATS);
453 nla_nest_end(msg, ptr);
454
455 unl_genl_request(&nl_priv->unl, msg, atenl_nl_dump_cb, (void *)nl_priv);
456
457 if (nl_priv->attr == MT76_TM_STATS_ATTR_TX_DONE)
458 *(u32 *)(hdr->data + 2 + 4 * an->cur_band) = htonl(res);
459
460 return 0;
461}
462
463static int atenl_nl_continuous_tx(struct atenl *an,
464 struct atenl_data *data,
465 struct atenl_nl_priv *nl_priv)
466{
467 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
468 struct nl_msg *msg = nl_priv->msg;
469 u32 *v = (u32 *)hdr->data;
470 u8 band = ntohl(v[0]);
471 bool enable = ntohl(v[1]);
472 void *ptr;
473
474 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
475 if (!ptr)
476 return -ENOMEM;
477
478 if (band >= MAX_BAND_NUM)
479 return -EINVAL;
480
481 if (!enable) {
482 int phy = get_band_val(an, band, phy_idx);
483 char cmd[64];
484
485 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_IDLE);
486 nla_nest_end(msg, ptr);
487 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
488
489 sprintf(cmd, "iw dev mon%d del", phy);
490 system(cmd);
491 sprintf(cmd, "iw phy phy%d interface add mon%d type monitor", phy, phy);
492 system(cmd);
493 sprintf(cmd, "ifconfig mon%d up", phy);
494 system(cmd);
495
496 return 0;
497 }
498
499 if (get_band_val(an, band, rf_mode) != ATENL_RF_MODE_TEST)
500 return 0;
501
502 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, ntohl(v[2]));
503 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, phy_type_to_attr(ntohl(v[3])));
504 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, ntohl(v[6]));
505
506 atenl_dbg("%s: enable = %d, ant=%u, tx_rate_mode=%u, rate_idx=%u\n",
507 __func__, enable, ntohl(v[2]), ntohl(v[3]), ntohl(v[6]));
508
509 atenl_set_attr_state(an, msg, band, MT76_TM_STATE_TX_CONT);
510
511 nla_nest_end(msg, ptr);
512
513 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
514}
515
516static int atenl_nl_get_rx_info_cb(struct nl_msg *msg, void *arg)
517{
518 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
519 struct atenl *an = nl_priv->an;
520 struct atenl_band *anb = &an->anb[an->cur_band];
521 struct atenl_data *data = nl_priv->res;
522 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
523 struct atenl_rx_info_hdr *rx_hdr;
524 struct atenl_rx_info_band *rx_band;
525 struct atenl_rx_info_user *rx_user;
526 struct atenl_rx_info_path *rx_path;
527 struct atenl_rx_info_comm *rx_comm;
528 struct nlattr *tb1[NUM_MT76_TM_ATTRS];
529 struct nlattr *tb2[NUM_MT76_TM_STATS_ATTRS];
530 struct nlattr *tb3[NUM_MT76_TM_RX_ATTRS];
531 struct nlattr *nl_attr, *cur;
532 struct atenl_rx_stat rx_cur, rx_diff = {};
533 u32 rcpi[4] = {};
534 u32 type_num = htonl(4);
535 s32 ib_rssi[4] = {}, wb_rssi[4] = {};
536 u8 path = an->anb[an->cur_band].chainmask;
537 u8 path_num = __builtin_popcount(path);
538 u8 *buf = hdr->data + 2;
539 int i, rem;
540
541 *(u32 *)buf = type_num;
542 buf += sizeof(type_num);
543
544#define RX_PUT_HDR(_hdr, _type, _val, _size) do { \
545 _hdr->type = htonl(_type); \
546 _hdr->val = htonl(_val); \
547 _hdr->len = htonl(_size); \
548 buf += sizeof(*_hdr); \
549 } while (0)
550
551 rx_hdr = (struct atenl_rx_info_hdr *)buf;
552 RX_PUT_HDR(rx_hdr, 0, BIT(an->cur_band), sizeof(*rx_band));
553 rx_band = (struct atenl_rx_info_band *)buf;
554 buf += sizeof(*rx_band);
555
556 rx_hdr = (struct atenl_rx_info_hdr *)buf;
557 RX_PUT_HDR(rx_hdr, 1, path, path_num * sizeof(*rx_path));
558 rx_path = (struct atenl_rx_info_path *)buf;
559 buf += path_num * sizeof(*rx_path);
560
561 rx_hdr = (struct atenl_rx_info_hdr *)buf;
562 RX_PUT_HDR(rx_hdr, 2, GENMASK(15, 0), 16 * sizeof(*rx_user));
563 rx_user = (struct atenl_rx_info_user *)buf;
564 buf += 16 * sizeof(*rx_user);
565
566 rx_hdr = (struct atenl_rx_info_hdr *)buf;
567 RX_PUT_HDR(rx_hdr, 3, BIT(0), sizeof(*rx_comm));
568 rx_comm = (struct atenl_rx_info_comm *)buf;
569 buf += sizeof(*rx_comm);
570
571 hdr->len = htons(buf - hdr->data);
572
573 nl_attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
574 if (!nl_attr) {
575 fprintf(stderr, "Testdata attribute not found\n");
576 return NL_SKIP;
577 }
578
579 nla_parse_nested(tb1, MT76_TM_ATTR_MAX, nl_attr, testdata_policy);
580 nla_parse_nested(tb2, MT76_TM_STATS_ATTR_MAX,
581 tb1[MT76_TM_ATTR_STATS], stats_policy);
582
583 rx_cur.total = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_PACKETS]);
584 rx_cur.err_cnt = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_FCS_ERROR]);
585 rx_cur.len_mismatch = nla_get_u64(tb2[MT76_TM_STATS_ATTR_RX_LEN_MISMATCH]);
586 rx_cur.ok_cnt = rx_cur.total - rx_cur.err_cnt - rx_cur.len_mismatch;
587
588 if (!anb->reset_rx_cnt) {
589#define RX_COUNT_DIFF(_field) \
590 rx_diff._field = (rx_cur._field) - (anb->rx_stat._field)
591 RX_COUNT_DIFF(total);
592 RX_COUNT_DIFF(err_cnt);
593 RX_COUNT_DIFF(len_mismatch);
594 RX_COUNT_DIFF(ok_cnt);
595#undef RX_COUNT_DIFF
596
597 memcpy(&anb->rx_stat, &rx_cur, sizeof(anb->rx_stat));
598 }
599
600 rx_band->mac_rx_mdrdy_cnt = htonl((u32)rx_diff.total);
601 rx_band->mac_rx_fcs_err_cnt = htonl((u32)rx_diff.err_cnt);
602 rx_band->mac_rx_fcs_ok_cnt = htonl((u32)rx_diff.ok_cnt);
603 rx_band->mac_rx_len_mismatch = htonl((u32)rx_diff.len_mismatch);
604 rx_user->fcs_error_cnt = htonl((u32)rx_diff.err_cnt);
605
606 nla_parse_nested(tb3, MT76_TM_RX_ATTR_MAX,
607 tb2[MT76_TM_STATS_ATTR_LAST_RX], rx_policy);
608
609 rx_user->freq_offset = htonl(nla_get_u32(tb3[MT76_TM_RX_ATTR_FREQ_OFFSET]));
610 rx_user->snr = htonl(nla_get_u8(tb3[MT76_TM_RX_ATTR_SNR]));
611
612 i = 0;
613 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_RCPI], rem) {
614 if (nla_len(cur) != 1 || i >= 4)
615 break;
616
617 rcpi[i++] = nla_get_u8(cur);
618 }
619
620 i = 0;
621 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_IB_RSSI], rem) {
622 if (nla_len(cur) != 1 || i >= 4)
623 break;
624
625 ib_rssi[i++] = (s8)nla_get_u8(cur);
626 }
627
628 i = 0;
629 nla_for_each_nested(cur, tb3[MT76_TM_RX_ATTR_WB_RSSI], rem) {
630 if (nla_len(cur) != 1 || i >= 4)
631 break;
632
633 wb_rssi[i++] = (s8)nla_get_u8(cur);
634 }
635
636 for (i = 0; i < 4; i++) {
637 struct atenl_rx_info_path *path = &rx_path[i];
638
639 path->rcpi = htonl(rcpi[i]);
640 path->rssi = htonl(to_rssi((u8)rcpi[i]));
641 path->fagc_ib_rssi = htonl(ib_rssi[i]);
642 path->fagc_wb_rssi = htonl(wb_rssi[i]);
643 }
644
645 return NL_SKIP;
646}
647
648static int atenl_nl_get_rx_info(struct atenl *an, struct atenl_data *data,
649 struct atenl_nl_priv *nl_priv)
650{
651 struct nl_msg *msg = nl_priv->msg;
652 void *ptr;
653
654 nl_priv->an = an;
655 nl_priv->res = (void *)data;
656
657 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
658 if (!ptr)
659 return -ENOMEM;
660
661 nla_put_flag(msg, MT76_TM_ATTR_STATS);
662
663 nla_nest_end(msg, ptr);
664
665 return unl_genl_request(&nl_priv->unl, msg, atenl_nl_get_rx_info_cb,
666 (void *)nl_priv);
667}
668
669static int
670atenl_nl_set_ru(struct atenl *an, struct atenl_data *data,
671 struct atenl_nl_priv *nl_priv)
672{
673 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
674 struct nl_msg *msg;
675 u32 *v = (u32 *)(hdr->data + 4);
676 u32 seg0_num = ntohl(v[0]); /* v[1] seg1_num unused */
677 void *ptr;
678 int i, ret;
679
680 if (seg0_num > 8)
681 return -EINVAL;
682
683 for (i = 0, v = &v[2]; i < seg0_num; i++, v += 11) {
684 u32 ru_alloc = ntohl(v[1]);
685 u32 aid = ntohl(v[2]);
686 u32 ru_idx = ntohl(v[3]);
687 u32 mcs = ntohl(v[4]);
688 u32 ldpc = ntohl(v[5]);
689 u32 nss = ntohl(v[6]);
690 u32 tx_length = ntohl(v[8]);
691 char buf[10];
692
693 if (unl_genl_init(&nl_priv->unl, "nl80211") < 0) {
694 fprintf(stderr, "Failed to connect to nl80211\n");
695 return 2;
696 }
697
698 msg = unl_genl_msg(&nl_priv->unl, NL80211_CMD_TESTMODE, false);
699 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
700
701 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
702 if (!ptr)
703 return -ENOMEM;
704
705 if (i == 0)
706 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_IDLE);
707
708 nla_put_u8(msg, MT76_TM_ATTR_AID, aid);
709 nla_put_u8(msg, MT76_TM_ATTR_RU_IDX, ru_idx);
710 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, mcs);
711 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_LDPC, ldpc);
712 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_NSS, nss);
713 nla_put_u32(msg, MT76_TM_ATTR_TX_LENGTH, tx_length);
714
715 ret = snprintf(buf, sizeof(buf), "%x", ru_alloc);
716 if (snprintf_error(sizeof(buf), ret))
717 return -EINVAL;
718
719 nla_put_u8(msg, MT76_TM_ATTR_RU_ALLOC, strtol(buf, NULL, 2));
720
721 nla_nest_end(msg, ptr);
722
723 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
724
725 unl_free(&nl_priv->unl);
726 }
727
728 return 0;
729}
730
731static int atenl_nl_ibf_set_val(struct atenl *an, struct atenl_data *data,
732 struct atenl_nl_priv *nl_priv)
733{
734#define MT_IBF(_act) MT76_TM_TXBF_ACT_##_act
735 static const u8 bf_act_map[] = {
736 [TXBF_ACT_IBF_PHASE_COMP] = MT_IBF(PHASE_COMP),
737 [TXBF_ACT_IBF_PROF_UPDATE] = MT_IBF(IBF_PROF_UPDATE),
738 [TXBF_ACT_EBF_PROF_UPDATE] = MT_IBF(EBF_PROF_UPDATE),
739 [TXBF_ACT_IBF_PHASE_CAL] = MT_IBF(PHASE_CAL),
740 };
741#undef MT_IBF
742 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
743 struct nl_msg *msg = nl_priv->msg;
744 u32 *v = (u32 *)(hdr->data + 4);
745 u32 action = ntohl(v[0]);
746 u16 val[8];
747 void *ptr, *a;
748 char cmd[64];
749 int i;
750
751 for (i = 0; i < 8; i++)
752 val[i] = ntohl(v[i + 1]);
753
754 atenl_dbg("%s: action = %u, val = %u, %u, %u, %u, %u\n",
755 __func__, action, val[0], val[1], val[2], val[3], val[4]);
756
757 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
758 if (!ptr)
759 return -ENOMEM;
760
761 switch (action) {
762 case TXBF_ACT_INIT:
763 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_MODE, MT76_TM_TX_MODE_HT);
764 nla_put_u8(msg, MT76_TM_ATTR_AID, 1);
765 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_INIT);
766
767 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
768 if (!a)
769 return -ENOMEM;
770 nla_put_u16(msg, 0, 1);
771 if (!val[0])
772 nla_put_u16(msg, 1, 1); /* init */
773 nla_nest_end(msg, a);
774 break;
775 case TXBF_ACT_CHANNEL:
776 sprintf(cmd, "iw dev mon%d set channel %u HT20",
777 get_band_val(an, an->cur_band, phy_idx), val[0]);
778 system(cmd);
779 printf("%s: %s", __func__, cmd);
780 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_UPDATE_CH);
781 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
782 if (!a)
783 return -ENOMEM;
784 nla_put_u16(msg, 0, 0);
785 nla_nest_end(msg, a);
786 break;
787 case TXBF_ACT_MCS:
788 nla_put_u8(msg, MT76_TM_ATTR_TX_RATE_IDX, val[0]);
789 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, (1 << DIV_ROUND_UP(val[0], 8)) - 1);
790 break;
791 case TXBF_ACT_POWER:
792 break;
793 case TXBF_ACT_TX_ANT:
794 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, val[0]);
795 break;
796 case TXBF_ACT_RX_START:
797 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_RX_FRAMES);
798 break;
799 case TXBF_ACT_RX_ANT:
800 nla_put_u8(msg, MT76_TM_ATTR_TX_ANTENNA, val[0]);
801 break;
802 case TXBF_ACT_TX_PKT:
803 nla_put_u8(msg, MT76_TM_ATTR_AID, val[1]);
804 nla_put_u32(msg, MT76_TM_ATTR_TX_COUNT, 10000000);
805 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_TX_PREP);
806 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
807 if (!a)
808 return -ENOMEM;
809
810 for (i = 0; i < 5; i++)
811 nla_put_u16(msg, i, val[i]);
812 nla_nest_end(msg, a);
813
814 atenl_set_attr_state(an, msg, an->cur_band, MT76_TM_STATE_TX_FRAMES);
815 break;
816 case TXBF_ACT_IBF_PHASE_COMP:
817 case TXBF_ACT_IBF_PROF_UPDATE:
818 case TXBF_ACT_EBF_PROF_UPDATE:
819 case TXBF_ACT_IBF_PHASE_CAL:
820 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, bf_act_map[action]);
821 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
822 if (!a)
823 return -ENOMEM;
824
825 for (i = 0; i < 5; i++)
826 nla_put_u16(msg, i, val[i]);
827 nla_nest_end(msg, a);
828 break;
829 case TXBF_ACT_IBF_PHASE_E2P_UPDATE:
830 atenl_eeprom_read_from_driver(an, 0x651, 0x28 * 9);
831 atenl_eeprom_write_mtd(an);
832
833 nla_put_u8(msg, MT76_TM_ATTR_AID, 0);
834 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_INIT);
835
836 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
837 if (!a)
838 return -ENOMEM;
839 nla_put_u16(msg, 0, 0);
840 nla_nest_end(msg, a);
841 break;
842 default:
843 break;
844 }
845
846 nla_nest_end(msg, ptr);
847
848 memcpy(hdr->data + 2, &data->ext_id, 4);
849
850 return unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
851}
852
853static int
854atenl_nl_ibf_get_status(struct atenl *an, struct atenl_data *data,
855 struct atenl_nl_priv *nl_priv)
856{
857 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
858 u32 status = htonl(1);
859
860 memcpy(hdr->data + 2, &data->ext_id, 4);
861 memcpy(hdr->data + 6, &status, 4);
862
863 return 0;
864}
865
866static int
867atenl_nl_ibf_profile_update_all(struct atenl *an, struct atenl_data *data,
868 struct atenl_nl_priv *nl_priv)
869{
870 struct atenl_cmd_hdr *hdr = atenl_hdr(data);
871 struct nl_msg *msg;
872 void *ptr, *a;
873 u32 *v = (u32 *)(hdr->data + 4);
874 u16 pfmu_idx = ntohl(v[0]);
875 int i;
876
877 for (i = 0, v = &v[5]; i < 64; i++, v += 5) {
878 int j;
879
880 if (unl_genl_init(&nl_priv->unl, "nl80211") < 0) {
881 fprintf(stderr, "Failed to connect to nl80211\n");
882 return 2;
883 }
884
885 msg = unl_genl_msg(&nl_priv->unl, NL80211_CMD_TESTMODE, false);
886 nla_put_u32(msg, NL80211_ATTR_WIPHY,
887 get_band_val(an, an->cur_band, phy_idx));
888
889 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
890 if (!ptr)
891 return -ENOMEM;
892
893 nla_put_u8(msg, MT76_TM_ATTR_TXBF_ACT, MT76_TM_TXBF_ACT_PROF_UPDATE_ALL);
894 a = nla_nest_start(msg, MT76_TM_ATTR_TXBF_PARAM);
895 if (!a)
896 return -ENOMEM;
897 nla_put_u16(msg, 0, pfmu_idx);
898
899 for (j = 0; j < 5; j++)
900 nla_put_u16(msg, j + 1, ntohl(v[j]));
901 nla_nest_end(msg, a);
902
903 nla_nest_end(msg, ptr);
904
905 unl_genl_request(&nl_priv->unl, msg, NULL, NULL);
906
907 unl_free(&nl_priv->unl);
908 }
909
910 memcpy(hdr->data + 2, &data->ext_id, 4);
911
912 return 0;
913}
914
915#define NL_OPS_GROUP(cmd, ...) [HQA_CMD_##cmd] = { __VA_ARGS__ }
916static const struct atenl_nl_ops nl_ops[] = {
917 NL_OPS_GROUP(SET_TX_PATH, .set=MT76_TM_ATTR_TX_ANTENNA),
918 NL_OPS_GROUP(SET_TX_POWER, .set=MT76_TM_ATTR_TX_POWER),
919 NL_OPS_GROUP(SET_RX_PATH, .set=MT76_TM_ATTR_TX_ANTENNA),
920 NL_OPS_GROUP(SET_FREQ_OFFSET, .set=MT76_TM_ATTR_FREQ_OFFSET),
921 NL_OPS_GROUP(SET_CFG, .ops=atenl_nl_set_cfg),
922 NL_OPS_GROUP(SET_TSSI, .ops=atenl_nl_set_cfg),
923 NL_OPS_GROUP(CONTINUOUS_TX, .ops=atenl_nl_continuous_tx),
924 NL_OPS_GROUP(GET_TX_INFO, .dump=MT76_TM_STATS_ATTR_TX_DONE),
925 NL_OPS_GROUP(GET_RX_INFO, .ops=atenl_nl_get_rx_info, .dump=true),
926 NL_OPS_GROUP(SET_RU, .ops=atenl_nl_set_ru),
927};
928#undef NL_OPS_GROUP
929
930#define NL_OPS_EXT(cmd, ...) [HQA_EXT_CMD_##cmd] = { __VA_ARGS__ }
931static const struct atenl_nl_ops nl_ops_ext[] = {
932 NL_OPS_EXT(SET_TX, .ops=atenl_nl_set_tx),
933 NL_OPS_EXT(START_TX, .ops=atenl_nl_tx),
934 NL_OPS_EXT(STOP_TX, .ops=atenl_nl_tx),
935 NL_OPS_EXT(START_RX, .ops=atenl_nl_rx),
936 NL_OPS_EXT(STOP_RX, .ops=atenl_nl_rx),
937 NL_OPS_EXT(OFF_CH_SCAN, .ops=atenl_off_ch_scan),
938 NL_OPS_EXT(IBF_SET_VAL, .ops=atenl_nl_ibf_set_val),
939 NL_OPS_EXT(IBF_GET_STATUS, .ops=atenl_nl_ibf_get_status),
940 NL_OPS_EXT(IBF_PROF_UPDATE_ALL, .ops=atenl_nl_ibf_profile_update_all),
941};
942#undef NL_OPS_EXT
943
944int atenl_nl_process(struct atenl *an, struct atenl_data *data)
945{
946 struct atenl_nl_priv nl_priv = {};
947 const struct atenl_nl_ops *ops;
948 struct nl_msg *msg;
949 int ret = 0;
950
951 if (data->ext_cmd != 0)
952 ops = &nl_ops_ext[data->ext_cmd];
953 else
954 ops = &nl_ops[data->cmd];
955
956 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
957 fprintf(stderr, "Failed to connect to nl80211\n");
958 return -1;
959 }
960
961 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, !!ops->dump);
962 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, an->cur_band, phy_idx));
963 nl_priv.msg = msg;
964
965 if (ops->ops) {
966 ret = ops->ops(an, data, &nl_priv);
967 } else if (ops->dump) {
968 nl_priv.attr = ops->dump;
969 ret = atenl_nl_dump_attr(an, data, &nl_priv);
970 } else {
971 nl_priv.attr = ops->set;
972 ret = atenl_nl_set_attr(an, data, &nl_priv);
973 }
974
975 if (ret)
976 atenl_err("command process error: %d (%d)\n", data->cmd, data->ext_cmd);
977
978 unl_free(&nl_priv.unl);
979
980 return ret;
981}
982
983int atenl_nl_process_many(struct atenl *an, struct atenl_data *data)
984{
985 struct atenl_nl_priv nl_priv = {};
986 const struct atenl_nl_ops *ops;
987 int ret = 0;
988
989 if (data->ext_cmd != 0)
990 ops = &nl_ops_ext[data->ext_cmd];
991 else
992 ops = &nl_ops[data->cmd];
993
994 if (ops->ops)
995 ret = ops->ops(an, data, &nl_priv);
996
997 return ret;
998}
999
1000int atenl_nl_set_state(struct atenl *an, u8 band,
1001 enum mt76_testmode_state state)
1002{
1003 struct atenl_nl_priv nl_priv = {};
1004 struct nl_msg *msg;
1005 void *ptr;
1006
1007 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1008 fprintf(stderr, "Failed to connect to nl80211\n");
1009 return 2;
1010 }
1011
1012 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1013 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, band, phy_idx));
1014
1015 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1016 if (!ptr)
1017 return -ENOMEM;
1018
1019 atenl_set_attr_state(an, msg, band, state);
1020
1021 nla_nest_end(msg, ptr);
1022
1023 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1024
1025 unl_free(&nl_priv.unl);
1026
1027 return 0;
1028}
1029
1030static int atenl_nl_check_mtd_cb(struct nl_msg *msg, void *arg)
1031{
1032 struct atenl_nl_priv *nl_priv = (struct atenl_nl_priv *)arg;
1033 struct atenl *an = nl_priv->an;
1034 struct nlattr *tb[NUM_MT76_TM_ATTRS];
1035 struct nlattr *attr;
1036
1037 attr = unl_find_attr(&nl_priv->unl, msg, NL80211_ATTR_TESTDATA);
1038 if (!attr)
1039 return NL_SKIP;
1040
1041 nla_parse_nested(tb, MT76_TM_ATTR_MAX, attr, testdata_policy);
1042 if (!tb[MT76_TM_ATTR_MTD_PART] || !tb[MT76_TM_ATTR_MTD_OFFSET])
1043 return NL_SKIP;
1044
1045 an->mtd_part = strdup(nla_get_string(tb[MT76_TM_ATTR_MTD_PART]));
1046 an->mtd_offset = nla_get_u32(tb[MT76_TM_ATTR_MTD_OFFSET]);
1047
1048 return NL_SKIP;
1049}
1050
1051int atenl_nl_check_mtd(struct atenl *an)
1052{
1053 struct atenl_nl_priv nl_priv = { .an = an };
1054 struct nl_msg *msg;
1055
1056 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1057 fprintf(stderr, "Failed to connect to nl80211\n");
1058 return 2;
1059 }
1060
1061 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, true);
1062 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1063 unl_genl_request(&nl_priv.unl, msg, atenl_nl_check_mtd_cb, (void *)&nl_priv);
1064
1065 unl_free(&nl_priv.unl);
1066
1067 return 0;
1068}
1069
1070int atenl_nl_write_eeprom(struct atenl *an, u32 offset, u8 *val, int len)
1071{
1072 struct atenl_nl_priv nl_priv = {};
1073 struct nl_msg *msg;
1074 void *ptr, *a;
1075 int i;
1076
1077 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1078 fprintf(stderr, "Failed to connect to nl80211\n");
1079 return 2;
1080 }
1081
1082 if (len > 16)
1083 return -EINVAL;
1084
1085 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1086 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1087
1088 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1089 if (!ptr)
1090 return -ENOMEM;
1091
1092 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1093 MT76_TM_EEPROM_ACTION_UPDATE_DATA);
1094 nla_put_u32(msg, MT76_TM_ATTR_EEPROM_OFFSET, offset);
1095
1096 a = nla_nest_start(msg, MT76_TM_ATTR_EEPROM_VAL);
1097 if (!a)
1098 return -ENOMEM;
1099
1100 for (i = 0; i < len; i++)
1101 if (nla_put_u8(msg, i, val[i]))
1102 goto out;
1103
1104 nla_nest_end(msg, a);
1105
1106 nla_nest_end(msg, ptr);
1107
1108 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1109
1110 unl_free(&nl_priv.unl);
1111
1112out:
1113 return 0;
1114}
1115
1116int atenl_nl_write_efuse_all(struct atenl *an, struct atenl_data *data)
1117{
1118 struct atenl_nl_priv nl_priv = {};
1119 struct nl_msg *msg;
1120 void *ptr;
1121
1122 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1123 fprintf(stderr, "Failed to connect to nl80211\n");
1124 return 2;
1125 }
1126
1127 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1128 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1129
1130 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1131 if (!ptr)
1132 return -ENOMEM;
1133
1134 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1135 MT76_TM_EEPROM_ACTION_WRITE_TO_EFUSE);
1136
1137 nla_nest_end(msg, ptr);
1138
1139 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1140
1141 unl_free(&nl_priv.unl);
1142
1143 return 0;
1144}
1145
1146int atenl_nl_update_buffer_mode(struct atenl *an)
1147{
1148 struct atenl_nl_priv nl_priv = {};
1149 struct nl_msg *msg;
1150 void *ptr;
1151
1152 if (unl_genl_init(&nl_priv.unl, "nl80211") < 0) {
1153 fprintf(stderr, "Failed to connect to nl80211\n");
1154 return 2;
1155 }
1156
1157 msg = unl_genl_msg(&nl_priv.unl, NL80211_CMD_TESTMODE, false);
1158 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_band_val(an, 0, phy_idx));
1159
1160 ptr = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
1161 if (!ptr)
1162 return -ENOMEM;
1163
1164 nla_put_u8(msg, MT76_TM_ATTR_EEPROM_ACTION,
1165 MT76_TM_EEPROM_ACTION_UPDATE_BUFFER_MODE);
1166
1167 nla_nest_end(msg, ptr);
1168
1169 unl_genl_request(&nl_priv.unl, msg, NULL, NULL);
1170
1171 unl_free(&nl_priv.unl);
1172
1173 return 0;
1174}
1175