1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
4 #include <linux/firmware.h>
5 #include "mt76_connac2_mac.h"
6 #include "mt76_connac_mcu.h"
8 int mt76_connac_mcu_start_firmware(struct mt76_dev *dev, u32 addr, u32 option)
14 .option = cpu_to_le32(option),
15 .addr = cpu_to_le32(addr),
18 return mt76_mcu_send_msg(dev, MCU_CMD(FW_START_REQ), &req,
21 EXPORT_SYMBOL_GPL(mt76_connac_mcu_start_firmware);
23 int mt76_connac_mcu_patch_sem_ctrl(struct mt76_dev *dev, bool get)
25 u32 op = get ? PATCH_SEM_GET : PATCH_SEM_RELEASE;
29 .op = cpu_to_le32(op),
32 return mt76_mcu_send_msg(dev, MCU_CMD(PATCH_SEM_CONTROL),
33 &req, sizeof(req), true);
35 EXPORT_SYMBOL_GPL(mt76_connac_mcu_patch_sem_ctrl);
37 int mt76_connac_mcu_start_patch(struct mt76_dev *dev)
46 return mt76_mcu_send_msg(dev, MCU_CMD(PATCH_FINISH_REQ),
47 &req, sizeof(req), true);
49 EXPORT_SYMBOL_GPL(mt76_connac_mcu_start_patch);
51 #define MCU_PATCH_ADDRESS 0x200000
53 int mt76_connac_mcu_init_download(struct mt76_dev *dev, u32 addr, u32 len,
61 .addr = cpu_to_le32(addr),
62 .len = cpu_to_le32(len),
63 .mode = cpu_to_le32(mode),
67 if ((!is_connac_v1(dev) && addr == MCU_PATCH_ADDRESS) ||
68 (is_mt7921(dev) && addr == 0x900000) ||
69 (is_mt7996(dev) && addr == 0x900000))
70 cmd = MCU_CMD(PATCH_START_REQ);
72 cmd = MCU_CMD(TARGET_ADDRESS_LEN_REQ);
74 return mt76_mcu_send_msg(dev, cmd, &req, sizeof(req), true);
76 EXPORT_SYMBOL_GPL(mt76_connac_mcu_init_download);
78 int mt76_connac_mcu_set_channel_domain(struct mt76_phy *phy)
80 int len, i, n_max_channels, n_2ch = 0, n_5ch = 0, n_6ch = 0;
81 struct mt76_connac_mcu_channel_domain {
82 u8 alpha2[4]; /* regulatory_request.alpha2 */
83 u8 bw_2g; /* BW_20_40M 0
98 .bw_5g = 3, /* BW_20_40_80_160M */
101 struct mt76_connac_mcu_chan {
106 struct mt76_dev *dev = phy->dev;
107 struct ieee80211_channel *chan;
110 n_max_channels = phy->sband_2g.sband.n_channels +
111 phy->sband_5g.sband.n_channels +
112 phy->sband_6g.sband.n_channels;
113 len = sizeof(hdr) + n_max_channels * sizeof(channel);
115 skb = mt76_mcu_msg_alloc(dev, NULL, len);
119 skb_reserve(skb, sizeof(hdr));
121 for (i = 0; i < phy->sband_2g.sband.n_channels; i++) {
122 chan = &phy->sband_2g.sband.channels[i];
123 if (chan->flags & IEEE80211_CHAN_DISABLED)
126 channel.hw_value = cpu_to_le16(chan->hw_value);
127 channel.flags = cpu_to_le32(chan->flags);
130 skb_put_data(skb, &channel, sizeof(channel));
133 for (i = 0; i < phy->sband_5g.sband.n_channels; i++) {
134 chan = &phy->sband_5g.sband.channels[i];
135 if (chan->flags & IEEE80211_CHAN_DISABLED)
138 channel.hw_value = cpu_to_le16(chan->hw_value);
139 channel.flags = cpu_to_le32(chan->flags);
142 skb_put_data(skb, &channel, sizeof(channel));
145 for (i = 0; i < phy->sband_6g.sband.n_channels; i++) {
146 chan = &phy->sband_6g.sband.channels[i];
147 if (chan->flags & IEEE80211_CHAN_DISABLED)
150 channel.hw_value = cpu_to_le16(chan->hw_value);
151 channel.flags = cpu_to_le32(chan->flags);
154 skb_put_data(skb, &channel, sizeof(channel));
158 BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(hdr.alpha2));
159 memcpy(hdr.alpha2, dev->alpha2, sizeof(dev->alpha2));
164 memcpy(__skb_push(skb, sizeof(hdr)), &hdr, sizeof(hdr));
166 return mt76_mcu_skb_send_msg(dev, skb, MCU_CE_CMD(SET_CHAN_DOMAIN),
169 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_channel_domain);
171 int mt76_connac_mcu_set_mac_enable(struct mt76_dev *dev, int band, bool enable,
178 } __packed req_mac = {
183 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(MAC_INIT_CTRL), &req_mac,
184 sizeof(req_mac), true);
186 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_mac_enable);
188 int mt76_connac_mcu_set_vif_ps(struct mt76_dev *dev, struct ieee80211_vif *vif)
190 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
193 u8 ps_state; /* 0: device awake
194 * 1: static power save
195 * 2: dynamic power saving
198 .bss_idx = mvif->idx,
199 .ps_state = vif->cfg.ps ? 2 : 0,
202 if (vif->type != NL80211_IFTYPE_STATION)
205 return mt76_mcu_send_msg(dev, MCU_CE_CMD(SET_PS_PROFILE),
206 &req, sizeof(req), false);
208 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_vif_ps);
210 int mt76_connac_mcu_set_rts_thresh(struct mt76_dev *dev, u32 val, u8 band)
221 .len_thresh = cpu_to_le32(val),
222 .pkt_thresh = cpu_to_le32(0x2),
225 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(PROTECT_CTRL), &req,
228 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rts_thresh);
230 void mt76_connac_mcu_beacon_loss_iter(void *priv, u8 *mac,
231 struct ieee80211_vif *vif)
233 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
234 struct mt76_connac_beacon_loss_event *event = priv;
236 if (mvif->idx != event->bss_idx)
239 if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
242 ieee80211_beacon_loss(vif);
244 EXPORT_SYMBOL_GPL(mt76_connac_mcu_beacon_loss_iter);
247 mt76_connac_mcu_add_nested_tlv(struct sk_buff *skb, int tag, int len,
248 void *sta_ntlv, void *sta_wtbl)
250 struct sta_ntlv_hdr *ntlv_hdr = sta_ntlv;
251 struct tlv *sta_hdr = sta_wtbl;
252 struct tlv *ptlv, tlv = {
253 .tag = cpu_to_le16(tag),
254 .len = cpu_to_le16(len),
258 ptlv = skb_put(skb, len);
259 memcpy(ptlv, &tlv, sizeof(tlv));
261 ntlv = le16_to_cpu(ntlv_hdr->tlv_num);
262 ntlv_hdr->tlv_num = cpu_to_le16(ntlv + 1);
265 len += le16_to_cpu(sta_hdr->len);
266 sta_hdr->len = cpu_to_le16(len);
271 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_nested_tlv);
274 __mt76_connac_mcu_alloc_sta_req(struct mt76_dev *dev, struct mt76_vif *mvif,
275 struct mt76_wcid *wcid, int len)
277 struct sta_req_hdr hdr = {
278 .bss_idx = mvif->idx,
279 .muar_idx = wcid ? mvif->omac_idx : 0,
284 mt76_connac_mcu_get_wlan_idx(dev, wcid, &hdr.wlan_idx_lo,
286 skb = mt76_mcu_msg_alloc(dev, NULL, len);
288 return ERR_PTR(-ENOMEM);
290 skb_put_data(skb, &hdr, sizeof(hdr));
294 EXPORT_SYMBOL_GPL(__mt76_connac_mcu_alloc_sta_req);
296 struct wtbl_req_hdr *
297 mt76_connac_mcu_alloc_wtbl_req(struct mt76_dev *dev, struct mt76_wcid *wcid,
298 int cmd, void *sta_wtbl, struct sk_buff **skb)
300 struct tlv *sta_hdr = sta_wtbl;
301 struct wtbl_req_hdr hdr = {
304 struct sk_buff *nskb = *skb;
306 mt76_connac_mcu_get_wlan_idx(dev, wcid, &hdr.wlan_idx_lo,
309 nskb = mt76_mcu_msg_alloc(dev, NULL,
310 MT76_CONNAC_WTBL_UPDATE_MAX_SIZE);
312 return ERR_PTR(-ENOMEM);
318 le16_add_cpu(&sta_hdr->len, sizeof(hdr));
320 return skb_put_data(nskb, &hdr, sizeof(hdr));
322 EXPORT_SYMBOL_GPL(mt76_connac_mcu_alloc_wtbl_req);
324 void mt76_connac_mcu_bss_omac_tlv(struct sk_buff *skb,
325 struct ieee80211_vif *vif)
327 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
328 u8 omac_idx = mvif->omac_idx;
329 struct bss_info_omac *omac;
334 case NL80211_IFTYPE_MONITOR:
335 case NL80211_IFTYPE_MESH_POINT:
336 case NL80211_IFTYPE_AP:
338 type = CONNECTION_P2P_GO;
340 type = CONNECTION_INFRA_AP;
342 case NL80211_IFTYPE_STATION:
344 type = CONNECTION_P2P_GC;
346 type = CONNECTION_INFRA_STA;
348 case NL80211_IFTYPE_ADHOC:
349 type = CONNECTION_IBSS_ADHOC;
356 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_OMAC, sizeof(*omac));
358 omac = (struct bss_info_omac *)tlv;
359 omac->conn_type = cpu_to_le32(type);
360 omac->omac_idx = mvif->omac_idx;
361 omac->band_idx = mvif->band_idx;
362 omac->hw_bss_idx = omac_idx > EXT_BSSID_START ? HW_BSSID_0 : omac_idx;
364 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_omac_tlv);
366 void mt76_connac_mcu_sta_basic_tlv(struct mt76_dev *dev, struct sk_buff *skb,
367 struct ieee80211_vif *vif,
368 struct ieee80211_sta *sta,
369 bool enable, bool newly)
371 struct sta_rec_basic *basic;
375 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BASIC, sizeof(*basic));
377 basic = (struct sta_rec_basic *)tlv;
378 basic->extra_info = cpu_to_le16(EXTRA_INFO_VER);
382 basic->extra_info |= cpu_to_le16(EXTRA_INFO_NEW);
383 basic->conn_state = CONN_STATE_PORT_SECURE;
385 basic->conn_state = CONN_STATE_DISCONNECT;
389 basic->conn_type = cpu_to_le32(CONNECTION_INFRA_BC);
390 eth_broadcast_addr(basic->peer_addr);
395 case NL80211_IFTYPE_MESH_POINT:
396 case NL80211_IFTYPE_AP:
397 if (vif->p2p && !is_mt7921(dev))
398 conn_type = CONNECTION_P2P_GC;
400 conn_type = CONNECTION_INFRA_STA;
401 basic->conn_type = cpu_to_le32(conn_type);
402 basic->aid = cpu_to_le16(sta->aid);
404 case NL80211_IFTYPE_STATION:
405 if (vif->p2p && !is_mt7921(dev))
406 conn_type = CONNECTION_P2P_GO;
408 conn_type = CONNECTION_INFRA_AP;
409 basic->conn_type = cpu_to_le32(conn_type);
410 basic->aid = cpu_to_le16(vif->cfg.aid);
412 case NL80211_IFTYPE_ADHOC:
413 basic->conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
414 basic->aid = cpu_to_le16(sta->aid);
421 memcpy(basic->peer_addr, sta->addr, ETH_ALEN);
422 basic->qos = sta->wme;
424 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_basic_tlv);
426 void mt76_connac_mcu_sta_uapsd(struct sk_buff *skb, struct ieee80211_vif *vif,
427 struct ieee80211_sta *sta)
429 struct sta_rec_uapsd *uapsd;
432 if (vif->type != NL80211_IFTYPE_AP || !sta->wme)
435 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_APPS, sizeof(*uapsd));
436 uapsd = (struct sta_rec_uapsd *)tlv;
438 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) {
439 uapsd->dac_map |= BIT(3);
440 uapsd->tac_map |= BIT(3);
442 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI) {
443 uapsd->dac_map |= BIT(2);
444 uapsd->tac_map |= BIT(2);
446 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE) {
447 uapsd->dac_map |= BIT(1);
448 uapsd->tac_map |= BIT(1);
450 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK) {
451 uapsd->dac_map |= BIT(0);
452 uapsd->tac_map |= BIT(0);
454 uapsd->max_sp = sta->max_sp;
456 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_uapsd);
458 void mt76_connac_mcu_wtbl_hdr_trans_tlv(struct sk_buff *skb,
459 struct ieee80211_vif *vif,
460 struct mt76_wcid *wcid,
461 void *sta_wtbl, void *wtbl_tlv)
463 struct wtbl_hdr_trans *htr;
466 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HDR_TRANS,
469 htr = (struct wtbl_hdr_trans *)tlv;
470 htr->no_rx_trans = true;
472 if (vif->type == NL80211_IFTYPE_STATION)
480 htr->no_rx_trans = !test_bit(MT_WCID_FLAG_HDR_TRANS, &wcid->flags);
481 if (test_bit(MT_WCID_FLAG_4ADDR, &wcid->flags)) {
486 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_hdr_trans_tlv);
488 int mt76_connac_mcu_sta_update_hdr_trans(struct mt76_dev *dev,
489 struct ieee80211_vif *vif,
490 struct mt76_wcid *wcid, int cmd)
492 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
493 struct wtbl_req_hdr *wtbl_hdr;
494 struct tlv *sta_wtbl;
497 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
501 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
504 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
506 if (IS_ERR(wtbl_hdr))
507 return PTR_ERR(wtbl_hdr);
509 mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, vif, wcid, sta_wtbl, wtbl_hdr);
511 return mt76_mcu_skb_send_msg(dev, skb, cmd, true);
513 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_update_hdr_trans);
515 int mt76_connac_mcu_wtbl_update_hdr_trans(struct mt76_dev *dev,
516 struct ieee80211_vif *vif,
517 struct ieee80211_sta *sta)
519 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
520 struct wtbl_req_hdr *wtbl_hdr;
521 struct sk_buff *skb = NULL;
523 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET, NULL,
525 if (IS_ERR(wtbl_hdr))
526 return PTR_ERR(wtbl_hdr);
528 mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, vif, wcid, NULL, wtbl_hdr);
530 return mt76_mcu_skb_send_msg(dev, skb, MCU_EXT_CMD(WTBL_UPDATE), true);
532 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_update_hdr_trans);
534 void mt76_connac_mcu_wtbl_generic_tlv(struct mt76_dev *dev,
536 struct ieee80211_vif *vif,
537 struct ieee80211_sta *sta,
538 void *sta_wtbl, void *wtbl_tlv)
540 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
541 struct wtbl_generic *generic;
543 struct wtbl_spe *spe;
546 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_GENERIC,
550 generic = (struct wtbl_generic *)tlv;
553 if (vif->type == NL80211_IFTYPE_STATION)
554 generic->partial_aid = cpu_to_le16(vif->cfg.aid);
556 generic->partial_aid = cpu_to_le16(sta->aid);
557 memcpy(generic->peer_addr, sta->addr, ETH_ALEN);
558 generic->muar_idx = mvif->omac_idx;
559 generic->qos = sta->wme;
561 if (!is_connac_v1(dev) && vif->type == NL80211_IFTYPE_STATION)
562 memcpy(generic->peer_addr, vif->bss_conf.bssid,
565 eth_broadcast_addr(generic->peer_addr);
567 generic->muar_idx = 0xe;
570 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RX, sizeof(*rx),
573 rx = (struct wtbl_rx *)tlv;
574 rx->rca1 = sta ? vif->type != NL80211_IFTYPE_AP : 1;
578 if (!is_connac_v1(dev))
581 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SPE, sizeof(*spe),
583 spe = (struct wtbl_spe *)tlv;
586 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_generic_tlv);
589 mt76_connac_mcu_sta_amsdu_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
590 struct ieee80211_vif *vif)
592 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
593 struct sta_rec_amsdu *amsdu;
596 if (vif->type != NL80211_IFTYPE_AP &&
597 vif->type != NL80211_IFTYPE_STATION)
600 if (!sta->deflink.agg.max_amsdu_len)
603 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HW_AMSDU, sizeof(*amsdu));
604 amsdu = (struct sta_rec_amsdu *)tlv;
605 amsdu->max_amsdu_num = 8;
606 amsdu->amsdu_en = true;
607 amsdu->max_mpdu_size = sta->deflink.agg.max_amsdu_len >=
608 IEEE80211_MAX_MPDU_LEN_VHT_7991;
613 #define HE_PHY(p, c) u8_get_bits(c, IEEE80211_HE_PHY_##p)
614 #define HE_MAC(m, c) u8_get_bits(c, IEEE80211_HE_MAC_##m)
616 mt76_connac_mcu_sta_he_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
618 struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
619 struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
620 struct sta_rec_he *he;
624 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE, sizeof(*he));
626 he = (struct sta_rec_he *)tlv;
628 if (elem->mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
629 cap |= STA_REC_HE_CAP_HTC;
631 if (elem->mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
632 cap |= STA_REC_HE_CAP_BSR;
634 if (elem->mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
635 cap |= STA_REC_HE_CAP_OM;
637 if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU)
638 cap |= STA_REC_HE_CAP_AMSDU_IN_AMPDU;
640 if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
641 cap |= STA_REC_HE_CAP_BQR;
643 if (elem->phy_cap_info[0] &
644 (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G |
645 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G))
646 cap |= STA_REC_HE_CAP_BW20_RU242_SUPPORT;
648 if (elem->phy_cap_info[1] &
649 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
650 cap |= STA_REC_HE_CAP_LDPC;
652 if (elem->phy_cap_info[1] &
653 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US)
654 cap |= STA_REC_HE_CAP_SU_PPDU_1LTF_8US_GI;
656 if (elem->phy_cap_info[2] &
657 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US)
658 cap |= STA_REC_HE_CAP_NDP_4LTF_3DOT2MS_GI;
660 if (elem->phy_cap_info[2] &
661 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ)
662 cap |= STA_REC_HE_CAP_LE_EQ_80M_TX_STBC;
664 if (elem->phy_cap_info[2] &
665 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
666 cap |= STA_REC_HE_CAP_LE_EQ_80M_RX_STBC;
668 if (elem->phy_cap_info[6] &
669 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE)
670 cap |= STA_REC_HE_CAP_PARTIAL_BW_EXT_RANGE;
672 if (elem->phy_cap_info[7] &
673 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI)
674 cap |= STA_REC_HE_CAP_SU_MU_PPDU_4LTF_8US_GI;
676 if (elem->phy_cap_info[7] &
677 IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ)
678 cap |= STA_REC_HE_CAP_GT_80M_TX_STBC;
680 if (elem->phy_cap_info[7] &
681 IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ)
682 cap |= STA_REC_HE_CAP_GT_80M_RX_STBC;
684 if (elem->phy_cap_info[8] &
685 IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI)
686 cap |= STA_REC_HE_CAP_ER_SU_PPDU_4LTF_8US_GI;
688 if (elem->phy_cap_info[8] &
689 IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI)
690 cap |= STA_REC_HE_CAP_ER_SU_PPDU_1LTF_8US_GI;
692 if (elem->phy_cap_info[9] &
693 IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK)
694 cap |= STA_REC_HE_CAP_TRIG_CQI_FK;
696 if (elem->phy_cap_info[9] &
697 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU)
698 cap |= STA_REC_HE_CAP_TX_1024QAM_UNDER_RU242;
700 if (elem->phy_cap_info[9] &
701 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU)
702 cap |= STA_REC_HE_CAP_RX_1024QAM_UNDER_RU242;
704 he->he_cap = cpu_to_le32(cap);
706 switch (sta->deflink.bandwidth) {
707 case IEEE80211_STA_RX_BW_160:
708 if (elem->phy_cap_info[0] &
709 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
710 he->max_nss_mcs[CMD_HE_MCS_BW8080] =
711 he_cap->he_mcs_nss_supp.rx_mcs_80p80;
713 he->max_nss_mcs[CMD_HE_MCS_BW160] =
714 he_cap->he_mcs_nss_supp.rx_mcs_160;
717 he->max_nss_mcs[CMD_HE_MCS_BW80] =
718 he_cap->he_mcs_nss_supp.rx_mcs_80;
723 HE_MAC(CAP1_TF_MAC_PAD_DUR_MASK, elem->mac_cap_info[1]);
725 HE_MAC(CAP3_MAX_AMPDU_LEN_EXP_MASK, elem->mac_cap_info[3]);
728 HE_PHY(CAP0_CHANNEL_WIDTH_SET_MASK, elem->phy_cap_info[0]);
730 HE_PHY(CAP1_DEVICE_CLASS_A, elem->phy_cap_info[1]);
732 HE_PHY(CAP1_PREAMBLE_PUNC_RX_MASK, elem->phy_cap_info[1]);
735 HE_PHY(CAP3_DCM_MAX_CONST_TX_MASK, elem->phy_cap_info[3]);
737 HE_PHY(CAP3_DCM_MAX_TX_NSS_2, elem->phy_cap_info[3]);
739 HE_PHY(CAP3_DCM_MAX_CONST_RX_MASK, elem->phy_cap_info[3]);
741 HE_PHY(CAP3_DCM_MAX_RX_NSS_2, elem->phy_cap_info[3]);
743 HE_PHY(CAP8_DCM_MAX_RU_MASK, elem->phy_cap_info[8]);
749 mt76_connac_mcu_sta_he_tlv_v2(struct sk_buff *skb, struct ieee80211_sta *sta)
751 struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
752 struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
753 struct sta_rec_he_v2 *he;
756 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_V2, sizeof(*he));
758 he = (struct sta_rec_he_v2 *)tlv;
759 memcpy(he->he_phy_cap, elem->phy_cap_info, sizeof(he->he_phy_cap));
760 memcpy(he->he_mac_cap, elem->mac_cap_info, sizeof(he->he_mac_cap));
762 switch (sta->deflink.bandwidth) {
763 case IEEE80211_STA_RX_BW_160:
764 if (elem->phy_cap_info[0] &
765 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
766 he->max_nss_mcs[CMD_HE_MCS_BW8080] =
767 he_cap->he_mcs_nss_supp.rx_mcs_80p80;
769 he->max_nss_mcs[CMD_HE_MCS_BW160] =
770 he_cap->he_mcs_nss_supp.rx_mcs_160;
773 he->max_nss_mcs[CMD_HE_MCS_BW80] =
774 he_cap->he_mcs_nss_supp.rx_mcs_80;
778 he->pkt_ext = IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US;
782 mt76_connac_get_phy_mode_v2(struct mt76_phy *mphy, struct ieee80211_vif *vif,
783 enum nl80211_band band, struct ieee80211_sta *sta)
785 struct ieee80211_sta_ht_cap *ht_cap;
786 struct ieee80211_sta_vht_cap *vht_cap;
787 const struct ieee80211_sta_he_cap *he_cap;
791 ht_cap = &sta->deflink.ht_cap;
792 vht_cap = &sta->deflink.vht_cap;
793 he_cap = &sta->deflink.he_cap;
795 struct ieee80211_supported_band *sband;
797 sband = mphy->hw->wiphy->bands[band];
798 ht_cap = &sband->ht_cap;
799 vht_cap = &sband->vht_cap;
800 he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
803 if (band == NL80211_BAND_2GHZ) {
804 mode |= PHY_TYPE_BIT_HR_DSSS | PHY_TYPE_BIT_ERP;
806 if (ht_cap->ht_supported)
807 mode |= PHY_TYPE_BIT_HT;
809 if (he_cap && he_cap->has_he)
810 mode |= PHY_TYPE_BIT_HE;
811 } else if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ) {
812 mode |= PHY_TYPE_BIT_OFDM;
814 if (ht_cap->ht_supported)
815 mode |= PHY_TYPE_BIT_HT;
817 if (vht_cap->vht_supported)
818 mode |= PHY_TYPE_BIT_VHT;
820 if (he_cap && he_cap->has_he)
821 mode |= PHY_TYPE_BIT_HE;
827 void mt76_connac_mcu_sta_tlv(struct mt76_phy *mphy, struct sk_buff *skb,
828 struct ieee80211_sta *sta,
829 struct ieee80211_vif *vif,
830 u8 rcpi, u8 sta_state)
832 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
833 struct cfg80211_chan_def *chandef = mvif->ctx ?
834 &mvif->ctx->def : &mphy->chandef;
835 enum nl80211_band band = chandef->chan->band;
836 struct mt76_dev *dev = mphy->dev;
837 struct sta_rec_ra_info *ra_info;
838 struct sta_rec_state *state;
839 struct sta_rec_phy *phy;
844 if (sta->deflink.ht_cap.ht_supported) {
845 struct sta_rec_ht *ht;
847 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
848 ht = (struct sta_rec_ht *)tlv;
849 ht->ht_cap = cpu_to_le16(sta->deflink.ht_cap.cap);
853 if (sta->deflink.vht_cap.vht_supported) {
854 struct sta_rec_vht *vht;
857 len = is_mt7921(dev) ? sizeof(*vht) : sizeof(*vht) - 4;
858 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_VHT, len);
859 vht = (struct sta_rec_vht *)tlv;
860 vht->vht_cap = cpu_to_le32(sta->deflink.vht_cap.cap);
861 vht->vht_rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
862 vht->vht_tx_mcs_map = sta->deflink.vht_cap.vht_mcs.tx_mcs_map;
866 mt76_connac_mcu_sta_uapsd(skb, vif, sta);
871 if (sta->deflink.ht_cap.ht_supported || sta->deflink.he_cap.has_he)
872 mt76_connac_mcu_sta_amsdu_tlv(skb, sta, vif);
875 if (sta->deflink.he_cap.has_he) {
876 mt76_connac_mcu_sta_he_tlv(skb, sta);
877 mt76_connac_mcu_sta_he_tlv_v2(skb, sta);
878 if (band == NL80211_BAND_6GHZ &&
879 sta_state == MT76_STA_INFO_STATE_ASSOC) {
880 struct sta_rec_he_6g_capa *he_6g_capa;
882 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_6G,
883 sizeof(*he_6g_capa));
884 he_6g_capa = (struct sta_rec_he_6g_capa *)tlv;
885 he_6g_capa->capa = sta->deflink.he_6ghz_capa.capa;
889 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_PHY, sizeof(*phy));
890 phy = (struct sta_rec_phy *)tlv;
891 phy->phy_type = mt76_connac_get_phy_mode_v2(mphy, vif, band, sta);
892 phy->basic_rate = cpu_to_le16((u16)vif->bss_conf.basic_rates);
894 phy->ampdu = FIELD_PREP(IEEE80211_HT_AMPDU_PARM_FACTOR,
895 sta->deflink.ht_cap.ampdu_factor) |
896 FIELD_PREP(IEEE80211_HT_AMPDU_PARM_DENSITY,
897 sta->deflink.ht_cap.ampdu_density);
899 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra_info));
900 ra_info = (struct sta_rec_ra_info *)tlv;
902 supp_rates = sta->deflink.supp_rates[band];
903 if (band == NL80211_BAND_2GHZ)
904 supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates >> 4) |
905 FIELD_PREP(RA_LEGACY_CCK, supp_rates & 0xf);
907 supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates);
909 ra_info->legacy = cpu_to_le16(supp_rates);
911 if (sta->deflink.ht_cap.ht_supported)
912 memcpy(ra_info->rx_mcs_bitmask,
913 sta->deflink.ht_cap.mcs.rx_mask,
916 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_STATE, sizeof(*state));
917 state = (struct sta_rec_state *)tlv;
918 state->state = sta_state;
920 if (sta->deflink.vht_cap.vht_supported) {
921 state->vht_opmode = sta->deflink.bandwidth;
922 state->vht_opmode |= (sta->deflink.rx_nss - 1) <<
923 IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
926 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_tlv);
928 void mt76_connac_mcu_wtbl_smps_tlv(struct sk_buff *skb,
929 struct ieee80211_sta *sta,
930 void *sta_wtbl, void *wtbl_tlv)
932 struct wtbl_smps *smps;
935 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SMPS, sizeof(*smps),
937 smps = (struct wtbl_smps *)tlv;
938 smps->smps = (sta->deflink.smps_mode == IEEE80211_SMPS_DYNAMIC);
940 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_smps_tlv);
942 void mt76_connac_mcu_wtbl_ht_tlv(struct mt76_dev *dev, struct sk_buff *skb,
943 struct ieee80211_sta *sta, void *sta_wtbl,
944 void *wtbl_tlv, bool ht_ldpc, bool vht_ldpc)
946 struct wtbl_ht *ht = NULL;
950 if (sta->deflink.ht_cap.ht_supported || sta->deflink.he_6ghz_capa.capa) {
951 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HT, sizeof(*ht),
953 ht = (struct wtbl_ht *)tlv;
954 ht->ldpc = ht_ldpc &&
955 !!(sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING);
957 if (sta->deflink.ht_cap.ht_supported) {
958 ht->af = sta->deflink.ht_cap.ampdu_factor;
959 ht->mm = sta->deflink.ht_cap.ampdu_density;
961 ht->af = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
962 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
963 ht->mm = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
964 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
970 if (sta->deflink.vht_cap.vht_supported || sta->deflink.he_6ghz_capa.capa) {
971 struct wtbl_vht *vht;
974 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_VHT,
975 sizeof(*vht), wtbl_tlv,
977 vht = (struct wtbl_vht *)tlv;
978 vht->ldpc = vht_ldpc &&
979 !!(sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC);
982 af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
983 sta->deflink.vht_cap.cap);
985 ht->af = max(ht->af, af);
988 mt76_connac_mcu_wtbl_smps_tlv(skb, sta, sta_wtbl, wtbl_tlv);
990 if (is_connac_v1(dev) && sta->deflink.ht_cap.ht_supported) {
992 u32 msk = MT_WTBL_W5_SHORT_GI_20 | MT_WTBL_W5_SHORT_GI_40 |
993 MT_WTBL_W5_SHORT_GI_80 | MT_WTBL_W5_SHORT_GI_160;
994 struct wtbl_raw *raw;
996 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RAW_DATA,
997 sizeof(*raw), wtbl_tlv,
1000 if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
1001 flags |= MT_WTBL_W5_SHORT_GI_20;
1002 if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
1003 flags |= MT_WTBL_W5_SHORT_GI_40;
1005 if (sta->deflink.vht_cap.vht_supported) {
1006 if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80)
1007 flags |= MT_WTBL_W5_SHORT_GI_80;
1008 if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160)
1009 flags |= MT_WTBL_W5_SHORT_GI_160;
1011 raw = (struct wtbl_raw *)tlv;
1012 raw->val = cpu_to_le32(flags);
1013 raw->msk = cpu_to_le32(~msk);
1018 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ht_tlv);
1020 int mt76_connac_mcu_sta_cmd(struct mt76_phy *phy,
1021 struct mt76_sta_cmd_info *info)
1023 struct mt76_vif *mvif = (struct mt76_vif *)info->vif->drv_priv;
1024 struct mt76_dev *dev = phy->dev;
1025 struct wtbl_req_hdr *wtbl_hdr;
1026 struct tlv *sta_wtbl;
1027 struct sk_buff *skb;
1029 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, info->wcid);
1031 return PTR_ERR(skb);
1033 if (info->sta || !info->offload_fw)
1034 mt76_connac_mcu_sta_basic_tlv(dev, skb, info->vif, info->sta,
1035 info->enable, info->newly);
1036 if (info->sta && info->enable)
1037 mt76_connac_mcu_sta_tlv(phy, skb, info->sta,
1038 info->vif, info->rcpi,
1041 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1042 sizeof(struct tlv));
1044 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, info->wcid,
1047 if (IS_ERR(wtbl_hdr))
1048 return PTR_ERR(wtbl_hdr);
1051 mt76_connac_mcu_wtbl_generic_tlv(dev, skb, info->vif,
1052 info->sta, sta_wtbl,
1054 mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, info->vif, info->wcid,
1055 sta_wtbl, wtbl_hdr);
1057 mt76_connac_mcu_wtbl_ht_tlv(dev, skb, info->sta,
1062 return mt76_mcu_skb_send_msg(dev, skb, info->cmd, true);
1064 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_cmd);
1066 void mt76_connac_mcu_wtbl_ba_tlv(struct mt76_dev *dev, struct sk_buff *skb,
1067 struct ieee80211_ampdu_params *params,
1068 bool enable, bool tx, void *sta_wtbl,
1074 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_BA, sizeof(*ba),
1075 wtbl_tlv, sta_wtbl);
1077 ba = (struct wtbl_ba *)tlv;
1078 ba->tid = params->tid;
1081 ba->ba_type = MT_BA_TYPE_ORIGINATOR;
1082 ba->sn = enable ? cpu_to_le16(params->ssn) : 0;
1083 ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
1086 memcpy(ba->peer_addr, params->sta->addr, ETH_ALEN);
1087 ba->ba_type = MT_BA_TYPE_RECIPIENT;
1088 ba->rst_ba_tid = params->tid;
1089 ba->rst_ba_sel = RST_BA_MAC_TID_MATCH;
1093 if (!is_connac_v1(dev)) {
1094 ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
1099 static const u8 ba_range[] = { 4, 8, 12, 24, 36, 48, 54, 64 };
1102 for (i = 7; i > 0; i--) {
1103 if (params->buf_size >= ba_range[i])
1106 ba->ba_winsize_idx = i;
1109 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ba_tlv);
1111 int mt76_connac_mcu_uni_add_dev(struct mt76_phy *phy,
1112 struct ieee80211_vif *vif,
1113 struct mt76_wcid *wcid,
1116 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1117 struct mt76_dev *dev = phy->dev;
1129 u8 omac_addr[ETH_ALEN];
1133 .omac_idx = mvif->omac_idx,
1134 .band_idx = mvif->band_idx,
1137 .tag = cpu_to_le16(DEV_INFO_ACTIVE),
1138 .len = cpu_to_le16(sizeof(struct req_tlv)),
1147 struct mt76_connac_bss_basic_tlv basic;
1150 .bss_idx = mvif->idx,
1153 .tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1154 .len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1155 .omac_idx = mvif->omac_idx,
1156 .band_idx = mvif->band_idx,
1157 .wmm_idx = mvif->wmm_idx,
1159 .bmc_tx_wlan_idx = cpu_to_le16(wcid->idx),
1160 .sta_idx = cpu_to_le16(wcid->idx),
1164 int err, idx, cmd, len;
1167 switch (vif->type) {
1168 case NL80211_IFTYPE_MESH_POINT:
1169 case NL80211_IFTYPE_MONITOR:
1170 case NL80211_IFTYPE_AP:
1171 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_AP);
1173 case NL80211_IFTYPE_STATION:
1174 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_STA);
1176 case NL80211_IFTYPE_ADHOC:
1177 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1184 idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1185 basic_req.basic.hw_bss_idx = idx;
1187 memcpy(dev_req.tlv.omac_addr, vif->addr, ETH_ALEN);
1189 cmd = enable ? MCU_UNI_CMD(DEV_INFO_UPDATE) : MCU_UNI_CMD(BSS_INFO_UPDATE);
1190 data = enable ? (void *)&dev_req : (void *)&basic_req;
1191 len = enable ? sizeof(dev_req) : sizeof(basic_req);
1193 err = mt76_mcu_send_msg(dev, cmd, data, len, true);
1197 cmd = enable ? MCU_UNI_CMD(BSS_INFO_UPDATE) : MCU_UNI_CMD(DEV_INFO_UPDATE);
1198 data = enable ? (void *)&basic_req : (void *)&dev_req;
1199 len = enable ? sizeof(basic_req) : sizeof(dev_req);
1201 return mt76_mcu_send_msg(dev, cmd, data, len, true);
1203 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_dev);
1205 void mt76_connac_mcu_sta_ba_tlv(struct sk_buff *skb,
1206 struct ieee80211_ampdu_params *params,
1207 bool enable, bool tx)
1209 struct sta_rec_ba *ba;
1212 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
1214 ba = (struct sta_rec_ba *)tlv;
1215 ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT;
1216 ba->winsize = cpu_to_le16(params->buf_size);
1217 ba->ssn = cpu_to_le16(params->ssn);
1218 ba->ba_en = enable << params->tid;
1219 ba->amsdu = params->amsdu;
1220 ba->tid = params->tid;
1222 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba_tlv);
1224 int mt76_connac_mcu_sta_wed_update(struct mt76_dev *dev, struct sk_buff *skb)
1226 if (!mt76_is_mmio(dev))
1229 if (!mtk_wed_device_active(&dev->mmio.wed))
1232 return mtk_wed_device_update_msg(&dev->mmio.wed, WED_WO_STA_REC,
1233 skb->data, skb->len);
1235 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_wed_update);
1237 int mt76_connac_mcu_sta_ba(struct mt76_dev *dev, struct mt76_vif *mvif,
1238 struct ieee80211_ampdu_params *params,
1239 int cmd, bool enable, bool tx)
1241 struct mt76_wcid *wcid = (struct mt76_wcid *)params->sta->drv_priv;
1242 struct wtbl_req_hdr *wtbl_hdr;
1243 struct tlv *sta_wtbl;
1244 struct sk_buff *skb;
1247 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1249 return PTR_ERR(skb);
1251 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1252 sizeof(struct tlv));
1254 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
1256 if (IS_ERR(wtbl_hdr))
1257 return PTR_ERR(wtbl_hdr);
1259 mt76_connac_mcu_wtbl_ba_tlv(dev, skb, params, enable, tx, sta_wtbl,
1262 ret = mt76_connac_mcu_sta_wed_update(dev, skb);
1266 ret = mt76_mcu_skb_send_msg(dev, skb, cmd, true);
1270 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1272 return PTR_ERR(skb);
1274 mt76_connac_mcu_sta_ba_tlv(skb, params, enable, tx);
1276 ret = mt76_connac_mcu_sta_wed_update(dev, skb);
1280 return mt76_mcu_skb_send_msg(dev, skb, cmd, true);
1282 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba);
1284 u8 mt76_connac_get_phy_mode(struct mt76_phy *phy, struct ieee80211_vif *vif,
1285 enum nl80211_band band, struct ieee80211_sta *sta)
1287 struct mt76_dev *dev = phy->dev;
1288 const struct ieee80211_sta_he_cap *he_cap;
1289 struct ieee80211_sta_vht_cap *vht_cap;
1290 struct ieee80211_sta_ht_cap *ht_cap;
1293 if (is_connac_v1(dev))
1297 ht_cap = &sta->deflink.ht_cap;
1298 vht_cap = &sta->deflink.vht_cap;
1299 he_cap = &sta->deflink.he_cap;
1301 struct ieee80211_supported_band *sband;
1303 sband = phy->hw->wiphy->bands[band];
1304 ht_cap = &sband->ht_cap;
1305 vht_cap = &sband->vht_cap;
1306 he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
1309 if (band == NL80211_BAND_2GHZ) {
1310 mode |= PHY_MODE_B | PHY_MODE_G;
1312 if (ht_cap->ht_supported)
1313 mode |= PHY_MODE_GN;
1315 if (he_cap && he_cap->has_he)
1316 mode |= PHY_MODE_AX_24G;
1317 } else if (band == NL80211_BAND_5GHZ) {
1320 if (ht_cap->ht_supported)
1321 mode |= PHY_MODE_AN;
1323 if (vht_cap->vht_supported)
1324 mode |= PHY_MODE_AC;
1326 if (he_cap && he_cap->has_he)
1327 mode |= PHY_MODE_AX_5G;
1328 } else if (band == NL80211_BAND_6GHZ) {
1329 mode |= PHY_MODE_A | PHY_MODE_AN |
1330 PHY_MODE_AC | PHY_MODE_AX_5G;
1335 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode);
1337 u8 mt76_connac_get_phy_mode_ext(struct mt76_phy *phy, struct ieee80211_vif *vif,
1338 enum nl80211_band band)
1340 const struct ieee80211_sta_eht_cap *eht_cap;
1341 struct ieee80211_supported_band *sband;
1344 if (band == NL80211_BAND_6GHZ)
1345 mode |= PHY_MODE_AX_6G;
1347 sband = phy->hw->wiphy->bands[band];
1348 eht_cap = ieee80211_get_eht_iftype_cap(sband, vif->type);
1350 if (!eht_cap || !eht_cap->has_eht || !vif->bss_conf.eht_support)
1354 case NL80211_BAND_6GHZ:
1355 mode |= PHY_MODE_BE_6G;
1357 case NL80211_BAND_5GHZ:
1358 mode |= PHY_MODE_BE_5G;
1360 case NL80211_BAND_2GHZ:
1361 mode |= PHY_MODE_BE_24G;
1369 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode_ext);
1371 const struct ieee80211_sta_he_cap *
1372 mt76_connac_get_he_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1374 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1375 struct cfg80211_chan_def *chandef = mvif->ctx ?
1376 &mvif->ctx->def : &phy->chandef;
1377 enum nl80211_band band = chandef->chan->band;
1378 struct ieee80211_supported_band *sband;
1380 sband = phy->hw->wiphy->bands[band];
1382 return ieee80211_get_he_iftype_cap(sband, vif->type);
1384 EXPORT_SYMBOL_GPL(mt76_connac_get_he_phy_cap);
1386 const struct ieee80211_sta_eht_cap *
1387 mt76_connac_get_eht_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1389 enum nl80211_band band = phy->chandef.chan->band;
1390 struct ieee80211_supported_band *sband;
1392 sband = phy->hw->wiphy->bands[band];
1394 return ieee80211_get_eht_iftype_cap(sband, vif->type);
1396 EXPORT_SYMBOL_GPL(mt76_connac_get_eht_phy_cap);
1398 #define DEFAULT_HE_PE_DURATION 4
1399 #define DEFAULT_HE_DURATION_RTS_THRES 1023
1401 mt76_connac_mcu_uni_bss_he_tlv(struct mt76_phy *phy, struct ieee80211_vif *vif,
1404 const struct ieee80211_sta_he_cap *cap;
1405 struct bss_info_uni_he *he;
1407 cap = mt76_connac_get_he_phy_cap(phy, vif);
1409 he = (struct bss_info_uni_he *)tlv;
1410 he->he_pe_duration = vif->bss_conf.htc_trig_based_pkt_ext;
1411 if (!he->he_pe_duration)
1412 he->he_pe_duration = DEFAULT_HE_PE_DURATION;
1414 he->he_rts_thres = cpu_to_le16(vif->bss_conf.frame_time_rts_th);
1415 if (!he->he_rts_thres)
1416 he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
1418 he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
1419 he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
1420 he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
1423 int mt76_connac_mcu_uni_set_chctx(struct mt76_phy *phy, struct mt76_vif *mvif,
1424 struct ieee80211_chanctx_conf *ctx)
1426 struct cfg80211_chan_def *chandef = ctx ? &ctx->def : &phy->chandef;
1427 int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
1428 enum nl80211_band band = chandef->chan->band;
1429 struct mt76_dev *mdev = phy->dev;
1450 } __packed rlm_req = {
1452 .bss_idx = mvif->idx,
1455 .tag = cpu_to_le16(UNI_BSS_INFO_RLM),
1456 .len = cpu_to_le16(sizeof(struct rlm_tlv)),
1457 .control_channel = chandef->chan->hw_value,
1458 .center_chan = ieee80211_frequency_to_channel(freq1),
1459 .center_chan2 = ieee80211_frequency_to_channel(freq2),
1460 .tx_streams = hweight8(phy->antenna_mask),
1461 .ht_op_info = 4, /* set HT 40M allowed */
1462 .rx_streams = phy->chainmask,
1468 switch (chandef->width) {
1469 case NL80211_CHAN_WIDTH_40:
1470 rlm_req.rlm.bw = CMD_CBW_40MHZ;
1472 case NL80211_CHAN_WIDTH_80:
1473 rlm_req.rlm.bw = CMD_CBW_80MHZ;
1475 case NL80211_CHAN_WIDTH_80P80:
1476 rlm_req.rlm.bw = CMD_CBW_8080MHZ;
1478 case NL80211_CHAN_WIDTH_160:
1479 rlm_req.rlm.bw = CMD_CBW_160MHZ;
1481 case NL80211_CHAN_WIDTH_5:
1482 rlm_req.rlm.bw = CMD_CBW_5MHZ;
1484 case NL80211_CHAN_WIDTH_10:
1485 rlm_req.rlm.bw = CMD_CBW_10MHZ;
1487 case NL80211_CHAN_WIDTH_20_NOHT:
1488 case NL80211_CHAN_WIDTH_20:
1490 rlm_req.rlm.bw = CMD_CBW_20MHZ;
1491 rlm_req.rlm.ht_op_info = 0;
1495 if (rlm_req.rlm.control_channel < rlm_req.rlm.center_chan)
1496 rlm_req.rlm.sco = 1; /* SCA */
1497 else if (rlm_req.rlm.control_channel > rlm_req.rlm.center_chan)
1498 rlm_req.rlm.sco = 3; /* SCB */
1500 return mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE), &rlm_req,
1501 sizeof(rlm_req), true);
1503 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_set_chctx);
1505 int mt76_connac_mcu_uni_add_bss(struct mt76_phy *phy,
1506 struct ieee80211_vif *vif,
1507 struct mt76_wcid *wcid,
1509 struct ieee80211_chanctx_conf *ctx)
1511 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1512 struct cfg80211_chan_def *chandef = ctx ? &ctx->def : &phy->chandef;
1513 enum nl80211_band band = chandef->chan->band;
1514 struct mt76_dev *mdev = phy->dev;
1520 struct mt76_connac_bss_basic_tlv basic;
1521 struct mt76_connac_bss_qos_tlv qos;
1524 .bss_idx = mvif->idx,
1527 .tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1528 .len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1529 .bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1530 .dtim_period = vif->bss_conf.dtim_period,
1531 .omac_idx = mvif->omac_idx,
1532 .band_idx = mvif->band_idx,
1533 .wmm_idx = mvif->wmm_idx,
1534 .active = true, /* keep bss deactivated */
1535 .phymode = mt76_connac_get_phy_mode(phy, vif, band, NULL),
1538 .tag = cpu_to_le16(UNI_BSS_INFO_QBSS),
1539 .len = cpu_to_le16(sizeof(struct mt76_connac_bss_qos_tlv)),
1540 .qos = vif->bss_conf.qos,
1546 idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1547 basic_req.basic.hw_bss_idx = idx;
1548 if (band == NL80211_BAND_6GHZ)
1549 basic_req.basic.phymode_ext = PHY_MODE_AX_6G;
1551 basic_phy = mt76_connac_get_phy_mode_v2(phy, vif, band, NULL);
1552 basic_req.basic.nonht_basic_phy = cpu_to_le16(basic_phy);
1554 switch (vif->type) {
1555 case NL80211_IFTYPE_MESH_POINT:
1556 case NL80211_IFTYPE_AP:
1558 conn_type = CONNECTION_P2P_GO;
1560 conn_type = CONNECTION_INFRA_AP;
1561 basic_req.basic.conn_type = cpu_to_le32(conn_type);
1562 /* Fully active/deactivate BSS network in AP mode only */
1563 basic_req.basic.active = enable;
1565 case NL80211_IFTYPE_STATION:
1567 conn_type = CONNECTION_P2P_GC;
1569 conn_type = CONNECTION_INFRA_STA;
1570 basic_req.basic.conn_type = cpu_to_le32(conn_type);
1572 case NL80211_IFTYPE_ADHOC:
1573 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1580 memcpy(basic_req.basic.bssid, vif->bss_conf.bssid, ETH_ALEN);
1581 basic_req.basic.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx);
1582 basic_req.basic.sta_idx = cpu_to_le16(wcid->idx);
1583 basic_req.basic.conn_state = !enable;
1585 err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE), &basic_req,
1586 sizeof(basic_req), true);
1590 if (vif->bss_conf.he_support) {
1596 struct bss_info_uni_he he;
1597 struct bss_info_uni_bss_color bss_color;
1600 .bss_idx = mvif->idx,
1603 .tag = cpu_to_le16(UNI_BSS_INFO_HE_BASIC),
1604 .len = cpu_to_le16(sizeof(struct bss_info_uni_he)),
1607 .tag = cpu_to_le16(UNI_BSS_INFO_BSS_COLOR),
1608 .len = cpu_to_le16(sizeof(struct bss_info_uni_bss_color)),
1615 he_req.bss_color.enable =
1616 vif->bss_conf.he_bss_color.enabled;
1617 he_req.bss_color.bss_color =
1618 vif->bss_conf.he_bss_color.color;
1621 mt76_connac_mcu_uni_bss_he_tlv(phy, vif,
1622 (struct tlv *)&he_req.he);
1623 err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE),
1624 &he_req, sizeof(he_req), true);
1629 return mt76_connac_mcu_uni_set_chctx(phy, mvif, ctx);
1631 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_bss);
1633 #define MT76_CONNAC_SCAN_CHANNEL_TIME 60
1634 int mt76_connac_mcu_hw_scan(struct mt76_phy *phy, struct ieee80211_vif *vif,
1635 struct ieee80211_scan_request *scan_req)
1637 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1638 struct cfg80211_scan_request *sreq = &scan_req->req;
1639 int n_ssids = 0, err, i, duration;
1640 int ext_channels_num = max_t(int, sreq->n_channels - 32, 0);
1641 struct ieee80211_channel **scan_list = sreq->channels;
1642 struct mt76_dev *mdev = phy->dev;
1643 struct mt76_connac_mcu_scan_channel *chan;
1644 struct mt76_connac_hw_scan_req *req;
1645 struct sk_buff *skb;
1647 if (test_bit(MT76_HW_SCANNING, &phy->state))
1650 skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req));
1654 set_bit(MT76_HW_SCANNING, &phy->state);
1655 mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1657 req = (struct mt76_connac_hw_scan_req *)skb_put(skb, sizeof(*req));
1659 req->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
1660 req->bss_idx = mvif->idx;
1661 req->scan_type = sreq->n_ssids ? 1 : 0;
1662 req->probe_req_num = sreq->n_ssids ? 2 : 0;
1665 for (i = 0; i < sreq->n_ssids; i++) {
1666 if (!sreq->ssids[i].ssid_len)
1669 req->ssids[i].ssid_len = cpu_to_le32(sreq->ssids[i].ssid_len);
1670 memcpy(req->ssids[i].ssid, sreq->ssids[i].ssid,
1671 sreq->ssids[i].ssid_len);
1674 req->ssid_type = n_ssids ? BIT(2) : BIT(0);
1675 req->ssid_type_ext = n_ssids ? BIT(0) : 0;
1676 req->ssids_num = n_ssids;
1678 duration = is_mt7921(phy->dev) ? 0 : MT76_CONNAC_SCAN_CHANNEL_TIME;
1679 /* increase channel time for passive scan */
1682 req->timeout_value = cpu_to_le16(sreq->n_channels * duration);
1683 req->channel_min_dwell_time = cpu_to_le16(duration);
1684 req->channel_dwell_time = cpu_to_le16(duration);
1686 if (sreq->n_channels == 0 || sreq->n_channels > 64) {
1687 req->channel_type = 0;
1688 req->channels_num = 0;
1689 req->ext_channels_num = 0;
1691 req->channel_type = 4;
1692 req->channels_num = min_t(u8, sreq->n_channels, 32);
1693 req->ext_channels_num = min_t(u8, ext_channels_num, 32);
1696 for (i = 0; i < req->channels_num + req->ext_channels_num; i++) {
1698 chan = &req->ext_channels[i - 32];
1700 chan = &req->channels[i];
1702 switch (scan_list[i]->band) {
1703 case NL80211_BAND_2GHZ:
1706 case NL80211_BAND_6GHZ:
1713 chan->channel_num = scan_list[i]->hw_value;
1716 if (sreq->ie_len > 0) {
1717 memcpy(req->ies, sreq->ie, sreq->ie_len);
1718 req->ies_len = cpu_to_le16(sreq->ie_len);
1721 if (is_mt7921(phy->dev))
1722 req->scan_func |= SCAN_FUNC_SPLIT_SCAN;
1724 memcpy(req->bssid, sreq->bssid, ETH_ALEN);
1725 if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1726 get_random_mask_addr(req->random_mac, sreq->mac_addr,
1727 sreq->mac_addr_mask);
1728 req->scan_func |= SCAN_FUNC_RANDOM_MAC;
1731 err = mt76_mcu_skb_send_msg(mdev, skb, MCU_CE_CMD(START_HW_SCAN),
1734 clear_bit(MT76_HW_SCANNING, &phy->state);
1738 EXPORT_SYMBOL_GPL(mt76_connac_mcu_hw_scan);
1740 int mt76_connac_mcu_cancel_hw_scan(struct mt76_phy *phy,
1741 struct ieee80211_vif *vif)
1743 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1749 .seq_num = mvif->scan_seq_num,
1752 if (test_and_clear_bit(MT76_HW_SCANNING, &phy->state)) {
1753 struct cfg80211_scan_info info = {
1757 ieee80211_scan_completed(phy->hw, &info);
1760 return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(CANCEL_HW_SCAN),
1761 &req, sizeof(req), false);
1763 EXPORT_SYMBOL_GPL(mt76_connac_mcu_cancel_hw_scan);
1765 int mt76_connac_mcu_sched_scan_req(struct mt76_phy *phy,
1766 struct ieee80211_vif *vif,
1767 struct cfg80211_sched_scan_request *sreq)
1769 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1770 struct ieee80211_channel **scan_list = sreq->channels;
1771 struct mt76_connac_mcu_scan_channel *chan;
1772 struct mt76_connac_sched_scan_req *req;
1773 struct mt76_dev *mdev = phy->dev;
1774 struct cfg80211_match_set *match;
1775 struct cfg80211_ssid *ssid;
1776 struct sk_buff *skb;
1779 skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req) + sreq->ie_len);
1783 mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1785 req = (struct mt76_connac_sched_scan_req *)skb_put(skb, sizeof(*req));
1787 req->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
1789 if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1790 u8 *addr = is_mt7663(phy->dev) ? req->mt7663.random_mac
1791 : req->mt7921.random_mac;
1794 get_random_mask_addr(addr, sreq->mac_addr,
1795 sreq->mac_addr_mask);
1797 if (is_mt7921(phy->dev)) {
1798 req->mt7921.bss_idx = mvif->idx;
1799 req->mt7921.delay = cpu_to_le32(sreq->delay);
1802 req->ssids_num = sreq->n_ssids;
1803 for (i = 0; i < req->ssids_num; i++) {
1804 ssid = &sreq->ssids[i];
1805 memcpy(req->ssids[i].ssid, ssid->ssid, ssid->ssid_len);
1806 req->ssids[i].ssid_len = cpu_to_le32(ssid->ssid_len);
1809 req->match_num = sreq->n_match_sets;
1810 for (i = 0; i < req->match_num; i++) {
1811 match = &sreq->match_sets[i];
1812 memcpy(req->match[i].ssid, match->ssid.ssid,
1813 match->ssid.ssid_len);
1814 req->match[i].rssi_th = cpu_to_le32(match->rssi_thold);
1815 req->match[i].ssid_len = match->ssid.ssid_len;
1818 req->channel_type = sreq->n_channels ? 4 : 0;
1819 req->channels_num = min_t(u8, sreq->n_channels, 64);
1820 for (i = 0; i < req->channels_num; i++) {
1821 chan = &req->channels[i];
1823 switch (scan_list[i]->band) {
1824 case NL80211_BAND_2GHZ:
1827 case NL80211_BAND_6GHZ:
1834 chan->channel_num = scan_list[i]->hw_value;
1837 req->intervals_num = sreq->n_scan_plans;
1838 for (i = 0; i < req->intervals_num; i++)
1839 req->intervals[i] = cpu_to_le16(sreq->scan_plans[i].interval);
1841 if (sreq->ie_len > 0) {
1842 req->ie_len = cpu_to_le16(sreq->ie_len);
1843 memcpy(skb_put(skb, sreq->ie_len), sreq->ie, sreq->ie_len);
1846 return mt76_mcu_skb_send_msg(mdev, skb, MCU_CE_CMD(SCHED_SCAN_REQ),
1849 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_req);
1851 int mt76_connac_mcu_sched_scan_enable(struct mt76_phy *phy,
1852 struct ieee80211_vif *vif,
1856 u8 active; /* 0: enabled 1: disabled */
1863 set_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1865 clear_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1867 return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(SCHED_SCAN_ENABLE),
1868 &req, sizeof(req), false);
1870 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_enable);
1872 int mt76_connac_mcu_chip_config(struct mt76_dev *dev)
1874 struct mt76_connac_config req = {
1878 memcpy(req.data, "assert", 7);
1880 return mt76_mcu_send_msg(dev, MCU_CE_CMD(CHIP_CONFIG),
1881 &req, sizeof(req), false);
1883 EXPORT_SYMBOL_GPL(mt76_connac_mcu_chip_config);
1885 int mt76_connac_mcu_set_deep_sleep(struct mt76_dev *dev, bool enable)
1887 struct mt76_connac_config req = {
1891 snprintf(req.data, sizeof(req.data), "KeepFullPwr %d", !enable);
1893 return mt76_mcu_send_msg(dev, MCU_CE_CMD(CHIP_CONFIG),
1894 &req, sizeof(req), false);
1896 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_deep_sleep);
1898 int mt76_connac_sta_state_dp(struct mt76_dev *dev,
1899 enum ieee80211_sta_state old_state,
1900 enum ieee80211_sta_state new_state)
1902 if ((old_state == IEEE80211_STA_ASSOC &&
1903 new_state == IEEE80211_STA_AUTHORIZED) ||
1904 (old_state == IEEE80211_STA_NONE &&
1905 new_state == IEEE80211_STA_NOTEXIST))
1906 mt76_connac_mcu_set_deep_sleep(dev, true);
1908 if ((old_state == IEEE80211_STA_NOTEXIST &&
1909 new_state == IEEE80211_STA_NONE) ||
1910 (old_state == IEEE80211_STA_AUTHORIZED &&
1911 new_state == IEEE80211_STA_ASSOC))
1912 mt76_connac_mcu_set_deep_sleep(dev, false);
1916 EXPORT_SYMBOL_GPL(mt76_connac_sta_state_dp);
1918 void mt76_connac_mcu_coredump_event(struct mt76_dev *dev, struct sk_buff *skb,
1919 struct mt76_connac_coredump *coredump)
1921 spin_lock_bh(&dev->lock);
1922 __skb_queue_tail(&coredump->msg_list, skb);
1923 spin_unlock_bh(&dev->lock);
1925 coredump->last_activity = jiffies;
1927 queue_delayed_work(dev->wq, &coredump->work,
1928 MT76_CONNAC_COREDUMP_TIMEOUT);
1930 EXPORT_SYMBOL_GPL(mt76_connac_mcu_coredump_event);
1932 static void mt76_connac_mcu_parse_tx_resource(struct mt76_dev *dev,
1933 struct sk_buff *skb)
1935 struct mt76_sdio *sdio = &dev->sdio;
1936 struct mt76_connac_tx_resource {
1938 __le32 pse_data_quota;
1939 __le32 pse_mcu_quota;
1940 __le32 ple_data_quota;
1941 __le32 ple_mcu_quota;
1942 __le16 pse_page_size;
1943 __le16 ple_page_size;
1946 } __packed * tx_res;
1948 tx_res = (struct mt76_connac_tx_resource *)skb->data;
1949 sdio->sched.pse_data_quota = le32_to_cpu(tx_res->pse_data_quota);
1950 sdio->sched.pse_mcu_quota = le32_to_cpu(tx_res->pse_mcu_quota);
1951 sdio->sched.ple_data_quota = le32_to_cpu(tx_res->ple_data_quota);
1952 sdio->sched.pse_page_size = le16_to_cpu(tx_res->pse_page_size);
1953 sdio->sched.deficit = tx_res->pp_padding;
1956 static void mt76_connac_mcu_parse_phy_cap(struct mt76_dev *dev,
1957 struct sk_buff *skb)
1959 struct mt76_connac_phy_cap {
1979 cap = (struct mt76_connac_phy_cap *)skb->data;
1981 dev->phy.antenna_mask = BIT(cap->nss) - 1;
1982 dev->phy.chainmask = dev->phy.antenna_mask;
1983 dev->phy.cap.has_2ghz = cap->hw_path & BIT(WF0_24G);
1984 dev->phy.cap.has_5ghz = cap->hw_path & BIT(WF0_5G);
1987 int mt76_connac_mcu_get_nic_capability(struct mt76_phy *phy)
1989 struct mt76_connac_cap_hdr {
1993 struct sk_buff *skb;
1996 ret = mt76_mcu_send_and_get_msg(phy->dev, MCU_CE_CMD(GET_NIC_CAPAB),
1997 NULL, 0, true, &skb);
2001 hdr = (struct mt76_connac_cap_hdr *)skb->data;
2002 if (skb->len < sizeof(*hdr)) {
2007 skb_pull(skb, sizeof(*hdr));
2009 for (i = 0; i < le16_to_cpu(hdr->n_element); i++) {
2013 } __packed * tlv = (struct tlv_hdr *)skb->data;
2016 if (skb->len < sizeof(*tlv))
2019 skb_pull(skb, sizeof(*tlv));
2021 len = le32_to_cpu(tlv->len);
2025 switch (le32_to_cpu(tlv->type)) {
2027 phy->cap.has_6ghz = skb->data[0];
2029 case MT_NIC_CAP_MAC_ADDR:
2030 memcpy(phy->macaddr, (void *)skb->data, ETH_ALEN);
2032 case MT_NIC_CAP_PHY:
2033 mt76_connac_mcu_parse_phy_cap(phy->dev, skb);
2035 case MT_NIC_CAP_TX_RESOURCE:
2036 if (mt76_is_sdio(phy->dev))
2037 mt76_connac_mcu_parse_tx_resource(phy->dev,
2050 EXPORT_SYMBOL_GPL(mt76_connac_mcu_get_nic_capability);
2053 mt76_connac_mcu_build_sku(struct mt76_dev *dev, s8 *sku,
2054 struct mt76_power_limits *limits,
2055 enum nl80211_band band)
2057 int max_power = is_mt7921(dev) ? 127 : 63;
2058 int i, offset = sizeof(limits->cck);
2060 memset(sku, max_power, MT_SKU_POWER_LIMIT);
2062 if (band == NL80211_BAND_2GHZ) {
2064 memcpy(sku, limits->cck, sizeof(limits->cck));
2068 memcpy(&sku[offset], limits->ofdm, sizeof(limits->ofdm));
2069 offset += sizeof(limits->ofdm);
2072 for (i = 0; i < 2; i++) {
2073 memcpy(&sku[offset], limits->mcs[i], 8);
2076 sku[offset++] = limits->mcs[0][0];
2079 for (i = 0; i < ARRAY_SIZE(limits->mcs); i++) {
2080 memcpy(&sku[offset], limits->mcs[i],
2081 ARRAY_SIZE(limits->mcs[i]));
2085 if (!is_mt7921(dev))
2089 for (i = 0; i < ARRAY_SIZE(limits->ru); i++) {
2090 memcpy(&sku[offset], limits->ru[i], ARRAY_SIZE(limits->ru[i]));
2091 offset += ARRAY_SIZE(limits->ru[i]);
2095 static s8 mt76_connac_get_ch_power(struct mt76_phy *phy,
2096 struct ieee80211_channel *chan,
2099 struct mt76_dev *dev = phy->dev;
2100 struct ieee80211_supported_band *sband;
2103 switch (chan->band) {
2104 case NL80211_BAND_2GHZ:
2105 sband = &phy->sband_2g.sband;
2107 case NL80211_BAND_5GHZ:
2108 sband = &phy->sband_5g.sband;
2110 case NL80211_BAND_6GHZ:
2111 sband = &phy->sband_6g.sband;
2114 return target_power;
2117 for (i = 0; i < sband->n_channels; i++) {
2118 struct ieee80211_channel *ch = &sband->channels[i];
2120 if (ch->hw_value == chan->hw_value) {
2121 if (!(ch->flags & IEEE80211_CHAN_DISABLED)) {
2122 int power = 2 * ch->max_reg_power;
2124 if (is_mt7663(dev) && (power > 63 || power < -64))
2126 target_power = min_t(s8, power, target_power);
2132 return target_power;
2136 mt76_connac_mcu_rate_txpower_band(struct mt76_phy *phy,
2137 enum nl80211_band band)
2139 struct mt76_dev *dev = phy->dev;
2140 int sku_len, batch_len = is_mt7921(dev) ? 8 : 16;
2141 static const u8 chan_list_2ghz[] = {
2142 1, 2, 3, 4, 5, 6, 7,
2143 8, 9, 10, 11, 12, 13, 14
2145 static const u8 chan_list_5ghz[] = {
2146 36, 38, 40, 42, 44, 46, 48,
2147 50, 52, 54, 56, 58, 60, 62,
2148 64, 100, 102, 104, 106, 108, 110,
2149 112, 114, 116, 118, 120, 122, 124,
2150 126, 128, 132, 134, 136, 138, 140,
2151 142, 144, 149, 151, 153, 155, 157,
2154 static const u8 chan_list_6ghz[] = {
2155 1, 3, 5, 7, 9, 11, 13,
2156 15, 17, 19, 21, 23, 25, 27,
2157 29, 33, 35, 37, 39, 41, 43,
2158 45, 47, 49, 51, 53, 55, 57,
2159 59, 61, 65, 67, 69, 71, 73,
2160 75, 77, 79, 81, 83, 85, 87,
2161 89, 91, 93, 97, 99, 101, 103,
2162 105, 107, 109, 111, 113, 115, 117,
2163 119, 121, 123, 125, 129, 131, 133,
2164 135, 137, 139, 141, 143, 145, 147,
2165 149, 151, 153, 155, 157, 161, 163,
2166 165, 167, 169, 171, 173, 175, 177,
2167 179, 181, 183, 185, 187, 189, 193,
2168 195, 197, 199, 201, 203, 205, 207,
2169 209, 211, 213, 215, 217, 219, 221,
2172 int i, n_chan, batch_size, idx = 0, tx_power, last_ch;
2173 struct mt76_connac_sku_tlv sku_tlbv;
2174 struct mt76_power_limits limits;
2177 sku_len = is_mt7921(dev) ? sizeof(sku_tlbv) : sizeof(sku_tlbv) - 92;
2178 tx_power = 2 * phy->hw->conf.power_level;
2182 if (band == NL80211_BAND_2GHZ) {
2183 n_chan = ARRAY_SIZE(chan_list_2ghz);
2184 ch_list = chan_list_2ghz;
2185 } else if (band == NL80211_BAND_6GHZ) {
2186 n_chan = ARRAY_SIZE(chan_list_6ghz);
2187 ch_list = chan_list_6ghz;
2189 n_chan = ARRAY_SIZE(chan_list_5ghz);
2190 ch_list = chan_list_5ghz;
2192 batch_size = DIV_ROUND_UP(n_chan, batch_len);
2194 if (phy->cap.has_6ghz)
2195 last_ch = chan_list_6ghz[ARRAY_SIZE(chan_list_6ghz) - 1];
2196 else if (phy->cap.has_5ghz)
2197 last_ch = chan_list_5ghz[ARRAY_SIZE(chan_list_5ghz) - 1];
2199 last_ch = chan_list_2ghz[ARRAY_SIZE(chan_list_2ghz) - 1];
2201 for (i = 0; i < batch_size; i++) {
2202 struct mt76_connac_tx_power_limit_tlv tx_power_tlv = {};
2203 int j, err, msg_len, num_ch;
2204 struct sk_buff *skb;
2206 num_ch = i == batch_size - 1 ? n_chan % batch_len : batch_len;
2207 msg_len = sizeof(tx_power_tlv) + num_ch * sizeof(sku_tlbv);
2208 skb = mt76_mcu_msg_alloc(dev, NULL, msg_len);
2212 skb_reserve(skb, sizeof(tx_power_tlv));
2214 BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(tx_power_tlv.alpha2));
2215 memcpy(tx_power_tlv.alpha2, dev->alpha2, sizeof(dev->alpha2));
2216 tx_power_tlv.n_chan = num_ch;
2219 case NL80211_BAND_2GHZ:
2220 tx_power_tlv.band = 1;
2222 case NL80211_BAND_6GHZ:
2223 tx_power_tlv.band = 3;
2226 tx_power_tlv.band = 2;
2230 for (j = 0; j < num_ch; j++, idx++) {
2231 struct ieee80211_channel chan = {
2232 .hw_value = ch_list[idx],
2235 s8 reg_power, sar_power;
2237 reg_power = mt76_connac_get_ch_power(phy, &chan,
2239 sar_power = mt76_get_sar_power(phy, &chan, reg_power);
2241 mt76_get_rate_power_limits(phy, &chan, &limits,
2244 tx_power_tlv.last_msg = ch_list[idx] == last_ch;
2245 sku_tlbv.channel = ch_list[idx];
2247 mt76_connac_mcu_build_sku(dev, sku_tlbv.pwr_limit,
2249 skb_put_data(skb, &sku_tlbv, sku_len);
2251 __skb_push(skb, sizeof(tx_power_tlv));
2252 memcpy(skb->data, &tx_power_tlv, sizeof(tx_power_tlv));
2254 err = mt76_mcu_skb_send_msg(dev, skb,
2255 MCU_CE_CMD(SET_RATE_TX_POWER),
2264 int mt76_connac_mcu_set_rate_txpower(struct mt76_phy *phy)
2268 if (phy->cap.has_2ghz) {
2269 err = mt76_connac_mcu_rate_txpower_band(phy,
2274 if (phy->cap.has_5ghz) {
2275 err = mt76_connac_mcu_rate_txpower_band(phy,
2280 if (phy->cap.has_6ghz) {
2281 err = mt76_connac_mcu_rate_txpower_band(phy,
2289 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rate_txpower);
2291 int mt76_connac_mcu_update_arp_filter(struct mt76_dev *dev,
2292 struct mt76_vif *vif,
2293 struct ieee80211_bss_conf *info)
2295 struct ieee80211_vif *mvif = container_of(info, struct ieee80211_vif,
2297 struct sk_buff *skb;
2298 int i, len = min_t(int, mvif->cfg.arp_addr_cnt,
2299 IEEE80211_BSS_ARP_ADDR_LIST_LEN);
2305 struct mt76_connac_arpns_tlv arp;
2308 .bss_idx = vif->idx,
2311 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
2312 .len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
2314 .mode = 2, /* update */
2319 skb = mt76_mcu_msg_alloc(dev, NULL,
2320 sizeof(req_hdr) + len * sizeof(__be32));
2324 skb_put_data(skb, &req_hdr, sizeof(req_hdr));
2325 for (i = 0; i < len; i++)
2326 skb_put_data(skb, &mvif->cfg.arp_addr_list[i], sizeof(__be32));
2328 return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(OFFLOAD), true);
2330 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_arp_filter);
2332 int mt76_connac_mcu_set_p2p_oppps(struct ieee80211_hw *hw,
2333 struct ieee80211_vif *vif)
2335 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2336 int ct_window = vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
2337 struct mt76_phy *phy = hw->priv;
2343 .ct_win = cpu_to_le32(ct_window),
2344 .bss_idx = mvif->idx,
2347 return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(SET_P2P_OPPPS),
2348 &req, sizeof(req), false);
2350 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_p2p_oppps);
2354 const struct wiphy_wowlan_support mt76_connac_wowlan_support = {
2355 .flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
2356 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY | WIPHY_WOWLAN_NET_DETECT,
2358 .pattern_min_len = 1,
2359 .pattern_max_len = MT76_CONNAC_WOW_PATTEN_MAX_LEN,
2360 .max_nd_match_sets = 10,
2362 EXPORT_SYMBOL_GPL(mt76_connac_wowlan_support);
2365 mt76_connac_mcu_key_iter(struct ieee80211_hw *hw,
2366 struct ieee80211_vif *vif,
2367 struct ieee80211_sta *sta,
2368 struct ieee80211_key_conf *key,
2371 struct mt76_connac_gtk_rekey_tlv *gtk_tlv = data;
2374 if (key->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
2375 key->cipher != WLAN_CIPHER_SUITE_CCMP &&
2376 key->cipher != WLAN_CIPHER_SUITE_TKIP)
2379 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2384 /* we are assuming here to have a single pairwise key */
2385 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
2386 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2387 gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_1);
2389 gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_2);
2391 gtk_tlv->pairwise_cipher = cpu_to_le32(cipher);
2392 gtk_tlv->keyid = key->keyidx;
2394 gtk_tlv->group_cipher = cpu_to_le32(cipher);
2398 int mt76_connac_mcu_update_gtk_rekey(struct ieee80211_hw *hw,
2399 struct ieee80211_vif *vif,
2400 struct cfg80211_gtk_rekey_data *key)
2402 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2403 struct mt76_connac_gtk_rekey_tlv *gtk_tlv;
2404 struct mt76_phy *phy = hw->priv;
2405 struct sk_buff *skb;
2410 .bss_idx = mvif->idx,
2413 skb = mt76_mcu_msg_alloc(phy->dev, NULL,
2414 sizeof(hdr) + sizeof(*gtk_tlv));
2418 skb_put_data(skb, &hdr, sizeof(hdr));
2419 gtk_tlv = (struct mt76_connac_gtk_rekey_tlv *)skb_put(skb,
2421 gtk_tlv->tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY);
2422 gtk_tlv->len = cpu_to_le16(sizeof(*gtk_tlv));
2423 gtk_tlv->rekey_mode = 2;
2424 gtk_tlv->option = 1;
2427 ieee80211_iter_keys_rcu(hw, vif, mt76_connac_mcu_key_iter, gtk_tlv);
2430 memcpy(gtk_tlv->kek, key->kek, NL80211_KEK_LEN);
2431 memcpy(gtk_tlv->kck, key->kck, NL80211_KCK_LEN);
2432 memcpy(gtk_tlv->replay_ctr, key->replay_ctr, NL80211_REPLAY_CTR_LEN);
2434 return mt76_mcu_skb_send_msg(phy->dev, skb,
2435 MCU_UNI_CMD(OFFLOAD), true);
2437 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_gtk_rekey);
2440 mt76_connac_mcu_set_arp_filter(struct mt76_dev *dev, struct ieee80211_vif *vif,
2443 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2449 struct mt76_connac_arpns_tlv arpns;
2452 .bss_idx = mvif->idx,
2455 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
2456 .len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
2461 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(OFFLOAD), &req,
2466 mt76_connac_mcu_set_gtk_rekey(struct mt76_dev *dev, struct ieee80211_vif *vif,
2469 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2475 struct mt76_connac_gtk_rekey_tlv gtk_tlv;
2478 .bss_idx = mvif->idx,
2481 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY),
2482 .len = cpu_to_le16(sizeof(struct mt76_connac_gtk_rekey_tlv)),
2483 .rekey_mode = !suspend,
2487 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(OFFLOAD), &req,
2492 mt76_connac_mcu_set_suspend_mode(struct mt76_dev *dev,
2493 struct ieee80211_vif *vif,
2494 bool enable, u8 mdtim,
2497 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2503 struct mt76_connac_suspend_tlv suspend_tlv;
2506 .bss_idx = mvif->idx,
2509 .tag = cpu_to_le16(UNI_SUSPEND_MODE_SETTING),
2510 .len = cpu_to_le16(sizeof(struct mt76_connac_suspend_tlv)),
2513 .wow_suspend = wow_suspend,
2517 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(SUSPEND), &req,
2522 mt76_connac_mcu_set_wow_pattern(struct mt76_dev *dev,
2523 struct ieee80211_vif *vif,
2524 u8 index, bool enable,
2525 struct cfg80211_pkt_pattern *pattern)
2527 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2528 struct mt76_connac_wow_pattern_tlv *ptlv;
2529 struct sk_buff *skb;
2534 .bss_idx = mvif->idx,
2537 skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(hdr) + sizeof(*ptlv));
2541 skb_put_data(skb, &hdr, sizeof(hdr));
2542 ptlv = (struct mt76_connac_wow_pattern_tlv *)skb_put(skb, sizeof(*ptlv));
2543 ptlv->tag = cpu_to_le16(UNI_SUSPEND_WOW_PATTERN);
2544 ptlv->len = cpu_to_le16(sizeof(*ptlv));
2545 ptlv->data_len = pattern->pattern_len;
2546 ptlv->enable = enable;
2547 ptlv->index = index;
2549 memcpy(ptlv->pattern, pattern->pattern, pattern->pattern_len);
2550 memcpy(ptlv->mask, pattern->mask, DIV_ROUND_UP(pattern->pattern_len, 8));
2552 return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(SUSPEND), true);
2556 mt76_connac_mcu_set_wow_ctrl(struct mt76_phy *phy, struct ieee80211_vif *vif,
2557 bool suspend, struct cfg80211_wowlan *wowlan)
2559 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2560 struct mt76_dev *dev = phy->dev;
2566 struct mt76_connac_wow_ctrl_tlv wow_ctrl_tlv;
2567 struct mt76_connac_wow_gpio_param_tlv gpio_tlv;
2570 .bss_idx = mvif->idx,
2573 .tag = cpu_to_le16(UNI_SUSPEND_WOW_CTRL),
2574 .len = cpu_to_le16(sizeof(struct mt76_connac_wow_ctrl_tlv)),
2575 .cmd = suspend ? 1 : 2,
2578 .tag = cpu_to_le16(UNI_SUSPEND_WOW_GPIO_PARAM),
2579 .len = cpu_to_le16(sizeof(struct mt76_connac_wow_gpio_param_tlv)),
2580 .gpio_pin = 0xff, /* follow fw about GPIO pin */
2584 if (wowlan->magic_pkt)
2585 req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_MAGIC;
2586 if (wowlan->disconnect)
2587 req.wow_ctrl_tlv.trigger |= (UNI_WOW_DETECT_TYPE_DISCONNECT |
2588 UNI_WOW_DETECT_TYPE_BCN_LOST);
2589 if (wowlan->nd_config) {
2590 mt76_connac_mcu_sched_scan_req(phy, vif, wowlan->nd_config);
2591 req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_SCH_SCAN_HIT;
2592 mt76_connac_mcu_sched_scan_enable(phy, vif, suspend);
2594 if (wowlan->n_patterns)
2595 req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_BITMAP;
2597 if (mt76_is_mmio(dev))
2598 req.wow_ctrl_tlv.wakeup_hif = WOW_PCIE;
2599 else if (mt76_is_usb(dev))
2600 req.wow_ctrl_tlv.wakeup_hif = WOW_USB;
2601 else if (mt76_is_sdio(dev))
2602 req.wow_ctrl_tlv.wakeup_hif = WOW_GPIO;
2604 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(SUSPEND), &req,
2608 int mt76_connac_mcu_set_hif_suspend(struct mt76_dev *dev, bool suspend)
2612 u8 hif_type; /* 0x0: HIF_SDIO
2618 struct hif_suspend_tlv {
2622 } __packed hif_suspend;
2625 .tag = cpu_to_le16(0), /* 0: UNI_HIF_CTRL_BASIC */
2626 .len = cpu_to_le16(sizeof(struct hif_suspend_tlv)),
2631 if (mt76_is_mmio(dev))
2632 req.hdr.hif_type = 2;
2633 else if (mt76_is_usb(dev))
2634 req.hdr.hif_type = 1;
2635 else if (mt76_is_sdio(dev))
2636 req.hdr.hif_type = 0;
2638 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(HIF_CTRL), &req,
2641 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_hif_suspend);
2643 void mt76_connac_mcu_set_suspend_iter(void *priv, u8 *mac,
2644 struct ieee80211_vif *vif)
2646 struct mt76_phy *phy = priv;
2647 bool suspend = !test_bit(MT76_STATE_RUNNING, &phy->state);
2648 struct ieee80211_hw *hw = phy->hw;
2649 struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
2652 mt76_connac_mcu_set_gtk_rekey(phy->dev, vif, suspend);
2653 mt76_connac_mcu_set_arp_filter(phy->dev, vif, suspend);
2655 mt76_connac_mcu_set_suspend_mode(phy->dev, vif, suspend, 1, true);
2657 for (i = 0; i < wowlan->n_patterns; i++)
2658 mt76_connac_mcu_set_wow_pattern(phy->dev, vif, i, suspend,
2659 &wowlan->patterns[i]);
2660 mt76_connac_mcu_set_wow_ctrl(phy, vif, suspend, wowlan);
2662 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_suspend_iter);
2663 #endif /* CONFIG_PM */
2665 u32 mt76_connac_mcu_reg_rr(struct mt76_dev *dev, u32 offset)
2671 .addr = cpu_to_le32(offset),
2674 return mt76_mcu_send_msg(dev, MCU_CE_QUERY(REG_READ), &req,
2677 EXPORT_SYMBOL_GPL(mt76_connac_mcu_reg_rr);
2679 void mt76_connac_mcu_reg_wr(struct mt76_dev *dev, u32 offset, u32 val)
2685 .addr = cpu_to_le32(offset),
2686 .val = cpu_to_le32(val),
2689 mt76_mcu_send_msg(dev, MCU_CE_CMD(REG_WRITE), &req,
2690 sizeof(req), false);
2692 EXPORT_SYMBOL_GPL(mt76_connac_mcu_reg_wr);
2695 mt76_connac_mcu_sta_key_tlv(struct mt76_connac_sta_key_conf *sta_key_conf,
2696 struct sk_buff *skb,
2697 struct ieee80211_key_conf *key,
2698 enum set_key_cmd cmd)
2700 struct sta_rec_sec *sec;
2701 u32 len = sizeof(*sec);
2704 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_KEY_V2, sizeof(*sec));
2705 sec = (struct sta_rec_sec *)tlv;
2708 if (cmd == SET_KEY) {
2709 struct sec_key *sec_key;
2712 cipher = mt76_connac_mcu_get_cipher(key->cipher);
2713 if (cipher == MCU_CIPHER_NONE)
2716 sec_key = &sec->key[0];
2717 sec_key->cipher_len = sizeof(*sec_key);
2719 if (cipher == MCU_CIPHER_BIP_CMAC_128) {
2720 sec_key->cipher_id = MCU_CIPHER_AES_CCMP;
2721 sec_key->key_id = sta_key_conf->keyidx;
2722 sec_key->key_len = 16;
2723 memcpy(sec_key->key, sta_key_conf->key, 16);
2725 sec_key = &sec->key[1];
2726 sec_key->cipher_id = MCU_CIPHER_BIP_CMAC_128;
2727 sec_key->cipher_len = sizeof(*sec_key);
2728 sec_key->key_len = 16;
2729 memcpy(sec_key->key, key->key, 16);
2732 sec_key->cipher_id = cipher;
2733 sec_key->key_id = key->keyidx;
2734 sec_key->key_len = key->keylen;
2735 memcpy(sec_key->key, key->key, key->keylen);
2737 if (cipher == MCU_CIPHER_TKIP) {
2738 /* Rx/Tx MIC keys are swapped */
2739 memcpy(sec_key->key + 16, key->key + 24, 8);
2740 memcpy(sec_key->key + 24, key->key + 16, 8);
2743 /* store key_conf for BIP batch update */
2744 if (cipher == MCU_CIPHER_AES_CCMP) {
2745 memcpy(sta_key_conf->key, key->key, key->keylen);
2746 sta_key_conf->keyidx = key->keyidx;
2749 len -= sizeof(*sec_key);
2753 len -= sizeof(sec->key);
2756 sec->len = cpu_to_le16(len);
2761 int mt76_connac_mcu_add_key(struct mt76_dev *dev, struct ieee80211_vif *vif,
2762 struct mt76_connac_sta_key_conf *sta_key_conf,
2763 struct ieee80211_key_conf *key, int mcu_cmd,
2764 struct mt76_wcid *wcid, enum set_key_cmd cmd)
2766 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2767 struct sk_buff *skb;
2770 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
2772 return PTR_ERR(skb);
2774 ret = mt76_connac_mcu_sta_key_tlv(sta_key_conf, skb, key, cmd);
2778 ret = mt76_connac_mcu_sta_wed_update(dev, skb);
2782 return mt76_mcu_skb_send_msg(dev, skb, mcu_cmd, true);
2784 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_key);
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 *mvif)
2790 struct bss_info_ext_bss *ext;
2791 int ext_bss_idx, tsf_offset;
2794 ext_bss_idx = mvif->omac_idx - EXT_BSSID_START;
2795 if (ext_bss_idx < 0)
2798 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_EXT_BSS, sizeof(*ext));
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);
2804 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_ext_tlv);
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,
2812 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2813 u32 type = vif->p2p ? NETWORK_P2P : NETWORK_INFRA;
2814 struct bss_info_basic *bss;
2817 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_BASIC, sizeof(*bss));
2818 bss = (struct bss_info_basic *)tlv;
2820 switch (vif->type) {
2821 case NL80211_IFTYPE_MESH_POINT:
2822 case NL80211_IFTYPE_MONITOR:
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;
2829 if (bssid_id > ilog2(wiphy->mbssid_max_interfaces))
2832 bss->non_tx_bssid = vif->bss_conf.bssid_index;
2833 bss->max_bssid = bssid_id;
2836 case NL80211_IFTYPE_STATION:
2840 sta = ieee80211_find_sta(vif,
2841 vif->bss_conf.bssid);
2842 /* TODO: enable BSS_INFO_UAPSD & BSS_INFO_PM */
2844 struct mt76_wcid *wcid;
2846 wcid = (struct mt76_wcid *)sta->drv_priv;
2847 wlan_idx = wcid->idx;
2852 case NL80211_IFTYPE_ADHOC:
2853 type = NETWORK_IBSS;
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;
2867 if (vif->type != NL80211_IFTYPE_MONITOR) {
2868 struct cfg80211_chan_def *chandef = &phy->chandef;
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);
2876 memcpy(bss->bssid, phy->macaddr, ETH_ALEN);
2881 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_basic_tlv);
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)
2893 __le16 bcn_interval;
2906 .pm_state = enter ? ENTER_PM_STATE : EXIT_PM_STATE,
2910 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(PM_STATE_CTRL), &req,
2913 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_pm);
2915 int mt76_connac_mcu_restart(struct mt76_dev *dev)
2924 return mt76_mcu_send_msg(dev, MCU_CMD(NIC_POWER_CTRL), &req,
2925 sizeof(req), false);
2927 EXPORT_SYMBOL_GPL(mt76_connac_mcu_restart);
2929 int mt76_connac_mcu_rdd_cmd(struct mt76_dev *dev, int cmd, u8 index,
2941 .rdd_rx_sel = rx_sel,
2945 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(SET_RDD_CTRL), &req,
2948 EXPORT_SYMBOL_GPL(mt76_connac_mcu_rdd_cmd);
2951 mt76_connac_mcu_send_ram_firmware(struct mt76_dev *dev,
2952 const struct mt76_connac2_fw_trailer *hdr,
2953 const u8 *data, bool is_wa)
2955 int i, offset = 0, max_len = mt76_is_sdio(dev) ? 2048 : 4096;
2956 u32 override = 0, option = 0;
2958 for (i = 0; i < hdr->n_region; i++) {
2959 const struct mt76_connac2_fw_region *region;
2960 u32 len, addr, mode;
2963 region = (const void *)((const u8 *)hdr -
2964 (hdr->n_region - i) * sizeof(*region));
2965 mode = mt76_connac_mcu_gen_dl_mode(dev, region->feature_set,
2967 len = le32_to_cpu(region->len);
2968 addr = le32_to_cpu(region->addr);
2970 if (region->feature_set & FW_FEATURE_NON_DL)
2973 if (region->feature_set & FW_FEATURE_OVERRIDE_ADDR)
2976 err = mt76_connac_mcu_init_download(dev, addr, len, mode);
2978 dev_err(dev->dev, "Download request failed\n");
2982 err = __mt76_mcu_send_firmware(dev, MCU_CMD(FW_SCATTER),
2983 data + offset, len, max_len);
2985 dev_err(dev->dev, "Failed to send firmware.\n");
2994 option |= FW_START_OVERRIDE;
2996 option |= FW_START_WORKING_PDA_CR4;
2998 return mt76_connac_mcu_start_firmware(dev, override, option);
3001 int mt76_connac2_load_ram(struct mt76_dev *dev, const char *fw_wm,
3004 const struct mt76_connac2_fw_trailer *hdr;
3005 const struct firmware *fw;
3008 ret = request_firmware(&fw, fw_wm, dev->dev);
3012 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
3013 dev_err(dev->dev, "Invalid firmware\n");
3018 hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
3019 dev_info(dev->dev, "WM Firmware Version: %.10s, Build Time: %.15s\n",
3020 hdr->fw_ver, hdr->build_date);
3022 ret = mt76_connac_mcu_send_ram_firmware(dev, hdr, fw->data, false);
3024 dev_err(dev->dev, "Failed to start WM firmware\n");
3028 snprintf(dev->hw->wiphy->fw_version,
3029 sizeof(dev->hw->wiphy->fw_version),
3030 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
3032 release_firmware(fw);
3037 ret = request_firmware(&fw, fw_wa, dev->dev);
3041 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
3042 dev_err(dev->dev, "Invalid firmware\n");
3047 hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
3048 dev_info(dev->dev, "WA Firmware Version: %.10s, Build Time: %.15s\n",
3049 hdr->fw_ver, hdr->build_date);
3051 ret = mt76_connac_mcu_send_ram_firmware(dev, hdr, fw->data, true);
3053 dev_err(dev->dev, "Failed to start WA firmware\n");
3057 snprintf(dev->hw->wiphy->fw_version,
3058 sizeof(dev->hw->wiphy->fw_version),
3059 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
3062 release_firmware(fw);
3066 EXPORT_SYMBOL_GPL(mt76_connac2_load_ram);
3068 static u32 mt76_connac2_get_data_mode(struct mt76_dev *dev, u32 info)
3070 u32 mode = DL_MODE_NEED_RSP;
3072 if (!is_mt7921(dev) || info == PATCH_SEC_NOT_SUPPORT)
3075 switch (FIELD_GET(PATCH_SEC_ENC_TYPE_MASK, info)) {
3076 case PATCH_SEC_ENC_TYPE_PLAIN:
3078 case PATCH_SEC_ENC_TYPE_AES:
3079 mode |= DL_MODE_ENCRYPT;
3080 mode |= FIELD_PREP(DL_MODE_KEY_IDX,
3081 (info & PATCH_SEC_ENC_AES_KEY_MASK)) & DL_MODE_KEY_IDX;
3082 mode |= DL_MODE_RESET_SEC_IV;
3084 case PATCH_SEC_ENC_TYPE_SCRAMBLE:
3085 mode |= DL_MODE_ENCRYPT;
3086 mode |= DL_CONFIG_ENCRY_MODE_SEL;
3087 mode |= DL_MODE_RESET_SEC_IV;
3090 dev_err(dev->dev, "Encryption type not support!\n");
3096 int mt76_connac2_load_patch(struct mt76_dev *dev, const char *fw_name)
3098 int i, ret, sem, max_len = mt76_is_sdio(dev) ? 2048 : 4096;
3099 const struct mt76_connac2_patch_hdr *hdr;
3100 const struct firmware *fw = NULL;
3102 sem = mt76_connac_mcu_patch_sem_ctrl(dev, true);
3106 case PATCH_NOT_DL_SEM_SUCCESS:
3109 dev_err(dev->dev, "Failed to get patch semaphore\n");
3113 ret = request_firmware(&fw, fw_name, dev->dev);
3117 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
3118 dev_err(dev->dev, "Invalid firmware\n");
3123 hdr = (const void *)fw->data;
3124 dev_info(dev->dev, "HW/SW Version: 0x%x, Build Time: %.16s\n",
3125 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date);
3127 for (i = 0; i < be32_to_cpu(hdr->desc.n_region); i++) {
3128 struct mt76_connac2_patch_sec *sec;
3129 u32 len, addr, mode;
3133 sec = (void *)(fw->data + sizeof(*hdr) + i * sizeof(*sec));
3134 if ((be32_to_cpu(sec->type) & PATCH_SEC_TYPE_MASK) !=
3135 PATCH_SEC_TYPE_INFO) {
3140 addr = be32_to_cpu(sec->info.addr);
3141 len = be32_to_cpu(sec->info.len);
3142 dl = fw->data + be32_to_cpu(sec->offs);
3143 sec_info = be32_to_cpu(sec->info.sec_key_idx);
3144 mode = mt76_connac2_get_data_mode(dev, sec_info);
3146 ret = mt76_connac_mcu_init_download(dev, addr, len, mode);
3148 dev_err(dev->dev, "Download request failed\n");
3152 ret = __mt76_mcu_send_firmware(dev, MCU_CMD(FW_SCATTER),
3155 dev_err(dev->dev, "Failed to send patch\n");
3160 ret = mt76_connac_mcu_start_patch(dev);
3162 dev_err(dev->dev, "Failed to start patch\n");
3165 sem = mt76_connac_mcu_patch_sem_ctrl(dev, false);
3167 case PATCH_REL_SEM_SUCCESS:
3171 dev_err(dev->dev, "Failed to release patch semaphore\n");
3175 release_firmware(fw);
3179 EXPORT_SYMBOL_GPL(mt76_connac2_load_patch);
3181 int mt76_connac2_mcu_fill_message(struct mt76_dev *dev, struct sk_buff *skb,
3182 int cmd, int *wait_seq)
3184 int txd_len, mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
3185 struct mt76_connac2_mcu_uni_txd *uni_txd;
3186 struct mt76_connac2_mcu_txd *mcu_txd;
3191 /* TODO: make dynamic based on msg type */
3192 dev->mcu.timeout = 20 * HZ;
3194 seq = ++dev->mcu.msg_seq & 0xf;
3196 seq = ++dev->mcu.msg_seq & 0xf;
3198 if (cmd == MCU_CMD(FW_SCATTER))
3201 txd_len = cmd & __MCU_CMD_FIELD_UNI ? sizeof(*uni_txd) : sizeof(*mcu_txd);
3202 txd = (__le32 *)skb_push(skb, txd_len);
3204 val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) |
3205 FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CMD) |
3206 FIELD_PREP(MT_TXD0_Q_IDX, MT_TX_MCU_PORT_RX_Q0);
3207 txd[0] = cpu_to_le32(val);
3209 val = MT_TXD1_LONG_FORMAT |
3210 FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD);
3211 txd[1] = cpu_to_le32(val);
3213 if (cmd & __MCU_CMD_FIELD_UNI) {
3214 uni_txd = (struct mt76_connac2_mcu_uni_txd *)txd;
3215 uni_txd->len = cpu_to_le16(skb->len - sizeof(uni_txd->txd));
3216 uni_txd->option = MCU_CMD_UNI_EXT_ACK;
3217 uni_txd->cid = cpu_to_le16(mcu_cmd);
3218 uni_txd->s2d_index = MCU_S2D_H2N;
3219 uni_txd->pkt_type = MCU_PKT_ID;
3225 mcu_txd = (struct mt76_connac2_mcu_txd *)txd;
3226 mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd));
3227 mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU,
3228 MT_TX_MCU_PORT_RX_Q0));
3229 mcu_txd->pkt_type = MCU_PKT_ID;
3231 mcu_txd->cid = mcu_cmd;
3232 mcu_txd->ext_cid = FIELD_GET(__MCU_CMD_FIELD_EXT_ID, cmd);
3234 if (mcu_txd->ext_cid || (cmd & __MCU_CMD_FIELD_CE)) {
3235 if (cmd & __MCU_CMD_FIELD_QUERY)
3236 mcu_txd->set_query = MCU_Q_QUERY;
3238 mcu_txd->set_query = MCU_Q_SET;
3239 mcu_txd->ext_cid_ack = !!mcu_txd->ext_cid;
3241 mcu_txd->set_query = MCU_Q_NA;
3244 if (cmd & __MCU_CMD_FIELD_WA)
3245 mcu_txd->s2d_index = MCU_S2D_H2C;
3247 mcu_txd->s2d_index = MCU_S2D_H2N;
3255 EXPORT_SYMBOL_GPL(mt76_connac2_mcu_fill_message);
3257 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
3258 MODULE_LICENSE("Dual BSD/GPL");