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_mt7925(dev) && addr == 0x900000) ||
70 (is_mt7996(dev) && addr == 0x900000) ||
71 (is_mt7992(dev) && addr == 0x900000))
72 cmd = MCU_CMD(PATCH_START_REQ);
74 cmd = MCU_CMD(TARGET_ADDRESS_LEN_REQ);
76 return mt76_mcu_send_msg(dev, cmd, &req, sizeof(req), true);
78 EXPORT_SYMBOL_GPL(mt76_connac_mcu_init_download);
80 int mt76_connac_mcu_set_channel_domain(struct mt76_phy *phy)
82 int len, i, n_max_channels, n_2ch = 0, n_5ch = 0, n_6ch = 0;
83 struct mt76_connac_mcu_channel_domain {
84 u8 alpha2[4]; /* regulatory_request.alpha2 */
85 u8 bw_2g; /* BW_20_40M 0
100 .bw_5g = 3, /* BW_20_40_80_160M */
103 struct mt76_connac_mcu_chan {
108 struct mt76_dev *dev = phy->dev;
109 struct ieee80211_channel *chan;
112 n_max_channels = phy->sband_2g.sband.n_channels +
113 phy->sband_5g.sband.n_channels +
114 phy->sband_6g.sband.n_channels;
115 len = sizeof(hdr) + n_max_channels * sizeof(channel);
117 skb = mt76_mcu_msg_alloc(dev, NULL, len);
121 skb_reserve(skb, sizeof(hdr));
123 for (i = 0; i < phy->sband_2g.sband.n_channels; i++) {
124 chan = &phy->sband_2g.sband.channels[i];
125 if (chan->flags & IEEE80211_CHAN_DISABLED)
128 channel.hw_value = cpu_to_le16(chan->hw_value);
129 channel.flags = cpu_to_le32(chan->flags);
132 skb_put_data(skb, &channel, sizeof(channel));
135 for (i = 0; i < phy->sband_5g.sband.n_channels; i++) {
136 chan = &phy->sband_5g.sband.channels[i];
137 if (chan->flags & IEEE80211_CHAN_DISABLED)
140 channel.hw_value = cpu_to_le16(chan->hw_value);
141 channel.flags = cpu_to_le32(chan->flags);
144 skb_put_data(skb, &channel, sizeof(channel));
147 for (i = 0; i < phy->sband_6g.sband.n_channels; i++) {
148 chan = &phy->sband_6g.sband.channels[i];
149 if (chan->flags & IEEE80211_CHAN_DISABLED)
152 channel.hw_value = cpu_to_le16(chan->hw_value);
153 channel.flags = cpu_to_le32(chan->flags);
156 skb_put_data(skb, &channel, sizeof(channel));
160 BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(hdr.alpha2));
161 memcpy(hdr.alpha2, dev->alpha2, sizeof(dev->alpha2));
166 memcpy(__skb_push(skb, sizeof(hdr)), &hdr, sizeof(hdr));
168 return mt76_mcu_skb_send_msg(dev, skb, MCU_CE_CMD(SET_CHAN_DOMAIN),
171 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_channel_domain);
173 int mt76_connac_mcu_set_mac_enable(struct mt76_dev *dev, int band, bool enable,
180 } __packed req_mac = {
185 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(MAC_INIT_CTRL), &req_mac,
186 sizeof(req_mac), true);
188 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_mac_enable);
190 int mt76_connac_mcu_set_vif_ps(struct mt76_dev *dev, struct ieee80211_vif *vif)
192 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
195 u8 ps_state; /* 0: device awake
196 * 1: static power save
197 * 2: dynamic power saving
200 .bss_idx = mvif->idx,
201 .ps_state = vif->cfg.ps ? 2 : 0,
204 if (vif->type != NL80211_IFTYPE_STATION)
207 return mt76_mcu_send_msg(dev, MCU_CE_CMD(SET_PS_PROFILE),
208 &req, sizeof(req), false);
210 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_vif_ps);
212 int mt76_connac_mcu_set_rts_thresh(struct mt76_dev *dev, u32 val, u8 band)
223 .len_thresh = cpu_to_le32(val),
224 .pkt_thresh = cpu_to_le32(0x2),
227 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(PROTECT_CTRL), &req,
230 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rts_thresh);
232 void mt76_connac_mcu_beacon_loss_iter(void *priv, u8 *mac,
233 struct ieee80211_vif *vif)
235 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
236 struct mt76_connac_beacon_loss_event *event = priv;
238 if (mvif->idx != event->bss_idx)
241 if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
244 ieee80211_beacon_loss(vif);
246 EXPORT_SYMBOL_GPL(mt76_connac_mcu_beacon_loss_iter);
249 mt76_connac_mcu_add_nested_tlv(struct sk_buff *skb, int tag, int len,
250 void *sta_ntlv, void *sta_wtbl)
252 struct sta_ntlv_hdr *ntlv_hdr = sta_ntlv;
253 struct tlv *sta_hdr = sta_wtbl;
254 struct tlv *ptlv, tlv = {
255 .tag = cpu_to_le16(tag),
256 .len = cpu_to_le16(len),
260 ptlv = skb_put(skb, len);
261 memcpy(ptlv, &tlv, sizeof(tlv));
263 ntlv = le16_to_cpu(ntlv_hdr->tlv_num);
264 ntlv_hdr->tlv_num = cpu_to_le16(ntlv + 1);
267 len += le16_to_cpu(sta_hdr->len);
268 sta_hdr->len = cpu_to_le16(len);
273 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_nested_tlv);
276 __mt76_connac_mcu_alloc_sta_req(struct mt76_dev *dev, struct mt76_vif *mvif,
277 struct mt76_wcid *wcid, int len)
279 struct sta_req_hdr hdr = {
280 .bss_idx = mvif->idx,
281 .muar_idx = wcid ? mvif->omac_idx : 0,
286 mt76_connac_mcu_get_wlan_idx(dev, wcid, &hdr.wlan_idx_lo,
288 skb = mt76_mcu_msg_alloc(dev, NULL, len);
290 return ERR_PTR(-ENOMEM);
292 skb_put_data(skb, &hdr, sizeof(hdr));
296 EXPORT_SYMBOL_GPL(__mt76_connac_mcu_alloc_sta_req);
298 struct wtbl_req_hdr *
299 mt76_connac_mcu_alloc_wtbl_req(struct mt76_dev *dev, struct mt76_wcid *wcid,
300 int cmd, void *sta_wtbl, struct sk_buff **skb)
302 struct tlv *sta_hdr = sta_wtbl;
303 struct wtbl_req_hdr hdr = {
306 struct sk_buff *nskb = *skb;
308 mt76_connac_mcu_get_wlan_idx(dev, wcid, &hdr.wlan_idx_lo,
311 nskb = mt76_mcu_msg_alloc(dev, NULL,
312 MT76_CONNAC_WTBL_UPDATE_MAX_SIZE);
314 return ERR_PTR(-ENOMEM);
320 le16_add_cpu(&sta_hdr->len, sizeof(hdr));
322 return skb_put_data(nskb, &hdr, sizeof(hdr));
324 EXPORT_SYMBOL_GPL(mt76_connac_mcu_alloc_wtbl_req);
326 void mt76_connac_mcu_bss_omac_tlv(struct sk_buff *skb,
327 struct ieee80211_vif *vif)
329 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
330 u8 omac_idx = mvif->omac_idx;
331 struct bss_info_omac *omac;
336 case NL80211_IFTYPE_MONITOR:
337 case NL80211_IFTYPE_MESH_POINT:
338 case NL80211_IFTYPE_AP:
340 type = CONNECTION_P2P_GO;
342 type = CONNECTION_INFRA_AP;
344 case NL80211_IFTYPE_STATION:
346 type = CONNECTION_P2P_GC;
348 type = CONNECTION_INFRA_STA;
350 case NL80211_IFTYPE_ADHOC:
351 type = CONNECTION_IBSS_ADHOC;
358 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_OMAC, sizeof(*omac));
360 omac = (struct bss_info_omac *)tlv;
361 omac->conn_type = cpu_to_le32(type);
362 omac->omac_idx = mvif->omac_idx;
363 omac->band_idx = mvif->band_idx;
364 omac->hw_bss_idx = omac_idx > EXT_BSSID_START ? HW_BSSID_0 : omac_idx;
366 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_omac_tlv);
368 void mt76_connac_mcu_sta_basic_tlv(struct mt76_dev *dev, struct sk_buff *skb,
369 struct ieee80211_vif *vif,
370 struct ieee80211_sta *sta,
371 bool enable, bool newly)
373 struct sta_rec_basic *basic;
377 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BASIC, sizeof(*basic));
379 basic = (struct sta_rec_basic *)tlv;
380 basic->extra_info = cpu_to_le16(EXTRA_INFO_VER);
384 basic->extra_info |= cpu_to_le16(EXTRA_INFO_NEW);
385 basic->conn_state = CONN_STATE_PORT_SECURE;
387 basic->conn_state = CONN_STATE_DISCONNECT;
391 basic->conn_type = cpu_to_le32(CONNECTION_INFRA_BC);
392 eth_broadcast_addr(basic->peer_addr);
397 case NL80211_IFTYPE_MESH_POINT:
398 case NL80211_IFTYPE_AP:
399 if (vif->p2p && !is_mt7921(dev))
400 conn_type = CONNECTION_P2P_GC;
402 conn_type = CONNECTION_INFRA_STA;
403 basic->conn_type = cpu_to_le32(conn_type);
404 basic->aid = cpu_to_le16(sta->aid);
406 case NL80211_IFTYPE_STATION:
407 if (vif->p2p && !is_mt7921(dev))
408 conn_type = CONNECTION_P2P_GO;
410 conn_type = CONNECTION_INFRA_AP;
411 basic->conn_type = cpu_to_le32(conn_type);
412 basic->aid = cpu_to_le16(vif->cfg.aid);
414 case NL80211_IFTYPE_ADHOC:
415 basic->conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
416 basic->aid = cpu_to_le16(sta->aid);
423 memcpy(basic->peer_addr, sta->addr, ETH_ALEN);
424 basic->qos = sta->wme;
426 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_basic_tlv);
428 void mt76_connac_mcu_sta_uapsd(struct sk_buff *skb, struct ieee80211_vif *vif,
429 struct ieee80211_sta *sta)
431 struct sta_rec_uapsd *uapsd;
434 if (vif->type != NL80211_IFTYPE_AP || !sta->wme)
437 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_APPS, sizeof(*uapsd));
438 uapsd = (struct sta_rec_uapsd *)tlv;
440 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) {
441 uapsd->dac_map |= BIT(3);
442 uapsd->tac_map |= BIT(3);
444 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI) {
445 uapsd->dac_map |= BIT(2);
446 uapsd->tac_map |= BIT(2);
448 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE) {
449 uapsd->dac_map |= BIT(1);
450 uapsd->tac_map |= BIT(1);
452 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK) {
453 uapsd->dac_map |= BIT(0);
454 uapsd->tac_map |= BIT(0);
456 uapsd->max_sp = sta->max_sp;
458 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_uapsd);
460 void mt76_connac_mcu_wtbl_hdr_trans_tlv(struct sk_buff *skb,
461 struct ieee80211_vif *vif,
462 struct mt76_wcid *wcid,
463 void *sta_wtbl, void *wtbl_tlv)
465 struct wtbl_hdr_trans *htr;
468 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HDR_TRANS,
471 htr = (struct wtbl_hdr_trans *)tlv;
472 htr->no_rx_trans = true;
474 if (vif->type == NL80211_IFTYPE_STATION)
482 htr->no_rx_trans = !test_bit(MT_WCID_FLAG_HDR_TRANS, &wcid->flags);
483 if (test_bit(MT_WCID_FLAG_4ADDR, &wcid->flags)) {
488 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_hdr_trans_tlv);
490 int mt76_connac_mcu_sta_update_hdr_trans(struct mt76_dev *dev,
491 struct ieee80211_vif *vif,
492 struct mt76_wcid *wcid, int cmd)
494 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
495 struct wtbl_req_hdr *wtbl_hdr;
496 struct tlv *sta_wtbl;
499 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
503 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
506 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
508 if (IS_ERR(wtbl_hdr))
509 return PTR_ERR(wtbl_hdr);
511 mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, vif, wcid, sta_wtbl, wtbl_hdr);
513 return mt76_mcu_skb_send_msg(dev, skb, cmd, true);
515 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_update_hdr_trans);
517 int mt76_connac_mcu_wtbl_update_hdr_trans(struct mt76_dev *dev,
518 struct ieee80211_vif *vif,
519 struct ieee80211_sta *sta)
521 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
522 struct wtbl_req_hdr *wtbl_hdr;
523 struct sk_buff *skb = NULL;
525 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET, NULL,
527 if (IS_ERR(wtbl_hdr))
528 return PTR_ERR(wtbl_hdr);
530 mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, vif, wcid, NULL, wtbl_hdr);
532 return mt76_mcu_skb_send_msg(dev, skb, MCU_EXT_CMD(WTBL_UPDATE), true);
534 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_update_hdr_trans);
536 void mt76_connac_mcu_wtbl_generic_tlv(struct mt76_dev *dev,
538 struct ieee80211_vif *vif,
539 struct ieee80211_sta *sta,
540 void *sta_wtbl, void *wtbl_tlv)
542 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
543 struct wtbl_generic *generic;
545 struct wtbl_spe *spe;
548 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_GENERIC,
552 generic = (struct wtbl_generic *)tlv;
555 if (vif->type == NL80211_IFTYPE_STATION)
556 generic->partial_aid = cpu_to_le16(vif->cfg.aid);
558 generic->partial_aid = cpu_to_le16(sta->aid);
559 memcpy(generic->peer_addr, sta->addr, ETH_ALEN);
560 generic->muar_idx = mvif->omac_idx;
561 generic->qos = sta->wme;
563 if (!is_connac_v1(dev) && vif->type == NL80211_IFTYPE_STATION)
564 memcpy(generic->peer_addr, vif->bss_conf.bssid,
567 eth_broadcast_addr(generic->peer_addr);
569 generic->muar_idx = 0xe;
572 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RX, sizeof(*rx),
575 rx = (struct wtbl_rx *)tlv;
576 rx->rca1 = sta ? vif->type != NL80211_IFTYPE_AP : 1;
580 if (!is_connac_v1(dev))
583 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SPE, sizeof(*spe),
585 spe = (struct wtbl_spe *)tlv;
588 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_generic_tlv);
591 mt76_connac_mcu_sta_amsdu_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
592 struct ieee80211_vif *vif)
594 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
595 struct sta_rec_amsdu *amsdu;
598 if (vif->type != NL80211_IFTYPE_AP &&
599 vif->type != NL80211_IFTYPE_STATION)
602 if (!sta->deflink.agg.max_amsdu_len)
605 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HW_AMSDU, sizeof(*amsdu));
606 amsdu = (struct sta_rec_amsdu *)tlv;
607 amsdu->max_amsdu_num = 8;
608 amsdu->amsdu_en = true;
609 amsdu->max_mpdu_size = sta->deflink.agg.max_amsdu_len >=
610 IEEE80211_MAX_MPDU_LEN_VHT_7991;
615 #define HE_PHY(p, c) u8_get_bits(c, IEEE80211_HE_PHY_##p)
616 #define HE_MAC(m, c) u8_get_bits(c, IEEE80211_HE_MAC_##m)
618 mt76_connac_mcu_sta_he_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
620 struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
621 struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
622 struct sta_rec_he *he;
626 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE, sizeof(*he));
628 he = (struct sta_rec_he *)tlv;
630 if (elem->mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
631 cap |= STA_REC_HE_CAP_HTC;
633 if (elem->mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
634 cap |= STA_REC_HE_CAP_BSR;
636 if (elem->mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
637 cap |= STA_REC_HE_CAP_OM;
639 if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU)
640 cap |= STA_REC_HE_CAP_AMSDU_IN_AMPDU;
642 if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
643 cap |= STA_REC_HE_CAP_BQR;
645 if (elem->phy_cap_info[0] &
646 (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G |
647 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G))
648 cap |= STA_REC_HE_CAP_BW20_RU242_SUPPORT;
650 if (elem->phy_cap_info[1] &
651 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
652 cap |= STA_REC_HE_CAP_LDPC;
654 if (elem->phy_cap_info[1] &
655 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US)
656 cap |= STA_REC_HE_CAP_SU_PPDU_1LTF_8US_GI;
658 if (elem->phy_cap_info[2] &
659 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US)
660 cap |= STA_REC_HE_CAP_NDP_4LTF_3DOT2MS_GI;
662 if (elem->phy_cap_info[2] &
663 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ)
664 cap |= STA_REC_HE_CAP_LE_EQ_80M_TX_STBC;
666 if (elem->phy_cap_info[2] &
667 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
668 cap |= STA_REC_HE_CAP_LE_EQ_80M_RX_STBC;
670 if (elem->phy_cap_info[6] &
671 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE)
672 cap |= STA_REC_HE_CAP_PARTIAL_BW_EXT_RANGE;
674 if (elem->phy_cap_info[7] &
675 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI)
676 cap |= STA_REC_HE_CAP_SU_MU_PPDU_4LTF_8US_GI;
678 if (elem->phy_cap_info[7] &
679 IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ)
680 cap |= STA_REC_HE_CAP_GT_80M_TX_STBC;
682 if (elem->phy_cap_info[7] &
683 IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ)
684 cap |= STA_REC_HE_CAP_GT_80M_RX_STBC;
686 if (elem->phy_cap_info[8] &
687 IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI)
688 cap |= STA_REC_HE_CAP_ER_SU_PPDU_4LTF_8US_GI;
690 if (elem->phy_cap_info[8] &
691 IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI)
692 cap |= STA_REC_HE_CAP_ER_SU_PPDU_1LTF_8US_GI;
694 if (elem->phy_cap_info[9] &
695 IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK)
696 cap |= STA_REC_HE_CAP_TRIG_CQI_FK;
698 if (elem->phy_cap_info[9] &
699 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU)
700 cap |= STA_REC_HE_CAP_TX_1024QAM_UNDER_RU242;
702 if (elem->phy_cap_info[9] &
703 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU)
704 cap |= STA_REC_HE_CAP_RX_1024QAM_UNDER_RU242;
706 he->he_cap = cpu_to_le32(cap);
708 switch (sta->deflink.bandwidth) {
709 case IEEE80211_STA_RX_BW_160:
710 if (elem->phy_cap_info[0] &
711 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
712 he->max_nss_mcs[CMD_HE_MCS_BW8080] =
713 he_cap->he_mcs_nss_supp.rx_mcs_80p80;
715 he->max_nss_mcs[CMD_HE_MCS_BW160] =
716 he_cap->he_mcs_nss_supp.rx_mcs_160;
719 he->max_nss_mcs[CMD_HE_MCS_BW80] =
720 he_cap->he_mcs_nss_supp.rx_mcs_80;
725 HE_MAC(CAP1_TF_MAC_PAD_DUR_MASK, elem->mac_cap_info[1]);
727 HE_MAC(CAP3_MAX_AMPDU_LEN_EXP_MASK, elem->mac_cap_info[3]);
730 HE_PHY(CAP0_CHANNEL_WIDTH_SET_MASK, elem->phy_cap_info[0]);
732 HE_PHY(CAP1_DEVICE_CLASS_A, elem->phy_cap_info[1]);
734 HE_PHY(CAP1_PREAMBLE_PUNC_RX_MASK, elem->phy_cap_info[1]);
737 HE_PHY(CAP3_DCM_MAX_CONST_TX_MASK, elem->phy_cap_info[3]);
739 HE_PHY(CAP3_DCM_MAX_TX_NSS_2, elem->phy_cap_info[3]);
741 HE_PHY(CAP3_DCM_MAX_CONST_RX_MASK, elem->phy_cap_info[3]);
743 HE_PHY(CAP3_DCM_MAX_RX_NSS_2, elem->phy_cap_info[3]);
745 HE_PHY(CAP8_DCM_MAX_RU_MASK, elem->phy_cap_info[8]);
751 mt76_connac_mcu_sta_he_tlv_v2(struct sk_buff *skb, struct ieee80211_sta *sta)
753 struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
754 struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
755 struct sta_rec_he_v2 *he;
758 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_V2, sizeof(*he));
760 he = (struct sta_rec_he_v2 *)tlv;
761 memcpy(he->he_phy_cap, elem->phy_cap_info, sizeof(he->he_phy_cap));
762 memcpy(he->he_mac_cap, elem->mac_cap_info, sizeof(he->he_mac_cap));
764 switch (sta->deflink.bandwidth) {
765 case IEEE80211_STA_RX_BW_160:
766 if (elem->phy_cap_info[0] &
767 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
768 he->max_nss_mcs[CMD_HE_MCS_BW8080] =
769 he_cap->he_mcs_nss_supp.rx_mcs_80p80;
771 he->max_nss_mcs[CMD_HE_MCS_BW160] =
772 he_cap->he_mcs_nss_supp.rx_mcs_160;
775 he->max_nss_mcs[CMD_HE_MCS_BW80] =
776 he_cap->he_mcs_nss_supp.rx_mcs_80;
780 he->pkt_ext = IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US;
782 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_he_tlv_v2);
785 mt76_connac_get_phy_mode_v2(struct mt76_phy *mphy, struct ieee80211_vif *vif,
786 enum nl80211_band band, struct ieee80211_sta *sta)
788 struct ieee80211_sta_ht_cap *ht_cap;
789 struct ieee80211_sta_vht_cap *vht_cap;
790 const struct ieee80211_sta_he_cap *he_cap;
791 const struct ieee80211_sta_eht_cap *eht_cap;
795 ht_cap = &sta->deflink.ht_cap;
796 vht_cap = &sta->deflink.vht_cap;
797 he_cap = &sta->deflink.he_cap;
798 eht_cap = &sta->deflink.eht_cap;
800 struct ieee80211_supported_band *sband;
802 sband = mphy->hw->wiphy->bands[band];
803 ht_cap = &sband->ht_cap;
804 vht_cap = &sband->vht_cap;
805 he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
806 eht_cap = ieee80211_get_eht_iftype_cap(sband, vif->type);
809 if (band == NL80211_BAND_2GHZ) {
810 mode |= PHY_TYPE_BIT_HR_DSSS | PHY_TYPE_BIT_ERP;
812 if (ht_cap->ht_supported)
813 mode |= PHY_TYPE_BIT_HT;
815 if (he_cap && he_cap->has_he)
816 mode |= PHY_TYPE_BIT_HE;
818 if (eht_cap && eht_cap->has_eht)
819 mode |= PHY_TYPE_BIT_BE;
820 } else if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ) {
821 mode |= PHY_TYPE_BIT_OFDM;
823 if (ht_cap->ht_supported)
824 mode |= PHY_TYPE_BIT_HT;
826 if (vht_cap->vht_supported)
827 mode |= PHY_TYPE_BIT_VHT;
829 if (he_cap && he_cap->has_he)
830 mode |= PHY_TYPE_BIT_HE;
832 if (eht_cap && eht_cap->has_eht)
833 mode |= PHY_TYPE_BIT_BE;
838 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode_v2);
840 void mt76_connac_mcu_sta_tlv(struct mt76_phy *mphy, struct sk_buff *skb,
841 struct ieee80211_sta *sta,
842 struct ieee80211_vif *vif,
843 u8 rcpi, u8 sta_state)
845 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
846 struct cfg80211_chan_def *chandef = mvif->ctx ?
847 &mvif->ctx->def : &mphy->chandef;
848 enum nl80211_band band = chandef->chan->band;
849 struct mt76_dev *dev = mphy->dev;
850 struct sta_rec_ra_info *ra_info;
851 struct sta_rec_state *state;
852 struct sta_rec_phy *phy;
857 if (sta->deflink.ht_cap.ht_supported) {
858 struct sta_rec_ht *ht;
860 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
861 ht = (struct sta_rec_ht *)tlv;
862 ht->ht_cap = cpu_to_le16(sta->deflink.ht_cap.cap);
866 if (sta->deflink.vht_cap.vht_supported) {
867 struct sta_rec_vht *vht;
870 len = is_mt7921(dev) ? sizeof(*vht) : sizeof(*vht) - 4;
871 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_VHT, len);
872 vht = (struct sta_rec_vht *)tlv;
873 vht->vht_cap = cpu_to_le32(sta->deflink.vht_cap.cap);
874 vht->vht_rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
875 vht->vht_tx_mcs_map = sta->deflink.vht_cap.vht_mcs.tx_mcs_map;
879 mt76_connac_mcu_sta_uapsd(skb, vif, sta);
884 if (sta->deflink.ht_cap.ht_supported || sta->deflink.he_cap.has_he)
885 mt76_connac_mcu_sta_amsdu_tlv(skb, sta, vif);
888 if (sta->deflink.he_cap.has_he) {
889 mt76_connac_mcu_sta_he_tlv(skb, sta);
890 mt76_connac_mcu_sta_he_tlv_v2(skb, sta);
891 if (band == NL80211_BAND_6GHZ &&
892 sta_state == MT76_STA_INFO_STATE_ASSOC) {
893 struct sta_rec_he_6g_capa *he_6g_capa;
895 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_6G,
896 sizeof(*he_6g_capa));
897 he_6g_capa = (struct sta_rec_he_6g_capa *)tlv;
898 he_6g_capa->capa = sta->deflink.he_6ghz_capa.capa;
902 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_PHY, sizeof(*phy));
903 phy = (struct sta_rec_phy *)tlv;
904 phy->phy_type = mt76_connac_get_phy_mode_v2(mphy, vif, band, sta);
905 phy->basic_rate = cpu_to_le16((u16)vif->bss_conf.basic_rates);
907 phy->ampdu = FIELD_PREP(IEEE80211_HT_AMPDU_PARM_FACTOR,
908 sta->deflink.ht_cap.ampdu_factor) |
909 FIELD_PREP(IEEE80211_HT_AMPDU_PARM_DENSITY,
910 sta->deflink.ht_cap.ampdu_density);
912 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra_info));
913 ra_info = (struct sta_rec_ra_info *)tlv;
915 supp_rates = sta->deflink.supp_rates[band];
916 if (band == NL80211_BAND_2GHZ)
917 supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates >> 4) |
918 FIELD_PREP(RA_LEGACY_CCK, supp_rates & 0xf);
920 supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates);
922 ra_info->legacy = cpu_to_le16(supp_rates);
924 if (sta->deflink.ht_cap.ht_supported)
925 memcpy(ra_info->rx_mcs_bitmask,
926 sta->deflink.ht_cap.mcs.rx_mask,
929 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_STATE, sizeof(*state));
930 state = (struct sta_rec_state *)tlv;
931 state->state = sta_state;
933 if (sta->deflink.vht_cap.vht_supported) {
934 state->vht_opmode = sta->deflink.bandwidth;
935 state->vht_opmode |= (sta->deflink.rx_nss - 1) <<
936 IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
939 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_tlv);
941 void mt76_connac_mcu_wtbl_smps_tlv(struct sk_buff *skb,
942 struct ieee80211_sta *sta,
943 void *sta_wtbl, void *wtbl_tlv)
945 struct wtbl_smps *smps;
948 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SMPS, sizeof(*smps),
950 smps = (struct wtbl_smps *)tlv;
951 smps->smps = (sta->deflink.smps_mode == IEEE80211_SMPS_DYNAMIC);
953 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_smps_tlv);
955 void mt76_connac_mcu_wtbl_ht_tlv(struct mt76_dev *dev, struct sk_buff *skb,
956 struct ieee80211_sta *sta, void *sta_wtbl,
957 void *wtbl_tlv, bool ht_ldpc, bool vht_ldpc)
959 struct wtbl_ht *ht = NULL;
963 if (sta->deflink.ht_cap.ht_supported || sta->deflink.he_6ghz_capa.capa) {
964 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HT, sizeof(*ht),
966 ht = (struct wtbl_ht *)tlv;
967 ht->ldpc = ht_ldpc &&
968 !!(sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING);
970 if (sta->deflink.ht_cap.ht_supported) {
971 ht->af = sta->deflink.ht_cap.ampdu_factor;
972 ht->mm = sta->deflink.ht_cap.ampdu_density;
974 ht->af = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
975 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
976 ht->mm = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
977 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
983 if (sta->deflink.vht_cap.vht_supported || sta->deflink.he_6ghz_capa.capa) {
984 struct wtbl_vht *vht;
987 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_VHT,
988 sizeof(*vht), wtbl_tlv,
990 vht = (struct wtbl_vht *)tlv;
991 vht->ldpc = vht_ldpc &&
992 !!(sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC);
995 af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
996 sta->deflink.vht_cap.cap);
998 ht->af = max(ht->af, af);
1001 mt76_connac_mcu_wtbl_smps_tlv(skb, sta, sta_wtbl, wtbl_tlv);
1003 if (is_connac_v1(dev) && sta->deflink.ht_cap.ht_supported) {
1005 u32 msk = MT_WTBL_W5_SHORT_GI_20 | MT_WTBL_W5_SHORT_GI_40 |
1006 MT_WTBL_W5_SHORT_GI_80 | MT_WTBL_W5_SHORT_GI_160;
1007 struct wtbl_raw *raw;
1009 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RAW_DATA,
1010 sizeof(*raw), wtbl_tlv,
1013 if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
1014 flags |= MT_WTBL_W5_SHORT_GI_20;
1015 if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
1016 flags |= MT_WTBL_W5_SHORT_GI_40;
1018 if (sta->deflink.vht_cap.vht_supported) {
1019 if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80)
1020 flags |= MT_WTBL_W5_SHORT_GI_80;
1021 if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160)
1022 flags |= MT_WTBL_W5_SHORT_GI_160;
1024 raw = (struct wtbl_raw *)tlv;
1025 raw->val = cpu_to_le32(flags);
1026 raw->msk = cpu_to_le32(~msk);
1031 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ht_tlv);
1033 int mt76_connac_mcu_sta_cmd(struct mt76_phy *phy,
1034 struct mt76_sta_cmd_info *info)
1036 struct mt76_vif *mvif = (struct mt76_vif *)info->vif->drv_priv;
1037 struct mt76_dev *dev = phy->dev;
1038 struct wtbl_req_hdr *wtbl_hdr;
1039 struct tlv *sta_wtbl;
1040 struct sk_buff *skb;
1042 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, info->wcid);
1044 return PTR_ERR(skb);
1046 if (info->sta || !info->offload_fw)
1047 mt76_connac_mcu_sta_basic_tlv(dev, skb, info->vif, info->sta,
1048 info->enable, info->newly);
1049 if (info->sta && info->enable)
1050 mt76_connac_mcu_sta_tlv(phy, skb, info->sta,
1051 info->vif, info->rcpi,
1054 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1055 sizeof(struct tlv));
1057 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, info->wcid,
1060 if (IS_ERR(wtbl_hdr))
1061 return PTR_ERR(wtbl_hdr);
1064 mt76_connac_mcu_wtbl_generic_tlv(dev, skb, info->vif,
1065 info->sta, sta_wtbl,
1067 mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, info->vif, info->wcid,
1068 sta_wtbl, wtbl_hdr);
1070 mt76_connac_mcu_wtbl_ht_tlv(dev, skb, info->sta,
1075 return mt76_mcu_skb_send_msg(dev, skb, info->cmd, true);
1077 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_cmd);
1079 void mt76_connac_mcu_wtbl_ba_tlv(struct mt76_dev *dev, struct sk_buff *skb,
1080 struct ieee80211_ampdu_params *params,
1081 bool enable, bool tx, void *sta_wtbl,
1087 tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_BA, sizeof(*ba),
1088 wtbl_tlv, sta_wtbl);
1090 ba = (struct wtbl_ba *)tlv;
1091 ba->tid = params->tid;
1094 ba->ba_type = MT_BA_TYPE_ORIGINATOR;
1095 ba->sn = enable ? cpu_to_le16(params->ssn) : 0;
1096 ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
1099 memcpy(ba->peer_addr, params->sta->addr, ETH_ALEN);
1100 ba->ba_type = MT_BA_TYPE_RECIPIENT;
1101 ba->rst_ba_tid = params->tid;
1102 ba->rst_ba_sel = RST_BA_MAC_TID_MATCH;
1106 if (!is_connac_v1(dev)) {
1107 ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
1112 static const u8 ba_range[] = { 4, 8, 12, 24, 36, 48, 54, 64 };
1115 for (i = 7; i > 0; i--) {
1116 if (params->buf_size >= ba_range[i])
1119 ba->ba_winsize_idx = i;
1122 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ba_tlv);
1124 int mt76_connac_mcu_uni_add_dev(struct mt76_phy *phy,
1125 struct ieee80211_vif *vif,
1126 struct mt76_wcid *wcid,
1129 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1130 struct mt76_dev *dev = phy->dev;
1142 u8 omac_addr[ETH_ALEN];
1146 .omac_idx = mvif->omac_idx,
1147 .band_idx = mvif->band_idx,
1150 .tag = cpu_to_le16(DEV_INFO_ACTIVE),
1151 .len = cpu_to_le16(sizeof(struct req_tlv)),
1160 struct mt76_connac_bss_basic_tlv basic;
1163 .bss_idx = mvif->idx,
1166 .tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1167 .len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1168 .omac_idx = mvif->omac_idx,
1169 .band_idx = mvif->band_idx,
1170 .wmm_idx = mvif->wmm_idx,
1172 .bmc_tx_wlan_idx = cpu_to_le16(wcid->idx),
1173 .sta_idx = cpu_to_le16(wcid->idx),
1177 int err, idx, cmd, len;
1180 switch (vif->type) {
1181 case NL80211_IFTYPE_MESH_POINT:
1182 case NL80211_IFTYPE_MONITOR:
1183 case NL80211_IFTYPE_AP:
1184 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_AP);
1186 case NL80211_IFTYPE_STATION:
1187 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_STA);
1189 case NL80211_IFTYPE_ADHOC:
1190 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1197 idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1198 basic_req.basic.hw_bss_idx = idx;
1200 memcpy(dev_req.tlv.omac_addr, vif->addr, ETH_ALEN);
1202 cmd = enable ? MCU_UNI_CMD(DEV_INFO_UPDATE) : MCU_UNI_CMD(BSS_INFO_UPDATE);
1203 data = enable ? (void *)&dev_req : (void *)&basic_req;
1204 len = enable ? sizeof(dev_req) : sizeof(basic_req);
1206 err = mt76_mcu_send_msg(dev, cmd, data, len, true);
1210 cmd = enable ? MCU_UNI_CMD(BSS_INFO_UPDATE) : MCU_UNI_CMD(DEV_INFO_UPDATE);
1211 data = enable ? (void *)&basic_req : (void *)&dev_req;
1212 len = enable ? sizeof(basic_req) : sizeof(dev_req);
1214 return mt76_mcu_send_msg(dev, cmd, data, len, true);
1216 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_dev);
1218 void mt76_connac_mcu_sta_ba_tlv(struct sk_buff *skb,
1219 struct ieee80211_ampdu_params *params,
1220 bool enable, bool tx)
1222 struct sta_rec_ba *ba;
1225 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
1227 ba = (struct sta_rec_ba *)tlv;
1228 ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT;
1229 ba->winsize = cpu_to_le16(params->buf_size);
1230 ba->ssn = cpu_to_le16(params->ssn);
1231 ba->ba_en = enable << params->tid;
1232 ba->amsdu = params->amsdu;
1233 ba->tid = params->tid;
1235 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba_tlv);
1237 int mt76_connac_mcu_sta_wed_update(struct mt76_dev *dev, struct sk_buff *skb)
1239 if (!mt76_is_mmio(dev))
1242 if (!mtk_wed_device_active(&dev->mmio.wed))
1245 return mtk_wed_device_update_msg(&dev->mmio.wed, WED_WO_STA_REC,
1246 skb->data, skb->len);
1248 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_wed_update);
1250 int mt76_connac_mcu_sta_ba(struct mt76_dev *dev, struct mt76_vif *mvif,
1251 struct ieee80211_ampdu_params *params,
1252 int cmd, bool enable, bool tx)
1254 struct mt76_wcid *wcid = (struct mt76_wcid *)params->sta->drv_priv;
1255 struct wtbl_req_hdr *wtbl_hdr;
1256 struct tlv *sta_wtbl;
1257 struct sk_buff *skb;
1260 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1262 return PTR_ERR(skb);
1264 sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1265 sizeof(struct tlv));
1267 wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
1269 if (IS_ERR(wtbl_hdr))
1270 return PTR_ERR(wtbl_hdr);
1272 mt76_connac_mcu_wtbl_ba_tlv(dev, skb, params, enable, tx, sta_wtbl,
1275 ret = mt76_connac_mcu_sta_wed_update(dev, skb);
1279 ret = mt76_mcu_skb_send_msg(dev, skb, cmd, true);
1283 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1285 return PTR_ERR(skb);
1287 mt76_connac_mcu_sta_ba_tlv(skb, params, enable, tx);
1289 ret = mt76_connac_mcu_sta_wed_update(dev, skb);
1293 return mt76_mcu_skb_send_msg(dev, skb, cmd, true);
1295 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba);
1297 u8 mt76_connac_get_phy_mode(struct mt76_phy *phy, struct ieee80211_vif *vif,
1298 enum nl80211_band band, struct ieee80211_sta *sta)
1300 struct mt76_dev *dev = phy->dev;
1301 const struct ieee80211_sta_he_cap *he_cap;
1302 struct ieee80211_sta_vht_cap *vht_cap;
1303 struct ieee80211_sta_ht_cap *ht_cap;
1306 if (is_connac_v1(dev))
1310 ht_cap = &sta->deflink.ht_cap;
1311 vht_cap = &sta->deflink.vht_cap;
1312 he_cap = &sta->deflink.he_cap;
1314 struct ieee80211_supported_band *sband;
1316 sband = phy->hw->wiphy->bands[band];
1317 ht_cap = &sband->ht_cap;
1318 vht_cap = &sband->vht_cap;
1319 he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
1322 if (band == NL80211_BAND_2GHZ) {
1323 mode |= PHY_MODE_B | PHY_MODE_G;
1325 if (ht_cap->ht_supported)
1326 mode |= PHY_MODE_GN;
1328 if (he_cap && he_cap->has_he)
1329 mode |= PHY_MODE_AX_24G;
1330 } else if (band == NL80211_BAND_5GHZ) {
1333 if (ht_cap->ht_supported)
1334 mode |= PHY_MODE_AN;
1336 if (vht_cap->vht_supported)
1337 mode |= PHY_MODE_AC;
1339 if (he_cap && he_cap->has_he)
1340 mode |= PHY_MODE_AX_5G;
1341 } else if (band == NL80211_BAND_6GHZ) {
1342 mode |= PHY_MODE_A | PHY_MODE_AN |
1343 PHY_MODE_AC | PHY_MODE_AX_5G;
1348 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode);
1350 u8 mt76_connac_get_phy_mode_ext(struct mt76_phy *phy, struct ieee80211_vif *vif,
1351 enum nl80211_band band)
1353 const struct ieee80211_sta_eht_cap *eht_cap;
1354 struct ieee80211_supported_band *sband;
1357 if (band == NL80211_BAND_6GHZ)
1358 mode |= PHY_MODE_AX_6G;
1360 sband = phy->hw->wiphy->bands[band];
1361 eht_cap = ieee80211_get_eht_iftype_cap(sband, vif->type);
1363 if (!eht_cap || !eht_cap->has_eht || !vif->bss_conf.eht_support)
1367 case NL80211_BAND_6GHZ:
1368 mode |= PHY_MODE_BE_6G;
1370 case NL80211_BAND_5GHZ:
1371 mode |= PHY_MODE_BE_5G;
1373 case NL80211_BAND_2GHZ:
1374 mode |= PHY_MODE_BE_24G;
1382 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode_ext);
1384 const struct ieee80211_sta_he_cap *
1385 mt76_connac_get_he_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1387 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1388 struct cfg80211_chan_def *chandef = mvif->ctx ?
1389 &mvif->ctx->def : &phy->chandef;
1390 enum nl80211_band band = chandef->chan->band;
1391 struct ieee80211_supported_band *sband;
1393 sband = phy->hw->wiphy->bands[band];
1395 return ieee80211_get_he_iftype_cap(sband, vif->type);
1397 EXPORT_SYMBOL_GPL(mt76_connac_get_he_phy_cap);
1399 const struct ieee80211_sta_eht_cap *
1400 mt76_connac_get_eht_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1402 enum nl80211_band band = phy->chandef.chan->band;
1403 struct ieee80211_supported_band *sband;
1405 sband = phy->hw->wiphy->bands[band];
1407 return ieee80211_get_eht_iftype_cap(sband, vif->type);
1409 EXPORT_SYMBOL_GPL(mt76_connac_get_eht_phy_cap);
1411 #define DEFAULT_HE_PE_DURATION 4
1412 #define DEFAULT_HE_DURATION_RTS_THRES 1023
1414 mt76_connac_mcu_uni_bss_he_tlv(struct mt76_phy *phy, struct ieee80211_vif *vif,
1417 const struct ieee80211_sta_he_cap *cap;
1418 struct bss_info_uni_he *he;
1420 cap = mt76_connac_get_he_phy_cap(phy, vif);
1422 he = (struct bss_info_uni_he *)tlv;
1423 he->he_pe_duration = vif->bss_conf.htc_trig_based_pkt_ext;
1424 if (!he->he_pe_duration)
1425 he->he_pe_duration = DEFAULT_HE_PE_DURATION;
1427 he->he_rts_thres = cpu_to_le16(vif->bss_conf.frame_time_rts_th);
1428 if (!he->he_rts_thres)
1429 he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
1431 he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
1432 he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
1433 he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
1436 int mt76_connac_mcu_uni_set_chctx(struct mt76_phy *phy, struct mt76_vif *mvif,
1437 struct ieee80211_chanctx_conf *ctx)
1439 struct cfg80211_chan_def *chandef = ctx ? &ctx->def : &phy->chandef;
1440 int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
1441 enum nl80211_band band = chandef->chan->band;
1442 struct mt76_dev *mdev = phy->dev;
1463 } __packed rlm_req = {
1465 .bss_idx = mvif->idx,
1468 .tag = cpu_to_le16(UNI_BSS_INFO_RLM),
1469 .len = cpu_to_le16(sizeof(struct rlm_tlv)),
1470 .control_channel = chandef->chan->hw_value,
1471 .center_chan = ieee80211_frequency_to_channel(freq1),
1472 .center_chan2 = ieee80211_frequency_to_channel(freq2),
1473 .tx_streams = hweight8(phy->antenna_mask),
1474 .ht_op_info = 4, /* set HT 40M allowed */
1475 .rx_streams = phy->chainmask,
1481 switch (chandef->width) {
1482 case NL80211_CHAN_WIDTH_40:
1483 rlm_req.rlm.bw = CMD_CBW_40MHZ;
1485 case NL80211_CHAN_WIDTH_80:
1486 rlm_req.rlm.bw = CMD_CBW_80MHZ;
1488 case NL80211_CHAN_WIDTH_80P80:
1489 rlm_req.rlm.bw = CMD_CBW_8080MHZ;
1491 case NL80211_CHAN_WIDTH_160:
1492 rlm_req.rlm.bw = CMD_CBW_160MHZ;
1494 case NL80211_CHAN_WIDTH_5:
1495 rlm_req.rlm.bw = CMD_CBW_5MHZ;
1497 case NL80211_CHAN_WIDTH_10:
1498 rlm_req.rlm.bw = CMD_CBW_10MHZ;
1500 case NL80211_CHAN_WIDTH_20_NOHT:
1501 case NL80211_CHAN_WIDTH_20:
1503 rlm_req.rlm.bw = CMD_CBW_20MHZ;
1504 rlm_req.rlm.ht_op_info = 0;
1508 if (rlm_req.rlm.control_channel < rlm_req.rlm.center_chan)
1509 rlm_req.rlm.sco = 1; /* SCA */
1510 else if (rlm_req.rlm.control_channel > rlm_req.rlm.center_chan)
1511 rlm_req.rlm.sco = 3; /* SCB */
1513 return mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE), &rlm_req,
1514 sizeof(rlm_req), true);
1516 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_set_chctx);
1518 int mt76_connac_mcu_uni_add_bss(struct mt76_phy *phy,
1519 struct ieee80211_vif *vif,
1520 struct mt76_wcid *wcid,
1522 struct ieee80211_chanctx_conf *ctx)
1524 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1525 struct cfg80211_chan_def *chandef = ctx ? &ctx->def : &phy->chandef;
1526 enum nl80211_band band = chandef->chan->band;
1527 struct mt76_dev *mdev = phy->dev;
1533 struct mt76_connac_bss_basic_tlv basic;
1534 struct mt76_connac_bss_qos_tlv qos;
1537 .bss_idx = mvif->idx,
1540 .tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1541 .len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1542 .bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1543 .dtim_period = vif->bss_conf.dtim_period,
1544 .omac_idx = mvif->omac_idx,
1545 .band_idx = mvif->band_idx,
1546 .wmm_idx = mvif->wmm_idx,
1547 .active = true, /* keep bss deactivated */
1548 .phymode = mt76_connac_get_phy_mode(phy, vif, band, NULL),
1551 .tag = cpu_to_le16(UNI_BSS_INFO_QBSS),
1552 .len = cpu_to_le16(sizeof(struct mt76_connac_bss_qos_tlv)),
1553 .qos = vif->bss_conf.qos,
1559 idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1560 basic_req.basic.hw_bss_idx = idx;
1561 if (band == NL80211_BAND_6GHZ)
1562 basic_req.basic.phymode_ext = PHY_MODE_AX_6G;
1564 basic_phy = mt76_connac_get_phy_mode_v2(phy, vif, band, NULL);
1565 basic_req.basic.nonht_basic_phy = cpu_to_le16(basic_phy);
1567 switch (vif->type) {
1568 case NL80211_IFTYPE_MESH_POINT:
1569 case NL80211_IFTYPE_AP:
1571 conn_type = CONNECTION_P2P_GO;
1573 conn_type = CONNECTION_INFRA_AP;
1574 basic_req.basic.conn_type = cpu_to_le32(conn_type);
1575 /* Fully active/deactivate BSS network in AP mode only */
1576 basic_req.basic.active = enable;
1578 case NL80211_IFTYPE_STATION:
1580 conn_type = CONNECTION_P2P_GC;
1582 conn_type = CONNECTION_INFRA_STA;
1583 basic_req.basic.conn_type = cpu_to_le32(conn_type);
1585 case NL80211_IFTYPE_ADHOC:
1586 basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1593 memcpy(basic_req.basic.bssid, vif->bss_conf.bssid, ETH_ALEN);
1594 basic_req.basic.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx);
1595 basic_req.basic.sta_idx = cpu_to_le16(wcid->idx);
1596 basic_req.basic.conn_state = !enable;
1598 err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE), &basic_req,
1599 sizeof(basic_req), true);
1603 if (vif->bss_conf.he_support) {
1609 struct bss_info_uni_he he;
1610 struct bss_info_uni_bss_color bss_color;
1613 .bss_idx = mvif->idx,
1616 .tag = cpu_to_le16(UNI_BSS_INFO_HE_BASIC),
1617 .len = cpu_to_le16(sizeof(struct bss_info_uni_he)),
1620 .tag = cpu_to_le16(UNI_BSS_INFO_BSS_COLOR),
1621 .len = cpu_to_le16(sizeof(struct bss_info_uni_bss_color)),
1628 he_req.bss_color.enable =
1629 vif->bss_conf.he_bss_color.enabled;
1630 he_req.bss_color.bss_color =
1631 vif->bss_conf.he_bss_color.color;
1634 mt76_connac_mcu_uni_bss_he_tlv(phy, vif,
1635 (struct tlv *)&he_req.he);
1636 err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE),
1637 &he_req, sizeof(he_req), true);
1642 return mt76_connac_mcu_uni_set_chctx(phy, mvif, ctx);
1644 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_bss);
1646 #define MT76_CONNAC_SCAN_CHANNEL_TIME 60
1647 int mt76_connac_mcu_hw_scan(struct mt76_phy *phy, struct ieee80211_vif *vif,
1648 struct ieee80211_scan_request *scan_req)
1650 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1651 struct cfg80211_scan_request *sreq = &scan_req->req;
1652 int n_ssids = 0, err, i, duration;
1653 int ext_channels_num = max_t(int, sreq->n_channels - 32, 0);
1654 struct ieee80211_channel **scan_list = sreq->channels;
1655 struct mt76_dev *mdev = phy->dev;
1656 struct mt76_connac_mcu_scan_channel *chan;
1657 struct mt76_connac_hw_scan_req *req;
1658 struct sk_buff *skb;
1660 if (test_bit(MT76_HW_SCANNING, &phy->state))
1663 skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req));
1667 set_bit(MT76_HW_SCANNING, &phy->state);
1668 mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1670 req = (struct mt76_connac_hw_scan_req *)skb_put(skb, sizeof(*req));
1672 req->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
1673 req->bss_idx = mvif->idx;
1674 req->scan_type = sreq->n_ssids ? 1 : 0;
1675 req->probe_req_num = sreq->n_ssids ? 2 : 0;
1678 for (i = 0; i < sreq->n_ssids; i++) {
1679 if (!sreq->ssids[i].ssid_len)
1682 req->ssids[i].ssid_len = cpu_to_le32(sreq->ssids[i].ssid_len);
1683 memcpy(req->ssids[i].ssid, sreq->ssids[i].ssid,
1684 sreq->ssids[i].ssid_len);
1687 req->ssid_type = n_ssids ? BIT(2) : BIT(0);
1688 req->ssid_type_ext = n_ssids ? BIT(0) : 0;
1689 req->ssids_num = n_ssids;
1691 duration = is_mt7921(phy->dev) ? 0 : MT76_CONNAC_SCAN_CHANNEL_TIME;
1692 /* increase channel time for passive scan */
1695 req->timeout_value = cpu_to_le16(sreq->n_channels * duration);
1696 req->channel_min_dwell_time = cpu_to_le16(duration);
1697 req->channel_dwell_time = cpu_to_le16(duration);
1699 if (sreq->n_channels == 0 || sreq->n_channels > 64) {
1700 req->channel_type = 0;
1701 req->channels_num = 0;
1702 req->ext_channels_num = 0;
1704 req->channel_type = 4;
1705 req->channels_num = min_t(u8, sreq->n_channels, 32);
1706 req->ext_channels_num = min_t(u8, ext_channels_num, 32);
1709 for (i = 0; i < req->channels_num + req->ext_channels_num; i++) {
1711 chan = &req->ext_channels[i - 32];
1713 chan = &req->channels[i];
1715 switch (scan_list[i]->band) {
1716 case NL80211_BAND_2GHZ:
1719 case NL80211_BAND_6GHZ:
1726 chan->channel_num = scan_list[i]->hw_value;
1729 if (sreq->ie_len > 0) {
1730 memcpy(req->ies, sreq->ie, sreq->ie_len);
1731 req->ies_len = cpu_to_le16(sreq->ie_len);
1734 if (is_mt7921(phy->dev))
1735 req->scan_func |= SCAN_FUNC_SPLIT_SCAN;
1737 memcpy(req->bssid, sreq->bssid, ETH_ALEN);
1738 if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1739 get_random_mask_addr(req->random_mac, sreq->mac_addr,
1740 sreq->mac_addr_mask);
1741 req->scan_func |= SCAN_FUNC_RANDOM_MAC;
1744 err = mt76_mcu_skb_send_msg(mdev, skb, MCU_CE_CMD(START_HW_SCAN),
1747 clear_bit(MT76_HW_SCANNING, &phy->state);
1751 EXPORT_SYMBOL_GPL(mt76_connac_mcu_hw_scan);
1753 int mt76_connac_mcu_cancel_hw_scan(struct mt76_phy *phy,
1754 struct ieee80211_vif *vif)
1756 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1762 .seq_num = mvif->scan_seq_num,
1765 if (test_and_clear_bit(MT76_HW_SCANNING, &phy->state)) {
1766 struct cfg80211_scan_info info = {
1770 ieee80211_scan_completed(phy->hw, &info);
1773 return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(CANCEL_HW_SCAN),
1774 &req, sizeof(req), false);
1776 EXPORT_SYMBOL_GPL(mt76_connac_mcu_cancel_hw_scan);
1778 int mt76_connac_mcu_sched_scan_req(struct mt76_phy *phy,
1779 struct ieee80211_vif *vif,
1780 struct cfg80211_sched_scan_request *sreq)
1782 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1783 struct ieee80211_channel **scan_list = sreq->channels;
1784 struct mt76_connac_mcu_scan_channel *chan;
1785 struct mt76_connac_sched_scan_req *req;
1786 struct mt76_dev *mdev = phy->dev;
1787 struct cfg80211_match_set *match;
1788 struct cfg80211_ssid *ssid;
1789 struct sk_buff *skb;
1792 skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req) + sreq->ie_len);
1796 mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1798 req = (struct mt76_connac_sched_scan_req *)skb_put(skb, sizeof(*req));
1800 req->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
1802 if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1803 u8 *addr = is_mt7663(phy->dev) ? req->mt7663.random_mac
1804 : req->mt7921.random_mac;
1807 get_random_mask_addr(addr, sreq->mac_addr,
1808 sreq->mac_addr_mask);
1810 if (is_mt7921(phy->dev)) {
1811 req->mt7921.bss_idx = mvif->idx;
1812 req->mt7921.delay = cpu_to_le32(sreq->delay);
1815 req->ssids_num = sreq->n_ssids;
1816 for (i = 0; i < req->ssids_num; i++) {
1817 ssid = &sreq->ssids[i];
1818 memcpy(req->ssids[i].ssid, ssid->ssid, ssid->ssid_len);
1819 req->ssids[i].ssid_len = cpu_to_le32(ssid->ssid_len);
1822 req->match_num = sreq->n_match_sets;
1823 for (i = 0; i < req->match_num; i++) {
1824 match = &sreq->match_sets[i];
1825 memcpy(req->match[i].ssid, match->ssid.ssid,
1826 match->ssid.ssid_len);
1827 req->match[i].rssi_th = cpu_to_le32(match->rssi_thold);
1828 req->match[i].ssid_len = match->ssid.ssid_len;
1831 req->channel_type = sreq->n_channels ? 4 : 0;
1832 req->channels_num = min_t(u8, sreq->n_channels, 64);
1833 for (i = 0; i < req->channels_num; i++) {
1834 chan = &req->channels[i];
1836 switch (scan_list[i]->band) {
1837 case NL80211_BAND_2GHZ:
1840 case NL80211_BAND_6GHZ:
1847 chan->channel_num = scan_list[i]->hw_value;
1850 req->intervals_num = sreq->n_scan_plans;
1851 for (i = 0; i < req->intervals_num; i++)
1852 req->intervals[i] = cpu_to_le16(sreq->scan_plans[i].interval);
1854 if (sreq->ie_len > 0) {
1855 req->ie_len = cpu_to_le16(sreq->ie_len);
1856 memcpy(skb_put(skb, sreq->ie_len), sreq->ie, sreq->ie_len);
1859 return mt76_mcu_skb_send_msg(mdev, skb, MCU_CE_CMD(SCHED_SCAN_REQ),
1862 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_req);
1864 int mt76_connac_mcu_sched_scan_enable(struct mt76_phy *phy,
1865 struct ieee80211_vif *vif,
1869 u8 active; /* 0: enabled 1: disabled */
1876 set_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1878 clear_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1880 return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(SCHED_SCAN_ENABLE),
1881 &req, sizeof(req), false);
1883 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_enable);
1885 int mt76_connac_mcu_chip_config(struct mt76_dev *dev)
1887 struct mt76_connac_config req = {
1891 memcpy(req.data, "assert", 7);
1893 return mt76_mcu_send_msg(dev, MCU_CE_CMD(CHIP_CONFIG),
1894 &req, sizeof(req), false);
1896 EXPORT_SYMBOL_GPL(mt76_connac_mcu_chip_config);
1898 int mt76_connac_mcu_set_deep_sleep(struct mt76_dev *dev, bool enable)
1900 struct mt76_connac_config req = {
1904 snprintf(req.data, sizeof(req.data), "KeepFullPwr %d", !enable);
1906 return mt76_mcu_send_msg(dev, MCU_CE_CMD(CHIP_CONFIG),
1907 &req, sizeof(req), false);
1909 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_deep_sleep);
1911 int mt76_connac_sta_state_dp(struct mt76_dev *dev,
1912 enum ieee80211_sta_state old_state,
1913 enum ieee80211_sta_state new_state)
1915 if ((old_state == IEEE80211_STA_ASSOC &&
1916 new_state == IEEE80211_STA_AUTHORIZED) ||
1917 (old_state == IEEE80211_STA_NONE &&
1918 new_state == IEEE80211_STA_NOTEXIST))
1919 mt76_connac_mcu_set_deep_sleep(dev, true);
1921 if ((old_state == IEEE80211_STA_NOTEXIST &&
1922 new_state == IEEE80211_STA_NONE) ||
1923 (old_state == IEEE80211_STA_AUTHORIZED &&
1924 new_state == IEEE80211_STA_ASSOC))
1925 mt76_connac_mcu_set_deep_sleep(dev, false);
1929 EXPORT_SYMBOL_GPL(mt76_connac_sta_state_dp);
1931 void mt76_connac_mcu_coredump_event(struct mt76_dev *dev, struct sk_buff *skb,
1932 struct mt76_connac_coredump *coredump)
1934 spin_lock_bh(&dev->lock);
1935 __skb_queue_tail(&coredump->msg_list, skb);
1936 spin_unlock_bh(&dev->lock);
1938 coredump->last_activity = jiffies;
1940 queue_delayed_work(dev->wq, &coredump->work,
1941 MT76_CONNAC_COREDUMP_TIMEOUT);
1943 EXPORT_SYMBOL_GPL(mt76_connac_mcu_coredump_event);
1946 mt76_connac_mcu_build_sku(struct mt76_dev *dev, s8 *sku,
1947 struct mt76_power_limits *limits,
1948 enum nl80211_band band)
1950 int max_power = is_mt7921(dev) ? 127 : 63;
1951 int i, offset = sizeof(limits->cck);
1953 memset(sku, max_power, MT_SKU_POWER_LIMIT);
1955 if (band == NL80211_BAND_2GHZ) {
1957 memcpy(sku, limits->cck, sizeof(limits->cck));
1961 memcpy(&sku[offset], limits->ofdm, sizeof(limits->ofdm));
1962 offset += sizeof(limits->ofdm);
1965 for (i = 0; i < 2; i++) {
1966 memcpy(&sku[offset], limits->mcs[i], 8);
1969 sku[offset++] = limits->mcs[0][0];
1972 for (i = 0; i < ARRAY_SIZE(limits->mcs); i++) {
1973 memcpy(&sku[offset], limits->mcs[i],
1974 ARRAY_SIZE(limits->mcs[i]));
1978 if (!is_mt7921(dev))
1982 for (i = 0; i < ARRAY_SIZE(limits->ru); i++) {
1983 memcpy(&sku[offset], limits->ru[i], ARRAY_SIZE(limits->ru[i]));
1984 offset += ARRAY_SIZE(limits->ru[i]);
1988 s8 mt76_connac_get_ch_power(struct mt76_phy *phy,
1989 struct ieee80211_channel *chan,
1992 struct mt76_dev *dev = phy->dev;
1993 struct ieee80211_supported_band *sband;
1996 switch (chan->band) {
1997 case NL80211_BAND_2GHZ:
1998 sband = &phy->sband_2g.sband;
2000 case NL80211_BAND_5GHZ:
2001 sband = &phy->sband_5g.sband;
2003 case NL80211_BAND_6GHZ:
2004 sband = &phy->sband_6g.sband;
2007 return target_power;
2010 for (i = 0; i < sband->n_channels; i++) {
2011 struct ieee80211_channel *ch = &sband->channels[i];
2013 if (ch->hw_value == chan->hw_value) {
2014 if (!(ch->flags & IEEE80211_CHAN_DISABLED)) {
2015 int power = 2 * ch->max_reg_power;
2017 if (is_mt7663(dev) && (power > 63 || power < -64))
2019 target_power = min_t(s8, power, target_power);
2025 return target_power;
2027 EXPORT_SYMBOL_GPL(mt76_connac_get_ch_power);
2030 mt76_connac_mcu_rate_txpower_band(struct mt76_phy *phy,
2031 enum nl80211_band band)
2033 struct mt76_dev *dev = phy->dev;
2034 int sku_len, batch_len = is_mt7921(dev) ? 8 : 16;
2035 static const u8 chan_list_2ghz[] = {
2036 1, 2, 3, 4, 5, 6, 7,
2037 8, 9, 10, 11, 12, 13, 14
2039 static const u8 chan_list_5ghz[] = {
2040 36, 38, 40, 42, 44, 46, 48,
2041 50, 52, 54, 56, 58, 60, 62,
2042 64, 100, 102, 104, 106, 108, 110,
2043 112, 114, 116, 118, 120, 122, 124,
2044 126, 128, 132, 134, 136, 138, 140,
2045 142, 144, 149, 151, 153, 155, 157,
2046 159, 161, 165, 169, 173, 177
2048 static const u8 chan_list_6ghz[] = {
2049 1, 3, 5, 7, 9, 11, 13,
2050 15, 17, 19, 21, 23, 25, 27,
2051 29, 33, 35, 37, 39, 41, 43,
2052 45, 47, 49, 51, 53, 55, 57,
2053 59, 61, 65, 67, 69, 71, 73,
2054 75, 77, 79, 81, 83, 85, 87,
2055 89, 91, 93, 97, 99, 101, 103,
2056 105, 107, 109, 111, 113, 115, 117,
2057 119, 121, 123, 125, 129, 131, 133,
2058 135, 137, 139, 141, 143, 145, 147,
2059 149, 151, 153, 155, 157, 161, 163,
2060 165, 167, 169, 171, 173, 175, 177,
2061 179, 181, 183, 185, 187, 189, 193,
2062 195, 197, 199, 201, 203, 205, 207,
2063 209, 211, 213, 215, 217, 219, 221,
2066 int i, n_chan, batch_size, idx = 0, tx_power, last_ch, err = 0;
2067 struct mt76_connac_sku_tlv sku_tlbv;
2068 struct mt76_power_limits *limits;
2071 limits = devm_kmalloc(dev->dev, sizeof(*limits), GFP_KERNEL);
2075 sku_len = is_mt7921(dev) ? sizeof(sku_tlbv) : sizeof(sku_tlbv) - 92;
2076 tx_power = 2 * phy->hw->conf.power_level;
2080 if (band == NL80211_BAND_2GHZ) {
2081 n_chan = ARRAY_SIZE(chan_list_2ghz);
2082 ch_list = chan_list_2ghz;
2083 } else if (band == NL80211_BAND_6GHZ) {
2084 n_chan = ARRAY_SIZE(chan_list_6ghz);
2085 ch_list = chan_list_6ghz;
2087 n_chan = ARRAY_SIZE(chan_list_5ghz);
2088 ch_list = chan_list_5ghz;
2090 batch_size = DIV_ROUND_UP(n_chan, batch_len);
2092 if (phy->cap.has_6ghz)
2093 last_ch = chan_list_6ghz[ARRAY_SIZE(chan_list_6ghz) - 1];
2094 else if (phy->cap.has_5ghz)
2095 last_ch = chan_list_5ghz[ARRAY_SIZE(chan_list_5ghz) - 1];
2097 last_ch = chan_list_2ghz[ARRAY_SIZE(chan_list_2ghz) - 1];
2099 for (i = 0; i < batch_size; i++) {
2100 struct mt76_connac_tx_power_limit_tlv tx_power_tlv = {};
2101 int j, msg_len, num_ch;
2102 struct sk_buff *skb;
2104 num_ch = i == batch_size - 1 ? n_chan % batch_len : batch_len;
2105 msg_len = sizeof(tx_power_tlv) + num_ch * sizeof(sku_tlbv);
2106 skb = mt76_mcu_msg_alloc(dev, NULL, msg_len);
2112 skb_reserve(skb, sizeof(tx_power_tlv));
2114 BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(tx_power_tlv.alpha2));
2115 memcpy(tx_power_tlv.alpha2, dev->alpha2, sizeof(dev->alpha2));
2116 tx_power_tlv.n_chan = num_ch;
2119 case NL80211_BAND_2GHZ:
2120 tx_power_tlv.band = 1;
2122 case NL80211_BAND_6GHZ:
2123 tx_power_tlv.band = 3;
2126 tx_power_tlv.band = 2;
2130 for (j = 0; j < num_ch; j++, idx++) {
2131 struct ieee80211_channel chan = {
2132 .hw_value = ch_list[idx],
2135 s8 reg_power, sar_power;
2137 reg_power = mt76_connac_get_ch_power(phy, &chan,
2139 sar_power = mt76_get_sar_power(phy, &chan, reg_power);
2141 mt76_get_rate_power_limits(phy, &chan, limits,
2144 tx_power_tlv.last_msg = ch_list[idx] == last_ch;
2145 sku_tlbv.channel = ch_list[idx];
2147 mt76_connac_mcu_build_sku(dev, sku_tlbv.pwr_limit,
2149 skb_put_data(skb, &sku_tlbv, sku_len);
2151 __skb_push(skb, sizeof(tx_power_tlv));
2152 memcpy(skb->data, &tx_power_tlv, sizeof(tx_power_tlv));
2154 err = mt76_mcu_skb_send_msg(dev, skb,
2155 MCU_CE_CMD(SET_RATE_TX_POWER),
2162 devm_kfree(dev->dev, limits);
2166 int mt76_connac_mcu_set_rate_txpower(struct mt76_phy *phy)
2170 if (phy->cap.has_2ghz) {
2171 err = mt76_connac_mcu_rate_txpower_band(phy,
2176 if (phy->cap.has_5ghz) {
2177 err = mt76_connac_mcu_rate_txpower_band(phy,
2182 if (phy->cap.has_6ghz) {
2183 err = mt76_connac_mcu_rate_txpower_band(phy,
2191 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rate_txpower);
2193 int mt76_connac_mcu_update_arp_filter(struct mt76_dev *dev,
2194 struct mt76_vif *vif,
2195 struct ieee80211_bss_conf *info)
2197 struct ieee80211_vif *mvif = container_of(info, struct ieee80211_vif,
2199 struct sk_buff *skb;
2200 int i, len = min_t(int, mvif->cfg.arp_addr_cnt,
2201 IEEE80211_BSS_ARP_ADDR_LIST_LEN);
2207 struct mt76_connac_arpns_tlv arp;
2210 .bss_idx = vif->idx,
2213 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
2214 .len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
2216 .mode = 2, /* update */
2221 skb = mt76_mcu_msg_alloc(dev, NULL,
2222 sizeof(req_hdr) + len * sizeof(__be32));
2226 skb_put_data(skb, &req_hdr, sizeof(req_hdr));
2227 for (i = 0; i < len; i++)
2228 skb_put_data(skb, &mvif->cfg.arp_addr_list[i], sizeof(__be32));
2230 return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(OFFLOAD), true);
2232 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_arp_filter);
2234 int mt76_connac_mcu_set_p2p_oppps(struct ieee80211_hw *hw,
2235 struct ieee80211_vif *vif)
2237 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2238 int ct_window = vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
2239 struct mt76_phy *phy = hw->priv;
2245 .ct_win = cpu_to_le32(ct_window),
2246 .bss_idx = mvif->idx,
2249 return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(SET_P2P_OPPPS),
2250 &req, sizeof(req), false);
2252 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_p2p_oppps);
2256 const struct wiphy_wowlan_support mt76_connac_wowlan_support = {
2257 .flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
2258 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY | WIPHY_WOWLAN_NET_DETECT,
2260 .pattern_min_len = 1,
2261 .pattern_max_len = MT76_CONNAC_WOW_PATTEN_MAX_LEN,
2262 .max_nd_match_sets = 10,
2264 EXPORT_SYMBOL_GPL(mt76_connac_wowlan_support);
2267 mt76_connac_mcu_key_iter(struct ieee80211_hw *hw,
2268 struct ieee80211_vif *vif,
2269 struct ieee80211_sta *sta,
2270 struct ieee80211_key_conf *key,
2273 struct mt76_connac_gtk_rekey_tlv *gtk_tlv = data;
2276 if (key->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
2277 key->cipher != WLAN_CIPHER_SUITE_CCMP &&
2278 key->cipher != WLAN_CIPHER_SUITE_TKIP)
2281 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2286 /* we are assuming here to have a single pairwise key */
2287 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
2288 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2289 gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_1);
2291 gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_2);
2293 gtk_tlv->pairwise_cipher = cpu_to_le32(cipher);
2294 gtk_tlv->keyid = key->keyidx;
2296 gtk_tlv->group_cipher = cpu_to_le32(cipher);
2300 int mt76_connac_mcu_update_gtk_rekey(struct ieee80211_hw *hw,
2301 struct ieee80211_vif *vif,
2302 struct cfg80211_gtk_rekey_data *key)
2304 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2305 struct mt76_connac_gtk_rekey_tlv *gtk_tlv;
2306 struct mt76_phy *phy = hw->priv;
2307 struct sk_buff *skb;
2312 .bss_idx = mvif->idx,
2315 skb = mt76_mcu_msg_alloc(phy->dev, NULL,
2316 sizeof(hdr) + sizeof(*gtk_tlv));
2320 skb_put_data(skb, &hdr, sizeof(hdr));
2321 gtk_tlv = (struct mt76_connac_gtk_rekey_tlv *)skb_put(skb,
2323 gtk_tlv->tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY);
2324 gtk_tlv->len = cpu_to_le16(sizeof(*gtk_tlv));
2325 gtk_tlv->rekey_mode = 2;
2326 gtk_tlv->option = 1;
2329 ieee80211_iter_keys_rcu(hw, vif, mt76_connac_mcu_key_iter, gtk_tlv);
2332 memcpy(gtk_tlv->kek, key->kek, NL80211_KEK_LEN);
2333 memcpy(gtk_tlv->kck, key->kck, NL80211_KCK_LEN);
2334 memcpy(gtk_tlv->replay_ctr, key->replay_ctr, NL80211_REPLAY_CTR_LEN);
2336 return mt76_mcu_skb_send_msg(phy->dev, skb,
2337 MCU_UNI_CMD(OFFLOAD), true);
2339 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_gtk_rekey);
2342 mt76_connac_mcu_set_arp_filter(struct mt76_dev *dev, struct ieee80211_vif *vif,
2345 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2351 struct mt76_connac_arpns_tlv arpns;
2354 .bss_idx = mvif->idx,
2357 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
2358 .len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
2363 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(OFFLOAD), &req,
2368 mt76_connac_mcu_set_gtk_rekey(struct mt76_dev *dev, struct ieee80211_vif *vif,
2371 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2377 struct mt76_connac_gtk_rekey_tlv gtk_tlv;
2380 .bss_idx = mvif->idx,
2383 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY),
2384 .len = cpu_to_le16(sizeof(struct mt76_connac_gtk_rekey_tlv)),
2385 .rekey_mode = !suspend,
2389 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(OFFLOAD), &req,
2392 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_gtk_rekey);
2395 mt76_connac_mcu_set_suspend_mode(struct mt76_dev *dev,
2396 struct ieee80211_vif *vif,
2397 bool enable, u8 mdtim,
2400 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2406 struct mt76_connac_suspend_tlv suspend_tlv;
2409 .bss_idx = mvif->idx,
2412 .tag = cpu_to_le16(UNI_SUSPEND_MODE_SETTING),
2413 .len = cpu_to_le16(sizeof(struct mt76_connac_suspend_tlv)),
2416 .wow_suspend = wow_suspend,
2420 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(SUSPEND), &req,
2423 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_suspend_mode);
2426 mt76_connac_mcu_set_wow_pattern(struct mt76_dev *dev,
2427 struct ieee80211_vif *vif,
2428 u8 index, bool enable,
2429 struct cfg80211_pkt_pattern *pattern)
2431 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2432 struct mt76_connac_wow_pattern_tlv *ptlv;
2433 struct sk_buff *skb;
2438 .bss_idx = mvif->idx,
2441 skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(hdr) + sizeof(*ptlv));
2445 skb_put_data(skb, &hdr, sizeof(hdr));
2446 ptlv = (struct mt76_connac_wow_pattern_tlv *)skb_put(skb, sizeof(*ptlv));
2447 ptlv->tag = cpu_to_le16(UNI_SUSPEND_WOW_PATTERN);
2448 ptlv->len = cpu_to_le16(sizeof(*ptlv));
2449 ptlv->data_len = pattern->pattern_len;
2450 ptlv->enable = enable;
2451 ptlv->index = index;
2453 memcpy(ptlv->pattern, pattern->pattern, pattern->pattern_len);
2454 memcpy(ptlv->mask, pattern->mask, DIV_ROUND_UP(pattern->pattern_len, 8));
2456 return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(SUSPEND), true);
2460 mt76_connac_mcu_set_wow_ctrl(struct mt76_phy *phy, struct ieee80211_vif *vif,
2461 bool suspend, struct cfg80211_wowlan *wowlan)
2463 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2464 struct mt76_dev *dev = phy->dev;
2470 struct mt76_connac_wow_ctrl_tlv wow_ctrl_tlv;
2471 struct mt76_connac_wow_gpio_param_tlv gpio_tlv;
2474 .bss_idx = mvif->idx,
2477 .tag = cpu_to_le16(UNI_SUSPEND_WOW_CTRL),
2478 .len = cpu_to_le16(sizeof(struct mt76_connac_wow_ctrl_tlv)),
2479 .cmd = suspend ? 1 : 2,
2482 .tag = cpu_to_le16(UNI_SUSPEND_WOW_GPIO_PARAM),
2483 .len = cpu_to_le16(sizeof(struct mt76_connac_wow_gpio_param_tlv)),
2484 .gpio_pin = 0xff, /* follow fw about GPIO pin */
2488 if (wowlan->magic_pkt)
2489 req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_MAGIC;
2490 if (wowlan->disconnect)
2491 req.wow_ctrl_tlv.trigger |= (UNI_WOW_DETECT_TYPE_DISCONNECT |
2492 UNI_WOW_DETECT_TYPE_BCN_LOST);
2493 if (wowlan->nd_config) {
2494 mt76_connac_mcu_sched_scan_req(phy, vif, wowlan->nd_config);
2495 req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_SCH_SCAN_HIT;
2496 mt76_connac_mcu_sched_scan_enable(phy, vif, suspend);
2498 if (wowlan->n_patterns)
2499 req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_BITMAP;
2501 if (mt76_is_mmio(dev))
2502 req.wow_ctrl_tlv.wakeup_hif = WOW_PCIE;
2503 else if (mt76_is_usb(dev))
2504 req.wow_ctrl_tlv.wakeup_hif = WOW_USB;
2505 else if (mt76_is_sdio(dev))
2506 req.wow_ctrl_tlv.wakeup_hif = WOW_GPIO;
2508 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(SUSPEND), &req,
2511 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_wow_ctrl);
2513 int mt76_connac_mcu_set_hif_suspend(struct mt76_dev *dev, bool suspend)
2517 u8 hif_type; /* 0x0: HIF_SDIO
2523 struct hif_suspend_tlv {
2527 } __packed hif_suspend;
2530 .tag = cpu_to_le16(0), /* 0: UNI_HIF_CTRL_BASIC */
2531 .len = cpu_to_le16(sizeof(struct hif_suspend_tlv)),
2536 if (mt76_is_mmio(dev))
2537 req.hdr.hif_type = 2;
2538 else if (mt76_is_usb(dev))
2539 req.hdr.hif_type = 1;
2540 else if (mt76_is_sdio(dev))
2541 req.hdr.hif_type = 0;
2543 return mt76_mcu_send_msg(dev, MCU_UNI_CMD(HIF_CTRL), &req,
2546 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_hif_suspend);
2548 void mt76_connac_mcu_set_suspend_iter(void *priv, u8 *mac,
2549 struct ieee80211_vif *vif)
2551 struct mt76_phy *phy = priv;
2552 bool suspend = !test_bit(MT76_STATE_RUNNING, &phy->state);
2553 struct ieee80211_hw *hw = phy->hw;
2554 struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
2557 mt76_connac_mcu_set_gtk_rekey(phy->dev, vif, suspend);
2558 mt76_connac_mcu_set_arp_filter(phy->dev, vif, suspend);
2560 mt76_connac_mcu_set_suspend_mode(phy->dev, vif, suspend, 1, true);
2562 for (i = 0; i < wowlan->n_patterns; i++)
2563 mt76_connac_mcu_set_wow_pattern(phy->dev, vif, i, suspend,
2564 &wowlan->patterns[i]);
2565 mt76_connac_mcu_set_wow_ctrl(phy, vif, suspend, wowlan);
2567 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_suspend_iter);
2568 #endif /* CONFIG_PM */
2570 u32 mt76_connac_mcu_reg_rr(struct mt76_dev *dev, u32 offset)
2576 .addr = cpu_to_le32(offset),
2579 return mt76_mcu_send_msg(dev, MCU_CE_QUERY(REG_READ), &req,
2582 EXPORT_SYMBOL_GPL(mt76_connac_mcu_reg_rr);
2584 void mt76_connac_mcu_reg_wr(struct mt76_dev *dev, u32 offset, u32 val)
2590 .addr = cpu_to_le32(offset),
2591 .val = cpu_to_le32(val),
2594 mt76_mcu_send_msg(dev, MCU_CE_CMD(REG_WRITE), &req,
2595 sizeof(req), false);
2597 EXPORT_SYMBOL_GPL(mt76_connac_mcu_reg_wr);
2600 mt76_connac_mcu_sta_key_tlv(struct mt76_connac_sta_key_conf *sta_key_conf,
2601 struct sk_buff *skb,
2602 struct ieee80211_key_conf *key,
2603 enum set_key_cmd cmd)
2605 struct sta_rec_sec *sec;
2606 u32 len = sizeof(*sec);
2609 tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_KEY_V2, sizeof(*sec));
2610 sec = (struct sta_rec_sec *)tlv;
2613 if (cmd == SET_KEY) {
2614 struct sec_key *sec_key;
2617 cipher = mt76_connac_mcu_get_cipher(key->cipher);
2618 if (cipher == MCU_CIPHER_NONE)
2621 sec_key = &sec->key[0];
2622 sec_key->cipher_len = sizeof(*sec_key);
2624 if (cipher == MCU_CIPHER_BIP_CMAC_128) {
2625 sec_key->cipher_id = MCU_CIPHER_AES_CCMP;
2626 sec_key->key_id = sta_key_conf->keyidx;
2627 sec_key->key_len = 16;
2628 memcpy(sec_key->key, sta_key_conf->key, 16);
2630 sec_key = &sec->key[1];
2631 sec_key->cipher_id = MCU_CIPHER_BIP_CMAC_128;
2632 sec_key->cipher_len = sizeof(*sec_key);
2633 sec_key->key_len = 16;
2634 memcpy(sec_key->key, key->key, 16);
2637 sec_key->cipher_id = cipher;
2638 sec_key->key_id = key->keyidx;
2639 sec_key->key_len = key->keylen;
2640 memcpy(sec_key->key, key->key, key->keylen);
2642 if (cipher == MCU_CIPHER_TKIP) {
2643 /* Rx/Tx MIC keys are swapped */
2644 memcpy(sec_key->key + 16, key->key + 24, 8);
2645 memcpy(sec_key->key + 24, key->key + 16, 8);
2648 /* store key_conf for BIP batch update */
2649 if (cipher == MCU_CIPHER_AES_CCMP) {
2650 memcpy(sta_key_conf->key, key->key, key->keylen);
2651 sta_key_conf->keyidx = key->keyidx;
2654 len -= sizeof(*sec_key);
2658 len -= sizeof(sec->key);
2661 sec->len = cpu_to_le16(len);
2666 int mt76_connac_mcu_add_key(struct mt76_dev *dev, struct ieee80211_vif *vif,
2667 struct mt76_connac_sta_key_conf *sta_key_conf,
2668 struct ieee80211_key_conf *key, int mcu_cmd,
2669 struct mt76_wcid *wcid, enum set_key_cmd cmd)
2671 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2672 struct sk_buff *skb;
2675 skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
2677 return PTR_ERR(skb);
2679 ret = mt76_connac_mcu_sta_key_tlv(sta_key_conf, skb, key, cmd);
2683 ret = mt76_connac_mcu_sta_wed_update(dev, skb);
2687 return mt76_mcu_skb_send_msg(dev, skb, mcu_cmd, true);
2689 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_key);
2691 /* SIFS 20us + 512 byte beacon transmitted by 1Mbps (3906us) */
2692 #define BCN_TX_ESTIMATE_TIME (4096 + 20)
2693 void mt76_connac_mcu_bss_ext_tlv(struct sk_buff *skb, struct mt76_vif *mvif)
2695 struct bss_info_ext_bss *ext;
2696 int ext_bss_idx, tsf_offset;
2699 ext_bss_idx = mvif->omac_idx - EXT_BSSID_START;
2700 if (ext_bss_idx < 0)
2703 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_EXT_BSS, sizeof(*ext));
2705 ext = (struct bss_info_ext_bss *)tlv;
2706 tsf_offset = ext_bss_idx * BCN_TX_ESTIMATE_TIME;
2707 ext->mbss_tsf_offset = cpu_to_le32(tsf_offset);
2709 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_ext_tlv);
2711 int mt76_connac_mcu_bss_basic_tlv(struct sk_buff *skb,
2712 struct ieee80211_vif *vif,
2713 struct ieee80211_sta *sta,
2714 struct mt76_phy *phy, u16 wlan_idx,
2717 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2718 u32 type = vif->p2p ? NETWORK_P2P : NETWORK_INFRA;
2719 struct bss_info_basic *bss;
2722 tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_BASIC, sizeof(*bss));
2723 bss = (struct bss_info_basic *)tlv;
2725 switch (vif->type) {
2726 case NL80211_IFTYPE_MESH_POINT:
2727 case NL80211_IFTYPE_MONITOR:
2729 case NL80211_IFTYPE_AP:
2730 if (ieee80211_hw_check(phy->hw, SUPPORTS_MULTI_BSSID)) {
2731 u8 bssid_id = vif->bss_conf.bssid_indicator;
2732 struct wiphy *wiphy = phy->hw->wiphy;
2734 if (bssid_id > ilog2(wiphy->mbssid_max_interfaces))
2737 bss->non_tx_bssid = vif->bss_conf.bssid_index;
2738 bss->max_bssid = bssid_id;
2741 case NL80211_IFTYPE_STATION:
2745 sta = ieee80211_find_sta(vif,
2746 vif->bss_conf.bssid);
2747 /* TODO: enable BSS_INFO_UAPSD & BSS_INFO_PM */
2749 struct mt76_wcid *wcid;
2751 wcid = (struct mt76_wcid *)sta->drv_priv;
2752 wlan_idx = wcid->idx;
2757 case NL80211_IFTYPE_ADHOC:
2758 type = NETWORK_IBSS;
2765 bss->network_type = cpu_to_le32(type);
2766 bss->bmc_wcid_lo = to_wcid_lo(wlan_idx);
2767 bss->bmc_wcid_hi = to_wcid_hi(wlan_idx);
2768 bss->wmm_idx = mvif->wmm_idx;
2769 bss->active = enable;
2770 bss->cipher = mvif->cipher;
2772 if (vif->type != NL80211_IFTYPE_MONITOR) {
2773 struct cfg80211_chan_def *chandef = &phy->chandef;
2775 memcpy(bss->bssid, vif->bss_conf.bssid, ETH_ALEN);
2776 bss->bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int);
2777 bss->dtim_period = vif->bss_conf.dtim_period;
2778 bss->phy_mode = mt76_connac_get_phy_mode(phy, vif,
2779 chandef->chan->band, NULL);
2781 memcpy(bss->bssid, phy->macaddr, ETH_ALEN);
2786 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_basic_tlv);
2788 #define ENTER_PM_STATE 1
2789 #define EXIT_PM_STATE 2
2790 int mt76_connac_mcu_set_pm(struct mt76_dev *dev, int band, int enter)
2798 __le16 bcn_interval;
2811 .pm_state = enter ? ENTER_PM_STATE : EXIT_PM_STATE,
2815 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(PM_STATE_CTRL), &req,
2818 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_pm);
2820 int mt76_connac_mcu_restart(struct mt76_dev *dev)
2829 return mt76_mcu_send_msg(dev, MCU_CMD(NIC_POWER_CTRL), &req,
2830 sizeof(req), false);
2832 EXPORT_SYMBOL_GPL(mt76_connac_mcu_restart);
2834 int mt76_connac_mcu_rdd_cmd(struct mt76_dev *dev, int cmd, u8 index,
2846 .rdd_rx_sel = rx_sel,
2850 return mt76_mcu_send_msg(dev, MCU_EXT_CMD(SET_RDD_CTRL), &req,
2853 EXPORT_SYMBOL_GPL(mt76_connac_mcu_rdd_cmd);
2856 mt76_connac_mcu_send_ram_firmware(struct mt76_dev *dev,
2857 const struct mt76_connac2_fw_trailer *hdr,
2858 const u8 *data, bool is_wa)
2860 int i, offset = 0, max_len = mt76_is_sdio(dev) ? 2048 : 4096;
2861 u32 override = 0, option = 0;
2863 for (i = 0; i < hdr->n_region; i++) {
2864 const struct mt76_connac2_fw_region *region;
2865 u32 len, addr, mode;
2868 region = (const void *)((const u8 *)hdr -
2869 (hdr->n_region - i) * sizeof(*region));
2870 mode = mt76_connac_mcu_gen_dl_mode(dev, region->feature_set,
2872 len = le32_to_cpu(region->len);
2873 addr = le32_to_cpu(region->addr);
2875 if (region->feature_set & FW_FEATURE_NON_DL)
2878 if (region->feature_set & FW_FEATURE_OVERRIDE_ADDR)
2881 err = mt76_connac_mcu_init_download(dev, addr, len, mode);
2883 dev_err(dev->dev, "Download request failed\n");
2887 err = __mt76_mcu_send_firmware(dev, MCU_CMD(FW_SCATTER),
2888 data + offset, len, max_len);
2890 dev_err(dev->dev, "Failed to send firmware.\n");
2899 option |= FW_START_OVERRIDE;
2901 option |= FW_START_WORKING_PDA_CR4;
2903 return mt76_connac_mcu_start_firmware(dev, override, option);
2906 int mt76_connac2_load_ram(struct mt76_dev *dev, const char *fw_wm,
2909 const struct mt76_connac2_fw_trailer *hdr;
2910 const struct firmware *fw;
2913 ret = request_firmware(&fw, fw_wm, dev->dev);
2917 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
2918 dev_err(dev->dev, "Invalid firmware\n");
2923 hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
2924 dev_info(dev->dev, "WM Firmware Version: %.10s, Build Time: %.15s\n",
2925 hdr->fw_ver, hdr->build_date);
2927 ret = mt76_connac_mcu_send_ram_firmware(dev, hdr, fw->data, false);
2929 dev_err(dev->dev, "Failed to start WM firmware\n");
2933 snprintf(dev->hw->wiphy->fw_version,
2934 sizeof(dev->hw->wiphy->fw_version),
2935 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
2937 release_firmware(fw);
2942 ret = request_firmware(&fw, fw_wa, dev->dev);
2946 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
2947 dev_err(dev->dev, "Invalid firmware\n");
2952 hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
2953 dev_info(dev->dev, "WA Firmware Version: %.10s, Build Time: %.15s\n",
2954 hdr->fw_ver, hdr->build_date);
2956 ret = mt76_connac_mcu_send_ram_firmware(dev, hdr, fw->data, true);
2958 dev_err(dev->dev, "Failed to start WA firmware\n");
2962 snprintf(dev->hw->wiphy->fw_version,
2963 sizeof(dev->hw->wiphy->fw_version),
2964 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
2967 release_firmware(fw);
2971 EXPORT_SYMBOL_GPL(mt76_connac2_load_ram);
2973 static u32 mt76_connac2_get_data_mode(struct mt76_dev *dev, u32 info)
2975 u32 mode = DL_MODE_NEED_RSP;
2977 if ((!is_mt7921(dev) && !is_mt7925(dev)) || info == PATCH_SEC_NOT_SUPPORT)
2980 switch (FIELD_GET(PATCH_SEC_ENC_TYPE_MASK, info)) {
2981 case PATCH_SEC_ENC_TYPE_PLAIN:
2983 case PATCH_SEC_ENC_TYPE_AES:
2984 mode |= DL_MODE_ENCRYPT;
2985 mode |= FIELD_PREP(DL_MODE_KEY_IDX,
2986 (info & PATCH_SEC_ENC_AES_KEY_MASK)) & DL_MODE_KEY_IDX;
2987 mode |= DL_MODE_RESET_SEC_IV;
2989 case PATCH_SEC_ENC_TYPE_SCRAMBLE:
2990 mode |= DL_MODE_ENCRYPT;
2991 mode |= DL_CONFIG_ENCRY_MODE_SEL;
2992 mode |= DL_MODE_RESET_SEC_IV;
2995 dev_err(dev->dev, "Encryption type not support!\n");
3001 int mt76_connac2_load_patch(struct mt76_dev *dev, const char *fw_name)
3003 int i, ret, sem, max_len = mt76_is_sdio(dev) ? 2048 : 4096;
3004 const struct mt76_connac2_patch_hdr *hdr;
3005 const struct firmware *fw = NULL;
3007 sem = mt76_connac_mcu_patch_sem_ctrl(dev, true);
3011 case PATCH_NOT_DL_SEM_SUCCESS:
3014 dev_err(dev->dev, "Failed to get patch semaphore\n");
3018 ret = request_firmware(&fw, fw_name, dev->dev);
3022 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
3023 dev_err(dev->dev, "Invalid firmware\n");
3028 hdr = (const void *)fw->data;
3029 dev_info(dev->dev, "HW/SW Version: 0x%x, Build Time: %.16s\n",
3030 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date);
3032 for (i = 0; i < be32_to_cpu(hdr->desc.n_region); i++) {
3033 struct mt76_connac2_patch_sec *sec;
3034 u32 len, addr, mode;
3038 sec = (void *)(fw->data + sizeof(*hdr) + i * sizeof(*sec));
3039 if ((be32_to_cpu(sec->type) & PATCH_SEC_TYPE_MASK) !=
3040 PATCH_SEC_TYPE_INFO) {
3045 addr = be32_to_cpu(sec->info.addr);
3046 len = be32_to_cpu(sec->info.len);
3047 dl = fw->data + be32_to_cpu(sec->offs);
3048 sec_info = be32_to_cpu(sec->info.sec_key_idx);
3049 mode = mt76_connac2_get_data_mode(dev, sec_info);
3051 ret = mt76_connac_mcu_init_download(dev, addr, len, mode);
3053 dev_err(dev->dev, "Download request failed\n");
3057 ret = __mt76_mcu_send_firmware(dev, MCU_CMD(FW_SCATTER),
3060 dev_err(dev->dev, "Failed to send patch\n");
3065 ret = mt76_connac_mcu_start_patch(dev);
3067 dev_err(dev->dev, "Failed to start patch\n");
3070 sem = mt76_connac_mcu_patch_sem_ctrl(dev, false);
3072 case PATCH_REL_SEM_SUCCESS:
3076 dev_err(dev->dev, "Failed to release patch semaphore\n");
3080 release_firmware(fw);
3084 EXPORT_SYMBOL_GPL(mt76_connac2_load_patch);
3086 int mt76_connac2_mcu_fill_message(struct mt76_dev *dev, struct sk_buff *skb,
3087 int cmd, int *wait_seq)
3089 int txd_len, mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
3090 struct mt76_connac2_mcu_uni_txd *uni_txd;
3091 struct mt76_connac2_mcu_txd *mcu_txd;
3096 /* TODO: make dynamic based on msg type */
3097 dev->mcu.timeout = 20 * HZ;
3099 seq = ++dev->mcu.msg_seq & 0xf;
3101 seq = ++dev->mcu.msg_seq & 0xf;
3103 if (cmd == MCU_CMD(FW_SCATTER))
3106 txd_len = cmd & __MCU_CMD_FIELD_UNI ? sizeof(*uni_txd) : sizeof(*mcu_txd);
3107 txd = (__le32 *)skb_push(skb, txd_len);
3109 val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) |
3110 FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CMD) |
3111 FIELD_PREP(MT_TXD0_Q_IDX, MT_TX_MCU_PORT_RX_Q0);
3112 txd[0] = cpu_to_le32(val);
3114 val = MT_TXD1_LONG_FORMAT |
3115 FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD);
3116 txd[1] = cpu_to_le32(val);
3118 if (cmd & __MCU_CMD_FIELD_UNI) {
3119 uni_txd = (struct mt76_connac2_mcu_uni_txd *)txd;
3120 uni_txd->len = cpu_to_le16(skb->len - sizeof(uni_txd->txd));
3121 uni_txd->option = MCU_CMD_UNI_EXT_ACK;
3122 uni_txd->cid = cpu_to_le16(mcu_cmd);
3123 uni_txd->s2d_index = MCU_S2D_H2N;
3124 uni_txd->pkt_type = MCU_PKT_ID;
3130 mcu_txd = (struct mt76_connac2_mcu_txd *)txd;
3131 mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd));
3132 mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU,
3133 MT_TX_MCU_PORT_RX_Q0));
3134 mcu_txd->pkt_type = MCU_PKT_ID;
3136 mcu_txd->cid = mcu_cmd;
3137 mcu_txd->ext_cid = FIELD_GET(__MCU_CMD_FIELD_EXT_ID, cmd);
3139 if (mcu_txd->ext_cid || (cmd & __MCU_CMD_FIELD_CE)) {
3140 if (cmd & __MCU_CMD_FIELD_QUERY)
3141 mcu_txd->set_query = MCU_Q_QUERY;
3143 mcu_txd->set_query = MCU_Q_SET;
3144 mcu_txd->ext_cid_ack = !!mcu_txd->ext_cid;
3146 mcu_txd->set_query = MCU_Q_NA;
3149 if (cmd & __MCU_CMD_FIELD_WA)
3150 mcu_txd->s2d_index = MCU_S2D_H2C;
3152 mcu_txd->s2d_index = MCU_S2D_H2N;
3160 EXPORT_SYMBOL_GPL(mt76_connac2_mcu_fill_message);
3162 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
3163 MODULE_LICENSE("Dual BSD/GPL");