Debian Codegraph @

drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.c

raw
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /* Copyright (C) 2020 MediaTek Inc. */
3
4 #include <linux/firmware.h>
5 #include "mt76_connac2_mac.h"
6 #include "mt76_connac_mcu.h"
7
8 int mt76_connac_mcu_start_firmware(struct mt76_dev *dev, u32 addr, u32 option)
9 {
10 struct {
11 __le32 option;
12 __le32 addr;
13 } req = {
14 .option = cpu_to_le32(option),
15 .addr = cpu_to_le32(addr),
16 };
17
18 return mt76_mcu_send_msg(dev, MCU_CMD(FW_START_REQ), &req,
19 sizeof(req), true);
20 }
21 EXPORT_SYMBOL_GPL(mt76_connac_mcu_start_firmware);
22
23 int mt76_connac_mcu_patch_sem_ctrl(struct mt76_dev *dev, bool get)
24 {
25 u32 op = get ? PATCH_SEM_GET : PATCH_SEM_RELEASE;
26 struct {
27 __le32 op;
28 } req = {
29 .op = cpu_to_le32(op),
30 };
31
32 return mt76_mcu_send_msg(dev, MCU_CMD(PATCH_SEM_CONTROL),
33 &req, sizeof(req), true);
34 }
35 EXPORT_SYMBOL_GPL(mt76_connac_mcu_patch_sem_ctrl);
36
37 int mt76_connac_mcu_start_patch(struct mt76_dev *dev)
38 {
39 struct {
40 u8 check_crc;
41 u8 reserved[3];
42 } req = {
43 .check_crc = 0,
44 };
45
46 return mt76_mcu_send_msg(dev, MCU_CMD(PATCH_FINISH_REQ),
47 &req, sizeof(req), true);
48 }
49 EXPORT_SYMBOL_GPL(mt76_connac_mcu_start_patch);
50
51 #define MCU_PATCH_ADDRESS 0x200000
52
53 int mt76_connac_mcu_init_download(struct mt76_dev *dev, u32 addr, u32 len,
54 u32 mode)
55 {
56 struct {
57 __le32 addr;
58 __le32 len;
59 __le32 mode;
60 } req = {
61 .addr = cpu_to_le32(addr),
62 .len = cpu_to_le32(len),
63 .mode = cpu_to_le32(mode),
64 };
65 int cmd;
66
67 if ((!is_connac_v1(dev) && addr == MCU_PATCH_ADDRESS) ||
68 (is_mt7921(dev) && addr == 0x900000) ||
69 (is_mt7925(dev) && (addr == 0x900000 || addr == 0xe0002800)) ||
70 (is_mt799x(dev) && addr == 0x900000))
71 cmd = MCU_CMD(PATCH_START_REQ);
72 else
73 cmd = MCU_CMD(TARGET_ADDRESS_LEN_REQ);
74
75 return mt76_mcu_send_msg(dev, cmd, &req, sizeof(req), true);
76 }
77 EXPORT_SYMBOL_GPL(mt76_connac_mcu_init_download);
78
79 int mt76_connac_mcu_set_channel_domain(struct mt76_phy *phy)
80 {
81 int len, i, n_max_channels, n_2ch = 0, n_5ch = 0, n_6ch = 0;
82 struct mt76_connac_mcu_channel_domain {
83 u8 alpha2[4]; /* regulatory_request.alpha2 */
84 u8 bw_2g; /* BW_20_40M 0
85 * BW_20M 1
86 * BW_20_40_80M 2
87 * BW_20_40_80_160M 3
88 * BW_20_40_80_8080M 4
89 */
90 u8 bw_5g;
91 u8 bw_6g;
92 u8 pad;
93 u8 n_2ch;
94 u8 n_5ch;
95 u8 n_6ch;
96 u8 pad2;
97 } __packed hdr = {
98 .bw_2g = 0,
99 .bw_5g = 3, /* BW_20_40_80_160M */
100 .bw_6g = 3,
101 };
102 struct mt76_connac_mcu_chan {
103 __le16 hw_value;
104 __le16 pad;
105 __le32 flags;
106 } __packed channel;
107 struct mt76_dev *dev = phy->dev;
108 struct ieee80211_channel *chan;
109 struct sk_buff *skb;
110
111 n_max_channels = phy->sband_2g.sband.n_channels +
112 phy->sband_5g.sband.n_channels +
113 phy->sband_6g.sband.n_channels;
114 len = sizeof(hdr) + n_max_channels * sizeof(channel);
115
116 skb = mt76_mcu_msg_alloc(dev, NULL, len);
117 if (!skb)
118 return -ENOMEM;
119
120 skb_reserve(skb, sizeof(hdr));
121
122 for (i = 0; i < phy->sband_2g.sband.n_channels; i++) {
123 chan = &phy->sband_2g.sband.channels[i];
124 if (chan->flags & IEEE80211_CHAN_DISABLED)
125 continue;
126
127 channel.hw_value = cpu_to_le16(chan->hw_value);
128 channel.flags = cpu_to_le32(chan->flags);
129 channel.pad = 0;
130
131 skb_put_data(skb, &channel, sizeof(channel));
132 n_2ch++;
133 }
134 for (i = 0; i < phy->sband_5g.sband.n_channels; i++) {
135 chan = &phy->sband_5g.sband.channels[i];
136 if (chan->flags & IEEE80211_CHAN_DISABLED)
137 continue;
138
139 channel.hw_value = cpu_to_le16(chan->hw_value);
140 channel.flags = cpu_to_le32(chan->flags);
141 channel.pad = 0;
142
143 skb_put_data(skb, &channel, sizeof(channel));
144 n_5ch++;
145 }
146 for (i = 0; i < phy->sband_6g.sband.n_channels; i++) {
147 chan = &phy->sband_6g.sband.channels[i];
148 if (chan->flags & IEEE80211_CHAN_DISABLED)
149 continue;
150
151 channel.hw_value = cpu_to_le16(chan->hw_value);
152 channel.flags = cpu_to_le32(chan->flags);
153 channel.pad = 0;
154
155 skb_put_data(skb, &channel, sizeof(channel));
156 n_6ch++;
157 }
158
159 BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(hdr.alpha2));
160 memcpy(hdr.alpha2, dev->alpha2, sizeof(dev->alpha2));
161 hdr.n_2ch = n_2ch;
162 hdr.n_5ch = n_5ch;
163 hdr.n_6ch = n_6ch;
164
165 memcpy(__skb_push(skb, sizeof(hdr)), &hdr, sizeof(hdr));
166
167 return mt76_mcu_skb_send_msg(dev, skb, MCU_CE_CMD(SET_CHAN_DOMAIN),
168 false);
169 }
170 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_channel_domain);
171
172 int mt76_connac_mcu_set_mac_enable(struct mt76_dev *dev, int band, bool enable,
173 bool hdr_trans)
174 {
175 struct {
176 u8 enable;
177 u8 band;
178 u8 rsv[2];
179 } __packed req_mac = {
180 .enable = enable,
181 .band = band,
182 };
183
184 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(MAC_INIT_CTRL), &req_mac,
185 sizeof(req_mac), true);
186 }
187 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_mac_enable);
188
189 int mt76_connac_mcu_set_vif_ps(struct mt76_dev *dev, struct ieee80211_vif *vif)
190 {
191 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
192 struct {
193 u8 bss_idx;
194 u8 ps_state; /* 0: device awake
195 * 1: static power save
196 * 2: dynamic power saving
197 */
198 } req = {
199 .bss_idx = mvif->idx,
200 .ps_state = vif->cfg.ps ? 2 : 0,
201 };
202
203 if (vif->type != NL80211_IFTYPE_STATION)
204 return -EOPNOTSUPP;
205
206 return mt76_mcu_send_msg(dev, MCU_CE_CMD(SET_PS_PROFILE),
207 &req, sizeof(req), false);
208 }
209 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_vif_ps);
210
211 int mt76_connac_mcu_set_rts_thresh(struct mt76_dev *dev, u32 val, u8 band)
212 {
213 struct {
214 u8 prot_idx;
215 u8 band;
216 u8 rsv[2];
217 __le32 len_thresh;
218 __le32 pkt_thresh;
219 } __packed req = {
220 .prot_idx = 1,
221 .band = band,
222 .len_thresh = cpu_to_le32(val),
223 .pkt_thresh = cpu_to_le32(0x2),
224 };
225
226 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(PROTECT_CTRL), &req,
227 sizeof(req), true);
228 }
229 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rts_thresh);
230
231 void mt76_connac_mcu_beacon_loss_iter(void *priv, u8 *mac,
232 struct ieee80211_vif *vif)
233 {
234 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
235 struct mt76_connac_beacon_loss_event *event = priv;
236
237 if (mvif->idx != event->bss_idx)
238 return;
239
240 if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
241 return;
242
243 ieee80211_beacon_loss(vif);
244 }
245 EXPORT_SYMBOL_GPL(mt76_connac_mcu_beacon_loss_iter);
246
247 struct tlv *
248 mt76_connac_mcu_add_nested_tlv(struct sk_buff *skb, int tag, int len,
249 void *sta_ntlv, void *sta_wtbl)
250 {
251 struct sta_ntlv_hdr *ntlv_hdr = sta_ntlv;
252 struct tlv *sta_hdr = sta_wtbl;
253 struct tlv *ptlv, tlv = {
254 .tag = cpu_to_le16(tag),
255 .len = cpu_to_le16(len),
256 };
257 u16 ntlv;
258
259 ptlv = skb_put_zero(skb, len);
260 memcpy(ptlv, &tlv, sizeof(tlv));
261
262 ntlv = le16_to_cpu(ntlv_hdr->tlv_num);
263 ntlv_hdr->tlv_num = cpu_to_le16(ntlv + 1);
264
265 if (sta_hdr) {
266 len += le16_to_cpu(sta_hdr->len);
267 sta_hdr->len = cpu_to_le16(len);
268 }
269
270 return ptlv;
271 }
272 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_nested_tlv);
273
274 struct sk_buff *
275 __mt76_connac_mcu_alloc_sta_req(struct mt76_dev *dev, struct mt76_vif_link *mvif,
276 struct mt76_wcid *wcid, int len)
277 {
278 struct sta_req_hdr hdr = {
279 .bss_idx = mvif->idx,
280 .muar_idx = wcid ? mvif->omac_idx : 0,
281 .is_tlv_append = 1,
282 };
283 struct sk_buff *skb;
284
285 if (wcid && !wcid->sta && !wcid->sta_disabled)
286 hdr.muar_idx = 0xe;
287
288 mt76_connac_mcu_get_wlan_idx(dev, wcid, &hdr.wlan_idx_lo,
289 &hdr.wlan_idx_hi);
290 skb = __mt76_mcu_msg_alloc(dev, NULL, len, len, GFP_ATOMIC);
291 if (!skb)
292 return ERR_PTR(-ENOMEM);
293
294 skb_put_data(skb, &hdr, sizeof(hdr));
295
296 return skb;
297 }
298 EXPORT_SYMBOL_GPL(__mt76_connac_mcu_alloc_sta_req);
299
300 struct wtbl_req_hdr *
301 mt76_connac_mcu_alloc_wtbl_req(struct mt76_dev *dev, struct mt76_wcid *wcid,
302 int cmd, void *sta_wtbl, struct sk_buff **skb)
303 {
304 struct tlv *sta_hdr = sta_wtbl;
305 struct wtbl_req_hdr hdr = {
306 .operation = cmd,
307 };
308 struct sk_buff *nskb = *skb;
309
310 mt76_connac_mcu_get_wlan_idx(dev, wcid, &hdr.wlan_idx_lo,
311 &hdr.wlan_idx_hi);
312 if (!nskb) {
313 nskb = mt76_mcu_msg_alloc(dev, NULL,
314 MT76_CONNAC_WTBL_UPDATE_MAX_SIZE);
315 if (!nskb)
316 return ERR_PTR(-ENOMEM);
317
318 *skb = nskb;
319 }
320
321 if (sta_hdr)
322 le16_add_cpu(&sta_hdr->len, sizeof(hdr));
323
324 return skb_put_data(nskb, &hdr, sizeof(hdr));
325 }
326 EXPORT_SYMBOL_GPL(mt76_connac_mcu_alloc_wtbl_req);
327
328 void mt76_connac_mcu_bss_omac_tlv(struct sk_buff *skb,
329 struct ieee80211_vif *vif)
330 {
331 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
332 u8 omac_idx = mvif->omac_idx;
333 struct bss_info_omac *omac;
334 struct tlv *tlv;
335 u32 type = 0;
336
337 switch (vif->type) {
338 case NL80211_IFTYPE_MONITOR:
339 case NL80211_IFTYPE_MESH_POINT:
340 case NL80211_IFTYPE_AP:
341 if (vif->p2p)
342 type = CONNECTION_P2P_GO;
343 else
344 type = CONNECTION_INFRA_AP;
345 break;
346 case NL80211_IFTYPE_STATION:
347 if (vif->p2p)
348 type = CONNECTION_P2P_GC;
349 else
350 type = CONNECTION_INFRA_STA;
351 break;
352 case NL80211_IFTYPE_ADHOC:
353 type = CONNECTION_IBSS_ADHOC;
354 break;
355 default:
356 WARN_ON(1);
357 break;
358 }
359
360 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_OMAC, sizeof(*omac));
361
362 omac = (struct bss_info_omac *)tlv;
363 omac->conn_type = cpu_to_le32(type);
364 omac->omac_idx = mvif->omac_idx;
365 omac->band_idx = mvif->band_idx;
366 omac->hw_bss_idx = omac_idx > EXT_BSSID_START ? HW_BSSID_0 : omac_idx;
367 }
368 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_omac_tlv);
369
370 void mt76_connac_mcu_sta_basic_tlv(struct mt76_dev *dev, struct sk_buff *skb,
371 struct ieee80211_bss_conf *link_conf,
372 struct ieee80211_link_sta *link_sta,
373 int conn_state, bool newly)
374 {
375 struct ieee80211_vif *vif = link_conf->vif;
376 struct sta_rec_basic *basic;
377 struct tlv *tlv;
378 int conn_type;
379
380 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BASIC, sizeof(*basic));
381
382 basic = (struct sta_rec_basic *)tlv;
383 basic->extra_info = cpu_to_le16(EXTRA_INFO_VER);
384
385 if (newly && conn_state != CONN_STATE_DISCONNECT)
386 basic->extra_info |= cpu_to_le16(EXTRA_INFO_NEW);
387 basic->conn_state = conn_state;
388
389 if (!link_sta) {
390 basic->conn_type = cpu_to_le32(CONNECTION_INFRA_BC);
391
392 if (vif->type == NL80211_IFTYPE_STATION &&
393 !is_zero_ether_addr(link_conf->bssid)) {
394 memcpy(basic->peer_addr, link_conf->bssid, ETH_ALEN);
395 basic->aid = cpu_to_le16(vif->cfg.aid);
396 } else {
397 eth_broadcast_addr(basic->peer_addr);
398 }
399 return;
400 }
401
402 switch (vif->type) {
403 case NL80211_IFTYPE_MESH_POINT:
404 case NL80211_IFTYPE_AP:
405 if (vif->p2p && !is_mt7921(dev))
406 conn_type = CONNECTION_P2P_GC;
407 else
408 conn_type = CONNECTION_INFRA_STA;
409 basic->conn_type = cpu_to_le32(conn_type);
410 basic->aid = cpu_to_le16(link_sta->sta->aid);
411 break;
412 case NL80211_IFTYPE_STATION:
413 if (vif->p2p && !is_mt7921(dev))
414 conn_type = CONNECTION_P2P_GO;
415 else
416 conn_type = CONNECTION_INFRA_AP;
417 basic->conn_type = cpu_to_le32(conn_type);
418 basic->aid = cpu_to_le16(vif->cfg.aid);
419 break;
420 case NL80211_IFTYPE_ADHOC:
421 basic->conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
422 basic->aid = cpu_to_le16(link_sta->sta->aid);
423 break;
424 default:
425 WARN_ON(1);
426 break;
427 }
428
429 memcpy(basic->peer_addr, link_sta->addr, ETH_ALEN);
430 basic->qos = link_sta->sta->wme;
431 }
432 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_basic_tlv);
433
434 void mt76_connac_mcu_sta_uapsd(struct sk_buff *skb, struct ieee80211_vif *vif,
435 struct ieee80211_sta *sta)
436 {
437 struct sta_rec_uapsd *uapsd;
438 struct tlv *tlv;
439
440 if (vif->type != NL80211_IFTYPE_AP || !sta->wme)
441 return;
442
443 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_APPS, sizeof(*uapsd));
444 uapsd = (struct sta_rec_uapsd *)tlv;
445
446 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) {
447 uapsd->dac_map |= BIT(3);
448 uapsd->tac_map |= BIT(3);
449 }
450 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI) {
451 uapsd->dac_map |= BIT(2);
452 uapsd->tac_map |= BIT(2);
453 }
454 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE) {
455 uapsd->dac_map |= BIT(1);
456 uapsd->tac_map |= BIT(1);
457 }
458 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK) {
459 uapsd->dac_map |= BIT(0);
460 uapsd->tac_map |= BIT(0);
461 }
462 uapsd->max_sp = sta->max_sp;
463 }
464 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_uapsd);
465
466 void mt76_connac_mcu_wtbl_hdr_trans_tlv(struct sk_buff *skb,
467 struct ieee80211_vif *vif,
468 struct mt76_wcid *wcid,
469 void *sta_wtbl, void *wtbl_tlv)
470 {
471 struct wtbl_hdr_trans *htr;
472 struct tlv *tlv;
473
474 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HDR_TRANS,
475 sizeof(*htr),
476 wtbl_tlv, sta_wtbl);
477 htr = (struct wtbl_hdr_trans *)tlv;
478 htr->no_rx_trans = true;
479
480 if (vif->type == NL80211_IFTYPE_STATION)
481 htr->to_ds = true;
482 else
483 htr->from_ds = true;
484
485 if (!wcid)
486 return;
487
488 htr->no_rx_trans = !test_bit(MT_WCID_FLAG_HDR_TRANS, &wcid->flags);
489 if (test_bit(MT_WCID_FLAG_4ADDR, &wcid->flags)) {
490 htr->to_ds = true;
491 htr->from_ds = true;
492 }
493 }
494 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_hdr_trans_tlv);
495
496 int mt76_connac_mcu_sta_update_hdr_trans(struct mt76_dev *dev,
497 struct ieee80211_vif *vif,
498 struct mt76_wcid *wcid, int cmd)
499 {
500 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
501 struct wtbl_req_hdr *wtbl_hdr;
502 struct tlv *sta_wtbl;
503 struct sk_buff *skb;
504
505 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
506 if (IS_ERR(skb))
507 return PTR_ERR(skb);
508
509 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
510 sizeof(struct tlv));
511
512 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
513 sta_wtbl, &skb);
514 if (IS_ERR(wtbl_hdr))
515 return PTR_ERR(wtbl_hdr);
516
517 mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, vif, wcid, sta_wtbl, wtbl_hdr);
518
519 return mt76_mcu_skb_send_msg(dev, skb, cmd, true);
520 }
521 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_update_hdr_trans);
522
523 int mt76_connac_mcu_wtbl_update_hdr_trans(struct mt76_dev *dev,
524 struct ieee80211_vif *vif,
525 struct ieee80211_sta *sta)
526 {
527 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
528 struct wtbl_req_hdr *wtbl_hdr;
529 struct sk_buff *skb = NULL;
530
531 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET, NULL,
532 &skb);
533 if (IS_ERR(wtbl_hdr))
534 return PTR_ERR(wtbl_hdr);
535
536 mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, vif, wcid, NULL, wtbl_hdr);
537
538 return mt76_mcu_skb_send_msg(dev, skb, MCU_EXT_CMD(WTBL_UPDATE), true);
539 }
540 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_update_hdr_trans);
541
542 void mt76_connac_mcu_wtbl_generic_tlv(struct mt76_dev *dev,
543 struct sk_buff *skb,
544 struct ieee80211_vif *vif,
545 struct ieee80211_sta *sta,
546 void *sta_wtbl, void *wtbl_tlv)
547 {
548 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
549 struct wtbl_generic *generic;
550 struct wtbl_rx *rx;
551 struct wtbl_spe *spe;
552 struct tlv *tlv;
553
554 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_GENERIC,
555 sizeof(*generic),
556 wtbl_tlv, sta_wtbl);
557
558 generic = (struct wtbl_generic *)tlv;
559
560 if (sta) {
561 if (vif->type == NL80211_IFTYPE_STATION)
562 generic->partial_aid = cpu_to_le16(vif->cfg.aid);
563 else
564 generic->partial_aid = cpu_to_le16(sta->aid);
565 memcpy(generic->peer_addr, sta->addr, ETH_ALEN);
566 generic->muar_idx = mvif->omac_idx;
567 generic->qos = sta->wme;
568 } else {
569 if (!is_connac_v1(dev) && vif->type == NL80211_IFTYPE_STATION)
570 memcpy(generic->peer_addr, vif->bss_conf.bssid,
571 ETH_ALEN);
572 else
573 eth_broadcast_addr(generic->peer_addr);
574
575 generic->muar_idx = 0xe;
576 }
577
578 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RX, sizeof(*rx),
579 wtbl_tlv, sta_wtbl);
580
581 rx = (struct wtbl_rx *)tlv;
582 rx->rca1 = sta ? vif->type != NL80211_IFTYPE_AP : 1;
583 rx->rca2 = 1;
584 rx->rv = 1;
585
586 if (!is_connac_v1(dev))
587 return;
588
589 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SPE, sizeof(*spe),
590 wtbl_tlv, sta_wtbl);
591 spe = (struct wtbl_spe *)tlv;
592 spe->spe_idx = 24;
593 }
594 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_generic_tlv);
595
596 static void
597 mt76_connac_mcu_sta_amsdu_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
598 struct ieee80211_vif *vif)
599 {
600 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
601 struct sta_rec_amsdu *amsdu;
602 struct tlv *tlv;
603
604 if (vif->type != NL80211_IFTYPE_AP &&
605 vif->type != NL80211_IFTYPE_STATION)
606 return;
607
608 if (!sta->deflink.agg.max_amsdu_len)
609 return;
610
611 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HW_AMSDU, sizeof(*amsdu));
612 amsdu = (struct sta_rec_amsdu *)tlv;
613 amsdu->max_amsdu_num = 8;
614 amsdu->amsdu_en = true;
615 amsdu->max_mpdu_size = sta->deflink.agg.max_amsdu_len >=
616 IEEE80211_MAX_MPDU_LEN_VHT_7991;
617
618 wcid->amsdu = true;
619 }
620
621 #define HE_PHY(p, c) u8_get_bits(c, IEEE80211_HE_PHY_##p)
622 #define HE_MAC(m, c) u8_get_bits(c, IEEE80211_HE_MAC_##m)
623 static void
624 mt76_connac_mcu_sta_he_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
625 {
626 struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
627 struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
628 struct sta_rec_he *he;
629 struct tlv *tlv;
630 u32 cap = 0;
631
632 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE, sizeof(*he));
633
634 he = (struct sta_rec_he *)tlv;
635
636 if (elem->mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
637 cap |= STA_REC_HE_CAP_HTC;
638
639 if (elem->mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
640 cap |= STA_REC_HE_CAP_BSR;
641
642 if (elem->mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
643 cap |= STA_REC_HE_CAP_OM;
644
645 if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU)
646 cap |= STA_REC_HE_CAP_AMSDU_IN_AMPDU;
647
648 if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
649 cap |= STA_REC_HE_CAP_BQR;
650
651 if (elem->phy_cap_info[0] &
652 (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G |
653 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G))
654 cap |= STA_REC_HE_CAP_BW20_RU242_SUPPORT;
655
656 if (elem->phy_cap_info[1] &
657 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
658 cap |= STA_REC_HE_CAP_LDPC;
659
660 if (elem->phy_cap_info[1] &
661 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US)
662 cap |= STA_REC_HE_CAP_SU_PPDU_1LTF_8US_GI;
663
664 if (elem->phy_cap_info[2] &
665 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US)
666 cap |= STA_REC_HE_CAP_NDP_4LTF_3DOT2MS_GI;
667
668 if (elem->phy_cap_info[2] &
669 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ)
670 cap |= STA_REC_HE_CAP_LE_EQ_80M_TX_STBC;
671
672 if (elem->phy_cap_info[2] &
673 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
674 cap |= STA_REC_HE_CAP_LE_EQ_80M_RX_STBC;
675
676 if (elem->phy_cap_info[6] &
677 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE)
678 cap |= STA_REC_HE_CAP_PARTIAL_BW_EXT_RANGE;
679
680 if (elem->phy_cap_info[7] &
681 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI)
682 cap |= STA_REC_HE_CAP_SU_MU_PPDU_4LTF_8US_GI;
683
684 if (elem->phy_cap_info[7] &
685 IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ)
686 cap |= STA_REC_HE_CAP_GT_80M_TX_STBC;
687
688 if (elem->phy_cap_info[7] &
689 IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ)
690 cap |= STA_REC_HE_CAP_GT_80M_RX_STBC;
691
692 if (elem->phy_cap_info[8] &
693 IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI)
694 cap |= STA_REC_HE_CAP_ER_SU_PPDU_4LTF_8US_GI;
695
696 if (elem->phy_cap_info[8] &
697 IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI)
698 cap |= STA_REC_HE_CAP_ER_SU_PPDU_1LTF_8US_GI;
699
700 if (elem->phy_cap_info[9] &
701 IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK)
702 cap |= STA_REC_HE_CAP_TRIG_CQI_FK;
703
704 if (elem->phy_cap_info[9] &
705 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU)
706 cap |= STA_REC_HE_CAP_TX_1024QAM_UNDER_RU242;
707
708 if (elem->phy_cap_info[9] &
709 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU)
710 cap |= STA_REC_HE_CAP_RX_1024QAM_UNDER_RU242;
711
712 he->he_cap = cpu_to_le32(cap);
713
714 switch (sta->deflink.bandwidth) {
715 case IEEE80211_STA_RX_BW_160:
716 if (elem->phy_cap_info[0] &
717 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
718 he->max_nss_mcs[CMD_HE_MCS_BW8080] =
719 he_cap->he_mcs_nss_supp.rx_mcs_80p80;
720
721 he->max_nss_mcs[CMD_HE_MCS_BW160] =
722 he_cap->he_mcs_nss_supp.rx_mcs_160;
723 fallthrough;
724 default:
725 he->max_nss_mcs[CMD_HE_MCS_BW80] =
726 he_cap->he_mcs_nss_supp.rx_mcs_80;
727 break;
728 }
729
730 he->t_frame_dur =
731 HE_MAC(CAP1_TF_MAC_PAD_DUR_MASK, elem->mac_cap_info[1]);
732 he->max_ampdu_exp =
733 HE_MAC(CAP3_MAX_AMPDU_LEN_EXP_MASK, elem->mac_cap_info[3]);
734
735 he->bw_set =
736 HE_PHY(CAP0_CHANNEL_WIDTH_SET_MASK, elem->phy_cap_info[0]);
737 he->device_class =
738 HE_PHY(CAP1_DEVICE_CLASS_A, elem->phy_cap_info[1]);
739 he->punc_pream_rx =
740 HE_PHY(CAP1_PREAMBLE_PUNC_RX_MASK, elem->phy_cap_info[1]);
741
742 he->dcm_tx_mode =
743 HE_PHY(CAP3_DCM_MAX_CONST_TX_MASK, elem->phy_cap_info[3]);
744 he->dcm_tx_max_nss =
745 HE_PHY(CAP3_DCM_MAX_TX_NSS_2, elem->phy_cap_info[3]);
746 he->dcm_rx_mode =
747 HE_PHY(CAP3_DCM_MAX_CONST_RX_MASK, elem->phy_cap_info[3]);
748 he->dcm_rx_max_nss =
749 HE_PHY(CAP3_DCM_MAX_RX_NSS_2, elem->phy_cap_info[3]);
750 he->dcm_rx_max_nss =
751 HE_PHY(CAP8_DCM_MAX_RU_MASK, elem->phy_cap_info[8]);
752
753 he->pkt_ext = 2;
754 }
755
756 void
757 mt76_connac_mcu_sta_he_tlv_v2(struct sk_buff *skb, struct ieee80211_sta *sta)
758 {
759 struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
760 struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
761 struct sta_rec_he_v2 *he;
762 struct tlv *tlv;
763
764 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_V2, sizeof(*he));
765
766 he = (struct sta_rec_he_v2 *)tlv;
767 memcpy(he->he_phy_cap, elem->phy_cap_info, sizeof(he->he_phy_cap));
768 memcpy(he->he_mac_cap, elem->mac_cap_info, sizeof(he->he_mac_cap));
769
770 switch (sta->deflink.bandwidth) {
771 case IEEE80211_STA_RX_BW_160:
772 if (elem->phy_cap_info[0] &
773 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
774 he->max_nss_mcs[CMD_HE_MCS_BW8080] =
775 he_cap->he_mcs_nss_supp.rx_mcs_80p80;
776
777 he->max_nss_mcs[CMD_HE_MCS_BW160] =
778 he_cap->he_mcs_nss_supp.rx_mcs_160;
779 fallthrough;
780 default:
781 he->max_nss_mcs[CMD_HE_MCS_BW80] =
782 he_cap->he_mcs_nss_supp.rx_mcs_80;
783 break;
784 }
785
786 he->pkt_ext = IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US;
787 }
788 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_he_tlv_v2);
789
790 u8
791 mt76_connac_get_phy_mode_v2(struct mt76_phy *mphy, struct ieee80211_vif *vif,
792 enum nl80211_band band,
793 struct ieee80211_link_sta *link_sta)
794 {
795 struct ieee80211_sta_ht_cap *ht_cap;
796 struct ieee80211_sta_vht_cap *vht_cap;
797 const struct ieee80211_sta_he_cap *he_cap;
798 const struct ieee80211_sta_eht_cap *eht_cap;
799 u8 mode = 0;
800
801 if (link_sta) {
802 ht_cap = &link_sta->ht_cap;
803 vht_cap = &link_sta->vht_cap;
804 he_cap = &link_sta->he_cap;
805 eht_cap = &link_sta->eht_cap;
806 } else {
807 struct ieee80211_supported_band *sband;
808
809 sband = mphy->hw->wiphy->bands[band];
810 ht_cap = &sband->ht_cap;
811 vht_cap = &sband->vht_cap;
812 he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
813 eht_cap = ieee80211_get_eht_iftype_cap(sband, vif->type);
814 }
815
816 if (band == NL80211_BAND_2GHZ) {
817 mode |= PHY_TYPE_BIT_HR_DSSS | PHY_TYPE_BIT_ERP;
818
819 if (ht_cap->ht_supported)
820 mode |= PHY_TYPE_BIT_HT;
821
822 if (he_cap && he_cap->has_he)
823 mode |= PHY_TYPE_BIT_HE;
824
825 if (eht_cap && eht_cap->has_eht)
826 mode |= PHY_TYPE_BIT_BE;
827 } else if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ) {
828 mode |= PHY_TYPE_BIT_OFDM;
829
830 if (ht_cap->ht_supported)
831 mode |= PHY_TYPE_BIT_HT;
832
833 if (vht_cap->vht_supported)
834 mode |= PHY_TYPE_BIT_VHT;
835
836 if (he_cap && he_cap->has_he)
837 mode |= PHY_TYPE_BIT_HE;
838
839 if (eht_cap && eht_cap->has_eht)
840 mode |= PHY_TYPE_BIT_BE;
841 }
842
843 return mode;
844 }
845 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode_v2);
846
847 void mt76_connac_mcu_sta_tlv(struct mt76_phy *mphy, struct sk_buff *skb,
848 struct ieee80211_sta *sta,
849 struct ieee80211_vif *vif,
850 u8 rcpi, u8 sta_state)
851 {
852 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
853 struct cfg80211_chan_def *chandef = mvif->ctx ?
854 &mvif->ctx->def : &mphy->chandef;
855 enum nl80211_band band = chandef->chan->band;
856 struct mt76_dev *dev = mphy->dev;
857 struct sta_rec_ra_info *ra_info;
858 struct sta_rec_state *state;
859 struct sta_rec_phy *phy;
860 struct tlv *tlv;
861 u16 supp_rates;
862
863 /* starec ht */
864 if (sta->deflink.ht_cap.ht_supported) {
865 struct sta_rec_ht *ht;
866
867 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
868 ht = (struct sta_rec_ht *)tlv;
869 ht->ht_cap = cpu_to_le16(sta->deflink.ht_cap.cap);
870 }
871
872 /* starec vht */
873 if (sta->deflink.vht_cap.vht_supported) {
874 struct sta_rec_vht *vht;
875 int len;
876
877 len = is_mt7921(dev) ? sizeof(*vht) : sizeof(*vht) - 4;
878 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_VHT, len);
879 vht = (struct sta_rec_vht *)tlv;
880 vht->vht_cap = cpu_to_le32(sta->deflink.vht_cap.cap);
881 vht->vht_rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
882 vht->vht_tx_mcs_map = sta->deflink.vht_cap.vht_mcs.tx_mcs_map;
883 }
884
885 /* starec uapsd */
886 mt76_connac_mcu_sta_uapsd(skb, vif, sta);
887
888 if (!is_mt7921(dev))
889 return;
890
891 if (sta->deflink.ht_cap.ht_supported || sta->deflink.he_cap.has_he)
892 mt76_connac_mcu_sta_amsdu_tlv(skb, sta, vif);
893
894 /* starec he */
895 if (sta->deflink.he_cap.has_he) {
896 mt76_connac_mcu_sta_he_tlv(skb, sta);
897 mt76_connac_mcu_sta_he_tlv_v2(skb, sta);
898 if (band == NL80211_BAND_6GHZ &&
899 sta_state == MT76_STA_INFO_STATE_ASSOC) {
900 struct sta_rec_he_6g_capa *he_6g_capa;
901
902 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_6G,
903 sizeof(*he_6g_capa));
904 he_6g_capa = (struct sta_rec_he_6g_capa *)tlv;
905 he_6g_capa->capa = sta->deflink.he_6ghz_capa.capa;
906 }
907 }
908
909 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_PHY, sizeof(*phy));
910 phy = (struct sta_rec_phy *)tlv;
911 phy->phy_type = mt76_connac_get_phy_mode_v2(mphy, vif, band,
912 &sta->deflink);
913 phy->basic_rate = cpu_to_le16((u16)vif->bss_conf.basic_rates);
914 phy->rcpi = rcpi;
915 phy->ampdu = FIELD_PREP(IEEE80211_HT_AMPDU_PARM_FACTOR,
916 sta->deflink.ht_cap.ampdu_factor) |
917 FIELD_PREP(IEEE80211_HT_AMPDU_PARM_DENSITY,
918 sta->deflink.ht_cap.ampdu_density);
919
920 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra_info));
921 ra_info = (struct sta_rec_ra_info *)tlv;
922
923 supp_rates = sta->deflink.supp_rates[band];
924 if (band == NL80211_BAND_2GHZ)
925 supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates >> 4) |
926 FIELD_PREP(RA_LEGACY_CCK, supp_rates & 0xf);
927 else
928 supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates);
929
930 ra_info->legacy = cpu_to_le16(supp_rates);
931
932 if (sta->deflink.ht_cap.ht_supported)
933 memcpy(ra_info->rx_mcs_bitmask,
934 sta->deflink.ht_cap.mcs.rx_mask,
935 HT_MCS_MASK_NUM);
936
937 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_STATE, sizeof(*state));
938 state = (struct sta_rec_state *)tlv;
939 state->state = sta_state;
940
941 if (sta->deflink.vht_cap.vht_supported) {
942 state->vht_opmode = sta->deflink.bandwidth;
943 state->vht_opmode |= (sta->deflink.rx_nss - 1) <<
944 IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
945 }
946 }
947 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_tlv);
948
949 void mt76_connac_mcu_wtbl_smps_tlv(struct sk_buff *skb,
950 struct ieee80211_sta *sta,
951 void *sta_wtbl, void *wtbl_tlv)
952 {
953 struct wtbl_smps *smps;
954 struct tlv *tlv;
955
956 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SMPS, sizeof(*smps),
957 wtbl_tlv, sta_wtbl);
958 smps = (struct wtbl_smps *)tlv;
959 smps->smps = (sta->deflink.smps_mode == IEEE80211_SMPS_DYNAMIC);
960 }
961 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_smps_tlv);
962
963 void mt76_connac_mcu_wtbl_ht_tlv(struct mt76_dev *dev, struct sk_buff *skb,
964 struct ieee80211_sta *sta, void *sta_wtbl,
965 void *wtbl_tlv, bool ht_ldpc, bool vht_ldpc)
966 {
967 struct wtbl_ht *ht = NULL;
968 struct tlv *tlv;
969 u32 flags = 0;
970
971 if (sta->deflink.ht_cap.ht_supported || sta->deflink.he_6ghz_capa.capa) {
972 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HT, sizeof(*ht),
973 wtbl_tlv, sta_wtbl);
974 ht = (struct wtbl_ht *)tlv;
975 ht->ldpc = ht_ldpc &&
976 !!(sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING);
977
978 if (sta->deflink.ht_cap.ht_supported) {
979 ht->af = sta->deflink.ht_cap.ampdu_factor;
980 ht->mm = sta->deflink.ht_cap.ampdu_density;
981 } else {
982 ht->af = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
983 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
984 ht->mm = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
985 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
986 }
987
988 ht->ht = true;
989 }
990
991 if (sta->deflink.vht_cap.vht_supported || sta->deflink.he_6ghz_capa.capa) {
992 struct wtbl_vht *vht;
993 u8 af;
994
995 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_VHT,
996 sizeof(*vht), wtbl_tlv,
997 sta_wtbl);
998 vht = (struct wtbl_vht *)tlv;
999 vht->ldpc = vht_ldpc &&
1000 !!(sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC);
1001 vht->vht = true;
1002
1003 af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
1004 sta->deflink.vht_cap.cap);
1005 if (ht)
1006 ht->af = max(ht->af, af);
1007 }
1008
1009 mt76_connac_mcu_wtbl_smps_tlv(skb, sta, sta_wtbl, wtbl_tlv);
1010
1011 if (is_connac_v1(dev) && sta->deflink.ht_cap.ht_supported) {
1012 /* sgi */
1013 u32 msk = MT_WTBL_W5_SHORT_GI_20 | MT_WTBL_W5_SHORT_GI_40 |
1014 MT_WTBL_W5_SHORT_GI_80 | MT_WTBL_W5_SHORT_GI_160;
1015 struct wtbl_raw *raw;
1016
1017 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RAW_DATA,
1018 sizeof(*raw), wtbl_tlv,
1019 sta_wtbl);
1020
1021 if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
1022 flags |= MT_WTBL_W5_SHORT_GI_20;
1023 if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
1024 flags |= MT_WTBL_W5_SHORT_GI_40;
1025
1026 if (sta->deflink.vht_cap.vht_supported) {
1027 if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80)
1028 flags |= MT_WTBL_W5_SHORT_GI_80;
1029 if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160)
1030 flags |= MT_WTBL_W5_SHORT_GI_160;
1031 }
1032 raw = (struct wtbl_raw *)tlv;
1033 raw->val = cpu_to_le32(flags);
1034 raw->msk = cpu_to_le32(~msk);
1035 raw->wtbl_idx = 1;
1036 raw->dw = 5;
1037 }
1038 }
1039 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ht_tlv);
1040
1041 int mt76_connac_mcu_sta_cmd(struct mt76_phy *phy,
1042 struct mt76_sta_cmd_info *info)
1043 {
1044 struct mt76_vif_link *mvif = (struct mt76_vif_link *)info->vif->drv_priv;
1045 struct ieee80211_link_sta *link_sta;
1046 struct mt76_dev *dev = phy->dev;
1047 struct wtbl_req_hdr *wtbl_hdr;
1048 struct tlv *sta_wtbl;
1049 struct sk_buff *skb;
1050 int conn_state;
1051
1052 if (!info->link_conf)
1053 info->link_conf = &info->vif->bss_conf;
1054
1055 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, info->wcid);
1056 if (IS_ERR(skb))
1057 return PTR_ERR(skb);
1058
1059 conn_state = info->enable ? CONN_STATE_PORT_SECURE :
1060 CONN_STATE_DISCONNECT;
1061 link_sta = info->sta ? &info->sta->deflink : NULL;
1062 if (info->sta || !info->offload_fw)
1063 mt76_connac_mcu_sta_basic_tlv(dev, skb, info->link_conf,
1064 link_sta, conn_state,
1065 info->newly);
1066 if (info->sta && info->enable)
1067 mt76_connac_mcu_sta_tlv(phy, skb, info->sta,
1068 info->vif, info->rcpi,
1069 info->state);
1070
1071 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1072 sizeof(struct tlv));
1073
1074 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, info->wcid,
1075 WTBL_RESET_AND_SET,
1076 sta_wtbl, &skb);
1077 if (IS_ERR(wtbl_hdr))
1078 return PTR_ERR(wtbl_hdr);
1079
1080 if (info->enable) {
1081 mt76_connac_mcu_wtbl_generic_tlv(dev, skb, info->vif,
1082 info->sta, sta_wtbl,
1083 wtbl_hdr);
1084 mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, info->vif, info->wcid,
1085 sta_wtbl, wtbl_hdr);
1086 if (info->sta)
1087 mt76_connac_mcu_wtbl_ht_tlv(dev, skb, info->sta,
1088 sta_wtbl, wtbl_hdr,
1089 true, true);
1090 }
1091
1092 return mt76_mcu_skb_send_msg(dev, skb, info->cmd, true);
1093 }
1094 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_cmd);
1095
1096 void mt76_connac_mcu_wtbl_ba_tlv(struct mt76_dev *dev, struct sk_buff *skb,
1097 struct ieee80211_ampdu_params *params,
1098 bool enable, bool tx, void *sta_wtbl,
1099 void *wtbl_tlv)
1100 {
1101 struct wtbl_ba *ba;
1102 struct tlv *tlv;
1103
1104 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_BA, sizeof(*ba),
1105 wtbl_tlv, sta_wtbl);
1106
1107 ba = (struct wtbl_ba *)tlv;
1108 ba->tid = params->tid;
1109
1110 if (tx) {
1111 ba->ba_type = MT_BA_TYPE_ORIGINATOR;
1112 ba->sn = enable ? cpu_to_le16(params->ssn) : 0;
1113 ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
1114 ba->ba_en = enable;
1115 } else {
1116 memcpy(ba->peer_addr, params->sta->addr, ETH_ALEN);
1117 ba->ba_type = MT_BA_TYPE_RECIPIENT;
1118 ba->rst_ba_tid = params->tid;
1119 ba->rst_ba_sel = RST_BA_MAC_TID_MATCH;
1120 ba->rst_ba_sb = 1;
1121 }
1122
1123 if (!is_connac_v1(dev)) {
1124 ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
1125 return;
1126 }
1127
1128 if (enable && tx) {
1129 static const u8 ba_range[] = { 4, 8, 12, 24, 36, 48, 54, 64 };
1130 int i;
1131
1132 for (i = 7; i > 0; i--) {
1133 if (params->buf_size >= ba_range[i])
1134 break;
1135 }
1136 ba->ba_winsize_idx = i;
1137 }
1138 }
1139 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ba_tlv);
1140
1141 int mt76_connac_mcu_uni_add_dev(struct mt76_phy *phy,
1142 struct ieee80211_bss_conf *bss_conf,
1143 struct mt76_vif_link *mvif,
1144 struct mt76_wcid *wcid,
1145 bool enable)
1146 {
1147 struct mt76_dev *dev = phy->dev;
1148 struct {
1149 struct {
1150 u8 omac_idx;
1151 u8 band_idx;
1152 __le16 pad;
1153 } __packed hdr;
1154 struct req_tlv {
1155 __le16 tag;
1156 __le16 len;
1157 u8 active;
1158 u8 link_idx; /* not link_id */
1159 u8 omac_addr[ETH_ALEN];
1160 } __packed tlv;
1161 } dev_req = {
1162 .hdr = {
1163 .omac_idx = mvif->omac_idx,
1164 .band_idx = mvif->band_idx,
1165 },
1166 .tlv = {
1167 .tag = cpu_to_le16(DEV_INFO_ACTIVE),
1168 .len = cpu_to_le16(sizeof(struct req_tlv)),
1169 .active = enable,
1170 .link_idx = mvif->link_idx,
1171 },
1172 };
1173 struct {
1174 struct {
1175 u8 bss_idx;
1176 u8 pad[3];
1177 } __packed hdr;
1178 struct mt76_connac_bss_basic_tlv basic;
1179 } basic_req = {
1180 .hdr = {
1181 .bss_idx = mvif->idx,
1182 },
1183 .basic = {
1184 .tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1185 .len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1186 .omac_idx = mvif->omac_idx,
1187 .band_idx = mvif->band_idx,
1188 .wmm_idx = mvif->wmm_idx,
1189 .active = enable,
1190 .bmc_tx_wlan_idx = cpu_to_le16(wcid->idx),
1191 .sta_idx = cpu_to_le16(wcid->idx),
1192 .conn_state = 1,
1193 .link_idx = mvif->link_idx,
1194 },
1195 };
1196 int err, idx, cmd, len;
1197 void *data;
1198
1199 switch (bss_conf->vif->type) {
1200 case NL80211_IFTYPE_MESH_POINT:
1201 case NL80211_IFTYPE_MONITOR:
1202 case NL80211_IFTYPE_AP:
1203 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_AP);
1204 break;
1205 case NL80211_IFTYPE_STATION:
1206 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_STA);
1207 break;
1208 case NL80211_IFTYPE_P2P_DEVICE:
1209 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_P2P_GO);
1210 break;
1211 case NL80211_IFTYPE_ADHOC:
1212 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1213 break;
1214 default:
1215 WARN_ON(1);
1216 break;
1217 }
1218
1219 idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1220 basic_req.basic.hw_bss_idx = idx;
1221
1222 memcpy(dev_req.tlv.omac_addr, bss_conf->addr, ETH_ALEN);
1223
1224 cmd = enable ? MCU_UNI_CMD(DEV_INFO_UPDATE) : MCU_UNI_CMD(BSS_INFO_UPDATE);
1225 data = enable ? (void *)&dev_req : (void *)&basic_req;
1226 len = enable ? sizeof(dev_req) : sizeof(basic_req);
1227
1228 err = mt76_mcu_send_msg(dev, cmd, data, len, true);
1229 if (err < 0)
1230 return err;
1231
1232 cmd = enable ? MCU_UNI_CMD(BSS_INFO_UPDATE) : MCU_UNI_CMD(DEV_INFO_UPDATE);
1233 data = enable ? (void *)&basic_req : (void *)&dev_req;
1234 len = enable ? sizeof(basic_req) : sizeof(dev_req);
1235
1236 return mt76_mcu_send_msg(dev, cmd, data, len, true);
1237 }
1238 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_dev);
1239
1240 void mt76_connac_mcu_sta_ba_tlv(struct sk_buff *skb,
1241 struct ieee80211_ampdu_params *params,
1242 bool enable, bool tx)
1243 {
1244 struct sta_rec_ba *ba;
1245 struct tlv *tlv;
1246
1247 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
1248
1249 ba = (struct sta_rec_ba *)tlv;
1250 ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT;
1251 ba->winsize = cpu_to_le16(params->buf_size);
1252 ba->ssn = cpu_to_le16(params->ssn);
1253 ba->ba_en = enable << params->tid;
1254 ba->amsdu = params->amsdu;
1255 ba->tid = params->tid;
1256 }
1257 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba_tlv);
1258
1259 int mt76_connac_mcu_sta_wed_update(struct mt76_dev *dev, struct sk_buff *skb)
1260 {
1261 if (!mt76_is_mmio(dev))
1262 return 0;
1263
1264 if (!mtk_wed_device_active(&dev->mmio.wed))
1265 return 0;
1266
1267 return mtk_wed_device_update_msg(&dev->mmio.wed, WED_WO_STA_REC,
1268 skb->data, skb->len);
1269 }
1270 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_wed_update);
1271
1272 int mt76_connac_mcu_sta_ba(struct mt76_dev *dev, struct mt76_vif_link *mvif,
1273 struct ieee80211_ampdu_params *params,
1274 int cmd, bool enable, bool tx)
1275 {
1276 struct mt76_wcid *wcid = (struct mt76_wcid *)params->sta->drv_priv;
1277 struct wtbl_req_hdr *wtbl_hdr;
1278 struct tlv *sta_wtbl;
1279 struct sk_buff *skb;
1280 int ret;
1281
1282 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1283 if (IS_ERR(skb))
1284 return PTR_ERR(skb);
1285
1286 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1287 sizeof(struct tlv));
1288
1289 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
1290 sta_wtbl, &skb);
1291 if (IS_ERR(wtbl_hdr))
1292 return PTR_ERR(wtbl_hdr);
1293
1294 mt76_connac_mcu_wtbl_ba_tlv(dev, skb, params, enable, tx, sta_wtbl,
1295 wtbl_hdr);
1296
1297 ret = mt76_connac_mcu_sta_wed_update(dev, skb);
1298 if (ret) {
1299 dev_kfree_skb(skb);
1300 return ret;
1301 }
1302
1303 ret = mt76_mcu_skb_send_msg(dev, skb, cmd, true);
1304 if (ret)
1305 return ret;
1306
1307 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1308 if (IS_ERR(skb))
1309 return PTR_ERR(skb);
1310
1311 mt76_connac_mcu_sta_ba_tlv(skb, params, enable, tx);
1312
1313 ret = mt76_connac_mcu_sta_wed_update(dev, skb);
1314 if (ret) {
1315 dev_kfree_skb(skb);
1316 return ret;
1317 }
1318
1319 return mt76_mcu_skb_send_msg(dev, skb, cmd, true);
1320 }
1321 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba);
1322
1323 u8 mt76_connac_get_phy_mode(struct mt76_phy *phy, struct ieee80211_vif *vif,
1324 enum nl80211_band band,
1325 struct ieee80211_link_sta *link_sta)
1326 {
1327 struct mt76_dev *dev = phy->dev;
1328 const struct ieee80211_sta_he_cap *he_cap;
1329 struct ieee80211_sta_vht_cap *vht_cap;
1330 struct ieee80211_sta_ht_cap *ht_cap;
1331 u8 mode = 0;
1332
1333 if (is_connac_v1(dev))
1334 return 0x38;
1335
1336 if (link_sta) {
1337 ht_cap = &link_sta->ht_cap;
1338 vht_cap = &link_sta->vht_cap;
1339 he_cap = &link_sta->he_cap;
1340 } else {
1341 struct ieee80211_supported_band *sband;
1342
1343 sband = phy->hw->wiphy->bands[band];
1344 ht_cap = &sband->ht_cap;
1345 vht_cap = &sband->vht_cap;
1346 he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
1347 }
1348
1349 if (band == NL80211_BAND_2GHZ) {
1350 mode |= PHY_MODE_B | PHY_MODE_G;
1351
1352 if (ht_cap->ht_supported)
1353 mode |= PHY_MODE_GN;
1354
1355 if (he_cap && he_cap->has_he)
1356 mode |= PHY_MODE_AX_24G;
1357 } else if (band == NL80211_BAND_5GHZ) {
1358 mode |= PHY_MODE_A;
1359
1360 if (ht_cap->ht_supported)
1361 mode |= PHY_MODE_AN;
1362
1363 if (vht_cap->vht_supported)
1364 mode |= PHY_MODE_AC;
1365
1366 if (he_cap && he_cap->has_he)
1367 mode |= PHY_MODE_AX_5G;
1368 } else if (band == NL80211_BAND_6GHZ) {
1369 mode |= PHY_MODE_A | PHY_MODE_AN |
1370 PHY_MODE_AC | PHY_MODE_AX_5G;
1371 }
1372
1373 return mode;
1374 }
1375 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode);
1376
1377 u8 mt76_connac_get_phy_mode_ext(struct mt76_phy *phy, struct ieee80211_bss_conf *conf,
1378 enum nl80211_band band)
1379 {
1380 const struct ieee80211_sta_eht_cap *eht_cap;
1381 struct ieee80211_supported_band *sband;
1382 u8 mode = 0;
1383
1384 if (band == NL80211_BAND_6GHZ)
1385 mode |= PHY_MODE_AX_6G;
1386
1387 sband = phy->hw->wiphy->bands[band];
1388 eht_cap = ieee80211_get_eht_iftype_cap(sband, conf->vif->type);
1389
1390 if (!eht_cap || !eht_cap->has_eht || !conf->eht_support)
1391 return mode;
1392
1393 switch (band) {
1394 case NL80211_BAND_6GHZ:
1395 mode |= PHY_MODE_BE_6G;
1396 break;
1397 case NL80211_BAND_5GHZ:
1398 mode |= PHY_MODE_BE_5G;
1399 break;
1400 case NL80211_BAND_2GHZ:
1401 mode |= PHY_MODE_BE_24G;
1402 break;
1403 default:
1404 break;
1405 }
1406
1407 return mode;
1408 }
1409 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode_ext);
1410
1411 const struct ieee80211_sta_he_cap *
1412 mt76_connac_get_he_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1413 {
1414 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
1415 struct cfg80211_chan_def *chandef = mvif->ctx ?
1416 &mvif->ctx->def : &phy->chandef;
1417 enum nl80211_band band = chandef->chan->band;
1418 struct ieee80211_supported_band *sband;
1419
1420 sband = phy->hw->wiphy->bands[band];
1421
1422 return ieee80211_get_he_iftype_cap(sband, vif->type);
1423 }
1424 EXPORT_SYMBOL_GPL(mt76_connac_get_he_phy_cap);
1425
1426 const struct ieee80211_sta_eht_cap *
1427 mt76_connac_get_eht_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1428 {
1429 enum nl80211_band band = phy->chandef.chan->band;
1430 struct ieee80211_supported_band *sband;
1431
1432 sband = phy->hw->wiphy->bands[band];
1433
1434 return ieee80211_get_eht_iftype_cap(sband, vif->type);
1435 }
1436 EXPORT_SYMBOL_GPL(mt76_connac_get_eht_phy_cap);
1437
1438 #define DEFAULT_HE_PE_DURATION 4
1439 #define DEFAULT_HE_DURATION_RTS_THRES 1023
1440 static void
1441 mt76_connac_mcu_uni_bss_he_tlv(struct mt76_phy *phy, struct ieee80211_vif *vif,
1442 struct tlv *tlv)
1443 {
1444 const struct ieee80211_sta_he_cap *cap;
1445 struct bss_info_uni_he *he;
1446
1447 cap = mt76_connac_get_he_phy_cap(phy, vif);
1448
1449 he = (struct bss_info_uni_he *)tlv;
1450 he->he_pe_duration = vif->bss_conf.htc_trig_based_pkt_ext;
1451 if (!he->he_pe_duration)
1452 he->he_pe_duration = DEFAULT_HE_PE_DURATION;
1453
1454 he->he_rts_thres = cpu_to_le16(vif->bss_conf.frame_time_rts_th);
1455 if (!he->he_rts_thres)
1456 he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
1457
1458 he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
1459 he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
1460 he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
1461 }
1462
1463 int mt76_connac_mcu_uni_set_chctx(struct mt76_phy *phy, struct mt76_vif_link *mvif,
1464 struct ieee80211_chanctx_conf *ctx)
1465 {
1466 struct cfg80211_chan_def *chandef = ctx ? &ctx->def : &phy->chandef;
1467 int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
1468 enum nl80211_band band = chandef->chan->band;
1469 struct mt76_dev *mdev = phy->dev;
1470 struct {
1471 struct {
1472 u8 bss_idx;
1473 u8 pad[3];
1474 } __packed hdr;
1475 struct rlm_tlv {
1476 __le16 tag;
1477 __le16 len;
1478 u8 control_channel;
1479 u8 center_chan;
1480 u8 center_chan2;
1481 u8 bw;
1482 u8 tx_streams;
1483 u8 rx_streams;
1484 u8 short_st;
1485 u8 ht_op_info;
1486 u8 sco;
1487 u8 band;
1488 u8 pad[2];
1489 } __packed rlm;
1490 } __packed rlm_req = {
1491 .hdr = {
1492 .bss_idx = mvif->idx,
1493 },
1494 .rlm = {
1495 .tag = cpu_to_le16(UNI_BSS_INFO_RLM),
1496 .len = cpu_to_le16(sizeof(struct rlm_tlv)),
1497 .control_channel = chandef->chan->hw_value,
1498 .center_chan = ieee80211_frequency_to_channel(freq1),
1499 .center_chan2 = ieee80211_frequency_to_channel(freq2),
1500 .tx_streams = hweight8(phy->antenna_mask),
1501 .ht_op_info = 4, /* set HT 40M allowed */
1502 .rx_streams = phy->chainmask,
1503 .short_st = true,
1504 .band = band,
1505 },
1506 };
1507
1508 switch (chandef->width) {
1509 case NL80211_CHAN_WIDTH_40:
1510 rlm_req.rlm.bw = CMD_CBW_40MHZ;
1511 break;
1512 case NL80211_CHAN_WIDTH_80:
1513 rlm_req.rlm.bw = CMD_CBW_80MHZ;
1514 break;
1515 case NL80211_CHAN_WIDTH_80P80:
1516 rlm_req.rlm.bw = CMD_CBW_8080MHZ;
1517 break;
1518 case NL80211_CHAN_WIDTH_160:
1519 rlm_req.rlm.bw = CMD_CBW_160MHZ;
1520 break;
1521 case NL80211_CHAN_WIDTH_5:
1522 rlm_req.rlm.bw = CMD_CBW_5MHZ;
1523 break;
1524 case NL80211_CHAN_WIDTH_10:
1525 rlm_req.rlm.bw = CMD_CBW_10MHZ;
1526 break;
1527 case NL80211_CHAN_WIDTH_20_NOHT:
1528 case NL80211_CHAN_WIDTH_20:
1529 default:
1530 rlm_req.rlm.bw = CMD_CBW_20MHZ;
1531 rlm_req.rlm.ht_op_info = 0;
1532 break;
1533 }
1534
1535 if (rlm_req.rlm.control_channel < rlm_req.rlm.center_chan)
1536 rlm_req.rlm.sco = 1; /* SCA */
1537 else if (rlm_req.rlm.control_channel > rlm_req.rlm.center_chan)
1538 rlm_req.rlm.sco = 3; /* SCB */
1539
1540 return mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE), &rlm_req,
1541 sizeof(rlm_req), true);
1542 }
1543 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_set_chctx);
1544
1545 int mt76_connac_mcu_uni_add_bss(struct mt76_phy *phy,
1546 struct ieee80211_vif *vif,
1547 struct mt76_wcid *wcid,
1548 bool enable,
1549 struct ieee80211_chanctx_conf *ctx)
1550 {
1551 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
1552 struct cfg80211_chan_def *chandef = ctx ? &ctx->def : &phy->chandef;
1553 enum nl80211_band band = chandef->chan->band;
1554 struct mt76_dev *mdev = phy->dev;
1555 struct {
1556 struct {
1557 u8 bss_idx;
1558 u8 pad[3];
1559 } __packed hdr;
1560 struct mt76_connac_bss_basic_tlv basic;
1561 struct mt76_connac_bss_qos_tlv qos;
1562 } basic_req = {
1563 .hdr = {
1564 .bss_idx = mvif->idx,
1565 },
1566 .basic = {
1567 .tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1568 .len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1569 .bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1570 .dtim_period = vif->bss_conf.dtim_period,
1571 .omac_idx = mvif->omac_idx,
1572 .band_idx = mvif->band_idx,
1573 .wmm_idx = mvif->wmm_idx,
1574 .active = true, /* keep bss deactivated */
1575 .phymode = mt76_connac_get_phy_mode(phy, vif, band, NULL),
1576 },
1577 .qos = {
1578 .tag = cpu_to_le16(UNI_BSS_INFO_QBSS),
1579 .len = cpu_to_le16(sizeof(struct mt76_connac_bss_qos_tlv)),
1580 .qos = vif->bss_conf.qos,
1581 },
1582 };
1583 int err, conn_type;
1584 u8 idx, basic_phy;
1585
1586 idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1587 basic_req.basic.hw_bss_idx = idx;
1588 if (band == NL80211_BAND_6GHZ)
1589 basic_req.basic.phymode_ext = PHY_MODE_AX_6G;
1590
1591 basic_phy = mt76_connac_get_phy_mode_v2(phy, vif, band, NULL);
1592 basic_req.basic.nonht_basic_phy = cpu_to_le16(basic_phy);
1593
1594 switch (vif->type) {
1595 case NL80211_IFTYPE_MESH_POINT:
1596 case NL80211_IFTYPE_AP:
1597 if (vif->p2p)
1598 conn_type = CONNECTION_P2P_GO;
1599 else
1600 conn_type = CONNECTION_INFRA_AP;
1601 basic_req.basic.conn_type = cpu_to_le32(conn_type);
1602 /* Fully active/deactivate BSS network in AP mode only */
1603 basic_req.basic.active = enable;
1604 break;
1605 case NL80211_IFTYPE_STATION:
1606 if (vif->p2p)
1607 conn_type = CONNECTION_P2P_GC;
1608 else
1609 conn_type = CONNECTION_INFRA_STA;
1610 basic_req.basic.conn_type = cpu_to_le32(conn_type);
1611 break;
1612 case NL80211_IFTYPE_ADHOC:
1613 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1614 break;
1615 default:
1616 WARN_ON(1);
1617 break;
1618 }
1619
1620 memcpy(basic_req.basic.bssid, vif->bss_conf.bssid, ETH_ALEN);
1621 basic_req.basic.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx);
1622 basic_req.basic.sta_idx = cpu_to_le16(wcid->idx);
1623 basic_req.basic.conn_state = !enable;
1624
1625 err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE), &basic_req,
1626 sizeof(basic_req), true);
1627 if (err < 0)
1628 return err;
1629
1630 if (vif->bss_conf.he_support) {
1631 struct {
1632 struct {
1633 u8 bss_idx;
1634 u8 pad[3];
1635 } __packed hdr;
1636 struct bss_info_uni_he he;
1637 struct bss_info_uni_bss_color bss_color;
1638 } he_req = {
1639 .hdr = {
1640 .bss_idx = mvif->idx,
1641 },
1642 .he = {
1643 .tag = cpu_to_le16(UNI_BSS_INFO_HE_BASIC),
1644 .len = cpu_to_le16(sizeof(struct bss_info_uni_he)),
1645 },
1646 .bss_color = {
1647 .tag = cpu_to_le16(UNI_BSS_INFO_BSS_COLOR),
1648 .len = cpu_to_le16(sizeof(struct bss_info_uni_bss_color)),
1649 .enable = 0,
1650 .bss_color = 0,
1651 },
1652 };
1653
1654 if (enable) {
1655 he_req.bss_color.enable =
1656 vif->bss_conf.he_bss_color.enabled;
1657 he_req.bss_color.bss_color =
1658 vif->bss_conf.he_bss_color.color;
1659 }
1660
1661 mt76_connac_mcu_uni_bss_he_tlv(phy, vif,
1662 (struct tlv *)&he_req.he);
1663 err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE),
1664 &he_req, sizeof(he_req), true);
1665 if (err < 0)
1666 return err;
1667 }
1668
1669 if (enable && vif->bss_conf.bssid_indicator) {
1670 struct {
1671 struct {
1672 u8 bss_idx;
1673 u8 pad[3];
1674 } __packed hdr;
1675 struct bss_info_uni_mbssid mbssid;
1676 } mbssid_req = {
1677 .hdr = {
1678 .bss_idx = mvif->idx,
1679 },
1680 .mbssid = {
1681 .tag = cpu_to_le16(UNI_BSS_INFO_11V_MBSSID),
1682 .len = cpu_to_le16(sizeof(struct bss_info_uni_mbssid)),
1683 .max_indicator = vif->bss_conf.bssid_indicator,
1684 .mbss_idx = vif->bss_conf.bssid_index,
1685 },
1686 };
1687
1688 err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE),
1689 &mbssid_req, sizeof(mbssid_req), true);
1690 if (err < 0)
1691 return err;
1692 }
1693
1694 return mt76_connac_mcu_uni_set_chctx(phy, mvif, ctx);
1695 }
1696 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_bss);
1697
1698 void mt76_connac_mcu_build_rnr_scan_param(struct mt76_dev *mdev,
1699 struct cfg80211_scan_request *sreq)
1700 {
1701 struct ieee80211_channel **scan_list = sreq->channels;
1702 int i, bssid_index = 0;
1703
1704 /* clear 6G active Scan BSSID table */
1705 memset(&mdev->rnr, 0, sizeof(mdev->rnr));
1706
1707 for (i = 0; i < sreq->n_6ghz_params; i++) {
1708 u8 ch_idx = sreq->scan_6ghz_params[i].channel_idx;
1709 int k = 0;
1710
1711 /* Remove the duplicated BSSID */
1712 for (k = 0; k < bssid_index; k++) {
1713 if (!memcmp(&mdev->rnr.bssid[k],
1714 sreq->scan_6ghz_params[i].bssid,
1715 ETH_ALEN))
1716 break;
1717 }
1718
1719 if (k == bssid_index &&
1720 bssid_index < MT76_RNR_SCAN_MAX_BSSIDS) {
1721 memcpy(&mdev->rnr.bssid[bssid_index++],
1722 sreq->scan_6ghz_params[i].bssid, ETH_ALEN);
1723 mdev->rnr.channel[k] = scan_list[ch_idx]->hw_value;
1724 }
1725 }
1726
1727 mdev->rnr.bssid_num = bssid_index;
1728
1729 if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1730 memcpy(mdev->rnr.random_mac, sreq->mac_addr, ETH_ALEN);
1731 mdev->rnr.sreq_flag = sreq->flags;
1732 }
1733 }
1734 EXPORT_SYMBOL_GPL(mt76_connac_mcu_build_rnr_scan_param);
1735
1736 #define MT76_CONNAC_SCAN_CHANNEL_TIME 60
1737 int mt76_connac_mcu_hw_scan(struct mt76_phy *phy, struct ieee80211_vif *vif,
1738 struct ieee80211_scan_request *scan_req)
1739 {
1740 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
1741 struct cfg80211_scan_request *sreq = &scan_req->req;
1742 int n_ssids = 0, err, i, duration;
1743 int ext_channels_num = max_t(int, sreq->n_channels - 32, 0);
1744 struct ieee80211_channel **scan_list = sreq->channels;
1745 struct mt76_dev *mdev = phy->dev;
1746 struct mt76_connac_mcu_scan_channel *chan;
1747 struct mt76_connac_hw_scan_req *req;
1748 struct sk_buff *skb;
1749
1750 if (test_bit(MT76_HW_SCANNING, &phy->state))
1751 return -EBUSY;
1752
1753 skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req));
1754 if (!skb)
1755 return -ENOMEM;
1756
1757 set_bit(MT76_HW_SCANNING, &phy->state);
1758 mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1759
1760 req = (struct mt76_connac_hw_scan_req *)skb_put_zero(skb, sizeof(*req));
1761
1762 req->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
1763 req->bss_idx = mvif->idx;
1764 req->scan_type = sreq->n_ssids ? 1 : 0;
1765 req->probe_req_num = sreq->n_ssids ? 2 : 0;
1766 req->version = 1;
1767
1768 for (i = 0; i < sreq->n_ssids; i++) {
1769 if (!sreq->ssids[i].ssid_len)
1770 continue;
1771
1772 req->ssids[n_ssids].ssid_len = cpu_to_le32(sreq->ssids[i].ssid_len);
1773 memcpy(req->ssids[n_ssids].ssid, sreq->ssids[i].ssid,
1774 sreq->ssids[i].ssid_len);
1775 n_ssids++;
1776 }
1777 req->ssid_type = n_ssids ? BIT(2) : BIT(0);
1778 req->ssid_type_ext = n_ssids ? BIT(0) : 0;
1779 req->ssids_num = n_ssids;
1780
1781 duration = is_mt7921(phy->dev) ? 0 : MT76_CONNAC_SCAN_CHANNEL_TIME;
1782 /* increase channel time for passive scan */
1783 if (!sreq->n_ssids)
1784 duration *= 2;
1785 req->timeout_value = cpu_to_le16(sreq->n_channels * duration);
1786 req->channel_min_dwell_time = cpu_to_le16(duration);
1787 req->channel_dwell_time = cpu_to_le16(duration);
1788
1789 if (sreq->n_channels == 0 || sreq->n_channels > 64) {
1790 req->channel_type = 0;
1791 req->channels_num = 0;
1792 req->ext_channels_num = 0;
1793 } else {
1794 req->channel_type = 4;
1795 req->channels_num = min_t(u8, sreq->n_channels, 32);
1796 req->ext_channels_num = min_t(u8, ext_channels_num, 32);
1797 }
1798
1799 for (i = 0; i < req->channels_num + req->ext_channels_num; i++) {
1800 if (i >= 32)
1801 chan = &req->ext_channels[i - 32];
1802 else
1803 chan = &req->channels[i];
1804
1805 switch (scan_list[i]->band) {
1806 case NL80211_BAND_2GHZ:
1807 chan->band = 1;
1808 break;
1809 case NL80211_BAND_6GHZ:
1810 chan->band = 3;
1811 break;
1812 default:
1813 chan->band = 2;
1814 break;
1815 }
1816 chan->channel_num = scan_list[i]->hw_value;
1817 }
1818
1819 if (sreq->ie_len > 0) {
1820 memcpy(req->ies, sreq->ie, sreq->ie_len);
1821 req->ies_len = cpu_to_le16(sreq->ie_len);
1822 }
1823
1824 if (is_mt7921(phy->dev))
1825 req->scan_func |= SCAN_FUNC_SPLIT_SCAN;
1826
1827 memcpy(req->bssid, sreq->bssid, ETH_ALEN);
1828 if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1829 get_random_mask_addr(req->random_mac, sreq->mac_addr,
1830 sreq->mac_addr_mask);
1831 req->scan_func |= SCAN_FUNC_RANDOM_MAC;
1832 }
1833
1834 err = mt76_mcu_skb_send_msg(mdev, skb, MCU_CE_CMD(START_HW_SCAN),
1835 false);
1836 if (err < 0)
1837 clear_bit(MT76_HW_SCANNING, &phy->state);
1838
1839 return err;
1840 }
1841 EXPORT_SYMBOL_GPL(mt76_connac_mcu_hw_scan);
1842
1843 int mt76_connac_mcu_cancel_hw_scan(struct mt76_phy *phy,
1844 struct ieee80211_vif *vif)
1845 {
1846 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
1847 struct {
1848 u8 seq_num;
1849 u8 is_ext_channel;
1850 u8 rsv[2];
1851 } __packed req = {
1852 .seq_num = mvif->scan_seq_num,
1853 };
1854
1855 if (test_and_clear_bit(MT76_HW_SCANNING, &phy->state)) {
1856 struct cfg80211_scan_info info = {
1857 .aborted = true,
1858 };
1859
1860 ieee80211_scan_completed(phy->hw, &info);
1861 }
1862
1863 return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(CANCEL_HW_SCAN),
1864 &req, sizeof(req), false);
1865 }
1866 EXPORT_SYMBOL_GPL(mt76_connac_mcu_cancel_hw_scan);
1867
1868 int mt76_connac_mcu_sched_scan_req(struct mt76_phy *phy,
1869 struct ieee80211_vif *vif,
1870 struct cfg80211_sched_scan_request *sreq)
1871 {
1872 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
1873 struct ieee80211_channel **scan_list = sreq->channels;
1874 struct mt76_connac_mcu_scan_channel *chan;
1875 struct mt76_connac_sched_scan_req *req;
1876 struct mt76_dev *mdev = phy->dev;
1877 struct cfg80211_match_set *match;
1878 struct cfg80211_ssid *ssid;
1879 struct sk_buff *skb;
1880 int i;
1881
1882 skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req) + sreq->ie_len);
1883 if (!skb)
1884 return -ENOMEM;
1885
1886 mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1887
1888 req = (struct mt76_connac_sched_scan_req *)skb_put_zero(skb, sizeof(*req));
1889 req->version = 1;
1890 req->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
1891
1892 if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1893 u8 *addr = is_mt7663(phy->dev) ? req->mt7663.random_mac
1894 : req->mt7921.random_mac;
1895
1896 req->scan_func = 1;
1897 get_random_mask_addr(addr, sreq->mac_addr,
1898 sreq->mac_addr_mask);
1899 }
1900 if (is_mt7921(phy->dev)) {
1901 req->mt7921.bss_idx = mvif->idx;
1902 req->mt7921.delay = cpu_to_le32(sreq->delay);
1903 }
1904
1905 req->ssids_num = sreq->n_ssids;
1906 for (i = 0; i < req->ssids_num; i++) {
1907 ssid = &sreq->ssids[i];
1908 memcpy(req->ssids[i].ssid, ssid->ssid, ssid->ssid_len);
1909 req->ssids[i].ssid_len = cpu_to_le32(ssid->ssid_len);
1910 }
1911
1912 req->match_num = sreq->n_match_sets;
1913 for (i = 0; i < req->match_num; i++) {
1914 match = &sreq->match_sets[i];
1915 memcpy(req->match[i].ssid, match->ssid.ssid,
1916 match->ssid.ssid_len);
1917 req->match[i].rssi_th = cpu_to_le32(match->rssi_thold);
1918 req->match[i].ssid_len = match->ssid.ssid_len;
1919 }
1920
1921 req->channel_type = sreq->n_channels ? 4 : 0;
1922 req->channels_num = min_t(u8, sreq->n_channels, 64);
1923 for (i = 0; i < req->channels_num; i++) {
1924 chan = &req->channels[i];
1925
1926 switch (scan_list[i]->band) {
1927 case NL80211_BAND_2GHZ:
1928 chan->band = 1;
1929 break;
1930 case NL80211_BAND_6GHZ:
1931 chan->band = 3;
1932 break;
1933 default:
1934 chan->band = 2;
1935 break;
1936 }
1937 chan->channel_num = scan_list[i]->hw_value;
1938 }
1939
1940 req->intervals_num = sreq->n_scan_plans;
1941 for (i = 0; i < req->intervals_num; i++)
1942 req->intervals[i] = cpu_to_le16(sreq->scan_plans[i].interval);
1943
1944 if (sreq->ie_len > 0) {
1945 req->ie_len = cpu_to_le16(sreq->ie_len);
1946 memcpy(skb_put(skb, sreq->ie_len), sreq->ie, sreq->ie_len);
1947 }
1948
1949 return mt76_mcu_skb_send_msg(mdev, skb, MCU_CE_CMD(SCHED_SCAN_REQ),
1950 false);
1951 }
1952 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_req);
1953
1954 int mt76_connac_mcu_sched_scan_enable(struct mt76_phy *phy,
1955 struct ieee80211_vif *vif,
1956 bool enable)
1957 {
1958 struct {
1959 u8 active; /* 0: enabled 1: disabled */
1960 u8 rsv[3];
1961 } __packed req = {
1962 .active = !enable,
1963 };
1964
1965 if (enable)
1966 set_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1967 else
1968 clear_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1969
1970 return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(SCHED_SCAN_ENABLE),
1971 &req, sizeof(req), false);
1972 }
1973 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_enable);
1974
1975 int mt76_connac_mcu_chip_config(struct mt76_dev *dev)
1976 {
1977 struct mt76_connac_config req = {
1978 .resp_type = 0,
1979 };
1980
1981 strscpy(req.data, "assert");
1982
1983 return mt76_mcu_send_msg(dev, MCU_CE_CMD(CHIP_CONFIG),
1984 &req, sizeof(req), false);
1985 }
1986 EXPORT_SYMBOL_GPL(mt76_connac_mcu_chip_config);
1987
1988 int mt76_connac_mcu_set_deep_sleep(struct mt76_dev *dev, bool enable)
1989 {
1990 struct mt76_connac_config req = {
1991 .resp_type = 0,
1992 };
1993
1994 snprintf(req.data, sizeof(req.data), "KeepFullPwr %d", !enable);
1995
1996 return mt76_mcu_send_msg(dev, MCU_CE_CMD(CHIP_CONFIG),
1997 &req, sizeof(req), false);
1998 }
1999 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_deep_sleep);
2000
2001 int mt76_connac_sta_state_dp(struct mt76_dev *dev,
2002 enum ieee80211_sta_state old_state,
2003 enum ieee80211_sta_state new_state)
2004 {
2005 if ((old_state == IEEE80211_STA_ASSOC &&
2006 new_state == IEEE80211_STA_AUTHORIZED) ||
2007 (old_state == IEEE80211_STA_NONE &&
2008 new_state == IEEE80211_STA_NOTEXIST))
2009 mt76_connac_mcu_set_deep_sleep(dev, true);
2010
2011 if ((old_state == IEEE80211_STA_NOTEXIST &&
2012 new_state == IEEE80211_STA_NONE) ||
2013 (old_state == IEEE80211_STA_AUTHORIZED &&
2014 new_state == IEEE80211_STA_ASSOC))
2015 mt76_connac_mcu_set_deep_sleep(dev, false);
2016
2017 return 0;
2018 }
2019 EXPORT_SYMBOL_GPL(mt76_connac_sta_state_dp);
2020
2021 void mt76_connac_mcu_coredump_event(struct mt76_dev *dev, struct sk_buff *skb,
2022 struct mt76_connac_coredump *coredump)
2023 {
2024 spin_lock_bh(&dev->lock);
2025 __skb_queue_tail(&coredump->msg_list, skb);
2026 spin_unlock_bh(&dev->lock);
2027
2028 coredump->last_activity = jiffies;
2029
2030 queue_delayed_work(dev->wq, &coredump->work,
2031 MT76_CONNAC_COREDUMP_TIMEOUT);
2032 }
2033 EXPORT_SYMBOL_GPL(mt76_connac_mcu_coredump_event);
2034
2035 static void
2036 mt76_connac_mcu_build_sku(struct mt76_dev *dev, s8 *sku,
2037 struct mt76_power_limits *limits,
2038 enum nl80211_band band)
2039 {
2040 int max_power = is_mt7921(dev) ? 127 : 63;
2041 int i, offset = sizeof(limits->cck);
2042
2043 memset(sku, max_power, MT_SKU_POWER_LIMIT);
2044
2045 if (band == NL80211_BAND_2GHZ) {
2046 /* cck */
2047 memcpy(sku, limits->cck, sizeof(limits->cck));
2048 }
2049
2050 /* ofdm */
2051 memcpy(&sku[offset], limits->ofdm, sizeof(limits->ofdm));
2052 offset += sizeof(limits->ofdm);
2053
2054 /* ht */
2055 for (i = 0; i < 2; i++) {
2056 memcpy(&sku[offset], limits->mcs[i], 8);
2057 offset += 8;
2058 }
2059 sku[offset++] = limits->mcs[0][0];
2060
2061 /* vht */
2062 for (i = 0; i < ARRAY_SIZE(limits->mcs); i++) {
2063 memcpy(&sku[offset], limits->mcs[i],
2064 ARRAY_SIZE(limits->mcs[i]));
2065 offset += 12;
2066 }
2067
2068 if (!is_mt7921(dev))
2069 return;
2070
2071 /* he */
2072 for (i = 0; i < ARRAY_SIZE(limits->ru); i++) {
2073 memcpy(&sku[offset], limits->ru[i], ARRAY_SIZE(limits->ru[i]));
2074 offset += ARRAY_SIZE(limits->ru[i]);
2075 }
2076 }
2077
2078 s8 mt76_connac_get_ch_power(struct mt76_phy *phy,
2079 struct ieee80211_channel *chan,
2080 s8 target_power)
2081 {
2082 struct mt76_dev *dev = phy->dev;
2083 struct ieee80211_supported_band *sband;
2084 int i;
2085
2086 switch (chan->band) {
2087 case NL80211_BAND_2GHZ:
2088 sband = &phy->sband_2g.sband;
2089 break;
2090 case NL80211_BAND_5GHZ:
2091 sband = &phy->sband_5g.sband;
2092 break;
2093 case NL80211_BAND_6GHZ:
2094 sband = &phy->sband_6g.sband;
2095 break;
2096 default:
2097 return target_power;
2098 }
2099
2100 for (i = 0; i < sband->n_channels; i++) {
2101 struct ieee80211_channel *ch = &sband->channels[i];
2102
2103 if (ch->hw_value == chan->hw_value) {
2104 if (!(ch->flags & IEEE80211_CHAN_DISABLED)) {
2105 int power = 2 * ch->max_reg_power;
2106
2107 if (is_mt7663(dev) && (power > 63 || power < -64))
2108 power = 63;
2109 target_power = min_t(s8, power, target_power);
2110 }
2111 break;
2112 }
2113 }
2114
2115 return target_power;
2116 }
2117 EXPORT_SYMBOL_GPL(mt76_connac_get_ch_power);
2118
2119 static int
2120 mt76_connac_mcu_rate_txpower_band(struct mt76_phy *phy,
2121 enum nl80211_band band)
2122 {
2123 struct mt76_dev *dev = phy->dev;
2124 int sku_len, batch_len = is_mt7921(dev) ? 8 : 16;
2125 static const u8 chan_list_2ghz[] = {
2126 1, 2, 3, 4, 5, 6, 7,
2127 8, 9, 10, 11, 12, 13, 14
2128 };
2129 static const u8 chan_list_5ghz[] = {
2130 36, 38, 40, 42, 44, 46, 48,
2131 50, 52, 54, 56, 58, 60, 62,
2132 64, 100, 102, 104, 106, 108, 110,
2133 112, 114, 116, 118, 120, 122, 124,
2134 126, 128, 132, 134, 136, 138, 140,
2135 142, 144, 149, 151, 153, 155, 157,
2136 159, 161, 165, 169, 173, 177
2137 };
2138 static const u8 chan_list_6ghz[] = {
2139 1, 3, 5, 7, 9, 11, 13,
2140 15, 17, 19, 21, 23, 25, 27,
2141 29, 33, 35, 37, 39, 41, 43,
2142 45, 47, 49, 51, 53, 55, 57,
2143 59, 61, 65, 67, 69, 71, 73,
2144 75, 77, 79, 81, 83, 85, 87,
2145 89, 91, 93, 97, 99, 101, 103,
2146 105, 107, 109, 111, 113, 115, 117,
2147 119, 121, 123, 125, 129, 131, 133,
2148 135, 137, 139, 141, 143, 145, 147,
2149 149, 151, 153, 155, 157, 161, 163,
2150 165, 167, 169, 171, 173, 175, 177,
2151 179, 181, 183, 185, 187, 189, 193,
2152 195, 197, 199, 201, 203, 205, 207,
2153 209, 211, 213, 215, 217, 219, 221,
2154 225, 227, 229, 233
2155 };
2156 int i, n_chan, batch_size, idx = 0, tx_power, last_ch, err = 0;
2157 struct mt76_connac_sku_tlv sku_tlbv;
2158 struct mt76_power_limits *limits;
2159 const u8 *ch_list;
2160
2161 limits = devm_kmalloc(dev->dev, sizeof(*limits), GFP_KERNEL);
2162 if (!limits)
2163 return -ENOMEM;
2164
2165 sku_len = is_mt7921(dev) ? sizeof(sku_tlbv) : sizeof(sku_tlbv) - 92;
2166 tx_power = 2 * phy->hw->conf.power_level;
2167 if (!tx_power)
2168 tx_power = 127;
2169
2170 if (band == NL80211_BAND_2GHZ) {
2171 n_chan = ARRAY_SIZE(chan_list_2ghz);
2172 ch_list = chan_list_2ghz;
2173 } else if (band == NL80211_BAND_6GHZ) {
2174 n_chan = ARRAY_SIZE(chan_list_6ghz);
2175 ch_list = chan_list_6ghz;
2176 } else {
2177 n_chan = ARRAY_SIZE(chan_list_5ghz);
2178 ch_list = chan_list_5ghz;
2179 }
2180 batch_size = DIV_ROUND_UP(n_chan, batch_len);
2181
2182 if (phy->cap.has_6ghz)
2183 last_ch = chan_list_6ghz[ARRAY_SIZE(chan_list_6ghz) - 1];
2184 else if (phy->cap.has_5ghz)
2185 last_ch = chan_list_5ghz[ARRAY_SIZE(chan_list_5ghz) - 1];
2186 else
2187 last_ch = chan_list_2ghz[ARRAY_SIZE(chan_list_2ghz) - 1];
2188
2189 for (i = 0; i < batch_size; i++) {
2190 struct mt76_connac_tx_power_limit_tlv tx_power_tlv = {};
2191 int j, msg_len, num_ch;
2192 struct sk_buff *skb;
2193
2194 num_ch = i == batch_size - 1 ? n_chan - i * batch_len : batch_len;
2195 msg_len = sizeof(tx_power_tlv) + num_ch * sizeof(sku_tlbv);
2196 skb = mt76_mcu_msg_alloc(dev, NULL, msg_len);
2197 if (!skb) {
2198 err = -ENOMEM;
2199 goto out;
2200 }
2201
2202 skb_reserve(skb, sizeof(tx_power_tlv));
2203
2204 BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(tx_power_tlv.alpha2));
2205 memcpy(tx_power_tlv.alpha2, dev->alpha2, sizeof(dev->alpha2));
2206 tx_power_tlv.n_chan = num_ch;
2207
2208 switch (band) {
2209 case NL80211_BAND_2GHZ:
2210 tx_power_tlv.band = 1;
2211 break;
2212 case NL80211_BAND_6GHZ:
2213 tx_power_tlv.band = 3;
2214 break;
2215 default:
2216 tx_power_tlv.band = 2;
2217 break;
2218 }
2219
2220 for (j = 0; j < num_ch; j++, idx++) {
2221 struct ieee80211_channel chan = {
2222 .hw_value = ch_list[idx],
2223 .band = band,
2224 };
2225 s8 reg_power, sar_power;
2226
2227 reg_power = mt76_connac_get_ch_power(phy, &chan,
2228 tx_power);
2229 sar_power = mt76_get_sar_power(phy, &chan, reg_power);
2230
2231 mt76_get_rate_power_limits(phy, &chan, limits,
2232 sar_power);
2233
2234 tx_power_tlv.last_msg = ch_list[idx] == last_ch;
2235 sku_tlbv.channel = ch_list[idx];
2236
2237 mt76_connac_mcu_build_sku(dev, sku_tlbv.pwr_limit,
2238 limits, band);
2239 skb_put_data(skb, &sku_tlbv, sku_len);
2240 }
2241 __skb_push(skb, sizeof(tx_power_tlv));
2242 memcpy(skb->data, &tx_power_tlv, sizeof(tx_power_tlv));
2243
2244 err = mt76_mcu_skb_send_msg(dev, skb,
2245 MCU_CE_CMD(SET_RATE_TX_POWER),
2246 false);
2247 if (err < 0)
2248 goto out;
2249 }
2250
2251 out:
2252 devm_kfree(dev->dev, limits);
2253 return err;
2254 }
2255
2256 int mt76_connac_mcu_set_rate_txpower(struct mt76_phy *phy)
2257 {
2258 int err;
2259
2260 if (phy->cap.has_2ghz) {
2261 err = mt76_connac_mcu_rate_txpower_band(phy,
2262 NL80211_BAND_2GHZ);
2263 if (err < 0)
2264 return err;
2265 }
2266 if (phy->cap.has_5ghz) {
2267 err = mt76_connac_mcu_rate_txpower_band(phy,
2268 NL80211_BAND_5GHZ);
2269 if (err < 0)
2270 return err;
2271 }
2272 if (phy->cap.has_6ghz) {
2273 err = mt76_connac_mcu_rate_txpower_band(phy,
2274 NL80211_BAND_6GHZ);
2275 if (err < 0)
2276 return err;
2277 }
2278
2279 return 0;
2280 }
2281 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rate_txpower);
2282
2283 int mt76_connac_mcu_update_arp_filter(struct mt76_dev *dev,
2284 struct mt76_vif_link *vif,
2285 struct ieee80211_bss_conf *info)
2286 {
2287 struct ieee80211_vif *mvif = container_of(info, struct ieee80211_vif,
2288 bss_conf);
2289 struct sk_buff *skb;
2290 int i, len = min_t(int, mvif->cfg.arp_addr_cnt,
2291 IEEE80211_BSS_ARP_ADDR_LIST_LEN);
2292 struct {
2293 struct {
2294 u8 bss_idx;
2295 u8 pad[3];
2296 } __packed hdr;
2297 struct mt76_connac_arpns_tlv arp;
2298 } req_hdr = {
2299 .hdr = {
2300 .bss_idx = vif->idx,
2301 },
2302 .arp = {
2303 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
2304 .len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
2305 .ips_num = len,
2306 .mode = 2, /* update */
2307 .option = 1,
2308 },
2309 };
2310
2311 skb = mt76_mcu_msg_alloc(dev, NULL,
2312 sizeof(req_hdr) + len * sizeof(__be32));
2313 if (!skb)
2314 return -ENOMEM;
2315
2316 skb_put_data(skb, &req_hdr, sizeof(req_hdr));
2317 for (i = 0; i < len; i++)
2318 skb_put_data(skb, &mvif->cfg.arp_addr_list[i], sizeof(__be32));
2319
2320 return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(OFFLOAD), true);
2321 }
2322 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_arp_filter);
2323
2324 int mt76_connac_mcu_set_p2p_oppps(struct ieee80211_hw *hw,
2325 struct ieee80211_vif *vif)
2326 {
2327 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2328 int ct_window = vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
2329 struct mt76_phy *phy = hw->priv;
2330 struct {
2331 __le32 ct_win;
2332 u8 bss_idx;
2333 u8 rsv[3];
2334 } __packed req = {
2335 .ct_win = cpu_to_le32(ct_window),
2336 .bss_idx = mvif->idx,
2337 };
2338
2339 return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(SET_P2P_OPPPS),
2340 &req, sizeof(req), false);
2341 }
2342 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_p2p_oppps);
2343
2344 #ifdef CONFIG_PM
2345
2346 const struct wiphy_wowlan_support mt76_connac_wowlan_support = {
2347 .flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
2348 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY | WIPHY_WOWLAN_NET_DETECT,
2349 .n_patterns = 1,
2350 .pattern_min_len = 1,
2351 .pattern_max_len = MT76_CONNAC_WOW_PATTEN_MAX_LEN,
2352 .max_nd_match_sets = 10,
2353 };
2354 EXPORT_SYMBOL_GPL(mt76_connac_wowlan_support);
2355
2356 static void
2357 mt76_connac_mcu_key_iter(struct ieee80211_hw *hw,
2358 struct ieee80211_vif *vif,
2359 struct ieee80211_sta *sta,
2360 struct ieee80211_key_conf *key,
2361 void *data)
2362 {
2363 struct mt76_connac_gtk_rekey_tlv *gtk_tlv = data;
2364 u32 cipher;
2365
2366 if (key->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
2367 key->cipher != WLAN_CIPHER_SUITE_CCMP &&
2368 key->cipher != WLAN_CIPHER_SUITE_TKIP)
2369 return;
2370
2371 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2372 cipher = BIT(3);
2373 else
2374 cipher = BIT(4);
2375
2376 /* we are assuming here to have a single pairwise key */
2377 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
2378 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2379 gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_1);
2380 else
2381 gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_2);
2382
2383 gtk_tlv->pairwise_cipher = cpu_to_le32(cipher);
2384 gtk_tlv->keyid = key->keyidx;
2385 } else {
2386 gtk_tlv->group_cipher = cpu_to_le32(cipher);
2387 }
2388 }
2389
2390 int mt76_connac_mcu_update_gtk_rekey(struct ieee80211_hw *hw,
2391 struct ieee80211_vif *vif,
2392 struct cfg80211_gtk_rekey_data *key)
2393 {
2394 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2395 struct mt76_connac_gtk_rekey_tlv *gtk_tlv;
2396 struct mt76_phy *phy = hw->priv;
2397 struct sk_buff *skb;
2398 struct {
2399 u8 bss_idx;
2400 u8 pad[3];
2401 } __packed hdr = {
2402 .bss_idx = mvif->idx,
2403 };
2404
2405 skb = mt76_mcu_msg_alloc(phy->dev, NULL,
2406 sizeof(hdr) + sizeof(*gtk_tlv));
2407 if (!skb)
2408 return -ENOMEM;
2409
2410 skb_put_data(skb, &hdr, sizeof(hdr));
2411 gtk_tlv = (struct mt76_connac_gtk_rekey_tlv *)skb_put_zero(skb,
2412 sizeof(*gtk_tlv));
2413 gtk_tlv->tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY);
2414 gtk_tlv->len = cpu_to_le16(sizeof(*gtk_tlv));
2415 gtk_tlv->rekey_mode = 2;
2416 gtk_tlv->option = 1;
2417
2418 rcu_read_lock();
2419 ieee80211_iter_keys_rcu(hw, vif, mt76_connac_mcu_key_iter, gtk_tlv);
2420 rcu_read_unlock();
2421
2422 memcpy(gtk_tlv->kek, key->kek, NL80211_KEK_LEN);
2423 memcpy(gtk_tlv->kck, key->kck, NL80211_KCK_LEN);
2424 memcpy(gtk_tlv->replay_ctr, key->replay_ctr, NL80211_REPLAY_CTR_LEN);
2425
2426 return mt76_mcu_skb_send_msg(phy->dev, skb,
2427 MCU_UNI_CMD(OFFLOAD), true);
2428 }
2429 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_gtk_rekey);
2430
2431 static int
2432 mt76_connac_mcu_set_arp_filter(struct mt76_dev *dev, struct ieee80211_vif *vif,
2433 bool suspend)
2434 {
2435 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2436 struct {
2437 struct {
2438 u8 bss_idx;
2439 u8 pad[3];
2440 } __packed hdr;
2441 struct mt76_connac_arpns_tlv arpns;
2442 } req = {
2443 .hdr = {
2444 .bss_idx = mvif->idx,
2445 },
2446 .arpns = {
2447 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
2448 .len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
2449 .mode = suspend,
2450 },
2451 };
2452
2453 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(OFFLOAD), &req,
2454 sizeof(req), true);
2455 }
2456
2457 int
2458 mt76_connac_mcu_set_gtk_rekey(struct mt76_dev *dev, struct ieee80211_vif *vif,
2459 bool suspend)
2460 {
2461 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2462 struct {
2463 struct {
2464 u8 bss_idx;
2465 u8 pad[3];
2466 } __packed hdr;
2467 struct mt76_connac_gtk_rekey_tlv gtk_tlv;
2468 } __packed req = {
2469 .hdr = {
2470 .bss_idx = mvif->idx,
2471 },
2472 .gtk_tlv = {
2473 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY),
2474 .len = cpu_to_le16(sizeof(struct mt76_connac_gtk_rekey_tlv)),
2475 .rekey_mode = !suspend,
2476 },
2477 };
2478
2479 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(OFFLOAD), &req,
2480 sizeof(req), true);
2481 }
2482 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_gtk_rekey);
2483
2484 int
2485 mt76_connac_mcu_set_suspend_mode(struct mt76_dev *dev,
2486 struct ieee80211_vif *vif,
2487 bool enable, u8 mdtim,
2488 bool wow_suspend)
2489 {
2490 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2491 struct {
2492 struct {
2493 u8 bss_idx;
2494 u8 pad[3];
2495 } __packed hdr;
2496 struct mt76_connac_suspend_tlv suspend_tlv;
2497 } req = {
2498 .hdr = {
2499 .bss_idx = mvif->idx,
2500 },
2501 .suspend_tlv = {
2502 .tag = cpu_to_le16(UNI_SUSPEND_MODE_SETTING),
2503 .len = cpu_to_le16(sizeof(struct mt76_connac_suspend_tlv)),
2504 .enable = enable,
2505 .mdtim = mdtim,
2506 .wow_suspend = wow_suspend,
2507 },
2508 };
2509
2510 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(SUSPEND), &req,
2511 sizeof(req), true);
2512 }
2513 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_suspend_mode);
2514
2515 static int
2516 mt76_connac_mcu_set_wow_pattern(struct mt76_dev *dev,
2517 struct ieee80211_vif *vif,
2518 u8 index, bool enable,
2519 struct cfg80211_pkt_pattern *pattern)
2520 {
2521 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2522 struct mt76_connac_wow_pattern_tlv *ptlv;
2523 struct sk_buff *skb;
2524 struct req_hdr {
2525 u8 bss_idx;
2526 u8 pad[3];
2527 } __packed hdr = {
2528 .bss_idx = mvif->idx,
2529 };
2530
2531 skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(hdr) + sizeof(*ptlv));
2532 if (!skb)
2533 return -ENOMEM;
2534
2535 skb_put_data(skb, &hdr, sizeof(hdr));
2536 ptlv = (struct mt76_connac_wow_pattern_tlv *)skb_put_zero(skb, sizeof(*ptlv));
2537 ptlv->tag = cpu_to_le16(UNI_SUSPEND_WOW_PATTERN);
2538 ptlv->len = cpu_to_le16(sizeof(*ptlv));
2539 ptlv->data_len = pattern->pattern_len;
2540 ptlv->enable = enable;
2541 ptlv->index = index;
2542
2543 memcpy(ptlv->pattern, pattern->pattern, pattern->pattern_len);
2544 memcpy(ptlv->mask, pattern->mask, DIV_ROUND_UP(pattern->pattern_len, 8));
2545
2546 return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(SUSPEND), true);
2547 }
2548
2549 int
2550 mt76_connac_mcu_set_wow_ctrl(struct mt76_phy *phy, struct ieee80211_vif *vif,
2551 bool suspend, struct cfg80211_wowlan *wowlan)
2552 {
2553 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2554 struct mt76_dev *dev = phy->dev;
2555 struct {
2556 struct {
2557 u8 bss_idx;
2558 u8 pad[3];
2559 } __packed hdr;
2560 struct mt76_connac_wow_ctrl_tlv wow_ctrl_tlv;
2561 struct mt76_connac_wow_gpio_param_tlv gpio_tlv;
2562 } req = {
2563 .hdr = {
2564 .bss_idx = mvif->idx,
2565 },
2566 .wow_ctrl_tlv = {
2567 .tag = cpu_to_le16(UNI_SUSPEND_WOW_CTRL),
2568 .len = cpu_to_le16(sizeof(struct mt76_connac_wow_ctrl_tlv)),
2569 .cmd = suspend ? 1 : 2,
2570 },
2571 .gpio_tlv = {
2572 .tag = cpu_to_le16(UNI_SUSPEND_WOW_GPIO_PARAM),
2573 .len = cpu_to_le16(sizeof(struct mt76_connac_wow_gpio_param_tlv)),
2574 .gpio_pin = 0xff, /* follow fw about GPIO pin */
2575 },
2576 };
2577
2578 if (wowlan->magic_pkt)
2579 req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_MAGIC;
2580 if (wowlan->disconnect)
2581 req.wow_ctrl_tlv.trigger |= (UNI_WOW_DETECT_TYPE_DISCONNECT |
2582 UNI_WOW_DETECT_TYPE_BCN_LOST);
2583 if (wowlan->nd_config) {
2584 mt76_connac_mcu_sched_scan_req(phy, vif, wowlan->nd_config);
2585 req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_SCH_SCAN_HIT;
2586 mt76_connac_mcu_sched_scan_enable(phy, vif, suspend);
2587 }
2588 if (wowlan->n_patterns)
2589 req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_BITMAP;
2590
2591 if (mt76_is_mmio(dev))
2592 req.wow_ctrl_tlv.wakeup_hif = WOW_PCIE;
2593 else if (mt76_is_usb(dev))
2594 req.wow_ctrl_tlv.wakeup_hif = WOW_USB;
2595 else if (mt76_is_sdio(dev))
2596 req.wow_ctrl_tlv.wakeup_hif = WOW_GPIO;
2597
2598 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(SUSPEND), &req,
2599 sizeof(req), true);
2600 }
2601 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_wow_ctrl);
2602
2603 int mt76_connac_mcu_set_hif_suspend(struct mt76_dev *dev, bool suspend, bool wait_resp)
2604 {
2605 struct {
2606 struct {
2607 u8 hif_type; /* 0x0: HIF_SDIO
2608 * 0x1: HIF_USB
2609 * 0x2: HIF_PCIE
2610 */
2611 u8 pad[3];
2612 } __packed hdr;
2613 struct hif_suspend_tlv {
2614 __le16 tag;
2615 __le16 len;
2616 u8 suspend;
2617 u8 pad[7];
2618 } __packed hif_suspend;
2619 } req = {
2620 .hif_suspend = {
2621 .tag = cpu_to_le16(0), /* 0: UNI_HIF_CTRL_BASIC */
2622 .len = cpu_to_le16(sizeof(struct hif_suspend_tlv)),
2623 .suspend = suspend,
2624 },
2625 };
2626
2627 if (mt76_is_mmio(dev))
2628 req.hdr.hif_type = 2;
2629 else if (mt76_is_usb(dev))
2630 req.hdr.hif_type = 1;
2631 else if (mt76_is_sdio(dev))
2632 req.hdr.hif_type = 0;
2633
2634 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(HIF_CTRL), &req,
2635 sizeof(req), wait_resp);
2636 }
2637 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_hif_suspend);
2638
2639 void mt76_connac_mcu_set_suspend_iter(void *priv, u8 *mac,
2640 struct ieee80211_vif *vif)
2641 {
2642 struct mt76_phy *phy = priv;
2643 bool suspend = !test_bit(MT76_STATE_RUNNING, &phy->state);
2644 struct ieee80211_hw *hw = phy->hw;
2645 struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
2646 int i;
2647
2648 mt76_connac_mcu_set_gtk_rekey(phy->dev, vif, suspend);
2649 mt76_connac_mcu_set_arp_filter(phy->dev, vif, suspend);
2650
2651 mt76_connac_mcu_set_suspend_mode(phy->dev, vif, suspend, 1, true);
2652
2653 for (i = 0; i < wowlan->n_patterns; i++)
2654 mt76_connac_mcu_set_wow_pattern(phy->dev, vif, i, suspend,
2655 &wowlan->patterns[i]);
2656 mt76_connac_mcu_set_wow_ctrl(phy, vif, suspend, wowlan);
2657 }
2658 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_suspend_iter);
2659 #endif /* CONFIG_PM */
2660
2661 u32 mt76_connac_mcu_reg_rr(struct mt76_dev *dev, u32 offset)
2662 {
2663 struct {
2664 __le32 addr;
2665 __le32 val;
2666 } __packed req = {
2667 .addr = cpu_to_le32(offset),
2668 };
2669
2670 return mt76_mcu_send_msg(dev, MCU_CE_QUERY(REG_READ), &req,
2671 sizeof(req), true);
2672 }
2673 EXPORT_SYMBOL_GPL(mt76_connac_mcu_reg_rr);
2674
2675 void mt76_connac_mcu_reg_wr(struct mt76_dev *dev, u32 offset, u32 val)
2676 {
2677 struct {
2678 __le32 addr;
2679 __le32 val;
2680 } __packed req = {
2681 .addr = cpu_to_le32(offset),
2682 .val = cpu_to_le32(val),
2683 };
2684
2685 mt76_mcu_send_msg(dev, MCU_CE_CMD(REG_WRITE), &req,
2686 sizeof(req), false);
2687 }
2688 EXPORT_SYMBOL_GPL(mt76_connac_mcu_reg_wr);
2689
2690 static int
2691 mt76_connac_mcu_sta_key_tlv(struct mt76_connac_sta_key_conf *sta_key_conf,
2692 struct sk_buff *skb,
2693 struct ieee80211_key_conf *key,
2694 enum set_key_cmd cmd)
2695 {
2696 struct sta_rec_sec *sec;
2697 u32 len = sizeof(*sec);
2698 struct tlv *tlv;
2699
2700 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_KEY_V2, sizeof(*sec));
2701 sec = (struct sta_rec_sec *)tlv;
2702 sec->add = cmd;
2703
2704 if (cmd == SET_KEY) {
2705 struct sec_key *sec_key;
2706 u8 cipher;
2707
2708 cipher = mt76_connac_mcu_get_cipher(key->cipher);
2709 if (cipher == MCU_CIPHER_NONE)
2710 return -EOPNOTSUPP;
2711
2712 sec_key = &sec->key[0];
2713 sec_key->cipher_len = sizeof(*sec_key);
2714
2715 if (cipher == MCU_CIPHER_BIP_CMAC_128) {
2716 sec_key->cipher_id = MCU_CIPHER_AES_CCMP;
2717 sec_key->key_id = sta_key_conf->keyidx;
2718 sec_key->key_len = 16;
2719 memcpy(sec_key->key, sta_key_conf->key, 16);
2720
2721 sec_key = &sec->key[1];
2722 sec_key->cipher_id = MCU_CIPHER_BIP_CMAC_128;
2723 sec_key->cipher_len = sizeof(*sec_key);
2724 sec_key->key_len = 16;
2725 memcpy(sec_key->key, key->key, 16);
2726 sec->n_cipher = 2;
2727 } else {
2728 sec_key->cipher_id = cipher;
2729 sec_key->key_id = key->keyidx;
2730 sec_key->key_len = key->keylen;
2731 memcpy(sec_key->key, key->key, key->keylen);
2732
2733 if (cipher == MCU_CIPHER_TKIP) {
2734 /* Rx/Tx MIC keys are swapped */
2735 memcpy(sec_key->key + 16, key->key + 24, 8);
2736 memcpy(sec_key->key + 24, key->key + 16, 8);
2737 }
2738
2739 /* store key_conf for BIP batch update */
2740 if (cipher == MCU_CIPHER_AES_CCMP) {
2741 memcpy(sta_key_conf->key, key->key, key->keylen);
2742 sta_key_conf->keyidx = key->keyidx;
2743 }
2744
2745 len -= sizeof(*sec_key);
2746 sec->n_cipher = 1;
2747 }
2748 } else {
2749 len -= sizeof(sec->key);
2750 sec->n_cipher = 0;
2751 }
2752 sec->len = cpu_to_le16(len);
2753
2754 return 0;
2755 }
2756
2757 int mt76_connac_mcu_add_key(struct mt76_dev *dev, struct ieee80211_vif *vif,
2758 struct mt76_connac_sta_key_conf *sta_key_conf,
2759 struct ieee80211_key_conf *key, int mcu_cmd,
2760 struct mt76_wcid *wcid, enum set_key_cmd cmd)
2761 {
2762 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2763 struct sk_buff *skb;
2764 int ret;
2765
2766 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
2767 if (IS_ERR(skb))
2768 return PTR_ERR(skb);
2769
2770 ret = mt76_connac_mcu_sta_key_tlv(sta_key_conf, skb, key, cmd);
2771 if (ret) {
2772 dev_kfree_skb(skb);
2773 return ret;
2774 }
2775
2776 ret = mt76_connac_mcu_sta_wed_update(dev, skb);
2777 if (ret) {
2778 dev_kfree_skb(skb);
2779 return ret;
2780 }
2781
2782 return mt76_mcu_skb_send_msg(dev, skb, mcu_cmd, true);
2783 }
2784 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_key);
2785
2786 /* SIFS 20us + 512 byte beacon transmitted by 1Mbps (3906us) */
2787 #define BCN_TX_ESTIMATE_TIME (4096 + 20)
2788 void mt76_connac_mcu_bss_ext_tlv(struct sk_buff *skb, struct mt76_vif_link *mvif)
2789 {
2790 struct bss_info_ext_bss *ext;
2791 int ext_bss_idx, tsf_offset;
2792 struct tlv *tlv;
2793
2794 ext_bss_idx = mvif->omac_idx - EXT_BSSID_START;
2795 if (ext_bss_idx < 0)
2796 return;
2797
2798 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_EXT_BSS, sizeof(*ext));
2799
2800 ext = (struct bss_info_ext_bss *)tlv;
2801 tsf_offset = ext_bss_idx * BCN_TX_ESTIMATE_TIME;
2802 ext->mbss_tsf_offset = cpu_to_le32(tsf_offset);
2803 }
2804 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_ext_tlv);
2805
2806 int mt76_connac_mcu_bss_basic_tlv(struct sk_buff *skb,
2807 struct ieee80211_vif *vif,
2808 struct ieee80211_sta *sta,
2809 struct mt76_phy *phy, u16 wlan_idx,
2810 bool enable)
2811 {
2812 struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2813 u32 type = vif->p2p ? NETWORK_P2P : NETWORK_INFRA;
2814 struct bss_info_basic *bss;
2815 struct tlv *tlv;
2816
2817 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_BASIC, sizeof(*bss));
2818 bss = (struct bss_info_basic *)tlv;
2819
2820 switch (vif->type) {
2821 case NL80211_IFTYPE_MESH_POINT:
2822 case NL80211_IFTYPE_MONITOR:
2823 break;
2824 case NL80211_IFTYPE_AP:
2825 if (ieee80211_hw_check(phy->hw, SUPPORTS_MULTI_BSSID)) {
2826 u8 bssid_id = vif->bss_conf.bssid_indicator;
2827 struct wiphy *wiphy = phy->hw->wiphy;
2828
2829 if (bssid_id > ilog2(wiphy->mbssid_max_interfaces))
2830 return -EINVAL;
2831
2832 bss->non_tx_bssid = vif->bss_conf.bssid_index;
2833 bss->max_bssid = bssid_id;
2834 }
2835 break;
2836 case NL80211_IFTYPE_STATION:
2837 if (enable) {
2838 rcu_read_lock();
2839 if (!sta)
2840 sta = ieee80211_find_sta(vif,
2841 vif->bss_conf.bssid);
2842 /* TODO: enable BSS_INFO_UAPSD & BSS_INFO_PM */
2843 if (sta) {
2844 struct mt76_wcid *wcid;
2845
2846 wcid = (struct mt76_wcid *)sta->drv_priv;
2847 wlan_idx = wcid->idx;
2848 }
2849 rcu_read_unlock();
2850 }
2851 break;
2852 case NL80211_IFTYPE_ADHOC:
2853 type = NETWORK_IBSS;
2854 break;
2855 default:
2856 WARN_ON(1);
2857 break;
2858 }
2859
2860 bss->network_type = cpu_to_le32(type);
2861 bss->bmc_wcid_lo = to_wcid_lo(wlan_idx);
2862 bss->bmc_wcid_hi = to_wcid_hi(wlan_idx);
2863 bss->wmm_idx = mvif->wmm_idx;
2864 bss->active = enable;
2865 bss->cipher = mvif->cipher;
2866
2867 if (vif->type != NL80211_IFTYPE_MONITOR) {
2868 struct cfg80211_chan_def *chandef = &phy->chandef;
2869
2870 memcpy(bss->bssid, vif->bss_conf.bssid, ETH_ALEN);
2871 bss->bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int);
2872 bss->dtim_period = vif->bss_conf.dtim_period;
2873 bss->phy_mode = mt76_connac_get_phy_mode(phy, vif,
2874 chandef->chan->band, NULL);
2875 } else {
2876 memcpy(bss->bssid, phy->macaddr, ETH_ALEN);
2877 }
2878
2879 return 0;
2880 }
2881 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_basic_tlv);
2882
2883 #define ENTER_PM_STATE 1
2884 #define EXIT_PM_STATE 2
2885 int mt76_connac_mcu_set_pm(struct mt76_dev *dev, int band, int enter)
2886 {
2887 struct {
2888 u8 pm_number;
2889 u8 pm_state;
2890 u8 bssid[ETH_ALEN];
2891 u8 dtim_period;
2892 u8 wlan_idx_lo;
2893 __le16 bcn_interval;
2894 __le32 aid;
2895 __le32 rx_filter;
2896 u8 band_idx;
2897 u8 wlan_idx_hi;
2898 u8 rsv[2];
2899 __le32 feature;
2900 u8 omac_idx;
2901 u8 wmm_idx;
2902 u8 bcn_loss_cnt;
2903 u8 bcn_sp_duration;
2904 } __packed req = {
2905 .pm_number = 5,
2906 .pm_state = enter ? ENTER_PM_STATE : EXIT_PM_STATE,
2907 .band_idx = band,
2908 };
2909
2910 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(PM_STATE_CTRL), &req,
2911 sizeof(req), true);
2912 }
2913 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_pm);
2914
2915 int mt76_connac_mcu_restart(struct mt76_dev *dev)
2916 {
2917 struct {
2918 u8 power_mode;
2919 u8 rsv[3];
2920 } req = {
2921 .power_mode = 1,
2922 };
2923
2924 return mt76_mcu_send_msg(dev, MCU_CMD(NIC_POWER_CTRL), &req,
2925 sizeof(req), false);
2926 }
2927 EXPORT_SYMBOL_GPL(mt76_connac_mcu_restart);
2928
2929 int mt76_connac_mcu_del_wtbl_all(struct mt76_dev *dev)
2930 {
2931 struct wtbl_req_hdr req = {
2932 .operation = WTBL_RESET_ALL,
2933 };
2934
2935 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(WTBL_UPDATE),
2936 &req, sizeof(req), true);
2937 }
2938 EXPORT_SYMBOL_GPL(mt76_connac_mcu_del_wtbl_all);
2939
2940 int mt76_connac_mcu_rdd_cmd(struct mt76_dev *dev, int cmd, u8 index,
2941 u8 rx_sel, u8 val)
2942 {
2943 struct {
2944 u8 ctrl;
2945 u8 rdd_idx;
2946 u8 rdd_rx_sel;
2947 u8 val;
2948 u8 rsv[4];
2949 } __packed req = {
2950 .ctrl = cmd,
2951 .rdd_idx = index,
2952 .rdd_rx_sel = rx_sel,
2953 .val = val,
2954 };
2955
2956 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(SET_RDD_CTRL), &req,
2957 sizeof(req), true);
2958 }
2959 EXPORT_SYMBOL_GPL(mt76_connac_mcu_rdd_cmd);
2960
2961 static int
2962 mt76_connac_mcu_send_ram_firmware(struct mt76_dev *dev,
2963 const struct mt76_connac2_fw_trailer *hdr,
2964 const u8 *data, bool is_wa)
2965 {
2966 int i, offset = 0, max_len = mt76_is_sdio(dev) ? 2048 : 4096;
2967 u32 override = 0, option = 0;
2968
2969 for (i = 0; i < hdr->n_region; i++) {
2970 const struct mt76_connac2_fw_region *region;
2971 u32 len, addr, mode;
2972 int err;
2973
2974 region = (const void *)((const u8 *)hdr -
2975 (hdr->n_region - i) * sizeof(*region));
2976 mode = mt76_connac_mcu_gen_dl_mode(dev, region->feature_set,
2977 is_wa);
2978 len = le32_to_cpu(region->len);
2979 addr = le32_to_cpu(region->addr);
2980
2981 if (region->feature_set & FW_FEATURE_NON_DL)
2982 goto next;
2983
2984 if (region->feature_set & FW_FEATURE_OVERRIDE_ADDR)
2985 override = addr;
2986
2987 err = mt76_connac_mcu_init_download(dev, addr, len, mode);
2988 if (err) {
2989 dev_err(dev->dev, "Download request failed\n");
2990 return err;
2991 }
2992
2993 err = __mt76_mcu_send_firmware(dev, MCU_CMD(FW_SCATTER),
2994 data + offset, len, max_len);
2995 if (err) {
2996 dev_err(dev->dev, "Failed to send firmware.\n");
2997 return err;
2998 }
2999
3000 next:
3001 offset += len;
3002 }
3003
3004 if (override)
3005 option |= FW_START_OVERRIDE;
3006 if (is_wa)
3007 option |= FW_START_WORKING_PDA_CR4;
3008
3009 return mt76_connac_mcu_start_firmware(dev, override, option);
3010 }
3011
3012 int mt76_connac2_load_ram(struct mt76_dev *dev, const char *fw_wm,
3013 const char *fw_wa)
3014 {
3015 const struct mt76_connac2_fw_trailer *hdr;
3016 const struct firmware *fw;
3017 int ret;
3018
3019 ret = request_firmware(&fw, fw_wm, dev->dev);
3020 if (ret)
3021 return ret;
3022
3023 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
3024 dev_err(dev->dev, "Invalid firmware\n");
3025 ret = -EINVAL;
3026 goto out;
3027 }
3028
3029 hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
3030 dev_info(dev->dev, "WM Firmware Version: %.10s, Build Time: %.15s",
3031 hdr->fw_ver, hdr->build_date);
3032
3033 ret = mt76_connac_mcu_send_ram_firmware(dev, hdr, fw->data, false);
3034 if (ret) {
3035 dev_err(dev->dev, "Failed to start WM firmware\n");
3036 goto out;
3037 }
3038
3039 snprintf(dev->hw->wiphy->fw_version,
3040 sizeof(dev->hw->wiphy->fw_version),
3041 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
3042
3043 release_firmware(fw);
3044
3045 if (!fw_wa)
3046 return 0;
3047
3048 ret = request_firmware(&fw, fw_wa, dev->dev);
3049 if (ret)
3050 return ret;
3051
3052 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
3053 dev_err(dev->dev, "Invalid firmware\n");
3054 ret = -EINVAL;
3055 goto out;
3056 }
3057
3058 hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
3059 dev_info(dev->dev, "WA Firmware Version: %.10s, Build Time: %.15s",
3060 hdr->fw_ver, hdr->build_date);
3061
3062 ret = mt76_connac_mcu_send_ram_firmware(dev, hdr, fw->data, true);
3063 if (ret) {
3064 dev_err(dev->dev, "Failed to start WA firmware\n");
3065 goto out;
3066 }
3067
3068 snprintf(dev->hw->wiphy->fw_version,
3069 sizeof(dev->hw->wiphy->fw_version),
3070 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
3071
3072 out:
3073 release_firmware(fw);
3074
3075 return ret;
3076 }
3077 EXPORT_SYMBOL_GPL(mt76_connac2_load_ram);
3078
3079 static u32 mt76_connac2_get_data_mode(struct mt76_dev *dev, u32 info)
3080 {
3081 u32 mode = DL_MODE_NEED_RSP;
3082
3083 if ((!is_mt7921(dev) && !is_mt7925(dev)) || info == PATCH_SEC_NOT_SUPPORT)
3084 return mode;
3085
3086 switch (FIELD_GET(PATCH_SEC_ENC_TYPE_MASK, info)) {
3087 case PATCH_SEC_ENC_TYPE_PLAIN:
3088 break;
3089 case PATCH_SEC_ENC_TYPE_AES:
3090 mode |= DL_MODE_ENCRYPT;
3091 mode |= FIELD_PREP(DL_MODE_KEY_IDX,
3092 (info & PATCH_SEC_ENC_AES_KEY_MASK)) & DL_MODE_KEY_IDX;
3093 mode |= DL_MODE_RESET_SEC_IV;
3094 break;
3095 case PATCH_SEC_ENC_TYPE_SCRAMBLE:
3096 mode |= DL_MODE_ENCRYPT;
3097 mode |= DL_CONFIG_ENCRY_MODE_SEL;
3098 mode |= DL_MODE_RESET_SEC_IV;
3099 break;
3100 default:
3101 dev_err(dev->dev, "Encryption type not support!\n");
3102 }
3103
3104 return mode;
3105 }
3106
3107 int mt76_connac2_load_patch(struct mt76_dev *dev, const char *fw_name)
3108 {
3109 int i, ret, sem, max_len = mt76_is_sdio(dev) ? 2048 : 4096;
3110 const struct mt76_connac2_patch_hdr *hdr;
3111 const struct firmware *fw = NULL;
3112
3113 sem = mt76_connac_mcu_patch_sem_ctrl(dev, true);
3114 switch (sem) {
3115 case PATCH_IS_DL:
3116 return 0;
3117 case PATCH_NOT_DL_SEM_SUCCESS:
3118 break;
3119 default:
3120 dev_err(dev->dev, "Failed to get patch semaphore\n");
3121 return -EAGAIN;
3122 }
3123
3124 ret = request_firmware(&fw, fw_name, dev->dev);
3125 if (ret)
3126 goto out;
3127
3128 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
3129 dev_err(dev->dev, "Invalid firmware\n");
3130 ret = -EINVAL;
3131 goto out;
3132 }
3133
3134 hdr = (const void *)fw->data;
3135 dev_info(dev->dev, "HW/SW Version: 0x%x, Build Time: %.16s",
3136 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date);
3137
3138 for (i = 0; i < be32_to_cpu(hdr->desc.n_region); i++) {
3139 struct mt76_connac2_patch_sec *sec;
3140 u32 len, addr, mode;
3141 const u8 *dl;
3142 u32 sec_info;
3143
3144 sec = (void *)(fw->data + sizeof(*hdr) + i * sizeof(*sec));
3145 if ((be32_to_cpu(sec->type) & PATCH_SEC_TYPE_MASK) !=
3146 PATCH_SEC_TYPE_INFO) {
3147 ret = -EINVAL;
3148 goto out;
3149 }
3150
3151 addr = be32_to_cpu(sec->info.addr);
3152 len = be32_to_cpu(sec->info.len);
3153 dl = fw->data + be32_to_cpu(sec->offs);
3154 sec_info = be32_to_cpu(sec->info.sec_key_idx);
3155 mode = mt76_connac2_get_data_mode(dev, sec_info);
3156
3157 ret = mt76_connac_mcu_init_download(dev, addr, len, mode);
3158 if (ret) {
3159 dev_err(dev->dev, "Download request failed\n");
3160 goto out;
3161 }
3162
3163 ret = __mt76_mcu_send_firmware(dev, MCU_CMD(FW_SCATTER),
3164 dl, len, max_len);
3165 if (ret) {
3166 dev_err(dev->dev, "Failed to send patch\n");
3167 goto out;
3168 }
3169 }
3170
3171 ret = mt76_connac_mcu_start_patch(dev);
3172 if (ret)
3173 dev_err(dev->dev, "Failed to start patch\n");
3174
3175 out:
3176 sem = mt76_connac_mcu_patch_sem_ctrl(dev, false);
3177 switch (sem) {
3178 case PATCH_REL_SEM_SUCCESS:
3179 break;
3180 default:
3181 ret = -EAGAIN;
3182 dev_err(dev->dev, "Failed to release patch semaphore\n");
3183 break;
3184 }
3185
3186 release_firmware(fw);
3187
3188 return ret;
3189 }
3190 EXPORT_SYMBOL_GPL(mt76_connac2_load_patch);
3191
3192 int mt76_connac2_mcu_fill_message(struct mt76_dev *dev, struct sk_buff *skb,
3193 int cmd, int *wait_seq)
3194 {
3195 int txd_len, mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
3196 struct mt76_connac2_mcu_uni_txd *uni_txd;
3197 struct mt76_connac2_mcu_txd *mcu_txd;
3198 __le32 *txd;
3199 u32 val;
3200 u8 seq;
3201
3202 /* TODO: make dynamic based on msg type */
3203 dev->mcu.timeout = 20 * HZ;
3204
3205 seq = ++dev->mcu.msg_seq & 0xf;
3206 if (!seq)
3207 seq = ++dev->mcu.msg_seq & 0xf;
3208
3209 if (cmd == MCU_CMD(FW_SCATTER))
3210 goto exit;
3211
3212 txd_len = cmd & __MCU_CMD_FIELD_UNI ? sizeof(*uni_txd) : sizeof(*mcu_txd);
3213 txd = (__le32 *)skb_push(skb, txd_len);
3214
3215 val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) |
3216 FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CMD) |
3217 FIELD_PREP(MT_TXD0_Q_IDX, MT_TX_MCU_PORT_RX_Q0);
3218 txd[0] = cpu_to_le32(val);
3219
3220 val = MT_TXD1_LONG_FORMAT |
3221 FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD);
3222 txd[1] = cpu_to_le32(val);
3223
3224 if (cmd & __MCU_CMD_FIELD_UNI) {
3225 uni_txd = (struct mt76_connac2_mcu_uni_txd *)txd;
3226 uni_txd->len = cpu_to_le16(skb->len - sizeof(uni_txd->txd));
3227 uni_txd->option = MCU_CMD_UNI_EXT_ACK;
3228 uni_txd->cid = cpu_to_le16(mcu_cmd);
3229 uni_txd->s2d_index = MCU_S2D_H2N;
3230 uni_txd->pkt_type = MCU_PKT_ID;
3231 uni_txd->seq = seq;
3232
3233 goto exit;
3234 }
3235
3236 mcu_txd = (struct mt76_connac2_mcu_txd *)txd;
3237 mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd));
3238 mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU,
3239 MT_TX_MCU_PORT_RX_Q0));
3240 mcu_txd->pkt_type = MCU_PKT_ID;
3241 mcu_txd->seq = seq;
3242 mcu_txd->cid = mcu_cmd;
3243 mcu_txd->ext_cid = FIELD_GET(__MCU_CMD_FIELD_EXT_ID, cmd);
3244
3245 if (mcu_txd->ext_cid || (cmd & __MCU_CMD_FIELD_CE)) {
3246 if (cmd & __MCU_CMD_FIELD_QUERY)
3247 mcu_txd->set_query = MCU_Q_QUERY;
3248 else
3249 mcu_txd->set_query = MCU_Q_SET;
3250 mcu_txd->ext_cid_ack = !!mcu_txd->ext_cid;
3251 } else {
3252 mcu_txd->set_query = MCU_Q_NA;
3253 }
3254
3255 if (cmd & __MCU_CMD_FIELD_WA)
3256 mcu_txd->s2d_index = MCU_S2D_H2C;
3257 else
3258 mcu_txd->s2d_index = MCU_S2D_H2N;
3259
3260 exit:
3261 if (wait_seq)
3262 *wait_seq = seq;
3263
3264 return 0;
3265 }
3266 EXPORT_SYMBOL_GPL(mt76_connac2_mcu_fill_message);
3267
3268 MODULE_AUTHOR("Lorenzo Bianconi <[email protected]>");
3269 MODULE_DESCRIPTION("MediaTek MT76x connac layer helpers");
3270 MODULE_LICENSE("Dual BSD/GPL");